]> git.ipfire.org Git - thirdparty/gcc.git/blob - gcc/cp/parser.c
cp-tree.h (build_scoped_method_call): Remove.
[thirdparty/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2 Copyright (C) 2000, 2001, 2002, 2003 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 cpp_ttype type;
72 /* The value associated with this token, if any. */
73 tree value;
74 /* If this token is a keyword, this value indicates which keyword.
75 Otherwise, this value is RID_MAX. */
76 enum rid keyword;
77 /* The location at which this token was found. */
78 location_t location;
79 } cp_token;
80
81 /* The number of tokens in a single token block. */
82
83 #define CP_TOKEN_BLOCK_NUM_TOKENS 32
84
85 /* A group of tokens. These groups are chained together to store
86 large numbers of tokens. (For example, a token block is created
87 when the body of an inline member function is first encountered;
88 the tokens are processed later after the class definition is
89 complete.)
90
91 This somewhat ungainly data structure (as opposed to, say, a
92 variable-length array), is used due to constraints imposed by the
93 current garbage-collection methodology. If it is made more
94 flexible, we could perhaps simplify the data structures involved. */
95
96 typedef struct cp_token_block GTY (())
97 {
98 /* The tokens. */
99 cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
100 /* The number of tokens in this block. */
101 size_t num_tokens;
102 /* The next token block in the chain. */
103 struct cp_token_block *next;
104 /* The previous block in the chain. */
105 struct cp_token_block *prev;
106 } cp_token_block;
107
108 typedef struct cp_token_cache GTY (())
109 {
110 /* The first block in the cache. NULL if there are no tokens in the
111 cache. */
112 cp_token_block *first;
113 /* The last block in the cache. NULL If there are no tokens in the
114 cache. */
115 cp_token_block *last;
116 } cp_token_cache;
117
118 /* Prototypes. */
119
120 static cp_token_cache *cp_token_cache_new
121 (void);
122 static void cp_token_cache_push_token
123 (cp_token_cache *, cp_token *);
124
125 /* Create a new cp_token_cache. */
126
127 static cp_token_cache *
128 cp_token_cache_new ()
129 {
130 return (cp_token_cache *) ggc_alloc_cleared (sizeof (cp_token_cache));
131 }
132
133 /* Add *TOKEN to *CACHE. */
134
135 static void
136 cp_token_cache_push_token (cp_token_cache *cache,
137 cp_token *token)
138 {
139 cp_token_block *b = cache->last;
140
141 /* See if we need to allocate a new token block. */
142 if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
143 {
144 b = ((cp_token_block *) ggc_alloc_cleared (sizeof (cp_token_block)));
145 b->prev = cache->last;
146 if (cache->last)
147 {
148 cache->last->next = b;
149 cache->last = b;
150 }
151 else
152 cache->first = cache->last = b;
153 }
154 /* Add this token to the current token block. */
155 b->tokens[b->num_tokens++] = *token;
156 }
157
158 /* The cp_lexer structure represents the C++ lexer. It is responsible
159 for managing the token stream from the preprocessor and supplying
160 it to the parser. */
161
162 typedef struct cp_lexer GTY (())
163 {
164 /* The memory allocated for the buffer. Never NULL. */
165 cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
166 /* A pointer just past the end of the memory allocated for the buffer. */
167 cp_token * GTY ((skip (""))) buffer_end;
168 /* The first valid token in the buffer, or NULL if none. */
169 cp_token * GTY ((skip (""))) first_token;
170 /* The next available token. If NEXT_TOKEN is NULL, then there are
171 no more available tokens. */
172 cp_token * GTY ((skip (""))) next_token;
173 /* A pointer just past the last available token. If FIRST_TOKEN is
174 NULL, however, there are no available tokens, and then this
175 location is simply the place in which the next token read will be
176 placed. If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
177 When the LAST_TOKEN == BUFFER, then the last token is at the
178 highest memory address in the BUFFER. */
179 cp_token * GTY ((skip (""))) last_token;
180
181 /* A stack indicating positions at which cp_lexer_save_tokens was
182 called. The top entry is the most recent position at which we
183 began saving tokens. The entries are differences in token
184 position between FIRST_TOKEN and the first saved token.
185
186 If the stack is non-empty, we are saving tokens. When a token is
187 consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
188 pointer will not. The token stream will be preserved so that it
189 can be reexamined later.
190
191 If the stack is empty, then we are not saving tokens. Whenever a
192 token is consumed, the FIRST_TOKEN pointer will be moved, and the
193 consumed token will be gone forever. */
194 varray_type saved_tokens;
195
196 /* The STRING_CST tokens encountered while processing the current
197 string literal. */
198 varray_type string_tokens;
199
200 /* True if we should obtain more tokens from the preprocessor; false
201 if we are processing a saved token cache. */
202 bool main_lexer_p;
203
204 /* True if we should output debugging information. */
205 bool debugging_p;
206
207 /* The next lexer in a linked list of lexers. */
208 struct cp_lexer *next;
209 } cp_lexer;
210
211 /* Prototypes. */
212
213 static cp_lexer *cp_lexer_new_main
214 (void);
215 static cp_lexer *cp_lexer_new_from_tokens
216 (struct cp_token_cache *);
217 static int cp_lexer_saving_tokens
218 (const cp_lexer *);
219 static cp_token *cp_lexer_next_token
220 (cp_lexer *, cp_token *);
221 static ptrdiff_t cp_lexer_token_difference
222 (cp_lexer *, cp_token *, cp_token *);
223 static cp_token *cp_lexer_read_token
224 (cp_lexer *);
225 static void cp_lexer_maybe_grow_buffer
226 (cp_lexer *);
227 static void cp_lexer_get_preprocessor_token
228 (cp_lexer *, cp_token *);
229 static cp_token *cp_lexer_peek_token
230 (cp_lexer *);
231 static cp_token *cp_lexer_peek_nth_token
232 (cp_lexer *, size_t);
233 static inline bool cp_lexer_next_token_is
234 (cp_lexer *, enum cpp_ttype);
235 static bool cp_lexer_next_token_is_not
236 (cp_lexer *, enum cpp_ttype);
237 static bool cp_lexer_next_token_is_keyword
238 (cp_lexer *, enum rid);
239 static cp_token *cp_lexer_consume_token
240 (cp_lexer *);
241 static void cp_lexer_purge_token
242 (cp_lexer *);
243 static void cp_lexer_purge_tokens_after
244 (cp_lexer *, cp_token *);
245 static void cp_lexer_save_tokens
246 (cp_lexer *);
247 static void cp_lexer_commit_tokens
248 (cp_lexer *);
249 static void cp_lexer_rollback_tokens
250 (cp_lexer *);
251 static inline void cp_lexer_set_source_position_from_token
252 (cp_lexer *, const cp_token *);
253 static void cp_lexer_print_token
254 (FILE *, cp_token *);
255 static inline bool cp_lexer_debugging_p
256 (cp_lexer *);
257 static void cp_lexer_start_debugging
258 (cp_lexer *) ATTRIBUTE_UNUSED;
259 static void cp_lexer_stop_debugging
260 (cp_lexer *) ATTRIBUTE_UNUSED;
261
262 /* Manifest constants. */
263
264 #define CP_TOKEN_BUFFER_SIZE 5
265 #define CP_SAVED_TOKENS_SIZE 5
266
267 /* A token type for keywords, as opposed to ordinary identifiers. */
268 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
269
270 /* A token type for template-ids. If a template-id is processed while
271 parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
272 the value of the CPP_TEMPLATE_ID is whatever was returned by
273 cp_parser_template_id. */
274 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
275
276 /* A token type for nested-name-specifiers. If a
277 nested-name-specifier is processed while parsing tentatively, it is
278 replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
279 CPP_NESTED_NAME_SPECIFIER is whatever was returned by
280 cp_parser_nested_name_specifier_opt. */
281 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
282
283 /* A token type for tokens that are not tokens at all; these are used
284 to mark the end of a token block. */
285 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
286
287 /* Variables. */
288
289 /* The stream to which debugging output should be written. */
290 static FILE *cp_lexer_debug_stream;
291
292 /* Create a new main C++ lexer, the lexer that gets tokens from the
293 preprocessor. */
294
295 static cp_lexer *
296 cp_lexer_new_main (void)
297 {
298 cp_lexer *lexer;
299 cp_token first_token;
300
301 /* It's possible that lexing the first token will load a PCH file,
302 which is a GC collection point. So we have to grab the first
303 token before allocating any memory. */
304 cp_lexer_get_preprocessor_token (NULL, &first_token);
305 cpp_get_callbacks (parse_in)->valid_pch = NULL;
306
307 /* Allocate the memory. */
308 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
309
310 /* Create the circular buffer. */
311 lexer->buffer = ((cp_token *)
312 ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token)));
313 lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
314
315 /* There is one token in the buffer. */
316 lexer->last_token = lexer->buffer + 1;
317 lexer->first_token = lexer->buffer;
318 lexer->next_token = lexer->buffer;
319 memcpy (lexer->buffer, &first_token, sizeof (cp_token));
320
321 /* This lexer obtains more tokens by calling c_lex. */
322 lexer->main_lexer_p = true;
323
324 /* Create the SAVED_TOKENS stack. */
325 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
326
327 /* Create the STRINGS array. */
328 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
329
330 /* Assume we are not debugging. */
331 lexer->debugging_p = false;
332
333 return lexer;
334 }
335
336 /* Create a new lexer whose token stream is primed with the TOKENS.
337 When these tokens are exhausted, no new tokens will be read. */
338
339 static cp_lexer *
340 cp_lexer_new_from_tokens (cp_token_cache *tokens)
341 {
342 cp_lexer *lexer;
343 cp_token *token;
344 cp_token_block *block;
345 ptrdiff_t num_tokens;
346
347 /* Allocate the memory. */
348 lexer = (cp_lexer *) ggc_alloc_cleared (sizeof (cp_lexer));
349
350 /* Create a new buffer, appropriately sized. */
351 num_tokens = 0;
352 for (block = tokens->first; block != NULL; block = block->next)
353 num_tokens += block->num_tokens;
354 lexer->buffer = ((cp_token *) ggc_alloc (num_tokens * sizeof (cp_token)));
355 lexer->buffer_end = lexer->buffer + num_tokens;
356
357 /* Install the tokens. */
358 token = lexer->buffer;
359 for (block = tokens->first; block != NULL; block = block->next)
360 {
361 memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
362 token += block->num_tokens;
363 }
364
365 /* The FIRST_TOKEN is the beginning of the buffer. */
366 lexer->first_token = lexer->buffer;
367 /* The next available token is also at the beginning of the buffer. */
368 lexer->next_token = lexer->buffer;
369 /* The buffer is full. */
370 lexer->last_token = lexer->first_token;
371
372 /* This lexer doesn't obtain more tokens. */
373 lexer->main_lexer_p = false;
374
375 /* Create the SAVED_TOKENS stack. */
376 VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
377
378 /* Create the STRINGS array. */
379 VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
380
381 /* Assume we are not debugging. */
382 lexer->debugging_p = false;
383
384 return lexer;
385 }
386
387 /* Returns nonzero if debugging information should be output. */
388
389 static inline bool
390 cp_lexer_debugging_p (cp_lexer *lexer)
391 {
392 return lexer->debugging_p;
393 }
394
395 /* Set the current source position from the information stored in
396 TOKEN. */
397
398 static inline void
399 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
400 const cp_token *token)
401 {
402 /* Ideally, the source position information would not be a global
403 variable, but it is. */
404
405 /* Update the line number. */
406 if (token->type != CPP_EOF)
407 input_location = token->location;
408 }
409
410 /* TOKEN points into the circular token buffer. Return a pointer to
411 the next token in the buffer. */
412
413 static inline cp_token *
414 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
415 {
416 token++;
417 if (token == lexer->buffer_end)
418 token = lexer->buffer;
419 return token;
420 }
421
422 /* nonzero if we are presently saving tokens. */
423
424 static int
425 cp_lexer_saving_tokens (const cp_lexer* lexer)
426 {
427 return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
428 }
429
430 /* Return a pointer to the token that is N tokens beyond TOKEN in the
431 buffer. */
432
433 static cp_token *
434 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
435 {
436 token += n;
437 if (token >= lexer->buffer_end)
438 token = lexer->buffer + (token - lexer->buffer_end);
439 return token;
440 }
441
442 /* Returns the number of times that START would have to be incremented
443 to reach FINISH. If START and FINISH are the same, returns zero. */
444
445 static ptrdiff_t
446 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
447 {
448 if (finish >= start)
449 return finish - start;
450 else
451 return ((lexer->buffer_end - lexer->buffer)
452 - (start - finish));
453 }
454
455 /* Obtain another token from the C preprocessor and add it to the
456 token buffer. Returns the newly read token. */
457
458 static cp_token *
459 cp_lexer_read_token (cp_lexer* lexer)
460 {
461 cp_token *token;
462
463 /* Make sure there is room in the buffer. */
464 cp_lexer_maybe_grow_buffer (lexer);
465
466 /* If there weren't any tokens, then this one will be the first. */
467 if (!lexer->first_token)
468 lexer->first_token = lexer->last_token;
469 /* Similarly, if there were no available tokens, there is one now. */
470 if (!lexer->next_token)
471 lexer->next_token = lexer->last_token;
472
473 /* Figure out where we're going to store the new token. */
474 token = lexer->last_token;
475
476 /* Get a new token from the preprocessor. */
477 cp_lexer_get_preprocessor_token (lexer, token);
478
479 /* Increment LAST_TOKEN. */
480 lexer->last_token = cp_lexer_next_token (lexer, token);
481
482 /* Strings should have type `const char []'. Right now, we will
483 have an ARRAY_TYPE that is constant rather than an array of
484 constant elements.
485 FIXME: Make fix_string_type get this right in the first place. */
486 if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
487 && flag_const_strings)
488 {
489 tree type;
490
491 /* Get the current type. It will be an ARRAY_TYPE. */
492 type = TREE_TYPE (token->value);
493 /* Use build_cplus_array_type to rebuild the array, thereby
494 getting the right type. */
495 type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
496 /* Reset the type of the token. */
497 TREE_TYPE (token->value) = type;
498 }
499
500 return token;
501 }
502
503 /* If the circular buffer is full, make it bigger. */
504
505 static void
506 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
507 {
508 /* If the buffer is full, enlarge it. */
509 if (lexer->last_token == lexer->first_token)
510 {
511 cp_token *new_buffer;
512 cp_token *old_buffer;
513 cp_token *new_first_token;
514 ptrdiff_t buffer_length;
515 size_t num_tokens_to_copy;
516
517 /* Remember the current buffer pointer. It will become invalid,
518 but we will need to do pointer arithmetic involving this
519 value. */
520 old_buffer = lexer->buffer;
521 /* Compute the current buffer size. */
522 buffer_length = lexer->buffer_end - lexer->buffer;
523 /* Allocate a buffer twice as big. */
524 new_buffer = ((cp_token *)
525 ggc_realloc (lexer->buffer,
526 2 * buffer_length * sizeof (cp_token)));
527
528 /* Because the buffer is circular, logically consecutive tokens
529 are not necessarily placed consecutively in memory.
530 Therefore, we must keep move the tokens that were before
531 FIRST_TOKEN to the second half of the newly allocated
532 buffer. */
533 num_tokens_to_copy = (lexer->first_token - old_buffer);
534 memcpy (new_buffer + buffer_length,
535 new_buffer,
536 num_tokens_to_copy * sizeof (cp_token));
537 /* Clear the rest of the buffer. We never look at this storage,
538 but the garbage collector may. */
539 memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
540 (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
541
542 /* Now recompute all of the buffer pointers. */
543 new_first_token
544 = new_buffer + (lexer->first_token - old_buffer);
545 if (lexer->next_token != NULL)
546 {
547 ptrdiff_t next_token_delta;
548
549 if (lexer->next_token > lexer->first_token)
550 next_token_delta = lexer->next_token - lexer->first_token;
551 else
552 next_token_delta =
553 buffer_length - (lexer->first_token - lexer->next_token);
554 lexer->next_token = new_first_token + next_token_delta;
555 }
556 lexer->last_token = new_first_token + buffer_length;
557 lexer->buffer = new_buffer;
558 lexer->buffer_end = new_buffer + buffer_length * 2;
559 lexer->first_token = new_first_token;
560 }
561 }
562
563 /* Store the next token from the preprocessor in *TOKEN. */
564
565 static void
566 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
567 cp_token *token)
568 {
569 bool done;
570
571 /* If this not the main lexer, return a terminating CPP_EOF token. */
572 if (lexer != NULL && !lexer->main_lexer_p)
573 {
574 token->type = CPP_EOF;
575 token->location.line = 0;
576 token->location.file = NULL;
577 token->value = NULL_TREE;
578 token->keyword = RID_MAX;
579
580 return;
581 }
582
583 done = false;
584 /* Keep going until we get a token we like. */
585 while (!done)
586 {
587 /* Get a new token from the preprocessor. */
588 token->type = c_lex (&token->value);
589 /* Issue messages about tokens we cannot process. */
590 switch (token->type)
591 {
592 case CPP_ATSIGN:
593 case CPP_HASH:
594 case CPP_PASTE:
595 error ("invalid token");
596 break;
597
598 default:
599 /* This is a good token, so we exit the loop. */
600 done = true;
601 break;
602 }
603 }
604 /* Now we've got our token. */
605 token->location = input_location;
606
607 /* Check to see if this token is a keyword. */
608 if (token->type == CPP_NAME
609 && C_IS_RESERVED_WORD (token->value))
610 {
611 /* Mark this token as a keyword. */
612 token->type = CPP_KEYWORD;
613 /* Record which keyword. */
614 token->keyword = C_RID_CODE (token->value);
615 /* Update the value. Some keywords are mapped to particular
616 entities, rather than simply having the value of the
617 corresponding IDENTIFIER_NODE. For example, `__const' is
618 mapped to `const'. */
619 token->value = ridpointers[token->keyword];
620 }
621 else
622 token->keyword = RID_MAX;
623 }
624
625 /* Return a pointer to the next token in the token stream, but do not
626 consume it. */
627
628 static cp_token *
629 cp_lexer_peek_token (cp_lexer* lexer)
630 {
631 cp_token *token;
632
633 /* If there are no tokens, read one now. */
634 if (!lexer->next_token)
635 cp_lexer_read_token (lexer);
636
637 /* Provide debugging output. */
638 if (cp_lexer_debugging_p (lexer))
639 {
640 fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
641 cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
642 fprintf (cp_lexer_debug_stream, "\n");
643 }
644
645 token = lexer->next_token;
646 cp_lexer_set_source_position_from_token (lexer, token);
647 return token;
648 }
649
650 /* Return true if the next token has the indicated TYPE. */
651
652 static bool
653 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
654 {
655 cp_token *token;
656
657 /* Peek at the next token. */
658 token = cp_lexer_peek_token (lexer);
659 /* Check to see if it has the indicated TYPE. */
660 return token->type == type;
661 }
662
663 /* Return true if the next token does not have the indicated TYPE. */
664
665 static bool
666 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
667 {
668 return !cp_lexer_next_token_is (lexer, type);
669 }
670
671 /* Return true if the next token is the indicated KEYWORD. */
672
673 static bool
674 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
675 {
676 cp_token *token;
677
678 /* Peek at the next token. */
679 token = cp_lexer_peek_token (lexer);
680 /* Check to see if it is the indicated keyword. */
681 return token->keyword == keyword;
682 }
683
684 /* Return a pointer to the Nth token in the token stream. If N is 1,
685 then this is precisely equivalent to cp_lexer_peek_token. */
686
687 static cp_token *
688 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
689 {
690 cp_token *token;
691
692 /* N is 1-based, not zero-based. */
693 my_friendly_assert (n > 0, 20000224);
694
695 /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary. */
696 token = lexer->next_token;
697 /* If there are no tokens in the buffer, get one now. */
698 if (!token)
699 {
700 cp_lexer_read_token (lexer);
701 token = lexer->next_token;
702 }
703
704 /* Now, read tokens until we have enough. */
705 while (--n > 0)
706 {
707 /* Advance to the next token. */
708 token = cp_lexer_next_token (lexer, token);
709 /* If that's all the tokens we have, read a new one. */
710 if (token == lexer->last_token)
711 token = cp_lexer_read_token (lexer);
712 }
713
714 return token;
715 }
716
717 /* Consume the next token. The pointer returned is valid only until
718 another token is read. Callers should preserve copy the token
719 explicitly if they will need its value for a longer period of
720 time. */
721
722 static cp_token *
723 cp_lexer_consume_token (cp_lexer* lexer)
724 {
725 cp_token *token;
726
727 /* If there are no tokens, read one now. */
728 if (!lexer->next_token)
729 cp_lexer_read_token (lexer);
730
731 /* Remember the token we'll be returning. */
732 token = lexer->next_token;
733
734 /* Increment NEXT_TOKEN. */
735 lexer->next_token = cp_lexer_next_token (lexer,
736 lexer->next_token);
737 /* Check to see if we're all out of tokens. */
738 if (lexer->next_token == lexer->last_token)
739 lexer->next_token = NULL;
740
741 /* If we're not saving tokens, then move FIRST_TOKEN too. */
742 if (!cp_lexer_saving_tokens (lexer))
743 {
744 /* If there are no tokens available, set FIRST_TOKEN to NULL. */
745 if (!lexer->next_token)
746 lexer->first_token = NULL;
747 else
748 lexer->first_token = lexer->next_token;
749 }
750
751 /* Provide debugging output. */
752 if (cp_lexer_debugging_p (lexer))
753 {
754 fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
755 cp_lexer_print_token (cp_lexer_debug_stream, token);
756 fprintf (cp_lexer_debug_stream, "\n");
757 }
758
759 return token;
760 }
761
762 /* Permanently remove the next token from the token stream. There
763 must be a valid next token already; this token never reads
764 additional tokens from the preprocessor. */
765
766 static void
767 cp_lexer_purge_token (cp_lexer *lexer)
768 {
769 cp_token *token;
770 cp_token *next_token;
771
772 token = lexer->next_token;
773 while (true)
774 {
775 next_token = cp_lexer_next_token (lexer, token);
776 if (next_token == lexer->last_token)
777 break;
778 *token = *next_token;
779 token = next_token;
780 }
781
782 lexer->last_token = token;
783 /* The token purged may have been the only token remaining; if so,
784 clear NEXT_TOKEN. */
785 if (lexer->next_token == token)
786 lexer->next_token = NULL;
787 }
788
789 /* Permanently remove all tokens after TOKEN, up to, but not
790 including, the token that will be returned next by
791 cp_lexer_peek_token. */
792
793 static void
794 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
795 {
796 cp_token *peek;
797 cp_token *t1;
798 cp_token *t2;
799
800 if (lexer->next_token)
801 {
802 /* Copy the tokens that have not yet been read to the location
803 immediately following TOKEN. */
804 t1 = cp_lexer_next_token (lexer, token);
805 t2 = peek = cp_lexer_peek_token (lexer);
806 /* Move tokens into the vacant area between TOKEN and PEEK. */
807 while (t2 != lexer->last_token)
808 {
809 *t1 = *t2;
810 t1 = cp_lexer_next_token (lexer, t1);
811 t2 = cp_lexer_next_token (lexer, t2);
812 }
813 /* Now, the next available token is right after TOKEN. */
814 lexer->next_token = cp_lexer_next_token (lexer, token);
815 /* And the last token is wherever we ended up. */
816 lexer->last_token = t1;
817 }
818 else
819 {
820 /* There are no tokens in the buffer, so there is nothing to
821 copy. The last token in the buffer is TOKEN itself. */
822 lexer->last_token = cp_lexer_next_token (lexer, token);
823 }
824 }
825
826 /* Begin saving tokens. All tokens consumed after this point will be
827 preserved. */
828
829 static void
830 cp_lexer_save_tokens (cp_lexer* lexer)
831 {
832 /* Provide debugging output. */
833 if (cp_lexer_debugging_p (lexer))
834 fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
835
836 /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
837 restore the tokens if required. */
838 if (!lexer->next_token)
839 cp_lexer_read_token (lexer);
840
841 VARRAY_PUSH_INT (lexer->saved_tokens,
842 cp_lexer_token_difference (lexer,
843 lexer->first_token,
844 lexer->next_token));
845 }
846
847 /* Commit to the portion of the token stream most recently saved. */
848
849 static void
850 cp_lexer_commit_tokens (cp_lexer* lexer)
851 {
852 /* Provide debugging output. */
853 if (cp_lexer_debugging_p (lexer))
854 fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
855
856 VARRAY_POP (lexer->saved_tokens);
857 }
858
859 /* Return all tokens saved since the last call to cp_lexer_save_tokens
860 to the token stream. Stop saving tokens. */
861
862 static void
863 cp_lexer_rollback_tokens (cp_lexer* lexer)
864 {
865 size_t delta;
866
867 /* Provide debugging output. */
868 if (cp_lexer_debugging_p (lexer))
869 fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
870
871 /* Find the token that was the NEXT_TOKEN when we started saving
872 tokens. */
873 delta = VARRAY_TOP_INT(lexer->saved_tokens);
874 /* Make it the next token again now. */
875 lexer->next_token = cp_lexer_advance_token (lexer,
876 lexer->first_token,
877 delta);
878 /* It might be the case that there were no tokens when we started
879 saving tokens, but that there are some tokens now. */
880 if (!lexer->next_token && lexer->first_token)
881 lexer->next_token = lexer->first_token;
882
883 /* Stop saving tokens. */
884 VARRAY_POP (lexer->saved_tokens);
885 }
886
887 /* Print a representation of the TOKEN on the STREAM. */
888
889 static void
890 cp_lexer_print_token (FILE * stream, cp_token* token)
891 {
892 const char *token_type = NULL;
893
894 /* Figure out what kind of token this is. */
895 switch (token->type)
896 {
897 case CPP_EQ:
898 token_type = "EQ";
899 break;
900
901 case CPP_COMMA:
902 token_type = "COMMA";
903 break;
904
905 case CPP_OPEN_PAREN:
906 token_type = "OPEN_PAREN";
907 break;
908
909 case CPP_CLOSE_PAREN:
910 token_type = "CLOSE_PAREN";
911 break;
912
913 case CPP_OPEN_BRACE:
914 token_type = "OPEN_BRACE";
915 break;
916
917 case CPP_CLOSE_BRACE:
918 token_type = "CLOSE_BRACE";
919 break;
920
921 case CPP_SEMICOLON:
922 token_type = "SEMICOLON";
923 break;
924
925 case CPP_NAME:
926 token_type = "NAME";
927 break;
928
929 case CPP_EOF:
930 token_type = "EOF";
931 break;
932
933 case CPP_KEYWORD:
934 token_type = "keyword";
935 break;
936
937 /* This is not a token that we know how to handle yet. */
938 default:
939 break;
940 }
941
942 /* If we have a name for the token, print it out. Otherwise, we
943 simply give the numeric code. */
944 if (token_type)
945 fprintf (stream, "%s", token_type);
946 else
947 fprintf (stream, "%d", token->type);
948 /* And, for an identifier, print the identifier name. */
949 if (token->type == CPP_NAME
950 /* Some keywords have a value that is not an IDENTIFIER_NODE.
951 For example, `struct' is mapped to an INTEGER_CST. */
952 || (token->type == CPP_KEYWORD
953 && TREE_CODE (token->value) == IDENTIFIER_NODE))
954 fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
955 }
956
957 /* Start emitting debugging information. */
958
959 static void
960 cp_lexer_start_debugging (cp_lexer* lexer)
961 {
962 ++lexer->debugging_p;
963 }
964
965 /* Stop emitting debugging information. */
966
967 static void
968 cp_lexer_stop_debugging (cp_lexer* lexer)
969 {
970 --lexer->debugging_p;
971 }
972
973 \f
974 /* The parser. */
975
976 /* Overview
977 --------
978
979 A cp_parser parses the token stream as specified by the C++
980 grammar. Its job is purely parsing, not semantic analysis. For
981 example, the parser breaks the token stream into declarators,
982 expressions, statements, and other similar syntactic constructs.
983 It does not check that the types of the expressions on either side
984 of an assignment-statement are compatible, or that a function is
985 not declared with a parameter of type `void'.
986
987 The parser invokes routines elsewhere in the compiler to perform
988 semantic analysis and to build up the abstract syntax tree for the
989 code processed.
990
991 The parser (and the template instantiation code, which is, in a
992 way, a close relative of parsing) are the only parts of the
993 compiler that should be calling push_scope and pop_scope, or
994 related functions. The parser (and template instantiation code)
995 keeps track of what scope is presently active; everything else
996 should simply honor that. (The code that generates static
997 initializers may also need to set the scope, in order to check
998 access control correctly when emitting the initializers.)
999
1000 Methodology
1001 -----------
1002
1003 The parser is of the standard recursive-descent variety. Upcoming
1004 tokens in the token stream are examined in order to determine which
1005 production to use when parsing a non-terminal. Some C++ constructs
1006 require arbitrary look ahead to disambiguate. For example, it is
1007 impossible, in the general case, to tell whether a statement is an
1008 expression or declaration without scanning the entire statement.
1009 Therefore, the parser is capable of "parsing tentatively." When the
1010 parser is not sure what construct comes next, it enters this mode.
1011 Then, while we attempt to parse the construct, the parser queues up
1012 error messages, rather than issuing them immediately, and saves the
1013 tokens it consumes. If the construct is parsed successfully, the
1014 parser "commits", i.e., it issues any queued error messages and
1015 the tokens that were being preserved are permanently discarded.
1016 If, however, the construct is not parsed successfully, the parser
1017 rolls back its state completely so that it can resume parsing using
1018 a different alternative.
1019
1020 Future Improvements
1021 -------------------
1022
1023 The performance of the parser could probably be improved
1024 substantially. Some possible improvements include:
1025
1026 - The expression parser recurses through the various levels of
1027 precedence as specified in the grammar, rather than using an
1028 operator-precedence technique. Therefore, parsing a simple
1029 identifier requires multiple recursive calls.
1030
1031 - We could often eliminate the need to parse tentatively by
1032 looking ahead a little bit. In some places, this approach
1033 might not entirely eliminate the need to parse tentatively, but
1034 it might still speed up the average case. */
1035
1036 /* Flags that are passed to some parsing functions. These values can
1037 be bitwise-ored together. */
1038
1039 typedef enum cp_parser_flags
1040 {
1041 /* No flags. */
1042 CP_PARSER_FLAGS_NONE = 0x0,
1043 /* The construct is optional. If it is not present, then no error
1044 should be issued. */
1045 CP_PARSER_FLAGS_OPTIONAL = 0x1,
1046 /* When parsing a type-specifier, do not allow user-defined types. */
1047 CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1048 } cp_parser_flags;
1049
1050 /* The different kinds of ids that we ecounter. */
1051
1052 typedef enum cp_parser_id_kind
1053 {
1054 /* Not an id at all. */
1055 CP_PARSER_ID_KIND_NONE,
1056 /* An unqualified-id that is not a template-id. */
1057 CP_PARSER_ID_KIND_UNQUALIFIED,
1058 /* An unqualified template-id. */
1059 CP_PARSER_ID_KIND_TEMPLATE_ID,
1060 /* A qualified-id. */
1061 CP_PARSER_ID_KIND_QUALIFIED
1062 } cp_parser_id_kind;
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 declartor. */
1069 CP_PARSER_DECLARATOR_ABSTRACT,
1070 /* We want a named declarator. */
1071 CP_PARSER_DECLARATOR_NAMED,
1072 /* We don't mind, but the name must be an unqualified-id */
1073 CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
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 cpp_ttype token_type;
1082 /* The corresponding tree code. */
1083 enum tree_code tree_type;
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 ((char *)context, 0, sizeof (*context));
1148 }
1149 else
1150 context = ((cp_parser_context *)
1151 ggc_alloc_cleared (sizeof (cp_parser_context)));
1152 /* No errors have occurred yet in this context. */
1153 context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1154 /* If this is not the bottomost context, copy information that we
1155 need from the previous context. */
1156 if (next)
1157 {
1158 /* If, in the NEXT context, we are parsing an `x->' or `x.'
1159 expression, then we are parsing one in this context, too. */
1160 context->object_type = next->object_type;
1161 /* Thread the stack. */
1162 context->next = next;
1163 }
1164
1165 return context;
1166 }
1167
1168 /* The cp_parser structure represents the C++ parser. */
1169
1170 typedef struct cp_parser GTY(())
1171 {
1172 /* The lexer from which we are obtaining tokens. */
1173 cp_lexer *lexer;
1174
1175 /* The scope in which names should be looked up. If NULL_TREE, then
1176 we look up names in the scope that is currently open in the
1177 source program. If non-NULL, this is either a TYPE or
1178 NAMESPACE_DECL for the scope in which we should look.
1179
1180 This value is not cleared automatically after a name is looked
1181 up, so we must be careful to clear it before starting a new look
1182 up sequence. (If it is not cleared, then `X::Y' followed by `Z'
1183 will look up `Z' in the scope of `X', rather than the current
1184 scope.) Unfortunately, it is difficult to tell when name lookup
1185 is complete, because we sometimes peek at a token, look it up,
1186 and then decide not to consume it. */
1187 tree scope;
1188
1189 /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1190 last lookup took place. OBJECT_SCOPE is used if an expression
1191 like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1192 respectively. QUALIFYING_SCOPE is used for an expression of the
1193 form "X::Y"; it refers to X. */
1194 tree object_scope;
1195 tree qualifying_scope;
1196
1197 /* A stack of parsing contexts. All but the bottom entry on the
1198 stack will be tentative contexts.
1199
1200 We parse tentatively in order to determine which construct is in
1201 use in some situations. For example, in order to determine
1202 whether a statement is an expression-statement or a
1203 declaration-statement we parse it tentatively as a
1204 declaration-statement. If that fails, we then reparse the same
1205 token stream as an expression-statement. */
1206 cp_parser_context *context;
1207
1208 /* True if we are parsing GNU C++. If this flag is not set, then
1209 GNU extensions are not recognized. */
1210 bool allow_gnu_extensions_p;
1211
1212 /* TRUE if the `>' token should be interpreted as the greater-than
1213 operator. FALSE if it is the end of a template-id or
1214 template-parameter-list. */
1215 bool greater_than_is_operator_p;
1216
1217 /* TRUE if default arguments are allowed within a parameter list
1218 that starts at this point. FALSE if only a gnu extension makes
1219 them permissable. */
1220 bool default_arg_ok_p;
1221
1222 /* TRUE if we are parsing an integral constant-expression. See
1223 [expr.const] for a precise definition. */
1224 bool constant_expression_p;
1225
1226 /* TRUE if we are parsing an integral constant-expression -- but a
1227 non-constant expression should be permitted as well. This flag
1228 is used when parsing an array bound so that GNU variable-length
1229 arrays are tolerated. */
1230 bool allow_non_constant_expression_p;
1231
1232 /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1233 been seen that makes the expression non-constant. */
1234 bool non_constant_expression_p;
1235
1236 /* TRUE if local variable names and `this' are forbidden in the
1237 current context. */
1238 bool local_variables_forbidden_p;
1239
1240 /* TRUE if the declaration we are parsing is part of a
1241 linkage-specification of the form `extern string-literal
1242 declaration'. */
1243 bool in_unbraced_linkage_specification_p;
1244
1245 /* TRUE if we are presently parsing a declarator, after the
1246 direct-declarator. */
1247 bool in_declarator_p;
1248
1249 /* If non-NULL, then we are parsing a construct where new type
1250 definitions are not permitted. The string stored here will be
1251 issued as an error message if a type is defined. */
1252 const char *type_definition_forbidden_message;
1253
1254 /* A list of lists. The outer list is a stack, used for member
1255 functions of local classes. At each level there are two sub-list,
1256 one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1257 sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1258 TREE_VALUE's. The functions are chained in reverse declaration
1259 order.
1260
1261 The TREE_PURPOSE sublist contains those functions with default
1262 arguments that need post processing, and the TREE_VALUE sublist
1263 contains those functions with definitions that need post
1264 processing.
1265
1266 These lists can only be processed once the outermost class being
1267 defined is complete. */
1268 tree unparsed_functions_queues;
1269
1270 /* The number of classes whose definitions are currently in
1271 progress. */
1272 unsigned num_classes_being_defined;
1273
1274 /* The number of template parameter lists that apply directly to the
1275 current declaration. */
1276 unsigned num_template_parameter_lists;
1277 } cp_parser;
1278
1279 /* The type of a function that parses some kind of expression */
1280 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1281
1282 /* Prototypes. */
1283
1284 /* Constructors and destructors. */
1285
1286 static cp_parser *cp_parser_new
1287 (void);
1288
1289 /* Routines to parse various constructs.
1290
1291 Those that return `tree' will return the error_mark_node (rather
1292 than NULL_TREE) if a parse error occurs, unless otherwise noted.
1293 Sometimes, they will return an ordinary node if error-recovery was
1294 attempted, even though a parse error occurred. So, to check
1295 whether or not a parse error occurred, you should always use
1296 cp_parser_error_occurred. If the construct is optional (indicated
1297 either by an `_opt' in the name of the function that does the
1298 parsing or via a FLAGS parameter), then NULL_TREE is returned if
1299 the construct is not present. */
1300
1301 /* Lexical conventions [gram.lex] */
1302
1303 static tree cp_parser_identifier
1304 (cp_parser *);
1305
1306 /* Basic concepts [gram.basic] */
1307
1308 static bool cp_parser_translation_unit
1309 (cp_parser *);
1310
1311 /* Expressions [gram.expr] */
1312
1313 static tree cp_parser_primary_expression
1314 (cp_parser *, cp_parser_id_kind *, tree *);
1315 static tree cp_parser_id_expression
1316 (cp_parser *, bool, bool, bool *);
1317 static tree cp_parser_unqualified_id
1318 (cp_parser *, bool, bool);
1319 static tree cp_parser_nested_name_specifier_opt
1320 (cp_parser *, bool, bool, bool);
1321 static tree cp_parser_nested_name_specifier
1322 (cp_parser *, bool, bool, bool);
1323 static tree cp_parser_class_or_namespace_name
1324 (cp_parser *, bool, bool, bool, bool);
1325 static tree cp_parser_postfix_expression
1326 (cp_parser *, bool);
1327 static tree cp_parser_expression_list
1328 (cp_parser *);
1329 static void cp_parser_pseudo_destructor_name
1330 (cp_parser *, tree *, tree *);
1331 static tree cp_parser_unary_expression
1332 (cp_parser *, bool);
1333 static enum tree_code cp_parser_unary_operator
1334 (cp_token *);
1335 static tree cp_parser_new_expression
1336 (cp_parser *);
1337 static tree cp_parser_new_placement
1338 (cp_parser *);
1339 static tree cp_parser_new_type_id
1340 (cp_parser *);
1341 static tree cp_parser_new_declarator_opt
1342 (cp_parser *);
1343 static tree cp_parser_direct_new_declarator
1344 (cp_parser *);
1345 static tree cp_parser_new_initializer
1346 (cp_parser *);
1347 static tree cp_parser_delete_expression
1348 (cp_parser *);
1349 static tree cp_parser_cast_expression
1350 (cp_parser *, bool);
1351 static tree cp_parser_pm_expression
1352 (cp_parser *);
1353 static tree cp_parser_multiplicative_expression
1354 (cp_parser *);
1355 static tree cp_parser_additive_expression
1356 (cp_parser *);
1357 static tree cp_parser_shift_expression
1358 (cp_parser *);
1359 static tree cp_parser_relational_expression
1360 (cp_parser *);
1361 static tree cp_parser_equality_expression
1362 (cp_parser *);
1363 static tree cp_parser_and_expression
1364 (cp_parser *);
1365 static tree cp_parser_exclusive_or_expression
1366 (cp_parser *);
1367 static tree cp_parser_inclusive_or_expression
1368 (cp_parser *);
1369 static tree cp_parser_logical_and_expression
1370 (cp_parser *);
1371 static tree cp_parser_logical_or_expression
1372 (cp_parser *);
1373 static tree cp_parser_conditional_expression
1374 (cp_parser *);
1375 static tree cp_parser_question_colon_clause
1376 (cp_parser *, tree);
1377 static tree cp_parser_assignment_expression
1378 (cp_parser *);
1379 static enum tree_code cp_parser_assignment_operator_opt
1380 (cp_parser *);
1381 static tree cp_parser_expression
1382 (cp_parser *);
1383 static tree cp_parser_constant_expression
1384 (cp_parser *, bool, bool *);
1385
1386 /* Statements [gram.stmt.stmt] */
1387
1388 static void cp_parser_statement
1389 (cp_parser *);
1390 static tree cp_parser_labeled_statement
1391 (cp_parser *);
1392 static tree cp_parser_expression_statement
1393 (cp_parser *);
1394 static tree cp_parser_compound_statement
1395 (cp_parser *);
1396 static void cp_parser_statement_seq_opt
1397 (cp_parser *);
1398 static tree cp_parser_selection_statement
1399 (cp_parser *);
1400 static tree cp_parser_condition
1401 (cp_parser *);
1402 static tree cp_parser_iteration_statement
1403 (cp_parser *);
1404 static void cp_parser_for_init_statement
1405 (cp_parser *);
1406 static tree cp_parser_jump_statement
1407 (cp_parser *);
1408 static void cp_parser_declaration_statement
1409 (cp_parser *);
1410
1411 static tree cp_parser_implicitly_scoped_statement
1412 (cp_parser *);
1413 static void cp_parser_already_scoped_statement
1414 (cp_parser *);
1415
1416 /* Declarations [gram.dcl.dcl] */
1417
1418 static void cp_parser_declaration_seq_opt
1419 (cp_parser *);
1420 static void cp_parser_declaration
1421 (cp_parser *);
1422 static void cp_parser_block_declaration
1423 (cp_parser *, bool);
1424 static void cp_parser_simple_declaration
1425 (cp_parser *, bool);
1426 static tree cp_parser_decl_specifier_seq
1427 (cp_parser *, cp_parser_flags, tree *, bool *);
1428 static tree cp_parser_storage_class_specifier_opt
1429 (cp_parser *);
1430 static tree cp_parser_function_specifier_opt
1431 (cp_parser *);
1432 static tree cp_parser_type_specifier
1433 (cp_parser *, cp_parser_flags, bool, bool, bool *, bool *);
1434 static tree cp_parser_simple_type_specifier
1435 (cp_parser *, cp_parser_flags);
1436 static tree cp_parser_type_name
1437 (cp_parser *);
1438 static tree cp_parser_elaborated_type_specifier
1439 (cp_parser *, bool, bool);
1440 static tree cp_parser_enum_specifier
1441 (cp_parser *);
1442 static void cp_parser_enumerator_list
1443 (cp_parser *, tree);
1444 static void cp_parser_enumerator_definition
1445 (cp_parser *, tree);
1446 static tree cp_parser_namespace_name
1447 (cp_parser *);
1448 static void cp_parser_namespace_definition
1449 (cp_parser *);
1450 static void cp_parser_namespace_body
1451 (cp_parser *);
1452 static tree cp_parser_qualified_namespace_specifier
1453 (cp_parser *);
1454 static void cp_parser_namespace_alias_definition
1455 (cp_parser *);
1456 static void cp_parser_using_declaration
1457 (cp_parser *);
1458 static void cp_parser_using_directive
1459 (cp_parser *);
1460 static void cp_parser_asm_definition
1461 (cp_parser *);
1462 static void cp_parser_linkage_specification
1463 (cp_parser *);
1464
1465 /* Declarators [gram.dcl.decl] */
1466
1467 static tree cp_parser_init_declarator
1468 (cp_parser *, tree, tree, bool, bool, bool *);
1469 static tree cp_parser_declarator
1470 (cp_parser *, cp_parser_declarator_kind, bool *);
1471 static tree cp_parser_direct_declarator
1472 (cp_parser *, cp_parser_declarator_kind, bool *);
1473 static enum tree_code cp_parser_ptr_operator
1474 (cp_parser *, tree *, tree *);
1475 static tree cp_parser_cv_qualifier_seq_opt
1476 (cp_parser *);
1477 static tree cp_parser_cv_qualifier_opt
1478 (cp_parser *);
1479 static tree cp_parser_declarator_id
1480 (cp_parser *);
1481 static tree cp_parser_type_id
1482 (cp_parser *);
1483 static tree cp_parser_type_specifier_seq
1484 (cp_parser *);
1485 static tree cp_parser_parameter_declaration_clause
1486 (cp_parser *);
1487 static tree cp_parser_parameter_declaration_list
1488 (cp_parser *);
1489 static tree cp_parser_parameter_declaration
1490 (cp_parser *, bool);
1491 static tree cp_parser_function_definition
1492 (cp_parser *, bool *);
1493 static void cp_parser_function_body
1494 (cp_parser *);
1495 static tree cp_parser_initializer
1496 (cp_parser *, bool *);
1497 static tree cp_parser_initializer_clause
1498 (cp_parser *);
1499 static tree cp_parser_initializer_list
1500 (cp_parser *);
1501
1502 static bool cp_parser_ctor_initializer_opt_and_function_body
1503 (cp_parser *);
1504
1505 /* Classes [gram.class] */
1506
1507 static tree cp_parser_class_name
1508 (cp_parser *, bool, bool, bool, bool, bool);
1509 static tree cp_parser_class_specifier
1510 (cp_parser *);
1511 static tree cp_parser_class_head
1512 (cp_parser *, bool *);
1513 static enum tag_types cp_parser_class_key
1514 (cp_parser *);
1515 static void cp_parser_member_specification_opt
1516 (cp_parser *);
1517 static void cp_parser_member_declaration
1518 (cp_parser *);
1519 static tree cp_parser_pure_specifier
1520 (cp_parser *);
1521 static tree cp_parser_constant_initializer
1522 (cp_parser *);
1523
1524 /* Derived classes [gram.class.derived] */
1525
1526 static tree cp_parser_base_clause
1527 (cp_parser *);
1528 static tree cp_parser_base_specifier
1529 (cp_parser *);
1530
1531 /* Special member functions [gram.special] */
1532
1533 static tree cp_parser_conversion_function_id
1534 (cp_parser *);
1535 static tree cp_parser_conversion_type_id
1536 (cp_parser *);
1537 static tree cp_parser_conversion_declarator_opt
1538 (cp_parser *);
1539 static bool cp_parser_ctor_initializer_opt
1540 (cp_parser *);
1541 static void cp_parser_mem_initializer_list
1542 (cp_parser *);
1543 static tree cp_parser_mem_initializer
1544 (cp_parser *);
1545 static tree cp_parser_mem_initializer_id
1546 (cp_parser *);
1547
1548 /* Overloading [gram.over] */
1549
1550 static tree cp_parser_operator_function_id
1551 (cp_parser *);
1552 static tree cp_parser_operator
1553 (cp_parser *);
1554
1555 /* Templates [gram.temp] */
1556
1557 static void cp_parser_template_declaration
1558 (cp_parser *, bool);
1559 static tree cp_parser_template_parameter_list
1560 (cp_parser *);
1561 static tree cp_parser_template_parameter
1562 (cp_parser *);
1563 static tree cp_parser_type_parameter
1564 (cp_parser *);
1565 static tree cp_parser_template_id
1566 (cp_parser *, bool, bool);
1567 static tree cp_parser_template_name
1568 (cp_parser *, bool, bool);
1569 static tree cp_parser_template_argument_list
1570 (cp_parser *);
1571 static tree cp_parser_template_argument
1572 (cp_parser *);
1573 static void cp_parser_explicit_instantiation
1574 (cp_parser *);
1575 static void cp_parser_explicit_specialization
1576 (cp_parser *);
1577
1578 /* Exception handling [gram.exception] */
1579
1580 static tree cp_parser_try_block
1581 (cp_parser *);
1582 static bool cp_parser_function_try_block
1583 (cp_parser *);
1584 static void cp_parser_handler_seq
1585 (cp_parser *);
1586 static void cp_parser_handler
1587 (cp_parser *);
1588 static tree cp_parser_exception_declaration
1589 (cp_parser *);
1590 static tree cp_parser_throw_expression
1591 (cp_parser *);
1592 static tree cp_parser_exception_specification_opt
1593 (cp_parser *);
1594 static tree cp_parser_type_id_list
1595 (cp_parser *);
1596
1597 /* GNU Extensions */
1598
1599 static tree cp_parser_asm_specification_opt
1600 (cp_parser *);
1601 static tree cp_parser_asm_operand_list
1602 (cp_parser *);
1603 static tree cp_parser_asm_clobber_list
1604 (cp_parser *);
1605 static tree cp_parser_attributes_opt
1606 (cp_parser *);
1607 static tree cp_parser_attribute_list
1608 (cp_parser *);
1609 static bool cp_parser_extension_opt
1610 (cp_parser *, int *);
1611 static void cp_parser_label_declaration
1612 (cp_parser *);
1613
1614 /* Utility Routines */
1615
1616 static tree cp_parser_lookup_name
1617 (cp_parser *, tree, bool, bool, bool);
1618 static tree cp_parser_lookup_name_simple
1619 (cp_parser *, tree);
1620 static tree cp_parser_maybe_treat_template_as_class
1621 (tree, bool);
1622 static bool cp_parser_check_declarator_template_parameters
1623 (cp_parser *, tree);
1624 static bool cp_parser_check_template_parameters
1625 (cp_parser *, unsigned);
1626 static tree cp_parser_simple_cast_expression
1627 (cp_parser *);
1628 static tree cp_parser_binary_expression
1629 (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1630 static tree cp_parser_global_scope_opt
1631 (cp_parser *, bool);
1632 static bool cp_parser_constructor_declarator_p
1633 (cp_parser *, bool);
1634 static tree cp_parser_function_definition_from_specifiers_and_declarator
1635 (cp_parser *, tree, tree, tree);
1636 static tree cp_parser_function_definition_after_declarator
1637 (cp_parser *, bool);
1638 static void cp_parser_template_declaration_after_export
1639 (cp_parser *, bool);
1640 static tree cp_parser_single_declaration
1641 (cp_parser *, bool, bool *);
1642 static tree cp_parser_functional_cast
1643 (cp_parser *, tree);
1644 static void cp_parser_save_default_args
1645 (cp_parser *, tree);
1646 static void cp_parser_late_parsing_for_member
1647 (cp_parser *, tree);
1648 static void cp_parser_late_parsing_default_args
1649 (cp_parser *, tree);
1650 static tree cp_parser_sizeof_operand
1651 (cp_parser *, enum rid);
1652 static bool cp_parser_declares_only_class_p
1653 (cp_parser *);
1654 static bool cp_parser_friend_p
1655 (tree);
1656 static cp_token *cp_parser_require
1657 (cp_parser *, enum cpp_ttype, const char *);
1658 static cp_token *cp_parser_require_keyword
1659 (cp_parser *, enum rid, const char *);
1660 static bool cp_parser_token_starts_function_definition_p
1661 (cp_token *);
1662 static bool cp_parser_next_token_starts_class_definition_p
1663 (cp_parser *);
1664 static enum tag_types cp_parser_token_is_class_key
1665 (cp_token *);
1666 static void cp_parser_check_class_key
1667 (enum tag_types, tree type);
1668 static bool cp_parser_optional_template_keyword
1669 (cp_parser *);
1670 static void cp_parser_pre_parsed_nested_name_specifier
1671 (cp_parser *);
1672 static void cp_parser_cache_group
1673 (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1674 static void cp_parser_parse_tentatively
1675 (cp_parser *);
1676 static void cp_parser_commit_to_tentative_parse
1677 (cp_parser *);
1678 static void cp_parser_abort_tentative_parse
1679 (cp_parser *);
1680 static bool cp_parser_parse_definitely
1681 (cp_parser *);
1682 static inline bool cp_parser_parsing_tentatively
1683 (cp_parser *);
1684 static bool cp_parser_committed_to_tentative_parse
1685 (cp_parser *);
1686 static void cp_parser_error
1687 (cp_parser *, const char *);
1688 static bool cp_parser_simulate_error
1689 (cp_parser *);
1690 static void cp_parser_check_type_definition
1691 (cp_parser *);
1692 static tree cp_parser_non_constant_expression
1693 (const char *);
1694 static tree cp_parser_non_constant_id_expression
1695 (tree);
1696 static bool cp_parser_diagnose_invalid_type_name
1697 (cp_parser *);
1698 static bool cp_parser_skip_to_closing_parenthesis
1699 (cp_parser *);
1700 static bool cp_parser_skip_to_closing_parenthesis_or_comma
1701 (cp_parser *);
1702 static void cp_parser_skip_to_end_of_statement
1703 (cp_parser *);
1704 static void cp_parser_consume_semicolon_at_end_of_statement
1705 (cp_parser *);
1706 static void cp_parser_skip_to_end_of_block_or_statement
1707 (cp_parser *);
1708 static void cp_parser_skip_to_closing_brace
1709 (cp_parser *);
1710 static void cp_parser_skip_until_found
1711 (cp_parser *, enum cpp_ttype, const char *);
1712 static bool cp_parser_error_occurred
1713 (cp_parser *);
1714 static bool cp_parser_allow_gnu_extensions_p
1715 (cp_parser *);
1716 static bool cp_parser_is_string_literal
1717 (cp_token *);
1718 static bool cp_parser_is_keyword
1719 (cp_token *, enum rid);
1720
1721 /* Returns nonzero if we are parsing tentatively. */
1722
1723 static inline bool
1724 cp_parser_parsing_tentatively (cp_parser* parser)
1725 {
1726 return parser->context->next != NULL;
1727 }
1728
1729 /* Returns nonzero if TOKEN is a string literal. */
1730
1731 static bool
1732 cp_parser_is_string_literal (cp_token* token)
1733 {
1734 return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1735 }
1736
1737 /* Returns nonzero if TOKEN is the indicated KEYWORD. */
1738
1739 static bool
1740 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1741 {
1742 return token->keyword == keyword;
1743 }
1744
1745 /* Issue the indicated error MESSAGE. */
1746
1747 static void
1748 cp_parser_error (cp_parser* parser, const char* message)
1749 {
1750 /* Output the MESSAGE -- unless we're parsing tentatively. */
1751 if (!cp_parser_simulate_error (parser))
1752 error (message);
1753 }
1754
1755 /* If we are parsing tentatively, remember that an error has occurred
1756 during this tentative parse. Returns true if the error was
1757 simulated; false if a messgae should be issued by the caller. */
1758
1759 static bool
1760 cp_parser_simulate_error (cp_parser* parser)
1761 {
1762 if (cp_parser_parsing_tentatively (parser)
1763 && !cp_parser_committed_to_tentative_parse (parser))
1764 {
1765 parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1766 return true;
1767 }
1768 return false;
1769 }
1770
1771 /* This function is called when a type is defined. If type
1772 definitions are forbidden at this point, an error message is
1773 issued. */
1774
1775 static void
1776 cp_parser_check_type_definition (cp_parser* parser)
1777 {
1778 /* If types are forbidden here, issue a message. */
1779 if (parser->type_definition_forbidden_message)
1780 /* Use `%s' to print the string in case there are any escape
1781 characters in the message. */
1782 error ("%s", parser->type_definition_forbidden_message);
1783 }
1784
1785 /* Issue an eror message about the fact that THING appeared in a
1786 constant-expression. Returns ERROR_MARK_NODE. */
1787
1788 static tree
1789 cp_parser_non_constant_expression (const char *thing)
1790 {
1791 error ("%s cannot appear in a constant-expression", thing);
1792 return error_mark_node;
1793 }
1794
1795 /* Issue an eror message about the fact that DECL appeared in a
1796 constant-expression. Returns ERROR_MARK_NODE. */
1797
1798 static tree
1799 cp_parser_non_constant_id_expression (tree decl)
1800 {
1801 error ("`%D' cannot appear in a constant-expression", decl);
1802 return error_mark_node;
1803 }
1804
1805 /* Check for a common situation where a type-name should be present,
1806 but is not, and issue a sensible error message. Returns true if an
1807 invalid type-name was detected. */
1808
1809 static bool
1810 cp_parser_diagnose_invalid_type_name (cp_parser *parser)
1811 {
1812 /* If the next two tokens are both identifiers, the code is
1813 erroneous. The usual cause of this situation is code like:
1814
1815 T t;
1816
1817 where "T" should name a type -- but does not. */
1818 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
1819 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME)
1820 {
1821 tree name;
1822
1823 /* If parsing tentatively, we should commit; we really are
1824 looking at a declaration. */
1825 /* Consume the first identifier. */
1826 name = cp_lexer_consume_token (parser->lexer)->value;
1827 /* Issue an error message. */
1828 error ("`%s' does not name a type", IDENTIFIER_POINTER (name));
1829 /* If we're in a template class, it's possible that the user was
1830 referring to a type from a base class. For example:
1831
1832 template <typename T> struct A { typedef T X; };
1833 template <typename T> struct B : public A<T> { X x; };
1834
1835 The user should have said "typename A<T>::X". */
1836 if (processing_template_decl && current_class_type)
1837 {
1838 tree b;
1839
1840 for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1841 b;
1842 b = TREE_CHAIN (b))
1843 {
1844 tree base_type = BINFO_TYPE (b);
1845 if (CLASS_TYPE_P (base_type)
1846 && dependent_type_p (base_type))
1847 {
1848 tree field;
1849 /* Go from a particular instantiation of the
1850 template (which will have an empty TYPE_FIELDs),
1851 to the main version. */
1852 base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1853 for (field = TYPE_FIELDS (base_type);
1854 field;
1855 field = TREE_CHAIN (field))
1856 if (TREE_CODE (field) == TYPE_DECL
1857 && DECL_NAME (field) == name)
1858 {
1859 error ("(perhaps `typename %T::%s' was intended)",
1860 BINFO_TYPE (b), IDENTIFIER_POINTER (name));
1861 break;
1862 }
1863 if (field)
1864 break;
1865 }
1866 }
1867 }
1868 /* Skip to the end of the declaration; there's no point in
1869 trying to process it. */
1870 cp_parser_skip_to_end_of_statement (parser);
1871
1872 return true;
1873 }
1874
1875 return false;
1876 }
1877
1878 /* Consume tokens up to, and including, the next non-nested closing `)'.
1879 Returns TRUE iff we found a closing `)'. */
1880
1881 static bool
1882 cp_parser_skip_to_closing_parenthesis (cp_parser *parser)
1883 {
1884 unsigned nesting_depth = 0;
1885
1886 while (true)
1887 {
1888 cp_token *token;
1889
1890 /* If we've run out of tokens, then there is no closing `)'. */
1891 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
1892 return false;
1893 /* Consume the token. */
1894 token = cp_lexer_consume_token (parser->lexer);
1895 /* If it is an `(', we have entered another level of nesting. */
1896 if (token->type == CPP_OPEN_PAREN)
1897 ++nesting_depth;
1898 /* If it is a `)', then we might be done. */
1899 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
1900 return true;
1901 }
1902 }
1903
1904 /* Consume tokens until the next token is a `)', or a `,'. Returns
1905 TRUE if the next token is a `,'. */
1906
1907 static bool
1908 cp_parser_skip_to_closing_parenthesis_or_comma (cp_parser *parser)
1909 {
1910 unsigned nesting_depth = 0;
1911
1912 while (true)
1913 {
1914 cp_token *token = cp_lexer_peek_token (parser->lexer);
1915
1916 /* If we've run out of tokens, then there is no closing `)'. */
1917 if (token->type == CPP_EOF)
1918 return false;
1919 /* If it is a `,' stop. */
1920 else if (token->type == CPP_COMMA && nesting_depth-- == 0)
1921 return true;
1922 /* If it is a `)', stop. */
1923 else if (token->type == CPP_CLOSE_PAREN && nesting_depth-- == 0)
1924 return false;
1925 /* If it is an `(', we have entered another level of nesting. */
1926 else if (token->type == CPP_OPEN_PAREN)
1927 ++nesting_depth;
1928 /* Consume the token. */
1929 token = cp_lexer_consume_token (parser->lexer);
1930 }
1931 }
1932
1933 /* Consume tokens until we reach the end of the current statement.
1934 Normally, that will be just before consuming a `;'. However, if a
1935 non-nested `}' comes first, then we stop before consuming that. */
1936
1937 static void
1938 cp_parser_skip_to_end_of_statement (cp_parser* parser)
1939 {
1940 unsigned nesting_depth = 0;
1941
1942 while (true)
1943 {
1944 cp_token *token;
1945
1946 /* Peek at the next token. */
1947 token = cp_lexer_peek_token (parser->lexer);
1948 /* If we've run out of tokens, stop. */
1949 if (token->type == CPP_EOF)
1950 break;
1951 /* If the next token is a `;', we have reached the end of the
1952 statement. */
1953 if (token->type == CPP_SEMICOLON && !nesting_depth)
1954 break;
1955 /* If the next token is a non-nested `}', then we have reached
1956 the end of the current block. */
1957 if (token->type == CPP_CLOSE_BRACE)
1958 {
1959 /* If this is a non-nested `}', stop before consuming it.
1960 That way, when confronted with something like:
1961
1962 { 3 + }
1963
1964 we stop before consuming the closing `}', even though we
1965 have not yet reached a `;'. */
1966 if (nesting_depth == 0)
1967 break;
1968 /* If it is the closing `}' for a block that we have
1969 scanned, stop -- but only after consuming the token.
1970 That way given:
1971
1972 void f g () { ... }
1973 typedef int I;
1974
1975 we will stop after the body of the erroneously declared
1976 function, but before consuming the following `typedef'
1977 declaration. */
1978 if (--nesting_depth == 0)
1979 {
1980 cp_lexer_consume_token (parser->lexer);
1981 break;
1982 }
1983 }
1984 /* If it the next token is a `{', then we are entering a new
1985 block. Consume the entire block. */
1986 else if (token->type == CPP_OPEN_BRACE)
1987 ++nesting_depth;
1988 /* Consume the token. */
1989 cp_lexer_consume_token (parser->lexer);
1990 }
1991 }
1992
1993 /* This function is called at the end of a statement or declaration.
1994 If the next token is a semicolon, it is consumed; otherwise, error
1995 recovery is attempted. */
1996
1997 static void
1998 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
1999 {
2000 /* Look for the trailing `;'. */
2001 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2002 {
2003 /* If there is additional (erroneous) input, skip to the end of
2004 the statement. */
2005 cp_parser_skip_to_end_of_statement (parser);
2006 /* If the next token is now a `;', consume it. */
2007 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2008 cp_lexer_consume_token (parser->lexer);
2009 }
2010 }
2011
2012 /* Skip tokens until we have consumed an entire block, or until we
2013 have consumed a non-nested `;'. */
2014
2015 static void
2016 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2017 {
2018 unsigned nesting_depth = 0;
2019
2020 while (true)
2021 {
2022 cp_token *token;
2023
2024 /* Peek at the next token. */
2025 token = cp_lexer_peek_token (parser->lexer);
2026 /* If we've run out of tokens, stop. */
2027 if (token->type == CPP_EOF)
2028 break;
2029 /* If the next token is a `;', we have reached the end of the
2030 statement. */
2031 if (token->type == CPP_SEMICOLON && !nesting_depth)
2032 {
2033 /* Consume the `;'. */
2034 cp_lexer_consume_token (parser->lexer);
2035 break;
2036 }
2037 /* Consume the token. */
2038 token = cp_lexer_consume_token (parser->lexer);
2039 /* If the next token is a non-nested `}', then we have reached
2040 the end of the current block. */
2041 if (token->type == CPP_CLOSE_BRACE
2042 && (nesting_depth == 0 || --nesting_depth == 0))
2043 break;
2044 /* If it the next token is a `{', then we are entering a new
2045 block. Consume the entire block. */
2046 if (token->type == CPP_OPEN_BRACE)
2047 ++nesting_depth;
2048 }
2049 }
2050
2051 /* Skip tokens until a non-nested closing curly brace is the next
2052 token. */
2053
2054 static void
2055 cp_parser_skip_to_closing_brace (cp_parser *parser)
2056 {
2057 unsigned nesting_depth = 0;
2058
2059 while (true)
2060 {
2061 cp_token *token;
2062
2063 /* Peek at the next token. */
2064 token = cp_lexer_peek_token (parser->lexer);
2065 /* If we've run out of tokens, stop. */
2066 if (token->type == CPP_EOF)
2067 break;
2068 /* If the next token is a non-nested `}', then we have reached
2069 the end of the current block. */
2070 if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2071 break;
2072 /* If it the next token is a `{', then we are entering a new
2073 block. Consume the entire block. */
2074 else if (token->type == CPP_OPEN_BRACE)
2075 ++nesting_depth;
2076 /* Consume the token. */
2077 cp_lexer_consume_token (parser->lexer);
2078 }
2079 }
2080
2081 /* Create a new C++ parser. */
2082
2083 static cp_parser *
2084 cp_parser_new (void)
2085 {
2086 cp_parser *parser;
2087 cp_lexer *lexer;
2088
2089 /* cp_lexer_new_main is called before calling ggc_alloc because
2090 cp_lexer_new_main might load a PCH file. */
2091 lexer = cp_lexer_new_main ();
2092
2093 parser = (cp_parser *) ggc_alloc_cleared (sizeof (cp_parser));
2094 parser->lexer = lexer;
2095 parser->context = cp_parser_context_new (NULL);
2096
2097 /* For now, we always accept GNU extensions. */
2098 parser->allow_gnu_extensions_p = 1;
2099
2100 /* The `>' token is a greater-than operator, not the end of a
2101 template-id. */
2102 parser->greater_than_is_operator_p = true;
2103
2104 parser->default_arg_ok_p = true;
2105
2106 /* We are not parsing a constant-expression. */
2107 parser->constant_expression_p = false;
2108 parser->allow_non_constant_expression_p = false;
2109 parser->non_constant_expression_p = false;
2110
2111 /* Local variable names are not forbidden. */
2112 parser->local_variables_forbidden_p = false;
2113
2114 /* We are not processing an `extern "C"' declaration. */
2115 parser->in_unbraced_linkage_specification_p = false;
2116
2117 /* We are not processing a declarator. */
2118 parser->in_declarator_p = false;
2119
2120 /* The unparsed function queue is empty. */
2121 parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2122
2123 /* There are no classes being defined. */
2124 parser->num_classes_being_defined = 0;
2125
2126 /* No template parameters apply. */
2127 parser->num_template_parameter_lists = 0;
2128
2129 return parser;
2130 }
2131
2132 /* Lexical conventions [gram.lex] */
2133
2134 /* Parse an identifier. Returns an IDENTIFIER_NODE representing the
2135 identifier. */
2136
2137 static tree
2138 cp_parser_identifier (cp_parser* parser)
2139 {
2140 cp_token *token;
2141
2142 /* Look for the identifier. */
2143 token = cp_parser_require (parser, CPP_NAME, "identifier");
2144 /* Return the value. */
2145 return token ? token->value : error_mark_node;
2146 }
2147
2148 /* Basic concepts [gram.basic] */
2149
2150 /* Parse a translation-unit.
2151
2152 translation-unit:
2153 declaration-seq [opt]
2154
2155 Returns TRUE if all went well. */
2156
2157 static bool
2158 cp_parser_translation_unit (cp_parser* parser)
2159 {
2160 while (true)
2161 {
2162 cp_parser_declaration_seq_opt (parser);
2163
2164 /* If there are no tokens left then all went well. */
2165 if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2166 break;
2167
2168 /* Otherwise, issue an error message. */
2169 cp_parser_error (parser, "expected declaration");
2170 return false;
2171 }
2172
2173 /* Consume the EOF token. */
2174 cp_parser_require (parser, CPP_EOF, "end-of-file");
2175
2176 /* Finish up. */
2177 finish_translation_unit ();
2178
2179 /* All went well. */
2180 return true;
2181 }
2182
2183 /* Expressions [gram.expr] */
2184
2185 /* Parse a primary-expression.
2186
2187 primary-expression:
2188 literal
2189 this
2190 ( expression )
2191 id-expression
2192
2193 GNU Extensions:
2194
2195 primary-expression:
2196 ( compound-statement )
2197 __builtin_va_arg ( assignment-expression , type-id )
2198
2199 literal:
2200 __null
2201
2202 Returns a representation of the expression.
2203
2204 *IDK indicates what kind of id-expression (if any) was present.
2205
2206 *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2207 used as the operand of a pointer-to-member. In that case,
2208 *QUALIFYING_CLASS gives the class that is used as the qualifying
2209 class in the pointer-to-member. */
2210
2211 static tree
2212 cp_parser_primary_expression (cp_parser *parser,
2213 cp_parser_id_kind *idk,
2214 tree *qualifying_class)
2215 {
2216 cp_token *token;
2217
2218 /* Assume the primary expression is not an id-expression. */
2219 *idk = CP_PARSER_ID_KIND_NONE;
2220 /* And that it cannot be used as pointer-to-member. */
2221 *qualifying_class = NULL_TREE;
2222
2223 /* Peek at the next token. */
2224 token = cp_lexer_peek_token (parser->lexer);
2225 switch (token->type)
2226 {
2227 /* literal:
2228 integer-literal
2229 character-literal
2230 floating-literal
2231 string-literal
2232 boolean-literal */
2233 case CPP_CHAR:
2234 case CPP_WCHAR:
2235 case CPP_STRING:
2236 case CPP_WSTRING:
2237 case CPP_NUMBER:
2238 token = cp_lexer_consume_token (parser->lexer);
2239 return token->value;
2240
2241 case CPP_OPEN_PAREN:
2242 {
2243 tree expr;
2244 bool saved_greater_than_is_operator_p;
2245
2246 /* Consume the `('. */
2247 cp_lexer_consume_token (parser->lexer);
2248 /* Within a parenthesized expression, a `>' token is always
2249 the greater-than operator. */
2250 saved_greater_than_is_operator_p
2251 = parser->greater_than_is_operator_p;
2252 parser->greater_than_is_operator_p = true;
2253 /* If we see `( { ' then we are looking at the beginning of
2254 a GNU statement-expression. */
2255 if (cp_parser_allow_gnu_extensions_p (parser)
2256 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2257 {
2258 /* Statement-expressions are not allowed by the standard. */
2259 if (pedantic)
2260 pedwarn ("ISO C++ forbids braced-groups within expressions");
2261
2262 /* And they're not allowed outside of a function-body; you
2263 cannot, for example, write:
2264
2265 int i = ({ int j = 3; j + 1; });
2266
2267 at class or namespace scope. */
2268 if (!at_function_scope_p ())
2269 error ("statement-expressions are allowed only inside functions");
2270 /* Start the statement-expression. */
2271 expr = begin_stmt_expr ();
2272 /* Parse the compound-statement. */
2273 cp_parser_compound_statement (parser);
2274 /* Finish up. */
2275 expr = finish_stmt_expr (expr);
2276 }
2277 else
2278 {
2279 /* Parse the parenthesized expression. */
2280 expr = cp_parser_expression (parser);
2281 /* Let the front end know that this expression was
2282 enclosed in parentheses. This matters in case, for
2283 example, the expression is of the form `A::B', since
2284 `&A::B' might be a pointer-to-member, but `&(A::B)' is
2285 not. */
2286 finish_parenthesized_expr (expr);
2287 }
2288 /* The `>' token might be the end of a template-id or
2289 template-parameter-list now. */
2290 parser->greater_than_is_operator_p
2291 = saved_greater_than_is_operator_p;
2292 /* Consume the `)'. */
2293 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2294 cp_parser_skip_to_end_of_statement (parser);
2295
2296 return expr;
2297 }
2298
2299 case CPP_KEYWORD:
2300 switch (token->keyword)
2301 {
2302 /* These two are the boolean literals. */
2303 case RID_TRUE:
2304 cp_lexer_consume_token (parser->lexer);
2305 return boolean_true_node;
2306 case RID_FALSE:
2307 cp_lexer_consume_token (parser->lexer);
2308 return boolean_false_node;
2309
2310 /* The `__null' literal. */
2311 case RID_NULL:
2312 cp_lexer_consume_token (parser->lexer);
2313 return null_node;
2314
2315 /* Recognize the `this' keyword. */
2316 case RID_THIS:
2317 cp_lexer_consume_token (parser->lexer);
2318 if (parser->local_variables_forbidden_p)
2319 {
2320 error ("`this' may not be used in this context");
2321 return error_mark_node;
2322 }
2323 /* Pointers cannot appear in constant-expressions. */
2324 if (parser->constant_expression_p)
2325 {
2326 if (!parser->allow_non_constant_expression_p)
2327 return cp_parser_non_constant_expression ("`this'");
2328 parser->non_constant_expression_p = true;
2329 }
2330 return finish_this_expr ();
2331
2332 /* The `operator' keyword can be the beginning of an
2333 id-expression. */
2334 case RID_OPERATOR:
2335 goto id_expression;
2336
2337 case RID_FUNCTION_NAME:
2338 case RID_PRETTY_FUNCTION_NAME:
2339 case RID_C99_FUNCTION_NAME:
2340 /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2341 __func__ are the names of variables -- but they are
2342 treated specially. Therefore, they are handled here,
2343 rather than relying on the generic id-expression logic
2344 below. Grammatically, these names are id-expressions.
2345
2346 Consume the token. */
2347 token = cp_lexer_consume_token (parser->lexer);
2348 /* Look up the name. */
2349 return finish_fname (token->value);
2350
2351 case RID_VA_ARG:
2352 {
2353 tree expression;
2354 tree type;
2355
2356 /* The `__builtin_va_arg' construct is used to handle
2357 `va_arg'. Consume the `__builtin_va_arg' token. */
2358 cp_lexer_consume_token (parser->lexer);
2359 /* Look for the opening `('. */
2360 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2361 /* Now, parse the assignment-expression. */
2362 expression = cp_parser_assignment_expression (parser);
2363 /* Look for the `,'. */
2364 cp_parser_require (parser, CPP_COMMA, "`,'");
2365 /* Parse the type-id. */
2366 type = cp_parser_type_id (parser);
2367 /* Look for the closing `)'. */
2368 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2369 /* Using `va_arg' in a constant-expression is not
2370 allowed. */
2371 if (parser->constant_expression_p)
2372 {
2373 if (!parser->allow_non_constant_expression_p)
2374 return cp_parser_non_constant_expression ("`va_arg'");
2375 parser->non_constant_expression_p = true;
2376 }
2377 return build_x_va_arg (expression, type);
2378 }
2379
2380 default:
2381 cp_parser_error (parser, "expected primary-expression");
2382 return error_mark_node;
2383 }
2384 /* Fall through. */
2385
2386 /* An id-expression can start with either an identifier, a
2387 `::' as the beginning of a qualified-id, or the "operator"
2388 keyword. */
2389 case CPP_NAME:
2390 case CPP_SCOPE:
2391 case CPP_TEMPLATE_ID:
2392 case CPP_NESTED_NAME_SPECIFIER:
2393 {
2394 tree id_expression;
2395 tree decl;
2396
2397 id_expression:
2398 /* Parse the id-expression. */
2399 id_expression
2400 = cp_parser_id_expression (parser,
2401 /*template_keyword_p=*/false,
2402 /*check_dependency_p=*/true,
2403 /*template_p=*/NULL);
2404 if (id_expression == error_mark_node)
2405 return error_mark_node;
2406 /* If we have a template-id, then no further lookup is
2407 required. If the template-id was for a template-class, we
2408 will sometimes have a TYPE_DECL at this point. */
2409 else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2410 || TREE_CODE (id_expression) == TYPE_DECL)
2411 decl = id_expression;
2412 /* Look up the name. */
2413 else
2414 {
2415 decl = cp_parser_lookup_name_simple (parser, id_expression);
2416 /* If name lookup gives us a SCOPE_REF, then the
2417 qualifying scope was dependent. Just propagate the
2418 name. */
2419 if (TREE_CODE (decl) == SCOPE_REF)
2420 {
2421 if (TYPE_P (TREE_OPERAND (decl, 0)))
2422 *qualifying_class = TREE_OPERAND (decl, 0);
2423 /* Since this name was dependent, the expression isn't
2424 constant -- yet. No error is issued because it
2425 might be constant when things are instantiated. */
2426 if (parser->constant_expression_p)
2427 parser->non_constant_expression_p = true;
2428 return decl;
2429 }
2430 /* Check to see if DECL is a local variable in a context
2431 where that is forbidden. */
2432 if (parser->local_variables_forbidden_p
2433 && local_variable_p (decl))
2434 {
2435 /* It might be that we only found DECL because we are
2436 trying to be generous with pre-ISO scoping rules.
2437 For example, consider:
2438
2439 int i;
2440 void g() {
2441 for (int i = 0; i < 10; ++i) {}
2442 extern void f(int j = i);
2443 }
2444
2445 Here, name look up will originally find the out
2446 of scope `i'. We need to issue a warning message,
2447 but then use the global `i'. */
2448 decl = check_for_out_of_scope_variable (decl);
2449 if (local_variable_p (decl))
2450 {
2451 error ("local variable `%D' may not appear in this context",
2452 decl);
2453 return error_mark_node;
2454 }
2455 }
2456
2457 if (decl == error_mark_node)
2458 {
2459 /* Name lookup failed. */
2460 if (!parser->scope
2461 && processing_template_decl)
2462 {
2463 /* Unqualified name lookup failed while processing a
2464 template. */
2465 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2466 /* If the next token is a parenthesis, assume that
2467 Koenig lookup will succeed when instantiating the
2468 template. */
2469 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
2470 return build_min_nt (LOOKUP_EXPR, id_expression);
2471 /* If we're not doing Koenig lookup, issue an error. */
2472 error ("`%D' has not been declared", id_expression);
2473 return error_mark_node;
2474 }
2475 else if (parser->scope
2476 && (!TYPE_P (parser->scope)
2477 || !dependent_type_p (parser->scope)))
2478 {
2479 /* Qualified name lookup failed, and the
2480 qualifying name was not a dependent type. That
2481 is always an error. */
2482 if (TYPE_P (parser->scope)
2483 && !COMPLETE_TYPE_P (parser->scope))
2484 error ("incomplete type `%T' used in nested name "
2485 "specifier",
2486 parser->scope);
2487 else if (parser->scope != global_namespace)
2488 error ("`%D' is not a member of `%D'",
2489 id_expression, parser->scope);
2490 else
2491 error ("`::%D' has not been declared", id_expression);
2492 return error_mark_node;
2493 }
2494 else if (!parser->scope && !processing_template_decl)
2495 {
2496 /* It may be resolvable as a koenig lookup function
2497 call. */
2498 *idk = CP_PARSER_ID_KIND_UNQUALIFIED;
2499 return id_expression;
2500 }
2501 }
2502 /* If DECL is a variable that would be out of scope under
2503 ANSI/ISO rules, but in scope in the ARM, name lookup
2504 will succeed. Issue a diagnostic here. */
2505 else
2506 decl = check_for_out_of_scope_variable (decl);
2507
2508 /* Remember that the name was used in the definition of
2509 the current class so that we can check later to see if
2510 the meaning would have been different after the class
2511 was entirely defined. */
2512 if (!parser->scope && decl != error_mark_node)
2513 maybe_note_name_used_in_class (id_expression, decl);
2514 }
2515
2516 /* If we didn't find anything, or what we found was a type,
2517 then this wasn't really an id-expression. */
2518 if (TREE_CODE (decl) == TEMPLATE_DECL
2519 && !DECL_FUNCTION_TEMPLATE_P (decl))
2520 {
2521 cp_parser_error (parser, "missing template arguments");
2522 return error_mark_node;
2523 }
2524 else if (TREE_CODE (decl) == TYPE_DECL
2525 || TREE_CODE (decl) == NAMESPACE_DECL)
2526 {
2527 cp_parser_error (parser,
2528 "expected primary-expression");
2529 return error_mark_node;
2530 }
2531
2532 /* If the name resolved to a template parameter, there is no
2533 need to look it up again later. Similarly, we resolve
2534 enumeration constants to their underlying values. */
2535 if (TREE_CODE (decl) == CONST_DECL)
2536 {
2537 *idk = CP_PARSER_ID_KIND_NONE;
2538 if (DECL_TEMPLATE_PARM_P (decl) || !processing_template_decl)
2539 return DECL_INITIAL (decl);
2540 return decl;
2541 }
2542 else
2543 {
2544 bool dependent_p;
2545
2546 /* If the declaration was explicitly qualified indicate
2547 that. The semantics of `A::f(3)' are different than
2548 `f(3)' if `f' is virtual. */
2549 *idk = (parser->scope
2550 ? CP_PARSER_ID_KIND_QUALIFIED
2551 : (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2552 ? CP_PARSER_ID_KIND_TEMPLATE_ID
2553 : CP_PARSER_ID_KIND_UNQUALIFIED));
2554
2555
2556 /* [temp.dep.expr]
2557
2558 An id-expression is type-dependent if it contains an
2559 identifier that was declared with a dependent type.
2560
2561 As an optimization, we could choose not to create a
2562 LOOKUP_EXPR for a name that resolved to a local
2563 variable in the template function that we are currently
2564 declaring; such a name cannot ever resolve to anything
2565 else. If we did that we would not have to look up
2566 these names at instantiation time.
2567
2568 The standard is not very specific about an
2569 id-expression that names a set of overloaded functions.
2570 What if some of them have dependent types and some of
2571 them do not? Presumably, such a name should be treated
2572 as a dependent name. */
2573 /* Assume the name is not dependent. */
2574 dependent_p = false;
2575 if (!processing_template_decl)
2576 /* No names are dependent outside a template. */
2577 ;
2578 /* A template-id where the name of the template was not
2579 resolved is definitely dependent. */
2580 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
2581 && (TREE_CODE (TREE_OPERAND (decl, 0))
2582 == IDENTIFIER_NODE))
2583 dependent_p = true;
2584 /* For anything except an overloaded function, just check
2585 its type. */
2586 else if (!is_overloaded_fn (decl))
2587 dependent_p
2588 = dependent_type_p (TREE_TYPE (decl));
2589 /* For a set of overloaded functions, check each of the
2590 functions. */
2591 else
2592 {
2593 tree fns = decl;
2594
2595 if (BASELINK_P (fns))
2596 fns = BASELINK_FUNCTIONS (fns);
2597
2598 /* For a template-id, check to see if the template
2599 arguments are dependent. */
2600 if (TREE_CODE (fns) == TEMPLATE_ID_EXPR)
2601 {
2602 tree args = TREE_OPERAND (fns, 1);
2603
2604 if (args && TREE_CODE (args) == TREE_LIST)
2605 {
2606 while (args)
2607 {
2608 if (dependent_template_arg_p (TREE_VALUE (args)))
2609 {
2610 dependent_p = true;
2611 break;
2612 }
2613 args = TREE_CHAIN (args);
2614 }
2615 }
2616 else if (args && TREE_CODE (args) == TREE_VEC)
2617 {
2618 int i;
2619 for (i = 0; i < TREE_VEC_LENGTH (args); ++i)
2620 if (dependent_template_arg_p (TREE_VEC_ELT (args, i)))
2621 {
2622 dependent_p = true;
2623 break;
2624 }
2625 }
2626
2627 /* The functions are those referred to by the
2628 template-id. */
2629 fns = TREE_OPERAND (fns, 0);
2630 }
2631
2632 /* If there are no dependent template arguments, go
2633 through the overlaoded functions. */
2634 while (fns && !dependent_p)
2635 {
2636 tree fn = OVL_CURRENT (fns);
2637
2638 /* Member functions of dependent classes are
2639 dependent. */
2640 if (TREE_CODE (fn) == FUNCTION_DECL
2641 && type_dependent_expression_p (fn))
2642 dependent_p = true;
2643 else if (TREE_CODE (fn) == TEMPLATE_DECL
2644 && dependent_template_p (fn))
2645 dependent_p = true;
2646
2647 fns = OVL_NEXT (fns);
2648 }
2649 }
2650
2651 /* If the name was dependent on a template parameter,
2652 we will resolve the name at instantiation time. */
2653 if (dependent_p)
2654 {
2655 /* Create a SCOPE_REF for qualified names, if the
2656 scope is dependent. */
2657 if (parser->scope)
2658 {
2659 if (TYPE_P (parser->scope))
2660 *qualifying_class = parser->scope;
2661 /* Since this name was dependent, the expression isn't
2662 constant -- yet. No error is issued because it
2663 might be constant when things are instantiated. */
2664 if (parser->constant_expression_p)
2665 parser->non_constant_expression_p = true;
2666 if (TYPE_P (parser->scope)
2667 && dependent_type_p (parser->scope))
2668 return build_nt (SCOPE_REF,
2669 parser->scope,
2670 id_expression);
2671 else
2672 return decl;
2673 }
2674 /* A TEMPLATE_ID already contains all the information
2675 we need. */
2676 if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR)
2677 return id_expression;
2678 /* Since this name was dependent, the expression isn't
2679 constant -- yet. No error is issued because it
2680 might be constant when things are instantiated. */
2681 if (parser->constant_expression_p)
2682 parser->non_constant_expression_p = true;
2683 /* Create a LOOKUP_EXPR for other unqualified names. */
2684 return build_min_nt (LOOKUP_EXPR, id_expression);
2685 }
2686
2687 /* Only certain kinds of names are allowed in constant
2688 expression. Enumerators have already been handled
2689 above. */
2690 if (parser->constant_expression_p
2691 /* Non-type template parameters of integral or
2692 enumeration type. */
2693 && !(TREE_CODE (decl) == TEMPLATE_PARM_INDEX
2694 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl)))
2695 /* Const variables or static data members of integral
2696 or enumeration types initialized with constant
2697 expressions (or dependent expressions - in this case
2698 the check will be done at instantiation time). */
2699 && !(TREE_CODE (decl) == VAR_DECL
2700 && INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (decl))
2701 && DECL_INITIAL (decl)
2702 && (TREE_CONSTANT (DECL_INITIAL (decl))
2703 || type_dependent_expression_p
2704 (DECL_INITIAL (decl))
2705 || value_dependent_expression_p
2706 (DECL_INITIAL (decl)))))
2707 {
2708 if (!parser->allow_non_constant_expression_p)
2709 return cp_parser_non_constant_id_expression (decl);
2710 parser->non_constant_expression_p = true;
2711 }
2712
2713 if (parser->scope)
2714 {
2715 decl = (adjust_result_of_qualified_name_lookup
2716 (decl, parser->scope, current_class_type));
2717 if (TREE_CODE (decl) == FIELD_DECL || BASELINK_P (decl))
2718 *qualifying_class = parser->scope;
2719 else if (!processing_template_decl)
2720 decl = convert_from_reference (decl);
2721 }
2722 else
2723 /* Transform references to non-static data members into
2724 COMPONENT_REFs. */
2725 decl = hack_identifier (decl, id_expression);
2726
2727 /* Resolve references to variables of anonymous unions
2728 into COMPONENT_REFs. */
2729 if (TREE_CODE (decl) == ALIAS_DECL)
2730 decl = DECL_INITIAL (decl);
2731 }
2732
2733 if (TREE_DEPRECATED (decl))
2734 warn_deprecated_use (decl);
2735
2736 return decl;
2737 }
2738
2739 /* Anything else is an error. */
2740 default:
2741 cp_parser_error (parser, "expected primary-expression");
2742 return error_mark_node;
2743 }
2744 }
2745
2746 /* Parse an id-expression.
2747
2748 id-expression:
2749 unqualified-id
2750 qualified-id
2751
2752 qualified-id:
2753 :: [opt] nested-name-specifier template [opt] unqualified-id
2754 :: identifier
2755 :: operator-function-id
2756 :: template-id
2757
2758 Return a representation of the unqualified portion of the
2759 identifier. Sets PARSER->SCOPE to the qualifying scope if there is
2760 a `::' or nested-name-specifier.
2761
2762 Often, if the id-expression was a qualified-id, the caller will
2763 want to make a SCOPE_REF to represent the qualified-id. This
2764 function does not do this in order to avoid wastefully creating
2765 SCOPE_REFs when they are not required.
2766
2767 If TEMPLATE_KEYWORD_P is true, then we have just seen the
2768 `template' keyword.
2769
2770 If CHECK_DEPENDENCY_P is false, then names are looked up inside
2771 uninstantiated templates.
2772
2773 If *TEMPLATE_P is non-NULL, it is set to true iff the
2774 `template' keyword is used to explicitly indicate that the entity
2775 named is a template. */
2776
2777 static tree
2778 cp_parser_id_expression (cp_parser *parser,
2779 bool template_keyword_p,
2780 bool check_dependency_p,
2781 bool *template_p)
2782 {
2783 bool global_scope_p;
2784 bool nested_name_specifier_p;
2785
2786 /* Assume the `template' keyword was not used. */
2787 if (template_p)
2788 *template_p = false;
2789
2790 /* Look for the optional `::' operator. */
2791 global_scope_p
2792 = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2793 != NULL_TREE);
2794 /* Look for the optional nested-name-specifier. */
2795 nested_name_specifier_p
2796 = (cp_parser_nested_name_specifier_opt (parser,
2797 /*typename_keyword_p=*/false,
2798 check_dependency_p,
2799 /*type_p=*/false)
2800 != NULL_TREE);
2801 /* If there is a nested-name-specifier, then we are looking at
2802 the first qualified-id production. */
2803 if (nested_name_specifier_p)
2804 {
2805 tree saved_scope;
2806 tree saved_object_scope;
2807 tree saved_qualifying_scope;
2808 tree unqualified_id;
2809 bool is_template;
2810
2811 /* See if the next token is the `template' keyword. */
2812 if (!template_p)
2813 template_p = &is_template;
2814 *template_p = cp_parser_optional_template_keyword (parser);
2815 /* Name lookup we do during the processing of the
2816 unqualified-id might obliterate SCOPE. */
2817 saved_scope = parser->scope;
2818 saved_object_scope = parser->object_scope;
2819 saved_qualifying_scope = parser->qualifying_scope;
2820 /* Process the final unqualified-id. */
2821 unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2822 check_dependency_p);
2823 /* Restore the SAVED_SCOPE for our caller. */
2824 parser->scope = saved_scope;
2825 parser->object_scope = saved_object_scope;
2826 parser->qualifying_scope = saved_qualifying_scope;
2827
2828 return unqualified_id;
2829 }
2830 /* Otherwise, if we are in global scope, then we are looking at one
2831 of the other qualified-id productions. */
2832 else if (global_scope_p)
2833 {
2834 cp_token *token;
2835 tree id;
2836
2837 /* Peek at the next token. */
2838 token = cp_lexer_peek_token (parser->lexer);
2839
2840 /* If it's an identifier, and the next token is not a "<", then
2841 we can avoid the template-id case. This is an optimization
2842 for this common case. */
2843 if (token->type == CPP_NAME
2844 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
2845 return cp_parser_identifier (parser);
2846
2847 cp_parser_parse_tentatively (parser);
2848 /* Try a template-id. */
2849 id = cp_parser_template_id (parser,
2850 /*template_keyword_p=*/false,
2851 /*check_dependency_p=*/true);
2852 /* If that worked, we're done. */
2853 if (cp_parser_parse_definitely (parser))
2854 return id;
2855
2856 /* Peek at the next token. (Changes in the token buffer may
2857 have invalidated the pointer obtained above.) */
2858 token = cp_lexer_peek_token (parser->lexer);
2859
2860 switch (token->type)
2861 {
2862 case CPP_NAME:
2863 return cp_parser_identifier (parser);
2864
2865 case CPP_KEYWORD:
2866 if (token->keyword == RID_OPERATOR)
2867 return cp_parser_operator_function_id (parser);
2868 /* Fall through. */
2869
2870 default:
2871 cp_parser_error (parser, "expected id-expression");
2872 return error_mark_node;
2873 }
2874 }
2875 else
2876 return cp_parser_unqualified_id (parser, template_keyword_p,
2877 /*check_dependency_p=*/true);
2878 }
2879
2880 /* Parse an unqualified-id.
2881
2882 unqualified-id:
2883 identifier
2884 operator-function-id
2885 conversion-function-id
2886 ~ class-name
2887 template-id
2888
2889 If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2890 keyword, in a construct like `A::template ...'.
2891
2892 Returns a representation of unqualified-id. For the `identifier'
2893 production, an IDENTIFIER_NODE is returned. For the `~ class-name'
2894 production a BIT_NOT_EXPR is returned; the operand of the
2895 BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name. For the
2896 other productions, see the documentation accompanying the
2897 corresponding parsing functions. If CHECK_DEPENDENCY_P is false,
2898 names are looked up in uninstantiated templates. */
2899
2900 static tree
2901 cp_parser_unqualified_id (cp_parser* parser,
2902 bool template_keyword_p,
2903 bool check_dependency_p)
2904 {
2905 cp_token *token;
2906
2907 /* Peek at the next token. */
2908 token = cp_lexer_peek_token (parser->lexer);
2909
2910 switch (token->type)
2911 {
2912 case CPP_NAME:
2913 {
2914 tree id;
2915
2916 /* We don't know yet whether or not this will be a
2917 template-id. */
2918 cp_parser_parse_tentatively (parser);
2919 /* Try a template-id. */
2920 id = cp_parser_template_id (parser, template_keyword_p,
2921 check_dependency_p);
2922 /* If it worked, we're done. */
2923 if (cp_parser_parse_definitely (parser))
2924 return id;
2925 /* Otherwise, it's an ordinary identifier. */
2926 return cp_parser_identifier (parser);
2927 }
2928
2929 case CPP_TEMPLATE_ID:
2930 return cp_parser_template_id (parser, template_keyword_p,
2931 check_dependency_p);
2932
2933 case CPP_COMPL:
2934 {
2935 tree type_decl;
2936 tree qualifying_scope;
2937 tree object_scope;
2938 tree scope;
2939
2940 /* Consume the `~' token. */
2941 cp_lexer_consume_token (parser->lexer);
2942 /* Parse the class-name. The standard, as written, seems to
2943 say that:
2944
2945 template <typename T> struct S { ~S (); };
2946 template <typename T> S<T>::~S() {}
2947
2948 is invalid, since `~' must be followed by a class-name, but
2949 `S<T>' is dependent, and so not known to be a class.
2950 That's not right; we need to look in uninstantiated
2951 templates. A further complication arises from:
2952
2953 template <typename T> void f(T t) {
2954 t.T::~T();
2955 }
2956
2957 Here, it is not possible to look up `T' in the scope of `T'
2958 itself. We must look in both the current scope, and the
2959 scope of the containing complete expression.
2960
2961 Yet another issue is:
2962
2963 struct S {
2964 int S;
2965 ~S();
2966 };
2967
2968 S::~S() {}
2969
2970 The standard does not seem to say that the `S' in `~S'
2971 should refer to the type `S' and not the data member
2972 `S::S'. */
2973
2974 /* DR 244 says that we look up the name after the "~" in the
2975 same scope as we looked up the qualifying name. That idea
2976 isn't fully worked out; it's more complicated than that. */
2977 scope = parser->scope;
2978 object_scope = parser->object_scope;
2979 qualifying_scope = parser->qualifying_scope;
2980
2981 /* If the name is of the form "X::~X" it's OK. */
2982 if (scope && TYPE_P (scope)
2983 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2984 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2985 == CPP_OPEN_PAREN)
2986 && (cp_lexer_peek_token (parser->lexer)->value
2987 == TYPE_IDENTIFIER (scope)))
2988 {
2989 cp_lexer_consume_token (parser->lexer);
2990 return build_nt (BIT_NOT_EXPR, scope);
2991 }
2992
2993 /* If there was an explicit qualification (S::~T), first look
2994 in the scope given by the qualification (i.e., S). */
2995 if (scope)
2996 {
2997 cp_parser_parse_tentatively (parser);
2998 type_decl = cp_parser_class_name (parser,
2999 /*typename_keyword_p=*/false,
3000 /*template_keyword_p=*/false,
3001 /*type_p=*/false,
3002 /*check_dependency=*/false,
3003 /*class_head_p=*/false);
3004 if (cp_parser_parse_definitely (parser))
3005 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3006 }
3007 /* In "N::S::~S", look in "N" as well. */
3008 if (scope && qualifying_scope)
3009 {
3010 cp_parser_parse_tentatively (parser);
3011 parser->scope = qualifying_scope;
3012 parser->object_scope = NULL_TREE;
3013 parser->qualifying_scope = NULL_TREE;
3014 type_decl
3015 = cp_parser_class_name (parser,
3016 /*typename_keyword_p=*/false,
3017 /*template_keyword_p=*/false,
3018 /*type_p=*/false,
3019 /*check_dependency=*/false,
3020 /*class_head_p=*/false);
3021 if (cp_parser_parse_definitely (parser))
3022 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3023 }
3024 /* In "p->S::~T", look in the scope given by "*p" as well. */
3025 else if (object_scope)
3026 {
3027 cp_parser_parse_tentatively (parser);
3028 parser->scope = object_scope;
3029 parser->object_scope = NULL_TREE;
3030 parser->qualifying_scope = NULL_TREE;
3031 type_decl
3032 = cp_parser_class_name (parser,
3033 /*typename_keyword_p=*/false,
3034 /*template_keyword_p=*/false,
3035 /*type_p=*/false,
3036 /*check_dependency=*/false,
3037 /*class_head_p=*/false);
3038 if (cp_parser_parse_definitely (parser))
3039 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3040 }
3041 /* Look in the surrounding context. */
3042 parser->scope = NULL_TREE;
3043 parser->object_scope = NULL_TREE;
3044 parser->qualifying_scope = NULL_TREE;
3045 type_decl
3046 = cp_parser_class_name (parser,
3047 /*typename_keyword_p=*/false,
3048 /*template_keyword_p=*/false,
3049 /*type_p=*/false,
3050 /*check_dependency=*/false,
3051 /*class_head_p=*/false);
3052 /* If an error occurred, assume that the name of the
3053 destructor is the same as the name of the qualifying
3054 class. That allows us to keep parsing after running
3055 into ill-formed destructor names. */
3056 if (type_decl == error_mark_node && scope && TYPE_P (scope))
3057 return build_nt (BIT_NOT_EXPR, scope);
3058 else if (type_decl == error_mark_node)
3059 return error_mark_node;
3060
3061 return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3062 }
3063
3064 case CPP_KEYWORD:
3065 if (token->keyword == RID_OPERATOR)
3066 {
3067 tree id;
3068
3069 /* This could be a template-id, so we try that first. */
3070 cp_parser_parse_tentatively (parser);
3071 /* Try a template-id. */
3072 id = cp_parser_template_id (parser, template_keyword_p,
3073 /*check_dependency_p=*/true);
3074 /* If that worked, we're done. */
3075 if (cp_parser_parse_definitely (parser))
3076 return id;
3077 /* We still don't know whether we're looking at an
3078 operator-function-id or a conversion-function-id. */
3079 cp_parser_parse_tentatively (parser);
3080 /* Try an operator-function-id. */
3081 id = cp_parser_operator_function_id (parser);
3082 /* If that didn't work, try a conversion-function-id. */
3083 if (!cp_parser_parse_definitely (parser))
3084 id = cp_parser_conversion_function_id (parser);
3085
3086 return id;
3087 }
3088 /* Fall through. */
3089
3090 default:
3091 cp_parser_error (parser, "expected unqualified-id");
3092 return error_mark_node;
3093 }
3094 }
3095
3096 /* Parse an (optional) nested-name-specifier.
3097
3098 nested-name-specifier:
3099 class-or-namespace-name :: nested-name-specifier [opt]
3100 class-or-namespace-name :: template nested-name-specifier [opt]
3101
3102 PARSER->SCOPE should be set appropriately before this function is
3103 called. TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3104 effect. TYPE_P is TRUE if we non-type bindings should be ignored
3105 in name lookups.
3106
3107 Sets PARSER->SCOPE to the class (TYPE) or namespace
3108 (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3109 it unchanged if there is no nested-name-specifier. Returns the new
3110 scope iff there is a nested-name-specifier, or NULL_TREE otherwise. */
3111
3112 static tree
3113 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3114 bool typename_keyword_p,
3115 bool check_dependency_p,
3116 bool type_p)
3117 {
3118 bool success = false;
3119 tree access_check = NULL_TREE;
3120 ptrdiff_t start;
3121 cp_token* token;
3122
3123 /* If the next token corresponds to a nested name specifier, there
3124 is no need to reparse it. However, if CHECK_DEPENDENCY_P is
3125 false, it may have been true before, in which case something
3126 like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3127 of `A<X>::', where it should now be `A<X>::B<Y>::'. So, when
3128 CHECK_DEPENDENCY_P is false, we have to fall through into the
3129 main loop. */
3130 if (check_dependency_p
3131 && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3132 {
3133 cp_parser_pre_parsed_nested_name_specifier (parser);
3134 return parser->scope;
3135 }
3136
3137 /* Remember where the nested-name-specifier starts. */
3138 if (cp_parser_parsing_tentatively (parser)
3139 && !cp_parser_committed_to_tentative_parse (parser))
3140 {
3141 token = cp_lexer_peek_token (parser->lexer);
3142 start = cp_lexer_token_difference (parser->lexer,
3143 parser->lexer->first_token,
3144 token);
3145 }
3146 else
3147 start = -1;
3148
3149 push_deferring_access_checks (dk_deferred);
3150
3151 while (true)
3152 {
3153 tree new_scope;
3154 tree old_scope;
3155 tree saved_qualifying_scope;
3156 bool template_keyword_p;
3157
3158 /* Spot cases that cannot be the beginning of a
3159 nested-name-specifier. */
3160 token = cp_lexer_peek_token (parser->lexer);
3161
3162 /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3163 the already parsed nested-name-specifier. */
3164 if (token->type == CPP_NESTED_NAME_SPECIFIER)
3165 {
3166 /* Grab the nested-name-specifier and continue the loop. */
3167 cp_parser_pre_parsed_nested_name_specifier (parser);
3168 success = true;
3169 continue;
3170 }
3171
3172 /* Spot cases that cannot be the beginning of a
3173 nested-name-specifier. On the second and subsequent times
3174 through the loop, we look for the `template' keyword. */
3175 if (success && token->keyword == RID_TEMPLATE)
3176 ;
3177 /* A template-id can start a nested-name-specifier. */
3178 else if (token->type == CPP_TEMPLATE_ID)
3179 ;
3180 else
3181 {
3182 /* If the next token is not an identifier, then it is
3183 definitely not a class-or-namespace-name. */
3184 if (token->type != CPP_NAME)
3185 break;
3186 /* If the following token is neither a `<' (to begin a
3187 template-id), nor a `::', then we are not looking at a
3188 nested-name-specifier. */
3189 token = cp_lexer_peek_nth_token (parser->lexer, 2);
3190 if (token->type != CPP_LESS && token->type != CPP_SCOPE)
3191 break;
3192 }
3193
3194 /* The nested-name-specifier is optional, so we parse
3195 tentatively. */
3196 cp_parser_parse_tentatively (parser);
3197
3198 /* Look for the optional `template' keyword, if this isn't the
3199 first time through the loop. */
3200 if (success)
3201 template_keyword_p = cp_parser_optional_template_keyword (parser);
3202 else
3203 template_keyword_p = false;
3204
3205 /* Save the old scope since the name lookup we are about to do
3206 might destroy it. */
3207 old_scope = parser->scope;
3208 saved_qualifying_scope = parser->qualifying_scope;
3209 /* Parse the qualifying entity. */
3210 new_scope
3211 = cp_parser_class_or_namespace_name (parser,
3212 typename_keyword_p,
3213 template_keyword_p,
3214 check_dependency_p,
3215 type_p);
3216 /* Look for the `::' token. */
3217 cp_parser_require (parser, CPP_SCOPE, "`::'");
3218
3219 /* If we found what we wanted, we keep going; otherwise, we're
3220 done. */
3221 if (!cp_parser_parse_definitely (parser))
3222 {
3223 bool error_p = false;
3224
3225 /* Restore the OLD_SCOPE since it was valid before the
3226 failed attempt at finding the last
3227 class-or-namespace-name. */
3228 parser->scope = old_scope;
3229 parser->qualifying_scope = saved_qualifying_scope;
3230 /* If the next token is an identifier, and the one after
3231 that is a `::', then any valid interpretation would have
3232 found a class-or-namespace-name. */
3233 while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3234 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3235 == CPP_SCOPE)
3236 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3237 != CPP_COMPL))
3238 {
3239 token = cp_lexer_consume_token (parser->lexer);
3240 if (!error_p)
3241 {
3242 tree decl;
3243
3244 decl = cp_parser_lookup_name_simple (parser, token->value);
3245 if (TREE_CODE (decl) == TEMPLATE_DECL)
3246 error ("`%D' used without template parameters",
3247 decl);
3248 else if (parser->scope)
3249 {
3250 if (TYPE_P (parser->scope))
3251 error ("`%T::%D' is not a class-name or "
3252 "namespace-name",
3253 parser->scope, token->value);
3254 else
3255 error ("`%D::%D' is not a class-name or "
3256 "namespace-name",
3257 parser->scope, token->value);
3258 }
3259 else
3260 error ("`%D' is not a class-name or namespace-name",
3261 token->value);
3262 parser->scope = NULL_TREE;
3263 error_p = true;
3264 /* Treat this as a successful nested-name-specifier
3265 due to:
3266
3267 [basic.lookup.qual]
3268
3269 If the name found is not a class-name (clause
3270 _class_) or namespace-name (_namespace.def_), the
3271 program is ill-formed. */
3272 success = true;
3273 }
3274 cp_lexer_consume_token (parser->lexer);
3275 }
3276 break;
3277 }
3278
3279 /* We've found one valid nested-name-specifier. */
3280 success = true;
3281 /* Make sure we look in the right scope the next time through
3282 the loop. */
3283 parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3284 ? TREE_TYPE (new_scope)
3285 : new_scope);
3286 /* If it is a class scope, try to complete it; we are about to
3287 be looking up names inside the class. */
3288 if (TYPE_P (parser->scope)
3289 /* Since checking types for dependency can be expensive,
3290 avoid doing it if the type is already complete. */
3291 && !COMPLETE_TYPE_P (parser->scope)
3292 /* Do not try to complete dependent types. */
3293 && !dependent_type_p (parser->scope))
3294 complete_type (parser->scope);
3295 }
3296
3297 /* Retrieve any deferred checks. Do not pop this access checks yet
3298 so the memory will not be reclaimed during token replacing below. */
3299 access_check = get_deferred_access_checks ();
3300
3301 /* If parsing tentatively, replace the sequence of tokens that makes
3302 up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3303 token. That way, should we re-parse the token stream, we will
3304 not have to repeat the effort required to do the parse, nor will
3305 we issue duplicate error messages. */
3306 if (success && start >= 0)
3307 {
3308 /* Find the token that corresponds to the start of the
3309 template-id. */
3310 token = cp_lexer_advance_token (parser->lexer,
3311 parser->lexer->first_token,
3312 start);
3313
3314 /* Reset the contents of the START token. */
3315 token->type = CPP_NESTED_NAME_SPECIFIER;
3316 token->value = build_tree_list (access_check, parser->scope);
3317 TREE_TYPE (token->value) = parser->qualifying_scope;
3318 token->keyword = RID_MAX;
3319 /* Purge all subsequent tokens. */
3320 cp_lexer_purge_tokens_after (parser->lexer, token);
3321 }
3322
3323 pop_deferring_access_checks ();
3324 return success ? parser->scope : NULL_TREE;
3325 }
3326
3327 /* Parse a nested-name-specifier. See
3328 cp_parser_nested_name_specifier_opt for details. This function
3329 behaves identically, except that it will an issue an error if no
3330 nested-name-specifier is present, and it will return
3331 ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3332 is present. */
3333
3334 static tree
3335 cp_parser_nested_name_specifier (cp_parser *parser,
3336 bool typename_keyword_p,
3337 bool check_dependency_p,
3338 bool type_p)
3339 {
3340 tree scope;
3341
3342 /* Look for the nested-name-specifier. */
3343 scope = cp_parser_nested_name_specifier_opt (parser,
3344 typename_keyword_p,
3345 check_dependency_p,
3346 type_p);
3347 /* If it was not present, issue an error message. */
3348 if (!scope)
3349 {
3350 cp_parser_error (parser, "expected nested-name-specifier");
3351 return error_mark_node;
3352 }
3353
3354 return scope;
3355 }
3356
3357 /* Parse a class-or-namespace-name.
3358
3359 class-or-namespace-name:
3360 class-name
3361 namespace-name
3362
3363 TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3364 TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3365 CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3366 TYPE_P is TRUE iff the next name should be taken as a class-name,
3367 even the same name is declared to be another entity in the same
3368 scope.
3369
3370 Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3371 specified by the class-or-namespace-name. If neither is found the
3372 ERROR_MARK_NODE is returned. */
3373
3374 static tree
3375 cp_parser_class_or_namespace_name (cp_parser *parser,
3376 bool typename_keyword_p,
3377 bool template_keyword_p,
3378 bool check_dependency_p,
3379 bool type_p)
3380 {
3381 tree saved_scope;
3382 tree saved_qualifying_scope;
3383 tree saved_object_scope;
3384 tree scope;
3385 bool only_class_p;
3386
3387 /* Before we try to parse the class-name, we must save away the
3388 current PARSER->SCOPE since cp_parser_class_name will destroy
3389 it. */
3390 saved_scope = parser->scope;
3391 saved_qualifying_scope = parser->qualifying_scope;
3392 saved_object_scope = parser->object_scope;
3393 /* Try for a class-name first. If the SAVED_SCOPE is a type, then
3394 there is no need to look for a namespace-name. */
3395 only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3396 if (!only_class_p)
3397 cp_parser_parse_tentatively (parser);
3398 scope = cp_parser_class_name (parser,
3399 typename_keyword_p,
3400 template_keyword_p,
3401 type_p,
3402 check_dependency_p,
3403 /*class_head_p=*/false);
3404 /* If that didn't work, try for a namespace-name. */
3405 if (!only_class_p && !cp_parser_parse_definitely (parser))
3406 {
3407 /* Restore the saved scope. */
3408 parser->scope = saved_scope;
3409 parser->qualifying_scope = saved_qualifying_scope;
3410 parser->object_scope = saved_object_scope;
3411 /* If we are not looking at an identifier followed by the scope
3412 resolution operator, then this is not part of a
3413 nested-name-specifier. (Note that this function is only used
3414 to parse the components of a nested-name-specifier.) */
3415 if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3416 || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3417 return error_mark_node;
3418 scope = cp_parser_namespace_name (parser);
3419 }
3420
3421 return scope;
3422 }
3423
3424 /* Parse a postfix-expression.
3425
3426 postfix-expression:
3427 primary-expression
3428 postfix-expression [ expression ]
3429 postfix-expression ( expression-list [opt] )
3430 simple-type-specifier ( expression-list [opt] )
3431 typename :: [opt] nested-name-specifier identifier
3432 ( expression-list [opt] )
3433 typename :: [opt] nested-name-specifier template [opt] template-id
3434 ( expression-list [opt] )
3435 postfix-expression . template [opt] id-expression
3436 postfix-expression -> template [opt] id-expression
3437 postfix-expression . pseudo-destructor-name
3438 postfix-expression -> pseudo-destructor-name
3439 postfix-expression ++
3440 postfix-expression --
3441 dynamic_cast < type-id > ( expression )
3442 static_cast < type-id > ( expression )
3443 reinterpret_cast < type-id > ( expression )
3444 const_cast < type-id > ( expression )
3445 typeid ( expression )
3446 typeid ( type-id )
3447
3448 GNU Extension:
3449
3450 postfix-expression:
3451 ( type-id ) { initializer-list , [opt] }
3452
3453 This extension is a GNU version of the C99 compound-literal
3454 construct. (The C99 grammar uses `type-name' instead of `type-id',
3455 but they are essentially the same concept.)
3456
3457 If ADDRESS_P is true, the postfix expression is the operand of the
3458 `&' operator.
3459
3460 Returns a representation of the expression. */
3461
3462 static tree
3463 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3464 {
3465 cp_token *token;
3466 enum rid keyword;
3467 cp_parser_id_kind idk = CP_PARSER_ID_KIND_NONE;
3468 tree postfix_expression = NULL_TREE;
3469 /* Non-NULL only if the current postfix-expression can be used to
3470 form a pointer-to-member. In that case, QUALIFYING_CLASS is the
3471 class used to qualify the member. */
3472 tree qualifying_class = NULL_TREE;
3473
3474 /* Peek at the next token. */
3475 token = cp_lexer_peek_token (parser->lexer);
3476 /* Some of the productions are determined by keywords. */
3477 keyword = token->keyword;
3478 switch (keyword)
3479 {
3480 case RID_DYNCAST:
3481 case RID_STATCAST:
3482 case RID_REINTCAST:
3483 case RID_CONSTCAST:
3484 {
3485 tree type;
3486 tree expression;
3487 const char *saved_message;
3488
3489 /* All of these can be handled in the same way from the point
3490 of view of parsing. Begin by consuming the token
3491 identifying the cast. */
3492 cp_lexer_consume_token (parser->lexer);
3493
3494 /* New types cannot be defined in the cast. */
3495 saved_message = parser->type_definition_forbidden_message;
3496 parser->type_definition_forbidden_message
3497 = "types may not be defined in casts";
3498
3499 /* Look for the opening `<'. */
3500 cp_parser_require (parser, CPP_LESS, "`<'");
3501 /* Parse the type to which we are casting. */
3502 type = cp_parser_type_id (parser);
3503 /* Look for the closing `>'. */
3504 cp_parser_require (parser, CPP_GREATER, "`>'");
3505 /* Restore the old message. */
3506 parser->type_definition_forbidden_message = saved_message;
3507
3508 /* And the expression which is being cast. */
3509 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3510 expression = cp_parser_expression (parser);
3511 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3512
3513 /* Only type conversions to integral or enumeration types
3514 can be used in constant-expressions. */
3515 if (parser->constant_expression_p
3516 && !dependent_type_p (type)
3517 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
3518 {
3519 if (!parser->allow_non_constant_expression_p)
3520 return (cp_parser_non_constant_expression
3521 ("a cast to a type other than an integral or "
3522 "enumeration type"));
3523 parser->non_constant_expression_p = true;
3524 }
3525
3526 switch (keyword)
3527 {
3528 case RID_DYNCAST:
3529 postfix_expression
3530 = build_dynamic_cast (type, expression);
3531 break;
3532 case RID_STATCAST:
3533 postfix_expression
3534 = build_static_cast (type, expression);
3535 break;
3536 case RID_REINTCAST:
3537 postfix_expression
3538 = build_reinterpret_cast (type, expression);
3539 break;
3540 case RID_CONSTCAST:
3541 postfix_expression
3542 = build_const_cast (type, expression);
3543 break;
3544 default:
3545 abort ();
3546 }
3547 }
3548 break;
3549
3550 case RID_TYPEID:
3551 {
3552 tree type;
3553 const char *saved_message;
3554
3555 /* Consume the `typeid' token. */
3556 cp_lexer_consume_token (parser->lexer);
3557 /* Look for the `(' token. */
3558 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3559 /* Types cannot be defined in a `typeid' expression. */
3560 saved_message = parser->type_definition_forbidden_message;
3561 parser->type_definition_forbidden_message
3562 = "types may not be defined in a `typeid\' expression";
3563 /* We can't be sure yet whether we're looking at a type-id or an
3564 expression. */
3565 cp_parser_parse_tentatively (parser);
3566 /* Try a type-id first. */
3567 type = cp_parser_type_id (parser);
3568 /* Look for the `)' token. Otherwise, we can't be sure that
3569 we're not looking at an expression: consider `typeid (int
3570 (3))', for example. */
3571 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3572 /* If all went well, simply lookup the type-id. */
3573 if (cp_parser_parse_definitely (parser))
3574 postfix_expression = get_typeid (type);
3575 /* Otherwise, fall back to the expression variant. */
3576 else
3577 {
3578 tree expression;
3579
3580 /* Look for an expression. */
3581 expression = cp_parser_expression (parser);
3582 /* Compute its typeid. */
3583 postfix_expression = build_typeid (expression);
3584 /* Look for the `)' token. */
3585 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3586 }
3587
3588 /* Restore the saved message. */
3589 parser->type_definition_forbidden_message = saved_message;
3590 }
3591 break;
3592
3593 case RID_TYPENAME:
3594 {
3595 bool template_p = false;
3596 tree id;
3597 tree type;
3598
3599 /* Consume the `typename' token. */
3600 cp_lexer_consume_token (parser->lexer);
3601 /* Look for the optional `::' operator. */
3602 cp_parser_global_scope_opt (parser,
3603 /*current_scope_valid_p=*/false);
3604 /* Look for the nested-name-specifier. */
3605 cp_parser_nested_name_specifier (parser,
3606 /*typename_keyword_p=*/true,
3607 /*check_dependency_p=*/true,
3608 /*type_p=*/true);
3609 /* Look for the optional `template' keyword. */
3610 template_p = cp_parser_optional_template_keyword (parser);
3611 /* We don't know whether we're looking at a template-id or an
3612 identifier. */
3613 cp_parser_parse_tentatively (parser);
3614 /* Try a template-id. */
3615 id = cp_parser_template_id (parser, template_p,
3616 /*check_dependency_p=*/true);
3617 /* If that didn't work, try an identifier. */
3618 if (!cp_parser_parse_definitely (parser))
3619 id = cp_parser_identifier (parser);
3620 /* Create a TYPENAME_TYPE to represent the type to which the
3621 functional cast is being performed. */
3622 type = make_typename_type (parser->scope, id,
3623 /*complain=*/1);
3624
3625 postfix_expression = cp_parser_functional_cast (parser, type);
3626 }
3627 break;
3628
3629 default:
3630 {
3631 tree type;
3632
3633 /* If the next thing is a simple-type-specifier, we may be
3634 looking at a functional cast. We could also be looking at
3635 an id-expression. So, we try the functional cast, and if
3636 that doesn't work we fall back to the primary-expression. */
3637 cp_parser_parse_tentatively (parser);
3638 /* Look for the simple-type-specifier. */
3639 type = cp_parser_simple_type_specifier (parser,
3640 CP_PARSER_FLAGS_NONE);
3641 /* Parse the cast itself. */
3642 if (!cp_parser_error_occurred (parser))
3643 postfix_expression
3644 = cp_parser_functional_cast (parser, type);
3645 /* If that worked, we're done. */
3646 if (cp_parser_parse_definitely (parser))
3647 break;
3648
3649 /* If the functional-cast didn't work out, try a
3650 compound-literal. */
3651 if (cp_parser_allow_gnu_extensions_p (parser)
3652 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3653 {
3654 tree initializer_list = NULL_TREE;
3655
3656 cp_parser_parse_tentatively (parser);
3657 /* Consume the `('. */
3658 cp_lexer_consume_token (parser->lexer);
3659 /* Parse the type. */
3660 type = cp_parser_type_id (parser);
3661 /* Look for the `)'. */
3662 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3663 /* Look for the `{'. */
3664 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3665 /* If things aren't going well, there's no need to
3666 keep going. */
3667 if (!cp_parser_error_occurred (parser))
3668 {
3669 /* Parse the initializer-list. */
3670 initializer_list
3671 = cp_parser_initializer_list (parser);
3672 /* Allow a trailing `,'. */
3673 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3674 cp_lexer_consume_token (parser->lexer);
3675 /* Look for the final `}'. */
3676 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3677 }
3678 /* If that worked, we're definitely looking at a
3679 compound-literal expression. */
3680 if (cp_parser_parse_definitely (parser))
3681 {
3682 /* Warn the user that a compound literal is not
3683 allowed in standard C++. */
3684 if (pedantic)
3685 pedwarn ("ISO C++ forbids compound-literals");
3686 /* Form the representation of the compound-literal. */
3687 postfix_expression
3688 = finish_compound_literal (type, initializer_list);
3689 break;
3690 }
3691 }
3692
3693 /* It must be a primary-expression. */
3694 postfix_expression = cp_parser_primary_expression (parser,
3695 &idk,
3696 &qualifying_class);
3697 }
3698 break;
3699 }
3700
3701 /* If we were avoiding committing to the processing of a
3702 qualified-id until we knew whether or not we had a
3703 pointer-to-member, we now know. */
3704 if (qualifying_class)
3705 {
3706 bool done;
3707
3708 /* Peek at the next token. */
3709 token = cp_lexer_peek_token (parser->lexer);
3710 done = (token->type != CPP_OPEN_SQUARE
3711 && token->type != CPP_OPEN_PAREN
3712 && token->type != CPP_DOT
3713 && token->type != CPP_DEREF
3714 && token->type != CPP_PLUS_PLUS
3715 && token->type != CPP_MINUS_MINUS);
3716
3717 postfix_expression = finish_qualified_id_expr (qualifying_class,
3718 postfix_expression,
3719 done,
3720 address_p);
3721 if (done)
3722 return postfix_expression;
3723 }
3724
3725 /* Remember that there was a reference to this entity. */
3726 if (DECL_P (postfix_expression))
3727 mark_used (postfix_expression);
3728
3729 /* Keep looping until the postfix-expression is complete. */
3730 while (true)
3731 {
3732 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3733 && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3734 {
3735 /* It is not a Koenig lookup function call. */
3736 unqualified_name_lookup_error (postfix_expression);
3737 postfix_expression = error_mark_node;
3738 }
3739
3740 /* Peek at the next token. */
3741 token = cp_lexer_peek_token (parser->lexer);
3742
3743 switch (token->type)
3744 {
3745 case CPP_OPEN_SQUARE:
3746 /* postfix-expression [ expression ] */
3747 {
3748 tree index;
3749
3750 /* Consume the `[' token. */
3751 cp_lexer_consume_token (parser->lexer);
3752 /* Parse the index expression. */
3753 index = cp_parser_expression (parser);
3754 /* Look for the closing `]'. */
3755 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3756
3757 /* Build the ARRAY_REF. */
3758 postfix_expression
3759 = grok_array_decl (postfix_expression, index);
3760 idk = CP_PARSER_ID_KIND_NONE;
3761 }
3762 break;
3763
3764 case CPP_OPEN_PAREN:
3765 /* postfix-expression ( expression-list [opt] ) */
3766 {
3767 tree args;
3768
3769 /* Consume the `(' token. */
3770 cp_lexer_consume_token (parser->lexer);
3771 /* If the next token is not a `)', then there are some
3772 arguments. */
3773 if (cp_lexer_next_token_is_not (parser->lexer,
3774 CPP_CLOSE_PAREN))
3775 args = cp_parser_expression_list (parser);
3776 else
3777 args = NULL_TREE;
3778 /* Look for the closing `)'. */
3779 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3780 /* Function calls are not permitted in
3781 constant-expressions. */
3782 if (parser->constant_expression_p)
3783 {
3784 if (!parser->allow_non_constant_expression_p)
3785 return cp_parser_non_constant_expression ("a function call");
3786 parser->non_constant_expression_p = true;
3787 }
3788
3789 if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3790 && (is_overloaded_fn (postfix_expression)
3791 || DECL_P (postfix_expression)
3792 || TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3793 && args)
3794 {
3795 tree arg;
3796 tree identifier = NULL_TREE;
3797 tree functions = NULL_TREE;
3798
3799 /* Find the name of the overloaded function. */
3800 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3801 identifier = postfix_expression;
3802 else if (is_overloaded_fn (postfix_expression))
3803 {
3804 functions = postfix_expression;
3805 identifier = DECL_NAME (get_first_fn (functions));
3806 }
3807 else if (DECL_P (postfix_expression))
3808 {
3809 functions = postfix_expression;
3810 identifier = DECL_NAME (postfix_expression);
3811 }
3812
3813 /* A call to a namespace-scope function using an
3814 unqualified name.
3815
3816 Do Koenig lookup -- unless any of the arguments are
3817 type-dependent. */
3818 for (arg = args; arg; arg = TREE_CHAIN (arg))
3819 if (type_dependent_expression_p (TREE_VALUE (arg)))
3820 break;
3821 if (!arg)
3822 {
3823 postfix_expression
3824 = lookup_arg_dependent (identifier, functions, args);
3825 if (!postfix_expression)
3826 {
3827 /* The unqualified name could not be resolved. */
3828 unqualified_name_lookup_error (identifier);
3829 postfix_expression = error_mark_node;
3830 }
3831 postfix_expression
3832 = build_call_from_tree (postfix_expression, args,
3833 /*diallow_virtual=*/false);
3834 break;
3835 }
3836 postfix_expression = build_min_nt (LOOKUP_EXPR,
3837 identifier);
3838 }
3839 else if (idk == CP_PARSER_ID_KIND_UNQUALIFIED
3840 && TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3841 {
3842 /* The unqualified name could not be resolved. */
3843 unqualified_name_lookup_error (postfix_expression);
3844 postfix_expression = error_mark_node;
3845 break;
3846 }
3847
3848 /* In the body of a template, no further processing is
3849 required. */
3850 if (processing_template_decl)
3851 {
3852 postfix_expression = build_nt (CALL_EXPR,
3853 postfix_expression,
3854 args);
3855 break;
3856 }
3857
3858 if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3859 postfix_expression
3860 = (build_new_method_call
3861 (TREE_OPERAND (postfix_expression, 0),
3862 TREE_OPERAND (postfix_expression, 1),
3863 args, NULL_TREE,
3864 (idk == CP_PARSER_ID_KIND_QUALIFIED
3865 ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3866 else if (TREE_CODE (postfix_expression) == OFFSET_REF)
3867 postfix_expression = (build_offset_ref_call_from_tree
3868 (postfix_expression, args));
3869 else if (idk == CP_PARSER_ID_KIND_QUALIFIED)
3870 /* A call to a static class member, or a namespace-scope
3871 function. */
3872 postfix_expression
3873 = finish_call_expr (postfix_expression, args,
3874 /*disallow_virtual=*/true);
3875 else
3876 /* All other function calls. */
3877 postfix_expression
3878 = finish_call_expr (postfix_expression, args,
3879 /*disallow_virtual=*/false);
3880
3881 /* The POSTFIX_EXPRESSION is certainly no longer an id. */
3882 idk = CP_PARSER_ID_KIND_NONE;
3883 }
3884 break;
3885
3886 case CPP_DOT:
3887 case CPP_DEREF:
3888 /* postfix-expression . template [opt] id-expression
3889 postfix-expression . pseudo-destructor-name
3890 postfix-expression -> template [opt] id-expression
3891 postfix-expression -> pseudo-destructor-name */
3892 {
3893 tree name;
3894 bool dependent_p;
3895 bool template_p;
3896 tree scope = NULL_TREE;
3897
3898 /* If this is a `->' operator, dereference the pointer. */
3899 if (token->type == CPP_DEREF)
3900 postfix_expression = build_x_arrow (postfix_expression);
3901 /* Check to see whether or not the expression is
3902 type-dependent. */
3903 dependent_p = type_dependent_expression_p (postfix_expression);
3904 /* The identifier following the `->' or `.' is not
3905 qualified. */
3906 parser->scope = NULL_TREE;
3907 parser->qualifying_scope = NULL_TREE;
3908 parser->object_scope = NULL_TREE;
3909 idk = CP_PARSER_ID_KIND_NONE;
3910 /* Enter the scope corresponding to the type of the object
3911 given by the POSTFIX_EXPRESSION. */
3912 if (!dependent_p
3913 && TREE_TYPE (postfix_expression) != NULL_TREE)
3914 {
3915 scope = TREE_TYPE (postfix_expression);
3916 /* According to the standard, no expression should
3917 ever have reference type. Unfortunately, we do not
3918 currently match the standard in this respect in
3919 that our internal representation of an expression
3920 may have reference type even when the standard says
3921 it does not. Therefore, we have to manually obtain
3922 the underlying type here. */
3923 scope = non_reference (scope);
3924 /* If the SCOPE is an OFFSET_TYPE, then we grab the
3925 type of the field. We get an OFFSET_TYPE for
3926 something like:
3927
3928 S::T.a ...
3929
3930 Probably, we should not get an OFFSET_TYPE here;
3931 that transformation should be made only if `&S::T'
3932 is written. */
3933 if (TREE_CODE (scope) == OFFSET_TYPE)
3934 scope = TREE_TYPE (scope);
3935 /* The type of the POSTFIX_EXPRESSION must be
3936 complete. */
3937 scope = complete_type_or_else (scope, NULL_TREE);
3938 /* Let the name lookup machinery know that we are
3939 processing a class member access expression. */
3940 parser->context->object_type = scope;
3941 /* If something went wrong, we want to be able to
3942 discern that case, as opposed to the case where
3943 there was no SCOPE due to the type of expression
3944 being dependent. */
3945 if (!scope)
3946 scope = error_mark_node;
3947 }
3948
3949 /* Consume the `.' or `->' operator. */
3950 cp_lexer_consume_token (parser->lexer);
3951 /* If the SCOPE is not a scalar type, we are looking at an
3952 ordinary class member access expression, rather than a
3953 pseudo-destructor-name. */
3954 if (!scope || !SCALAR_TYPE_P (scope))
3955 {
3956 template_p = cp_parser_optional_template_keyword (parser);
3957 /* Parse the id-expression. */
3958 name = cp_parser_id_expression (parser,
3959 template_p,
3960 /*check_dependency_p=*/true,
3961 /*template_p=*/NULL);
3962 /* In general, build a SCOPE_REF if the member name is
3963 qualified. However, if the name was not dependent
3964 and has already been resolved; there is no need to
3965 build the SCOPE_REF. For example;
3966
3967 struct X { void f(); };
3968 template <typename T> void f(T* t) { t->X::f(); }
3969
3970 Even though "t" is dependent, "X::f" is not and has
3971 except that for a BASELINK there is no need to
3972 include scope information. */
3973
3974 /* But we do need to remember that there was an explicit
3975 scope for virtual function calls. */
3976 if (parser->scope)
3977 idk = CP_PARSER_ID_KIND_QUALIFIED;
3978
3979 if (name != error_mark_node
3980 && !BASELINK_P (name)
3981 && parser->scope)
3982 {
3983 name = build_nt (SCOPE_REF, parser->scope, name);
3984 parser->scope = NULL_TREE;
3985 parser->qualifying_scope = NULL_TREE;
3986 parser->object_scope = NULL_TREE;
3987 }
3988 postfix_expression
3989 = finish_class_member_access_expr (postfix_expression, name);
3990 }
3991 /* Otherwise, try the pseudo-destructor-name production. */
3992 else
3993 {
3994 tree s;
3995 tree type;
3996
3997 /* Parse the pseudo-destructor-name. */
3998 cp_parser_pseudo_destructor_name (parser, &s, &type);
3999 /* Form the call. */
4000 postfix_expression
4001 = finish_pseudo_destructor_expr (postfix_expression,
4002 s, TREE_TYPE (type));
4003 }
4004
4005 /* We no longer need to look up names in the scope of the
4006 object on the left-hand side of the `.' or `->'
4007 operator. */
4008 parser->context->object_type = NULL_TREE;
4009 }
4010 break;
4011
4012 case CPP_PLUS_PLUS:
4013 /* postfix-expression ++ */
4014 /* Consume the `++' token. */
4015 cp_lexer_consume_token (parser->lexer);
4016 /* Increments may not appear in constant-expressions. */
4017 if (parser->constant_expression_p)
4018 {
4019 if (!parser->allow_non_constant_expression_p)
4020 return cp_parser_non_constant_expression ("an increment");
4021 parser->non_constant_expression_p = true;
4022 }
4023 /* Generate a representation for the complete expression. */
4024 postfix_expression
4025 = finish_increment_expr (postfix_expression,
4026 POSTINCREMENT_EXPR);
4027 idk = CP_PARSER_ID_KIND_NONE;
4028 break;
4029
4030 case CPP_MINUS_MINUS:
4031 /* postfix-expression -- */
4032 /* Consume the `--' token. */
4033 cp_lexer_consume_token (parser->lexer);
4034 /* Decrements may not appear in constant-expressions. */
4035 if (parser->constant_expression_p)
4036 {
4037 if (!parser->allow_non_constant_expression_p)
4038 return cp_parser_non_constant_expression ("a decrement");
4039 parser->non_constant_expression_p = true;
4040 }
4041 /* Generate a representation for the complete expression. */
4042 postfix_expression
4043 = finish_increment_expr (postfix_expression,
4044 POSTDECREMENT_EXPR);
4045 idk = CP_PARSER_ID_KIND_NONE;
4046 break;
4047
4048 default:
4049 return postfix_expression;
4050 }
4051 }
4052
4053 /* We should never get here. */
4054 abort ();
4055 return error_mark_node;
4056 }
4057
4058 /* Parse an expression-list.
4059
4060 expression-list:
4061 assignment-expression
4062 expression-list, assignment-expression
4063
4064 Returns a TREE_LIST. The TREE_VALUE of each node is a
4065 representation of an assignment-expression. Note that a TREE_LIST
4066 is returned even if there is only a single expression in the list. */
4067
4068 static tree
4069 cp_parser_expression_list (cp_parser* parser)
4070 {
4071 tree expression_list = NULL_TREE;
4072
4073 /* Consume expressions until there are no more. */
4074 while (true)
4075 {
4076 tree expr;
4077
4078 /* Parse the next assignment-expression. */
4079 expr = cp_parser_assignment_expression (parser);
4080 /* Add it to the list. */
4081 expression_list = tree_cons (NULL_TREE, expr, expression_list);
4082
4083 /* If the next token isn't a `,', then we are done. */
4084 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4085 {
4086 /* All uses of expression-list in the grammar are followed
4087 by a `)'. Therefore, if the next token is not a `)' an
4088 error will be issued, unless we are parsing tentatively.
4089 Skip ahead to see if there is another `,' before the `)';
4090 if so, we can go there and recover. */
4091 if (cp_parser_parsing_tentatively (parser)
4092 || cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
4093 || !cp_parser_skip_to_closing_parenthesis_or_comma (parser))
4094 break;
4095 }
4096
4097 /* Otherwise, consume the `,' and keep going. */
4098 cp_lexer_consume_token (parser->lexer);
4099 }
4100
4101 /* We built up the list in reverse order so we must reverse it now. */
4102 return nreverse (expression_list);
4103 }
4104
4105 /* Parse a pseudo-destructor-name.
4106
4107 pseudo-destructor-name:
4108 :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4109 :: [opt] nested-name-specifier template template-id :: ~ type-name
4110 :: [opt] nested-name-specifier [opt] ~ type-name
4111
4112 If either of the first two productions is used, sets *SCOPE to the
4113 TYPE specified before the final `::'. Otherwise, *SCOPE is set to
4114 NULL_TREE. *TYPE is set to the TYPE_DECL for the final type-name,
4115 or ERROR_MARK_NODE if no type-name is present. */
4116
4117 static void
4118 cp_parser_pseudo_destructor_name (cp_parser* parser,
4119 tree* scope,
4120 tree* type)
4121 {
4122 bool nested_name_specifier_p;
4123
4124 /* Look for the optional `::' operator. */
4125 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4126 /* Look for the optional nested-name-specifier. */
4127 nested_name_specifier_p
4128 = (cp_parser_nested_name_specifier_opt (parser,
4129 /*typename_keyword_p=*/false,
4130 /*check_dependency_p=*/true,
4131 /*type_p=*/false)
4132 != NULL_TREE);
4133 /* Now, if we saw a nested-name-specifier, we might be doing the
4134 second production. */
4135 if (nested_name_specifier_p
4136 && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4137 {
4138 /* Consume the `template' keyword. */
4139 cp_lexer_consume_token (parser->lexer);
4140 /* Parse the template-id. */
4141 cp_parser_template_id (parser,
4142 /*template_keyword_p=*/true,
4143 /*check_dependency_p=*/false);
4144 /* Look for the `::' token. */
4145 cp_parser_require (parser, CPP_SCOPE, "`::'");
4146 }
4147 /* If the next token is not a `~', then there might be some
4148 additional qualification. */
4149 else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4150 {
4151 /* Look for the type-name. */
4152 *scope = TREE_TYPE (cp_parser_type_name (parser));
4153 /* Look for the `::' token. */
4154 cp_parser_require (parser, CPP_SCOPE, "`::'");
4155 }
4156 else
4157 *scope = NULL_TREE;
4158
4159 /* Look for the `~'. */
4160 cp_parser_require (parser, CPP_COMPL, "`~'");
4161 /* Look for the type-name again. We are not responsible for
4162 checking that it matches the first type-name. */
4163 *type = cp_parser_type_name (parser);
4164 }
4165
4166 /* Parse a unary-expression.
4167
4168 unary-expression:
4169 postfix-expression
4170 ++ cast-expression
4171 -- cast-expression
4172 unary-operator cast-expression
4173 sizeof unary-expression
4174 sizeof ( type-id )
4175 new-expression
4176 delete-expression
4177
4178 GNU Extensions:
4179
4180 unary-expression:
4181 __extension__ cast-expression
4182 __alignof__ unary-expression
4183 __alignof__ ( type-id )
4184 __real__ cast-expression
4185 __imag__ cast-expression
4186 && identifier
4187
4188 ADDRESS_P is true iff the unary-expression is appearing as the
4189 operand of the `&' operator.
4190
4191 Returns a representation of the expression. */
4192
4193 static tree
4194 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4195 {
4196 cp_token *token;
4197 enum tree_code unary_operator;
4198
4199 /* Peek at the next token. */
4200 token = cp_lexer_peek_token (parser->lexer);
4201 /* Some keywords give away the kind of expression. */
4202 if (token->type == CPP_KEYWORD)
4203 {
4204 enum rid keyword = token->keyword;
4205
4206 switch (keyword)
4207 {
4208 case RID_ALIGNOF:
4209 {
4210 /* Consume the `alignof' token. */
4211 cp_lexer_consume_token (parser->lexer);
4212 /* Parse the operand. */
4213 return finish_alignof (cp_parser_sizeof_operand
4214 (parser, keyword));
4215 }
4216
4217 case RID_SIZEOF:
4218 {
4219 tree operand;
4220
4221 /* Consume the `sizeof' token. */
4222 cp_lexer_consume_token (parser->lexer);
4223 /* Parse the operand. */
4224 operand = cp_parser_sizeof_operand (parser, keyword);
4225
4226 /* If the type of the operand cannot be determined build a
4227 SIZEOF_EXPR. */
4228 if (TYPE_P (operand)
4229 ? dependent_type_p (operand)
4230 : type_dependent_expression_p (operand))
4231 return build_min (SIZEOF_EXPR, size_type_node, operand);
4232 /* Otherwise, compute the constant value. */
4233 else
4234 return finish_sizeof (operand);
4235 }
4236
4237 case RID_NEW:
4238 return cp_parser_new_expression (parser);
4239
4240 case RID_DELETE:
4241 return cp_parser_delete_expression (parser);
4242
4243 case RID_EXTENSION:
4244 {
4245 /* The saved value of the PEDANTIC flag. */
4246 int saved_pedantic;
4247 tree expr;
4248
4249 /* Save away the PEDANTIC flag. */
4250 cp_parser_extension_opt (parser, &saved_pedantic);
4251 /* Parse the cast-expression. */
4252 expr = cp_parser_simple_cast_expression (parser);
4253 /* Restore the PEDANTIC flag. */
4254 pedantic = saved_pedantic;
4255
4256 return expr;
4257 }
4258
4259 case RID_REALPART:
4260 case RID_IMAGPART:
4261 {
4262 tree expression;
4263
4264 /* Consume the `__real__' or `__imag__' token. */
4265 cp_lexer_consume_token (parser->lexer);
4266 /* Parse the cast-expression. */
4267 expression = cp_parser_simple_cast_expression (parser);
4268 /* Create the complete representation. */
4269 return build_x_unary_op ((keyword == RID_REALPART
4270 ? REALPART_EXPR : IMAGPART_EXPR),
4271 expression);
4272 }
4273 break;
4274
4275 default:
4276 break;
4277 }
4278 }
4279
4280 /* Look for the `:: new' and `:: delete', which also signal the
4281 beginning of a new-expression, or delete-expression,
4282 respectively. If the next token is `::', then it might be one of
4283 these. */
4284 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4285 {
4286 enum rid keyword;
4287
4288 /* See if the token after the `::' is one of the keywords in
4289 which we're interested. */
4290 keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4291 /* If it's `new', we have a new-expression. */
4292 if (keyword == RID_NEW)
4293 return cp_parser_new_expression (parser);
4294 /* Similarly, for `delete'. */
4295 else if (keyword == RID_DELETE)
4296 return cp_parser_delete_expression (parser);
4297 }
4298
4299 /* Look for a unary operator. */
4300 unary_operator = cp_parser_unary_operator (token);
4301 /* The `++' and `--' operators can be handled similarly, even though
4302 they are not technically unary-operators in the grammar. */
4303 if (unary_operator == ERROR_MARK)
4304 {
4305 if (token->type == CPP_PLUS_PLUS)
4306 unary_operator = PREINCREMENT_EXPR;
4307 else if (token->type == CPP_MINUS_MINUS)
4308 unary_operator = PREDECREMENT_EXPR;
4309 /* Handle the GNU address-of-label extension. */
4310 else if (cp_parser_allow_gnu_extensions_p (parser)
4311 && token->type == CPP_AND_AND)
4312 {
4313 tree identifier;
4314
4315 /* Consume the '&&' token. */
4316 cp_lexer_consume_token (parser->lexer);
4317 /* Look for the identifier. */
4318 identifier = cp_parser_identifier (parser);
4319 /* Create an expression representing the address. */
4320 return finish_label_address_expr (identifier);
4321 }
4322 }
4323 if (unary_operator != ERROR_MARK)
4324 {
4325 tree cast_expression;
4326
4327 /* Consume the operator token. */
4328 token = cp_lexer_consume_token (parser->lexer);
4329 /* Parse the cast-expression. */
4330 cast_expression
4331 = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4332 /* Now, build an appropriate representation. */
4333 switch (unary_operator)
4334 {
4335 case INDIRECT_REF:
4336 return build_x_indirect_ref (cast_expression, "unary *");
4337
4338 case ADDR_EXPR:
4339 return build_x_unary_op (ADDR_EXPR, cast_expression);
4340
4341 case PREINCREMENT_EXPR:
4342 case PREDECREMENT_EXPR:
4343 if (parser->constant_expression_p)
4344 {
4345 if (!parser->allow_non_constant_expression_p)
4346 return cp_parser_non_constant_expression (PREINCREMENT_EXPR
4347 ? "an increment"
4348 : "a decrement");
4349 parser->non_constant_expression_p = true;
4350 }
4351 /* Fall through. */
4352 case CONVERT_EXPR:
4353 case NEGATE_EXPR:
4354 case TRUTH_NOT_EXPR:
4355 return finish_unary_op_expr (unary_operator, cast_expression);
4356
4357 case BIT_NOT_EXPR:
4358 return build_x_unary_op (BIT_NOT_EXPR, cast_expression);
4359
4360 default:
4361 abort ();
4362 return error_mark_node;
4363 }
4364 }
4365
4366 return cp_parser_postfix_expression (parser, address_p);
4367 }
4368
4369 /* Returns ERROR_MARK if TOKEN is not a unary-operator. If TOKEN is a
4370 unary-operator, the corresponding tree code is returned. */
4371
4372 static enum tree_code
4373 cp_parser_unary_operator (cp_token* token)
4374 {
4375 switch (token->type)
4376 {
4377 case CPP_MULT:
4378 return INDIRECT_REF;
4379
4380 case CPP_AND:
4381 return ADDR_EXPR;
4382
4383 case CPP_PLUS:
4384 return CONVERT_EXPR;
4385
4386 case CPP_MINUS:
4387 return NEGATE_EXPR;
4388
4389 case CPP_NOT:
4390 return TRUTH_NOT_EXPR;
4391
4392 case CPP_COMPL:
4393 return BIT_NOT_EXPR;
4394
4395 default:
4396 return ERROR_MARK;
4397 }
4398 }
4399
4400 /* Parse a new-expression.
4401
4402 new-expression:
4403 :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4404 :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4405
4406 Returns a representation of the expression. */
4407
4408 static tree
4409 cp_parser_new_expression (cp_parser* parser)
4410 {
4411 bool global_scope_p;
4412 tree placement;
4413 tree type;
4414 tree initializer;
4415
4416 /* Look for the optional `::' operator. */
4417 global_scope_p
4418 = (cp_parser_global_scope_opt (parser,
4419 /*current_scope_valid_p=*/false)
4420 != NULL_TREE);
4421 /* Look for the `new' operator. */
4422 cp_parser_require_keyword (parser, RID_NEW, "`new'");
4423 /* There's no easy way to tell a new-placement from the
4424 `( type-id )' construct. */
4425 cp_parser_parse_tentatively (parser);
4426 /* Look for a new-placement. */
4427 placement = cp_parser_new_placement (parser);
4428 /* If that didn't work out, there's no new-placement. */
4429 if (!cp_parser_parse_definitely (parser))
4430 placement = NULL_TREE;
4431
4432 /* If the next token is a `(', then we have a parenthesized
4433 type-id. */
4434 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4435 {
4436 /* Consume the `('. */
4437 cp_lexer_consume_token (parser->lexer);
4438 /* Parse the type-id. */
4439 type = cp_parser_type_id (parser);
4440 /* Look for the closing `)'. */
4441 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4442 }
4443 /* Otherwise, there must be a new-type-id. */
4444 else
4445 type = cp_parser_new_type_id (parser);
4446
4447 /* If the next token is a `(', then we have a new-initializer. */
4448 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4449 initializer = cp_parser_new_initializer (parser);
4450 else
4451 initializer = NULL_TREE;
4452
4453 /* Create a representation of the new-expression. */
4454 return build_new (placement, type, initializer, global_scope_p);
4455 }
4456
4457 /* Parse a new-placement.
4458
4459 new-placement:
4460 ( expression-list )
4461
4462 Returns the same representation as for an expression-list. */
4463
4464 static tree
4465 cp_parser_new_placement (cp_parser* parser)
4466 {
4467 tree expression_list;
4468
4469 /* Look for the opening `('. */
4470 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4471 return error_mark_node;
4472 /* Parse the expression-list. */
4473 expression_list = cp_parser_expression_list (parser);
4474 /* Look for the closing `)'. */
4475 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4476
4477 return expression_list;
4478 }
4479
4480 /* Parse a new-type-id.
4481
4482 new-type-id:
4483 type-specifier-seq new-declarator [opt]
4484
4485 Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4486 and whose TREE_VALUE is the new-declarator. */
4487
4488 static tree
4489 cp_parser_new_type_id (cp_parser* parser)
4490 {
4491 tree type_specifier_seq;
4492 tree declarator;
4493 const char *saved_message;
4494
4495 /* The type-specifier sequence must not contain type definitions.
4496 (It cannot contain declarations of new types either, but if they
4497 are not definitions we will catch that because they are not
4498 complete.) */
4499 saved_message = parser->type_definition_forbidden_message;
4500 parser->type_definition_forbidden_message
4501 = "types may not be defined in a new-type-id";
4502 /* Parse the type-specifier-seq. */
4503 type_specifier_seq = cp_parser_type_specifier_seq (parser);
4504 /* Restore the old message. */
4505 parser->type_definition_forbidden_message = saved_message;
4506 /* Parse the new-declarator. */
4507 declarator = cp_parser_new_declarator_opt (parser);
4508
4509 return build_tree_list (type_specifier_seq, declarator);
4510 }
4511
4512 /* Parse an (optional) new-declarator.
4513
4514 new-declarator:
4515 ptr-operator new-declarator [opt]
4516 direct-new-declarator
4517
4518 Returns a representation of the declarator. See
4519 cp_parser_declarator for the representations used. */
4520
4521 static tree
4522 cp_parser_new_declarator_opt (cp_parser* parser)
4523 {
4524 enum tree_code code;
4525 tree type;
4526 tree cv_qualifier_seq;
4527
4528 /* We don't know if there's a ptr-operator next, or not. */
4529 cp_parser_parse_tentatively (parser);
4530 /* Look for a ptr-operator. */
4531 code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4532 /* If that worked, look for more new-declarators. */
4533 if (cp_parser_parse_definitely (parser))
4534 {
4535 tree declarator;
4536
4537 /* Parse another optional declarator. */
4538 declarator = cp_parser_new_declarator_opt (parser);
4539
4540 /* Create the representation of the declarator. */
4541 if (code == INDIRECT_REF)
4542 declarator = make_pointer_declarator (cv_qualifier_seq,
4543 declarator);
4544 else
4545 declarator = make_reference_declarator (cv_qualifier_seq,
4546 declarator);
4547
4548 /* Handle the pointer-to-member case. */
4549 if (type)
4550 declarator = build_nt (SCOPE_REF, type, declarator);
4551
4552 return declarator;
4553 }
4554
4555 /* If the next token is a `[', there is a direct-new-declarator. */
4556 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4557 return cp_parser_direct_new_declarator (parser);
4558
4559 return NULL_TREE;
4560 }
4561
4562 /* Parse a direct-new-declarator.
4563
4564 direct-new-declarator:
4565 [ expression ]
4566 direct-new-declarator [constant-expression]
4567
4568 Returns an ARRAY_REF, following the same conventions as are
4569 documented for cp_parser_direct_declarator. */
4570
4571 static tree
4572 cp_parser_direct_new_declarator (cp_parser* parser)
4573 {
4574 tree declarator = NULL_TREE;
4575
4576 while (true)
4577 {
4578 tree expression;
4579
4580 /* Look for the opening `['. */
4581 cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4582 /* The first expression is not required to be constant. */
4583 if (!declarator)
4584 {
4585 expression = cp_parser_expression (parser);
4586 /* The standard requires that the expression have integral
4587 type. DR 74 adds enumeration types. We believe that the
4588 real intent is that these expressions be handled like the
4589 expression in a `switch' condition, which also allows
4590 classes with a single conversion to integral or
4591 enumeration type. */
4592 if (!processing_template_decl)
4593 {
4594 expression
4595 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4596 expression,
4597 /*complain=*/true);
4598 if (!expression)
4599 {
4600 error ("expression in new-declarator must have integral or enumeration type");
4601 expression = error_mark_node;
4602 }
4603 }
4604 }
4605 /* But all the other expressions must be. */
4606 else
4607 expression
4608 = cp_parser_constant_expression (parser,
4609 /*allow_non_constant=*/false,
4610 NULL);
4611 /* Look for the closing `]'. */
4612 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4613
4614 /* Add this bound to the declarator. */
4615 declarator = build_nt (ARRAY_REF, declarator, expression);
4616
4617 /* If the next token is not a `[', then there are no more
4618 bounds. */
4619 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4620 break;
4621 }
4622
4623 return declarator;
4624 }
4625
4626 /* Parse a new-initializer.
4627
4628 new-initializer:
4629 ( expression-list [opt] )
4630
4631 Returns a representation of the expression-list. If there is no
4632 expression-list, VOID_ZERO_NODE is returned. */
4633
4634 static tree
4635 cp_parser_new_initializer (cp_parser* parser)
4636 {
4637 tree expression_list;
4638
4639 /* Look for the opening parenthesis. */
4640 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
4641 /* If the next token is not a `)', then there is an
4642 expression-list. */
4643 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4644 expression_list = cp_parser_expression_list (parser);
4645 else
4646 expression_list = void_zero_node;
4647 /* Look for the closing parenthesis. */
4648 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4649
4650 return expression_list;
4651 }
4652
4653 /* Parse a delete-expression.
4654
4655 delete-expression:
4656 :: [opt] delete cast-expression
4657 :: [opt] delete [ ] cast-expression
4658
4659 Returns a representation of the expression. */
4660
4661 static tree
4662 cp_parser_delete_expression (cp_parser* parser)
4663 {
4664 bool global_scope_p;
4665 bool array_p;
4666 tree expression;
4667
4668 /* Look for the optional `::' operator. */
4669 global_scope_p
4670 = (cp_parser_global_scope_opt (parser,
4671 /*current_scope_valid_p=*/false)
4672 != NULL_TREE);
4673 /* Look for the `delete' keyword. */
4674 cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4675 /* See if the array syntax is in use. */
4676 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4677 {
4678 /* Consume the `[' token. */
4679 cp_lexer_consume_token (parser->lexer);
4680 /* Look for the `]' token. */
4681 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4682 /* Remember that this is the `[]' construct. */
4683 array_p = true;
4684 }
4685 else
4686 array_p = false;
4687
4688 /* Parse the cast-expression. */
4689 expression = cp_parser_simple_cast_expression (parser);
4690
4691 return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4692 }
4693
4694 /* Parse a cast-expression.
4695
4696 cast-expression:
4697 unary-expression
4698 ( type-id ) cast-expression
4699
4700 Returns a representation of the expression. */
4701
4702 static tree
4703 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4704 {
4705 /* If it's a `(', then we might be looking at a cast. */
4706 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4707 {
4708 tree type = NULL_TREE;
4709 tree expr = NULL_TREE;
4710 bool compound_literal_p;
4711 const char *saved_message;
4712
4713 /* There's no way to know yet whether or not this is a cast.
4714 For example, `(int (3))' is a unary-expression, while `(int)
4715 3' is a cast. So, we resort to parsing tentatively. */
4716 cp_parser_parse_tentatively (parser);
4717 /* Types may not be defined in a cast. */
4718 saved_message = parser->type_definition_forbidden_message;
4719 parser->type_definition_forbidden_message
4720 = "types may not be defined in casts";
4721 /* Consume the `('. */
4722 cp_lexer_consume_token (parser->lexer);
4723 /* A very tricky bit is that `(struct S) { 3 }' is a
4724 compound-literal (which we permit in C++ as an extension).
4725 But, that construct is not a cast-expression -- it is a
4726 postfix-expression. (The reason is that `(struct S) { 3 }.i'
4727 is legal; if the compound-literal were a cast-expression,
4728 you'd need an extra set of parentheses.) But, if we parse
4729 the type-id, and it happens to be a class-specifier, then we
4730 will commit to the parse at that point, because we cannot
4731 undo the action that is done when creating a new class. So,
4732 then we cannot back up and do a postfix-expression.
4733
4734 Therefore, we scan ahead to the closing `)', and check to see
4735 if the token after the `)' is a `{'. If so, we are not
4736 looking at a cast-expression.
4737
4738 Save tokens so that we can put them back. */
4739 cp_lexer_save_tokens (parser->lexer);
4740 /* Skip tokens until the next token is a closing parenthesis.
4741 If we find the closing `)', and the next token is a `{', then
4742 we are looking at a compound-literal. */
4743 compound_literal_p
4744 = (cp_parser_skip_to_closing_parenthesis (parser)
4745 && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4746 /* Roll back the tokens we skipped. */
4747 cp_lexer_rollback_tokens (parser->lexer);
4748 /* If we were looking at a compound-literal, simulate an error
4749 so that the call to cp_parser_parse_definitely below will
4750 fail. */
4751 if (compound_literal_p)
4752 cp_parser_simulate_error (parser);
4753 else
4754 {
4755 /* Look for the type-id. */
4756 type = cp_parser_type_id (parser);
4757 /* Look for the closing `)'. */
4758 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4759 }
4760
4761 /* Restore the saved message. */
4762 parser->type_definition_forbidden_message = saved_message;
4763
4764 /* If ok so far, parse the dependent expression. We cannot be
4765 sure it is a cast. Consider `(T ())'. It is a parenthesized
4766 ctor of T, but looks like a cast to function returning T
4767 without a dependent expression. */
4768 if (!cp_parser_error_occurred (parser))
4769 expr = cp_parser_simple_cast_expression (parser);
4770
4771 if (cp_parser_parse_definitely (parser))
4772 {
4773 /* Warn about old-style casts, if so requested. */
4774 if (warn_old_style_cast
4775 && !in_system_header
4776 && !VOID_TYPE_P (type)
4777 && current_lang_name != lang_name_c)
4778 warning ("use of old-style cast");
4779
4780 /* Only type conversions to integral or enumeration types
4781 can be used in constant-expressions. */
4782 if (parser->constant_expression_p
4783 && !dependent_type_p (type)
4784 && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4785 {
4786 if (!parser->allow_non_constant_expression_p)
4787 return (cp_parser_non_constant_expression
4788 ("a casts to a type other than an integral or "
4789 "enumeration type"));
4790 parser->non_constant_expression_p = true;
4791 }
4792 /* Perform the cast. */
4793 expr = build_c_cast (type, expr);
4794 return expr;
4795 }
4796 }
4797
4798 /* If we get here, then it's not a cast, so it must be a
4799 unary-expression. */
4800 return cp_parser_unary_expression (parser, address_p);
4801 }
4802
4803 /* Parse a pm-expression.
4804
4805 pm-expression:
4806 cast-expression
4807 pm-expression .* cast-expression
4808 pm-expression ->* cast-expression
4809
4810 Returns a representation of the expression. */
4811
4812 static tree
4813 cp_parser_pm_expression (cp_parser* parser)
4814 {
4815 static const cp_parser_token_tree_map map = {
4816 { CPP_DEREF_STAR, MEMBER_REF },
4817 { CPP_DOT_STAR, DOTSTAR_EXPR },
4818 { CPP_EOF, ERROR_MARK }
4819 };
4820
4821 return cp_parser_binary_expression (parser, map,
4822 cp_parser_simple_cast_expression);
4823 }
4824
4825 /* Parse a multiplicative-expression.
4826
4827 mulitplicative-expression:
4828 pm-expression
4829 multiplicative-expression * pm-expression
4830 multiplicative-expression / pm-expression
4831 multiplicative-expression % pm-expression
4832
4833 Returns a representation of the expression. */
4834
4835 static tree
4836 cp_parser_multiplicative_expression (cp_parser* parser)
4837 {
4838 static const cp_parser_token_tree_map map = {
4839 { CPP_MULT, MULT_EXPR },
4840 { CPP_DIV, TRUNC_DIV_EXPR },
4841 { CPP_MOD, TRUNC_MOD_EXPR },
4842 { CPP_EOF, ERROR_MARK }
4843 };
4844
4845 return cp_parser_binary_expression (parser,
4846 map,
4847 cp_parser_pm_expression);
4848 }
4849
4850 /* Parse an additive-expression.
4851
4852 additive-expression:
4853 multiplicative-expression
4854 additive-expression + multiplicative-expression
4855 additive-expression - multiplicative-expression
4856
4857 Returns a representation of the expression. */
4858
4859 static tree
4860 cp_parser_additive_expression (cp_parser* parser)
4861 {
4862 static const cp_parser_token_tree_map map = {
4863 { CPP_PLUS, PLUS_EXPR },
4864 { CPP_MINUS, MINUS_EXPR },
4865 { CPP_EOF, ERROR_MARK }
4866 };
4867
4868 return cp_parser_binary_expression (parser,
4869 map,
4870 cp_parser_multiplicative_expression);
4871 }
4872
4873 /* Parse a shift-expression.
4874
4875 shift-expression:
4876 additive-expression
4877 shift-expression << additive-expression
4878 shift-expression >> additive-expression
4879
4880 Returns a representation of the expression. */
4881
4882 static tree
4883 cp_parser_shift_expression (cp_parser* parser)
4884 {
4885 static const cp_parser_token_tree_map map = {
4886 { CPP_LSHIFT, LSHIFT_EXPR },
4887 { CPP_RSHIFT, RSHIFT_EXPR },
4888 { CPP_EOF, ERROR_MARK }
4889 };
4890
4891 return cp_parser_binary_expression (parser,
4892 map,
4893 cp_parser_additive_expression);
4894 }
4895
4896 /* Parse a relational-expression.
4897
4898 relational-expression:
4899 shift-expression
4900 relational-expression < shift-expression
4901 relational-expression > shift-expression
4902 relational-expression <= shift-expression
4903 relational-expression >= shift-expression
4904
4905 GNU Extension:
4906
4907 relational-expression:
4908 relational-expression <? shift-expression
4909 relational-expression >? shift-expression
4910
4911 Returns a representation of the expression. */
4912
4913 static tree
4914 cp_parser_relational_expression (cp_parser* parser)
4915 {
4916 static const cp_parser_token_tree_map map = {
4917 { CPP_LESS, LT_EXPR },
4918 { CPP_GREATER, GT_EXPR },
4919 { CPP_LESS_EQ, LE_EXPR },
4920 { CPP_GREATER_EQ, GE_EXPR },
4921 { CPP_MIN, MIN_EXPR },
4922 { CPP_MAX, MAX_EXPR },
4923 { CPP_EOF, ERROR_MARK }
4924 };
4925
4926 return cp_parser_binary_expression (parser,
4927 map,
4928 cp_parser_shift_expression);
4929 }
4930
4931 /* Parse an equality-expression.
4932
4933 equality-expression:
4934 relational-expression
4935 equality-expression == relational-expression
4936 equality-expression != relational-expression
4937
4938 Returns a representation of the expression. */
4939
4940 static tree
4941 cp_parser_equality_expression (cp_parser* parser)
4942 {
4943 static const cp_parser_token_tree_map map = {
4944 { CPP_EQ_EQ, EQ_EXPR },
4945 { CPP_NOT_EQ, NE_EXPR },
4946 { CPP_EOF, ERROR_MARK }
4947 };
4948
4949 return cp_parser_binary_expression (parser,
4950 map,
4951 cp_parser_relational_expression);
4952 }
4953
4954 /* Parse an and-expression.
4955
4956 and-expression:
4957 equality-expression
4958 and-expression & equality-expression
4959
4960 Returns a representation of the expression. */
4961
4962 static tree
4963 cp_parser_and_expression (cp_parser* parser)
4964 {
4965 static const cp_parser_token_tree_map map = {
4966 { CPP_AND, BIT_AND_EXPR },
4967 { CPP_EOF, ERROR_MARK }
4968 };
4969
4970 return cp_parser_binary_expression (parser,
4971 map,
4972 cp_parser_equality_expression);
4973 }
4974
4975 /* Parse an exclusive-or-expression.
4976
4977 exclusive-or-expression:
4978 and-expression
4979 exclusive-or-expression ^ and-expression
4980
4981 Returns a representation of the expression. */
4982
4983 static tree
4984 cp_parser_exclusive_or_expression (cp_parser* parser)
4985 {
4986 static const cp_parser_token_tree_map map = {
4987 { CPP_XOR, BIT_XOR_EXPR },
4988 { CPP_EOF, ERROR_MARK }
4989 };
4990
4991 return cp_parser_binary_expression (parser,
4992 map,
4993 cp_parser_and_expression);
4994 }
4995
4996
4997 /* Parse an inclusive-or-expression.
4998
4999 inclusive-or-expression:
5000 exclusive-or-expression
5001 inclusive-or-expression | exclusive-or-expression
5002
5003 Returns a representation of the expression. */
5004
5005 static tree
5006 cp_parser_inclusive_or_expression (cp_parser* parser)
5007 {
5008 static const cp_parser_token_tree_map map = {
5009 { CPP_OR, BIT_IOR_EXPR },
5010 { CPP_EOF, ERROR_MARK }
5011 };
5012
5013 return cp_parser_binary_expression (parser,
5014 map,
5015 cp_parser_exclusive_or_expression);
5016 }
5017
5018 /* Parse a logical-and-expression.
5019
5020 logical-and-expression:
5021 inclusive-or-expression
5022 logical-and-expression && inclusive-or-expression
5023
5024 Returns a representation of the expression. */
5025
5026 static tree
5027 cp_parser_logical_and_expression (cp_parser* parser)
5028 {
5029 static const cp_parser_token_tree_map map = {
5030 { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5031 { CPP_EOF, ERROR_MARK }
5032 };
5033
5034 return cp_parser_binary_expression (parser,
5035 map,
5036 cp_parser_inclusive_or_expression);
5037 }
5038
5039 /* Parse a logical-or-expression.
5040
5041 logical-or-expression:
5042 logical-and-expression
5043 logical-or-expression || logical-and-expression
5044
5045 Returns a representation of the expression. */
5046
5047 static tree
5048 cp_parser_logical_or_expression (cp_parser* parser)
5049 {
5050 static const cp_parser_token_tree_map map = {
5051 { CPP_OR_OR, TRUTH_ORIF_EXPR },
5052 { CPP_EOF, ERROR_MARK }
5053 };
5054
5055 return cp_parser_binary_expression (parser,
5056 map,
5057 cp_parser_logical_and_expression);
5058 }
5059
5060 /* Parse a conditional-expression.
5061
5062 conditional-expression:
5063 logical-or-expression
5064 logical-or-expression ? expression : assignment-expression
5065
5066 GNU Extensions:
5067
5068 conditional-expression:
5069 logical-or-expression ? : assignment-expression
5070
5071 Returns a representation of the expression. */
5072
5073 static tree
5074 cp_parser_conditional_expression (cp_parser* parser)
5075 {
5076 tree logical_or_expr;
5077
5078 /* Parse the logical-or-expression. */
5079 logical_or_expr = cp_parser_logical_or_expression (parser);
5080 /* If the next token is a `?', then we have a real conditional
5081 expression. */
5082 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5083 return cp_parser_question_colon_clause (parser, logical_or_expr);
5084 /* Otherwise, the value is simply the logical-or-expression. */
5085 else
5086 return logical_or_expr;
5087 }
5088
5089 /* Parse the `? expression : assignment-expression' part of a
5090 conditional-expression. The LOGICAL_OR_EXPR is the
5091 logical-or-expression that started the conditional-expression.
5092 Returns a representation of the entire conditional-expression.
5093
5094 This routine exists only so that it can be shared between
5095 cp_parser_conditional_expression and
5096 cp_parser_assignment_expression.
5097
5098 ? expression : assignment-expression
5099
5100 GNU Extensions:
5101
5102 ? : assignment-expression */
5103
5104 static tree
5105 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5106 {
5107 tree expr;
5108 tree assignment_expr;
5109
5110 /* Consume the `?' token. */
5111 cp_lexer_consume_token (parser->lexer);
5112 if (cp_parser_allow_gnu_extensions_p (parser)
5113 && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5114 /* Implicit true clause. */
5115 expr = NULL_TREE;
5116 else
5117 /* Parse the expression. */
5118 expr = cp_parser_expression (parser);
5119
5120 /* The next token should be a `:'. */
5121 cp_parser_require (parser, CPP_COLON, "`:'");
5122 /* Parse the assignment-expression. */
5123 assignment_expr = cp_parser_assignment_expression (parser);
5124
5125 /* Build the conditional-expression. */
5126 return build_x_conditional_expr (logical_or_expr,
5127 expr,
5128 assignment_expr);
5129 }
5130
5131 /* Parse an assignment-expression.
5132
5133 assignment-expression:
5134 conditional-expression
5135 logical-or-expression assignment-operator assignment_expression
5136 throw-expression
5137
5138 Returns a representation for the expression. */
5139
5140 static tree
5141 cp_parser_assignment_expression (cp_parser* parser)
5142 {
5143 tree expr;
5144
5145 /* If the next token is the `throw' keyword, then we're looking at
5146 a throw-expression. */
5147 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5148 expr = cp_parser_throw_expression (parser);
5149 /* Otherwise, it must be that we are looking at a
5150 logical-or-expression. */
5151 else
5152 {
5153 /* Parse the logical-or-expression. */
5154 expr = cp_parser_logical_or_expression (parser);
5155 /* If the next token is a `?' then we're actually looking at a
5156 conditional-expression. */
5157 if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5158 return cp_parser_question_colon_clause (parser, expr);
5159 else
5160 {
5161 enum tree_code assignment_operator;
5162
5163 /* If it's an assignment-operator, we're using the second
5164 production. */
5165 assignment_operator
5166 = cp_parser_assignment_operator_opt (parser);
5167 if (assignment_operator != ERROR_MARK)
5168 {
5169 tree rhs;
5170
5171 /* Parse the right-hand side of the assignment. */
5172 rhs = cp_parser_assignment_expression (parser);
5173 /* An assignment may not appear in a
5174 constant-expression. */
5175 if (parser->constant_expression_p)
5176 {
5177 if (!parser->allow_non_constant_expression_p)
5178 return cp_parser_non_constant_expression ("an assignment");
5179 parser->non_constant_expression_p = true;
5180 }
5181 /* Build the assignment expression. */
5182 expr = build_x_modify_expr (expr,
5183 assignment_operator,
5184 rhs);
5185 }
5186 }
5187 }
5188
5189 return expr;
5190 }
5191
5192 /* Parse an (optional) assignment-operator.
5193
5194 assignment-operator: one of
5195 = *= /= %= += -= >>= <<= &= ^= |=
5196
5197 GNU Extension:
5198
5199 assignment-operator: one of
5200 <?= >?=
5201
5202 If the next token is an assignment operator, the corresponding tree
5203 code is returned, and the token is consumed. For example, for
5204 `+=', PLUS_EXPR is returned. For `=' itself, the code returned is
5205 NOP_EXPR. For `/', TRUNC_DIV_EXPR is returned; for `%',
5206 TRUNC_MOD_EXPR is returned. If TOKEN is not an assignment
5207 operator, ERROR_MARK is returned. */
5208
5209 static enum tree_code
5210 cp_parser_assignment_operator_opt (cp_parser* parser)
5211 {
5212 enum tree_code op;
5213 cp_token *token;
5214
5215 /* Peek at the next toen. */
5216 token = cp_lexer_peek_token (parser->lexer);
5217
5218 switch (token->type)
5219 {
5220 case CPP_EQ:
5221 op = NOP_EXPR;
5222 break;
5223
5224 case CPP_MULT_EQ:
5225 op = MULT_EXPR;
5226 break;
5227
5228 case CPP_DIV_EQ:
5229 op = TRUNC_DIV_EXPR;
5230 break;
5231
5232 case CPP_MOD_EQ:
5233 op = TRUNC_MOD_EXPR;
5234 break;
5235
5236 case CPP_PLUS_EQ:
5237 op = PLUS_EXPR;
5238 break;
5239
5240 case CPP_MINUS_EQ:
5241 op = MINUS_EXPR;
5242 break;
5243
5244 case CPP_RSHIFT_EQ:
5245 op = RSHIFT_EXPR;
5246 break;
5247
5248 case CPP_LSHIFT_EQ:
5249 op = LSHIFT_EXPR;
5250 break;
5251
5252 case CPP_AND_EQ:
5253 op = BIT_AND_EXPR;
5254 break;
5255
5256 case CPP_XOR_EQ:
5257 op = BIT_XOR_EXPR;
5258 break;
5259
5260 case CPP_OR_EQ:
5261 op = BIT_IOR_EXPR;
5262 break;
5263
5264 case CPP_MIN_EQ:
5265 op = MIN_EXPR;
5266 break;
5267
5268 case CPP_MAX_EQ:
5269 op = MAX_EXPR;
5270 break;
5271
5272 default:
5273 /* Nothing else is an assignment operator. */
5274 op = ERROR_MARK;
5275 }
5276
5277 /* If it was an assignment operator, consume it. */
5278 if (op != ERROR_MARK)
5279 cp_lexer_consume_token (parser->lexer);
5280
5281 return op;
5282 }
5283
5284 /* Parse an expression.
5285
5286 expression:
5287 assignment-expression
5288 expression , assignment-expression
5289
5290 Returns a representation of the expression. */
5291
5292 static tree
5293 cp_parser_expression (cp_parser* parser)
5294 {
5295 tree expression = NULL_TREE;
5296 bool saw_comma_p = false;
5297
5298 while (true)
5299 {
5300 tree assignment_expression;
5301
5302 /* Parse the next assignment-expression. */
5303 assignment_expression
5304 = cp_parser_assignment_expression (parser);
5305 /* If this is the first assignment-expression, we can just
5306 save it away. */
5307 if (!expression)
5308 expression = assignment_expression;
5309 /* Otherwise, chain the expressions together. It is unclear why
5310 we do not simply build COMPOUND_EXPRs as we go. */
5311 else
5312 expression = tree_cons (NULL_TREE,
5313 assignment_expression,
5314 expression);
5315 /* If the next token is not a comma, then we are done with the
5316 expression. */
5317 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5318 break;
5319 /* Consume the `,'. */
5320 cp_lexer_consume_token (parser->lexer);
5321 /* The first time we see a `,', we must take special action
5322 because the representation used for a single expression is
5323 different from that used for a list containing the single
5324 expression. */
5325 if (!saw_comma_p)
5326 {
5327 /* Remember that this expression has a `,' in it. */
5328 saw_comma_p = true;
5329 /* Turn the EXPRESSION into a TREE_LIST so that we can link
5330 additional expressions to it. */
5331 expression = build_tree_list (NULL_TREE, expression);
5332 }
5333 }
5334
5335 /* Build a COMPOUND_EXPR to represent the entire expression, if
5336 necessary. We built up the list in reverse order, so we must
5337 straighten it out here. */
5338 if (saw_comma_p)
5339 {
5340 /* A comma operator cannot appear in a constant-expression. */
5341 if (parser->constant_expression_p)
5342 {
5343 if (!parser->allow_non_constant_expression_p)
5344 return cp_parser_non_constant_expression ("a comma operator");
5345 parser->non_constant_expression_p = true;
5346 }
5347 expression = build_x_compound_expr (nreverse (expression));
5348 }
5349
5350 return expression;
5351 }
5352
5353 /* Parse a constant-expression.
5354
5355 constant-expression:
5356 conditional-expression
5357
5358 If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5359 accepted. In that case *NON_CONSTANT_P is set to TRUE. If
5360 ALLOW_NON_CONSTANT_P is false, NON_CONSTANT_P should be NULL. */
5361
5362 static tree
5363 cp_parser_constant_expression (cp_parser* parser,
5364 bool allow_non_constant_p,
5365 bool *non_constant_p)
5366 {
5367 bool saved_constant_expression_p;
5368 bool saved_allow_non_constant_expression_p;
5369 bool saved_non_constant_expression_p;
5370 tree expression;
5371
5372 /* It might seem that we could simply parse the
5373 conditional-expression, and then check to see if it were
5374 TREE_CONSTANT. However, an expression that is TREE_CONSTANT is
5375 one that the compiler can figure out is constant, possibly after
5376 doing some simplifications or optimizations. The standard has a
5377 precise definition of constant-expression, and we must honor
5378 that, even though it is somewhat more restrictive.
5379
5380 For example:
5381
5382 int i[(2, 3)];
5383
5384 is not a legal declaration, because `(2, 3)' is not a
5385 constant-expression. The `,' operator is forbidden in a
5386 constant-expression. However, GCC's constant-folding machinery
5387 will fold this operation to an INTEGER_CST for `3'. */
5388
5389 /* Save the old settings. */
5390 saved_constant_expression_p = parser->constant_expression_p;
5391 saved_allow_non_constant_expression_p
5392 = parser->allow_non_constant_expression_p;
5393 saved_non_constant_expression_p = parser->non_constant_expression_p;
5394 /* We are now parsing a constant-expression. */
5395 parser->constant_expression_p = true;
5396 parser->allow_non_constant_expression_p = allow_non_constant_p;
5397 parser->non_constant_expression_p = false;
5398 /* Parse the conditional-expression. */
5399 expression = cp_parser_conditional_expression (parser);
5400 /* Restore the old settings. */
5401 parser->constant_expression_p = saved_constant_expression_p;
5402 parser->allow_non_constant_expression_p
5403 = saved_allow_non_constant_expression_p;
5404 if (allow_non_constant_p)
5405 *non_constant_p = parser->non_constant_expression_p;
5406 parser->non_constant_expression_p = saved_non_constant_expression_p;
5407
5408 return expression;
5409 }
5410
5411 /* Statements [gram.stmt.stmt] */
5412
5413 /* Parse a statement.
5414
5415 statement:
5416 labeled-statement
5417 expression-statement
5418 compound-statement
5419 selection-statement
5420 iteration-statement
5421 jump-statement
5422 declaration-statement
5423 try-block */
5424
5425 static void
5426 cp_parser_statement (cp_parser* parser)
5427 {
5428 tree statement;
5429 cp_token *token;
5430 int statement_line_number;
5431
5432 /* There is no statement yet. */
5433 statement = NULL_TREE;
5434 /* Peek at the next token. */
5435 token = cp_lexer_peek_token (parser->lexer);
5436 /* Remember the line number of the first token in the statement. */
5437 statement_line_number = token->location.line;
5438 /* If this is a keyword, then that will often determine what kind of
5439 statement we have. */
5440 if (token->type == CPP_KEYWORD)
5441 {
5442 enum rid keyword = token->keyword;
5443
5444 switch (keyword)
5445 {
5446 case RID_CASE:
5447 case RID_DEFAULT:
5448 statement = cp_parser_labeled_statement (parser);
5449 break;
5450
5451 case RID_IF:
5452 case RID_SWITCH:
5453 statement = cp_parser_selection_statement (parser);
5454 break;
5455
5456 case RID_WHILE:
5457 case RID_DO:
5458 case RID_FOR:
5459 statement = cp_parser_iteration_statement (parser);
5460 break;
5461
5462 case RID_BREAK:
5463 case RID_CONTINUE:
5464 case RID_RETURN:
5465 case RID_GOTO:
5466 statement = cp_parser_jump_statement (parser);
5467 break;
5468
5469 case RID_TRY:
5470 statement = cp_parser_try_block (parser);
5471 break;
5472
5473 default:
5474 /* It might be a keyword like `int' that can start a
5475 declaration-statement. */
5476 break;
5477 }
5478 }
5479 else if (token->type == CPP_NAME)
5480 {
5481 /* If the next token is a `:', then we are looking at a
5482 labeled-statement. */
5483 token = cp_lexer_peek_nth_token (parser->lexer, 2);
5484 if (token->type == CPP_COLON)
5485 statement = cp_parser_labeled_statement (parser);
5486 }
5487 /* Anything that starts with a `{' must be a compound-statement. */
5488 else if (token->type == CPP_OPEN_BRACE)
5489 statement = cp_parser_compound_statement (parser);
5490
5491 /* Everything else must be a declaration-statement or an
5492 expression-statement. Try for the declaration-statement
5493 first, unless we are looking at a `;', in which case we know that
5494 we have an expression-statement. */
5495 if (!statement)
5496 {
5497 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5498 {
5499 cp_parser_parse_tentatively (parser);
5500 /* Try to parse the declaration-statement. */
5501 cp_parser_declaration_statement (parser);
5502 /* If that worked, we're done. */
5503 if (cp_parser_parse_definitely (parser))
5504 return;
5505 }
5506 /* Look for an expression-statement instead. */
5507 statement = cp_parser_expression_statement (parser);
5508 }
5509
5510 /* Set the line number for the statement. */
5511 if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5512 STMT_LINENO (statement) = statement_line_number;
5513 }
5514
5515 /* Parse a labeled-statement.
5516
5517 labeled-statement:
5518 identifier : statement
5519 case constant-expression : statement
5520 default : statement
5521
5522 Returns the new CASE_LABEL, for a `case' or `default' label. For
5523 an ordinary label, returns a LABEL_STMT. */
5524
5525 static tree
5526 cp_parser_labeled_statement (cp_parser* parser)
5527 {
5528 cp_token *token;
5529 tree statement = NULL_TREE;
5530
5531 /* The next token should be an identifier. */
5532 token = cp_lexer_peek_token (parser->lexer);
5533 if (token->type != CPP_NAME
5534 && token->type != CPP_KEYWORD)
5535 {
5536 cp_parser_error (parser, "expected labeled-statement");
5537 return error_mark_node;
5538 }
5539
5540 switch (token->keyword)
5541 {
5542 case RID_CASE:
5543 {
5544 tree expr;
5545
5546 /* Consume the `case' token. */
5547 cp_lexer_consume_token (parser->lexer);
5548 /* Parse the constant-expression. */
5549 expr = cp_parser_constant_expression (parser,
5550 /*allow_non_constant=*/false,
5551 NULL);
5552 /* Create the label. */
5553 statement = finish_case_label (expr, NULL_TREE);
5554 }
5555 break;
5556
5557 case RID_DEFAULT:
5558 /* Consume the `default' token. */
5559 cp_lexer_consume_token (parser->lexer);
5560 /* Create the label. */
5561 statement = finish_case_label (NULL_TREE, NULL_TREE);
5562 break;
5563
5564 default:
5565 /* Anything else must be an ordinary label. */
5566 statement = finish_label_stmt (cp_parser_identifier (parser));
5567 break;
5568 }
5569
5570 /* Require the `:' token. */
5571 cp_parser_require (parser, CPP_COLON, "`:'");
5572 /* Parse the labeled statement. */
5573 cp_parser_statement (parser);
5574
5575 /* Return the label, in the case of a `case' or `default' label. */
5576 return statement;
5577 }
5578
5579 /* Parse an expression-statement.
5580
5581 expression-statement:
5582 expression [opt] ;
5583
5584 Returns the new EXPR_STMT -- or NULL_TREE if the expression
5585 statement consists of nothing more than an `;'. */
5586
5587 static tree
5588 cp_parser_expression_statement (cp_parser* parser)
5589 {
5590 tree statement;
5591
5592 /* If the next token is not a `;', then there is an expression to parse. */
5593 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5594 statement = finish_expr_stmt (cp_parser_expression (parser));
5595 /* Otherwise, we do not even bother to build an EXPR_STMT. */
5596 else
5597 {
5598 finish_stmt ();
5599 statement = NULL_TREE;
5600 }
5601 /* Consume the final `;'. */
5602 cp_parser_consume_semicolon_at_end_of_statement (parser);
5603
5604 return statement;
5605 }
5606
5607 /* Parse a compound-statement.
5608
5609 compound-statement:
5610 { statement-seq [opt] }
5611
5612 Returns a COMPOUND_STMT representing the statement. */
5613
5614 static tree
5615 cp_parser_compound_statement (cp_parser *parser)
5616 {
5617 tree compound_stmt;
5618
5619 /* Consume the `{'. */
5620 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5621 return error_mark_node;
5622 /* Begin the compound-statement. */
5623 compound_stmt = begin_compound_stmt (/*has_no_scope=*/0);
5624 /* Parse an (optional) statement-seq. */
5625 cp_parser_statement_seq_opt (parser);
5626 /* Finish the compound-statement. */
5627 finish_compound_stmt (/*has_no_scope=*/0, compound_stmt);
5628 /* Consume the `}'. */
5629 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5630
5631 return compound_stmt;
5632 }
5633
5634 /* Parse an (optional) statement-seq.
5635
5636 statement-seq:
5637 statement
5638 statement-seq [opt] statement */
5639
5640 static void
5641 cp_parser_statement_seq_opt (cp_parser* parser)
5642 {
5643 /* Scan statements until there aren't any more. */
5644 while (true)
5645 {
5646 /* If we're looking at a `}', then we've run out of statements. */
5647 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5648 || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5649 break;
5650
5651 /* Parse the statement. */
5652 cp_parser_statement (parser);
5653 }
5654 }
5655
5656 /* Parse a selection-statement.
5657
5658 selection-statement:
5659 if ( condition ) statement
5660 if ( condition ) statement else statement
5661 switch ( condition ) statement
5662
5663 Returns the new IF_STMT or SWITCH_STMT. */
5664
5665 static tree
5666 cp_parser_selection_statement (cp_parser* parser)
5667 {
5668 cp_token *token;
5669 enum rid keyword;
5670
5671 /* Peek at the next token. */
5672 token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5673
5674 /* See what kind of keyword it is. */
5675 keyword = token->keyword;
5676 switch (keyword)
5677 {
5678 case RID_IF:
5679 case RID_SWITCH:
5680 {
5681 tree statement;
5682 tree condition;
5683
5684 /* Look for the `('. */
5685 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5686 {
5687 cp_parser_skip_to_end_of_statement (parser);
5688 return error_mark_node;
5689 }
5690
5691 /* Begin the selection-statement. */
5692 if (keyword == RID_IF)
5693 statement = begin_if_stmt ();
5694 else
5695 statement = begin_switch_stmt ();
5696
5697 /* Parse the condition. */
5698 condition = cp_parser_condition (parser);
5699 /* Look for the `)'. */
5700 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5701 cp_parser_skip_to_closing_parenthesis (parser);
5702
5703 if (keyword == RID_IF)
5704 {
5705 tree then_stmt;
5706
5707 /* Add the condition. */
5708 finish_if_stmt_cond (condition, statement);
5709
5710 /* Parse the then-clause. */
5711 then_stmt = cp_parser_implicitly_scoped_statement (parser);
5712 finish_then_clause (statement);
5713
5714 /* If the next token is `else', parse the else-clause. */
5715 if (cp_lexer_next_token_is_keyword (parser->lexer,
5716 RID_ELSE))
5717 {
5718 tree else_stmt;
5719
5720 /* Consume the `else' keyword. */
5721 cp_lexer_consume_token (parser->lexer);
5722 /* Parse the else-clause. */
5723 else_stmt
5724 = cp_parser_implicitly_scoped_statement (parser);
5725 finish_else_clause (statement);
5726 }
5727
5728 /* Now we're all done with the if-statement. */
5729 finish_if_stmt ();
5730 }
5731 else
5732 {
5733 tree body;
5734
5735 /* Add the condition. */
5736 finish_switch_cond (condition, statement);
5737
5738 /* Parse the body of the switch-statement. */
5739 body = cp_parser_implicitly_scoped_statement (parser);
5740
5741 /* Now we're all done with the switch-statement. */
5742 finish_switch_stmt (statement);
5743 }
5744
5745 return statement;
5746 }
5747 break;
5748
5749 default:
5750 cp_parser_error (parser, "expected selection-statement");
5751 return error_mark_node;
5752 }
5753 }
5754
5755 /* Parse a condition.
5756
5757 condition:
5758 expression
5759 type-specifier-seq declarator = assignment-expression
5760
5761 GNU Extension:
5762
5763 condition:
5764 type-specifier-seq declarator asm-specification [opt]
5765 attributes [opt] = assignment-expression
5766
5767 Returns the expression that should be tested. */
5768
5769 static tree
5770 cp_parser_condition (cp_parser* parser)
5771 {
5772 tree type_specifiers;
5773 const char *saved_message;
5774
5775 /* Try the declaration first. */
5776 cp_parser_parse_tentatively (parser);
5777 /* New types are not allowed in the type-specifier-seq for a
5778 condition. */
5779 saved_message = parser->type_definition_forbidden_message;
5780 parser->type_definition_forbidden_message
5781 = "types may not be defined in conditions";
5782 /* Parse the type-specifier-seq. */
5783 type_specifiers = cp_parser_type_specifier_seq (parser);
5784 /* Restore the saved message. */
5785 parser->type_definition_forbidden_message = saved_message;
5786 /* If all is well, we might be looking at a declaration. */
5787 if (!cp_parser_error_occurred (parser))
5788 {
5789 tree decl;
5790 tree asm_specification;
5791 tree attributes;
5792 tree declarator;
5793 tree initializer = NULL_TREE;
5794
5795 /* Parse the declarator. */
5796 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5797 /*ctor_dtor_or_conv_p=*/NULL);
5798 /* Parse the attributes. */
5799 attributes = cp_parser_attributes_opt (parser);
5800 /* Parse the asm-specification. */
5801 asm_specification = cp_parser_asm_specification_opt (parser);
5802 /* If the next token is not an `=', then we might still be
5803 looking at an expression. For example:
5804
5805 if (A(a).x)
5806
5807 looks like a decl-specifier-seq and a declarator -- but then
5808 there is no `=', so this is an expression. */
5809 cp_parser_require (parser, CPP_EQ, "`='");
5810 /* If we did see an `=', then we are looking at a declaration
5811 for sure. */
5812 if (cp_parser_parse_definitely (parser))
5813 {
5814 /* Create the declaration. */
5815 decl = start_decl (declarator, type_specifiers,
5816 /*initialized_p=*/true,
5817 attributes, /*prefix_attributes=*/NULL_TREE);
5818 /* Parse the assignment-expression. */
5819 initializer = cp_parser_assignment_expression (parser);
5820
5821 /* Process the initializer. */
5822 cp_finish_decl (decl,
5823 initializer,
5824 asm_specification,
5825 LOOKUP_ONLYCONVERTING);
5826
5827 return convert_from_reference (decl);
5828 }
5829 }
5830 /* If we didn't even get past the declarator successfully, we are
5831 definitely not looking at a declaration. */
5832 else
5833 cp_parser_abort_tentative_parse (parser);
5834
5835 /* Otherwise, we are looking at an expression. */
5836 return cp_parser_expression (parser);
5837 }
5838
5839 /* Parse an iteration-statement.
5840
5841 iteration-statement:
5842 while ( condition ) statement
5843 do statement while ( expression ) ;
5844 for ( for-init-statement condition [opt] ; expression [opt] )
5845 statement
5846
5847 Returns the new WHILE_STMT, DO_STMT, or FOR_STMT. */
5848
5849 static tree
5850 cp_parser_iteration_statement (cp_parser* parser)
5851 {
5852 cp_token *token;
5853 enum rid keyword;
5854 tree statement;
5855
5856 /* Peek at the next token. */
5857 token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5858 if (!token)
5859 return error_mark_node;
5860
5861 /* See what kind of keyword it is. */
5862 keyword = token->keyword;
5863 switch (keyword)
5864 {
5865 case RID_WHILE:
5866 {
5867 tree condition;
5868
5869 /* Begin the while-statement. */
5870 statement = begin_while_stmt ();
5871 /* Look for the `('. */
5872 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5873 /* Parse the condition. */
5874 condition = cp_parser_condition (parser);
5875 finish_while_stmt_cond (condition, statement);
5876 /* Look for the `)'. */
5877 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5878 /* Parse the dependent statement. */
5879 cp_parser_already_scoped_statement (parser);
5880 /* We're done with the while-statement. */
5881 finish_while_stmt (statement);
5882 }
5883 break;
5884
5885 case RID_DO:
5886 {
5887 tree expression;
5888
5889 /* Begin the do-statement. */
5890 statement = begin_do_stmt ();
5891 /* Parse the body of the do-statement. */
5892 cp_parser_implicitly_scoped_statement (parser);
5893 finish_do_body (statement);
5894 /* Look for the `while' keyword. */
5895 cp_parser_require_keyword (parser, RID_WHILE, "`while'");
5896 /* Look for the `('. */
5897 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5898 /* Parse the expression. */
5899 expression = cp_parser_expression (parser);
5900 /* We're done with the do-statement. */
5901 finish_do_stmt (expression, statement);
5902 /* Look for the `)'. */
5903 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5904 /* Look for the `;'. */
5905 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5906 }
5907 break;
5908
5909 case RID_FOR:
5910 {
5911 tree condition = NULL_TREE;
5912 tree expression = NULL_TREE;
5913
5914 /* Begin the for-statement. */
5915 statement = begin_for_stmt ();
5916 /* Look for the `('. */
5917 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5918 /* Parse the initialization. */
5919 cp_parser_for_init_statement (parser);
5920 finish_for_init_stmt (statement);
5921
5922 /* If there's a condition, process it. */
5923 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5924 condition = cp_parser_condition (parser);
5925 finish_for_cond (condition, statement);
5926 /* Look for the `;'. */
5927 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
5928
5929 /* If there's an expression, process it. */
5930 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5931 expression = cp_parser_expression (parser);
5932 finish_for_expr (expression, statement);
5933 /* Look for the `)'. */
5934 cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
5935
5936 /* Parse the body of the for-statement. */
5937 cp_parser_already_scoped_statement (parser);
5938
5939 /* We're done with the for-statement. */
5940 finish_for_stmt (statement);
5941 }
5942 break;
5943
5944 default:
5945 cp_parser_error (parser, "expected iteration-statement");
5946 statement = error_mark_node;
5947 break;
5948 }
5949
5950 return statement;
5951 }
5952
5953 /* Parse a for-init-statement.
5954
5955 for-init-statement:
5956 expression-statement
5957 simple-declaration */
5958
5959 static void
5960 cp_parser_for_init_statement (cp_parser* parser)
5961 {
5962 /* If the next token is a `;', then we have an empty
5963 expression-statement. Grammatically, this is also a
5964 simple-declaration, but an invalid one, because it does not
5965 declare anything. Therefore, if we did not handle this case
5966 specially, we would issue an error message about an invalid
5967 declaration. */
5968 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5969 {
5970 /* We're going to speculatively look for a declaration, falling back
5971 to an expression, if necessary. */
5972 cp_parser_parse_tentatively (parser);
5973 /* Parse the declaration. */
5974 cp_parser_simple_declaration (parser,
5975 /*function_definition_allowed_p=*/false);
5976 /* If the tentative parse failed, then we shall need to look for an
5977 expression-statement. */
5978 if (cp_parser_parse_definitely (parser))
5979 return;
5980 }
5981
5982 cp_parser_expression_statement (parser);
5983 }
5984
5985 /* Parse a jump-statement.
5986
5987 jump-statement:
5988 break ;
5989 continue ;
5990 return expression [opt] ;
5991 goto identifier ;
5992
5993 GNU extension:
5994
5995 jump-statement:
5996 goto * expression ;
5997
5998 Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
5999 GOTO_STMT. */
6000
6001 static tree
6002 cp_parser_jump_statement (cp_parser* parser)
6003 {
6004 tree statement = error_mark_node;
6005 cp_token *token;
6006 enum rid keyword;
6007
6008 /* Peek at the next token. */
6009 token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6010 if (!token)
6011 return error_mark_node;
6012
6013 /* See what kind of keyword it is. */
6014 keyword = token->keyword;
6015 switch (keyword)
6016 {
6017 case RID_BREAK:
6018 statement = finish_break_stmt ();
6019 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6020 break;
6021
6022 case RID_CONTINUE:
6023 statement = finish_continue_stmt ();
6024 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6025 break;
6026
6027 case RID_RETURN:
6028 {
6029 tree expr;
6030
6031 /* If the next token is a `;', then there is no
6032 expression. */
6033 if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6034 expr = cp_parser_expression (parser);
6035 else
6036 expr = NULL_TREE;
6037 /* Build the return-statement. */
6038 statement = finish_return_stmt (expr);
6039 /* Look for the final `;'. */
6040 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6041 }
6042 break;
6043
6044 case RID_GOTO:
6045 /* Create the goto-statement. */
6046 if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6047 {
6048 /* Issue a warning about this use of a GNU extension. */
6049 if (pedantic)
6050 pedwarn ("ISO C++ forbids computed gotos");
6051 /* Consume the '*' token. */
6052 cp_lexer_consume_token (parser->lexer);
6053 /* Parse the dependent expression. */
6054 finish_goto_stmt (cp_parser_expression (parser));
6055 }
6056 else
6057 finish_goto_stmt (cp_parser_identifier (parser));
6058 /* Look for the final `;'. */
6059 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6060 break;
6061
6062 default:
6063 cp_parser_error (parser, "expected jump-statement");
6064 break;
6065 }
6066
6067 return statement;
6068 }
6069
6070 /* Parse a declaration-statement.
6071
6072 declaration-statement:
6073 block-declaration */
6074
6075 static void
6076 cp_parser_declaration_statement (cp_parser* parser)
6077 {
6078 /* Parse the block-declaration. */
6079 cp_parser_block_declaration (parser, /*statement_p=*/true);
6080
6081 /* Finish off the statement. */
6082 finish_stmt ();
6083 }
6084
6085 /* Some dependent statements (like `if (cond) statement'), are
6086 implicitly in their own scope. In other words, if the statement is
6087 a single statement (as opposed to a compound-statement), it is
6088 none-the-less treated as if it were enclosed in braces. Any
6089 declarations appearing in the dependent statement are out of scope
6090 after control passes that point. This function parses a statement,
6091 but ensures that is in its own scope, even if it is not a
6092 compound-statement.
6093
6094 Returns the new statement. */
6095
6096 static tree
6097 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6098 {
6099 tree statement;
6100
6101 /* If the token is not a `{', then we must take special action. */
6102 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6103 {
6104 /* Create a compound-statement. */
6105 statement = begin_compound_stmt (/*has_no_scope=*/0);
6106 /* Parse the dependent-statement. */
6107 cp_parser_statement (parser);
6108 /* Finish the dummy compound-statement. */
6109 finish_compound_stmt (/*has_no_scope=*/0, statement);
6110 }
6111 /* Otherwise, we simply parse the statement directly. */
6112 else
6113 statement = cp_parser_compound_statement (parser);
6114
6115 /* Return the statement. */
6116 return statement;
6117 }
6118
6119 /* For some dependent statements (like `while (cond) statement'), we
6120 have already created a scope. Therefore, even if the dependent
6121 statement is a compound-statement, we do not want to create another
6122 scope. */
6123
6124 static void
6125 cp_parser_already_scoped_statement (cp_parser* parser)
6126 {
6127 /* If the token is not a `{', then we must take special action. */
6128 if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6129 {
6130 tree statement;
6131
6132 /* Create a compound-statement. */
6133 statement = begin_compound_stmt (/*has_no_scope=*/1);
6134 /* Parse the dependent-statement. */
6135 cp_parser_statement (parser);
6136 /* Finish the dummy compound-statement. */
6137 finish_compound_stmt (/*has_no_scope=*/1, statement);
6138 }
6139 /* Otherwise, we simply parse the statement directly. */
6140 else
6141 cp_parser_statement (parser);
6142 }
6143
6144 /* Declarations [gram.dcl.dcl] */
6145
6146 /* Parse an optional declaration-sequence.
6147
6148 declaration-seq:
6149 declaration
6150 declaration-seq declaration */
6151
6152 static void
6153 cp_parser_declaration_seq_opt (cp_parser* parser)
6154 {
6155 while (true)
6156 {
6157 cp_token *token;
6158
6159 token = cp_lexer_peek_token (parser->lexer);
6160
6161 if (token->type == CPP_CLOSE_BRACE
6162 || token->type == CPP_EOF)
6163 break;
6164
6165 if (token->type == CPP_SEMICOLON)
6166 {
6167 /* A declaration consisting of a single semicolon is
6168 invalid. Allow it unless we're being pedantic. */
6169 if (pedantic)
6170 pedwarn ("extra `;'");
6171 cp_lexer_consume_token (parser->lexer);
6172 continue;
6173 }
6174
6175 /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6176 parser to enter or exit implicit `extern "C"' blocks. */
6177 while (pending_lang_change > 0)
6178 {
6179 push_lang_context (lang_name_c);
6180 --pending_lang_change;
6181 }
6182 while (pending_lang_change < 0)
6183 {
6184 pop_lang_context ();
6185 ++pending_lang_change;
6186 }
6187
6188 /* Parse the declaration itself. */
6189 cp_parser_declaration (parser);
6190 }
6191 }
6192
6193 /* Parse a declaration.
6194
6195 declaration:
6196 block-declaration
6197 function-definition
6198 template-declaration
6199 explicit-instantiation
6200 explicit-specialization
6201 linkage-specification
6202 namespace-definition
6203
6204 GNU extension:
6205
6206 declaration:
6207 __extension__ declaration */
6208
6209 static void
6210 cp_parser_declaration (cp_parser* parser)
6211 {
6212 cp_token token1;
6213 cp_token token2;
6214 int saved_pedantic;
6215
6216 /* Check for the `__extension__' keyword. */
6217 if (cp_parser_extension_opt (parser, &saved_pedantic))
6218 {
6219 /* Parse the qualified declaration. */
6220 cp_parser_declaration (parser);
6221 /* Restore the PEDANTIC flag. */
6222 pedantic = saved_pedantic;
6223
6224 return;
6225 }
6226
6227 /* Try to figure out what kind of declaration is present. */
6228 token1 = *cp_lexer_peek_token (parser->lexer);
6229 if (token1.type != CPP_EOF)
6230 token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6231
6232 /* If the next token is `extern' and the following token is a string
6233 literal, then we have a linkage specification. */
6234 if (token1.keyword == RID_EXTERN
6235 && cp_parser_is_string_literal (&token2))
6236 cp_parser_linkage_specification (parser);
6237 /* If the next token is `template', then we have either a template
6238 declaration, an explicit instantiation, or an explicit
6239 specialization. */
6240 else if (token1.keyword == RID_TEMPLATE)
6241 {
6242 /* `template <>' indicates a template specialization. */
6243 if (token2.type == CPP_LESS
6244 && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6245 cp_parser_explicit_specialization (parser);
6246 /* `template <' indicates a template declaration. */
6247 else if (token2.type == CPP_LESS)
6248 cp_parser_template_declaration (parser, /*member_p=*/false);
6249 /* Anything else must be an explicit instantiation. */
6250 else
6251 cp_parser_explicit_instantiation (parser);
6252 }
6253 /* If the next token is `export', then we have a template
6254 declaration. */
6255 else if (token1.keyword == RID_EXPORT)
6256 cp_parser_template_declaration (parser, /*member_p=*/false);
6257 /* If the next token is `extern', 'static' or 'inline' and the one
6258 after that is `template', we have a GNU extended explicit
6259 instantiation directive. */
6260 else if (cp_parser_allow_gnu_extensions_p (parser)
6261 && (token1.keyword == RID_EXTERN
6262 || token1.keyword == RID_STATIC
6263 || token1.keyword == RID_INLINE)
6264 && token2.keyword == RID_TEMPLATE)
6265 cp_parser_explicit_instantiation (parser);
6266 /* If the next token is `namespace', check for a named or unnamed
6267 namespace definition. */
6268 else if (token1.keyword == RID_NAMESPACE
6269 && (/* A named namespace definition. */
6270 (token2.type == CPP_NAME
6271 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6272 == CPP_OPEN_BRACE))
6273 /* An unnamed namespace definition. */
6274 || token2.type == CPP_OPEN_BRACE))
6275 cp_parser_namespace_definition (parser);
6276 /* We must have either a block declaration or a function
6277 definition. */
6278 else
6279 /* Try to parse a block-declaration, or a function-definition. */
6280 cp_parser_block_declaration (parser, /*statement_p=*/false);
6281 }
6282
6283 /* Parse a block-declaration.
6284
6285 block-declaration:
6286 simple-declaration
6287 asm-definition
6288 namespace-alias-definition
6289 using-declaration
6290 using-directive
6291
6292 GNU Extension:
6293
6294 block-declaration:
6295 __extension__ block-declaration
6296 label-declaration
6297
6298 If STATEMENT_P is TRUE, then this block-declaration is occurring as
6299 part of a declaration-statement. */
6300
6301 static void
6302 cp_parser_block_declaration (cp_parser *parser,
6303 bool statement_p)
6304 {
6305 cp_token *token1;
6306 int saved_pedantic;
6307
6308 /* Check for the `__extension__' keyword. */
6309 if (cp_parser_extension_opt (parser, &saved_pedantic))
6310 {
6311 /* Parse the qualified declaration. */
6312 cp_parser_block_declaration (parser, statement_p);
6313 /* Restore the PEDANTIC flag. */
6314 pedantic = saved_pedantic;
6315
6316 return;
6317 }
6318
6319 /* Peek at the next token to figure out which kind of declaration is
6320 present. */
6321 token1 = cp_lexer_peek_token (parser->lexer);
6322
6323 /* If the next keyword is `asm', we have an asm-definition. */
6324 if (token1->keyword == RID_ASM)
6325 {
6326 if (statement_p)
6327 cp_parser_commit_to_tentative_parse (parser);
6328 cp_parser_asm_definition (parser);
6329 }
6330 /* If the next keyword is `namespace', we have a
6331 namespace-alias-definition. */
6332 else if (token1->keyword == RID_NAMESPACE)
6333 cp_parser_namespace_alias_definition (parser);
6334 /* If the next keyword is `using', we have either a
6335 using-declaration or a using-directive. */
6336 else if (token1->keyword == RID_USING)
6337 {
6338 cp_token *token2;
6339
6340 if (statement_p)
6341 cp_parser_commit_to_tentative_parse (parser);
6342 /* If the token after `using' is `namespace', then we have a
6343 using-directive. */
6344 token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6345 if (token2->keyword == RID_NAMESPACE)
6346 cp_parser_using_directive (parser);
6347 /* Otherwise, it's a using-declaration. */
6348 else
6349 cp_parser_using_declaration (parser);
6350 }
6351 /* If the next keyword is `__label__' we have a label declaration. */
6352 else if (token1->keyword == RID_LABEL)
6353 {
6354 if (statement_p)
6355 cp_parser_commit_to_tentative_parse (parser);
6356 cp_parser_label_declaration (parser);
6357 }
6358 /* Anything else must be a simple-declaration. */
6359 else
6360 cp_parser_simple_declaration (parser, !statement_p);
6361 }
6362
6363 /* Parse a simple-declaration.
6364
6365 simple-declaration:
6366 decl-specifier-seq [opt] init-declarator-list [opt] ;
6367
6368 init-declarator-list:
6369 init-declarator
6370 init-declarator-list , init-declarator
6371
6372 If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6373 function-definition as a simple-declaration. */
6374
6375 static void
6376 cp_parser_simple_declaration (cp_parser* parser,
6377 bool function_definition_allowed_p)
6378 {
6379 tree decl_specifiers;
6380 tree attributes;
6381 bool declares_class_or_enum;
6382 bool saw_declarator;
6383
6384 /* Defer access checks until we know what is being declared; the
6385 checks for names appearing in the decl-specifier-seq should be
6386 done as if we were in the scope of the thing being declared. */
6387 push_deferring_access_checks (dk_deferred);
6388
6389 /* Parse the decl-specifier-seq. We have to keep track of whether
6390 or not the decl-specifier-seq declares a named class or
6391 enumeration type, since that is the only case in which the
6392 init-declarator-list is allowed to be empty.
6393
6394 [dcl.dcl]
6395
6396 In a simple-declaration, the optional init-declarator-list can be
6397 omitted only when declaring a class or enumeration, that is when
6398 the decl-specifier-seq contains either a class-specifier, an
6399 elaborated-type-specifier, or an enum-specifier. */
6400 decl_specifiers
6401 = cp_parser_decl_specifier_seq (parser,
6402 CP_PARSER_FLAGS_OPTIONAL,
6403 &attributes,
6404 &declares_class_or_enum);
6405 /* We no longer need to defer access checks. */
6406 stop_deferring_access_checks ();
6407
6408 /* If the next two tokens are both identifiers, the code is
6409 erroneous. The usual cause of this situation is code like:
6410
6411 T t;
6412
6413 where "T" should name a type -- but does not. */
6414 if (cp_parser_diagnose_invalid_type_name (parser))
6415 {
6416 /* If parsing tentatively, we should commit; we really are
6417 looking at a declaration. */
6418 cp_parser_commit_to_tentative_parse (parser);
6419 /* Give up. */
6420 return;
6421 }
6422
6423 /* Keep going until we hit the `;' at the end of the simple
6424 declaration. */
6425 saw_declarator = false;
6426 while (cp_lexer_next_token_is_not (parser->lexer,
6427 CPP_SEMICOLON))
6428 {
6429 cp_token *token;
6430 bool function_definition_p;
6431
6432 saw_declarator = true;
6433 /* Parse the init-declarator. */
6434 cp_parser_init_declarator (parser, decl_specifiers, attributes,
6435 function_definition_allowed_p,
6436 /*member_p=*/false,
6437 &function_definition_p);
6438 /* If an error occurred while parsing tentatively, exit quickly.
6439 (That usually happens when in the body of a function; each
6440 statement is treated as a declaration-statement until proven
6441 otherwise.) */
6442 if (cp_parser_error_occurred (parser))
6443 {
6444 pop_deferring_access_checks ();
6445 return;
6446 }
6447 /* Handle function definitions specially. */
6448 if (function_definition_p)
6449 {
6450 /* If the next token is a `,', then we are probably
6451 processing something like:
6452
6453 void f() {}, *p;
6454
6455 which is erroneous. */
6456 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6457 error ("mixing declarations and function-definitions is forbidden");
6458 /* Otherwise, we're done with the list of declarators. */
6459 else
6460 {
6461 pop_deferring_access_checks ();
6462 return;
6463 }
6464 }
6465 /* The next token should be either a `,' or a `;'. */
6466 token = cp_lexer_peek_token (parser->lexer);
6467 /* If it's a `,', there are more declarators to come. */
6468 if (token->type == CPP_COMMA)
6469 cp_lexer_consume_token (parser->lexer);
6470 /* If it's a `;', we are done. */
6471 else if (token->type == CPP_SEMICOLON)
6472 break;
6473 /* Anything else is an error. */
6474 else
6475 {
6476 cp_parser_error (parser, "expected `,' or `;'");
6477 /* Skip tokens until we reach the end of the statement. */
6478 cp_parser_skip_to_end_of_statement (parser);
6479 pop_deferring_access_checks ();
6480 return;
6481 }
6482 /* After the first time around, a function-definition is not
6483 allowed -- even if it was OK at first. For example:
6484
6485 int i, f() {}
6486
6487 is not valid. */
6488 function_definition_allowed_p = false;
6489 }
6490
6491 /* Issue an error message if no declarators are present, and the
6492 decl-specifier-seq does not itself declare a class or
6493 enumeration. */
6494 if (!saw_declarator)
6495 {
6496 if (cp_parser_declares_only_class_p (parser))
6497 shadow_tag (decl_specifiers);
6498 /* Perform any deferred access checks. */
6499 perform_deferred_access_checks ();
6500 }
6501
6502 pop_deferring_access_checks ();
6503
6504 /* Consume the `;'. */
6505 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6506
6507 /* Mark all the classes that appeared in the decl-specifier-seq as
6508 having received a `;'. */
6509 note_list_got_semicolon (decl_specifiers);
6510 }
6511
6512 /* Parse a decl-specifier-seq.
6513
6514 decl-specifier-seq:
6515 decl-specifier-seq [opt] decl-specifier
6516
6517 decl-specifier:
6518 storage-class-specifier
6519 type-specifier
6520 function-specifier
6521 friend
6522 typedef
6523
6524 GNU Extension:
6525
6526 decl-specifier-seq:
6527 decl-specifier-seq [opt] attributes
6528
6529 Returns a TREE_LIST, giving the decl-specifiers in the order they
6530 appear in the source code. The TREE_VALUE of each node is the
6531 decl-specifier. For a keyword (such as `auto' or `friend'), the
6532 TREE_VALUE is simply the corresponding TREE_IDENTIFIER. For the
6533 representation of a type-specifier, see cp_parser_type_specifier.
6534
6535 If there are attributes, they will be stored in *ATTRIBUTES,
6536 represented as described above cp_parser_attributes.
6537
6538 If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6539 appears, and the entity that will be a friend is not going to be a
6540 class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE. Note that
6541 even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6542 friendship is granted might not be a class. */
6543
6544 static tree
6545 cp_parser_decl_specifier_seq (cp_parser* parser,
6546 cp_parser_flags flags,
6547 tree* attributes,
6548 bool* declares_class_or_enum)
6549 {
6550 tree decl_specs = NULL_TREE;
6551 bool friend_p = false;
6552 bool constructor_possible_p = !parser->in_declarator_p;
6553
6554 /* Assume no class or enumeration type is declared. */
6555 *declares_class_or_enum = false;
6556
6557 /* Assume there are no attributes. */
6558 *attributes = NULL_TREE;
6559
6560 /* Keep reading specifiers until there are no more to read. */
6561 while (true)
6562 {
6563 tree decl_spec = NULL_TREE;
6564 bool constructor_p;
6565 cp_token *token;
6566
6567 /* Peek at the next token. */
6568 token = cp_lexer_peek_token (parser->lexer);
6569 /* Handle attributes. */
6570 if (token->keyword == RID_ATTRIBUTE)
6571 {
6572 /* Parse the attributes. */
6573 decl_spec = cp_parser_attributes_opt (parser);
6574 /* Add them to the list. */
6575 *attributes = chainon (*attributes, decl_spec);
6576 continue;
6577 }
6578 /* If the next token is an appropriate keyword, we can simply
6579 add it to the list. */
6580 switch (token->keyword)
6581 {
6582 case RID_FRIEND:
6583 /* decl-specifier:
6584 friend */
6585 friend_p = true;
6586 /* The representation of the specifier is simply the
6587 appropriate TREE_IDENTIFIER node. */
6588 decl_spec = token->value;
6589 /* Consume the token. */
6590 cp_lexer_consume_token (parser->lexer);
6591 break;
6592
6593 /* function-specifier:
6594 inline
6595 virtual
6596 explicit */
6597 case RID_INLINE:
6598 case RID_VIRTUAL:
6599 case RID_EXPLICIT:
6600 decl_spec = cp_parser_function_specifier_opt (parser);
6601 break;
6602
6603 /* decl-specifier:
6604 typedef */
6605 case RID_TYPEDEF:
6606 /* The representation of the specifier is simply the
6607 appropriate TREE_IDENTIFIER node. */
6608 decl_spec = token->value;
6609 /* Consume the token. */
6610 cp_lexer_consume_token (parser->lexer);
6611 /* A constructor declarator cannot appear in a typedef. */
6612 constructor_possible_p = false;
6613 /* The "typedef" keyword can only occur in a declaration; we
6614 may as well commit at this point. */
6615 cp_parser_commit_to_tentative_parse (parser);
6616 break;
6617
6618 /* storage-class-specifier:
6619 auto
6620 register
6621 static
6622 extern
6623 mutable
6624
6625 GNU Extension:
6626 thread */
6627 case RID_AUTO:
6628 case RID_REGISTER:
6629 case RID_STATIC:
6630 case RID_EXTERN:
6631 case RID_MUTABLE:
6632 case RID_THREAD:
6633 decl_spec = cp_parser_storage_class_specifier_opt (parser);
6634 break;
6635
6636 default:
6637 break;
6638 }
6639
6640 /* Constructors are a special case. The `S' in `S()' is not a
6641 decl-specifier; it is the beginning of the declarator. */
6642 constructor_p = (!decl_spec
6643 && constructor_possible_p
6644 && cp_parser_constructor_declarator_p (parser,
6645 friend_p));
6646
6647 /* If we don't have a DECL_SPEC yet, then we must be looking at
6648 a type-specifier. */
6649 if (!decl_spec && !constructor_p)
6650 {
6651 bool decl_spec_declares_class_or_enum;
6652 bool is_cv_qualifier;
6653
6654 decl_spec
6655 = cp_parser_type_specifier (parser, flags,
6656 friend_p,
6657 /*is_declaration=*/true,
6658 &decl_spec_declares_class_or_enum,
6659 &is_cv_qualifier);
6660
6661 *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6662
6663 /* If this type-specifier referenced a user-defined type
6664 (a typedef, class-name, etc.), then we can't allow any
6665 more such type-specifiers henceforth.
6666
6667 [dcl.spec]
6668
6669 The longest sequence of decl-specifiers that could
6670 possibly be a type name is taken as the
6671 decl-specifier-seq of a declaration. The sequence shall
6672 be self-consistent as described below.
6673
6674 [dcl.type]
6675
6676 As a general rule, at most one type-specifier is allowed
6677 in the complete decl-specifier-seq of a declaration. The
6678 only exceptions are the following:
6679
6680 -- const or volatile can be combined with any other
6681 type-specifier.
6682
6683 -- signed or unsigned can be combined with char, long,
6684 short, or int.
6685
6686 -- ..
6687
6688 Example:
6689
6690 typedef char* Pc;
6691 void g (const int Pc);
6692
6693 Here, Pc is *not* part of the decl-specifier seq; it's
6694 the declarator. Therefore, once we see a type-specifier
6695 (other than a cv-qualifier), we forbid any additional
6696 user-defined types. We *do* still allow things like `int
6697 int' to be considered a decl-specifier-seq, and issue the
6698 error message later. */
6699 if (decl_spec && !is_cv_qualifier)
6700 flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6701 /* A constructor declarator cannot follow a type-specifier. */
6702 if (decl_spec)
6703 constructor_possible_p = false;
6704 }
6705
6706 /* If we still do not have a DECL_SPEC, then there are no more
6707 decl-specifiers. */
6708 if (!decl_spec)
6709 {
6710 /* Issue an error message, unless the entire construct was
6711 optional. */
6712 if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6713 {
6714 cp_parser_error (parser, "expected decl specifier");
6715 return error_mark_node;
6716 }
6717
6718 break;
6719 }
6720
6721 /* Add the DECL_SPEC to the list of specifiers. */
6722 decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6723
6724 /* After we see one decl-specifier, further decl-specifiers are
6725 always optional. */
6726 flags |= CP_PARSER_FLAGS_OPTIONAL;
6727 }
6728
6729 /* We have built up the DECL_SPECS in reverse order. Return them in
6730 the correct order. */
6731 return nreverse (decl_specs);
6732 }
6733
6734 /* Parse an (optional) storage-class-specifier.
6735
6736 storage-class-specifier:
6737 auto
6738 register
6739 static
6740 extern
6741 mutable
6742
6743 GNU Extension:
6744
6745 storage-class-specifier:
6746 thread
6747
6748 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6749
6750 static tree
6751 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6752 {
6753 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6754 {
6755 case RID_AUTO:
6756 case RID_REGISTER:
6757 case RID_STATIC:
6758 case RID_EXTERN:
6759 case RID_MUTABLE:
6760 case RID_THREAD:
6761 /* Consume the token. */
6762 return cp_lexer_consume_token (parser->lexer)->value;
6763
6764 default:
6765 return NULL_TREE;
6766 }
6767 }
6768
6769 /* Parse an (optional) function-specifier.
6770
6771 function-specifier:
6772 inline
6773 virtual
6774 explicit
6775
6776 Returns an IDENTIFIER_NODE corresponding to the keyword used. */
6777
6778 static tree
6779 cp_parser_function_specifier_opt (cp_parser* parser)
6780 {
6781 switch (cp_lexer_peek_token (parser->lexer)->keyword)
6782 {
6783 case RID_INLINE:
6784 case RID_VIRTUAL:
6785 case RID_EXPLICIT:
6786 /* Consume the token. */
6787 return cp_lexer_consume_token (parser->lexer)->value;
6788
6789 default:
6790 return NULL_TREE;
6791 }
6792 }
6793
6794 /* Parse a linkage-specification.
6795
6796 linkage-specification:
6797 extern string-literal { declaration-seq [opt] }
6798 extern string-literal declaration */
6799
6800 static void
6801 cp_parser_linkage_specification (cp_parser* parser)
6802 {
6803 cp_token *token;
6804 tree linkage;
6805
6806 /* Look for the `extern' keyword. */
6807 cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6808
6809 /* Peek at the next token. */
6810 token = cp_lexer_peek_token (parser->lexer);
6811 /* If it's not a string-literal, then there's a problem. */
6812 if (!cp_parser_is_string_literal (token))
6813 {
6814 cp_parser_error (parser, "expected language-name");
6815 return;
6816 }
6817 /* Consume the token. */
6818 cp_lexer_consume_token (parser->lexer);
6819
6820 /* Transform the literal into an identifier. If the literal is a
6821 wide-character string, or contains embedded NULs, then we can't
6822 handle it as the user wants. */
6823 if (token->type == CPP_WSTRING
6824 || (strlen (TREE_STRING_POINTER (token->value))
6825 != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6826 {
6827 cp_parser_error (parser, "invalid linkage-specification");
6828 /* Assume C++ linkage. */
6829 linkage = get_identifier ("c++");
6830 }
6831 /* If it's a simple string constant, things are easier. */
6832 else
6833 linkage = get_identifier (TREE_STRING_POINTER (token->value));
6834
6835 /* We're now using the new linkage. */
6836 push_lang_context (linkage);
6837
6838 /* If the next token is a `{', then we're using the first
6839 production. */
6840 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6841 {
6842 /* Consume the `{' token. */
6843 cp_lexer_consume_token (parser->lexer);
6844 /* Parse the declarations. */
6845 cp_parser_declaration_seq_opt (parser);
6846 /* Look for the closing `}'. */
6847 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6848 }
6849 /* Otherwise, there's just one declaration. */
6850 else
6851 {
6852 bool saved_in_unbraced_linkage_specification_p;
6853
6854 saved_in_unbraced_linkage_specification_p
6855 = parser->in_unbraced_linkage_specification_p;
6856 parser->in_unbraced_linkage_specification_p = true;
6857 have_extern_spec = true;
6858 cp_parser_declaration (parser);
6859 have_extern_spec = false;
6860 parser->in_unbraced_linkage_specification_p
6861 = saved_in_unbraced_linkage_specification_p;
6862 }
6863
6864 /* We're done with the linkage-specification. */
6865 pop_lang_context ();
6866 }
6867
6868 /* Special member functions [gram.special] */
6869
6870 /* Parse a conversion-function-id.
6871
6872 conversion-function-id:
6873 operator conversion-type-id
6874
6875 Returns an IDENTIFIER_NODE representing the operator. */
6876
6877 static tree
6878 cp_parser_conversion_function_id (cp_parser* parser)
6879 {
6880 tree type;
6881 tree saved_scope;
6882 tree saved_qualifying_scope;
6883 tree saved_object_scope;
6884
6885 /* Look for the `operator' token. */
6886 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
6887 return error_mark_node;
6888 /* When we parse the conversion-type-id, the current scope will be
6889 reset. However, we need that information in able to look up the
6890 conversion function later, so we save it here. */
6891 saved_scope = parser->scope;
6892 saved_qualifying_scope = parser->qualifying_scope;
6893 saved_object_scope = parser->object_scope;
6894 /* We must enter the scope of the class so that the names of
6895 entities declared within the class are available in the
6896 conversion-type-id. For example, consider:
6897
6898 struct S {
6899 typedef int I;
6900 operator I();
6901 };
6902
6903 S::operator I() { ... }
6904
6905 In order to see that `I' is a type-name in the definition, we
6906 must be in the scope of `S'. */
6907 if (saved_scope)
6908 push_scope (saved_scope);
6909 /* Parse the conversion-type-id. */
6910 type = cp_parser_conversion_type_id (parser);
6911 /* Leave the scope of the class, if any. */
6912 if (saved_scope)
6913 pop_scope (saved_scope);
6914 /* Restore the saved scope. */
6915 parser->scope = saved_scope;
6916 parser->qualifying_scope = saved_qualifying_scope;
6917 parser->object_scope = saved_object_scope;
6918 /* If the TYPE is invalid, indicate failure. */
6919 if (type == error_mark_node)
6920 return error_mark_node;
6921 return mangle_conv_op_name_for_type (type);
6922 }
6923
6924 /* Parse a conversion-type-id:
6925
6926 conversion-type-id:
6927 type-specifier-seq conversion-declarator [opt]
6928
6929 Returns the TYPE specified. */
6930
6931 static tree
6932 cp_parser_conversion_type_id (cp_parser* parser)
6933 {
6934 tree attributes;
6935 tree type_specifiers;
6936 tree declarator;
6937
6938 /* Parse the attributes. */
6939 attributes = cp_parser_attributes_opt (parser);
6940 /* Parse the type-specifiers. */
6941 type_specifiers = cp_parser_type_specifier_seq (parser);
6942 /* If that didn't work, stop. */
6943 if (type_specifiers == error_mark_node)
6944 return error_mark_node;
6945 /* Parse the conversion-declarator. */
6946 declarator = cp_parser_conversion_declarator_opt (parser);
6947
6948 return grokdeclarator (declarator, type_specifiers, TYPENAME,
6949 /*initialized=*/0, &attributes);
6950 }
6951
6952 /* Parse an (optional) conversion-declarator.
6953
6954 conversion-declarator:
6955 ptr-operator conversion-declarator [opt]
6956
6957 Returns a representation of the declarator. See
6958 cp_parser_declarator for details. */
6959
6960 static tree
6961 cp_parser_conversion_declarator_opt (cp_parser* parser)
6962 {
6963 enum tree_code code;
6964 tree class_type;
6965 tree cv_qualifier_seq;
6966
6967 /* We don't know if there's a ptr-operator next, or not. */
6968 cp_parser_parse_tentatively (parser);
6969 /* Try the ptr-operator. */
6970 code = cp_parser_ptr_operator (parser, &class_type,
6971 &cv_qualifier_seq);
6972 /* If it worked, look for more conversion-declarators. */
6973 if (cp_parser_parse_definitely (parser))
6974 {
6975 tree declarator;
6976
6977 /* Parse another optional declarator. */
6978 declarator = cp_parser_conversion_declarator_opt (parser);
6979
6980 /* Create the representation of the declarator. */
6981 if (code == INDIRECT_REF)
6982 declarator = make_pointer_declarator (cv_qualifier_seq,
6983 declarator);
6984 else
6985 declarator = make_reference_declarator (cv_qualifier_seq,
6986 declarator);
6987
6988 /* Handle the pointer-to-member case. */
6989 if (class_type)
6990 declarator = build_nt (SCOPE_REF, class_type, declarator);
6991
6992 return declarator;
6993 }
6994
6995 return NULL_TREE;
6996 }
6997
6998 /* Parse an (optional) ctor-initializer.
6999
7000 ctor-initializer:
7001 : mem-initializer-list
7002
7003 Returns TRUE iff the ctor-initializer was actually present. */
7004
7005 static bool
7006 cp_parser_ctor_initializer_opt (cp_parser* parser)
7007 {
7008 /* If the next token is not a `:', then there is no
7009 ctor-initializer. */
7010 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7011 {
7012 /* Do default initialization of any bases and members. */
7013 if (DECL_CONSTRUCTOR_P (current_function_decl))
7014 finish_mem_initializers (NULL_TREE);
7015
7016 return false;
7017 }
7018
7019 /* Consume the `:' token. */
7020 cp_lexer_consume_token (parser->lexer);
7021 /* And the mem-initializer-list. */
7022 cp_parser_mem_initializer_list (parser);
7023
7024 return true;
7025 }
7026
7027 /* Parse a mem-initializer-list.
7028
7029 mem-initializer-list:
7030 mem-initializer
7031 mem-initializer , mem-initializer-list */
7032
7033 static void
7034 cp_parser_mem_initializer_list (cp_parser* parser)
7035 {
7036 tree mem_initializer_list = NULL_TREE;
7037
7038 /* Let the semantic analysis code know that we are starting the
7039 mem-initializer-list. */
7040 if (!DECL_CONSTRUCTOR_P (current_function_decl))
7041 error ("only constructors take base initializers");
7042
7043 /* Loop through the list. */
7044 while (true)
7045 {
7046 tree mem_initializer;
7047
7048 /* Parse the mem-initializer. */
7049 mem_initializer = cp_parser_mem_initializer (parser);
7050 /* Add it to the list, unless it was erroneous. */
7051 if (mem_initializer)
7052 {
7053 TREE_CHAIN (mem_initializer) = mem_initializer_list;
7054 mem_initializer_list = mem_initializer;
7055 }
7056 /* If the next token is not a `,', we're done. */
7057 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7058 break;
7059 /* Consume the `,' token. */
7060 cp_lexer_consume_token (parser->lexer);
7061 }
7062
7063 /* Perform semantic analysis. */
7064 if (DECL_CONSTRUCTOR_P (current_function_decl))
7065 finish_mem_initializers (mem_initializer_list);
7066 }
7067
7068 /* Parse a mem-initializer.
7069
7070 mem-initializer:
7071 mem-initializer-id ( expression-list [opt] )
7072
7073 GNU extension:
7074
7075 mem-initializer:
7076 ( expression-list [opt] )
7077
7078 Returns a TREE_LIST. The TREE_PURPOSE is the TYPE (for a base
7079 class) or FIELD_DECL (for a non-static data member) to initialize;
7080 the TREE_VALUE is the expression-list. */
7081
7082 static tree
7083 cp_parser_mem_initializer (cp_parser* parser)
7084 {
7085 tree mem_initializer_id;
7086 tree expression_list;
7087 tree member;
7088
7089 /* Find out what is being initialized. */
7090 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7091 {
7092 pedwarn ("anachronistic old-style base class initializer");
7093 mem_initializer_id = NULL_TREE;
7094 }
7095 else
7096 mem_initializer_id = cp_parser_mem_initializer_id (parser);
7097 member = expand_member_init (mem_initializer_id);
7098 if (member && !DECL_P (member))
7099 in_base_initializer = 1;
7100
7101 /* Look for the opening `('. */
7102 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
7103 /* Parse the expression-list. */
7104 if (cp_lexer_next_token_is_not (parser->lexer,
7105 CPP_CLOSE_PAREN))
7106 expression_list = cp_parser_expression_list (parser);
7107 else
7108 expression_list = void_type_node;
7109 /* Look for the closing `)'. */
7110 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7111
7112 in_base_initializer = 0;
7113
7114 return member ? build_tree_list (member, expression_list) : NULL_TREE;
7115 }
7116
7117 /* Parse a mem-initializer-id.
7118
7119 mem-initializer-id:
7120 :: [opt] nested-name-specifier [opt] class-name
7121 identifier
7122
7123 Returns a TYPE indicating the class to be initializer for the first
7124 production. Returns an IDENTIFIER_NODE indicating the data member
7125 to be initialized for the second production. */
7126
7127 static tree
7128 cp_parser_mem_initializer_id (cp_parser* parser)
7129 {
7130 bool global_scope_p;
7131 bool nested_name_specifier_p;
7132 tree id;
7133
7134 /* Look for the optional `::' operator. */
7135 global_scope_p
7136 = (cp_parser_global_scope_opt (parser,
7137 /*current_scope_valid_p=*/false)
7138 != NULL_TREE);
7139 /* Look for the optional nested-name-specifier. The simplest way to
7140 implement:
7141
7142 [temp.res]
7143
7144 The keyword `typename' is not permitted in a base-specifier or
7145 mem-initializer; in these contexts a qualified name that
7146 depends on a template-parameter is implicitly assumed to be a
7147 type name.
7148
7149 is to assume that we have seen the `typename' keyword at this
7150 point. */
7151 nested_name_specifier_p
7152 = (cp_parser_nested_name_specifier_opt (parser,
7153 /*typename_keyword_p=*/true,
7154 /*check_dependency_p=*/true,
7155 /*type_p=*/true)
7156 != NULL_TREE);
7157 /* If there is a `::' operator or a nested-name-specifier, then we
7158 are definitely looking for a class-name. */
7159 if (global_scope_p || nested_name_specifier_p)
7160 return cp_parser_class_name (parser,
7161 /*typename_keyword_p=*/true,
7162 /*template_keyword_p=*/false,
7163 /*type_p=*/false,
7164 /*check_dependency_p=*/true,
7165 /*class_head_p=*/false);
7166 /* Otherwise, we could also be looking for an ordinary identifier. */
7167 cp_parser_parse_tentatively (parser);
7168 /* Try a class-name. */
7169 id = cp_parser_class_name (parser,
7170 /*typename_keyword_p=*/true,
7171 /*template_keyword_p=*/false,
7172 /*type_p=*/false,
7173 /*check_dependency_p=*/true,
7174 /*class_head_p=*/false);
7175 /* If we found one, we're done. */
7176 if (cp_parser_parse_definitely (parser))
7177 return id;
7178 /* Otherwise, look for an ordinary identifier. */
7179 return cp_parser_identifier (parser);
7180 }
7181
7182 /* Overloading [gram.over] */
7183
7184 /* Parse an operator-function-id.
7185
7186 operator-function-id:
7187 operator operator
7188
7189 Returns an IDENTIFIER_NODE for the operator which is a
7190 human-readable spelling of the identifier, e.g., `operator +'. */
7191
7192 static tree
7193 cp_parser_operator_function_id (cp_parser* parser)
7194 {
7195 /* Look for the `operator' keyword. */
7196 if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7197 return error_mark_node;
7198 /* And then the name of the operator itself. */
7199 return cp_parser_operator (parser);
7200 }
7201
7202 /* Parse an operator.
7203
7204 operator:
7205 new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7206 += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7207 || ++ -- , ->* -> () []
7208
7209 GNU Extensions:
7210
7211 operator:
7212 <? >? <?= >?=
7213
7214 Returns an IDENTIFIER_NODE for the operator which is a
7215 human-readable spelling of the identifier, e.g., `operator +'. */
7216
7217 static tree
7218 cp_parser_operator (cp_parser* parser)
7219 {
7220 tree id = NULL_TREE;
7221 cp_token *token;
7222
7223 /* Peek at the next token. */
7224 token = cp_lexer_peek_token (parser->lexer);
7225 /* Figure out which operator we have. */
7226 switch (token->type)
7227 {
7228 case CPP_KEYWORD:
7229 {
7230 enum tree_code op;
7231
7232 /* The keyword should be either `new' or `delete'. */
7233 if (token->keyword == RID_NEW)
7234 op = NEW_EXPR;
7235 else if (token->keyword == RID_DELETE)
7236 op = DELETE_EXPR;
7237 else
7238 break;
7239
7240 /* Consume the `new' or `delete' token. */
7241 cp_lexer_consume_token (parser->lexer);
7242
7243 /* Peek at the next token. */
7244 token = cp_lexer_peek_token (parser->lexer);
7245 /* If it's a `[' token then this is the array variant of the
7246 operator. */
7247 if (token->type == CPP_OPEN_SQUARE)
7248 {
7249 /* Consume the `[' token. */
7250 cp_lexer_consume_token (parser->lexer);
7251 /* Look for the `]' token. */
7252 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7253 id = ansi_opname (op == NEW_EXPR
7254 ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7255 }
7256 /* Otherwise, we have the non-array variant. */
7257 else
7258 id = ansi_opname (op);
7259
7260 return id;
7261 }
7262
7263 case CPP_PLUS:
7264 id = ansi_opname (PLUS_EXPR);
7265 break;
7266
7267 case CPP_MINUS:
7268 id = ansi_opname (MINUS_EXPR);
7269 break;
7270
7271 case CPP_MULT:
7272 id = ansi_opname (MULT_EXPR);
7273 break;
7274
7275 case CPP_DIV:
7276 id = ansi_opname (TRUNC_DIV_EXPR);
7277 break;
7278
7279 case CPP_MOD:
7280 id = ansi_opname (TRUNC_MOD_EXPR);
7281 break;
7282
7283 case CPP_XOR:
7284 id = ansi_opname (BIT_XOR_EXPR);
7285 break;
7286
7287 case CPP_AND:
7288 id = ansi_opname (BIT_AND_EXPR);
7289 break;
7290
7291 case CPP_OR:
7292 id = ansi_opname (BIT_IOR_EXPR);
7293 break;
7294
7295 case CPP_COMPL:
7296 id = ansi_opname (BIT_NOT_EXPR);
7297 break;
7298
7299 case CPP_NOT:
7300 id = ansi_opname (TRUTH_NOT_EXPR);
7301 break;
7302
7303 case CPP_EQ:
7304 id = ansi_assopname (NOP_EXPR);
7305 break;
7306
7307 case CPP_LESS:
7308 id = ansi_opname (LT_EXPR);
7309 break;
7310
7311 case CPP_GREATER:
7312 id = ansi_opname (GT_EXPR);
7313 break;
7314
7315 case CPP_PLUS_EQ:
7316 id = ansi_assopname (PLUS_EXPR);
7317 break;
7318
7319 case CPP_MINUS_EQ:
7320 id = ansi_assopname (MINUS_EXPR);
7321 break;
7322
7323 case CPP_MULT_EQ:
7324 id = ansi_assopname (MULT_EXPR);
7325 break;
7326
7327 case CPP_DIV_EQ:
7328 id = ansi_assopname (TRUNC_DIV_EXPR);
7329 break;
7330
7331 case CPP_MOD_EQ:
7332 id = ansi_assopname (TRUNC_MOD_EXPR);
7333 break;
7334
7335 case CPP_XOR_EQ:
7336 id = ansi_assopname (BIT_XOR_EXPR);
7337 break;
7338
7339 case CPP_AND_EQ:
7340 id = ansi_assopname (BIT_AND_EXPR);
7341 break;
7342
7343 case CPP_OR_EQ:
7344 id = ansi_assopname (BIT_IOR_EXPR);
7345 break;
7346
7347 case CPP_LSHIFT:
7348 id = ansi_opname (LSHIFT_EXPR);
7349 break;
7350
7351 case CPP_RSHIFT:
7352 id = ansi_opname (RSHIFT_EXPR);
7353 break;
7354
7355 case CPP_LSHIFT_EQ:
7356 id = ansi_assopname (LSHIFT_EXPR);
7357 break;
7358
7359 case CPP_RSHIFT_EQ:
7360 id = ansi_assopname (RSHIFT_EXPR);
7361 break;
7362
7363 case CPP_EQ_EQ:
7364 id = ansi_opname (EQ_EXPR);
7365 break;
7366
7367 case CPP_NOT_EQ:
7368 id = ansi_opname (NE_EXPR);
7369 break;
7370
7371 case CPP_LESS_EQ:
7372 id = ansi_opname (LE_EXPR);
7373 break;
7374
7375 case CPP_GREATER_EQ:
7376 id = ansi_opname (GE_EXPR);
7377 break;
7378
7379 case CPP_AND_AND:
7380 id = ansi_opname (TRUTH_ANDIF_EXPR);
7381 break;
7382
7383 case CPP_OR_OR:
7384 id = ansi_opname (TRUTH_ORIF_EXPR);
7385 break;
7386
7387 case CPP_PLUS_PLUS:
7388 id = ansi_opname (POSTINCREMENT_EXPR);
7389 break;
7390
7391 case CPP_MINUS_MINUS:
7392 id = ansi_opname (PREDECREMENT_EXPR);
7393 break;
7394
7395 case CPP_COMMA:
7396 id = ansi_opname (COMPOUND_EXPR);
7397 break;
7398
7399 case CPP_DEREF_STAR:
7400 id = ansi_opname (MEMBER_REF);
7401 break;
7402
7403 case CPP_DEREF:
7404 id = ansi_opname (COMPONENT_REF);
7405 break;
7406
7407 case CPP_OPEN_PAREN:
7408 /* Consume the `('. */
7409 cp_lexer_consume_token (parser->lexer);
7410 /* Look for the matching `)'. */
7411 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7412 return ansi_opname (CALL_EXPR);
7413
7414 case CPP_OPEN_SQUARE:
7415 /* Consume the `['. */
7416 cp_lexer_consume_token (parser->lexer);
7417 /* Look for the matching `]'. */
7418 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7419 return ansi_opname (ARRAY_REF);
7420
7421 /* Extensions. */
7422 case CPP_MIN:
7423 id = ansi_opname (MIN_EXPR);
7424 break;
7425
7426 case CPP_MAX:
7427 id = ansi_opname (MAX_EXPR);
7428 break;
7429
7430 case CPP_MIN_EQ:
7431 id = ansi_assopname (MIN_EXPR);
7432 break;
7433
7434 case CPP_MAX_EQ:
7435 id = ansi_assopname (MAX_EXPR);
7436 break;
7437
7438 default:
7439 /* Anything else is an error. */
7440 break;
7441 }
7442
7443 /* If we have selected an identifier, we need to consume the
7444 operator token. */
7445 if (id)
7446 cp_lexer_consume_token (parser->lexer);
7447 /* Otherwise, no valid operator name was present. */
7448 else
7449 {
7450 cp_parser_error (parser, "expected operator");
7451 id = error_mark_node;
7452 }
7453
7454 return id;
7455 }
7456
7457 /* Parse a template-declaration.
7458
7459 template-declaration:
7460 export [opt] template < template-parameter-list > declaration
7461
7462 If MEMBER_P is TRUE, this template-declaration occurs within a
7463 class-specifier.
7464
7465 The grammar rule given by the standard isn't correct. What
7466 is really meant is:
7467
7468 template-declaration:
7469 export [opt] template-parameter-list-seq
7470 decl-specifier-seq [opt] init-declarator [opt] ;
7471 export [opt] template-parameter-list-seq
7472 function-definition
7473
7474 template-parameter-list-seq:
7475 template-parameter-list-seq [opt]
7476 template < template-parameter-list > */
7477
7478 static void
7479 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7480 {
7481 /* Check for `export'. */
7482 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7483 {
7484 /* Consume the `export' token. */
7485 cp_lexer_consume_token (parser->lexer);
7486 /* Warn that we do not support `export'. */
7487 warning ("keyword `export' not implemented, and will be ignored");
7488 }
7489
7490 cp_parser_template_declaration_after_export (parser, member_p);
7491 }
7492
7493 /* Parse a template-parameter-list.
7494
7495 template-parameter-list:
7496 template-parameter
7497 template-parameter-list , template-parameter
7498
7499 Returns a TREE_LIST. Each node represents a template parameter.
7500 The nodes are connected via their TREE_CHAINs. */
7501
7502 static tree
7503 cp_parser_template_parameter_list (cp_parser* parser)
7504 {
7505 tree parameter_list = NULL_TREE;
7506
7507 while (true)
7508 {
7509 tree parameter;
7510 cp_token *token;
7511
7512 /* Parse the template-parameter. */
7513 parameter = cp_parser_template_parameter (parser);
7514 /* Add it to the list. */
7515 parameter_list = process_template_parm (parameter_list,
7516 parameter);
7517
7518 /* Peek at the next token. */
7519 token = cp_lexer_peek_token (parser->lexer);
7520 /* If it's not a `,', we're done. */
7521 if (token->type != CPP_COMMA)
7522 break;
7523 /* Otherwise, consume the `,' token. */
7524 cp_lexer_consume_token (parser->lexer);
7525 }
7526
7527 return parameter_list;
7528 }
7529
7530 /* Parse a template-parameter.
7531
7532 template-parameter:
7533 type-parameter
7534 parameter-declaration
7535
7536 Returns a TREE_LIST. The TREE_VALUE represents the parameter. The
7537 TREE_PURPOSE is the default value, if any. */
7538
7539 static tree
7540 cp_parser_template_parameter (cp_parser* parser)
7541 {
7542 cp_token *token;
7543
7544 /* Peek at the next token. */
7545 token = cp_lexer_peek_token (parser->lexer);
7546 /* If it is `class' or `template', we have a type-parameter. */
7547 if (token->keyword == RID_TEMPLATE)
7548 return cp_parser_type_parameter (parser);
7549 /* If it is `class' or `typename' we do not know yet whether it is a
7550 type parameter or a non-type parameter. Consider:
7551
7552 template <typename T, typename T::X X> ...
7553
7554 or:
7555
7556 template <class C, class D*> ...
7557
7558 Here, the first parameter is a type parameter, and the second is
7559 a non-type parameter. We can tell by looking at the token after
7560 the identifier -- if it is a `,', `=', or `>' then we have a type
7561 parameter. */
7562 if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7563 {
7564 /* Peek at the token after `class' or `typename'. */
7565 token = cp_lexer_peek_nth_token (parser->lexer, 2);
7566 /* If it's an identifier, skip it. */
7567 if (token->type == CPP_NAME)
7568 token = cp_lexer_peek_nth_token (parser->lexer, 3);
7569 /* Now, see if the token looks like the end of a template
7570 parameter. */
7571 if (token->type == CPP_COMMA
7572 || token->type == CPP_EQ
7573 || token->type == CPP_GREATER)
7574 return cp_parser_type_parameter (parser);
7575 }
7576
7577 /* Otherwise, it is a non-type parameter.
7578
7579 [temp.param]
7580
7581 When parsing a default template-argument for a non-type
7582 template-parameter, the first non-nested `>' is taken as the end
7583 of the template parameter-list rather than a greater-than
7584 operator. */
7585 return
7586 cp_parser_parameter_declaration (parser, /*template_parm_p=*/true);
7587 }
7588
7589 /* Parse a type-parameter.
7590
7591 type-parameter:
7592 class identifier [opt]
7593 class identifier [opt] = type-id
7594 typename identifier [opt]
7595 typename identifier [opt] = type-id
7596 template < template-parameter-list > class identifier [opt]
7597 template < template-parameter-list > class identifier [opt]
7598 = id-expression
7599
7600 Returns a TREE_LIST. The TREE_VALUE is itself a TREE_LIST. The
7601 TREE_PURPOSE is the default-argument, if any. The TREE_VALUE is
7602 the declaration of the parameter. */
7603
7604 static tree
7605 cp_parser_type_parameter (cp_parser* parser)
7606 {
7607 cp_token *token;
7608 tree parameter;
7609
7610 /* Look for a keyword to tell us what kind of parameter this is. */
7611 token = cp_parser_require (parser, CPP_KEYWORD,
7612 "`class', `typename', or `template'");
7613 if (!token)
7614 return error_mark_node;
7615
7616 switch (token->keyword)
7617 {
7618 case RID_CLASS:
7619 case RID_TYPENAME:
7620 {
7621 tree identifier;
7622 tree default_argument;
7623
7624 /* If the next token is an identifier, then it names the
7625 parameter. */
7626 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7627 identifier = cp_parser_identifier (parser);
7628 else
7629 identifier = NULL_TREE;
7630
7631 /* Create the parameter. */
7632 parameter = finish_template_type_parm (class_type_node, identifier);
7633
7634 /* If the next token is an `=', we have a default argument. */
7635 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7636 {
7637 /* Consume the `=' token. */
7638 cp_lexer_consume_token (parser->lexer);
7639 /* Parse the default-argument. */
7640 default_argument = cp_parser_type_id (parser);
7641 }
7642 else
7643 default_argument = NULL_TREE;
7644
7645 /* Create the combined representation of the parameter and the
7646 default argument. */
7647 parameter = build_tree_list (default_argument,
7648 parameter);
7649 }
7650 break;
7651
7652 case RID_TEMPLATE:
7653 {
7654 tree parameter_list;
7655 tree identifier;
7656 tree default_argument;
7657
7658 /* Look for the `<'. */
7659 cp_parser_require (parser, CPP_LESS, "`<'");
7660 /* Parse the template-parameter-list. */
7661 begin_template_parm_list ();
7662 parameter_list
7663 = cp_parser_template_parameter_list (parser);
7664 parameter_list = end_template_parm_list (parameter_list);
7665 /* Look for the `>'. */
7666 cp_parser_require (parser, CPP_GREATER, "`>'");
7667 /* Look for the `class' keyword. */
7668 cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7669 /* If the next token is an `=', then there is a
7670 default-argument. If the next token is a `>', we are at
7671 the end of the parameter-list. If the next token is a `,',
7672 then we are at the end of this parameter. */
7673 if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7674 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7675 && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7676 identifier = cp_parser_identifier (parser);
7677 else
7678 identifier = NULL_TREE;
7679 /* Create the template parameter. */
7680 parameter = finish_template_template_parm (class_type_node,
7681 identifier);
7682
7683 /* If the next token is an `=', then there is a
7684 default-argument. */
7685 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7686 {
7687 /* Consume the `='. */
7688 cp_lexer_consume_token (parser->lexer);
7689 /* Parse the id-expression. */
7690 default_argument
7691 = cp_parser_id_expression (parser,
7692 /*template_keyword_p=*/false,
7693 /*check_dependency_p=*/true,
7694 /*template_p=*/NULL);
7695 /* Look up the name. */
7696 default_argument
7697 = cp_parser_lookup_name_simple (parser, default_argument);
7698 /* See if the default argument is valid. */
7699 default_argument
7700 = check_template_template_default_arg (default_argument);
7701 }
7702 else
7703 default_argument = NULL_TREE;
7704
7705 /* Create the combined representation of the parameter and the
7706 default argument. */
7707 parameter = build_tree_list (default_argument,
7708 parameter);
7709 }
7710 break;
7711
7712 default:
7713 /* Anything else is an error. */
7714 cp_parser_error (parser,
7715 "expected `class', `typename', or `template'");
7716 parameter = error_mark_node;
7717 }
7718
7719 return parameter;
7720 }
7721
7722 /* Parse a template-id.
7723
7724 template-id:
7725 template-name < template-argument-list [opt] >
7726
7727 If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7728 `template' keyword. In this case, a TEMPLATE_ID_EXPR will be
7729 returned. Otherwise, if the template-name names a function, or set
7730 of functions, returns a TEMPLATE_ID_EXPR. If the template-name
7731 names a class, returns a TYPE_DECL for the specialization.
7732
7733 If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7734 uninstantiated templates. */
7735
7736 static tree
7737 cp_parser_template_id (cp_parser *parser,
7738 bool template_keyword_p,
7739 bool check_dependency_p)
7740 {
7741 tree template;
7742 tree arguments;
7743 tree saved_scope;
7744 tree saved_qualifying_scope;
7745 tree saved_object_scope;
7746 tree template_id;
7747 bool saved_greater_than_is_operator_p;
7748 ptrdiff_t start_of_id;
7749 tree access_check = NULL_TREE;
7750 cp_token *next_token;
7751
7752 /* If the next token corresponds to a template-id, there is no need
7753 to reparse it. */
7754 next_token = cp_lexer_peek_token (parser->lexer);
7755 if (next_token->type == CPP_TEMPLATE_ID)
7756 {
7757 tree value;
7758 tree check;
7759
7760 /* Get the stored value. */
7761 value = cp_lexer_consume_token (parser->lexer)->value;
7762 /* Perform any access checks that were deferred. */
7763 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7764 perform_or_defer_access_check (TREE_PURPOSE (check),
7765 TREE_VALUE (check));
7766 /* Return the stored value. */
7767 return TREE_VALUE (value);
7768 }
7769
7770 /* Avoid performing name lookup if there is no possibility of
7771 finding a template-id. */
7772 if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7773 || (next_token->type == CPP_NAME
7774 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS))
7775 {
7776 cp_parser_error (parser, "expected template-id");
7777 return error_mark_node;
7778 }
7779
7780 /* Remember where the template-id starts. */
7781 if (cp_parser_parsing_tentatively (parser)
7782 && !cp_parser_committed_to_tentative_parse (parser))
7783 {
7784 next_token = cp_lexer_peek_token (parser->lexer);
7785 start_of_id = cp_lexer_token_difference (parser->lexer,
7786 parser->lexer->first_token,
7787 next_token);
7788 }
7789 else
7790 start_of_id = -1;
7791
7792 push_deferring_access_checks (dk_deferred);
7793
7794 /* Parse the template-name. */
7795 template = cp_parser_template_name (parser, template_keyword_p,
7796 check_dependency_p);
7797 if (template == error_mark_node)
7798 {
7799 pop_deferring_access_checks ();
7800 return error_mark_node;
7801 }
7802
7803 /* Look for the `<' that starts the template-argument-list. */
7804 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
7805 {
7806 pop_deferring_access_checks ();
7807 return error_mark_node;
7808 }
7809
7810 /* [temp.names]
7811
7812 When parsing a template-id, the first non-nested `>' is taken as
7813 the end of the template-argument-list rather than a greater-than
7814 operator. */
7815 saved_greater_than_is_operator_p
7816 = parser->greater_than_is_operator_p;
7817 parser->greater_than_is_operator_p = false;
7818 /* Parsing the argument list may modify SCOPE, so we save it
7819 here. */
7820 saved_scope = parser->scope;
7821 saved_qualifying_scope = parser->qualifying_scope;
7822 saved_object_scope = parser->object_scope;
7823 /* Parse the template-argument-list itself. */
7824 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
7825 arguments = NULL_TREE;
7826 else
7827 arguments = cp_parser_template_argument_list (parser);
7828 /* Look for the `>' that ends the template-argument-list. */
7829 cp_parser_require (parser, CPP_GREATER, "`>'");
7830 /* The `>' token might be a greater-than operator again now. */
7831 parser->greater_than_is_operator_p
7832 = saved_greater_than_is_operator_p;
7833 /* Restore the SAVED_SCOPE. */
7834 parser->scope = saved_scope;
7835 parser->qualifying_scope = saved_qualifying_scope;
7836 parser->object_scope = saved_object_scope;
7837
7838 /* Build a representation of the specialization. */
7839 if (TREE_CODE (template) == IDENTIFIER_NODE)
7840 template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
7841 else if (DECL_CLASS_TEMPLATE_P (template)
7842 || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
7843 template_id
7844 = finish_template_type (template, arguments,
7845 cp_lexer_next_token_is (parser->lexer,
7846 CPP_SCOPE));
7847 else
7848 {
7849 /* If it's not a class-template or a template-template, it should be
7850 a function-template. */
7851 my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
7852 || TREE_CODE (template) == OVERLOAD
7853 || BASELINK_P (template)),
7854 20010716);
7855
7856 template_id = lookup_template_function (template, arguments);
7857 }
7858
7859 /* Retrieve any deferred checks. Do not pop this access checks yet
7860 so the memory will not be reclaimed during token replacing below. */
7861 access_check = get_deferred_access_checks ();
7862
7863 /* If parsing tentatively, replace the sequence of tokens that makes
7864 up the template-id with a CPP_TEMPLATE_ID token. That way,
7865 should we re-parse the token stream, we will not have to repeat
7866 the effort required to do the parse, nor will we issue duplicate
7867 error messages about problems during instantiation of the
7868 template. */
7869 if (start_of_id >= 0)
7870 {
7871 cp_token *token;
7872
7873 /* Find the token that corresponds to the start of the
7874 template-id. */
7875 token = cp_lexer_advance_token (parser->lexer,
7876 parser->lexer->first_token,
7877 start_of_id);
7878
7879 /* Reset the contents of the START_OF_ID token. */
7880 token->type = CPP_TEMPLATE_ID;
7881 token->value = build_tree_list (access_check, template_id);
7882 token->keyword = RID_MAX;
7883 /* Purge all subsequent tokens. */
7884 cp_lexer_purge_tokens_after (parser->lexer, token);
7885 }
7886
7887 pop_deferring_access_checks ();
7888 return template_id;
7889 }
7890
7891 /* Parse a template-name.
7892
7893 template-name:
7894 identifier
7895
7896 The standard should actually say:
7897
7898 template-name:
7899 identifier
7900 operator-function-id
7901 conversion-function-id
7902
7903 A defect report has been filed about this issue.
7904
7905 If TEMPLATE_KEYWORD_P is true, then we have just seen the
7906 `template' keyword, in a construction like:
7907
7908 T::template f<3>()
7909
7910 In that case `f' is taken to be a template-name, even though there
7911 is no way of knowing for sure.
7912
7913 Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
7914 name refers to a set of overloaded functions, at least one of which
7915 is a template, or an IDENTIFIER_NODE with the name of the template,
7916 if TEMPLATE_KEYWORD_P is true. If CHECK_DEPENDENCY_P is FALSE,
7917 names are looked up inside uninstantiated templates. */
7918
7919 static tree
7920 cp_parser_template_name (cp_parser* parser,
7921 bool template_keyword_p,
7922 bool check_dependency_p)
7923 {
7924 tree identifier;
7925 tree decl;
7926 tree fns;
7927
7928 /* If the next token is `operator', then we have either an
7929 operator-function-id or a conversion-function-id. */
7930 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
7931 {
7932 /* We don't know whether we're looking at an
7933 operator-function-id or a conversion-function-id. */
7934 cp_parser_parse_tentatively (parser);
7935 /* Try an operator-function-id. */
7936 identifier = cp_parser_operator_function_id (parser);
7937 /* If that didn't work, try a conversion-function-id. */
7938 if (!cp_parser_parse_definitely (parser))
7939 identifier = cp_parser_conversion_function_id (parser);
7940 }
7941 /* Look for the identifier. */
7942 else
7943 identifier = cp_parser_identifier (parser);
7944
7945 /* If we didn't find an identifier, we don't have a template-id. */
7946 if (identifier == error_mark_node)
7947 return error_mark_node;
7948
7949 /* If the name immediately followed the `template' keyword, then it
7950 is a template-name. However, if the next token is not `<', then
7951 we do not treat it as a template-name, since it is not being used
7952 as part of a template-id. This enables us to handle constructs
7953 like:
7954
7955 template <typename T> struct S { S(); };
7956 template <typename T> S<T>::S();
7957
7958 correctly. We would treat `S' as a template -- if it were `S<T>'
7959 -- but we do not if there is no `<'. */
7960 if (template_keyword_p && processing_template_decl
7961 && cp_lexer_next_token_is (parser->lexer, CPP_LESS))
7962 return identifier;
7963
7964 /* Look up the name. */
7965 decl = cp_parser_lookup_name (parser, identifier,
7966 /*is_type=*/false,
7967 /*is_namespace=*/false,
7968 check_dependency_p);
7969 decl = maybe_get_template_decl_from_type_decl (decl);
7970
7971 /* If DECL is a template, then the name was a template-name. */
7972 if (TREE_CODE (decl) == TEMPLATE_DECL)
7973 ;
7974 else
7975 {
7976 /* The standard does not explicitly indicate whether a name that
7977 names a set of overloaded declarations, some of which are
7978 templates, is a template-name. However, such a name should
7979 be a template-name; otherwise, there is no way to form a
7980 template-id for the overloaded templates. */
7981 fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
7982 if (TREE_CODE (fns) == OVERLOAD)
7983 {
7984 tree fn;
7985
7986 for (fn = fns; fn; fn = OVL_NEXT (fn))
7987 if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
7988 break;
7989 }
7990 else
7991 {
7992 /* Otherwise, the name does not name a template. */
7993 cp_parser_error (parser, "expected template-name");
7994 return error_mark_node;
7995 }
7996 }
7997
7998 /* If DECL is dependent, and refers to a function, then just return
7999 its name; we will look it up again during template instantiation. */
8000 if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8001 {
8002 tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8003 if (TYPE_P (scope) && dependent_type_p (scope))
8004 return identifier;
8005 }
8006
8007 return decl;
8008 }
8009
8010 /* Parse a template-argument-list.
8011
8012 template-argument-list:
8013 template-argument
8014 template-argument-list , template-argument
8015
8016 Returns a TREE_LIST representing the arguments, in the order they
8017 appeared. The TREE_VALUE of each node is a representation of the
8018 argument. */
8019
8020 static tree
8021 cp_parser_template_argument_list (cp_parser* parser)
8022 {
8023 tree arguments = NULL_TREE;
8024
8025 while (true)
8026 {
8027 tree argument;
8028
8029 /* Parse the template-argument. */
8030 argument = cp_parser_template_argument (parser);
8031 /* Add it to the list. */
8032 arguments = tree_cons (NULL_TREE, argument, arguments);
8033 /* If it is not a `,', then there are no more arguments. */
8034 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8035 break;
8036 /* Otherwise, consume the ','. */
8037 cp_lexer_consume_token (parser->lexer);
8038 }
8039
8040 /* We built up the arguments in reverse order. */
8041 return nreverse (arguments);
8042 }
8043
8044 /* Parse a template-argument.
8045
8046 template-argument:
8047 assignment-expression
8048 type-id
8049 id-expression
8050
8051 The representation is that of an assignment-expression, type-id, or
8052 id-expression -- except that the qualified id-expression is
8053 evaluated, so that the value returned is either a DECL or an
8054 OVERLOAD. */
8055
8056 static tree
8057 cp_parser_template_argument (cp_parser* parser)
8058 {
8059 tree argument;
8060 bool template_p;
8061
8062 /* There's really no way to know what we're looking at, so we just
8063 try each alternative in order.
8064
8065 [temp.arg]
8066
8067 In a template-argument, an ambiguity between a type-id and an
8068 expression is resolved to a type-id, regardless of the form of
8069 the corresponding template-parameter.
8070
8071 Therefore, we try a type-id first. */
8072 cp_parser_parse_tentatively (parser);
8073 argument = cp_parser_type_id (parser);
8074 /* If the next token isn't a `,' or a `>', then this argument wasn't
8075 really finished. */
8076 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8077 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8078 cp_parser_error (parser, "expected template-argument");
8079 /* If that worked, we're done. */
8080 if (cp_parser_parse_definitely (parser))
8081 return argument;
8082 /* We're still not sure what the argument will be. */
8083 cp_parser_parse_tentatively (parser);
8084 /* Try a template. */
8085 argument = cp_parser_id_expression (parser,
8086 /*template_keyword_p=*/false,
8087 /*check_dependency_p=*/true,
8088 &template_p);
8089 /* If the next token isn't a `,' or a `>', then this argument wasn't
8090 really finished. */
8091 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA)
8092 && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER))
8093 cp_parser_error (parser, "expected template-argument");
8094 if (!cp_parser_error_occurred (parser))
8095 {
8096 /* Figure out what is being referred to. */
8097 argument = cp_parser_lookup_name_simple (parser, argument);
8098 if (template_p)
8099 argument = make_unbound_class_template (TREE_OPERAND (argument, 0),
8100 TREE_OPERAND (argument, 1),
8101 tf_error);
8102 else if (TREE_CODE (argument) != TEMPLATE_DECL)
8103 cp_parser_error (parser, "expected template-name");
8104 }
8105 if (cp_parser_parse_definitely (parser))
8106 return argument;
8107 /* It must be an assignment-expression. */
8108 return cp_parser_assignment_expression (parser);
8109 }
8110
8111 /* Parse an explicit-instantiation.
8112
8113 explicit-instantiation:
8114 template declaration
8115
8116 Although the standard says `declaration', what it really means is:
8117
8118 explicit-instantiation:
8119 template decl-specifier-seq [opt] declarator [opt] ;
8120
8121 Things like `template int S<int>::i = 5, int S<double>::j;' are not
8122 supposed to be allowed. A defect report has been filed about this
8123 issue.
8124
8125 GNU Extension:
8126
8127 explicit-instantiation:
8128 storage-class-specifier template
8129 decl-specifier-seq [opt] declarator [opt] ;
8130 function-specifier template
8131 decl-specifier-seq [opt] declarator [opt] ; */
8132
8133 static void
8134 cp_parser_explicit_instantiation (cp_parser* parser)
8135 {
8136 bool declares_class_or_enum;
8137 tree decl_specifiers;
8138 tree attributes;
8139 tree extension_specifier = NULL_TREE;
8140
8141 /* Look for an (optional) storage-class-specifier or
8142 function-specifier. */
8143 if (cp_parser_allow_gnu_extensions_p (parser))
8144 {
8145 extension_specifier
8146 = cp_parser_storage_class_specifier_opt (parser);
8147 if (!extension_specifier)
8148 extension_specifier = cp_parser_function_specifier_opt (parser);
8149 }
8150
8151 /* Look for the `template' keyword. */
8152 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8153 /* Let the front end know that we are processing an explicit
8154 instantiation. */
8155 begin_explicit_instantiation ();
8156 /* [temp.explicit] says that we are supposed to ignore access
8157 control while processing explicit instantiation directives. */
8158 push_deferring_access_checks (dk_no_check);
8159 /* Parse a decl-specifier-seq. */
8160 decl_specifiers
8161 = cp_parser_decl_specifier_seq (parser,
8162 CP_PARSER_FLAGS_OPTIONAL,
8163 &attributes,
8164 &declares_class_or_enum);
8165 /* If there was exactly one decl-specifier, and it declared a class,
8166 and there's no declarator, then we have an explicit type
8167 instantiation. */
8168 if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8169 {
8170 tree type;
8171
8172 type = check_tag_decl (decl_specifiers);
8173 /* Turn access control back on for names used during
8174 template instantiation. */
8175 pop_deferring_access_checks ();
8176 if (type)
8177 do_type_instantiation (type, extension_specifier, /*complain=*/1);
8178 }
8179 else
8180 {
8181 tree declarator;
8182 tree decl;
8183
8184 /* Parse the declarator. */
8185 declarator
8186 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8187 /*ctor_dtor_or_conv_p=*/NULL);
8188 decl = grokdeclarator (declarator, decl_specifiers,
8189 NORMAL, 0, NULL);
8190 /* Turn access control back on for names used during
8191 template instantiation. */
8192 pop_deferring_access_checks ();
8193 /* Do the explicit instantiation. */
8194 do_decl_instantiation (decl, extension_specifier);
8195 }
8196 /* We're done with the instantiation. */
8197 end_explicit_instantiation ();
8198
8199 cp_parser_consume_semicolon_at_end_of_statement (parser);
8200 }
8201
8202 /* Parse an explicit-specialization.
8203
8204 explicit-specialization:
8205 template < > declaration
8206
8207 Although the standard says `declaration', what it really means is:
8208
8209 explicit-specialization:
8210 template <> decl-specifier [opt] init-declarator [opt] ;
8211 template <> function-definition
8212 template <> explicit-specialization
8213 template <> template-declaration */
8214
8215 static void
8216 cp_parser_explicit_specialization (cp_parser* parser)
8217 {
8218 /* Look for the `template' keyword. */
8219 cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8220 /* Look for the `<'. */
8221 cp_parser_require (parser, CPP_LESS, "`<'");
8222 /* Look for the `>'. */
8223 cp_parser_require (parser, CPP_GREATER, "`>'");
8224 /* We have processed another parameter list. */
8225 ++parser->num_template_parameter_lists;
8226 /* Let the front end know that we are beginning a specialization. */
8227 begin_specialization ();
8228
8229 /* If the next keyword is `template', we need to figure out whether
8230 or not we're looking a template-declaration. */
8231 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8232 {
8233 if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8234 && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8235 cp_parser_template_declaration_after_export (parser,
8236 /*member_p=*/false);
8237 else
8238 cp_parser_explicit_specialization (parser);
8239 }
8240 else
8241 /* Parse the dependent declaration. */
8242 cp_parser_single_declaration (parser,
8243 /*member_p=*/false,
8244 /*friend_p=*/NULL);
8245
8246 /* We're done with the specialization. */
8247 end_specialization ();
8248 /* We're done with this parameter list. */
8249 --parser->num_template_parameter_lists;
8250 }
8251
8252 /* Parse a type-specifier.
8253
8254 type-specifier:
8255 simple-type-specifier
8256 class-specifier
8257 enum-specifier
8258 elaborated-type-specifier
8259 cv-qualifier
8260
8261 GNU Extension:
8262
8263 type-specifier:
8264 __complex__
8265
8266 Returns a representation of the type-specifier. If the
8267 type-specifier is a keyword (like `int' or `const', or
8268 `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8269 For a class-specifier, enum-specifier, or elaborated-type-specifier
8270 a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8271
8272 If IS_FRIEND is TRUE then this type-specifier is being declared a
8273 `friend'. If IS_DECLARATION is TRUE, then this type-specifier is
8274 appearing in a decl-specifier-seq.
8275
8276 If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8277 class-specifier, enum-specifier, or elaborated-type-specifier, then
8278 *DECLARES_CLASS_OR_ENUM is set to TRUE. Otherwise, it is set to
8279 FALSE.
8280
8281 If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8282 cv-qualifier, then IS_CV_QUALIFIER is set to TRUE. Otherwise, it
8283 is set to FALSE. */
8284
8285 static tree
8286 cp_parser_type_specifier (cp_parser* parser,
8287 cp_parser_flags flags,
8288 bool is_friend,
8289 bool is_declaration,
8290 bool* declares_class_or_enum,
8291 bool* is_cv_qualifier)
8292 {
8293 tree type_spec = NULL_TREE;
8294 cp_token *token;
8295 enum rid keyword;
8296
8297 /* Assume this type-specifier does not declare a new type. */
8298 if (declares_class_or_enum)
8299 *declares_class_or_enum = false;
8300 /* And that it does not specify a cv-qualifier. */
8301 if (is_cv_qualifier)
8302 *is_cv_qualifier = false;
8303 /* Peek at the next token. */
8304 token = cp_lexer_peek_token (parser->lexer);
8305
8306 /* If we're looking at a keyword, we can use that to guide the
8307 production we choose. */
8308 keyword = token->keyword;
8309 switch (keyword)
8310 {
8311 /* Any of these indicate either a class-specifier, or an
8312 elaborated-type-specifier. */
8313 case RID_CLASS:
8314 case RID_STRUCT:
8315 case RID_UNION:
8316 case RID_ENUM:
8317 /* Parse tentatively so that we can back up if we don't find a
8318 class-specifier or enum-specifier. */
8319 cp_parser_parse_tentatively (parser);
8320 /* Look for the class-specifier or enum-specifier. */
8321 if (keyword == RID_ENUM)
8322 type_spec = cp_parser_enum_specifier (parser);
8323 else
8324 type_spec = cp_parser_class_specifier (parser);
8325
8326 /* If that worked, we're done. */
8327 if (cp_parser_parse_definitely (parser))
8328 {
8329 if (declares_class_or_enum)
8330 *declares_class_or_enum = true;
8331 return type_spec;
8332 }
8333
8334 /* Fall through. */
8335
8336 case RID_TYPENAME:
8337 /* Look for an elaborated-type-specifier. */
8338 type_spec = cp_parser_elaborated_type_specifier (parser,
8339 is_friend,
8340 is_declaration);
8341 /* We're declaring a class or enum -- unless we're using
8342 `typename'. */
8343 if (declares_class_or_enum && keyword != RID_TYPENAME)
8344 *declares_class_or_enum = true;
8345 return type_spec;
8346
8347 case RID_CONST:
8348 case RID_VOLATILE:
8349 case RID_RESTRICT:
8350 type_spec = cp_parser_cv_qualifier_opt (parser);
8351 /* Even though we call a routine that looks for an optional
8352 qualifier, we know that there should be one. */
8353 my_friendly_assert (type_spec != NULL, 20000328);
8354 /* This type-specifier was a cv-qualified. */
8355 if (is_cv_qualifier)
8356 *is_cv_qualifier = true;
8357
8358 return type_spec;
8359
8360 case RID_COMPLEX:
8361 /* The `__complex__' keyword is a GNU extension. */
8362 return cp_lexer_consume_token (parser->lexer)->value;
8363
8364 default:
8365 break;
8366 }
8367
8368 /* If we do not already have a type-specifier, assume we are looking
8369 at a simple-type-specifier. */
8370 type_spec = cp_parser_simple_type_specifier (parser, flags);
8371
8372 /* If we didn't find a type-specifier, and a type-specifier was not
8373 optional in this context, issue an error message. */
8374 if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8375 {
8376 cp_parser_error (parser, "expected type specifier");
8377 return error_mark_node;
8378 }
8379
8380 return type_spec;
8381 }
8382
8383 /* Parse a simple-type-specifier.
8384
8385 simple-type-specifier:
8386 :: [opt] nested-name-specifier [opt] type-name
8387 :: [opt] nested-name-specifier template template-id
8388 char
8389 wchar_t
8390 bool
8391 short
8392 int
8393 long
8394 signed
8395 unsigned
8396 float
8397 double
8398 void
8399
8400 GNU Extension:
8401
8402 simple-type-specifier:
8403 __typeof__ unary-expression
8404 __typeof__ ( type-id )
8405
8406 For the various keywords, the value returned is simply the
8407 TREE_IDENTIFIER representing the keyword. For the first two
8408 productions, the value returned is the indicated TYPE_DECL. */
8409
8410 static tree
8411 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags)
8412 {
8413 tree type = NULL_TREE;
8414 cp_token *token;
8415
8416 /* Peek at the next token. */
8417 token = cp_lexer_peek_token (parser->lexer);
8418
8419 /* If we're looking at a keyword, things are easy. */
8420 switch (token->keyword)
8421 {
8422 case RID_CHAR:
8423 case RID_WCHAR:
8424 case RID_BOOL:
8425 case RID_SHORT:
8426 case RID_INT:
8427 case RID_LONG:
8428 case RID_SIGNED:
8429 case RID_UNSIGNED:
8430 case RID_FLOAT:
8431 case RID_DOUBLE:
8432 case RID_VOID:
8433 /* Consume the token. */
8434 return cp_lexer_consume_token (parser->lexer)->value;
8435
8436 case RID_TYPEOF:
8437 {
8438 tree operand;
8439
8440 /* Consume the `typeof' token. */
8441 cp_lexer_consume_token (parser->lexer);
8442 /* Parse the operand to `typeof' */
8443 operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8444 /* If it is not already a TYPE, take its type. */
8445 if (!TYPE_P (operand))
8446 operand = finish_typeof (operand);
8447
8448 return operand;
8449 }
8450
8451 default:
8452 break;
8453 }
8454
8455 /* The type-specifier must be a user-defined type. */
8456 if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8457 {
8458 /* Don't gobble tokens or issue error messages if this is an
8459 optional type-specifier. */
8460 if (flags & CP_PARSER_FLAGS_OPTIONAL)
8461 cp_parser_parse_tentatively (parser);
8462
8463 /* Look for the optional `::' operator. */
8464 cp_parser_global_scope_opt (parser,
8465 /*current_scope_valid_p=*/false);
8466 /* Look for the nested-name specifier. */
8467 cp_parser_nested_name_specifier_opt (parser,
8468 /*typename_keyword_p=*/false,
8469 /*check_dependency_p=*/true,
8470 /*type_p=*/false);
8471 /* If we have seen a nested-name-specifier, and the next token
8472 is `template', then we are using the template-id production. */
8473 if (parser->scope
8474 && cp_parser_optional_template_keyword (parser))
8475 {
8476 /* Look for the template-id. */
8477 type = cp_parser_template_id (parser,
8478 /*template_keyword_p=*/true,
8479 /*check_dependency_p=*/true);
8480 /* If the template-id did not name a type, we are out of
8481 luck. */
8482 if (TREE_CODE (type) != TYPE_DECL)
8483 {
8484 cp_parser_error (parser, "expected template-id for type");
8485 type = NULL_TREE;
8486 }
8487 }
8488 /* Otherwise, look for a type-name. */
8489 else
8490 {
8491 type = cp_parser_type_name (parser);
8492 if (type == error_mark_node)
8493 type = NULL_TREE;
8494 }
8495
8496 /* If it didn't work out, we don't have a TYPE. */
8497 if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8498 && !cp_parser_parse_definitely (parser))
8499 type = NULL_TREE;
8500 }
8501
8502 /* If we didn't get a type-name, issue an error message. */
8503 if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8504 {
8505 cp_parser_error (parser, "expected type-name");
8506 return error_mark_node;
8507 }
8508
8509 return type;
8510 }
8511
8512 /* Parse a type-name.
8513
8514 type-name:
8515 class-name
8516 enum-name
8517 typedef-name
8518
8519 enum-name:
8520 identifier
8521
8522 typedef-name:
8523 identifier
8524
8525 Returns a TYPE_DECL for the the type. */
8526
8527 static tree
8528 cp_parser_type_name (cp_parser* parser)
8529 {
8530 tree type_decl;
8531 tree identifier;
8532
8533 /* We can't know yet whether it is a class-name or not. */
8534 cp_parser_parse_tentatively (parser);
8535 /* Try a class-name. */
8536 type_decl = cp_parser_class_name (parser,
8537 /*typename_keyword_p=*/false,
8538 /*template_keyword_p=*/false,
8539 /*type_p=*/false,
8540 /*check_dependency_p=*/true,
8541 /*class_head_p=*/false);
8542 /* If it's not a class-name, keep looking. */
8543 if (!cp_parser_parse_definitely (parser))
8544 {
8545 /* It must be a typedef-name or an enum-name. */
8546 identifier = cp_parser_identifier (parser);
8547 if (identifier == error_mark_node)
8548 return error_mark_node;
8549
8550 /* Look up the type-name. */
8551 type_decl = cp_parser_lookup_name_simple (parser, identifier);
8552 /* Issue an error if we did not find a type-name. */
8553 if (TREE_CODE (type_decl) != TYPE_DECL)
8554 {
8555 cp_parser_error (parser, "expected type-name");
8556 type_decl = error_mark_node;
8557 }
8558 /* Remember that the name was used in the definition of the
8559 current class so that we can check later to see if the
8560 meaning would have been different after the class was
8561 entirely defined. */
8562 else if (type_decl != error_mark_node
8563 && !parser->scope)
8564 maybe_note_name_used_in_class (identifier, type_decl);
8565 }
8566
8567 return type_decl;
8568 }
8569
8570
8571 /* Parse an elaborated-type-specifier. Note that the grammar given
8572 here incorporates the resolution to DR68.
8573
8574 elaborated-type-specifier:
8575 class-key :: [opt] nested-name-specifier [opt] identifier
8576 class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
8577 enum :: [opt] nested-name-specifier [opt] identifier
8578 typename :: [opt] nested-name-specifier identifier
8579 typename :: [opt] nested-name-specifier template [opt]
8580 template-id
8581
8582 If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
8583 declared `friend'. If IS_DECLARATION is TRUE, then this
8584 elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
8585 something is being declared.
8586
8587 Returns the TYPE specified. */
8588
8589 static tree
8590 cp_parser_elaborated_type_specifier (cp_parser* parser,
8591 bool is_friend,
8592 bool is_declaration)
8593 {
8594 enum tag_types tag_type;
8595 tree identifier;
8596 tree type = NULL_TREE;
8597
8598 /* See if we're looking at the `enum' keyword. */
8599 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
8600 {
8601 /* Consume the `enum' token. */
8602 cp_lexer_consume_token (parser->lexer);
8603 /* Remember that it's an enumeration type. */
8604 tag_type = enum_type;
8605 }
8606 /* Or, it might be `typename'. */
8607 else if (cp_lexer_next_token_is_keyword (parser->lexer,
8608 RID_TYPENAME))
8609 {
8610 /* Consume the `typename' token. */
8611 cp_lexer_consume_token (parser->lexer);
8612 /* Remember that it's a `typename' type. */
8613 tag_type = typename_type;
8614 /* The `typename' keyword is only allowed in templates. */
8615 if (!processing_template_decl)
8616 pedwarn ("using `typename' outside of template");
8617 }
8618 /* Otherwise it must be a class-key. */
8619 else
8620 {
8621 tag_type = cp_parser_class_key (parser);
8622 if (tag_type == none_type)
8623 return error_mark_node;
8624 }
8625
8626 /* Look for the `::' operator. */
8627 cp_parser_global_scope_opt (parser,
8628 /*current_scope_valid_p=*/false);
8629 /* Look for the nested-name-specifier. */
8630 if (tag_type == typename_type)
8631 {
8632 if (cp_parser_nested_name_specifier (parser,
8633 /*typename_keyword_p=*/true,
8634 /*check_dependency_p=*/true,
8635 /*type_p=*/true)
8636 == error_mark_node)
8637 return error_mark_node;
8638 }
8639 else
8640 /* Even though `typename' is not present, the proposed resolution
8641 to Core Issue 180 says that in `class A<T>::B', `B' should be
8642 considered a type-name, even if `A<T>' is dependent. */
8643 cp_parser_nested_name_specifier_opt (parser,
8644 /*typename_keyword_p=*/true,
8645 /*check_dependency_p=*/true,
8646 /*type_p=*/true);
8647 /* For everything but enumeration types, consider a template-id. */
8648 if (tag_type != enum_type)
8649 {
8650 bool template_p = false;
8651 tree decl;
8652
8653 /* Allow the `template' keyword. */
8654 template_p = cp_parser_optional_template_keyword (parser);
8655 /* If we didn't see `template', we don't know if there's a
8656 template-id or not. */
8657 if (!template_p)
8658 cp_parser_parse_tentatively (parser);
8659 /* Parse the template-id. */
8660 decl = cp_parser_template_id (parser, template_p,
8661 /*check_dependency_p=*/true);
8662 /* If we didn't find a template-id, look for an ordinary
8663 identifier. */
8664 if (!template_p && !cp_parser_parse_definitely (parser))
8665 ;
8666 /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
8667 in effect, then we must assume that, upon instantiation, the
8668 template will correspond to a class. */
8669 else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
8670 && tag_type == typename_type)
8671 type = make_typename_type (parser->scope, decl,
8672 /*complain=*/1);
8673 else
8674 type = TREE_TYPE (decl);
8675 }
8676
8677 /* For an enumeration type, consider only a plain identifier. */
8678 if (!type)
8679 {
8680 identifier = cp_parser_identifier (parser);
8681
8682 if (identifier == error_mark_node)
8683 return error_mark_node;
8684
8685 /* For a `typename', we needn't call xref_tag. */
8686 if (tag_type == typename_type)
8687 return make_typename_type (parser->scope, identifier,
8688 /*complain=*/1);
8689 /* Look up a qualified name in the usual way. */
8690 if (parser->scope)
8691 {
8692 tree decl;
8693
8694 /* In an elaborated-type-specifier, names are assumed to name
8695 types, so we set IS_TYPE to TRUE when calling
8696 cp_parser_lookup_name. */
8697 decl = cp_parser_lookup_name (parser, identifier,
8698 /*is_type=*/true,
8699 /*is_namespace=*/false,
8700 /*check_dependency=*/true);
8701
8702 /* If we are parsing friend declaration, DECL may be a
8703 TEMPLATE_DECL tree node here. However, we need to check
8704 whether this TEMPLATE_DECL results in valid code. Consider
8705 the following example:
8706
8707 namespace N {
8708 template <class T> class C {};
8709 }
8710 class X {
8711 template <class T> friend class N::C; // #1, valid code
8712 };
8713 template <class T> class Y {
8714 friend class N::C; // #2, invalid code
8715 };
8716
8717 For both case #1 and #2, we arrive at a TEMPLATE_DECL after
8718 name lookup of `N::C'. We see that friend declaration must
8719 be template for the code to be valid. Note that
8720 processing_template_decl does not work here since it is
8721 always 1 for the above two cases. */
8722
8723 decl = (cp_parser_maybe_treat_template_as_class
8724 (decl, /*tag_name_p=*/is_friend
8725 && parser->num_template_parameter_lists));
8726
8727 if (TREE_CODE (decl) != TYPE_DECL)
8728 {
8729 error ("expected type-name");
8730 return error_mark_node;
8731 }
8732 else if (TREE_CODE (TREE_TYPE (decl)) == ENUMERAL_TYPE
8733 && tag_type != enum_type)
8734 error ("`%T' referred to as `%s'", TREE_TYPE (decl),
8735 tag_type == record_type ? "struct" : "class");
8736 else if (TREE_CODE (TREE_TYPE (decl)) != ENUMERAL_TYPE
8737 && tag_type == enum_type)
8738 error ("`%T' referred to as enum", TREE_TYPE (decl));
8739
8740 type = TREE_TYPE (decl);
8741 }
8742 else
8743 {
8744 /* An elaborated-type-specifier sometimes introduces a new type and
8745 sometimes names an existing type. Normally, the rule is that it
8746 introduces a new type only if there is not an existing type of
8747 the same name already in scope. For example, given:
8748
8749 struct S {};
8750 void f() { struct S s; }
8751
8752 the `struct S' in the body of `f' is the same `struct S' as in
8753 the global scope; the existing definition is used. However, if
8754 there were no global declaration, this would introduce a new
8755 local class named `S'.
8756
8757 An exception to this rule applies to the following code:
8758
8759 namespace N { struct S; }
8760
8761 Here, the elaborated-type-specifier names a new type
8762 unconditionally; even if there is already an `S' in the
8763 containing scope this declaration names a new type.
8764 This exception only applies if the elaborated-type-specifier
8765 forms the complete declaration:
8766
8767 [class.name]
8768
8769 A declaration consisting solely of `class-key identifier ;' is
8770 either a redeclaration of the name in the current scope or a
8771 forward declaration of the identifier as a class name. It
8772 introduces the name into the current scope.
8773
8774 We are in this situation precisely when the next token is a `;'.
8775
8776 An exception to the exception is that a `friend' declaration does
8777 *not* name a new type; i.e., given:
8778
8779 struct S { friend struct T; };
8780
8781 `T' is not a new type in the scope of `S'.
8782
8783 Also, `new struct S' or `sizeof (struct S)' never results in the
8784 definition of a new type; a new type can only be declared in a
8785 declaration context. */
8786
8787 type = xref_tag (tag_type, identifier,
8788 /*attributes=*/NULL_TREE,
8789 (is_friend
8790 || !is_declaration
8791 || cp_lexer_next_token_is_not (parser->lexer,
8792 CPP_SEMICOLON)));
8793 }
8794 }
8795 if (tag_type != enum_type)
8796 cp_parser_check_class_key (tag_type, type);
8797 return type;
8798 }
8799
8800 /* Parse an enum-specifier.
8801
8802 enum-specifier:
8803 enum identifier [opt] { enumerator-list [opt] }
8804
8805 Returns an ENUM_TYPE representing the enumeration. */
8806
8807 static tree
8808 cp_parser_enum_specifier (cp_parser* parser)
8809 {
8810 cp_token *token;
8811 tree identifier = NULL_TREE;
8812 tree type;
8813
8814 /* Look for the `enum' keyword. */
8815 if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
8816 return error_mark_node;
8817 /* Peek at the next token. */
8818 token = cp_lexer_peek_token (parser->lexer);
8819
8820 /* See if it is an identifier. */
8821 if (token->type == CPP_NAME)
8822 identifier = cp_parser_identifier (parser);
8823
8824 /* Look for the `{'. */
8825 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
8826 return error_mark_node;
8827
8828 /* At this point, we're going ahead with the enum-specifier, even
8829 if some other problem occurs. */
8830 cp_parser_commit_to_tentative_parse (parser);
8831
8832 /* Issue an error message if type-definitions are forbidden here. */
8833 cp_parser_check_type_definition (parser);
8834
8835 /* Create the new type. */
8836 type = start_enum (identifier ? identifier : make_anon_name ());
8837
8838 /* Peek at the next token. */
8839 token = cp_lexer_peek_token (parser->lexer);
8840 /* If it's not a `}', then there are some enumerators. */
8841 if (token->type != CPP_CLOSE_BRACE)
8842 cp_parser_enumerator_list (parser, type);
8843 /* Look for the `}'. */
8844 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
8845
8846 /* Finish up the enumeration. */
8847 finish_enum (type);
8848
8849 return type;
8850 }
8851
8852 /* Parse an enumerator-list. The enumerators all have the indicated
8853 TYPE.
8854
8855 enumerator-list:
8856 enumerator-definition
8857 enumerator-list , enumerator-definition */
8858
8859 static void
8860 cp_parser_enumerator_list (cp_parser* parser, tree type)
8861 {
8862 while (true)
8863 {
8864 cp_token *token;
8865
8866 /* Parse an enumerator-definition. */
8867 cp_parser_enumerator_definition (parser, type);
8868 /* Peek at the next token. */
8869 token = cp_lexer_peek_token (parser->lexer);
8870 /* If it's not a `,', then we've reached the end of the
8871 list. */
8872 if (token->type != CPP_COMMA)
8873 break;
8874 /* Otherwise, consume the `,' and keep going. */
8875 cp_lexer_consume_token (parser->lexer);
8876 /* If the next token is a `}', there is a trailing comma. */
8877 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8878 {
8879 if (pedantic && !in_system_header)
8880 pedwarn ("comma at end of enumerator list");
8881 break;
8882 }
8883 }
8884 }
8885
8886 /* Parse an enumerator-definition. The enumerator has the indicated
8887 TYPE.
8888
8889 enumerator-definition:
8890 enumerator
8891 enumerator = constant-expression
8892
8893 enumerator:
8894 identifier */
8895
8896 static void
8897 cp_parser_enumerator_definition (cp_parser* parser, tree type)
8898 {
8899 cp_token *token;
8900 tree identifier;
8901 tree value;
8902
8903 /* Look for the identifier. */
8904 identifier = cp_parser_identifier (parser);
8905 if (identifier == error_mark_node)
8906 return;
8907
8908 /* Peek at the next token. */
8909 token = cp_lexer_peek_token (parser->lexer);
8910 /* If it's an `=', then there's an explicit value. */
8911 if (token->type == CPP_EQ)
8912 {
8913 /* Consume the `=' token. */
8914 cp_lexer_consume_token (parser->lexer);
8915 /* Parse the value. */
8916 value = cp_parser_constant_expression (parser,
8917 /*allow_non_constant=*/false,
8918 NULL);
8919 }
8920 else
8921 value = NULL_TREE;
8922
8923 /* Create the enumerator. */
8924 build_enumerator (identifier, value, type);
8925 }
8926
8927 /* Parse a namespace-name.
8928
8929 namespace-name:
8930 original-namespace-name
8931 namespace-alias
8932
8933 Returns the NAMESPACE_DECL for the namespace. */
8934
8935 static tree
8936 cp_parser_namespace_name (cp_parser* parser)
8937 {
8938 tree identifier;
8939 tree namespace_decl;
8940
8941 /* Get the name of the namespace. */
8942 identifier = cp_parser_identifier (parser);
8943 if (identifier == error_mark_node)
8944 return error_mark_node;
8945
8946 /* Look up the identifier in the currently active scope. Look only
8947 for namespaces, due to:
8948
8949 [basic.lookup.udir]
8950
8951 When looking up a namespace-name in a using-directive or alias
8952 definition, only namespace names are considered.
8953
8954 And:
8955
8956 [basic.lookup.qual]
8957
8958 During the lookup of a name preceding the :: scope resolution
8959 operator, object, function, and enumerator names are ignored.
8960
8961 (Note that cp_parser_class_or_namespace_name only calls this
8962 function if the token after the name is the scope resolution
8963 operator.) */
8964 namespace_decl = cp_parser_lookup_name (parser, identifier,
8965 /*is_type=*/false,
8966 /*is_namespace=*/true,
8967 /*check_dependency=*/true);
8968 /* If it's not a namespace, issue an error. */
8969 if (namespace_decl == error_mark_node
8970 || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
8971 {
8972 cp_parser_error (parser, "expected namespace-name");
8973 namespace_decl = error_mark_node;
8974 }
8975
8976 return namespace_decl;
8977 }
8978
8979 /* Parse a namespace-definition.
8980
8981 namespace-definition:
8982 named-namespace-definition
8983 unnamed-namespace-definition
8984
8985 named-namespace-definition:
8986 original-namespace-definition
8987 extension-namespace-definition
8988
8989 original-namespace-definition:
8990 namespace identifier { namespace-body }
8991
8992 extension-namespace-definition:
8993 namespace original-namespace-name { namespace-body }
8994
8995 unnamed-namespace-definition:
8996 namespace { namespace-body } */
8997
8998 static void
8999 cp_parser_namespace_definition (cp_parser* parser)
9000 {
9001 tree identifier;
9002
9003 /* Look for the `namespace' keyword. */
9004 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9005
9006 /* Get the name of the namespace. We do not attempt to distinguish
9007 between an original-namespace-definition and an
9008 extension-namespace-definition at this point. The semantic
9009 analysis routines are responsible for that. */
9010 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9011 identifier = cp_parser_identifier (parser);
9012 else
9013 identifier = NULL_TREE;
9014
9015 /* Look for the `{' to start the namespace. */
9016 cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9017 /* Start the namespace. */
9018 push_namespace (identifier);
9019 /* Parse the body of the namespace. */
9020 cp_parser_namespace_body (parser);
9021 /* Finish the namespace. */
9022 pop_namespace ();
9023 /* Look for the final `}'. */
9024 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9025 }
9026
9027 /* Parse a namespace-body.
9028
9029 namespace-body:
9030 declaration-seq [opt] */
9031
9032 static void
9033 cp_parser_namespace_body (cp_parser* parser)
9034 {
9035 cp_parser_declaration_seq_opt (parser);
9036 }
9037
9038 /* Parse a namespace-alias-definition.
9039
9040 namespace-alias-definition:
9041 namespace identifier = qualified-namespace-specifier ; */
9042
9043 static void
9044 cp_parser_namespace_alias_definition (cp_parser* parser)
9045 {
9046 tree identifier;
9047 tree namespace_specifier;
9048
9049 /* Look for the `namespace' keyword. */
9050 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9051 /* Look for the identifier. */
9052 identifier = cp_parser_identifier (parser);
9053 if (identifier == error_mark_node)
9054 return;
9055 /* Look for the `=' token. */
9056 cp_parser_require (parser, CPP_EQ, "`='");
9057 /* Look for the qualified-namespace-specifier. */
9058 namespace_specifier
9059 = cp_parser_qualified_namespace_specifier (parser);
9060 /* Look for the `;' token. */
9061 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9062
9063 /* Register the alias in the symbol table. */
9064 do_namespace_alias (identifier, namespace_specifier);
9065 }
9066
9067 /* Parse a qualified-namespace-specifier.
9068
9069 qualified-namespace-specifier:
9070 :: [opt] nested-name-specifier [opt] namespace-name
9071
9072 Returns a NAMESPACE_DECL corresponding to the specified
9073 namespace. */
9074
9075 static tree
9076 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9077 {
9078 /* Look for the optional `::'. */
9079 cp_parser_global_scope_opt (parser,
9080 /*current_scope_valid_p=*/false);
9081
9082 /* Look for the optional nested-name-specifier. */
9083 cp_parser_nested_name_specifier_opt (parser,
9084 /*typename_keyword_p=*/false,
9085 /*check_dependency_p=*/true,
9086 /*type_p=*/false);
9087
9088 return cp_parser_namespace_name (parser);
9089 }
9090
9091 /* Parse a using-declaration.
9092
9093 using-declaration:
9094 using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9095 using :: unqualified-id ; */
9096
9097 static void
9098 cp_parser_using_declaration (cp_parser* parser)
9099 {
9100 cp_token *token;
9101 bool typename_p = false;
9102 bool global_scope_p;
9103 tree decl;
9104 tree identifier;
9105 tree scope;
9106
9107 /* Look for the `using' keyword. */
9108 cp_parser_require_keyword (parser, RID_USING, "`using'");
9109
9110 /* Peek at the next token. */
9111 token = cp_lexer_peek_token (parser->lexer);
9112 /* See if it's `typename'. */
9113 if (token->keyword == RID_TYPENAME)
9114 {
9115 /* Remember that we've seen it. */
9116 typename_p = true;
9117 /* Consume the `typename' token. */
9118 cp_lexer_consume_token (parser->lexer);
9119 }
9120
9121 /* Look for the optional global scope qualification. */
9122 global_scope_p
9123 = (cp_parser_global_scope_opt (parser,
9124 /*current_scope_valid_p=*/false)
9125 != NULL_TREE);
9126
9127 /* If we saw `typename', or didn't see `::', then there must be a
9128 nested-name-specifier present. */
9129 if (typename_p || !global_scope_p)
9130 cp_parser_nested_name_specifier (parser, typename_p,
9131 /*check_dependency_p=*/true,
9132 /*type_p=*/false);
9133 /* Otherwise, we could be in either of the two productions. In that
9134 case, treat the nested-name-specifier as optional. */
9135 else
9136 cp_parser_nested_name_specifier_opt (parser,
9137 /*typename_keyword_p=*/false,
9138 /*check_dependency_p=*/true,
9139 /*type_p=*/false);
9140
9141 /* Parse the unqualified-id. */
9142 identifier = cp_parser_unqualified_id (parser,
9143 /*template_keyword_p=*/false,
9144 /*check_dependency_p=*/true);
9145
9146 /* The function we call to handle a using-declaration is different
9147 depending on what scope we are in. */
9148 scope = current_scope ();
9149 if (scope && TYPE_P (scope))
9150 {
9151 /* Create the USING_DECL. */
9152 decl = do_class_using_decl (build_nt (SCOPE_REF,
9153 parser->scope,
9154 identifier));
9155 /* Add it to the list of members in this class. */
9156 finish_member_declaration (decl);
9157 }
9158 else
9159 {
9160 decl = cp_parser_lookup_name_simple (parser, identifier);
9161 if (decl == error_mark_node)
9162 {
9163 if (parser->scope && parser->scope != global_namespace)
9164 error ("`%D::%D' has not been declared",
9165 parser->scope, identifier);
9166 else
9167 error ("`::%D' has not been declared", identifier);
9168 }
9169 else if (scope)
9170 do_local_using_decl (decl);
9171 else
9172 do_toplevel_using_decl (decl);
9173 }
9174
9175 /* Look for the final `;'. */
9176 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9177 }
9178
9179 /* Parse a using-directive.
9180
9181 using-directive:
9182 using namespace :: [opt] nested-name-specifier [opt]
9183 namespace-name ; */
9184
9185 static void
9186 cp_parser_using_directive (cp_parser* parser)
9187 {
9188 tree namespace_decl;
9189
9190 /* Look for the `using' keyword. */
9191 cp_parser_require_keyword (parser, RID_USING, "`using'");
9192 /* And the `namespace' keyword. */
9193 cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9194 /* Look for the optional `::' operator. */
9195 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9196 /* And the optional nested-name-specifier. */
9197 cp_parser_nested_name_specifier_opt (parser,
9198 /*typename_keyword_p=*/false,
9199 /*check_dependency_p=*/true,
9200 /*type_p=*/false);
9201 /* Get the namespace being used. */
9202 namespace_decl = cp_parser_namespace_name (parser);
9203 /* Update the symbol table. */
9204 do_using_directive (namespace_decl);
9205 /* Look for the final `;'. */
9206 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9207 }
9208
9209 /* Parse an asm-definition.
9210
9211 asm-definition:
9212 asm ( string-literal ) ;
9213
9214 GNU Extension:
9215
9216 asm-definition:
9217 asm volatile [opt] ( string-literal ) ;
9218 asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9219 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9220 : asm-operand-list [opt] ) ;
9221 asm volatile [opt] ( string-literal : asm-operand-list [opt]
9222 : asm-operand-list [opt]
9223 : asm-operand-list [opt] ) ; */
9224
9225 static void
9226 cp_parser_asm_definition (cp_parser* parser)
9227 {
9228 cp_token *token;
9229 tree string;
9230 tree outputs = NULL_TREE;
9231 tree inputs = NULL_TREE;
9232 tree clobbers = NULL_TREE;
9233 tree asm_stmt;
9234 bool volatile_p = false;
9235 bool extended_p = false;
9236
9237 /* Look for the `asm' keyword. */
9238 cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9239 /* See if the next token is `volatile'. */
9240 if (cp_parser_allow_gnu_extensions_p (parser)
9241 && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9242 {
9243 /* Remember that we saw the `volatile' keyword. */
9244 volatile_p = true;
9245 /* Consume the token. */
9246 cp_lexer_consume_token (parser->lexer);
9247 }
9248 /* Look for the opening `('. */
9249 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9250 /* Look for the string. */
9251 token = cp_parser_require (parser, CPP_STRING, "asm body");
9252 if (!token)
9253 return;
9254 string = token->value;
9255 /* If we're allowing GNU extensions, check for the extended assembly
9256 syntax. Unfortunately, the `:' tokens need not be separated by
9257 a space in C, and so, for compatibility, we tolerate that here
9258 too. Doing that means that we have to treat the `::' operator as
9259 two `:' tokens. */
9260 if (cp_parser_allow_gnu_extensions_p (parser)
9261 && at_function_scope_p ()
9262 && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9263 || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9264 {
9265 bool inputs_p = false;
9266 bool clobbers_p = false;
9267
9268 /* The extended syntax was used. */
9269 extended_p = true;
9270
9271 /* Look for outputs. */
9272 if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9273 {
9274 /* Consume the `:'. */
9275 cp_lexer_consume_token (parser->lexer);
9276 /* Parse the output-operands. */
9277 if (cp_lexer_next_token_is_not (parser->lexer,
9278 CPP_COLON)
9279 && cp_lexer_next_token_is_not (parser->lexer,
9280 CPP_SCOPE)
9281 && cp_lexer_next_token_is_not (parser->lexer,
9282 CPP_CLOSE_PAREN))
9283 outputs = cp_parser_asm_operand_list (parser);
9284 }
9285 /* If the next token is `::', there are no outputs, and the
9286 next token is the beginning of the inputs. */
9287 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9288 {
9289 /* Consume the `::' token. */
9290 cp_lexer_consume_token (parser->lexer);
9291 /* The inputs are coming next. */
9292 inputs_p = true;
9293 }
9294
9295 /* Look for inputs. */
9296 if (inputs_p
9297 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9298 {
9299 if (!inputs_p)
9300 /* Consume the `:'. */
9301 cp_lexer_consume_token (parser->lexer);
9302 /* Parse the output-operands. */
9303 if (cp_lexer_next_token_is_not (parser->lexer,
9304 CPP_COLON)
9305 && cp_lexer_next_token_is_not (parser->lexer,
9306 CPP_SCOPE)
9307 && cp_lexer_next_token_is_not (parser->lexer,
9308 CPP_CLOSE_PAREN))
9309 inputs = cp_parser_asm_operand_list (parser);
9310 }
9311 else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9312 /* The clobbers are coming next. */
9313 clobbers_p = true;
9314
9315 /* Look for clobbers. */
9316 if (clobbers_p
9317 || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9318 {
9319 if (!clobbers_p)
9320 /* Consume the `:'. */
9321 cp_lexer_consume_token (parser->lexer);
9322 /* Parse the clobbers. */
9323 if (cp_lexer_next_token_is_not (parser->lexer,
9324 CPP_CLOSE_PAREN))
9325 clobbers = cp_parser_asm_clobber_list (parser);
9326 }
9327 }
9328 /* Look for the closing `)'. */
9329 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9330 cp_parser_skip_to_closing_parenthesis (parser);
9331 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9332
9333 /* Create the ASM_STMT. */
9334 if (at_function_scope_p ())
9335 {
9336 asm_stmt =
9337 finish_asm_stmt (volatile_p
9338 ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9339 string, outputs, inputs, clobbers);
9340 /* If the extended syntax was not used, mark the ASM_STMT. */
9341 if (!extended_p)
9342 ASM_INPUT_P (asm_stmt) = 1;
9343 }
9344 else
9345 assemble_asm (string);
9346 }
9347
9348 /* Declarators [gram.dcl.decl] */
9349
9350 /* Parse an init-declarator.
9351
9352 init-declarator:
9353 declarator initializer [opt]
9354
9355 GNU Extension:
9356
9357 init-declarator:
9358 declarator asm-specification [opt] attributes [opt] initializer [opt]
9359
9360 The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9361 Returns a representation of the entity declared. If MEMBER_P is TRUE,
9362 then this declarator appears in a class scope. The new DECL created
9363 by this declarator is returned.
9364
9365 If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9366 for a function-definition here as well. If the declarator is a
9367 declarator for a function-definition, *FUNCTION_DEFINITION_P will
9368 be TRUE upon return. By that point, the function-definition will
9369 have been completely parsed.
9370
9371 FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9372 is FALSE. */
9373
9374 static tree
9375 cp_parser_init_declarator (cp_parser* parser,
9376 tree decl_specifiers,
9377 tree prefix_attributes,
9378 bool function_definition_allowed_p,
9379 bool member_p,
9380 bool* function_definition_p)
9381 {
9382 cp_token *token;
9383 tree declarator;
9384 tree attributes;
9385 tree asm_specification;
9386 tree initializer;
9387 tree decl = NULL_TREE;
9388 tree scope;
9389 bool is_initialized;
9390 bool is_parenthesized_init;
9391 bool ctor_dtor_or_conv_p;
9392 bool friend_p;
9393
9394 /* Assume that this is not the declarator for a function
9395 definition. */
9396 if (function_definition_p)
9397 *function_definition_p = false;
9398
9399 /* Defer access checks while parsing the declarator; we cannot know
9400 what names are accessible until we know what is being
9401 declared. */
9402 resume_deferring_access_checks ();
9403
9404 /* Parse the declarator. */
9405 declarator
9406 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9407 &ctor_dtor_or_conv_p);
9408 /* Gather up the deferred checks. */
9409 stop_deferring_access_checks ();
9410
9411 /* If the DECLARATOR was erroneous, there's no need to go
9412 further. */
9413 if (declarator == error_mark_node)
9414 return error_mark_node;
9415
9416 /* Figure out what scope the entity declared by the DECLARATOR is
9417 located in. `grokdeclarator' sometimes changes the scope, so
9418 we compute it now. */
9419 scope = get_scope_of_declarator (declarator);
9420
9421 /* If we're allowing GNU extensions, look for an asm-specification
9422 and attributes. */
9423 if (cp_parser_allow_gnu_extensions_p (parser))
9424 {
9425 /* Look for an asm-specification. */
9426 asm_specification = cp_parser_asm_specification_opt (parser);
9427 /* And attributes. */
9428 attributes = cp_parser_attributes_opt (parser);
9429 }
9430 else
9431 {
9432 asm_specification = NULL_TREE;
9433 attributes = NULL_TREE;
9434 }
9435
9436 /* Peek at the next token. */
9437 token = cp_lexer_peek_token (parser->lexer);
9438 /* Check to see if the token indicates the start of a
9439 function-definition. */
9440 if (cp_parser_token_starts_function_definition_p (token))
9441 {
9442 if (!function_definition_allowed_p)
9443 {
9444 /* If a function-definition should not appear here, issue an
9445 error message. */
9446 cp_parser_error (parser,
9447 "a function-definition is not allowed here");
9448 return error_mark_node;
9449 }
9450 else
9451 {
9452 /* Neither attributes nor an asm-specification are allowed
9453 on a function-definition. */
9454 if (asm_specification)
9455 error ("an asm-specification is not allowed on a function-definition");
9456 if (attributes)
9457 error ("attributes are not allowed on a function-definition");
9458 /* This is a function-definition. */
9459 *function_definition_p = true;
9460
9461 /* Parse the function definition. */
9462 decl = (cp_parser_function_definition_from_specifiers_and_declarator
9463 (parser, decl_specifiers, prefix_attributes, declarator));
9464
9465 return decl;
9466 }
9467 }
9468
9469 /* [dcl.dcl]
9470
9471 Only in function declarations for constructors, destructors, and
9472 type conversions can the decl-specifier-seq be omitted.
9473
9474 We explicitly postpone this check past the point where we handle
9475 function-definitions because we tolerate function-definitions
9476 that are missing their return types in some modes. */
9477 if (!decl_specifiers && !ctor_dtor_or_conv_p)
9478 {
9479 cp_parser_error (parser,
9480 "expected constructor, destructor, or type conversion");
9481 return error_mark_node;
9482 }
9483
9484 /* An `=' or an `(' indicates an initializer. */
9485 is_initialized = (token->type == CPP_EQ
9486 || token->type == CPP_OPEN_PAREN);
9487 /* If the init-declarator isn't initialized and isn't followed by a
9488 `,' or `;', it's not a valid init-declarator. */
9489 if (!is_initialized
9490 && token->type != CPP_COMMA
9491 && token->type != CPP_SEMICOLON)
9492 {
9493 cp_parser_error (parser, "expected init-declarator");
9494 return error_mark_node;
9495 }
9496
9497 /* Because start_decl has side-effects, we should only call it if we
9498 know we're going ahead. By this point, we know that we cannot
9499 possibly be looking at any other construct. */
9500 cp_parser_commit_to_tentative_parse (parser);
9501
9502 /* Check to see whether or not this declaration is a friend. */
9503 friend_p = cp_parser_friend_p (decl_specifiers);
9504
9505 /* Check that the number of template-parameter-lists is OK. */
9506 if (!cp_parser_check_declarator_template_parameters (parser,
9507 declarator))
9508 return error_mark_node;
9509
9510 /* Enter the newly declared entry in the symbol table. If we're
9511 processing a declaration in a class-specifier, we wait until
9512 after processing the initializer. */
9513 if (!member_p)
9514 {
9515 if (parser->in_unbraced_linkage_specification_p)
9516 {
9517 decl_specifiers = tree_cons (error_mark_node,
9518 get_identifier ("extern"),
9519 decl_specifiers);
9520 have_extern_spec = false;
9521 }
9522 decl = start_decl (declarator,
9523 decl_specifiers,
9524 is_initialized,
9525 attributes,
9526 prefix_attributes);
9527 }
9528
9529 /* Enter the SCOPE. That way unqualified names appearing in the
9530 initializer will be looked up in SCOPE. */
9531 if (scope)
9532 push_scope (scope);
9533
9534 /* Perform deferred access control checks, now that we know in which
9535 SCOPE the declared entity resides. */
9536 if (!member_p && decl)
9537 {
9538 tree saved_current_function_decl = NULL_TREE;
9539
9540 /* If the entity being declared is a function, pretend that we
9541 are in its scope. If it is a `friend', it may have access to
9542 things that would not otherwise be accessible. */
9543 if (TREE_CODE (decl) == FUNCTION_DECL)
9544 {
9545 saved_current_function_decl = current_function_decl;
9546 current_function_decl = decl;
9547 }
9548
9549 /* Perform the access control checks for the declarator and the
9550 the decl-specifiers. */
9551 perform_deferred_access_checks ();
9552
9553 /* Restore the saved value. */
9554 if (TREE_CODE (decl) == FUNCTION_DECL)
9555 current_function_decl = saved_current_function_decl;
9556 }
9557
9558 /* Parse the initializer. */
9559 if (is_initialized)
9560 initializer = cp_parser_initializer (parser, &is_parenthesized_init);
9561 else
9562 {
9563 initializer = NULL_TREE;
9564 is_parenthesized_init = false;
9565 }
9566
9567 /* The old parser allows attributes to appear after a parenthesized
9568 initializer. Mark Mitchell proposed removing this functionality
9569 on the GCC mailing lists on 2002-08-13. This parser accepts the
9570 attributes -- but ignores them. */
9571 if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
9572 if (cp_parser_attributes_opt (parser))
9573 warning ("attributes after parenthesized initializer ignored");
9574
9575 /* Leave the SCOPE, now that we have processed the initializer. It
9576 is important to do this before calling cp_finish_decl because it
9577 makes decisions about whether to create DECL_STMTs or not based
9578 on the current scope. */
9579 if (scope)
9580 pop_scope (scope);
9581
9582 /* For an in-class declaration, use `grokfield' to create the
9583 declaration. */
9584 if (member_p)
9585 {
9586 decl = grokfield (declarator, decl_specifiers,
9587 initializer, /*asmspec=*/NULL_TREE,
9588 /*attributes=*/NULL_TREE);
9589 if (decl && TREE_CODE (decl) == FUNCTION_DECL)
9590 cp_parser_save_default_args (parser, decl);
9591 }
9592
9593 /* Finish processing the declaration. But, skip friend
9594 declarations. */
9595 if (!friend_p && decl)
9596 cp_finish_decl (decl,
9597 initializer,
9598 asm_specification,
9599 /* If the initializer is in parentheses, then this is
9600 a direct-initialization, which means that an
9601 `explicit' constructor is OK. Otherwise, an
9602 `explicit' constructor cannot be used. */
9603 ((is_parenthesized_init || !is_initialized)
9604 ? 0 : LOOKUP_ONLYCONVERTING));
9605
9606 return decl;
9607 }
9608
9609 /* Parse a declarator.
9610
9611 declarator:
9612 direct-declarator
9613 ptr-operator declarator
9614
9615 abstract-declarator:
9616 ptr-operator abstract-declarator [opt]
9617 direct-abstract-declarator
9618
9619 GNU Extensions:
9620
9621 declarator:
9622 attributes [opt] direct-declarator
9623 attributes [opt] ptr-operator declarator
9624
9625 abstract-declarator:
9626 attributes [opt] ptr-operator abstract-declarator [opt]
9627 attributes [opt] direct-abstract-declarator
9628
9629 Returns a representation of the declarator. If the declarator has
9630 the form `* declarator', then an INDIRECT_REF is returned, whose
9631 only operand is the sub-declarator. Analogously, `& declarator' is
9632 represented as an ADDR_EXPR. For `X::* declarator', a SCOPE_REF is
9633 used. The first operand is the TYPE for `X'. The second operand
9634 is an INDIRECT_REF whose operand is the sub-declarator.
9635
9636 Otherwise, the representation is as for a direct-declarator.
9637
9638 (It would be better to define a structure type to represent
9639 declarators, rather than abusing `tree' nodes to represent
9640 declarators. That would be much clearer and save some memory.
9641 There is no reason for declarators to be garbage-collected, for
9642 example; they are created during parser and no longer needed after
9643 `grokdeclarator' has been called.)
9644
9645 For a ptr-operator that has the optional cv-qualifier-seq,
9646 cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
9647 node.
9648
9649 If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is set to
9650 true if this declarator represents a constructor, destructor, or
9651 type conversion operator. Otherwise, it is set to false.
9652
9653 (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
9654 a decl-specifier-seq unless it declares a constructor, destructor,
9655 or conversion. It might seem that we could check this condition in
9656 semantic analysis, rather than parsing, but that makes it difficult
9657 to handle something like `f()'. We want to notice that there are
9658 no decl-specifiers, and therefore realize that this is an
9659 expression, not a declaration.) */
9660
9661 static tree
9662 cp_parser_declarator (cp_parser* parser,
9663 cp_parser_declarator_kind dcl_kind,
9664 bool* ctor_dtor_or_conv_p)
9665 {
9666 cp_token *token;
9667 tree declarator;
9668 enum tree_code code;
9669 tree cv_qualifier_seq;
9670 tree class_type;
9671 tree attributes = NULL_TREE;
9672
9673 /* Assume this is not a constructor, destructor, or type-conversion
9674 operator. */
9675 if (ctor_dtor_or_conv_p)
9676 *ctor_dtor_or_conv_p = false;
9677
9678 if (cp_parser_allow_gnu_extensions_p (parser))
9679 attributes = cp_parser_attributes_opt (parser);
9680
9681 /* Peek at the next token. */
9682 token = cp_lexer_peek_token (parser->lexer);
9683
9684 /* Check for the ptr-operator production. */
9685 cp_parser_parse_tentatively (parser);
9686 /* Parse the ptr-operator. */
9687 code = cp_parser_ptr_operator (parser,
9688 &class_type,
9689 &cv_qualifier_seq);
9690 /* If that worked, then we have a ptr-operator. */
9691 if (cp_parser_parse_definitely (parser))
9692 {
9693 /* The dependent declarator is optional if we are parsing an
9694 abstract-declarator. */
9695 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9696 cp_parser_parse_tentatively (parser);
9697
9698 /* Parse the dependent declarator. */
9699 declarator = cp_parser_declarator (parser, dcl_kind,
9700 /*ctor_dtor_or_conv_p=*/NULL);
9701
9702 /* If we are parsing an abstract-declarator, we must handle the
9703 case where the dependent declarator is absent. */
9704 if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
9705 && !cp_parser_parse_definitely (parser))
9706 declarator = NULL_TREE;
9707
9708 /* Build the representation of the ptr-operator. */
9709 if (code == INDIRECT_REF)
9710 declarator = make_pointer_declarator (cv_qualifier_seq,
9711 declarator);
9712 else
9713 declarator = make_reference_declarator (cv_qualifier_seq,
9714 declarator);
9715 /* Handle the pointer-to-member case. */
9716 if (class_type)
9717 declarator = build_nt (SCOPE_REF, class_type, declarator);
9718 }
9719 /* Everything else is a direct-declarator. */
9720 else
9721 declarator = cp_parser_direct_declarator (parser,
9722 dcl_kind,
9723 ctor_dtor_or_conv_p);
9724
9725 if (attributes && declarator != error_mark_node)
9726 declarator = tree_cons (attributes, declarator, NULL_TREE);
9727
9728 return declarator;
9729 }
9730
9731 /* Parse a direct-declarator or direct-abstract-declarator.
9732
9733 direct-declarator:
9734 declarator-id
9735 direct-declarator ( parameter-declaration-clause )
9736 cv-qualifier-seq [opt]
9737 exception-specification [opt]
9738 direct-declarator [ constant-expression [opt] ]
9739 ( declarator )
9740
9741 direct-abstract-declarator:
9742 direct-abstract-declarator [opt]
9743 ( parameter-declaration-clause )
9744 cv-qualifier-seq [opt]
9745 exception-specification [opt]
9746 direct-abstract-declarator [opt] [ constant-expression [opt] ]
9747 ( abstract-declarator )
9748
9749 Returns a representation of the declarator. DCL_KIND is
9750 CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
9751 direct-abstract-declarator. It is CP_PARSER_DECLARATOR_NAMED, if
9752 we are parsing a direct-declarator. It is
9753 CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
9754 of ambiguity we prefer an abstract declarator, as per
9755 [dcl.ambig.res]. CTOR_DTOR_OR_CONV_P is as for
9756 cp_parser_declarator.
9757
9758 For the declarator-id production, the representation is as for an
9759 id-expression, except that a qualified name is represented as a
9760 SCOPE_REF. A function-declarator is represented as a CALL_EXPR;
9761 see the documentation of the FUNCTION_DECLARATOR_* macros for
9762 information about how to find the various declarator components.
9763 An array-declarator is represented as an ARRAY_REF. The
9764 direct-declarator is the first operand; the constant-expression
9765 indicating the size of the array is the second operand. */
9766
9767 static tree
9768 cp_parser_direct_declarator (cp_parser* parser,
9769 cp_parser_declarator_kind dcl_kind,
9770 bool* ctor_dtor_or_conv_p)
9771 {
9772 cp_token *token;
9773 tree declarator = NULL_TREE;
9774 tree scope = NULL_TREE;
9775 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
9776 bool saved_in_declarator_p = parser->in_declarator_p;
9777 bool first = true;
9778
9779 while (true)
9780 {
9781 /* Peek at the next token. */
9782 token = cp_lexer_peek_token (parser->lexer);
9783 if (token->type == CPP_OPEN_PAREN)
9784 {
9785 /* This is either a parameter-declaration-clause, or a
9786 parenthesized declarator. When we know we are parsing a
9787 named declarator, it must be a parenthesized declarator
9788 if FIRST is true. For instance, `(int)' is a
9789 parameter-declaration-clause, with an omitted
9790 direct-abstract-declarator. But `((*))', is a
9791 parenthesized abstract declarator. Finally, when T is a
9792 template parameter `(T)' is a
9793 parameter-declaration-clause, and not a parenthesized
9794 named declarator.
9795
9796 We first try and parse a parameter-declaration-clause,
9797 and then try a nested declarator (if FIRST is true).
9798
9799 It is not an error for it not to be a
9800 parameter-declaration-clause, even when FIRST is
9801 false. Consider,
9802
9803 int i (int);
9804 int i (3);
9805
9806 The first is the declaration of a function while the
9807 second is a the definition of a variable, including its
9808 initializer.
9809
9810 Having seen only the parenthesis, we cannot know which of
9811 these two alternatives should be selected. Even more
9812 complex are examples like:
9813
9814 int i (int (a));
9815 int i (int (3));
9816
9817 The former is a function-declaration; the latter is a
9818 variable initialization.
9819
9820 Thus again, we try a parameter-declaration-clause, and if
9821 that fails, we back out and return. */
9822
9823 if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9824 {
9825 tree params;
9826
9827 cp_parser_parse_tentatively (parser);
9828
9829 /* Consume the `('. */
9830 cp_lexer_consume_token (parser->lexer);
9831 if (first)
9832 {
9833 /* If this is going to be an abstract declarator, we're
9834 in a declarator and we can't have default args. */
9835 parser->default_arg_ok_p = false;
9836 parser->in_declarator_p = true;
9837 }
9838
9839 /* Parse the parameter-declaration-clause. */
9840 params = cp_parser_parameter_declaration_clause (parser);
9841
9842 /* If all went well, parse the cv-qualifier-seq and the
9843 exception-specification. */
9844 if (cp_parser_parse_definitely (parser))
9845 {
9846 tree cv_qualifiers;
9847 tree exception_specification;
9848
9849 first = false;
9850 /* Consume the `)'. */
9851 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
9852
9853 /* Parse the cv-qualifier-seq. */
9854 cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
9855 /* And the exception-specification. */
9856 exception_specification
9857 = cp_parser_exception_specification_opt (parser);
9858
9859 /* Create the function-declarator. */
9860 declarator = make_call_declarator (declarator,
9861 params,
9862 cv_qualifiers,
9863 exception_specification);
9864 /* Any subsequent parameter lists are to do with
9865 return type, so are not those of the declared
9866 function. */
9867 parser->default_arg_ok_p = false;
9868
9869 /* Repeat the main loop. */
9870 continue;
9871 }
9872 }
9873
9874 /* If this is the first, we can try a parenthesized
9875 declarator. */
9876 if (first)
9877 {
9878 parser->default_arg_ok_p = saved_default_arg_ok_p;
9879 parser->in_declarator_p = saved_in_declarator_p;
9880
9881 /* Consume the `('. */
9882 cp_lexer_consume_token (parser->lexer);
9883 /* Parse the nested declarator. */
9884 declarator
9885 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p);
9886 first = false;
9887 /* Expect a `)'. */
9888 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9889 declarator = error_mark_node;
9890 if (declarator == error_mark_node)
9891 break;
9892
9893 goto handle_declarator;
9894 }
9895 /* Otherwise, we must be done. */
9896 else
9897 break;
9898 }
9899 else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
9900 && token->type == CPP_OPEN_SQUARE)
9901 {
9902 /* Parse an array-declarator. */
9903 tree bounds;
9904
9905 first = false;
9906 parser->default_arg_ok_p = false;
9907 parser->in_declarator_p = true;
9908 /* Consume the `['. */
9909 cp_lexer_consume_token (parser->lexer);
9910 /* Peek at the next token. */
9911 token = cp_lexer_peek_token (parser->lexer);
9912 /* If the next token is `]', then there is no
9913 constant-expression. */
9914 if (token->type != CPP_CLOSE_SQUARE)
9915 {
9916 bool non_constant_p;
9917
9918 bounds
9919 = cp_parser_constant_expression (parser,
9920 /*allow_non_constant=*/true,
9921 &non_constant_p);
9922 /* If we're in a template, but the constant-expression
9923 isn't value dependent, simplify it. We're supposed
9924 to treat:
9925
9926 template <typename T> void f(T[1 + 1]);
9927 template <typename T> void f(T[2]);
9928
9929 as two declarations of the same function, for
9930 example. */
9931 if (processing_template_decl
9932 && !non_constant_p
9933 && !value_dependent_expression_p (bounds))
9934 {
9935 HOST_WIDE_INT saved_processing_template_decl;
9936
9937 saved_processing_template_decl = processing_template_decl;
9938 processing_template_decl = 0;
9939 bounds = tsubst_copy_and_build (bounds,
9940 /*args=*/NULL_TREE,
9941 tf_error,
9942 /*in_decl=*/NULL_TREE);
9943 processing_template_decl = saved_processing_template_decl;
9944 }
9945 }
9946 else
9947 bounds = NULL_TREE;
9948 /* Look for the closing `]'. */
9949 if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
9950 {
9951 declarator = error_mark_node;
9952 break;
9953 }
9954
9955 declarator = build_nt (ARRAY_REF, declarator, bounds);
9956 }
9957 else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
9958 {
9959 /* Parse a declarator_id */
9960 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9961 cp_parser_parse_tentatively (parser);
9962 declarator = cp_parser_declarator_id (parser);
9963 if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
9964 {
9965 if (!cp_parser_parse_definitely (parser))
9966 declarator = error_mark_node;
9967 else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
9968 {
9969 cp_parser_error (parser, "expected unqualified-id");
9970 declarator = error_mark_node;
9971 }
9972 }
9973
9974 if (declarator == error_mark_node)
9975 break;
9976
9977 if (TREE_CODE (declarator) == SCOPE_REF)
9978 {
9979 tree scope = TREE_OPERAND (declarator, 0);
9980
9981 /* In the declaration of a member of a template class
9982 outside of the class itself, the SCOPE will sometimes
9983 be a TYPENAME_TYPE. For example, given:
9984
9985 template <typename T>
9986 int S<T>::R::i = 3;
9987
9988 the SCOPE will be a TYPENAME_TYPE for `S<T>::R'. In
9989 this context, we must resolve S<T>::R to an ordinary
9990 type, rather than a typename type.
9991
9992 The reason we normally avoid resolving TYPENAME_TYPEs
9993 is that a specialization of `S' might render
9994 `S<T>::R' not a type. However, if `S' is
9995 specialized, then this `i' will not be used, so there
9996 is no harm in resolving the types here. */
9997 if (TREE_CODE (scope) == TYPENAME_TYPE)
9998 {
9999 tree type;
10000
10001 /* Resolve the TYPENAME_TYPE. */
10002 type = resolve_typename_type (scope,
10003 /*only_current_p=*/false);
10004 /* If that failed, the declarator is invalid. */
10005 if (type != error_mark_node)
10006 scope = type;
10007 /* Build a new DECLARATOR. */
10008 declarator = build_nt (SCOPE_REF,
10009 scope,
10010 TREE_OPERAND (declarator, 1));
10011 }
10012 }
10013
10014 /* Check to see whether the declarator-id names a constructor,
10015 destructor, or conversion. */
10016 if (declarator && ctor_dtor_or_conv_p
10017 && ((TREE_CODE (declarator) == SCOPE_REF
10018 && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10019 || (TREE_CODE (declarator) != SCOPE_REF
10020 && at_class_scope_p ())))
10021 {
10022 tree unqualified_name;
10023 tree class_type;
10024
10025 /* Get the unqualified part of the name. */
10026 if (TREE_CODE (declarator) == SCOPE_REF)
10027 {
10028 class_type = TREE_OPERAND (declarator, 0);
10029 unqualified_name = TREE_OPERAND (declarator, 1);
10030 }
10031 else
10032 {
10033 class_type = current_class_type;
10034 unqualified_name = declarator;
10035 }
10036
10037 /* See if it names ctor, dtor or conv. */
10038 if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10039 || IDENTIFIER_TYPENAME_P (unqualified_name)
10040 || constructor_name_p (unqualified_name, class_type))
10041 *ctor_dtor_or_conv_p = true;
10042 }
10043
10044 handle_declarator:;
10045 scope = get_scope_of_declarator (declarator);
10046 if (scope)
10047 /* Any names that appear after the declarator-id for a member
10048 are looked up in the containing scope. */
10049 push_scope (scope);
10050 parser->in_declarator_p = true;
10051 if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10052 || (declarator
10053 && (TREE_CODE (declarator) == SCOPE_REF
10054 || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10055 /* Default args are only allowed on function
10056 declarations. */
10057 parser->default_arg_ok_p = saved_default_arg_ok_p;
10058 else
10059 parser->default_arg_ok_p = false;
10060
10061 first = false;
10062 }
10063 /* We're done. */
10064 else
10065 break;
10066 }
10067
10068 /* For an abstract declarator, we might wind up with nothing at this
10069 point. That's an error; the declarator is not optional. */
10070 if (!declarator)
10071 cp_parser_error (parser, "expected declarator");
10072
10073 /* If we entered a scope, we must exit it now. */
10074 if (scope)
10075 pop_scope (scope);
10076
10077 parser->default_arg_ok_p = saved_default_arg_ok_p;
10078 parser->in_declarator_p = saved_in_declarator_p;
10079
10080 return declarator;
10081 }
10082
10083 /* Parse a ptr-operator.
10084
10085 ptr-operator:
10086 * cv-qualifier-seq [opt]
10087 &
10088 :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10089
10090 GNU Extension:
10091
10092 ptr-operator:
10093 & cv-qualifier-seq [opt]
10094
10095 Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10096 used. Returns ADDR_EXPR if a reference was used. In the
10097 case of a pointer-to-member, *TYPE is filled in with the
10098 TYPE containing the member. *CV_QUALIFIER_SEQ is filled in
10099 with the cv-qualifier-seq, or NULL_TREE, if there are no
10100 cv-qualifiers. Returns ERROR_MARK if an error occurred. */
10101
10102 static enum tree_code
10103 cp_parser_ptr_operator (cp_parser* parser,
10104 tree* type,
10105 tree* cv_qualifier_seq)
10106 {
10107 enum tree_code code = ERROR_MARK;
10108 cp_token *token;
10109
10110 /* Assume that it's not a pointer-to-member. */
10111 *type = NULL_TREE;
10112 /* And that there are no cv-qualifiers. */
10113 *cv_qualifier_seq = NULL_TREE;
10114
10115 /* Peek at the next token. */
10116 token = cp_lexer_peek_token (parser->lexer);
10117 /* If it's a `*' or `&' we have a pointer or reference. */
10118 if (token->type == CPP_MULT || token->type == CPP_AND)
10119 {
10120 /* Remember which ptr-operator we were processing. */
10121 code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10122
10123 /* Consume the `*' or `&'. */
10124 cp_lexer_consume_token (parser->lexer);
10125
10126 /* A `*' can be followed by a cv-qualifier-seq, and so can a
10127 `&', if we are allowing GNU extensions. (The only qualifier
10128 that can legally appear after `&' is `restrict', but that is
10129 enforced during semantic analysis. */
10130 if (code == INDIRECT_REF
10131 || cp_parser_allow_gnu_extensions_p (parser))
10132 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10133 }
10134 else
10135 {
10136 /* Try the pointer-to-member case. */
10137 cp_parser_parse_tentatively (parser);
10138 /* Look for the optional `::' operator. */
10139 cp_parser_global_scope_opt (parser,
10140 /*current_scope_valid_p=*/false);
10141 /* Look for the nested-name specifier. */
10142 cp_parser_nested_name_specifier (parser,
10143 /*typename_keyword_p=*/false,
10144 /*check_dependency_p=*/true,
10145 /*type_p=*/false);
10146 /* If we found it, and the next token is a `*', then we are
10147 indeed looking at a pointer-to-member operator. */
10148 if (!cp_parser_error_occurred (parser)
10149 && cp_parser_require (parser, CPP_MULT, "`*'"))
10150 {
10151 /* The type of which the member is a member is given by the
10152 current SCOPE. */
10153 *type = parser->scope;
10154 /* The next name will not be qualified. */
10155 parser->scope = NULL_TREE;
10156 parser->qualifying_scope = NULL_TREE;
10157 parser->object_scope = NULL_TREE;
10158 /* Indicate that the `*' operator was used. */
10159 code = INDIRECT_REF;
10160 /* Look for the optional cv-qualifier-seq. */
10161 *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10162 }
10163 /* If that didn't work we don't have a ptr-operator. */
10164 if (!cp_parser_parse_definitely (parser))
10165 cp_parser_error (parser, "expected ptr-operator");
10166 }
10167
10168 return code;
10169 }
10170
10171 /* Parse an (optional) cv-qualifier-seq.
10172
10173 cv-qualifier-seq:
10174 cv-qualifier cv-qualifier-seq [opt]
10175
10176 Returns a TREE_LIST. The TREE_VALUE of each node is the
10177 representation of a cv-qualifier. */
10178
10179 static tree
10180 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10181 {
10182 tree cv_qualifiers = NULL_TREE;
10183
10184 while (true)
10185 {
10186 tree cv_qualifier;
10187
10188 /* Look for the next cv-qualifier. */
10189 cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10190 /* If we didn't find one, we're done. */
10191 if (!cv_qualifier)
10192 break;
10193
10194 /* Add this cv-qualifier to the list. */
10195 cv_qualifiers
10196 = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10197 }
10198
10199 /* We built up the list in reverse order. */
10200 return nreverse (cv_qualifiers);
10201 }
10202
10203 /* Parse an (optional) cv-qualifier.
10204
10205 cv-qualifier:
10206 const
10207 volatile
10208
10209 GNU Extension:
10210
10211 cv-qualifier:
10212 __restrict__ */
10213
10214 static tree
10215 cp_parser_cv_qualifier_opt (cp_parser* parser)
10216 {
10217 cp_token *token;
10218 tree cv_qualifier = NULL_TREE;
10219
10220 /* Peek at the next token. */
10221 token = cp_lexer_peek_token (parser->lexer);
10222 /* See if it's a cv-qualifier. */
10223 switch (token->keyword)
10224 {
10225 case RID_CONST:
10226 case RID_VOLATILE:
10227 case RID_RESTRICT:
10228 /* Save the value of the token. */
10229 cv_qualifier = token->value;
10230 /* Consume the token. */
10231 cp_lexer_consume_token (parser->lexer);
10232 break;
10233
10234 default:
10235 break;
10236 }
10237
10238 return cv_qualifier;
10239 }
10240
10241 /* Parse a declarator-id.
10242
10243 declarator-id:
10244 id-expression
10245 :: [opt] nested-name-specifier [opt] type-name
10246
10247 In the `id-expression' case, the value returned is as for
10248 cp_parser_id_expression if the id-expression was an unqualified-id.
10249 If the id-expression was a qualified-id, then a SCOPE_REF is
10250 returned. The first operand is the scope (either a NAMESPACE_DECL
10251 or TREE_TYPE), but the second is still just a representation of an
10252 unqualified-id. */
10253
10254 static tree
10255 cp_parser_declarator_id (cp_parser* parser)
10256 {
10257 tree id_expression;
10258
10259 /* The expression must be an id-expression. Assume that qualified
10260 names are the names of types so that:
10261
10262 template <class T>
10263 int S<T>::R::i = 3;
10264
10265 will work; we must treat `S<T>::R' as the name of a type.
10266 Similarly, assume that qualified names are templates, where
10267 required, so that:
10268
10269 template <class T>
10270 int S<T>::R<T>::i = 3;
10271
10272 will work, too. */
10273 id_expression = cp_parser_id_expression (parser,
10274 /*template_keyword_p=*/false,
10275 /*check_dependency_p=*/false,
10276 /*template_p=*/NULL);
10277 /* If the name was qualified, create a SCOPE_REF to represent
10278 that. */
10279 if (parser->scope)
10280 {
10281 id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10282 parser->scope = NULL_TREE;
10283 }
10284
10285 return id_expression;
10286 }
10287
10288 /* Parse a type-id.
10289
10290 type-id:
10291 type-specifier-seq abstract-declarator [opt]
10292
10293 Returns the TYPE specified. */
10294
10295 static tree
10296 cp_parser_type_id (cp_parser* parser)
10297 {
10298 tree type_specifier_seq;
10299 tree abstract_declarator;
10300
10301 /* Parse the type-specifier-seq. */
10302 type_specifier_seq
10303 = cp_parser_type_specifier_seq (parser);
10304 if (type_specifier_seq == error_mark_node)
10305 return error_mark_node;
10306
10307 /* There might or might not be an abstract declarator. */
10308 cp_parser_parse_tentatively (parser);
10309 /* Look for the declarator. */
10310 abstract_declarator
10311 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL);
10312 /* Check to see if there really was a declarator. */
10313 if (!cp_parser_parse_definitely (parser))
10314 abstract_declarator = NULL_TREE;
10315
10316 return groktypename (build_tree_list (type_specifier_seq,
10317 abstract_declarator));
10318 }
10319
10320 /* Parse a type-specifier-seq.
10321
10322 type-specifier-seq:
10323 type-specifier type-specifier-seq [opt]
10324
10325 GNU extension:
10326
10327 type-specifier-seq:
10328 attributes type-specifier-seq [opt]
10329
10330 Returns a TREE_LIST. Either the TREE_VALUE of each node is a
10331 type-specifier, or the TREE_PURPOSE is a list of attributes. */
10332
10333 static tree
10334 cp_parser_type_specifier_seq (cp_parser* parser)
10335 {
10336 bool seen_type_specifier = false;
10337 tree type_specifier_seq = NULL_TREE;
10338
10339 /* Parse the type-specifiers and attributes. */
10340 while (true)
10341 {
10342 tree type_specifier;
10343
10344 /* Check for attributes first. */
10345 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10346 {
10347 type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10348 NULL_TREE,
10349 type_specifier_seq);
10350 continue;
10351 }
10352
10353 /* After the first type-specifier, others are optional. */
10354 if (seen_type_specifier)
10355 cp_parser_parse_tentatively (parser);
10356 /* Look for the type-specifier. */
10357 type_specifier = cp_parser_type_specifier (parser,
10358 CP_PARSER_FLAGS_NONE,
10359 /*is_friend=*/false,
10360 /*is_declaration=*/false,
10361 NULL,
10362 NULL);
10363 /* If the first type-specifier could not be found, this is not a
10364 type-specifier-seq at all. */
10365 if (!seen_type_specifier && type_specifier == error_mark_node)
10366 return error_mark_node;
10367 /* If subsequent type-specifiers could not be found, the
10368 type-specifier-seq is complete. */
10369 else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10370 break;
10371
10372 /* Add the new type-specifier to the list. */
10373 type_specifier_seq
10374 = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10375 seen_type_specifier = true;
10376 }
10377
10378 /* We built up the list in reverse order. */
10379 return nreverse (type_specifier_seq);
10380 }
10381
10382 /* Parse a parameter-declaration-clause.
10383
10384 parameter-declaration-clause:
10385 parameter-declaration-list [opt] ... [opt]
10386 parameter-declaration-list , ...
10387
10388 Returns a representation for the parameter declarations. Each node
10389 is a TREE_LIST. (See cp_parser_parameter_declaration for the exact
10390 representation.) If the parameter-declaration-clause ends with an
10391 ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10392 list. A return value of NULL_TREE indicates a
10393 parameter-declaration-clause consisting only of an ellipsis. */
10394
10395 static tree
10396 cp_parser_parameter_declaration_clause (cp_parser* parser)
10397 {
10398 tree parameters;
10399 cp_token *token;
10400 bool ellipsis_p;
10401
10402 /* Peek at the next token. */
10403 token = cp_lexer_peek_token (parser->lexer);
10404 /* Check for trivial parameter-declaration-clauses. */
10405 if (token->type == CPP_ELLIPSIS)
10406 {
10407 /* Consume the `...' token. */
10408 cp_lexer_consume_token (parser->lexer);
10409 return NULL_TREE;
10410 }
10411 else if (token->type == CPP_CLOSE_PAREN)
10412 /* There are no parameters. */
10413 {
10414 #ifndef NO_IMPLICIT_EXTERN_C
10415 if (in_system_header && current_class_type == NULL
10416 && current_lang_name == lang_name_c)
10417 return NULL_TREE;
10418 else
10419 #endif
10420 return void_list_node;
10421 }
10422 /* Check for `(void)', too, which is a special case. */
10423 else if (token->keyword == RID_VOID
10424 && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
10425 == CPP_CLOSE_PAREN))
10426 {
10427 /* Consume the `void' token. */
10428 cp_lexer_consume_token (parser->lexer);
10429 /* There are no parameters. */
10430 return void_list_node;
10431 }
10432
10433 /* Parse the parameter-declaration-list. */
10434 parameters = cp_parser_parameter_declaration_list (parser);
10435 /* If a parse error occurred while parsing the
10436 parameter-declaration-list, then the entire
10437 parameter-declaration-clause is erroneous. */
10438 if (parameters == error_mark_node)
10439 return error_mark_node;
10440
10441 /* Peek at the next token. */
10442 token = cp_lexer_peek_token (parser->lexer);
10443 /* If it's a `,', the clause should terminate with an ellipsis. */
10444 if (token->type == CPP_COMMA)
10445 {
10446 /* Consume the `,'. */
10447 cp_lexer_consume_token (parser->lexer);
10448 /* Expect an ellipsis. */
10449 ellipsis_p
10450 = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
10451 }
10452 /* It might also be `...' if the optional trailing `,' was
10453 omitted. */
10454 else if (token->type == CPP_ELLIPSIS)
10455 {
10456 /* Consume the `...' token. */
10457 cp_lexer_consume_token (parser->lexer);
10458 /* And remember that we saw it. */
10459 ellipsis_p = true;
10460 }
10461 else
10462 ellipsis_p = false;
10463
10464 /* Finish the parameter list. */
10465 return finish_parmlist (parameters, ellipsis_p);
10466 }
10467
10468 /* Parse a parameter-declaration-list.
10469
10470 parameter-declaration-list:
10471 parameter-declaration
10472 parameter-declaration-list , parameter-declaration
10473
10474 Returns a representation of the parameter-declaration-list, as for
10475 cp_parser_parameter_declaration_clause. However, the
10476 `void_list_node' is never appended to the list. */
10477
10478 static tree
10479 cp_parser_parameter_declaration_list (cp_parser* parser)
10480 {
10481 tree parameters = NULL_TREE;
10482
10483 /* Look for more parameters. */
10484 while (true)
10485 {
10486 tree parameter;
10487 /* Parse the parameter. */
10488 parameter
10489 = cp_parser_parameter_declaration (parser, /*template_parm_p=*/false);
10490
10491 /* If a parse error occurred parsing the parameter declaration,
10492 then the entire parameter-declaration-list is erroneous. */
10493 if (parameter == error_mark_node)
10494 {
10495 parameters = error_mark_node;
10496 break;
10497 }
10498 /* Add the new parameter to the list. */
10499 TREE_CHAIN (parameter) = parameters;
10500 parameters = parameter;
10501
10502 /* Peek at the next token. */
10503 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
10504 || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10505 /* The parameter-declaration-list is complete. */
10506 break;
10507 else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10508 {
10509 cp_token *token;
10510
10511 /* Peek at the next token. */
10512 token = cp_lexer_peek_nth_token (parser->lexer, 2);
10513 /* If it's an ellipsis, then the list is complete. */
10514 if (token->type == CPP_ELLIPSIS)
10515 break;
10516 /* Otherwise, there must be more parameters. Consume the
10517 `,'. */
10518 cp_lexer_consume_token (parser->lexer);
10519 }
10520 else
10521 {
10522 cp_parser_error (parser, "expected `,' or `...'");
10523 break;
10524 }
10525 }
10526
10527 /* We built up the list in reverse order; straighten it out now. */
10528 return nreverse (parameters);
10529 }
10530
10531 /* Parse a parameter declaration.
10532
10533 parameter-declaration:
10534 decl-specifier-seq declarator
10535 decl-specifier-seq declarator = assignment-expression
10536 decl-specifier-seq abstract-declarator [opt]
10537 decl-specifier-seq abstract-declarator [opt] = assignment-expression
10538
10539 If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
10540 declares a template parameter. (In that case, a non-nested `>'
10541 token encountered during the parsing of the assignment-expression
10542 is not interpreted as a greater-than operator.)
10543
10544 Returns a TREE_LIST representing the parameter-declaration. The
10545 TREE_VALUE is a representation of the decl-specifier-seq and
10546 declarator. In particular, the TREE_VALUE will be a TREE_LIST
10547 whose TREE_PURPOSE represents the decl-specifier-seq and whose
10548 TREE_VALUE represents the declarator. */
10549
10550 static tree
10551 cp_parser_parameter_declaration (cp_parser *parser,
10552 bool template_parm_p)
10553 {
10554 bool declares_class_or_enum;
10555 bool greater_than_is_operator_p;
10556 tree decl_specifiers;
10557 tree attributes;
10558 tree declarator;
10559 tree default_argument;
10560 tree parameter;
10561 cp_token *token;
10562 const char *saved_message;
10563
10564 /* In a template parameter, `>' is not an operator.
10565
10566 [temp.param]
10567
10568 When parsing a default template-argument for a non-type
10569 template-parameter, the first non-nested `>' is taken as the end
10570 of the template parameter-list rather than a greater-than
10571 operator. */
10572 greater_than_is_operator_p = !template_parm_p;
10573
10574 /* Type definitions may not appear in parameter types. */
10575 saved_message = parser->type_definition_forbidden_message;
10576 parser->type_definition_forbidden_message
10577 = "types may not be defined in parameter types";
10578
10579 /* Parse the declaration-specifiers. */
10580 decl_specifiers
10581 = cp_parser_decl_specifier_seq (parser,
10582 CP_PARSER_FLAGS_NONE,
10583 &attributes,
10584 &declares_class_or_enum);
10585 /* If an error occurred, there's no reason to attempt to parse the
10586 rest of the declaration. */
10587 if (cp_parser_error_occurred (parser))
10588 {
10589 parser->type_definition_forbidden_message = saved_message;
10590 return error_mark_node;
10591 }
10592
10593 /* Peek at the next token. */
10594 token = cp_lexer_peek_token (parser->lexer);
10595 /* If the next token is a `)', `,', `=', `>', or `...', then there
10596 is no declarator. */
10597 if (token->type == CPP_CLOSE_PAREN
10598 || token->type == CPP_COMMA
10599 || token->type == CPP_EQ
10600 || token->type == CPP_ELLIPSIS
10601 || token->type == CPP_GREATER)
10602 declarator = NULL_TREE;
10603 /* Otherwise, there should be a declarator. */
10604 else
10605 {
10606 bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10607 parser->default_arg_ok_p = false;
10608
10609 declarator = cp_parser_declarator (parser,
10610 CP_PARSER_DECLARATOR_EITHER,
10611 /*ctor_dtor_or_conv_p=*/NULL);
10612 parser->default_arg_ok_p = saved_default_arg_ok_p;
10613 /* After the declarator, allow more attributes. */
10614 attributes = chainon (attributes, cp_parser_attributes_opt (parser));
10615 }
10616
10617 /* The restriction on defining new types applies only to the type
10618 of the parameter, not to the default argument. */
10619 parser->type_definition_forbidden_message = saved_message;
10620
10621 /* If the next token is `=', then process a default argument. */
10622 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10623 {
10624 bool saved_greater_than_is_operator_p;
10625 /* Consume the `='. */
10626 cp_lexer_consume_token (parser->lexer);
10627
10628 /* If we are defining a class, then the tokens that make up the
10629 default argument must be saved and processed later. */
10630 if (!template_parm_p && at_class_scope_p ()
10631 && TYPE_BEING_DEFINED (current_class_type))
10632 {
10633 unsigned depth = 0;
10634
10635 /* Create a DEFAULT_ARG to represented the unparsed default
10636 argument. */
10637 default_argument = make_node (DEFAULT_ARG);
10638 DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
10639
10640 /* Add tokens until we have processed the entire default
10641 argument. */
10642 while (true)
10643 {
10644 bool done = false;
10645 cp_token *token;
10646
10647 /* Peek at the next token. */
10648 token = cp_lexer_peek_token (parser->lexer);
10649 /* What we do depends on what token we have. */
10650 switch (token->type)
10651 {
10652 /* In valid code, a default argument must be
10653 immediately followed by a `,' `)', or `...'. */
10654 case CPP_COMMA:
10655 case CPP_CLOSE_PAREN:
10656 case CPP_ELLIPSIS:
10657 /* If we run into a non-nested `;', `}', or `]',
10658 then the code is invalid -- but the default
10659 argument is certainly over. */
10660 case CPP_SEMICOLON:
10661 case CPP_CLOSE_BRACE:
10662 case CPP_CLOSE_SQUARE:
10663 if (depth == 0)
10664 done = true;
10665 /* Update DEPTH, if necessary. */
10666 else if (token->type == CPP_CLOSE_PAREN
10667 || token->type == CPP_CLOSE_BRACE
10668 || token->type == CPP_CLOSE_SQUARE)
10669 --depth;
10670 break;
10671
10672 case CPP_OPEN_PAREN:
10673 case CPP_OPEN_SQUARE:
10674 case CPP_OPEN_BRACE:
10675 ++depth;
10676 break;
10677
10678 case CPP_GREATER:
10679 /* If we see a non-nested `>', and `>' is not an
10680 operator, then it marks the end of the default
10681 argument. */
10682 if (!depth && !greater_than_is_operator_p)
10683 done = true;
10684 break;
10685
10686 /* If we run out of tokens, issue an error message. */
10687 case CPP_EOF:
10688 error ("file ends in default argument");
10689 done = true;
10690 break;
10691
10692 case CPP_NAME:
10693 case CPP_SCOPE:
10694 /* In these cases, we should look for template-ids.
10695 For example, if the default argument is
10696 `X<int, double>()', we need to do name lookup to
10697 figure out whether or not `X' is a template; if
10698 so, the `,' does not end the default argument.
10699
10700 That is not yet done. */
10701 break;
10702
10703 default:
10704 break;
10705 }
10706
10707 /* If we've reached the end, stop. */
10708 if (done)
10709 break;
10710
10711 /* Add the token to the token block. */
10712 token = cp_lexer_consume_token (parser->lexer);
10713 cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
10714 token);
10715 }
10716 }
10717 /* Outside of a class definition, we can just parse the
10718 assignment-expression. */
10719 else
10720 {
10721 bool saved_local_variables_forbidden_p;
10722
10723 /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
10724 set correctly. */
10725 saved_greater_than_is_operator_p
10726 = parser->greater_than_is_operator_p;
10727 parser->greater_than_is_operator_p = greater_than_is_operator_p;
10728 /* Local variable names (and the `this' keyword) may not
10729 appear in a default argument. */
10730 saved_local_variables_forbidden_p
10731 = parser->local_variables_forbidden_p;
10732 parser->local_variables_forbidden_p = true;
10733 /* Parse the assignment-expression. */
10734 default_argument = cp_parser_assignment_expression (parser);
10735 /* Restore saved state. */
10736 parser->greater_than_is_operator_p
10737 = saved_greater_than_is_operator_p;
10738 parser->local_variables_forbidden_p
10739 = saved_local_variables_forbidden_p;
10740 }
10741 if (!parser->default_arg_ok_p)
10742 {
10743 pedwarn ("default arguments are only permitted on functions");
10744 if (flag_pedantic_errors)
10745 default_argument = NULL_TREE;
10746 }
10747 }
10748 else
10749 default_argument = NULL_TREE;
10750
10751 /* Create the representation of the parameter. */
10752 if (attributes)
10753 decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
10754 parameter = build_tree_list (default_argument,
10755 build_tree_list (decl_specifiers,
10756 declarator));
10757
10758 return parameter;
10759 }
10760
10761 /* Parse a function-definition.
10762
10763 function-definition:
10764 decl-specifier-seq [opt] declarator ctor-initializer [opt]
10765 function-body
10766 decl-specifier-seq [opt] declarator function-try-block
10767
10768 GNU Extension:
10769
10770 function-definition:
10771 __extension__ function-definition
10772
10773 Returns the FUNCTION_DECL for the function. If FRIEND_P is
10774 non-NULL, *FRIEND_P is set to TRUE iff the function was declared to
10775 be a `friend'. */
10776
10777 static tree
10778 cp_parser_function_definition (cp_parser* parser, bool* friend_p)
10779 {
10780 tree decl_specifiers;
10781 tree attributes;
10782 tree declarator;
10783 tree fn;
10784 cp_token *token;
10785 bool declares_class_or_enum;
10786 bool member_p;
10787 /* The saved value of the PEDANTIC flag. */
10788 int saved_pedantic;
10789
10790 /* Any pending qualification must be cleared by our caller. It is
10791 more robust to force the callers to clear PARSER->SCOPE than to
10792 do it here since if the qualification is in effect here, it might
10793 also end up in effect elsewhere that it is not intended. */
10794 my_friendly_assert (!parser->scope, 20010821);
10795
10796 /* Handle `__extension__'. */
10797 if (cp_parser_extension_opt (parser, &saved_pedantic))
10798 {
10799 /* Parse the function-definition. */
10800 fn = cp_parser_function_definition (parser, friend_p);
10801 /* Restore the PEDANTIC flag. */
10802 pedantic = saved_pedantic;
10803
10804 return fn;
10805 }
10806
10807 /* Check to see if this definition appears in a class-specifier. */
10808 member_p = (at_class_scope_p ()
10809 && TYPE_BEING_DEFINED (current_class_type));
10810 /* Defer access checks in the decl-specifier-seq until we know what
10811 function is being defined. There is no need to do this for the
10812 definition of member functions; we cannot be defining a member
10813 from another class. */
10814 push_deferring_access_checks (member_p ? dk_no_check: dk_deferred);
10815
10816 /* Parse the decl-specifier-seq. */
10817 decl_specifiers
10818 = cp_parser_decl_specifier_seq (parser,
10819 CP_PARSER_FLAGS_OPTIONAL,
10820 &attributes,
10821 &declares_class_or_enum);
10822 /* Figure out whether this declaration is a `friend'. */
10823 if (friend_p)
10824 *friend_p = cp_parser_friend_p (decl_specifiers);
10825
10826 /* Parse the declarator. */
10827 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10828 /*ctor_dtor_or_conv_p=*/NULL);
10829
10830 /* Gather up any access checks that occurred. */
10831 stop_deferring_access_checks ();
10832
10833 /* If something has already gone wrong, we may as well stop now. */
10834 if (declarator == error_mark_node)
10835 {
10836 /* Skip to the end of the function, or if this wasn't anything
10837 like a function-definition, to a `;' in the hopes of finding
10838 a sensible place from which to continue parsing. */
10839 cp_parser_skip_to_end_of_block_or_statement (parser);
10840 pop_deferring_access_checks ();
10841 return error_mark_node;
10842 }
10843
10844 /* The next character should be a `{' (for a simple function
10845 definition), a `:' (for a ctor-initializer), or `try' (for a
10846 function-try block). */
10847 token = cp_lexer_peek_token (parser->lexer);
10848 if (!cp_parser_token_starts_function_definition_p (token))
10849 {
10850 /* Issue the error-message. */
10851 cp_parser_error (parser, "expected function-definition");
10852 /* Skip to the next `;'. */
10853 cp_parser_skip_to_end_of_block_or_statement (parser);
10854
10855 pop_deferring_access_checks ();
10856 return error_mark_node;
10857 }
10858
10859 /* If we are in a class scope, then we must handle
10860 function-definitions specially. In particular, we save away the
10861 tokens that make up the function body, and parse them again
10862 later, in order to handle code like:
10863
10864 struct S {
10865 int f () { return i; }
10866 int i;
10867 };
10868
10869 Here, we cannot parse the body of `f' until after we have seen
10870 the declaration of `i'. */
10871 if (member_p)
10872 {
10873 cp_token_cache *cache;
10874
10875 /* Create the function-declaration. */
10876 fn = start_method (decl_specifiers, declarator, attributes);
10877 /* If something went badly wrong, bail out now. */
10878 if (fn == error_mark_node)
10879 {
10880 /* If there's a function-body, skip it. */
10881 if (cp_parser_token_starts_function_definition_p
10882 (cp_lexer_peek_token (parser->lexer)))
10883 cp_parser_skip_to_end_of_block_or_statement (parser);
10884 pop_deferring_access_checks ();
10885 return error_mark_node;
10886 }
10887
10888 /* Remember it, if there default args to post process. */
10889 cp_parser_save_default_args (parser, fn);
10890
10891 /* Create a token cache. */
10892 cache = cp_token_cache_new ();
10893 /* Save away the tokens that make up the body of the
10894 function. */
10895 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10896 /* Handle function try blocks. */
10897 while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
10898 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
10899
10900 /* Save away the inline definition; we will process it when the
10901 class is complete. */
10902 DECL_PENDING_INLINE_INFO (fn) = cache;
10903 DECL_PENDING_INLINE_P (fn) = 1;
10904
10905 /* We need to know that this was defined in the class, so that
10906 friend templates are handled correctly. */
10907 DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
10908
10909 /* We're done with the inline definition. */
10910 finish_method (fn);
10911
10912 /* Add FN to the queue of functions to be parsed later. */
10913 TREE_VALUE (parser->unparsed_functions_queues)
10914 = tree_cons (NULL_TREE, fn,
10915 TREE_VALUE (parser->unparsed_functions_queues));
10916
10917 pop_deferring_access_checks ();
10918 return fn;
10919 }
10920
10921 /* Check that the number of template-parameter-lists is OK. */
10922 if (!cp_parser_check_declarator_template_parameters (parser,
10923 declarator))
10924 {
10925 cp_parser_skip_to_end_of_block_or_statement (parser);
10926 pop_deferring_access_checks ();
10927 return error_mark_node;
10928 }
10929
10930 fn = cp_parser_function_definition_from_specifiers_and_declarator
10931 (parser, decl_specifiers, attributes, declarator);
10932 pop_deferring_access_checks ();
10933 return fn;
10934 }
10935
10936 /* Parse a function-body.
10937
10938 function-body:
10939 compound_statement */
10940
10941 static void
10942 cp_parser_function_body (cp_parser *parser)
10943 {
10944 cp_parser_compound_statement (parser);
10945 }
10946
10947 /* Parse a ctor-initializer-opt followed by a function-body. Return
10948 true if a ctor-initializer was present. */
10949
10950 static bool
10951 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
10952 {
10953 tree body;
10954 bool ctor_initializer_p;
10955
10956 /* Begin the function body. */
10957 body = begin_function_body ();
10958 /* Parse the optional ctor-initializer. */
10959 ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
10960 /* Parse the function-body. */
10961 cp_parser_function_body (parser);
10962 /* Finish the function body. */
10963 finish_function_body (body);
10964
10965 return ctor_initializer_p;
10966 }
10967
10968 /* Parse an initializer.
10969
10970 initializer:
10971 = initializer-clause
10972 ( expression-list )
10973
10974 Returns a expression representing the initializer. If no
10975 initializer is present, NULL_TREE is returned.
10976
10977 *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
10978 production is used, and zero otherwise. *IS_PARENTHESIZED_INIT is
10979 set to FALSE if there is no initializer present. */
10980
10981 static tree
10982 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init)
10983 {
10984 cp_token *token;
10985 tree init;
10986
10987 /* Peek at the next token. */
10988 token = cp_lexer_peek_token (parser->lexer);
10989
10990 /* Let our caller know whether or not this initializer was
10991 parenthesized. */
10992 *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
10993
10994 if (token->type == CPP_EQ)
10995 {
10996 /* Consume the `='. */
10997 cp_lexer_consume_token (parser->lexer);
10998 /* Parse the initializer-clause. */
10999 init = cp_parser_initializer_clause (parser);
11000 }
11001 else if (token->type == CPP_OPEN_PAREN)
11002 {
11003 /* Consume the `('. */
11004 cp_lexer_consume_token (parser->lexer);
11005 /* Parse the expression-list. */
11006 init = cp_parser_expression_list (parser);
11007 /* Consume the `)' token. */
11008 if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11009 cp_parser_skip_to_closing_parenthesis (parser);
11010 }
11011 else
11012 {
11013 /* Anything else is an error. */
11014 cp_parser_error (parser, "expected initializer");
11015 init = error_mark_node;
11016 }
11017
11018 return init;
11019 }
11020
11021 /* Parse an initializer-clause.
11022
11023 initializer-clause:
11024 assignment-expression
11025 { initializer-list , [opt] }
11026 { }
11027
11028 Returns an expression representing the initializer.
11029
11030 If the `assignment-expression' production is used the value
11031 returned is simply a representation for the expression.
11032
11033 Otherwise, a CONSTRUCTOR is returned. The CONSTRUCTOR_ELTS will be
11034 the elements of the initializer-list (or NULL_TREE, if the last
11035 production is used). The TREE_TYPE for the CONSTRUCTOR will be
11036 NULL_TREE. There is no way to detect whether or not the optional
11037 trailing `,' was provided. */
11038
11039 static tree
11040 cp_parser_initializer_clause (cp_parser* parser)
11041 {
11042 tree initializer;
11043
11044 /* If it is not a `{', then we are looking at an
11045 assignment-expression. */
11046 if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11047 initializer = cp_parser_assignment_expression (parser);
11048 else
11049 {
11050 /* Consume the `{' token. */
11051 cp_lexer_consume_token (parser->lexer);
11052 /* Create a CONSTRUCTOR to represent the braced-initializer. */
11053 initializer = make_node (CONSTRUCTOR);
11054 /* Mark it with TREE_HAS_CONSTRUCTOR. This should not be
11055 necessary, but check_initializer depends upon it, for
11056 now. */
11057 TREE_HAS_CONSTRUCTOR (initializer) = 1;
11058 /* If it's not a `}', then there is a non-trivial initializer. */
11059 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11060 {
11061 /* Parse the initializer list. */
11062 CONSTRUCTOR_ELTS (initializer)
11063 = cp_parser_initializer_list (parser);
11064 /* A trailing `,' token is allowed. */
11065 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11066 cp_lexer_consume_token (parser->lexer);
11067 }
11068
11069 /* Now, there should be a trailing `}'. */
11070 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11071 }
11072
11073 return initializer;
11074 }
11075
11076 /* Parse an initializer-list.
11077
11078 initializer-list:
11079 initializer-clause
11080 initializer-list , initializer-clause
11081
11082 GNU Extension:
11083
11084 initializer-list:
11085 identifier : initializer-clause
11086 initializer-list, identifier : initializer-clause
11087
11088 Returns a TREE_LIST. The TREE_VALUE of each node is an expression
11089 for the initializer. If the TREE_PURPOSE is non-NULL, it is the
11090 IDENTIFIER_NODE naming the field to initialize. */
11091
11092 static tree
11093 cp_parser_initializer_list (cp_parser* parser)
11094 {
11095 tree initializers = NULL_TREE;
11096
11097 /* Parse the rest of the list. */
11098 while (true)
11099 {
11100 cp_token *token;
11101 tree identifier;
11102 tree initializer;
11103
11104 /* If the next token is an identifier and the following one is a
11105 colon, we are looking at the GNU designated-initializer
11106 syntax. */
11107 if (cp_parser_allow_gnu_extensions_p (parser)
11108 && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11109 && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11110 {
11111 /* Consume the identifier. */
11112 identifier = cp_lexer_consume_token (parser->lexer)->value;
11113 /* Consume the `:'. */
11114 cp_lexer_consume_token (parser->lexer);
11115 }
11116 else
11117 identifier = NULL_TREE;
11118
11119 /* Parse the initializer. */
11120 initializer = cp_parser_initializer_clause (parser);
11121
11122 /* Add it to the list. */
11123 initializers = tree_cons (identifier, initializer, initializers);
11124
11125 /* If the next token is not a comma, we have reached the end of
11126 the list. */
11127 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11128 break;
11129
11130 /* Peek at the next token. */
11131 token = cp_lexer_peek_nth_token (parser->lexer, 2);
11132 /* If the next token is a `}', then we're still done. An
11133 initializer-clause can have a trailing `,' after the
11134 initializer-list and before the closing `}'. */
11135 if (token->type == CPP_CLOSE_BRACE)
11136 break;
11137
11138 /* Consume the `,' token. */
11139 cp_lexer_consume_token (parser->lexer);
11140 }
11141
11142 /* The initializers were built up in reverse order, so we need to
11143 reverse them now. */
11144 return nreverse (initializers);
11145 }
11146
11147 /* Classes [gram.class] */
11148
11149 /* Parse a class-name.
11150
11151 class-name:
11152 identifier
11153 template-id
11154
11155 TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11156 to indicate that names looked up in dependent types should be
11157 assumed to be types. TEMPLATE_KEYWORD_P is true iff the `template'
11158 keyword has been used to indicate that the name that appears next
11159 is a template. TYPE_P is true iff the next name should be treated
11160 as class-name, even if it is declared to be some other kind of name
11161 as well. If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11162 dependent scopes. If CLASS_HEAD_P is TRUE, this class is the class
11163 being defined in a class-head.
11164
11165 Returns the TYPE_DECL representing the class. */
11166
11167 static tree
11168 cp_parser_class_name (cp_parser *parser,
11169 bool typename_keyword_p,
11170 bool template_keyword_p,
11171 bool type_p,
11172 bool check_dependency_p,
11173 bool class_head_p)
11174 {
11175 tree decl;
11176 tree scope;
11177 bool typename_p;
11178 cp_token *token;
11179
11180 /* All class-names start with an identifier. */
11181 token = cp_lexer_peek_token (parser->lexer);
11182 if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11183 {
11184 cp_parser_error (parser, "expected class-name");
11185 return error_mark_node;
11186 }
11187
11188 /* PARSER->SCOPE can be cleared when parsing the template-arguments
11189 to a template-id, so we save it here. */
11190 scope = parser->scope;
11191 /* Any name names a type if we're following the `typename' keyword
11192 in a qualified name where the enclosing scope is type-dependent. */
11193 typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11194 && dependent_type_p (scope));
11195 /* Handle the common case (an identifier, but not a template-id)
11196 efficiently. */
11197 if (token->type == CPP_NAME
11198 && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_LESS)
11199 {
11200 tree identifier;
11201
11202 /* Look for the identifier. */
11203 identifier = cp_parser_identifier (parser);
11204 /* If the next token isn't an identifier, we are certainly not
11205 looking at a class-name. */
11206 if (identifier == error_mark_node)
11207 decl = error_mark_node;
11208 /* If we know this is a type-name, there's no need to look it
11209 up. */
11210 else if (typename_p)
11211 decl = identifier;
11212 else
11213 {
11214 /* If the next token is a `::', then the name must be a type
11215 name.
11216
11217 [basic.lookup.qual]
11218
11219 During the lookup for a name preceding the :: scope
11220 resolution operator, object, function, and enumerator
11221 names are ignored. */
11222 if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11223 type_p = true;
11224 /* Look up the name. */
11225 decl = cp_parser_lookup_name (parser, identifier,
11226 type_p,
11227 /*is_namespace=*/false,
11228 check_dependency_p);
11229 }
11230 }
11231 else
11232 {
11233 /* Try a template-id. */
11234 decl = cp_parser_template_id (parser, template_keyword_p,
11235 check_dependency_p);
11236 if (decl == error_mark_node)
11237 return error_mark_node;
11238 }
11239
11240 decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11241
11242 /* If this is a typename, create a TYPENAME_TYPE. */
11243 if (typename_p && decl != error_mark_node)
11244 decl = TYPE_NAME (make_typename_type (scope, decl,
11245 /*complain=*/1));
11246
11247 /* Check to see that it is really the name of a class. */
11248 if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11249 && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11250 && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11251 /* Situations like this:
11252
11253 template <typename T> struct A {
11254 typename T::template X<int>::I i;
11255 };
11256
11257 are problematic. Is `T::template X<int>' a class-name? The
11258 standard does not seem to be definitive, but there is no other
11259 valid interpretation of the following `::'. Therefore, those
11260 names are considered class-names. */
11261 decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11262 else if (decl == error_mark_node
11263 || TREE_CODE (decl) != TYPE_DECL
11264 || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11265 {
11266 cp_parser_error (parser, "expected class-name");
11267 return error_mark_node;
11268 }
11269
11270 return decl;
11271 }
11272
11273 /* Parse a class-specifier.
11274
11275 class-specifier:
11276 class-head { member-specification [opt] }
11277
11278 Returns the TREE_TYPE representing the class. */
11279
11280 static tree
11281 cp_parser_class_specifier (cp_parser* parser)
11282 {
11283 cp_token *token;
11284 tree type;
11285 tree attributes = NULL_TREE;
11286 int has_trailing_semicolon;
11287 bool nested_name_specifier_p;
11288 unsigned saved_num_template_parameter_lists;
11289
11290 push_deferring_access_checks (dk_no_deferred);
11291
11292 /* Parse the class-head. */
11293 type = cp_parser_class_head (parser,
11294 &nested_name_specifier_p);
11295 /* If the class-head was a semantic disaster, skip the entire body
11296 of the class. */
11297 if (!type)
11298 {
11299 cp_parser_skip_to_end_of_block_or_statement (parser);
11300 pop_deferring_access_checks ();
11301 return error_mark_node;
11302 }
11303
11304 /* Look for the `{'. */
11305 if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11306 {
11307 pop_deferring_access_checks ();
11308 return error_mark_node;
11309 }
11310
11311 /* Issue an error message if type-definitions are forbidden here. */
11312 cp_parser_check_type_definition (parser);
11313 /* Remember that we are defining one more class. */
11314 ++parser->num_classes_being_defined;
11315 /* Inside the class, surrounding template-parameter-lists do not
11316 apply. */
11317 saved_num_template_parameter_lists
11318 = parser->num_template_parameter_lists;
11319 parser->num_template_parameter_lists = 0;
11320
11321 /* Start the class. */
11322 type = begin_class_definition (type);
11323 if (type == error_mark_node)
11324 /* If the type is erroneous, skip the entire body of the class. */
11325 cp_parser_skip_to_closing_brace (parser);
11326 else
11327 /* Parse the member-specification. */
11328 cp_parser_member_specification_opt (parser);
11329 /* Look for the trailing `}'. */
11330 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11331 /* We get better error messages by noticing a common problem: a
11332 missing trailing `;'. */
11333 token = cp_lexer_peek_token (parser->lexer);
11334 has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11335 /* Look for attributes to apply to this class. */
11336 if (cp_parser_allow_gnu_extensions_p (parser))
11337 attributes = cp_parser_attributes_opt (parser);
11338 /* Finish the class definition. */
11339 type = finish_class_definition (type,
11340 attributes,
11341 has_trailing_semicolon,
11342 nested_name_specifier_p);
11343 /* If this class is not itself within the scope of another class,
11344 then we need to parse the bodies of all of the queued function
11345 definitions. Note that the queued functions defined in a class
11346 are not always processed immediately following the
11347 class-specifier for that class. Consider:
11348
11349 struct A {
11350 struct B { void f() { sizeof (A); } };
11351 };
11352
11353 If `f' were processed before the processing of `A' were
11354 completed, there would be no way to compute the size of `A'.
11355 Note that the nesting we are interested in here is lexical --
11356 not the semantic nesting given by TYPE_CONTEXT. In particular,
11357 for:
11358
11359 struct A { struct B; };
11360 struct A::B { void f() { } };
11361
11362 there is no need to delay the parsing of `A::B::f'. */
11363 if (--parser->num_classes_being_defined == 0)
11364 {
11365 tree queue_entry;
11366 tree fn;
11367
11368 /* In a first pass, parse default arguments to the functions.
11369 Then, in a second pass, parse the bodies of the functions.
11370 This two-phased approach handles cases like:
11371
11372 struct S {
11373 void f() { g(); }
11374 void g(int i = 3);
11375 };
11376
11377 */
11378 for (TREE_PURPOSE (parser->unparsed_functions_queues)
11379 = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11380 (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11381 TREE_PURPOSE (parser->unparsed_functions_queues)
11382 = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11383 {
11384 fn = TREE_VALUE (queue_entry);
11385 /* Make sure that any template parameters are in scope. */
11386 maybe_begin_member_template_processing (fn);
11387 /* If there are default arguments that have not yet been processed,
11388 take care of them now. */
11389 cp_parser_late_parsing_default_args (parser, fn);
11390 /* Remove any template parameters from the symbol table. */
11391 maybe_end_member_template_processing ();
11392 }
11393 /* Now parse the body of the functions. */
11394 for (TREE_VALUE (parser->unparsed_functions_queues)
11395 = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11396 (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11397 TREE_VALUE (parser->unparsed_functions_queues)
11398 = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11399 {
11400 /* Figure out which function we need to process. */
11401 fn = TREE_VALUE (queue_entry);
11402
11403 /* Parse the function. */
11404 cp_parser_late_parsing_for_member (parser, fn);
11405 }
11406
11407 }
11408
11409 /* Put back any saved access checks. */
11410 pop_deferring_access_checks ();
11411
11412 /* Restore the count of active template-parameter-lists. */
11413 parser->num_template_parameter_lists
11414 = saved_num_template_parameter_lists;
11415
11416 return type;
11417 }
11418
11419 /* Parse a class-head.
11420
11421 class-head:
11422 class-key identifier [opt] base-clause [opt]
11423 class-key nested-name-specifier identifier base-clause [opt]
11424 class-key nested-name-specifier [opt] template-id
11425 base-clause [opt]
11426
11427 GNU Extensions:
11428 class-key attributes identifier [opt] base-clause [opt]
11429 class-key attributes nested-name-specifier identifier base-clause [opt]
11430 class-key attributes nested-name-specifier [opt] template-id
11431 base-clause [opt]
11432
11433 Returns the TYPE of the indicated class. Sets
11434 *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11435 involving a nested-name-specifier was used, and FALSE otherwise.
11436
11437 Returns NULL_TREE if the class-head is syntactically valid, but
11438 semantically invalid in a way that means we should skip the entire
11439 body of the class. */
11440
11441 static tree
11442 cp_parser_class_head (cp_parser* parser,
11443 bool* nested_name_specifier_p)
11444 {
11445 cp_token *token;
11446 tree nested_name_specifier;
11447 enum tag_types class_key;
11448 tree id = NULL_TREE;
11449 tree type = NULL_TREE;
11450 tree attributes;
11451 bool template_id_p = false;
11452 bool qualified_p = false;
11453 bool invalid_nested_name_p = false;
11454 unsigned num_templates;
11455
11456 /* Assume no nested-name-specifier will be present. */
11457 *nested_name_specifier_p = false;
11458 /* Assume no template parameter lists will be used in defining the
11459 type. */
11460 num_templates = 0;
11461
11462 /* Look for the class-key. */
11463 class_key = cp_parser_class_key (parser);
11464 if (class_key == none_type)
11465 return error_mark_node;
11466
11467 /* Parse the attributes. */
11468 attributes = cp_parser_attributes_opt (parser);
11469
11470 /* If the next token is `::', that is invalid -- but sometimes
11471 people do try to write:
11472
11473 struct ::S {};
11474
11475 Handle this gracefully by accepting the extra qualifier, and then
11476 issuing an error about it later if this really is a
11477 class-head. If it turns out just to be an elaborated type
11478 specifier, remain silent. */
11479 if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11480 qualified_p = true;
11481
11482 push_deferring_access_checks (dk_no_check);
11483
11484 /* Determine the name of the class. Begin by looking for an
11485 optional nested-name-specifier. */
11486 nested_name_specifier
11487 = cp_parser_nested_name_specifier_opt (parser,
11488 /*typename_keyword_p=*/false,
11489 /*check_dependency_p=*/false,
11490 /*type_p=*/false);
11491 /* If there was a nested-name-specifier, then there *must* be an
11492 identifier. */
11493 if (nested_name_specifier)
11494 {
11495 /* Although the grammar says `identifier', it really means
11496 `class-name' or `template-name'. You are only allowed to
11497 define a class that has already been declared with this
11498 syntax.
11499
11500 The proposed resolution for Core Issue 180 says that whever
11501 you see `class T::X' you should treat `X' as a type-name.
11502
11503 It is OK to define an inaccessible class; for example:
11504
11505 class A { class B; };
11506 class A::B {};
11507
11508 We do not know if we will see a class-name, or a
11509 template-name. We look for a class-name first, in case the
11510 class-name is a template-id; if we looked for the
11511 template-name first we would stop after the template-name. */
11512 cp_parser_parse_tentatively (parser);
11513 type = cp_parser_class_name (parser,
11514 /*typename_keyword_p=*/false,
11515 /*template_keyword_p=*/false,
11516 /*type_p=*/true,
11517 /*check_dependency_p=*/false,
11518 /*class_head_p=*/true);
11519 /* If that didn't work, ignore the nested-name-specifier. */
11520 if (!cp_parser_parse_definitely (parser))
11521 {
11522 invalid_nested_name_p = true;
11523 id = cp_parser_identifier (parser);
11524 if (id == error_mark_node)
11525 id = NULL_TREE;
11526 }
11527 /* If we could not find a corresponding TYPE, treat this
11528 declaration like an unqualified declaration. */
11529 if (type == error_mark_node)
11530 nested_name_specifier = NULL_TREE;
11531 /* Otherwise, count the number of templates used in TYPE and its
11532 containing scopes. */
11533 else
11534 {
11535 tree scope;
11536
11537 for (scope = TREE_TYPE (type);
11538 scope && TREE_CODE (scope) != NAMESPACE_DECL;
11539 scope = (TYPE_P (scope)
11540 ? TYPE_CONTEXT (scope)
11541 : DECL_CONTEXT (scope)))
11542 if (TYPE_P (scope)
11543 && CLASS_TYPE_P (scope)
11544 && CLASSTYPE_TEMPLATE_INFO (scope)
11545 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
11546 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
11547 ++num_templates;
11548 }
11549 }
11550 /* Otherwise, the identifier is optional. */
11551 else
11552 {
11553 /* We don't know whether what comes next is a template-id,
11554 an identifier, or nothing at all. */
11555 cp_parser_parse_tentatively (parser);
11556 /* Check for a template-id. */
11557 id = cp_parser_template_id (parser,
11558 /*template_keyword_p=*/false,
11559 /*check_dependency_p=*/true);
11560 /* If that didn't work, it could still be an identifier. */
11561 if (!cp_parser_parse_definitely (parser))
11562 {
11563 if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11564 id = cp_parser_identifier (parser);
11565 else
11566 id = NULL_TREE;
11567 }
11568 else
11569 {
11570 template_id_p = true;
11571 ++num_templates;
11572 }
11573 }
11574
11575 pop_deferring_access_checks ();
11576
11577 /* If it's not a `:' or a `{' then we can't really be looking at a
11578 class-head, since a class-head only appears as part of a
11579 class-specifier. We have to detect this situation before calling
11580 xref_tag, since that has irreversible side-effects. */
11581 if (!cp_parser_next_token_starts_class_definition_p (parser))
11582 {
11583 cp_parser_error (parser, "expected `{' or `:'");
11584 return error_mark_node;
11585 }
11586
11587 /* At this point, we're going ahead with the class-specifier, even
11588 if some other problem occurs. */
11589 cp_parser_commit_to_tentative_parse (parser);
11590 /* Issue the error about the overly-qualified name now. */
11591 if (qualified_p)
11592 cp_parser_error (parser,
11593 "global qualification of class name is invalid");
11594 else if (invalid_nested_name_p)
11595 cp_parser_error (parser,
11596 "qualified name does not name a class");
11597 /* Make sure that the right number of template parameters were
11598 present. */
11599 if (!cp_parser_check_template_parameters (parser, num_templates))
11600 /* If something went wrong, there is no point in even trying to
11601 process the class-definition. */
11602 return NULL_TREE;
11603
11604 /* Look up the type. */
11605 if (template_id_p)
11606 {
11607 type = TREE_TYPE (id);
11608 maybe_process_partial_specialization (type);
11609 }
11610 else if (!nested_name_specifier)
11611 {
11612 /* If the class was unnamed, create a dummy name. */
11613 if (!id)
11614 id = make_anon_name ();
11615 type = xref_tag (class_key, id, attributes, /*globalize=*/0);
11616 }
11617 else
11618 {
11619 tree class_type;
11620 tree scope;
11621
11622 /* Given:
11623
11624 template <typename T> struct S { struct T };
11625 template <typename T> struct S<T>::T { };
11626
11627 we will get a TYPENAME_TYPE when processing the definition of
11628 `S::T'. We need to resolve it to the actual type before we
11629 try to define it. */
11630 if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
11631 {
11632 class_type = resolve_typename_type (TREE_TYPE (type),
11633 /*only_current_p=*/false);
11634 if (class_type != error_mark_node)
11635 type = TYPE_NAME (class_type);
11636 else
11637 {
11638 cp_parser_error (parser, "could not resolve typename type");
11639 type = error_mark_node;
11640 }
11641 }
11642
11643 /* Figure out in what scope the declaration is being placed. */
11644 scope = current_scope ();
11645 if (!scope)
11646 scope = current_namespace;
11647 /* If that scope does not contain the scope in which the
11648 class was originally declared, the program is invalid. */
11649 if (scope && !is_ancestor (scope, CP_DECL_CONTEXT (type)))
11650 {
11651 error ("declaration of `%D' in `%D' which does not "
11652 "enclose `%D'", type, scope, nested_name_specifier);
11653 return NULL_TREE;
11654 }
11655
11656 maybe_process_partial_specialization (TREE_TYPE (type));
11657 class_type = current_class_type;
11658 type = TREE_TYPE (handle_class_head (class_key,
11659 nested_name_specifier,
11660 type,
11661 attributes));
11662 if (type != error_mark_node)
11663 {
11664 if (!class_type && TYPE_CONTEXT (type))
11665 *nested_name_specifier_p = true;
11666 else if (class_type && !same_type_p (TYPE_CONTEXT (type),
11667 class_type))
11668 *nested_name_specifier_p = true;
11669 }
11670 }
11671 /* Indicate whether this class was declared as a `class' or as a
11672 `struct'. */
11673 if (TREE_CODE (type) == RECORD_TYPE)
11674 CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
11675 cp_parser_check_class_key (class_key, type);
11676
11677 /* Enter the scope containing the class; the names of base classes
11678 should be looked up in that context. For example, given:
11679
11680 struct A { struct B {}; struct C; };
11681 struct A::C : B {};
11682
11683 is valid. */
11684 if (nested_name_specifier)
11685 push_scope (nested_name_specifier);
11686 /* Now, look for the base-clause. */
11687 token = cp_lexer_peek_token (parser->lexer);
11688 if (token->type == CPP_COLON)
11689 {
11690 tree bases;
11691
11692 /* Get the list of base-classes. */
11693 bases = cp_parser_base_clause (parser);
11694 /* Process them. */
11695 xref_basetypes (type, bases);
11696 }
11697 /* Leave the scope given by the nested-name-specifier. We will
11698 enter the class scope itself while processing the members. */
11699 if (nested_name_specifier)
11700 pop_scope (nested_name_specifier);
11701
11702 return type;
11703 }
11704
11705 /* Parse a class-key.
11706
11707 class-key:
11708 class
11709 struct
11710 union
11711
11712 Returns the kind of class-key specified, or none_type to indicate
11713 error. */
11714
11715 static enum tag_types
11716 cp_parser_class_key (cp_parser* parser)
11717 {
11718 cp_token *token;
11719 enum tag_types tag_type;
11720
11721 /* Look for the class-key. */
11722 token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
11723 if (!token)
11724 return none_type;
11725
11726 /* Check to see if the TOKEN is a class-key. */
11727 tag_type = cp_parser_token_is_class_key (token);
11728 if (!tag_type)
11729 cp_parser_error (parser, "expected class-key");
11730 return tag_type;
11731 }
11732
11733 /* Parse an (optional) member-specification.
11734
11735 member-specification:
11736 member-declaration member-specification [opt]
11737 access-specifier : member-specification [opt] */
11738
11739 static void
11740 cp_parser_member_specification_opt (cp_parser* parser)
11741 {
11742 while (true)
11743 {
11744 cp_token *token;
11745 enum rid keyword;
11746
11747 /* Peek at the next token. */
11748 token = cp_lexer_peek_token (parser->lexer);
11749 /* If it's a `}', or EOF then we've seen all the members. */
11750 if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
11751 break;
11752
11753 /* See if this token is a keyword. */
11754 keyword = token->keyword;
11755 switch (keyword)
11756 {
11757 case RID_PUBLIC:
11758 case RID_PROTECTED:
11759 case RID_PRIVATE:
11760 /* Consume the access-specifier. */
11761 cp_lexer_consume_token (parser->lexer);
11762 /* Remember which access-specifier is active. */
11763 current_access_specifier = token->value;
11764 /* Look for the `:'. */
11765 cp_parser_require (parser, CPP_COLON, "`:'");
11766 break;
11767
11768 default:
11769 /* Otherwise, the next construction must be a
11770 member-declaration. */
11771 cp_parser_member_declaration (parser);
11772 }
11773 }
11774 }
11775
11776 /* Parse a member-declaration.
11777
11778 member-declaration:
11779 decl-specifier-seq [opt] member-declarator-list [opt] ;
11780 function-definition ; [opt]
11781 :: [opt] nested-name-specifier template [opt] unqualified-id ;
11782 using-declaration
11783 template-declaration
11784
11785 member-declarator-list:
11786 member-declarator
11787 member-declarator-list , member-declarator
11788
11789 member-declarator:
11790 declarator pure-specifier [opt]
11791 declarator constant-initializer [opt]
11792 identifier [opt] : constant-expression
11793
11794 GNU Extensions:
11795
11796 member-declaration:
11797 __extension__ member-declaration
11798
11799 member-declarator:
11800 declarator attributes [opt] pure-specifier [opt]
11801 declarator attributes [opt] constant-initializer [opt]
11802 identifier [opt] attributes [opt] : constant-expression */
11803
11804 static void
11805 cp_parser_member_declaration (cp_parser* parser)
11806 {
11807 tree decl_specifiers;
11808 tree prefix_attributes;
11809 tree decl;
11810 bool declares_class_or_enum;
11811 bool friend_p;
11812 cp_token *token;
11813 int saved_pedantic;
11814
11815 /* Check for the `__extension__' keyword. */
11816 if (cp_parser_extension_opt (parser, &saved_pedantic))
11817 {
11818 /* Recurse. */
11819 cp_parser_member_declaration (parser);
11820 /* Restore the old value of the PEDANTIC flag. */
11821 pedantic = saved_pedantic;
11822
11823 return;
11824 }
11825
11826 /* Check for a template-declaration. */
11827 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11828 {
11829 /* Parse the template-declaration. */
11830 cp_parser_template_declaration (parser, /*member_p=*/true);
11831
11832 return;
11833 }
11834
11835 /* Check for a using-declaration. */
11836 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
11837 {
11838 /* Parse the using-declaration. */
11839 cp_parser_using_declaration (parser);
11840
11841 return;
11842 }
11843
11844 /* We can't tell whether we're looking at a declaration or a
11845 function-definition. */
11846 cp_parser_parse_tentatively (parser);
11847
11848 /* Parse the decl-specifier-seq. */
11849 decl_specifiers
11850 = cp_parser_decl_specifier_seq (parser,
11851 CP_PARSER_FLAGS_OPTIONAL,
11852 &prefix_attributes,
11853 &declares_class_or_enum);
11854 /* Check for an invalid type-name. */
11855 if (cp_parser_diagnose_invalid_type_name (parser))
11856 return;
11857 /* If there is no declarator, then the decl-specifier-seq should
11858 specify a type. */
11859 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
11860 {
11861 /* If there was no decl-specifier-seq, and the next token is a
11862 `;', then we have something like:
11863
11864 struct S { ; };
11865
11866 [class.mem]
11867
11868 Each member-declaration shall declare at least one member
11869 name of the class. */
11870 if (!decl_specifiers)
11871 {
11872 if (pedantic)
11873 pedwarn ("extra semicolon");
11874 }
11875 else
11876 {
11877 tree type;
11878
11879 /* See if this declaration is a friend. */
11880 friend_p = cp_parser_friend_p (decl_specifiers);
11881 /* If there were decl-specifiers, check to see if there was
11882 a class-declaration. */
11883 type = check_tag_decl (decl_specifiers);
11884 /* Nested classes have already been added to the class, but
11885 a `friend' needs to be explicitly registered. */
11886 if (friend_p)
11887 {
11888 /* If the `friend' keyword was present, the friend must
11889 be introduced with a class-key. */
11890 if (!declares_class_or_enum)
11891 error ("a class-key must be used when declaring a friend");
11892 /* In this case:
11893
11894 template <typename T> struct A {
11895 friend struct A<T>::B;
11896 };
11897
11898 A<T>::B will be represented by a TYPENAME_TYPE, and
11899 therefore not recognized by check_tag_decl. */
11900 if (!type)
11901 {
11902 tree specifier;
11903
11904 for (specifier = decl_specifiers;
11905 specifier;
11906 specifier = TREE_CHAIN (specifier))
11907 {
11908 tree s = TREE_VALUE (specifier);
11909
11910 if (TREE_CODE (s) == IDENTIFIER_NODE
11911 && IDENTIFIER_GLOBAL_VALUE (s))
11912 type = IDENTIFIER_GLOBAL_VALUE (s);
11913 if (TREE_CODE (s) == TYPE_DECL)
11914 s = TREE_TYPE (s);
11915 if (TYPE_P (s))
11916 {
11917 type = s;
11918 break;
11919 }
11920 }
11921 }
11922 if (!type)
11923 error ("friend declaration does not name a class or "
11924 "function");
11925 else
11926 make_friend_class (current_class_type, type);
11927 }
11928 /* If there is no TYPE, an error message will already have
11929 been issued. */
11930 else if (!type)
11931 ;
11932 /* An anonymous aggregate has to be handled specially; such
11933 a declaration really declares a data member (with a
11934 particular type), as opposed to a nested class. */
11935 else if (ANON_AGGR_TYPE_P (type))
11936 {
11937 /* Remove constructors and such from TYPE, now that we
11938 know it is an anonymous aggregate. */
11939 fixup_anonymous_aggr (type);
11940 /* And make the corresponding data member. */
11941 decl = build_decl (FIELD_DECL, NULL_TREE, type);
11942 /* Add it to the class. */
11943 finish_member_declaration (decl);
11944 }
11945 }
11946 }
11947 else
11948 {
11949 /* See if these declarations will be friends. */
11950 friend_p = cp_parser_friend_p (decl_specifiers);
11951
11952 /* Keep going until we hit the `;' at the end of the
11953 declaration. */
11954 while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
11955 {
11956 tree attributes = NULL_TREE;
11957 tree first_attribute;
11958
11959 /* Peek at the next token. */
11960 token = cp_lexer_peek_token (parser->lexer);
11961
11962 /* Check for a bitfield declaration. */
11963 if (token->type == CPP_COLON
11964 || (token->type == CPP_NAME
11965 && cp_lexer_peek_nth_token (parser->lexer, 2)->type
11966 == CPP_COLON))
11967 {
11968 tree identifier;
11969 tree width;
11970
11971 /* Get the name of the bitfield. Note that we cannot just
11972 check TOKEN here because it may have been invalidated by
11973 the call to cp_lexer_peek_nth_token above. */
11974 if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
11975 identifier = cp_parser_identifier (parser);
11976 else
11977 identifier = NULL_TREE;
11978
11979 /* Consume the `:' token. */
11980 cp_lexer_consume_token (parser->lexer);
11981 /* Get the width of the bitfield. */
11982 width
11983 = cp_parser_constant_expression (parser,
11984 /*allow_non_constant=*/false,
11985 NULL);
11986
11987 /* Look for attributes that apply to the bitfield. */
11988 attributes = cp_parser_attributes_opt (parser);
11989 /* Remember which attributes are prefix attributes and
11990 which are not. */
11991 first_attribute = attributes;
11992 /* Combine the attributes. */
11993 attributes = chainon (prefix_attributes, attributes);
11994
11995 /* Create the bitfield declaration. */
11996 decl = grokbitfield (identifier,
11997 decl_specifiers,
11998 width);
11999 /* Apply the attributes. */
12000 cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12001 }
12002 else
12003 {
12004 tree declarator;
12005 tree initializer;
12006 tree asm_specification;
12007 bool ctor_dtor_or_conv_p;
12008
12009 /* Parse the declarator. */
12010 declarator
12011 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12012 &ctor_dtor_or_conv_p);
12013
12014 /* If something went wrong parsing the declarator, make sure
12015 that we at least consume some tokens. */
12016 if (declarator == error_mark_node)
12017 {
12018 /* Skip to the end of the statement. */
12019 cp_parser_skip_to_end_of_statement (parser);
12020 break;
12021 }
12022
12023 /* Look for an asm-specification. */
12024 asm_specification = cp_parser_asm_specification_opt (parser);
12025 /* Look for attributes that apply to the declaration. */
12026 attributes = cp_parser_attributes_opt (parser);
12027 /* Remember which attributes are prefix attributes and
12028 which are not. */
12029 first_attribute = attributes;
12030 /* Combine the attributes. */
12031 attributes = chainon (prefix_attributes, attributes);
12032
12033 /* If it's an `=', then we have a constant-initializer or a
12034 pure-specifier. It is not correct to parse the
12035 initializer before registering the member declaration
12036 since the member declaration should be in scope while
12037 its initializer is processed. However, the rest of the
12038 front end does not yet provide an interface that allows
12039 us to handle this correctly. */
12040 if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12041 {
12042 /* In [class.mem]:
12043
12044 A pure-specifier shall be used only in the declaration of
12045 a virtual function.
12046
12047 A member-declarator can contain a constant-initializer
12048 only if it declares a static member of integral or
12049 enumeration type.
12050
12051 Therefore, if the DECLARATOR is for a function, we look
12052 for a pure-specifier; otherwise, we look for a
12053 constant-initializer. When we call `grokfield', it will
12054 perform more stringent semantics checks. */
12055 if (TREE_CODE (declarator) == CALL_EXPR)
12056 initializer = cp_parser_pure_specifier (parser);
12057 else
12058 {
12059 /* This declaration cannot be a function
12060 definition. */
12061 cp_parser_commit_to_tentative_parse (parser);
12062 /* Parse the initializer. */
12063 initializer = cp_parser_constant_initializer (parser);
12064 }
12065 }
12066 /* Otherwise, there is no initializer. */
12067 else
12068 initializer = NULL_TREE;
12069
12070 /* See if we are probably looking at a function
12071 definition. We are certainly not looking at at a
12072 member-declarator. Calling `grokfield' has
12073 side-effects, so we must not do it unless we are sure
12074 that we are looking at a member-declarator. */
12075 if (cp_parser_token_starts_function_definition_p
12076 (cp_lexer_peek_token (parser->lexer)))
12077 decl = error_mark_node;
12078 else
12079 /* Create the declaration. */
12080 decl = grokfield (declarator,
12081 decl_specifiers,
12082 initializer,
12083 asm_specification,
12084 attributes);
12085 }
12086
12087 /* Reset PREFIX_ATTRIBUTES. */
12088 while (attributes && TREE_CHAIN (attributes) != first_attribute)
12089 attributes = TREE_CHAIN (attributes);
12090 if (attributes)
12091 TREE_CHAIN (attributes) = NULL_TREE;
12092
12093 /* If there is any qualification still in effect, clear it
12094 now; we will be starting fresh with the next declarator. */
12095 parser->scope = NULL_TREE;
12096 parser->qualifying_scope = NULL_TREE;
12097 parser->object_scope = NULL_TREE;
12098 /* If it's a `,', then there are more declarators. */
12099 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12100 cp_lexer_consume_token (parser->lexer);
12101 /* If the next token isn't a `;', then we have a parse error. */
12102 else if (cp_lexer_next_token_is_not (parser->lexer,
12103 CPP_SEMICOLON))
12104 {
12105 cp_parser_error (parser, "expected `;'");
12106 /* Skip tokens until we find a `;' */
12107 cp_parser_skip_to_end_of_statement (parser);
12108
12109 break;
12110 }
12111
12112 if (decl)
12113 {
12114 /* Add DECL to the list of members. */
12115 if (!friend_p)
12116 finish_member_declaration (decl);
12117
12118 if (TREE_CODE (decl) == FUNCTION_DECL)
12119 cp_parser_save_default_args (parser, decl);
12120 }
12121 }
12122 }
12123
12124 /* If everything went well, look for the `;'. */
12125 if (cp_parser_parse_definitely (parser))
12126 {
12127 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12128 return;
12129 }
12130
12131 /* Parse the function-definition. */
12132 decl = cp_parser_function_definition (parser, &friend_p);
12133 /* If the member was not a friend, declare it here. */
12134 if (!friend_p)
12135 finish_member_declaration (decl);
12136 /* Peek at the next token. */
12137 token = cp_lexer_peek_token (parser->lexer);
12138 /* If the next token is a semicolon, consume it. */
12139 if (token->type == CPP_SEMICOLON)
12140 cp_lexer_consume_token (parser->lexer);
12141 }
12142
12143 /* Parse a pure-specifier.
12144
12145 pure-specifier:
12146 = 0
12147
12148 Returns INTEGER_ZERO_NODE if a pure specifier is found.
12149 Otherwiser, ERROR_MARK_NODE is returned. */
12150
12151 static tree
12152 cp_parser_pure_specifier (cp_parser* parser)
12153 {
12154 cp_token *token;
12155
12156 /* Look for the `=' token. */
12157 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12158 return error_mark_node;
12159 /* Look for the `0' token. */
12160 token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12161 /* Unfortunately, this will accept `0L' and `0x00' as well. We need
12162 to get information from the lexer about how the number was
12163 spelled in order to fix this problem. */
12164 if (!token || !integer_zerop (token->value))
12165 return error_mark_node;
12166
12167 return integer_zero_node;
12168 }
12169
12170 /* Parse a constant-initializer.
12171
12172 constant-initializer:
12173 = constant-expression
12174
12175 Returns a representation of the constant-expression. */
12176
12177 static tree
12178 cp_parser_constant_initializer (cp_parser* parser)
12179 {
12180 /* Look for the `=' token. */
12181 if (!cp_parser_require (parser, CPP_EQ, "`='"))
12182 return error_mark_node;
12183
12184 /* It is invalid to write:
12185
12186 struct S { static const int i = { 7 }; };
12187
12188 */
12189 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12190 {
12191 cp_parser_error (parser,
12192 "a brace-enclosed initializer is not allowed here");
12193 /* Consume the opening brace. */
12194 cp_lexer_consume_token (parser->lexer);
12195 /* Skip the initializer. */
12196 cp_parser_skip_to_closing_brace (parser);
12197 /* Look for the trailing `}'. */
12198 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12199
12200 return error_mark_node;
12201 }
12202
12203 return cp_parser_constant_expression (parser,
12204 /*allow_non_constant=*/false,
12205 NULL);
12206 }
12207
12208 /* Derived classes [gram.class.derived] */
12209
12210 /* Parse a base-clause.
12211
12212 base-clause:
12213 : base-specifier-list
12214
12215 base-specifier-list:
12216 base-specifier
12217 base-specifier-list , base-specifier
12218
12219 Returns a TREE_LIST representing the base-classes, in the order in
12220 which they were declared. The representation of each node is as
12221 described by cp_parser_base_specifier.
12222
12223 In the case that no bases are specified, this function will return
12224 NULL_TREE, not ERROR_MARK_NODE. */
12225
12226 static tree
12227 cp_parser_base_clause (cp_parser* parser)
12228 {
12229 tree bases = NULL_TREE;
12230
12231 /* Look for the `:' that begins the list. */
12232 cp_parser_require (parser, CPP_COLON, "`:'");
12233
12234 /* Scan the base-specifier-list. */
12235 while (true)
12236 {
12237 cp_token *token;
12238 tree base;
12239
12240 /* Look for the base-specifier. */
12241 base = cp_parser_base_specifier (parser);
12242 /* Add BASE to the front of the list. */
12243 if (base != error_mark_node)
12244 {
12245 TREE_CHAIN (base) = bases;
12246 bases = base;
12247 }
12248 /* Peek at the next token. */
12249 token = cp_lexer_peek_token (parser->lexer);
12250 /* If it's not a comma, then the list is complete. */
12251 if (token->type != CPP_COMMA)
12252 break;
12253 /* Consume the `,'. */
12254 cp_lexer_consume_token (parser->lexer);
12255 }
12256
12257 /* PARSER->SCOPE may still be non-NULL at this point, if the last
12258 base class had a qualified name. However, the next name that
12259 appears is certainly not qualified. */
12260 parser->scope = NULL_TREE;
12261 parser->qualifying_scope = NULL_TREE;
12262 parser->object_scope = NULL_TREE;
12263
12264 return nreverse (bases);
12265 }
12266
12267 /* Parse a base-specifier.
12268
12269 base-specifier:
12270 :: [opt] nested-name-specifier [opt] class-name
12271 virtual access-specifier [opt] :: [opt] nested-name-specifier
12272 [opt] class-name
12273 access-specifier virtual [opt] :: [opt] nested-name-specifier
12274 [opt] class-name
12275
12276 Returns a TREE_LIST. The TREE_PURPOSE will be one of
12277 ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12278 indicate the specifiers provided. The TREE_VALUE will be a TYPE
12279 (or the ERROR_MARK_NODE) indicating the type that was specified. */
12280
12281 static tree
12282 cp_parser_base_specifier (cp_parser* parser)
12283 {
12284 cp_token *token;
12285 bool done = false;
12286 bool virtual_p = false;
12287 bool duplicate_virtual_error_issued_p = false;
12288 bool duplicate_access_error_issued_p = false;
12289 bool class_scope_p, template_p;
12290 tree access = access_default_node;
12291 tree type;
12292
12293 /* Process the optional `virtual' and `access-specifier'. */
12294 while (!done)
12295 {
12296 /* Peek at the next token. */
12297 token = cp_lexer_peek_token (parser->lexer);
12298 /* Process `virtual'. */
12299 switch (token->keyword)
12300 {
12301 case RID_VIRTUAL:
12302 /* If `virtual' appears more than once, issue an error. */
12303 if (virtual_p && !duplicate_virtual_error_issued_p)
12304 {
12305 cp_parser_error (parser,
12306 "`virtual' specified more than once in base-specified");
12307 duplicate_virtual_error_issued_p = true;
12308 }
12309
12310 virtual_p = true;
12311
12312 /* Consume the `virtual' token. */
12313 cp_lexer_consume_token (parser->lexer);
12314
12315 break;
12316
12317 case RID_PUBLIC:
12318 case RID_PROTECTED:
12319 case RID_PRIVATE:
12320 /* If more than one access specifier appears, issue an
12321 error. */
12322 if (access != access_default_node
12323 && !duplicate_access_error_issued_p)
12324 {
12325 cp_parser_error (parser,
12326 "more than one access specifier in base-specified");
12327 duplicate_access_error_issued_p = true;
12328 }
12329
12330 access = ridpointers[(int) token->keyword];
12331
12332 /* Consume the access-specifier. */
12333 cp_lexer_consume_token (parser->lexer);
12334
12335 break;
12336
12337 default:
12338 done = true;
12339 break;
12340 }
12341 }
12342
12343 /* Look for the optional `::' operator. */
12344 cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12345 /* Look for the nested-name-specifier. The simplest way to
12346 implement:
12347
12348 [temp.res]
12349
12350 The keyword `typename' is not permitted in a base-specifier or
12351 mem-initializer; in these contexts a qualified name that
12352 depends on a template-parameter is implicitly assumed to be a
12353 type name.
12354
12355 is to pretend that we have seen the `typename' keyword at this
12356 point. */
12357 cp_parser_nested_name_specifier_opt (parser,
12358 /*typename_keyword_p=*/true,
12359 /*check_dependency_p=*/true,
12360 /*type_p=*/true);
12361 /* If the base class is given by a qualified name, assume that names
12362 we see are type names or templates, as appropriate. */
12363 class_scope_p = (parser->scope && TYPE_P (parser->scope));
12364 template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12365
12366 /* Finally, look for the class-name. */
12367 type = cp_parser_class_name (parser,
12368 class_scope_p,
12369 template_p,
12370 /*type_p=*/true,
12371 /*check_dependency_p=*/true,
12372 /*class_head_p=*/false);
12373
12374 if (type == error_mark_node)
12375 return error_mark_node;
12376
12377 return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12378 }
12379
12380 /* Exception handling [gram.exception] */
12381
12382 /* Parse an (optional) exception-specification.
12383
12384 exception-specification:
12385 throw ( type-id-list [opt] )
12386
12387 Returns a TREE_LIST representing the exception-specification. The
12388 TREE_VALUE of each node is a type. */
12389
12390 static tree
12391 cp_parser_exception_specification_opt (cp_parser* parser)
12392 {
12393 cp_token *token;
12394 tree type_id_list;
12395
12396 /* Peek at the next token. */
12397 token = cp_lexer_peek_token (parser->lexer);
12398 /* If it's not `throw', then there's no exception-specification. */
12399 if (!cp_parser_is_keyword (token, RID_THROW))
12400 return NULL_TREE;
12401
12402 /* Consume the `throw'. */
12403 cp_lexer_consume_token (parser->lexer);
12404
12405 /* Look for the `('. */
12406 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12407
12408 /* Peek at the next token. */
12409 token = cp_lexer_peek_token (parser->lexer);
12410 /* If it's not a `)', then there is a type-id-list. */
12411 if (token->type != CPP_CLOSE_PAREN)
12412 {
12413 const char *saved_message;
12414
12415 /* Types may not be defined in an exception-specification. */
12416 saved_message = parser->type_definition_forbidden_message;
12417 parser->type_definition_forbidden_message
12418 = "types may not be defined in an exception-specification";
12419 /* Parse the type-id-list. */
12420 type_id_list = cp_parser_type_id_list (parser);
12421 /* Restore the saved message. */
12422 parser->type_definition_forbidden_message = saved_message;
12423 }
12424 else
12425 type_id_list = empty_except_spec;
12426
12427 /* Look for the `)'. */
12428 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12429
12430 return type_id_list;
12431 }
12432
12433 /* Parse an (optional) type-id-list.
12434
12435 type-id-list:
12436 type-id
12437 type-id-list , type-id
12438
12439 Returns a TREE_LIST. The TREE_VALUE of each node is a TYPE,
12440 in the order that the types were presented. */
12441
12442 static tree
12443 cp_parser_type_id_list (cp_parser* parser)
12444 {
12445 tree types = NULL_TREE;
12446
12447 while (true)
12448 {
12449 cp_token *token;
12450 tree type;
12451
12452 /* Get the next type-id. */
12453 type = cp_parser_type_id (parser);
12454 /* Add it to the list. */
12455 types = add_exception_specifier (types, type, /*complain=*/1);
12456 /* Peek at the next token. */
12457 token = cp_lexer_peek_token (parser->lexer);
12458 /* If it is not a `,', we are done. */
12459 if (token->type != CPP_COMMA)
12460 break;
12461 /* Consume the `,'. */
12462 cp_lexer_consume_token (parser->lexer);
12463 }
12464
12465 return nreverse (types);
12466 }
12467
12468 /* Parse a try-block.
12469
12470 try-block:
12471 try compound-statement handler-seq */
12472
12473 static tree
12474 cp_parser_try_block (cp_parser* parser)
12475 {
12476 tree try_block;
12477
12478 cp_parser_require_keyword (parser, RID_TRY, "`try'");
12479 try_block = begin_try_block ();
12480 cp_parser_compound_statement (parser);
12481 finish_try_block (try_block);
12482 cp_parser_handler_seq (parser);
12483 finish_handler_sequence (try_block);
12484
12485 return try_block;
12486 }
12487
12488 /* Parse a function-try-block.
12489
12490 function-try-block:
12491 try ctor-initializer [opt] function-body handler-seq */
12492
12493 static bool
12494 cp_parser_function_try_block (cp_parser* parser)
12495 {
12496 tree try_block;
12497 bool ctor_initializer_p;
12498
12499 /* Look for the `try' keyword. */
12500 if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
12501 return false;
12502 /* Let the rest of the front-end know where we are. */
12503 try_block = begin_function_try_block ();
12504 /* Parse the function-body. */
12505 ctor_initializer_p
12506 = cp_parser_ctor_initializer_opt_and_function_body (parser);
12507 /* We're done with the `try' part. */
12508 finish_function_try_block (try_block);
12509 /* Parse the handlers. */
12510 cp_parser_handler_seq (parser);
12511 /* We're done with the handlers. */
12512 finish_function_handler_sequence (try_block);
12513
12514 return ctor_initializer_p;
12515 }
12516
12517 /* Parse a handler-seq.
12518
12519 handler-seq:
12520 handler handler-seq [opt] */
12521
12522 static void
12523 cp_parser_handler_seq (cp_parser* parser)
12524 {
12525 while (true)
12526 {
12527 cp_token *token;
12528
12529 /* Parse the handler. */
12530 cp_parser_handler (parser);
12531 /* Peek at the next token. */
12532 token = cp_lexer_peek_token (parser->lexer);
12533 /* If it's not `catch' then there are no more handlers. */
12534 if (!cp_parser_is_keyword (token, RID_CATCH))
12535 break;
12536 }
12537 }
12538
12539 /* Parse a handler.
12540
12541 handler:
12542 catch ( exception-declaration ) compound-statement */
12543
12544 static void
12545 cp_parser_handler (cp_parser* parser)
12546 {
12547 tree handler;
12548 tree declaration;
12549
12550 cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
12551 handler = begin_handler ();
12552 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12553 declaration = cp_parser_exception_declaration (parser);
12554 finish_handler_parms (declaration, handler);
12555 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12556 cp_parser_compound_statement (parser);
12557 finish_handler (handler);
12558 }
12559
12560 /* Parse an exception-declaration.
12561
12562 exception-declaration:
12563 type-specifier-seq declarator
12564 type-specifier-seq abstract-declarator
12565 type-specifier-seq
12566 ...
12567
12568 Returns a VAR_DECL for the declaration, or NULL_TREE if the
12569 ellipsis variant is used. */
12570
12571 static tree
12572 cp_parser_exception_declaration (cp_parser* parser)
12573 {
12574 tree type_specifiers;
12575 tree declarator;
12576 const char *saved_message;
12577
12578 /* If it's an ellipsis, it's easy to handle. */
12579 if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12580 {
12581 /* Consume the `...' token. */
12582 cp_lexer_consume_token (parser->lexer);
12583 return NULL_TREE;
12584 }
12585
12586 /* Types may not be defined in exception-declarations. */
12587 saved_message = parser->type_definition_forbidden_message;
12588 parser->type_definition_forbidden_message
12589 = "types may not be defined in exception-declarations";
12590
12591 /* Parse the type-specifier-seq. */
12592 type_specifiers = cp_parser_type_specifier_seq (parser);
12593 /* If it's a `)', then there is no declarator. */
12594 if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
12595 declarator = NULL_TREE;
12596 else
12597 declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
12598 /*ctor_dtor_or_conv_p=*/NULL);
12599
12600 /* Restore the saved message. */
12601 parser->type_definition_forbidden_message = saved_message;
12602
12603 return start_handler_parms (type_specifiers, declarator);
12604 }
12605
12606 /* Parse a throw-expression.
12607
12608 throw-expression:
12609 throw assignment-expression [opt]
12610
12611 Returns a THROW_EXPR representing the throw-expression. */
12612
12613 static tree
12614 cp_parser_throw_expression (cp_parser* parser)
12615 {
12616 tree expression;
12617
12618 cp_parser_require_keyword (parser, RID_THROW, "`throw'");
12619 /* We can't be sure if there is an assignment-expression or not. */
12620 cp_parser_parse_tentatively (parser);
12621 /* Try it. */
12622 expression = cp_parser_assignment_expression (parser);
12623 /* If it didn't work, this is just a rethrow. */
12624 if (!cp_parser_parse_definitely (parser))
12625 expression = NULL_TREE;
12626
12627 return build_throw (expression);
12628 }
12629
12630 /* GNU Extensions */
12631
12632 /* Parse an (optional) asm-specification.
12633
12634 asm-specification:
12635 asm ( string-literal )
12636
12637 If the asm-specification is present, returns a STRING_CST
12638 corresponding to the string-literal. Otherwise, returns
12639 NULL_TREE. */
12640
12641 static tree
12642 cp_parser_asm_specification_opt (cp_parser* parser)
12643 {
12644 cp_token *token;
12645 tree asm_specification;
12646
12647 /* Peek at the next token. */
12648 token = cp_lexer_peek_token (parser->lexer);
12649 /* If the next token isn't the `asm' keyword, then there's no
12650 asm-specification. */
12651 if (!cp_parser_is_keyword (token, RID_ASM))
12652 return NULL_TREE;
12653
12654 /* Consume the `asm' token. */
12655 cp_lexer_consume_token (parser->lexer);
12656 /* Look for the `('. */
12657 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12658
12659 /* Look for the string-literal. */
12660 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12661 if (token)
12662 asm_specification = token->value;
12663 else
12664 asm_specification = NULL_TREE;
12665
12666 /* Look for the `)'. */
12667 cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
12668
12669 return asm_specification;
12670 }
12671
12672 /* Parse an asm-operand-list.
12673
12674 asm-operand-list:
12675 asm-operand
12676 asm-operand-list , asm-operand
12677
12678 asm-operand:
12679 string-literal ( expression )
12680 [ string-literal ] string-literal ( expression )
12681
12682 Returns a TREE_LIST representing the operands. The TREE_VALUE of
12683 each node is the expression. The TREE_PURPOSE is itself a
12684 TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
12685 string-literal (or NULL_TREE if not present) and whose TREE_VALUE
12686 is a STRING_CST for the string literal before the parenthesis. */
12687
12688 static tree
12689 cp_parser_asm_operand_list (cp_parser* parser)
12690 {
12691 tree asm_operands = NULL_TREE;
12692
12693 while (true)
12694 {
12695 tree string_literal;
12696 tree expression;
12697 tree name;
12698 cp_token *token;
12699
12700 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
12701 {
12702 /* Consume the `[' token. */
12703 cp_lexer_consume_token (parser->lexer);
12704 /* Read the operand name. */
12705 name = cp_parser_identifier (parser);
12706 if (name != error_mark_node)
12707 name = build_string (IDENTIFIER_LENGTH (name),
12708 IDENTIFIER_POINTER (name));
12709 /* Look for the closing `]'. */
12710 cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
12711 }
12712 else
12713 name = NULL_TREE;
12714 /* Look for the string-literal. */
12715 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12716 string_literal = token ? token->value : error_mark_node;
12717 /* Look for the `('. */
12718 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12719 /* Parse the expression. */
12720 expression = cp_parser_expression (parser);
12721 /* Look for the `)'. */
12722 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12723 /* Add this operand to the list. */
12724 asm_operands = tree_cons (build_tree_list (name, string_literal),
12725 expression,
12726 asm_operands);
12727 /* If the next token is not a `,', there are no more
12728 operands. */
12729 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12730 break;
12731 /* Consume the `,'. */
12732 cp_lexer_consume_token (parser->lexer);
12733 }
12734
12735 return nreverse (asm_operands);
12736 }
12737
12738 /* Parse an asm-clobber-list.
12739
12740 asm-clobber-list:
12741 string-literal
12742 asm-clobber-list , string-literal
12743
12744 Returns a TREE_LIST, indicating the clobbers in the order that they
12745 appeared. The TREE_VALUE of each node is a STRING_CST. */
12746
12747 static tree
12748 cp_parser_asm_clobber_list (cp_parser* parser)
12749 {
12750 tree clobbers = NULL_TREE;
12751
12752 while (true)
12753 {
12754 cp_token *token;
12755 tree string_literal;
12756
12757 /* Look for the string literal. */
12758 token = cp_parser_require (parser, CPP_STRING, "string-literal");
12759 string_literal = token ? token->value : error_mark_node;
12760 /* Add it to the list. */
12761 clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
12762 /* If the next token is not a `,', then the list is
12763 complete. */
12764 if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12765 break;
12766 /* Consume the `,' token. */
12767 cp_lexer_consume_token (parser->lexer);
12768 }
12769
12770 return clobbers;
12771 }
12772
12773 /* Parse an (optional) series of attributes.
12774
12775 attributes:
12776 attributes attribute
12777
12778 attribute:
12779 __attribute__ (( attribute-list [opt] ))
12780
12781 The return value is as for cp_parser_attribute_list. */
12782
12783 static tree
12784 cp_parser_attributes_opt (cp_parser* parser)
12785 {
12786 tree attributes = NULL_TREE;
12787
12788 while (true)
12789 {
12790 cp_token *token;
12791 tree attribute_list;
12792
12793 /* Peek at the next token. */
12794 token = cp_lexer_peek_token (parser->lexer);
12795 /* If it's not `__attribute__', then we're done. */
12796 if (token->keyword != RID_ATTRIBUTE)
12797 break;
12798
12799 /* Consume the `__attribute__' keyword. */
12800 cp_lexer_consume_token (parser->lexer);
12801 /* Look for the two `(' tokens. */
12802 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12803 cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
12804
12805 /* Peek at the next token. */
12806 token = cp_lexer_peek_token (parser->lexer);
12807 if (token->type != CPP_CLOSE_PAREN)
12808 /* Parse the attribute-list. */
12809 attribute_list = cp_parser_attribute_list (parser);
12810 else
12811 /* If the next token is a `)', then there is no attribute
12812 list. */
12813 attribute_list = NULL;
12814
12815 /* Look for the two `)' tokens. */
12816 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12817 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12818
12819 /* Add these new attributes to the list. */
12820 attributes = chainon (attributes, attribute_list);
12821 }
12822
12823 return attributes;
12824 }
12825
12826 /* Parse an attribute-list.
12827
12828 attribute-list:
12829 attribute
12830 attribute-list , attribute
12831
12832 attribute:
12833 identifier
12834 identifier ( identifier )
12835 identifier ( identifier , expression-list )
12836 identifier ( expression-list )
12837
12838 Returns a TREE_LIST. Each node corresponds to an attribute. THe
12839 TREE_PURPOSE of each node is the identifier indicating which
12840 attribute is in use. The TREE_VALUE represents the arguments, if
12841 any. */
12842
12843 static tree
12844 cp_parser_attribute_list (cp_parser* parser)
12845 {
12846 tree attribute_list = NULL_TREE;
12847
12848 while (true)
12849 {
12850 cp_token *token;
12851 tree identifier;
12852 tree attribute;
12853
12854 /* Look for the identifier. We also allow keywords here; for
12855 example `__attribute__ ((const))' is legal. */
12856 token = cp_lexer_peek_token (parser->lexer);
12857 if (token->type != CPP_NAME
12858 && token->type != CPP_KEYWORD)
12859 return error_mark_node;
12860 /* Consume the token. */
12861 token = cp_lexer_consume_token (parser->lexer);
12862
12863 /* Save away the identifier that indicates which attribute this is. */
12864 identifier = token->value;
12865 attribute = build_tree_list (identifier, NULL_TREE);
12866
12867 /* Peek at the next token. */
12868 token = cp_lexer_peek_token (parser->lexer);
12869 /* If it's an `(', then parse the attribute arguments. */
12870 if (token->type == CPP_OPEN_PAREN)
12871 {
12872 tree arguments;
12873 int arguments_allowed_p = 1;
12874
12875 /* Consume the `('. */
12876 cp_lexer_consume_token (parser->lexer);
12877 /* Peek at the next token. */
12878 token = cp_lexer_peek_token (parser->lexer);
12879 /* Check to see if the next token is an identifier. */
12880 if (token->type == CPP_NAME)
12881 {
12882 /* Save the identifier. */
12883 identifier = token->value;
12884 /* Consume the identifier. */
12885 cp_lexer_consume_token (parser->lexer);
12886 /* Peek at the next token. */
12887 token = cp_lexer_peek_token (parser->lexer);
12888 /* If the next token is a `,', then there are some other
12889 expressions as well. */
12890 if (token->type == CPP_COMMA)
12891 /* Consume the comma. */
12892 cp_lexer_consume_token (parser->lexer);
12893 else
12894 arguments_allowed_p = 0;
12895 }
12896 else
12897 identifier = NULL_TREE;
12898
12899 /* If there are arguments, parse them too. */
12900 if (arguments_allowed_p)
12901 arguments = cp_parser_expression_list (parser);
12902 else
12903 arguments = NULL_TREE;
12904
12905 /* Combine the identifier and the arguments. */
12906 if (identifier)
12907 arguments = tree_cons (NULL_TREE, identifier, arguments);
12908
12909 /* Save the identifier and arguments away. */
12910 TREE_VALUE (attribute) = arguments;
12911
12912 /* Look for the closing `)'. */
12913 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
12914 }
12915
12916 /* Add this attribute to the list. */
12917 TREE_CHAIN (attribute) = attribute_list;
12918 attribute_list = attribute;
12919
12920 /* Now, look for more attributes. */
12921 token = cp_lexer_peek_token (parser->lexer);
12922 /* If the next token isn't a `,', we're done. */
12923 if (token->type != CPP_COMMA)
12924 break;
12925
12926 /* Consume the commma and keep going. */
12927 cp_lexer_consume_token (parser->lexer);
12928 }
12929
12930 /* We built up the list in reverse order. */
12931 return nreverse (attribute_list);
12932 }
12933
12934 /* Parse an optional `__extension__' keyword. Returns TRUE if it is
12935 present, and FALSE otherwise. *SAVED_PEDANTIC is set to the
12936 current value of the PEDANTIC flag, regardless of whether or not
12937 the `__extension__' keyword is present. The caller is responsible
12938 for restoring the value of the PEDANTIC flag. */
12939
12940 static bool
12941 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
12942 {
12943 /* Save the old value of the PEDANTIC flag. */
12944 *saved_pedantic = pedantic;
12945
12946 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
12947 {
12948 /* Consume the `__extension__' token. */
12949 cp_lexer_consume_token (parser->lexer);
12950 /* We're not being pedantic while the `__extension__' keyword is
12951 in effect. */
12952 pedantic = 0;
12953
12954 return true;
12955 }
12956
12957 return false;
12958 }
12959
12960 /* Parse a label declaration.
12961
12962 label-declaration:
12963 __label__ label-declarator-seq ;
12964
12965 label-declarator-seq:
12966 identifier , label-declarator-seq
12967 identifier */
12968
12969 static void
12970 cp_parser_label_declaration (cp_parser* parser)
12971 {
12972 /* Look for the `__label__' keyword. */
12973 cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
12974
12975 while (true)
12976 {
12977 tree identifier;
12978
12979 /* Look for an identifier. */
12980 identifier = cp_parser_identifier (parser);
12981 /* Declare it as a lobel. */
12982 finish_label_decl (identifier);
12983 /* If the next token is a `;', stop. */
12984 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12985 break;
12986 /* Look for the `,' separating the label declarations. */
12987 cp_parser_require (parser, CPP_COMMA, "`,'");
12988 }
12989
12990 /* Look for the final `;'. */
12991 cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12992 }
12993
12994 /* Support Functions */
12995
12996 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
12997 NAME should have one of the representations used for an
12998 id-expression. If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
12999 is returned. If PARSER->SCOPE is a dependent type, then a
13000 SCOPE_REF is returned.
13001
13002 If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13003 returned; the name was already resolved when the TEMPLATE_ID_EXPR
13004 was formed. Abstractly, such entities should not be passed to this
13005 function, because they do not need to be looked up, but it is
13006 simpler to check for this special case here, rather than at the
13007 call-sites.
13008
13009 In cases not explicitly covered above, this function returns a
13010 DECL, OVERLOAD, or baselink representing the result of the lookup.
13011 If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13012 is returned.
13013
13014 If IS_TYPE is TRUE, bindings that do not refer to types are
13015 ignored.
13016
13017 If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13018 are ignored.
13019
13020 If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13021 types. */
13022
13023 static tree
13024 cp_parser_lookup_name (cp_parser *parser, tree name,
13025 bool is_type, bool is_namespace, bool check_dependency)
13026 {
13027 tree decl;
13028 tree object_type = parser->context->object_type;
13029
13030 /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13031 no longer valid. Note that if we are parsing tentatively, and
13032 the parse fails, OBJECT_TYPE will be automatically restored. */
13033 parser->context->object_type = NULL_TREE;
13034
13035 if (name == error_mark_node)
13036 return error_mark_node;
13037
13038 /* A template-id has already been resolved; there is no lookup to
13039 do. */
13040 if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13041 return name;
13042 if (BASELINK_P (name))
13043 {
13044 my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13045 == TEMPLATE_ID_EXPR),
13046 20020909);
13047 return name;
13048 }
13049
13050 /* A BIT_NOT_EXPR is used to represent a destructor. By this point,
13051 it should already have been checked to make sure that the name
13052 used matches the type being destroyed. */
13053 if (TREE_CODE (name) == BIT_NOT_EXPR)
13054 {
13055 tree type;
13056
13057 /* Figure out to which type this destructor applies. */
13058 if (parser->scope)
13059 type = parser->scope;
13060 else if (object_type)
13061 type = object_type;
13062 else
13063 type = current_class_type;
13064 /* If that's not a class type, there is no destructor. */
13065 if (!type || !CLASS_TYPE_P (type))
13066 return error_mark_node;
13067 /* If it was a class type, return the destructor. */
13068 return CLASSTYPE_DESTRUCTORS (type);
13069 }
13070
13071 /* By this point, the NAME should be an ordinary identifier. If
13072 the id-expression was a qualified name, the qualifying scope is
13073 stored in PARSER->SCOPE at this point. */
13074 my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13075 20000619);
13076
13077 /* Perform the lookup. */
13078 if (parser->scope)
13079 {
13080 bool dependent_p;
13081
13082 if (parser->scope == error_mark_node)
13083 return error_mark_node;
13084
13085 /* If the SCOPE is dependent, the lookup must be deferred until
13086 the template is instantiated -- unless we are explicitly
13087 looking up names in uninstantiated templates. Even then, we
13088 cannot look up the name if the scope is not a class type; it
13089 might, for example, be a template type parameter. */
13090 dependent_p = (TYPE_P (parser->scope)
13091 && !(parser->in_declarator_p
13092 && currently_open_class (parser->scope))
13093 && dependent_type_p (parser->scope));
13094 if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13095 && dependent_p)
13096 {
13097 if (!is_type)
13098 decl = build_nt (SCOPE_REF, parser->scope, name);
13099 else
13100 /* The resolution to Core Issue 180 says that `struct A::B'
13101 should be considered a type-name, even if `A' is
13102 dependent. */
13103 decl = TYPE_NAME (make_typename_type (parser->scope,
13104 name,
13105 /*complain=*/1));
13106 }
13107 else
13108 {
13109 /* If PARSER->SCOPE is a dependent type, then it must be a
13110 class type, and we must not be checking dependencies;
13111 otherwise, we would have processed this lookup above. So
13112 that PARSER->SCOPE is not considered a dependent base by
13113 lookup_member, we must enter the scope here. */
13114 if (dependent_p)
13115 push_scope (parser->scope);
13116 /* If the PARSER->SCOPE is a a template specialization, it
13117 may be instantiated during name lookup. In that case,
13118 errors may be issued. Even if we rollback the current
13119 tentative parse, those errors are valid. */
13120 decl = lookup_qualified_name (parser->scope, name, is_type);
13121 if (dependent_p)
13122 pop_scope (parser->scope);
13123 }
13124 parser->qualifying_scope = parser->scope;
13125 parser->object_scope = NULL_TREE;
13126 }
13127 else if (object_type)
13128 {
13129 tree object_decl = NULL_TREE;
13130 /* Look up the name in the scope of the OBJECT_TYPE, unless the
13131 OBJECT_TYPE is not a class. */
13132 if (CLASS_TYPE_P (object_type))
13133 /* If the OBJECT_TYPE is a template specialization, it may
13134 be instantiated during name lookup. In that case, errors
13135 may be issued. Even if we rollback the current tentative
13136 parse, those errors are valid. */
13137 object_decl = lookup_member (object_type,
13138 name,
13139 /*protect=*/0, is_type);
13140 /* Look it up in the enclosing context, too. */
13141 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13142 is_namespace,
13143 /*flags=*/0);
13144 parser->object_scope = object_type;
13145 parser->qualifying_scope = NULL_TREE;
13146 if (object_decl)
13147 decl = object_decl;
13148 }
13149 else
13150 {
13151 decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13152 is_namespace,
13153 /*flags=*/0);
13154 parser->qualifying_scope = NULL_TREE;
13155 parser->object_scope = NULL_TREE;
13156 }
13157
13158 /* If the lookup failed, let our caller know. */
13159 if (!decl
13160 || decl == error_mark_node
13161 || (TREE_CODE (decl) == FUNCTION_DECL
13162 && DECL_ANTICIPATED (decl)))
13163 return error_mark_node;
13164
13165 /* If it's a TREE_LIST, the result of the lookup was ambiguous. */
13166 if (TREE_CODE (decl) == TREE_LIST)
13167 {
13168 /* The error message we have to print is too complicated for
13169 cp_parser_error, so we incorporate its actions directly. */
13170 if (!cp_parser_simulate_error (parser))
13171 {
13172 error ("reference to `%D' is ambiguous", name);
13173 print_candidates (decl);
13174 }
13175 return error_mark_node;
13176 }
13177
13178 my_friendly_assert (DECL_P (decl)
13179 || TREE_CODE (decl) == OVERLOAD
13180 || TREE_CODE (decl) == SCOPE_REF
13181 || BASELINK_P (decl),
13182 20000619);
13183
13184 /* If we have resolved the name of a member declaration, check to
13185 see if the declaration is accessible. When the name resolves to
13186 set of overloaded functions, accessibility is checked when
13187 overload resolution is done.
13188
13189 During an explicit instantiation, access is not checked at all,
13190 as per [temp.explicit]. */
13191 if (DECL_P (decl))
13192 check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13193
13194 return decl;
13195 }
13196
13197 /* Like cp_parser_lookup_name, but for use in the typical case where
13198 CHECK_ACCESS is TRUE, IS_TYPE is FALSE, and CHECK_DEPENDENCY is
13199 TRUE. */
13200
13201 static tree
13202 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13203 {
13204 return cp_parser_lookup_name (parser, name,
13205 /*is_type=*/false,
13206 /*is_namespace=*/false,
13207 /*check_dependency=*/true);
13208 }
13209
13210 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13211 the current context, return the TYPE_DECL. If TAG_NAME_P is
13212 true, the DECL indicates the class being defined in a class-head,
13213 or declared in an elaborated-type-specifier.
13214
13215 Otherwise, return DECL. */
13216
13217 static tree
13218 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13219 {
13220 /* If the TEMPLATE_DECL is being declared as part of a class-head,
13221 the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13222
13223 struct A {
13224 template <typename T> struct B;
13225 };
13226
13227 template <typename T> struct A::B {};
13228
13229 Similarly, in a elaborated-type-specifier:
13230
13231 namespace N { struct X{}; }
13232
13233 struct A {
13234 template <typename T> friend struct N::X;
13235 };
13236
13237 However, if the DECL refers to a class type, and we are in
13238 the scope of the class, then the name lookup automatically
13239 finds the TYPE_DECL created by build_self_reference rather
13240 than a TEMPLATE_DECL. For example, in:
13241
13242 template <class T> struct S {
13243 S s;
13244 };
13245
13246 there is no need to handle such case. */
13247
13248 if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13249 return DECL_TEMPLATE_RESULT (decl);
13250
13251 return decl;
13252 }
13253
13254 /* If too many, or too few, template-parameter lists apply to the
13255 declarator, issue an error message. Returns TRUE if all went well,
13256 and FALSE otherwise. */
13257
13258 static bool
13259 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13260 tree declarator)
13261 {
13262 unsigned num_templates;
13263
13264 /* We haven't seen any classes that involve template parameters yet. */
13265 num_templates = 0;
13266
13267 switch (TREE_CODE (declarator))
13268 {
13269 case CALL_EXPR:
13270 case ARRAY_REF:
13271 case INDIRECT_REF:
13272 case ADDR_EXPR:
13273 {
13274 tree main_declarator = TREE_OPERAND (declarator, 0);
13275 return
13276 cp_parser_check_declarator_template_parameters (parser,
13277 main_declarator);
13278 }
13279
13280 case SCOPE_REF:
13281 {
13282 tree scope;
13283 tree member;
13284
13285 scope = TREE_OPERAND (declarator, 0);
13286 member = TREE_OPERAND (declarator, 1);
13287
13288 /* If this is a pointer-to-member, then we are not interested
13289 in the SCOPE, because it does not qualify the thing that is
13290 being declared. */
13291 if (TREE_CODE (member) == INDIRECT_REF)
13292 return (cp_parser_check_declarator_template_parameters
13293 (parser, member));
13294
13295 while (scope && CLASS_TYPE_P (scope))
13296 {
13297 /* You're supposed to have one `template <...>'
13298 for every template class, but you don't need one
13299 for a full specialization. For example:
13300
13301 template <class T> struct S{};
13302 template <> struct S<int> { void f(); };
13303 void S<int>::f () {}
13304
13305 is correct; there shouldn't be a `template <>' for
13306 the definition of `S<int>::f'. */
13307 if (CLASSTYPE_TEMPLATE_INFO (scope)
13308 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13309 || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13310 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13311 ++num_templates;
13312
13313 scope = TYPE_CONTEXT (scope);
13314 }
13315 }
13316
13317 /* Fall through. */
13318
13319 default:
13320 /* If the DECLARATOR has the form `X<y>' then it uses one
13321 additional level of template parameters. */
13322 if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13323 ++num_templates;
13324
13325 return cp_parser_check_template_parameters (parser,
13326 num_templates);
13327 }
13328 }
13329
13330 /* NUM_TEMPLATES were used in the current declaration. If that is
13331 invalid, return FALSE and issue an error messages. Otherwise,
13332 return TRUE. */
13333
13334 static bool
13335 cp_parser_check_template_parameters (cp_parser* parser,
13336 unsigned num_templates)
13337 {
13338 /* If there are more template classes than parameter lists, we have
13339 something like:
13340
13341 template <class T> void S<T>::R<T>::f (); */
13342 if (parser->num_template_parameter_lists < num_templates)
13343 {
13344 error ("too few template-parameter-lists");
13345 return false;
13346 }
13347 /* If there are the same number of template classes and parameter
13348 lists, that's OK. */
13349 if (parser->num_template_parameter_lists == num_templates)
13350 return true;
13351 /* If there are more, but only one more, then we are referring to a
13352 member template. That's OK too. */
13353 if (parser->num_template_parameter_lists == num_templates + 1)
13354 return true;
13355 /* Otherwise, there are too many template parameter lists. We have
13356 something like:
13357
13358 template <class T> template <class U> void S::f(); */
13359 error ("too many template-parameter-lists");
13360 return false;
13361 }
13362
13363 /* Parse a binary-expression of the general form:
13364
13365 binary-expression:
13366 <expr>
13367 binary-expression <token> <expr>
13368
13369 The TOKEN_TREE_MAP maps <token> types to <expr> codes. FN is used
13370 to parser the <expr>s. If the first production is used, then the
13371 value returned by FN is returned directly. Otherwise, a node with
13372 the indicated EXPR_TYPE is returned, with operands corresponding to
13373 the two sub-expressions. */
13374
13375 static tree
13376 cp_parser_binary_expression (cp_parser* parser,
13377 const cp_parser_token_tree_map token_tree_map,
13378 cp_parser_expression_fn fn)
13379 {
13380 tree lhs;
13381
13382 /* Parse the first expression. */
13383 lhs = (*fn) (parser);
13384 /* Now, look for more expressions. */
13385 while (true)
13386 {
13387 cp_token *token;
13388 const cp_parser_token_tree_map_node *map_node;
13389 tree rhs;
13390
13391 /* Peek at the next token. */
13392 token = cp_lexer_peek_token (parser->lexer);
13393 /* If the token is `>', and that's not an operator at the
13394 moment, then we're done. */
13395 if (token->type == CPP_GREATER
13396 && !parser->greater_than_is_operator_p)
13397 break;
13398 /* If we find one of the tokens we want, build the corresponding
13399 tree representation. */
13400 for (map_node = token_tree_map;
13401 map_node->token_type != CPP_EOF;
13402 ++map_node)
13403 if (map_node->token_type == token->type)
13404 {
13405 /* Consume the operator token. */
13406 cp_lexer_consume_token (parser->lexer);
13407 /* Parse the right-hand side of the expression. */
13408 rhs = (*fn) (parser);
13409 /* Build the binary tree node. */
13410 lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13411 break;
13412 }
13413
13414 /* If the token wasn't one of the ones we want, we're done. */
13415 if (map_node->token_type == CPP_EOF)
13416 break;
13417 }
13418
13419 return lhs;
13420 }
13421
13422 /* Parse an optional `::' token indicating that the following name is
13423 from the global namespace. If so, PARSER->SCOPE is set to the
13424 GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
13425 unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
13426 Returns the new value of PARSER->SCOPE, if the `::' token is
13427 present, and NULL_TREE otherwise. */
13428
13429 static tree
13430 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
13431 {
13432 cp_token *token;
13433
13434 /* Peek at the next token. */
13435 token = cp_lexer_peek_token (parser->lexer);
13436 /* If we're looking at a `::' token then we're starting from the
13437 global namespace, not our current location. */
13438 if (token->type == CPP_SCOPE)
13439 {
13440 /* Consume the `::' token. */
13441 cp_lexer_consume_token (parser->lexer);
13442 /* Set the SCOPE so that we know where to start the lookup. */
13443 parser->scope = global_namespace;
13444 parser->qualifying_scope = global_namespace;
13445 parser->object_scope = NULL_TREE;
13446
13447 return parser->scope;
13448 }
13449 else if (!current_scope_valid_p)
13450 {
13451 parser->scope = NULL_TREE;
13452 parser->qualifying_scope = NULL_TREE;
13453 parser->object_scope = NULL_TREE;
13454 }
13455
13456 return NULL_TREE;
13457 }
13458
13459 /* Returns TRUE if the upcoming token sequence is the start of a
13460 constructor declarator. If FRIEND_P is true, the declarator is
13461 preceded by the `friend' specifier. */
13462
13463 static bool
13464 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
13465 {
13466 bool constructor_p;
13467 tree type_decl = NULL_TREE;
13468 bool nested_name_p;
13469 cp_token *next_token;
13470
13471 /* The common case is that this is not a constructor declarator, so
13472 try to avoid doing lots of work if at all possible. It's not
13473 valid declare a constructor at function scope. */
13474 if (at_function_scope_p ())
13475 return false;
13476 /* And only certain tokens can begin a constructor declarator. */
13477 next_token = cp_lexer_peek_token (parser->lexer);
13478 if (next_token->type != CPP_NAME
13479 && next_token->type != CPP_SCOPE
13480 && next_token->type != CPP_NESTED_NAME_SPECIFIER
13481 && next_token->type != CPP_TEMPLATE_ID)
13482 return false;
13483
13484 /* Parse tentatively; we are going to roll back all of the tokens
13485 consumed here. */
13486 cp_parser_parse_tentatively (parser);
13487 /* Assume that we are looking at a constructor declarator. */
13488 constructor_p = true;
13489
13490 /* Look for the optional `::' operator. */
13491 cp_parser_global_scope_opt (parser,
13492 /*current_scope_valid_p=*/false);
13493 /* Look for the nested-name-specifier. */
13494 nested_name_p
13495 = (cp_parser_nested_name_specifier_opt (parser,
13496 /*typename_keyword_p=*/false,
13497 /*check_dependency_p=*/false,
13498 /*type_p=*/false)
13499 != NULL_TREE);
13500 /* Outside of a class-specifier, there must be a
13501 nested-name-specifier. */
13502 if (!nested_name_p &&
13503 (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
13504 || friend_p))
13505 constructor_p = false;
13506 /* If we still think that this might be a constructor-declarator,
13507 look for a class-name. */
13508 if (constructor_p)
13509 {
13510 /* If we have:
13511
13512 template <typename T> struct S { S(); };
13513 template <typename T> S<T>::S ();
13514
13515 we must recognize that the nested `S' names a class.
13516 Similarly, for:
13517
13518 template <typename T> S<T>::S<T> ();
13519
13520 we must recognize that the nested `S' names a template. */
13521 type_decl = cp_parser_class_name (parser,
13522 /*typename_keyword_p=*/false,
13523 /*template_keyword_p=*/false,
13524 /*type_p=*/false,
13525 /*check_dependency_p=*/false,
13526 /*class_head_p=*/false);
13527 /* If there was no class-name, then this is not a constructor. */
13528 constructor_p = !cp_parser_error_occurred (parser);
13529 }
13530
13531 /* If we're still considering a constructor, we have to see a `(',
13532 to begin the parameter-declaration-clause, followed by either a
13533 `)', an `...', or a decl-specifier. We need to check for a
13534 type-specifier to avoid being fooled into thinking that:
13535
13536 S::S (f) (int);
13537
13538 is a constructor. (It is actually a function named `f' that
13539 takes one parameter (of type `int') and returns a value of type
13540 `S::S'. */
13541 if (constructor_p
13542 && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13543 {
13544 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
13545 && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
13546 && !cp_parser_storage_class_specifier_opt (parser))
13547 {
13548 tree type;
13549
13550 /* Names appearing in the type-specifier should be looked up
13551 in the scope of the class. */
13552 if (current_class_type)
13553 type = NULL_TREE;
13554 else
13555 {
13556 type = TREE_TYPE (type_decl);
13557 if (TREE_CODE (type) == TYPENAME_TYPE)
13558 {
13559 type = resolve_typename_type (type,
13560 /*only_current_p=*/false);
13561 if (type == error_mark_node)
13562 {
13563 cp_parser_abort_tentative_parse (parser);
13564 return false;
13565 }
13566 }
13567 push_scope (type);
13568 }
13569 /* Look for the type-specifier. */
13570 cp_parser_type_specifier (parser,
13571 CP_PARSER_FLAGS_NONE,
13572 /*is_friend=*/false,
13573 /*is_declarator=*/true,
13574 /*declares_class_or_enum=*/NULL,
13575 /*is_cv_qualifier=*/NULL);
13576 /* Leave the scope of the class. */
13577 if (type)
13578 pop_scope (type);
13579
13580 constructor_p = !cp_parser_error_occurred (parser);
13581 }
13582 }
13583 else
13584 constructor_p = false;
13585 /* We did not really want to consume any tokens. */
13586 cp_parser_abort_tentative_parse (parser);
13587
13588 return constructor_p;
13589 }
13590
13591 /* Parse the definition of the function given by the DECL_SPECIFIERS,
13592 ATTRIBUTES, and DECLARATOR. The access checks have been deferred;
13593 they must be performed once we are in the scope of the function.
13594
13595 Returns the function defined. */
13596
13597 static tree
13598 cp_parser_function_definition_from_specifiers_and_declarator
13599 (cp_parser* parser,
13600 tree decl_specifiers,
13601 tree attributes,
13602 tree declarator)
13603 {
13604 tree fn;
13605 bool success_p;
13606
13607 /* Begin the function-definition. */
13608 success_p = begin_function_definition (decl_specifiers,
13609 attributes,
13610 declarator);
13611
13612 /* If there were names looked up in the decl-specifier-seq that we
13613 did not check, check them now. We must wait until we are in the
13614 scope of the function to perform the checks, since the function
13615 might be a friend. */
13616 perform_deferred_access_checks ();
13617
13618 if (!success_p)
13619 {
13620 /* If begin_function_definition didn't like the definition, skip
13621 the entire function. */
13622 error ("invalid function declaration");
13623 cp_parser_skip_to_end_of_block_or_statement (parser);
13624 fn = error_mark_node;
13625 }
13626 else
13627 fn = cp_parser_function_definition_after_declarator (parser,
13628 /*inline_p=*/false);
13629
13630 return fn;
13631 }
13632
13633 /* Parse the part of a function-definition that follows the
13634 declarator. INLINE_P is TRUE iff this function is an inline
13635 function defined with a class-specifier.
13636
13637 Returns the function defined. */
13638
13639 static tree
13640 cp_parser_function_definition_after_declarator (cp_parser* parser,
13641 bool inline_p)
13642 {
13643 tree fn;
13644 bool ctor_initializer_p = false;
13645 bool saved_in_unbraced_linkage_specification_p;
13646 unsigned saved_num_template_parameter_lists;
13647
13648 /* If the next token is `return', then the code may be trying to
13649 make use of the "named return value" extension that G++ used to
13650 support. */
13651 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
13652 {
13653 /* Consume the `return' keyword. */
13654 cp_lexer_consume_token (parser->lexer);
13655 /* Look for the identifier that indicates what value is to be
13656 returned. */
13657 cp_parser_identifier (parser);
13658 /* Issue an error message. */
13659 error ("named return values are no longer supported");
13660 /* Skip tokens until we reach the start of the function body. */
13661 while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
13662 cp_lexer_consume_token (parser->lexer);
13663 }
13664 /* The `extern' in `extern "C" void f () { ... }' does not apply to
13665 anything declared inside `f'. */
13666 saved_in_unbraced_linkage_specification_p
13667 = parser->in_unbraced_linkage_specification_p;
13668 parser->in_unbraced_linkage_specification_p = false;
13669 /* Inside the function, surrounding template-parameter-lists do not
13670 apply. */
13671 saved_num_template_parameter_lists
13672 = parser->num_template_parameter_lists;
13673 parser->num_template_parameter_lists = 0;
13674 /* If the next token is `try', then we are looking at a
13675 function-try-block. */
13676 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
13677 ctor_initializer_p = cp_parser_function_try_block (parser);
13678 /* A function-try-block includes the function-body, so we only do
13679 this next part if we're not processing a function-try-block. */
13680 else
13681 ctor_initializer_p
13682 = cp_parser_ctor_initializer_opt_and_function_body (parser);
13683
13684 /* Finish the function. */
13685 fn = finish_function ((ctor_initializer_p ? 1 : 0) |
13686 (inline_p ? 2 : 0));
13687 /* Generate code for it, if necessary. */
13688 expand_or_defer_fn (fn);
13689 /* Restore the saved values. */
13690 parser->in_unbraced_linkage_specification_p
13691 = saved_in_unbraced_linkage_specification_p;
13692 parser->num_template_parameter_lists
13693 = saved_num_template_parameter_lists;
13694
13695 return fn;
13696 }
13697
13698 /* Parse a template-declaration, assuming that the `export' (and
13699 `extern') keywords, if present, has already been scanned. MEMBER_P
13700 is as for cp_parser_template_declaration. */
13701
13702 static void
13703 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
13704 {
13705 tree decl = NULL_TREE;
13706 tree parameter_list;
13707 bool friend_p = false;
13708
13709 /* Look for the `template' keyword. */
13710 if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
13711 return;
13712
13713 /* And the `<'. */
13714 if (!cp_parser_require (parser, CPP_LESS, "`<'"))
13715 return;
13716
13717 /* Parse the template parameters. */
13718 begin_template_parm_list ();
13719 /* If the next token is `>', then we have an invalid
13720 specialization. Rather than complain about an invalid template
13721 parameter, issue an error message here. */
13722 if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
13723 {
13724 cp_parser_error (parser, "invalid explicit specialization");
13725 parameter_list = NULL_TREE;
13726 }
13727 else
13728 parameter_list = cp_parser_template_parameter_list (parser);
13729 parameter_list = end_template_parm_list (parameter_list);
13730 /* Look for the `>'. */
13731 cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
13732 /* We just processed one more parameter list. */
13733 ++parser->num_template_parameter_lists;
13734 /* If the next token is `template', there are more template
13735 parameters. */
13736 if (cp_lexer_next_token_is_keyword (parser->lexer,
13737 RID_TEMPLATE))
13738 cp_parser_template_declaration_after_export (parser, member_p);
13739 else
13740 {
13741 decl = cp_parser_single_declaration (parser,
13742 member_p,
13743 &friend_p);
13744
13745 /* If this is a member template declaration, let the front
13746 end know. */
13747 if (member_p && !friend_p && decl)
13748 decl = finish_member_template_decl (decl);
13749 else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
13750 make_friend_class (current_class_type, TREE_TYPE (decl));
13751 }
13752 /* We are done with the current parameter list. */
13753 --parser->num_template_parameter_lists;
13754
13755 /* Finish up. */
13756 finish_template_decl (parameter_list);
13757
13758 /* Register member declarations. */
13759 if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
13760 finish_member_declaration (decl);
13761
13762 /* If DECL is a function template, we must return to parse it later.
13763 (Even though there is no definition, there might be default
13764 arguments that need handling.) */
13765 if (member_p && decl
13766 && (TREE_CODE (decl) == FUNCTION_DECL
13767 || DECL_FUNCTION_TEMPLATE_P (decl)))
13768 TREE_VALUE (parser->unparsed_functions_queues)
13769 = tree_cons (NULL_TREE, decl,
13770 TREE_VALUE (parser->unparsed_functions_queues));
13771 }
13772
13773 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
13774 `function-definition' sequence. MEMBER_P is true, this declaration
13775 appears in a class scope.
13776
13777 Returns the DECL for the declared entity. If FRIEND_P is non-NULL,
13778 *FRIEND_P is set to TRUE iff the declaration is a friend. */
13779
13780 static tree
13781 cp_parser_single_declaration (cp_parser* parser,
13782 bool member_p,
13783 bool* friend_p)
13784 {
13785 bool declares_class_or_enum;
13786 tree decl = NULL_TREE;
13787 tree decl_specifiers;
13788 tree attributes;
13789
13790 /* Parse the dependent declaration. We don't know yet
13791 whether it will be a function-definition. */
13792 cp_parser_parse_tentatively (parser);
13793 /* Defer access checks until we know what is being declared. */
13794 push_deferring_access_checks (dk_deferred);
13795
13796 /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
13797 alternative. */
13798 decl_specifiers
13799 = cp_parser_decl_specifier_seq (parser,
13800 CP_PARSER_FLAGS_OPTIONAL,
13801 &attributes,
13802 &declares_class_or_enum);
13803 /* Gather up the access checks that occurred the
13804 decl-specifier-seq. */
13805 stop_deferring_access_checks ();
13806
13807 /* Check for the declaration of a template class. */
13808 if (declares_class_or_enum)
13809 {
13810 if (cp_parser_declares_only_class_p (parser))
13811 {
13812 decl = shadow_tag (decl_specifiers);
13813 if (decl)
13814 decl = TYPE_NAME (decl);
13815 else
13816 decl = error_mark_node;
13817 }
13818 }
13819 else
13820 decl = NULL_TREE;
13821 /* If it's not a template class, try for a template function. If
13822 the next token is a `;', then this declaration does not declare
13823 anything. But, if there were errors in the decl-specifiers, then
13824 the error might well have come from an attempted class-specifier.
13825 In that case, there's no need to warn about a missing declarator. */
13826 if (!decl
13827 && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
13828 || !value_member (error_mark_node, decl_specifiers)))
13829 decl = cp_parser_init_declarator (parser,
13830 decl_specifiers,
13831 attributes,
13832 /*function_definition_allowed_p=*/false,
13833 member_p,
13834 /*function_definition_p=*/NULL);
13835
13836 pop_deferring_access_checks ();
13837
13838 /* Clear any current qualification; whatever comes next is the start
13839 of something new. */
13840 parser->scope = NULL_TREE;
13841 parser->qualifying_scope = NULL_TREE;
13842 parser->object_scope = NULL_TREE;
13843 /* Look for a trailing `;' after the declaration. */
13844 if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'")
13845 && cp_parser_committed_to_tentative_parse (parser))
13846 cp_parser_skip_to_end_of_block_or_statement (parser);
13847 /* If it worked, set *FRIEND_P based on the DECL_SPECIFIERS. */
13848 if (cp_parser_parse_definitely (parser))
13849 {
13850 if (friend_p)
13851 *friend_p = cp_parser_friend_p (decl_specifiers);
13852 }
13853 /* Otherwise, try a function-definition. */
13854 else
13855 decl = cp_parser_function_definition (parser, friend_p);
13856
13857 return decl;
13858 }
13859
13860 /* Parse a cast-expression that is not the operand of a unary "&". */
13861
13862 static tree
13863 cp_parser_simple_cast_expression (cp_parser *parser)
13864 {
13865 return cp_parser_cast_expression (parser, /*address_p=*/false);
13866 }
13867
13868 /* Parse a functional cast to TYPE. Returns an expression
13869 representing the cast. */
13870
13871 static tree
13872 cp_parser_functional_cast (cp_parser* parser, tree type)
13873 {
13874 tree expression_list;
13875
13876 /* Look for the opening `('. */
13877 if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
13878 return error_mark_node;
13879 /* If the next token is not an `)', there are arguments to the
13880 cast. */
13881 if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
13882 expression_list = cp_parser_expression_list (parser);
13883 else
13884 expression_list = NULL_TREE;
13885 /* Look for the closing `)'. */
13886 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13887
13888 return build_functional_cast (type, expression_list);
13889 }
13890
13891 /* MEMBER_FUNCTION is a member function, or a friend. If default
13892 arguments, or the body of the function have not yet been parsed,
13893 parse them now. */
13894
13895 static void
13896 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
13897 {
13898 cp_lexer *saved_lexer;
13899
13900 /* If this member is a template, get the underlying
13901 FUNCTION_DECL. */
13902 if (DECL_FUNCTION_TEMPLATE_P (member_function))
13903 member_function = DECL_TEMPLATE_RESULT (member_function);
13904
13905 /* There should not be any class definitions in progress at this
13906 point; the bodies of members are only parsed outside of all class
13907 definitions. */
13908 my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
13909 /* While we're parsing the member functions we might encounter more
13910 classes. We want to handle them right away, but we don't want
13911 them getting mixed up with functions that are currently in the
13912 queue. */
13913 parser->unparsed_functions_queues
13914 = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
13915
13916 /* Make sure that any template parameters are in scope. */
13917 maybe_begin_member_template_processing (member_function);
13918
13919 /* If the body of the function has not yet been parsed, parse it
13920 now. */
13921 if (DECL_PENDING_INLINE_P (member_function))
13922 {
13923 tree function_scope;
13924 cp_token_cache *tokens;
13925
13926 /* The function is no longer pending; we are processing it. */
13927 tokens = DECL_PENDING_INLINE_INFO (member_function);
13928 DECL_PENDING_INLINE_INFO (member_function) = NULL;
13929 DECL_PENDING_INLINE_P (member_function) = 0;
13930 /* If this was an inline function in a local class, enter the scope
13931 of the containing function. */
13932 function_scope = decl_function_context (member_function);
13933 if (function_scope)
13934 push_function_context_to (function_scope);
13935
13936 /* Save away the current lexer. */
13937 saved_lexer = parser->lexer;
13938 /* Make a new lexer to feed us the tokens saved for this function. */
13939 parser->lexer = cp_lexer_new_from_tokens (tokens);
13940 parser->lexer->next = saved_lexer;
13941
13942 /* Set the current source position to be the location of the first
13943 token in the saved inline body. */
13944 cp_lexer_peek_token (parser->lexer);
13945
13946 /* Let the front end know that we going to be defining this
13947 function. */
13948 start_function (NULL_TREE, member_function, NULL_TREE,
13949 SF_PRE_PARSED | SF_INCLASS_INLINE);
13950
13951 /* Now, parse the body of the function. */
13952 cp_parser_function_definition_after_declarator (parser,
13953 /*inline_p=*/true);
13954
13955 /* Leave the scope of the containing function. */
13956 if (function_scope)
13957 pop_function_context_from (function_scope);
13958 /* Restore the lexer. */
13959 parser->lexer = saved_lexer;
13960 }
13961
13962 /* Remove any template parameters from the symbol table. */
13963 maybe_end_member_template_processing ();
13964
13965 /* Restore the queue. */
13966 parser->unparsed_functions_queues
13967 = TREE_CHAIN (parser->unparsed_functions_queues);
13968 }
13969
13970 /* If DECL contains any default args, remeber it on the unparsed
13971 functions queue. */
13972
13973 static void
13974 cp_parser_save_default_args (cp_parser* parser, tree decl)
13975 {
13976 tree probe;
13977
13978 for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
13979 probe;
13980 probe = TREE_CHAIN (probe))
13981 if (TREE_PURPOSE (probe))
13982 {
13983 TREE_PURPOSE (parser->unparsed_functions_queues)
13984 = tree_cons (NULL_TREE, decl,
13985 TREE_PURPOSE (parser->unparsed_functions_queues));
13986 break;
13987 }
13988 return;
13989 }
13990
13991 /* FN is a FUNCTION_DECL which may contains a parameter with an
13992 unparsed DEFAULT_ARG. Parse the default args now. */
13993
13994 static void
13995 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
13996 {
13997 cp_lexer *saved_lexer;
13998 cp_token_cache *tokens;
13999 bool saved_local_variables_forbidden_p;
14000 tree parameters;
14001
14002 for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14003 parameters;
14004 parameters = TREE_CHAIN (parameters))
14005 {
14006 if (!TREE_PURPOSE (parameters)
14007 || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14008 continue;
14009
14010 /* Save away the current lexer. */
14011 saved_lexer = parser->lexer;
14012 /* Create a new one, using the tokens we have saved. */
14013 tokens = DEFARG_TOKENS (TREE_PURPOSE (parameters));
14014 parser->lexer = cp_lexer_new_from_tokens (tokens);
14015
14016 /* Set the current source position to be the location of the
14017 first token in the default argument. */
14018 cp_lexer_peek_token (parser->lexer);
14019
14020 /* Local variable names (and the `this' keyword) may not appear
14021 in a default argument. */
14022 saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14023 parser->local_variables_forbidden_p = true;
14024 /* Parse the assignment-expression. */
14025 if (DECL_CONTEXT (fn))
14026 push_nested_class (DECL_CONTEXT (fn));
14027 TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14028 if (DECL_CONTEXT (fn))
14029 pop_nested_class ();
14030
14031 /* Restore saved state. */
14032 parser->lexer = saved_lexer;
14033 parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14034 }
14035 }
14036
14037 /* Parse the operand of `sizeof' (or a similar operator). Returns
14038 either a TYPE or an expression, depending on the form of the
14039 input. The KEYWORD indicates which kind of expression we have
14040 encountered. */
14041
14042 static tree
14043 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14044 {
14045 static const char *format;
14046 tree expr = NULL_TREE;
14047 const char *saved_message;
14048 bool saved_constant_expression_p;
14049
14050 /* Initialize FORMAT the first time we get here. */
14051 if (!format)
14052 format = "types may not be defined in `%s' expressions";
14053
14054 /* Types cannot be defined in a `sizeof' expression. Save away the
14055 old message. */
14056 saved_message = parser->type_definition_forbidden_message;
14057 /* And create the new one. */
14058 parser->type_definition_forbidden_message
14059 = ((const char *)
14060 xmalloc (strlen (format)
14061 + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14062 + 1 /* `\0' */));
14063 sprintf ((char *) parser->type_definition_forbidden_message,
14064 format, IDENTIFIER_POINTER (ridpointers[keyword]));
14065
14066 /* The restrictions on constant-expressions do not apply inside
14067 sizeof expressions. */
14068 saved_constant_expression_p = parser->constant_expression_p;
14069 parser->constant_expression_p = false;
14070
14071 /* Do not actually evaluate the expression. */
14072 ++skip_evaluation;
14073 /* If it's a `(', then we might be looking at the type-id
14074 construction. */
14075 if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14076 {
14077 tree type;
14078
14079 /* We can't be sure yet whether we're looking at a type-id or an
14080 expression. */
14081 cp_parser_parse_tentatively (parser);
14082 /* Consume the `('. */
14083 cp_lexer_consume_token (parser->lexer);
14084 /* Parse the type-id. */
14085 type = cp_parser_type_id (parser);
14086 /* Now, look for the trailing `)'. */
14087 cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14088 /* If all went well, then we're done. */
14089 if (cp_parser_parse_definitely (parser))
14090 {
14091 /* Build a list of decl-specifiers; right now, we have only
14092 a single type-specifier. */
14093 type = build_tree_list (NULL_TREE,
14094 type);
14095
14096 /* Call grokdeclarator to figure out what type this is. */
14097 expr = grokdeclarator (NULL_TREE,
14098 type,
14099 TYPENAME,
14100 /*initialized=*/0,
14101 /*attrlist=*/NULL);
14102 }
14103 }
14104
14105 /* If the type-id production did not work out, then we must be
14106 looking at the unary-expression production. */
14107 if (!expr)
14108 expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14109 /* Go back to evaluating expressions. */
14110 --skip_evaluation;
14111
14112 /* Free the message we created. */
14113 free ((char *) parser->type_definition_forbidden_message);
14114 /* And restore the old one. */
14115 parser->type_definition_forbidden_message = saved_message;
14116 parser->constant_expression_p = saved_constant_expression_p;
14117
14118 return expr;
14119 }
14120
14121 /* If the current declaration has no declarator, return true. */
14122
14123 static bool
14124 cp_parser_declares_only_class_p (cp_parser *parser)
14125 {
14126 /* If the next token is a `;' or a `,' then there is no
14127 declarator. */
14128 return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14129 || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14130 }
14131
14132 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14133 Returns TRUE iff `friend' appears among the DECL_SPECIFIERS. */
14134
14135 static bool
14136 cp_parser_friend_p (tree decl_specifiers)
14137 {
14138 while (decl_specifiers)
14139 {
14140 /* See if this decl-specifier is `friend'. */
14141 if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14142 && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14143 return true;
14144
14145 /* Go on to the next decl-specifier. */
14146 decl_specifiers = TREE_CHAIN (decl_specifiers);
14147 }
14148
14149 return false;
14150 }
14151
14152 /* If the next token is of the indicated TYPE, consume it. Otherwise,
14153 issue an error message indicating that TOKEN_DESC was expected.
14154
14155 Returns the token consumed, if the token had the appropriate type.
14156 Otherwise, returns NULL. */
14157
14158 static cp_token *
14159 cp_parser_require (cp_parser* parser,
14160 enum cpp_ttype type,
14161 const char* token_desc)
14162 {
14163 if (cp_lexer_next_token_is (parser->lexer, type))
14164 return cp_lexer_consume_token (parser->lexer);
14165 else
14166 {
14167 /* Output the MESSAGE -- unless we're parsing tentatively. */
14168 if (!cp_parser_simulate_error (parser))
14169 error ("expected %s", token_desc);
14170 return NULL;
14171 }
14172 }
14173
14174 /* Like cp_parser_require, except that tokens will be skipped until
14175 the desired token is found. An error message is still produced if
14176 the next token is not as expected. */
14177
14178 static void
14179 cp_parser_skip_until_found (cp_parser* parser,
14180 enum cpp_ttype type,
14181 const char* token_desc)
14182 {
14183 cp_token *token;
14184 unsigned nesting_depth = 0;
14185
14186 if (cp_parser_require (parser, type, token_desc))
14187 return;
14188
14189 /* Skip tokens until the desired token is found. */
14190 while (true)
14191 {
14192 /* Peek at the next token. */
14193 token = cp_lexer_peek_token (parser->lexer);
14194 /* If we've reached the token we want, consume it and
14195 stop. */
14196 if (token->type == type && !nesting_depth)
14197 {
14198 cp_lexer_consume_token (parser->lexer);
14199 return;
14200 }
14201 /* If we've run out of tokens, stop. */
14202 if (token->type == CPP_EOF)
14203 return;
14204 if (token->type == CPP_OPEN_BRACE
14205 || token->type == CPP_OPEN_PAREN
14206 || token->type == CPP_OPEN_SQUARE)
14207 ++nesting_depth;
14208 else if (token->type == CPP_CLOSE_BRACE
14209 || token->type == CPP_CLOSE_PAREN
14210 || token->type == CPP_CLOSE_SQUARE)
14211 {
14212 if (nesting_depth-- == 0)
14213 return;
14214 }
14215 /* Consume this token. */
14216 cp_lexer_consume_token (parser->lexer);
14217 }
14218 }
14219
14220 /* If the next token is the indicated keyword, consume it. Otherwise,
14221 issue an error message indicating that TOKEN_DESC was expected.
14222
14223 Returns the token consumed, if the token had the appropriate type.
14224 Otherwise, returns NULL. */
14225
14226 static cp_token *
14227 cp_parser_require_keyword (cp_parser* parser,
14228 enum rid keyword,
14229 const char* token_desc)
14230 {
14231 cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14232
14233 if (token && token->keyword != keyword)
14234 {
14235 dyn_string_t error_msg;
14236
14237 /* Format the error message. */
14238 error_msg = dyn_string_new (0);
14239 dyn_string_append_cstr (error_msg, "expected ");
14240 dyn_string_append_cstr (error_msg, token_desc);
14241 cp_parser_error (parser, error_msg->s);
14242 dyn_string_delete (error_msg);
14243 return NULL;
14244 }
14245
14246 return token;
14247 }
14248
14249 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14250 function-definition. */
14251
14252 static bool
14253 cp_parser_token_starts_function_definition_p (cp_token* token)
14254 {
14255 return (/* An ordinary function-body begins with an `{'. */
14256 token->type == CPP_OPEN_BRACE
14257 /* A ctor-initializer begins with a `:'. */
14258 || token->type == CPP_COLON
14259 /* A function-try-block begins with `try'. */
14260 || token->keyword == RID_TRY
14261 /* The named return value extension begins with `return'. */
14262 || token->keyword == RID_RETURN);
14263 }
14264
14265 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
14266 definition. */
14267
14268 static bool
14269 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
14270 {
14271 cp_token *token;
14272
14273 token = cp_lexer_peek_token (parser->lexer);
14274 return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
14275 }
14276
14277 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
14278 or none_type otherwise. */
14279
14280 static enum tag_types
14281 cp_parser_token_is_class_key (cp_token* token)
14282 {
14283 switch (token->keyword)
14284 {
14285 case RID_CLASS:
14286 return class_type;
14287 case RID_STRUCT:
14288 return record_type;
14289 case RID_UNION:
14290 return union_type;
14291
14292 default:
14293 return none_type;
14294 }
14295 }
14296
14297 /* Issue an error message if the CLASS_KEY does not match the TYPE. */
14298
14299 static void
14300 cp_parser_check_class_key (enum tag_types class_key, tree type)
14301 {
14302 if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
14303 pedwarn ("`%s' tag used in naming `%#T'",
14304 class_key == union_type ? "union"
14305 : class_key == record_type ? "struct" : "class",
14306 type);
14307 }
14308
14309 /* Look for the `template' keyword, as a syntactic disambiguator.
14310 Return TRUE iff it is present, in which case it will be
14311 consumed. */
14312
14313 static bool
14314 cp_parser_optional_template_keyword (cp_parser *parser)
14315 {
14316 if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
14317 {
14318 /* The `template' keyword can only be used within templates;
14319 outside templates the parser can always figure out what is a
14320 template and what is not. */
14321 if (!processing_template_decl)
14322 {
14323 error ("`template' (as a disambiguator) is only allowed "
14324 "within templates");
14325 /* If this part of the token stream is rescanned, the same
14326 error message would be generated. So, we purge the token
14327 from the stream. */
14328 cp_lexer_purge_token (parser->lexer);
14329 return false;
14330 }
14331 else
14332 {
14333 /* Consume the `template' keyword. */
14334 cp_lexer_consume_token (parser->lexer);
14335 return true;
14336 }
14337 }
14338
14339 return false;
14340 }
14341
14342 /* The next token is a CPP_NESTED_NAME_SPECIFIER. Consume the token,
14343 set PARSER->SCOPE, and perform other related actions. */
14344
14345 static void
14346 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
14347 {
14348 tree value;
14349 tree check;
14350
14351 /* Get the stored value. */
14352 value = cp_lexer_consume_token (parser->lexer)->value;
14353 /* Perform any access checks that were deferred. */
14354 for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
14355 perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
14356 /* Set the scope from the stored value. */
14357 parser->scope = TREE_VALUE (value);
14358 parser->qualifying_scope = TREE_TYPE (value);
14359 parser->object_scope = NULL_TREE;
14360 }
14361
14362 /* Add tokens to CACHE until an non-nested END token appears. */
14363
14364 static void
14365 cp_parser_cache_group (cp_parser *parser,
14366 cp_token_cache *cache,
14367 enum cpp_ttype end,
14368 unsigned depth)
14369 {
14370 while (true)
14371 {
14372 cp_token *token;
14373
14374 /* Abort a parenthesized expression if we encounter a brace. */
14375 if ((end == CPP_CLOSE_PAREN || depth == 0)
14376 && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14377 return;
14378 /* Consume the next token. */
14379 token = cp_lexer_consume_token (parser->lexer);
14380 /* If we've reached the end of the file, stop. */
14381 if (token->type == CPP_EOF)
14382 return;
14383 /* Add this token to the tokens we are saving. */
14384 cp_token_cache_push_token (cache, token);
14385 /* See if it starts a new group. */
14386 if (token->type == CPP_OPEN_BRACE)
14387 {
14388 cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
14389 if (depth == 0)
14390 return;
14391 }
14392 else if (token->type == CPP_OPEN_PAREN)
14393 cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
14394 else if (token->type == end)
14395 return;
14396 }
14397 }
14398
14399 /* Begin parsing tentatively. We always save tokens while parsing
14400 tentatively so that if the tentative parsing fails we can restore the
14401 tokens. */
14402
14403 static void
14404 cp_parser_parse_tentatively (cp_parser* parser)
14405 {
14406 /* Enter a new parsing context. */
14407 parser->context = cp_parser_context_new (parser->context);
14408 /* Begin saving tokens. */
14409 cp_lexer_save_tokens (parser->lexer);
14410 /* In order to avoid repetitive access control error messages,
14411 access checks are queued up until we are no longer parsing
14412 tentatively. */
14413 push_deferring_access_checks (dk_deferred);
14414 }
14415
14416 /* Commit to the currently active tentative parse. */
14417
14418 static void
14419 cp_parser_commit_to_tentative_parse (cp_parser* parser)
14420 {
14421 cp_parser_context *context;
14422 cp_lexer *lexer;
14423
14424 /* Mark all of the levels as committed. */
14425 lexer = parser->lexer;
14426 for (context = parser->context; context->next; context = context->next)
14427 {
14428 if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
14429 break;
14430 context->status = CP_PARSER_STATUS_KIND_COMMITTED;
14431 while (!cp_lexer_saving_tokens (lexer))
14432 lexer = lexer->next;
14433 cp_lexer_commit_tokens (lexer);
14434 }
14435 }
14436
14437 /* Abort the currently active tentative parse. All consumed tokens
14438 will be rolled back, and no diagnostics will be issued. */
14439
14440 static void
14441 cp_parser_abort_tentative_parse (cp_parser* parser)
14442 {
14443 cp_parser_simulate_error (parser);
14444 /* Now, pretend that we want to see if the construct was
14445 successfully parsed. */
14446 cp_parser_parse_definitely (parser);
14447 }
14448
14449 /* Stop parsing tentatively. If a parse error has occurred, restore the
14450 token stream. Otherwise, commit to the tokens we have consumed.
14451 Returns true if no error occurred; false otherwise. */
14452
14453 static bool
14454 cp_parser_parse_definitely (cp_parser* parser)
14455 {
14456 bool error_occurred;
14457 cp_parser_context *context;
14458
14459 /* Remember whether or not an error occurred, since we are about to
14460 destroy that information. */
14461 error_occurred = cp_parser_error_occurred (parser);
14462 /* Remove the topmost context from the stack. */
14463 context = parser->context;
14464 parser->context = context->next;
14465 /* If no parse errors occurred, commit to the tentative parse. */
14466 if (!error_occurred)
14467 {
14468 /* Commit to the tokens read tentatively, unless that was
14469 already done. */
14470 if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
14471 cp_lexer_commit_tokens (parser->lexer);
14472
14473 pop_to_parent_deferring_access_checks ();
14474 }
14475 /* Otherwise, if errors occurred, roll back our state so that things
14476 are just as they were before we began the tentative parse. */
14477 else
14478 {
14479 cp_lexer_rollback_tokens (parser->lexer);
14480 pop_deferring_access_checks ();
14481 }
14482 /* Add the context to the front of the free list. */
14483 context->next = cp_parser_context_free_list;
14484 cp_parser_context_free_list = context;
14485
14486 return !error_occurred;
14487 }
14488
14489 /* Returns true if we are parsing tentatively -- but have decided that
14490 we will stick with this tentative parse, even if errors occur. */
14491
14492 static bool
14493 cp_parser_committed_to_tentative_parse (cp_parser* parser)
14494 {
14495 return (cp_parser_parsing_tentatively (parser)
14496 && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
14497 }
14498
14499 /* Returns nonzero iff an error has occurred during the most recent
14500 tentative parse. */
14501
14502 static bool
14503 cp_parser_error_occurred (cp_parser* parser)
14504 {
14505 return (cp_parser_parsing_tentatively (parser)
14506 && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
14507 }
14508
14509 /* Returns nonzero if GNU extensions are allowed. */
14510
14511 static bool
14512 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
14513 {
14514 return parser->allow_gnu_extensions_p;
14515 }
14516
14517 \f
14518
14519 /* The parser. */
14520
14521 static GTY (()) cp_parser *the_parser;
14522
14523 /* External interface. */
14524
14525 /* Parse the entire translation unit. */
14526
14527 int
14528 yyparse (void)
14529 {
14530 bool error_occurred;
14531
14532 the_parser = cp_parser_new ();
14533 push_deferring_access_checks (flag_access_control
14534 ? dk_no_deferred : dk_no_check);
14535 error_occurred = cp_parser_translation_unit (the_parser);
14536 the_parser = NULL;
14537
14538 finish_file ();
14539
14540 return error_occurred;
14541 }
14542
14543 /* Clean up after parsing the entire translation unit. */
14544
14545 void
14546 free_parser_stacks (void)
14547 {
14548 /* Nothing to do. */
14549 }
14550
14551 /* This variable must be provided by every front end. */
14552
14553 int yydebug;
14554
14555 #include "gt-cp-parser.h"