9184f0ea1db4b202afa43289495a7d2975baf107
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37
38 \f
39 /* The lexer.  */
40
41 /* Overview
42    --------
43
44    A cp_lexer represents a stream of cp_tokens.  It allows arbitrary
45    look-ahead.
46
47    Methodology
48    -----------
49
50    We use a circular buffer to store incoming tokens.
51
52    Some artifacts of the C++ language (such as the
53    expression/declaration ambiguity) require arbitrary look-ahead.
54    The strategy we adopt for dealing with these problems is to attempt
55    to parse one construct (e.g., the declaration) and fall back to the
56    other (e.g., the expression) if that attempt does not succeed.
57    Therefore, we must sometimes store an arbitrary number of tokens.
58
59    The parser routinely peeks at the next token, and then consumes it
60    later.  That also requires a buffer in which to store the tokens.
61
62    In order to easily permit adding tokens to the end of the buffer,
63    while removing them from the beginning of the buffer, we use a
64    circular buffer.  */
65
66 /* A C++ token.  */
67
68 typedef struct cp_token GTY (())
69 {
70   /* The kind of token.  */
71   ENUM_BITFIELD (cpp_ttype) type : 8;
72   /* If this token is a keyword, this value indicates which keyword.
73      Otherwise, this value is RID_MAX.  */
74   ENUM_BITFIELD (rid) keyword : 8;
75   /* Token flags.  */
76   unsigned char flags;
77   /* The value associated with this token, if any.  */
78   tree value;
79   /* The location at which this token was found.  */
80   location_t location;
81 } cp_token;
82
83 /* The number of tokens in a single token block.
84    Computed so that cp_token_block fits in a 512B allocation unit.  */
85
86 #define CP_TOKEN_BLOCK_NUM_TOKENS ((512 - 3*sizeof (char*))/sizeof (cp_token))
87
88 /* A group of tokens.  These groups are chained together to store
89    large numbers of tokens.  (For example, a token block is created
90    when the body of an inline member function is first encountered;
91    the tokens are processed later after the class definition is
92    complete.)
93
94    This somewhat ungainly data structure (as opposed to, say, a
95    variable-length array), is used due to constraints imposed by the
96    current garbage-collection methodology.  If it is made more
97    flexible, we could perhaps simplify the data structures involved.  */
98
99 typedef struct cp_token_block GTY (())
100 {
101   /* The tokens.  */
102   cp_token tokens[CP_TOKEN_BLOCK_NUM_TOKENS];
103   /* The number of tokens in this block.  */
104   size_t num_tokens;
105   /* The next token block in the chain.  */
106   struct cp_token_block *next;
107   /* The previous block in the chain.  */
108   struct cp_token_block *prev;
109 } cp_token_block;
110
111 typedef struct cp_token_cache GTY (())
112 {
113   /* The first block in the cache.  NULL if there are no tokens in the
114      cache.  */
115   cp_token_block *first;
116   /* The last block in the cache.  NULL If there are no tokens in the
117      cache.  */
118   cp_token_block *last;
119 } cp_token_cache;
120
121 /* Prototypes.  */
122
123 static cp_token_cache *cp_token_cache_new
124   (void);
125 static void cp_token_cache_push_token
126   (cp_token_cache *, cp_token *);
127
128 /* Create a new cp_token_cache.  */
129
130 static cp_token_cache *
131 cp_token_cache_new (void)
132 {
133   return ggc_alloc_cleared (sizeof (cp_token_cache));
134 }
135
136 /* Add *TOKEN to *CACHE.  */
137
138 static void
139 cp_token_cache_push_token (cp_token_cache *cache,
140                            cp_token *token)
141 {
142   cp_token_block *b = cache->last;
143
144   /* See if we need to allocate a new token block.  */
145   if (!b || b->num_tokens == CP_TOKEN_BLOCK_NUM_TOKENS)
146     {
147       b = ggc_alloc_cleared (sizeof (cp_token_block));
148       b->prev = cache->last;
149       if (cache->last)
150         {
151           cache->last->next = b;
152           cache->last = b;
153         }
154       else
155         cache->first = cache->last = b;
156     }
157   /* Add this token to the current token block.  */
158   b->tokens[b->num_tokens++] = *token;
159 }
160
161 /* The cp_lexer structure represents the C++ lexer.  It is responsible
162    for managing the token stream from the preprocessor and supplying
163    it to the parser.  */
164
165 typedef struct cp_lexer GTY (())
166 {
167   /* The memory allocated for the buffer.  Never NULL.  */
168   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
169   /* A pointer just past the end of the memory allocated for the buffer.  */
170   cp_token * GTY ((skip (""))) buffer_end;
171   /* The first valid token in the buffer, or NULL if none.  */
172   cp_token * GTY ((skip (""))) first_token;
173   /* The next available token.  If NEXT_TOKEN is NULL, then there are
174      no more available tokens.  */
175   cp_token * GTY ((skip (""))) next_token;
176   /* A pointer just past the last available token.  If FIRST_TOKEN is
177      NULL, however, there are no available tokens, and then this
178      location is simply the place in which the next token read will be
179      placed.  If LAST_TOKEN == FIRST_TOKEN, then the buffer is full.
180      When the LAST_TOKEN == BUFFER, then the last token is at the
181      highest memory address in the BUFFER.  */
182   cp_token * GTY ((skip (""))) last_token;
183
184   /* A stack indicating positions at which cp_lexer_save_tokens was
185      called.  The top entry is the most recent position at which we
186      began saving tokens.  The entries are differences in token
187      position between FIRST_TOKEN and the first saved token.
188
189      If the stack is non-empty, we are saving tokens.  When a token is
190      consumed, the NEXT_TOKEN pointer will move, but the FIRST_TOKEN
191      pointer will not.  The token stream will be preserved so that it
192      can be reexamined later.
193
194      If the stack is empty, then we are not saving tokens.  Whenever a
195      token is consumed, the FIRST_TOKEN pointer will be moved, and the
196      consumed token will be gone forever.  */
197   varray_type saved_tokens;
198
199   /* The STRING_CST tokens encountered while processing the current
200      string literal.  */
201   varray_type string_tokens;
202
203   /* True if we should obtain more tokens from the preprocessor; false
204      if we are processing a saved token cache.  */
205   bool main_lexer_p;
206
207   /* True if we should output debugging information.  */
208   bool debugging_p;
209
210   /* The next lexer in a linked list of lexers.  */
211   struct cp_lexer *next;
212 } cp_lexer;
213
214 /* Prototypes.  */
215
216 static cp_lexer *cp_lexer_new_main
217   (void);
218 static cp_lexer *cp_lexer_new_from_tokens
219   (struct cp_token_cache *);
220 static int cp_lexer_saving_tokens
221   (const cp_lexer *);
222 static cp_token *cp_lexer_next_token
223   (cp_lexer *, cp_token *);
224 static cp_token *cp_lexer_prev_token
225   (cp_lexer *, cp_token *);
226 static ptrdiff_t cp_lexer_token_difference
227   (cp_lexer *, cp_token *, cp_token *);
228 static cp_token *cp_lexer_read_token
229   (cp_lexer *);
230 static void cp_lexer_maybe_grow_buffer
231   (cp_lexer *);
232 static void cp_lexer_get_preprocessor_token
233   (cp_lexer *, cp_token *);
234 static cp_token *cp_lexer_peek_token
235   (cp_lexer *);
236 static cp_token *cp_lexer_peek_nth_token
237   (cp_lexer *, size_t);
238 static inline bool cp_lexer_next_token_is
239   (cp_lexer *, enum cpp_ttype);
240 static bool cp_lexer_next_token_is_not
241   (cp_lexer *, enum cpp_ttype);
242 static bool cp_lexer_next_token_is_keyword
243   (cp_lexer *, enum rid);
244 static cp_token *cp_lexer_consume_token
245   (cp_lexer *);
246 static void cp_lexer_purge_token
247   (cp_lexer *);
248 static void cp_lexer_purge_tokens_after
249   (cp_lexer *, cp_token *);
250 static void cp_lexer_save_tokens
251   (cp_lexer *);
252 static void cp_lexer_commit_tokens
253   (cp_lexer *);
254 static void cp_lexer_rollback_tokens
255   (cp_lexer *);
256 static inline void cp_lexer_set_source_position_from_token
257   (cp_lexer *, const cp_token *);
258 static void cp_lexer_print_token
259   (FILE *, cp_token *);
260 static inline bool cp_lexer_debugging_p
261   (cp_lexer *);
262 static void cp_lexer_start_debugging
263   (cp_lexer *) ATTRIBUTE_UNUSED;
264 static void cp_lexer_stop_debugging
265   (cp_lexer *) ATTRIBUTE_UNUSED;
266
267 /* Manifest constants.  */
268
269 #define CP_TOKEN_BUFFER_SIZE 5
270 #define CP_SAVED_TOKENS_SIZE 5
271
272 /* A token type for keywords, as opposed to ordinary identifiers.  */
273 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
274
275 /* A token type for template-ids.  If a template-id is processed while
276    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
277    the value of the CPP_TEMPLATE_ID is whatever was returned by
278    cp_parser_template_id.  */
279 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
280
281 /* A token type for nested-name-specifiers.  If a
282    nested-name-specifier is processed while parsing tentatively, it is
283    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
284    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
285    cp_parser_nested_name_specifier_opt.  */
286 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
287
288 /* A token type for tokens that are not tokens at all; these are used
289    to mark the end of a token block.  */
290 #define CPP_NONE (CPP_NESTED_NAME_SPECIFIER + 1)
291
292 /* Variables.  */
293
294 /* The stream to which debugging output should be written.  */
295 static FILE *cp_lexer_debug_stream;
296
297 /* Create a new main C++ lexer, the lexer that gets tokens from the
298    preprocessor.  */
299
300 static cp_lexer *
301 cp_lexer_new_main (void)
302 {
303   cp_lexer *lexer;
304   cp_token first_token;
305
306   /* It's possible that lexing the first token will load a PCH file,
307      which is a GC collection point.  So we have to grab the first
308      token before allocating any memory.  */
309   cp_lexer_get_preprocessor_token (NULL, &first_token);
310   c_common_no_more_pch ();
311
312   /* Allocate the memory.  */
313   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
314
315   /* Create the circular buffer.  */
316   lexer->buffer = ggc_calloc (CP_TOKEN_BUFFER_SIZE, sizeof (cp_token));
317   lexer->buffer_end = lexer->buffer + CP_TOKEN_BUFFER_SIZE;
318
319   /* There is one token in the buffer.  */
320   lexer->last_token = lexer->buffer + 1;
321   lexer->first_token = lexer->buffer;
322   lexer->next_token = lexer->buffer;
323   memcpy (lexer->buffer, &first_token, sizeof (cp_token));
324
325   /* This lexer obtains more tokens by calling c_lex.  */
326   lexer->main_lexer_p = true;
327
328   /* Create the SAVED_TOKENS stack.  */
329   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
330
331   /* Create the STRINGS array.  */
332   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
333
334   /* Assume we are not debugging.  */
335   lexer->debugging_p = false;
336
337   return lexer;
338 }
339
340 /* Create a new lexer whose token stream is primed with the TOKENS.
341    When these tokens are exhausted, no new tokens will be read.  */
342
343 static cp_lexer *
344 cp_lexer_new_from_tokens (cp_token_cache *tokens)
345 {
346   cp_lexer *lexer;
347   cp_token *token;
348   cp_token_block *block;
349   ptrdiff_t num_tokens;
350
351   /* Allocate the memory.  */
352   lexer = ggc_alloc_cleared (sizeof (cp_lexer));
353
354   /* Create a new buffer, appropriately sized.  */
355   num_tokens = 0;
356   for (block = tokens->first; block != NULL; block = block->next)
357     num_tokens += block->num_tokens;
358   lexer->buffer = ggc_alloc (num_tokens * sizeof (cp_token));
359   lexer->buffer_end = lexer->buffer + num_tokens;
360
361   /* Install the tokens.  */
362   token = lexer->buffer;
363   for (block = tokens->first; block != NULL; block = block->next)
364     {
365       memcpy (token, block->tokens, block->num_tokens * sizeof (cp_token));
366       token += block->num_tokens;
367     }
368
369   /* The FIRST_TOKEN is the beginning of the buffer.  */
370   lexer->first_token = lexer->buffer;
371   /* The next available token is also at the beginning of the buffer.  */
372   lexer->next_token = lexer->buffer;
373   /* The buffer is full.  */
374   lexer->last_token = lexer->first_token;
375
376   /* This lexer doesn't obtain more tokens.  */
377   lexer->main_lexer_p = false;
378
379   /* Create the SAVED_TOKENS stack.  */
380   VARRAY_INT_INIT (lexer->saved_tokens, CP_SAVED_TOKENS_SIZE, "saved_tokens");
381
382   /* Create the STRINGS array.  */
383   VARRAY_TREE_INIT (lexer->string_tokens, 32, "strings");
384
385   /* Assume we are not debugging.  */
386   lexer->debugging_p = false;
387
388   return lexer;
389 }
390
391 /* Returns nonzero if debugging information should be output.  */
392
393 static inline bool
394 cp_lexer_debugging_p (cp_lexer *lexer)
395 {
396   return lexer->debugging_p;
397 }
398
399 /* Set the current source position from the information stored in
400    TOKEN.  */
401
402 static inline void
403 cp_lexer_set_source_position_from_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
404                                          const cp_token *token)
405 {
406   /* Ideally, the source position information would not be a global
407      variable, but it is.  */
408
409   /* Update the line number.  */
410   if (token->type != CPP_EOF)
411     input_location = token->location;
412 }
413
414 /* TOKEN points into the circular token buffer.  Return a pointer to
415    the next token in the buffer.  */
416
417 static inline cp_token *
418 cp_lexer_next_token (cp_lexer* lexer, cp_token* token)
419 {
420   token++;
421   if (token == lexer->buffer_end)
422     token = lexer->buffer;
423   return token;
424 }
425
426 /* TOKEN points into the circular token buffer.  Return a pointer to
427    the previous token in the buffer.  */
428
429 static inline cp_token *
430 cp_lexer_prev_token (cp_lexer* lexer, cp_token* token)
431 {
432   if (token == lexer->buffer)
433     token = lexer->buffer_end;
434   return token - 1;
435 }
436
437 /* nonzero if we are presently saving tokens.  */
438
439 static int
440 cp_lexer_saving_tokens (const cp_lexer* lexer)
441 {
442   return VARRAY_ACTIVE_SIZE (lexer->saved_tokens) != 0;
443 }
444
445 /* Return a pointer to the token that is N tokens beyond TOKEN in the
446    buffer.  */
447
448 static cp_token *
449 cp_lexer_advance_token (cp_lexer *lexer, cp_token *token, ptrdiff_t n)
450 {
451   token += n;
452   if (token >= lexer->buffer_end)
453     token = lexer->buffer + (token - lexer->buffer_end);
454   return token;
455 }
456
457 /* Returns the number of times that START would have to be incremented
458    to reach FINISH.  If START and FINISH are the same, returns zero.  */
459
460 static ptrdiff_t
461 cp_lexer_token_difference (cp_lexer* lexer, cp_token* start, cp_token* finish)
462 {
463   if (finish >= start)
464     return finish - start;
465   else
466     return ((lexer->buffer_end - lexer->buffer)
467             - (start - finish));
468 }
469
470 /* Obtain another token from the C preprocessor and add it to the
471    token buffer.  Returns the newly read token.  */
472
473 static cp_token *
474 cp_lexer_read_token (cp_lexer* lexer)
475 {
476   cp_token *token;
477
478   /* Make sure there is room in the buffer.  */
479   cp_lexer_maybe_grow_buffer (lexer);
480
481   /* If there weren't any tokens, then this one will be the first.  */
482   if (!lexer->first_token)
483     lexer->first_token = lexer->last_token;
484   /* Similarly, if there were no available tokens, there is one now.  */
485   if (!lexer->next_token)
486     lexer->next_token = lexer->last_token;
487
488   /* Figure out where we're going to store the new token.  */
489   token = lexer->last_token;
490
491   /* Get a new token from the preprocessor.  */
492   cp_lexer_get_preprocessor_token (lexer, token);
493
494   /* Increment LAST_TOKEN.  */
495   lexer->last_token = cp_lexer_next_token (lexer, token);
496
497   /* Strings should have type `const char []'.  Right now, we will
498      have an ARRAY_TYPE that is constant rather than an array of
499      constant elements.
500      FIXME: Make fix_string_type get this right in the first place.  */
501   if ((token->type == CPP_STRING || token->type == CPP_WSTRING)
502       && flag_const_strings)
503     {
504       tree type;
505
506       /* Get the current type.  It will be an ARRAY_TYPE.  */
507       type = TREE_TYPE (token->value);
508       /* Use build_cplus_array_type to rebuild the array, thereby
509          getting the right type.  */
510       type = build_cplus_array_type (TREE_TYPE (type), TYPE_DOMAIN (type));
511       /* Reset the type of the token.  */
512       TREE_TYPE (token->value) = type;
513     }
514
515   return token;
516 }
517
518 /* If the circular buffer is full, make it bigger.  */
519
520 static void
521 cp_lexer_maybe_grow_buffer (cp_lexer* lexer)
522 {
523   /* If the buffer is full, enlarge it.  */
524   if (lexer->last_token == lexer->first_token)
525     {
526       cp_token *new_buffer;
527       cp_token *old_buffer;
528       cp_token *new_first_token;
529       ptrdiff_t buffer_length;
530       size_t num_tokens_to_copy;
531
532       /* Remember the current buffer pointer.  It will become invalid,
533          but we will need to do pointer arithmetic involving this
534          value.  */
535       old_buffer = lexer->buffer;
536       /* Compute the current buffer size.  */
537       buffer_length = lexer->buffer_end - lexer->buffer;
538       /* Allocate a buffer twice as big.  */
539       new_buffer = ggc_realloc (lexer->buffer,
540                                 2 * buffer_length * sizeof (cp_token));
541
542       /* Because the buffer is circular, logically consecutive tokens
543          are not necessarily placed consecutively in memory.
544          Therefore, we must keep move the tokens that were before
545          FIRST_TOKEN to the second half of the newly allocated
546          buffer.  */
547       num_tokens_to_copy = (lexer->first_token - old_buffer);
548       memcpy (new_buffer + buffer_length,
549               new_buffer,
550               num_tokens_to_copy * sizeof (cp_token));
551       /* Clear the rest of the buffer.  We never look at this storage,
552          but the garbage collector may.  */
553       memset (new_buffer + buffer_length + num_tokens_to_copy, 0,
554               (buffer_length - num_tokens_to_copy) * sizeof (cp_token));
555
556       /* Now recompute all of the buffer pointers.  */
557       new_first_token
558         = new_buffer + (lexer->first_token - old_buffer);
559       if (lexer->next_token != NULL)
560         {
561           ptrdiff_t next_token_delta;
562
563           if (lexer->next_token > lexer->first_token)
564             next_token_delta = lexer->next_token - lexer->first_token;
565           else
566             next_token_delta =
567               buffer_length - (lexer->first_token - lexer->next_token);
568           lexer->next_token = new_first_token + next_token_delta;
569         }
570       lexer->last_token = new_first_token + buffer_length;
571       lexer->buffer = new_buffer;
572       lexer->buffer_end = new_buffer + buffer_length * 2;
573       lexer->first_token = new_first_token;
574     }
575 }
576
577 /* Store the next token from the preprocessor in *TOKEN.  */
578
579 static void
580 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
581                                  cp_token *token)
582 {
583   bool done;
584
585   /* If this not the main lexer, return a terminating CPP_EOF token.  */
586   if (lexer != NULL && !lexer->main_lexer_p)
587     {
588       token->type = CPP_EOF;
589       token->location.line = 0;
590       token->location.file = NULL;
591       token->value = NULL_TREE;
592       token->keyword = RID_MAX;
593
594       return;
595     }
596
597   done = false;
598   /* Keep going until we get a token we like.  */
599   while (!done)
600     {
601       /* Get a new token from the preprocessor.  */
602       token->type = c_lex_with_flags (&token->value, &token->flags);
603       /* Issue messages about tokens we cannot process.  */
604       switch (token->type)
605         {
606         case CPP_ATSIGN:
607         case CPP_HASH:
608         case CPP_PASTE:
609           error ("invalid token");
610           break;
611
612         default:
613           /* This is a good token, so we exit the loop.  */
614           done = true;
615           break;
616         }
617     }
618   /* Now we've got our token.  */
619   token->location = input_location;
620
621   /* Check to see if this token is a keyword.  */
622   if (token->type == CPP_NAME
623       && C_IS_RESERVED_WORD (token->value))
624     {
625       /* Mark this token as a keyword.  */
626       token->type = CPP_KEYWORD;
627       /* Record which keyword.  */
628       token->keyword = C_RID_CODE (token->value);
629       /* Update the value.  Some keywords are mapped to particular
630          entities, rather than simply having the value of the
631          corresponding IDENTIFIER_NODE.  For example, `__const' is
632          mapped to `const'.  */
633       token->value = ridpointers[token->keyword];
634     }
635   else
636     token->keyword = RID_MAX;
637 }
638
639 /* Return a pointer to the next token in the token stream, but do not
640    consume it.  */
641
642 static cp_token *
643 cp_lexer_peek_token (cp_lexer* lexer)
644 {
645   cp_token *token;
646
647   /* If there are no tokens, read one now.  */
648   if (!lexer->next_token)
649     cp_lexer_read_token (lexer);
650
651   /* Provide debugging output.  */
652   if (cp_lexer_debugging_p (lexer))
653     {
654       fprintf (cp_lexer_debug_stream, "cp_lexer: peeking at token: ");
655       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
656       fprintf (cp_lexer_debug_stream, "\n");
657     }
658
659   token = lexer->next_token;
660   cp_lexer_set_source_position_from_token (lexer, token);
661   return token;
662 }
663
664 /* Return true if the next token has the indicated TYPE.  */
665
666 static bool
667 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
668 {
669   cp_token *token;
670
671   /* Peek at the next token.  */
672   token = cp_lexer_peek_token (lexer);
673   /* Check to see if it has the indicated TYPE.  */
674   return token->type == type;
675 }
676
677 /* Return true if the next token does not have the indicated TYPE.  */
678
679 static bool
680 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
681 {
682   return !cp_lexer_next_token_is (lexer, type);
683 }
684
685 /* Return true if the next token is the indicated KEYWORD.  */
686
687 static bool
688 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
689 {
690   cp_token *token;
691
692   /* Peek at the next token.  */
693   token = cp_lexer_peek_token (lexer);
694   /* Check to see if it is the indicated keyword.  */
695   return token->keyword == keyword;
696 }
697
698 /* Return a pointer to the Nth token in the token stream.  If N is 1,
699    then this is precisely equivalent to cp_lexer_peek_token.  */
700
701 static cp_token *
702 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
703 {
704   cp_token *token;
705
706   /* N is 1-based, not zero-based.  */
707   my_friendly_assert (n > 0, 20000224);
708
709   /* Skip ahead from NEXT_TOKEN, reading more tokens as necessary.  */
710   token = lexer->next_token;
711   /* If there are no tokens in the buffer, get one now.  */
712   if (!token)
713     {
714       cp_lexer_read_token (lexer);
715       token = lexer->next_token;
716     }
717
718   /* Now, read tokens until we have enough.  */
719   while (--n > 0)
720     {
721       /* Advance to the next token.  */
722       token = cp_lexer_next_token (lexer, token);
723       /* If that's all the tokens we have, read a new one.  */
724       if (token == lexer->last_token)
725         token = cp_lexer_read_token (lexer);
726     }
727
728   return token;
729 }
730
731 /* Consume the next token.  The pointer returned is valid only until
732    another token is read.  Callers should preserve copy the token
733    explicitly if they will need its value for a longer period of
734    time.  */
735
736 static cp_token *
737 cp_lexer_consume_token (cp_lexer* lexer)
738 {
739   cp_token *token;
740
741   /* If there are no tokens, read one now.  */
742   if (!lexer->next_token)
743     cp_lexer_read_token (lexer);
744
745   /* Remember the token we'll be returning.  */
746   token = lexer->next_token;
747
748   /* Increment NEXT_TOKEN.  */
749   lexer->next_token = cp_lexer_next_token (lexer,
750                                            lexer->next_token);
751   /* Check to see if we're all out of tokens.  */
752   if (lexer->next_token == lexer->last_token)
753     lexer->next_token = NULL;
754
755   /* If we're not saving tokens, then move FIRST_TOKEN too.  */
756   if (!cp_lexer_saving_tokens (lexer))
757     {
758       /* If there are no tokens available, set FIRST_TOKEN to NULL.  */
759       if (!lexer->next_token)
760         lexer->first_token = NULL;
761       else
762         lexer->first_token = lexer->next_token;
763     }
764
765   /* Provide debugging output.  */
766   if (cp_lexer_debugging_p (lexer))
767     {
768       fprintf (cp_lexer_debug_stream, "cp_lexer: consuming token: ");
769       cp_lexer_print_token (cp_lexer_debug_stream, token);
770       fprintf (cp_lexer_debug_stream, "\n");
771     }
772
773   return token;
774 }
775
776 /* Permanently remove the next token from the token stream.  There
777    must be a valid next token already; this token never reads
778    additional tokens from the preprocessor.  */
779
780 static void
781 cp_lexer_purge_token (cp_lexer *lexer)
782 {
783   cp_token *token;
784   cp_token *next_token;
785
786   token = lexer->next_token;
787   while (true)
788     {
789       next_token = cp_lexer_next_token (lexer, token);
790       if (next_token == lexer->last_token)
791         break;
792       *token = *next_token;
793       token = next_token;
794     }
795
796   lexer->last_token = token;
797   /* The token purged may have been the only token remaining; if so,
798      clear NEXT_TOKEN.  */
799   if (lexer->next_token == token)
800     lexer->next_token = NULL;
801 }
802
803 /* Permanently remove all tokens after TOKEN, up to, but not
804    including, the token that will be returned next by
805    cp_lexer_peek_token.  */
806
807 static void
808 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *token)
809 {
810   cp_token *peek;
811   cp_token *t1;
812   cp_token *t2;
813
814   if (lexer->next_token)
815     {
816       /* Copy the tokens that have not yet been read to the location
817          immediately following TOKEN.  */
818       t1 = cp_lexer_next_token (lexer, token);
819       t2 = peek = cp_lexer_peek_token (lexer);
820       /* Move tokens into the vacant area between TOKEN and PEEK.  */
821       while (t2 != lexer->last_token)
822         {
823           *t1 = *t2;
824           t1 = cp_lexer_next_token (lexer, t1);
825           t2 = cp_lexer_next_token (lexer, t2);
826         }
827       /* Now, the next available token is right after TOKEN.  */
828       lexer->next_token = cp_lexer_next_token (lexer, token);
829       /* And the last token is wherever we ended up.  */
830       lexer->last_token = t1;
831     }
832   else
833     {
834       /* There are no tokens in the buffer, so there is nothing to
835          copy.  The last token in the buffer is TOKEN itself.  */
836       lexer->last_token = cp_lexer_next_token (lexer, token);
837     }
838 }
839
840 /* Begin saving tokens.  All tokens consumed after this point will be
841    preserved.  */
842
843 static void
844 cp_lexer_save_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
849
850   /* Make sure that LEXER->NEXT_TOKEN is non-NULL so that we can
851      restore the tokens if required.  */
852   if (!lexer->next_token)
853     cp_lexer_read_token (lexer);
854
855   VARRAY_PUSH_INT (lexer->saved_tokens,
856                    cp_lexer_token_difference (lexer,
857                                               lexer->first_token,
858                                               lexer->next_token));
859 }
860
861 /* Commit to the portion of the token stream most recently saved.  */
862
863 static void
864 cp_lexer_commit_tokens (cp_lexer* lexer)
865 {
866   /* Provide debugging output.  */
867   if (cp_lexer_debugging_p (lexer))
868     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
869
870   VARRAY_POP (lexer->saved_tokens);
871 }
872
873 /* Return all tokens saved since the last call to cp_lexer_save_tokens
874    to the token stream.  Stop saving tokens.  */
875
876 static void
877 cp_lexer_rollback_tokens (cp_lexer* lexer)
878 {
879   size_t delta;
880
881   /* Provide debugging output.  */
882   if (cp_lexer_debugging_p (lexer))
883     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
884
885   /* Find the token that was the NEXT_TOKEN when we started saving
886      tokens.  */
887   delta = VARRAY_TOP_INT(lexer->saved_tokens);
888   /* Make it the next token again now.  */
889   lexer->next_token = cp_lexer_advance_token (lexer,
890                                               lexer->first_token,
891                                               delta);
892   /* It might be the case that there were no tokens when we started
893      saving tokens, but that there are some tokens now.  */
894   if (!lexer->next_token && lexer->first_token)
895     lexer->next_token = lexer->first_token;
896
897   /* Stop saving tokens.  */
898   VARRAY_POP (lexer->saved_tokens);
899 }
900
901 /* Print a representation of the TOKEN on the STREAM.  */
902
903 static void
904 cp_lexer_print_token (FILE * stream, cp_token* token)
905 {
906   const char *token_type = NULL;
907
908   /* Figure out what kind of token this is.  */
909   switch (token->type)
910     {
911     case CPP_EQ:
912       token_type = "EQ";
913       break;
914
915     case CPP_COMMA:
916       token_type = "COMMA";
917       break;
918
919     case CPP_OPEN_PAREN:
920       token_type = "OPEN_PAREN";
921       break;
922
923     case CPP_CLOSE_PAREN:
924       token_type = "CLOSE_PAREN";
925       break;
926
927     case CPP_OPEN_BRACE:
928       token_type = "OPEN_BRACE";
929       break;
930
931     case CPP_CLOSE_BRACE:
932       token_type = "CLOSE_BRACE";
933       break;
934
935     case CPP_SEMICOLON:
936       token_type = "SEMICOLON";
937       break;
938
939     case CPP_NAME:
940       token_type = "NAME";
941       break;
942
943     case CPP_EOF:
944       token_type = "EOF";
945       break;
946
947     case CPP_KEYWORD:
948       token_type = "keyword";
949       break;
950
951       /* This is not a token that we know how to handle yet.  */
952     default:
953       break;
954     }
955
956   /* If we have a name for the token, print it out.  Otherwise, we
957      simply give the numeric code.  */
958   if (token_type)
959     fprintf (stream, "%s", token_type);
960   else
961     fprintf (stream, "%d", token->type);
962   /* And, for an identifier, print the identifier name.  */
963   if (token->type == CPP_NAME
964       /* Some keywords have a value that is not an IDENTIFIER_NODE.
965          For example, `struct' is mapped to an INTEGER_CST.  */
966       || (token->type == CPP_KEYWORD
967           && TREE_CODE (token->value) == IDENTIFIER_NODE))
968     fprintf (stream, " %s", IDENTIFIER_POINTER (token->value));
969 }
970
971 /* Start emitting debugging information.  */
972
973 static void
974 cp_lexer_start_debugging (cp_lexer* lexer)
975 {
976   ++lexer->debugging_p;
977 }
978
979 /* Stop emitting debugging information.  */
980
981 static void
982 cp_lexer_stop_debugging (cp_lexer* lexer)
983 {
984   --lexer->debugging_p;
985 }
986
987 \f
988 /* The parser.  */
989
990 /* Overview
991    --------
992
993    A cp_parser parses the token stream as specified by the C++
994    grammar.  Its job is purely parsing, not semantic analysis.  For
995    example, the parser breaks the token stream into declarators,
996    expressions, statements, and other similar syntactic constructs.
997    It does not check that the types of the expressions on either side
998    of an assignment-statement are compatible, or that a function is
999    not declared with a parameter of type `void'.
1000
1001    The parser invokes routines elsewhere in the compiler to perform
1002    semantic analysis and to build up the abstract syntax tree for the
1003    code processed.
1004
1005    The parser (and the template instantiation code, which is, in a
1006    way, a close relative of parsing) are the only parts of the
1007    compiler that should be calling push_scope and pop_scope, or
1008    related functions.  The parser (and template instantiation code)
1009    keeps track of what scope is presently active; everything else
1010    should simply honor that.  (The code that generates static
1011    initializers may also need to set the scope, in order to check
1012    access control correctly when emitting the initializers.)
1013
1014    Methodology
1015    -----------
1016
1017    The parser is of the standard recursive-descent variety.  Upcoming
1018    tokens in the token stream are examined in order to determine which
1019    production to use when parsing a non-terminal.  Some C++ constructs
1020    require arbitrary look ahead to disambiguate.  For example, it is
1021    impossible, in the general case, to tell whether a statement is an
1022    expression or declaration without scanning the entire statement.
1023    Therefore, the parser is capable of "parsing tentatively."  When the
1024    parser is not sure what construct comes next, it enters this mode.
1025    Then, while we attempt to parse the construct, the parser queues up
1026    error messages, rather than issuing them immediately, and saves the
1027    tokens it consumes.  If the construct is parsed successfully, the
1028    parser "commits", i.e., it issues any queued error messages and
1029    the tokens that were being preserved are permanently discarded.
1030    If, however, the construct is not parsed successfully, the parser
1031    rolls back its state completely so that it can resume parsing using
1032    a different alternative.
1033
1034    Future Improvements
1035    -------------------
1036
1037    The performance of the parser could probably be improved
1038    substantially.  Some possible improvements include:
1039
1040      - The expression parser recurses through the various levels of
1041        precedence as specified in the grammar, rather than using an
1042        operator-precedence technique.  Therefore, parsing a simple
1043        identifier requires multiple recursive calls.
1044
1045      - We could often eliminate the need to parse tentatively by
1046        looking ahead a little bit.  In some places, this approach
1047        might not entirely eliminate the need to parse tentatively, but
1048        it might still speed up the average case.  */
1049
1050 /* Flags that are passed to some parsing functions.  These values can
1051    be bitwise-ored together.  */
1052
1053 typedef enum cp_parser_flags
1054 {
1055   /* No flags.  */
1056   CP_PARSER_FLAGS_NONE = 0x0,
1057   /* The construct is optional.  If it is not present, then no error
1058      should be issued.  */
1059   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1060   /* When parsing a type-specifier, do not allow user-defined types.  */
1061   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1062 } cp_parser_flags;
1063
1064 /* The different kinds of declarators we want to parse.  */
1065
1066 typedef enum cp_parser_declarator_kind
1067 {
1068   /* We want an abstract declarator.  */
1069   CP_PARSER_DECLARATOR_ABSTRACT,
1070   /* We want a named declarator.  */
1071   CP_PARSER_DECLARATOR_NAMED,
1072   /* We don't mind, but the name must be an unqualified-id.  */
1073   CP_PARSER_DECLARATOR_EITHER
1074 } cp_parser_declarator_kind;
1075
1076 /* A mapping from a token type to a corresponding tree node type.  */
1077
1078 typedef struct cp_parser_token_tree_map_node
1079 {
1080   /* The token type.  */
1081   ENUM_BITFIELD (cpp_ttype) token_type : 8;
1082   /* The corresponding tree code.  */
1083   ENUM_BITFIELD (tree_code) tree_type : 8;
1084 } cp_parser_token_tree_map_node;
1085
1086 /* A complete map consists of several ordinary entries, followed by a
1087    terminator.  The terminating entry has a token_type of CPP_EOF.  */
1088
1089 typedef cp_parser_token_tree_map_node cp_parser_token_tree_map[];
1090
1091 /* The status of a tentative parse.  */
1092
1093 typedef enum cp_parser_status_kind
1094 {
1095   /* No errors have occurred.  */
1096   CP_PARSER_STATUS_KIND_NO_ERROR,
1097   /* An error has occurred.  */
1098   CP_PARSER_STATUS_KIND_ERROR,
1099   /* We are committed to this tentative parse, whether or not an error
1100      has occurred.  */
1101   CP_PARSER_STATUS_KIND_COMMITTED
1102 } cp_parser_status_kind;
1103
1104 /* Context that is saved and restored when parsing tentatively.  */
1105
1106 typedef struct cp_parser_context GTY (())
1107 {
1108   /* If this is a tentative parsing context, the status of the
1109      tentative parse.  */
1110   enum cp_parser_status_kind status;
1111   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1112      that are looked up in this context must be looked up both in the
1113      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1114      the context of the containing expression.  */
1115   tree object_type;
1116   /* The next parsing context in the stack.  */
1117   struct cp_parser_context *next;
1118 } cp_parser_context;
1119
1120 /* Prototypes.  */
1121
1122 /* Constructors and destructors.  */
1123
1124 static cp_parser_context *cp_parser_context_new
1125   (cp_parser_context *);
1126
1127 /* Class variables.  */
1128
1129 static GTY((deletable (""))) cp_parser_context* cp_parser_context_free_list;
1130
1131 /* Constructors and destructors.  */
1132
1133 /* Construct a new context.  The context below this one on the stack
1134    is given by NEXT.  */
1135
1136 static cp_parser_context *
1137 cp_parser_context_new (cp_parser_context* next)
1138 {
1139   cp_parser_context *context;
1140
1141   /* Allocate the storage.  */
1142   if (cp_parser_context_free_list != NULL)
1143     {
1144       /* Pull the first entry from the free list.  */
1145       context = cp_parser_context_free_list;
1146       cp_parser_context_free_list = context->next;
1147       memset (context, 0, sizeof (*context));
1148     }
1149   else
1150     context = ggc_alloc_cleared (sizeof (cp_parser_context));
1151   /* No errors have occurred yet in this context.  */
1152   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1153   /* If this is not the bottomost context, copy information that we
1154      need from the previous context.  */
1155   if (next)
1156     {
1157       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1158          expression, then we are parsing one in this context, too.  */
1159       context->object_type = next->object_type;
1160       /* Thread the stack.  */
1161       context->next = next;
1162     }
1163
1164   return context;
1165 }
1166
1167 /* The cp_parser structure represents the C++ parser.  */
1168
1169 typedef struct cp_parser GTY(())
1170 {
1171   /* The lexer from which we are obtaining tokens.  */
1172   cp_lexer *lexer;
1173
1174   /* The scope in which names should be looked up.  If NULL_TREE, then
1175      we look up names in the scope that is currently open in the
1176      source program.  If non-NULL, this is either a TYPE or
1177      NAMESPACE_DECL for the scope in which we should look.
1178
1179      This value is not cleared automatically after a name is looked
1180      up, so we must be careful to clear it before starting a new look
1181      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1182      will look up `Z' in the scope of `X', rather than the current
1183      scope.)  Unfortunately, it is difficult to tell when name lookup
1184      is complete, because we sometimes peek at a token, look it up,
1185      and then decide not to consume it.  */
1186   tree scope;
1187
1188   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1189      last lookup took place.  OBJECT_SCOPE is used if an expression
1190      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1191      respectively.  QUALIFYING_SCOPE is used for an expression of the
1192      form "X::Y"; it refers to X.  */
1193   tree object_scope;
1194   tree qualifying_scope;
1195
1196   /* A stack of parsing contexts.  All but the bottom entry on the
1197      stack will be tentative contexts.
1198
1199      We parse tentatively in order to determine which construct is in
1200      use in some situations.  For example, in order to determine
1201      whether a statement is an expression-statement or a
1202      declaration-statement we parse it tentatively as a
1203      declaration-statement.  If that fails, we then reparse the same
1204      token stream as an expression-statement.  */
1205   cp_parser_context *context;
1206
1207   /* True if we are parsing GNU C++.  If this flag is not set, then
1208      GNU extensions are not recognized.  */
1209   bool allow_gnu_extensions_p;
1210
1211   /* TRUE if the `>' token should be interpreted as the greater-than
1212      operator.  FALSE if it is the end of a template-id or
1213      template-parameter-list.  */
1214   bool greater_than_is_operator_p;
1215
1216   /* TRUE if default arguments are allowed within a parameter list
1217      that starts at this point. FALSE if only a gnu extension makes
1218      them permissible.  */
1219   bool default_arg_ok_p;
1220
1221   /* TRUE if we are parsing an integral constant-expression.  See
1222      [expr.const] for a precise definition.  */
1223   bool integral_constant_expression_p;
1224
1225   /* TRUE if we are parsing an integral constant-expression -- but a
1226      non-constant expression should be permitted as well.  This flag
1227      is used when parsing an array bound so that GNU variable-length
1228      arrays are tolerated.  */
1229   bool allow_non_integral_constant_expression_p;
1230
1231   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1232      been seen that makes the expression non-constant.  */
1233   bool non_integral_constant_expression_p;
1234
1235   /* TRUE if we are parsing the argument to "__offsetof__".  */
1236   bool in_offsetof_p;
1237
1238   /* TRUE if local variable names and `this' are forbidden in the
1239      current context.  */
1240   bool local_variables_forbidden_p;
1241
1242   /* TRUE if the declaration we are parsing is part of a
1243      linkage-specification of the form `extern string-literal
1244      declaration'.  */
1245   bool in_unbraced_linkage_specification_p;
1246
1247   /* TRUE if we are presently parsing a declarator, after the
1248      direct-declarator.  */
1249   bool in_declarator_p;
1250
1251   /* TRUE if we are presently parsing a template-argument-list.  */
1252   bool in_template_argument_list_p;
1253
1254   /* TRUE if we are presently parsing the body of an
1255      iteration-statement.  */
1256   bool in_iteration_statement_p;
1257
1258   /* TRUE if we are presently parsing the body of a switch
1259      statement.  */
1260   bool in_switch_statement_p;
1261
1262   /* TRUE if we are parsing a type-id in an expression context.  In
1263      such a situation, both "type (expr)" and "type (type)" are valid
1264      alternatives.  */
1265   bool in_type_id_in_expr_p;
1266
1267   /* If non-NULL, then we are parsing a construct where new type
1268      definitions are not permitted.  The string stored here will be
1269      issued as an error message if a type is defined.  */
1270   const char *type_definition_forbidden_message;
1271
1272   /* A list of lists. The outer list is a stack, used for member
1273      functions of local classes. At each level there are two sub-list,
1274      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1275      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1276      TREE_VALUE's. The functions are chained in reverse declaration
1277      order.
1278
1279      The TREE_PURPOSE sublist contains those functions with default
1280      arguments that need post processing, and the TREE_VALUE sublist
1281      contains those functions with definitions that need post
1282      processing.
1283
1284      These lists can only be processed once the outermost class being
1285      defined is complete.  */
1286   tree unparsed_functions_queues;
1287
1288   /* The number of classes whose definitions are currently in
1289      progress.  */
1290   unsigned num_classes_being_defined;
1291
1292   /* The number of template parameter lists that apply directly to the
1293      current declaration.  */
1294   unsigned num_template_parameter_lists;
1295 } cp_parser;
1296
1297 /* The type of a function that parses some kind of expression.  */
1298 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1299
1300 /* Prototypes.  */
1301
1302 /* Constructors and destructors.  */
1303
1304 static cp_parser *cp_parser_new
1305   (void);
1306
1307 /* Routines to parse various constructs.
1308
1309    Those that return `tree' will return the error_mark_node (rather
1310    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1311    Sometimes, they will return an ordinary node if error-recovery was
1312    attempted, even though a parse error occurred.  So, to check
1313    whether or not a parse error occurred, you should always use
1314    cp_parser_error_occurred.  If the construct is optional (indicated
1315    either by an `_opt' in the name of the function that does the
1316    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1317    the construct is not present.  */
1318
1319 /* Lexical conventions [gram.lex]  */
1320
1321 static tree cp_parser_identifier
1322   (cp_parser *);
1323
1324 /* Basic concepts [gram.basic]  */
1325
1326 static bool cp_parser_translation_unit
1327   (cp_parser *);
1328
1329 /* Expressions [gram.expr]  */
1330
1331 static tree cp_parser_primary_expression
1332   (cp_parser *, cp_id_kind *, tree *);
1333 static tree cp_parser_id_expression
1334   (cp_parser *, bool, bool, bool *, bool);
1335 static tree cp_parser_unqualified_id
1336   (cp_parser *, bool, bool, bool);
1337 static tree cp_parser_nested_name_specifier_opt
1338   (cp_parser *, bool, bool, bool, bool);
1339 static tree cp_parser_nested_name_specifier
1340   (cp_parser *, bool, bool, bool, bool);
1341 static tree cp_parser_class_or_namespace_name
1342   (cp_parser *, bool, bool, bool, bool, bool);
1343 static tree cp_parser_postfix_expression
1344   (cp_parser *, bool);
1345 static tree cp_parser_parenthesized_expression_list
1346   (cp_parser *, bool, bool *);
1347 static void cp_parser_pseudo_destructor_name
1348   (cp_parser *, tree *, tree *);
1349 static tree cp_parser_unary_expression
1350   (cp_parser *, bool);
1351 static enum tree_code cp_parser_unary_operator
1352   (cp_token *);
1353 static tree cp_parser_new_expression
1354   (cp_parser *);
1355 static tree cp_parser_new_placement
1356   (cp_parser *);
1357 static tree cp_parser_new_type_id
1358   (cp_parser *);
1359 static tree cp_parser_new_declarator_opt
1360   (cp_parser *);
1361 static tree cp_parser_direct_new_declarator
1362   (cp_parser *);
1363 static tree cp_parser_new_initializer
1364   (cp_parser *);
1365 static tree cp_parser_delete_expression
1366   (cp_parser *);
1367 static tree cp_parser_cast_expression
1368   (cp_parser *, bool);
1369 static tree cp_parser_pm_expression
1370   (cp_parser *);
1371 static tree cp_parser_multiplicative_expression
1372   (cp_parser *);
1373 static tree cp_parser_additive_expression
1374   (cp_parser *);
1375 static tree cp_parser_shift_expression
1376   (cp_parser *);
1377 static tree cp_parser_relational_expression
1378   (cp_parser *);
1379 static tree cp_parser_equality_expression
1380   (cp_parser *);
1381 static tree cp_parser_and_expression
1382   (cp_parser *);
1383 static tree cp_parser_exclusive_or_expression
1384   (cp_parser *);
1385 static tree cp_parser_inclusive_or_expression
1386   (cp_parser *);
1387 static tree cp_parser_logical_and_expression
1388   (cp_parser *);
1389 static tree cp_parser_logical_or_expression
1390   (cp_parser *);
1391 static tree cp_parser_question_colon_clause
1392   (cp_parser *, tree);
1393 static tree cp_parser_assignment_expression
1394   (cp_parser *);
1395 static enum tree_code cp_parser_assignment_operator_opt
1396   (cp_parser *);
1397 static tree cp_parser_expression
1398   (cp_parser *);
1399 static tree cp_parser_constant_expression
1400   (cp_parser *, bool, bool *);
1401
1402 /* Statements [gram.stmt.stmt]  */
1403
1404 static void cp_parser_statement
1405   (cp_parser *, bool);
1406 static tree cp_parser_labeled_statement
1407   (cp_parser *, bool);
1408 static tree cp_parser_expression_statement
1409   (cp_parser *, bool);
1410 static tree cp_parser_compound_statement
1411   (cp_parser *, bool);
1412 static void cp_parser_statement_seq_opt
1413   (cp_parser *, bool);
1414 static tree cp_parser_selection_statement
1415   (cp_parser *);
1416 static tree cp_parser_condition
1417   (cp_parser *);
1418 static tree cp_parser_iteration_statement
1419   (cp_parser *);
1420 static void cp_parser_for_init_statement
1421   (cp_parser *);
1422 static tree cp_parser_jump_statement
1423   (cp_parser *);
1424 static void cp_parser_declaration_statement
1425   (cp_parser *);
1426
1427 static tree cp_parser_implicitly_scoped_statement
1428   (cp_parser *);
1429 static void cp_parser_already_scoped_statement
1430   (cp_parser *);
1431
1432 /* Declarations [gram.dcl.dcl] */
1433
1434 static void cp_parser_declaration_seq_opt
1435   (cp_parser *);
1436 static void cp_parser_declaration
1437   (cp_parser *);
1438 static void cp_parser_block_declaration
1439   (cp_parser *, bool);
1440 static void cp_parser_simple_declaration
1441   (cp_parser *, bool);
1442 static tree cp_parser_decl_specifier_seq
1443   (cp_parser *, cp_parser_flags, tree *, int *);
1444 static tree cp_parser_storage_class_specifier_opt
1445   (cp_parser *);
1446 static tree cp_parser_function_specifier_opt
1447   (cp_parser *);
1448 static tree cp_parser_type_specifier
1449   (cp_parser *, cp_parser_flags, bool, bool, int *, bool *);
1450 static tree cp_parser_simple_type_specifier
1451   (cp_parser *, cp_parser_flags, bool);
1452 static tree cp_parser_type_name
1453   (cp_parser *);
1454 static tree cp_parser_elaborated_type_specifier
1455   (cp_parser *, bool, bool);
1456 static tree cp_parser_enum_specifier
1457   (cp_parser *);
1458 static void cp_parser_enumerator_list
1459   (cp_parser *, tree);
1460 static void cp_parser_enumerator_definition
1461   (cp_parser *, tree);
1462 static tree cp_parser_namespace_name
1463   (cp_parser *);
1464 static void cp_parser_namespace_definition
1465   (cp_parser *);
1466 static void cp_parser_namespace_body
1467   (cp_parser *);
1468 static tree cp_parser_qualified_namespace_specifier
1469   (cp_parser *);
1470 static void cp_parser_namespace_alias_definition
1471   (cp_parser *);
1472 static void cp_parser_using_declaration
1473   (cp_parser *);
1474 static void cp_parser_using_directive
1475   (cp_parser *);
1476 static void cp_parser_asm_definition
1477   (cp_parser *);
1478 static void cp_parser_linkage_specification
1479   (cp_parser *);
1480
1481 /* Declarators [gram.dcl.decl] */
1482
1483 static tree cp_parser_init_declarator
1484   (cp_parser *, tree, tree, bool, bool, int, bool *);
1485 static tree cp_parser_declarator
1486   (cp_parser *, cp_parser_declarator_kind, int *, bool *);
1487 static tree cp_parser_direct_declarator
1488   (cp_parser *, cp_parser_declarator_kind, int *);
1489 static enum tree_code cp_parser_ptr_operator
1490   (cp_parser *, tree *, tree *);
1491 static tree cp_parser_cv_qualifier_seq_opt
1492   (cp_parser *);
1493 static tree cp_parser_cv_qualifier_opt
1494   (cp_parser *);
1495 static tree cp_parser_declarator_id
1496   (cp_parser *);
1497 static tree cp_parser_type_id
1498   (cp_parser *);
1499 static tree cp_parser_type_specifier_seq
1500   (cp_parser *);
1501 static tree cp_parser_parameter_declaration_clause
1502   (cp_parser *);
1503 static tree cp_parser_parameter_declaration_list
1504   (cp_parser *);
1505 static tree cp_parser_parameter_declaration
1506   (cp_parser *, bool, bool *);
1507 static void cp_parser_function_body
1508   (cp_parser *);
1509 static tree cp_parser_initializer
1510   (cp_parser *, bool *, bool *);
1511 static tree cp_parser_initializer_clause
1512   (cp_parser *, bool *);
1513 static tree cp_parser_initializer_list
1514   (cp_parser *, bool *);
1515
1516 static bool cp_parser_ctor_initializer_opt_and_function_body
1517   (cp_parser *);
1518
1519 /* Classes [gram.class] */
1520
1521 static tree cp_parser_class_name
1522   (cp_parser *, bool, bool, bool, bool, bool, bool);
1523 static tree cp_parser_class_specifier
1524   (cp_parser *);
1525 static tree cp_parser_class_head
1526   (cp_parser *, bool *);
1527 static enum tag_types cp_parser_class_key
1528   (cp_parser *);
1529 static void cp_parser_member_specification_opt
1530   (cp_parser *);
1531 static void cp_parser_member_declaration
1532   (cp_parser *);
1533 static tree cp_parser_pure_specifier
1534   (cp_parser *);
1535 static tree cp_parser_constant_initializer
1536   (cp_parser *);
1537
1538 /* Derived classes [gram.class.derived] */
1539
1540 static tree cp_parser_base_clause
1541   (cp_parser *);
1542 static tree cp_parser_base_specifier
1543   (cp_parser *);
1544
1545 /* Special member functions [gram.special] */
1546
1547 static tree cp_parser_conversion_function_id
1548   (cp_parser *);
1549 static tree cp_parser_conversion_type_id
1550   (cp_parser *);
1551 static tree cp_parser_conversion_declarator_opt
1552   (cp_parser *);
1553 static bool cp_parser_ctor_initializer_opt
1554   (cp_parser *);
1555 static void cp_parser_mem_initializer_list
1556   (cp_parser *);
1557 static tree cp_parser_mem_initializer
1558   (cp_parser *);
1559 static tree cp_parser_mem_initializer_id
1560   (cp_parser *);
1561
1562 /* Overloading [gram.over] */
1563
1564 static tree cp_parser_operator_function_id
1565   (cp_parser *);
1566 static tree cp_parser_operator
1567   (cp_parser *);
1568
1569 /* Templates [gram.temp] */
1570
1571 static void cp_parser_template_declaration
1572   (cp_parser *, bool);
1573 static tree cp_parser_template_parameter_list
1574   (cp_parser *);
1575 static tree cp_parser_template_parameter
1576   (cp_parser *);
1577 static tree cp_parser_type_parameter
1578   (cp_parser *);
1579 static tree cp_parser_template_id
1580   (cp_parser *, bool, bool, bool);
1581 static tree cp_parser_template_name
1582   (cp_parser *, bool, bool, bool, bool *);
1583 static tree cp_parser_template_argument_list
1584   (cp_parser *);
1585 static tree cp_parser_template_argument
1586   (cp_parser *);
1587 static void cp_parser_explicit_instantiation
1588   (cp_parser *);
1589 static void cp_parser_explicit_specialization
1590   (cp_parser *);
1591
1592 /* Exception handling [gram.exception] */
1593
1594 static tree cp_parser_try_block
1595   (cp_parser *);
1596 static bool cp_parser_function_try_block
1597   (cp_parser *);
1598 static void cp_parser_handler_seq
1599   (cp_parser *);
1600 static void cp_parser_handler
1601   (cp_parser *);
1602 static tree cp_parser_exception_declaration
1603   (cp_parser *);
1604 static tree cp_parser_throw_expression
1605   (cp_parser *);
1606 static tree cp_parser_exception_specification_opt
1607   (cp_parser *);
1608 static tree cp_parser_type_id_list
1609   (cp_parser *);
1610
1611 /* GNU Extensions */
1612
1613 static tree cp_parser_asm_specification_opt
1614   (cp_parser *);
1615 static tree cp_parser_asm_operand_list
1616   (cp_parser *);
1617 static tree cp_parser_asm_clobber_list
1618   (cp_parser *);
1619 static tree cp_parser_attributes_opt
1620   (cp_parser *);
1621 static tree cp_parser_attribute_list
1622   (cp_parser *);
1623 static bool cp_parser_extension_opt
1624   (cp_parser *, int *);
1625 static void cp_parser_label_declaration
1626   (cp_parser *);
1627
1628 /* Utility Routines */
1629
1630 static tree cp_parser_lookup_name
1631   (cp_parser *, tree, bool, bool, bool, bool);
1632 static tree cp_parser_lookup_name_simple
1633   (cp_parser *, tree);
1634 static tree cp_parser_maybe_treat_template_as_class
1635   (tree, bool);
1636 static bool cp_parser_check_declarator_template_parameters
1637   (cp_parser *, tree);
1638 static bool cp_parser_check_template_parameters
1639   (cp_parser *, unsigned);
1640 static tree cp_parser_simple_cast_expression
1641   (cp_parser *);
1642 static tree cp_parser_binary_expression
1643   (cp_parser *, const cp_parser_token_tree_map, cp_parser_expression_fn);
1644 static tree cp_parser_global_scope_opt
1645   (cp_parser *, bool);
1646 static bool cp_parser_constructor_declarator_p
1647   (cp_parser *, bool);
1648 static tree cp_parser_function_definition_from_specifiers_and_declarator
1649   (cp_parser *, tree, tree, tree);
1650 static tree cp_parser_function_definition_after_declarator
1651   (cp_parser *, bool);
1652 static void cp_parser_template_declaration_after_export
1653   (cp_parser *, bool);
1654 static tree cp_parser_single_declaration
1655   (cp_parser *, bool, bool *);
1656 static tree cp_parser_functional_cast
1657   (cp_parser *, tree);
1658 static tree cp_parser_save_member_function_body
1659   (cp_parser *, tree, tree, tree);
1660 static tree cp_parser_enclosed_template_argument_list
1661   (cp_parser *);
1662 static void cp_parser_save_default_args
1663   (cp_parser *, tree);
1664 static void cp_parser_late_parsing_for_member
1665   (cp_parser *, tree);
1666 static void cp_parser_late_parsing_default_args
1667   (cp_parser *, tree);
1668 static tree cp_parser_sizeof_operand
1669   (cp_parser *, enum rid);
1670 static bool cp_parser_declares_only_class_p
1671   (cp_parser *);
1672 static bool cp_parser_friend_p
1673   (tree);
1674 static cp_token *cp_parser_require
1675   (cp_parser *, enum cpp_ttype, const char *);
1676 static cp_token *cp_parser_require_keyword
1677   (cp_parser *, enum rid, const char *);
1678 static bool cp_parser_token_starts_function_definition_p
1679   (cp_token *);
1680 static bool cp_parser_next_token_starts_class_definition_p
1681   (cp_parser *);
1682 static bool cp_parser_next_token_ends_template_argument_p
1683   (cp_parser *);
1684 static bool cp_parser_nth_token_starts_template_argument_list_p
1685   (cp_parser *, size_t);
1686 static enum tag_types cp_parser_token_is_class_key
1687   (cp_token *);
1688 static void cp_parser_check_class_key
1689   (enum tag_types, tree type);
1690 static void cp_parser_check_access_in_redeclaration
1691   (tree type);
1692 static bool cp_parser_optional_template_keyword
1693   (cp_parser *);
1694 static void cp_parser_pre_parsed_nested_name_specifier
1695   (cp_parser *);
1696 static void cp_parser_cache_group
1697   (cp_parser *, cp_token_cache *, enum cpp_ttype, unsigned);
1698 static void cp_parser_parse_tentatively
1699   (cp_parser *);
1700 static void cp_parser_commit_to_tentative_parse
1701   (cp_parser *);
1702 static void cp_parser_abort_tentative_parse
1703   (cp_parser *);
1704 static bool cp_parser_parse_definitely
1705   (cp_parser *);
1706 static inline bool cp_parser_parsing_tentatively
1707   (cp_parser *);
1708 static bool cp_parser_committed_to_tentative_parse
1709   (cp_parser *);
1710 static void cp_parser_error
1711   (cp_parser *, const char *);
1712 static void cp_parser_name_lookup_error
1713   (cp_parser *, tree, tree, const char *);
1714 static bool cp_parser_simulate_error
1715   (cp_parser *);
1716 static void cp_parser_check_type_definition
1717   (cp_parser *);
1718 static void cp_parser_check_for_definition_in_return_type
1719   (tree, int);
1720 static void cp_parser_check_for_invalid_template_id
1721   (cp_parser *, tree);
1722 static tree cp_parser_non_integral_constant_expression
1723   (const char *);
1724 static void cp_parser_diagnose_invalid_type_name
1725   (cp_parser *, tree, tree);
1726 static bool cp_parser_parse_and_diagnose_invalid_type_name
1727   (cp_parser *);
1728 static int cp_parser_skip_to_closing_parenthesis
1729   (cp_parser *, bool, bool, bool);
1730 static void cp_parser_skip_to_end_of_statement
1731   (cp_parser *);
1732 static void cp_parser_consume_semicolon_at_end_of_statement
1733   (cp_parser *);
1734 static void cp_parser_skip_to_end_of_block_or_statement
1735   (cp_parser *);
1736 static void cp_parser_skip_to_closing_brace
1737   (cp_parser *);
1738 static void cp_parser_skip_until_found
1739   (cp_parser *, enum cpp_ttype, const char *);
1740 static bool cp_parser_error_occurred
1741   (cp_parser *);
1742 static bool cp_parser_allow_gnu_extensions_p
1743   (cp_parser *);
1744 static bool cp_parser_is_string_literal
1745   (cp_token *);
1746 static bool cp_parser_is_keyword
1747   (cp_token *, enum rid);
1748 static tree cp_parser_make_typename_type
1749   (cp_parser *, tree, tree);
1750
1751 /* Returns nonzero if we are parsing tentatively.  */
1752
1753 static inline bool
1754 cp_parser_parsing_tentatively (cp_parser* parser)
1755 {
1756   return parser->context->next != NULL;
1757 }
1758
1759 /* Returns nonzero if TOKEN is a string literal.  */
1760
1761 static bool
1762 cp_parser_is_string_literal (cp_token* token)
1763 {
1764   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1765 }
1766
1767 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1768
1769 static bool
1770 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1771 {
1772   return token->keyword == keyword;
1773 }
1774
1775 /* Issue the indicated error MESSAGE.  */
1776
1777 static void
1778 cp_parser_error (cp_parser* parser, const char* message)
1779 {
1780   /* Output the MESSAGE -- unless we're parsing tentatively.  */
1781   if (!cp_parser_simulate_error (parser))
1782     {
1783       cp_token *token;
1784       token = cp_lexer_peek_token (parser->lexer);
1785       c_parse_error (message,
1786                      /* Because c_parser_error does not understand
1787                         CPP_KEYWORD, keywords are treated like
1788                         identifiers.  */
1789                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1790                      token->value);
1791     }
1792 }
1793
1794 /* Issue an error about name-lookup failing.  NAME is the
1795    IDENTIFIER_NODE DECL is the result of
1796    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1797    the thing that we hoped to find.  */
1798
1799 static void
1800 cp_parser_name_lookup_error (cp_parser* parser,
1801                              tree name,
1802                              tree decl,
1803                              const char* desired)
1804 {
1805   /* If name lookup completely failed, tell the user that NAME was not
1806      declared.  */
1807   if (decl == error_mark_node)
1808     {
1809       if (parser->scope && parser->scope != global_namespace)
1810         error ("`%D::%D' has not been declared",
1811                parser->scope, name);
1812       else if (parser->scope == global_namespace)
1813         error ("`::%D' has not been declared", name);
1814       else
1815         error ("`%D' has not been declared", name);
1816     }
1817   else if (parser->scope && parser->scope != global_namespace)
1818     error ("`%D::%D' %s", parser->scope, name, desired);
1819   else if (parser->scope == global_namespace)
1820     error ("`::%D' %s", name, desired);
1821   else
1822     error ("`%D' %s", name, desired);
1823 }
1824
1825 /* If we are parsing tentatively, remember that an error has occurred
1826    during this tentative parse.  Returns true if the error was
1827    simulated; false if a message should be issued by the caller.  */
1828
1829 static bool
1830 cp_parser_simulate_error (cp_parser* parser)
1831 {
1832   if (cp_parser_parsing_tentatively (parser)
1833       && !cp_parser_committed_to_tentative_parse (parser))
1834     {
1835       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1836       return true;
1837     }
1838   return false;
1839 }
1840
1841 /* This function is called when a type is defined.  If type
1842    definitions are forbidden at this point, an error message is
1843    issued.  */
1844
1845 static void
1846 cp_parser_check_type_definition (cp_parser* parser)
1847 {
1848   /* If types are forbidden here, issue a message.  */
1849   if (parser->type_definition_forbidden_message)
1850     /* Use `%s' to print the string in case there are any escape
1851        characters in the message.  */
1852     error ("%s", parser->type_definition_forbidden_message);
1853 }
1854
1855 /* This function is called when a declaration is parsed.  If
1856    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1857    indicates that a type was defined in the decl-specifiers for DECL,
1858    then an error is issued.  */
1859
1860 static void
1861 cp_parser_check_for_definition_in_return_type (tree declarator,
1862                                                int declares_class_or_enum)
1863 {
1864   /* [dcl.fct] forbids type definitions in return types.
1865      Unfortunately, it's not easy to know whether or not we are
1866      processing a return type until after the fact.  */
1867   while (declarator
1868          && (TREE_CODE (declarator) == INDIRECT_REF
1869              || TREE_CODE (declarator) == ADDR_EXPR))
1870     declarator = TREE_OPERAND (declarator, 0);
1871   if (declarator
1872       && TREE_CODE (declarator) == CALL_EXPR
1873       && declares_class_or_enum & 2)
1874     error ("new types may not be defined in a return type");
1875 }
1876
1877 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1878    "<" in any valid C++ program.  If the next token is indeed "<",
1879    issue a message warning the user about what appears to be an
1880    invalid attempt to form a template-id.  */
1881
1882 static void
1883 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1884                                          tree type)
1885 {
1886   ptrdiff_t start;
1887   cp_token *token;
1888
1889   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1890     {
1891       if (TYPE_P (type))
1892         error ("`%T' is not a template", type);
1893       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1894         error ("`%s' is not a template", IDENTIFIER_POINTER (type));
1895       else
1896         error ("invalid template-id");
1897       /* Remember the location of the invalid "<".  */
1898       if (cp_parser_parsing_tentatively (parser)
1899           && !cp_parser_committed_to_tentative_parse (parser))
1900         {
1901           token = cp_lexer_peek_token (parser->lexer);
1902           token = cp_lexer_prev_token (parser->lexer, token);
1903           start = cp_lexer_token_difference (parser->lexer,
1904                                              parser->lexer->first_token,
1905                                              token);
1906         }
1907       else
1908         start = -1;
1909       /* Consume the "<".  */
1910       cp_lexer_consume_token (parser->lexer);
1911       /* Parse the template arguments.  */
1912       cp_parser_enclosed_template_argument_list (parser);
1913       /* Permanently remove the invalid template arguments so that
1914          this error message is not issued again.  */
1915       if (start >= 0)
1916         {
1917           token = cp_lexer_advance_token (parser->lexer,
1918                                           parser->lexer->first_token,
1919                                           start);
1920           cp_lexer_purge_tokens_after (parser->lexer, token);
1921         }
1922     }
1923 }
1924
1925 /* Issue an error message about the fact that THING appeared in a
1926    constant-expression.  Returns ERROR_MARK_NODE.  */
1927
1928 static tree
1929 cp_parser_non_integral_constant_expression (const char *thing)
1930 {
1931   error ("%s cannot appear in a constant-expression", thing);
1932   return error_mark_node;
1933 }
1934
1935 /* Emit a diagnostic for an invalid type name. Consider also if it is
1936    qualified or not and the result of a lookup, to provide a better
1937    message.  */
1938
1939 static void
1940 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1941 {
1942   tree decl, old_scope;
1943   /* Try to lookup the identifier.  */
1944   old_scope = parser->scope;
1945   parser->scope = scope;
1946   decl = cp_parser_lookup_name_simple (parser, id);
1947   parser->scope = old_scope;
1948   /* If the lookup found a template-name, it means that the user forgot
1949   to specify an argument list. Emit an useful error message.  */
1950   if (TREE_CODE (decl) == TEMPLATE_DECL)
1951     error ("invalid use of template-name `%E' without an argument list",
1952       decl);
1953   else if (!parser->scope)
1954     {
1955       /* Issue an error message.  */
1956       error ("`%E' does not name a type", id);
1957       /* If we're in a template class, it's possible that the user was
1958          referring to a type from a base class.  For example:
1959
1960            template <typename T> struct A { typedef T X; };
1961            template <typename T> struct B : public A<T> { X x; };
1962
1963          The user should have said "typename A<T>::X".  */
1964       if (processing_template_decl && current_class_type)
1965         {
1966           tree b;
1967
1968           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1969                b;
1970                b = TREE_CHAIN (b))
1971             {
1972               tree base_type = BINFO_TYPE (b);
1973               if (CLASS_TYPE_P (base_type)
1974                   && dependent_type_p (base_type))
1975                 {
1976                   tree field;
1977                   /* Go from a particular instantiation of the
1978                      template (which will have an empty TYPE_FIELDs),
1979                      to the main version.  */
1980                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
1981                   for (field = TYPE_FIELDS (base_type);
1982                        field;
1983                        field = TREE_CHAIN (field))
1984                     if (TREE_CODE (field) == TYPE_DECL
1985                         && DECL_NAME (field) == id)
1986                       {
1987                         inform ("(perhaps `typename %T::%E' was intended)",
1988                                 BINFO_TYPE (b), id);
1989                         break;
1990                       }
1991                   if (field)
1992                     break;
1993                 }
1994             }
1995         }
1996     }
1997   /* Here we diagnose qualified-ids where the scope is actually correct,
1998      but the identifier does not resolve to a valid type name.  */
1999   else
2000     {
2001       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2002         error ("`%E' in namespace `%E' does not name a type",
2003                id, parser->scope);
2004       else if (TYPE_P (parser->scope))
2005         error ("`%E' in class `%T' does not name a type",
2006                id, parser->scope);
2007       else
2008         abort();
2009     }
2010 }
2011
2012 /* Check for a common situation where a type-name should be present,
2013    but is not, and issue a sensible error message.  Returns true if an
2014    invalid type-name was detected.
2015
2016    The situation handled by this function are variable declarations of the
2017    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2018    Usually, `ID' should name a type, but if we got here it means that it
2019    does not. We try to emit the best possible error message depending on
2020    how exactly the id-expression looks like.
2021 */
2022
2023 static bool
2024 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2025 {
2026   tree id;
2027
2028   cp_parser_parse_tentatively (parser);
2029   id = cp_parser_id_expression (parser,
2030                                 /*template_keyword_p=*/false,
2031                                 /*check_dependency_p=*/true,
2032                                 /*template_p=*/NULL,
2033                                 /*declarator_p=*/true);
2034   /* After the id-expression, there should be a plain identifier,
2035      otherwise this is not a simple variable declaration. Also, if
2036      the scope is dependent, we cannot do much.  */
2037   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2038       || (parser->scope && TYPE_P (parser->scope)
2039           && dependent_type_p (parser->scope)))
2040     {
2041       cp_parser_abort_tentative_parse (parser);
2042       return false;
2043     }
2044   if (!cp_parser_parse_definitely (parser))
2045     return false;
2046
2047   /* If we got here, this cannot be a valid variable declaration, thus
2048      the cp_parser_id_expression must have resolved to a plain identifier
2049      node (not a TYPE_DECL or TEMPLATE_ID_EXPR).  */
2050   my_friendly_assert (TREE_CODE (id) == IDENTIFIER_NODE, 20030203);
2051   /* Emit a diagnostic for the invalid type.  */
2052   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2053   /* Skip to the end of the declaration; there's no point in
2054      trying to process it.  */
2055   cp_parser_skip_to_end_of_block_or_statement (parser);
2056   return true;
2057 }
2058
2059 /* Consume tokens up to, and including, the next non-nested closing `)'.
2060    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2061    are doing error recovery. Returns -1 if OR_COMMA is true and we
2062    found an unnested comma.  */
2063
2064 static int
2065 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2066                                        bool recovering,
2067                                        bool or_comma,
2068                                        bool consume_paren)
2069 {
2070   unsigned paren_depth = 0;
2071   unsigned brace_depth = 0;
2072
2073   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2074       && !cp_parser_committed_to_tentative_parse (parser))
2075     return 0;
2076
2077   while (true)
2078     {
2079       cp_token *token;
2080
2081       /* If we've run out of tokens, then there is no closing `)'.  */
2082       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2083         return 0;
2084
2085       token = cp_lexer_peek_token (parser->lexer);
2086
2087       /* This matches the processing in skip_to_end_of_statement.  */
2088       if (token->type == CPP_SEMICOLON && !brace_depth)
2089         return 0;
2090       if (token->type == CPP_OPEN_BRACE)
2091         ++brace_depth;
2092       if (token->type == CPP_CLOSE_BRACE)
2093         {
2094           if (!brace_depth--)
2095             return 0;
2096         }
2097       if (recovering && or_comma && token->type == CPP_COMMA
2098           && !brace_depth && !paren_depth)
2099         return -1;
2100
2101       if (!brace_depth)
2102         {
2103           /* If it is an `(', we have entered another level of nesting.  */
2104           if (token->type == CPP_OPEN_PAREN)
2105             ++paren_depth;
2106           /* If it is a `)', then we might be done.  */
2107           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2108             {
2109               if (consume_paren)
2110                 cp_lexer_consume_token (parser->lexer);
2111               return 1;
2112             }
2113         }
2114
2115       /* Consume the token.  */
2116       cp_lexer_consume_token (parser->lexer);
2117     }
2118 }
2119
2120 /* Consume tokens until we reach the end of the current statement.
2121    Normally, that will be just before consuming a `;'.  However, if a
2122    non-nested `}' comes first, then we stop before consuming that.  */
2123
2124 static void
2125 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2126 {
2127   unsigned nesting_depth = 0;
2128
2129   while (true)
2130     {
2131       cp_token *token;
2132
2133       /* Peek at the next token.  */
2134       token = cp_lexer_peek_token (parser->lexer);
2135       /* If we've run out of tokens, stop.  */
2136       if (token->type == CPP_EOF)
2137         break;
2138       /* If the next token is a `;', we have reached the end of the
2139          statement.  */
2140       if (token->type == CPP_SEMICOLON && !nesting_depth)
2141         break;
2142       /* If the next token is a non-nested `}', then we have reached
2143          the end of the current block.  */
2144       if (token->type == CPP_CLOSE_BRACE)
2145         {
2146           /* If this is a non-nested `}', stop before consuming it.
2147              That way, when confronted with something like:
2148
2149                { 3 + }
2150
2151              we stop before consuming the closing `}', even though we
2152              have not yet reached a `;'.  */
2153           if (nesting_depth == 0)
2154             break;
2155           /* If it is the closing `}' for a block that we have
2156              scanned, stop -- but only after consuming the token.
2157              That way given:
2158
2159                 void f g () { ... }
2160                 typedef int I;
2161
2162              we will stop after the body of the erroneously declared
2163              function, but before consuming the following `typedef'
2164              declaration.  */
2165           if (--nesting_depth == 0)
2166             {
2167               cp_lexer_consume_token (parser->lexer);
2168               break;
2169             }
2170         }
2171       /* If it the next token is a `{', then we are entering a new
2172          block.  Consume the entire block.  */
2173       else if (token->type == CPP_OPEN_BRACE)
2174         ++nesting_depth;
2175       /* Consume the token.  */
2176       cp_lexer_consume_token (parser->lexer);
2177     }
2178 }
2179
2180 /* This function is called at the end of a statement or declaration.
2181    If the next token is a semicolon, it is consumed; otherwise, error
2182    recovery is attempted.  */
2183
2184 static void
2185 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2186 {
2187   /* Look for the trailing `;'.  */
2188   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2189     {
2190       /* If there is additional (erroneous) input, skip to the end of
2191          the statement.  */
2192       cp_parser_skip_to_end_of_statement (parser);
2193       /* If the next token is now a `;', consume it.  */
2194       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2195         cp_lexer_consume_token (parser->lexer);
2196     }
2197 }
2198
2199 /* Skip tokens until we have consumed an entire block, or until we
2200    have consumed a non-nested `;'.  */
2201
2202 static void
2203 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2204 {
2205   unsigned nesting_depth = 0;
2206
2207   while (true)
2208     {
2209       cp_token *token;
2210
2211       /* Peek at the next token.  */
2212       token = cp_lexer_peek_token (parser->lexer);
2213       /* If we've run out of tokens, stop.  */
2214       if (token->type == CPP_EOF)
2215         break;
2216       /* If the next token is a `;', we have reached the end of the
2217          statement.  */
2218       if (token->type == CPP_SEMICOLON && !nesting_depth)
2219         {
2220           /* Consume the `;'.  */
2221           cp_lexer_consume_token (parser->lexer);
2222           break;
2223         }
2224       /* Consume the token.  */
2225       token = cp_lexer_consume_token (parser->lexer);
2226       /* If the next token is a non-nested `}', then we have reached
2227          the end of the current block.  */
2228       if (token->type == CPP_CLOSE_BRACE
2229           && (nesting_depth == 0 || --nesting_depth == 0))
2230         break;
2231       /* If it the next token is a `{', then we are entering a new
2232          block.  Consume the entire block.  */
2233       if (token->type == CPP_OPEN_BRACE)
2234         ++nesting_depth;
2235     }
2236 }
2237
2238 /* Skip tokens until a non-nested closing curly brace is the next
2239    token.  */
2240
2241 static void
2242 cp_parser_skip_to_closing_brace (cp_parser *parser)
2243 {
2244   unsigned nesting_depth = 0;
2245
2246   while (true)
2247     {
2248       cp_token *token;
2249
2250       /* Peek at the next token.  */
2251       token = cp_lexer_peek_token (parser->lexer);
2252       /* If we've run out of tokens, stop.  */
2253       if (token->type == CPP_EOF)
2254         break;
2255       /* If the next token is a non-nested `}', then we have reached
2256          the end of the current block.  */
2257       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2258         break;
2259       /* If it the next token is a `{', then we are entering a new
2260          block.  Consume the entire block.  */
2261       else if (token->type == CPP_OPEN_BRACE)
2262         ++nesting_depth;
2263       /* Consume the token.  */
2264       cp_lexer_consume_token (parser->lexer);
2265     }
2266 }
2267
2268 /* This is a simple wrapper around make_typename_type. When the id is
2269    an unresolved identifier node, we can provide a superior diagnostic
2270    using cp_parser_diagnose_invalid_type_name.  */
2271
2272 static tree
2273 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2274 {
2275   tree result;
2276   if (TREE_CODE (id) == IDENTIFIER_NODE)
2277     {
2278       result = make_typename_type (scope, id, /*complain=*/0);
2279       if (result == error_mark_node)
2280         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2281       return result;
2282     }
2283   return make_typename_type (scope, id, tf_error);
2284 }
2285
2286
2287 /* Create a new C++ parser.  */
2288
2289 static cp_parser *
2290 cp_parser_new (void)
2291 {
2292   cp_parser *parser;
2293   cp_lexer *lexer;
2294
2295   /* cp_lexer_new_main is called before calling ggc_alloc because
2296      cp_lexer_new_main might load a PCH file.  */
2297   lexer = cp_lexer_new_main ();
2298
2299   parser = ggc_alloc_cleared (sizeof (cp_parser));
2300   parser->lexer = lexer;
2301   parser->context = cp_parser_context_new (NULL);
2302
2303   /* For now, we always accept GNU extensions.  */
2304   parser->allow_gnu_extensions_p = 1;
2305
2306   /* The `>' token is a greater-than operator, not the end of a
2307      template-id.  */
2308   parser->greater_than_is_operator_p = true;
2309
2310   parser->default_arg_ok_p = true;
2311
2312   /* We are not parsing a constant-expression.  */
2313   parser->integral_constant_expression_p = false;
2314   parser->allow_non_integral_constant_expression_p = false;
2315   parser->non_integral_constant_expression_p = false;
2316
2317   /* We are not parsing offsetof.  */
2318   parser->in_offsetof_p = false;
2319
2320   /* Local variable names are not forbidden.  */
2321   parser->local_variables_forbidden_p = false;
2322
2323   /* We are not processing an `extern "C"' declaration.  */
2324   parser->in_unbraced_linkage_specification_p = false;
2325
2326   /* We are not processing a declarator.  */
2327   parser->in_declarator_p = false;
2328
2329   /* We are not processing a template-argument-list.  */
2330   parser->in_template_argument_list_p = false;
2331
2332   /* We are not in an iteration statement.  */
2333   parser->in_iteration_statement_p = false;
2334
2335   /* We are not in a switch statement.  */
2336   parser->in_switch_statement_p = false;
2337
2338   /* We are not parsing a type-id inside an expression.  */
2339   parser->in_type_id_in_expr_p = false;
2340
2341   /* The unparsed function queue is empty.  */
2342   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2343
2344   /* There are no classes being defined.  */
2345   parser->num_classes_being_defined = 0;
2346
2347   /* No template parameters apply.  */
2348   parser->num_template_parameter_lists = 0;
2349
2350   return parser;
2351 }
2352
2353 /* Lexical conventions [gram.lex]  */
2354
2355 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2356    identifier.  */
2357
2358 static tree
2359 cp_parser_identifier (cp_parser* parser)
2360 {
2361   cp_token *token;
2362
2363   /* Look for the identifier.  */
2364   token = cp_parser_require (parser, CPP_NAME, "identifier");
2365   /* Return the value.  */
2366   return token ? token->value : error_mark_node;
2367 }
2368
2369 /* Basic concepts [gram.basic]  */
2370
2371 /* Parse a translation-unit.
2372
2373    translation-unit:
2374      declaration-seq [opt]
2375
2376    Returns TRUE if all went well.  */
2377
2378 static bool
2379 cp_parser_translation_unit (cp_parser* parser)
2380 {
2381   while (true)
2382     {
2383       cp_parser_declaration_seq_opt (parser);
2384
2385       /* If there are no tokens left then all went well.  */
2386       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2387         break;
2388
2389       /* Otherwise, issue an error message.  */
2390       cp_parser_error (parser, "expected declaration");
2391       return false;
2392     }
2393
2394   /* Consume the EOF token.  */
2395   cp_parser_require (parser, CPP_EOF, "end-of-file");
2396
2397   /* Finish up.  */
2398   finish_translation_unit ();
2399
2400   /* All went well.  */
2401   return true;
2402 }
2403
2404 /* Expressions [gram.expr] */
2405
2406 /* Parse a primary-expression.
2407
2408    primary-expression:
2409      literal
2410      this
2411      ( expression )
2412      id-expression
2413
2414    GNU Extensions:
2415
2416    primary-expression:
2417      ( compound-statement )
2418      __builtin_va_arg ( assignment-expression , type-id )
2419
2420    literal:
2421      __null
2422
2423    Returns a representation of the expression.
2424
2425    *IDK indicates what kind of id-expression (if any) was present.
2426
2427    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2428    used as the operand of a pointer-to-member.  In that case,
2429    *QUALIFYING_CLASS gives the class that is used as the qualifying
2430    class in the pointer-to-member.  */
2431
2432 static tree
2433 cp_parser_primary_expression (cp_parser *parser,
2434                               cp_id_kind *idk,
2435                               tree *qualifying_class)
2436 {
2437   cp_token *token;
2438
2439   /* Assume the primary expression is not an id-expression.  */
2440   *idk = CP_ID_KIND_NONE;
2441   /* And that it cannot be used as pointer-to-member.  */
2442   *qualifying_class = NULL_TREE;
2443
2444   /* Peek at the next token.  */
2445   token = cp_lexer_peek_token (parser->lexer);
2446   switch (token->type)
2447     {
2448       /* literal:
2449            integer-literal
2450            character-literal
2451            floating-literal
2452            string-literal
2453            boolean-literal  */
2454     case CPP_CHAR:
2455     case CPP_WCHAR:
2456     case CPP_STRING:
2457     case CPP_WSTRING:
2458     case CPP_NUMBER:
2459       token = cp_lexer_consume_token (parser->lexer);
2460       return token->value;
2461
2462     case CPP_OPEN_PAREN:
2463       {
2464         tree expr;
2465         bool saved_greater_than_is_operator_p;
2466
2467         /* Consume the `('.  */
2468         cp_lexer_consume_token (parser->lexer);
2469         /* Within a parenthesized expression, a `>' token is always
2470            the greater-than operator.  */
2471         saved_greater_than_is_operator_p
2472           = parser->greater_than_is_operator_p;
2473         parser->greater_than_is_operator_p = true;
2474         /* If we see `( { ' then we are looking at the beginning of
2475            a GNU statement-expression.  */
2476         if (cp_parser_allow_gnu_extensions_p (parser)
2477             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2478           {
2479             /* Statement-expressions are not allowed by the standard.  */
2480             if (pedantic)
2481               pedwarn ("ISO C++ forbids braced-groups within expressions");
2482
2483             /* And they're not allowed outside of a function-body; you
2484                cannot, for example, write:
2485
2486                  int i = ({ int j = 3; j + 1; });
2487
2488                at class or namespace scope.  */
2489             if (!at_function_scope_p ())
2490               error ("statement-expressions are allowed only inside functions");
2491             /* Start the statement-expression.  */
2492             expr = begin_stmt_expr ();
2493             /* Parse the compound-statement.  */
2494             cp_parser_compound_statement (parser, true);
2495             /* Finish up.  */
2496             expr = finish_stmt_expr (expr, false);
2497           }
2498         else
2499           {
2500             /* Parse the parenthesized expression.  */
2501             expr = cp_parser_expression (parser);
2502             /* Let the front end know that this expression was
2503                enclosed in parentheses. This matters in case, for
2504                example, the expression is of the form `A::B', since
2505                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2506                not.  */
2507             finish_parenthesized_expr (expr);
2508           }
2509         /* The `>' token might be the end of a template-id or
2510            template-parameter-list now.  */
2511         parser->greater_than_is_operator_p
2512           = saved_greater_than_is_operator_p;
2513         /* Consume the `)'.  */
2514         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2515           cp_parser_skip_to_end_of_statement (parser);
2516
2517         return expr;
2518       }
2519
2520     case CPP_KEYWORD:
2521       switch (token->keyword)
2522         {
2523           /* These two are the boolean literals.  */
2524         case RID_TRUE:
2525           cp_lexer_consume_token (parser->lexer);
2526           return boolean_true_node;
2527         case RID_FALSE:
2528           cp_lexer_consume_token (parser->lexer);
2529           return boolean_false_node;
2530
2531           /* The `__null' literal.  */
2532         case RID_NULL:
2533           cp_lexer_consume_token (parser->lexer);
2534           return null_node;
2535
2536           /* Recognize the `this' keyword.  */
2537         case RID_THIS:
2538           cp_lexer_consume_token (parser->lexer);
2539           if (parser->local_variables_forbidden_p)
2540             {
2541               error ("`this' may not be used in this context");
2542               return error_mark_node;
2543             }
2544           /* Pointers cannot appear in constant-expressions.  */
2545           if (parser->integral_constant_expression_p)
2546             {
2547               if (!parser->allow_non_integral_constant_expression_p)
2548                 return cp_parser_non_integral_constant_expression ("`this'");
2549               parser->non_integral_constant_expression_p = true;
2550             }
2551           return finish_this_expr ();
2552
2553           /* The `operator' keyword can be the beginning of an
2554              id-expression.  */
2555         case RID_OPERATOR:
2556           goto id_expression;
2557
2558         case RID_FUNCTION_NAME:
2559         case RID_PRETTY_FUNCTION_NAME:
2560         case RID_C99_FUNCTION_NAME:
2561           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2562              __func__ are the names of variables -- but they are
2563              treated specially.  Therefore, they are handled here,
2564              rather than relying on the generic id-expression logic
2565              below.  Grammatically, these names are id-expressions.
2566
2567              Consume the token.  */
2568           token = cp_lexer_consume_token (parser->lexer);
2569           /* Look up the name.  */
2570           return finish_fname (token->value);
2571
2572         case RID_VA_ARG:
2573           {
2574             tree expression;
2575             tree type;
2576
2577             /* The `__builtin_va_arg' construct is used to handle
2578                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2579             cp_lexer_consume_token (parser->lexer);
2580             /* Look for the opening `('.  */
2581             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2582             /* Now, parse the assignment-expression.  */
2583             expression = cp_parser_assignment_expression (parser);
2584             /* Look for the `,'.  */
2585             cp_parser_require (parser, CPP_COMMA, "`,'");
2586             /* Parse the type-id.  */
2587             type = cp_parser_type_id (parser);
2588             /* Look for the closing `)'.  */
2589             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2590             /* Using `va_arg' in a constant-expression is not
2591                allowed.  */
2592             if (parser->integral_constant_expression_p)
2593               {
2594                 if (!parser->allow_non_integral_constant_expression_p)
2595                   return cp_parser_non_integral_constant_expression ("`va_arg'");
2596                 parser->non_integral_constant_expression_p = true;
2597               }
2598             return build_x_va_arg (expression, type);
2599           }
2600
2601         case RID_OFFSETOF:
2602           {
2603             tree expression;
2604             bool saved_in_offsetof_p;
2605
2606             /* Consume the "__offsetof__" token.  */
2607             cp_lexer_consume_token (parser->lexer);
2608             /* Consume the opening `('.  */
2609             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2610             /* Parse the parenthesized (almost) constant-expression.  */
2611             saved_in_offsetof_p = parser->in_offsetof_p;
2612             parser->in_offsetof_p = true;
2613             expression
2614               = cp_parser_constant_expression (parser,
2615                                                /*allow_non_constant_p=*/false,
2616                                                /*non_constant_p=*/NULL);
2617             parser->in_offsetof_p = saved_in_offsetof_p;
2618             /* Consume the closing ')'.  */
2619             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2620
2621             return expression;
2622           }
2623
2624         default:
2625           cp_parser_error (parser, "expected primary-expression");
2626           return error_mark_node;
2627         }
2628
2629       /* An id-expression can start with either an identifier, a
2630          `::' as the beginning of a qualified-id, or the "operator"
2631          keyword.  */
2632     case CPP_NAME:
2633     case CPP_SCOPE:
2634     case CPP_TEMPLATE_ID:
2635     case CPP_NESTED_NAME_SPECIFIER:
2636       {
2637         tree id_expression;
2638         tree decl;
2639         const char *error_msg;
2640
2641       id_expression:
2642         /* Parse the id-expression.  */
2643         id_expression
2644           = cp_parser_id_expression (parser,
2645                                      /*template_keyword_p=*/false,
2646                                      /*check_dependency_p=*/true,
2647                                      /*template_p=*/NULL,
2648                                      /*declarator_p=*/false);
2649         if (id_expression == error_mark_node)
2650           return error_mark_node;
2651         /* If we have a template-id, then no further lookup is
2652            required.  If the template-id was for a template-class, we
2653            will sometimes have a TYPE_DECL at this point.  */
2654         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2655             || TREE_CODE (id_expression) == TYPE_DECL)
2656           decl = id_expression;
2657         /* Look up the name.  */
2658         else
2659           {
2660             decl = cp_parser_lookup_name_simple (parser, id_expression);
2661             /* If name lookup gives us a SCOPE_REF, then the
2662                qualifying scope was dependent.  Just propagate the
2663                name.  */
2664             if (TREE_CODE (decl) == SCOPE_REF)
2665               {
2666                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2667                   *qualifying_class = TREE_OPERAND (decl, 0);
2668                 return decl;
2669               }
2670             /* Check to see if DECL is a local variable in a context
2671                where that is forbidden.  */
2672             if (parser->local_variables_forbidden_p
2673                 && local_variable_p (decl))
2674               {
2675                 /* It might be that we only found DECL because we are
2676                    trying to be generous with pre-ISO scoping rules.
2677                    For example, consider:
2678
2679                      int i;
2680                      void g() {
2681                        for (int i = 0; i < 10; ++i) {}
2682                        extern void f(int j = i);
2683                      }
2684
2685                    Here, name look up will originally find the out
2686                    of scope `i'.  We need to issue a warning message,
2687                    but then use the global `i'.  */
2688                 decl = check_for_out_of_scope_variable (decl);
2689                 if (local_variable_p (decl))
2690                   {
2691                     error ("local variable `%D' may not appear in this context",
2692                            decl);
2693                     return error_mark_node;
2694                   }
2695               }
2696           }
2697
2698         decl = finish_id_expression (id_expression, decl, parser->scope,
2699                                      idk, qualifying_class,
2700                                      parser->integral_constant_expression_p,
2701                                      parser->allow_non_integral_constant_expression_p,
2702                                      &parser->non_integral_constant_expression_p,
2703                                      &error_msg);
2704         if (error_msg)
2705           cp_parser_error (parser, error_msg);
2706         return decl;
2707       }
2708
2709       /* Anything else is an error.  */
2710     default:
2711       cp_parser_error (parser, "expected primary-expression");
2712       return error_mark_node;
2713     }
2714 }
2715
2716 /* Parse an id-expression.
2717
2718    id-expression:
2719      unqualified-id
2720      qualified-id
2721
2722    qualified-id:
2723      :: [opt] nested-name-specifier template [opt] unqualified-id
2724      :: identifier
2725      :: operator-function-id
2726      :: template-id
2727
2728    Return a representation of the unqualified portion of the
2729    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2730    a `::' or nested-name-specifier.
2731
2732    Often, if the id-expression was a qualified-id, the caller will
2733    want to make a SCOPE_REF to represent the qualified-id.  This
2734    function does not do this in order to avoid wastefully creating
2735    SCOPE_REFs when they are not required.
2736
2737    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2738    `template' keyword.
2739
2740    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2741    uninstantiated templates.
2742
2743    If *TEMPLATE_P is non-NULL, it is set to true iff the
2744    `template' keyword is used to explicitly indicate that the entity
2745    named is a template.
2746
2747    If DECLARATOR_P is true, the id-expression is appearing as part of
2748    a declarator, rather than as part of an expression.  */
2749
2750 static tree
2751 cp_parser_id_expression (cp_parser *parser,
2752                          bool template_keyword_p,
2753                          bool check_dependency_p,
2754                          bool *template_p,
2755                          bool declarator_p)
2756 {
2757   bool global_scope_p;
2758   bool nested_name_specifier_p;
2759
2760   /* Assume the `template' keyword was not used.  */
2761   if (template_p)
2762     *template_p = false;
2763
2764   /* Look for the optional `::' operator.  */
2765   global_scope_p
2766     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2767        != NULL_TREE);
2768   /* Look for the optional nested-name-specifier.  */
2769   nested_name_specifier_p
2770     = (cp_parser_nested_name_specifier_opt (parser,
2771                                             /*typename_keyword_p=*/false,
2772                                             check_dependency_p,
2773                                             /*type_p=*/false,
2774                                             /*is_declarator=*/false)
2775        != NULL_TREE);
2776   /* If there is a nested-name-specifier, then we are looking at
2777      the first qualified-id production.  */
2778   if (nested_name_specifier_p)
2779     {
2780       tree saved_scope;
2781       tree saved_object_scope;
2782       tree saved_qualifying_scope;
2783       tree unqualified_id;
2784       bool is_template;
2785
2786       /* See if the next token is the `template' keyword.  */
2787       if (!template_p)
2788         template_p = &is_template;
2789       *template_p = cp_parser_optional_template_keyword (parser);
2790       /* Name lookup we do during the processing of the
2791          unqualified-id might obliterate SCOPE.  */
2792       saved_scope = parser->scope;
2793       saved_object_scope = parser->object_scope;
2794       saved_qualifying_scope = parser->qualifying_scope;
2795       /* Process the final unqualified-id.  */
2796       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
2797                                                  check_dependency_p,
2798                                                  declarator_p);
2799       /* Restore the SAVED_SCOPE for our caller.  */
2800       parser->scope = saved_scope;
2801       parser->object_scope = saved_object_scope;
2802       parser->qualifying_scope = saved_qualifying_scope;
2803
2804       return unqualified_id;
2805     }
2806   /* Otherwise, if we are in global scope, then we are looking at one
2807      of the other qualified-id productions.  */
2808   else if (global_scope_p)
2809     {
2810       cp_token *token;
2811       tree id;
2812
2813       /* Peek at the next token.  */
2814       token = cp_lexer_peek_token (parser->lexer);
2815
2816       /* If it's an identifier, and the next token is not a "<", then
2817          we can avoid the template-id case.  This is an optimization
2818          for this common case.  */
2819       if (token->type == CPP_NAME
2820           && !cp_parser_nth_token_starts_template_argument_list_p
2821                (parser, 2))
2822         return cp_parser_identifier (parser);
2823
2824       cp_parser_parse_tentatively (parser);
2825       /* Try a template-id.  */
2826       id = cp_parser_template_id (parser,
2827                                   /*template_keyword_p=*/false,
2828                                   /*check_dependency_p=*/true,
2829                                   declarator_p);
2830       /* If that worked, we're done.  */
2831       if (cp_parser_parse_definitely (parser))
2832         return id;
2833
2834       /* Peek at the next token.  (Changes in the token buffer may
2835          have invalidated the pointer obtained above.)  */
2836       token = cp_lexer_peek_token (parser->lexer);
2837
2838       switch (token->type)
2839         {
2840         case CPP_NAME:
2841           return cp_parser_identifier (parser);
2842
2843         case CPP_KEYWORD:
2844           if (token->keyword == RID_OPERATOR)
2845             return cp_parser_operator_function_id (parser);
2846           /* Fall through.  */
2847
2848         default:
2849           cp_parser_error (parser, "expected id-expression");
2850           return error_mark_node;
2851         }
2852     }
2853   else
2854     return cp_parser_unqualified_id (parser, template_keyword_p,
2855                                      /*check_dependency_p=*/true,
2856                                      declarator_p);
2857 }
2858
2859 /* Parse an unqualified-id.
2860
2861    unqualified-id:
2862      identifier
2863      operator-function-id
2864      conversion-function-id
2865      ~ class-name
2866      template-id
2867
2868    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
2869    keyword, in a construct like `A::template ...'.
2870
2871    Returns a representation of unqualified-id.  For the `identifier'
2872    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
2873    production a BIT_NOT_EXPR is returned; the operand of the
2874    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
2875    other productions, see the documentation accompanying the
2876    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
2877    names are looked up in uninstantiated templates.  If DECLARATOR_P
2878    is true, the unqualified-id is appearing as part of a declarator,
2879    rather than as part of an expression.  */
2880
2881 static tree
2882 cp_parser_unqualified_id (cp_parser* parser,
2883                           bool template_keyword_p,
2884                           bool check_dependency_p,
2885                           bool declarator_p)
2886 {
2887   cp_token *token;
2888
2889   /* Peek at the next token.  */
2890   token = cp_lexer_peek_token (parser->lexer);
2891
2892   switch (token->type)
2893     {
2894     case CPP_NAME:
2895       {
2896         tree id;
2897
2898         /* We don't know yet whether or not this will be a
2899            template-id.  */
2900         cp_parser_parse_tentatively (parser);
2901         /* Try a template-id.  */
2902         id = cp_parser_template_id (parser, template_keyword_p,
2903                                     check_dependency_p,
2904                                     declarator_p);
2905         /* If it worked, we're done.  */
2906         if (cp_parser_parse_definitely (parser))
2907           return id;
2908         /* Otherwise, it's an ordinary identifier.  */
2909         return cp_parser_identifier (parser);
2910       }
2911
2912     case CPP_TEMPLATE_ID:
2913       return cp_parser_template_id (parser, template_keyword_p,
2914                                     check_dependency_p,
2915                                     declarator_p);
2916
2917     case CPP_COMPL:
2918       {
2919         tree type_decl;
2920         tree qualifying_scope;
2921         tree object_scope;
2922         tree scope;
2923
2924         /* Consume the `~' token.  */
2925         cp_lexer_consume_token (parser->lexer);
2926         /* Parse the class-name.  The standard, as written, seems to
2927            say that:
2928
2929              template <typename T> struct S { ~S (); };
2930              template <typename T> S<T>::~S() {}
2931
2932            is invalid, since `~' must be followed by a class-name, but
2933            `S<T>' is dependent, and so not known to be a class.
2934            That's not right; we need to look in uninstantiated
2935            templates.  A further complication arises from:
2936
2937              template <typename T> void f(T t) {
2938                t.T::~T();
2939              }
2940
2941            Here, it is not possible to look up `T' in the scope of `T'
2942            itself.  We must look in both the current scope, and the
2943            scope of the containing complete expression.
2944
2945            Yet another issue is:
2946
2947              struct S {
2948                int S;
2949                ~S();
2950              };
2951
2952              S::~S() {}
2953
2954            The standard does not seem to say that the `S' in `~S'
2955            should refer to the type `S' and not the data member
2956            `S::S'.  */
2957
2958         /* DR 244 says that we look up the name after the "~" in the
2959            same scope as we looked up the qualifying name.  That idea
2960            isn't fully worked out; it's more complicated than that.  */
2961         scope = parser->scope;
2962         object_scope = parser->object_scope;
2963         qualifying_scope = parser->qualifying_scope;
2964
2965         /* If the name is of the form "X::~X" it's OK.  */
2966         if (scope && TYPE_P (scope)
2967             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2968             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
2969                 == CPP_OPEN_PAREN)
2970             && (cp_lexer_peek_token (parser->lexer)->value
2971                 == TYPE_IDENTIFIER (scope)))
2972           {
2973             cp_lexer_consume_token (parser->lexer);
2974             return build_nt (BIT_NOT_EXPR, scope);
2975           }
2976
2977         /* If there was an explicit qualification (S::~T), first look
2978            in the scope given by the qualification (i.e., S).  */
2979         if (scope)
2980           {
2981             cp_parser_parse_tentatively (parser);
2982             type_decl = cp_parser_class_name (parser,
2983                                               /*typename_keyword_p=*/false,
2984                                               /*template_keyword_p=*/false,
2985                                               /*type_p=*/false,
2986                                               /*check_dependency=*/false,
2987                                               /*class_head_p=*/false,
2988                                               declarator_p);
2989             if (cp_parser_parse_definitely (parser))
2990               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
2991           }
2992         /* In "N::S::~S", look in "N" as well.  */
2993         if (scope && qualifying_scope)
2994           {
2995             cp_parser_parse_tentatively (parser);
2996             parser->scope = qualifying_scope;
2997             parser->object_scope = NULL_TREE;
2998             parser->qualifying_scope = NULL_TREE;
2999             type_decl
3000               = cp_parser_class_name (parser,
3001                                       /*typename_keyword_p=*/false,
3002                                       /*template_keyword_p=*/false,
3003                                       /*type_p=*/false,
3004                                       /*check_dependency=*/false,
3005                                       /*class_head_p=*/false,
3006                                       declarator_p);
3007             if (cp_parser_parse_definitely (parser))
3008               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3009           }
3010         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3011         else if (object_scope)
3012           {
3013             cp_parser_parse_tentatively (parser);
3014             parser->scope = object_scope;
3015             parser->object_scope = NULL_TREE;
3016             parser->qualifying_scope = NULL_TREE;
3017             type_decl
3018               = cp_parser_class_name (parser,
3019                                       /*typename_keyword_p=*/false,
3020                                       /*template_keyword_p=*/false,
3021                                       /*type_p=*/false,
3022                                       /*check_dependency=*/false,
3023                                       /*class_head_p=*/false,
3024                                       declarator_p);
3025             if (cp_parser_parse_definitely (parser))
3026               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3027           }
3028         /* Look in the surrounding context.  */
3029         parser->scope = NULL_TREE;
3030         parser->object_scope = NULL_TREE;
3031         parser->qualifying_scope = NULL_TREE;
3032         type_decl
3033           = cp_parser_class_name (parser,
3034                                   /*typename_keyword_p=*/false,
3035                                   /*template_keyword_p=*/false,
3036                                   /*type_p=*/false,
3037                                   /*check_dependency=*/false,
3038                                   /*class_head_p=*/false,
3039                                   declarator_p);
3040         /* If an error occurred, assume that the name of the
3041            destructor is the same as the name of the qualifying
3042            class.  That allows us to keep parsing after running
3043            into ill-formed destructor names.  */
3044         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3045           return build_nt (BIT_NOT_EXPR, scope);
3046         else if (type_decl == error_mark_node)
3047           return error_mark_node;
3048
3049         /* [class.dtor]
3050
3051            A typedef-name that names a class shall not be used as the
3052            identifier in the declarator for a destructor declaration.  */
3053         if (declarator_p
3054             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3055             && !DECL_SELF_REFERENCE_P (type_decl))
3056           error ("typedef-name `%D' used as destructor declarator",
3057                  type_decl);
3058
3059         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3060       }
3061
3062     case CPP_KEYWORD:
3063       if (token->keyword == RID_OPERATOR)
3064         {
3065           tree id;
3066
3067           /* This could be a template-id, so we try that first.  */
3068           cp_parser_parse_tentatively (parser);
3069           /* Try a template-id.  */
3070           id = cp_parser_template_id (parser, template_keyword_p,
3071                                       /*check_dependency_p=*/true,
3072                                       declarator_p);
3073           /* If that worked, we're done.  */
3074           if (cp_parser_parse_definitely (parser))
3075             return id;
3076           /* We still don't know whether we're looking at an
3077              operator-function-id or a conversion-function-id.  */
3078           cp_parser_parse_tentatively (parser);
3079           /* Try an operator-function-id.  */
3080           id = cp_parser_operator_function_id (parser);
3081           /* If that didn't work, try a conversion-function-id.  */
3082           if (!cp_parser_parse_definitely (parser))
3083             id = cp_parser_conversion_function_id (parser);
3084
3085           return id;
3086         }
3087       /* Fall through.  */
3088
3089     default:
3090       cp_parser_error (parser, "expected unqualified-id");
3091       return error_mark_node;
3092     }
3093 }
3094
3095 /* Parse an (optional) nested-name-specifier.
3096
3097    nested-name-specifier:
3098      class-or-namespace-name :: nested-name-specifier [opt]
3099      class-or-namespace-name :: template nested-name-specifier [opt]
3100
3101    PARSER->SCOPE should be set appropriately before this function is
3102    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3103    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3104    in name lookups.
3105
3106    Sets PARSER->SCOPE to the class (TYPE) or namespace
3107    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3108    it unchanged if there is no nested-name-specifier.  Returns the new
3109    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3110
3111    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3112    part of a declaration and/or decl-specifier.  */
3113
3114 static tree
3115 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3116                                      bool typename_keyword_p,
3117                                      bool check_dependency_p,
3118                                      bool type_p,
3119                                      bool is_declaration)
3120 {
3121   bool success = false;
3122   tree access_check = NULL_TREE;
3123   ptrdiff_t start;
3124   cp_token* token;
3125
3126   /* If the next token corresponds to a nested name specifier, there
3127      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3128      false, it may have been true before, in which case something
3129      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3130      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3131      CHECK_DEPENDENCY_P is false, we have to fall through into the
3132      main loop.  */
3133   if (check_dependency_p
3134       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3135     {
3136       cp_parser_pre_parsed_nested_name_specifier (parser);
3137       return parser->scope;
3138     }
3139
3140   /* Remember where the nested-name-specifier starts.  */
3141   if (cp_parser_parsing_tentatively (parser)
3142       && !cp_parser_committed_to_tentative_parse (parser))
3143     {
3144       token = cp_lexer_peek_token (parser->lexer);
3145       start = cp_lexer_token_difference (parser->lexer,
3146                                          parser->lexer->first_token,
3147                                          token);
3148     }
3149   else
3150     start = -1;
3151
3152   push_deferring_access_checks (dk_deferred);
3153
3154   while (true)
3155     {
3156       tree new_scope;
3157       tree old_scope;
3158       tree saved_qualifying_scope;
3159       bool template_keyword_p;
3160
3161       /* Spot cases that cannot be the beginning of a
3162          nested-name-specifier.  */
3163       token = cp_lexer_peek_token (parser->lexer);
3164
3165       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3166          the already parsed nested-name-specifier.  */
3167       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3168         {
3169           /* Grab the nested-name-specifier and continue the loop.  */
3170           cp_parser_pre_parsed_nested_name_specifier (parser);
3171           success = true;
3172           continue;
3173         }
3174
3175       /* Spot cases that cannot be the beginning of a
3176          nested-name-specifier.  On the second and subsequent times
3177          through the loop, we look for the `template' keyword.  */
3178       if (success && token->keyword == RID_TEMPLATE)
3179         ;
3180       /* A template-id can start a nested-name-specifier.  */
3181       else if (token->type == CPP_TEMPLATE_ID)
3182         ;
3183       else
3184         {
3185           /* If the next token is not an identifier, then it is
3186              definitely not a class-or-namespace-name.  */
3187           if (token->type != CPP_NAME)
3188             break;
3189           /* If the following token is neither a `<' (to begin a
3190              template-id), nor a `::', then we are not looking at a
3191              nested-name-specifier.  */
3192           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3193           if (token->type != CPP_SCOPE
3194               && !cp_parser_nth_token_starts_template_argument_list_p
3195                   (parser, 2))
3196             break;
3197         }
3198
3199       /* The nested-name-specifier is optional, so we parse
3200          tentatively.  */
3201       cp_parser_parse_tentatively (parser);
3202
3203       /* Look for the optional `template' keyword, if this isn't the
3204          first time through the loop.  */
3205       if (success)
3206         template_keyword_p = cp_parser_optional_template_keyword (parser);
3207       else
3208         template_keyword_p = false;
3209
3210       /* Save the old scope since the name lookup we are about to do
3211          might destroy it.  */
3212       old_scope = parser->scope;
3213       saved_qualifying_scope = parser->qualifying_scope;
3214       /* Parse the qualifying entity.  */
3215       new_scope
3216         = cp_parser_class_or_namespace_name (parser,
3217                                              typename_keyword_p,
3218                                              template_keyword_p,
3219                                              check_dependency_p,
3220                                              type_p,
3221                                              is_declaration);
3222       /* Look for the `::' token.  */
3223       cp_parser_require (parser, CPP_SCOPE, "`::'");
3224
3225       /* If we found what we wanted, we keep going; otherwise, we're
3226          done.  */
3227       if (!cp_parser_parse_definitely (parser))
3228         {
3229           bool error_p = false;
3230
3231           /* Restore the OLD_SCOPE since it was valid before the
3232              failed attempt at finding the last
3233              class-or-namespace-name.  */
3234           parser->scope = old_scope;
3235           parser->qualifying_scope = saved_qualifying_scope;
3236           /* If the next token is an identifier, and the one after
3237              that is a `::', then any valid interpretation would have
3238              found a class-or-namespace-name.  */
3239           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3240                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3241                      == CPP_SCOPE)
3242                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3243                      != CPP_COMPL))
3244             {
3245               token = cp_lexer_consume_token (parser->lexer);
3246               if (!error_p)
3247                 {
3248                   tree decl;
3249
3250                   decl = cp_parser_lookup_name_simple (parser, token->value);
3251                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3252                     error ("`%D' used without template parameters",
3253                            decl);
3254                   else
3255                     cp_parser_name_lookup_error
3256                       (parser, token->value, decl,
3257                        "is not a class or namespace");
3258                   parser->scope = NULL_TREE;
3259                   error_p = true;
3260                   /* Treat this as a successful nested-name-specifier
3261                      due to:
3262
3263                      [basic.lookup.qual]
3264
3265                      If the name found is not a class-name (clause
3266                      _class_) or namespace-name (_namespace.def_), the
3267                      program is ill-formed.  */
3268                   success = true;
3269                 }
3270               cp_lexer_consume_token (parser->lexer);
3271             }
3272           break;
3273         }
3274
3275       /* We've found one valid nested-name-specifier.  */
3276       success = true;
3277       /* Make sure we look in the right scope the next time through
3278          the loop.  */
3279       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3280                        ? TREE_TYPE (new_scope)
3281                        : new_scope);
3282       /* If it is a class scope, try to complete it; we are about to
3283          be looking up names inside the class.  */
3284       if (TYPE_P (parser->scope)
3285           /* Since checking types for dependency can be expensive,
3286              avoid doing it if the type is already complete.  */
3287           && !COMPLETE_TYPE_P (parser->scope)
3288           /* Do not try to complete dependent types.  */
3289           && !dependent_type_p (parser->scope))
3290         complete_type (parser->scope);
3291     }
3292
3293   /* Retrieve any deferred checks.  Do not pop this access checks yet
3294      so the memory will not be reclaimed during token replacing below.  */
3295   access_check = get_deferred_access_checks ();
3296
3297   /* If parsing tentatively, replace the sequence of tokens that makes
3298      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3299      token.  That way, should we re-parse the token stream, we will
3300      not have to repeat the effort required to do the parse, nor will
3301      we issue duplicate error messages.  */
3302   if (success && start >= 0)
3303     {
3304       /* Find the token that corresponds to the start of the
3305          template-id.  */
3306       token = cp_lexer_advance_token (parser->lexer,
3307                                       parser->lexer->first_token,
3308                                       start);
3309
3310       /* Reset the contents of the START token.  */
3311       token->type = CPP_NESTED_NAME_SPECIFIER;
3312       token->value = build_tree_list (access_check, parser->scope);
3313       TREE_TYPE (token->value) = parser->qualifying_scope;
3314       token->keyword = RID_MAX;
3315       /* Purge all subsequent tokens.  */
3316       cp_lexer_purge_tokens_after (parser->lexer, token);
3317     }
3318
3319   pop_deferring_access_checks ();
3320   return success ? parser->scope : NULL_TREE;
3321 }
3322
3323 /* Parse a nested-name-specifier.  See
3324    cp_parser_nested_name_specifier_opt for details.  This function
3325    behaves identically, except that it will an issue an error if no
3326    nested-name-specifier is present, and it will return
3327    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3328    is present.  */
3329
3330 static tree
3331 cp_parser_nested_name_specifier (cp_parser *parser,
3332                                  bool typename_keyword_p,
3333                                  bool check_dependency_p,
3334                                  bool type_p,
3335                                  bool is_declaration)
3336 {
3337   tree scope;
3338
3339   /* Look for the nested-name-specifier.  */
3340   scope = cp_parser_nested_name_specifier_opt (parser,
3341                                                typename_keyword_p,
3342                                                check_dependency_p,
3343                                                type_p,
3344                                                is_declaration);
3345   /* If it was not present, issue an error message.  */
3346   if (!scope)
3347     {
3348       cp_parser_error (parser, "expected nested-name-specifier");
3349       parser->scope = NULL_TREE;
3350       return error_mark_node;
3351     }
3352
3353   return scope;
3354 }
3355
3356 /* Parse a class-or-namespace-name.
3357
3358    class-or-namespace-name:
3359      class-name
3360      namespace-name
3361
3362    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3363    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3364    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3365    TYPE_P is TRUE iff the next name should be taken as a class-name,
3366    even the same name is declared to be another entity in the same
3367    scope.
3368
3369    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3370    specified by the class-or-namespace-name.  If neither is found the
3371    ERROR_MARK_NODE is returned.  */
3372
3373 static tree
3374 cp_parser_class_or_namespace_name (cp_parser *parser,
3375                                    bool typename_keyword_p,
3376                                    bool template_keyword_p,
3377                                    bool check_dependency_p,
3378                                    bool type_p,
3379                                    bool is_declaration)
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                                 is_declaration);
3405   /* If that didn't work, try for a namespace-name.  */
3406   if (!only_class_p && !cp_parser_parse_definitely (parser))
3407     {
3408       /* Restore the saved scope.  */
3409       parser->scope = saved_scope;
3410       parser->qualifying_scope = saved_qualifying_scope;
3411       parser->object_scope = saved_object_scope;
3412       /* If we are not looking at an identifier followed by the scope
3413          resolution operator, then this is not part of a
3414          nested-name-specifier.  (Note that this function is only used
3415          to parse the components of a nested-name-specifier.)  */
3416       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3417           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3418         return error_mark_node;
3419       scope = cp_parser_namespace_name (parser);
3420     }
3421
3422   return scope;
3423 }
3424
3425 /* Parse a postfix-expression.
3426
3427    postfix-expression:
3428      primary-expression
3429      postfix-expression [ expression ]
3430      postfix-expression ( expression-list [opt] )
3431      simple-type-specifier ( expression-list [opt] )
3432      typename :: [opt] nested-name-specifier identifier
3433        ( expression-list [opt] )
3434      typename :: [opt] nested-name-specifier template [opt] template-id
3435        ( expression-list [opt] )
3436      postfix-expression . template [opt] id-expression
3437      postfix-expression -> template [opt] id-expression
3438      postfix-expression . pseudo-destructor-name
3439      postfix-expression -> pseudo-destructor-name
3440      postfix-expression ++
3441      postfix-expression --
3442      dynamic_cast < type-id > ( expression )
3443      static_cast < type-id > ( expression )
3444      reinterpret_cast < type-id > ( expression )
3445      const_cast < type-id > ( expression )
3446      typeid ( expression )
3447      typeid ( type-id )
3448
3449    GNU Extension:
3450
3451    postfix-expression:
3452      ( type-id ) { initializer-list , [opt] }
3453
3454    This extension is a GNU version of the C99 compound-literal
3455    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3456    but they are essentially the same concept.)
3457
3458    If ADDRESS_P is true, the postfix expression is the operand of the
3459    `&' operator.
3460
3461    Returns a representation of the expression.  */
3462
3463 static tree
3464 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3465 {
3466   cp_token *token;
3467   enum rid keyword;
3468   cp_id_kind idk = CP_ID_KIND_NONE;
3469   tree postfix_expression = NULL_TREE;
3470   /* Non-NULL only if the current postfix-expression can be used to
3471      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3472      class used to qualify the member.  */
3473   tree qualifying_class = NULL_TREE;
3474
3475   /* Peek at the next token.  */
3476   token = cp_lexer_peek_token (parser->lexer);
3477   /* Some of the productions are determined by keywords.  */
3478   keyword = token->keyword;
3479   switch (keyword)
3480     {
3481     case RID_DYNCAST:
3482     case RID_STATCAST:
3483     case RID_REINTCAST:
3484     case RID_CONSTCAST:
3485       {
3486         tree type;
3487         tree expression;
3488         const char *saved_message;
3489
3490         /* All of these can be handled in the same way from the point
3491            of view of parsing.  Begin by consuming the token
3492            identifying the cast.  */
3493         cp_lexer_consume_token (parser->lexer);
3494
3495         /* New types cannot be defined in the cast.  */
3496         saved_message = parser->type_definition_forbidden_message;
3497         parser->type_definition_forbidden_message
3498           = "types may not be defined in casts";
3499
3500         /* Look for the opening `<'.  */
3501         cp_parser_require (parser, CPP_LESS, "`<'");
3502         /* Parse the type to which we are casting.  */
3503         type = cp_parser_type_id (parser);
3504         /* Look for the closing `>'.  */
3505         cp_parser_require (parser, CPP_GREATER, "`>'");
3506         /* Restore the old message.  */
3507         parser->type_definition_forbidden_message = saved_message;
3508
3509         /* And the expression which is being cast.  */
3510         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3511         expression = cp_parser_expression (parser);
3512         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3513
3514         /* Only type conversions to integral or enumeration types
3515            can be used in constant-expressions.  */
3516         if (parser->integral_constant_expression_p
3517             && !dependent_type_p (type)
3518             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3519             /* A cast to pointer or reference type is allowed in the
3520                implementation of "offsetof".  */
3521             && !(parser->in_offsetof_p && POINTER_TYPE_P (type)))
3522           {
3523             if (!parser->allow_non_integral_constant_expression_p)
3524               return (cp_parser_non_integral_constant_expression
3525                       ("a cast to a type other than an integral or "
3526                        "enumeration type"));
3527             parser->non_integral_constant_expression_p = true;
3528           }
3529
3530         switch (keyword)
3531           {
3532           case RID_DYNCAST:
3533             postfix_expression
3534               = build_dynamic_cast (type, expression);
3535             break;
3536           case RID_STATCAST:
3537             postfix_expression
3538               = build_static_cast (type, expression);
3539             break;
3540           case RID_REINTCAST:
3541             postfix_expression
3542               = build_reinterpret_cast (type, expression);
3543             break;
3544           case RID_CONSTCAST:
3545             postfix_expression
3546               = build_const_cast (type, expression);
3547             break;
3548           default:
3549             abort ();
3550           }
3551       }
3552       break;
3553
3554     case RID_TYPEID:
3555       {
3556         tree type;
3557         const char *saved_message;
3558         bool saved_in_type_id_in_expr_p;
3559
3560         /* Consume the `typeid' token.  */
3561         cp_lexer_consume_token (parser->lexer);
3562         /* Look for the `(' token.  */
3563         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3564         /* Types cannot be defined in a `typeid' expression.  */
3565         saved_message = parser->type_definition_forbidden_message;
3566         parser->type_definition_forbidden_message
3567           = "types may not be defined in a `typeid\' expression";
3568         /* We can't be sure yet whether we're looking at a type-id or an
3569            expression.  */
3570         cp_parser_parse_tentatively (parser);
3571         /* Try a type-id first.  */
3572         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3573         parser->in_type_id_in_expr_p = true;
3574         type = cp_parser_type_id (parser);
3575         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3576         /* Look for the `)' token.  Otherwise, we can't be sure that
3577            we're not looking at an expression: consider `typeid (int
3578            (3))', for example.  */
3579         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3580         /* If all went well, simply lookup the type-id.  */
3581         if (cp_parser_parse_definitely (parser))
3582           postfix_expression = get_typeid (type);
3583         /* Otherwise, fall back to the expression variant.  */
3584         else
3585           {
3586             tree expression;
3587
3588             /* Look for an expression.  */
3589             expression = cp_parser_expression (parser);
3590             /* Compute its typeid.  */
3591             postfix_expression = build_typeid (expression);
3592             /* Look for the `)' token.  */
3593             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3594           }
3595
3596         /* Restore the saved message.  */
3597         parser->type_definition_forbidden_message = saved_message;
3598       }
3599       break;
3600
3601     case RID_TYPENAME:
3602       {
3603         bool template_p = false;
3604         tree id;
3605         tree type;
3606
3607         /* Consume the `typename' token.  */
3608         cp_lexer_consume_token (parser->lexer);
3609         /* Look for the optional `::' operator.  */
3610         cp_parser_global_scope_opt (parser,
3611                                     /*current_scope_valid_p=*/false);
3612         /* Look for the nested-name-specifier.  */
3613         cp_parser_nested_name_specifier (parser,
3614                                          /*typename_keyword_p=*/true,
3615                                          /*check_dependency_p=*/true,
3616                                          /*type_p=*/true,
3617                                          /*is_declaration=*/true);
3618         /* Look for the optional `template' keyword.  */
3619         template_p = cp_parser_optional_template_keyword (parser);
3620         /* We don't know whether we're looking at a template-id or an
3621            identifier.  */
3622         cp_parser_parse_tentatively (parser);
3623         /* Try a template-id.  */
3624         id = cp_parser_template_id (parser, template_p,
3625                                     /*check_dependency_p=*/true,
3626                                     /*is_declaration=*/true);
3627         /* If that didn't work, try an identifier.  */
3628         if (!cp_parser_parse_definitely (parser))
3629           id = cp_parser_identifier (parser);
3630         /* Create a TYPENAME_TYPE to represent the type to which the
3631            functional cast is being performed.  */
3632         type = make_typename_type (parser->scope, id,
3633                                    /*complain=*/1);
3634
3635         postfix_expression = cp_parser_functional_cast (parser, type);
3636       }
3637       break;
3638
3639     default:
3640       {
3641         tree type;
3642
3643         /* If the next thing is a simple-type-specifier, we may be
3644            looking at a functional cast.  We could also be looking at
3645            an id-expression.  So, we try the functional cast, and if
3646            that doesn't work we fall back to the primary-expression.  */
3647         cp_parser_parse_tentatively (parser);
3648         /* Look for the simple-type-specifier.  */
3649         type = cp_parser_simple_type_specifier (parser,
3650                                                 CP_PARSER_FLAGS_NONE,
3651                                                 /*identifier_p=*/false);
3652         /* Parse the cast itself.  */
3653         if (!cp_parser_error_occurred (parser))
3654           postfix_expression
3655             = cp_parser_functional_cast (parser, type);
3656         /* If that worked, we're done.  */
3657         if (cp_parser_parse_definitely (parser))
3658           break;
3659
3660         /* If the functional-cast didn't work out, try a
3661            compound-literal.  */
3662         if (cp_parser_allow_gnu_extensions_p (parser)
3663             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3664           {
3665             tree initializer_list = NULL_TREE;
3666             bool saved_in_type_id_in_expr_p;
3667
3668             cp_parser_parse_tentatively (parser);
3669             /* Consume the `('.  */
3670             cp_lexer_consume_token (parser->lexer);
3671             /* Parse the type.  */
3672             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3673             parser->in_type_id_in_expr_p = true;
3674             type = cp_parser_type_id (parser);
3675             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3676             /* Look for the `)'.  */
3677             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3678             /* Look for the `{'.  */
3679             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3680             /* If things aren't going well, there's no need to
3681                keep going.  */
3682             if (!cp_parser_error_occurred (parser))
3683               {
3684                 bool non_constant_p;
3685                 /* Parse the initializer-list.  */
3686                 initializer_list
3687                   = cp_parser_initializer_list (parser, &non_constant_p);
3688                 /* Allow a trailing `,'.  */
3689                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3690                   cp_lexer_consume_token (parser->lexer);
3691                 /* Look for the final `}'.  */
3692                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3693               }
3694             /* If that worked, we're definitely looking at a
3695                compound-literal expression.  */
3696             if (cp_parser_parse_definitely (parser))
3697               {
3698                 /* Warn the user that a compound literal is not
3699                    allowed in standard C++.  */
3700                 if (pedantic)
3701                   pedwarn ("ISO C++ forbids compound-literals");
3702                 /* Form the representation of the compound-literal.  */
3703                 postfix_expression
3704                   = finish_compound_literal (type, initializer_list);
3705                 break;
3706               }
3707           }
3708
3709         /* It must be a primary-expression.  */
3710         postfix_expression = cp_parser_primary_expression (parser,
3711                                                            &idk,
3712                                                            &qualifying_class);
3713       }
3714       break;
3715     }
3716
3717   /* If we were avoiding committing to the processing of a
3718      qualified-id until we knew whether or not we had a
3719      pointer-to-member, we now know.  */
3720   if (qualifying_class)
3721     {
3722       bool done;
3723
3724       /* Peek at the next token.  */
3725       token = cp_lexer_peek_token (parser->lexer);
3726       done = (token->type != CPP_OPEN_SQUARE
3727               && token->type != CPP_OPEN_PAREN
3728               && token->type != CPP_DOT
3729               && token->type != CPP_DEREF
3730               && token->type != CPP_PLUS_PLUS
3731               && token->type != CPP_MINUS_MINUS);
3732
3733       postfix_expression = finish_qualified_id_expr (qualifying_class,
3734                                                      postfix_expression,
3735                                                      done,
3736                                                      address_p);
3737       if (done)
3738         return postfix_expression;
3739     }
3740
3741   /* Keep looping until the postfix-expression is complete.  */
3742   while (true)
3743     {
3744       if (idk == CP_ID_KIND_UNQUALIFIED
3745           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3746           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3747         /* It is not a Koenig lookup function call.  */
3748         postfix_expression
3749           = unqualified_name_lookup_error (postfix_expression);
3750
3751       /* Peek at the next token.  */
3752       token = cp_lexer_peek_token (parser->lexer);
3753
3754       switch (token->type)
3755         {
3756         case CPP_OPEN_SQUARE:
3757           /* postfix-expression [ expression ] */
3758           {
3759             tree index;
3760
3761             /* Consume the `[' token.  */
3762             cp_lexer_consume_token (parser->lexer);
3763             /* Parse the index expression.  */
3764             index = cp_parser_expression (parser);
3765             /* Look for the closing `]'.  */
3766             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
3767
3768             /* Build the ARRAY_REF.  */
3769             postfix_expression
3770               = grok_array_decl (postfix_expression, index);
3771             idk = CP_ID_KIND_NONE;
3772             /* Array references are not permitted in
3773                constant-expressions.  */
3774             if (parser->integral_constant_expression_p)
3775               {
3776                 if (!parser->allow_non_integral_constant_expression_p)
3777                   postfix_expression
3778                     = cp_parser_non_integral_constant_expression ("an array reference");
3779                 parser->non_integral_constant_expression_p = true;
3780               }
3781           }
3782           break;
3783
3784         case CPP_OPEN_PAREN:
3785           /* postfix-expression ( expression-list [opt] ) */
3786           {
3787             bool koenig_p;
3788             tree args = (cp_parser_parenthesized_expression_list
3789                          (parser, false, /*non_constant_p=*/NULL));
3790
3791             if (args == error_mark_node)
3792               {
3793                 postfix_expression = error_mark_node;
3794                 break;
3795               }
3796
3797             /* Function calls are not permitted in
3798                constant-expressions.  */
3799             if (parser->integral_constant_expression_p)
3800               {
3801                 if (!parser->allow_non_integral_constant_expression_p)
3802                   {
3803                     postfix_expression
3804                       = cp_parser_non_integral_constant_expression ("a function call");
3805                     break;
3806                   }
3807                 parser->non_integral_constant_expression_p = true;
3808               }
3809
3810             koenig_p = false;
3811             if (idk == CP_ID_KIND_UNQUALIFIED)
3812               {
3813                 /* We do not perform argument-dependent lookup if
3814                    normal lookup finds a non-function, in accordance
3815                    with the expected resolution of DR 218.  */
3816                 if (args
3817                     && (is_overloaded_fn (postfix_expression)
3818                         || TREE_CODE (postfix_expression) == IDENTIFIER_NODE))
3819                   {
3820                     koenig_p = true;
3821                     postfix_expression
3822                       = perform_koenig_lookup (postfix_expression, args);
3823                   }
3824                 else if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3825                   postfix_expression
3826                     = unqualified_fn_lookup_error (postfix_expression);
3827               }
3828
3829             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
3830               {
3831                 tree instance = TREE_OPERAND (postfix_expression, 0);
3832                 tree fn = TREE_OPERAND (postfix_expression, 1);
3833
3834                 if (processing_template_decl
3835                     && (type_dependent_expression_p (instance)
3836                         || (!BASELINK_P (fn)
3837                             && TREE_CODE (fn) != FIELD_DECL)
3838                         || type_dependent_expression_p (fn)
3839                         || any_type_dependent_arguments_p (args)))
3840                   {
3841                     postfix_expression
3842                       = build_min_nt (CALL_EXPR, postfix_expression, args);
3843                     break;
3844                   }
3845
3846                 if (BASELINK_P (fn))
3847                   postfix_expression
3848                     = (build_new_method_call
3849                        (instance, fn, args, NULL_TREE,
3850                         (idk == CP_ID_KIND_QUALIFIED
3851                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
3852                 else
3853                   postfix_expression
3854                     = finish_call_expr (postfix_expression, args,
3855                                         /*disallow_virtual=*/false,
3856                                         /*koenig_p=*/false);
3857               }
3858             else if (TREE_CODE (postfix_expression) == OFFSET_REF
3859                      || TREE_CODE (postfix_expression) == MEMBER_REF
3860                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
3861               postfix_expression = (build_offset_ref_call_from_tree
3862                                     (postfix_expression, args));
3863             else if (idk == CP_ID_KIND_QUALIFIED)
3864               /* A call to a static class member, or a namespace-scope
3865                  function.  */
3866               postfix_expression
3867                 = finish_call_expr (postfix_expression, args,
3868                                     /*disallow_virtual=*/true,
3869                                     koenig_p);
3870             else
3871               /* All other function calls.  */
3872               postfix_expression
3873                 = finish_call_expr (postfix_expression, args,
3874                                     /*disallow_virtual=*/false,
3875                                     koenig_p);
3876
3877             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
3878             idk = CP_ID_KIND_NONE;
3879           }
3880           break;
3881
3882         case CPP_DOT:
3883         case CPP_DEREF:
3884           /* postfix-expression . template [opt] id-expression
3885              postfix-expression . pseudo-destructor-name
3886              postfix-expression -> template [opt] id-expression
3887              postfix-expression -> pseudo-destructor-name */
3888           {
3889             tree name;
3890             bool dependent_p;
3891             bool template_p;
3892             tree scope = NULL_TREE;
3893             enum cpp_ttype token_type = token->type;
3894
3895             /* If this is a `->' operator, dereference the pointer.  */
3896             if (token->type == CPP_DEREF)
3897               postfix_expression = build_x_arrow (postfix_expression);
3898             /* Check to see whether or not the expression is
3899                type-dependent.  */
3900             dependent_p = type_dependent_expression_p (postfix_expression);
3901             /* The identifier following the `->' or `.' is not
3902                qualified.  */
3903             parser->scope = NULL_TREE;
3904             parser->qualifying_scope = NULL_TREE;
3905             parser->object_scope = NULL_TREE;
3906             idk = CP_ID_KIND_NONE;
3907             /* Enter the scope corresponding to the type of the object
3908                given by the POSTFIX_EXPRESSION.  */
3909             if (!dependent_p
3910                 && TREE_TYPE (postfix_expression) != NULL_TREE)
3911               {
3912                 scope = TREE_TYPE (postfix_expression);
3913                 /* According to the standard, no expression should
3914                    ever have reference type.  Unfortunately, we do not
3915                    currently match the standard in this respect in
3916                    that our internal representation of an expression
3917                    may have reference type even when the standard says
3918                    it does not.  Therefore, we have to manually obtain
3919                    the underlying type here.  */
3920                 scope = non_reference (scope);
3921                 /* The type of the POSTFIX_EXPRESSION must be
3922                    complete.  */
3923                 scope = complete_type_or_else (scope, NULL_TREE);
3924                 /* Let the name lookup machinery know that we are
3925                    processing a class member access expression.  */
3926                 parser->context->object_type = scope;
3927                 /* If something went wrong, we want to be able to
3928                    discern that case, as opposed to the case where
3929                    there was no SCOPE due to the type of expression
3930                    being dependent.  */
3931                 if (!scope)
3932                   scope = error_mark_node;
3933                 /* If the SCOPE was erroneous, make the various
3934                    semantic analysis functions exit quickly -- and
3935                    without issuing additional error messages.  */
3936                 if (scope == error_mark_node)
3937                   postfix_expression = error_mark_node;
3938               }
3939
3940             /* Consume the `.' or `->' operator.  */
3941             cp_lexer_consume_token (parser->lexer);
3942             /* If the SCOPE is not a scalar type, we are looking at an
3943                ordinary class member access expression, rather than a
3944                pseudo-destructor-name.  */
3945             if (!scope || !SCALAR_TYPE_P (scope))
3946               {
3947                 template_p = cp_parser_optional_template_keyword (parser);
3948                 /* Parse the id-expression.  */
3949                 name = cp_parser_id_expression (parser,
3950                                                 template_p,
3951                                                 /*check_dependency_p=*/true,
3952                                                 /*template_p=*/NULL,
3953                                                 /*declarator_p=*/false);
3954                 /* In general, build a SCOPE_REF if the member name is
3955                    qualified.  However, if the name was not dependent
3956                    and has already been resolved; there is no need to
3957                    build the SCOPE_REF.  For example;
3958
3959                      struct X { void f(); };
3960                      template <typename T> void f(T* t) { t->X::f(); }
3961
3962                    Even though "t" is dependent, "X::f" is not and has
3963                    been resolved to a BASELINK; there is no need to
3964                    include scope information.  */
3965
3966                 /* But we do need to remember that there was an explicit
3967                    scope for virtual function calls.  */
3968                 if (parser->scope)
3969                   idk = CP_ID_KIND_QUALIFIED;
3970
3971                 if (name != error_mark_node
3972                     && !BASELINK_P (name)
3973                     && parser->scope)
3974                   {
3975                     name = build_nt (SCOPE_REF, parser->scope, name);
3976                     parser->scope = NULL_TREE;
3977                     parser->qualifying_scope = NULL_TREE;
3978                     parser->object_scope = NULL_TREE;
3979                   }
3980                 postfix_expression
3981                   = finish_class_member_access_expr (postfix_expression, name);
3982               }
3983             /* Otherwise, try the pseudo-destructor-name production.  */
3984             else
3985               {
3986                 tree s = NULL_TREE;
3987                 tree type;
3988
3989                 /* Parse the pseudo-destructor-name.  */
3990                 cp_parser_pseudo_destructor_name (parser, &s, &type);
3991                 /* Form the call.  */
3992                 postfix_expression
3993                   = finish_pseudo_destructor_expr (postfix_expression,
3994                                                    s, TREE_TYPE (type));
3995               }
3996
3997             /* We no longer need to look up names in the scope of the
3998                object on the left-hand side of the `.' or `->'
3999                operator.  */
4000             parser->context->object_type = NULL_TREE;
4001             /* These operators may not appear in constant-expressions.  */
4002             if (parser->integral_constant_expression_p
4003                 /* The "->" operator is allowed in the implementation
4004                    of "offsetof".  The "." operator may appear in the
4005                    name of the member.  */
4006                 && !parser->in_offsetof_p)
4007               {
4008                 if (!parser->allow_non_integral_constant_expression_p)
4009                   postfix_expression
4010                     = (cp_parser_non_integral_constant_expression
4011                        (token_type == CPP_DEREF ? "'->'" : "`.'"));
4012                 parser->non_integral_constant_expression_p = true;
4013               }
4014           }
4015           break;
4016
4017         case CPP_PLUS_PLUS:
4018           /* postfix-expression ++  */
4019           /* Consume the `++' token.  */
4020           cp_lexer_consume_token (parser->lexer);
4021           /* Generate a representation for the complete expression.  */
4022           postfix_expression
4023             = finish_increment_expr (postfix_expression,
4024                                      POSTINCREMENT_EXPR);
4025           /* Increments may not appear in constant-expressions.  */
4026           if (parser->integral_constant_expression_p)
4027             {
4028               if (!parser->allow_non_integral_constant_expression_p)
4029                 postfix_expression
4030                   = cp_parser_non_integral_constant_expression ("an increment");
4031               parser->non_integral_constant_expression_p = true;
4032             }
4033           idk = CP_ID_KIND_NONE;
4034           break;
4035
4036         case CPP_MINUS_MINUS:
4037           /* postfix-expression -- */
4038           /* Consume the `--' token.  */
4039           cp_lexer_consume_token (parser->lexer);
4040           /* Generate a representation for the complete expression.  */
4041           postfix_expression
4042             = finish_increment_expr (postfix_expression,
4043                                      POSTDECREMENT_EXPR);
4044           /* Decrements may not appear in constant-expressions.  */
4045           if (parser->integral_constant_expression_p)
4046             {
4047               if (!parser->allow_non_integral_constant_expression_p)
4048                 postfix_expression
4049                   = cp_parser_non_integral_constant_expression ("a decrement");
4050               parser->non_integral_constant_expression_p = true;
4051             }
4052           idk = CP_ID_KIND_NONE;
4053           break;
4054
4055         default:
4056           return postfix_expression;
4057         }
4058     }
4059
4060   /* We should never get here.  */
4061   abort ();
4062   return error_mark_node;
4063 }
4064
4065 /* Parse a parenthesized expression-list.
4066
4067    expression-list:
4068      assignment-expression
4069      expression-list, assignment-expression
4070
4071    attribute-list:
4072      expression-list
4073      identifier
4074      identifier, expression-list
4075
4076    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4077    representation of an assignment-expression.  Note that a TREE_LIST
4078    is returned even if there is only a single expression in the list.
4079    error_mark_node is returned if the ( and or ) are
4080    missing. NULL_TREE is returned on no expressions. The parentheses
4081    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4082    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4083    indicates whether or not all of the expressions in the list were
4084    constant.  */
4085
4086 static tree
4087 cp_parser_parenthesized_expression_list (cp_parser* parser,
4088                                          bool is_attribute_list,
4089                                          bool *non_constant_p)
4090 {
4091   tree expression_list = NULL_TREE;
4092   tree identifier = NULL_TREE;
4093
4094   /* Assume all the expressions will be constant.  */
4095   if (non_constant_p)
4096     *non_constant_p = false;
4097
4098   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4099     return error_mark_node;
4100
4101   /* Consume expressions until there are no more.  */
4102   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4103     while (true)
4104       {
4105         tree expr;
4106
4107         /* At the beginning of attribute lists, check to see if the
4108            next token is an identifier.  */
4109         if (is_attribute_list
4110             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4111           {
4112             cp_token *token;
4113
4114             /* Consume the identifier.  */
4115             token = cp_lexer_consume_token (parser->lexer);
4116             /* Save the identifier.  */
4117             identifier = token->value;
4118           }
4119         else
4120           {
4121             /* Parse the next assignment-expression.  */
4122             if (non_constant_p)
4123               {
4124                 bool expr_non_constant_p;
4125                 expr = (cp_parser_constant_expression
4126                         (parser, /*allow_non_constant_p=*/true,
4127                          &expr_non_constant_p));
4128                 if (expr_non_constant_p)
4129                   *non_constant_p = true;
4130               }
4131             else
4132               expr = cp_parser_assignment_expression (parser);
4133
4134              /* Add it to the list.  We add error_mark_node
4135                 expressions to the list, so that we can still tell if
4136                 the correct form for a parenthesized expression-list
4137                 is found. That gives better errors.  */
4138             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4139
4140             if (expr == error_mark_node)
4141               goto skip_comma;
4142           }
4143
4144         /* After the first item, attribute lists look the same as
4145            expression lists.  */
4146         is_attribute_list = false;
4147
4148       get_comma:;
4149         /* If the next token isn't a `,', then we are done.  */
4150         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4151           break;
4152
4153         /* Otherwise, consume the `,' and keep going.  */
4154         cp_lexer_consume_token (parser->lexer);
4155       }
4156
4157   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4158     {
4159       int ending;
4160
4161     skip_comma:;
4162       /* We try and resync to an unnested comma, as that will give the
4163          user better diagnostics.  */
4164       ending = cp_parser_skip_to_closing_parenthesis (parser,
4165                                                       /*recovering=*/true,
4166                                                       /*or_comma=*/true,
4167                                                       /*consume_paren=*/true);
4168       if (ending < 0)
4169         goto get_comma;
4170       if (!ending)
4171         return error_mark_node;
4172     }
4173
4174   /* We built up the list in reverse order so we must reverse it now.  */
4175   expression_list = nreverse (expression_list);
4176   if (identifier)
4177     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4178
4179   return expression_list;
4180 }
4181
4182 /* Parse a pseudo-destructor-name.
4183
4184    pseudo-destructor-name:
4185      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4186      :: [opt] nested-name-specifier template template-id :: ~ type-name
4187      :: [opt] nested-name-specifier [opt] ~ type-name
4188
4189    If either of the first two productions is used, sets *SCOPE to the
4190    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4191    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4192    or ERROR_MARK_NODE if the parse fails.  */
4193
4194 static void
4195 cp_parser_pseudo_destructor_name (cp_parser* parser,
4196                                   tree* scope,
4197                                   tree* type)
4198 {
4199   bool nested_name_specifier_p;
4200
4201   /* Look for the optional `::' operator.  */
4202   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4203   /* Look for the optional nested-name-specifier.  */
4204   nested_name_specifier_p
4205     = (cp_parser_nested_name_specifier_opt (parser,
4206                                             /*typename_keyword_p=*/false,
4207                                             /*check_dependency_p=*/true,
4208                                             /*type_p=*/false,
4209                                             /*is_declaration=*/true)
4210        != NULL_TREE);
4211   /* Now, if we saw a nested-name-specifier, we might be doing the
4212      second production.  */
4213   if (nested_name_specifier_p
4214       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4215     {
4216       /* Consume the `template' keyword.  */
4217       cp_lexer_consume_token (parser->lexer);
4218       /* Parse the template-id.  */
4219       cp_parser_template_id (parser,
4220                              /*template_keyword_p=*/true,
4221                              /*check_dependency_p=*/false,
4222                              /*is_declaration=*/true);
4223       /* Look for the `::' token.  */
4224       cp_parser_require (parser, CPP_SCOPE, "`::'");
4225     }
4226   /* If the next token is not a `~', then there might be some
4227      additional qualification.  */
4228   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4229     {
4230       /* Look for the type-name.  */
4231       *scope = TREE_TYPE (cp_parser_type_name (parser));
4232
4233       /* If we didn't get an aggregate type, or we don't have ::~,
4234          then something has gone wrong.  Since the only caller of this
4235          function is looking for something after `.' or `->' after a
4236          scalar type, most likely the program is trying to get a
4237          member of a non-aggregate type.  */
4238       if (*scope == error_mark_node
4239           || cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4240           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4241         {
4242           cp_parser_error (parser, "request for member of non-aggregate type");
4243           *type = error_mark_node;
4244           return;
4245         }
4246
4247       /* Look for the `::' token.  */
4248       cp_parser_require (parser, CPP_SCOPE, "`::'");
4249     }
4250   else
4251     *scope = NULL_TREE;
4252
4253   /* Look for the `~'.  */
4254   cp_parser_require (parser, CPP_COMPL, "`~'");
4255   /* Look for the type-name again.  We are not responsible for
4256      checking that it matches the first type-name.  */
4257   *type = cp_parser_type_name (parser);
4258 }
4259
4260 /* Parse a unary-expression.
4261
4262    unary-expression:
4263      postfix-expression
4264      ++ cast-expression
4265      -- cast-expression
4266      unary-operator cast-expression
4267      sizeof unary-expression
4268      sizeof ( type-id )
4269      new-expression
4270      delete-expression
4271
4272    GNU Extensions:
4273
4274    unary-expression:
4275      __extension__ cast-expression
4276      __alignof__ unary-expression
4277      __alignof__ ( type-id )
4278      __real__ cast-expression
4279      __imag__ cast-expression
4280      && identifier
4281
4282    ADDRESS_P is true iff the unary-expression is appearing as the
4283    operand of the `&' operator.
4284
4285    Returns a representation of the expression.  */
4286
4287 static tree
4288 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4289 {
4290   cp_token *token;
4291   enum tree_code unary_operator;
4292
4293   /* Peek at the next token.  */
4294   token = cp_lexer_peek_token (parser->lexer);
4295   /* Some keywords give away the kind of expression.  */
4296   if (token->type == CPP_KEYWORD)
4297     {
4298       enum rid keyword = token->keyword;
4299
4300       switch (keyword)
4301         {
4302         case RID_ALIGNOF:
4303         case RID_SIZEOF:
4304           {
4305             tree operand;
4306             enum tree_code op;
4307
4308             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4309             /* Consume the token.  */
4310             cp_lexer_consume_token (parser->lexer);
4311             /* Parse the operand.  */
4312             operand = cp_parser_sizeof_operand (parser, keyword);
4313
4314             if (TYPE_P (operand))
4315               return cxx_sizeof_or_alignof_type (operand, op, true);
4316             else
4317               return cxx_sizeof_or_alignof_expr (operand, op);
4318           }
4319
4320         case RID_NEW:
4321           return cp_parser_new_expression (parser);
4322
4323         case RID_DELETE:
4324           return cp_parser_delete_expression (parser);
4325
4326         case RID_EXTENSION:
4327           {
4328             /* The saved value of the PEDANTIC flag.  */
4329             int saved_pedantic;
4330             tree expr;
4331
4332             /* Save away the PEDANTIC flag.  */
4333             cp_parser_extension_opt (parser, &saved_pedantic);
4334             /* Parse the cast-expression.  */
4335             expr = cp_parser_simple_cast_expression (parser);
4336             /* Restore the PEDANTIC flag.  */
4337             pedantic = saved_pedantic;
4338
4339             return expr;
4340           }
4341
4342         case RID_REALPART:
4343         case RID_IMAGPART:
4344           {
4345             tree expression;
4346
4347             /* Consume the `__real__' or `__imag__' token.  */
4348             cp_lexer_consume_token (parser->lexer);
4349             /* Parse the cast-expression.  */
4350             expression = cp_parser_simple_cast_expression (parser);
4351             /* Create the complete representation.  */
4352             return build_x_unary_op ((keyword == RID_REALPART
4353                                       ? REALPART_EXPR : IMAGPART_EXPR),
4354                                      expression);
4355           }
4356           break;
4357
4358         default:
4359           break;
4360         }
4361     }
4362
4363   /* Look for the `:: new' and `:: delete', which also signal the
4364      beginning of a new-expression, or delete-expression,
4365      respectively.  If the next token is `::', then it might be one of
4366      these.  */
4367   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4368     {
4369       enum rid keyword;
4370
4371       /* See if the token after the `::' is one of the keywords in
4372          which we're interested.  */
4373       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4374       /* If it's `new', we have a new-expression.  */
4375       if (keyword == RID_NEW)
4376         return cp_parser_new_expression (parser);
4377       /* Similarly, for `delete'.  */
4378       else if (keyword == RID_DELETE)
4379         return cp_parser_delete_expression (parser);
4380     }
4381
4382   /* Look for a unary operator.  */
4383   unary_operator = cp_parser_unary_operator (token);
4384   /* The `++' and `--' operators can be handled similarly, even though
4385      they are not technically unary-operators in the grammar.  */
4386   if (unary_operator == ERROR_MARK)
4387     {
4388       if (token->type == CPP_PLUS_PLUS)
4389         unary_operator = PREINCREMENT_EXPR;
4390       else if (token->type == CPP_MINUS_MINUS)
4391         unary_operator = PREDECREMENT_EXPR;
4392       /* Handle the GNU address-of-label extension.  */
4393       else if (cp_parser_allow_gnu_extensions_p (parser)
4394                && token->type == CPP_AND_AND)
4395         {
4396           tree identifier;
4397
4398           /* Consume the '&&' token.  */
4399           cp_lexer_consume_token (parser->lexer);
4400           /* Look for the identifier.  */
4401           identifier = cp_parser_identifier (parser);
4402           /* Create an expression representing the address.  */
4403           return finish_label_address_expr (identifier);
4404         }
4405     }
4406   if (unary_operator != ERROR_MARK)
4407     {
4408       tree cast_expression;
4409       tree expression = error_mark_node;
4410       const char *non_constant_p = NULL;
4411
4412       /* Consume the operator token.  */
4413       token = cp_lexer_consume_token (parser->lexer);
4414       /* Parse the cast-expression.  */
4415       cast_expression
4416         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4417       /* Now, build an appropriate representation.  */
4418       switch (unary_operator)
4419         {
4420         case INDIRECT_REF:
4421           non_constant_p = "`*'";
4422           expression = build_x_indirect_ref (cast_expression, "unary *");
4423           break;
4424
4425         case ADDR_EXPR:
4426           /* The "&" operator is allowed in the implementation of
4427              "offsetof".  */
4428           if (!parser->in_offsetof_p)
4429             non_constant_p = "`&'";
4430           /* Fall through.  */
4431         case BIT_NOT_EXPR:
4432           expression = build_x_unary_op (unary_operator, cast_expression);
4433           break;
4434
4435         case PREINCREMENT_EXPR:
4436         case PREDECREMENT_EXPR:
4437           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4438                             ? "`++'" : "`--'");
4439           /* Fall through.  */
4440         case CONVERT_EXPR:
4441         case NEGATE_EXPR:
4442         case TRUTH_NOT_EXPR:
4443           expression = finish_unary_op_expr (unary_operator, cast_expression);
4444           break;
4445
4446         default:
4447           abort ();
4448         }
4449
4450       if (non_constant_p && parser->integral_constant_expression_p)
4451         {
4452           if (!parser->allow_non_integral_constant_expression_p)
4453             return cp_parser_non_integral_constant_expression (non_constant_p);
4454           parser->non_integral_constant_expression_p = true;
4455         }
4456
4457       return expression;
4458     }
4459
4460   return cp_parser_postfix_expression (parser, address_p);
4461 }
4462
4463 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4464    unary-operator, the corresponding tree code is returned.  */
4465
4466 static enum tree_code
4467 cp_parser_unary_operator (cp_token* token)
4468 {
4469   switch (token->type)
4470     {
4471     case CPP_MULT:
4472       return INDIRECT_REF;
4473
4474     case CPP_AND:
4475       return ADDR_EXPR;
4476
4477     case CPP_PLUS:
4478       return CONVERT_EXPR;
4479
4480     case CPP_MINUS:
4481       return NEGATE_EXPR;
4482
4483     case CPP_NOT:
4484       return TRUTH_NOT_EXPR;
4485
4486     case CPP_COMPL:
4487       return BIT_NOT_EXPR;
4488
4489     default:
4490       return ERROR_MARK;
4491     }
4492 }
4493
4494 /* Parse a new-expression.
4495
4496    new-expression:
4497      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4498      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4499
4500    Returns a representation of the expression.  */
4501
4502 static tree
4503 cp_parser_new_expression (cp_parser* parser)
4504 {
4505   bool global_scope_p;
4506   tree placement;
4507   tree type;
4508   tree initializer;
4509
4510   /* Look for the optional `::' operator.  */
4511   global_scope_p
4512     = (cp_parser_global_scope_opt (parser,
4513                                    /*current_scope_valid_p=*/false)
4514        != NULL_TREE);
4515   /* Look for the `new' operator.  */
4516   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4517   /* There's no easy way to tell a new-placement from the
4518      `( type-id )' construct.  */
4519   cp_parser_parse_tentatively (parser);
4520   /* Look for a new-placement.  */
4521   placement = cp_parser_new_placement (parser);
4522   /* If that didn't work out, there's no new-placement.  */
4523   if (!cp_parser_parse_definitely (parser))
4524     placement = NULL_TREE;
4525
4526   /* If the next token is a `(', then we have a parenthesized
4527      type-id.  */
4528   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4529     {
4530       /* Consume the `('.  */
4531       cp_lexer_consume_token (parser->lexer);
4532       /* Parse the type-id.  */
4533       type = cp_parser_type_id (parser);
4534       /* Look for the closing `)'.  */
4535       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4536       /* There should not be a direct-new-declarator in this production, 
4537          but GCC used to allowed this, so we check and emit a sensible error
4538          message for this case.  */
4539       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4540         {\r
4541           error ("array bound forbidden after parenthesized type-id");\r
4542           inform ("try removing the parentheses around the type-id");\r
4543           cp_parser_direct_new_declarator (parser);
4544         }
4545     }
4546   /* Otherwise, there must be a new-type-id.  */
4547   else
4548     type = cp_parser_new_type_id (parser);
4549
4550   /* If the next token is a `(', then we have a new-initializer.  */
4551   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4552     initializer = cp_parser_new_initializer (parser);
4553   else
4554     initializer = NULL_TREE;
4555
4556   /* Create a representation of the new-expression.  */
4557   return build_new (placement, type, initializer, global_scope_p);
4558 }
4559
4560 /* Parse a new-placement.
4561
4562    new-placement:
4563      ( expression-list )
4564
4565    Returns the same representation as for an expression-list.  */
4566
4567 static tree
4568 cp_parser_new_placement (cp_parser* parser)
4569 {
4570   tree expression_list;
4571
4572   /* Parse the expression-list.  */
4573   expression_list = (cp_parser_parenthesized_expression_list
4574                      (parser, false, /*non_constant_p=*/NULL));
4575
4576   return expression_list;
4577 }
4578
4579 /* Parse a new-type-id.
4580
4581    new-type-id:
4582      type-specifier-seq new-declarator [opt]
4583
4584    Returns a TREE_LIST whose TREE_PURPOSE is the type-specifier-seq,
4585    and whose TREE_VALUE is the new-declarator.  */
4586
4587 static tree
4588 cp_parser_new_type_id (cp_parser* parser)
4589 {
4590   tree type_specifier_seq;
4591   tree declarator;
4592   const char *saved_message;
4593
4594   /* The type-specifier sequence must not contain type definitions.
4595      (It cannot contain declarations of new types either, but if they
4596      are not definitions we will catch that because they are not
4597      complete.)  */
4598   saved_message = parser->type_definition_forbidden_message;
4599   parser->type_definition_forbidden_message
4600     = "types may not be defined in a new-type-id";
4601   /* Parse the type-specifier-seq.  */
4602   type_specifier_seq = cp_parser_type_specifier_seq (parser);
4603   /* Restore the old message.  */
4604   parser->type_definition_forbidden_message = saved_message;
4605   /* Parse the new-declarator.  */
4606   declarator = cp_parser_new_declarator_opt (parser);
4607
4608   return build_tree_list (type_specifier_seq, declarator);
4609 }
4610
4611 /* Parse an (optional) new-declarator.
4612
4613    new-declarator:
4614      ptr-operator new-declarator [opt]
4615      direct-new-declarator
4616
4617    Returns a representation of the declarator.  See
4618    cp_parser_declarator for the representations used.  */
4619
4620 static tree
4621 cp_parser_new_declarator_opt (cp_parser* parser)
4622 {
4623   enum tree_code code;
4624   tree type;
4625   tree cv_qualifier_seq;
4626
4627   /* We don't know if there's a ptr-operator next, or not.  */
4628   cp_parser_parse_tentatively (parser);
4629   /* Look for a ptr-operator.  */
4630   code = cp_parser_ptr_operator (parser, &type, &cv_qualifier_seq);
4631   /* If that worked, look for more new-declarators.  */
4632   if (cp_parser_parse_definitely (parser))
4633     {
4634       tree declarator;
4635
4636       /* Parse another optional declarator.  */
4637       declarator = cp_parser_new_declarator_opt (parser);
4638
4639       /* Create the representation of the declarator.  */
4640       if (code == INDIRECT_REF)
4641         declarator = make_pointer_declarator (cv_qualifier_seq,
4642                                               declarator);
4643       else
4644         declarator = make_reference_declarator (cv_qualifier_seq,
4645                                                 declarator);
4646
4647      /* Handle the pointer-to-member case.  */
4648      if (type)
4649        declarator = build_nt (SCOPE_REF, type, declarator);
4650
4651       return declarator;
4652     }
4653
4654   /* If the next token is a `[', there is a direct-new-declarator.  */
4655   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4656     return cp_parser_direct_new_declarator (parser);
4657
4658   return NULL_TREE;
4659 }
4660
4661 /* Parse a direct-new-declarator.
4662
4663    direct-new-declarator:
4664      [ expression ]
4665      direct-new-declarator [constant-expression]
4666
4667    Returns an ARRAY_REF, following the same conventions as are
4668    documented for cp_parser_direct_declarator.  */
4669
4670 static tree
4671 cp_parser_direct_new_declarator (cp_parser* parser)
4672 {
4673   tree declarator = NULL_TREE;
4674
4675   while (true)
4676     {
4677       tree expression;
4678
4679       /* Look for the opening `['.  */
4680       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4681       /* The first expression is not required to be constant.  */
4682       if (!declarator)
4683         {
4684           expression = cp_parser_expression (parser);
4685           /* The standard requires that the expression have integral
4686              type.  DR 74 adds enumeration types.  We believe that the
4687              real intent is that these expressions be handled like the
4688              expression in a `switch' condition, which also allows
4689              classes with a single conversion to integral or
4690              enumeration type.  */
4691           if (!processing_template_decl)
4692             {
4693               expression
4694                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
4695                                               expression,
4696                                               /*complain=*/true);
4697               if (!expression)
4698                 {
4699                   error ("expression in new-declarator must have integral or enumeration type");
4700                   expression = error_mark_node;
4701                 }
4702             }
4703         }
4704       /* But all the other expressions must be.  */
4705       else
4706         expression
4707           = cp_parser_constant_expression (parser,
4708                                            /*allow_non_constant=*/false,
4709                                            NULL);
4710       /* Look for the closing `]'.  */
4711       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4712
4713       /* Add this bound to the declarator.  */
4714       declarator = build_nt (ARRAY_REF, declarator, expression);
4715
4716       /* If the next token is not a `[', then there are no more
4717          bounds.  */
4718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
4719         break;
4720     }
4721
4722   return declarator;
4723 }
4724
4725 /* Parse a new-initializer.
4726
4727    new-initializer:
4728      ( expression-list [opt] )
4729
4730    Returns a representation of the expression-list.  If there is no
4731    expression-list, VOID_ZERO_NODE is returned.  */
4732
4733 static tree
4734 cp_parser_new_initializer (cp_parser* parser)
4735 {
4736   tree expression_list;
4737
4738   expression_list = (cp_parser_parenthesized_expression_list
4739                      (parser, false, /*non_constant_p=*/NULL));
4740   if (!expression_list)
4741     expression_list = void_zero_node;
4742
4743   return expression_list;
4744 }
4745
4746 /* Parse a delete-expression.
4747
4748    delete-expression:
4749      :: [opt] delete cast-expression
4750      :: [opt] delete [ ] cast-expression
4751
4752    Returns a representation of the expression.  */
4753
4754 static tree
4755 cp_parser_delete_expression (cp_parser* parser)
4756 {
4757   bool global_scope_p;
4758   bool array_p;
4759   tree expression;
4760
4761   /* Look for the optional `::' operator.  */
4762   global_scope_p
4763     = (cp_parser_global_scope_opt (parser,
4764                                    /*current_scope_valid_p=*/false)
4765        != NULL_TREE);
4766   /* Look for the `delete' keyword.  */
4767   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
4768   /* See if the array syntax is in use.  */
4769   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4770     {
4771       /* Consume the `[' token.  */
4772       cp_lexer_consume_token (parser->lexer);
4773       /* Look for the `]' token.  */
4774       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4775       /* Remember that this is the `[]' construct.  */
4776       array_p = true;
4777     }
4778   else
4779     array_p = false;
4780
4781   /* Parse the cast-expression.  */
4782   expression = cp_parser_simple_cast_expression (parser);
4783
4784   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
4785 }
4786
4787 /* Parse a cast-expression.
4788
4789    cast-expression:
4790      unary-expression
4791      ( type-id ) cast-expression
4792
4793    Returns a representation of the expression.  */
4794
4795 static tree
4796 cp_parser_cast_expression (cp_parser *parser, bool address_p)
4797 {
4798   /* If it's a `(', then we might be looking at a cast.  */
4799   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4800     {
4801       tree type = NULL_TREE;
4802       tree expr = NULL_TREE;
4803       bool compound_literal_p;
4804       const char *saved_message;
4805
4806       /* There's no way to know yet whether or not this is a cast.
4807          For example, `(int (3))' is a unary-expression, while `(int)
4808          3' is a cast.  So, we resort to parsing tentatively.  */
4809       cp_parser_parse_tentatively (parser);
4810       /* Types may not be defined in a cast.  */
4811       saved_message = parser->type_definition_forbidden_message;
4812       parser->type_definition_forbidden_message
4813         = "types may not be defined in casts";
4814       /* Consume the `('.  */
4815       cp_lexer_consume_token (parser->lexer);
4816       /* A very tricky bit is that `(struct S) { 3 }' is a
4817          compound-literal (which we permit in C++ as an extension).
4818          But, that construct is not a cast-expression -- it is a
4819          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
4820          is legal; if the compound-literal were a cast-expression,
4821          you'd need an extra set of parentheses.)  But, if we parse
4822          the type-id, and it happens to be a class-specifier, then we
4823          will commit to the parse at that point, because we cannot
4824          undo the action that is done when creating a new class.  So,
4825          then we cannot back up and do a postfix-expression.
4826
4827          Therefore, we scan ahead to the closing `)', and check to see
4828          if the token after the `)' is a `{'.  If so, we are not
4829          looking at a cast-expression.
4830
4831          Save tokens so that we can put them back.  */
4832       cp_lexer_save_tokens (parser->lexer);
4833       /* Skip tokens until the next token is a closing parenthesis.
4834          If we find the closing `)', and the next token is a `{', then
4835          we are looking at a compound-literal.  */
4836       compound_literal_p
4837         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
4838                                                   /*consume_paren=*/true)
4839            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
4840       /* Roll back the tokens we skipped.  */
4841       cp_lexer_rollback_tokens (parser->lexer);
4842       /* If we were looking at a compound-literal, simulate an error
4843          so that the call to cp_parser_parse_definitely below will
4844          fail.  */
4845       if (compound_literal_p)
4846         cp_parser_simulate_error (parser);
4847       else
4848         {
4849           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4850           parser->in_type_id_in_expr_p = true;
4851           /* Look for the type-id.  */
4852           type = cp_parser_type_id (parser);
4853           /* Look for the closing `)'.  */
4854           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4855           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4856         }
4857
4858       /* Restore the saved message.  */
4859       parser->type_definition_forbidden_message = saved_message;
4860
4861       /* If ok so far, parse the dependent expression. We cannot be
4862          sure it is a cast. Consider `(T ())'.  It is a parenthesized
4863          ctor of T, but looks like a cast to function returning T
4864          without a dependent expression.  */
4865       if (!cp_parser_error_occurred (parser))
4866         expr = cp_parser_simple_cast_expression (parser);
4867
4868       if (cp_parser_parse_definitely (parser))
4869         {
4870           /* Warn about old-style casts, if so requested.  */
4871           if (warn_old_style_cast
4872               && !in_system_header
4873               && !VOID_TYPE_P (type)
4874               && current_lang_name != lang_name_c)
4875             warning ("use of old-style cast");
4876
4877           /* Only type conversions to integral or enumeration types
4878              can be used in constant-expressions.  */
4879           if (parser->integral_constant_expression_p
4880               && !dependent_type_p (type)
4881               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type))
4882             {
4883               if (!parser->allow_non_integral_constant_expression_p)
4884                 return (cp_parser_non_integral_constant_expression
4885                         ("a casts to a type other than an integral or "
4886                          "enumeration type"));
4887               parser->non_integral_constant_expression_p = true;
4888             }
4889           /* Perform the cast.  */
4890           expr = build_c_cast (type, expr);
4891           return expr;
4892         }
4893     }
4894
4895   /* If we get here, then it's not a cast, so it must be a
4896      unary-expression.  */
4897   return cp_parser_unary_expression (parser, address_p);
4898 }
4899
4900 /* Parse a pm-expression.
4901
4902    pm-expression:
4903      cast-expression
4904      pm-expression .* cast-expression
4905      pm-expression ->* cast-expression
4906
4907      Returns a representation of the expression.  */
4908
4909 static tree
4910 cp_parser_pm_expression (cp_parser* parser)
4911 {
4912   static const cp_parser_token_tree_map map = {
4913     { CPP_DEREF_STAR, MEMBER_REF },
4914     { CPP_DOT_STAR, DOTSTAR_EXPR },
4915     { CPP_EOF, ERROR_MARK }
4916   };
4917
4918   return cp_parser_binary_expression (parser, map,
4919                                       cp_parser_simple_cast_expression);
4920 }
4921
4922 /* Parse a multiplicative-expression.
4923
4924    multiplicative-expression:
4925      pm-expression
4926      multiplicative-expression * pm-expression
4927      multiplicative-expression / pm-expression
4928      multiplicative-expression % pm-expression
4929
4930    Returns a representation of the expression.  */
4931
4932 static tree
4933 cp_parser_multiplicative_expression (cp_parser* parser)
4934 {
4935   static const cp_parser_token_tree_map map = {
4936     { CPP_MULT, MULT_EXPR },
4937     { CPP_DIV, TRUNC_DIV_EXPR },
4938     { CPP_MOD, TRUNC_MOD_EXPR },
4939     { CPP_EOF, ERROR_MARK }
4940   };
4941
4942   return cp_parser_binary_expression (parser,
4943                                       map,
4944                                       cp_parser_pm_expression);
4945 }
4946
4947 /* Parse an additive-expression.
4948
4949    additive-expression:
4950      multiplicative-expression
4951      additive-expression + multiplicative-expression
4952      additive-expression - multiplicative-expression
4953
4954    Returns a representation of the expression.  */
4955
4956 static tree
4957 cp_parser_additive_expression (cp_parser* parser)
4958 {
4959   static const cp_parser_token_tree_map map = {
4960     { CPP_PLUS, PLUS_EXPR },
4961     { CPP_MINUS, MINUS_EXPR },
4962     { CPP_EOF, ERROR_MARK }
4963   };
4964
4965   return cp_parser_binary_expression (parser,
4966                                       map,
4967                                       cp_parser_multiplicative_expression);
4968 }
4969
4970 /* Parse a shift-expression.
4971
4972    shift-expression:
4973      additive-expression
4974      shift-expression << additive-expression
4975      shift-expression >> additive-expression
4976
4977    Returns a representation of the expression.  */
4978
4979 static tree
4980 cp_parser_shift_expression (cp_parser* parser)
4981 {
4982   static const cp_parser_token_tree_map map = {
4983     { CPP_LSHIFT, LSHIFT_EXPR },
4984     { CPP_RSHIFT, RSHIFT_EXPR },
4985     { CPP_EOF, ERROR_MARK }
4986   };
4987
4988   return cp_parser_binary_expression (parser,
4989                                       map,
4990                                       cp_parser_additive_expression);
4991 }
4992
4993 /* Parse a relational-expression.
4994
4995    relational-expression:
4996      shift-expression
4997      relational-expression < shift-expression
4998      relational-expression > shift-expression
4999      relational-expression <= shift-expression
5000      relational-expression >= shift-expression
5001
5002    GNU Extension:
5003
5004    relational-expression:
5005      relational-expression <? shift-expression
5006      relational-expression >? shift-expression
5007
5008    Returns a representation of the expression.  */
5009
5010 static tree
5011 cp_parser_relational_expression (cp_parser* parser)
5012 {
5013   static const cp_parser_token_tree_map map = {
5014     { CPP_LESS, LT_EXPR },
5015     { CPP_GREATER, GT_EXPR },
5016     { CPP_LESS_EQ, LE_EXPR },
5017     { CPP_GREATER_EQ, GE_EXPR },
5018     { CPP_MIN, MIN_EXPR },
5019     { CPP_MAX, MAX_EXPR },
5020     { CPP_EOF, ERROR_MARK }
5021   };
5022
5023   return cp_parser_binary_expression (parser,
5024                                       map,
5025                                       cp_parser_shift_expression);
5026 }
5027
5028 /* Parse an equality-expression.
5029
5030    equality-expression:
5031      relational-expression
5032      equality-expression == relational-expression
5033      equality-expression != relational-expression
5034
5035    Returns a representation of the expression.  */
5036
5037 static tree
5038 cp_parser_equality_expression (cp_parser* parser)
5039 {
5040   static const cp_parser_token_tree_map map = {
5041     { CPP_EQ_EQ, EQ_EXPR },
5042     { CPP_NOT_EQ, NE_EXPR },
5043     { CPP_EOF, ERROR_MARK }
5044   };
5045
5046   return cp_parser_binary_expression (parser,
5047                                       map,
5048                                       cp_parser_relational_expression);
5049 }
5050
5051 /* Parse an and-expression.
5052
5053    and-expression:
5054      equality-expression
5055      and-expression & equality-expression
5056
5057    Returns a representation of the expression.  */
5058
5059 static tree
5060 cp_parser_and_expression (cp_parser* parser)
5061 {
5062   static const cp_parser_token_tree_map map = {
5063     { CPP_AND, BIT_AND_EXPR },
5064     { CPP_EOF, ERROR_MARK }
5065   };
5066
5067   return cp_parser_binary_expression (parser,
5068                                       map,
5069                                       cp_parser_equality_expression);
5070 }
5071
5072 /* Parse an exclusive-or-expression.
5073
5074    exclusive-or-expression:
5075      and-expression
5076      exclusive-or-expression ^ and-expression
5077
5078    Returns a representation of the expression.  */
5079
5080 static tree
5081 cp_parser_exclusive_or_expression (cp_parser* parser)
5082 {
5083   static const cp_parser_token_tree_map map = {
5084     { CPP_XOR, BIT_XOR_EXPR },
5085     { CPP_EOF, ERROR_MARK }
5086   };
5087
5088   return cp_parser_binary_expression (parser,
5089                                       map,
5090                                       cp_parser_and_expression);
5091 }
5092
5093
5094 /* Parse an inclusive-or-expression.
5095
5096    inclusive-or-expression:
5097      exclusive-or-expression
5098      inclusive-or-expression | exclusive-or-expression
5099
5100    Returns a representation of the expression.  */
5101
5102 static tree
5103 cp_parser_inclusive_or_expression (cp_parser* parser)
5104 {
5105   static const cp_parser_token_tree_map map = {
5106     { CPP_OR, BIT_IOR_EXPR },
5107     { CPP_EOF, ERROR_MARK }
5108   };
5109
5110   return cp_parser_binary_expression (parser,
5111                                       map,
5112                                       cp_parser_exclusive_or_expression);
5113 }
5114
5115 /* Parse a logical-and-expression.
5116
5117    logical-and-expression:
5118      inclusive-or-expression
5119      logical-and-expression && inclusive-or-expression
5120
5121    Returns a representation of the expression.  */
5122
5123 static tree
5124 cp_parser_logical_and_expression (cp_parser* parser)
5125 {
5126   static const cp_parser_token_tree_map map = {
5127     { CPP_AND_AND, TRUTH_ANDIF_EXPR },
5128     { CPP_EOF, ERROR_MARK }
5129   };
5130
5131   return cp_parser_binary_expression (parser,
5132                                       map,
5133                                       cp_parser_inclusive_or_expression);
5134 }
5135
5136 /* Parse a logical-or-expression.
5137
5138    logical-or-expression:
5139      logical-and-expression
5140      logical-or-expression || logical-and-expression
5141
5142    Returns a representation of the expression.  */
5143
5144 static tree
5145 cp_parser_logical_or_expression (cp_parser* parser)
5146 {
5147   static const cp_parser_token_tree_map map = {
5148     { CPP_OR_OR, TRUTH_ORIF_EXPR },
5149     { CPP_EOF, ERROR_MARK }
5150   };
5151
5152   return cp_parser_binary_expression (parser,
5153                                       map,
5154                                       cp_parser_logical_and_expression);
5155 }
5156
5157 /* Parse the `? expression : assignment-expression' part of a
5158    conditional-expression.  The LOGICAL_OR_EXPR is the
5159    logical-or-expression that started the conditional-expression.
5160    Returns a representation of the entire conditional-expression.
5161
5162    This routine is used by cp_parser_assignment_expression.
5163
5164      ? expression : assignment-expression
5165
5166    GNU Extensions:
5167
5168      ? : assignment-expression */
5169
5170 static tree
5171 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5172 {
5173   tree expr;
5174   tree assignment_expr;
5175
5176   /* Consume the `?' token.  */
5177   cp_lexer_consume_token (parser->lexer);
5178   if (cp_parser_allow_gnu_extensions_p (parser)
5179       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5180     /* Implicit true clause.  */
5181     expr = NULL_TREE;
5182   else
5183     /* Parse the expression.  */
5184     expr = cp_parser_expression (parser);
5185
5186   /* The next token should be a `:'.  */
5187   cp_parser_require (parser, CPP_COLON, "`:'");
5188   /* Parse the assignment-expression.  */
5189   assignment_expr = cp_parser_assignment_expression (parser);
5190
5191   /* Build the conditional-expression.  */
5192   return build_x_conditional_expr (logical_or_expr,
5193                                    expr,
5194                                    assignment_expr);
5195 }
5196
5197 /* Parse an assignment-expression.
5198
5199    assignment-expression:
5200      conditional-expression
5201      logical-or-expression assignment-operator assignment_expression
5202      throw-expression
5203
5204    Returns a representation for the expression.  */
5205
5206 static tree
5207 cp_parser_assignment_expression (cp_parser* parser)
5208 {
5209   tree expr;
5210
5211   /* If the next token is the `throw' keyword, then we're looking at
5212      a throw-expression.  */
5213   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5214     expr = cp_parser_throw_expression (parser);
5215   /* Otherwise, it must be that we are looking at a
5216      logical-or-expression.  */
5217   else
5218     {
5219       /* Parse the logical-or-expression.  */
5220       expr = cp_parser_logical_or_expression (parser);
5221       /* If the next token is a `?' then we're actually looking at a
5222          conditional-expression.  */
5223       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5224         return cp_parser_question_colon_clause (parser, expr);
5225       else
5226         {
5227           enum tree_code assignment_operator;
5228
5229           /* If it's an assignment-operator, we're using the second
5230              production.  */
5231           assignment_operator
5232             = cp_parser_assignment_operator_opt (parser);
5233           if (assignment_operator != ERROR_MARK)
5234             {
5235               tree rhs;
5236
5237               /* Parse the right-hand side of the assignment.  */
5238               rhs = cp_parser_assignment_expression (parser);
5239               /* An assignment may not appear in a
5240                  constant-expression.  */
5241               if (parser->integral_constant_expression_p)
5242                 {
5243                   if (!parser->allow_non_integral_constant_expression_p)
5244                     return cp_parser_non_integral_constant_expression ("an assignment");
5245                   parser->non_integral_constant_expression_p = true;
5246                 }
5247               /* Build the assignment expression.  */
5248               expr = build_x_modify_expr (expr,
5249                                           assignment_operator,
5250                                           rhs);
5251             }
5252         }
5253     }
5254
5255   return expr;
5256 }
5257
5258 /* Parse an (optional) assignment-operator.
5259
5260    assignment-operator: one of
5261      = *= /= %= += -= >>= <<= &= ^= |=
5262
5263    GNU Extension:
5264
5265    assignment-operator: one of
5266      <?= >?=
5267
5268    If the next token is an assignment operator, the corresponding tree
5269    code is returned, and the token is consumed.  For example, for
5270    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5271    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5272    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5273    operator, ERROR_MARK is returned.  */
5274
5275 static enum tree_code
5276 cp_parser_assignment_operator_opt (cp_parser* parser)
5277 {
5278   enum tree_code op;
5279   cp_token *token;
5280
5281   /* Peek at the next toen.  */
5282   token = cp_lexer_peek_token (parser->lexer);
5283
5284   switch (token->type)
5285     {
5286     case CPP_EQ:
5287       op = NOP_EXPR;
5288       break;
5289
5290     case CPP_MULT_EQ:
5291       op = MULT_EXPR;
5292       break;
5293
5294     case CPP_DIV_EQ:
5295       op = TRUNC_DIV_EXPR;
5296       break;
5297
5298     case CPP_MOD_EQ:
5299       op = TRUNC_MOD_EXPR;
5300       break;
5301
5302     case CPP_PLUS_EQ:
5303       op = PLUS_EXPR;
5304       break;
5305
5306     case CPP_MINUS_EQ:
5307       op = MINUS_EXPR;
5308       break;
5309
5310     case CPP_RSHIFT_EQ:
5311       op = RSHIFT_EXPR;
5312       break;
5313
5314     case CPP_LSHIFT_EQ:
5315       op = LSHIFT_EXPR;
5316       break;
5317
5318     case CPP_AND_EQ:
5319       op = BIT_AND_EXPR;
5320       break;
5321
5322     case CPP_XOR_EQ:
5323       op = BIT_XOR_EXPR;
5324       break;
5325
5326     case CPP_OR_EQ:
5327       op = BIT_IOR_EXPR;
5328       break;
5329
5330     case CPP_MIN_EQ:
5331       op = MIN_EXPR;
5332       break;
5333
5334     case CPP_MAX_EQ:
5335       op = MAX_EXPR;
5336       break;
5337
5338     default:
5339       /* Nothing else is an assignment operator.  */
5340       op = ERROR_MARK;
5341     }
5342
5343   /* If it was an assignment operator, consume it.  */
5344   if (op != ERROR_MARK)
5345     cp_lexer_consume_token (parser->lexer);
5346
5347   return op;
5348 }
5349
5350 /* Parse an expression.
5351
5352    expression:
5353      assignment-expression
5354      expression , assignment-expression
5355
5356    Returns a representation of the expression.  */
5357
5358 static tree
5359 cp_parser_expression (cp_parser* parser)
5360 {
5361   tree expression = NULL_TREE;
5362
5363   while (true)
5364     {
5365       tree assignment_expression;
5366
5367       /* Parse the next assignment-expression.  */
5368       assignment_expression
5369         = cp_parser_assignment_expression (parser);
5370       /* If this is the first assignment-expression, we can just
5371          save it away.  */
5372       if (!expression)
5373         expression = assignment_expression;
5374       else
5375         expression = build_x_compound_expr (expression,
5376                                             assignment_expression);
5377       /* If the next token is not a comma, then we are done with the
5378          expression.  */
5379       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5380         break;
5381       /* Consume the `,'.  */
5382       cp_lexer_consume_token (parser->lexer);
5383       /* A comma operator cannot appear in a constant-expression.  */
5384       if (parser->integral_constant_expression_p)
5385         {
5386           if (!parser->allow_non_integral_constant_expression_p)
5387             expression
5388               = cp_parser_non_integral_constant_expression ("a comma operator");
5389           parser->non_integral_constant_expression_p = true;
5390         }
5391     }
5392
5393   return expression;
5394 }
5395
5396 /* Parse a constant-expression.
5397
5398    constant-expression:
5399      conditional-expression
5400
5401   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5402   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5403   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5404   is false, NON_CONSTANT_P should be NULL.  */
5405
5406 static tree
5407 cp_parser_constant_expression (cp_parser* parser,
5408                                bool allow_non_constant_p,
5409                                bool *non_constant_p)
5410 {
5411   bool saved_integral_constant_expression_p;
5412   bool saved_allow_non_integral_constant_expression_p;
5413   bool saved_non_integral_constant_expression_p;
5414   tree expression;
5415
5416   /* It might seem that we could simply parse the
5417      conditional-expression, and then check to see if it were
5418      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5419      one that the compiler can figure out is constant, possibly after
5420      doing some simplifications or optimizations.  The standard has a
5421      precise definition of constant-expression, and we must honor
5422      that, even though it is somewhat more restrictive.
5423
5424      For example:
5425
5426        int i[(2, 3)];
5427
5428      is not a legal declaration, because `(2, 3)' is not a
5429      constant-expression.  The `,' operator is forbidden in a
5430      constant-expression.  However, GCC's constant-folding machinery
5431      will fold this operation to an INTEGER_CST for `3'.  */
5432
5433   /* Save the old settings.  */
5434   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5435   saved_allow_non_integral_constant_expression_p
5436     = parser->allow_non_integral_constant_expression_p;
5437   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5438   /* We are now parsing a constant-expression.  */
5439   parser->integral_constant_expression_p = true;
5440   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5441   parser->non_integral_constant_expression_p = false;
5442   /* Although the grammar says "conditional-expression", we parse an
5443      "assignment-expression", which also permits "throw-expression"
5444      and the use of assignment operators.  In the case that
5445      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5446      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5447      actually essential that we look for an assignment-expression.
5448      For example, cp_parser_initializer_clauses uses this function to
5449      determine whether a particular assignment-expression is in fact
5450      constant.  */
5451   expression = cp_parser_assignment_expression (parser);
5452   /* Restore the old settings.  */
5453   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5454   parser->allow_non_integral_constant_expression_p
5455     = saved_allow_non_integral_constant_expression_p;
5456   if (allow_non_constant_p)
5457     *non_constant_p = parser->non_integral_constant_expression_p;
5458   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5459
5460   return expression;
5461 }
5462
5463 /* Statements [gram.stmt.stmt]  */
5464
5465 /* Parse a statement.
5466
5467    statement:
5468      labeled-statement
5469      expression-statement
5470      compound-statement
5471      selection-statement
5472      iteration-statement
5473      jump-statement
5474      declaration-statement
5475      try-block  */
5476
5477 static void
5478 cp_parser_statement (cp_parser* parser, bool in_statement_expr_p)
5479 {
5480   tree statement;
5481   cp_token *token;
5482   int statement_line_number;
5483
5484   /* There is no statement yet.  */
5485   statement = NULL_TREE;
5486   /* Peek at the next token.  */
5487   token = cp_lexer_peek_token (parser->lexer);
5488   /* Remember the line number of the first token in the statement.  */
5489   statement_line_number = token->location.line;
5490   /* If this is a keyword, then that will often determine what kind of
5491      statement we have.  */
5492   if (token->type == CPP_KEYWORD)
5493     {
5494       enum rid keyword = token->keyword;
5495
5496       switch (keyword)
5497         {
5498         case RID_CASE:
5499         case RID_DEFAULT:
5500           statement = cp_parser_labeled_statement (parser,
5501                                                    in_statement_expr_p);
5502           break;
5503
5504         case RID_IF:
5505         case RID_SWITCH:
5506           statement = cp_parser_selection_statement (parser);
5507           break;
5508
5509         case RID_WHILE:
5510         case RID_DO:
5511         case RID_FOR:
5512           statement = cp_parser_iteration_statement (parser);
5513           break;
5514
5515         case RID_BREAK:
5516         case RID_CONTINUE:
5517         case RID_RETURN:
5518         case RID_GOTO:
5519           statement = cp_parser_jump_statement (parser);
5520           break;
5521
5522         case RID_TRY:
5523           statement = cp_parser_try_block (parser);
5524           break;
5525
5526         default:
5527           /* It might be a keyword like `int' that can start a
5528              declaration-statement.  */
5529           break;
5530         }
5531     }
5532   else if (token->type == CPP_NAME)
5533     {
5534       /* If the next token is a `:', then we are looking at a
5535          labeled-statement.  */
5536       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5537       if (token->type == CPP_COLON)
5538         statement = cp_parser_labeled_statement (parser, in_statement_expr_p);
5539     }
5540   /* Anything that starts with a `{' must be a compound-statement.  */
5541   else if (token->type == CPP_OPEN_BRACE)
5542     statement = cp_parser_compound_statement (parser, false);
5543
5544   /* Everything else must be a declaration-statement or an
5545      expression-statement.  Try for the declaration-statement
5546      first, unless we are looking at a `;', in which case we know that
5547      we have an expression-statement.  */
5548   if (!statement)
5549     {
5550       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5551         {
5552           cp_parser_parse_tentatively (parser);
5553           /* Try to parse the declaration-statement.  */
5554           cp_parser_declaration_statement (parser);
5555           /* If that worked, we're done.  */
5556           if (cp_parser_parse_definitely (parser))
5557             return;
5558         }
5559       /* Look for an expression-statement instead.  */
5560       statement = cp_parser_expression_statement (parser, in_statement_expr_p);
5561     }
5562
5563   /* Set the line number for the statement.  */
5564   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5565     STMT_LINENO (statement) = statement_line_number;
5566 }
5567
5568 /* Parse a labeled-statement.
5569
5570    labeled-statement:
5571      identifier : statement
5572      case constant-expression : statement
5573      default : statement
5574
5575    GNU Extension:
5576
5577    labeled-statement:
5578      case constant-expression ... constant-expression : statement
5579
5580    Returns the new CASE_LABEL, for a `case' or `default' label.  For
5581    an ordinary label, returns a LABEL_STMT.  */
5582
5583 static tree
5584 cp_parser_labeled_statement (cp_parser* parser, bool in_statement_expr_p)
5585 {
5586   cp_token *token;
5587   tree statement = error_mark_node;
5588
5589   /* The next token should be an identifier.  */
5590   token = cp_lexer_peek_token (parser->lexer);
5591   if (token->type != CPP_NAME
5592       && token->type != CPP_KEYWORD)
5593     {
5594       cp_parser_error (parser, "expected labeled-statement");
5595       return error_mark_node;
5596     }
5597
5598   switch (token->keyword)
5599     {
5600     case RID_CASE:
5601       {
5602         tree expr, expr_hi;
5603         cp_token *ellipsis;
5604
5605         /* Consume the `case' token.  */
5606         cp_lexer_consume_token (parser->lexer);
5607         /* Parse the constant-expression.  */
5608         expr = cp_parser_constant_expression (parser,
5609                                               /*allow_non_constant_p=*/false,
5610                                               NULL);
5611
5612         ellipsis = cp_lexer_peek_token (parser->lexer);
5613         if (ellipsis->type == CPP_ELLIPSIS)
5614           {
5615             /* Consume the `...' token.  */
5616             cp_lexer_consume_token (parser->lexer);
5617             expr_hi =
5618               cp_parser_constant_expression (parser,
5619                                              /*allow_non_constant_p=*/false,
5620                                              NULL);
5621             /* We don't need to emit warnings here, as the common code
5622                will do this for us.  */
5623           }
5624         else
5625           expr_hi = NULL_TREE;
5626
5627         if (!parser->in_switch_statement_p)
5628           error ("case label `%E' not within a switch statement", expr);
5629         else
5630           statement = finish_case_label (expr, expr_hi);
5631       }
5632       break;
5633
5634     case RID_DEFAULT:
5635       /* Consume the `default' token.  */
5636       cp_lexer_consume_token (parser->lexer);
5637       if (!parser->in_switch_statement_p)
5638         error ("case label not within a switch statement");
5639       else
5640         statement = finish_case_label (NULL_TREE, NULL_TREE);
5641       break;
5642
5643     default:
5644       /* Anything else must be an ordinary label.  */
5645       statement = finish_label_stmt (cp_parser_identifier (parser));
5646       break;
5647     }
5648
5649   /* Require the `:' token.  */
5650   cp_parser_require (parser, CPP_COLON, "`:'");
5651   /* Parse the labeled statement.  */
5652   cp_parser_statement (parser, in_statement_expr_p);
5653
5654   /* Return the label, in the case of a `case' or `default' label.  */
5655   return statement;
5656 }
5657
5658 /* Parse an expression-statement.
5659
5660    expression-statement:
5661      expression [opt] ;
5662
5663    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5664    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5665    indicates whether this expression-statement is part of an
5666    expression statement.  */
5667
5668 static tree
5669 cp_parser_expression_statement (cp_parser* parser, bool in_statement_expr_p)
5670 {
5671   tree statement = NULL_TREE;
5672
5673   /* If the next token is a ';', then there is no expression
5674      statement.  */
5675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5676     statement = cp_parser_expression (parser);
5677
5678   /* Consume the final `;'.  */
5679   cp_parser_consume_semicolon_at_end_of_statement (parser);
5680
5681   if (in_statement_expr_p
5682       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
5683     {
5684       /* This is the final expression statement of a statement
5685          expression.  */
5686       statement = finish_stmt_expr_expr (statement);
5687     }
5688   else if (statement)
5689     statement = finish_expr_stmt (statement);
5690   else
5691     finish_stmt ();
5692
5693   return statement;
5694 }
5695
5696 /* Parse a compound-statement.
5697
5698    compound-statement:
5699      { statement-seq [opt] }
5700
5701    Returns a COMPOUND_STMT representing the statement.  */
5702
5703 static tree
5704 cp_parser_compound_statement (cp_parser *parser, bool in_statement_expr_p)
5705 {
5706   tree compound_stmt;
5707
5708   /* Consume the `{'.  */
5709   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
5710     return error_mark_node;
5711   /* Begin the compound-statement.  */
5712   compound_stmt = begin_compound_stmt (/*has_no_scope=*/false);
5713   /* Parse an (optional) statement-seq.  */
5714   cp_parser_statement_seq_opt (parser, in_statement_expr_p);
5715   /* Finish the compound-statement.  */
5716   finish_compound_stmt (compound_stmt);
5717   /* Consume the `}'.  */
5718   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
5719
5720   return compound_stmt;
5721 }
5722
5723 /* Parse an (optional) statement-seq.
5724
5725    statement-seq:
5726      statement
5727      statement-seq [opt] statement  */
5728
5729 static void
5730 cp_parser_statement_seq_opt (cp_parser* parser, bool in_statement_expr_p)
5731 {
5732   /* Scan statements until there aren't any more.  */
5733   while (true)
5734     {
5735       /* If we're looking at a `}', then we've run out of statements.  */
5736       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
5737           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
5738         break;
5739
5740       /* Parse the statement.  */
5741       cp_parser_statement (parser, in_statement_expr_p);
5742     }
5743 }
5744
5745 /* Parse a selection-statement.
5746
5747    selection-statement:
5748      if ( condition ) statement
5749      if ( condition ) statement else statement
5750      switch ( condition ) statement
5751
5752    Returns the new IF_STMT or SWITCH_STMT.  */
5753
5754 static tree
5755 cp_parser_selection_statement (cp_parser* parser)
5756 {
5757   cp_token *token;
5758   enum rid keyword;
5759
5760   /* Peek at the next token.  */
5761   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
5762
5763   /* See what kind of keyword it is.  */
5764   keyword = token->keyword;
5765   switch (keyword)
5766     {
5767     case RID_IF:
5768     case RID_SWITCH:
5769       {
5770         tree statement;
5771         tree condition;
5772
5773         /* Look for the `('.  */
5774         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
5775           {
5776             cp_parser_skip_to_end_of_statement (parser);
5777             return error_mark_node;
5778           }
5779
5780         /* Begin the selection-statement.  */
5781         if (keyword == RID_IF)
5782           statement = begin_if_stmt ();
5783         else
5784           statement = begin_switch_stmt ();
5785
5786         /* Parse the condition.  */
5787         condition = cp_parser_condition (parser);
5788         /* Look for the `)'.  */
5789         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
5790           cp_parser_skip_to_closing_parenthesis (parser, true, false,
5791                                                  /*consume_paren=*/true);
5792
5793         if (keyword == RID_IF)
5794           {
5795             tree then_stmt;
5796
5797             /* Add the condition.  */
5798             finish_if_stmt_cond (condition, statement);
5799
5800             /* Parse the then-clause.  */
5801             then_stmt = cp_parser_implicitly_scoped_statement (parser);
5802             finish_then_clause (statement);
5803
5804             /* If the next token is `else', parse the else-clause.  */
5805             if (cp_lexer_next_token_is_keyword (parser->lexer,
5806                                                 RID_ELSE))
5807               {
5808                 tree else_stmt;
5809
5810                 /* Consume the `else' keyword.  */
5811                 cp_lexer_consume_token (parser->lexer);
5812                 /* Parse the else-clause.  */
5813                 else_stmt
5814                   = cp_parser_implicitly_scoped_statement (parser);
5815                 finish_else_clause (statement);
5816               }
5817
5818             /* Now we're all done with the if-statement.  */
5819             finish_if_stmt ();
5820           }
5821         else
5822           {
5823             tree body;
5824             bool in_switch_statement_p;
5825
5826             /* Add the condition.  */
5827             finish_switch_cond (condition, statement);
5828
5829             /* Parse the body of the switch-statement.  */
5830             in_switch_statement_p = parser->in_switch_statement_p;
5831             parser->in_switch_statement_p = true;
5832             body = cp_parser_implicitly_scoped_statement (parser);
5833             parser->in_switch_statement_p = in_switch_statement_p;
5834
5835             /* Now we're all done with the switch-statement.  */
5836             finish_switch_stmt (statement);
5837           }
5838
5839         return statement;
5840       }
5841       break;
5842
5843     default:
5844       cp_parser_error (parser, "expected selection-statement");
5845       return error_mark_node;
5846     }
5847 }
5848
5849 /* Parse a condition.
5850
5851    condition:
5852      expression
5853      type-specifier-seq declarator = assignment-expression
5854
5855    GNU Extension:
5856
5857    condition:
5858      type-specifier-seq declarator asm-specification [opt]
5859        attributes [opt] = assignment-expression
5860
5861    Returns the expression that should be tested.  */
5862
5863 static tree
5864 cp_parser_condition (cp_parser* parser)
5865 {
5866   tree type_specifiers;
5867   const char *saved_message;
5868
5869   /* Try the declaration first.  */
5870   cp_parser_parse_tentatively (parser);
5871   /* New types are not allowed in the type-specifier-seq for a
5872      condition.  */
5873   saved_message = parser->type_definition_forbidden_message;
5874   parser->type_definition_forbidden_message
5875     = "types may not be defined in conditions";
5876   /* Parse the type-specifier-seq.  */
5877   type_specifiers = cp_parser_type_specifier_seq (parser);
5878   /* Restore the saved message.  */
5879   parser->type_definition_forbidden_message = saved_message;
5880   /* If all is well, we might be looking at a declaration.  */
5881   if (!cp_parser_error_occurred (parser))
5882     {
5883       tree decl;
5884       tree asm_specification;
5885       tree attributes;
5886       tree declarator;
5887       tree initializer = NULL_TREE;
5888
5889       /* Parse the declarator.  */
5890       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
5891                                          /*ctor_dtor_or_conv_p=*/NULL,
5892                                          /*parenthesized_p=*/NULL);
5893       /* Parse the attributes.  */
5894       attributes = cp_parser_attributes_opt (parser);
5895       /* Parse the asm-specification.  */
5896       asm_specification = cp_parser_asm_specification_opt (parser);
5897       /* If the next token is not an `=', then we might still be
5898          looking at an expression.  For example:
5899
5900            if (A(a).x)
5901
5902          looks like a decl-specifier-seq and a declarator -- but then
5903          there is no `=', so this is an expression.  */
5904       cp_parser_require (parser, CPP_EQ, "`='");
5905       /* If we did see an `=', then we are looking at a declaration
5906          for sure.  */
5907       if (cp_parser_parse_definitely (parser))
5908         {
5909           /* Create the declaration.  */
5910           decl = start_decl (declarator, type_specifiers,
5911                              /*initialized_p=*/true,
5912                              attributes, /*prefix_attributes=*/NULL_TREE);
5913           /* Parse the assignment-expression.  */
5914           initializer = cp_parser_assignment_expression (parser);
5915
5916           /* Process the initializer.  */
5917           cp_finish_decl (decl,
5918                           initializer,
5919                           asm_specification,
5920                           LOOKUP_ONLYCONVERTING);
5921
5922           return convert_from_reference (decl);
5923         }
5924     }
5925   /* If we didn't even get past the declarator successfully, we are
5926      definitely not looking at a declaration.  */
5927   else
5928     cp_parser_abort_tentative_parse (parser);
5929
5930   /* Otherwise, we are looking at an expression.  */
5931   return cp_parser_expression (parser);
5932 }
5933
5934 /* Parse an iteration-statement.
5935
5936    iteration-statement:
5937      while ( condition ) statement
5938      do statement while ( expression ) ;
5939      for ( for-init-statement condition [opt] ; expression [opt] )
5940        statement
5941
5942    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
5943
5944 static tree
5945 cp_parser_iteration_statement (cp_parser* parser)
5946 {
5947   cp_token *token;
5948   enum rid keyword;
5949   tree statement;
5950   bool in_iteration_statement_p;
5951
5952
5953   /* Peek at the next token.  */
5954   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
5955   if (!token)
5956     return error_mark_node;
5957
5958   /* Remember whether or not we are already within an iteration
5959      statement.  */
5960   in_iteration_statement_p = parser->in_iteration_statement_p;
5961
5962   /* See what kind of keyword it is.  */
5963   keyword = token->keyword;
5964   switch (keyword)
5965     {
5966     case RID_WHILE:
5967       {
5968         tree condition;
5969
5970         /* Begin the while-statement.  */
5971         statement = begin_while_stmt ();
5972         /* Look for the `('.  */
5973         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5974         /* Parse the condition.  */
5975         condition = cp_parser_condition (parser);
5976         finish_while_stmt_cond (condition, statement);
5977         /* Look for the `)'.  */
5978         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5979         /* Parse the dependent statement.  */
5980         parser->in_iteration_statement_p = true;
5981         cp_parser_already_scoped_statement (parser);
5982         parser->in_iteration_statement_p = in_iteration_statement_p;
5983         /* We're done with the while-statement.  */
5984         finish_while_stmt (statement);
5985       }
5986       break;
5987
5988     case RID_DO:
5989       {
5990         tree expression;
5991
5992         /* Begin the do-statement.  */
5993         statement = begin_do_stmt ();
5994         /* Parse the body of the do-statement.  */
5995         parser->in_iteration_statement_p = true;
5996         cp_parser_implicitly_scoped_statement (parser);
5997         parser->in_iteration_statement_p = in_iteration_statement_p;
5998         finish_do_body (statement);
5999         /* Look for the `while' keyword.  */
6000         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6001         /* Look for the `('.  */
6002         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6003         /* Parse the expression.  */
6004         expression = cp_parser_expression (parser);
6005         /* We're done with the do-statement.  */
6006         finish_do_stmt (expression, statement);
6007         /* Look for the `)'.  */
6008         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6009         /* Look for the `;'.  */
6010         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6011       }
6012       break;
6013
6014     case RID_FOR:
6015       {
6016         tree condition = NULL_TREE;
6017         tree expression = NULL_TREE;
6018
6019         /* Begin the for-statement.  */
6020         statement = begin_for_stmt ();
6021         /* Look for the `('.  */
6022         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6023         /* Parse the initialization.  */
6024         cp_parser_for_init_statement (parser);
6025         finish_for_init_stmt (statement);
6026
6027         /* If there's a condition, process it.  */
6028         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6029           condition = cp_parser_condition (parser);
6030         finish_for_cond (condition, statement);
6031         /* Look for the `;'.  */
6032         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6033
6034         /* If there's an expression, process it.  */
6035         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6036           expression = cp_parser_expression (parser);
6037         finish_for_expr (expression, statement);
6038         /* Look for the `)'.  */
6039         cp_parser_require (parser, CPP_CLOSE_PAREN, "`;'");
6040
6041         /* Parse the body of the for-statement.  */
6042         parser->in_iteration_statement_p = true;
6043         cp_parser_already_scoped_statement (parser);
6044         parser->in_iteration_statement_p = in_iteration_statement_p;
6045
6046         /* We're done with the for-statement.  */
6047         finish_for_stmt (statement);
6048       }
6049       break;
6050
6051     default:
6052       cp_parser_error (parser, "expected iteration-statement");
6053       statement = error_mark_node;
6054       break;
6055     }
6056
6057   return statement;
6058 }
6059
6060 /* Parse a for-init-statement.
6061
6062    for-init-statement:
6063      expression-statement
6064      simple-declaration  */
6065
6066 static void
6067 cp_parser_for_init_statement (cp_parser* parser)
6068 {
6069   /* If the next token is a `;', then we have an empty
6070      expression-statement.  Grammatically, this is also a
6071      simple-declaration, but an invalid one, because it does not
6072      declare anything.  Therefore, if we did not handle this case
6073      specially, we would issue an error message about an invalid
6074      declaration.  */
6075   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6076     {
6077       /* We're going to speculatively look for a declaration, falling back
6078          to an expression, if necessary.  */
6079       cp_parser_parse_tentatively (parser);
6080       /* Parse the declaration.  */
6081       cp_parser_simple_declaration (parser,
6082                                     /*function_definition_allowed_p=*/false);
6083       /* If the tentative parse failed, then we shall need to look for an
6084          expression-statement.  */
6085       if (cp_parser_parse_definitely (parser))
6086         return;
6087     }
6088
6089   cp_parser_expression_statement (parser, false);
6090 }
6091
6092 /* Parse a jump-statement.
6093
6094    jump-statement:
6095      break ;
6096      continue ;
6097      return expression [opt] ;
6098      goto identifier ;
6099
6100    GNU extension:
6101
6102    jump-statement:
6103      goto * expression ;
6104
6105    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_STMT, or
6106    GOTO_STMT.  */
6107
6108 static tree
6109 cp_parser_jump_statement (cp_parser* parser)
6110 {
6111   tree statement = error_mark_node;
6112   cp_token *token;
6113   enum rid keyword;
6114
6115   /* Peek at the next token.  */
6116   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6117   if (!token)
6118     return error_mark_node;
6119
6120   /* See what kind of keyword it is.  */
6121   keyword = token->keyword;
6122   switch (keyword)
6123     {
6124     case RID_BREAK:
6125       if (!parser->in_switch_statement_p
6126           && !parser->in_iteration_statement_p)
6127         {
6128           error ("break statement not within loop or switch");
6129           statement = error_mark_node;
6130         }
6131       else
6132         statement = finish_break_stmt ();
6133       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6134       break;
6135
6136     case RID_CONTINUE:
6137       if (!parser->in_iteration_statement_p)
6138         {
6139           error ("continue statement not within a loop");
6140           statement = error_mark_node;
6141         }
6142       else
6143         statement = finish_continue_stmt ();
6144       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6145       break;
6146
6147     case RID_RETURN:
6148       {
6149         tree expr;
6150
6151         /* If the next token is a `;', then there is no
6152            expression.  */
6153         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6154           expr = cp_parser_expression (parser);
6155         else
6156           expr = NULL_TREE;
6157         /* Build the return-statement.  */
6158         statement = finish_return_stmt (expr);
6159         /* Look for the final `;'.  */
6160         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6161       }
6162       break;
6163
6164     case RID_GOTO:
6165       /* Create the goto-statement.  */
6166       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6167         {
6168           /* Issue a warning about this use of a GNU extension.  */
6169           if (pedantic)
6170             pedwarn ("ISO C++ forbids computed gotos");
6171           /* Consume the '*' token.  */
6172           cp_lexer_consume_token (parser->lexer);
6173           /* Parse the dependent expression.  */
6174           finish_goto_stmt (cp_parser_expression (parser));
6175         }
6176       else
6177         finish_goto_stmt (cp_parser_identifier (parser));
6178       /* Look for the final `;'.  */
6179       cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6180       break;
6181
6182     default:
6183       cp_parser_error (parser, "expected jump-statement");
6184       break;
6185     }
6186
6187   return statement;
6188 }
6189
6190 /* Parse a declaration-statement.
6191
6192    declaration-statement:
6193      block-declaration  */
6194
6195 static void
6196 cp_parser_declaration_statement (cp_parser* parser)
6197 {
6198   /* Parse the block-declaration.  */
6199   cp_parser_block_declaration (parser, /*statement_p=*/true);
6200
6201   /* Finish off the statement.  */
6202   finish_stmt ();
6203 }
6204
6205 /* Some dependent statements (like `if (cond) statement'), are
6206    implicitly in their own scope.  In other words, if the statement is
6207    a single statement (as opposed to a compound-statement), it is
6208    none-the-less treated as if it were enclosed in braces.  Any
6209    declarations appearing in the dependent statement are out of scope
6210    after control passes that point.  This function parses a statement,
6211    but ensures that is in its own scope, even if it is not a
6212    compound-statement.
6213
6214    Returns the new statement.  */
6215
6216 static tree
6217 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6218 {
6219   tree statement;
6220
6221   /* If the token is not a `{', then we must take special action.  */
6222   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6223     {
6224       /* Create a compound-statement.  */
6225       statement = begin_compound_stmt (/*has_no_scope=*/false);
6226       /* Parse the dependent-statement.  */
6227       cp_parser_statement (parser, false);
6228       /* Finish the dummy compound-statement.  */
6229       finish_compound_stmt (statement);
6230     }
6231   /* Otherwise, we simply parse the statement directly.  */
6232   else
6233     statement = cp_parser_compound_statement (parser, false);
6234
6235   /* Return the statement.  */
6236   return statement;
6237 }
6238
6239 /* For some dependent statements (like `while (cond) statement'), we
6240    have already created a scope.  Therefore, even if the dependent
6241    statement is a compound-statement, we do not want to create another
6242    scope.  */
6243
6244 static void
6245 cp_parser_already_scoped_statement (cp_parser* parser)
6246 {
6247   /* If the token is not a `{', then we must take special action.  */
6248   if (cp_lexer_next_token_is_not(parser->lexer, CPP_OPEN_BRACE))
6249     {
6250       tree statement;
6251
6252       /* Create a compound-statement.  */
6253       statement = begin_compound_stmt (/*has_no_scope=*/true);
6254       /* Parse the dependent-statement.  */
6255       cp_parser_statement (parser, false);
6256       /* Finish the dummy compound-statement.  */
6257       finish_compound_stmt (statement);
6258     }
6259   /* Otherwise, we simply parse the statement directly.  */
6260   else
6261     cp_parser_statement (parser, false);
6262 }
6263
6264 /* Declarations [gram.dcl.dcl] */
6265
6266 /* Parse an optional declaration-sequence.
6267
6268    declaration-seq:
6269      declaration
6270      declaration-seq declaration  */
6271
6272 static void
6273 cp_parser_declaration_seq_opt (cp_parser* parser)
6274 {
6275   while (true)
6276     {
6277       cp_token *token;
6278
6279       token = cp_lexer_peek_token (parser->lexer);
6280
6281       if (token->type == CPP_CLOSE_BRACE
6282           || token->type == CPP_EOF)
6283         break;
6284
6285       if (token->type == CPP_SEMICOLON)
6286         {
6287           /* A declaration consisting of a single semicolon is
6288              invalid.  Allow it unless we're being pedantic.  */
6289           if (pedantic && !in_system_header)
6290             pedwarn ("extra `;'");
6291           cp_lexer_consume_token (parser->lexer);
6292           continue;
6293         }
6294
6295       /* The C lexer modifies PENDING_LANG_CHANGE when it wants the
6296          parser to enter or exit implicit `extern "C"' blocks.  */
6297       while (pending_lang_change > 0)
6298         {
6299           push_lang_context (lang_name_c);
6300           --pending_lang_change;
6301         }
6302       while (pending_lang_change < 0)
6303         {
6304           pop_lang_context ();
6305           ++pending_lang_change;
6306         }
6307
6308       /* Parse the declaration itself.  */
6309       cp_parser_declaration (parser);
6310     }
6311 }
6312
6313 /* Parse a declaration.
6314
6315    declaration:
6316      block-declaration
6317      function-definition
6318      template-declaration
6319      explicit-instantiation
6320      explicit-specialization
6321      linkage-specification
6322      namespace-definition
6323
6324    GNU extension:
6325
6326    declaration:
6327       __extension__ declaration */
6328
6329 static void
6330 cp_parser_declaration (cp_parser* parser)
6331 {
6332   cp_token token1;
6333   cp_token token2;
6334   int saved_pedantic;
6335
6336   /* Set this here since we can be called after
6337      pushing the linkage specification.  */
6338   c_lex_string_translate = true;
6339
6340   /* Check for the `__extension__' keyword.  */
6341   if (cp_parser_extension_opt (parser, &saved_pedantic))
6342     {
6343       /* Parse the qualified declaration.  */
6344       cp_parser_declaration (parser);
6345       /* Restore the PEDANTIC flag.  */
6346       pedantic = saved_pedantic;
6347
6348       return;
6349     }
6350
6351   /* Try to figure out what kind of declaration is present.  */
6352   token1 = *cp_lexer_peek_token (parser->lexer);
6353
6354   /* Don't translate the CPP_STRING in extern "C".  */
6355   if (token1.keyword == RID_EXTERN)
6356     c_lex_string_translate = false;
6357
6358   if (token1.type != CPP_EOF)
6359     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6360
6361   /* If the next token is `extern' and the following token is a string
6362      literal, then we have a linkage specification.  */
6363   if (token1.keyword == RID_EXTERN
6364       && cp_parser_is_string_literal (&token2))
6365     cp_parser_linkage_specification (parser);
6366   /* If the next token is `template', then we have either a template
6367      declaration, an explicit instantiation, or an explicit
6368      specialization.  */
6369   else if (token1.keyword == RID_TEMPLATE)
6370     {
6371       /* `template <>' indicates a template specialization.  */
6372       if (token2.type == CPP_LESS
6373           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6374         cp_parser_explicit_specialization (parser);
6375       /* `template <' indicates a template declaration.  */
6376       else if (token2.type == CPP_LESS)
6377         cp_parser_template_declaration (parser, /*member_p=*/false);
6378       /* Anything else must be an explicit instantiation.  */
6379       else
6380         cp_parser_explicit_instantiation (parser);
6381     }
6382   /* If the next token is `export', then we have a template
6383      declaration.  */
6384   else if (token1.keyword == RID_EXPORT)
6385     cp_parser_template_declaration (parser, /*member_p=*/false);
6386   /* If the next token is `extern', 'static' or 'inline' and the one
6387      after that is `template', we have a GNU extended explicit
6388      instantiation directive.  */
6389   else if (cp_parser_allow_gnu_extensions_p (parser)
6390            && (token1.keyword == RID_EXTERN
6391                || token1.keyword == RID_STATIC
6392                || token1.keyword == RID_INLINE)
6393            && token2.keyword == RID_TEMPLATE)
6394     cp_parser_explicit_instantiation (parser);
6395   /* If the next token is `namespace', check for a named or unnamed
6396      namespace definition.  */
6397   else if (token1.keyword == RID_NAMESPACE
6398            && (/* A named namespace definition.  */
6399                (token2.type == CPP_NAME
6400                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6401                     == CPP_OPEN_BRACE))
6402                /* An unnamed namespace definition.  */
6403                || token2.type == CPP_OPEN_BRACE))
6404     cp_parser_namespace_definition (parser);
6405   /* We must have either a block declaration or a function
6406      definition.  */
6407   else
6408     /* Try to parse a block-declaration, or a function-definition.  */
6409     cp_parser_block_declaration (parser, /*statement_p=*/false);
6410
6411   c_lex_string_translate = true;
6412 }
6413
6414 /* Parse a block-declaration.
6415
6416    block-declaration:
6417      simple-declaration
6418      asm-definition
6419      namespace-alias-definition
6420      using-declaration
6421      using-directive
6422
6423    GNU Extension:
6424
6425    block-declaration:
6426      __extension__ block-declaration
6427      label-declaration
6428
6429    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6430    part of a declaration-statement.  */
6431
6432 static void
6433 cp_parser_block_declaration (cp_parser *parser,
6434                              bool      statement_p)
6435 {
6436   cp_token *token1;
6437   int saved_pedantic;
6438
6439   /* Check for the `__extension__' keyword.  */
6440   if (cp_parser_extension_opt (parser, &saved_pedantic))
6441     {
6442       /* Parse the qualified declaration.  */
6443       cp_parser_block_declaration (parser, statement_p);
6444       /* Restore the PEDANTIC flag.  */
6445       pedantic = saved_pedantic;
6446
6447       return;
6448     }
6449
6450   /* Peek at the next token to figure out which kind of declaration is
6451      present.  */
6452   token1 = cp_lexer_peek_token (parser->lexer);
6453
6454   /* If the next keyword is `asm', we have an asm-definition.  */
6455   if (token1->keyword == RID_ASM)
6456     {
6457       if (statement_p)
6458         cp_parser_commit_to_tentative_parse (parser);
6459       cp_parser_asm_definition (parser);
6460     }
6461   /* If the next keyword is `namespace', we have a
6462      namespace-alias-definition.  */
6463   else if (token1->keyword == RID_NAMESPACE)
6464     cp_parser_namespace_alias_definition (parser);
6465   /* If the next keyword is `using', we have either a
6466      using-declaration or a using-directive.  */
6467   else if (token1->keyword == RID_USING)
6468     {
6469       cp_token *token2;
6470
6471       if (statement_p)
6472         cp_parser_commit_to_tentative_parse (parser);
6473       /* If the token after `using' is `namespace', then we have a
6474          using-directive.  */
6475       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6476       if (token2->keyword == RID_NAMESPACE)
6477         cp_parser_using_directive (parser);
6478       /* Otherwise, it's a using-declaration.  */
6479       else
6480         cp_parser_using_declaration (parser);
6481     }
6482   /* If the next keyword is `__label__' we have a label declaration.  */
6483   else if (token1->keyword == RID_LABEL)
6484     {
6485       if (statement_p)
6486         cp_parser_commit_to_tentative_parse (parser);
6487       cp_parser_label_declaration (parser);
6488     }
6489   /* Anything else must be a simple-declaration.  */
6490   else
6491     cp_parser_simple_declaration (parser, !statement_p);
6492 }
6493
6494 /* Parse a simple-declaration.
6495
6496    simple-declaration:
6497      decl-specifier-seq [opt] init-declarator-list [opt] ;
6498
6499    init-declarator-list:
6500      init-declarator
6501      init-declarator-list , init-declarator
6502
6503    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6504    function-definition as a simple-declaration.  */
6505
6506 static void
6507 cp_parser_simple_declaration (cp_parser* parser,
6508                               bool function_definition_allowed_p)
6509 {
6510   tree decl_specifiers;
6511   tree attributes;
6512   int declares_class_or_enum;
6513   bool saw_declarator;
6514
6515   /* Defer access checks until we know what is being declared; the
6516      checks for names appearing in the decl-specifier-seq should be
6517      done as if we were in the scope of the thing being declared.  */
6518   push_deferring_access_checks (dk_deferred);
6519
6520   /* Parse the decl-specifier-seq.  We have to keep track of whether
6521      or not the decl-specifier-seq declares a named class or
6522      enumeration type, since that is the only case in which the
6523      init-declarator-list is allowed to be empty.
6524
6525      [dcl.dcl]
6526
6527      In a simple-declaration, the optional init-declarator-list can be
6528      omitted only when declaring a class or enumeration, that is when
6529      the decl-specifier-seq contains either a class-specifier, an
6530      elaborated-type-specifier, or an enum-specifier.  */
6531   decl_specifiers
6532     = cp_parser_decl_specifier_seq (parser,
6533                                     CP_PARSER_FLAGS_OPTIONAL,
6534                                     &attributes,
6535                                     &declares_class_or_enum);
6536   /* We no longer need to defer access checks.  */
6537   stop_deferring_access_checks ();
6538
6539   /* In a block scope, a valid declaration must always have a
6540      decl-specifier-seq.  By not trying to parse declarators, we can
6541      resolve the declaration/expression ambiguity more quickly.  */
6542   if (!function_definition_allowed_p && !decl_specifiers)
6543     {
6544       cp_parser_error (parser, "expected declaration");
6545       goto done;
6546     }
6547
6548   /* If the next two tokens are both identifiers, the code is
6549      erroneous. The usual cause of this situation is code like:
6550
6551        T t;
6552
6553      where "T" should name a type -- but does not.  */
6554   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
6555     {
6556       /* If parsing tentatively, we should commit; we really are
6557          looking at a declaration.  */
6558       cp_parser_commit_to_tentative_parse (parser);
6559       /* Give up.  */
6560       goto done;
6561     }
6562
6563   /* Keep going until we hit the `;' at the end of the simple
6564      declaration.  */
6565   saw_declarator = false;
6566   while (cp_lexer_next_token_is_not (parser->lexer,
6567                                      CPP_SEMICOLON))
6568     {
6569       cp_token *token;
6570       bool function_definition_p;
6571       tree decl;
6572
6573       saw_declarator = true;
6574       /* Parse the init-declarator.  */
6575       decl = cp_parser_init_declarator (parser, decl_specifiers, attributes,
6576                                         function_definition_allowed_p,
6577                                         /*member_p=*/false,
6578                                         declares_class_or_enum,
6579                                         &function_definition_p);
6580       /* If an error occurred while parsing tentatively, exit quickly.
6581          (That usually happens when in the body of a function; each
6582          statement is treated as a declaration-statement until proven
6583          otherwise.)  */
6584       if (cp_parser_error_occurred (parser))
6585         goto done;
6586       /* Handle function definitions specially.  */
6587       if (function_definition_p)
6588         {
6589           /* If the next token is a `,', then we are probably
6590              processing something like:
6591
6592                void f() {}, *p;
6593
6594              which is erroneous.  */
6595           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6596             error ("mixing declarations and function-definitions is forbidden");
6597           /* Otherwise, we're done with the list of declarators.  */
6598           else
6599             {
6600               pop_deferring_access_checks ();
6601               return;
6602             }
6603         }
6604       /* The next token should be either a `,' or a `;'.  */
6605       token = cp_lexer_peek_token (parser->lexer);
6606       /* If it's a `,', there are more declarators to come.  */
6607       if (token->type == CPP_COMMA)
6608         cp_lexer_consume_token (parser->lexer);
6609       /* If it's a `;', we are done.  */
6610       else if (token->type == CPP_SEMICOLON)
6611         break;
6612       /* Anything else is an error.  */
6613       else
6614         {
6615           cp_parser_error (parser, "expected `,' or `;'");
6616           /* Skip tokens until we reach the end of the statement.  */
6617           cp_parser_skip_to_end_of_statement (parser);
6618           /* If the next token is now a `;', consume it.  */
6619           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6620             cp_lexer_consume_token (parser->lexer);
6621           goto done;
6622         }
6623       /* After the first time around, a function-definition is not
6624          allowed -- even if it was OK at first.  For example:
6625
6626            int i, f() {}
6627
6628          is not valid.  */
6629       function_definition_allowed_p = false;
6630     }
6631
6632   /* Issue an error message if no declarators are present, and the
6633      decl-specifier-seq does not itself declare a class or
6634      enumeration.  */
6635   if (!saw_declarator)
6636     {
6637       if (cp_parser_declares_only_class_p (parser))
6638         shadow_tag (decl_specifiers);
6639       /* Perform any deferred access checks.  */
6640       perform_deferred_access_checks ();
6641     }
6642
6643   /* Consume the `;'.  */
6644   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6645
6646  done:
6647   pop_deferring_access_checks ();
6648 }
6649
6650 /* Parse a decl-specifier-seq.
6651
6652    decl-specifier-seq:
6653      decl-specifier-seq [opt] decl-specifier
6654
6655    decl-specifier:
6656      storage-class-specifier
6657      type-specifier
6658      function-specifier
6659      friend
6660      typedef
6661
6662    GNU Extension:
6663
6664    decl-specifier-seq:
6665      decl-specifier-seq [opt] attributes
6666
6667    Returns a TREE_LIST, giving the decl-specifiers in the order they
6668    appear in the source code.  The TREE_VALUE of each node is the
6669    decl-specifier.  For a keyword (such as `auto' or `friend'), the
6670    TREE_VALUE is simply the corresponding TREE_IDENTIFIER.  For the
6671    representation of a type-specifier, see cp_parser_type_specifier.
6672
6673    If there are attributes, they will be stored in *ATTRIBUTES,
6674    represented as described above cp_parser_attributes.
6675
6676    If FRIEND_IS_NOT_CLASS_P is non-NULL, and the `friend' specifier
6677    appears, and the entity that will be a friend is not going to be a
6678    class, then *FRIEND_IS_NOT_CLASS_P will be set to TRUE.  Note that
6679    even if *FRIEND_IS_NOT_CLASS_P is FALSE, the entity to which
6680    friendship is granted might not be a class.
6681
6682    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
6683    flags:
6684
6685      1: one of the decl-specifiers is an elaborated-type-specifier
6686         (i.e., a type declaration)
6687      2: one of the decl-specifiers is an enum-specifier or a
6688         class-specifier (i.e., a type definition)
6689
6690    */
6691
6692 static tree
6693 cp_parser_decl_specifier_seq (cp_parser* parser,
6694                               cp_parser_flags flags,
6695                               tree* attributes,
6696                               int* declares_class_or_enum)
6697 {
6698   tree decl_specs = NULL_TREE;
6699   bool friend_p = false;
6700   bool constructor_possible_p = !parser->in_declarator_p;
6701
6702   /* Assume no class or enumeration type is declared.  */
6703   *declares_class_or_enum = 0;
6704
6705   /* Assume there are no attributes.  */
6706   *attributes = NULL_TREE;
6707
6708   /* Keep reading specifiers until there are no more to read.  */
6709   while (true)
6710     {
6711       tree decl_spec = NULL_TREE;
6712       bool constructor_p;
6713       cp_token *token;
6714
6715       /* Peek at the next token.  */
6716       token = cp_lexer_peek_token (parser->lexer);
6717       /* Handle attributes.  */
6718       if (token->keyword == RID_ATTRIBUTE)
6719         {
6720           /* Parse the attributes.  */
6721           decl_spec = cp_parser_attributes_opt (parser);
6722           /* Add them to the list.  */
6723           *attributes = chainon (*attributes, decl_spec);
6724           continue;
6725         }
6726       /* If the next token is an appropriate keyword, we can simply
6727          add it to the list.  */
6728       switch (token->keyword)
6729         {
6730         case RID_FRIEND:
6731           /* decl-specifier:
6732                friend  */
6733           if (friend_p)
6734             error ("duplicate `friend'");
6735           else
6736             friend_p = true;
6737           /* The representation of the specifier is simply the
6738              appropriate TREE_IDENTIFIER node.  */
6739           decl_spec = token->value;
6740           /* Consume the token.  */
6741           cp_lexer_consume_token (parser->lexer);
6742           break;
6743
6744           /* function-specifier:
6745                inline
6746                virtual
6747                explicit  */
6748         case RID_INLINE:
6749         case RID_VIRTUAL:
6750         case RID_EXPLICIT:
6751           decl_spec = cp_parser_function_specifier_opt (parser);
6752           break;
6753
6754           /* decl-specifier:
6755                typedef  */
6756         case RID_TYPEDEF:
6757           /* The representation of the specifier is simply the
6758              appropriate TREE_IDENTIFIER node.  */
6759           decl_spec = token->value;
6760           /* Consume the token.  */
6761           cp_lexer_consume_token (parser->lexer);
6762           /* A constructor declarator cannot appear in a typedef.  */
6763           constructor_possible_p = false;
6764           /* The "typedef" keyword can only occur in a declaration; we
6765              may as well commit at this point.  */
6766           cp_parser_commit_to_tentative_parse (parser);
6767           break;
6768
6769           /* storage-class-specifier:
6770                auto
6771                register
6772                static
6773                extern
6774                mutable
6775
6776              GNU Extension:
6777                thread  */
6778         case RID_AUTO:
6779         case RID_REGISTER:
6780         case RID_STATIC:
6781         case RID_EXTERN:
6782         case RID_MUTABLE:
6783         case RID_THREAD:
6784           decl_spec = cp_parser_storage_class_specifier_opt (parser);
6785           break;
6786
6787         default:
6788           break;
6789         }
6790
6791       /* Constructors are a special case.  The `S' in `S()' is not a
6792          decl-specifier; it is the beginning of the declarator.  */
6793       constructor_p = (!decl_spec
6794                        && constructor_possible_p
6795                        && cp_parser_constructor_declarator_p (parser,
6796                                                               friend_p));
6797
6798       /* If we don't have a DECL_SPEC yet, then we must be looking at
6799          a type-specifier.  */
6800       if (!decl_spec && !constructor_p)
6801         {
6802           int decl_spec_declares_class_or_enum;
6803           bool is_cv_qualifier;
6804
6805           decl_spec
6806             = cp_parser_type_specifier (parser, flags,
6807                                         friend_p,
6808                                         /*is_declaration=*/true,
6809                                         &decl_spec_declares_class_or_enum,
6810                                         &is_cv_qualifier);
6811
6812           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
6813
6814           /* If this type-specifier referenced a user-defined type
6815              (a typedef, class-name, etc.), then we can't allow any
6816              more such type-specifiers henceforth.
6817
6818              [dcl.spec]
6819
6820              The longest sequence of decl-specifiers that could
6821              possibly be a type name is taken as the
6822              decl-specifier-seq of a declaration.  The sequence shall
6823              be self-consistent as described below.
6824
6825              [dcl.type]
6826
6827              As a general rule, at most one type-specifier is allowed
6828              in the complete decl-specifier-seq of a declaration.  The
6829              only exceptions are the following:
6830
6831              -- const or volatile can be combined with any other
6832                 type-specifier.
6833
6834              -- signed or unsigned can be combined with char, long,
6835                 short, or int.
6836
6837              -- ..
6838
6839              Example:
6840
6841                typedef char* Pc;
6842                void g (const int Pc);
6843
6844              Here, Pc is *not* part of the decl-specifier seq; it's
6845              the declarator.  Therefore, once we see a type-specifier
6846              (other than a cv-qualifier), we forbid any additional
6847              user-defined types.  We *do* still allow things like `int
6848              int' to be considered a decl-specifier-seq, and issue the
6849              error message later.  */
6850           if (decl_spec && !is_cv_qualifier)
6851             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
6852           /* A constructor declarator cannot follow a type-specifier.  */
6853           if (decl_spec)
6854             constructor_possible_p = false;
6855         }
6856
6857       /* If we still do not have a DECL_SPEC, then there are no more
6858          decl-specifiers.  */
6859       if (!decl_spec)
6860         {
6861           /* Issue an error message, unless the entire construct was
6862              optional.  */
6863           if (!(flags & CP_PARSER_FLAGS_OPTIONAL))
6864             {
6865               cp_parser_error (parser, "expected decl specifier");
6866               return error_mark_node;
6867             }
6868
6869           break;
6870         }
6871
6872       /* Add the DECL_SPEC to the list of specifiers.  */
6873       if (decl_specs == NULL || TREE_VALUE (decl_specs) != error_mark_node)
6874         decl_specs = tree_cons (NULL_TREE, decl_spec, decl_specs);
6875
6876       /* After we see one decl-specifier, further decl-specifiers are
6877          always optional.  */
6878       flags |= CP_PARSER_FLAGS_OPTIONAL;
6879     }
6880
6881   /* Don't allow a friend specifier with a class definition.  */
6882   if (friend_p && (*declares_class_or_enum & 2))
6883     error ("class definition may not be declared a friend");
6884
6885   /* We have built up the DECL_SPECS in reverse order.  Return them in
6886      the correct order.  */
6887   return nreverse (decl_specs);
6888 }
6889
6890 /* Parse an (optional) storage-class-specifier.
6891
6892    storage-class-specifier:
6893      auto
6894      register
6895      static
6896      extern
6897      mutable
6898
6899    GNU Extension:
6900
6901    storage-class-specifier:
6902      thread
6903
6904    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6905
6906 static tree
6907 cp_parser_storage_class_specifier_opt (cp_parser* parser)
6908 {
6909   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6910     {
6911     case RID_AUTO:
6912     case RID_REGISTER:
6913     case RID_STATIC:
6914     case RID_EXTERN:
6915     case RID_MUTABLE:
6916     case RID_THREAD:
6917       /* Consume the token.  */
6918       return cp_lexer_consume_token (parser->lexer)->value;
6919
6920     default:
6921       return NULL_TREE;
6922     }
6923 }
6924
6925 /* Parse an (optional) function-specifier.
6926
6927    function-specifier:
6928      inline
6929      virtual
6930      explicit
6931
6932    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
6933
6934 static tree
6935 cp_parser_function_specifier_opt (cp_parser* parser)
6936 {
6937   switch (cp_lexer_peek_token (parser->lexer)->keyword)
6938     {
6939     case RID_INLINE:
6940     case RID_VIRTUAL:
6941     case RID_EXPLICIT:
6942       /* Consume the token.  */
6943       return cp_lexer_consume_token (parser->lexer)->value;
6944
6945     default:
6946       return NULL_TREE;
6947     }
6948 }
6949
6950 /* Parse a linkage-specification.
6951
6952    linkage-specification:
6953      extern string-literal { declaration-seq [opt] }
6954      extern string-literal declaration  */
6955
6956 static void
6957 cp_parser_linkage_specification (cp_parser* parser)
6958 {
6959   cp_token *token;
6960   tree linkage;
6961
6962   /* Look for the `extern' keyword.  */
6963   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
6964
6965   /* Peek at the next token.  */
6966   token = cp_lexer_peek_token (parser->lexer);
6967   /* If it's not a string-literal, then there's a problem.  */
6968   if (!cp_parser_is_string_literal (token))
6969     {
6970       cp_parser_error (parser, "expected language-name");
6971       return;
6972     }
6973   /* Consume the token.  */
6974   cp_lexer_consume_token (parser->lexer);
6975
6976   /* Transform the literal into an identifier.  If the literal is a
6977      wide-character string, or contains embedded NULs, then we can't
6978      handle it as the user wants.  */
6979   if (token->type == CPP_WSTRING
6980       || (strlen (TREE_STRING_POINTER (token->value))
6981           != (size_t) (TREE_STRING_LENGTH (token->value) - 1)))
6982     {
6983       cp_parser_error (parser, "invalid linkage-specification");
6984       /* Assume C++ linkage.  */
6985       linkage = get_identifier ("c++");
6986     }
6987   /* If it's a simple string constant, things are easier.  */
6988   else
6989     linkage = get_identifier (TREE_STRING_POINTER (token->value));
6990
6991   /* We're now using the new linkage.  */
6992   push_lang_context (linkage);
6993
6994   /* If the next token is a `{', then we're using the first
6995      production.  */
6996   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6997     {
6998       /* Consume the `{' token.  */
6999       cp_lexer_consume_token (parser->lexer);
7000       /* Parse the declarations.  */
7001       cp_parser_declaration_seq_opt (parser);
7002       /* Look for the closing `}'.  */
7003       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7004     }
7005   /* Otherwise, there's just one declaration.  */
7006   else
7007     {
7008       bool saved_in_unbraced_linkage_specification_p;
7009
7010       saved_in_unbraced_linkage_specification_p
7011         = parser->in_unbraced_linkage_specification_p;
7012       parser->in_unbraced_linkage_specification_p = true;
7013       have_extern_spec = true;
7014       cp_parser_declaration (parser);
7015       have_extern_spec = false;
7016       parser->in_unbraced_linkage_specification_p
7017         = saved_in_unbraced_linkage_specification_p;
7018     }
7019
7020   /* We're done with the linkage-specification.  */
7021   pop_lang_context ();
7022 }
7023
7024 /* Special member functions [gram.special] */
7025
7026 /* Parse a conversion-function-id.
7027
7028    conversion-function-id:
7029      operator conversion-type-id
7030
7031    Returns an IDENTIFIER_NODE representing the operator.  */
7032
7033 static tree
7034 cp_parser_conversion_function_id (cp_parser* parser)
7035 {
7036   tree type;
7037   tree saved_scope;
7038   tree saved_qualifying_scope;
7039   tree saved_object_scope;
7040   bool pop_p = false;
7041
7042   /* Look for the `operator' token.  */
7043   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7044     return error_mark_node;
7045   /* When we parse the conversion-type-id, the current scope will be
7046      reset.  However, we need that information in able to look up the
7047      conversion function later, so we save it here.  */
7048   saved_scope = parser->scope;
7049   saved_qualifying_scope = parser->qualifying_scope;
7050   saved_object_scope = parser->object_scope;
7051   /* We must enter the scope of the class so that the names of
7052      entities declared within the class are available in the
7053      conversion-type-id.  For example, consider:
7054
7055        struct S {
7056          typedef int I;
7057          operator I();
7058        };
7059
7060        S::operator I() { ... }
7061
7062      In order to see that `I' is a type-name in the definition, we
7063      must be in the scope of `S'.  */
7064   if (saved_scope)
7065     pop_p = push_scope (saved_scope);
7066   /* Parse the conversion-type-id.  */
7067   type = cp_parser_conversion_type_id (parser);
7068   /* Leave the scope of the class, if any.  */
7069   if (pop_p)
7070     pop_scope (saved_scope);
7071   /* Restore the saved scope.  */
7072   parser->scope = saved_scope;
7073   parser->qualifying_scope = saved_qualifying_scope;
7074   parser->object_scope = saved_object_scope;
7075   /* If the TYPE is invalid, indicate failure.  */
7076   if (type == error_mark_node)
7077     return error_mark_node;
7078   return mangle_conv_op_name_for_type (type);
7079 }
7080
7081 /* Parse a conversion-type-id:
7082
7083    conversion-type-id:
7084      type-specifier-seq conversion-declarator [opt]
7085
7086    Returns the TYPE specified.  */
7087
7088 static tree
7089 cp_parser_conversion_type_id (cp_parser* parser)
7090 {
7091   tree attributes;
7092   tree type_specifiers;
7093   tree declarator;
7094
7095   /* Parse the attributes.  */
7096   attributes = cp_parser_attributes_opt (parser);
7097   /* Parse the type-specifiers.  */
7098   type_specifiers = cp_parser_type_specifier_seq (parser);
7099   /* If that didn't work, stop.  */
7100   if (type_specifiers == error_mark_node)
7101     return error_mark_node;
7102   /* Parse the conversion-declarator.  */
7103   declarator = cp_parser_conversion_declarator_opt (parser);
7104
7105   return grokdeclarator (declarator, type_specifiers, TYPENAME,
7106                          /*initialized=*/0, &attributes);
7107 }
7108
7109 /* Parse an (optional) conversion-declarator.
7110
7111    conversion-declarator:
7112      ptr-operator conversion-declarator [opt]
7113
7114    Returns a representation of the declarator.  See
7115    cp_parser_declarator for details.  */
7116
7117 static tree
7118 cp_parser_conversion_declarator_opt (cp_parser* parser)
7119 {
7120   enum tree_code code;
7121   tree class_type;
7122   tree cv_qualifier_seq;
7123
7124   /* We don't know if there's a ptr-operator next, or not.  */
7125   cp_parser_parse_tentatively (parser);
7126   /* Try the ptr-operator.  */
7127   code = cp_parser_ptr_operator (parser, &class_type,
7128                                  &cv_qualifier_seq);
7129   /* If it worked, look for more conversion-declarators.  */
7130   if (cp_parser_parse_definitely (parser))
7131     {
7132      tree declarator;
7133
7134      /* Parse another optional declarator.  */
7135      declarator = cp_parser_conversion_declarator_opt (parser);
7136
7137      /* Create the representation of the declarator.  */
7138      if (code == INDIRECT_REF)
7139        declarator = make_pointer_declarator (cv_qualifier_seq,
7140                                              declarator);
7141      else
7142        declarator =  make_reference_declarator (cv_qualifier_seq,
7143                                                 declarator);
7144
7145      /* Handle the pointer-to-member case.  */
7146      if (class_type)
7147        declarator = build_nt (SCOPE_REF, class_type, declarator);
7148
7149      return declarator;
7150    }
7151
7152   return NULL_TREE;
7153 }
7154
7155 /* Parse an (optional) ctor-initializer.
7156
7157    ctor-initializer:
7158      : mem-initializer-list
7159
7160    Returns TRUE iff the ctor-initializer was actually present.  */
7161
7162 static bool
7163 cp_parser_ctor_initializer_opt (cp_parser* parser)
7164 {
7165   /* If the next token is not a `:', then there is no
7166      ctor-initializer.  */
7167   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7168     {
7169       /* Do default initialization of any bases and members.  */
7170       if (DECL_CONSTRUCTOR_P (current_function_decl))
7171         finish_mem_initializers (NULL_TREE);
7172
7173       return false;
7174     }
7175
7176   /* Consume the `:' token.  */
7177   cp_lexer_consume_token (parser->lexer);
7178   /* And the mem-initializer-list.  */
7179   cp_parser_mem_initializer_list (parser);
7180
7181   return true;
7182 }
7183
7184 /* Parse a mem-initializer-list.
7185
7186    mem-initializer-list:
7187      mem-initializer
7188      mem-initializer , mem-initializer-list  */
7189
7190 static void
7191 cp_parser_mem_initializer_list (cp_parser* parser)
7192 {
7193   tree mem_initializer_list = NULL_TREE;
7194
7195   /* Let the semantic analysis code know that we are starting the
7196      mem-initializer-list.  */
7197   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7198     error ("only constructors take base initializers");
7199
7200   /* Loop through the list.  */
7201   while (true)
7202     {
7203       tree mem_initializer;
7204
7205       /* Parse the mem-initializer.  */
7206       mem_initializer = cp_parser_mem_initializer (parser);
7207       /* Add it to the list, unless it was erroneous.  */
7208       if (mem_initializer)
7209         {
7210           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7211           mem_initializer_list = mem_initializer;
7212         }
7213       /* If the next token is not a `,', we're done.  */
7214       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7215         break;
7216       /* Consume the `,' token.  */
7217       cp_lexer_consume_token (parser->lexer);
7218     }
7219
7220   /* Perform semantic analysis.  */
7221   if (DECL_CONSTRUCTOR_P (current_function_decl))
7222     finish_mem_initializers (mem_initializer_list);
7223 }
7224
7225 /* Parse a mem-initializer.
7226
7227    mem-initializer:
7228      mem-initializer-id ( expression-list [opt] )
7229
7230    GNU extension:
7231
7232    mem-initializer:
7233      ( expression-list [opt] )
7234
7235    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7236    class) or FIELD_DECL (for a non-static data member) to initialize;
7237    the TREE_VALUE is the expression-list.  */
7238
7239 static tree
7240 cp_parser_mem_initializer (cp_parser* parser)
7241 {
7242   tree mem_initializer_id;
7243   tree expression_list;
7244   tree member;
7245
7246   /* Find out what is being initialized.  */
7247   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7248     {
7249       pedwarn ("anachronistic old-style base class initializer");
7250       mem_initializer_id = NULL_TREE;
7251     }
7252   else
7253     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7254   member = expand_member_init (mem_initializer_id);
7255   if (member && !DECL_P (member))
7256     in_base_initializer = 1;
7257
7258   expression_list
7259     = cp_parser_parenthesized_expression_list (parser, false,
7260                                                /*non_constant_p=*/NULL);
7261   if (!expression_list)
7262     expression_list = void_type_node;
7263
7264   in_base_initializer = 0;
7265
7266   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7267 }
7268
7269 /* Parse a mem-initializer-id.
7270
7271    mem-initializer-id:
7272      :: [opt] nested-name-specifier [opt] class-name
7273      identifier
7274
7275    Returns a TYPE indicating the class to be initializer for the first
7276    production.  Returns an IDENTIFIER_NODE indicating the data member
7277    to be initialized for the second production.  */
7278
7279 static tree
7280 cp_parser_mem_initializer_id (cp_parser* parser)
7281 {
7282   bool global_scope_p;
7283   bool nested_name_specifier_p;
7284   tree id;
7285
7286   /* Look for the optional `::' operator.  */
7287   global_scope_p
7288     = (cp_parser_global_scope_opt (parser,
7289                                    /*current_scope_valid_p=*/false)
7290        != NULL_TREE);
7291   /* Look for the optional nested-name-specifier.  The simplest way to
7292      implement:
7293
7294        [temp.res]
7295
7296        The keyword `typename' is not permitted in a base-specifier or
7297        mem-initializer; in these contexts a qualified name that
7298        depends on a template-parameter is implicitly assumed to be a
7299        type name.
7300
7301      is to assume that we have seen the `typename' keyword at this
7302      point.  */
7303   nested_name_specifier_p
7304     = (cp_parser_nested_name_specifier_opt (parser,
7305                                             /*typename_keyword_p=*/true,
7306                                             /*check_dependency_p=*/true,
7307                                             /*type_p=*/true,
7308                                             /*is_declaration=*/true)
7309        != NULL_TREE);
7310   /* If there is a `::' operator or a nested-name-specifier, then we
7311      are definitely looking for a class-name.  */
7312   if (global_scope_p || nested_name_specifier_p)
7313     return cp_parser_class_name (parser,
7314                                  /*typename_keyword_p=*/true,
7315                                  /*template_keyword_p=*/false,
7316                                  /*type_p=*/false,
7317                                  /*check_dependency_p=*/true,
7318                                  /*class_head_p=*/false,
7319                                  /*is_declaration=*/true);
7320   /* Otherwise, we could also be looking for an ordinary identifier.  */
7321   cp_parser_parse_tentatively (parser);
7322   /* Try a class-name.  */
7323   id = cp_parser_class_name (parser,
7324                              /*typename_keyword_p=*/true,
7325                              /*template_keyword_p=*/false,
7326                              /*type_p=*/false,
7327                              /*check_dependency_p=*/true,
7328                              /*class_head_p=*/false,
7329                              /*is_declaration=*/true);
7330   /* If we found one, we're done.  */
7331   if (cp_parser_parse_definitely (parser))
7332     return id;
7333   /* Otherwise, look for an ordinary identifier.  */
7334   return cp_parser_identifier (parser);
7335 }
7336
7337 /* Overloading [gram.over] */
7338
7339 /* Parse an operator-function-id.
7340
7341    operator-function-id:
7342      operator operator
7343
7344    Returns an IDENTIFIER_NODE for the operator which is a
7345    human-readable spelling of the identifier, e.g., `operator +'.  */
7346
7347 static tree
7348 cp_parser_operator_function_id (cp_parser* parser)
7349 {
7350   /* Look for the `operator' keyword.  */
7351   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7352     return error_mark_node;
7353   /* And then the name of the operator itself.  */
7354   return cp_parser_operator (parser);
7355 }
7356
7357 /* Parse an operator.
7358
7359    operator:
7360      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7361      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7362      || ++ -- , ->* -> () []
7363
7364    GNU Extensions:
7365
7366    operator:
7367      <? >? <?= >?=
7368
7369    Returns an IDENTIFIER_NODE for the operator which is a
7370    human-readable spelling of the identifier, e.g., `operator +'.  */
7371
7372 static tree
7373 cp_parser_operator (cp_parser* parser)
7374 {
7375   tree id = NULL_TREE;
7376   cp_token *token;
7377
7378   /* Peek at the next token.  */
7379   token = cp_lexer_peek_token (parser->lexer);
7380   /* Figure out which operator we have.  */
7381   switch (token->type)
7382     {
7383     case CPP_KEYWORD:
7384       {
7385         enum tree_code op;
7386
7387         /* The keyword should be either `new' or `delete'.  */
7388         if (token->keyword == RID_NEW)
7389           op = NEW_EXPR;
7390         else if (token->keyword == RID_DELETE)
7391           op = DELETE_EXPR;
7392         else
7393           break;
7394
7395         /* Consume the `new' or `delete' token.  */
7396         cp_lexer_consume_token (parser->lexer);
7397
7398         /* Peek at the next token.  */
7399         token = cp_lexer_peek_token (parser->lexer);
7400         /* If it's a `[' token then this is the array variant of the
7401            operator.  */
7402         if (token->type == CPP_OPEN_SQUARE)
7403           {
7404             /* Consume the `[' token.  */
7405             cp_lexer_consume_token (parser->lexer);
7406             /* Look for the `]' token.  */
7407             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7408             id = ansi_opname (op == NEW_EXPR
7409                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7410           }
7411         /* Otherwise, we have the non-array variant.  */
7412         else
7413           id = ansi_opname (op);
7414
7415         return id;
7416       }
7417
7418     case CPP_PLUS:
7419       id = ansi_opname (PLUS_EXPR);
7420       break;
7421
7422     case CPP_MINUS:
7423       id = ansi_opname (MINUS_EXPR);
7424       break;
7425
7426     case CPP_MULT:
7427       id = ansi_opname (MULT_EXPR);
7428       break;
7429
7430     case CPP_DIV:
7431       id = ansi_opname (TRUNC_DIV_EXPR);
7432       break;
7433
7434     case CPP_MOD:
7435       id = ansi_opname (TRUNC_MOD_EXPR);
7436       break;
7437
7438     case CPP_XOR:
7439       id = ansi_opname (BIT_XOR_EXPR);
7440       break;
7441
7442     case CPP_AND:
7443       id = ansi_opname (BIT_AND_EXPR);
7444       break;
7445
7446     case CPP_OR:
7447       id = ansi_opname (BIT_IOR_EXPR);
7448       break;
7449
7450     case CPP_COMPL:
7451       id = ansi_opname (BIT_NOT_EXPR);
7452       break;
7453
7454     case CPP_NOT:
7455       id = ansi_opname (TRUTH_NOT_EXPR);
7456       break;
7457
7458     case CPP_EQ:
7459       id = ansi_assopname (NOP_EXPR);
7460       break;
7461
7462     case CPP_LESS:
7463       id = ansi_opname (LT_EXPR);
7464       break;
7465
7466     case CPP_GREATER:
7467       id = ansi_opname (GT_EXPR);
7468       break;
7469
7470     case CPP_PLUS_EQ:
7471       id = ansi_assopname (PLUS_EXPR);
7472       break;
7473
7474     case CPP_MINUS_EQ:
7475       id = ansi_assopname (MINUS_EXPR);
7476       break;
7477
7478     case CPP_MULT_EQ:
7479       id = ansi_assopname (MULT_EXPR);
7480       break;
7481
7482     case CPP_DIV_EQ:
7483       id = ansi_assopname (TRUNC_DIV_EXPR);
7484       break;
7485
7486     case CPP_MOD_EQ:
7487       id = ansi_assopname (TRUNC_MOD_EXPR);
7488       break;
7489
7490     case CPP_XOR_EQ:
7491       id = ansi_assopname (BIT_XOR_EXPR);
7492       break;
7493
7494     case CPP_AND_EQ:
7495       id = ansi_assopname (BIT_AND_EXPR);
7496       break;
7497
7498     case CPP_OR_EQ:
7499       id = ansi_assopname (BIT_IOR_EXPR);
7500       break;
7501
7502     case CPP_LSHIFT:
7503       id = ansi_opname (LSHIFT_EXPR);
7504       break;
7505
7506     case CPP_RSHIFT:
7507       id = ansi_opname (RSHIFT_EXPR);
7508       break;
7509
7510     case CPP_LSHIFT_EQ:
7511       id = ansi_assopname (LSHIFT_EXPR);
7512       break;
7513
7514     case CPP_RSHIFT_EQ:
7515       id = ansi_assopname (RSHIFT_EXPR);
7516       break;
7517
7518     case CPP_EQ_EQ:
7519       id = ansi_opname (EQ_EXPR);
7520       break;
7521
7522     case CPP_NOT_EQ:
7523       id = ansi_opname (NE_EXPR);
7524       break;
7525
7526     case CPP_LESS_EQ:
7527       id = ansi_opname (LE_EXPR);
7528       break;
7529
7530     case CPP_GREATER_EQ:
7531       id = ansi_opname (GE_EXPR);
7532       break;
7533
7534     case CPP_AND_AND:
7535       id = ansi_opname (TRUTH_ANDIF_EXPR);
7536       break;
7537
7538     case CPP_OR_OR:
7539       id = ansi_opname (TRUTH_ORIF_EXPR);
7540       break;
7541
7542     case CPP_PLUS_PLUS:
7543       id = ansi_opname (POSTINCREMENT_EXPR);
7544       break;
7545
7546     case CPP_MINUS_MINUS:
7547       id = ansi_opname (PREDECREMENT_EXPR);
7548       break;
7549
7550     case CPP_COMMA:
7551       id = ansi_opname (COMPOUND_EXPR);
7552       break;
7553
7554     case CPP_DEREF_STAR:
7555       id = ansi_opname (MEMBER_REF);
7556       break;
7557
7558     case CPP_DEREF:
7559       id = ansi_opname (COMPONENT_REF);
7560       break;
7561
7562     case CPP_OPEN_PAREN:
7563       /* Consume the `('.  */
7564       cp_lexer_consume_token (parser->lexer);
7565       /* Look for the matching `)'.  */
7566       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7567       return ansi_opname (CALL_EXPR);
7568
7569     case CPP_OPEN_SQUARE:
7570       /* Consume the `['.  */
7571       cp_lexer_consume_token (parser->lexer);
7572       /* Look for the matching `]'.  */
7573       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7574       return ansi_opname (ARRAY_REF);
7575
7576       /* Extensions.  */
7577     case CPP_MIN:
7578       id = ansi_opname (MIN_EXPR);
7579       break;
7580
7581     case CPP_MAX:
7582       id = ansi_opname (MAX_EXPR);
7583       break;
7584
7585     case CPP_MIN_EQ:
7586       id = ansi_assopname (MIN_EXPR);
7587       break;
7588
7589     case CPP_MAX_EQ:
7590       id = ansi_assopname (MAX_EXPR);
7591       break;
7592
7593     default:
7594       /* Anything else is an error.  */
7595       break;
7596     }
7597
7598   /* If we have selected an identifier, we need to consume the
7599      operator token.  */
7600   if (id)
7601     cp_lexer_consume_token (parser->lexer);
7602   /* Otherwise, no valid operator name was present.  */
7603   else
7604     {
7605       cp_parser_error (parser, "expected operator");
7606       id = error_mark_node;
7607     }
7608
7609   return id;
7610 }
7611
7612 /* Parse a template-declaration.
7613
7614    template-declaration:
7615      export [opt] template < template-parameter-list > declaration
7616
7617    If MEMBER_P is TRUE, this template-declaration occurs within a
7618    class-specifier.
7619
7620    The grammar rule given by the standard isn't correct.  What
7621    is really meant is:
7622
7623    template-declaration:
7624      export [opt] template-parameter-list-seq
7625        decl-specifier-seq [opt] init-declarator [opt] ;
7626      export [opt] template-parameter-list-seq
7627        function-definition
7628
7629    template-parameter-list-seq:
7630      template-parameter-list-seq [opt]
7631      template < template-parameter-list >  */
7632
7633 static void
7634 cp_parser_template_declaration (cp_parser* parser, bool member_p)
7635 {
7636   /* Check for `export'.  */
7637   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
7638     {
7639       /* Consume the `export' token.  */
7640       cp_lexer_consume_token (parser->lexer);
7641       /* Warn that we do not support `export'.  */
7642       warning ("keyword `export' not implemented, and will be ignored");
7643     }
7644
7645   cp_parser_template_declaration_after_export (parser, member_p);
7646 }
7647
7648 /* Parse a template-parameter-list.
7649
7650    template-parameter-list:
7651      template-parameter
7652      template-parameter-list , template-parameter
7653
7654    Returns a TREE_LIST.  Each node represents a template parameter.
7655    The nodes are connected via their TREE_CHAINs.  */
7656
7657 static tree
7658 cp_parser_template_parameter_list (cp_parser* parser)
7659 {
7660   tree parameter_list = NULL_TREE;
7661
7662   while (true)
7663     {
7664       tree parameter;
7665       cp_token *token;
7666
7667       /* Parse the template-parameter.  */
7668       parameter = cp_parser_template_parameter (parser);
7669       /* Add it to the list.  */
7670       parameter_list = process_template_parm (parameter_list,
7671                                               parameter);
7672
7673       /* Peek at the next token.  */
7674       token = cp_lexer_peek_token (parser->lexer);
7675       /* If it's not a `,', we're done.  */
7676       if (token->type != CPP_COMMA)
7677         break;
7678       /* Otherwise, consume the `,' token.  */
7679       cp_lexer_consume_token (parser->lexer);
7680     }
7681
7682   return parameter_list;
7683 }
7684
7685 /* Parse a template-parameter.
7686
7687    template-parameter:
7688      type-parameter
7689      parameter-declaration
7690
7691    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
7692    TREE_PURPOSE is the default value, if any.  */
7693
7694 static tree
7695 cp_parser_template_parameter (cp_parser* parser)
7696 {
7697   cp_token *token;
7698
7699   /* Peek at the next token.  */
7700   token = cp_lexer_peek_token (parser->lexer);
7701   /* If it is `class' or `template', we have a type-parameter.  */
7702   if (token->keyword == RID_TEMPLATE)
7703     return cp_parser_type_parameter (parser);
7704   /* If it is `class' or `typename' we do not know yet whether it is a
7705      type parameter or a non-type parameter.  Consider:
7706
7707        template <typename T, typename T::X X> ...
7708
7709      or:
7710
7711        template <class C, class D*> ...
7712
7713      Here, the first parameter is a type parameter, and the second is
7714      a non-type parameter.  We can tell by looking at the token after
7715      the identifier -- if it is a `,', `=', or `>' then we have a type
7716      parameter.  */
7717   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
7718     {
7719       /* Peek at the token after `class' or `typename'.  */
7720       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7721       /* If it's an identifier, skip it.  */
7722       if (token->type == CPP_NAME)
7723         token = cp_lexer_peek_nth_token (parser->lexer, 3);
7724       /* Now, see if the token looks like the end of a template
7725          parameter.  */
7726       if (token->type == CPP_COMMA
7727           || token->type == CPP_EQ
7728           || token->type == CPP_GREATER)
7729         return cp_parser_type_parameter (parser);
7730     }
7731
7732   /* Otherwise, it is a non-type parameter.
7733
7734      [temp.param]
7735
7736      When parsing a default template-argument for a non-type
7737      template-parameter, the first non-nested `>' is taken as the end
7738      of the template parameter-list rather than a greater-than
7739      operator.  */
7740   return
7741     cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
7742                                      /*parenthesized_p=*/NULL);
7743 }
7744
7745 /* Parse a type-parameter.
7746
7747    type-parameter:
7748      class identifier [opt]
7749      class identifier [opt] = type-id
7750      typename identifier [opt]
7751      typename identifier [opt] = type-id
7752      template < template-parameter-list > class identifier [opt]
7753      template < template-parameter-list > class identifier [opt]
7754        = id-expression
7755
7756    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
7757    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
7758    the declaration of the parameter.  */
7759
7760 static tree
7761 cp_parser_type_parameter (cp_parser* parser)
7762 {
7763   cp_token *token;
7764   tree parameter;
7765
7766   /* Look for a keyword to tell us what kind of parameter this is.  */
7767   token = cp_parser_require (parser, CPP_KEYWORD,
7768                              "`class', `typename', or `template'");
7769   if (!token)
7770     return error_mark_node;
7771
7772   switch (token->keyword)
7773     {
7774     case RID_CLASS:
7775     case RID_TYPENAME:
7776       {
7777         tree identifier;
7778         tree default_argument;
7779
7780         /* If the next token is an identifier, then it names the
7781            parameter.  */
7782         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
7783           identifier = cp_parser_identifier (parser);
7784         else
7785           identifier = NULL_TREE;
7786
7787         /* Create the parameter.  */
7788         parameter = finish_template_type_parm (class_type_node, identifier);
7789
7790         /* If the next token is an `=', we have a default argument.  */
7791         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7792           {
7793             /* Consume the `=' token.  */
7794             cp_lexer_consume_token (parser->lexer);
7795             /* Parse the default-argument.  */
7796             default_argument = cp_parser_type_id (parser);
7797           }
7798         else
7799           default_argument = NULL_TREE;
7800
7801         /* Create the combined representation of the parameter and the
7802            default argument.  */
7803         parameter = build_tree_list (default_argument, parameter);
7804       }
7805       break;
7806
7807     case RID_TEMPLATE:
7808       {
7809         tree parameter_list;
7810         tree identifier;
7811         tree default_argument;
7812
7813         /* Look for the `<'.  */
7814         cp_parser_require (parser, CPP_LESS, "`<'");
7815         /* Parse the template-parameter-list.  */
7816         begin_template_parm_list ();
7817         parameter_list
7818           = cp_parser_template_parameter_list (parser);
7819         parameter_list = end_template_parm_list (parameter_list);
7820         /* Look for the `>'.  */
7821         cp_parser_require (parser, CPP_GREATER, "`>'");
7822         /* Look for the `class' keyword.  */
7823         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
7824         /* If the next token is an `=', then there is a
7825            default-argument.  If the next token is a `>', we are at
7826            the end of the parameter-list.  If the next token is a `,',
7827            then we are at the end of this parameter.  */
7828         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
7829             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
7830             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7831           identifier = cp_parser_identifier (parser);
7832         else
7833           identifier = NULL_TREE;
7834         /* Create the template parameter.  */
7835         parameter = finish_template_template_parm (class_type_node,
7836                                                    identifier);
7837
7838         /* If the next token is an `=', then there is a
7839            default-argument.  */
7840         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7841           {
7842             bool is_template;
7843
7844             /* Consume the `='.  */
7845             cp_lexer_consume_token (parser->lexer);
7846             /* Parse the id-expression.  */
7847             default_argument
7848               = cp_parser_id_expression (parser,
7849                                          /*template_keyword_p=*/false,
7850                                          /*check_dependency_p=*/true,
7851                                          /*template_p=*/&is_template,
7852                                          /*declarator_p=*/false);
7853             if (TREE_CODE (default_argument) == TYPE_DECL)
7854               /* If the id-expression was a template-id that refers to
7855                  a template-class, we already have the declaration here,
7856                  so no further lookup is needed.  */
7857                  ;
7858             else
7859               /* Look up the name.  */
7860               default_argument
7861                 = cp_parser_lookup_name (parser, default_argument,
7862                                         /*is_type=*/false,
7863                                         /*is_template=*/is_template,
7864                                         /*is_namespace=*/false,
7865                                         /*check_dependency=*/true);
7866             /* See if the default argument is valid.  */
7867             default_argument
7868               = check_template_template_default_arg (default_argument);
7869           }
7870         else
7871           default_argument = NULL_TREE;
7872
7873         /* Create the combined representation of the parameter and the
7874            default argument.  */
7875         parameter =  build_tree_list (default_argument, parameter);
7876       }
7877       break;
7878
7879     default:
7880       /* Anything else is an error.  */
7881       cp_parser_error (parser,
7882                        "expected `class', `typename', or `template'");
7883       parameter = error_mark_node;
7884     }
7885
7886   return parameter;
7887 }
7888
7889 /* Parse a template-id.
7890
7891    template-id:
7892      template-name < template-argument-list [opt] >
7893
7894    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
7895    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
7896    returned.  Otherwise, if the template-name names a function, or set
7897    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
7898    names a class, returns a TYPE_DECL for the specialization.
7899
7900    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
7901    uninstantiated templates.  */
7902
7903 static tree
7904 cp_parser_template_id (cp_parser *parser,
7905                        bool template_keyword_p,
7906                        bool check_dependency_p,
7907                        bool is_declaration)
7908 {
7909   tree template;
7910   tree arguments;
7911   tree template_id;
7912   ptrdiff_t start_of_id;
7913   tree access_check = NULL_TREE;
7914   cp_token *next_token, *next_token_2;
7915   bool is_identifier;
7916
7917   /* If the next token corresponds to a template-id, there is no need
7918      to reparse it.  */
7919   next_token = cp_lexer_peek_token (parser->lexer);
7920   if (next_token->type == CPP_TEMPLATE_ID)
7921     {
7922       tree value;
7923       tree check;
7924
7925       /* Get the stored value.  */
7926       value = cp_lexer_consume_token (parser->lexer)->value;
7927       /* Perform any access checks that were deferred.  */
7928       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
7929         perform_or_defer_access_check (TREE_PURPOSE (check),
7930                                        TREE_VALUE (check));
7931       /* Return the stored value.  */
7932       return TREE_VALUE (value);
7933     }
7934
7935   /* Avoid performing name lookup if there is no possibility of
7936      finding a template-id.  */
7937   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
7938       || (next_token->type == CPP_NAME
7939           && !cp_parser_nth_token_starts_template_argument_list_p
7940                (parser, 2)))
7941     {
7942       cp_parser_error (parser, "expected template-id");
7943       return error_mark_node;
7944     }
7945
7946   /* Remember where the template-id starts.  */
7947   if (cp_parser_parsing_tentatively (parser)
7948       && !cp_parser_committed_to_tentative_parse (parser))
7949     {
7950       next_token = cp_lexer_peek_token (parser->lexer);
7951       start_of_id = cp_lexer_token_difference (parser->lexer,
7952                                                parser->lexer->first_token,
7953                                                next_token);
7954     }
7955   else
7956     start_of_id = -1;
7957
7958   push_deferring_access_checks (dk_deferred);
7959
7960   /* Parse the template-name.  */
7961   is_identifier = false;
7962   template = cp_parser_template_name (parser, template_keyword_p,
7963                                       check_dependency_p,
7964                                       is_declaration,
7965                                       &is_identifier);
7966   if (template == error_mark_node || is_identifier)
7967     {
7968       pop_deferring_access_checks ();
7969       return template;
7970     }
7971
7972   /* If we find the sequence `[:' after a template-name, it's probably
7973      a digraph-typo for `< ::'. Substitute the tokens and check if we can
7974      parse correctly the argument list.  */
7975   next_token = cp_lexer_peek_nth_token (parser->lexer, 1);
7976   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7977   if (next_token->type == CPP_OPEN_SQUARE
7978       && next_token->flags & DIGRAPH
7979       && next_token_2->type == CPP_COLON
7980       && !(next_token_2->flags & PREV_WHITE))
7981     {
7982       cp_parser_parse_tentatively (parser);
7983       /* Change `:' into `::'.  */
7984       next_token_2->type = CPP_SCOPE;
7985       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
7986          CPP_LESS.  */
7987       cp_lexer_consume_token (parser->lexer);
7988       /* Parse the arguments.  */
7989       arguments = cp_parser_enclosed_template_argument_list (parser);
7990       if (!cp_parser_parse_definitely (parser))
7991         {
7992           /* If we couldn't parse an argument list, then we revert our changes
7993              and return simply an error. Maybe this is not a template-id
7994              after all.  */
7995           next_token_2->type = CPP_COLON;
7996           cp_parser_error (parser, "expected `<'");
7997           pop_deferring_access_checks ();
7998           return error_mark_node;
7999         }
8000       /* Otherwise, emit an error about the invalid digraph, but continue
8001          parsing because we got our argument list.  */
8002       pedwarn ("`<::' cannot begin a template-argument list");
8003       inform ("`<:' is an alternate spelling for `['. Insert whitespace "
8004               "between `<' and `::'");
8005       if (!flag_permissive)
8006         {
8007           static bool hint;
8008           if (!hint)
8009             {
8010               inform ("(if you use `-fpermissive' G++ will accept your code)");
8011               hint = true;
8012             }
8013         }
8014     }
8015   else
8016     {
8017       /* Look for the `<' that starts the template-argument-list.  */
8018       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8019         {
8020           pop_deferring_access_checks ();
8021           return error_mark_node;
8022         }
8023       /* Parse the arguments.  */
8024       arguments = cp_parser_enclosed_template_argument_list (parser);
8025     }
8026
8027   /* Build a representation of the specialization.  */
8028   if (TREE_CODE (template) == IDENTIFIER_NODE)
8029     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8030   else if (DECL_CLASS_TEMPLATE_P (template)
8031            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8032     template_id
8033       = finish_template_type (template, arguments,
8034                               cp_lexer_next_token_is (parser->lexer,
8035                                                       CPP_SCOPE));
8036   else
8037     {
8038       /* If it's not a class-template or a template-template, it should be
8039          a function-template.  */
8040       my_friendly_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8041                            || TREE_CODE (template) == OVERLOAD
8042                            || BASELINK_P (template)),
8043                           20010716);
8044
8045       template_id = lookup_template_function (template, arguments);
8046     }
8047
8048   /* Retrieve any deferred checks.  Do not pop this access checks yet
8049      so the memory will not be reclaimed during token replacing below.  */
8050   access_check = get_deferred_access_checks ();
8051
8052   /* If parsing tentatively, replace the sequence of tokens that makes
8053      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8054      should we re-parse the token stream, we will not have to repeat
8055      the effort required to do the parse, nor will we issue duplicate
8056      error messages about problems during instantiation of the
8057      template.  */
8058   if (start_of_id >= 0)
8059     {
8060       cp_token *token;
8061
8062       /* Find the token that corresponds to the start of the
8063          template-id.  */
8064       token = cp_lexer_advance_token (parser->lexer,
8065                                       parser->lexer->first_token,
8066                                       start_of_id);
8067
8068       /* Reset the contents of the START_OF_ID token.  */
8069       token->type = CPP_TEMPLATE_ID;
8070       token->value = build_tree_list (access_check, template_id);
8071       token->keyword = RID_MAX;
8072       /* Purge all subsequent tokens.  */
8073       cp_lexer_purge_tokens_after (parser->lexer, token);
8074     }
8075
8076   pop_deferring_access_checks ();
8077   return template_id;
8078 }
8079
8080 /* Parse a template-name.
8081
8082    template-name:
8083      identifier
8084
8085    The standard should actually say:
8086
8087    template-name:
8088      identifier
8089      operator-function-id
8090
8091    A defect report has been filed about this issue.
8092
8093    A conversion-function-id cannot be a template name because they cannot
8094    be part of a template-id. In fact, looking at this code:
8095
8096    a.operator K<int>()
8097
8098    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8099    It is impossible to call a templated conversion-function-id with an
8100    explicit argument list, since the only allowed template parameter is
8101    the type to which it is converting.
8102
8103    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8104    `template' keyword, in a construction like:
8105
8106      T::template f<3>()
8107
8108    In that case `f' is taken to be a template-name, even though there
8109    is no way of knowing for sure.
8110
8111    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8112    name refers to a set of overloaded functions, at least one of which
8113    is a template, or an IDENTIFIER_NODE with the name of the template,
8114    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8115    names are looked up inside uninstantiated templates.  */
8116
8117 static tree
8118 cp_parser_template_name (cp_parser* parser,
8119                          bool template_keyword_p,
8120                          bool check_dependency_p,
8121                          bool is_declaration,
8122                          bool *is_identifier)
8123 {
8124   tree identifier;
8125   tree decl;
8126   tree fns;
8127
8128   /* If the next token is `operator', then we have either an
8129      operator-function-id or a conversion-function-id.  */
8130   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8131     {
8132       /* We don't know whether we're looking at an
8133          operator-function-id or a conversion-function-id.  */
8134       cp_parser_parse_tentatively (parser);
8135       /* Try an operator-function-id.  */
8136       identifier = cp_parser_operator_function_id (parser);
8137       /* If that didn't work, try a conversion-function-id.  */
8138       if (!cp_parser_parse_definitely (parser))
8139         {
8140           cp_parser_error (parser, "expected template-name");
8141           return error_mark_node;
8142         }
8143     }
8144   /* Look for the identifier.  */
8145   else
8146     identifier = cp_parser_identifier (parser);
8147
8148   /* If we didn't find an identifier, we don't have a template-id.  */
8149   if (identifier == error_mark_node)
8150     return error_mark_node;
8151
8152   /* If the name immediately followed the `template' keyword, then it
8153      is a template-name.  However, if the next token is not `<', then
8154      we do not treat it as a template-name, since it is not being used
8155      as part of a template-id.  This enables us to handle constructs
8156      like:
8157
8158        template <typename T> struct S { S(); };
8159        template <typename T> S<T>::S();
8160
8161      correctly.  We would treat `S' as a template -- if it were `S<T>'
8162      -- but we do not if there is no `<'.  */
8163
8164   if (processing_template_decl
8165       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8166     {
8167       /* In a declaration, in a dependent context, we pretend that the
8168          "template" keyword was present in order to improve error
8169          recovery.  For example, given:
8170
8171            template <typename T> void f(T::X<int>);
8172
8173          we want to treat "X<int>" as a template-id.  */
8174       if (is_declaration
8175           && !template_keyword_p
8176           && parser->scope && TYPE_P (parser->scope)
8177           && dependent_type_p (parser->scope))
8178         {
8179           ptrdiff_t start;
8180           cp_token* token;
8181           /* Explain what went wrong.  */
8182           error ("non-template `%D' used as template", identifier);
8183           error ("(use `%T::template %D' to indicate that it is a template)",
8184                  parser->scope, identifier);
8185           /* If parsing tentatively, find the location of the "<"
8186              token.  */
8187           if (cp_parser_parsing_tentatively (parser)
8188               && !cp_parser_committed_to_tentative_parse (parser))
8189             {
8190               cp_parser_simulate_error (parser);
8191               token = cp_lexer_peek_token (parser->lexer);
8192               token = cp_lexer_prev_token (parser->lexer, token);
8193               start = cp_lexer_token_difference (parser->lexer,
8194                                                  parser->lexer->first_token,
8195                                                  token);
8196             }
8197           else
8198             start = -1;
8199           /* Parse the template arguments so that we can issue error
8200              messages about them.  */
8201           cp_lexer_consume_token (parser->lexer);
8202           cp_parser_enclosed_template_argument_list (parser);
8203           /* Skip tokens until we find a good place from which to
8204              continue parsing.  */
8205           cp_parser_skip_to_closing_parenthesis (parser,
8206                                                  /*recovering=*/true,
8207                                                  /*or_comma=*/true,
8208                                                  /*consume_paren=*/false);
8209           /* If parsing tentatively, permanently remove the
8210              template argument list.  That will prevent duplicate
8211              error messages from being issued about the missing
8212              "template" keyword.  */
8213           if (start >= 0)
8214             {
8215               token = cp_lexer_advance_token (parser->lexer,
8216                                               parser->lexer->first_token,
8217                                               start);
8218               cp_lexer_purge_tokens_after (parser->lexer, token);
8219             }
8220           if (is_identifier)
8221             *is_identifier = true;
8222           return identifier;
8223         }
8224       if (template_keyword_p)
8225         return identifier;
8226     }
8227
8228   /* Look up the name.  */
8229   decl = cp_parser_lookup_name (parser, identifier,
8230                                 /*is_type=*/false,
8231                                 /*is_template=*/false,
8232                                 /*is_namespace=*/false,
8233                                 check_dependency_p);
8234   decl = maybe_get_template_decl_from_type_decl (decl);
8235
8236   /* If DECL is a template, then the name was a template-name.  */
8237   if (TREE_CODE (decl) == TEMPLATE_DECL)
8238     ;
8239   else
8240     {
8241       /* The standard does not explicitly indicate whether a name that
8242          names a set of overloaded declarations, some of which are
8243          templates, is a template-name.  However, such a name should
8244          be a template-name; otherwise, there is no way to form a
8245          template-id for the overloaded templates.  */
8246       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8247       if (TREE_CODE (fns) == OVERLOAD)
8248         {
8249           tree fn;
8250
8251           for (fn = fns; fn; fn = OVL_NEXT (fn))
8252             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8253               break;
8254         }
8255       else
8256         {
8257           /* Otherwise, the name does not name a template.  */
8258           cp_parser_error (parser, "expected template-name");
8259           return error_mark_node;
8260         }
8261     }
8262
8263   /* If DECL is dependent, and refers to a function, then just return
8264      its name; we will look it up again during template instantiation.  */
8265   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8266     {
8267       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8268       if (TYPE_P (scope) && dependent_type_p (scope))
8269         return identifier;
8270     }
8271
8272   return decl;
8273 }
8274
8275 /* Parse a template-argument-list.
8276
8277    template-argument-list:
8278      template-argument
8279      template-argument-list , template-argument
8280
8281    Returns a TREE_VEC containing the arguments.  */
8282
8283 static tree
8284 cp_parser_template_argument_list (cp_parser* parser)
8285 {
8286   tree fixed_args[10];
8287   unsigned n_args = 0;
8288   unsigned alloced = 10;
8289   tree *arg_ary = fixed_args;
8290   tree vec;
8291   bool saved_in_template_argument_list_p;
8292
8293   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8294   parser->in_template_argument_list_p = true;
8295   do
8296     {
8297       tree argument;
8298
8299       if (n_args)
8300         /* Consume the comma.  */
8301         cp_lexer_consume_token (parser->lexer);
8302
8303       /* Parse the template-argument.  */
8304       argument = cp_parser_template_argument (parser);
8305       if (n_args == alloced)
8306         {
8307           alloced *= 2;
8308
8309           if (arg_ary == fixed_args)
8310             {
8311               arg_ary = xmalloc (sizeof (tree) * alloced);
8312               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8313             }
8314           else
8315             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8316         }
8317       arg_ary[n_args++] = argument;
8318     }
8319   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8320
8321   vec = make_tree_vec (n_args);
8322
8323   while (n_args--)
8324     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8325
8326   if (arg_ary != fixed_args)
8327     free (arg_ary);
8328   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8329   return vec;
8330 }
8331
8332 /* Parse a template-argument.
8333
8334    template-argument:
8335      assignment-expression
8336      type-id
8337      id-expression
8338
8339    The representation is that of an assignment-expression, type-id, or
8340    id-expression -- except that the qualified id-expression is
8341    evaluated, so that the value returned is either a DECL or an
8342    OVERLOAD.
8343
8344    Although the standard says "assignment-expression", it forbids
8345    throw-expressions or assignments in the template argument.
8346    Therefore, we use "conditional-expression" instead.  */
8347
8348 static tree
8349 cp_parser_template_argument (cp_parser* parser)
8350 {
8351   tree argument;
8352   bool template_p;
8353   bool address_p;
8354   bool maybe_type_id = false;
8355   cp_token *token;
8356   cp_id_kind idk;
8357   tree qualifying_class;
8358
8359   /* There's really no way to know what we're looking at, so we just
8360      try each alternative in order.
8361
8362        [temp.arg]
8363
8364        In a template-argument, an ambiguity between a type-id and an
8365        expression is resolved to a type-id, regardless of the form of
8366        the corresponding template-parameter.
8367
8368      Therefore, we try a type-id first.  */
8369   cp_parser_parse_tentatively (parser);
8370   argument = cp_parser_type_id (parser);
8371   /* If there was no error parsing the type-id but the next token is a '>>',
8372      we probably found a typo for '> >'. But there are type-id which are
8373      also valid expressions. For instance:
8374
8375      struct X { int operator >> (int); };
8376      template <int V> struct Foo {};
8377      Foo<X () >> 5> r;
8378
8379      Here 'X()' is a valid type-id of a function type, but the user just
8380      wanted to write the expression "X() >> 5". Thus, we remember that we
8381      found a valid type-id, but we still try to parse the argument as an
8382      expression to see what happens.  */
8383   if (!cp_parser_error_occurred (parser)
8384       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8385     {
8386       maybe_type_id = true;
8387       cp_parser_abort_tentative_parse (parser);
8388     }
8389   else
8390     {
8391       /* If the next token isn't a `,' or a `>', then this argument wasn't
8392       really finished. This means that the argument is not a valid
8393       type-id.  */
8394       if (!cp_parser_next_token_ends_template_argument_p (parser))
8395         cp_parser_error (parser, "expected template-argument");
8396       /* If that worked, we're done.  */
8397       if (cp_parser_parse_definitely (parser))
8398         return argument;
8399     }
8400   /* We're still not sure what the argument will be.  */
8401   cp_parser_parse_tentatively (parser);
8402   /* Try a template.  */
8403   argument = cp_parser_id_expression (parser,
8404                                       /*template_keyword_p=*/false,
8405                                       /*check_dependency_p=*/true,
8406                                       &template_p,
8407                                       /*declarator_p=*/false);
8408   /* If the next token isn't a `,' or a `>', then this argument wasn't
8409      really finished.  */
8410   if (!cp_parser_next_token_ends_template_argument_p (parser))
8411     cp_parser_error (parser, "expected template-argument");
8412   if (!cp_parser_error_occurred (parser))
8413     {
8414       /* Figure out what is being referred to.  */
8415       argument = cp_parser_lookup_name (parser, argument,
8416                                         /*is_type=*/false,
8417                                         /*is_template=*/template_p,
8418                                         /*is_namespace=*/false,
8419                                         /*check_dependency=*/true);
8420       if (TREE_CODE (argument) != TEMPLATE_DECL
8421           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8422         cp_parser_error (parser, "expected template-name");
8423     }
8424   if (cp_parser_parse_definitely (parser))
8425     return argument;
8426   /* It must be a non-type argument.  There permitted cases are given
8427      in [temp.arg.nontype]:
8428
8429      -- an integral constant-expression of integral or enumeration
8430         type; or
8431
8432      -- the name of a non-type template-parameter; or
8433
8434      -- the name of an object or function with external linkage...
8435
8436      -- the address of an object or function with external linkage...
8437
8438      -- a pointer to member...  */
8439   /* Look for a non-type template parameter.  */
8440   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8441     {
8442       cp_parser_parse_tentatively (parser);
8443       argument = cp_parser_primary_expression (parser,
8444                                                &idk,
8445                                                &qualifying_class);
8446       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8447           || !cp_parser_next_token_ends_template_argument_p (parser))
8448         cp_parser_simulate_error (parser);
8449       if (cp_parser_parse_definitely (parser))
8450         return argument;
8451     }
8452   /* If the next token is "&", the argument must be the address of an
8453      object or function with external linkage.  */
8454   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8455   if (address_p)
8456     cp_lexer_consume_token (parser->lexer);
8457   /* See if we might have an id-expression.  */
8458   token = cp_lexer_peek_token (parser->lexer);
8459   if (token->type == CPP_NAME
8460       || token->keyword == RID_OPERATOR
8461       || token->type == CPP_SCOPE
8462       || token->type == CPP_TEMPLATE_ID
8463       || token->type == CPP_NESTED_NAME_SPECIFIER)
8464     {
8465       cp_parser_parse_tentatively (parser);
8466       argument = cp_parser_primary_expression (parser,
8467                                                &idk,
8468                                                &qualifying_class);
8469       if (cp_parser_error_occurred (parser)
8470           || !cp_parser_next_token_ends_template_argument_p (parser))
8471         cp_parser_abort_tentative_parse (parser);
8472       else
8473         {
8474           if (qualifying_class)
8475             argument = finish_qualified_id_expr (qualifying_class,
8476                                                  argument,
8477                                                  /*done=*/true,
8478                                                  address_p);
8479           if (TREE_CODE (argument) == VAR_DECL)
8480             {
8481               /* A variable without external linkage might still be a
8482                  valid constant-expression, so no error is issued here
8483                  if the external-linkage check fails.  */
8484               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8485                 cp_parser_simulate_error (parser);
8486             }
8487           else if (is_overloaded_fn (argument))
8488             /* All overloaded functions are allowed; if the external
8489                linkage test does not pass, an error will be issued
8490                later.  */
8491             ;
8492           else if (address_p
8493                    && (TREE_CODE (argument) == OFFSET_REF
8494                        || TREE_CODE (argument) == SCOPE_REF))
8495             /* A pointer-to-member.  */
8496             ;
8497           else
8498             cp_parser_simulate_error (parser);
8499
8500           if (cp_parser_parse_definitely (parser))
8501             {
8502               if (address_p)
8503                 argument = build_x_unary_op (ADDR_EXPR, argument);
8504               return argument;
8505             }
8506         }
8507     }
8508   /* If the argument started with "&", there are no other valid
8509      alternatives at this point.  */
8510   if (address_p)
8511     {
8512       cp_parser_error (parser, "invalid non-type template argument");
8513       return error_mark_node;
8514     }
8515   /* If the argument wasn't successfully parsed as a type-id followed
8516      by '>>', the argument can only be a constant expression now.
8517      Otherwise, we try parsing the constant-expression tentatively,
8518      because the argument could really be a type-id.  */
8519   if (maybe_type_id)
8520     cp_parser_parse_tentatively (parser);
8521   argument = cp_parser_constant_expression (parser,
8522                                             /*allow_non_constant_p=*/false,
8523                                             /*non_constant_p=*/NULL);
8524   argument = fold_non_dependent_expr (argument);
8525   if (!maybe_type_id)
8526     return argument;
8527   if (!cp_parser_next_token_ends_template_argument_p (parser))
8528     cp_parser_error (parser, "expected template-argument");
8529   if (cp_parser_parse_definitely (parser))
8530     return argument;
8531   /* We did our best to parse the argument as a non type-id, but that
8532      was the only alternative that matched (albeit with a '>' after
8533      it). We can assume it's just a typo from the user, and a
8534      diagnostic will then be issued.  */
8535   return cp_parser_type_id (parser);
8536 }
8537
8538 /* Parse an explicit-instantiation.
8539
8540    explicit-instantiation:
8541      template declaration
8542
8543    Although the standard says `declaration', what it really means is:
8544
8545    explicit-instantiation:
8546      template decl-specifier-seq [opt] declarator [opt] ;
8547
8548    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8549    supposed to be allowed.  A defect report has been filed about this
8550    issue.
8551
8552    GNU Extension:
8553
8554    explicit-instantiation:
8555      storage-class-specifier template
8556        decl-specifier-seq [opt] declarator [opt] ;
8557      function-specifier template
8558        decl-specifier-seq [opt] declarator [opt] ;  */
8559
8560 static void
8561 cp_parser_explicit_instantiation (cp_parser* parser)
8562 {
8563   int declares_class_or_enum;
8564   tree decl_specifiers;
8565   tree attributes;
8566   tree extension_specifier = NULL_TREE;
8567
8568   /* Look for an (optional) storage-class-specifier or
8569      function-specifier.  */
8570   if (cp_parser_allow_gnu_extensions_p (parser))
8571     {
8572       extension_specifier
8573         = cp_parser_storage_class_specifier_opt (parser);
8574       if (!extension_specifier)
8575         extension_specifier = cp_parser_function_specifier_opt (parser);
8576     }
8577
8578   /* Look for the `template' keyword.  */
8579   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8580   /* Let the front end know that we are processing an explicit
8581      instantiation.  */
8582   begin_explicit_instantiation ();
8583   /* [temp.explicit] says that we are supposed to ignore access
8584      control while processing explicit instantiation directives.  */
8585   push_deferring_access_checks (dk_no_check);
8586   /* Parse a decl-specifier-seq.  */
8587   decl_specifiers
8588     = cp_parser_decl_specifier_seq (parser,
8589                                     CP_PARSER_FLAGS_OPTIONAL,
8590                                     &attributes,
8591                                     &declares_class_or_enum);
8592   /* If there was exactly one decl-specifier, and it declared a class,
8593      and there's no declarator, then we have an explicit type
8594      instantiation.  */
8595   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8596     {
8597       tree type;
8598
8599       type = check_tag_decl (decl_specifiers);
8600       /* Turn access control back on for names used during
8601          template instantiation.  */
8602       pop_deferring_access_checks ();
8603       if (type)
8604         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8605     }
8606   else
8607     {
8608       tree declarator;
8609       tree decl;
8610
8611       /* Parse the declarator.  */
8612       declarator
8613         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8614                                 /*ctor_dtor_or_conv_p=*/NULL,
8615                                 /*parenthesized_p=*/NULL);
8616       cp_parser_check_for_definition_in_return_type (declarator,
8617                                                      declares_class_or_enum);
8618       if (declarator != error_mark_node)
8619         {
8620           decl = grokdeclarator (declarator, decl_specifiers,
8621                                  NORMAL, 0, NULL);
8622           /* Turn access control back on for names used during
8623              template instantiation.  */
8624           pop_deferring_access_checks ();
8625           /* Do the explicit instantiation.  */
8626           do_decl_instantiation (decl, extension_specifier);
8627         }
8628       else
8629         {
8630           pop_deferring_access_checks ();
8631           /* Skip the body of the explicit instantiation.  */
8632           cp_parser_skip_to_end_of_statement (parser);
8633         }
8634     }
8635   /* We're done with the instantiation.  */
8636   end_explicit_instantiation ();
8637
8638   cp_parser_consume_semicolon_at_end_of_statement (parser);
8639 }
8640
8641 /* Parse an explicit-specialization.
8642
8643    explicit-specialization:
8644      template < > declaration
8645
8646    Although the standard says `declaration', what it really means is:
8647
8648    explicit-specialization:
8649      template <> decl-specifier [opt] init-declarator [opt] ;
8650      template <> function-definition
8651      template <> explicit-specialization
8652      template <> template-declaration  */
8653
8654 static void
8655 cp_parser_explicit_specialization (cp_parser* parser)
8656 {
8657   /* Look for the `template' keyword.  */
8658   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8659   /* Look for the `<'.  */
8660   cp_parser_require (parser, CPP_LESS, "`<'");
8661   /* Look for the `>'.  */
8662   cp_parser_require (parser, CPP_GREATER, "`>'");
8663   /* We have processed another parameter list.  */
8664   ++parser->num_template_parameter_lists;
8665   /* Let the front end know that we are beginning a specialization.  */
8666   begin_specialization ();
8667
8668   /* If the next keyword is `template', we need to figure out whether
8669      or not we're looking a template-declaration.  */
8670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
8671     {
8672       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
8673           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
8674         cp_parser_template_declaration_after_export (parser,
8675                                                      /*member_p=*/false);
8676       else
8677         cp_parser_explicit_specialization (parser);
8678     }
8679   else
8680     /* Parse the dependent declaration.  */
8681     cp_parser_single_declaration (parser,
8682                                   /*member_p=*/false,
8683                                   /*friend_p=*/NULL);
8684
8685   /* We're done with the specialization.  */
8686   end_specialization ();
8687   /* We're done with this parameter list.  */
8688   --parser->num_template_parameter_lists;
8689 }
8690
8691 /* Parse a type-specifier.
8692
8693    type-specifier:
8694      simple-type-specifier
8695      class-specifier
8696      enum-specifier
8697      elaborated-type-specifier
8698      cv-qualifier
8699
8700    GNU Extension:
8701
8702    type-specifier:
8703      __complex__
8704
8705    Returns a representation of the type-specifier.  If the
8706    type-specifier is a keyword (like `int' or `const', or
8707    `__complex__') then the corresponding IDENTIFIER_NODE is returned.
8708    For a class-specifier, enum-specifier, or elaborated-type-specifier
8709    a TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
8710
8711    If IS_FRIEND is TRUE then this type-specifier is being declared a
8712    `friend'.  If IS_DECLARATION is TRUE, then this type-specifier is
8713    appearing in a decl-specifier-seq.
8714
8715    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
8716    class-specifier, enum-specifier, or elaborated-type-specifier, then
8717    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
8718    if a type is declared; 2 if it is defined.  Otherwise, it is set to
8719    zero.
8720
8721    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
8722    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
8723    is set to FALSE.  */
8724
8725 static tree
8726 cp_parser_type_specifier (cp_parser* parser,
8727                           cp_parser_flags flags,
8728                           bool is_friend,
8729                           bool is_declaration,
8730                           int* declares_class_or_enum,
8731                           bool* is_cv_qualifier)
8732 {
8733   tree type_spec = NULL_TREE;
8734   cp_token *token;
8735   enum rid keyword;
8736
8737   /* Assume this type-specifier does not declare a new type.  */
8738   if (declares_class_or_enum)
8739     *declares_class_or_enum = 0;
8740   /* And that it does not specify a cv-qualifier.  */
8741   if (is_cv_qualifier)
8742     *is_cv_qualifier = false;
8743   /* Peek at the next token.  */
8744   token = cp_lexer_peek_token (parser->lexer);
8745
8746   /* If we're looking at a keyword, we can use that to guide the
8747      production we choose.  */
8748   keyword = token->keyword;
8749   switch (keyword)
8750     {
8751       /* Any of these indicate either a class-specifier, or an
8752          elaborated-type-specifier.  */
8753     case RID_CLASS:
8754     case RID_STRUCT:
8755     case RID_UNION:
8756     case RID_ENUM:
8757       /* Parse tentatively so that we can back up if we don't find a
8758          class-specifier or enum-specifier.  */
8759       cp_parser_parse_tentatively (parser);
8760       /* Look for the class-specifier or enum-specifier.  */
8761       if (keyword == RID_ENUM)
8762         type_spec = cp_parser_enum_specifier (parser);
8763       else
8764         type_spec = cp_parser_class_specifier (parser);
8765
8766       /* If that worked, we're done.  */
8767       if (cp_parser_parse_definitely (parser))
8768         {
8769           if (declares_class_or_enum)
8770             *declares_class_or_enum = 2;
8771           return type_spec;
8772         }
8773
8774       /* Fall through.  */
8775
8776     case RID_TYPENAME:
8777       /* Look for an elaborated-type-specifier.  */
8778       type_spec = cp_parser_elaborated_type_specifier (parser,
8779                                                        is_friend,
8780                                                        is_declaration);
8781       /* We're declaring a class or enum -- unless we're using
8782          `typename'.  */
8783       if (declares_class_or_enum && keyword != RID_TYPENAME)
8784         *declares_class_or_enum = 1;
8785       return type_spec;
8786
8787     case RID_CONST:
8788     case RID_VOLATILE:
8789     case RID_RESTRICT:
8790       type_spec = cp_parser_cv_qualifier_opt (parser);
8791       /* Even though we call a routine that looks for an optional
8792          qualifier, we know that there should be one.  */
8793       my_friendly_assert (type_spec != NULL, 20000328);
8794       /* This type-specifier was a cv-qualified.  */
8795       if (is_cv_qualifier)
8796         *is_cv_qualifier = true;
8797
8798       return type_spec;
8799
8800     case RID_COMPLEX:
8801       /* The `__complex__' keyword is a GNU extension.  */
8802       return cp_lexer_consume_token (parser->lexer)->value;
8803
8804     default:
8805       break;
8806     }
8807
8808   /* If we do not already have a type-specifier, assume we are looking
8809      at a simple-type-specifier.  */
8810   type_spec = cp_parser_simple_type_specifier (parser, flags,
8811                                                /*identifier_p=*/true);
8812
8813   /* If we didn't find a type-specifier, and a type-specifier was not
8814      optional in this context, issue an error message.  */
8815   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8816     {
8817       cp_parser_error (parser, "expected type specifier");
8818       return error_mark_node;
8819     }
8820
8821   return type_spec;
8822 }
8823
8824 /* Parse a simple-type-specifier.
8825
8826    simple-type-specifier:
8827      :: [opt] nested-name-specifier [opt] type-name
8828      :: [opt] nested-name-specifier template template-id
8829      char
8830      wchar_t
8831      bool
8832      short
8833      int
8834      long
8835      signed
8836      unsigned
8837      float
8838      double
8839      void
8840
8841    GNU Extension:
8842
8843    simple-type-specifier:
8844      __typeof__ unary-expression
8845      __typeof__ ( type-id )
8846
8847    For the various keywords, the value returned is simply the
8848    TREE_IDENTIFIER representing the keyword if IDENTIFIER_P is true.
8849    For the first two productions, and if IDENTIFIER_P is false, the
8850    value returned is the indicated TYPE_DECL.  */
8851
8852 static tree
8853 cp_parser_simple_type_specifier (cp_parser* parser, cp_parser_flags flags,
8854                                  bool identifier_p)
8855 {
8856   tree type = NULL_TREE;
8857   cp_token *token;
8858
8859   /* Peek at the next token.  */
8860   token = cp_lexer_peek_token (parser->lexer);
8861
8862   /* If we're looking at a keyword, things are easy.  */
8863   switch (token->keyword)
8864     {
8865     case RID_CHAR:
8866       type = char_type_node;
8867       break;
8868     case RID_WCHAR:
8869       type = wchar_type_node;
8870       break;
8871     case RID_BOOL:
8872       type = boolean_type_node;
8873       break;
8874     case RID_SHORT:
8875       type = short_integer_type_node;
8876       break;
8877     case RID_INT:
8878       type = integer_type_node;
8879       break;
8880     case RID_LONG:
8881       type = long_integer_type_node;
8882       break;
8883     case RID_SIGNED:
8884       type = integer_type_node;
8885       break;
8886     case RID_UNSIGNED:
8887       type = unsigned_type_node;
8888       break;
8889     case RID_FLOAT:
8890       type = float_type_node;
8891       break;
8892     case RID_DOUBLE:
8893       type = double_type_node;
8894       break;
8895     case RID_VOID:
8896       type = void_type_node;
8897       break;
8898
8899     case RID_TYPEOF:
8900       {
8901         tree operand;
8902
8903         /* Consume the `typeof' token.  */
8904         cp_lexer_consume_token (parser->lexer);
8905         /* Parse the operand to `typeof'.  */
8906         operand = cp_parser_sizeof_operand (parser, RID_TYPEOF);
8907         /* If it is not already a TYPE, take its type.  */
8908         if (!TYPE_P (operand))
8909           operand = finish_typeof (operand);
8910
8911         return operand;
8912       }
8913
8914     default:
8915       break;
8916     }
8917
8918   /* If the type-specifier was for a built-in type, we're done.  */
8919   if (type)
8920     {
8921       tree id;
8922
8923       /* Consume the token.  */
8924       id = cp_lexer_consume_token (parser->lexer)->value;
8925
8926       /* There is no valid C++ program where a non-template type is
8927          followed by a "<".  That usually indicates that the user thought
8928          that the type was a template.  */
8929       cp_parser_check_for_invalid_template_id (parser, type);
8930
8931       return identifier_p ? id : TYPE_NAME (type);
8932     }
8933
8934   /* The type-specifier must be a user-defined type.  */
8935   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
8936     {
8937       /* Don't gobble tokens or issue error messages if this is an
8938          optional type-specifier.  */
8939       if (flags & CP_PARSER_FLAGS_OPTIONAL)
8940         cp_parser_parse_tentatively (parser);
8941
8942       /* Look for the optional `::' operator.  */
8943       cp_parser_global_scope_opt (parser,
8944                                   /*current_scope_valid_p=*/false);
8945       /* Look for the nested-name specifier.  */
8946       cp_parser_nested_name_specifier_opt (parser,
8947                                            /*typename_keyword_p=*/false,
8948                                            /*check_dependency_p=*/true,
8949                                            /*type_p=*/false,
8950                                            /*is_declaration=*/false);
8951       /* If we have seen a nested-name-specifier, and the next token
8952          is `template', then we are using the template-id production.  */
8953       if (parser->scope
8954           && cp_parser_optional_template_keyword (parser))
8955         {
8956           /* Look for the template-id.  */
8957           type = cp_parser_template_id (parser,
8958                                         /*template_keyword_p=*/true,
8959                                         /*check_dependency_p=*/true,
8960                                         /*is_declaration=*/false);
8961           /* If the template-id did not name a type, we are out of
8962              luck.  */
8963           if (TREE_CODE (type) != TYPE_DECL)
8964             {
8965               cp_parser_error (parser, "expected template-id for type");
8966               type = NULL_TREE;
8967             }
8968         }
8969       /* Otherwise, look for a type-name.  */
8970       else
8971         type = cp_parser_type_name (parser);
8972       /* If it didn't work out, we don't have a TYPE.  */
8973       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
8974           && !cp_parser_parse_definitely (parser))
8975         type = NULL_TREE;
8976     }
8977
8978   /* If we didn't get a type-name, issue an error message.  */
8979   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
8980     {
8981       cp_parser_error (parser, "expected type-name");
8982       return error_mark_node;
8983     }
8984
8985   /* There is no valid C++ program where a non-template type is
8986      followed by a "<".  That usually indicates that the user thought
8987      that the type was a template.  */
8988   if (type && type != error_mark_node)
8989     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
8990
8991   return type;
8992 }
8993
8994 /* Parse a type-name.
8995
8996    type-name:
8997      class-name
8998      enum-name
8999      typedef-name
9000
9001    enum-name:
9002      identifier
9003
9004    typedef-name:
9005      identifier
9006
9007    Returns a TYPE_DECL for the the type.  */
9008
9009 static tree
9010 cp_parser_type_name (cp_parser* parser)
9011 {
9012   tree type_decl;
9013   tree identifier;
9014
9015   /* We can't know yet whether it is a class-name or not.  */
9016   cp_parser_parse_tentatively (parser);
9017   /* Try a class-name.  */
9018   type_decl = cp_parser_class_name (parser,
9019                                     /*typename_keyword_p=*/false,
9020                                     /*template_keyword_p=*/false,
9021                                     /*type_p=*/false,
9022                                     /*check_dependency_p=*/true,
9023                                     /*class_head_p=*/false,
9024                                     /*is_declaration=*/false);
9025   /* If it's not a class-name, keep looking.  */
9026   if (!cp_parser_parse_definitely (parser))
9027     {
9028       /* It must be a typedef-name or an enum-name.  */
9029       identifier = cp_parser_identifier (parser);
9030       if (identifier == error_mark_node)
9031         return error_mark_node;
9032
9033       /* Look up the type-name.  */
9034       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9035       /* Issue an error if we did not find a type-name.  */
9036       if (TREE_CODE (type_decl) != TYPE_DECL)
9037         {
9038           if (!cp_parser_simulate_error (parser))
9039             cp_parser_name_lookup_error (parser, identifier, type_decl,
9040                                          "is not a type");
9041           type_decl = error_mark_node;
9042         }
9043       /* Remember that the name was used in the definition of the
9044          current class so that we can check later to see if the
9045          meaning would have been different after the class was
9046          entirely defined.  */
9047       else if (type_decl != error_mark_node
9048                && !parser->scope)
9049         maybe_note_name_used_in_class (identifier, type_decl);
9050     }
9051
9052   return type_decl;
9053 }
9054
9055
9056 /* Parse an elaborated-type-specifier.  Note that the grammar given
9057    here incorporates the resolution to DR68.
9058
9059    elaborated-type-specifier:
9060      class-key :: [opt] nested-name-specifier [opt] identifier
9061      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9062      enum :: [opt] nested-name-specifier [opt] identifier
9063      typename :: [opt] nested-name-specifier identifier
9064      typename :: [opt] nested-name-specifier template [opt]
9065        template-id
9066
9067    GNU extension:
9068
9069    elaborated-type-specifier:
9070      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9071      class-key attributes :: [opt] nested-name-specifier [opt]
9072                template [opt] template-id
9073      enum attributes :: [opt] nested-name-specifier [opt] identifier
9074
9075    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9076    declared `friend'.  If IS_DECLARATION is TRUE, then this
9077    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9078    something is being declared.
9079
9080    Returns the TYPE specified.  */
9081
9082 static tree
9083 cp_parser_elaborated_type_specifier (cp_parser* parser,
9084                                      bool is_friend,
9085                                      bool is_declaration)
9086 {
9087   enum tag_types tag_type;
9088   tree identifier;
9089   tree type = NULL_TREE;
9090   tree attributes = NULL_TREE;
9091
9092   /* See if we're looking at the `enum' keyword.  */
9093   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9094     {
9095       /* Consume the `enum' token.  */
9096       cp_lexer_consume_token (parser->lexer);
9097       /* Remember that it's an enumeration type.  */
9098       tag_type = enum_type;
9099       /* Parse the attributes.  */
9100       attributes = cp_parser_attributes_opt (parser);
9101     }
9102   /* Or, it might be `typename'.  */
9103   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9104                                            RID_TYPENAME))
9105     {
9106       /* Consume the `typename' token.  */
9107       cp_lexer_consume_token (parser->lexer);
9108       /* Remember that it's a `typename' type.  */
9109       tag_type = typename_type;
9110       /* The `typename' keyword is only allowed in templates.  */
9111       if (!processing_template_decl)
9112         pedwarn ("using `typename' outside of template");
9113     }
9114   /* Otherwise it must be a class-key.  */
9115   else
9116     {
9117       tag_type = cp_parser_class_key (parser);
9118       if (tag_type == none_type)
9119         return error_mark_node;
9120       /* Parse the attributes.  */
9121       attributes = cp_parser_attributes_opt (parser);
9122     }
9123
9124   /* Look for the `::' operator.  */
9125   cp_parser_global_scope_opt (parser,
9126                               /*current_scope_valid_p=*/false);
9127   /* Look for the nested-name-specifier.  */
9128   if (tag_type == typename_type)
9129     {
9130       if (cp_parser_nested_name_specifier (parser,
9131                                            /*typename_keyword_p=*/true,
9132                                            /*check_dependency_p=*/true,
9133                                            /*type_p=*/true,
9134                                            is_declaration)
9135           == error_mark_node)
9136         return error_mark_node;
9137     }
9138   else
9139     /* Even though `typename' is not present, the proposed resolution
9140        to Core Issue 180 says that in `class A<T>::B', `B' should be
9141        considered a type-name, even if `A<T>' is dependent.  */
9142     cp_parser_nested_name_specifier_opt (parser,
9143                                          /*typename_keyword_p=*/true,
9144                                          /*check_dependency_p=*/true,
9145                                          /*type_p=*/true,
9146                                          is_declaration);
9147   /* For everything but enumeration types, consider a template-id.  */
9148   if (tag_type != enum_type)
9149     {
9150       bool template_p = false;
9151       tree decl;
9152
9153       /* Allow the `template' keyword.  */
9154       template_p = cp_parser_optional_template_keyword (parser);
9155       /* If we didn't see `template', we don't know if there's a
9156          template-id or not.  */
9157       if (!template_p)
9158         cp_parser_parse_tentatively (parser);
9159       /* Parse the template-id.  */
9160       decl = cp_parser_template_id (parser, template_p,
9161                                     /*check_dependency_p=*/true,
9162                                     is_declaration);
9163       /* If we didn't find a template-id, look for an ordinary
9164          identifier.  */
9165       if (!template_p && !cp_parser_parse_definitely (parser))
9166         ;
9167       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9168          in effect, then we must assume that, upon instantiation, the
9169          template will correspond to a class.  */
9170       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9171                && tag_type == typename_type)
9172         type = make_typename_type (parser->scope, decl,
9173                                    /*complain=*/1);
9174       else
9175         type = TREE_TYPE (decl);
9176     }
9177
9178   /* For an enumeration type, consider only a plain identifier.  */
9179   if (!type)
9180     {
9181       identifier = cp_parser_identifier (parser);
9182
9183       if (identifier == error_mark_node)
9184         {
9185           parser->scope = NULL_TREE;
9186           return error_mark_node;
9187         }
9188
9189       /* For a `typename', we needn't call xref_tag.  */
9190       if (tag_type == typename_type)
9191         return cp_parser_make_typename_type (parser, parser->scope,
9192                                              identifier);
9193       /* Look up a qualified name in the usual way.  */
9194       if (parser->scope)
9195         {
9196           tree decl;
9197
9198           /* In an elaborated-type-specifier, names are assumed to name
9199              types, so we set IS_TYPE to TRUE when calling
9200              cp_parser_lookup_name.  */
9201           decl = cp_parser_lookup_name (parser, identifier,
9202                                         /*is_type=*/true,
9203                                         /*is_template=*/false,
9204                                         /*is_namespace=*/false,
9205                                         /*check_dependency=*/true);
9206
9207           /* If we are parsing friend declaration, DECL may be a
9208              TEMPLATE_DECL tree node here.  However, we need to check
9209              whether this TEMPLATE_DECL results in valid code.  Consider
9210              the following example:
9211
9212                namespace N {
9213                  template <class T> class C {};
9214                }
9215                class X {
9216                  template <class T> friend class N::C; // #1, valid code
9217                };
9218                template <class T> class Y {
9219                  friend class N::C;                    // #2, invalid code
9220                };
9221
9222              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9223              name lookup of `N::C'.  We see that friend declaration must
9224              be template for the code to be valid.  Note that
9225              processing_template_decl does not work here since it is
9226              always 1 for the above two cases.  */
9227
9228           decl = (cp_parser_maybe_treat_template_as_class
9229                   (decl, /*tag_name_p=*/is_friend
9230                          && parser->num_template_parameter_lists));
9231
9232           if (TREE_CODE (decl) != TYPE_DECL)
9233             {
9234               error ("expected type-name");
9235               return error_mark_node;
9236             }
9237
9238           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9239             check_elaborated_type_specifier
9240               (tag_type, decl,
9241                (parser->num_template_parameter_lists
9242                 || DECL_SELF_REFERENCE_P (decl)));
9243
9244           type = TREE_TYPE (decl);
9245         }
9246       else
9247         {
9248           /* An elaborated-type-specifier sometimes introduces a new type and
9249              sometimes names an existing type.  Normally, the rule is that it
9250              introduces a new type only if there is not an existing type of
9251              the same name already in scope.  For example, given:
9252
9253                struct S {};
9254                void f() { struct S s; }
9255
9256              the `struct S' in the body of `f' is the same `struct S' as in
9257              the global scope; the existing definition is used.  However, if
9258              there were no global declaration, this would introduce a new
9259              local class named `S'.
9260
9261              An exception to this rule applies to the following code:
9262
9263                namespace N { struct S; }
9264
9265              Here, the elaborated-type-specifier names a new type
9266              unconditionally; even if there is already an `S' in the
9267              containing scope this declaration names a new type.
9268              This exception only applies if the elaborated-type-specifier
9269              forms the complete declaration:
9270
9271                [class.name]
9272
9273                A declaration consisting solely of `class-key identifier ;' is
9274                either a redeclaration of the name in the current scope or a
9275                forward declaration of the identifier as a class name.  It
9276                introduces the name into the current scope.
9277
9278              We are in this situation precisely when the next token is a `;'.
9279
9280              An exception to the exception is that a `friend' declaration does
9281              *not* name a new type; i.e., given:
9282
9283                struct S { friend struct T; };
9284
9285              `T' is not a new type in the scope of `S'.
9286
9287              Also, `new struct S' or `sizeof (struct S)' never results in the
9288              definition of a new type; a new type can only be declared in a
9289              declaration context.  */
9290
9291           /* Warn about attributes. They are ignored.  */
9292           if (attributes)
9293             warning ("type attributes are honored only at type definition");
9294
9295           type = xref_tag (tag_type, identifier,
9296                            /*attributes=*/NULL_TREE,
9297                            (is_friend
9298                             || !is_declaration
9299                             || cp_lexer_next_token_is_not (parser->lexer,
9300                                                            CPP_SEMICOLON)),
9301                            parser->num_template_parameter_lists);
9302         }
9303     }
9304   if (tag_type != enum_type)
9305     cp_parser_check_class_key (tag_type, type);
9306
9307   /* A "<" cannot follow an elaborated type specifier.  If that
9308      happens, the user was probably trying to form a template-id.  */
9309   cp_parser_check_for_invalid_template_id (parser, type);
9310
9311   return type;
9312 }
9313
9314 /* Parse an enum-specifier.
9315
9316    enum-specifier:
9317      enum identifier [opt] { enumerator-list [opt] }
9318
9319    Returns an ENUM_TYPE representing the enumeration.  */
9320
9321 static tree
9322 cp_parser_enum_specifier (cp_parser* parser)
9323 {
9324   cp_token *token;
9325   tree identifier = NULL_TREE;
9326   tree type;
9327
9328   /* Look for the `enum' keyword.  */
9329   if (!cp_parser_require_keyword (parser, RID_ENUM, "`enum'"))
9330     return error_mark_node;
9331   /* Peek at the next token.  */
9332   token = cp_lexer_peek_token (parser->lexer);
9333
9334   /* See if it is an identifier.  */
9335   if (token->type == CPP_NAME)
9336     identifier = cp_parser_identifier (parser);
9337
9338   /* Look for the `{'.  */
9339   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
9340     return error_mark_node;
9341
9342   /* At this point, we're going ahead with the enum-specifier, even
9343      if some other problem occurs.  */
9344   cp_parser_commit_to_tentative_parse (parser);
9345
9346   /* Issue an error message if type-definitions are forbidden here.  */
9347   cp_parser_check_type_definition (parser);
9348
9349   /* Create the new type.  */
9350   type = start_enum (identifier ? identifier : make_anon_name ());
9351
9352   /* Peek at the next token.  */
9353   token = cp_lexer_peek_token (parser->lexer);
9354   /* If it's not a `}', then there are some enumerators.  */
9355   if (token->type != CPP_CLOSE_BRACE)
9356     cp_parser_enumerator_list (parser, type);
9357   /* Look for the `}'.  */
9358   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9359
9360   /* Finish up the enumeration.  */
9361   finish_enum (type);
9362
9363   return type;
9364 }
9365
9366 /* Parse an enumerator-list.  The enumerators all have the indicated
9367    TYPE.
9368
9369    enumerator-list:
9370      enumerator-definition
9371      enumerator-list , enumerator-definition  */
9372
9373 static void
9374 cp_parser_enumerator_list (cp_parser* parser, tree type)
9375 {
9376   while (true)
9377     {
9378       cp_token *token;
9379
9380       /* Parse an enumerator-definition.  */
9381       cp_parser_enumerator_definition (parser, type);
9382       /* Peek at the next token.  */
9383       token = cp_lexer_peek_token (parser->lexer);
9384       /* If it's not a `,', then we've reached the end of the
9385          list.  */
9386       if (token->type != CPP_COMMA)
9387         break;
9388       /* Otherwise, consume the `,' and keep going.  */
9389       cp_lexer_consume_token (parser->lexer);
9390       /* If the next token is a `}', there is a trailing comma.  */
9391       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9392         {
9393           if (pedantic && !in_system_header)
9394             pedwarn ("comma at end of enumerator list");
9395           break;
9396         }
9397     }
9398 }
9399
9400 /* Parse an enumerator-definition.  The enumerator has the indicated
9401    TYPE.
9402
9403    enumerator-definition:
9404      enumerator
9405      enumerator = constant-expression
9406
9407    enumerator:
9408      identifier  */
9409
9410 static void
9411 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9412 {
9413   cp_token *token;
9414   tree identifier;
9415   tree value;
9416
9417   /* Look for the identifier.  */
9418   identifier = cp_parser_identifier (parser);
9419   if (identifier == error_mark_node)
9420     return;
9421
9422   /* Peek at the next token.  */
9423   token = cp_lexer_peek_token (parser->lexer);
9424   /* If it's an `=', then there's an explicit value.  */
9425   if (token->type == CPP_EQ)
9426     {
9427       /* Consume the `=' token.  */
9428       cp_lexer_consume_token (parser->lexer);
9429       /* Parse the value.  */
9430       value = cp_parser_constant_expression (parser,
9431                                              /*allow_non_constant_p=*/false,
9432                                              NULL);
9433     }
9434   else
9435     value = NULL_TREE;
9436
9437   /* Create the enumerator.  */
9438   build_enumerator (identifier, value, type);
9439 }
9440
9441 /* Parse a namespace-name.
9442
9443    namespace-name:
9444      original-namespace-name
9445      namespace-alias
9446
9447    Returns the NAMESPACE_DECL for the namespace.  */
9448
9449 static tree
9450 cp_parser_namespace_name (cp_parser* parser)
9451 {
9452   tree identifier;
9453   tree namespace_decl;
9454
9455   /* Get the name of the namespace.  */
9456   identifier = cp_parser_identifier (parser);
9457   if (identifier == error_mark_node)
9458     return error_mark_node;
9459
9460   /* Look up the identifier in the currently active scope.  Look only
9461      for namespaces, due to:
9462
9463        [basic.lookup.udir]
9464
9465        When looking up a namespace-name in a using-directive or alias
9466        definition, only namespace names are considered.
9467
9468      And:
9469
9470        [basic.lookup.qual]
9471
9472        During the lookup of a name preceding the :: scope resolution
9473        operator, object, function, and enumerator names are ignored.
9474
9475      (Note that cp_parser_class_or_namespace_name only calls this
9476      function if the token after the name is the scope resolution
9477      operator.)  */
9478   namespace_decl = cp_parser_lookup_name (parser, identifier,
9479                                           /*is_type=*/false,
9480                                           /*is_template=*/false,
9481                                           /*is_namespace=*/true,
9482                                           /*check_dependency=*/true);
9483   /* If it's not a namespace, issue an error.  */
9484   if (namespace_decl == error_mark_node
9485       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9486     {
9487       cp_parser_error (parser, "expected namespace-name");
9488       namespace_decl = error_mark_node;
9489     }
9490
9491   return namespace_decl;
9492 }
9493
9494 /* Parse a namespace-definition.
9495
9496    namespace-definition:
9497      named-namespace-definition
9498      unnamed-namespace-definition
9499
9500    named-namespace-definition:
9501      original-namespace-definition
9502      extension-namespace-definition
9503
9504    original-namespace-definition:
9505      namespace identifier { namespace-body }
9506
9507    extension-namespace-definition:
9508      namespace original-namespace-name { namespace-body }
9509
9510    unnamed-namespace-definition:
9511      namespace { namespace-body } */
9512
9513 static void
9514 cp_parser_namespace_definition (cp_parser* parser)
9515 {
9516   tree identifier;
9517
9518   /* Look for the `namespace' keyword.  */
9519   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9520
9521   /* Get the name of the namespace.  We do not attempt to distinguish
9522      between an original-namespace-definition and an
9523      extension-namespace-definition at this point.  The semantic
9524      analysis routines are responsible for that.  */
9525   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9526     identifier = cp_parser_identifier (parser);
9527   else
9528     identifier = NULL_TREE;
9529
9530   /* Look for the `{' to start the namespace.  */
9531   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9532   /* Start the namespace.  */
9533   push_namespace (identifier);
9534   /* Parse the body of the namespace.  */
9535   cp_parser_namespace_body (parser);
9536   /* Finish the namespace.  */
9537   pop_namespace ();
9538   /* Look for the final `}'.  */
9539   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9540 }
9541
9542 /* Parse a namespace-body.
9543
9544    namespace-body:
9545      declaration-seq [opt]  */
9546
9547 static void
9548 cp_parser_namespace_body (cp_parser* parser)
9549 {
9550   cp_parser_declaration_seq_opt (parser);
9551 }
9552
9553 /* Parse a namespace-alias-definition.
9554
9555    namespace-alias-definition:
9556      namespace identifier = qualified-namespace-specifier ;  */
9557
9558 static void
9559 cp_parser_namespace_alias_definition (cp_parser* parser)
9560 {
9561   tree identifier;
9562   tree namespace_specifier;
9563
9564   /* Look for the `namespace' keyword.  */
9565   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9566   /* Look for the identifier.  */
9567   identifier = cp_parser_identifier (parser);
9568   if (identifier == error_mark_node)
9569     return;
9570   /* Look for the `=' token.  */
9571   cp_parser_require (parser, CPP_EQ, "`='");
9572   /* Look for the qualified-namespace-specifier.  */
9573   namespace_specifier
9574     = cp_parser_qualified_namespace_specifier (parser);
9575   /* Look for the `;' token.  */
9576   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9577
9578   /* Register the alias in the symbol table.  */
9579   do_namespace_alias (identifier, namespace_specifier);
9580 }
9581
9582 /* Parse a qualified-namespace-specifier.
9583
9584    qualified-namespace-specifier:
9585      :: [opt] nested-name-specifier [opt] namespace-name
9586
9587    Returns a NAMESPACE_DECL corresponding to the specified
9588    namespace.  */
9589
9590 static tree
9591 cp_parser_qualified_namespace_specifier (cp_parser* parser)
9592 {
9593   /* Look for the optional `::'.  */
9594   cp_parser_global_scope_opt (parser,
9595                               /*current_scope_valid_p=*/false);
9596
9597   /* Look for the optional nested-name-specifier.  */
9598   cp_parser_nested_name_specifier_opt (parser,
9599                                        /*typename_keyword_p=*/false,
9600                                        /*check_dependency_p=*/true,
9601                                        /*type_p=*/false,
9602                                        /*is_declaration=*/true);
9603
9604   return cp_parser_namespace_name (parser);
9605 }
9606
9607 /* Parse a using-declaration.
9608
9609    using-declaration:
9610      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
9611      using :: unqualified-id ;  */
9612
9613 static void
9614 cp_parser_using_declaration (cp_parser* parser)
9615 {
9616   cp_token *token;
9617   bool typename_p = false;
9618   bool global_scope_p;
9619   tree decl;
9620   tree identifier;
9621   tree scope;
9622   tree qscope;
9623
9624   /* Look for the `using' keyword.  */
9625   cp_parser_require_keyword (parser, RID_USING, "`using'");
9626
9627   /* Peek at the next token.  */
9628   token = cp_lexer_peek_token (parser->lexer);
9629   /* See if it's `typename'.  */
9630   if (token->keyword == RID_TYPENAME)
9631     {
9632       /* Remember that we've seen it.  */
9633       typename_p = true;
9634       /* Consume the `typename' token.  */
9635       cp_lexer_consume_token (parser->lexer);
9636     }
9637
9638   /* Look for the optional global scope qualification.  */
9639   global_scope_p
9640     = (cp_parser_global_scope_opt (parser,
9641                                    /*current_scope_valid_p=*/false)
9642        != NULL_TREE);
9643
9644   /* If we saw `typename', or didn't see `::', then there must be a
9645      nested-name-specifier present.  */
9646   if (typename_p || !global_scope_p)
9647     qscope = cp_parser_nested_name_specifier (parser, typename_p,
9648                                               /*check_dependency_p=*/true,
9649                                               /*type_p=*/false,
9650                                               /*is_declaration=*/true);
9651   /* Otherwise, we could be in either of the two productions.  In that
9652      case, treat the nested-name-specifier as optional.  */
9653   else
9654     qscope = cp_parser_nested_name_specifier_opt (parser,
9655                                                   /*typename_keyword_p=*/false,
9656                                                   /*check_dependency_p=*/true,
9657                                                   /*type_p=*/false,
9658                                                   /*is_declaration=*/true);
9659   if (!qscope)
9660     qscope = global_namespace;
9661
9662   /* Parse the unqualified-id.  */
9663   identifier = cp_parser_unqualified_id (parser,
9664                                          /*template_keyword_p=*/false,
9665                                          /*check_dependency_p=*/true,
9666                                          /*declarator_p=*/true);
9667
9668   /* The function we call to handle a using-declaration is different
9669      depending on what scope we are in.  */
9670   if (identifier == error_mark_node)
9671     ;
9672   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
9673            && TREE_CODE (identifier) != BIT_NOT_EXPR)
9674     /* [namespace.udecl]
9675
9676        A using declaration shall not name a template-id.  */
9677     error ("a template-id may not appear in a using-declaration");
9678   else
9679     {
9680       scope = current_scope ();
9681       if (scope && TYPE_P (scope))
9682         {
9683           /* Create the USING_DECL.  */
9684           decl = do_class_using_decl (build_nt (SCOPE_REF,
9685                                                 parser->scope,
9686                                                 identifier));
9687           /* Add it to the list of members in this class.  */
9688           finish_member_declaration (decl);
9689         }
9690       else
9691         {
9692           decl = cp_parser_lookup_name_simple (parser, identifier);
9693           if (decl == error_mark_node)
9694             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
9695           else if (scope)
9696             do_local_using_decl (decl, qscope, identifier);
9697           else
9698             do_toplevel_using_decl (decl, qscope, identifier);
9699         }
9700     }
9701
9702   /* Look for the final `;'.  */
9703   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9704 }
9705
9706 /* Parse a using-directive.
9707
9708    using-directive:
9709      using namespace :: [opt] nested-name-specifier [opt]
9710        namespace-name ;  */
9711
9712 static void
9713 cp_parser_using_directive (cp_parser* parser)
9714 {
9715   tree namespace_decl;
9716   tree attribs;
9717
9718   /* Look for the `using' keyword.  */
9719   cp_parser_require_keyword (parser, RID_USING, "`using'");
9720   /* And the `namespace' keyword.  */
9721   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9722   /* Look for the optional `::' operator.  */
9723   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
9724   /* And the optional nested-name-specifier.  */
9725   cp_parser_nested_name_specifier_opt (parser,
9726                                        /*typename_keyword_p=*/false,
9727                                        /*check_dependency_p=*/true,
9728                                        /*type_p=*/false,
9729                                        /*is_declaration=*/true);
9730   /* Get the namespace being used.  */
9731   namespace_decl = cp_parser_namespace_name (parser);
9732   /* And any specified attributes.  */
9733   attribs = cp_parser_attributes_opt (parser);
9734   /* Update the symbol table.  */
9735   parse_using_directive (namespace_decl, attribs);
9736   /* Look for the final `;'.  */
9737   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9738 }
9739
9740 /* Parse an asm-definition.
9741
9742    asm-definition:
9743      asm ( string-literal ) ;
9744
9745    GNU Extension:
9746
9747    asm-definition:
9748      asm volatile [opt] ( string-literal ) ;
9749      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
9750      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9751                           : asm-operand-list [opt] ) ;
9752      asm volatile [opt] ( string-literal : asm-operand-list [opt]
9753                           : asm-operand-list [opt]
9754                           : asm-operand-list [opt] ) ;  */
9755
9756 static void
9757 cp_parser_asm_definition (cp_parser* parser)
9758 {
9759   cp_token *token;
9760   tree string;
9761   tree outputs = NULL_TREE;
9762   tree inputs = NULL_TREE;
9763   tree clobbers = NULL_TREE;
9764   tree asm_stmt;
9765   bool volatile_p = false;
9766   bool extended_p = false;
9767
9768   /* Look for the `asm' keyword.  */
9769   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
9770   /* See if the next token is `volatile'.  */
9771   if (cp_parser_allow_gnu_extensions_p (parser)
9772       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
9773     {
9774       /* Remember that we saw the `volatile' keyword.  */
9775       volatile_p = true;
9776       /* Consume the token.  */
9777       cp_lexer_consume_token (parser->lexer);
9778     }
9779   /* Look for the opening `('.  */
9780   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
9781   /* Look for the string.  */
9782   c_lex_string_translate = false;
9783   token = cp_parser_require (parser, CPP_STRING, "asm body");
9784   if (!token)
9785     goto finish;
9786   string = token->value;
9787   /* If we're allowing GNU extensions, check for the extended assembly
9788      syntax.  Unfortunately, the `:' tokens need not be separated by
9789      a space in C, and so, for compatibility, we tolerate that here
9790      too.  Doing that means that we have to treat the `::' operator as
9791      two `:' tokens.  */
9792   if (cp_parser_allow_gnu_extensions_p (parser)
9793       && at_function_scope_p ()
9794       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
9795           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
9796     {
9797       bool inputs_p = false;
9798       bool clobbers_p = false;
9799
9800       /* The extended syntax was used.  */
9801       extended_p = true;
9802
9803       /* Look for outputs.  */
9804       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9805         {
9806           /* Consume the `:'.  */
9807           cp_lexer_consume_token (parser->lexer);
9808           /* Parse the output-operands.  */
9809           if (cp_lexer_next_token_is_not (parser->lexer,
9810                                           CPP_COLON)
9811               && cp_lexer_next_token_is_not (parser->lexer,
9812                                              CPP_SCOPE)
9813               && cp_lexer_next_token_is_not (parser->lexer,
9814                                              CPP_CLOSE_PAREN))
9815             outputs = cp_parser_asm_operand_list (parser);
9816         }
9817       /* If the next token is `::', there are no outputs, and the
9818          next token is the beginning of the inputs.  */
9819       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9820         {
9821           /* Consume the `::' token.  */
9822           cp_lexer_consume_token (parser->lexer);
9823           /* The inputs are coming next.  */
9824           inputs_p = true;
9825         }
9826
9827       /* Look for inputs.  */
9828       if (inputs_p
9829           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9830         {
9831           if (!inputs_p)
9832             /* Consume the `:'.  */
9833             cp_lexer_consume_token (parser->lexer);
9834           /* Parse the output-operands.  */
9835           if (cp_lexer_next_token_is_not (parser->lexer,
9836                                           CPP_COLON)
9837               && cp_lexer_next_token_is_not (parser->lexer,
9838                                              CPP_SCOPE)
9839               && cp_lexer_next_token_is_not (parser->lexer,
9840                                              CPP_CLOSE_PAREN))
9841             inputs = cp_parser_asm_operand_list (parser);
9842         }
9843       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
9844         /* The clobbers are coming next.  */
9845         clobbers_p = true;
9846
9847       /* Look for clobbers.  */
9848       if (clobbers_p
9849           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9850         {
9851           if (!clobbers_p)
9852             /* Consume the `:'.  */
9853             cp_lexer_consume_token (parser->lexer);
9854           /* Parse the clobbers.  */
9855           if (cp_lexer_next_token_is_not (parser->lexer,
9856                                           CPP_CLOSE_PAREN))
9857             clobbers = cp_parser_asm_clobber_list (parser);
9858         }
9859     }
9860   /* Look for the closing `)'.  */
9861   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
9862     cp_parser_skip_to_closing_parenthesis (parser, true, false,
9863                                            /*consume_paren=*/true);
9864   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
9865
9866   /* Create the ASM_STMT.  */
9867   if (at_function_scope_p ())
9868     {
9869       asm_stmt =
9870         finish_asm_stmt (volatile_p
9871                          ? ridpointers[(int) RID_VOLATILE] : NULL_TREE,
9872                          string, outputs, inputs, clobbers);
9873       /* If the extended syntax was not used, mark the ASM_STMT.  */
9874       if (!extended_p)
9875         ASM_INPUT_P (asm_stmt) = 1;
9876     }
9877   else
9878     assemble_asm (string);
9879
9880  finish:
9881   c_lex_string_translate = true;
9882 }
9883
9884 /* Declarators [gram.dcl.decl] */
9885
9886 /* Parse an init-declarator.
9887
9888    init-declarator:
9889      declarator initializer [opt]
9890
9891    GNU Extension:
9892
9893    init-declarator:
9894      declarator asm-specification [opt] attributes [opt] initializer [opt]
9895
9896    function-definition:
9897      decl-specifier-seq [opt] declarator ctor-initializer [opt]
9898        function-body
9899      decl-specifier-seq [opt] declarator function-try-block
9900
9901    GNU Extension:
9902
9903    function-definition:
9904      __extension__ function-definition
9905
9906    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
9907    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
9908    then this declarator appears in a class scope.  The new DECL created
9909    by this declarator is returned.
9910
9911    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
9912    for a function-definition here as well.  If the declarator is a
9913    declarator for a function-definition, *FUNCTION_DEFINITION_P will
9914    be TRUE upon return.  By that point, the function-definition will
9915    have been completely parsed.
9916
9917    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
9918    is FALSE.  */
9919
9920 static tree
9921 cp_parser_init_declarator (cp_parser* parser,
9922                            tree decl_specifiers,
9923                            tree prefix_attributes,
9924                            bool function_definition_allowed_p,
9925                            bool member_p,
9926                            int declares_class_or_enum,
9927                            bool* function_definition_p)
9928 {
9929   cp_token *token;
9930   tree declarator;
9931   tree attributes;
9932   tree asm_specification;
9933   tree initializer;
9934   tree decl = NULL_TREE;
9935   tree scope;
9936   bool is_initialized;
9937   bool is_parenthesized_init;
9938   bool is_non_constant_init;
9939   int ctor_dtor_or_conv_p;
9940   bool friend_p;
9941   bool pop_p = false;
9942
9943   /* Assume that this is not the declarator for a function
9944      definition.  */
9945   if (function_definition_p)
9946     *function_definition_p = false;
9947
9948   /* Defer access checks while parsing the declarator; we cannot know
9949      what names are accessible until we know what is being
9950      declared.  */
9951   resume_deferring_access_checks ();
9952
9953   /* Parse the declarator.  */
9954   declarator
9955     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9956                             &ctor_dtor_or_conv_p,
9957                             /*parenthesized_p=*/NULL);
9958   /* Gather up the deferred checks.  */
9959   stop_deferring_access_checks ();
9960
9961   /* If the DECLARATOR was erroneous, there's no need to go
9962      further.  */
9963   if (declarator == error_mark_node)
9964     return error_mark_node;
9965
9966   cp_parser_check_for_definition_in_return_type (declarator,
9967                                                  declares_class_or_enum);
9968
9969   /* Figure out what scope the entity declared by the DECLARATOR is
9970      located in.  `grokdeclarator' sometimes changes the scope, so
9971      we compute it now.  */
9972   scope = get_scope_of_declarator (declarator);
9973
9974   /* If we're allowing GNU extensions, look for an asm-specification
9975      and attributes.  */
9976   if (cp_parser_allow_gnu_extensions_p (parser))
9977     {
9978       /* Look for an asm-specification.  */
9979       asm_specification = cp_parser_asm_specification_opt (parser);
9980       /* And attributes.  */
9981       attributes = cp_parser_attributes_opt (parser);
9982     }
9983   else
9984     {
9985       asm_specification = NULL_TREE;
9986       attributes = NULL_TREE;
9987     }
9988
9989   /* Peek at the next token.  */
9990   token = cp_lexer_peek_token (parser->lexer);
9991   /* Check to see if the token indicates the start of a
9992      function-definition.  */
9993   if (cp_parser_token_starts_function_definition_p (token))
9994     {
9995       if (!function_definition_allowed_p)
9996         {
9997           /* If a function-definition should not appear here, issue an
9998              error message.  */
9999           cp_parser_error (parser,
10000                            "a function-definition is not allowed here");
10001           return error_mark_node;
10002         }
10003       else
10004         {
10005           /* Neither attributes nor an asm-specification are allowed
10006              on a function-definition.  */
10007           if (asm_specification)
10008             error ("an asm-specification is not allowed on a function-definition");
10009           if (attributes)
10010             error ("attributes are not allowed on a function-definition");
10011           /* This is a function-definition.  */
10012           *function_definition_p = true;
10013
10014           /* Parse the function definition.  */
10015           if (member_p)
10016             decl = cp_parser_save_member_function_body (parser,
10017                                                         decl_specifiers,
10018                                                         declarator,
10019                                                         prefix_attributes);
10020           else
10021             decl
10022               = (cp_parser_function_definition_from_specifiers_and_declarator
10023                  (parser, decl_specifiers, prefix_attributes, declarator));
10024
10025           return decl;
10026         }
10027     }
10028
10029   /* [dcl.dcl]
10030
10031      Only in function declarations for constructors, destructors, and
10032      type conversions can the decl-specifier-seq be omitted.
10033
10034      We explicitly postpone this check past the point where we handle
10035      function-definitions because we tolerate function-definitions
10036      that are missing their return types in some modes.  */
10037   if (!decl_specifiers && ctor_dtor_or_conv_p <= 0)
10038     {
10039       cp_parser_error (parser,
10040                        "expected constructor, destructor, or type conversion");
10041       return error_mark_node;
10042     }
10043
10044   /* An `=' or an `(' indicates an initializer.  */
10045   is_initialized = (token->type == CPP_EQ
10046                      || token->type == CPP_OPEN_PAREN);
10047   /* If the init-declarator isn't initialized and isn't followed by a
10048      `,' or `;', it's not a valid init-declarator.  */
10049   if (!is_initialized
10050       && token->type != CPP_COMMA
10051       && token->type != CPP_SEMICOLON)
10052     {
10053       cp_parser_error (parser, "expected init-declarator");
10054       return error_mark_node;
10055     }
10056
10057   /* Because start_decl has side-effects, we should only call it if we
10058      know we're going ahead.  By this point, we know that we cannot
10059      possibly be looking at any other construct.  */
10060   cp_parser_commit_to_tentative_parse (parser);
10061
10062   /* If the decl specifiers were bad, issue an error now that we're
10063      sure this was intended to be a declarator.  Then continue
10064      declaring the variable(s), as int, to try to cut down on further
10065      errors.  */
10066   if (decl_specifiers != NULL
10067       && TREE_VALUE (decl_specifiers) == error_mark_node)
10068     {
10069       cp_parser_error (parser, "invalid type in declaration");
10070       TREE_VALUE (decl_specifiers) = integer_type_node;
10071     }
10072
10073   /* Check to see whether or not this declaration is a friend.  */
10074   friend_p = cp_parser_friend_p (decl_specifiers);
10075
10076   /* Check that the number of template-parameter-lists is OK.  */
10077   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10078     return error_mark_node;
10079
10080   /* Enter the newly declared entry in the symbol table.  If we're
10081      processing a declaration in a class-specifier, we wait until
10082      after processing the initializer.  */
10083   if (!member_p)
10084     {
10085       if (parser->in_unbraced_linkage_specification_p)
10086         {
10087           decl_specifiers = tree_cons (error_mark_node,
10088                                        get_identifier ("extern"),
10089                                        decl_specifiers);
10090           have_extern_spec = false;
10091         }
10092       decl = start_decl (declarator, decl_specifiers,
10093                          is_initialized, attributes, prefix_attributes);
10094     }
10095
10096   /* Enter the SCOPE.  That way unqualified names appearing in the
10097      initializer will be looked up in SCOPE.  */
10098   if (scope)
10099     pop_p = push_scope (scope);
10100
10101   /* Perform deferred access control checks, now that we know in which
10102      SCOPE the declared entity resides.  */
10103   if (!member_p && decl)
10104     {
10105       tree saved_current_function_decl = NULL_TREE;
10106
10107       /* If the entity being declared is a function, pretend that we
10108          are in its scope.  If it is a `friend', it may have access to
10109          things that would not otherwise be accessible.  */
10110       if (TREE_CODE (decl) == FUNCTION_DECL)
10111         {
10112           saved_current_function_decl = current_function_decl;
10113           current_function_decl = decl;
10114         }
10115
10116       /* Perform the access control checks for the declarator and the
10117          the decl-specifiers.  */
10118       perform_deferred_access_checks ();
10119
10120       /* Restore the saved value.  */
10121       if (TREE_CODE (decl) == FUNCTION_DECL)
10122         current_function_decl = saved_current_function_decl;
10123     }
10124
10125   /* Parse the initializer.  */
10126   if (is_initialized)
10127     initializer = cp_parser_initializer (parser,
10128                                          &is_parenthesized_init,
10129                                          &is_non_constant_init);
10130   else
10131     {
10132       initializer = NULL_TREE;
10133       is_parenthesized_init = false;
10134       is_non_constant_init = true;
10135     }
10136
10137   /* The old parser allows attributes to appear after a parenthesized
10138      initializer.  Mark Mitchell proposed removing this functionality
10139      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10140      attributes -- but ignores them.  */
10141   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10142     if (cp_parser_attributes_opt (parser))
10143       warning ("attributes after parenthesized initializer ignored");
10144
10145   /* Leave the SCOPE, now that we have processed the initializer.  It
10146      is important to do this before calling cp_finish_decl because it
10147      makes decisions about whether to create DECL_STMTs or not based
10148      on the current scope.  */
10149   if (pop_p)
10150     pop_scope (scope);
10151
10152   /* For an in-class declaration, use `grokfield' to create the
10153      declaration.  */
10154   if (member_p)
10155     {
10156       decl = grokfield (declarator, decl_specifiers,
10157                         initializer, /*asmspec=*/NULL_TREE,
10158                         /*attributes=*/NULL_TREE);
10159       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10160         cp_parser_save_default_args (parser, decl);
10161     }
10162
10163   /* Finish processing the declaration.  But, skip friend
10164      declarations.  */
10165   if (!friend_p && decl)
10166     cp_finish_decl (decl,
10167                     initializer,
10168                     asm_specification,
10169                     /* If the initializer is in parentheses, then this is
10170                        a direct-initialization, which means that an
10171                        `explicit' constructor is OK.  Otherwise, an
10172                        `explicit' constructor cannot be used.  */
10173                     ((is_parenthesized_init || !is_initialized)
10174                      ? 0 : LOOKUP_ONLYCONVERTING));
10175
10176   /* Remember whether or not variables were initialized by
10177      constant-expressions.  */
10178   if (decl && TREE_CODE (decl) == VAR_DECL
10179       && is_initialized && !is_non_constant_init)
10180     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10181
10182   return decl;
10183 }
10184
10185 /* Parse a declarator.
10186
10187    declarator:
10188      direct-declarator
10189      ptr-operator declarator
10190
10191    abstract-declarator:
10192      ptr-operator abstract-declarator [opt]
10193      direct-abstract-declarator
10194
10195    GNU Extensions:
10196
10197    declarator:
10198      attributes [opt] direct-declarator
10199      attributes [opt] ptr-operator declarator
10200
10201    abstract-declarator:
10202      attributes [opt] ptr-operator abstract-declarator [opt]
10203      attributes [opt] direct-abstract-declarator
10204
10205    Returns a representation of the declarator.  If the declarator has
10206    the form `* declarator', then an INDIRECT_REF is returned, whose
10207    only operand is the sub-declarator.  Analogously, `& declarator' is
10208    represented as an ADDR_EXPR.  For `X::* declarator', a SCOPE_REF is
10209    used.  The first operand is the TYPE for `X'.  The second operand
10210    is an INDIRECT_REF whose operand is the sub-declarator.
10211
10212    Otherwise, the representation is as for a direct-declarator.
10213
10214    (It would be better to define a structure type to represent
10215    declarators, rather than abusing `tree' nodes to represent
10216    declarators.  That would be much clearer and save some memory.
10217    There is no reason for declarators to be garbage-collected, for
10218    example; they are created during parser and no longer needed after
10219    `grokdeclarator' has been called.)
10220
10221    For a ptr-operator that has the optional cv-qualifier-seq,
10222    cv-qualifiers will be stored in the TREE_TYPE of the INDIRECT_REF
10223    node.
10224
10225    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10226    detect constructor, destructor or conversion operators. It is set
10227    to -1 if the declarator is a name, and +1 if it is a
10228    function. Otherwise it is set to zero. Usually you just want to
10229    test for >0, but internally the negative value is used.
10230
10231    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10232    a decl-specifier-seq unless it declares a constructor, destructor,
10233    or conversion.  It might seem that we could check this condition in
10234    semantic analysis, rather than parsing, but that makes it difficult
10235    to handle something like `f()'.  We want to notice that there are
10236    no decl-specifiers, and therefore realize that this is an
10237    expression, not a declaration.)
10238
10239    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10240    the declarator is a direct-declarator of the form "(...)".  */
10241
10242 static tree
10243 cp_parser_declarator (cp_parser* parser,
10244                       cp_parser_declarator_kind dcl_kind,
10245                       int* ctor_dtor_or_conv_p,
10246                       bool* parenthesized_p)
10247 {
10248   cp_token *token;
10249   tree declarator;
10250   enum tree_code code;
10251   tree cv_qualifier_seq;
10252   tree class_type;
10253   tree attributes = NULL_TREE;
10254
10255   /* Assume this is not a constructor, destructor, or type-conversion
10256      operator.  */
10257   if (ctor_dtor_or_conv_p)
10258     *ctor_dtor_or_conv_p = 0;
10259
10260   if (cp_parser_allow_gnu_extensions_p (parser))
10261     attributes = cp_parser_attributes_opt (parser);
10262
10263   /* Peek at the next token.  */
10264   token = cp_lexer_peek_token (parser->lexer);
10265
10266   /* Check for the ptr-operator production.  */
10267   cp_parser_parse_tentatively (parser);
10268   /* Parse the ptr-operator.  */
10269   code = cp_parser_ptr_operator (parser,
10270                                  &class_type,
10271                                  &cv_qualifier_seq);
10272   /* If that worked, then we have a ptr-operator.  */
10273   if (cp_parser_parse_definitely (parser))
10274     {
10275       /* If a ptr-operator was found, then this declarator was not
10276          parenthesized.  */
10277       if (parenthesized_p)
10278         *parenthesized_p = true;
10279       /* The dependent declarator is optional if we are parsing an
10280          abstract-declarator.  */
10281       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10282         cp_parser_parse_tentatively (parser);
10283
10284       /* Parse the dependent declarator.  */
10285       declarator = cp_parser_declarator (parser, dcl_kind,
10286                                          /*ctor_dtor_or_conv_p=*/NULL,
10287                                          /*parenthesized_p=*/NULL);
10288
10289       /* If we are parsing an abstract-declarator, we must handle the
10290          case where the dependent declarator is absent.  */
10291       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10292           && !cp_parser_parse_definitely (parser))
10293         declarator = NULL_TREE;
10294
10295       /* Build the representation of the ptr-operator.  */
10296       if (code == INDIRECT_REF)
10297         declarator = make_pointer_declarator (cv_qualifier_seq,
10298                                               declarator);
10299       else
10300         declarator = make_reference_declarator (cv_qualifier_seq,
10301                                                 declarator);
10302       /* Handle the pointer-to-member case.  */
10303       if (class_type)
10304         declarator = build_nt (SCOPE_REF, class_type, declarator);
10305     }
10306   /* Everything else is a direct-declarator.  */
10307   else
10308     {
10309       if (parenthesized_p)
10310         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10311                                                    CPP_OPEN_PAREN);
10312       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10313                                                 ctor_dtor_or_conv_p);
10314     }
10315
10316   if (attributes && declarator != error_mark_node)
10317     declarator = tree_cons (attributes, declarator, NULL_TREE);
10318
10319   return declarator;
10320 }
10321
10322 /* Parse a direct-declarator or direct-abstract-declarator.
10323
10324    direct-declarator:
10325      declarator-id
10326      direct-declarator ( parameter-declaration-clause )
10327        cv-qualifier-seq [opt]
10328        exception-specification [opt]
10329      direct-declarator [ constant-expression [opt] ]
10330      ( declarator )
10331
10332    direct-abstract-declarator:
10333      direct-abstract-declarator [opt]
10334        ( parameter-declaration-clause )
10335        cv-qualifier-seq [opt]
10336        exception-specification [opt]
10337      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10338      ( abstract-declarator )
10339
10340    Returns a representation of the declarator.  DCL_KIND is
10341    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10342    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10343    we are parsing a direct-declarator.  It is
10344    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10345    of ambiguity we prefer an abstract declarator, as per
10346    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P is as for
10347    cp_parser_declarator.
10348
10349    For the declarator-id production, the representation is as for an
10350    id-expression, except that a qualified name is represented as a
10351    SCOPE_REF.  A function-declarator is represented as a CALL_EXPR;
10352    see the documentation of the FUNCTION_DECLARATOR_* macros for
10353    information about how to find the various declarator components.
10354    An array-declarator is represented as an ARRAY_REF.  The
10355    direct-declarator is the first operand; the constant-expression
10356    indicating the size of the array is the second operand.  */
10357
10358 static tree
10359 cp_parser_direct_declarator (cp_parser* parser,
10360                              cp_parser_declarator_kind dcl_kind,
10361                              int* ctor_dtor_or_conv_p)
10362 {
10363   cp_token *token;
10364   tree declarator = NULL_TREE;
10365   tree scope = NULL_TREE;
10366   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10367   bool saved_in_declarator_p = parser->in_declarator_p;
10368   bool first = true;
10369   bool pop_p = false;
10370
10371   while (true)
10372     {
10373       /* Peek at the next token.  */
10374       token = cp_lexer_peek_token (parser->lexer);
10375       if (token->type == CPP_OPEN_PAREN)
10376         {
10377           /* This is either a parameter-declaration-clause, or a
10378              parenthesized declarator. When we know we are parsing a
10379              named declarator, it must be a parenthesized declarator
10380              if FIRST is true. For instance, `(int)' is a
10381              parameter-declaration-clause, with an omitted
10382              direct-abstract-declarator. But `((*))', is a
10383              parenthesized abstract declarator. Finally, when T is a
10384              template parameter `(T)' is a
10385              parameter-declaration-clause, and not a parenthesized
10386              named declarator.
10387
10388              We first try and parse a parameter-declaration-clause,
10389              and then try a nested declarator (if FIRST is true).
10390
10391              It is not an error for it not to be a
10392              parameter-declaration-clause, even when FIRST is
10393              false. Consider,
10394
10395                int i (int);
10396                int i (3);
10397
10398              The first is the declaration of a function while the
10399              second is a the definition of a variable, including its
10400              initializer.
10401
10402              Having seen only the parenthesis, we cannot know which of
10403              these two alternatives should be selected.  Even more
10404              complex are examples like:
10405
10406                int i (int (a));
10407                int i (int (3));
10408
10409              The former is a function-declaration; the latter is a
10410              variable initialization.
10411
10412              Thus again, we try a parameter-declaration-clause, and if
10413              that fails, we back out and return.  */
10414
10415           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10416             {
10417               tree params;
10418               unsigned saved_num_template_parameter_lists;
10419
10420               cp_parser_parse_tentatively (parser);
10421
10422               /* Consume the `('.  */
10423               cp_lexer_consume_token (parser->lexer);
10424               if (first)
10425                 {
10426                   /* If this is going to be an abstract declarator, we're
10427                      in a declarator and we can't have default args.  */
10428                   parser->default_arg_ok_p = false;
10429                   parser->in_declarator_p = true;
10430                 }
10431
10432               /* Inside the function parameter list, surrounding
10433                  template-parameter-lists do not apply.  */
10434               saved_num_template_parameter_lists
10435                 = parser->num_template_parameter_lists;
10436               parser->num_template_parameter_lists = 0;
10437
10438               /* Parse the parameter-declaration-clause.  */
10439               params = cp_parser_parameter_declaration_clause (parser);
10440
10441               parser->num_template_parameter_lists
10442                 = saved_num_template_parameter_lists;
10443
10444               /* If all went well, parse the cv-qualifier-seq and the
10445                  exception-specification.  */
10446               if (cp_parser_parse_definitely (parser))
10447                 {
10448                   tree cv_qualifiers;
10449                   tree exception_specification;
10450
10451                   if (ctor_dtor_or_conv_p)
10452                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10453                   first = false;
10454                   /* Consume the `)'.  */
10455                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10456
10457                   /* Parse the cv-qualifier-seq.  */
10458                   cv_qualifiers = cp_parser_cv_qualifier_seq_opt (parser);
10459                   /* And the exception-specification.  */
10460                   exception_specification
10461                     = cp_parser_exception_specification_opt (parser);
10462
10463                   /* Create the function-declarator.  */
10464                   declarator = make_call_declarator (declarator,
10465                                                      params,
10466                                                      cv_qualifiers,
10467                                                      exception_specification);
10468                   /* Any subsequent parameter lists are to do with
10469                      return type, so are not those of the declared
10470                      function.  */
10471                   parser->default_arg_ok_p = false;
10472
10473                   /* Repeat the main loop.  */
10474                   continue;
10475                 }
10476             }
10477
10478           /* If this is the first, we can try a parenthesized
10479              declarator.  */
10480           if (first)
10481             {
10482               bool saved_in_type_id_in_expr_p;
10483
10484               parser->default_arg_ok_p = saved_default_arg_ok_p;
10485               parser->in_declarator_p = saved_in_declarator_p;
10486
10487               /* Consume the `('.  */
10488               cp_lexer_consume_token (parser->lexer);
10489               /* Parse the nested declarator.  */
10490               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10491               parser->in_type_id_in_expr_p = true;
10492               declarator
10493                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10494                                         /*parenthesized_p=*/NULL);
10495               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10496               first = false;
10497               /* Expect a `)'.  */
10498               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10499                 declarator = error_mark_node;
10500               if (declarator == error_mark_node)
10501                 break;
10502
10503               goto handle_declarator;
10504             }
10505           /* Otherwise, we must be done.  */
10506           else
10507             break;
10508         }
10509       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10510                && token->type == CPP_OPEN_SQUARE)
10511         {
10512           /* Parse an array-declarator.  */
10513           tree bounds;
10514
10515           if (ctor_dtor_or_conv_p)
10516             *ctor_dtor_or_conv_p = 0;
10517
10518           first = false;
10519           parser->default_arg_ok_p = false;
10520           parser->in_declarator_p = true;
10521           /* Consume the `['.  */
10522           cp_lexer_consume_token (parser->lexer);
10523           /* Peek at the next token.  */
10524           token = cp_lexer_peek_token (parser->lexer);
10525           /* If the next token is `]', then there is no
10526              constant-expression.  */
10527           if (token->type != CPP_CLOSE_SQUARE)
10528             {
10529               bool non_constant_p;
10530
10531               bounds
10532                 = cp_parser_constant_expression (parser,
10533                                                  /*allow_non_constant=*/true,
10534                                                  &non_constant_p);
10535               if (!non_constant_p)
10536                 bounds = fold_non_dependent_expr (bounds);
10537             }
10538           else
10539             bounds = NULL_TREE;
10540           /* Look for the closing `]'.  */
10541           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10542             {
10543               declarator = error_mark_node;
10544               break;
10545             }
10546
10547           declarator = build_nt (ARRAY_REF, declarator, bounds);
10548         }
10549       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10550         {
10551           /* Parse a declarator-id */
10552           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10553             cp_parser_parse_tentatively (parser);
10554           declarator = cp_parser_declarator_id (parser);
10555           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10556             {
10557               if (!cp_parser_parse_definitely (parser))
10558                 declarator = error_mark_node;
10559               else if (TREE_CODE (declarator) != IDENTIFIER_NODE)
10560                 {
10561                   cp_parser_error (parser, "expected unqualified-id");
10562                   declarator = error_mark_node;
10563                 }
10564             }
10565
10566           if (declarator == error_mark_node)
10567             break;
10568
10569           if (TREE_CODE (declarator) == SCOPE_REF
10570               && !current_scope ())
10571             {
10572               tree scope = TREE_OPERAND (declarator, 0);
10573
10574               /* In the declaration of a member of a template class
10575                  outside of the class itself, the SCOPE will sometimes
10576                  be a TYPENAME_TYPE.  For example, given:
10577
10578                  template <typename T>
10579                  int S<T>::R::i = 3;
10580
10581                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
10582                  this context, we must resolve S<T>::R to an ordinary
10583                  type, rather than a typename type.
10584
10585                  The reason we normally avoid resolving TYPENAME_TYPEs
10586                  is that a specialization of `S' might render
10587                  `S<T>::R' not a type.  However, if `S' is
10588                  specialized, then this `i' will not be used, so there
10589                  is no harm in resolving the types here.  */
10590               if (TREE_CODE (scope) == TYPENAME_TYPE)
10591                 {
10592                   tree type;
10593
10594                   /* Resolve the TYPENAME_TYPE.  */
10595                   type = resolve_typename_type (scope,
10596                                                  /*only_current_p=*/false);
10597                   /* If that failed, the declarator is invalid.  */
10598                   if (type != error_mark_node)
10599                     scope = type;
10600                   /* Build a new DECLARATOR.  */
10601                   declarator = build_nt (SCOPE_REF,
10602                                          scope,
10603                                          TREE_OPERAND (declarator, 1));
10604                 }
10605             }
10606
10607           /* Check to see whether the declarator-id names a constructor,
10608              destructor, or conversion.  */
10609           if (declarator && ctor_dtor_or_conv_p
10610               && ((TREE_CODE (declarator) == SCOPE_REF
10611                    && CLASS_TYPE_P (TREE_OPERAND (declarator, 0)))
10612                   || (TREE_CODE (declarator) != SCOPE_REF
10613                       && at_class_scope_p ())))
10614             {
10615               tree unqualified_name;
10616               tree class_type;
10617
10618               /* Get the unqualified part of the name.  */
10619               if (TREE_CODE (declarator) == SCOPE_REF)
10620                 {
10621                   class_type = TREE_OPERAND (declarator, 0);
10622                   unqualified_name = TREE_OPERAND (declarator, 1);
10623                 }
10624               else
10625                 {
10626                   class_type = current_class_type;
10627                   unqualified_name = declarator;
10628                 }
10629
10630               /* See if it names ctor, dtor or conv.  */
10631               if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR
10632                   || IDENTIFIER_TYPENAME_P (unqualified_name)
10633                   || constructor_name_p (unqualified_name, class_type)
10634                   || (TREE_CODE (unqualified_name) == TYPE_DECL
10635                       && same_type_p (TREE_TYPE (unqualified_name),
10636                                       class_type)))
10637                 *ctor_dtor_or_conv_p = -1;
10638             }
10639
10640         handle_declarator:;
10641           scope = get_scope_of_declarator (declarator);
10642           if (scope)
10643             /* Any names that appear after the declarator-id for a
10644                member are looked up in the containing scope.  */
10645             pop_p = push_scope (scope);
10646           parser->in_declarator_p = true;
10647           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
10648               || (declarator
10649                   && (TREE_CODE (declarator) == SCOPE_REF
10650                       || TREE_CODE (declarator) == IDENTIFIER_NODE)))
10651             /* Default args are only allowed on function
10652                declarations.  */
10653             parser->default_arg_ok_p = saved_default_arg_ok_p;
10654           else
10655             parser->default_arg_ok_p = false;
10656
10657           first = false;
10658         }
10659       /* We're done.  */
10660       else
10661         break;
10662     }
10663
10664   /* For an abstract declarator, we might wind up with nothing at this
10665      point.  That's an error; the declarator is not optional.  */
10666   if (!declarator)
10667     cp_parser_error (parser, "expected declarator");
10668
10669   /* If we entered a scope, we must exit it now.  */
10670   if (pop_p)
10671     pop_scope (scope);
10672
10673   parser->default_arg_ok_p = saved_default_arg_ok_p;
10674   parser->in_declarator_p = saved_in_declarator_p;
10675
10676   return declarator;
10677 }
10678
10679 /* Parse a ptr-operator.
10680
10681    ptr-operator:
10682      * cv-qualifier-seq [opt]
10683      &
10684      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
10685
10686    GNU Extension:
10687
10688    ptr-operator:
10689      & cv-qualifier-seq [opt]
10690
10691    Returns INDIRECT_REF if a pointer, or pointer-to-member, was
10692    used.  Returns ADDR_EXPR if a reference was used.  In the
10693    case of a pointer-to-member, *TYPE is filled in with the
10694    TYPE containing the member.  *CV_QUALIFIER_SEQ is filled in
10695    with the cv-qualifier-seq, or NULL_TREE, if there are no
10696    cv-qualifiers.  Returns ERROR_MARK if an error occurred.  */
10697
10698 static enum tree_code
10699 cp_parser_ptr_operator (cp_parser* parser,
10700                         tree* type,
10701                         tree* cv_qualifier_seq)
10702 {
10703   enum tree_code code = ERROR_MARK;
10704   cp_token *token;
10705
10706   /* Assume that it's not a pointer-to-member.  */
10707   *type = NULL_TREE;
10708   /* And that there are no cv-qualifiers.  */
10709   *cv_qualifier_seq = NULL_TREE;
10710
10711   /* Peek at the next token.  */
10712   token = cp_lexer_peek_token (parser->lexer);
10713   /* If it's a `*' or `&' we have a pointer or reference.  */
10714   if (token->type == CPP_MULT || token->type == CPP_AND)
10715     {
10716       /* Remember which ptr-operator we were processing.  */
10717       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
10718
10719       /* Consume the `*' or `&'.  */
10720       cp_lexer_consume_token (parser->lexer);
10721
10722       /* A `*' can be followed by a cv-qualifier-seq, and so can a
10723          `&', if we are allowing GNU extensions.  (The only qualifier
10724          that can legally appear after `&' is `restrict', but that is
10725          enforced during semantic analysis.  */
10726       if (code == INDIRECT_REF
10727           || cp_parser_allow_gnu_extensions_p (parser))
10728         *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10729     }
10730   else
10731     {
10732       /* Try the pointer-to-member case.  */
10733       cp_parser_parse_tentatively (parser);
10734       /* Look for the optional `::' operator.  */
10735       cp_parser_global_scope_opt (parser,
10736                                   /*current_scope_valid_p=*/false);
10737       /* Look for the nested-name specifier.  */
10738       cp_parser_nested_name_specifier (parser,
10739                                        /*typename_keyword_p=*/false,
10740                                        /*check_dependency_p=*/true,
10741                                        /*type_p=*/false,
10742                                        /*is_declaration=*/false);
10743       /* If we found it, and the next token is a `*', then we are
10744          indeed looking at a pointer-to-member operator.  */
10745       if (!cp_parser_error_occurred (parser)
10746           && cp_parser_require (parser, CPP_MULT, "`*'"))
10747         {
10748           /* The type of which the member is a member is given by the
10749              current SCOPE.  */
10750           *type = parser->scope;
10751           /* The next name will not be qualified.  */
10752           parser->scope = NULL_TREE;
10753           parser->qualifying_scope = NULL_TREE;
10754           parser->object_scope = NULL_TREE;
10755           /* Indicate that the `*' operator was used.  */
10756           code = INDIRECT_REF;
10757           /* Look for the optional cv-qualifier-seq.  */
10758           *cv_qualifier_seq = cp_parser_cv_qualifier_seq_opt (parser);
10759         }
10760       /* If that didn't work we don't have a ptr-operator.  */
10761       if (!cp_parser_parse_definitely (parser))
10762         cp_parser_error (parser, "expected ptr-operator");
10763     }
10764
10765   return code;
10766 }
10767
10768 /* Parse an (optional) cv-qualifier-seq.
10769
10770    cv-qualifier-seq:
10771      cv-qualifier cv-qualifier-seq [opt]
10772
10773    Returns a TREE_LIST.  The TREE_VALUE of each node is the
10774    representation of a cv-qualifier.  */
10775
10776 static tree
10777 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
10778 {
10779   tree cv_qualifiers = NULL_TREE;
10780
10781   while (true)
10782     {
10783       tree cv_qualifier;
10784
10785       /* Look for the next cv-qualifier.  */
10786       cv_qualifier = cp_parser_cv_qualifier_opt (parser);
10787       /* If we didn't find one, we're done.  */
10788       if (!cv_qualifier)
10789         break;
10790
10791       /* Add this cv-qualifier to the list.  */
10792       cv_qualifiers
10793         = tree_cons (NULL_TREE, cv_qualifier, cv_qualifiers);
10794     }
10795
10796   /* We built up the list in reverse order.  */
10797   return nreverse (cv_qualifiers);
10798 }
10799
10800 /* Parse an (optional) cv-qualifier.
10801
10802    cv-qualifier:
10803      const
10804      volatile
10805
10806    GNU Extension:
10807
10808    cv-qualifier:
10809      __restrict__ */
10810
10811 static tree
10812 cp_parser_cv_qualifier_opt (cp_parser* parser)
10813 {
10814   cp_token *token;
10815   tree cv_qualifier = NULL_TREE;
10816
10817   /* Peek at the next token.  */
10818   token = cp_lexer_peek_token (parser->lexer);
10819   /* See if it's a cv-qualifier.  */
10820   switch (token->keyword)
10821     {
10822     case RID_CONST:
10823     case RID_VOLATILE:
10824     case RID_RESTRICT:
10825       /* Save the value of the token.  */
10826       cv_qualifier = token->value;
10827       /* Consume the token.  */
10828       cp_lexer_consume_token (parser->lexer);
10829       break;
10830
10831     default:
10832       break;
10833     }
10834
10835   return cv_qualifier;
10836 }
10837
10838 /* Parse a declarator-id.
10839
10840    declarator-id:
10841      id-expression
10842      :: [opt] nested-name-specifier [opt] type-name
10843
10844    In the `id-expression' case, the value returned is as for
10845    cp_parser_id_expression if the id-expression was an unqualified-id.
10846    If the id-expression was a qualified-id, then a SCOPE_REF is
10847    returned.  The first operand is the scope (either a NAMESPACE_DECL
10848    or TREE_TYPE), but the second is still just a representation of an
10849    unqualified-id.  */
10850
10851 static tree
10852 cp_parser_declarator_id (cp_parser* parser)
10853 {
10854   tree id_expression;
10855
10856   /* The expression must be an id-expression.  Assume that qualified
10857      names are the names of types so that:
10858
10859        template <class T>
10860        int S<T>::R::i = 3;
10861
10862      will work; we must treat `S<T>::R' as the name of a type.
10863      Similarly, assume that qualified names are templates, where
10864      required, so that:
10865
10866        template <class T>
10867        int S<T>::R<T>::i = 3;
10868
10869      will work, too.  */
10870   id_expression = cp_parser_id_expression (parser,
10871                                            /*template_keyword_p=*/false,
10872                                            /*check_dependency_p=*/false,
10873                                            /*template_p=*/NULL,
10874                                            /*declarator_p=*/true);
10875   /* If the name was qualified, create a SCOPE_REF to represent
10876      that.  */
10877   if (parser->scope)
10878     {
10879       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
10880       parser->scope = NULL_TREE;
10881     }
10882
10883   return id_expression;
10884 }
10885
10886 /* Parse a type-id.
10887
10888    type-id:
10889      type-specifier-seq abstract-declarator [opt]
10890
10891    Returns the TYPE specified.  */
10892
10893 static tree
10894 cp_parser_type_id (cp_parser* parser)
10895 {
10896   tree type_specifier_seq;
10897   tree abstract_declarator;
10898
10899   /* Parse the type-specifier-seq.  */
10900   type_specifier_seq
10901     = cp_parser_type_specifier_seq (parser);
10902   if (type_specifier_seq == error_mark_node)
10903     return error_mark_node;
10904
10905   /* There might or might not be an abstract declarator.  */
10906   cp_parser_parse_tentatively (parser);
10907   /* Look for the declarator.  */
10908   abstract_declarator
10909     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
10910                             /*parenthesized_p=*/NULL);
10911   /* Check to see if there really was a declarator.  */
10912   if (!cp_parser_parse_definitely (parser))
10913     abstract_declarator = NULL_TREE;
10914
10915   return groktypename (build_tree_list (type_specifier_seq,
10916                                         abstract_declarator));
10917 }
10918
10919 /* Parse a type-specifier-seq.
10920
10921    type-specifier-seq:
10922      type-specifier type-specifier-seq [opt]
10923
10924    GNU extension:
10925
10926    type-specifier-seq:
10927      attributes type-specifier-seq [opt]
10928
10929    Returns a TREE_LIST.  Either the TREE_VALUE of each node is a
10930    type-specifier, or the TREE_PURPOSE is a list of attributes.  */
10931
10932 static tree
10933 cp_parser_type_specifier_seq (cp_parser* parser)
10934 {
10935   bool seen_type_specifier = false;
10936   tree type_specifier_seq = NULL_TREE;
10937
10938   /* Parse the type-specifiers and attributes.  */
10939   while (true)
10940     {
10941       tree type_specifier;
10942
10943       /* Check for attributes first.  */
10944       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
10945         {
10946           type_specifier_seq = tree_cons (cp_parser_attributes_opt (parser),
10947                                           NULL_TREE,
10948                                           type_specifier_seq);
10949           continue;
10950         }
10951
10952       /* After the first type-specifier, others are optional.  */
10953       if (seen_type_specifier)
10954         cp_parser_parse_tentatively (parser);
10955       /* Look for the type-specifier.  */
10956       type_specifier = cp_parser_type_specifier (parser,
10957                                                  CP_PARSER_FLAGS_NONE,
10958                                                  /*is_friend=*/false,
10959                                                  /*is_declaration=*/false,
10960                                                  NULL,
10961                                                  NULL);
10962       /* If the first type-specifier could not be found, this is not a
10963          type-specifier-seq at all.  */
10964       if (!seen_type_specifier && type_specifier == error_mark_node)
10965         return error_mark_node;
10966       /* If subsequent type-specifiers could not be found, the
10967          type-specifier-seq is complete.  */
10968       else if (seen_type_specifier && !cp_parser_parse_definitely (parser))
10969         break;
10970
10971       /* Add the new type-specifier to the list.  */
10972       type_specifier_seq
10973         = tree_cons (NULL_TREE, type_specifier, type_specifier_seq);
10974       seen_type_specifier = true;
10975     }
10976
10977   /* We built up the list in reverse order.  */
10978   return nreverse (type_specifier_seq);
10979 }
10980
10981 /* Parse a parameter-declaration-clause.
10982
10983    parameter-declaration-clause:
10984      parameter-declaration-list [opt] ... [opt]
10985      parameter-declaration-list , ...
10986
10987    Returns a representation for the parameter declarations.  Each node
10988    is a TREE_LIST.  (See cp_parser_parameter_declaration for the exact
10989    representation.)  If the parameter-declaration-clause ends with an
10990    ellipsis, PARMLIST_ELLIPSIS_P will hold of the first node in the
10991    list.  A return value of NULL_TREE indicates a
10992    parameter-declaration-clause consisting only of an ellipsis.  */
10993
10994 static tree
10995 cp_parser_parameter_declaration_clause (cp_parser* parser)
10996 {
10997   tree parameters;
10998   cp_token *token;
10999   bool ellipsis_p;
11000
11001   /* Peek at the next token.  */
11002   token = cp_lexer_peek_token (parser->lexer);
11003   /* Check for trivial parameter-declaration-clauses.  */
11004   if (token->type == CPP_ELLIPSIS)
11005     {
11006       /* Consume the `...' token.  */
11007       cp_lexer_consume_token (parser->lexer);
11008       return NULL_TREE;
11009     }
11010   else if (token->type == CPP_CLOSE_PAREN)
11011     /* There are no parameters.  */
11012     {
11013 #ifndef NO_IMPLICIT_EXTERN_C
11014       if (in_system_header && current_class_type == NULL
11015           && current_lang_name == lang_name_c)
11016         return NULL_TREE;
11017       else
11018 #endif
11019         return void_list_node;
11020     }
11021   /* Check for `(void)', too, which is a special case.  */
11022   else if (token->keyword == RID_VOID
11023            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11024                == CPP_CLOSE_PAREN))
11025     {
11026       /* Consume the `void' token.  */
11027       cp_lexer_consume_token (parser->lexer);
11028       /* There are no parameters.  */
11029       return void_list_node;
11030     }
11031
11032   /* Parse the parameter-declaration-list.  */
11033   parameters = cp_parser_parameter_declaration_list (parser);
11034   /* If a parse error occurred while parsing the
11035      parameter-declaration-list, then the entire
11036      parameter-declaration-clause is erroneous.  */
11037   if (parameters == error_mark_node)
11038     return error_mark_node;
11039
11040   /* Peek at the next token.  */
11041   token = cp_lexer_peek_token (parser->lexer);
11042   /* If it's a `,', the clause should terminate with an ellipsis.  */
11043   if (token->type == CPP_COMMA)
11044     {
11045       /* Consume the `,'.  */
11046       cp_lexer_consume_token (parser->lexer);
11047       /* Expect an ellipsis.  */
11048       ellipsis_p
11049         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11050     }
11051   /* It might also be `...' if the optional trailing `,' was
11052      omitted.  */
11053   else if (token->type == CPP_ELLIPSIS)
11054     {
11055       /* Consume the `...' token.  */
11056       cp_lexer_consume_token (parser->lexer);
11057       /* And remember that we saw it.  */
11058       ellipsis_p = true;
11059     }
11060   else
11061     ellipsis_p = false;
11062
11063   /* Finish the parameter list.  */
11064   return finish_parmlist (parameters, ellipsis_p);
11065 }
11066
11067 /* Parse a parameter-declaration-list.
11068
11069    parameter-declaration-list:
11070      parameter-declaration
11071      parameter-declaration-list , parameter-declaration
11072
11073    Returns a representation of the parameter-declaration-list, as for
11074    cp_parser_parameter_declaration_clause.  However, the
11075    `void_list_node' is never appended to the list.  */
11076
11077 static tree
11078 cp_parser_parameter_declaration_list (cp_parser* parser)
11079 {
11080   tree parameters = NULL_TREE;
11081
11082   /* Look for more parameters.  */
11083   while (true)
11084     {
11085       tree parameter;
11086       bool parenthesized_p;
11087       /* Parse the parameter.  */
11088       parameter
11089         = cp_parser_parameter_declaration (parser,
11090                                            /*template_parm_p=*/false,
11091                                            &parenthesized_p);
11092
11093       /* If a parse error occurred parsing the parameter declaration,
11094          then the entire parameter-declaration-list is erroneous.  */
11095       if (parameter == error_mark_node)
11096         {
11097           parameters = error_mark_node;
11098           break;
11099         }
11100       /* Add the new parameter to the list.  */
11101       TREE_CHAIN (parameter) = parameters;
11102       parameters = parameter;
11103
11104       /* Peek at the next token.  */
11105       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11106           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11107         /* The parameter-declaration-list is complete.  */
11108         break;
11109       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11110         {
11111           cp_token *token;
11112
11113           /* Peek at the next token.  */
11114           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11115           /* If it's an ellipsis, then the list is complete.  */
11116           if (token->type == CPP_ELLIPSIS)
11117             break;
11118           /* Otherwise, there must be more parameters.  Consume the
11119              `,'.  */
11120           cp_lexer_consume_token (parser->lexer);
11121           /* When parsing something like:
11122
11123                 int i(float f, double d)
11124
11125              we can tell after seeing the declaration for "f" that we
11126              are not looking at an initialization of a variable "i",
11127              but rather at the declaration of a function "i".
11128
11129              Due to the fact that the parsing of template arguments
11130              (as specified to a template-id) requires backtracking we
11131              cannot use this technique when inside a template argument
11132              list.  */
11133           if (!parser->in_template_argument_list_p
11134               && !parser->in_type_id_in_expr_p
11135               && cp_parser_parsing_tentatively (parser)
11136               && !cp_parser_committed_to_tentative_parse (parser)
11137               /* However, a parameter-declaration of the form
11138                  "foat(f)" (which is a valid declaration of a
11139                  parameter "f") can also be interpreted as an
11140                  expression (the conversion of "f" to "float").  */
11141               && !parenthesized_p)
11142             cp_parser_commit_to_tentative_parse (parser);
11143         }
11144       else
11145         {
11146           cp_parser_error (parser, "expected `,' or `...'");
11147           if (!cp_parser_parsing_tentatively (parser)
11148               || cp_parser_committed_to_tentative_parse (parser))
11149             cp_parser_skip_to_closing_parenthesis (parser,
11150                                                    /*recovering=*/true,
11151                                                    /*or_comma=*/false,
11152                                                    /*consume_paren=*/false);
11153           break;
11154         }
11155     }
11156
11157   /* We built up the list in reverse order; straighten it out now.  */
11158   return nreverse (parameters);
11159 }
11160
11161 /* Parse a parameter declaration.
11162
11163    parameter-declaration:
11164      decl-specifier-seq declarator
11165      decl-specifier-seq declarator = assignment-expression
11166      decl-specifier-seq abstract-declarator [opt]
11167      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11168
11169    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11170    declares a template parameter.  (In that case, a non-nested `>'
11171    token encountered during the parsing of the assignment-expression
11172    is not interpreted as a greater-than operator.)
11173
11174    Returns a TREE_LIST representing the parameter-declaration.  The
11175    TREE_PURPOSE is the default argument expression, or NULL_TREE if
11176    there is no default argument.  The TREE_VALUE is a representation
11177    of the decl-specifier-seq and declarator.  In particular, the
11178    TREE_VALUE will be a TREE_LIST whose TREE_PURPOSE represents the
11179    decl-specifier-seq and whose TREE_VALUE represents the declarator.
11180    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
11181    the declarator is of the form "(p)".  */
11182
11183 static tree
11184 cp_parser_parameter_declaration (cp_parser *parser,
11185                                  bool template_parm_p,
11186                                  bool *parenthesized_p)
11187 {
11188   int declares_class_or_enum;
11189   bool greater_than_is_operator_p;
11190   tree decl_specifiers;
11191   tree attributes;
11192   tree declarator;
11193   tree default_argument;
11194   tree parameter;
11195   cp_token *token;
11196   const char *saved_message;
11197
11198   /* In a template parameter, `>' is not an operator.
11199
11200      [temp.param]
11201
11202      When parsing a default template-argument for a non-type
11203      template-parameter, the first non-nested `>' is taken as the end
11204      of the template parameter-list rather than a greater-than
11205      operator.  */
11206   greater_than_is_operator_p = !template_parm_p;
11207
11208   /* Type definitions may not appear in parameter types.  */
11209   saved_message = parser->type_definition_forbidden_message;
11210   parser->type_definition_forbidden_message
11211     = "types may not be defined in parameter types";
11212
11213   /* Parse the declaration-specifiers.  */
11214   decl_specifiers
11215     = cp_parser_decl_specifier_seq (parser,
11216                                     CP_PARSER_FLAGS_NONE,
11217                                     &attributes,
11218                                     &declares_class_or_enum);
11219   /* If an error occurred, there's no reason to attempt to parse the
11220      rest of the declaration.  */
11221   if (cp_parser_error_occurred (parser))
11222     {
11223       parser->type_definition_forbidden_message = saved_message;
11224       return error_mark_node;
11225     }
11226
11227   /* Peek at the next token.  */
11228   token = cp_lexer_peek_token (parser->lexer);
11229   /* If the next token is a `)', `,', `=', `>', or `...', then there
11230      is no declarator.  */
11231   if (token->type == CPP_CLOSE_PAREN
11232       || token->type == CPP_COMMA
11233       || token->type == CPP_EQ
11234       || token->type == CPP_ELLIPSIS
11235       || token->type == CPP_GREATER)
11236     {
11237       declarator = NULL_TREE;
11238       if (parenthesized_p)
11239         *parenthesized_p = false;
11240     }
11241   /* Otherwise, there should be a declarator.  */
11242   else
11243     {
11244       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11245       parser->default_arg_ok_p = false;
11246
11247       /* After seeing a decl-specifier-seq, if the next token is not a
11248          "(", there is no possibility that the code is a valid
11249          expression.  Therefore, if parsing tentatively, we commit at
11250          this point.  */
11251       if (!parser->in_template_argument_list_p
11252           /* In an expression context, having seen:
11253
11254                (int((char ...
11255
11256              we cannot be sure whether we are looking at a
11257              function-type (taking a "char" as a parameter) or a cast
11258              of some object of type "char" to "int".  */
11259           && !parser->in_type_id_in_expr_p
11260           && cp_parser_parsing_tentatively (parser)
11261           && !cp_parser_committed_to_tentative_parse (parser)
11262           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11263         cp_parser_commit_to_tentative_parse (parser);
11264       /* Parse the declarator.  */
11265       declarator = cp_parser_declarator (parser,
11266                                          CP_PARSER_DECLARATOR_EITHER,
11267                                          /*ctor_dtor_or_conv_p=*/NULL,
11268                                          parenthesized_p);
11269       parser->default_arg_ok_p = saved_default_arg_ok_p;
11270       /* After the declarator, allow more attributes.  */
11271       attributes = chainon (attributes, cp_parser_attributes_opt (parser));
11272     }
11273
11274   /* The restriction on defining new types applies only to the type
11275      of the parameter, not to the default argument.  */
11276   parser->type_definition_forbidden_message = saved_message;
11277
11278   /* If the next token is `=', then process a default argument.  */
11279   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11280     {
11281       bool saved_greater_than_is_operator_p;
11282       /* Consume the `='.  */
11283       cp_lexer_consume_token (parser->lexer);
11284
11285       /* If we are defining a class, then the tokens that make up the
11286          default argument must be saved and processed later.  */
11287       if (!template_parm_p && at_class_scope_p ()
11288           && TYPE_BEING_DEFINED (current_class_type))
11289         {
11290           unsigned depth = 0;
11291
11292           /* Create a DEFAULT_ARG to represented the unparsed default
11293              argument.  */
11294           default_argument = make_node (DEFAULT_ARG);
11295           DEFARG_TOKENS (default_argument) = cp_token_cache_new ();
11296
11297           /* Add tokens until we have processed the entire default
11298              argument.  */
11299           while (true)
11300             {
11301               bool done = false;
11302               cp_token *token;
11303
11304               /* Peek at the next token.  */
11305               token = cp_lexer_peek_token (parser->lexer);
11306               /* What we do depends on what token we have.  */
11307               switch (token->type)
11308                 {
11309                   /* In valid code, a default argument must be
11310                      immediately followed by a `,' `)', or `...'.  */
11311                 case CPP_COMMA:
11312                 case CPP_CLOSE_PAREN:
11313                 case CPP_ELLIPSIS:
11314                   /* If we run into a non-nested `;', `}', or `]',
11315                      then the code is invalid -- but the default
11316                      argument is certainly over.  */
11317                 case CPP_SEMICOLON:
11318                 case CPP_CLOSE_BRACE:
11319                 case CPP_CLOSE_SQUARE:
11320                   if (depth == 0)
11321                     done = true;
11322                   /* Update DEPTH, if necessary.  */
11323                   else if (token->type == CPP_CLOSE_PAREN
11324                            || token->type == CPP_CLOSE_BRACE
11325                            || token->type == CPP_CLOSE_SQUARE)
11326                     --depth;
11327                   break;
11328
11329                 case CPP_OPEN_PAREN:
11330                 case CPP_OPEN_SQUARE:
11331                 case CPP_OPEN_BRACE:
11332                   ++depth;
11333                   break;
11334
11335                 case CPP_GREATER:
11336                   /* If we see a non-nested `>', and `>' is not an
11337                      operator, then it marks the end of the default
11338                      argument.  */
11339                   if (!depth && !greater_than_is_operator_p)
11340                     done = true;
11341                   break;
11342
11343                   /* If we run out of tokens, issue an error message.  */
11344                 case CPP_EOF:
11345                   error ("file ends in default argument");
11346                   done = true;
11347                   break;
11348
11349                 case CPP_NAME:
11350                 case CPP_SCOPE:
11351                   /* In these cases, we should look for template-ids.
11352                      For example, if the default argument is
11353                      `X<int, double>()', we need to do name lookup to
11354                      figure out whether or not `X' is a template; if
11355                      so, the `,' does not end the default argument.
11356
11357                      That is not yet done.  */
11358                   break;
11359
11360                 default:
11361                   break;
11362                 }
11363
11364               /* If we've reached the end, stop.  */
11365               if (done)
11366                 break;
11367
11368               /* Add the token to the token block.  */
11369               token = cp_lexer_consume_token (parser->lexer);
11370               cp_token_cache_push_token (DEFARG_TOKENS (default_argument),
11371                                          token);
11372             }
11373         }
11374       /* Outside of a class definition, we can just parse the
11375          assignment-expression.  */
11376       else
11377         {
11378           bool saved_local_variables_forbidden_p;
11379
11380           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11381              set correctly.  */
11382           saved_greater_than_is_operator_p
11383             = parser->greater_than_is_operator_p;
11384           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11385           /* Local variable names (and the `this' keyword) may not
11386              appear in a default argument.  */
11387           saved_local_variables_forbidden_p
11388             = parser->local_variables_forbidden_p;
11389           parser->local_variables_forbidden_p = true;
11390           /* Parse the assignment-expression.  */
11391           default_argument = cp_parser_assignment_expression (parser);
11392           /* Restore saved state.  */
11393           parser->greater_than_is_operator_p
11394             = saved_greater_than_is_operator_p;
11395           parser->local_variables_forbidden_p
11396             = saved_local_variables_forbidden_p;
11397         }
11398       if (!parser->default_arg_ok_p)
11399         {
11400           if (!flag_pedantic_errors)
11401             warning ("deprecated use of default argument for parameter of non-function");
11402           else
11403             {
11404               error ("default arguments are only permitted for function parameters");
11405               default_argument = NULL_TREE;
11406             }
11407         }
11408     }
11409   else
11410     default_argument = NULL_TREE;
11411
11412   /* Create the representation of the parameter.  */
11413   if (attributes)
11414     decl_specifiers = tree_cons (attributes, NULL_TREE, decl_specifiers);
11415   parameter = build_tree_list (default_argument,
11416                                build_tree_list (decl_specifiers,
11417                                                 declarator));
11418
11419   return parameter;
11420 }
11421
11422 /* Parse a function-body.
11423
11424    function-body:
11425      compound_statement  */
11426
11427 static void
11428 cp_parser_function_body (cp_parser *parser)
11429 {
11430   cp_parser_compound_statement (parser, false);
11431 }
11432
11433 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11434    true if a ctor-initializer was present.  */
11435
11436 static bool
11437 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11438 {
11439   tree body;
11440   bool ctor_initializer_p;
11441
11442   /* Begin the function body.  */
11443   body = begin_function_body ();
11444   /* Parse the optional ctor-initializer.  */
11445   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11446   /* Parse the function-body.  */
11447   cp_parser_function_body (parser);
11448   /* Finish the function body.  */
11449   finish_function_body (body);
11450
11451   return ctor_initializer_p;
11452 }
11453
11454 /* Parse an initializer.
11455
11456    initializer:
11457      = initializer-clause
11458      ( expression-list )
11459
11460    Returns a expression representing the initializer.  If no
11461    initializer is present, NULL_TREE is returned.
11462
11463    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11464    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11465    set to FALSE if there is no initializer present.  If there is an
11466    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11467    is set to true; otherwise it is set to false.  */
11468
11469 static tree
11470 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11471                        bool* non_constant_p)
11472 {
11473   cp_token *token;
11474   tree init;
11475
11476   /* Peek at the next token.  */
11477   token = cp_lexer_peek_token (parser->lexer);
11478
11479   /* Let our caller know whether or not this initializer was
11480      parenthesized.  */
11481   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11482   /* Assume that the initializer is constant.  */
11483   *non_constant_p = false;
11484
11485   if (token->type == CPP_EQ)
11486     {
11487       /* Consume the `='.  */
11488       cp_lexer_consume_token (parser->lexer);
11489       /* Parse the initializer-clause.  */
11490       init = cp_parser_initializer_clause (parser, non_constant_p);
11491     }
11492   else if (token->type == CPP_OPEN_PAREN)
11493     init = cp_parser_parenthesized_expression_list (parser, false,
11494                                                     non_constant_p);
11495   else
11496     {
11497       /* Anything else is an error.  */
11498       cp_parser_error (parser, "expected initializer");
11499       init = error_mark_node;
11500     }
11501
11502   return init;
11503 }
11504
11505 /* Parse an initializer-clause.
11506
11507    initializer-clause:
11508      assignment-expression
11509      { initializer-list , [opt] }
11510      { }
11511
11512    Returns an expression representing the initializer.
11513
11514    If the `assignment-expression' production is used the value
11515    returned is simply a representation for the expression.
11516
11517    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11518    the elements of the initializer-list (or NULL_TREE, if the last
11519    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11520    NULL_TREE.  There is no way to detect whether or not the optional
11521    trailing `,' was provided.  NON_CONSTANT_P is as for
11522    cp_parser_initializer.  */
11523
11524 static tree
11525 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11526 {
11527   tree initializer;
11528
11529   /* If it is not a `{', then we are looking at an
11530      assignment-expression.  */
11531   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11532     initializer
11533       = cp_parser_constant_expression (parser,
11534                                        /*allow_non_constant_p=*/true,
11535                                        non_constant_p);
11536   else
11537     {
11538       /* Consume the `{' token.  */
11539       cp_lexer_consume_token (parser->lexer);
11540       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11541       initializer = make_node (CONSTRUCTOR);
11542       /* Mark it with TREE_HAS_CONSTRUCTOR.  This should not be
11543          necessary, but check_initializer depends upon it, for
11544          now.  */
11545       TREE_HAS_CONSTRUCTOR (initializer) = 1;
11546       /* If it's not a `}', then there is a non-trivial initializer.  */
11547       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11548         {
11549           /* Parse the initializer list.  */
11550           CONSTRUCTOR_ELTS (initializer)
11551             = cp_parser_initializer_list (parser, non_constant_p);
11552           /* A trailing `,' token is allowed.  */
11553           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11554             cp_lexer_consume_token (parser->lexer);
11555         }
11556       /* Now, there should be a trailing `}'.  */
11557       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11558     }
11559
11560   return initializer;
11561 }
11562
11563 /* Parse an initializer-list.
11564
11565    initializer-list:
11566      initializer-clause
11567      initializer-list , initializer-clause
11568
11569    GNU Extension:
11570
11571    initializer-list:
11572      identifier : initializer-clause
11573      initializer-list, identifier : initializer-clause
11574
11575    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
11576    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
11577    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
11578    as for cp_parser_initializer.  */
11579
11580 static tree
11581 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
11582 {
11583   tree initializers = NULL_TREE;
11584
11585   /* Assume all of the expressions are constant.  */
11586   *non_constant_p = false;
11587
11588   /* Parse the rest of the list.  */
11589   while (true)
11590     {
11591       cp_token *token;
11592       tree identifier;
11593       tree initializer;
11594       bool clause_non_constant_p;
11595
11596       /* If the next token is an identifier and the following one is a
11597          colon, we are looking at the GNU designated-initializer
11598          syntax.  */
11599       if (cp_parser_allow_gnu_extensions_p (parser)
11600           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
11601           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
11602         {
11603           /* Consume the identifier.  */
11604           identifier = cp_lexer_consume_token (parser->lexer)->value;
11605           /* Consume the `:'.  */
11606           cp_lexer_consume_token (parser->lexer);
11607         }
11608       else
11609         identifier = NULL_TREE;
11610
11611       /* Parse the initializer.  */
11612       initializer = cp_parser_initializer_clause (parser,
11613                                                   &clause_non_constant_p);
11614       /* If any clause is non-constant, so is the entire initializer.  */
11615       if (clause_non_constant_p)
11616         *non_constant_p = true;
11617       /* Add it to the list.  */
11618       initializers = tree_cons (identifier, initializer, initializers);
11619
11620       /* If the next token is not a comma, we have reached the end of
11621          the list.  */
11622       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11623         break;
11624
11625       /* Peek at the next token.  */
11626       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11627       /* If the next token is a `}', then we're still done.  An
11628          initializer-clause can have a trailing `,' after the
11629          initializer-list and before the closing `}'.  */
11630       if (token->type == CPP_CLOSE_BRACE)
11631         break;
11632
11633       /* Consume the `,' token.  */
11634       cp_lexer_consume_token (parser->lexer);
11635     }
11636
11637   /* The initializers were built up in reverse order, so we need to
11638      reverse them now.  */
11639   return nreverse (initializers);
11640 }
11641
11642 /* Classes [gram.class] */
11643
11644 /* Parse a class-name.
11645
11646    class-name:
11647      identifier
11648      template-id
11649
11650    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
11651    to indicate that names looked up in dependent types should be
11652    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
11653    keyword has been used to indicate that the name that appears next
11654    is a template.  TYPE_P is true iff the next name should be treated
11655    as class-name, even if it is declared to be some other kind of name
11656    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11657    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
11658    being defined in a class-head.
11659
11660    Returns the TYPE_DECL representing the class.  */
11661
11662 static tree
11663 cp_parser_class_name (cp_parser *parser,
11664                       bool typename_keyword_p,
11665                       bool template_keyword_p,
11666                       bool type_p,
11667                       bool check_dependency_p,
11668                       bool class_head_p,
11669                       bool is_declaration)
11670 {
11671   tree decl;
11672   tree scope;
11673   bool typename_p;
11674   cp_token *token;
11675
11676   /* All class-names start with an identifier.  */
11677   token = cp_lexer_peek_token (parser->lexer);
11678   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
11679     {
11680       cp_parser_error (parser, "expected class-name");
11681       return error_mark_node;
11682     }
11683
11684   /* PARSER->SCOPE can be cleared when parsing the template-arguments
11685      to a template-id, so we save it here.  */
11686   scope = parser->scope;
11687   if (scope == error_mark_node)
11688     return error_mark_node;
11689
11690   /* Any name names a type if we're following the `typename' keyword
11691      in a qualified name where the enclosing scope is type-dependent.  */
11692   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
11693                 && dependent_type_p (scope));
11694   /* Handle the common case (an identifier, but not a template-id)
11695      efficiently.  */
11696   if (token->type == CPP_NAME
11697       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
11698     {
11699       tree identifier;
11700
11701       /* Look for the identifier.  */
11702       identifier = cp_parser_identifier (parser);
11703       /* If the next token isn't an identifier, we are certainly not
11704          looking at a class-name.  */
11705       if (identifier == error_mark_node)
11706         decl = error_mark_node;
11707       /* If we know this is a type-name, there's no need to look it
11708          up.  */
11709       else if (typename_p)
11710         decl = identifier;
11711       else
11712         {
11713           /* If the next token is a `::', then the name must be a type
11714              name.
11715
11716              [basic.lookup.qual]
11717
11718              During the lookup for a name preceding the :: scope
11719              resolution operator, object, function, and enumerator
11720              names are ignored.  */
11721           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11722             type_p = true;
11723           /* Look up the name.  */
11724           decl = cp_parser_lookup_name (parser, identifier,
11725                                         type_p,
11726                                         /*is_template=*/false,
11727                                         /*is_namespace=*/false,
11728                                         check_dependency_p);
11729         }
11730     }
11731   else
11732     {
11733       /* Try a template-id.  */
11734       decl = cp_parser_template_id (parser, template_keyword_p,
11735                                     check_dependency_p,
11736                                     is_declaration);
11737       if (decl == error_mark_node)
11738         return error_mark_node;
11739     }
11740
11741   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
11742
11743   /* If this is a typename, create a TYPENAME_TYPE.  */
11744   if (typename_p && decl != error_mark_node)
11745     {
11746       decl = make_typename_type (scope, decl, /*complain=*/1);
11747       if (decl != error_mark_node)
11748         decl = TYPE_NAME (decl);
11749     }
11750
11751   /* Check to see that it is really the name of a class.  */
11752   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
11753       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
11754       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
11755     /* Situations like this:
11756
11757          template <typename T> struct A {
11758            typename T::template X<int>::I i;
11759          };
11760
11761        are problematic.  Is `T::template X<int>' a class-name?  The
11762        standard does not seem to be definitive, but there is no other
11763        valid interpretation of the following `::'.  Therefore, those
11764        names are considered class-names.  */
11765     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
11766   else if (decl == error_mark_node
11767            || TREE_CODE (decl) != TYPE_DECL
11768            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
11769     {
11770       cp_parser_error (parser, "expected class-name");
11771       return error_mark_node;
11772     }
11773
11774   return decl;
11775 }
11776
11777 /* Parse a class-specifier.
11778
11779    class-specifier:
11780      class-head { member-specification [opt] }
11781
11782    Returns the TREE_TYPE representing the class.  */
11783
11784 static tree
11785 cp_parser_class_specifier (cp_parser* parser)
11786 {
11787   cp_token *token;
11788   tree type;
11789   tree attributes = NULL_TREE;
11790   int has_trailing_semicolon;
11791   bool nested_name_specifier_p;
11792   unsigned saved_num_template_parameter_lists;
11793   bool pop_p = false;
11794
11795   push_deferring_access_checks (dk_no_deferred);
11796
11797   /* Parse the class-head.  */
11798   type = cp_parser_class_head (parser,
11799                                &nested_name_specifier_p);
11800   /* If the class-head was a semantic disaster, skip the entire body
11801      of the class.  */
11802   if (!type)
11803     {
11804       cp_parser_skip_to_end_of_block_or_statement (parser);
11805       pop_deferring_access_checks ();
11806       return error_mark_node;
11807     }
11808
11809   /* Look for the `{'.  */
11810   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
11811     {
11812       pop_deferring_access_checks ();
11813       return error_mark_node;
11814     }
11815
11816   /* Issue an error message if type-definitions are forbidden here.  */
11817   cp_parser_check_type_definition (parser);
11818   /* Remember that we are defining one more class.  */
11819   ++parser->num_classes_being_defined;
11820   /* Inside the class, surrounding template-parameter-lists do not
11821      apply.  */
11822   saved_num_template_parameter_lists
11823     = parser->num_template_parameter_lists;
11824   parser->num_template_parameter_lists = 0;
11825
11826   /* Start the class.  */
11827   if (nested_name_specifier_p)
11828     pop_p = push_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11829   type = begin_class_definition (type);
11830   if (type == error_mark_node)
11831     /* If the type is erroneous, skip the entire body of the class.  */
11832     cp_parser_skip_to_closing_brace (parser);
11833   else
11834     /* Parse the member-specification.  */
11835     cp_parser_member_specification_opt (parser);
11836   /* Look for the trailing `}'.  */
11837   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11838   /* We get better error messages by noticing a common problem: a
11839      missing trailing `;'.  */
11840   token = cp_lexer_peek_token (parser->lexer);
11841   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
11842   /* Look for attributes to apply to this class.  */
11843   if (cp_parser_allow_gnu_extensions_p (parser))
11844     attributes = cp_parser_attributes_opt (parser);
11845   /* If we got any attributes in class_head, xref_tag will stick them in
11846      TREE_TYPE of the type.  Grab them now.  */
11847   if (type != error_mark_node)
11848     {
11849       attributes = chainon (TYPE_ATTRIBUTES (type), attributes);
11850       TYPE_ATTRIBUTES (type) = NULL_TREE;
11851       type = finish_struct (type, attributes);
11852     }
11853   if (pop_p)
11854     pop_scope (CP_DECL_CONTEXT (TYPE_MAIN_DECL (type)));
11855   /* If this class is not itself within the scope of another class,
11856      then we need to parse the bodies of all of the queued function
11857      definitions.  Note that the queued functions defined in a class
11858      are not always processed immediately following the
11859      class-specifier for that class.  Consider:
11860
11861        struct A {
11862          struct B { void f() { sizeof (A); } };
11863        };
11864
11865      If `f' were processed before the processing of `A' were
11866      completed, there would be no way to compute the size of `A'.
11867      Note that the nesting we are interested in here is lexical --
11868      not the semantic nesting given by TYPE_CONTEXT.  In particular,
11869      for:
11870
11871        struct A { struct B; };
11872        struct A::B { void f() { } };
11873
11874      there is no need to delay the parsing of `A::B::f'.  */
11875   if (--parser->num_classes_being_defined == 0)
11876     {
11877       tree queue_entry;
11878       tree fn;
11879
11880       /* In a first pass, parse default arguments to the functions.
11881          Then, in a second pass, parse the bodies of the functions.
11882          This two-phased approach handles cases like:
11883
11884             struct S {
11885               void f() { g(); }
11886               void g(int i = 3);
11887             };
11888
11889          */
11890       for (TREE_PURPOSE (parser->unparsed_functions_queues)
11891              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
11892            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
11893            TREE_PURPOSE (parser->unparsed_functions_queues)
11894              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
11895         {
11896           fn = TREE_VALUE (queue_entry);
11897           /* Make sure that any template parameters are in scope.  */
11898           maybe_begin_member_template_processing (fn);
11899           /* If there are default arguments that have not yet been processed,
11900              take care of them now.  */
11901           cp_parser_late_parsing_default_args (parser, fn);
11902           /* Remove any template parameters from the symbol table.  */
11903           maybe_end_member_template_processing ();
11904         }
11905       /* Now parse the body of the functions.  */
11906       for (TREE_VALUE (parser->unparsed_functions_queues)
11907              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
11908            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
11909            TREE_VALUE (parser->unparsed_functions_queues)
11910              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
11911         {
11912           /* Figure out which function we need to process.  */
11913           fn = TREE_VALUE (queue_entry);
11914
11915           /* A hack to prevent garbage collection.  */
11916           function_depth++;
11917
11918           /* Parse the function.  */
11919           cp_parser_late_parsing_for_member (parser, fn);
11920           function_depth--;
11921         }
11922
11923     }
11924
11925   /* Put back any saved access checks.  */
11926   pop_deferring_access_checks ();
11927
11928   /* Restore the count of active template-parameter-lists.  */
11929   parser->num_template_parameter_lists
11930     = saved_num_template_parameter_lists;
11931
11932   return type;
11933 }
11934
11935 /* Parse a class-head.
11936
11937    class-head:
11938      class-key identifier [opt] base-clause [opt]
11939      class-key nested-name-specifier identifier base-clause [opt]
11940      class-key nested-name-specifier [opt] template-id
11941        base-clause [opt]
11942
11943    GNU Extensions:
11944      class-key attributes identifier [opt] base-clause [opt]
11945      class-key attributes nested-name-specifier identifier base-clause [opt]
11946      class-key attributes nested-name-specifier [opt] template-id
11947        base-clause [opt]
11948
11949    Returns the TYPE of the indicated class.  Sets
11950    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
11951    involving a nested-name-specifier was used, and FALSE otherwise.
11952
11953    Returns NULL_TREE if the class-head is syntactically valid, but
11954    semantically invalid in a way that means we should skip the entire
11955    body of the class.  */
11956
11957 static tree
11958 cp_parser_class_head (cp_parser* parser,
11959                       bool* nested_name_specifier_p)
11960 {
11961   cp_token *token;
11962   tree nested_name_specifier;
11963   enum tag_types class_key;
11964   tree id = NULL_TREE;
11965   tree type = NULL_TREE;
11966   tree attributes;
11967   bool template_id_p = false;
11968   bool qualified_p = false;
11969   bool invalid_nested_name_p = false;
11970   bool invalid_explicit_specialization_p = false;
11971   bool pop_p = false;
11972   unsigned num_templates;
11973
11974   /* Assume no nested-name-specifier will be present.  */
11975   *nested_name_specifier_p = false;
11976   /* Assume no template parameter lists will be used in defining the
11977      type.  */
11978   num_templates = 0;
11979
11980   /* Look for the class-key.  */
11981   class_key = cp_parser_class_key (parser);
11982   if (class_key == none_type)
11983     return error_mark_node;
11984
11985   /* Parse the attributes.  */
11986   attributes = cp_parser_attributes_opt (parser);
11987
11988   /* If the next token is `::', that is invalid -- but sometimes
11989      people do try to write:
11990
11991        struct ::S {};
11992
11993      Handle this gracefully by accepting the extra qualifier, and then
11994      issuing an error about it later if this really is a
11995      class-head.  If it turns out just to be an elaborated type
11996      specifier, remain silent.  */
11997   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
11998     qualified_p = true;
11999
12000   push_deferring_access_checks (dk_no_check);
12001
12002   /* Determine the name of the class.  Begin by looking for an
12003      optional nested-name-specifier.  */
12004   nested_name_specifier
12005     = cp_parser_nested_name_specifier_opt (parser,
12006                                            /*typename_keyword_p=*/false,
12007                                            /*check_dependency_p=*/false,
12008                                            /*type_p=*/false,
12009                                            /*is_declaration=*/false);
12010   /* If there was a nested-name-specifier, then there *must* be an
12011      identifier.  */
12012   if (nested_name_specifier)
12013     {
12014       /* Although the grammar says `identifier', it really means
12015          `class-name' or `template-name'.  You are only allowed to
12016          define a class that has already been declared with this
12017          syntax.
12018
12019          The proposed resolution for Core Issue 180 says that whever
12020          you see `class T::X' you should treat `X' as a type-name.
12021
12022          It is OK to define an inaccessible class; for example:
12023
12024            class A { class B; };
12025            class A::B {};
12026
12027          We do not know if we will see a class-name, or a
12028          template-name.  We look for a class-name first, in case the
12029          class-name is a template-id; if we looked for the
12030          template-name first we would stop after the template-name.  */
12031       cp_parser_parse_tentatively (parser);
12032       type = cp_parser_class_name (parser,
12033                                    /*typename_keyword_p=*/false,
12034                                    /*template_keyword_p=*/false,
12035                                    /*type_p=*/true,
12036                                    /*check_dependency_p=*/false,
12037                                    /*class_head_p=*/true,
12038                                    /*is_declaration=*/false);
12039       /* If that didn't work, ignore the nested-name-specifier.  */
12040       if (!cp_parser_parse_definitely (parser))
12041         {
12042           invalid_nested_name_p = true;
12043           id = cp_parser_identifier (parser);
12044           if (id == error_mark_node)
12045             id = NULL_TREE;
12046         }
12047       /* If we could not find a corresponding TYPE, treat this
12048          declaration like an unqualified declaration.  */
12049       if (type == error_mark_node)
12050         nested_name_specifier = NULL_TREE;
12051       /* Otherwise, count the number of templates used in TYPE and its
12052          containing scopes.  */
12053       else
12054         {
12055           tree scope;
12056
12057           for (scope = TREE_TYPE (type);
12058                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12059                scope = (TYPE_P (scope)
12060                         ? TYPE_CONTEXT (scope)
12061                         : DECL_CONTEXT (scope)))
12062             if (TYPE_P (scope)
12063                 && CLASS_TYPE_P (scope)
12064                 && CLASSTYPE_TEMPLATE_INFO (scope)
12065                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12066                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12067               ++num_templates;
12068         }
12069     }
12070   /* Otherwise, the identifier is optional.  */
12071   else
12072     {
12073       /* We don't know whether what comes next is a template-id,
12074          an identifier, or nothing at all.  */
12075       cp_parser_parse_tentatively (parser);
12076       /* Check for a template-id.  */
12077       id = cp_parser_template_id (parser,
12078                                   /*template_keyword_p=*/false,
12079                                   /*check_dependency_p=*/true,
12080                                   /*is_declaration=*/true);
12081       /* If that didn't work, it could still be an identifier.  */
12082       if (!cp_parser_parse_definitely (parser))
12083         {
12084           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12085             id = cp_parser_identifier (parser);
12086           else
12087             id = NULL_TREE;
12088         }
12089       else
12090         {
12091           template_id_p = true;
12092           ++num_templates;
12093         }
12094     }
12095
12096   pop_deferring_access_checks ();
12097
12098   cp_parser_check_for_invalid_template_id (parser, id);
12099
12100   /* If it's not a `:' or a `{' then we can't really be looking at a
12101      class-head, since a class-head only appears as part of a
12102      class-specifier.  We have to detect this situation before calling
12103      xref_tag, since that has irreversible side-effects.  */
12104   if (!cp_parser_next_token_starts_class_definition_p (parser))
12105     {
12106       cp_parser_error (parser, "expected `{' or `:'");
12107       return error_mark_node;
12108     }
12109
12110   /* At this point, we're going ahead with the class-specifier, even
12111      if some other problem occurs.  */
12112   cp_parser_commit_to_tentative_parse (parser);
12113   /* Issue the error about the overly-qualified name now.  */
12114   if (qualified_p)
12115     cp_parser_error (parser,
12116                      "global qualification of class name is invalid");
12117   else if (invalid_nested_name_p)
12118     cp_parser_error (parser,
12119                      "qualified name does not name a class");
12120   else if (nested_name_specifier)
12121     {
12122       tree scope;
12123       /* Figure out in what scope the declaration is being placed.  */
12124       scope = current_scope ();
12125       if (!scope)
12126         scope = current_namespace;
12127       /* If that scope does not contain the scope in which the
12128          class was originally declared, the program is invalid.  */
12129       if (scope && !is_ancestor (scope, nested_name_specifier))
12130         {
12131           error ("declaration of `%D' in `%D' which does not "
12132                  "enclose `%D'", type, scope, nested_name_specifier);
12133           type = NULL_TREE;
12134           goto done;
12135         }
12136       /* [dcl.meaning]
12137
12138          A declarator-id shall not be qualified exception of the
12139          definition of a ... nested class outside of its class
12140          ... [or] a the definition or explicit instantiation of a
12141          class member of a namespace outside of its namespace.  */
12142       if (scope == nested_name_specifier)
12143         {
12144           pedwarn ("extra qualification ignored");
12145           nested_name_specifier = NULL_TREE;
12146           num_templates = 0;
12147         }
12148     }
12149   /* An explicit-specialization must be preceded by "template <>".  If
12150      it is not, try to recover gracefully.  */
12151   if (at_namespace_scope_p ()
12152       && parser->num_template_parameter_lists == 0
12153       && template_id_p)
12154     {
12155       error ("an explicit specialization must be preceded by 'template <>'");
12156       invalid_explicit_specialization_p = true;
12157       /* Take the same action that would have been taken by
12158          cp_parser_explicit_specialization.  */
12159       ++parser->num_template_parameter_lists;
12160       begin_specialization ();
12161     }
12162   /* There must be no "return" statements between this point and the
12163      end of this function; set "type "to the correct return value and
12164      use "goto done;" to return.  */
12165   /* Make sure that the right number of template parameters were
12166      present.  */
12167   if (!cp_parser_check_template_parameters (parser, num_templates))
12168     {
12169       /* If something went wrong, there is no point in even trying to
12170          process the class-definition.  */
12171       type = NULL_TREE;
12172       goto done;
12173     }
12174
12175   /* Look up the type.  */
12176   if (template_id_p)
12177     {
12178       type = TREE_TYPE (id);
12179       maybe_process_partial_specialization (type);
12180     }
12181   else if (!nested_name_specifier)
12182     {
12183       /* If the class was unnamed, create a dummy name.  */
12184       if (!id)
12185         id = make_anon_name ();
12186       type = xref_tag (class_key, id, attributes, /*globalize=*/false,
12187                        parser->num_template_parameter_lists);
12188     }
12189   else
12190     {
12191       tree class_type;
12192       bool pop_p = false;
12193
12194       /* Given:
12195
12196             template <typename T> struct S { struct T };
12197             template <typename T> struct S<T>::T { };
12198
12199          we will get a TYPENAME_TYPE when processing the definition of
12200          `S::T'.  We need to resolve it to the actual type before we
12201          try to define it.  */
12202       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12203         {
12204           class_type = resolve_typename_type (TREE_TYPE (type),
12205                                               /*only_current_p=*/false);
12206           if (class_type != error_mark_node)
12207             type = TYPE_NAME (class_type);
12208           else
12209             {
12210               cp_parser_error (parser, "could not resolve typename type");
12211               type = error_mark_node;
12212             }
12213         }
12214
12215       maybe_process_partial_specialization (TREE_TYPE (type));
12216       class_type = current_class_type;
12217       /* Enter the scope indicated by the nested-name-specifier.  */
12218       if (nested_name_specifier)
12219         pop_p = push_scope (nested_name_specifier);
12220       /* Get the canonical version of this type.  */
12221       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12222       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12223           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12224         type = push_template_decl (type);
12225       type = TREE_TYPE (type);
12226       if (nested_name_specifier)
12227         {
12228           *nested_name_specifier_p = true;
12229           if (pop_p)
12230             pop_scope (nested_name_specifier);
12231         }
12232     }
12233   /* Indicate whether this class was declared as a `class' or as a
12234      `struct'.  */
12235   if (TREE_CODE (type) == RECORD_TYPE)
12236     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12237   cp_parser_check_class_key (class_key, type);
12238
12239   /* Enter the scope containing the class; the names of base classes
12240      should be looked up in that context.  For example, given:
12241
12242        struct A { struct B {}; struct C; };
12243        struct A::C : B {};
12244
12245      is valid.  */
12246   if (nested_name_specifier)
12247     pop_p = push_scope (nested_name_specifier);
12248   /* Now, look for the base-clause.  */
12249   token = cp_lexer_peek_token (parser->lexer);
12250   if (token->type == CPP_COLON)
12251     {
12252       tree bases;
12253
12254       /* Get the list of base-classes.  */
12255       bases = cp_parser_base_clause (parser);
12256       /* Process them.  */
12257       xref_basetypes (type, bases);
12258     }
12259   /* Leave the scope given by the nested-name-specifier.  We will
12260      enter the class scope itself while processing the members.  */
12261   if (pop_p)
12262     pop_scope (nested_name_specifier);
12263
12264  done:
12265   if (invalid_explicit_specialization_p)
12266     {
12267       end_specialization ();
12268       --parser->num_template_parameter_lists;
12269     }
12270   return type;
12271 }
12272
12273 /* Parse a class-key.
12274
12275    class-key:
12276      class
12277      struct
12278      union
12279
12280    Returns the kind of class-key specified, or none_type to indicate
12281    error.  */
12282
12283 static enum tag_types
12284 cp_parser_class_key (cp_parser* parser)
12285 {
12286   cp_token *token;
12287   enum tag_types tag_type;
12288
12289   /* Look for the class-key.  */
12290   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12291   if (!token)
12292     return none_type;
12293
12294   /* Check to see if the TOKEN is a class-key.  */
12295   tag_type = cp_parser_token_is_class_key (token);
12296   if (!tag_type)
12297     cp_parser_error (parser, "expected class-key");
12298   return tag_type;
12299 }
12300
12301 /* Parse an (optional) member-specification.
12302
12303    member-specification:
12304      member-declaration member-specification [opt]
12305      access-specifier : member-specification [opt]  */
12306
12307 static void
12308 cp_parser_member_specification_opt (cp_parser* parser)
12309 {
12310   while (true)
12311     {
12312       cp_token *token;
12313       enum rid keyword;
12314
12315       /* Peek at the next token.  */
12316       token = cp_lexer_peek_token (parser->lexer);
12317       /* If it's a `}', or EOF then we've seen all the members.  */
12318       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12319         break;
12320
12321       /* See if this token is a keyword.  */
12322       keyword = token->keyword;
12323       switch (keyword)
12324         {
12325         case RID_PUBLIC:
12326         case RID_PROTECTED:
12327         case RID_PRIVATE:
12328           /* Consume the access-specifier.  */
12329           cp_lexer_consume_token (parser->lexer);
12330           /* Remember which access-specifier is active.  */
12331           current_access_specifier = token->value;
12332           /* Look for the `:'.  */
12333           cp_parser_require (parser, CPP_COLON, "`:'");
12334           break;
12335
12336         default:
12337           /* Otherwise, the next construction must be a
12338              member-declaration.  */
12339           cp_parser_member_declaration (parser);
12340         }
12341     }
12342 }
12343
12344 /* Parse a member-declaration.
12345
12346    member-declaration:
12347      decl-specifier-seq [opt] member-declarator-list [opt] ;
12348      function-definition ; [opt]
12349      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12350      using-declaration
12351      template-declaration
12352
12353    member-declarator-list:
12354      member-declarator
12355      member-declarator-list , member-declarator
12356
12357    member-declarator:
12358      declarator pure-specifier [opt]
12359      declarator constant-initializer [opt]
12360      identifier [opt] : constant-expression
12361
12362    GNU Extensions:
12363
12364    member-declaration:
12365      __extension__ member-declaration
12366
12367    member-declarator:
12368      declarator attributes [opt] pure-specifier [opt]
12369      declarator attributes [opt] constant-initializer [opt]
12370      identifier [opt] attributes [opt] : constant-expression  */
12371
12372 static void
12373 cp_parser_member_declaration (cp_parser* parser)
12374 {
12375   tree decl_specifiers;
12376   tree prefix_attributes;
12377   tree decl;
12378   int declares_class_or_enum;
12379   bool friend_p;
12380   cp_token *token;
12381   int saved_pedantic;
12382
12383   /* Check for the `__extension__' keyword.  */
12384   if (cp_parser_extension_opt (parser, &saved_pedantic))
12385     {
12386       /* Recurse.  */
12387       cp_parser_member_declaration (parser);
12388       /* Restore the old value of the PEDANTIC flag.  */
12389       pedantic = saved_pedantic;
12390
12391       return;
12392     }
12393
12394   /* Check for a template-declaration.  */
12395   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12396     {
12397       /* Parse the template-declaration.  */
12398       cp_parser_template_declaration (parser, /*member_p=*/true);
12399
12400       return;
12401     }
12402
12403   /* Check for a using-declaration.  */
12404   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12405     {
12406       /* Parse the using-declaration.  */
12407       cp_parser_using_declaration (parser);
12408
12409       return;
12410     }
12411
12412   /* Parse the decl-specifier-seq.  */
12413   decl_specifiers
12414     = cp_parser_decl_specifier_seq (parser,
12415                                     CP_PARSER_FLAGS_OPTIONAL,
12416                                     &prefix_attributes,
12417                                     &declares_class_or_enum);
12418   /* Check for an invalid type-name.  */
12419   if (cp_parser_parse_and_diagnose_invalid_type_name (parser))
12420     return;
12421   /* If there is no declarator, then the decl-specifier-seq should
12422      specify a type.  */
12423   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12424     {
12425       /* If there was no decl-specifier-seq, and the next token is a
12426          `;', then we have something like:
12427
12428            struct S { ; };
12429
12430          [class.mem]
12431
12432          Each member-declaration shall declare at least one member
12433          name of the class.  */
12434       if (!decl_specifiers)
12435         {
12436           if (pedantic)
12437             pedwarn ("extra semicolon");
12438         }
12439       else
12440         {
12441           tree type;
12442
12443           /* See if this declaration is a friend.  */
12444           friend_p = cp_parser_friend_p (decl_specifiers);
12445           /* If there were decl-specifiers, check to see if there was
12446              a class-declaration.  */
12447           type = check_tag_decl (decl_specifiers);
12448           /* Nested classes have already been added to the class, but
12449              a `friend' needs to be explicitly registered.  */
12450           if (friend_p)
12451             {
12452               /* If the `friend' keyword was present, the friend must
12453                  be introduced with a class-key.  */
12454                if (!declares_class_or_enum)
12455                  error ("a class-key must be used when declaring a friend");
12456                /* In this case:
12457
12458                     template <typename T> struct A {
12459                       friend struct A<T>::B;
12460                     };
12461
12462                   A<T>::B will be represented by a TYPENAME_TYPE, and
12463                   therefore not recognized by check_tag_decl.  */
12464                if (!type)
12465                  {
12466                    tree specifier;
12467
12468                    for (specifier = decl_specifiers;
12469                         specifier;
12470                         specifier = TREE_CHAIN (specifier))
12471                      {
12472                        tree s = TREE_VALUE (specifier);
12473
12474                        if (TREE_CODE (s) == IDENTIFIER_NODE)
12475                          get_global_value_if_present (s, &type);
12476                        if (TREE_CODE (s) == TYPE_DECL)
12477                          s = TREE_TYPE (s);
12478                        if (TYPE_P (s))
12479                          {
12480                            type = s;
12481                            break;
12482                          }
12483                      }
12484                  }
12485                if (!type || !TYPE_P (type))
12486                  error ("friend declaration does not name a class or "
12487                         "function");
12488                else
12489                  make_friend_class (current_class_type, type,
12490                                     /*complain=*/true);
12491             }
12492           /* If there is no TYPE, an error message will already have
12493              been issued.  */
12494           else if (!type)
12495             ;
12496           /* An anonymous aggregate has to be handled specially; such
12497              a declaration really declares a data member (with a
12498              particular type), as opposed to a nested class.  */
12499           else if (ANON_AGGR_TYPE_P (type))
12500             {
12501               /* Remove constructors and such from TYPE, now that we
12502                  know it is an anonymous aggregate.  */
12503               fixup_anonymous_aggr (type);
12504               /* And make the corresponding data member.  */
12505               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12506               /* Add it to the class.  */
12507               finish_member_declaration (decl);
12508             }
12509           else
12510             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12511         }
12512     }
12513   else
12514     {
12515       /* See if these declarations will be friends.  */
12516       friend_p = cp_parser_friend_p (decl_specifiers);
12517
12518       /* Keep going until we hit the `;' at the end of the
12519          declaration.  */
12520       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12521         {
12522           tree attributes = NULL_TREE;
12523           tree first_attribute;
12524
12525           /* Peek at the next token.  */
12526           token = cp_lexer_peek_token (parser->lexer);
12527
12528           /* Check for a bitfield declaration.  */
12529           if (token->type == CPP_COLON
12530               || (token->type == CPP_NAME
12531                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12532                   == CPP_COLON))
12533             {
12534               tree identifier;
12535               tree width;
12536
12537               /* Get the name of the bitfield.  Note that we cannot just
12538                  check TOKEN here because it may have been invalidated by
12539                  the call to cp_lexer_peek_nth_token above.  */
12540               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12541                 identifier = cp_parser_identifier (parser);
12542               else
12543                 identifier = NULL_TREE;
12544
12545               /* Consume the `:' token.  */
12546               cp_lexer_consume_token (parser->lexer);
12547               /* Get the width of the bitfield.  */
12548               width
12549                 = cp_parser_constant_expression (parser,
12550                                                  /*allow_non_constant=*/false,
12551                                                  NULL);
12552
12553               /* Look for attributes that apply to the bitfield.  */
12554               attributes = cp_parser_attributes_opt (parser);
12555               /* Remember which attributes are prefix attributes and
12556                  which are not.  */
12557               first_attribute = attributes;
12558               /* Combine the attributes.  */
12559               attributes = chainon (prefix_attributes, attributes);
12560
12561               /* Create the bitfield declaration.  */
12562               decl = grokbitfield (identifier,
12563                                    decl_specifiers,
12564                                    width);
12565               /* Apply the attributes.  */
12566               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
12567             }
12568           else
12569             {
12570               tree declarator;
12571               tree initializer;
12572               tree asm_specification;
12573               int ctor_dtor_or_conv_p;
12574
12575               /* Parse the declarator.  */
12576               declarator
12577                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12578                                         &ctor_dtor_or_conv_p,
12579                                         /*parenthesized_p=*/NULL);
12580
12581               /* If something went wrong parsing the declarator, make sure
12582                  that we at least consume some tokens.  */
12583               if (declarator == error_mark_node)
12584                 {
12585                   /* Skip to the end of the statement.  */
12586                   cp_parser_skip_to_end_of_statement (parser);
12587                   /* If the next token is not a semicolon, that is
12588                      probably because we just skipped over the body of
12589                      a function.  So, we consume a semicolon if
12590                      present, but do not issue an error message if it
12591                      is not present.  */
12592                   if (cp_lexer_next_token_is (parser->lexer,
12593                                               CPP_SEMICOLON))
12594                     cp_lexer_consume_token (parser->lexer);
12595                   return;
12596                 }
12597
12598               cp_parser_check_for_definition_in_return_type
12599                 (declarator, declares_class_or_enum);
12600
12601               /* Look for an asm-specification.  */
12602               asm_specification = cp_parser_asm_specification_opt (parser);
12603               /* Look for attributes that apply to the declaration.  */
12604               attributes = cp_parser_attributes_opt (parser);
12605               /* Remember which attributes are prefix attributes and
12606                  which are not.  */
12607               first_attribute = attributes;
12608               /* Combine the attributes.  */
12609               attributes = chainon (prefix_attributes, attributes);
12610
12611               /* If it's an `=', then we have a constant-initializer or a
12612                  pure-specifier.  It is not correct to parse the
12613                  initializer before registering the member declaration
12614                  since the member declaration should be in scope while
12615                  its initializer is processed.  However, the rest of the
12616                  front end does not yet provide an interface that allows
12617                  us to handle this correctly.  */
12618               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12619                 {
12620                   /* In [class.mem]:
12621
12622                      A pure-specifier shall be used only in the declaration of
12623                      a virtual function.
12624
12625                      A member-declarator can contain a constant-initializer
12626                      only if it declares a static member of integral or
12627                      enumeration type.
12628
12629                      Therefore, if the DECLARATOR is for a function, we look
12630                      for a pure-specifier; otherwise, we look for a
12631                      constant-initializer.  When we call `grokfield', it will
12632                      perform more stringent semantics checks.  */
12633                   if (TREE_CODE (declarator) == CALL_EXPR)
12634                     initializer = cp_parser_pure_specifier (parser);
12635                   else
12636                     /* Parse the initializer.  */
12637                     initializer = cp_parser_constant_initializer (parser);
12638                 }
12639               /* Otherwise, there is no initializer.  */
12640               else
12641                 initializer = NULL_TREE;
12642
12643               /* See if we are probably looking at a function
12644                  definition.  We are certainly not looking at at a
12645                  member-declarator.  Calling `grokfield' has
12646                  side-effects, so we must not do it unless we are sure
12647                  that we are looking at a member-declarator.  */
12648               if (cp_parser_token_starts_function_definition_p
12649                   (cp_lexer_peek_token (parser->lexer)))
12650                 {
12651                   /* The grammar does not allow a pure-specifier to be
12652                      used when a member function is defined.  (It is
12653                      possible that this fact is an oversight in the
12654                      standard, since a pure function may be defined
12655                      outside of the class-specifier.  */
12656                   if (initializer)
12657                     error ("pure-specifier on function-definition");
12658                   decl = cp_parser_save_member_function_body (parser,
12659                                                               decl_specifiers,
12660                                                               declarator,
12661                                                               attributes);
12662                   /* If the member was not a friend, declare it here.  */
12663                   if (!friend_p)
12664                     finish_member_declaration (decl);
12665                   /* Peek at the next token.  */
12666                   token = cp_lexer_peek_token (parser->lexer);
12667                   /* If the next token is a semicolon, consume it.  */
12668                   if (token->type == CPP_SEMICOLON)
12669                     cp_lexer_consume_token (parser->lexer);
12670                   return;
12671                 }
12672               else
12673                 {
12674                   /* Create the declaration.  */
12675                   decl = grokfield (declarator, decl_specifiers,
12676                                     initializer, asm_specification,
12677                                     attributes);
12678                   /* Any initialization must have been from a
12679                      constant-expression.  */
12680                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
12681                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
12682                 }
12683             }
12684
12685           /* Reset PREFIX_ATTRIBUTES.  */
12686           while (attributes && TREE_CHAIN (attributes) != first_attribute)
12687             attributes = TREE_CHAIN (attributes);
12688           if (attributes)
12689             TREE_CHAIN (attributes) = NULL_TREE;
12690
12691           /* If there is any qualification still in effect, clear it
12692              now; we will be starting fresh with the next declarator.  */
12693           parser->scope = NULL_TREE;
12694           parser->qualifying_scope = NULL_TREE;
12695           parser->object_scope = NULL_TREE;
12696           /* If it's a `,', then there are more declarators.  */
12697           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12698             cp_lexer_consume_token (parser->lexer);
12699           /* If the next token isn't a `;', then we have a parse error.  */
12700           else if (cp_lexer_next_token_is_not (parser->lexer,
12701                                                CPP_SEMICOLON))
12702             {
12703               cp_parser_error (parser, "expected `;'");
12704               /* Skip tokens until we find a `;'.  */
12705               cp_parser_skip_to_end_of_statement (parser);
12706
12707               break;
12708             }
12709
12710           if (decl)
12711             {
12712               /* Add DECL to the list of members.  */
12713               if (!friend_p)
12714                 finish_member_declaration (decl);
12715
12716               if (TREE_CODE (decl) == FUNCTION_DECL)
12717                 cp_parser_save_default_args (parser, decl);
12718             }
12719         }
12720     }
12721
12722   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
12723 }
12724
12725 /* Parse a pure-specifier.
12726
12727    pure-specifier:
12728      = 0
12729
12730    Returns INTEGER_ZERO_NODE if a pure specifier is found.
12731    Otherwise, ERROR_MARK_NODE is returned.  */
12732
12733 static tree
12734 cp_parser_pure_specifier (cp_parser* parser)
12735 {
12736   cp_token *token;
12737
12738   /* Look for the `=' token.  */
12739   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12740     return error_mark_node;
12741   /* Look for the `0' token.  */
12742   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
12743   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
12744      to get information from the lexer about how the number was
12745      spelled in order to fix this problem.  */
12746   if (!token || !integer_zerop (token->value))
12747     return error_mark_node;
12748
12749   return integer_zero_node;
12750 }
12751
12752 /* Parse a constant-initializer.
12753
12754    constant-initializer:
12755      = constant-expression
12756
12757    Returns a representation of the constant-expression.  */
12758
12759 static tree
12760 cp_parser_constant_initializer (cp_parser* parser)
12761 {
12762   /* Look for the `=' token.  */
12763   if (!cp_parser_require (parser, CPP_EQ, "`='"))
12764     return error_mark_node;
12765
12766   /* It is invalid to write:
12767
12768        struct S { static const int i = { 7 }; };
12769
12770      */
12771   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12772     {
12773       cp_parser_error (parser,
12774                        "a brace-enclosed initializer is not allowed here");
12775       /* Consume the opening brace.  */
12776       cp_lexer_consume_token (parser->lexer);
12777       /* Skip the initializer.  */
12778       cp_parser_skip_to_closing_brace (parser);
12779       /* Look for the trailing `}'.  */
12780       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12781
12782       return error_mark_node;
12783     }
12784
12785   return cp_parser_constant_expression (parser,
12786                                         /*allow_non_constant=*/false,
12787                                         NULL);
12788 }
12789
12790 /* Derived classes [gram.class.derived] */
12791
12792 /* Parse a base-clause.
12793
12794    base-clause:
12795      : base-specifier-list
12796
12797    base-specifier-list:
12798      base-specifier
12799      base-specifier-list , base-specifier
12800
12801    Returns a TREE_LIST representing the base-classes, in the order in
12802    which they were declared.  The representation of each node is as
12803    described by cp_parser_base_specifier.
12804
12805    In the case that no bases are specified, this function will return
12806    NULL_TREE, not ERROR_MARK_NODE.  */
12807
12808 static tree
12809 cp_parser_base_clause (cp_parser* parser)
12810 {
12811   tree bases = NULL_TREE;
12812
12813   /* Look for the `:' that begins the list.  */
12814   cp_parser_require (parser, CPP_COLON, "`:'");
12815
12816   /* Scan the base-specifier-list.  */
12817   while (true)
12818     {
12819       cp_token *token;
12820       tree base;
12821
12822       /* Look for the base-specifier.  */
12823       base = cp_parser_base_specifier (parser);
12824       /* Add BASE to the front of the list.  */
12825       if (base != error_mark_node)
12826         {
12827           TREE_CHAIN (base) = bases;
12828           bases = base;
12829         }
12830       /* Peek at the next token.  */
12831       token = cp_lexer_peek_token (parser->lexer);
12832       /* If it's not a comma, then the list is complete.  */
12833       if (token->type != CPP_COMMA)
12834         break;
12835       /* Consume the `,'.  */
12836       cp_lexer_consume_token (parser->lexer);
12837     }
12838
12839   /* PARSER->SCOPE may still be non-NULL at this point, if the last
12840      base class had a qualified name.  However, the next name that
12841      appears is certainly not qualified.  */
12842   parser->scope = NULL_TREE;
12843   parser->qualifying_scope = NULL_TREE;
12844   parser->object_scope = NULL_TREE;
12845
12846   return nreverse (bases);
12847 }
12848
12849 /* Parse a base-specifier.
12850
12851    base-specifier:
12852      :: [opt] nested-name-specifier [opt] class-name
12853      virtual access-specifier [opt] :: [opt] nested-name-specifier
12854        [opt] class-name
12855      access-specifier virtual [opt] :: [opt] nested-name-specifier
12856        [opt] class-name
12857
12858    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
12859    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
12860    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
12861    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
12862
12863 static tree
12864 cp_parser_base_specifier (cp_parser* parser)
12865 {
12866   cp_token *token;
12867   bool done = false;
12868   bool virtual_p = false;
12869   bool duplicate_virtual_error_issued_p = false;
12870   bool duplicate_access_error_issued_p = false;
12871   bool class_scope_p, template_p;
12872   tree access = access_default_node;
12873   tree type;
12874
12875   /* Process the optional `virtual' and `access-specifier'.  */
12876   while (!done)
12877     {
12878       /* Peek at the next token.  */
12879       token = cp_lexer_peek_token (parser->lexer);
12880       /* Process `virtual'.  */
12881       switch (token->keyword)
12882         {
12883         case RID_VIRTUAL:
12884           /* If `virtual' appears more than once, issue an error.  */
12885           if (virtual_p && !duplicate_virtual_error_issued_p)
12886             {
12887               cp_parser_error (parser,
12888                                "`virtual' specified more than once in base-specified");
12889               duplicate_virtual_error_issued_p = true;
12890             }
12891
12892           virtual_p = true;
12893
12894           /* Consume the `virtual' token.  */
12895           cp_lexer_consume_token (parser->lexer);
12896
12897           break;
12898
12899         case RID_PUBLIC:
12900         case RID_PROTECTED:
12901         case RID_PRIVATE:
12902           /* If more than one access specifier appears, issue an
12903              error.  */
12904           if (access != access_default_node
12905               && !duplicate_access_error_issued_p)
12906             {
12907               cp_parser_error (parser,
12908                                "more than one access specifier in base-specified");
12909               duplicate_access_error_issued_p = true;
12910             }
12911
12912           access = ridpointers[(int) token->keyword];
12913
12914           /* Consume the access-specifier.  */
12915           cp_lexer_consume_token (parser->lexer);
12916
12917           break;
12918
12919         default:
12920           done = true;
12921           break;
12922         }
12923     }
12924   /* It is not uncommon to see programs mechanically, erroneously, use
12925      the 'typename' keyword to denote (dependent) qualified types
12926      as base classes.  */
12927   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
12928     {
12929       if (!processing_template_decl)
12930         error ("keyword `typename' not allowed outside of templates");
12931       else
12932         error ("keyword `typename' not allowed in this context "
12933                "(the base class is implicitly a type)");
12934       cp_lexer_consume_token (parser->lexer);
12935     }
12936
12937   /* Look for the optional `::' operator.  */
12938   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
12939   /* Look for the nested-name-specifier.  The simplest way to
12940      implement:
12941
12942        [temp.res]
12943
12944        The keyword `typename' is not permitted in a base-specifier or
12945        mem-initializer; in these contexts a qualified name that
12946        depends on a template-parameter is implicitly assumed to be a
12947        type name.
12948
12949      is to pretend that we have seen the `typename' keyword at this
12950      point.  */
12951   cp_parser_nested_name_specifier_opt (parser,
12952                                        /*typename_keyword_p=*/true,
12953                                        /*check_dependency_p=*/true,
12954                                        /*type_p=*/true,
12955                                        /*is_declaration=*/true);
12956   /* If the base class is given by a qualified name, assume that names
12957      we see are type names or templates, as appropriate.  */
12958   class_scope_p = (parser->scope && TYPE_P (parser->scope));
12959   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
12960
12961   /* Finally, look for the class-name.  */
12962   type = cp_parser_class_name (parser,
12963                                class_scope_p,
12964                                template_p,
12965                                /*type_p=*/true,
12966                                /*check_dependency_p=*/true,
12967                                /*class_head_p=*/false,
12968                                /*is_declaration=*/true);
12969
12970   if (type == error_mark_node)
12971     return error_mark_node;
12972
12973   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
12974 }
12975
12976 /* Exception handling [gram.exception] */
12977
12978 /* Parse an (optional) exception-specification.
12979
12980    exception-specification:
12981      throw ( type-id-list [opt] )
12982
12983    Returns a TREE_LIST representing the exception-specification.  The
12984    TREE_VALUE of each node is a type.  */
12985
12986 static tree
12987 cp_parser_exception_specification_opt (cp_parser* parser)
12988 {
12989   cp_token *token;
12990   tree type_id_list;
12991
12992   /* Peek at the next token.  */
12993   token = cp_lexer_peek_token (parser->lexer);
12994   /* If it's not `throw', then there's no exception-specification.  */
12995   if (!cp_parser_is_keyword (token, RID_THROW))
12996     return NULL_TREE;
12997
12998   /* Consume the `throw'.  */
12999   cp_lexer_consume_token (parser->lexer);
13000
13001   /* Look for the `('.  */
13002   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13003
13004   /* Peek at the next token.  */
13005   token = cp_lexer_peek_token (parser->lexer);
13006   /* If it's not a `)', then there is a type-id-list.  */
13007   if (token->type != CPP_CLOSE_PAREN)
13008     {
13009       const char *saved_message;
13010
13011       /* Types may not be defined in an exception-specification.  */
13012       saved_message = parser->type_definition_forbidden_message;
13013       parser->type_definition_forbidden_message
13014         = "types may not be defined in an exception-specification";
13015       /* Parse the type-id-list.  */
13016       type_id_list = cp_parser_type_id_list (parser);
13017       /* Restore the saved message.  */
13018       parser->type_definition_forbidden_message = saved_message;
13019     }
13020   else
13021     type_id_list = empty_except_spec;
13022
13023   /* Look for the `)'.  */
13024   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13025
13026   return type_id_list;
13027 }
13028
13029 /* Parse an (optional) type-id-list.
13030
13031    type-id-list:
13032      type-id
13033      type-id-list , type-id
13034
13035    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13036    in the order that the types were presented.  */
13037
13038 static tree
13039 cp_parser_type_id_list (cp_parser* parser)
13040 {
13041   tree types = NULL_TREE;
13042
13043   while (true)
13044     {
13045       cp_token *token;
13046       tree type;
13047
13048       /* Get the next type-id.  */
13049       type = cp_parser_type_id (parser);
13050       /* Add it to the list.  */
13051       types = add_exception_specifier (types, type, /*complain=*/1);
13052       /* Peek at the next token.  */
13053       token = cp_lexer_peek_token (parser->lexer);
13054       /* If it is not a `,', we are done.  */
13055       if (token->type != CPP_COMMA)
13056         break;
13057       /* Consume the `,'.  */
13058       cp_lexer_consume_token (parser->lexer);
13059     }
13060
13061   return nreverse (types);
13062 }
13063
13064 /* Parse a try-block.
13065
13066    try-block:
13067      try compound-statement handler-seq  */
13068
13069 static tree
13070 cp_parser_try_block (cp_parser* parser)
13071 {
13072   tree try_block;
13073
13074   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13075   try_block = begin_try_block ();
13076   cp_parser_compound_statement (parser, false);
13077   finish_try_block (try_block);
13078   cp_parser_handler_seq (parser);
13079   finish_handler_sequence (try_block);
13080
13081   return try_block;
13082 }
13083
13084 /* Parse a function-try-block.
13085
13086    function-try-block:
13087      try ctor-initializer [opt] function-body handler-seq  */
13088
13089 static bool
13090 cp_parser_function_try_block (cp_parser* parser)
13091 {
13092   tree try_block;
13093   bool ctor_initializer_p;
13094
13095   /* Look for the `try' keyword.  */
13096   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13097     return false;
13098   /* Let the rest of the front-end know where we are.  */
13099   try_block = begin_function_try_block ();
13100   /* Parse the function-body.  */
13101   ctor_initializer_p
13102     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13103   /* We're done with the `try' part.  */
13104   finish_function_try_block (try_block);
13105   /* Parse the handlers.  */
13106   cp_parser_handler_seq (parser);
13107   /* We're done with the handlers.  */
13108   finish_function_handler_sequence (try_block);
13109
13110   return ctor_initializer_p;
13111 }
13112
13113 /* Parse a handler-seq.
13114
13115    handler-seq:
13116      handler handler-seq [opt]  */
13117
13118 static void
13119 cp_parser_handler_seq (cp_parser* parser)
13120 {
13121   while (true)
13122     {
13123       cp_token *token;
13124
13125       /* Parse the handler.  */
13126       cp_parser_handler (parser);
13127       /* Peek at the next token.  */
13128       token = cp_lexer_peek_token (parser->lexer);
13129       /* If it's not `catch' then there are no more handlers.  */
13130       if (!cp_parser_is_keyword (token, RID_CATCH))
13131         break;
13132     }
13133 }
13134
13135 /* Parse a handler.
13136
13137    handler:
13138      catch ( exception-declaration ) compound-statement  */
13139
13140 static void
13141 cp_parser_handler (cp_parser* parser)
13142 {
13143   tree handler;
13144   tree declaration;
13145
13146   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13147   handler = begin_handler ();
13148   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13149   declaration = cp_parser_exception_declaration (parser);
13150   finish_handler_parms (declaration, handler);
13151   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13152   cp_parser_compound_statement (parser, false);
13153   finish_handler (handler);
13154 }
13155
13156 /* Parse an exception-declaration.
13157
13158    exception-declaration:
13159      type-specifier-seq declarator
13160      type-specifier-seq abstract-declarator
13161      type-specifier-seq
13162      ...
13163
13164    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13165    ellipsis variant is used.  */
13166
13167 static tree
13168 cp_parser_exception_declaration (cp_parser* parser)
13169 {
13170   tree type_specifiers;
13171   tree declarator;
13172   const char *saved_message;
13173
13174   /* If it's an ellipsis, it's easy to handle.  */
13175   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13176     {
13177       /* Consume the `...' token.  */
13178       cp_lexer_consume_token (parser->lexer);
13179       return NULL_TREE;
13180     }
13181
13182   /* Types may not be defined in exception-declarations.  */
13183   saved_message = parser->type_definition_forbidden_message;
13184   parser->type_definition_forbidden_message
13185     = "types may not be defined in exception-declarations";
13186
13187   /* Parse the type-specifier-seq.  */
13188   type_specifiers = cp_parser_type_specifier_seq (parser);
13189   /* If it's a `)', then there is no declarator.  */
13190   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13191     declarator = NULL_TREE;
13192   else
13193     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13194                                        /*ctor_dtor_or_conv_p=*/NULL,
13195                                        /*parenthesized_p=*/NULL);
13196
13197   /* Restore the saved message.  */
13198   parser->type_definition_forbidden_message = saved_message;
13199
13200   return start_handler_parms (type_specifiers, declarator);
13201 }
13202
13203 /* Parse a throw-expression.
13204
13205    throw-expression:
13206      throw assignment-expression [opt]
13207
13208    Returns a THROW_EXPR representing the throw-expression.  */
13209
13210 static tree
13211 cp_parser_throw_expression (cp_parser* parser)
13212 {
13213   tree expression;
13214   cp_token* token;
13215
13216   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13217   token = cp_lexer_peek_token (parser->lexer);
13218   /* Figure out whether or not there is an assignment-expression
13219      following the "throw" keyword.  */
13220   if (token->type == CPP_COMMA
13221       || token->type == CPP_SEMICOLON
13222       || token->type == CPP_CLOSE_PAREN
13223       || token->type == CPP_CLOSE_SQUARE
13224       || token->type == CPP_CLOSE_BRACE
13225       || token->type == CPP_COLON)
13226     expression = NULL_TREE;
13227   else
13228     expression = cp_parser_assignment_expression (parser);
13229
13230   return build_throw (expression);
13231 }
13232
13233 /* GNU Extensions */
13234
13235 /* Parse an (optional) asm-specification.
13236
13237    asm-specification:
13238      asm ( string-literal )
13239
13240    If the asm-specification is present, returns a STRING_CST
13241    corresponding to the string-literal.  Otherwise, returns
13242    NULL_TREE.  */
13243
13244 static tree
13245 cp_parser_asm_specification_opt (cp_parser* parser)
13246 {
13247   cp_token *token;
13248   tree asm_specification;
13249
13250   /* Peek at the next token.  */
13251   token = cp_lexer_peek_token (parser->lexer);
13252   /* If the next token isn't the `asm' keyword, then there's no
13253      asm-specification.  */
13254   if (!cp_parser_is_keyword (token, RID_ASM))
13255     return NULL_TREE;
13256
13257   /* Consume the `asm' token.  */
13258   cp_lexer_consume_token (parser->lexer);
13259   /* Look for the `('.  */
13260   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13261
13262   /* Look for the string-literal.  */
13263   token = cp_parser_require (parser, CPP_STRING, "string-literal");
13264   if (token)
13265     asm_specification = token->value;
13266   else
13267     asm_specification = NULL_TREE;
13268
13269   /* Look for the `)'.  */
13270   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13271
13272   return asm_specification;
13273 }
13274
13275 /* Parse an asm-operand-list.
13276
13277    asm-operand-list:
13278      asm-operand
13279      asm-operand-list , asm-operand
13280
13281    asm-operand:
13282      string-literal ( expression )
13283      [ string-literal ] string-literal ( expression )
13284
13285    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13286    each node is the expression.  The TREE_PURPOSE is itself a
13287    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13288    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13289    is a STRING_CST for the string literal before the parenthesis.  */
13290
13291 static tree
13292 cp_parser_asm_operand_list (cp_parser* parser)
13293 {
13294   tree asm_operands = NULL_TREE;
13295
13296   while (true)
13297     {
13298       tree string_literal;
13299       tree expression;
13300       tree name;
13301       cp_token *token;
13302
13303       c_lex_string_translate = false;
13304
13305       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13306         {
13307           /* Consume the `[' token.  */
13308           cp_lexer_consume_token (parser->lexer);
13309           /* Read the operand name.  */
13310           name = cp_parser_identifier (parser);
13311           if (name != error_mark_node)
13312             name = build_string (IDENTIFIER_LENGTH (name),
13313                                  IDENTIFIER_POINTER (name));
13314           /* Look for the closing `]'.  */
13315           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13316         }
13317       else
13318         name = NULL_TREE;
13319       /* Look for the string-literal.  */
13320       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13321       string_literal = token ? token->value : error_mark_node;
13322       c_lex_string_translate = true;
13323       /* Look for the `('.  */
13324       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13325       /* Parse the expression.  */
13326       expression = cp_parser_expression (parser);
13327       /* Look for the `)'.  */
13328       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13329       c_lex_string_translate = false;
13330       /* Add this operand to the list.  */
13331       asm_operands = tree_cons (build_tree_list (name, string_literal),
13332                                 expression,
13333                                 asm_operands);
13334       /* If the next token is not a `,', there are no more
13335          operands.  */
13336       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13337         break;
13338       /* Consume the `,'.  */
13339       cp_lexer_consume_token (parser->lexer);
13340     }
13341
13342   return nreverse (asm_operands);
13343 }
13344
13345 /* Parse an asm-clobber-list.
13346
13347    asm-clobber-list:
13348      string-literal
13349      asm-clobber-list , string-literal
13350
13351    Returns a TREE_LIST, indicating the clobbers in the order that they
13352    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13353
13354 static tree
13355 cp_parser_asm_clobber_list (cp_parser* parser)
13356 {
13357   tree clobbers = NULL_TREE;
13358
13359   while (true)
13360     {
13361       cp_token *token;
13362       tree string_literal;
13363
13364       /* Look for the string literal.  */
13365       token = cp_parser_require (parser, CPP_STRING, "string-literal");
13366       string_literal = token ? token->value : error_mark_node;
13367       /* Add it to the list.  */
13368       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13369       /* If the next token is not a `,', then the list is
13370          complete.  */
13371       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13372         break;
13373       /* Consume the `,' token.  */
13374       cp_lexer_consume_token (parser->lexer);
13375     }
13376
13377   return clobbers;
13378 }
13379
13380 /* Parse an (optional) series of attributes.
13381
13382    attributes:
13383      attributes attribute
13384
13385    attribute:
13386      __attribute__ (( attribute-list [opt] ))
13387
13388    The return value is as for cp_parser_attribute_list.  */
13389
13390 static tree
13391 cp_parser_attributes_opt (cp_parser* parser)
13392 {
13393   tree attributes = NULL_TREE;
13394
13395   while (true)
13396     {
13397       cp_token *token;
13398       tree attribute_list;
13399
13400       /* Peek at the next token.  */
13401       token = cp_lexer_peek_token (parser->lexer);
13402       /* If it's not `__attribute__', then we're done.  */
13403       if (token->keyword != RID_ATTRIBUTE)
13404         break;
13405
13406       /* Consume the `__attribute__' keyword.  */
13407       cp_lexer_consume_token (parser->lexer);
13408       /* Look for the two `(' tokens.  */
13409       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13410       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13411
13412       /* Peek at the next token.  */
13413       token = cp_lexer_peek_token (parser->lexer);
13414       if (token->type != CPP_CLOSE_PAREN)
13415         /* Parse the attribute-list.  */
13416         attribute_list = cp_parser_attribute_list (parser);
13417       else
13418         /* If the next token is a `)', then there is no attribute
13419            list.  */
13420         attribute_list = NULL;
13421
13422       /* Look for the two `)' tokens.  */
13423       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13424       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13425
13426       /* Add these new attributes to the list.  */
13427       attributes = chainon (attributes, attribute_list);
13428     }
13429
13430   return attributes;
13431 }
13432
13433 /* Parse an attribute-list.
13434
13435    attribute-list:
13436      attribute
13437      attribute-list , attribute
13438
13439    attribute:
13440      identifier
13441      identifier ( identifier )
13442      identifier ( identifier , expression-list )
13443      identifier ( expression-list )
13444
13445    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13446    TREE_PURPOSE of each node is the identifier indicating which
13447    attribute is in use.  The TREE_VALUE represents the arguments, if
13448    any.  */
13449
13450 static tree
13451 cp_parser_attribute_list (cp_parser* parser)
13452 {
13453   tree attribute_list = NULL_TREE;
13454
13455   c_lex_string_translate = false;
13456   while (true)
13457     {
13458       cp_token *token;
13459       tree identifier;
13460       tree attribute;
13461
13462       /* Look for the identifier.  We also allow keywords here; for
13463          example `__attribute__ ((const))' is legal.  */
13464       token = cp_lexer_peek_token (parser->lexer);
13465       if (token->type != CPP_NAME
13466           && token->type != CPP_KEYWORD)
13467         return error_mark_node;
13468       /* Consume the token.  */
13469       token = cp_lexer_consume_token (parser->lexer);
13470
13471       /* Save away the identifier that indicates which attribute this is.  */
13472       identifier = token->value;
13473       attribute = build_tree_list (identifier, NULL_TREE);
13474
13475       /* Peek at the next token.  */
13476       token = cp_lexer_peek_token (parser->lexer);
13477       /* If it's an `(', then parse the attribute arguments.  */
13478       if (token->type == CPP_OPEN_PAREN)
13479         {
13480           tree arguments;
13481
13482           arguments = (cp_parser_parenthesized_expression_list
13483                        (parser, true, /*non_constant_p=*/NULL));
13484           /* Save the identifier and arguments away.  */
13485           TREE_VALUE (attribute) = arguments;
13486         }
13487
13488       /* Add this attribute to the list.  */
13489       TREE_CHAIN (attribute) = attribute_list;
13490       attribute_list = attribute;
13491
13492       /* Now, look for more attributes.  */
13493       token = cp_lexer_peek_token (parser->lexer);
13494       /* If the next token isn't a `,', we're done.  */
13495       if (token->type != CPP_COMMA)
13496         break;
13497
13498       /* Consume the comma and keep going.  */
13499       cp_lexer_consume_token (parser->lexer);
13500     }
13501   c_lex_string_translate = true;
13502
13503   /* We built up the list in reverse order.  */
13504   return nreverse (attribute_list);
13505 }
13506
13507 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13508    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13509    current value of the PEDANTIC flag, regardless of whether or not
13510    the `__extension__' keyword is present.  The caller is responsible
13511    for restoring the value of the PEDANTIC flag.  */
13512
13513 static bool
13514 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13515 {
13516   /* Save the old value of the PEDANTIC flag.  */
13517   *saved_pedantic = pedantic;
13518
13519   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13520     {
13521       /* Consume the `__extension__' token.  */
13522       cp_lexer_consume_token (parser->lexer);
13523       /* We're not being pedantic while the `__extension__' keyword is
13524          in effect.  */
13525       pedantic = 0;
13526
13527       return true;
13528     }
13529
13530   return false;
13531 }
13532
13533 /* Parse a label declaration.
13534
13535    label-declaration:
13536      __label__ label-declarator-seq ;
13537
13538    label-declarator-seq:
13539      identifier , label-declarator-seq
13540      identifier  */
13541
13542 static void
13543 cp_parser_label_declaration (cp_parser* parser)
13544 {
13545   /* Look for the `__label__' keyword.  */
13546   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
13547
13548   while (true)
13549     {
13550       tree identifier;
13551
13552       /* Look for an identifier.  */
13553       identifier = cp_parser_identifier (parser);
13554       /* Declare it as a lobel.  */
13555       finish_label_decl (identifier);
13556       /* If the next token is a `;', stop.  */
13557       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13558         break;
13559       /* Look for the `,' separating the label declarations.  */
13560       cp_parser_require (parser, CPP_COMMA, "`,'");
13561     }
13562
13563   /* Look for the final `;'.  */
13564   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13565 }
13566
13567 /* Support Functions */
13568
13569 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
13570    NAME should have one of the representations used for an
13571    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
13572    is returned.  If PARSER->SCOPE is a dependent type, then a
13573    SCOPE_REF is returned.
13574
13575    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
13576    returned; the name was already resolved when the TEMPLATE_ID_EXPR
13577    was formed.  Abstractly, such entities should not be passed to this
13578    function, because they do not need to be looked up, but it is
13579    simpler to check for this special case here, rather than at the
13580    call-sites.
13581
13582    In cases not explicitly covered above, this function returns a
13583    DECL, OVERLOAD, or baselink representing the result of the lookup.
13584    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
13585    is returned.
13586
13587    If IS_TYPE is TRUE, bindings that do not refer to types are
13588    ignored.
13589
13590    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
13591    ignored.
13592
13593    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
13594    are ignored.
13595
13596    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
13597    types.  */
13598
13599 static tree
13600 cp_parser_lookup_name (cp_parser *parser, tree name,
13601                        bool is_type, bool is_template, bool is_namespace,
13602                        bool check_dependency)
13603 {
13604   tree decl;
13605   tree object_type = parser->context->object_type;
13606
13607   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
13608      no longer valid.  Note that if we are parsing tentatively, and
13609      the parse fails, OBJECT_TYPE will be automatically restored.  */
13610   parser->context->object_type = NULL_TREE;
13611
13612   if (name == error_mark_node)
13613     return error_mark_node;
13614
13615   /* A template-id has already been resolved; there is no lookup to
13616      do.  */
13617   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
13618     return name;
13619   if (BASELINK_P (name))
13620     {
13621       my_friendly_assert ((TREE_CODE (BASELINK_FUNCTIONS (name))
13622                            == TEMPLATE_ID_EXPR),
13623                           20020909);
13624       return name;
13625     }
13626
13627   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
13628      it should already have been checked to make sure that the name
13629      used matches the type being destroyed.  */
13630   if (TREE_CODE (name) == BIT_NOT_EXPR)
13631     {
13632       tree type;
13633
13634       /* Figure out to which type this destructor applies.  */
13635       if (parser->scope)
13636         type = parser->scope;
13637       else if (object_type)
13638         type = object_type;
13639       else
13640         type = current_class_type;
13641       /* If that's not a class type, there is no destructor.  */
13642       if (!type || !CLASS_TYPE_P (type))
13643         return error_mark_node;
13644       if (!CLASSTYPE_DESTRUCTORS (type))
13645           return error_mark_node;
13646       /* If it was a class type, return the destructor.  */
13647       return CLASSTYPE_DESTRUCTORS (type);
13648     }
13649
13650   /* By this point, the NAME should be an ordinary identifier.  If
13651      the id-expression was a qualified name, the qualifying scope is
13652      stored in PARSER->SCOPE at this point.  */
13653   my_friendly_assert (TREE_CODE (name) == IDENTIFIER_NODE,
13654                       20000619);
13655
13656   /* Perform the lookup.  */
13657   if (parser->scope)
13658     {
13659       bool dependent_p;
13660
13661       if (parser->scope == error_mark_node)
13662         return error_mark_node;
13663
13664       /* If the SCOPE is dependent, the lookup must be deferred until
13665          the template is instantiated -- unless we are explicitly
13666          looking up names in uninstantiated templates.  Even then, we
13667          cannot look up the name if the scope is not a class type; it
13668          might, for example, be a template type parameter.  */
13669       dependent_p = (TYPE_P (parser->scope)
13670                      && !(parser->in_declarator_p
13671                           && currently_open_class (parser->scope))
13672                      && dependent_type_p (parser->scope));
13673       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
13674            && dependent_p)
13675         {
13676           if (is_type)
13677             /* The resolution to Core Issue 180 says that `struct A::B'
13678                should be considered a type-name, even if `A' is
13679                dependent.  */
13680             decl = TYPE_NAME (make_typename_type (parser->scope,
13681                                                   name,
13682                                                   /*complain=*/1));
13683           else if (is_template)
13684             decl = make_unbound_class_template (parser->scope,
13685                                                 name,
13686                                                 /*complain=*/1);
13687           else
13688             decl = build_nt (SCOPE_REF, parser->scope, name);
13689         }
13690       else
13691         {
13692           bool pop_p = false;
13693
13694           /* If PARSER->SCOPE is a dependent type, then it must be a
13695              class type, and we must not be checking dependencies;
13696              otherwise, we would have processed this lookup above.  So
13697              that PARSER->SCOPE is not considered a dependent base by
13698              lookup_member, we must enter the scope here.  */
13699           if (dependent_p)
13700             pop_p = push_scope (parser->scope);
13701           /* If the PARSER->SCOPE is a a template specialization, it
13702              may be instantiated during name lookup.  In that case,
13703              errors may be issued.  Even if we rollback the current
13704              tentative parse, those errors are valid.  */
13705           decl = lookup_qualified_name (parser->scope, name, is_type,
13706                                         /*complain=*/true);
13707           if (pop_p)
13708             pop_scope (parser->scope);
13709         }
13710       parser->qualifying_scope = parser->scope;
13711       parser->object_scope = NULL_TREE;
13712     }
13713   else if (object_type)
13714     {
13715       tree object_decl = NULL_TREE;
13716       /* Look up the name in the scope of the OBJECT_TYPE, unless the
13717          OBJECT_TYPE is not a class.  */
13718       if (CLASS_TYPE_P (object_type))
13719         /* If the OBJECT_TYPE is a template specialization, it may
13720            be instantiated during name lookup.  In that case, errors
13721            may be issued.  Even if we rollback the current tentative
13722            parse, those errors are valid.  */
13723         object_decl = lookup_member (object_type,
13724                                      name,
13725                                      /*protect=*/0, is_type);
13726       /* Look it up in the enclosing context, too.  */
13727       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13728                                is_namespace,
13729                                /*flags=*/0);
13730       parser->object_scope = object_type;
13731       parser->qualifying_scope = NULL_TREE;
13732       if (object_decl)
13733         decl = object_decl;
13734     }
13735   else
13736     {
13737       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
13738                                is_namespace,
13739                                /*flags=*/0);
13740       parser->qualifying_scope = NULL_TREE;
13741       parser->object_scope = NULL_TREE;
13742     }
13743
13744   /* If the lookup failed, let our caller know.  */
13745   if (!decl
13746       || decl == error_mark_node
13747       || (TREE_CODE (decl) == FUNCTION_DECL
13748           && DECL_ANTICIPATED (decl)))
13749     return error_mark_node;
13750
13751   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
13752   if (TREE_CODE (decl) == TREE_LIST)
13753     {
13754       /* The error message we have to print is too complicated for
13755          cp_parser_error, so we incorporate its actions directly.  */
13756       if (!cp_parser_simulate_error (parser))
13757         {
13758           error ("reference to `%D' is ambiguous", name);
13759           print_candidates (decl);
13760         }
13761       return error_mark_node;
13762     }
13763
13764   my_friendly_assert (DECL_P (decl)
13765                       || TREE_CODE (decl) == OVERLOAD
13766                       || TREE_CODE (decl) == SCOPE_REF
13767                       || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
13768                       || BASELINK_P (decl),
13769                       20000619);
13770
13771   /* If we have resolved the name of a member declaration, check to
13772      see if the declaration is accessible.  When the name resolves to
13773      set of overloaded functions, accessibility is checked when
13774      overload resolution is done.
13775
13776      During an explicit instantiation, access is not checked at all,
13777      as per [temp.explicit].  */
13778   if (DECL_P (decl))
13779     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
13780
13781   return decl;
13782 }
13783
13784 /* Like cp_parser_lookup_name, but for use in the typical case where
13785    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
13786    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
13787
13788 static tree
13789 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
13790 {
13791   return cp_parser_lookup_name (parser, name,
13792                                 /*is_type=*/false,
13793                                 /*is_template=*/false,
13794                                 /*is_namespace=*/false,
13795                                 /*check_dependency=*/true);
13796 }
13797
13798 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
13799    the current context, return the TYPE_DECL.  If TAG_NAME_P is
13800    true, the DECL indicates the class being defined in a class-head,
13801    or declared in an elaborated-type-specifier.
13802
13803    Otherwise, return DECL.  */
13804
13805 static tree
13806 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
13807 {
13808   /* If the TEMPLATE_DECL is being declared as part of a class-head,
13809      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
13810
13811        struct A {
13812          template <typename T> struct B;
13813        };
13814
13815        template <typename T> struct A::B {};
13816
13817      Similarly, in a elaborated-type-specifier:
13818
13819        namespace N { struct X{}; }
13820
13821        struct A {
13822          template <typename T> friend struct N::X;
13823        };
13824
13825      However, if the DECL refers to a class type, and we are in
13826      the scope of the class, then the name lookup automatically
13827      finds the TYPE_DECL created by build_self_reference rather
13828      than a TEMPLATE_DECL.  For example, in:
13829
13830        template <class T> struct S {
13831          S s;
13832        };
13833
13834      there is no need to handle such case.  */
13835
13836   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
13837     return DECL_TEMPLATE_RESULT (decl);
13838
13839   return decl;
13840 }
13841
13842 /* If too many, or too few, template-parameter lists apply to the
13843    declarator, issue an error message.  Returns TRUE if all went well,
13844    and FALSE otherwise.  */
13845
13846 static bool
13847 cp_parser_check_declarator_template_parameters (cp_parser* parser,
13848                                                 tree declarator)
13849 {
13850   unsigned num_templates;
13851
13852   /* We haven't seen any classes that involve template parameters yet.  */
13853   num_templates = 0;
13854
13855   switch (TREE_CODE (declarator))
13856     {
13857     case CALL_EXPR:
13858     case ARRAY_REF:
13859     case INDIRECT_REF:
13860     case ADDR_EXPR:
13861       {
13862         tree main_declarator = TREE_OPERAND (declarator, 0);
13863         return
13864           cp_parser_check_declarator_template_parameters (parser,
13865                                                           main_declarator);
13866       }
13867
13868     case SCOPE_REF:
13869       {
13870         tree scope;
13871         tree member;
13872
13873         scope = TREE_OPERAND (declarator, 0);
13874         member = TREE_OPERAND (declarator, 1);
13875
13876         /* If this is a pointer-to-member, then we are not interested
13877            in the SCOPE, because it does not qualify the thing that is
13878            being declared.  */
13879         if (TREE_CODE (member) == INDIRECT_REF)
13880           return (cp_parser_check_declarator_template_parameters
13881                   (parser, member));
13882
13883         while (scope && CLASS_TYPE_P (scope))
13884           {
13885             /* You're supposed to have one `template <...>'
13886                for every template class, but you don't need one
13887                for a full specialization.  For example:
13888
13889                template <class T> struct S{};
13890                template <> struct S<int> { void f(); };
13891                void S<int>::f () {}
13892
13893                is correct; there shouldn't be a `template <>' for
13894                the definition of `S<int>::f'.  */
13895             if (CLASSTYPE_TEMPLATE_INFO (scope)
13896                 && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
13897                     || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
13898                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
13899               ++num_templates;
13900
13901             scope = TYPE_CONTEXT (scope);
13902           }
13903       }
13904
13905       /* Fall through.  */
13906
13907     default:
13908       /* If the DECLARATOR has the form `X<y>' then it uses one
13909          additional level of template parameters.  */
13910       if (TREE_CODE (declarator) == TEMPLATE_ID_EXPR)
13911         ++num_templates;
13912
13913       return cp_parser_check_template_parameters (parser,
13914                                                   num_templates);
13915     }
13916 }
13917
13918 /* NUM_TEMPLATES were used in the current declaration.  If that is
13919    invalid, return FALSE and issue an error messages.  Otherwise,
13920    return TRUE.  */
13921
13922 static bool
13923 cp_parser_check_template_parameters (cp_parser* parser,
13924                                      unsigned num_templates)
13925 {
13926   /* If there are more template classes than parameter lists, we have
13927      something like:
13928
13929        template <class T> void S<T>::R<T>::f ();  */
13930   if (parser->num_template_parameter_lists < num_templates)
13931     {
13932       error ("too few template-parameter-lists");
13933       return false;
13934     }
13935   /* If there are the same number of template classes and parameter
13936      lists, that's OK.  */
13937   if (parser->num_template_parameter_lists == num_templates)
13938     return true;
13939   /* If there are more, but only one more, then we are referring to a
13940      member template.  That's OK too.  */
13941   if (parser->num_template_parameter_lists == num_templates + 1)
13942       return true;
13943   /* Otherwise, there are too many template parameter lists.  We have
13944      something like:
13945
13946      template <class T> template <class U> void S::f();  */
13947   error ("too many template-parameter-lists");
13948   return false;
13949 }
13950
13951 /* Parse a binary-expression of the general form:
13952
13953    binary-expression:
13954      <expr>
13955      binary-expression <token> <expr>
13956
13957    The TOKEN_TREE_MAP maps <token> types to <expr> codes.  FN is used
13958    to parser the <expr>s.  If the first production is used, then the
13959    value returned by FN is returned directly.  Otherwise, a node with
13960    the indicated EXPR_TYPE is returned, with operands corresponding to
13961    the two sub-expressions.  */
13962
13963 static tree
13964 cp_parser_binary_expression (cp_parser* parser,
13965                              const cp_parser_token_tree_map token_tree_map,
13966                              cp_parser_expression_fn fn)
13967 {
13968   tree lhs;
13969
13970   /* Parse the first expression.  */
13971   lhs = (*fn) (parser);
13972   /* Now, look for more expressions.  */
13973   while (true)
13974     {
13975       cp_token *token;
13976       const cp_parser_token_tree_map_node *map_node;
13977       tree rhs;
13978
13979       /* Peek at the next token.  */
13980       token = cp_lexer_peek_token (parser->lexer);
13981       /* If the token is `>', and that's not an operator at the
13982          moment, then we're done.  */
13983       if (token->type == CPP_GREATER
13984           && !parser->greater_than_is_operator_p)
13985         break;
13986       /* If we find one of the tokens we want, build the corresponding
13987          tree representation.  */
13988       for (map_node = token_tree_map;
13989            map_node->token_type != CPP_EOF;
13990            ++map_node)
13991         if (map_node->token_type == token->type)
13992           {
13993             /* Consume the operator token.  */
13994             cp_lexer_consume_token (parser->lexer);
13995             /* Parse the right-hand side of the expression.  */
13996             rhs = (*fn) (parser);
13997             /* Build the binary tree node.  */
13998             lhs = build_x_binary_op (map_node->tree_type, lhs, rhs);
13999             break;
14000           }
14001
14002       /* If the token wasn't one of the ones we want, we're done.  */
14003       if (map_node->token_type == CPP_EOF)
14004         break;
14005     }
14006
14007   return lhs;
14008 }
14009
14010 /* Parse an optional `::' token indicating that the following name is
14011    from the global namespace.  If so, PARSER->SCOPE is set to the
14012    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14013    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14014    Returns the new value of PARSER->SCOPE, if the `::' token is
14015    present, and NULL_TREE otherwise.  */
14016
14017 static tree
14018 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14019 {
14020   cp_token *token;
14021
14022   /* Peek at the next token.  */
14023   token = cp_lexer_peek_token (parser->lexer);
14024   /* If we're looking at a `::' token then we're starting from the
14025      global namespace, not our current location.  */
14026   if (token->type == CPP_SCOPE)
14027     {
14028       /* Consume the `::' token.  */
14029       cp_lexer_consume_token (parser->lexer);
14030       /* Set the SCOPE so that we know where to start the lookup.  */
14031       parser->scope = global_namespace;
14032       parser->qualifying_scope = global_namespace;
14033       parser->object_scope = NULL_TREE;
14034
14035       return parser->scope;
14036     }
14037   else if (!current_scope_valid_p)
14038     {
14039       parser->scope = NULL_TREE;
14040       parser->qualifying_scope = NULL_TREE;
14041       parser->object_scope = NULL_TREE;
14042     }
14043
14044   return NULL_TREE;
14045 }
14046
14047 /* Returns TRUE if the upcoming token sequence is the start of a
14048    constructor declarator.  If FRIEND_P is true, the declarator is
14049    preceded by the `friend' specifier.  */
14050
14051 static bool
14052 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14053 {
14054   bool constructor_p;
14055   tree type_decl = NULL_TREE;
14056   bool nested_name_p;
14057   cp_token *next_token;
14058
14059   /* The common case is that this is not a constructor declarator, so
14060      try to avoid doing lots of work if at all possible.  It's not
14061      valid declare a constructor at function scope.  */
14062   if (at_function_scope_p ())
14063     return false;
14064   /* And only certain tokens can begin a constructor declarator.  */
14065   next_token = cp_lexer_peek_token (parser->lexer);
14066   if (next_token->type != CPP_NAME
14067       && next_token->type != CPP_SCOPE
14068       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14069       && next_token->type != CPP_TEMPLATE_ID)
14070     return false;
14071
14072   /* Parse tentatively; we are going to roll back all of the tokens
14073      consumed here.  */
14074   cp_parser_parse_tentatively (parser);
14075   /* Assume that we are looking at a constructor declarator.  */
14076   constructor_p = true;
14077
14078   /* Look for the optional `::' operator.  */
14079   cp_parser_global_scope_opt (parser,
14080                               /*current_scope_valid_p=*/false);
14081   /* Look for the nested-name-specifier.  */
14082   nested_name_p
14083     = (cp_parser_nested_name_specifier_opt (parser,
14084                                             /*typename_keyword_p=*/false,
14085                                             /*check_dependency_p=*/false,
14086                                             /*type_p=*/false,
14087                                             /*is_declaration=*/false)
14088        != NULL_TREE);
14089   /* Outside of a class-specifier, there must be a
14090      nested-name-specifier.  */
14091   if (!nested_name_p &&
14092       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14093        || friend_p))
14094     constructor_p = false;
14095   /* If we still think that this might be a constructor-declarator,
14096      look for a class-name.  */
14097   if (constructor_p)
14098     {
14099       /* If we have:
14100
14101            template <typename T> struct S { S(); };
14102            template <typename T> S<T>::S ();
14103
14104          we must recognize that the nested `S' names a class.
14105          Similarly, for:
14106
14107            template <typename T> S<T>::S<T> ();
14108
14109          we must recognize that the nested `S' names a template.  */
14110       type_decl = cp_parser_class_name (parser,
14111                                         /*typename_keyword_p=*/false,
14112                                         /*template_keyword_p=*/false,
14113                                         /*type_p=*/false,
14114                                         /*check_dependency_p=*/false,
14115                                         /*class_head_p=*/false,
14116                                         /*is_declaration=*/false);
14117       /* If there was no class-name, then this is not a constructor.  */
14118       constructor_p = !cp_parser_error_occurred (parser);
14119     }
14120
14121   /* If we're still considering a constructor, we have to see a `(',
14122      to begin the parameter-declaration-clause, followed by either a
14123      `)', an `...', or a decl-specifier.  We need to check for a
14124      type-specifier to avoid being fooled into thinking that:
14125
14126        S::S (f) (int);
14127
14128      is a constructor.  (It is actually a function named `f' that
14129      takes one parameter (of type `int') and returns a value of type
14130      `S::S'.  */
14131   if (constructor_p
14132       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14133     {
14134       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14135           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14136           && !cp_parser_storage_class_specifier_opt (parser))
14137         {
14138           tree type;
14139           bool pop_p = false;
14140           unsigned saved_num_template_parameter_lists;
14141
14142           /* Names appearing in the type-specifier should be looked up
14143              in the scope of the class.  */
14144           if (current_class_type)
14145             type = NULL_TREE;
14146           else
14147             {
14148               type = TREE_TYPE (type_decl);
14149               if (TREE_CODE (type) == TYPENAME_TYPE)
14150                 {
14151                   type = resolve_typename_type (type,
14152                                                 /*only_current_p=*/false);
14153                   if (type == error_mark_node)
14154                     {
14155                       cp_parser_abort_tentative_parse (parser);
14156                       return false;
14157                     }
14158                 }
14159               pop_p = push_scope (type);
14160             }
14161
14162           /* Inside the constructor parameter list, surrounding
14163              template-parameter-lists do not apply.  */
14164           saved_num_template_parameter_lists
14165             = parser->num_template_parameter_lists;
14166           parser->num_template_parameter_lists = 0;
14167
14168           /* Look for the type-specifier.  */
14169           cp_parser_type_specifier (parser,
14170                                     CP_PARSER_FLAGS_NONE,
14171                                     /*is_friend=*/false,
14172                                     /*is_declarator=*/true,
14173                                     /*declares_class_or_enum=*/NULL,
14174                                     /*is_cv_qualifier=*/NULL);
14175
14176           parser->num_template_parameter_lists
14177             = saved_num_template_parameter_lists;
14178
14179           /* Leave the scope of the class.  */
14180           if (pop_p)
14181             pop_scope (type);
14182
14183           constructor_p = !cp_parser_error_occurred (parser);
14184         }
14185     }
14186   else
14187     constructor_p = false;
14188   /* We did not really want to consume any tokens.  */
14189   cp_parser_abort_tentative_parse (parser);
14190
14191   return constructor_p;
14192 }
14193
14194 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14195    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14196    they must be performed once we are in the scope of the function.
14197
14198    Returns the function defined.  */
14199
14200 static tree
14201 cp_parser_function_definition_from_specifiers_and_declarator
14202   (cp_parser* parser,
14203    tree decl_specifiers,
14204    tree attributes,
14205    tree declarator)
14206 {
14207   tree fn;
14208   bool success_p;
14209
14210   /* Begin the function-definition.  */
14211   success_p = begin_function_definition (decl_specifiers,
14212                                          attributes,
14213                                          declarator);
14214
14215   /* If there were names looked up in the decl-specifier-seq that we
14216      did not check, check them now.  We must wait until we are in the
14217      scope of the function to perform the checks, since the function
14218      might be a friend.  */
14219   perform_deferred_access_checks ();
14220
14221   if (!success_p)
14222     {
14223       /* If begin_function_definition didn't like the definition, skip
14224          the entire function.  */
14225       error ("invalid function declaration");
14226       cp_parser_skip_to_end_of_block_or_statement (parser);
14227       fn = error_mark_node;
14228     }
14229   else
14230     fn = cp_parser_function_definition_after_declarator (parser,
14231                                                          /*inline_p=*/false);
14232
14233   return fn;
14234 }
14235
14236 /* Parse the part of a function-definition that follows the
14237    declarator.  INLINE_P is TRUE iff this function is an inline
14238    function defined with a class-specifier.
14239
14240    Returns the function defined.  */
14241
14242 static tree
14243 cp_parser_function_definition_after_declarator (cp_parser* parser,
14244                                                 bool inline_p)
14245 {
14246   tree fn;
14247   bool ctor_initializer_p = false;
14248   bool saved_in_unbraced_linkage_specification_p;
14249   unsigned saved_num_template_parameter_lists;
14250
14251   /* If the next token is `return', then the code may be trying to
14252      make use of the "named return value" extension that G++ used to
14253      support.  */
14254   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14255     {
14256       /* Consume the `return' keyword.  */
14257       cp_lexer_consume_token (parser->lexer);
14258       /* Look for the identifier that indicates what value is to be
14259          returned.  */
14260       cp_parser_identifier (parser);
14261       /* Issue an error message.  */
14262       error ("named return values are no longer supported");
14263       /* Skip tokens until we reach the start of the function body.  */
14264       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14265              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14266         cp_lexer_consume_token (parser->lexer);
14267     }
14268   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14269      anything declared inside `f'.  */
14270   saved_in_unbraced_linkage_specification_p
14271     = parser->in_unbraced_linkage_specification_p;
14272   parser->in_unbraced_linkage_specification_p = false;
14273   /* Inside the function, surrounding template-parameter-lists do not
14274      apply.  */
14275   saved_num_template_parameter_lists
14276     = parser->num_template_parameter_lists;
14277   parser->num_template_parameter_lists = 0;
14278   /* If the next token is `try', then we are looking at a
14279      function-try-block.  */
14280   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14281     ctor_initializer_p = cp_parser_function_try_block (parser);
14282   /* A function-try-block includes the function-body, so we only do
14283      this next part if we're not processing a function-try-block.  */
14284   else
14285     ctor_initializer_p
14286       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14287
14288   /* Finish the function.  */
14289   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14290                         (inline_p ? 2 : 0));
14291   /* Generate code for it, if necessary.  */
14292   expand_or_defer_fn (fn);
14293   /* Restore the saved values.  */
14294   parser->in_unbraced_linkage_specification_p
14295     = saved_in_unbraced_linkage_specification_p;
14296   parser->num_template_parameter_lists
14297     = saved_num_template_parameter_lists;
14298
14299   return fn;
14300 }
14301
14302 /* Parse a template-declaration, assuming that the `export' (and
14303    `extern') keywords, if present, has already been scanned.  MEMBER_P
14304    is as for cp_parser_template_declaration.  */
14305
14306 static void
14307 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14308 {
14309   tree decl = NULL_TREE;
14310   tree parameter_list;
14311   bool friend_p = false;
14312
14313   /* Look for the `template' keyword.  */
14314   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14315     return;
14316
14317   /* And the `<'.  */
14318   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14319     return;
14320
14321   /* If the next token is `>', then we have an invalid
14322      specialization.  Rather than complain about an invalid template
14323      parameter, issue an error message here.  */
14324   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14325     {
14326       cp_parser_error (parser, "invalid explicit specialization");
14327       begin_specialization ();
14328       parameter_list = NULL_TREE;
14329     }
14330   else
14331     {
14332       /* Parse the template parameters.  */
14333       begin_template_parm_list ();
14334       parameter_list = cp_parser_template_parameter_list (parser);
14335       parameter_list = end_template_parm_list (parameter_list);
14336     }
14337
14338   /* Look for the `>'.  */
14339   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14340   /* We just processed one more parameter list.  */
14341   ++parser->num_template_parameter_lists;
14342   /* If the next token is `template', there are more template
14343      parameters.  */
14344   if (cp_lexer_next_token_is_keyword (parser->lexer,
14345                                       RID_TEMPLATE))
14346     cp_parser_template_declaration_after_export (parser, member_p);
14347   else
14348     {
14349       decl = cp_parser_single_declaration (parser,
14350                                            member_p,
14351                                            &friend_p);
14352
14353       /* If this is a member template declaration, let the front
14354          end know.  */
14355       if (member_p && !friend_p && decl)
14356         {
14357           if (TREE_CODE (decl) == TYPE_DECL)
14358             cp_parser_check_access_in_redeclaration (decl);
14359
14360           decl = finish_member_template_decl (decl);
14361         }
14362       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14363         make_friend_class (current_class_type, TREE_TYPE (decl),
14364                            /*complain=*/true);
14365     }
14366   /* We are done with the current parameter list.  */
14367   --parser->num_template_parameter_lists;
14368
14369   /* Finish up.  */
14370   finish_template_decl (parameter_list);
14371
14372   /* Register member declarations.  */
14373   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14374     finish_member_declaration (decl);
14375
14376   /* If DECL is a function template, we must return to parse it later.
14377      (Even though there is no definition, there might be default
14378      arguments that need handling.)  */
14379   if (member_p && decl
14380       && (TREE_CODE (decl) == FUNCTION_DECL
14381           || DECL_FUNCTION_TEMPLATE_P (decl)))
14382     TREE_VALUE (parser->unparsed_functions_queues)
14383       = tree_cons (NULL_TREE, decl,
14384                    TREE_VALUE (parser->unparsed_functions_queues));
14385 }
14386
14387 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14388    `function-definition' sequence.  MEMBER_P is true, this declaration
14389    appears in a class scope.
14390
14391    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14392    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14393
14394 static tree
14395 cp_parser_single_declaration (cp_parser* parser,
14396                               bool member_p,
14397                               bool* friend_p)
14398 {
14399   int declares_class_or_enum;
14400   tree decl = NULL_TREE;
14401   tree decl_specifiers;
14402   tree attributes;
14403   bool function_definition_p = false;
14404
14405   /* Defer access checks until we know what is being declared.  */
14406   push_deferring_access_checks (dk_deferred);
14407
14408   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14409      alternative.  */
14410   decl_specifiers
14411     = cp_parser_decl_specifier_seq (parser,
14412                                     CP_PARSER_FLAGS_OPTIONAL,
14413                                     &attributes,
14414                                     &declares_class_or_enum);
14415   if (friend_p)
14416     *friend_p = cp_parser_friend_p (decl_specifiers);
14417   /* Gather up the access checks that occurred the
14418      decl-specifier-seq.  */
14419   stop_deferring_access_checks ();
14420
14421   /* Check for the declaration of a template class.  */
14422   if (declares_class_or_enum)
14423     {
14424       if (cp_parser_declares_only_class_p (parser))
14425         {
14426           decl = shadow_tag (decl_specifiers);
14427           if (decl)
14428             decl = TYPE_NAME (decl);
14429           else
14430             decl = error_mark_node;
14431         }
14432     }
14433   else
14434     decl = NULL_TREE;
14435   /* If it's not a template class, try for a template function.  If
14436      the next token is a `;', then this declaration does not declare
14437      anything.  But, if there were errors in the decl-specifiers, then
14438      the error might well have come from an attempted class-specifier.
14439      In that case, there's no need to warn about a missing declarator.  */
14440   if (!decl
14441       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14442           || !value_member (error_mark_node, decl_specifiers)))
14443     decl = cp_parser_init_declarator (parser,
14444                                       decl_specifiers,
14445                                       attributes,
14446                                       /*function_definition_allowed_p=*/true,
14447                                       member_p,
14448                                       declares_class_or_enum,
14449                                       &function_definition_p);
14450
14451   pop_deferring_access_checks ();
14452
14453   /* Clear any current qualification; whatever comes next is the start
14454      of something new.  */
14455   parser->scope = NULL_TREE;
14456   parser->qualifying_scope = NULL_TREE;
14457   parser->object_scope = NULL_TREE;
14458   /* Look for a trailing `;' after the declaration.  */
14459   if (!function_definition_p
14460       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14461     cp_parser_skip_to_end_of_block_or_statement (parser);
14462
14463   return decl;
14464 }
14465
14466 /* Parse a cast-expression that is not the operand of a unary "&".  */
14467
14468 static tree
14469 cp_parser_simple_cast_expression (cp_parser *parser)
14470 {
14471   return cp_parser_cast_expression (parser, /*address_p=*/false);
14472 }
14473
14474 /* Parse a functional cast to TYPE.  Returns an expression
14475    representing the cast.  */
14476
14477 static tree
14478 cp_parser_functional_cast (cp_parser* parser, tree type)
14479 {
14480   tree expression_list;
14481
14482   expression_list
14483     = cp_parser_parenthesized_expression_list (parser, false,
14484                                                /*non_constant_p=*/NULL);
14485
14486   return build_functional_cast (type, expression_list);
14487 }
14488
14489 /* Save the tokens that make up the body of a member function defined
14490    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14491    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14492    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14493    for the member function.  */
14494
14495 static tree
14496 cp_parser_save_member_function_body (cp_parser* parser,
14497                                      tree decl_specifiers,
14498                                      tree declarator,
14499                                      tree attributes)
14500 {
14501   cp_token_cache *cache;
14502   tree fn;
14503
14504   /* Create the function-declaration.  */
14505   fn = start_method (decl_specifiers, declarator, attributes);
14506   /* If something went badly wrong, bail out now.  */
14507   if (fn == error_mark_node)
14508     {
14509       /* If there's a function-body, skip it.  */
14510       if (cp_parser_token_starts_function_definition_p
14511           (cp_lexer_peek_token (parser->lexer)))
14512         cp_parser_skip_to_end_of_block_or_statement (parser);
14513       return error_mark_node;
14514     }
14515
14516   /* Remember it, if there default args to post process.  */
14517   cp_parser_save_default_args (parser, fn);
14518
14519   /* Create a token cache.  */
14520   cache = cp_token_cache_new ();
14521   /* Save away the tokens that make up the body of the
14522      function.  */
14523   cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14524   /* Handle function try blocks.  */
14525   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14526     cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, /*depth=*/0);
14527
14528   /* Save away the inline definition; we will process it when the
14529      class is complete.  */
14530   DECL_PENDING_INLINE_INFO (fn) = cache;
14531   DECL_PENDING_INLINE_P (fn) = 1;
14532
14533   /* We need to know that this was defined in the class, so that
14534      friend templates are handled correctly.  */
14535   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14536
14537   /* We're done with the inline definition.  */
14538   finish_method (fn);
14539
14540   /* Add FN to the queue of functions to be parsed later.  */
14541   TREE_VALUE (parser->unparsed_functions_queues)
14542     = tree_cons (NULL_TREE, fn,
14543                  TREE_VALUE (parser->unparsed_functions_queues));
14544
14545   return fn;
14546 }
14547
14548 /* Parse a template-argument-list, as well as the trailing ">" (but
14549    not the opening ">").  See cp_parser_template_argument_list for the
14550    return value.  */
14551
14552 static tree
14553 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14554 {
14555   tree arguments;
14556   tree saved_scope;
14557   tree saved_qualifying_scope;
14558   tree saved_object_scope;
14559   bool saved_greater_than_is_operator_p;
14560
14561   /* [temp.names]
14562
14563      When parsing a template-id, the first non-nested `>' is taken as
14564      the end of the template-argument-list rather than a greater-than
14565      operator.  */
14566   saved_greater_than_is_operator_p
14567     = parser->greater_than_is_operator_p;
14568   parser->greater_than_is_operator_p = false;
14569   /* Parsing the argument list may modify SCOPE, so we save it
14570      here.  */
14571   saved_scope = parser->scope;
14572   saved_qualifying_scope = parser->qualifying_scope;
14573   saved_object_scope = parser->object_scope;
14574   /* Parse the template-argument-list itself.  */
14575   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14576     arguments = NULL_TREE;
14577   else
14578     arguments = cp_parser_template_argument_list (parser);
14579   /* Look for the `>' that ends the template-argument-list. If we find
14580      a '>>' instead, it's probably just a typo.  */
14581   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
14582     {
14583       if (!saved_greater_than_is_operator_p)
14584         {
14585           /* If we're in a nested template argument list, the '>>' has to be
14586             a typo for '> >'. We emit the error message, but we continue
14587             parsing and we push a '>' as next token, so that the argument
14588             list will be parsed correctly..  */
14589           cp_token* token;
14590           error ("`>>' should be `> >' within a nested template argument list");
14591           token = cp_lexer_peek_token (parser->lexer);
14592           token->type = CPP_GREATER;
14593         }
14594       else
14595         {
14596           /* If this is not a nested template argument list, the '>>' is
14597             a typo for '>'. Emit an error message and continue.  */
14598           error ("spurious `>>', use `>' to terminate a template argument list");
14599           cp_lexer_consume_token (parser->lexer);
14600         }
14601     }
14602   else if (!cp_parser_require (parser, CPP_GREATER, "`>'"))
14603     error ("missing `>' to terminate the template argument list");
14604   /* The `>' token might be a greater-than operator again now.  */
14605   parser->greater_than_is_operator_p
14606     = saved_greater_than_is_operator_p;
14607   /* Restore the SAVED_SCOPE.  */
14608   parser->scope = saved_scope;
14609   parser->qualifying_scope = saved_qualifying_scope;
14610   parser->object_scope = saved_object_scope;
14611
14612   return arguments;
14613 }
14614
14615 /* MEMBER_FUNCTION is a member function, or a friend.  If default
14616    arguments, or the body of the function have not yet been parsed,
14617    parse them now.  */
14618
14619 static void
14620 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
14621 {
14622   cp_lexer *saved_lexer;
14623
14624   /* If this member is a template, get the underlying
14625      FUNCTION_DECL.  */
14626   if (DECL_FUNCTION_TEMPLATE_P (member_function))
14627     member_function = DECL_TEMPLATE_RESULT (member_function);
14628
14629   /* There should not be any class definitions in progress at this
14630      point; the bodies of members are only parsed outside of all class
14631      definitions.  */
14632   my_friendly_assert (parser->num_classes_being_defined == 0, 20010816);
14633   /* While we're parsing the member functions we might encounter more
14634      classes.  We want to handle them right away, but we don't want
14635      them getting mixed up with functions that are currently in the
14636      queue.  */
14637   parser->unparsed_functions_queues
14638     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14639
14640   /* Make sure that any template parameters are in scope.  */
14641   maybe_begin_member_template_processing (member_function);
14642
14643   /* If the body of the function has not yet been parsed, parse it
14644      now.  */
14645   if (DECL_PENDING_INLINE_P (member_function))
14646     {
14647       tree function_scope;
14648       cp_token_cache *tokens;
14649
14650       /* The function is no longer pending; we are processing it.  */
14651       tokens = DECL_PENDING_INLINE_INFO (member_function);
14652       DECL_PENDING_INLINE_INFO (member_function) = NULL;
14653       DECL_PENDING_INLINE_P (member_function) = 0;
14654       /* If this was an inline function in a local class, enter the scope
14655          of the containing function.  */
14656       function_scope = decl_function_context (member_function);
14657       if (function_scope)
14658         push_function_context_to (function_scope);
14659
14660       /* Save away the current lexer.  */
14661       saved_lexer = parser->lexer;
14662       /* Make a new lexer to feed us the tokens saved for this function.  */
14663       parser->lexer = cp_lexer_new_from_tokens (tokens);
14664       parser->lexer->next = saved_lexer;
14665
14666       /* Set the current source position to be the location of the first
14667          token in the saved inline body.  */
14668       cp_lexer_peek_token (parser->lexer);
14669
14670       /* Let the front end know that we going to be defining this
14671          function.  */
14672       start_function (NULL_TREE, member_function, NULL_TREE,
14673                       SF_PRE_PARSED | SF_INCLASS_INLINE);
14674
14675       /* Now, parse the body of the function.  */
14676       cp_parser_function_definition_after_declarator (parser,
14677                                                       /*inline_p=*/true);
14678
14679       /* Leave the scope of the containing function.  */
14680       if (function_scope)
14681         pop_function_context_from (function_scope);
14682       /* Restore the lexer.  */
14683       parser->lexer = saved_lexer;
14684     }
14685
14686   /* Remove any template parameters from the symbol table.  */
14687   maybe_end_member_template_processing ();
14688
14689   /* Restore the queue.  */
14690   parser->unparsed_functions_queues
14691     = TREE_CHAIN (parser->unparsed_functions_queues);
14692 }
14693
14694 /* If DECL contains any default args, remember it on the unparsed
14695    functions queue.  */
14696
14697 static void
14698 cp_parser_save_default_args (cp_parser* parser, tree decl)
14699 {
14700   tree probe;
14701
14702   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
14703        probe;
14704        probe = TREE_CHAIN (probe))
14705     if (TREE_PURPOSE (probe))
14706       {
14707         TREE_PURPOSE (parser->unparsed_functions_queues)
14708           = tree_cons (NULL_TREE, decl,
14709                        TREE_PURPOSE (parser->unparsed_functions_queues));
14710         break;
14711       }
14712   return;
14713 }
14714
14715 /* FN is a FUNCTION_DECL which may contains a parameter with an
14716    unparsed DEFAULT_ARG.  Parse the default args now.  */
14717
14718 static void
14719 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
14720 {
14721   cp_lexer *saved_lexer;
14722   cp_token_cache *tokens;
14723   bool saved_local_variables_forbidden_p;
14724   tree parameters;
14725
14726   /* While we're parsing the default args, we might (due to the
14727      statement expression extension) encounter more classes.  We want
14728      to handle them right away, but we don't want them getting mixed
14729      up with default args that are currently in the queue.  */
14730   parser->unparsed_functions_queues
14731     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
14732
14733   for (parameters = TYPE_ARG_TYPES (TREE_TYPE (fn));
14734        parameters;
14735        parameters = TREE_CHAIN (parameters))
14736     {
14737       if (!TREE_PURPOSE (parameters)
14738           || TREE_CODE (TREE_PURPOSE (parameters)) != DEFAULT_ARG)
14739         continue;
14740
14741        /* Save away the current lexer.  */
14742       saved_lexer = parser->lexer;
14743        /* Create a new one, using the tokens we have saved.  */
14744       tokens =  DEFARG_TOKENS (TREE_PURPOSE (parameters));
14745       parser->lexer = cp_lexer_new_from_tokens (tokens);
14746
14747        /* Set the current source position to be the location of the
14748           first token in the default argument.  */
14749       cp_lexer_peek_token (parser->lexer);
14750
14751        /* Local variable names (and the `this' keyword) may not appear
14752           in a default argument.  */
14753       saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
14754       parser->local_variables_forbidden_p = true;
14755        /* Parse the assignment-expression.  */
14756       if (DECL_CLASS_SCOPE_P (fn))
14757         push_nested_class (DECL_CONTEXT (fn));
14758       TREE_PURPOSE (parameters) = cp_parser_assignment_expression (parser);
14759       if (DECL_CLASS_SCOPE_P (fn))
14760         pop_nested_class ();
14761
14762       /* If the token stream has not been completely used up, then
14763          there was extra junk after the end of the default
14764          argument.  */
14765       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
14766         cp_parser_error (parser, "expected `,'");
14767
14768        /* Restore saved state.  */
14769       parser->lexer = saved_lexer;
14770       parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
14771     }
14772
14773   /* Restore the queue.  */
14774   parser->unparsed_functions_queues
14775     = TREE_CHAIN (parser->unparsed_functions_queues);
14776 }
14777
14778 /* Parse the operand of `sizeof' (or a similar operator).  Returns
14779    either a TYPE or an expression, depending on the form of the
14780    input.  The KEYWORD indicates which kind of expression we have
14781    encountered.  */
14782
14783 static tree
14784 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
14785 {
14786   static const char *format;
14787   tree expr = NULL_TREE;
14788   const char *saved_message;
14789   bool saved_integral_constant_expression_p;
14790
14791   /* Initialize FORMAT the first time we get here.  */
14792   if (!format)
14793     format = "types may not be defined in `%s' expressions";
14794
14795   /* Types cannot be defined in a `sizeof' expression.  Save away the
14796      old message.  */
14797   saved_message = parser->type_definition_forbidden_message;
14798   /* And create the new one.  */
14799   parser->type_definition_forbidden_message
14800     = xmalloc (strlen (format)
14801                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
14802                + 1 /* `\0' */);
14803   sprintf ((char *) parser->type_definition_forbidden_message,
14804            format, IDENTIFIER_POINTER (ridpointers[keyword]));
14805
14806   /* The restrictions on constant-expressions do not apply inside
14807      sizeof expressions.  */
14808   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
14809   parser->integral_constant_expression_p = false;
14810
14811   /* Do not actually evaluate the expression.  */
14812   ++skip_evaluation;
14813   /* If it's a `(', then we might be looking at the type-id
14814      construction.  */
14815   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
14816     {
14817       tree type;
14818       bool saved_in_type_id_in_expr_p;
14819
14820       /* We can't be sure yet whether we're looking at a type-id or an
14821          expression.  */
14822       cp_parser_parse_tentatively (parser);
14823       /* Consume the `('.  */
14824       cp_lexer_consume_token (parser->lexer);
14825       /* Parse the type-id.  */
14826       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14827       parser->in_type_id_in_expr_p = true;
14828       type = cp_parser_type_id (parser);
14829       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14830       /* Now, look for the trailing `)'.  */
14831       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14832       /* If all went well, then we're done.  */
14833       if (cp_parser_parse_definitely (parser))
14834         {
14835           /* Build a list of decl-specifiers; right now, we have only
14836              a single type-specifier.  */
14837           type = build_tree_list (NULL_TREE,
14838                                   type);
14839
14840           /* Call grokdeclarator to figure out what type this is.  */
14841           expr = grokdeclarator (NULL_TREE,
14842                                  type,
14843                                  TYPENAME,
14844                                  /*initialized=*/0,
14845                                  /*attrlist=*/NULL);
14846         }
14847     }
14848
14849   /* If the type-id production did not work out, then we must be
14850      looking at the unary-expression production.  */
14851   if (!expr)
14852     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
14853   /* Go back to evaluating expressions.  */
14854   --skip_evaluation;
14855
14856   /* Free the message we created.  */
14857   free ((char *) parser->type_definition_forbidden_message);
14858   /* And restore the old one.  */
14859   parser->type_definition_forbidden_message = saved_message;
14860   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
14861
14862   return expr;
14863 }
14864
14865 /* If the current declaration has no declarator, return true.  */
14866
14867 static bool
14868 cp_parser_declares_only_class_p (cp_parser *parser)
14869 {
14870   /* If the next token is a `;' or a `,' then there is no
14871      declarator.  */
14872   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14873           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
14874 }
14875
14876 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
14877    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
14878
14879 static bool
14880 cp_parser_friend_p (tree decl_specifiers)
14881 {
14882   while (decl_specifiers)
14883     {
14884       /* See if this decl-specifier is `friend'.  */
14885       if (TREE_CODE (TREE_VALUE (decl_specifiers)) == IDENTIFIER_NODE
14886           && C_RID_CODE (TREE_VALUE (decl_specifiers)) == RID_FRIEND)
14887         return true;
14888
14889       /* Go on to the next decl-specifier.  */
14890       decl_specifiers = TREE_CHAIN (decl_specifiers);
14891     }
14892
14893   return false;
14894 }
14895
14896 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
14897    issue an error message indicating that TOKEN_DESC was expected.
14898
14899    Returns the token consumed, if the token had the appropriate type.
14900    Otherwise, returns NULL.  */
14901
14902 static cp_token *
14903 cp_parser_require (cp_parser* parser,
14904                    enum cpp_ttype type,
14905                    const char* token_desc)
14906 {
14907   if (cp_lexer_next_token_is (parser->lexer, type))
14908     return cp_lexer_consume_token (parser->lexer);
14909   else
14910     {
14911       /* Output the MESSAGE -- unless we're parsing tentatively.  */
14912       if (!cp_parser_simulate_error (parser))
14913         {
14914           char *message = concat ("expected ", token_desc, NULL);
14915           cp_parser_error (parser, message);
14916           free (message);
14917         }
14918       return NULL;
14919     }
14920 }
14921
14922 /* Like cp_parser_require, except that tokens will be skipped until
14923    the desired token is found.  An error message is still produced if
14924    the next token is not as expected.  */
14925
14926 static void
14927 cp_parser_skip_until_found (cp_parser* parser,
14928                             enum cpp_ttype type,
14929                             const char* token_desc)
14930 {
14931   cp_token *token;
14932   unsigned nesting_depth = 0;
14933
14934   if (cp_parser_require (parser, type, token_desc))
14935     return;
14936
14937   /* Skip tokens until the desired token is found.  */
14938   while (true)
14939     {
14940       /* Peek at the next token.  */
14941       token = cp_lexer_peek_token (parser->lexer);
14942       /* If we've reached the token we want, consume it and
14943          stop.  */
14944       if (token->type == type && !nesting_depth)
14945         {
14946           cp_lexer_consume_token (parser->lexer);
14947           return;
14948         }
14949       /* If we've run out of tokens, stop.  */
14950       if (token->type == CPP_EOF)
14951         return;
14952       if (token->type == CPP_OPEN_BRACE
14953           || token->type == CPP_OPEN_PAREN
14954           || token->type == CPP_OPEN_SQUARE)
14955         ++nesting_depth;
14956       else if (token->type == CPP_CLOSE_BRACE
14957                || token->type == CPP_CLOSE_PAREN
14958                || token->type == CPP_CLOSE_SQUARE)
14959         {
14960           if (nesting_depth-- == 0)
14961             return;
14962         }
14963       /* Consume this token.  */
14964       cp_lexer_consume_token (parser->lexer);
14965     }
14966 }
14967
14968 /* If the next token is the indicated keyword, consume it.  Otherwise,
14969    issue an error message indicating that TOKEN_DESC was expected.
14970
14971    Returns the token consumed, if the token had the appropriate type.
14972    Otherwise, returns NULL.  */
14973
14974 static cp_token *
14975 cp_parser_require_keyword (cp_parser* parser,
14976                            enum rid keyword,
14977                            const char* token_desc)
14978 {
14979   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
14980
14981   if (token && token->keyword != keyword)
14982     {
14983       dyn_string_t error_msg;
14984
14985       /* Format the error message.  */
14986       error_msg = dyn_string_new (0);
14987       dyn_string_append_cstr (error_msg, "expected ");
14988       dyn_string_append_cstr (error_msg, token_desc);
14989       cp_parser_error (parser, error_msg->s);
14990       dyn_string_delete (error_msg);
14991       return NULL;
14992     }
14993
14994   return token;
14995 }
14996
14997 /* Returns TRUE iff TOKEN is a token that can begin the body of a
14998    function-definition.  */
14999
15000 static bool
15001 cp_parser_token_starts_function_definition_p (cp_token* token)
15002 {
15003   return (/* An ordinary function-body begins with an `{'.  */
15004           token->type == CPP_OPEN_BRACE
15005           /* A ctor-initializer begins with a `:'.  */
15006           || token->type == CPP_COLON
15007           /* A function-try-block begins with `try'.  */
15008           || token->keyword == RID_TRY
15009           /* The named return value extension begins with `return'.  */
15010           || token->keyword == RID_RETURN);
15011 }
15012
15013 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15014    definition.  */
15015
15016 static bool
15017 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15018 {
15019   cp_token *token;
15020
15021   token = cp_lexer_peek_token (parser->lexer);
15022   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15023 }
15024
15025 /* Returns TRUE iff the next token is the "," or ">" ending a
15026    template-argument. ">>" is also accepted (after the full
15027    argument was parsed) because it's probably a typo for "> >",
15028    and there is a specific diagnostic for this.  */
15029
15030 static bool
15031 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15032 {
15033   cp_token *token;
15034
15035   token = cp_lexer_peek_token (parser->lexer);
15036   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15037           || token->type == CPP_RSHIFT);
15038 }
15039
15040 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15041    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15042
15043 static bool
15044 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15045                                                      size_t n)
15046 {
15047   cp_token *token;
15048
15049   token = cp_lexer_peek_nth_token (parser->lexer, n);
15050   if (token->type == CPP_LESS)
15051     return true;
15052   /* Check for the sequence `<::' in the original code. It would be lexed as
15053      `[:', where `[' is a digraph, and there is no whitespace before
15054      `:'.  */
15055   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15056     {
15057       cp_token *token2;
15058       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15059       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15060         return true;
15061     }
15062   return false;
15063 }
15064
15065 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15066    or none_type otherwise.  */
15067
15068 static enum tag_types
15069 cp_parser_token_is_class_key (cp_token* token)
15070 {
15071   switch (token->keyword)
15072     {
15073     case RID_CLASS:
15074       return class_type;
15075     case RID_STRUCT:
15076       return record_type;
15077     case RID_UNION:
15078       return union_type;
15079
15080     default:
15081       return none_type;
15082     }
15083 }
15084
15085 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15086
15087 static void
15088 cp_parser_check_class_key (enum tag_types class_key, tree type)
15089 {
15090   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15091     pedwarn ("`%s' tag used in naming `%#T'",
15092             class_key == union_type ? "union"
15093              : class_key == record_type ? "struct" : "class",
15094              type);
15095 }
15096
15097 /* Issue an error message if DECL is redeclared with different
15098    access than its original declaration [class.access.spec/3].
15099    This applies to nested classes and nested class templates.
15100    [class.mem/1].  */
15101
15102 static void cp_parser_check_access_in_redeclaration (tree decl)
15103 {
15104   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15105     return;
15106
15107   if ((TREE_PRIVATE (decl)
15108        != (current_access_specifier == access_private_node))
15109       || (TREE_PROTECTED (decl)
15110           != (current_access_specifier == access_protected_node)))
15111     error ("%D redeclared with different access", decl);
15112 }
15113
15114 /* Look for the `template' keyword, as a syntactic disambiguator.
15115    Return TRUE iff it is present, in which case it will be
15116    consumed.  */
15117
15118 static bool
15119 cp_parser_optional_template_keyword (cp_parser *parser)
15120 {
15121   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15122     {
15123       /* The `template' keyword can only be used within templates;
15124          outside templates the parser can always figure out what is a
15125          template and what is not.  */
15126       if (!processing_template_decl)
15127         {
15128           error ("`template' (as a disambiguator) is only allowed "
15129                  "within templates");
15130           /* If this part of the token stream is rescanned, the same
15131              error message would be generated.  So, we purge the token
15132              from the stream.  */
15133           cp_lexer_purge_token (parser->lexer);
15134           return false;
15135         }
15136       else
15137         {
15138           /* Consume the `template' keyword.  */
15139           cp_lexer_consume_token (parser->lexer);
15140           return true;
15141         }
15142     }
15143
15144   return false;
15145 }
15146
15147 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15148    set PARSER->SCOPE, and perform other related actions.  */
15149
15150 static void
15151 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15152 {
15153   tree value;
15154   tree check;
15155
15156   /* Get the stored value.  */
15157   value = cp_lexer_consume_token (parser->lexer)->value;
15158   /* Perform any access checks that were deferred.  */
15159   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15160     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15161   /* Set the scope from the stored value.  */
15162   parser->scope = TREE_VALUE (value);
15163   parser->qualifying_scope = TREE_TYPE (value);
15164   parser->object_scope = NULL_TREE;
15165 }
15166
15167 /* Add tokens to CACHE until a non-nested END token appears.  */
15168
15169 static void
15170 cp_parser_cache_group (cp_parser *parser,
15171                        cp_token_cache *cache,
15172                        enum cpp_ttype end,
15173                        unsigned depth)
15174 {
15175   while (true)
15176     {
15177       cp_token *token;
15178
15179       /* Abort a parenthesized expression if we encounter a brace.  */
15180       if ((end == CPP_CLOSE_PAREN || depth == 0)
15181           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15182         return;
15183       /* If we've reached the end of the file, stop.  */
15184       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15185         return;
15186       /* Consume the next token.  */
15187       token = cp_lexer_consume_token (parser->lexer);
15188       /* Add this token to the tokens we are saving.  */
15189       cp_token_cache_push_token (cache, token);
15190       /* See if it starts a new group.  */
15191       if (token->type == CPP_OPEN_BRACE)
15192         {
15193           cp_parser_cache_group (parser, cache, CPP_CLOSE_BRACE, depth + 1);
15194           if (depth == 0)
15195             return;
15196         }
15197       else if (token->type == CPP_OPEN_PAREN)
15198         cp_parser_cache_group (parser, cache, CPP_CLOSE_PAREN, depth + 1);
15199       else if (token->type == end)
15200         return;
15201     }
15202 }
15203
15204 /* Begin parsing tentatively.  We always save tokens while parsing
15205    tentatively so that if the tentative parsing fails we can restore the
15206    tokens.  */
15207
15208 static void
15209 cp_parser_parse_tentatively (cp_parser* parser)
15210 {
15211   /* Enter a new parsing context.  */
15212   parser->context = cp_parser_context_new (parser->context);
15213   /* Begin saving tokens.  */
15214   cp_lexer_save_tokens (parser->lexer);
15215   /* In order to avoid repetitive access control error messages,
15216      access checks are queued up until we are no longer parsing
15217      tentatively.  */
15218   push_deferring_access_checks (dk_deferred);
15219 }
15220
15221 /* Commit to the currently active tentative parse.  */
15222
15223 static void
15224 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15225 {
15226   cp_parser_context *context;
15227   cp_lexer *lexer;
15228
15229   /* Mark all of the levels as committed.  */
15230   lexer = parser->lexer;
15231   for (context = parser->context; context->next; context = context->next)
15232     {
15233       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15234         break;
15235       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15236       while (!cp_lexer_saving_tokens (lexer))
15237         lexer = lexer->next;
15238       cp_lexer_commit_tokens (lexer);
15239     }
15240 }
15241
15242 /* Abort the currently active tentative parse.  All consumed tokens
15243    will be rolled back, and no diagnostics will be issued.  */
15244
15245 static void
15246 cp_parser_abort_tentative_parse (cp_parser* parser)
15247 {
15248   cp_parser_simulate_error (parser);
15249   /* Now, pretend that we want to see if the construct was
15250      successfully parsed.  */
15251   cp_parser_parse_definitely (parser);
15252 }
15253
15254 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15255    token stream.  Otherwise, commit to the tokens we have consumed.
15256    Returns true if no error occurred; false otherwise.  */
15257
15258 static bool
15259 cp_parser_parse_definitely (cp_parser* parser)
15260 {
15261   bool error_occurred;
15262   cp_parser_context *context;
15263
15264   /* Remember whether or not an error occurred, since we are about to
15265      destroy that information.  */
15266   error_occurred = cp_parser_error_occurred (parser);
15267   /* Remove the topmost context from the stack.  */
15268   context = parser->context;
15269   parser->context = context->next;
15270   /* If no parse errors occurred, commit to the tentative parse.  */
15271   if (!error_occurred)
15272     {
15273       /* Commit to the tokens read tentatively, unless that was
15274          already done.  */
15275       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15276         cp_lexer_commit_tokens (parser->lexer);
15277
15278       pop_to_parent_deferring_access_checks ();
15279     }
15280   /* Otherwise, if errors occurred, roll back our state so that things
15281      are just as they were before we began the tentative parse.  */
15282   else
15283     {
15284       cp_lexer_rollback_tokens (parser->lexer);
15285       pop_deferring_access_checks ();
15286     }
15287   /* Add the context to the front of the free list.  */
15288   context->next = cp_parser_context_free_list;
15289   cp_parser_context_free_list = context;
15290
15291   return !error_occurred;
15292 }
15293
15294 /* Returns true if we are parsing tentatively -- but have decided that
15295    we will stick with this tentative parse, even if errors occur.  */
15296
15297 static bool
15298 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15299 {
15300   return (cp_parser_parsing_tentatively (parser)
15301           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15302 }
15303
15304 /* Returns nonzero iff an error has occurred during the most recent
15305    tentative parse.  */
15306
15307 static bool
15308 cp_parser_error_occurred (cp_parser* parser)
15309 {
15310   return (cp_parser_parsing_tentatively (parser)
15311           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15312 }
15313
15314 /* Returns nonzero if GNU extensions are allowed.  */
15315
15316 static bool
15317 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15318 {
15319   return parser->allow_gnu_extensions_p;
15320 }
15321
15322 \f
15323
15324 /* The parser.  */
15325
15326 static GTY (()) cp_parser *the_parser;
15327
15328 /* External interface.  */
15329
15330 /* Parse one entire translation unit.  */
15331
15332 void
15333 c_parse_file (void)
15334 {
15335   bool error_occurred;
15336
15337   the_parser = cp_parser_new ();
15338   push_deferring_access_checks (flag_access_control
15339                                 ? dk_no_deferred : dk_no_check);
15340   error_occurred = cp_parser_translation_unit (the_parser);
15341   the_parser = NULL;
15342 }
15343
15344 /* This variable must be provided by every front end.  */
15345
15346 int yydebug;
15347
15348 #include "gt-cp-parser.h"