OSDN Git Service

* parser.c (cp_token_position): New typedef. Define VEC thereof.
[pf3gnuchains/gcc-fork.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 #include "target.h"
38
39 \f
40 /* The lexer.  */
41
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43    and c-lex.c) and the C++ parser.  */
44
45 /* A C++ token.  */
46
47 typedef struct cp_token GTY (())
48 {
49   /* The kind of token.  */
50   ENUM_BITFIELD (cpp_ttype) type : 8;
51   /* If this token is a keyword, this value indicates which keyword.
52      Otherwise, this value is RID_MAX.  */
53   ENUM_BITFIELD (rid) keyword : 8;
54   /* Token flags.  */
55   unsigned char flags;
56   /* True if this token is from a system header. */
57   BOOL_BITFIELD in_system_header : 1;
58   /* True if this token is from a context where it is implicitly extern "C" */
59   BOOL_BITFIELD implicit_extern_c : 1;
60   /* The value associated with this token, if any.  */
61   tree value;
62   /* The location at which this token was found.  */
63   location_t location;
64 } cp_token;
65
66 /* We use a stack of token pointer for saving token sets.  */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
69
70 static cp_token eof_token = {CPP_EOF, 0, 0, 0, 0, NULL_TREE, {0, 0}};
71
72 /* The cp_lexer structure represents the C++ lexer.  It is responsible
73    for managing the token stream from the preprocessor and supplying
74    it to the parser.  Tokens are never added to the cp_lexer after
75    it is created. */
76
77 typedef struct cp_lexer GTY (())
78 {
79   /* The memory allocated for the buffer.  NULL if this lexer does not
80      own the token buffer.  */
81   cp_token * GTY ((length ("(%h.buffer_end - %h.buffer)"))) buffer;
82   /* If non-null, a pointer just past the end of the memory allocated
83      for the buffer.  */
84   cp_token * GTY ((skip)) buffer_end;
85   
86   /* A pointer just past the last available token.  The tokens
87      in this lexer are [buffer, last_token). */
88   cp_token_position GTY ((skip)) last_token;
89
90   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
91      no more available tokens.  */
92   cp_token_position GTY ((skip)) next_token;
93
94   /* A stack indicating positions at which cp_lexer_save_tokens was
95      called.  The top entry is the most recent position at which we
96      began saving tokens.  If the stack is non-empty, we are saving
97      tokens.  */
98   VEC (cp_token_position) *GTY ((skip)) saved_tokens;
99
100   /* True if we should output debugging information.  */
101   bool debugging_p;
102
103   /* The next lexer in a linked list of lexers.  */
104   struct cp_lexer *next;
105 } cp_lexer;
106
107 /* cp_token_cache is a range of tokens.  There is no need to represent
108    allocate heap memory for it, since tokens are never removed from the
109    lexer's array.  There is also no need for the GC to walk through
110    a cp_token_cache, since everything in here is referenced through
111    a lexer. */
112
113 typedef struct cp_token_cache GTY(())
114 {
115   /* The beginning of the token range. */
116   cp_token * GTY((skip)) first;
117
118   /* Points immediately after the last token in the range. */
119   cp_token * GTY ((skip)) last;
120 } cp_token_cache;
121
122 /* Prototypes.  */
123
124 static cp_lexer *cp_lexer_new_main
125   (void);
126 static cp_lexer *cp_lexer_new_from_tokens
127   (cp_token_cache *tokens);
128 static void cp_lexer_destroy
129   (cp_lexer *);
130 static int cp_lexer_saving_tokens
131   (const cp_lexer *);
132 static cp_token_position cp_lexer_token_position
133   (cp_lexer *, bool);
134 static cp_token *cp_lexer_token_at
135   (cp_lexer *, cp_token_position);
136 static void cp_lexer_grow_buffer
137   (cp_lexer *);
138 static void cp_lexer_get_preprocessor_token
139   (cp_lexer *, cp_token *);
140 static inline cp_token *cp_lexer_peek_token
141   (cp_lexer *);
142 static cp_token *cp_lexer_peek_nth_token
143   (cp_lexer *, size_t);
144 static inline bool cp_lexer_next_token_is
145   (cp_lexer *, enum cpp_ttype);
146 static bool cp_lexer_next_token_is_not
147   (cp_lexer *, enum cpp_ttype);
148 static bool cp_lexer_next_token_is_keyword
149   (cp_lexer *, enum rid);
150 static cp_token *cp_lexer_consume_token
151   (cp_lexer *);
152 static void cp_lexer_purge_token
153   (cp_lexer *);
154 static void cp_lexer_purge_tokens_after
155   (cp_lexer *, cp_token_position);
156 static void cp_lexer_handle_pragma
157   (cp_lexer *);
158 static void cp_lexer_save_tokens
159   (cp_lexer *);
160 static void cp_lexer_commit_tokens
161   (cp_lexer *);
162 static void cp_lexer_rollback_tokens
163   (cp_lexer *);
164 #ifdef ENABLE_CHECKING
165 static void cp_lexer_print_token
166   (FILE *, cp_token *);
167 static inline bool cp_lexer_debugging_p
168   (cp_lexer *);
169 static void cp_lexer_start_debugging
170   (cp_lexer *) ATTRIBUTE_UNUSED;
171 static void cp_lexer_stop_debugging
172   (cp_lexer *) ATTRIBUTE_UNUSED;
173 #else
174 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
175    about passing NULL to functions that require non-NULL arguments
176    (fputs, fprintf).  It will never be used, so all we need is a value
177    of the right type that's guaranteed not to be NULL.  */
178 #define cp_lexer_debug_stream stdout
179 #define cp_lexer_print_token(str, tok) (void) 0
180 #define cp_lexer_debugging_p(lexer) 0
181 #endif /* ENABLE_CHECKING */
182
183 static cp_token_cache *cp_token_cache_new
184   (cp_token *, cp_token *);
185
186 /* Manifest constants.  */
187 #define CP_LEXER_BUFFER_SIZE 10000
188 #define CP_SAVED_TOKEN_STACK 5
189
190 /* A token type for keywords, as opposed to ordinary identifiers.  */
191 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
192
193 /* A token type for template-ids.  If a template-id is processed while
194    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
195    the value of the CPP_TEMPLATE_ID is whatever was returned by
196    cp_parser_template_id.  */
197 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
198
199 /* A token type for nested-name-specifiers.  If a
200    nested-name-specifier is processed while parsing tentatively, it is
201    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
202    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
203    cp_parser_nested_name_specifier_opt.  */
204 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
205
206 /* A token type for tokens that are not tokens at all; these are used
207    to represent slots in the array where there used to be a token
208    that has now been deleted. */
209 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
210
211 /* The number of token types, including C++-specific ones.  */
212 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
213
214 /* Variables.  */
215
216 #ifdef ENABLE_CHECKING
217 /* The stream to which debugging output should be written.  */
218 static FILE *cp_lexer_debug_stream;
219 #endif /* ENABLE_CHECKING */
220
221 /* Create a new main C++ lexer, the lexer that gets tokens from the
222    preprocessor.  */
223
224 static cp_lexer *
225 cp_lexer_new_main (void)
226 {
227   cp_lexer *lexer;
228   cp_token first_token;
229
230   /* Tell cpplib we want CPP_PRAGMA tokens. */
231   cpp_get_options (parse_in)->defer_pragmas = true;
232
233   /* Tell c_lex not to merge string constants.  */
234   c_lex_return_raw_strings = true;
235
236   /* It's possible that lexing the first token will load a PCH file,
237      which is a GC collection point.  So we have to grab the first
238      token before allocating any memory.  */
239   cp_lexer_get_preprocessor_token (NULL, &first_token);
240   c_common_no_more_pch ();
241
242   /* Allocate the memory.  */
243   lexer = GGC_CNEW (cp_lexer);
244
245   /* Create the buffer.  */
246   lexer->buffer = ggc_calloc (CP_LEXER_BUFFER_SIZE, sizeof (cp_token));
247   lexer->buffer_end = lexer->buffer + CP_LEXER_BUFFER_SIZE;
248  
249   /* There is one token in the buffer.  */
250   lexer->last_token = lexer->buffer;
251   lexer->next_token = lexer->buffer;
252   *lexer->next_token = first_token;
253
254   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
255
256 #ifdef ENABLE_CHECKING  
257   /* Initially we are not debugging.  */
258   lexer->debugging_p = false;
259 #endif /* ENABLE_CHECKING */
260
261   /* Get the rest of the tokens from the preprocessor. */
262   while (lexer->last_token->type != CPP_EOF)
263     {
264       lexer->last_token++;
265       if (lexer->last_token == lexer->buffer_end)
266         cp_lexer_grow_buffer (lexer);
267       cp_lexer_get_preprocessor_token (lexer, lexer->last_token);
268     }
269
270   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
271      direct calls to c_lex.  Those callers all expect c_lex to do
272      string constant concatenation.  */
273   c_lex_return_raw_strings = false;
274
275   gcc_assert (lexer->next_token->type != CPP_PURGED);
276   return lexer;
277 }
278
279 /* Create a new lexer whose token stream is primed with the tokens in
280    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
281
282 static cp_lexer *
283 cp_lexer_new_from_tokens (cp_token_cache *cache)
284 {
285   cp_token *first = cache->first;
286   cp_token *last = cache->last;
287   cp_lexer *lexer = GGC_CNEW (cp_lexer);
288
289   /* We do not own the buffer.  */
290   lexer->buffer = lexer->buffer_end = NULL;
291   lexer->next_token = first == last ? &eof_token : first;
292   lexer->last_token = last;
293   
294   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
295
296 #ifdef ENABLE_CHECKING
297   /* Initially we are not debugging.  */
298   lexer->debugging_p = false;
299 #endif
300
301   gcc_assert (lexer->next_token->type != CPP_PURGED);
302   return lexer;
303 }
304
305 /* Frees all resources associated with LEXER. */
306
307 static void
308 cp_lexer_destroy (cp_lexer *lexer)
309 {
310   if (lexer->buffer)
311     ggc_free (lexer->buffer);
312   VEC_free (cp_token_position, lexer->saved_tokens);
313   ggc_free (lexer);
314 }
315
316 /* Returns nonzero if debugging information should be output.  */
317
318 #ifdef ENABLE_CHECKING
319
320 static inline bool
321 cp_lexer_debugging_p (cp_lexer *lexer)
322 {
323   return lexer->debugging_p;
324 }
325
326 #endif /* ENABLE_CHECKING */
327
328 static inline cp_token_position
329 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
330 {
331   gcc_assert (!previous_p || lexer->next_token != &eof_token);
332   
333   return lexer->next_token - previous_p;
334 }
335
336 static inline cp_token *
337 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
338 {
339   return pos;
340 }
341
342 /* nonzero if we are presently saving tokens.  */
343
344 static inline int
345 cp_lexer_saving_tokens (const cp_lexer* lexer)
346 {
347   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
348 }
349
350 /* If the buffer is full, make it bigger.  */
351 static void
352 cp_lexer_grow_buffer (cp_lexer* lexer)
353 {
354   cp_token *old_buffer;
355   cp_token *new_buffer;
356   ptrdiff_t buffer_length;
357
358   /* This function should only be called when buffer is full. */
359   gcc_assert (lexer->last_token == lexer->buffer_end);
360
361   /* Remember the current buffer pointer.  It will become invalid,
362      but we will need to do pointer arithmetic involving this
363      value.  */
364   old_buffer = lexer->buffer;
365   /* Compute the current buffer size.  */
366   buffer_length = lexer->buffer_end - lexer->buffer;
367   /* Allocate a buffer twice as big.  */
368   new_buffer = ggc_realloc (lexer->buffer,
369                             2 * buffer_length * sizeof (cp_token));
370
371   /* Recompute buffer positions. */
372   lexer->buffer = new_buffer;
373   lexer->buffer_end = new_buffer + 2 * buffer_length;
374   lexer->last_token = new_buffer + (lexer->last_token - old_buffer);
375   lexer->next_token = new_buffer + (lexer->next_token - old_buffer);
376
377   /* Clear the rest of the buffer.  We never look at this storage,
378      but the garbage collector may.  */
379   memset (lexer->last_token, 0,
380           (lexer->buffer_end - lexer->last_token) * sizeof(cp_token));
381 }
382
383 /* Store the next token from the preprocessor in *TOKEN.  */
384
385 static void
386 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
387                                  cp_token *token)
388 {
389   static int is_extern_c = 0;
390   bool done;
391
392   done = false;
393   /* Keep going until we get a token we like.  */
394   while (!done)
395     {
396       /* Get a new token from the preprocessor.  */
397       token->type = c_lex_with_flags (&token->value, &token->flags);
398       /* Issue messages about tokens we cannot process.  */
399       switch (token->type)
400         {
401         case CPP_ATSIGN:
402         case CPP_HASH:
403         case CPP_PASTE:
404           error ("invalid token");
405           break;
406
407         default:
408           /* This is a good token, so we exit the loop.  */
409           done = true;
410           break;
411         }
412     }
413   /* Now we've got our token.  */
414   token->location = input_location;
415   token->in_system_header = in_system_header;
416
417   /* On some systems, some header files are surrounded by an 
418      implicit extern "C" block.  Set a flag in the token if it
419      comes from such a header. */
420   is_extern_c += pending_lang_change;
421   pending_lang_change = 0;
422   token->implicit_extern_c = is_extern_c > 0;
423
424   /* Check to see if this token is a keyword.  */
425   if (token->type == CPP_NAME
426       && C_IS_RESERVED_WORD (token->value))
427     {
428       /* Mark this token as a keyword.  */
429       token->type = CPP_KEYWORD;
430       /* Record which keyword.  */
431       token->keyword = C_RID_CODE (token->value);
432       /* Update the value.  Some keywords are mapped to particular
433          entities, rather than simply having the value of the
434          corresponding IDENTIFIER_NODE.  For example, `__const' is
435          mapped to `const'.  */
436       token->value = ridpointers[token->keyword];
437     }
438   else
439     token->keyword = RID_MAX;
440 }
441
442 /* Update the globals input_location and in_system_header from TOKEN.   */
443 static inline void
444 cp_lexer_set_source_position_from_token (cp_token *token)
445 {
446   if (token->type != CPP_EOF)
447     {
448       input_location = token->location;
449       in_system_header = token->in_system_header;
450     }
451 }
452
453 /* Return a pointer to the next token in the token stream, but do not
454    consume it.  */
455
456 static inline cp_token *
457 cp_lexer_peek_token (cp_lexer *lexer)
458 {
459   if (cp_lexer_debugging_p (lexer))
460     {
461       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
462       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
463       putc ('\n', cp_lexer_debug_stream);
464     }
465   return lexer->next_token;
466 }
467
468 /* Return true if the next token has the indicated TYPE.  */
469
470 static inline bool
471 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
472 {
473   return cp_lexer_peek_token (lexer)->type == type;
474 }
475
476 /* Return true if the next token does not have the indicated TYPE.  */
477
478 static inline bool
479 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
480 {
481   return !cp_lexer_next_token_is (lexer, type);
482 }
483
484 /* Return true if the next token is the indicated KEYWORD.  */
485
486 static inline bool
487 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
488 {
489   cp_token *token;
490
491   /* Peek at the next token.  */
492   token = cp_lexer_peek_token (lexer);
493   /* Check to see if it is the indicated keyword.  */
494   return token->keyword == keyword;
495 }
496
497 /* Return a pointer to the Nth token in the token stream.  If N is 1,
498    then this is precisely equivalent to cp_lexer_peek_token (except
499    that it is not inline).  One would like to disallow that case, but
500    there is one case (cp_parser_nth_token_starts_template_id) where
501    the caller passes a variable for N and it might be 1.  */
502
503 static cp_token *
504 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
505 {
506   cp_token *token;
507
508   /* N is 1-based, not zero-based.  */
509   gcc_assert (n > 0 && lexer->next_token != &eof_token);
510
511   if (cp_lexer_debugging_p (lexer))
512     fprintf (cp_lexer_debug_stream,
513              "cp_lexer: peeking ahead %ld at token: ", (long)n);
514
515   --n;
516   token = lexer->next_token;
517   while (n != 0)
518     {
519       ++token;
520       if (token == lexer->last_token)
521         {
522           token = &eof_token;
523           break;
524         }
525       
526       if (token->type != CPP_PURGED)
527         --n;
528     }
529
530   if (cp_lexer_debugging_p (lexer))
531     {
532       cp_lexer_print_token (cp_lexer_debug_stream, token);
533       putc ('\n', cp_lexer_debug_stream);
534     }
535
536   return token;
537 }
538
539 /* Return the next token, and advance the lexer's next_token pointer
540    to point to the next non-purged token.  */
541
542 static cp_token *
543 cp_lexer_consume_token (cp_lexer* lexer)
544 {
545   cp_token *token = lexer->next_token;
546
547   gcc_assert (token != &eof_token);
548   
549   do
550     {
551       lexer->next_token++;
552       if (lexer->next_token == lexer->last_token)
553         {
554           lexer->next_token = &eof_token;
555           break;
556         }
557       
558     }
559   while (lexer->next_token->type == CPP_PURGED);
560   
561   cp_lexer_set_source_position_from_token (token);
562   
563   /* Provide debugging output.  */
564   if (cp_lexer_debugging_p (lexer))
565     {
566       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
567       cp_lexer_print_token (cp_lexer_debug_stream, token);
568       putc ('\n', cp_lexer_debug_stream);
569     }
570   
571   return token;
572 }
573
574 /* Permanently remove the next token from the token stream, and
575    advance the next_token pointer to refer to the next non-purged
576    token.  */
577
578 static void
579 cp_lexer_purge_token (cp_lexer *lexer)
580 {
581   cp_token *tok = lexer->next_token;
582   
583   gcc_assert (tok != &eof_token);
584   tok->type = CPP_PURGED;
585   tok->location = UNKNOWN_LOCATION;
586   tok->value = NULL_TREE;
587   tok->keyword = RID_MAX;
588
589   do
590     {
591       tok++;
592       if (tok == lexer->last_token)
593         {
594           tok = &eof_token;
595           break;
596         }
597     }
598   while (tok->type == CPP_PURGED);
599   lexer->next_token = tok;
600 }
601
602 /* Permanently remove all tokens after TOK, up to, but not
603    including, the token that will be returned next by
604    cp_lexer_peek_token.  */
605
606 static void
607 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
608 {
609   cp_token *peek = lexer->next_token;
610
611   if (peek == &eof_token)
612     peek = lexer->last_token;
613   
614   gcc_assert (tok < peek);
615
616   for ( tok += 1; tok != peek; tok += 1)
617     {
618       tok->type = CPP_PURGED;
619       tok->location = UNKNOWN_LOCATION;
620       tok->value = NULL_TREE;
621       tok->keyword = RID_MAX;
622     }
623 }
624
625 /* Consume and handle a pragma token.   */
626 static void
627 cp_lexer_handle_pragma (cp_lexer *lexer)
628 {
629   cpp_string s;
630   cp_token *token = cp_lexer_consume_token (lexer);
631   gcc_assert (token->type == CPP_PRAGMA);
632   gcc_assert (token->value);
633
634   s.len = TREE_STRING_LENGTH (token->value);
635   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
636
637   cpp_handle_deferred_pragma (parse_in, &s);
638
639   /* Clearing token->value here means that we will get an ICE if we
640      try to process this #pragma again (which should be impossible).  */
641   token->value = NULL;
642 }
643
644 /* Begin saving tokens.  All tokens consumed after this point will be
645    preserved.  */
646
647 static void
648 cp_lexer_save_tokens (cp_lexer* lexer)
649 {
650   /* Provide debugging output.  */
651   if (cp_lexer_debugging_p (lexer))
652     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
653
654   VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
655 }
656
657 /* Commit to the portion of the token stream most recently saved.  */
658
659 static void
660 cp_lexer_commit_tokens (cp_lexer* lexer)
661 {
662   /* Provide debugging output.  */
663   if (cp_lexer_debugging_p (lexer))
664     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
665
666   VEC_pop (cp_token_position, lexer->saved_tokens);
667 }
668
669 /* Return all tokens saved since the last call to cp_lexer_save_tokens
670    to the token stream.  Stop saving tokens.  */
671
672 static void
673 cp_lexer_rollback_tokens (cp_lexer* lexer)
674 {
675   /* Provide debugging output.  */
676   if (cp_lexer_debugging_p (lexer))
677     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
678
679   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
680 }
681
682 /* Print a representation of the TOKEN on the STREAM.  */
683
684 #ifdef ENABLE_CHECKING
685
686 static void
687 cp_lexer_print_token (FILE * stream, cp_token *token)
688 {
689   /* We don't use cpp_type2name here because the parser defines
690      a few tokens of its own.  */
691   static const char *const token_names[] = {
692     /* cpplib-defined token types */
693 #define OP(e, s) #e,
694 #define TK(e, s) #e,
695     TTYPE_TABLE
696 #undef OP
697 #undef TK
698     /* C++ parser token types - see "Manifest constants", above.  */
699     "KEYWORD",
700     "TEMPLATE_ID",
701     "NESTED_NAME_SPECIFIER",
702     "PURGED"
703   };
704   
705   /* If we have a name for the token, print it out.  Otherwise, we
706      simply give the numeric code.  */
707   gcc_assert (token->type < ARRAY_SIZE(token_names));
708   fputs (token_names[token->type], stream);
709
710   /* For some tokens, print the associated data.  */
711   switch (token->type)
712     {
713     case CPP_KEYWORD:
714       /* Some keywords have a value that is not an IDENTIFIER_NODE.
715          For example, `struct' is mapped to an INTEGER_CST.  */
716       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
717         break;
718       /* else fall through */
719     case CPP_NAME:
720       fputs (IDENTIFIER_POINTER (token->value), stream);
721       break;
722
723     case CPP_STRING:
724     case CPP_WSTRING:
725     case CPP_PRAGMA:
726       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
727       break;
728
729     default:
730       break;
731     }
732 }
733
734 /* Start emitting debugging information.  */
735
736 static void
737 cp_lexer_start_debugging (cp_lexer* lexer)
738 {
739   ++lexer->debugging_p;
740 }
741
742 /* Stop emitting debugging information.  */
743
744 static void
745 cp_lexer_stop_debugging (cp_lexer* lexer)
746 {
747   --lexer->debugging_p;
748 }
749
750 #endif /* ENABLE_CHECKING */
751
752 /* Create a new cp_token_cache, representing a range of tokens. */
753
754 static cp_token_cache *
755 cp_token_cache_new (cp_token *first, cp_token *last)
756 {
757   cp_token_cache *cache = GGC_NEW (cp_token_cache);
758   cache->first = first;
759   cache->last = last;
760   return cache;
761 }
762
763 \f
764 /* Decl-specifiers.  */
765
766 static void clear_decl_specs
767   (cp_decl_specifier_seq *);
768
769 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
770
771 static void
772 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
773 {
774   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
775 }
776
777 /* Declarators.  */
778
779 /* Nothing other than the parser should be creating declarators;
780    declarators are a semi-syntactic representation of C++ entities.
781    Other parts of the front end that need to create entities (like
782    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
783
784 static cp_declarator *make_id_declarator
785   (tree);
786 static cp_declarator *make_call_declarator
787   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
788 static cp_declarator *make_array_declarator
789   (cp_declarator *, tree);
790 static cp_declarator *make_pointer_declarator
791   (cp_cv_quals, cp_declarator *);
792 static cp_declarator *make_reference_declarator
793   (cp_cv_quals, cp_declarator *);
794 static cp_parameter_declarator *make_parameter_declarator
795   (cp_decl_specifier_seq *, cp_declarator *, tree);
796 static cp_declarator *make_ptrmem_declarator
797   (cp_cv_quals, tree, cp_declarator *);
798
799 cp_declarator *cp_error_declarator;
800
801 /* The obstack on which declarators and related data structures are
802    allocated.  */
803 static struct obstack declarator_obstack;
804
805 /* Alloc BYTES from the declarator memory pool.  */
806
807 static inline void *
808 alloc_declarator (size_t bytes)
809 {
810   return obstack_alloc (&declarator_obstack, bytes);
811 }
812
813 /* Allocate a declarator of the indicated KIND.  Clear fields that are
814    common to all declarators.  */
815
816 static cp_declarator *
817 make_declarator (cp_declarator_kind kind)
818 {
819   cp_declarator *declarator;
820
821   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
822   declarator->kind = kind;
823   declarator->attributes = NULL_TREE;
824   declarator->declarator = NULL;
825
826   return declarator;
827 }
828
829 /* Make a declarator for a generalized identifier.  */
830
831 cp_declarator *
832 make_id_declarator (tree id)
833 {
834   cp_declarator *declarator;
835
836   declarator = make_declarator (cdk_id);
837   declarator->u.id.name = id;
838   declarator->u.id.sfk = sfk_none;
839
840   return declarator;
841 }
842
843 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
844    of modifiers such as const or volatile to apply to the pointer
845    type, represented as identifiers.  */
846
847 cp_declarator *
848 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
849 {
850   cp_declarator *declarator;
851
852   declarator = make_declarator (cdk_pointer);
853   declarator->declarator = target;
854   declarator->u.pointer.qualifiers = cv_qualifiers;
855   declarator->u.pointer.class_type = NULL_TREE;
856
857   return declarator;
858 }
859
860 /* Like make_pointer_declarator -- but for references.  */
861
862 cp_declarator *
863 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
864 {
865   cp_declarator *declarator;
866
867   declarator = make_declarator (cdk_reference);
868   declarator->declarator = target;
869   declarator->u.pointer.qualifiers = cv_qualifiers;
870   declarator->u.pointer.class_type = NULL_TREE;
871
872   return declarator;
873 }
874
875 /* Like make_pointer_declarator -- but for a pointer to a non-static
876    member of CLASS_TYPE.  */
877
878 cp_declarator *
879 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
880                         cp_declarator *pointee)
881 {
882   cp_declarator *declarator;
883
884   declarator = make_declarator (cdk_ptrmem);
885   declarator->declarator = pointee;
886   declarator->u.pointer.qualifiers = cv_qualifiers;
887   declarator->u.pointer.class_type = class_type;
888
889   return declarator;
890 }
891
892 /* Make a declarator for the function given by TARGET, with the
893    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
894    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
895    indicates what exceptions can be thrown.  */
896
897 cp_declarator *
898 make_call_declarator (cp_declarator *target,
899                       cp_parameter_declarator *parms,
900                       cp_cv_quals cv_qualifiers,
901                       tree exception_specification)
902 {
903   cp_declarator *declarator;
904
905   declarator = make_declarator (cdk_function);
906   declarator->declarator = target;
907   declarator->u.function.parameters = parms;
908   declarator->u.function.qualifiers = cv_qualifiers;
909   declarator->u.function.exception_specification = exception_specification;
910
911   return declarator;
912 }
913
914 /* Make a declarator for an array of BOUNDS elements, each of which is
915    defined by ELEMENT.  */
916
917 cp_declarator *
918 make_array_declarator (cp_declarator *element, tree bounds)
919 {
920   cp_declarator *declarator;
921
922   declarator = make_declarator (cdk_array);
923   declarator->declarator = element;
924   declarator->u.array.bounds = bounds;
925
926   return declarator;
927 }
928
929 cp_parameter_declarator *no_parameters;
930
931 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
932    DECLARATOR and DEFAULT_ARGUMENT.  */
933
934 cp_parameter_declarator *
935 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
936                            cp_declarator *declarator,
937                            tree default_argument)
938 {
939   cp_parameter_declarator *parameter;
940
941   parameter = ((cp_parameter_declarator *)
942                alloc_declarator (sizeof (cp_parameter_declarator)));
943   parameter->next = NULL;
944   if (decl_specifiers)
945     parameter->decl_specifiers = *decl_specifiers;
946   else
947     clear_decl_specs (&parameter->decl_specifiers);
948   parameter->declarator = declarator;
949   parameter->default_argument = default_argument;
950   parameter->ellipsis_p = false;
951
952   return parameter;
953 }
954
955 /* The parser.  */
956
957 /* Overview
958    --------
959
960    A cp_parser parses the token stream as specified by the C++
961    grammar.  Its job is purely parsing, not semantic analysis.  For
962    example, the parser breaks the token stream into declarators,
963    expressions, statements, and other similar syntactic constructs.
964    It does not check that the types of the expressions on either side
965    of an assignment-statement are compatible, or that a function is
966    not declared with a parameter of type `void'.
967
968    The parser invokes routines elsewhere in the compiler to perform
969    semantic analysis and to build up the abstract syntax tree for the
970    code processed.
971
972    The parser (and the template instantiation code, which is, in a
973    way, a close relative of parsing) are the only parts of the
974    compiler that should be calling push_scope and pop_scope, or
975    related functions.  The parser (and template instantiation code)
976    keeps track of what scope is presently active; everything else
977    should simply honor that.  (The code that generates static
978    initializers may also need to set the scope, in order to check
979    access control correctly when emitting the initializers.)
980
981    Methodology
982    -----------
983
984    The parser is of the standard recursive-descent variety.  Upcoming
985    tokens in the token stream are examined in order to determine which
986    production to use when parsing a non-terminal.  Some C++ constructs
987    require arbitrary look ahead to disambiguate.  For example, it is
988    impossible, in the general case, to tell whether a statement is an
989    expression or declaration without scanning the entire statement.
990    Therefore, the parser is capable of "parsing tentatively."  When the
991    parser is not sure what construct comes next, it enters this mode.
992    Then, while we attempt to parse the construct, the parser queues up
993    error messages, rather than issuing them immediately, and saves the
994    tokens it consumes.  If the construct is parsed successfully, the
995    parser "commits", i.e., it issues any queued error messages and
996    the tokens that were being preserved are permanently discarded.
997    If, however, the construct is not parsed successfully, the parser
998    rolls back its state completely so that it can resume parsing using
999    a different alternative.
1000
1001    Future Improvements
1002    -------------------
1003
1004    The performance of the parser could probably be improved substantially.
1005    We could often eliminate the need to parse tentatively by looking ahead
1006    a little bit.  In some places, this approach might not entirely eliminate
1007    the need to parse tentatively, but it might still speed up the average
1008    case.  */
1009
1010 /* Flags that are passed to some parsing functions.  These values can
1011    be bitwise-ored together.  */
1012
1013 typedef enum cp_parser_flags
1014 {
1015   /* No flags.  */
1016   CP_PARSER_FLAGS_NONE = 0x0,
1017   /* The construct is optional.  If it is not present, then no error
1018      should be issued.  */
1019   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1020   /* When parsing a type-specifier, do not allow user-defined types.  */
1021   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1022 } cp_parser_flags;
1023
1024 /* The different kinds of declarators we want to parse.  */
1025
1026 typedef enum cp_parser_declarator_kind
1027 {
1028   /* We want an abstract declarator.  */
1029   CP_PARSER_DECLARATOR_ABSTRACT,
1030   /* We want a named declarator.  */
1031   CP_PARSER_DECLARATOR_NAMED,
1032   /* We don't mind, but the name must be an unqualified-id.  */
1033   CP_PARSER_DECLARATOR_EITHER
1034 } cp_parser_declarator_kind;
1035
1036 /* The precedence values used to parse binary expressions.  The minimum value
1037    of PREC must be 1, because zero is reserved to quickly discriminate
1038    binary operators from other tokens.  */
1039
1040 enum cp_parser_prec
1041 {
1042   PREC_NOT_OPERATOR,
1043   PREC_LOGICAL_OR_EXPRESSION,
1044   PREC_LOGICAL_AND_EXPRESSION,
1045   PREC_INCLUSIVE_OR_EXPRESSION,
1046   PREC_EXCLUSIVE_OR_EXPRESSION,
1047   PREC_AND_EXPRESSION,
1048   PREC_EQUALITY_EXPRESSION,
1049   PREC_RELATIONAL_EXPRESSION,
1050   PREC_SHIFT_EXPRESSION,
1051   PREC_ADDITIVE_EXPRESSION,
1052   PREC_MULTIPLICATIVE_EXPRESSION,
1053   PREC_PM_EXPRESSION,
1054   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1055 };
1056
1057 /* A mapping from a token type to a corresponding tree node type, with a
1058    precedence value.  */
1059
1060 typedef struct cp_parser_binary_operations_map_node
1061 {
1062   /* The token type.  */
1063   enum cpp_ttype token_type;
1064   /* The corresponding tree code.  */
1065   enum tree_code tree_type;
1066   /* The precedence of this operator.  */
1067   enum cp_parser_prec prec;
1068 } cp_parser_binary_operations_map_node;
1069
1070 /* The status of a tentative parse.  */
1071
1072 typedef enum cp_parser_status_kind
1073 {
1074   /* No errors have occurred.  */
1075   CP_PARSER_STATUS_KIND_NO_ERROR,
1076   /* An error has occurred.  */
1077   CP_PARSER_STATUS_KIND_ERROR,
1078   /* We are committed to this tentative parse, whether or not an error
1079      has occurred.  */
1080   CP_PARSER_STATUS_KIND_COMMITTED
1081 } cp_parser_status_kind;
1082
1083 typedef struct cp_parser_expression_stack_entry
1084 {
1085   tree lhs;
1086   enum tree_code tree_type;
1087   int prec;
1088 } cp_parser_expression_stack_entry;
1089
1090 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1091    entries because precedence levels on the stack are monotonically
1092    increasing.  */
1093 typedef struct cp_parser_expression_stack_entry
1094   cp_parser_expression_stack[NUM_PREC_VALUES];
1095
1096 /* Context that is saved and restored when parsing tentatively.  */
1097 typedef struct cp_parser_context GTY (())
1098 {
1099   /* If this is a tentative parsing context, the status of the
1100      tentative parse.  */
1101   enum cp_parser_status_kind status;
1102   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1103      that are looked up in this context must be looked up both in the
1104      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1105      the context of the containing expression.  */
1106   tree object_type;
1107
1108   /* The next parsing context in the stack.  */
1109   struct cp_parser_context *next;
1110 } cp_parser_context;
1111
1112 /* Prototypes.  */
1113
1114 /* Constructors and destructors.  */
1115
1116 static cp_parser_context *cp_parser_context_new
1117   (cp_parser_context *);
1118
1119 /* Class variables.  */
1120
1121 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1122
1123 /* The operator-precedence table used by cp_parser_binary_expression.
1124    Transformed into an associative array (binops_by_token) by
1125    cp_parser_new.  */
1126
1127 static const cp_parser_binary_operations_map_node binops[] = {
1128   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1129   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1130
1131   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1132   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1133   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1134
1135   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1136   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1137
1138   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1139   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1140
1141   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1142   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1143   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1144   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1145   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1146   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1147
1148   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1149   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1150
1151   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1152
1153   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1154
1155   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1156
1157   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1158
1159   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1160 };
1161
1162 /* The same as binops, but initialized by cp_parser_new so that
1163    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1164    for speed.  */
1165 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1166
1167 /* Constructors and destructors.  */
1168
1169 /* Construct a new context.  The context below this one on the stack
1170    is given by NEXT.  */
1171
1172 static cp_parser_context *
1173 cp_parser_context_new (cp_parser_context* next)
1174 {
1175   cp_parser_context *context;
1176
1177   /* Allocate the storage.  */
1178   if (cp_parser_context_free_list != NULL)
1179     {
1180       /* Pull the first entry from the free list.  */
1181       context = cp_parser_context_free_list;
1182       cp_parser_context_free_list = context->next;
1183       memset (context, 0, sizeof (*context));
1184     }
1185   else
1186     context = GGC_CNEW (cp_parser_context);
1187
1188   /* No errors have occurred yet in this context.  */
1189   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1190   /* If this is not the bottomost context, copy information that we
1191      need from the previous context.  */
1192   if (next)
1193     {
1194       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1195          expression, then we are parsing one in this context, too.  */
1196       context->object_type = next->object_type;
1197       /* Thread the stack.  */
1198       context->next = next;
1199     }
1200
1201   return context;
1202 }
1203
1204 /* The cp_parser structure represents the C++ parser.  */
1205
1206 typedef struct cp_parser GTY(())
1207 {
1208   /* The lexer from which we are obtaining tokens.  */
1209   cp_lexer *lexer;
1210
1211   /* The scope in which names should be looked up.  If NULL_TREE, then
1212      we look up names in the scope that is currently open in the
1213      source program.  If non-NULL, this is either a TYPE or
1214      NAMESPACE_DECL for the scope in which we should look.
1215
1216      This value is not cleared automatically after a name is looked
1217      up, so we must be careful to clear it before starting a new look
1218      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1219      will look up `Z' in the scope of `X', rather than the current
1220      scope.)  Unfortunately, it is difficult to tell when name lookup
1221      is complete, because we sometimes peek at a token, look it up,
1222      and then decide not to consume it.  */
1223   tree scope;
1224
1225   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1226      last lookup took place.  OBJECT_SCOPE is used if an expression
1227      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1228      respectively.  QUALIFYING_SCOPE is used for an expression of the
1229      form "X::Y"; it refers to X.  */
1230   tree object_scope;
1231   tree qualifying_scope;
1232
1233   /* A stack of parsing contexts.  All but the bottom entry on the
1234      stack will be tentative contexts.
1235
1236      We parse tentatively in order to determine which construct is in
1237      use in some situations.  For example, in order to determine
1238      whether a statement is an expression-statement or a
1239      declaration-statement we parse it tentatively as a
1240      declaration-statement.  If that fails, we then reparse the same
1241      token stream as an expression-statement.  */
1242   cp_parser_context *context;
1243
1244   /* True if we are parsing GNU C++.  If this flag is not set, then
1245      GNU extensions are not recognized.  */
1246   bool allow_gnu_extensions_p;
1247
1248   /* TRUE if the `>' token should be interpreted as the greater-than
1249      operator.  FALSE if it is the end of a template-id or
1250      template-parameter-list.  */
1251   bool greater_than_is_operator_p;
1252
1253   /* TRUE if default arguments are allowed within a parameter list
1254      that starts at this point. FALSE if only a gnu extension makes
1255      them permissible.  */
1256   bool default_arg_ok_p;
1257
1258   /* TRUE if we are parsing an integral constant-expression.  See
1259      [expr.const] for a precise definition.  */
1260   bool integral_constant_expression_p;
1261
1262   /* TRUE if we are parsing an integral constant-expression -- but a
1263      non-constant expression should be permitted as well.  This flag
1264      is used when parsing an array bound so that GNU variable-length
1265      arrays are tolerated.  */
1266   bool allow_non_integral_constant_expression_p;
1267
1268   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1269      been seen that makes the expression non-constant.  */
1270   bool non_integral_constant_expression_p;
1271
1272   /* TRUE if local variable names and `this' are forbidden in the
1273      current context.  */
1274   bool local_variables_forbidden_p;
1275
1276   /* TRUE if the declaration we are parsing is part of a
1277      linkage-specification of the form `extern string-literal
1278      declaration'.  */
1279   bool in_unbraced_linkage_specification_p;
1280
1281   /* TRUE if we are presently parsing a declarator, after the
1282      direct-declarator.  */
1283   bool in_declarator_p;
1284
1285   /* TRUE if we are presently parsing a template-argument-list.  */
1286   bool in_template_argument_list_p;
1287
1288   /* TRUE if we are presently parsing the body of an
1289      iteration-statement.  */
1290   bool in_iteration_statement_p;
1291
1292   /* TRUE if we are presently parsing the body of a switch
1293      statement.  */
1294   bool in_switch_statement_p;
1295
1296   /* TRUE if we are parsing a type-id in an expression context.  In
1297      such a situation, both "type (expr)" and "type (type)" are valid
1298      alternatives.  */
1299   bool in_type_id_in_expr_p;
1300
1301   /* TRUE if we are currently in a header file where declarations are
1302      implicitly extern "C". */
1303   bool implicit_extern_c;
1304
1305   /* TRUE if strings in expressions should be translated to the execution
1306      character set.  */
1307   bool translate_strings_p;
1308
1309   /* If non-NULL, then we are parsing a construct where new type
1310      definitions are not permitted.  The string stored here will be
1311      issued as an error message if a type is defined.  */
1312   const char *type_definition_forbidden_message;
1313
1314   /* A list of lists. The outer list is a stack, used for member
1315      functions of local classes. At each level there are two sub-list,
1316      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1317      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1318      TREE_VALUE's. The functions are chained in reverse declaration
1319      order.
1320
1321      The TREE_PURPOSE sublist contains those functions with default
1322      arguments that need post processing, and the TREE_VALUE sublist
1323      contains those functions with definitions that need post
1324      processing.
1325
1326      These lists can only be processed once the outermost class being
1327      defined is complete.  */
1328   tree unparsed_functions_queues;
1329
1330   /* The number of classes whose definitions are currently in
1331      progress.  */
1332   unsigned num_classes_being_defined;
1333
1334   /* The number of template parameter lists that apply directly to the
1335      current declaration.  */
1336   unsigned num_template_parameter_lists;
1337 } cp_parser;
1338
1339 /* The type of a function that parses some kind of expression.  */
1340 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1341
1342 /* Prototypes.  */
1343
1344 /* Constructors and destructors.  */
1345
1346 static cp_parser *cp_parser_new
1347   (void);
1348
1349 /* Routines to parse various constructs.
1350
1351    Those that return `tree' will return the error_mark_node (rather
1352    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1353    Sometimes, they will return an ordinary node if error-recovery was
1354    attempted, even though a parse error occurred.  So, to check
1355    whether or not a parse error occurred, you should always use
1356    cp_parser_error_occurred.  If the construct is optional (indicated
1357    either by an `_opt' in the name of the function that does the
1358    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1359    the construct is not present.  */
1360
1361 /* Lexical conventions [gram.lex]  */
1362
1363 static tree cp_parser_identifier
1364   (cp_parser *);
1365 static tree cp_parser_string_literal
1366   (cp_parser *, bool, bool);
1367
1368 /* Basic concepts [gram.basic]  */
1369
1370 static bool cp_parser_translation_unit
1371   (cp_parser *);
1372
1373 /* Expressions [gram.expr]  */
1374
1375 static tree cp_parser_primary_expression
1376   (cp_parser *, cp_id_kind *, tree *);
1377 static tree cp_parser_id_expression
1378   (cp_parser *, bool, bool, bool *, bool);
1379 static tree cp_parser_unqualified_id
1380   (cp_parser *, bool, bool, bool);
1381 static tree cp_parser_nested_name_specifier_opt
1382   (cp_parser *, bool, bool, bool, bool);
1383 static tree cp_parser_nested_name_specifier
1384   (cp_parser *, bool, bool, bool, bool);
1385 static tree cp_parser_class_or_namespace_name
1386   (cp_parser *, bool, bool, bool, bool, bool);
1387 static tree cp_parser_postfix_expression
1388   (cp_parser *, bool);
1389 static tree cp_parser_postfix_open_square_expression
1390   (cp_parser *, tree, bool);
1391 static tree cp_parser_postfix_dot_deref_expression
1392   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1393 static tree cp_parser_parenthesized_expression_list
1394   (cp_parser *, bool, bool *);
1395 static void cp_parser_pseudo_destructor_name
1396   (cp_parser *, tree *, tree *);
1397 static tree cp_parser_unary_expression
1398   (cp_parser *, bool);
1399 static enum tree_code cp_parser_unary_operator
1400   (cp_token *);
1401 static tree cp_parser_new_expression
1402   (cp_parser *);
1403 static tree cp_parser_new_placement
1404   (cp_parser *);
1405 static tree cp_parser_new_type_id
1406   (cp_parser *, tree *);
1407 static cp_declarator *cp_parser_new_declarator_opt
1408   (cp_parser *);
1409 static cp_declarator *cp_parser_direct_new_declarator
1410   (cp_parser *);
1411 static tree cp_parser_new_initializer
1412   (cp_parser *);
1413 static tree cp_parser_delete_expression
1414   (cp_parser *);
1415 static tree cp_parser_cast_expression
1416   (cp_parser *, bool);
1417 static tree cp_parser_binary_expression
1418   (cp_parser *);
1419 static tree cp_parser_question_colon_clause
1420   (cp_parser *, tree);
1421 static tree cp_parser_assignment_expression
1422   (cp_parser *);
1423 static enum tree_code cp_parser_assignment_operator_opt
1424   (cp_parser *);
1425 static tree cp_parser_expression
1426   (cp_parser *);
1427 static tree cp_parser_constant_expression
1428   (cp_parser *, bool, bool *);
1429 static tree cp_parser_builtin_offsetof
1430   (cp_parser *);
1431
1432 /* Statements [gram.stmt.stmt]  */
1433
1434 static void cp_parser_statement
1435   (cp_parser *, tree);
1436 static tree cp_parser_labeled_statement
1437   (cp_parser *, tree);
1438 static tree cp_parser_expression_statement
1439   (cp_parser *, tree);
1440 static tree cp_parser_compound_statement
1441   (cp_parser *, tree, bool);
1442 static void cp_parser_statement_seq_opt
1443   (cp_parser *, tree);
1444 static tree cp_parser_selection_statement
1445   (cp_parser *);
1446 static tree cp_parser_condition
1447   (cp_parser *);
1448 static tree cp_parser_iteration_statement
1449   (cp_parser *);
1450 static void cp_parser_for_init_statement
1451   (cp_parser *);
1452 static tree cp_parser_jump_statement
1453   (cp_parser *);
1454 static void cp_parser_declaration_statement
1455   (cp_parser *);
1456
1457 static tree cp_parser_implicitly_scoped_statement
1458   (cp_parser *);
1459 static void cp_parser_already_scoped_statement
1460   (cp_parser *);
1461
1462 /* Declarations [gram.dcl.dcl] */
1463
1464 static void cp_parser_declaration_seq_opt
1465   (cp_parser *);
1466 static void cp_parser_declaration
1467   (cp_parser *);
1468 static void cp_parser_block_declaration
1469   (cp_parser *, bool);
1470 static void cp_parser_simple_declaration
1471   (cp_parser *, bool);
1472 static void cp_parser_decl_specifier_seq
1473   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1474 static tree cp_parser_storage_class_specifier_opt
1475   (cp_parser *);
1476 static tree cp_parser_function_specifier_opt
1477   (cp_parser *, cp_decl_specifier_seq *);
1478 static tree cp_parser_type_specifier
1479   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1480    int *, bool *);
1481 static tree cp_parser_simple_type_specifier
1482   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1483 static tree cp_parser_type_name
1484   (cp_parser *);
1485 static tree cp_parser_elaborated_type_specifier
1486   (cp_parser *, bool, bool);
1487 static tree cp_parser_enum_specifier
1488   (cp_parser *);
1489 static void cp_parser_enumerator_list
1490   (cp_parser *, tree);
1491 static void cp_parser_enumerator_definition
1492   (cp_parser *, tree);
1493 static tree cp_parser_namespace_name
1494   (cp_parser *);
1495 static void cp_parser_namespace_definition
1496   (cp_parser *);
1497 static void cp_parser_namespace_body
1498   (cp_parser *);
1499 static tree cp_parser_qualified_namespace_specifier
1500   (cp_parser *);
1501 static void cp_parser_namespace_alias_definition
1502   (cp_parser *);
1503 static void cp_parser_using_declaration
1504   (cp_parser *);
1505 static void cp_parser_using_directive
1506   (cp_parser *);
1507 static void cp_parser_asm_definition
1508   (cp_parser *);
1509 static void cp_parser_linkage_specification
1510   (cp_parser *);
1511
1512 /* Declarators [gram.dcl.decl] */
1513
1514 static tree cp_parser_init_declarator
1515   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1516 static cp_declarator *cp_parser_declarator
1517   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1518 static cp_declarator *cp_parser_direct_declarator
1519   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1520 static enum tree_code cp_parser_ptr_operator
1521   (cp_parser *, tree *, cp_cv_quals *);
1522 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1523   (cp_parser *);
1524 static tree cp_parser_declarator_id
1525   (cp_parser *);
1526 static tree cp_parser_type_id
1527   (cp_parser *);
1528 static void cp_parser_type_specifier_seq
1529   (cp_parser *, cp_decl_specifier_seq *);
1530 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1531   (cp_parser *);
1532 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1533   (cp_parser *, bool *);
1534 static cp_parameter_declarator *cp_parser_parameter_declaration
1535   (cp_parser *, bool, bool *);
1536 static void cp_parser_function_body
1537   (cp_parser *);
1538 static tree cp_parser_initializer
1539   (cp_parser *, bool *, bool *);
1540 static tree cp_parser_initializer_clause
1541   (cp_parser *, bool *);
1542 static tree cp_parser_initializer_list
1543   (cp_parser *, bool *);
1544
1545 static bool cp_parser_ctor_initializer_opt_and_function_body
1546   (cp_parser *);
1547
1548 /* Classes [gram.class] */
1549
1550 static tree cp_parser_class_name
1551   (cp_parser *, bool, bool, bool, bool, bool, bool);
1552 static tree cp_parser_class_specifier
1553   (cp_parser *);
1554 static tree cp_parser_class_head
1555   (cp_parser *, bool *, tree *);
1556 static enum tag_types cp_parser_class_key
1557   (cp_parser *);
1558 static void cp_parser_member_specification_opt
1559   (cp_parser *);
1560 static void cp_parser_member_declaration
1561   (cp_parser *);
1562 static tree cp_parser_pure_specifier
1563   (cp_parser *);
1564 static tree cp_parser_constant_initializer
1565   (cp_parser *);
1566
1567 /* Derived classes [gram.class.derived] */
1568
1569 static tree cp_parser_base_clause
1570   (cp_parser *);
1571 static tree cp_parser_base_specifier
1572   (cp_parser *);
1573
1574 /* Special member functions [gram.special] */
1575
1576 static tree cp_parser_conversion_function_id
1577   (cp_parser *);
1578 static tree cp_parser_conversion_type_id
1579   (cp_parser *);
1580 static cp_declarator *cp_parser_conversion_declarator_opt
1581   (cp_parser *);
1582 static bool cp_parser_ctor_initializer_opt
1583   (cp_parser *);
1584 static void cp_parser_mem_initializer_list
1585   (cp_parser *);
1586 static tree cp_parser_mem_initializer
1587   (cp_parser *);
1588 static tree cp_parser_mem_initializer_id
1589   (cp_parser *);
1590
1591 /* Overloading [gram.over] */
1592
1593 static tree cp_parser_operator_function_id
1594   (cp_parser *);
1595 static tree cp_parser_operator
1596   (cp_parser *);
1597
1598 /* Templates [gram.temp] */
1599
1600 static void cp_parser_template_declaration
1601   (cp_parser *, bool);
1602 static tree cp_parser_template_parameter_list
1603   (cp_parser *);
1604 static tree cp_parser_template_parameter
1605   (cp_parser *, bool *);
1606 static tree cp_parser_type_parameter
1607   (cp_parser *);
1608 static tree cp_parser_template_id
1609   (cp_parser *, bool, bool, bool);
1610 static tree cp_parser_template_name
1611   (cp_parser *, bool, bool, bool, bool *);
1612 static tree cp_parser_template_argument_list
1613   (cp_parser *);
1614 static tree cp_parser_template_argument
1615   (cp_parser *);
1616 static void cp_parser_explicit_instantiation
1617   (cp_parser *);
1618 static void cp_parser_explicit_specialization
1619   (cp_parser *);
1620
1621 /* Exception handling [gram.exception] */
1622
1623 static tree cp_parser_try_block
1624   (cp_parser *);
1625 static bool cp_parser_function_try_block
1626   (cp_parser *);
1627 static void cp_parser_handler_seq
1628   (cp_parser *);
1629 static void cp_parser_handler
1630   (cp_parser *);
1631 static tree cp_parser_exception_declaration
1632   (cp_parser *);
1633 static tree cp_parser_throw_expression
1634   (cp_parser *);
1635 static tree cp_parser_exception_specification_opt
1636   (cp_parser *);
1637 static tree cp_parser_type_id_list
1638   (cp_parser *);
1639
1640 /* GNU Extensions */
1641
1642 static tree cp_parser_asm_specification_opt
1643   (cp_parser *);
1644 static tree cp_parser_asm_operand_list
1645   (cp_parser *);
1646 static tree cp_parser_asm_clobber_list
1647   (cp_parser *);
1648 static tree cp_parser_attributes_opt
1649   (cp_parser *);
1650 static tree cp_parser_attribute_list
1651   (cp_parser *);
1652 static bool cp_parser_extension_opt
1653   (cp_parser *, int *);
1654 static void cp_parser_label_declaration
1655   (cp_parser *);
1656
1657 /* Utility Routines */
1658
1659 static tree cp_parser_lookup_name
1660   (cp_parser *, tree, bool, bool, bool, bool, bool *);
1661 static tree cp_parser_lookup_name_simple
1662   (cp_parser *, tree);
1663 static tree cp_parser_maybe_treat_template_as_class
1664   (tree, bool);
1665 static bool cp_parser_check_declarator_template_parameters
1666   (cp_parser *, cp_declarator *);
1667 static bool cp_parser_check_template_parameters
1668   (cp_parser *, unsigned);
1669 static tree cp_parser_simple_cast_expression
1670   (cp_parser *);
1671 static tree cp_parser_global_scope_opt
1672   (cp_parser *, bool);
1673 static bool cp_parser_constructor_declarator_p
1674   (cp_parser *, bool);
1675 static tree cp_parser_function_definition_from_specifiers_and_declarator
1676   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1677 static tree cp_parser_function_definition_after_declarator
1678   (cp_parser *, bool);
1679 static void cp_parser_template_declaration_after_export
1680   (cp_parser *, bool);
1681 static tree cp_parser_single_declaration
1682   (cp_parser *, bool, bool *);
1683 static tree cp_parser_functional_cast
1684   (cp_parser *, tree);
1685 static tree cp_parser_save_member_function_body
1686   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1687 static tree cp_parser_enclosed_template_argument_list
1688   (cp_parser *);
1689 static void cp_parser_save_default_args
1690   (cp_parser *, tree);
1691 static void cp_parser_late_parsing_for_member
1692   (cp_parser *, tree);
1693 static void cp_parser_late_parsing_default_args
1694   (cp_parser *, tree);
1695 static tree cp_parser_sizeof_operand
1696   (cp_parser *, enum rid);
1697 static bool cp_parser_declares_only_class_p
1698   (cp_parser *);
1699 static void cp_parser_set_storage_class
1700   (cp_decl_specifier_seq *, cp_storage_class);
1701 static void cp_parser_set_decl_spec_type
1702   (cp_decl_specifier_seq *, tree, bool);
1703 static bool cp_parser_friend_p
1704   (const cp_decl_specifier_seq *);
1705 static cp_token *cp_parser_require
1706   (cp_parser *, enum cpp_ttype, const char *);
1707 static cp_token *cp_parser_require_keyword
1708   (cp_parser *, enum rid, const char *);
1709 static bool cp_parser_token_starts_function_definition_p
1710   (cp_token *);
1711 static bool cp_parser_next_token_starts_class_definition_p
1712   (cp_parser *);
1713 static bool cp_parser_next_token_ends_template_argument_p
1714   (cp_parser *);
1715 static bool cp_parser_nth_token_starts_template_argument_list_p
1716   (cp_parser *, size_t);
1717 static enum tag_types cp_parser_token_is_class_key
1718   (cp_token *);
1719 static void cp_parser_check_class_key
1720   (enum tag_types, tree type);
1721 static void cp_parser_check_access_in_redeclaration
1722   (tree type);
1723 static bool cp_parser_optional_template_keyword
1724   (cp_parser *);
1725 static void cp_parser_pre_parsed_nested_name_specifier
1726   (cp_parser *);
1727 static void cp_parser_cache_group
1728   (cp_parser *, enum cpp_ttype, unsigned);
1729 static void cp_parser_parse_tentatively
1730   (cp_parser *);
1731 static void cp_parser_commit_to_tentative_parse
1732   (cp_parser *);
1733 static void cp_parser_abort_tentative_parse
1734   (cp_parser *);
1735 static bool cp_parser_parse_definitely
1736   (cp_parser *);
1737 static inline bool cp_parser_parsing_tentatively
1738   (cp_parser *);
1739 static bool cp_parser_committed_to_tentative_parse
1740   (cp_parser *);
1741 static void cp_parser_error
1742   (cp_parser *, const char *);
1743 static void cp_parser_name_lookup_error
1744   (cp_parser *, tree, tree, const char *);
1745 static bool cp_parser_simulate_error
1746   (cp_parser *);
1747 static void cp_parser_check_type_definition
1748   (cp_parser *);
1749 static void cp_parser_check_for_definition_in_return_type
1750   (cp_declarator *, int);
1751 static void cp_parser_check_for_invalid_template_id
1752   (cp_parser *, tree);
1753 static bool cp_parser_non_integral_constant_expression
1754   (cp_parser *, const char *);
1755 static void cp_parser_diagnose_invalid_type_name
1756   (cp_parser *, tree, tree);
1757 static bool cp_parser_parse_and_diagnose_invalid_type_name
1758   (cp_parser *);
1759 static int cp_parser_skip_to_closing_parenthesis
1760   (cp_parser *, bool, bool, bool);
1761 static void cp_parser_skip_to_end_of_statement
1762   (cp_parser *);
1763 static void cp_parser_consume_semicolon_at_end_of_statement
1764   (cp_parser *);
1765 static void cp_parser_skip_to_end_of_block_or_statement
1766   (cp_parser *);
1767 static void cp_parser_skip_to_closing_brace
1768   (cp_parser *);
1769 static void cp_parser_skip_until_found
1770   (cp_parser *, enum cpp_ttype, const char *);
1771 static bool cp_parser_error_occurred
1772   (cp_parser *);
1773 static bool cp_parser_allow_gnu_extensions_p
1774   (cp_parser *);
1775 static bool cp_parser_is_string_literal
1776   (cp_token *);
1777 static bool cp_parser_is_keyword
1778   (cp_token *, enum rid);
1779 static tree cp_parser_make_typename_type
1780   (cp_parser *, tree, tree);
1781
1782 /* Returns nonzero if we are parsing tentatively.  */
1783
1784 static inline bool
1785 cp_parser_parsing_tentatively (cp_parser* parser)
1786 {
1787   return parser->context->next != NULL;
1788 }
1789
1790 /* Returns nonzero if TOKEN is a string literal.  */
1791
1792 static bool
1793 cp_parser_is_string_literal (cp_token* token)
1794 {
1795   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1796 }
1797
1798 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1799
1800 static bool
1801 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1802 {
1803   return token->keyword == keyword;
1804 }
1805
1806 /* If not parsing tentatively, issue a diagnostic of the form
1807       FILE:LINE: MESSAGE before TOKEN
1808    where TOKEN is the next token in the input stream.  MESSAGE
1809    (specified by the caller) is usually of the form "expected
1810    OTHER-TOKEN".  */
1811
1812 static void
1813 cp_parser_error (cp_parser* parser, const char* message)
1814 {
1815   if (!cp_parser_simulate_error (parser))
1816     {
1817       cp_token *token = cp_lexer_peek_token (parser->lexer);
1818       /* This diagnostic makes more sense if it is tagged to the line
1819          of the token we just peeked at.  */
1820       cp_lexer_set_source_position_from_token (token);
1821       c_parse_error (message,
1822                      /* Because c_parser_error does not understand
1823                         CPP_KEYWORD, keywords are treated like
1824                         identifiers.  */
1825                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1826                      token->value);
1827     }
1828 }
1829
1830 /* Issue an error about name-lookup failing.  NAME is the
1831    IDENTIFIER_NODE DECL is the result of
1832    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1833    the thing that we hoped to find.  */
1834
1835 static void
1836 cp_parser_name_lookup_error (cp_parser* parser,
1837                              tree name,
1838                              tree decl,
1839                              const char* desired)
1840 {
1841   /* If name lookup completely failed, tell the user that NAME was not
1842      declared.  */
1843   if (decl == error_mark_node)
1844     {
1845       if (parser->scope && parser->scope != global_namespace)
1846         error ("%<%D::%D%> has not been declared",
1847                parser->scope, name);
1848       else if (parser->scope == global_namespace)
1849         error ("%<::%D%> has not been declared", name);
1850       else if (parser->object_scope 
1851                && !CLASS_TYPE_P (parser->object_scope))
1852         error ("request for member %qD in non-class type %qT",
1853                name, parser->object_scope);
1854       else if (parser->object_scope)
1855         error ("%<%T::%D%> has not been declared", 
1856                parser->object_scope, name);
1857       else
1858         error ("`%D' has not been declared", name);
1859     }
1860   else if (parser->scope && parser->scope != global_namespace)
1861     error ("%<%D::%D%> %s", parser->scope, name, desired);
1862   else if (parser->scope == global_namespace)
1863     error ("%<::%D%> %s", name, desired);
1864   else
1865     error ("%qD %s", name, desired);
1866 }
1867
1868 /* If we are parsing tentatively, remember that an error has occurred
1869    during this tentative parse.  Returns true if the error was
1870    simulated; false if a message should be issued by the caller.  */
1871
1872 static bool
1873 cp_parser_simulate_error (cp_parser* parser)
1874 {
1875   if (cp_parser_parsing_tentatively (parser)
1876       && !cp_parser_committed_to_tentative_parse (parser))
1877     {
1878       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1879       return true;
1880     }
1881   return false;
1882 }
1883
1884 /* This function is called when a type is defined.  If type
1885    definitions are forbidden at this point, an error message is
1886    issued.  */
1887
1888 static void
1889 cp_parser_check_type_definition (cp_parser* parser)
1890 {
1891   /* If types are forbidden here, issue a message.  */
1892   if (parser->type_definition_forbidden_message)
1893     /* Use `%s' to print the string in case there are any escape
1894        characters in the message.  */
1895     error ("%s", parser->type_definition_forbidden_message);
1896 }
1897
1898 /* This function is called when a declaration is parsed.  If
1899    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1900    indicates that a type was defined in the decl-specifiers for DECL,
1901    then an error is issued.  */
1902
1903 static void
1904 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1905                                                int declares_class_or_enum)
1906 {
1907   /* [dcl.fct] forbids type definitions in return types.
1908      Unfortunately, it's not easy to know whether or not we are
1909      processing a return type until after the fact.  */
1910   while (declarator
1911          && (declarator->kind == cdk_pointer
1912              || declarator->kind == cdk_reference
1913              || declarator->kind == cdk_ptrmem))
1914     declarator = declarator->declarator;
1915   if (declarator
1916       && declarator->kind == cdk_function
1917       && declares_class_or_enum & 2)
1918     error ("new types may not be defined in a return type");
1919 }
1920
1921 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1922    "<" in any valid C++ program.  If the next token is indeed "<",
1923    issue a message warning the user about what appears to be an
1924    invalid attempt to form a template-id.  */
1925
1926 static void
1927 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1928                                          tree type)
1929 {
1930   cp_token_position start = 0;
1931
1932   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1933     {
1934       if (TYPE_P (type))
1935         error ("%qT is not a template", type);
1936       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1937         error ("%qE is not a template", type);
1938       else
1939         error ("invalid template-id");
1940       /* Remember the location of the invalid "<".  */
1941       if (cp_parser_parsing_tentatively (parser)
1942           && !cp_parser_committed_to_tentative_parse (parser))
1943         start = cp_lexer_token_position (parser->lexer, true);
1944       /* Consume the "<".  */
1945       cp_lexer_consume_token (parser->lexer);
1946       /* Parse the template arguments.  */
1947       cp_parser_enclosed_template_argument_list (parser);
1948       /* Permanently remove the invalid template arguments so that
1949          this error message is not issued again.  */
1950       if (start)
1951         cp_lexer_purge_tokens_after (parser->lexer, start);
1952     }
1953 }
1954
1955 /* If parsing an integral constant-expression, issue an error message
1956    about the fact that THING appeared and return true.  Otherwise,
1957    return false, marking the current expression as non-constant.  */
1958
1959 static bool
1960 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1961                                             const char *thing)
1962 {
1963   if (parser->integral_constant_expression_p)
1964     {
1965       if (!parser->allow_non_integral_constant_expression_p)
1966         {
1967           error ("%s cannot appear in a constant-expression", thing);
1968           return true;
1969         }
1970       parser->non_integral_constant_expression_p = true;
1971     }
1972   return false;
1973 }
1974
1975 /* Emit a diagnostic for an invalid type name. Consider also if it is
1976    qualified or not and the result of a lookup, to provide a better
1977    message.  */
1978
1979 static void
1980 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1981 {
1982   tree decl, old_scope;
1983   /* Try to lookup the identifier.  */
1984   old_scope = parser->scope;
1985   parser->scope = scope;
1986   decl = cp_parser_lookup_name_simple (parser, id);
1987   parser->scope = old_scope;
1988   /* If the lookup found a template-name, it means that the user forgot
1989   to specify an argument list. Emit an useful error message.  */
1990   if (TREE_CODE (decl) == TEMPLATE_DECL)
1991     error ("invalid use of template-name %qE without an argument list",
1992       decl);
1993   else if (!parser->scope)
1994     {
1995       /* Issue an error message.  */
1996       error ("%qE does not name a type", id);
1997       /* If we're in a template class, it's possible that the user was
1998          referring to a type from a base class.  For example:
1999
2000            template <typename T> struct A { typedef T X; };
2001            template <typename T> struct B : public A<T> { X x; };
2002
2003          The user should have said "typename A<T>::X".  */
2004       if (processing_template_decl && current_class_type)
2005         {
2006           tree b;
2007
2008           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2009                b;
2010                b = TREE_CHAIN (b))
2011             {
2012               tree base_type = BINFO_TYPE (b);
2013               if (CLASS_TYPE_P (base_type)
2014                   && dependent_type_p (base_type))
2015                 {
2016                   tree field;
2017                   /* Go from a particular instantiation of the
2018                      template (which will have an empty TYPE_FIELDs),
2019                      to the main version.  */
2020                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2021                   for (field = TYPE_FIELDS (base_type);
2022                        field;
2023                        field = TREE_CHAIN (field))
2024                     if (TREE_CODE (field) == TYPE_DECL
2025                         && DECL_NAME (field) == id)
2026                       {
2027                         inform ("(perhaps `typename %T::%E' was intended)",
2028                                 BINFO_TYPE (b), id);
2029                         break;
2030                       }
2031                   if (field)
2032                     break;
2033                 }
2034             }
2035         }
2036     }
2037   /* Here we diagnose qualified-ids where the scope is actually correct,
2038      but the identifier does not resolve to a valid type name.  */
2039   else
2040     {
2041       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2042         error ("%qE in namespace %qE does not name a type",
2043                id, parser->scope);
2044       else if (TYPE_P (parser->scope))
2045         error ("q%E in class %qT does not name a type", id, parser->scope);
2046       else
2047         gcc_unreachable ();
2048     }
2049 }
2050
2051 /* Check for a common situation where a type-name should be present,
2052    but is not, and issue a sensible error message.  Returns true if an
2053    invalid type-name was detected.
2054
2055    The situation handled by this function are variable declarations of the
2056    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2057    Usually, `ID' should name a type, but if we got here it means that it
2058    does not. We try to emit the best possible error message depending on
2059    how exactly the id-expression looks like.
2060 */
2061
2062 static bool
2063 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2064 {
2065   tree id;
2066
2067   cp_parser_parse_tentatively (parser);
2068   id = cp_parser_id_expression (parser,
2069                                 /*template_keyword_p=*/false,
2070                                 /*check_dependency_p=*/true,
2071                                 /*template_p=*/NULL,
2072                                 /*declarator_p=*/true);
2073   /* After the id-expression, there should be a plain identifier,
2074      otherwise this is not a simple variable declaration. Also, if
2075      the scope is dependent, we cannot do much.  */
2076   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2077       || (parser->scope && TYPE_P (parser->scope)
2078           && dependent_type_p (parser->scope)))
2079     {
2080       cp_parser_abort_tentative_parse (parser);
2081       return false;
2082     }
2083   if (!cp_parser_parse_definitely (parser)
2084       || TREE_CODE (id) != IDENTIFIER_NODE)
2085     return false;
2086
2087   /* Emit a diagnostic for the invalid type.  */
2088   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2089   /* Skip to the end of the declaration; there's no point in
2090      trying to process it.  */
2091   cp_parser_skip_to_end_of_block_or_statement (parser);
2092   return true;
2093 }
2094
2095 /* Consume tokens up to, and including, the next non-nested closing `)'.
2096    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2097    are doing error recovery. Returns -1 if OR_COMMA is true and we
2098    found an unnested comma.  */
2099
2100 static int
2101 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2102                                        bool recovering,
2103                                        bool or_comma,
2104                                        bool consume_paren)
2105 {
2106   unsigned paren_depth = 0;
2107   unsigned brace_depth = 0;
2108   int result;
2109
2110   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2111       && !cp_parser_committed_to_tentative_parse (parser))
2112     return 0;
2113
2114   while (true)
2115     {
2116       cp_token *token;
2117
2118       /* If we've run out of tokens, then there is no closing `)'.  */
2119       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2120         {
2121           result = 0;
2122           break;
2123         }
2124
2125       token = cp_lexer_peek_token (parser->lexer);
2126
2127       /* This matches the processing in skip_to_end_of_statement.  */
2128       if (token->type == CPP_SEMICOLON && !brace_depth)
2129         {
2130           result = 0;
2131           break;
2132         }
2133       if (token->type == CPP_OPEN_BRACE)
2134         ++brace_depth;
2135       if (token->type == CPP_CLOSE_BRACE)
2136         {
2137           if (!brace_depth--)
2138             {
2139               result = 0;
2140               break;
2141             }
2142         }
2143       if (recovering && or_comma && token->type == CPP_COMMA
2144           && !brace_depth && !paren_depth)
2145         {
2146           result = -1;
2147           break;
2148         }
2149
2150       if (!brace_depth)
2151         {
2152           /* If it is an `(', we have entered another level of nesting.  */
2153           if (token->type == CPP_OPEN_PAREN)
2154             ++paren_depth;
2155           /* If it is a `)', then we might be done.  */
2156           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2157             {
2158               if (consume_paren)
2159                 cp_lexer_consume_token (parser->lexer);
2160               {
2161                 result = 1;
2162                 break;
2163               }
2164             }
2165         }
2166
2167       /* Consume the token.  */
2168       cp_lexer_consume_token (parser->lexer);
2169     }
2170
2171   return result;
2172 }
2173
2174 /* Consume tokens until we reach the end of the current statement.
2175    Normally, that will be just before consuming a `;'.  However, if a
2176    non-nested `}' comes first, then we stop before consuming that.  */
2177
2178 static void
2179 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2180 {
2181   unsigned nesting_depth = 0;
2182
2183   while (true)
2184     {
2185       cp_token *token;
2186
2187       /* Peek at the next token.  */
2188       token = cp_lexer_peek_token (parser->lexer);
2189       /* If we've run out of tokens, stop.  */
2190       if (token->type == CPP_EOF)
2191         break;
2192       /* If the next token is a `;', we have reached the end of the
2193          statement.  */
2194       if (token->type == CPP_SEMICOLON && !nesting_depth)
2195         break;
2196       /* If the next token is a non-nested `}', then we have reached
2197          the end of the current block.  */
2198       if (token->type == CPP_CLOSE_BRACE)
2199         {
2200           /* If this is a non-nested `}', stop before consuming it.
2201              That way, when confronted with something like:
2202
2203                { 3 + }
2204
2205              we stop before consuming the closing `}', even though we
2206              have not yet reached a `;'.  */
2207           if (nesting_depth == 0)
2208             break;
2209           /* If it is the closing `}' for a block that we have
2210              scanned, stop -- but only after consuming the token.
2211              That way given:
2212
2213                 void f g () { ... }
2214                 typedef int I;
2215
2216              we will stop after the body of the erroneously declared
2217              function, but before consuming the following `typedef'
2218              declaration.  */
2219           if (--nesting_depth == 0)
2220             {
2221               cp_lexer_consume_token (parser->lexer);
2222               break;
2223             }
2224         }
2225       /* If it the next token is a `{', then we are entering a new
2226          block.  Consume the entire block.  */
2227       else if (token->type == CPP_OPEN_BRACE)
2228         ++nesting_depth;
2229       /* Consume the token.  */
2230       cp_lexer_consume_token (parser->lexer);
2231     }
2232 }
2233
2234 /* This function is called at the end of a statement or declaration.
2235    If the next token is a semicolon, it is consumed; otherwise, error
2236    recovery is attempted.  */
2237
2238 static void
2239 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2240 {
2241   /* Look for the trailing `;'.  */
2242   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2243     {
2244       /* If there is additional (erroneous) input, skip to the end of
2245          the statement.  */
2246       cp_parser_skip_to_end_of_statement (parser);
2247       /* If the next token is now a `;', consume it.  */
2248       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2249         cp_lexer_consume_token (parser->lexer);
2250     }
2251 }
2252
2253 /* Skip tokens until we have consumed an entire block, or until we
2254    have consumed a non-nested `;'.  */
2255
2256 static void
2257 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2258 {
2259   unsigned nesting_depth = 0;
2260
2261   while (true)
2262     {
2263       cp_token *token;
2264
2265       /* Peek at the next token.  */
2266       token = cp_lexer_peek_token (parser->lexer);
2267       /* If we've run out of tokens, stop.  */
2268       if (token->type == CPP_EOF)
2269         break;
2270       /* If the next token is a `;', we have reached the end of the
2271          statement.  */
2272       if (token->type == CPP_SEMICOLON && !nesting_depth)
2273         {
2274           /* Consume the `;'.  */
2275           cp_lexer_consume_token (parser->lexer);
2276           break;
2277         }
2278       /* Consume the token.  */
2279       token = cp_lexer_consume_token (parser->lexer);
2280       /* If the next token is a non-nested `}', then we have reached
2281          the end of the current block.  */
2282       if (token->type == CPP_CLOSE_BRACE
2283           && (nesting_depth == 0 || --nesting_depth == 0))
2284         break;
2285       /* If it the next token is a `{', then we are entering a new
2286          block.  Consume the entire block.  */
2287       if (token->type == CPP_OPEN_BRACE)
2288         ++nesting_depth;
2289     }
2290 }
2291
2292 /* Skip tokens until a non-nested closing curly brace is the next
2293    token.  */
2294
2295 static void
2296 cp_parser_skip_to_closing_brace (cp_parser *parser)
2297 {
2298   unsigned nesting_depth = 0;
2299
2300   while (true)
2301     {
2302       cp_token *token;
2303
2304       /* Peek at the next token.  */
2305       token = cp_lexer_peek_token (parser->lexer);
2306       /* If we've run out of tokens, stop.  */
2307       if (token->type == CPP_EOF)
2308         break;
2309       /* If the next token is a non-nested `}', then we have reached
2310          the end of the current block.  */
2311       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2312         break;
2313       /* If it the next token is a `{', then we are entering a new
2314          block.  Consume the entire block.  */
2315       else if (token->type == CPP_OPEN_BRACE)
2316         ++nesting_depth;
2317       /* Consume the token.  */
2318       cp_lexer_consume_token (parser->lexer);
2319     }
2320 }
2321
2322 /* This is a simple wrapper around make_typename_type. When the id is
2323    an unresolved identifier node, we can provide a superior diagnostic
2324    using cp_parser_diagnose_invalid_type_name.  */
2325
2326 static tree
2327 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2328 {
2329   tree result;
2330   if (TREE_CODE (id) == IDENTIFIER_NODE)
2331     {
2332       result = make_typename_type (scope, id, /*complain=*/0);
2333       if (result == error_mark_node)
2334         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2335       return result;
2336     }
2337   return make_typename_type (scope, id, tf_error);
2338 }
2339
2340
2341 /* Create a new C++ parser.  */
2342
2343 static cp_parser *
2344 cp_parser_new (void)
2345 {
2346   cp_parser *parser;
2347   cp_lexer *lexer;
2348   unsigned i;
2349
2350   /* cp_lexer_new_main is called before calling ggc_alloc because
2351      cp_lexer_new_main might load a PCH file.  */
2352   lexer = cp_lexer_new_main ();
2353
2354   /* Initialize the binops_by_token so that we can get the tree
2355      directly from the token.  */
2356   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2357     binops_by_token[binops[i].token_type] = binops[i];
2358
2359   parser = GGC_CNEW (cp_parser);
2360   parser->lexer = lexer;
2361   parser->context = cp_parser_context_new (NULL);
2362
2363   /* For now, we always accept GNU extensions.  */
2364   parser->allow_gnu_extensions_p = 1;
2365
2366   /* The `>' token is a greater-than operator, not the end of a
2367      template-id.  */
2368   parser->greater_than_is_operator_p = true;
2369
2370   parser->default_arg_ok_p = true;
2371
2372   /* We are not parsing a constant-expression.  */
2373   parser->integral_constant_expression_p = false;
2374   parser->allow_non_integral_constant_expression_p = false;
2375   parser->non_integral_constant_expression_p = false;
2376
2377   /* Local variable names are not forbidden.  */
2378   parser->local_variables_forbidden_p = false;
2379
2380   /* We are not processing an `extern "C"' declaration.  */
2381   parser->in_unbraced_linkage_specification_p = false;
2382
2383   /* We are not processing a declarator.  */
2384   parser->in_declarator_p = false;
2385
2386   /* We are not processing a template-argument-list.  */
2387   parser->in_template_argument_list_p = false;
2388
2389   /* We are not in an iteration statement.  */
2390   parser->in_iteration_statement_p = false;
2391
2392   /* We are not in a switch statement.  */
2393   parser->in_switch_statement_p = false;
2394
2395   /* We are not parsing a type-id inside an expression.  */
2396   parser->in_type_id_in_expr_p = false;
2397
2398   /* Declarations aren't implicitly extern "C". */
2399   parser->implicit_extern_c = false;
2400
2401   /* String literals should be translated to the execution character set.  */
2402   parser->translate_strings_p = true;
2403
2404   /* The unparsed function queue is empty.  */
2405   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2406
2407   /* There are no classes being defined.  */
2408   parser->num_classes_being_defined = 0;
2409
2410   /* No template parameters apply.  */
2411   parser->num_template_parameter_lists = 0;
2412
2413   return parser;
2414 }
2415
2416 /* Create a cp_lexer structure which will emit the tokens in CACHE
2417    and push it onto the parser's lexer stack.  This is used for delayed
2418    parsing of in-class method bodies and default arguments, and should
2419    not be confused with tentative parsing.  */
2420 static void
2421 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2422 {
2423   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2424   lexer->next = parser->lexer;
2425   parser->lexer = lexer;
2426
2427   /* Move the current source position to that of the first token in the
2428      new lexer.  */
2429   cp_lexer_set_source_position_from_token (lexer->next_token);
2430 }
2431
2432 /* Pop the top lexer off the parser stack.  This is never used for the
2433    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2434 static void
2435 cp_parser_pop_lexer (cp_parser *parser)
2436 {
2437   cp_lexer *lexer = parser->lexer;
2438   parser->lexer = lexer->next;
2439   cp_lexer_destroy (lexer);
2440
2441   /* Put the current source position back where it was before this
2442      lexer was pushed.  */
2443   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2444 }
2445
2446 /* Lexical conventions [gram.lex]  */
2447
2448 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2449    identifier.  */
2450
2451 static tree
2452 cp_parser_identifier (cp_parser* parser)
2453 {
2454   cp_token *token;
2455
2456   /* Look for the identifier.  */
2457   token = cp_parser_require (parser, CPP_NAME, "identifier");
2458   /* Return the value.  */
2459   return token ? token->value : error_mark_node;
2460 }
2461
2462 /* Parse a sequence of adjacent string constants.  Returns a
2463    TREE_STRING representing the combined, nul-terminated string
2464    constant.  If TRANSLATE is true, translate the string to the
2465    execution character set.  If WIDE_OK is true, a wide string is
2466    invalid here.
2467
2468    C++98 [lex.string] says that if a narrow string literal token is
2469    adjacent to a wide string literal token, the behavior is undefined.
2470    However, C99 6.4.5p4 says that this results in a wide string literal.
2471    We follow C99 here, for consistency with the C front end.
2472
2473    This code is largely lifted from lex_string() in c-lex.c.
2474
2475    FUTURE: ObjC++ will need to handle @-strings here.  */
2476 static tree
2477 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2478 {
2479   tree value;
2480   bool wide = false;
2481   size_t count;
2482   struct obstack str_ob;
2483   cpp_string str, istr, *strs;
2484   cp_token *tok;
2485
2486   tok = cp_lexer_peek_token (parser->lexer);
2487   if (!cp_parser_is_string_literal (tok))
2488     {
2489       cp_parser_error (parser, "expected string-literal");
2490       return error_mark_node;
2491     }
2492
2493   /* Try to avoid the overhead of creating and destroying an obstack
2494      for the common case of just one string.  */
2495   if (!cp_parser_is_string_literal
2496       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2497     {
2498       cp_lexer_consume_token (parser->lexer);
2499
2500       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2501       str.len = TREE_STRING_LENGTH (tok->value);
2502       count = 1;
2503       if (tok->type == CPP_WSTRING)
2504         wide = true;
2505
2506       strs = &str;
2507     }
2508   else
2509     {
2510       gcc_obstack_init (&str_ob);
2511       count = 0;
2512
2513       do
2514         {
2515           cp_lexer_consume_token (parser->lexer);
2516           count++;
2517           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2518           str.len = TREE_STRING_LENGTH (tok->value);
2519           if (tok->type == CPP_WSTRING)
2520             wide = true;
2521
2522           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2523
2524           tok = cp_lexer_peek_token (parser->lexer);
2525         }
2526       while (cp_parser_is_string_literal (tok));
2527
2528       strs = (cpp_string *) obstack_finish (&str_ob);
2529     }
2530
2531   if (wide && !wide_ok)
2532     {
2533       cp_parser_error (parser, "a wide string is invalid in this context");
2534       wide = false;
2535     }
2536
2537   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2538       (parse_in, strs, count, &istr, wide))
2539     {
2540       value = build_string (istr.len, (char *)istr.text);
2541       free ((void *)istr.text);
2542
2543       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2544       value = fix_string_type (value);
2545     }
2546   else
2547     /* cpp_interpret_string has issued an error.  */
2548     value = error_mark_node;
2549
2550   if (count > 1)
2551     obstack_free (&str_ob, 0);
2552
2553   return value;
2554 }
2555
2556
2557 /* Basic concepts [gram.basic]  */
2558
2559 /* Parse a translation-unit.
2560
2561    translation-unit:
2562      declaration-seq [opt]
2563
2564    Returns TRUE if all went well.  */
2565
2566 static bool
2567 cp_parser_translation_unit (cp_parser* parser)
2568 {
2569   /* The address of the first non-permanent object on the declarator
2570      obstack.  */
2571   static void *declarator_obstack_base;
2572
2573   bool success;
2574
2575   /* Create the declarator obstack, if necessary.  */
2576   if (!cp_error_declarator)
2577     {
2578       gcc_obstack_init (&declarator_obstack);
2579       /* Create the error declarator.  */
2580       cp_error_declarator = make_declarator (cdk_error);
2581       /* Create the empty parameter list.  */
2582       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2583       /* Remember where the base of the declarator obstack lies.  */
2584       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2585     }
2586
2587   while (true)
2588     {
2589       cp_parser_declaration_seq_opt (parser);
2590
2591       /* If there are no tokens left then all went well.  */
2592       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2593         {
2594           /* Get rid of the token array; we don't need it any more. */
2595           cp_lexer_destroy (parser->lexer);
2596           parser->lexer = NULL;
2597
2598           /* This file might have been a context that's implicitly extern
2599              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2600           if (parser->implicit_extern_c)
2601             {
2602               pop_lang_context ();
2603               parser->implicit_extern_c = false;
2604             }
2605
2606           /* Finish up.  */
2607           finish_translation_unit ();
2608
2609           success = true;
2610           break;
2611         }
2612       else
2613         {
2614           cp_parser_error (parser, "expected declaration");
2615           success = false;
2616           break;
2617         }
2618     }
2619
2620   /* Make sure the declarator obstack was fully cleaned up.  */
2621   gcc_assert (obstack_next_free (&declarator_obstack)
2622               == declarator_obstack_base);
2623
2624   /* All went well.  */
2625   return success;
2626 }
2627
2628 /* Expressions [gram.expr] */
2629
2630 /* Parse a primary-expression.
2631
2632    primary-expression:
2633      literal
2634      this
2635      ( expression )
2636      id-expression
2637
2638    GNU Extensions:
2639
2640    primary-expression:
2641      ( compound-statement )
2642      __builtin_va_arg ( assignment-expression , type-id )
2643
2644    literal:
2645      __null
2646
2647    Returns a representation of the expression.
2648
2649    *IDK indicates what kind of id-expression (if any) was present.
2650
2651    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2652    used as the operand of a pointer-to-member.  In that case,
2653    *QUALIFYING_CLASS gives the class that is used as the qualifying
2654    class in the pointer-to-member.  */
2655
2656 static tree
2657 cp_parser_primary_expression (cp_parser *parser,
2658                               cp_id_kind *idk,
2659                               tree *qualifying_class)
2660 {
2661   cp_token *token;
2662
2663   /* Assume the primary expression is not an id-expression.  */
2664   *idk = CP_ID_KIND_NONE;
2665   /* And that it cannot be used as pointer-to-member.  */
2666   *qualifying_class = NULL_TREE;
2667
2668   /* Peek at the next token.  */
2669   token = cp_lexer_peek_token (parser->lexer);
2670   switch (token->type)
2671     {
2672       /* literal:
2673            integer-literal
2674            character-literal
2675            floating-literal
2676            string-literal
2677            boolean-literal  */
2678     case CPP_CHAR:
2679     case CPP_WCHAR:
2680     case CPP_NUMBER:
2681       token = cp_lexer_consume_token (parser->lexer);
2682       return token->value;
2683
2684     case CPP_STRING:
2685     case CPP_WSTRING:
2686       /* ??? Should wide strings be allowed when parser->translate_strings_p
2687          is false (i.e. in attributes)?  If not, we can kill the third
2688          argument to cp_parser_string_literal.  */
2689       return cp_parser_string_literal (parser,
2690                                        parser->translate_strings_p,
2691                                        true);
2692
2693     case CPP_OPEN_PAREN:
2694       {
2695         tree expr;
2696         bool saved_greater_than_is_operator_p;
2697
2698         /* Consume the `('.  */
2699         cp_lexer_consume_token (parser->lexer);
2700         /* Within a parenthesized expression, a `>' token is always
2701            the greater-than operator.  */
2702         saved_greater_than_is_operator_p
2703           = parser->greater_than_is_operator_p;
2704         parser->greater_than_is_operator_p = true;
2705         /* If we see `( { ' then we are looking at the beginning of
2706            a GNU statement-expression.  */
2707         if (cp_parser_allow_gnu_extensions_p (parser)
2708             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2709           {
2710             /* Statement-expressions are not allowed by the standard.  */
2711             if (pedantic)
2712               pedwarn ("ISO C++ forbids braced-groups within expressions");
2713
2714             /* And they're not allowed outside of a function-body; you
2715                cannot, for example, write:
2716
2717                  int i = ({ int j = 3; j + 1; });
2718
2719                at class or namespace scope.  */
2720             if (!at_function_scope_p ())
2721               error ("statement-expressions are allowed only inside functions");
2722             /* Start the statement-expression.  */
2723             expr = begin_stmt_expr ();
2724             /* Parse the compound-statement.  */
2725             cp_parser_compound_statement (parser, expr, false);
2726             /* Finish up.  */
2727             expr = finish_stmt_expr (expr, false);
2728           }
2729         else
2730           {
2731             /* Parse the parenthesized expression.  */
2732             expr = cp_parser_expression (parser);
2733             /* Let the front end know that this expression was
2734                enclosed in parentheses. This matters in case, for
2735                example, the expression is of the form `A::B', since
2736                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2737                not.  */
2738             finish_parenthesized_expr (expr);
2739           }
2740         /* The `>' token might be the end of a template-id or
2741            template-parameter-list now.  */
2742         parser->greater_than_is_operator_p
2743           = saved_greater_than_is_operator_p;
2744         /* Consume the `)'.  */
2745         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2746           cp_parser_skip_to_end_of_statement (parser);
2747
2748         return expr;
2749       }
2750
2751     case CPP_KEYWORD:
2752       switch (token->keyword)
2753         {
2754           /* These two are the boolean literals.  */
2755         case RID_TRUE:
2756           cp_lexer_consume_token (parser->lexer);
2757           return boolean_true_node;
2758         case RID_FALSE:
2759           cp_lexer_consume_token (parser->lexer);
2760           return boolean_false_node;
2761
2762           /* The `__null' literal.  */
2763         case RID_NULL:
2764           cp_lexer_consume_token (parser->lexer);
2765           return null_node;
2766
2767           /* Recognize the `this' keyword.  */
2768         case RID_THIS:
2769           cp_lexer_consume_token (parser->lexer);
2770           if (parser->local_variables_forbidden_p)
2771             {
2772               error ("%<this%> may not be used in this context");
2773               return error_mark_node;
2774             }
2775           /* Pointers cannot appear in constant-expressions.  */
2776           if (cp_parser_non_integral_constant_expression (parser,
2777                                                           "`this'"))
2778             return error_mark_node;
2779           return finish_this_expr ();
2780
2781           /* The `operator' keyword can be the beginning of an
2782              id-expression.  */
2783         case RID_OPERATOR:
2784           goto id_expression;
2785
2786         case RID_FUNCTION_NAME:
2787         case RID_PRETTY_FUNCTION_NAME:
2788         case RID_C99_FUNCTION_NAME:
2789           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2790              __func__ are the names of variables -- but they are
2791              treated specially.  Therefore, they are handled here,
2792              rather than relying on the generic id-expression logic
2793              below.  Grammatically, these names are id-expressions.
2794
2795              Consume the token.  */
2796           token = cp_lexer_consume_token (parser->lexer);
2797           /* Look up the name.  */
2798           return finish_fname (token->value);
2799
2800         case RID_VA_ARG:
2801           {
2802             tree expression;
2803             tree type;
2804
2805             /* The `__builtin_va_arg' construct is used to handle
2806                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2807             cp_lexer_consume_token (parser->lexer);
2808             /* Look for the opening `('.  */
2809             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2810             /* Now, parse the assignment-expression.  */
2811             expression = cp_parser_assignment_expression (parser);
2812             /* Look for the `,'.  */
2813             cp_parser_require (parser, CPP_COMMA, "`,'");
2814             /* Parse the type-id.  */
2815             type = cp_parser_type_id (parser);
2816             /* Look for the closing `)'.  */
2817             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2818             /* Using `va_arg' in a constant-expression is not
2819                allowed.  */
2820             if (cp_parser_non_integral_constant_expression (parser,
2821                                                             "`va_arg'"))
2822               return error_mark_node;
2823             return build_x_va_arg (expression, type);
2824           }
2825
2826         case RID_OFFSETOF:
2827           return cp_parser_builtin_offsetof (parser);
2828
2829         default:
2830           cp_parser_error (parser, "expected primary-expression");
2831           return error_mark_node;
2832         }
2833
2834       /* An id-expression can start with either an identifier, a
2835          `::' as the beginning of a qualified-id, or the "operator"
2836          keyword.  */
2837     case CPP_NAME:
2838     case CPP_SCOPE:
2839     case CPP_TEMPLATE_ID:
2840     case CPP_NESTED_NAME_SPECIFIER:
2841       {
2842         tree id_expression;
2843         tree decl;
2844         const char *error_msg;
2845
2846       id_expression:
2847         /* Parse the id-expression.  */
2848         id_expression
2849           = cp_parser_id_expression (parser,
2850                                      /*template_keyword_p=*/false,
2851                                      /*check_dependency_p=*/true,
2852                                      /*template_p=*/NULL,
2853                                      /*declarator_p=*/false);
2854         if (id_expression == error_mark_node)
2855           return error_mark_node;
2856         /* If we have a template-id, then no further lookup is
2857            required.  If the template-id was for a template-class, we
2858            will sometimes have a TYPE_DECL at this point.  */
2859         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2860             || TREE_CODE (id_expression) == TYPE_DECL)
2861           decl = id_expression;
2862         /* Look up the name.  */
2863         else
2864           {
2865             bool ambiguous_p;
2866
2867             decl = cp_parser_lookup_name (parser, id_expression,
2868                                           /*is_type=*/false,
2869                                           /*is_template=*/false,
2870                                           /*is_namespace=*/false,
2871                                           /*check_dependency=*/true,
2872                                           &ambiguous_p);
2873             /* If the lookup was ambiguous, an error will already have
2874                been issued.  */
2875             if (ambiguous_p)
2876               return error_mark_node;
2877             /* If name lookup gives us a SCOPE_REF, then the
2878                qualifying scope was dependent.  Just propagate the
2879                name.  */
2880             if (TREE_CODE (decl) == SCOPE_REF)
2881               {
2882                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2883                   *qualifying_class = TREE_OPERAND (decl, 0);
2884                 return decl;
2885               }
2886             /* Check to see if DECL is a local variable in a context
2887                where that is forbidden.  */
2888             if (parser->local_variables_forbidden_p
2889                 && local_variable_p (decl))
2890               {
2891                 /* It might be that we only found DECL because we are
2892                    trying to be generous with pre-ISO scoping rules.
2893                    For example, consider:
2894
2895                      int i;
2896                      void g() {
2897                        for (int i = 0; i < 10; ++i) {}
2898                        extern void f(int j = i);
2899                      }
2900
2901                    Here, name look up will originally find the out
2902                    of scope `i'.  We need to issue a warning message,
2903                    but then use the global `i'.  */
2904                 decl = check_for_out_of_scope_variable (decl);
2905                 if (local_variable_p (decl))
2906                   {
2907                     error ("local variable %qD may not appear in this context",
2908                            decl);
2909                     return error_mark_node;
2910                   }
2911               }
2912           }
2913
2914         decl = finish_id_expression (id_expression, decl, parser->scope,
2915                                      idk, qualifying_class,
2916                                      parser->integral_constant_expression_p,
2917                                      parser->allow_non_integral_constant_expression_p,
2918                                      &parser->non_integral_constant_expression_p,
2919                                      &error_msg);
2920         if (error_msg)
2921           cp_parser_error (parser, error_msg);
2922         return decl;
2923       }
2924
2925       /* Anything else is an error.  */
2926     default:
2927       cp_parser_error (parser, "expected primary-expression");
2928       return error_mark_node;
2929     }
2930 }
2931
2932 /* Parse an id-expression.
2933
2934    id-expression:
2935      unqualified-id
2936      qualified-id
2937
2938    qualified-id:
2939      :: [opt] nested-name-specifier template [opt] unqualified-id
2940      :: identifier
2941      :: operator-function-id
2942      :: template-id
2943
2944    Return a representation of the unqualified portion of the
2945    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2946    a `::' or nested-name-specifier.
2947
2948    Often, if the id-expression was a qualified-id, the caller will
2949    want to make a SCOPE_REF to represent the qualified-id.  This
2950    function does not do this in order to avoid wastefully creating
2951    SCOPE_REFs when they are not required.
2952
2953    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2954    `template' keyword.
2955
2956    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2957    uninstantiated templates.
2958
2959    If *TEMPLATE_P is non-NULL, it is set to true iff the
2960    `template' keyword is used to explicitly indicate that the entity
2961    named is a template.
2962
2963    If DECLARATOR_P is true, the id-expression is appearing as part of
2964    a declarator, rather than as part of an expression.  */
2965
2966 static tree
2967 cp_parser_id_expression (cp_parser *parser,
2968                          bool template_keyword_p,
2969                          bool check_dependency_p,
2970                          bool *template_p,
2971                          bool declarator_p)
2972 {
2973   bool global_scope_p;
2974   bool nested_name_specifier_p;
2975
2976   /* Assume the `template' keyword was not used.  */
2977   if (template_p)
2978     *template_p = false;
2979
2980   /* Look for the optional `::' operator.  */
2981   global_scope_p
2982     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2983        != NULL_TREE);
2984   /* Look for the optional nested-name-specifier.  */
2985   nested_name_specifier_p
2986     = (cp_parser_nested_name_specifier_opt (parser,
2987                                             /*typename_keyword_p=*/false,
2988                                             check_dependency_p,
2989                                             /*type_p=*/false,
2990                                             declarator_p)
2991        != NULL_TREE);
2992   /* If there is a nested-name-specifier, then we are looking at
2993      the first qualified-id production.  */
2994   if (nested_name_specifier_p)
2995     {
2996       tree saved_scope;
2997       tree saved_object_scope;
2998       tree saved_qualifying_scope;
2999       tree unqualified_id;
3000       bool is_template;
3001
3002       /* See if the next token is the `template' keyword.  */
3003       if (!template_p)
3004         template_p = &is_template;
3005       *template_p = cp_parser_optional_template_keyword (parser);
3006       /* Name lookup we do during the processing of the
3007          unqualified-id might obliterate SCOPE.  */
3008       saved_scope = parser->scope;
3009       saved_object_scope = parser->object_scope;
3010       saved_qualifying_scope = parser->qualifying_scope;
3011       /* Process the final unqualified-id.  */
3012       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3013                                                  check_dependency_p,
3014                                                  declarator_p);
3015       /* Restore the SAVED_SCOPE for our caller.  */
3016       parser->scope = saved_scope;
3017       parser->object_scope = saved_object_scope;
3018       parser->qualifying_scope = saved_qualifying_scope;
3019
3020       return unqualified_id;
3021     }
3022   /* Otherwise, if we are in global scope, then we are looking at one
3023      of the other qualified-id productions.  */
3024   else if (global_scope_p)
3025     {
3026       cp_token *token;
3027       tree id;
3028
3029       /* Peek at the next token.  */
3030       token = cp_lexer_peek_token (parser->lexer);
3031
3032       /* If it's an identifier, and the next token is not a "<", then
3033          we can avoid the template-id case.  This is an optimization
3034          for this common case.  */
3035       if (token->type == CPP_NAME
3036           && !cp_parser_nth_token_starts_template_argument_list_p
3037                (parser, 2))
3038         return cp_parser_identifier (parser);
3039
3040       cp_parser_parse_tentatively (parser);
3041       /* Try a template-id.  */
3042       id = cp_parser_template_id (parser,
3043                                   /*template_keyword_p=*/false,
3044                                   /*check_dependency_p=*/true,
3045                                   declarator_p);
3046       /* If that worked, we're done.  */
3047       if (cp_parser_parse_definitely (parser))
3048         return id;
3049
3050       /* Peek at the next token.  (Changes in the token buffer may
3051          have invalidated the pointer obtained above.)  */
3052       token = cp_lexer_peek_token (parser->lexer);
3053
3054       switch (token->type)
3055         {
3056         case CPP_NAME:
3057           return cp_parser_identifier (parser);
3058
3059         case CPP_KEYWORD:
3060           if (token->keyword == RID_OPERATOR)
3061             return cp_parser_operator_function_id (parser);
3062           /* Fall through.  */
3063
3064         default:
3065           cp_parser_error (parser, "expected id-expression");
3066           return error_mark_node;
3067         }
3068     }
3069   else
3070     return cp_parser_unqualified_id (parser, template_keyword_p,
3071                                      /*check_dependency_p=*/true,
3072                                      declarator_p);
3073 }
3074
3075 /* Parse an unqualified-id.
3076
3077    unqualified-id:
3078      identifier
3079      operator-function-id
3080      conversion-function-id
3081      ~ class-name
3082      template-id
3083
3084    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3085    keyword, in a construct like `A::template ...'.
3086
3087    Returns a representation of unqualified-id.  For the `identifier'
3088    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3089    production a BIT_NOT_EXPR is returned; the operand of the
3090    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3091    other productions, see the documentation accompanying the
3092    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3093    names are looked up in uninstantiated templates.  If DECLARATOR_P
3094    is true, the unqualified-id is appearing as part of a declarator,
3095    rather than as part of an expression.  */
3096
3097 static tree
3098 cp_parser_unqualified_id (cp_parser* parser,
3099                           bool template_keyword_p,
3100                           bool check_dependency_p,
3101                           bool declarator_p)
3102 {
3103   cp_token *token;
3104
3105   /* Peek at the next token.  */
3106   token = cp_lexer_peek_token (parser->lexer);
3107
3108   switch (token->type)
3109     {
3110     case CPP_NAME:
3111       {
3112         tree id;
3113
3114         /* We don't know yet whether or not this will be a
3115            template-id.  */
3116         cp_parser_parse_tentatively (parser);
3117         /* Try a template-id.  */
3118         id = cp_parser_template_id (parser, template_keyword_p,
3119                                     check_dependency_p,
3120                                     declarator_p);
3121         /* If it worked, we're done.  */
3122         if (cp_parser_parse_definitely (parser))
3123           return id;
3124         /* Otherwise, it's an ordinary identifier.  */
3125         return cp_parser_identifier (parser);
3126       }
3127
3128     case CPP_TEMPLATE_ID:
3129       return cp_parser_template_id (parser, template_keyword_p,
3130                                     check_dependency_p,
3131                                     declarator_p);
3132
3133     case CPP_COMPL:
3134       {
3135         tree type_decl;
3136         tree qualifying_scope;
3137         tree object_scope;
3138         tree scope;
3139
3140         /* Consume the `~' token.  */
3141         cp_lexer_consume_token (parser->lexer);
3142         /* Parse the class-name.  The standard, as written, seems to
3143            say that:
3144
3145              template <typename T> struct S { ~S (); };
3146              template <typename T> S<T>::~S() {}
3147
3148            is invalid, since `~' must be followed by a class-name, but
3149            `S<T>' is dependent, and so not known to be a class.
3150            That's not right; we need to look in uninstantiated
3151            templates.  A further complication arises from:
3152
3153              template <typename T> void f(T t) {
3154                t.T::~T();
3155              }
3156
3157            Here, it is not possible to look up `T' in the scope of `T'
3158            itself.  We must look in both the current scope, and the
3159            scope of the containing complete expression.
3160
3161            Yet another issue is:
3162
3163              struct S {
3164                int S;
3165                ~S();
3166              };
3167
3168              S::~S() {}
3169
3170            The standard does not seem to say that the `S' in `~S'
3171            should refer to the type `S' and not the data member
3172            `S::S'.  */
3173
3174         /* DR 244 says that we look up the name after the "~" in the
3175            same scope as we looked up the qualifying name.  That idea
3176            isn't fully worked out; it's more complicated than that.  */
3177         scope = parser->scope;
3178         object_scope = parser->object_scope;
3179         qualifying_scope = parser->qualifying_scope;
3180
3181         /* If the name is of the form "X::~X" it's OK.  */
3182         if (scope && TYPE_P (scope)
3183             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3184             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3185                 == CPP_OPEN_PAREN)
3186             && (cp_lexer_peek_token (parser->lexer)->value
3187                 == TYPE_IDENTIFIER (scope)))
3188           {
3189             cp_lexer_consume_token (parser->lexer);
3190             return build_nt (BIT_NOT_EXPR, scope);
3191           }
3192
3193         /* If there was an explicit qualification (S::~T), first look
3194            in the scope given by the qualification (i.e., S).  */
3195         if (scope)
3196           {
3197             cp_parser_parse_tentatively (parser);
3198             type_decl = cp_parser_class_name (parser,
3199                                               /*typename_keyword_p=*/false,
3200                                               /*template_keyword_p=*/false,
3201                                               /*type_p=*/false,
3202                                               /*check_dependency=*/false,
3203                                               /*class_head_p=*/false,
3204                                               declarator_p);
3205             if (cp_parser_parse_definitely (parser))
3206               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3207           }
3208         /* In "N::S::~S", look in "N" as well.  */
3209         if (scope && qualifying_scope)
3210           {
3211             cp_parser_parse_tentatively (parser);
3212             parser->scope = qualifying_scope;
3213             parser->object_scope = NULL_TREE;
3214             parser->qualifying_scope = NULL_TREE;
3215             type_decl
3216               = cp_parser_class_name (parser,
3217                                       /*typename_keyword_p=*/false,
3218                                       /*template_keyword_p=*/false,
3219                                       /*type_p=*/false,
3220                                       /*check_dependency=*/false,
3221                                       /*class_head_p=*/false,
3222                                       declarator_p);
3223             if (cp_parser_parse_definitely (parser))
3224               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3225           }
3226         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3227         else if (object_scope)
3228           {
3229             cp_parser_parse_tentatively (parser);
3230             parser->scope = object_scope;
3231             parser->object_scope = NULL_TREE;
3232             parser->qualifying_scope = NULL_TREE;
3233             type_decl
3234               = cp_parser_class_name (parser,
3235                                       /*typename_keyword_p=*/false,
3236                                       /*template_keyword_p=*/false,
3237                                       /*type_p=*/false,
3238                                       /*check_dependency=*/false,
3239                                       /*class_head_p=*/false,
3240                                       declarator_p);
3241             if (cp_parser_parse_definitely (parser))
3242               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3243           }
3244         /* Look in the surrounding context.  */
3245         parser->scope = NULL_TREE;
3246         parser->object_scope = NULL_TREE;
3247         parser->qualifying_scope = NULL_TREE;
3248         type_decl
3249           = cp_parser_class_name (parser,
3250                                   /*typename_keyword_p=*/false,
3251                                   /*template_keyword_p=*/false,
3252                                   /*type_p=*/false,
3253                                   /*check_dependency=*/false,
3254                                   /*class_head_p=*/false,
3255                                   declarator_p);
3256         /* If an error occurred, assume that the name of the
3257            destructor is the same as the name of the qualifying
3258            class.  That allows us to keep parsing after running
3259            into ill-formed destructor names.  */
3260         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3261           return build_nt (BIT_NOT_EXPR, scope);
3262         else if (type_decl == error_mark_node)
3263           return error_mark_node;
3264
3265         /* [class.dtor]
3266
3267            A typedef-name that names a class shall not be used as the
3268            identifier in the declarator for a destructor declaration.  */
3269         if (declarator_p
3270             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3271             && !DECL_SELF_REFERENCE_P (type_decl))
3272           error ("typedef-name %qD used as destructor declarator",
3273                  type_decl);
3274
3275         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3276       }
3277
3278     case CPP_KEYWORD:
3279       if (token->keyword == RID_OPERATOR)
3280         {
3281           tree id;
3282
3283           /* This could be a template-id, so we try that first.  */
3284           cp_parser_parse_tentatively (parser);
3285           /* Try a template-id.  */
3286           id = cp_parser_template_id (parser, template_keyword_p,
3287                                       /*check_dependency_p=*/true,
3288                                       declarator_p);
3289           /* If that worked, we're done.  */
3290           if (cp_parser_parse_definitely (parser))
3291             return id;
3292           /* We still don't know whether we're looking at an
3293              operator-function-id or a conversion-function-id.  */
3294           cp_parser_parse_tentatively (parser);
3295           /* Try an operator-function-id.  */
3296           id = cp_parser_operator_function_id (parser);
3297           /* If that didn't work, try a conversion-function-id.  */
3298           if (!cp_parser_parse_definitely (parser))
3299             id = cp_parser_conversion_function_id (parser);
3300
3301           return id;
3302         }
3303       /* Fall through.  */
3304
3305     default:
3306       cp_parser_error (parser, "expected unqualified-id");
3307       return error_mark_node;
3308     }
3309 }
3310
3311 /* Parse an (optional) nested-name-specifier.
3312
3313    nested-name-specifier:
3314      class-or-namespace-name :: nested-name-specifier [opt]
3315      class-or-namespace-name :: template nested-name-specifier [opt]
3316
3317    PARSER->SCOPE should be set appropriately before this function is
3318    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3319    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3320    in name lookups.
3321
3322    Sets PARSER->SCOPE to the class (TYPE) or namespace
3323    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3324    it unchanged if there is no nested-name-specifier.  Returns the new
3325    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3326
3327    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3328    part of a declaration and/or decl-specifier.  */
3329
3330 static tree
3331 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3332                                      bool typename_keyword_p,
3333                                      bool check_dependency_p,
3334                                      bool type_p,
3335                                      bool is_declaration)
3336 {
3337   bool success = false;
3338   tree access_check = NULL_TREE;
3339   cp_token_position start = 0;
3340   cp_token *token;
3341
3342   /* If the next token corresponds to a nested name specifier, there
3343      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3344      false, it may have been true before, in which case something
3345      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3346      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3347      CHECK_DEPENDENCY_P is false, we have to fall through into the
3348      main loop.  */
3349   if (check_dependency_p
3350       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3351     {
3352       cp_parser_pre_parsed_nested_name_specifier (parser);
3353       return parser->scope;
3354     }
3355
3356   /* Remember where the nested-name-specifier starts.  */
3357   if (cp_parser_parsing_tentatively (parser)
3358       && !cp_parser_committed_to_tentative_parse (parser))
3359     start = cp_lexer_token_position (parser->lexer, false);
3360
3361   push_deferring_access_checks (dk_deferred);
3362
3363   while (true)
3364     {
3365       tree new_scope;
3366       tree old_scope;
3367       tree saved_qualifying_scope;
3368       bool template_keyword_p;
3369
3370       /* Spot cases that cannot be the beginning of a
3371          nested-name-specifier.  */
3372       token = cp_lexer_peek_token (parser->lexer);
3373
3374       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3375          the already parsed nested-name-specifier.  */
3376       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3377         {
3378           /* Grab the nested-name-specifier and continue the loop.  */
3379           cp_parser_pre_parsed_nested_name_specifier (parser);
3380           success = true;
3381           continue;
3382         }
3383
3384       /* Spot cases that cannot be the beginning of a
3385          nested-name-specifier.  On the second and subsequent times
3386          through the loop, we look for the `template' keyword.  */
3387       if (success && token->keyword == RID_TEMPLATE)
3388         ;
3389       /* A template-id can start a nested-name-specifier.  */
3390       else if (token->type == CPP_TEMPLATE_ID)
3391         ;
3392       else
3393         {
3394           /* If the next token is not an identifier, then it is
3395              definitely not a class-or-namespace-name.  */
3396           if (token->type != CPP_NAME)
3397             break;
3398           /* If the following token is neither a `<' (to begin a
3399              template-id), nor a `::', then we are not looking at a
3400              nested-name-specifier.  */
3401           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3402           if (token->type != CPP_SCOPE
3403               && !cp_parser_nth_token_starts_template_argument_list_p
3404                   (parser, 2))
3405             break;
3406         }
3407
3408       /* The nested-name-specifier is optional, so we parse
3409          tentatively.  */
3410       cp_parser_parse_tentatively (parser);
3411
3412       /* Look for the optional `template' keyword, if this isn't the
3413          first time through the loop.  */
3414       if (success)
3415         template_keyword_p = cp_parser_optional_template_keyword (parser);
3416       else
3417         template_keyword_p = false;
3418
3419       /* Save the old scope since the name lookup we are about to do
3420          might destroy it.  */
3421       old_scope = parser->scope;
3422       saved_qualifying_scope = parser->qualifying_scope;
3423       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3424          look up names in "X<T>::I" in order to determine that "Y" is
3425          a template.  So, if we have a typename at this point, we make
3426          an effort to look through it.  */
3427       if (is_declaration 
3428           && !typename_keyword_p
3429           && parser->scope 
3430           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3431         parser->scope = resolve_typename_type (parser->scope, 
3432                                                /*only_current_p=*/false);
3433       /* Parse the qualifying entity.  */
3434       new_scope
3435         = cp_parser_class_or_namespace_name (parser,
3436                                              typename_keyword_p,
3437                                              template_keyword_p,
3438                                              check_dependency_p,
3439                                              type_p,
3440                                              is_declaration);
3441       /* Look for the `::' token.  */
3442       cp_parser_require (parser, CPP_SCOPE, "`::'");
3443
3444       /* If we found what we wanted, we keep going; otherwise, we're
3445          done.  */
3446       if (!cp_parser_parse_definitely (parser))
3447         {
3448           bool error_p = false;
3449
3450           /* Restore the OLD_SCOPE since it was valid before the
3451              failed attempt at finding the last
3452              class-or-namespace-name.  */
3453           parser->scope = old_scope;
3454           parser->qualifying_scope = saved_qualifying_scope;
3455           /* If the next token is an identifier, and the one after
3456              that is a `::', then any valid interpretation would have
3457              found a class-or-namespace-name.  */
3458           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3459                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3460                      == CPP_SCOPE)
3461                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3462                      != CPP_COMPL))
3463             {
3464               token = cp_lexer_consume_token (parser->lexer);
3465               if (!error_p)
3466                 {
3467                   tree decl;
3468
3469                   decl = cp_parser_lookup_name_simple (parser, token->value);
3470                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3471                     error ("%qD used without template parameters", decl);
3472                   else
3473                     cp_parser_name_lookup_error
3474                       (parser, token->value, decl,
3475                        "is not a class or namespace");
3476                   parser->scope = NULL_TREE;
3477                   error_p = true;
3478                   /* Treat this as a successful nested-name-specifier
3479                      due to:
3480
3481                      [basic.lookup.qual]
3482
3483                      If the name found is not a class-name (clause
3484                      _class_) or namespace-name (_namespace.def_), the
3485                      program is ill-formed.  */
3486                   success = true;
3487                 }
3488               cp_lexer_consume_token (parser->lexer);
3489             }
3490           break;
3491         }
3492
3493       /* We've found one valid nested-name-specifier.  */
3494       success = true;
3495       /* Make sure we look in the right scope the next time through
3496          the loop.  */
3497       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3498                        ? TREE_TYPE (new_scope)
3499                        : new_scope);
3500       /* If it is a class scope, try to complete it; we are about to
3501          be looking up names inside the class.  */
3502       if (TYPE_P (parser->scope)
3503           /* Since checking types for dependency can be expensive,
3504              avoid doing it if the type is already complete.  */
3505           && !COMPLETE_TYPE_P (parser->scope)
3506           /* Do not try to complete dependent types.  */
3507           && !dependent_type_p (parser->scope))
3508         complete_type (parser->scope);
3509     }
3510
3511   /* Retrieve any deferred checks.  Do not pop this access checks yet
3512      so the memory will not be reclaimed during token replacing below.  */
3513   access_check = get_deferred_access_checks ();
3514
3515   /* If parsing tentatively, replace the sequence of tokens that makes
3516      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3517      token.  That way, should we re-parse the token stream, we will
3518      not have to repeat the effort required to do the parse, nor will
3519      we issue duplicate error messages.  */
3520   if (success && start)
3521     {
3522       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3523       
3524       /* Reset the contents of the START token.  */
3525       token->type = CPP_NESTED_NAME_SPECIFIER;
3526       token->value = build_tree_list (access_check, parser->scope);
3527       TREE_TYPE (token->value) = parser->qualifying_scope;
3528       token->keyword = RID_MAX;
3529       
3530       /* Purge all subsequent tokens.  */
3531       cp_lexer_purge_tokens_after (parser->lexer, start);
3532     }
3533
3534   pop_deferring_access_checks ();
3535   return success ? parser->scope : NULL_TREE;
3536 }
3537
3538 /* Parse a nested-name-specifier.  See
3539    cp_parser_nested_name_specifier_opt for details.  This function
3540    behaves identically, except that it will an issue an error if no
3541    nested-name-specifier is present, and it will return
3542    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3543    is present.  */
3544
3545 static tree
3546 cp_parser_nested_name_specifier (cp_parser *parser,
3547                                  bool typename_keyword_p,
3548                                  bool check_dependency_p,
3549                                  bool type_p,
3550                                  bool is_declaration)
3551 {
3552   tree scope;
3553
3554   /* Look for the nested-name-specifier.  */
3555   scope = cp_parser_nested_name_specifier_opt (parser,
3556                                                typename_keyword_p,
3557                                                check_dependency_p,
3558                                                type_p,
3559                                                is_declaration);
3560   /* If it was not present, issue an error message.  */
3561   if (!scope)
3562     {
3563       cp_parser_error (parser, "expected nested-name-specifier");
3564       parser->scope = NULL_TREE;
3565       return error_mark_node;
3566     }
3567
3568   return scope;
3569 }
3570
3571 /* Parse a class-or-namespace-name.
3572
3573    class-or-namespace-name:
3574      class-name
3575      namespace-name
3576
3577    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3578    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3579    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3580    TYPE_P is TRUE iff the next name should be taken as a class-name,
3581    even the same name is declared to be another entity in the same
3582    scope.
3583
3584    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3585    specified by the class-or-namespace-name.  If neither is found the
3586    ERROR_MARK_NODE is returned.  */
3587
3588 static tree
3589 cp_parser_class_or_namespace_name (cp_parser *parser,
3590                                    bool typename_keyword_p,
3591                                    bool template_keyword_p,
3592                                    bool check_dependency_p,
3593                                    bool type_p,
3594                                    bool is_declaration)
3595 {
3596   tree saved_scope;
3597   tree saved_qualifying_scope;
3598   tree saved_object_scope;
3599   tree scope;
3600   bool only_class_p;
3601
3602   /* Before we try to parse the class-name, we must save away the
3603      current PARSER->SCOPE since cp_parser_class_name will destroy
3604      it.  */
3605   saved_scope = parser->scope;
3606   saved_qualifying_scope = parser->qualifying_scope;
3607   saved_object_scope = parser->object_scope;
3608   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3609      there is no need to look for a namespace-name.  */
3610   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3611   if (!only_class_p)
3612     cp_parser_parse_tentatively (parser);
3613   scope = cp_parser_class_name (parser,
3614                                 typename_keyword_p,
3615                                 template_keyword_p,
3616                                 type_p,
3617                                 check_dependency_p,
3618                                 /*class_head_p=*/false,
3619                                 is_declaration);
3620   /* If that didn't work, try for a namespace-name.  */
3621   if (!only_class_p && !cp_parser_parse_definitely (parser))
3622     {
3623       /* Restore the saved scope.  */
3624       parser->scope = saved_scope;
3625       parser->qualifying_scope = saved_qualifying_scope;
3626       parser->object_scope = saved_object_scope;
3627       /* If we are not looking at an identifier followed by the scope
3628          resolution operator, then this is not part of a
3629          nested-name-specifier.  (Note that this function is only used
3630          to parse the components of a nested-name-specifier.)  */
3631       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3632           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3633         return error_mark_node;
3634       scope = cp_parser_namespace_name (parser);
3635     }
3636
3637   return scope;
3638 }
3639
3640 /* Parse a postfix-expression.
3641
3642    postfix-expression:
3643      primary-expression
3644      postfix-expression [ expression ]
3645      postfix-expression ( expression-list [opt] )
3646      simple-type-specifier ( expression-list [opt] )
3647      typename :: [opt] nested-name-specifier identifier
3648        ( expression-list [opt] )
3649      typename :: [opt] nested-name-specifier template [opt] template-id
3650        ( expression-list [opt] )
3651      postfix-expression . template [opt] id-expression
3652      postfix-expression -> template [opt] id-expression
3653      postfix-expression . pseudo-destructor-name
3654      postfix-expression -> pseudo-destructor-name
3655      postfix-expression ++
3656      postfix-expression --
3657      dynamic_cast < type-id > ( expression )
3658      static_cast < type-id > ( expression )
3659      reinterpret_cast < type-id > ( expression )
3660      const_cast < type-id > ( expression )
3661      typeid ( expression )
3662      typeid ( type-id )
3663
3664    GNU Extension:
3665
3666    postfix-expression:
3667      ( type-id ) { initializer-list , [opt] }
3668
3669    This extension is a GNU version of the C99 compound-literal
3670    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3671    but they are essentially the same concept.)
3672
3673    If ADDRESS_P is true, the postfix expression is the operand of the
3674    `&' operator.
3675
3676    Returns a representation of the expression.  */
3677
3678 static tree
3679 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3680 {
3681   cp_token *token;
3682   enum rid keyword;
3683   cp_id_kind idk = CP_ID_KIND_NONE;
3684   tree postfix_expression = NULL_TREE;
3685   /* Non-NULL only if the current postfix-expression can be used to
3686      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3687      class used to qualify the member.  */
3688   tree qualifying_class = NULL_TREE;
3689
3690   /* Peek at the next token.  */
3691   token = cp_lexer_peek_token (parser->lexer);
3692   /* Some of the productions are determined by keywords.  */
3693   keyword = token->keyword;
3694   switch (keyword)
3695     {
3696     case RID_DYNCAST:
3697     case RID_STATCAST:
3698     case RID_REINTCAST:
3699     case RID_CONSTCAST:
3700       {
3701         tree type;
3702         tree expression;
3703         const char *saved_message;
3704
3705         /* All of these can be handled in the same way from the point
3706            of view of parsing.  Begin by consuming the token
3707            identifying the cast.  */
3708         cp_lexer_consume_token (parser->lexer);
3709
3710         /* New types cannot be defined in the cast.  */
3711         saved_message = parser->type_definition_forbidden_message;
3712         parser->type_definition_forbidden_message
3713           = "types may not be defined in casts";
3714
3715         /* Look for the opening `<'.  */
3716         cp_parser_require (parser, CPP_LESS, "`<'");
3717         /* Parse the type to which we are casting.  */
3718         type = cp_parser_type_id (parser);
3719         /* Look for the closing `>'.  */
3720         cp_parser_require (parser, CPP_GREATER, "`>'");
3721         /* Restore the old message.  */
3722         parser->type_definition_forbidden_message = saved_message;
3723
3724         /* And the expression which is being cast.  */
3725         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3726         expression = cp_parser_expression (parser);
3727         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3728
3729         /* Only type conversions to integral or enumeration types
3730            can be used in constant-expressions.  */
3731         if (parser->integral_constant_expression_p
3732             && !dependent_type_p (type)
3733             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3734             && (cp_parser_non_integral_constant_expression
3735                 (parser,
3736                  "a cast to a type other than an integral or "
3737                  "enumeration type")))
3738           return error_mark_node;
3739
3740         switch (keyword)
3741           {
3742           case RID_DYNCAST:
3743             postfix_expression
3744               = build_dynamic_cast (type, expression);
3745             break;
3746           case RID_STATCAST:
3747             postfix_expression
3748               = build_static_cast (type, expression);
3749             break;
3750           case RID_REINTCAST:
3751             postfix_expression
3752               = build_reinterpret_cast (type, expression);
3753             break;
3754           case RID_CONSTCAST:
3755             postfix_expression
3756               = build_const_cast (type, expression);
3757             break;
3758           default:
3759             gcc_unreachable ();
3760           }
3761       }
3762       break;
3763
3764     case RID_TYPEID:
3765       {
3766         tree type;
3767         const char *saved_message;
3768         bool saved_in_type_id_in_expr_p;
3769
3770         /* Consume the `typeid' token.  */
3771         cp_lexer_consume_token (parser->lexer);
3772         /* Look for the `(' token.  */
3773         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3774         /* Types cannot be defined in a `typeid' expression.  */
3775         saved_message = parser->type_definition_forbidden_message;
3776         parser->type_definition_forbidden_message
3777           = "types may not be defined in a `typeid\' expression";
3778         /* We can't be sure yet whether we're looking at a type-id or an
3779            expression.  */
3780         cp_parser_parse_tentatively (parser);
3781         /* Try a type-id first.  */
3782         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3783         parser->in_type_id_in_expr_p = true;
3784         type = cp_parser_type_id (parser);
3785         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3786         /* Look for the `)' token.  Otherwise, we can't be sure that
3787            we're not looking at an expression: consider `typeid (int
3788            (3))', for example.  */
3789         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3790         /* If all went well, simply lookup the type-id.  */
3791         if (cp_parser_parse_definitely (parser))
3792           postfix_expression = get_typeid (type);
3793         /* Otherwise, fall back to the expression variant.  */
3794         else
3795           {
3796             tree expression;
3797
3798             /* Look for an expression.  */
3799             expression = cp_parser_expression (parser);
3800             /* Compute its typeid.  */
3801             postfix_expression = build_typeid (expression);
3802             /* Look for the `)' token.  */
3803             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3804           }
3805         /* `typeid' may not appear in an integral constant expression.  */
3806         if (cp_parser_non_integral_constant_expression(parser,
3807                                                        "`typeid' operator"))
3808           return error_mark_node;
3809         /* Restore the saved message.  */
3810         parser->type_definition_forbidden_message = saved_message;
3811       }
3812       break;
3813
3814     case RID_TYPENAME:
3815       {
3816         bool template_p = false;
3817         tree id;
3818         tree type;
3819
3820         /* Consume the `typename' token.  */
3821         cp_lexer_consume_token (parser->lexer);
3822         /* Look for the optional `::' operator.  */
3823         cp_parser_global_scope_opt (parser,
3824                                     /*current_scope_valid_p=*/false);
3825         /* Look for the nested-name-specifier.  */
3826         cp_parser_nested_name_specifier (parser,
3827                                          /*typename_keyword_p=*/true,
3828                                          /*check_dependency_p=*/true,
3829                                          /*type_p=*/true,
3830                                          /*is_declaration=*/true);
3831         /* Look for the optional `template' keyword.  */
3832         template_p = cp_parser_optional_template_keyword (parser);
3833         /* We don't know whether we're looking at a template-id or an
3834            identifier.  */
3835         cp_parser_parse_tentatively (parser);
3836         /* Try a template-id.  */
3837         id = cp_parser_template_id (parser, template_p,
3838                                     /*check_dependency_p=*/true,
3839                                     /*is_declaration=*/true);
3840         /* If that didn't work, try an identifier.  */
3841         if (!cp_parser_parse_definitely (parser))
3842           id = cp_parser_identifier (parser);
3843         /* If we look up a template-id in a non-dependent qualifying
3844            scope, there's no need to create a dependent type.  */
3845         if (TREE_CODE (id) == TYPE_DECL
3846             && !dependent_type_p (parser->scope))
3847           type = TREE_TYPE (id);
3848         /* Create a TYPENAME_TYPE to represent the type to which the
3849            functional cast is being performed.  */
3850         else
3851           type = make_typename_type (parser->scope, id,
3852                                      /*complain=*/1);
3853
3854         postfix_expression = cp_parser_functional_cast (parser, type);
3855       }
3856       break;
3857
3858     default:
3859       {
3860         tree type;
3861
3862         /* If the next thing is a simple-type-specifier, we may be
3863            looking at a functional cast.  We could also be looking at
3864            an id-expression.  So, we try the functional cast, and if
3865            that doesn't work we fall back to the primary-expression.  */
3866         cp_parser_parse_tentatively (parser);
3867         /* Look for the simple-type-specifier.  */
3868         type = cp_parser_simple_type_specifier (parser,
3869                                                 /*decl_specs=*/NULL,
3870                                                 CP_PARSER_FLAGS_NONE);
3871         /* Parse the cast itself.  */
3872         if (!cp_parser_error_occurred (parser))
3873           postfix_expression
3874             = cp_parser_functional_cast (parser, type);
3875         /* If that worked, we're done.  */
3876         if (cp_parser_parse_definitely (parser))
3877           break;
3878
3879         /* If the functional-cast didn't work out, try a
3880            compound-literal.  */
3881         if (cp_parser_allow_gnu_extensions_p (parser)
3882             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3883           {
3884             tree initializer_list = NULL_TREE;
3885             bool saved_in_type_id_in_expr_p;
3886
3887             cp_parser_parse_tentatively (parser);
3888             /* Consume the `('.  */
3889             cp_lexer_consume_token (parser->lexer);
3890             /* Parse the type.  */
3891             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3892             parser->in_type_id_in_expr_p = true;
3893             type = cp_parser_type_id (parser);
3894             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3895             /* Look for the `)'.  */
3896             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3897             /* Look for the `{'.  */
3898             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3899             /* If things aren't going well, there's no need to
3900                keep going.  */
3901             if (!cp_parser_error_occurred (parser))
3902               {
3903                 bool non_constant_p;
3904                 /* Parse the initializer-list.  */
3905                 initializer_list
3906                   = cp_parser_initializer_list (parser, &non_constant_p);
3907                 /* Allow a trailing `,'.  */
3908                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3909                   cp_lexer_consume_token (parser->lexer);
3910                 /* Look for the final `}'.  */
3911                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3912               }
3913             /* If that worked, we're definitely looking at a
3914                compound-literal expression.  */
3915             if (cp_parser_parse_definitely (parser))
3916               {
3917                 /* Warn the user that a compound literal is not
3918                    allowed in standard C++.  */
3919                 if (pedantic)
3920                   pedwarn ("ISO C++ forbids compound-literals");
3921                 /* Form the representation of the compound-literal.  */
3922                 postfix_expression
3923                   = finish_compound_literal (type, initializer_list);
3924                 break;
3925               }
3926           }
3927
3928         /* It must be a primary-expression.  */
3929         postfix_expression = cp_parser_primary_expression (parser,
3930                                                            &idk,
3931                                                            &qualifying_class);
3932       }
3933       break;
3934     }
3935
3936   /* If we were avoiding committing to the processing of a
3937      qualified-id until we knew whether or not we had a
3938      pointer-to-member, we now know.  */
3939   if (qualifying_class)
3940     {
3941       bool done;
3942
3943       /* Peek at the next token.  */
3944       token = cp_lexer_peek_token (parser->lexer);
3945       done = (token->type != CPP_OPEN_SQUARE
3946               && token->type != CPP_OPEN_PAREN
3947               && token->type != CPP_DOT
3948               && token->type != CPP_DEREF
3949               && token->type != CPP_PLUS_PLUS
3950               && token->type != CPP_MINUS_MINUS);
3951
3952       postfix_expression = finish_qualified_id_expr (qualifying_class,
3953                                                      postfix_expression,
3954                                                      done,
3955                                                      address_p);
3956       if (done)
3957         return postfix_expression;
3958     }
3959
3960   /* Keep looping until the postfix-expression is complete.  */
3961   while (true)
3962     {
3963       if (idk == CP_ID_KIND_UNQUALIFIED
3964           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3965           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3966         /* It is not a Koenig lookup function call.  */
3967         postfix_expression
3968           = unqualified_name_lookup_error (postfix_expression);
3969
3970       /* Peek at the next token.  */
3971       token = cp_lexer_peek_token (parser->lexer);
3972
3973       switch (token->type)
3974         {
3975         case CPP_OPEN_SQUARE:
3976           postfix_expression
3977             = cp_parser_postfix_open_square_expression (parser,
3978                                                         postfix_expression,
3979                                                         false);
3980           idk = CP_ID_KIND_NONE;
3981           break;
3982
3983         case CPP_OPEN_PAREN:
3984           /* postfix-expression ( expression-list [opt] ) */
3985           {
3986             bool koenig_p;
3987             tree args = (cp_parser_parenthesized_expression_list
3988                          (parser, false, /*non_constant_p=*/NULL));
3989
3990             if (args == error_mark_node)
3991               {
3992                 postfix_expression = error_mark_node;
3993                 break;
3994               }
3995
3996             /* Function calls are not permitted in
3997                constant-expressions.  */
3998             if (cp_parser_non_integral_constant_expression (parser,
3999                                                             "a function call"))
4000               {
4001                 postfix_expression = error_mark_node;
4002                 break;
4003               }
4004
4005             koenig_p = false;
4006             if (idk == CP_ID_KIND_UNQUALIFIED)
4007               {
4008                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4009                   {
4010                     if (args)
4011                       {
4012                         koenig_p = true;
4013                         postfix_expression
4014                           = perform_koenig_lookup (postfix_expression, args);
4015                       }
4016                     else
4017                       postfix_expression
4018                         = unqualified_fn_lookup_error (postfix_expression);
4019                   }
4020                 /* We do not perform argument-dependent lookup if
4021                    normal lookup finds a non-function, in accordance
4022                    with the expected resolution of DR 218.  */
4023                 else if (args && is_overloaded_fn (postfix_expression))
4024                   {
4025                     tree fn = get_first_fn (postfix_expression);
4026
4027                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4028                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4029
4030                     /* Only do argument dependent lookup if regular
4031                        lookup does not find a set of member functions.
4032                        [basic.lookup.koenig]/2a  */
4033                     if (!DECL_FUNCTION_MEMBER_P (fn))
4034                       {
4035                         koenig_p = true;
4036                         postfix_expression
4037                           = perform_koenig_lookup (postfix_expression, args);
4038                       }
4039                   }
4040               }
4041
4042             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4043               {
4044                 tree instance = TREE_OPERAND (postfix_expression, 0);
4045                 tree fn = TREE_OPERAND (postfix_expression, 1);
4046
4047                 if (processing_template_decl
4048                     && (type_dependent_expression_p (instance)
4049                         || (!BASELINK_P (fn)
4050                             && TREE_CODE (fn) != FIELD_DECL)
4051                         || type_dependent_expression_p (fn)
4052                         || any_type_dependent_arguments_p (args)))
4053                   {
4054                     postfix_expression
4055                       = build_min_nt (CALL_EXPR, postfix_expression,
4056                                       args, NULL_TREE);
4057                     break;
4058                   }
4059
4060                 if (BASELINK_P (fn))
4061                   postfix_expression
4062                     = (build_new_method_call
4063                        (instance, fn, args, NULL_TREE,
4064                         (idk == CP_ID_KIND_QUALIFIED
4065                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4066                 else
4067                   postfix_expression
4068                     = finish_call_expr (postfix_expression, args,
4069                                         /*disallow_virtual=*/false,
4070                                         /*koenig_p=*/false);
4071               }
4072             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4073                      || TREE_CODE (postfix_expression) == MEMBER_REF
4074                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4075               postfix_expression = (build_offset_ref_call_from_tree
4076                                     (postfix_expression, args));
4077             else if (idk == CP_ID_KIND_QUALIFIED)
4078               /* A call to a static class member, or a namespace-scope
4079                  function.  */
4080               postfix_expression
4081                 = finish_call_expr (postfix_expression, args,
4082                                     /*disallow_virtual=*/true,
4083                                     koenig_p);
4084             else
4085               /* All other function calls.  */
4086               postfix_expression
4087                 = finish_call_expr (postfix_expression, args,
4088                                     /*disallow_virtual=*/false,
4089                                     koenig_p);
4090
4091             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4092             idk = CP_ID_KIND_NONE;
4093           }
4094           break;
4095
4096         case CPP_DOT:
4097         case CPP_DEREF:
4098           /* postfix-expression . template [opt] id-expression
4099              postfix-expression . pseudo-destructor-name
4100              postfix-expression -> template [opt] id-expression
4101              postfix-expression -> pseudo-destructor-name */
4102
4103           /* Consume the `.' or `->' operator.  */
4104           cp_lexer_consume_token (parser->lexer);
4105
4106           postfix_expression
4107             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4108                                                       postfix_expression,
4109                                                       false, &idk);
4110           break;
4111
4112         case CPP_PLUS_PLUS:
4113           /* postfix-expression ++  */
4114           /* Consume the `++' token.  */
4115           cp_lexer_consume_token (parser->lexer);
4116           /* Generate a representation for the complete expression.  */
4117           postfix_expression
4118             = finish_increment_expr (postfix_expression,
4119                                      POSTINCREMENT_EXPR);
4120           /* Increments may not appear in constant-expressions.  */
4121           if (cp_parser_non_integral_constant_expression (parser,
4122                                                           "an increment"))
4123             postfix_expression = error_mark_node;
4124           idk = CP_ID_KIND_NONE;
4125           break;
4126
4127         case CPP_MINUS_MINUS:
4128           /* postfix-expression -- */
4129           /* Consume the `--' token.  */
4130           cp_lexer_consume_token (parser->lexer);
4131           /* Generate a representation for the complete expression.  */
4132           postfix_expression
4133             = finish_increment_expr (postfix_expression,
4134                                      POSTDECREMENT_EXPR);
4135           /* Decrements may not appear in constant-expressions.  */
4136           if (cp_parser_non_integral_constant_expression (parser,
4137                                                           "a decrement"))
4138             postfix_expression = error_mark_node;
4139           idk = CP_ID_KIND_NONE;
4140           break;
4141
4142         default:
4143           return postfix_expression;
4144         }
4145     }
4146
4147   /* We should never get here.  */
4148   gcc_unreachable ();
4149   return error_mark_node;
4150 }
4151
4152 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4153    by cp_parser_builtin_offsetof.  We're looking for
4154
4155      postfix-expression [ expression ]
4156
4157    FOR_OFFSETOF is set if we're being called in that context, which
4158    changes how we deal with integer constant expressions.  */
4159
4160 static tree
4161 cp_parser_postfix_open_square_expression (cp_parser *parser,
4162                                           tree postfix_expression,
4163                                           bool for_offsetof)
4164 {
4165   tree index;
4166
4167   /* Consume the `[' token.  */
4168   cp_lexer_consume_token (parser->lexer);
4169
4170   /* Parse the index expression.  */
4171   /* ??? For offsetof, there is a question of what to allow here.  If
4172      offsetof is not being used in an integral constant expression context,
4173      then we *could* get the right answer by computing the value at runtime.
4174      If we are in an integral constant expression context, then we might
4175      could accept any constant expression; hard to say without analysis.
4176      Rather than open the barn door too wide right away, allow only integer
4177      constant expressions here.  */
4178   if (for_offsetof)
4179     index = cp_parser_constant_expression (parser, false, NULL);
4180   else
4181     index = cp_parser_expression (parser);
4182
4183   /* Look for the closing `]'.  */
4184   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4185
4186   /* Build the ARRAY_REF.  */
4187   postfix_expression = grok_array_decl (postfix_expression, index);
4188
4189   /* When not doing offsetof, array references are not permitted in
4190      constant-expressions.  */
4191   if (!for_offsetof
4192       && (cp_parser_non_integral_constant_expression
4193           (parser, "an array reference")))
4194     postfix_expression = error_mark_node;
4195
4196   return postfix_expression;
4197 }
4198
4199 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4200    by cp_parser_builtin_offsetof.  We're looking for
4201
4202      postfix-expression . template [opt] id-expression
4203      postfix-expression . pseudo-destructor-name
4204      postfix-expression -> template [opt] id-expression
4205      postfix-expression -> pseudo-destructor-name
4206
4207    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4208    limits what of the above we'll actually accept, but nevermind.
4209    TOKEN_TYPE is the "." or "->" token, which will already have been
4210    removed from the stream.  */
4211
4212 static tree
4213 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4214                                         enum cpp_ttype token_type,
4215                                         tree postfix_expression,
4216                                         bool for_offsetof, cp_id_kind *idk)
4217 {
4218   tree name;
4219   bool dependent_p;
4220   bool template_p;
4221   bool pseudo_destructor_p;
4222   tree scope = NULL_TREE;
4223
4224   /* If this is a `->' operator, dereference the pointer.  */
4225   if (token_type == CPP_DEREF)
4226     postfix_expression = build_x_arrow (postfix_expression);
4227   /* Check to see whether or not the expression is type-dependent.  */
4228   dependent_p = type_dependent_expression_p (postfix_expression);
4229   /* The identifier following the `->' or `.' is not qualified.  */
4230   parser->scope = NULL_TREE;
4231   parser->qualifying_scope = NULL_TREE;
4232   parser->object_scope = NULL_TREE;
4233   *idk = CP_ID_KIND_NONE;
4234   /* Enter the scope corresponding to the type of the object
4235      given by the POSTFIX_EXPRESSION.  */
4236   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4237     {
4238       scope = TREE_TYPE (postfix_expression);
4239       /* According to the standard, no expression should ever have
4240          reference type.  Unfortunately, we do not currently match
4241          the standard in this respect in that our internal representation
4242          of an expression may have reference type even when the standard
4243          says it does not.  Therefore, we have to manually obtain the
4244          underlying type here.  */
4245       scope = non_reference (scope);
4246       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4247       scope = complete_type_or_else (scope, NULL_TREE);
4248       /* Let the name lookup machinery know that we are processing a
4249          class member access expression.  */
4250       parser->context->object_type = scope;
4251       /* If something went wrong, we want to be able to discern that case,
4252          as opposed to the case where there was no SCOPE due to the type
4253          of expression being dependent.  */
4254       if (!scope)
4255         scope = error_mark_node;
4256       /* If the SCOPE was erroneous, make the various semantic analysis
4257          functions exit quickly -- and without issuing additional error
4258          messages.  */
4259       if (scope == error_mark_node)
4260         postfix_expression = error_mark_node;
4261     }
4262
4263   /* Assume this expression is not a pseudo-destructor access.  */
4264   pseudo_destructor_p = false;
4265
4266   /* If the SCOPE is a scalar type, then, if this is a valid program,
4267      we must be looking at a pseudo-destructor-name.  */
4268   if (scope && SCALAR_TYPE_P (scope))
4269     {
4270       tree s;
4271       tree type;
4272
4273       cp_parser_parse_tentatively (parser);
4274       /* Parse the pseudo-destructor-name.  */
4275       s = NULL_TREE;
4276       cp_parser_pseudo_destructor_name (parser, &s, &type);
4277       if (cp_parser_parse_definitely (parser))
4278         {
4279           pseudo_destructor_p = true;
4280           postfix_expression
4281             = finish_pseudo_destructor_expr (postfix_expression,
4282                                              s, TREE_TYPE (type));
4283         }
4284     }
4285
4286   if (!pseudo_destructor_p)
4287     {
4288       /* If the SCOPE is not a scalar type, we are looking at an
4289          ordinary class member access expression, rather than a
4290          pseudo-destructor-name.  */
4291       template_p = cp_parser_optional_template_keyword (parser);
4292       /* Parse the id-expression.  */
4293       name = cp_parser_id_expression (parser, template_p,
4294                                       /*check_dependency_p=*/true,
4295                                       /*template_p=*/NULL,
4296                                       /*declarator_p=*/false);
4297       /* In general, build a SCOPE_REF if the member name is qualified.
4298          However, if the name was not dependent and has already been
4299          resolved; there is no need to build the SCOPE_REF.  For example;
4300
4301              struct X { void f(); };
4302              template <typename T> void f(T* t) { t->X::f(); }
4303
4304          Even though "t" is dependent, "X::f" is not and has been resolved
4305          to a BASELINK; there is no need to include scope information.  */
4306
4307       /* But we do need to remember that there was an explicit scope for
4308          virtual function calls.  */
4309       if (parser->scope)
4310         *idk = CP_ID_KIND_QUALIFIED;
4311
4312       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4313         {
4314           name = build_nt (SCOPE_REF, parser->scope, name);
4315           parser->scope = NULL_TREE;
4316           parser->qualifying_scope = NULL_TREE;
4317           parser->object_scope = NULL_TREE;
4318         }
4319       if (scope && name && BASELINK_P (name))
4320         adjust_result_of_qualified_name_lookup
4321           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4322       postfix_expression
4323         = finish_class_member_access_expr (postfix_expression, name);
4324     }
4325
4326   /* We no longer need to look up names in the scope of the object on
4327      the left-hand side of the `.' or `->' operator.  */
4328   parser->context->object_type = NULL_TREE;
4329
4330   /* Outside of offsetof, these operators may not appear in
4331      constant-expressions.  */
4332   if (!for_offsetof
4333       && (cp_parser_non_integral_constant_expression
4334           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4335     postfix_expression = error_mark_node;
4336
4337   return postfix_expression;
4338 }
4339
4340 /* Parse a parenthesized expression-list.
4341
4342    expression-list:
4343      assignment-expression
4344      expression-list, assignment-expression
4345
4346    attribute-list:
4347      expression-list
4348      identifier
4349      identifier, expression-list
4350
4351    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4352    representation of an assignment-expression.  Note that a TREE_LIST
4353    is returned even if there is only a single expression in the list.
4354    error_mark_node is returned if the ( and or ) are
4355    missing. NULL_TREE is returned on no expressions. The parentheses
4356    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4357    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4358    indicates whether or not all of the expressions in the list were
4359    constant.  */
4360
4361 static tree
4362 cp_parser_parenthesized_expression_list (cp_parser* parser,
4363                                          bool is_attribute_list,
4364                                          bool *non_constant_p)
4365 {
4366   tree expression_list = NULL_TREE;
4367   bool fold_expr_p = is_attribute_list;
4368   tree identifier = NULL_TREE;
4369
4370   /* Assume all the expressions will be constant.  */
4371   if (non_constant_p)
4372     *non_constant_p = false;
4373
4374   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4375     return error_mark_node;
4376
4377   /* Consume expressions until there are no more.  */
4378   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4379     while (true)
4380       {
4381         tree expr;
4382
4383         /* At the beginning of attribute lists, check to see if the
4384            next token is an identifier.  */
4385         if (is_attribute_list
4386             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4387           {
4388             cp_token *token;
4389
4390             /* Consume the identifier.  */
4391             token = cp_lexer_consume_token (parser->lexer);
4392             /* Save the identifier.  */
4393             identifier = token->value;
4394           }
4395         else
4396           {
4397             /* Parse the next assignment-expression.  */
4398             if (non_constant_p)
4399               {
4400                 bool expr_non_constant_p;
4401                 expr = (cp_parser_constant_expression
4402                         (parser, /*allow_non_constant_p=*/true,
4403                          &expr_non_constant_p));
4404                 if (expr_non_constant_p)
4405                   *non_constant_p = true;
4406               }
4407             else
4408               expr = cp_parser_assignment_expression (parser);
4409
4410             if (fold_expr_p)
4411               expr = fold_non_dependent_expr (expr);
4412
4413              /* Add it to the list.  We add error_mark_node
4414                 expressions to the list, so that we can still tell if
4415                 the correct form for a parenthesized expression-list
4416                 is found. That gives better errors.  */
4417             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4418
4419             if (expr == error_mark_node)
4420               goto skip_comma;
4421           }
4422
4423         /* After the first item, attribute lists look the same as
4424            expression lists.  */
4425         is_attribute_list = false;
4426
4427       get_comma:;
4428         /* If the next token isn't a `,', then we are done.  */
4429         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4430           break;
4431
4432         /* Otherwise, consume the `,' and keep going.  */
4433         cp_lexer_consume_token (parser->lexer);
4434       }
4435
4436   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4437     {
4438       int ending;
4439
4440     skip_comma:;
4441       /* We try and resync to an unnested comma, as that will give the
4442          user better diagnostics.  */
4443       ending = cp_parser_skip_to_closing_parenthesis (parser,
4444                                                       /*recovering=*/true,
4445                                                       /*or_comma=*/true,
4446                                                       /*consume_paren=*/true);
4447       if (ending < 0)
4448         goto get_comma;
4449       if (!ending)
4450         return error_mark_node;
4451     }
4452
4453   /* We built up the list in reverse order so we must reverse it now.  */
4454   expression_list = nreverse (expression_list);
4455   if (identifier)
4456     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4457
4458   return expression_list;
4459 }
4460
4461 /* Parse a pseudo-destructor-name.
4462
4463    pseudo-destructor-name:
4464      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4465      :: [opt] nested-name-specifier template template-id :: ~ type-name
4466      :: [opt] nested-name-specifier [opt] ~ type-name
4467
4468    If either of the first two productions is used, sets *SCOPE to the
4469    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4470    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4471    or ERROR_MARK_NODE if the parse fails.  */
4472
4473 static void
4474 cp_parser_pseudo_destructor_name (cp_parser* parser,
4475                                   tree* scope,
4476                                   tree* type)
4477 {
4478   bool nested_name_specifier_p;
4479
4480   /* Assume that things will not work out.  */
4481   *type = error_mark_node;
4482
4483   /* Look for the optional `::' operator.  */
4484   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4485   /* Look for the optional nested-name-specifier.  */
4486   nested_name_specifier_p
4487     = (cp_parser_nested_name_specifier_opt (parser,
4488                                             /*typename_keyword_p=*/false,
4489                                             /*check_dependency_p=*/true,
4490                                             /*type_p=*/false,
4491                                             /*is_declaration=*/true)
4492        != NULL_TREE);
4493   /* Now, if we saw a nested-name-specifier, we might be doing the
4494      second production.  */
4495   if (nested_name_specifier_p
4496       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4497     {
4498       /* Consume the `template' keyword.  */
4499       cp_lexer_consume_token (parser->lexer);
4500       /* Parse the template-id.  */
4501       cp_parser_template_id (parser,
4502                              /*template_keyword_p=*/true,
4503                              /*check_dependency_p=*/false,
4504                              /*is_declaration=*/true);
4505       /* Look for the `::' token.  */
4506       cp_parser_require (parser, CPP_SCOPE, "`::'");
4507     }
4508   /* If the next token is not a `~', then there might be some
4509      additional qualification.  */
4510   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4511     {
4512       /* Look for the type-name.  */
4513       *scope = TREE_TYPE (cp_parser_type_name (parser));
4514
4515       if (*scope == error_mark_node)
4516         return;
4517
4518       /* If we don't have ::~, then something has gone wrong.  Since
4519          the only caller of this function is looking for something
4520          after `.' or `->' after a scalar type, most likely the
4521          program is trying to get a member of a non-aggregate
4522          type.  */
4523       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4524           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4525         {
4526           cp_parser_error (parser, "request for member of non-aggregate type");
4527           return;
4528         }
4529
4530       /* Look for the `::' token.  */
4531       cp_parser_require (parser, CPP_SCOPE, "`::'");
4532     }
4533   else
4534     *scope = NULL_TREE;
4535
4536   /* Look for the `~'.  */
4537   cp_parser_require (parser, CPP_COMPL, "`~'");
4538   /* Look for the type-name again.  We are not responsible for
4539      checking that it matches the first type-name.  */
4540   *type = cp_parser_type_name (parser);
4541 }
4542
4543 /* Parse a unary-expression.
4544
4545    unary-expression:
4546      postfix-expression
4547      ++ cast-expression
4548      -- cast-expression
4549      unary-operator cast-expression
4550      sizeof unary-expression
4551      sizeof ( type-id )
4552      new-expression
4553      delete-expression
4554
4555    GNU Extensions:
4556
4557    unary-expression:
4558      __extension__ cast-expression
4559      __alignof__ unary-expression
4560      __alignof__ ( type-id )
4561      __real__ cast-expression
4562      __imag__ cast-expression
4563      && identifier
4564
4565    ADDRESS_P is true iff the unary-expression is appearing as the
4566    operand of the `&' operator.
4567
4568    Returns a representation of the expression.  */
4569
4570 static tree
4571 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4572 {
4573   cp_token *token;
4574   enum tree_code unary_operator;
4575
4576   /* Peek at the next token.  */
4577   token = cp_lexer_peek_token (parser->lexer);
4578   /* Some keywords give away the kind of expression.  */
4579   if (token->type == CPP_KEYWORD)
4580     {
4581       enum rid keyword = token->keyword;
4582
4583       switch (keyword)
4584         {
4585         case RID_ALIGNOF:
4586         case RID_SIZEOF:
4587           {
4588             tree operand;
4589             enum tree_code op;
4590
4591             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4592             /* Consume the token.  */
4593             cp_lexer_consume_token (parser->lexer);
4594             /* Parse the operand.  */
4595             operand = cp_parser_sizeof_operand (parser, keyword);
4596
4597             if (TYPE_P (operand))
4598               return cxx_sizeof_or_alignof_type (operand, op, true);
4599             else
4600               return cxx_sizeof_or_alignof_expr (operand, op);
4601           }
4602
4603         case RID_NEW:
4604           return cp_parser_new_expression (parser);
4605
4606         case RID_DELETE:
4607           return cp_parser_delete_expression (parser);
4608
4609         case RID_EXTENSION:
4610           {
4611             /* The saved value of the PEDANTIC flag.  */
4612             int saved_pedantic;
4613             tree expr;
4614
4615             /* Save away the PEDANTIC flag.  */
4616             cp_parser_extension_opt (parser, &saved_pedantic);
4617             /* Parse the cast-expression.  */
4618             expr = cp_parser_simple_cast_expression (parser);
4619             /* Restore the PEDANTIC flag.  */
4620             pedantic = saved_pedantic;
4621
4622             return expr;
4623           }
4624
4625         case RID_REALPART:
4626         case RID_IMAGPART:
4627           {
4628             tree expression;
4629
4630             /* Consume the `__real__' or `__imag__' token.  */
4631             cp_lexer_consume_token (parser->lexer);
4632             /* Parse the cast-expression.  */
4633             expression = cp_parser_simple_cast_expression (parser);
4634             /* Create the complete representation.  */
4635             return build_x_unary_op ((keyword == RID_REALPART
4636                                       ? REALPART_EXPR : IMAGPART_EXPR),
4637                                      expression);
4638           }
4639           break;
4640
4641         default:
4642           break;
4643         }
4644     }
4645
4646   /* Look for the `:: new' and `:: delete', which also signal the
4647      beginning of a new-expression, or delete-expression,
4648      respectively.  If the next token is `::', then it might be one of
4649      these.  */
4650   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4651     {
4652       enum rid keyword;
4653
4654       /* See if the token after the `::' is one of the keywords in
4655          which we're interested.  */
4656       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4657       /* If it's `new', we have a new-expression.  */
4658       if (keyword == RID_NEW)
4659         return cp_parser_new_expression (parser);
4660       /* Similarly, for `delete'.  */
4661       else if (keyword == RID_DELETE)
4662         return cp_parser_delete_expression (parser);
4663     }
4664
4665   /* Look for a unary operator.  */
4666   unary_operator = cp_parser_unary_operator (token);
4667   /* The `++' and `--' operators can be handled similarly, even though
4668      they are not technically unary-operators in the grammar.  */
4669   if (unary_operator == ERROR_MARK)
4670     {
4671       if (token->type == CPP_PLUS_PLUS)
4672         unary_operator = PREINCREMENT_EXPR;
4673       else if (token->type == CPP_MINUS_MINUS)
4674         unary_operator = PREDECREMENT_EXPR;
4675       /* Handle the GNU address-of-label extension.  */
4676       else if (cp_parser_allow_gnu_extensions_p (parser)
4677                && token->type == CPP_AND_AND)
4678         {
4679           tree identifier;
4680
4681           /* Consume the '&&' token.  */
4682           cp_lexer_consume_token (parser->lexer);
4683           /* Look for the identifier.  */
4684           identifier = cp_parser_identifier (parser);
4685           /* Create an expression representing the address.  */
4686           return finish_label_address_expr (identifier);
4687         }
4688     }
4689   if (unary_operator != ERROR_MARK)
4690     {
4691       tree cast_expression;
4692       tree expression = error_mark_node;
4693       const char *non_constant_p = NULL;
4694
4695       /* Consume the operator token.  */
4696       token = cp_lexer_consume_token (parser->lexer);
4697       /* Parse the cast-expression.  */
4698       cast_expression
4699         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4700       /* Now, build an appropriate representation.  */
4701       switch (unary_operator)
4702         {
4703         case INDIRECT_REF:
4704           non_constant_p = "`*'";
4705           expression = build_x_indirect_ref (cast_expression, "unary *");
4706           break;
4707
4708         case ADDR_EXPR:
4709           non_constant_p = "`&'";
4710           /* Fall through.  */
4711         case BIT_NOT_EXPR:
4712           expression = build_x_unary_op (unary_operator, cast_expression);
4713           break;
4714
4715         case PREINCREMENT_EXPR:
4716         case PREDECREMENT_EXPR:
4717           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4718                             ? "`++'" : "`--'");
4719           /* Fall through.  */
4720         case CONVERT_EXPR:
4721         case NEGATE_EXPR:
4722         case TRUTH_NOT_EXPR:
4723           expression = finish_unary_op_expr (unary_operator, cast_expression);
4724           break;
4725
4726         default:
4727           gcc_unreachable ();
4728         }
4729
4730       if (non_constant_p
4731           && cp_parser_non_integral_constant_expression (parser,
4732                                                          non_constant_p))
4733         expression = error_mark_node;
4734
4735       return expression;
4736     }
4737
4738   return cp_parser_postfix_expression (parser, address_p);
4739 }
4740
4741 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4742    unary-operator, the corresponding tree code is returned.  */
4743
4744 static enum tree_code
4745 cp_parser_unary_operator (cp_token* token)
4746 {
4747   switch (token->type)
4748     {
4749     case CPP_MULT:
4750       return INDIRECT_REF;
4751
4752     case CPP_AND:
4753       return ADDR_EXPR;
4754
4755     case CPP_PLUS:
4756       return CONVERT_EXPR;
4757
4758     case CPP_MINUS:
4759       return NEGATE_EXPR;
4760
4761     case CPP_NOT:
4762       return TRUTH_NOT_EXPR;
4763
4764     case CPP_COMPL:
4765       return BIT_NOT_EXPR;
4766
4767     default:
4768       return ERROR_MARK;
4769     }
4770 }
4771
4772 /* Parse a new-expression.
4773
4774    new-expression:
4775      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4776      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4777
4778    Returns a representation of the expression.  */
4779
4780 static tree
4781 cp_parser_new_expression (cp_parser* parser)
4782 {
4783   bool global_scope_p;
4784   tree placement;
4785   tree type;
4786   tree initializer;
4787   tree nelts;
4788
4789   /* Look for the optional `::' operator.  */
4790   global_scope_p
4791     = (cp_parser_global_scope_opt (parser,
4792                                    /*current_scope_valid_p=*/false)
4793        != NULL_TREE);
4794   /* Look for the `new' operator.  */
4795   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4796   /* There's no easy way to tell a new-placement from the
4797      `( type-id )' construct.  */
4798   cp_parser_parse_tentatively (parser);
4799   /* Look for a new-placement.  */
4800   placement = cp_parser_new_placement (parser);
4801   /* If that didn't work out, there's no new-placement.  */
4802   if (!cp_parser_parse_definitely (parser))
4803     placement = NULL_TREE;
4804
4805   /* If the next token is a `(', then we have a parenthesized
4806      type-id.  */
4807   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4808     {
4809       /* Consume the `('.  */
4810       cp_lexer_consume_token (parser->lexer);
4811       /* Parse the type-id.  */
4812       type = cp_parser_type_id (parser);
4813       /* Look for the closing `)'.  */
4814       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4815       /* There should not be a direct-new-declarator in this production,
4816          but GCC used to allowed this, so we check and emit a sensible error
4817          message for this case.  */
4818       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4819         {
4820           error ("array bound forbidden after parenthesized type-id");
4821           inform ("try removing the parentheses around the type-id");
4822           cp_parser_direct_new_declarator (parser);
4823         }
4824       nelts = NULL_TREE;
4825     }
4826   /* Otherwise, there must be a new-type-id.  */
4827   else
4828     type = cp_parser_new_type_id (parser, &nelts);
4829
4830   /* If the next token is a `(', then we have a new-initializer.  */
4831   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4832     initializer = cp_parser_new_initializer (parser);
4833   else
4834     initializer = NULL_TREE;
4835
4836   /* A new-expression may not appear in an integral constant
4837      expression.  */
4838   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4839     return error_mark_node;
4840
4841   /* Create a representation of the new-expression.  */
4842   return build_new (placement, type, nelts, initializer, global_scope_p);
4843 }
4844
4845 /* Parse a new-placement.
4846
4847    new-placement:
4848      ( expression-list )
4849
4850    Returns the same representation as for an expression-list.  */
4851
4852 static tree
4853 cp_parser_new_placement (cp_parser* parser)
4854 {
4855   tree expression_list;
4856
4857   /* Parse the expression-list.  */
4858   expression_list = (cp_parser_parenthesized_expression_list
4859                      (parser, false, /*non_constant_p=*/NULL));
4860
4861   return expression_list;
4862 }
4863
4864 /* Parse a new-type-id.
4865
4866    new-type-id:
4867      type-specifier-seq new-declarator [opt]
4868
4869    Returns the TYPE allocated.  If the new-type-id indicates an array
4870    type, *NELTS is set to the number of elements in the last array
4871    bound; the TYPE will not include the last array bound.  */
4872
4873 static tree
4874 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4875 {
4876   cp_decl_specifier_seq type_specifier_seq;
4877   cp_declarator *new_declarator;
4878   cp_declarator *declarator;
4879   cp_declarator *outer_declarator;
4880   const char *saved_message;
4881   tree type;
4882
4883   /* The type-specifier sequence must not contain type definitions.
4884      (It cannot contain declarations of new types either, but if they
4885      are not definitions we will catch that because they are not
4886      complete.)  */
4887   saved_message = parser->type_definition_forbidden_message;
4888   parser->type_definition_forbidden_message
4889     = "types may not be defined in a new-type-id";
4890   /* Parse the type-specifier-seq.  */
4891   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4892   /* Restore the old message.  */
4893   parser->type_definition_forbidden_message = saved_message;
4894   /* Parse the new-declarator.  */
4895   new_declarator = cp_parser_new_declarator_opt (parser);
4896
4897   /* Determine the number of elements in the last array dimension, if
4898      any.  */
4899   *nelts = NULL_TREE;
4900   /* Skip down to the last array dimension.  */
4901   declarator = new_declarator;
4902   outer_declarator = NULL;
4903   while (declarator && (declarator->kind == cdk_pointer
4904                         || declarator->kind == cdk_ptrmem))
4905     {
4906       outer_declarator = declarator;
4907       declarator = declarator->declarator;
4908     }
4909   while (declarator
4910          && declarator->kind == cdk_array
4911          && declarator->declarator
4912          && declarator->declarator->kind == cdk_array)
4913     {
4914       outer_declarator = declarator;
4915       declarator = declarator->declarator;
4916     }
4917
4918   if (declarator && declarator->kind == cdk_array)
4919     {
4920       *nelts = declarator->u.array.bounds;
4921       if (*nelts == error_mark_node)
4922         *nelts = integer_one_node;
4923       else if (!processing_template_decl)
4924         {
4925           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4926                                            false))
4927             pedwarn ("size in array new must have integral type");
4928           *nelts = save_expr (cp_convert (sizetype, *nelts));
4929           if (*nelts == integer_zero_node)
4930             warning ("zero size array reserves no space");
4931         }
4932       if (outer_declarator)
4933         outer_declarator->declarator = declarator->declarator;
4934       else
4935         new_declarator = NULL;
4936     }
4937
4938   type = groktypename (&type_specifier_seq, new_declarator);
4939   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4940     {
4941       *nelts = array_type_nelts_top (type);
4942       type = TREE_TYPE (type);
4943     }
4944   return type;
4945 }
4946
4947 /* Parse an (optional) new-declarator.
4948
4949    new-declarator:
4950      ptr-operator new-declarator [opt]
4951      direct-new-declarator
4952
4953    Returns the declarator.  */
4954
4955 static cp_declarator *
4956 cp_parser_new_declarator_opt (cp_parser* parser)
4957 {
4958   enum tree_code code;
4959   tree type;
4960   cp_cv_quals cv_quals;
4961
4962   /* We don't know if there's a ptr-operator next, or not.  */
4963   cp_parser_parse_tentatively (parser);
4964   /* Look for a ptr-operator.  */
4965   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4966   /* If that worked, look for more new-declarators.  */
4967   if (cp_parser_parse_definitely (parser))
4968     {
4969       cp_declarator *declarator;
4970
4971       /* Parse another optional declarator.  */
4972       declarator = cp_parser_new_declarator_opt (parser);
4973
4974       /* Create the representation of the declarator.  */
4975       if (type)
4976         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4977       else if (code == INDIRECT_REF)
4978         declarator = make_pointer_declarator (cv_quals, declarator);
4979       else
4980         declarator = make_reference_declarator (cv_quals, declarator);
4981
4982       return declarator;
4983     }
4984
4985   /* If the next token is a `[', there is a direct-new-declarator.  */
4986   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4987     return cp_parser_direct_new_declarator (parser);
4988
4989   return NULL;
4990 }
4991
4992 /* Parse a direct-new-declarator.
4993
4994    direct-new-declarator:
4995      [ expression ]
4996      direct-new-declarator [constant-expression]
4997
4998    */
4999
5000 static cp_declarator *
5001 cp_parser_direct_new_declarator (cp_parser* parser)
5002 {
5003   cp_declarator *declarator = NULL;
5004
5005   while (true)
5006     {
5007       tree expression;
5008
5009       /* Look for the opening `['.  */
5010       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5011       /* The first expression is not required to be constant.  */
5012       if (!declarator)
5013         {
5014           expression = cp_parser_expression (parser);
5015           /* The standard requires that the expression have integral
5016              type.  DR 74 adds enumeration types.  We believe that the
5017              real intent is that these expressions be handled like the
5018              expression in a `switch' condition, which also allows
5019              classes with a single conversion to integral or
5020              enumeration type.  */
5021           if (!processing_template_decl)
5022             {
5023               expression
5024                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5025                                               expression,
5026                                               /*complain=*/true);
5027               if (!expression)
5028                 {
5029                   error ("expression in new-declarator must have integral "
5030                          "or enumeration type");
5031                   expression = error_mark_node;
5032                 }
5033             }
5034         }
5035       /* But all the other expressions must be.  */
5036       else
5037         expression
5038           = cp_parser_constant_expression (parser,
5039                                            /*allow_non_constant=*/false,
5040                                            NULL);
5041       /* Look for the closing `]'.  */
5042       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5043
5044       /* Add this bound to the declarator.  */
5045       declarator = make_array_declarator (declarator, expression);
5046
5047       /* If the next token is not a `[', then there are no more
5048          bounds.  */
5049       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5050         break;
5051     }
5052
5053   return declarator;
5054 }
5055
5056 /* Parse a new-initializer.
5057
5058    new-initializer:
5059      ( expression-list [opt] )
5060
5061    Returns a representation of the expression-list.  If there is no
5062    expression-list, VOID_ZERO_NODE is returned.  */
5063
5064 static tree
5065 cp_parser_new_initializer (cp_parser* parser)
5066 {
5067   tree expression_list;
5068
5069   expression_list = (cp_parser_parenthesized_expression_list
5070                      (parser, false, /*non_constant_p=*/NULL));
5071   if (!expression_list)
5072     expression_list = void_zero_node;
5073
5074   return expression_list;
5075 }
5076
5077 /* Parse a delete-expression.
5078
5079    delete-expression:
5080      :: [opt] delete cast-expression
5081      :: [opt] delete [ ] cast-expression
5082
5083    Returns a representation of the expression.  */
5084
5085 static tree
5086 cp_parser_delete_expression (cp_parser* parser)
5087 {
5088   bool global_scope_p;
5089   bool array_p;
5090   tree expression;
5091
5092   /* Look for the optional `::' operator.  */
5093   global_scope_p
5094     = (cp_parser_global_scope_opt (parser,
5095                                    /*current_scope_valid_p=*/false)
5096        != NULL_TREE);
5097   /* Look for the `delete' keyword.  */
5098   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5099   /* See if the array syntax is in use.  */
5100   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5101     {
5102       /* Consume the `[' token.  */
5103       cp_lexer_consume_token (parser->lexer);
5104       /* Look for the `]' token.  */
5105       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5106       /* Remember that this is the `[]' construct.  */
5107       array_p = true;
5108     }
5109   else
5110     array_p = false;
5111
5112   /* Parse the cast-expression.  */
5113   expression = cp_parser_simple_cast_expression (parser);
5114
5115   /* A delete-expression may not appear in an integral constant
5116      expression.  */
5117   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5118     return error_mark_node;
5119
5120   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5121 }
5122
5123 /* Parse a cast-expression.
5124
5125    cast-expression:
5126      unary-expression
5127      ( type-id ) cast-expression
5128
5129    Returns a representation of the expression.  */
5130
5131 static tree
5132 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5133 {
5134   /* If it's a `(', then we might be looking at a cast.  */
5135   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5136     {
5137       tree type = NULL_TREE;
5138       tree expr = NULL_TREE;
5139       bool compound_literal_p;
5140       const char *saved_message;
5141
5142       /* There's no way to know yet whether or not this is a cast.
5143          For example, `(int (3))' is a unary-expression, while `(int)
5144          3' is a cast.  So, we resort to parsing tentatively.  */
5145       cp_parser_parse_tentatively (parser);
5146       /* Types may not be defined in a cast.  */
5147       saved_message = parser->type_definition_forbidden_message;
5148       parser->type_definition_forbidden_message
5149         = "types may not be defined in casts";
5150       /* Consume the `('.  */
5151       cp_lexer_consume_token (parser->lexer);
5152       /* A very tricky bit is that `(struct S) { 3 }' is a
5153          compound-literal (which we permit in C++ as an extension).
5154          But, that construct is not a cast-expression -- it is a
5155          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5156          is legal; if the compound-literal were a cast-expression,
5157          you'd need an extra set of parentheses.)  But, if we parse
5158          the type-id, and it happens to be a class-specifier, then we
5159          will commit to the parse at that point, because we cannot
5160          undo the action that is done when creating a new class.  So,
5161          then we cannot back up and do a postfix-expression.
5162
5163          Therefore, we scan ahead to the closing `)', and check to see
5164          if the token after the `)' is a `{'.  If so, we are not
5165          looking at a cast-expression.
5166
5167          Save tokens so that we can put them back.  */
5168       cp_lexer_save_tokens (parser->lexer);
5169       /* Skip tokens until the next token is a closing parenthesis.
5170          If we find the closing `)', and the next token is a `{', then
5171          we are looking at a compound-literal.  */
5172       compound_literal_p
5173         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5174                                                   /*consume_paren=*/true)
5175            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5176       /* Roll back the tokens we skipped.  */
5177       cp_lexer_rollback_tokens (parser->lexer);
5178       /* If we were looking at a compound-literal, simulate an error
5179          so that the call to cp_parser_parse_definitely below will
5180          fail.  */
5181       if (compound_literal_p)
5182         cp_parser_simulate_error (parser);
5183       else
5184         {
5185           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5186           parser->in_type_id_in_expr_p = true;
5187           /* Look for the type-id.  */
5188           type = cp_parser_type_id (parser);
5189           /* Look for the closing `)'.  */
5190           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5191           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5192         }
5193
5194       /* Restore the saved message.  */
5195       parser->type_definition_forbidden_message = saved_message;
5196
5197       /* If ok so far, parse the dependent expression. We cannot be
5198          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5199          ctor of T, but looks like a cast to function returning T
5200          without a dependent expression.  */
5201       if (!cp_parser_error_occurred (parser))
5202         expr = cp_parser_simple_cast_expression (parser);
5203
5204       if (cp_parser_parse_definitely (parser))
5205         {
5206           /* Warn about old-style casts, if so requested.  */
5207           if (warn_old_style_cast
5208               && !in_system_header
5209               && !VOID_TYPE_P (type)
5210               && current_lang_name != lang_name_c)
5211             warning ("use of old-style cast");
5212
5213           /* Only type conversions to integral or enumeration types
5214              can be used in constant-expressions.  */
5215           if (parser->integral_constant_expression_p
5216               && !dependent_type_p (type)
5217               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5218               && (cp_parser_non_integral_constant_expression
5219                   (parser,
5220                    "a cast to a type other than an integral or "
5221                    "enumeration type")))
5222             return error_mark_node;
5223
5224           /* Perform the cast.  */
5225           expr = build_c_cast (type, expr);
5226           return expr;
5227         }
5228     }
5229
5230   /* If we get here, then it's not a cast, so it must be a
5231      unary-expression.  */
5232   return cp_parser_unary_expression (parser, address_p);
5233 }
5234
5235 /* Parse a binary expression of the general form:
5236
5237    pm-expression:
5238      cast-expression
5239      pm-expression .* cast-expression
5240      pm-expression ->* cast-expression
5241
5242    multiplicative-expression:
5243      pm-expression
5244      multiplicative-expression * pm-expression
5245      multiplicative-expression / pm-expression
5246      multiplicative-expression % pm-expression
5247
5248    additive-expression:
5249      multiplicative-expression
5250      additive-expression + multiplicative-expression
5251      additive-expression - multiplicative-expression
5252
5253    shift-expression:
5254      additive-expression
5255      shift-expression << additive-expression
5256      shift-expression >> additive-expression
5257
5258    relational-expression:
5259      shift-expression
5260      relational-expression < shift-expression
5261      relational-expression > shift-expression
5262      relational-expression <= shift-expression
5263      relational-expression >= shift-expression
5264
5265   GNU Extension:
5266   
5267    relational-expression:
5268      relational-expression <? shift-expression
5269      relational-expression >? shift-expression
5270
5271    equality-expression:
5272      relational-expression
5273      equality-expression == relational-expression
5274      equality-expression != relational-expression
5275
5276    and-expression:
5277      equality-expression
5278      and-expression & equality-expression
5279
5280    exclusive-or-expression:
5281      and-expression
5282      exclusive-or-expression ^ and-expression
5283
5284    inclusive-or-expression:
5285      exclusive-or-expression
5286      inclusive-or-expression | exclusive-or-expression
5287
5288    logical-and-expression:
5289      inclusive-or-expression
5290      logical-and-expression && inclusive-or-expression
5291
5292    logical-or-expression:
5293      logical-and-expression
5294      logical-or-expression || logical-and-expression
5295
5296    All these are implemented with a single function like:
5297
5298    binary-expression:
5299      simple-cast-expression
5300      binary-expression <token> binary-expression
5301
5302    The binops_by_token map is used to get the tree codes for each <token> type.
5303    binary-expressions are associated according to a precedence table.  */
5304
5305 #define TOKEN_PRECEDENCE(token) \
5306   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5307    ? PREC_NOT_OPERATOR \
5308    : binops_by_token[token->type].prec)
5309
5310 static tree
5311 cp_parser_binary_expression (cp_parser* parser)
5312 {
5313   cp_parser_expression_stack stack;
5314   cp_parser_expression_stack_entry *sp = &stack[0];
5315   tree lhs, rhs;
5316   cp_token *token;
5317   enum tree_code tree_type;
5318   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5319   bool overloaded_p;
5320
5321   /* Parse the first expression.  */
5322   lhs = cp_parser_simple_cast_expression (parser);
5323
5324   for (;;)
5325     {
5326       /* Get an operator token.  */
5327       token = cp_lexer_peek_token (parser->lexer);
5328       new_prec = TOKEN_PRECEDENCE (token);
5329
5330       /* Popping an entry off the stack means we completed a subexpression:
5331          - either we found a token which is not an operator (`>' where it is not
5332            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5333            will happen repeatedly;
5334          - or, we found an operator which has lower priority.  This is the case 
5335            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5336            parsing `3 * 4'. */
5337       if (new_prec <= prec)
5338         {
5339           if (sp == stack)
5340             break;
5341           else
5342             goto pop;
5343         }
5344
5345      get_rhs:
5346       tree_type = binops_by_token[token->type].tree_type;
5347
5348       /* We used the operator token. */
5349       cp_lexer_consume_token (parser->lexer);
5350
5351       /* Extract another operand.  It may be the RHS of this expression
5352          or the LHS of a new, higher priority expression.  */
5353       rhs = cp_parser_simple_cast_expression (parser);
5354
5355       /* Get another operator token.  Look up its precedence to avoid
5356          building a useless (immediately popped) stack entry for common
5357          cases such as 3 + 4 + 5 or 3 * 4 + 5.   */
5358       token = cp_lexer_peek_token (parser->lexer);
5359       lookahead_prec = TOKEN_PRECEDENCE (token);
5360       if (lookahead_prec > new_prec)
5361         {
5362           /* ... and prepare to parse the RHS of the new, higher priority
5363              expression.  Since precedence levels on the stack are
5364              monotonically increasing, we do not have to care about
5365              stack overflows.  */
5366           sp->prec = prec;
5367           sp->tree_type = tree_type;
5368           sp->lhs = lhs;
5369           sp++;
5370           lhs = rhs;
5371           prec = new_prec;
5372           new_prec = lookahead_prec;
5373           goto get_rhs;
5374
5375          pop:
5376           /* If the stack is not empty, we have parsed into LHS the right side
5377              (`4' in the example above) of an expression we had suspended.
5378              We can use the information on the stack to recover the LHS (`3') 
5379              from the stack together with the tree code (`MULT_EXPR'), and
5380              the precedence of the higher level subexpression
5381              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5382              which will be used to actually build the additive expression.  */
5383           --sp;
5384           prec = sp->prec;
5385           tree_type = sp->tree_type;
5386           rhs = lhs;
5387           lhs = sp->lhs;
5388         }
5389
5390       overloaded_p = false;
5391       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5392
5393       /* If the binary operator required the use of an overloaded operator,
5394          then this expression cannot be an integral constant-expression.
5395          An overloaded operator can be used even if both operands are
5396          otherwise permissible in an integral constant-expression if at
5397          least one of the operands is of enumeration type.  */
5398
5399       if (overloaded_p
5400           && (cp_parser_non_integral_constant_expression 
5401               (parser, "calls to overloaded operators")))
5402         return error_mark_node;
5403     }
5404
5405   return lhs;
5406 }
5407
5408
5409 /* Parse the `? expression : assignment-expression' part of a
5410    conditional-expression.  The LOGICAL_OR_EXPR is the
5411    logical-or-expression that started the conditional-expression.
5412    Returns a representation of the entire conditional-expression.
5413
5414    This routine is used by cp_parser_assignment_expression.
5415
5416      ? expression : assignment-expression
5417
5418    GNU Extensions:
5419
5420      ? : assignment-expression */
5421
5422 static tree
5423 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5424 {
5425   tree expr;
5426   tree assignment_expr;
5427
5428   /* Consume the `?' token.  */
5429   cp_lexer_consume_token (parser->lexer);
5430   if (cp_parser_allow_gnu_extensions_p (parser)
5431       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5432     /* Implicit true clause.  */
5433     expr = NULL_TREE;
5434   else
5435     /* Parse the expression.  */
5436     expr = cp_parser_expression (parser);
5437
5438   /* The next token should be a `:'.  */
5439   cp_parser_require (parser, CPP_COLON, "`:'");
5440   /* Parse the assignment-expression.  */
5441   assignment_expr = cp_parser_assignment_expression (parser);
5442
5443   /* Build the conditional-expression.  */
5444   return build_x_conditional_expr (logical_or_expr,
5445                                    expr,
5446                                    assignment_expr);
5447 }
5448
5449 /* Parse an assignment-expression.
5450
5451    assignment-expression:
5452      conditional-expression
5453      logical-or-expression assignment-operator assignment_expression
5454      throw-expression
5455
5456    Returns a representation for the expression.  */
5457
5458 static tree
5459 cp_parser_assignment_expression (cp_parser* parser)
5460 {
5461   tree expr;
5462
5463   /* If the next token is the `throw' keyword, then we're looking at
5464      a throw-expression.  */
5465   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5466     expr = cp_parser_throw_expression (parser);
5467   /* Otherwise, it must be that we are looking at a
5468      logical-or-expression.  */
5469   else
5470     {
5471       /* Parse the binary expressions (logical-or-expression).  */
5472       expr = cp_parser_binary_expression (parser);
5473       /* If the next token is a `?' then we're actually looking at a
5474          conditional-expression.  */
5475       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5476         return cp_parser_question_colon_clause (parser, expr);
5477       else
5478         {
5479           enum tree_code assignment_operator;
5480
5481           /* If it's an assignment-operator, we're using the second
5482              production.  */
5483           assignment_operator
5484             = cp_parser_assignment_operator_opt (parser);
5485           if (assignment_operator != ERROR_MARK)
5486             {
5487               tree rhs;
5488
5489               /* Parse the right-hand side of the assignment.  */
5490               rhs = cp_parser_assignment_expression (parser);
5491               /* An assignment may not appear in a
5492                  constant-expression.  */
5493               if (cp_parser_non_integral_constant_expression (parser,
5494                                                               "an assignment"))
5495                 return error_mark_node;
5496               /* Build the assignment expression.  */
5497               expr = build_x_modify_expr (expr,
5498                                           assignment_operator,
5499                                           rhs);
5500             }
5501         }
5502     }
5503
5504   return expr;
5505 }
5506
5507 /* Parse an (optional) assignment-operator.
5508
5509    assignment-operator: one of
5510      = *= /= %= += -= >>= <<= &= ^= |=
5511
5512    GNU Extension:
5513
5514    assignment-operator: one of
5515      <?= >?=
5516
5517    If the next token is an assignment operator, the corresponding tree
5518    code is returned, and the token is consumed.  For example, for
5519    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5520    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5521    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5522    operator, ERROR_MARK is returned.  */
5523
5524 static enum tree_code
5525 cp_parser_assignment_operator_opt (cp_parser* parser)
5526 {
5527   enum tree_code op;
5528   cp_token *token;
5529
5530   /* Peek at the next toen.  */
5531   token = cp_lexer_peek_token (parser->lexer);
5532
5533   switch (token->type)
5534     {
5535     case CPP_EQ:
5536       op = NOP_EXPR;
5537       break;
5538
5539     case CPP_MULT_EQ:
5540       op = MULT_EXPR;
5541       break;
5542
5543     case CPP_DIV_EQ:
5544       op = TRUNC_DIV_EXPR;
5545       break;
5546
5547     case CPP_MOD_EQ:
5548       op = TRUNC_MOD_EXPR;
5549       break;
5550
5551     case CPP_PLUS_EQ:
5552       op = PLUS_EXPR;
5553       break;
5554
5555     case CPP_MINUS_EQ:
5556       op = MINUS_EXPR;
5557       break;
5558
5559     case CPP_RSHIFT_EQ:
5560       op = RSHIFT_EXPR;
5561       break;
5562
5563     case CPP_LSHIFT_EQ:
5564       op = LSHIFT_EXPR;
5565       break;
5566
5567     case CPP_AND_EQ:
5568       op = BIT_AND_EXPR;
5569       break;
5570
5571     case CPP_XOR_EQ:
5572       op = BIT_XOR_EXPR;
5573       break;
5574
5575     case CPP_OR_EQ:
5576       op = BIT_IOR_EXPR;
5577       break;
5578
5579     case CPP_MIN_EQ:
5580       op = MIN_EXPR;
5581       break;
5582
5583     case CPP_MAX_EQ:
5584       op = MAX_EXPR;
5585       break;
5586
5587     default:
5588       /* Nothing else is an assignment operator.  */
5589       op = ERROR_MARK;
5590     }
5591
5592   /* If it was an assignment operator, consume it.  */
5593   if (op != ERROR_MARK)
5594     cp_lexer_consume_token (parser->lexer);
5595
5596   return op;
5597 }
5598
5599 /* Parse an expression.
5600
5601    expression:
5602      assignment-expression
5603      expression , assignment-expression
5604
5605    Returns a representation of the expression.  */
5606
5607 static tree
5608 cp_parser_expression (cp_parser* parser)
5609 {
5610   tree expression = NULL_TREE;
5611
5612   while (true)
5613     {
5614       tree assignment_expression;
5615
5616       /* Parse the next assignment-expression.  */
5617       assignment_expression
5618         = cp_parser_assignment_expression (parser);
5619       /* If this is the first assignment-expression, we can just
5620          save it away.  */
5621       if (!expression)
5622         expression = assignment_expression;
5623       else
5624         expression = build_x_compound_expr (expression,
5625                                             assignment_expression);
5626       /* If the next token is not a comma, then we are done with the
5627          expression.  */
5628       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5629         break;
5630       /* Consume the `,'.  */
5631       cp_lexer_consume_token (parser->lexer);
5632       /* A comma operator cannot appear in a constant-expression.  */
5633       if (cp_parser_non_integral_constant_expression (parser,
5634                                                       "a comma operator"))
5635         expression = error_mark_node;
5636     }
5637
5638   return expression;
5639 }
5640
5641 /* Parse a constant-expression.
5642
5643    constant-expression:
5644      conditional-expression
5645
5646   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5647   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5648   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5649   is false, NON_CONSTANT_P should be NULL.  */
5650
5651 static tree
5652 cp_parser_constant_expression (cp_parser* parser,
5653                                bool allow_non_constant_p,
5654                                bool *non_constant_p)
5655 {
5656   bool saved_integral_constant_expression_p;
5657   bool saved_allow_non_integral_constant_expression_p;
5658   bool saved_non_integral_constant_expression_p;
5659   tree expression;
5660
5661   /* It might seem that we could simply parse the
5662      conditional-expression, and then check to see if it were
5663      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5664      one that the compiler can figure out is constant, possibly after
5665      doing some simplifications or optimizations.  The standard has a
5666      precise definition of constant-expression, and we must honor
5667      that, even though it is somewhat more restrictive.
5668
5669      For example:
5670
5671        int i[(2, 3)];
5672
5673      is not a legal declaration, because `(2, 3)' is not a
5674      constant-expression.  The `,' operator is forbidden in a
5675      constant-expression.  However, GCC's constant-folding machinery
5676      will fold this operation to an INTEGER_CST for `3'.  */
5677
5678   /* Save the old settings.  */
5679   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5680   saved_allow_non_integral_constant_expression_p
5681     = parser->allow_non_integral_constant_expression_p;
5682   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5683   /* We are now parsing a constant-expression.  */
5684   parser->integral_constant_expression_p = true;
5685   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5686   parser->non_integral_constant_expression_p = false;
5687   /* Although the grammar says "conditional-expression", we parse an
5688      "assignment-expression", which also permits "throw-expression"
5689      and the use of assignment operators.  In the case that
5690      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5691      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5692      actually essential that we look for an assignment-expression.
5693      For example, cp_parser_initializer_clauses uses this function to
5694      determine whether a particular assignment-expression is in fact
5695      constant.  */
5696   expression = cp_parser_assignment_expression (parser);
5697   /* Restore the old settings.  */
5698   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5699   parser->allow_non_integral_constant_expression_p
5700     = saved_allow_non_integral_constant_expression_p;
5701   if (allow_non_constant_p)
5702     *non_constant_p = parser->non_integral_constant_expression_p;
5703   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5704
5705   return expression;
5706 }
5707
5708 /* Parse __builtin_offsetof.
5709
5710    offsetof-expression:
5711      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5712
5713    offsetof-member-designator:
5714      id-expression
5715      | offsetof-member-designator "." id-expression
5716      | offsetof-member-designator "[" expression "]"
5717 */
5718
5719 static tree
5720 cp_parser_builtin_offsetof (cp_parser *parser)
5721 {
5722   int save_ice_p, save_non_ice_p;
5723   tree type, expr;
5724   cp_id_kind dummy;
5725
5726   /* We're about to accept non-integral-constant things, but will
5727      definitely yield an integral constant expression.  Save and
5728      restore these values around our local parsing.  */
5729   save_ice_p = parser->integral_constant_expression_p;
5730   save_non_ice_p = parser->non_integral_constant_expression_p;
5731
5732   /* Consume the "__builtin_offsetof" token.  */
5733   cp_lexer_consume_token (parser->lexer);
5734   /* Consume the opening `('.  */
5735   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5736   /* Parse the type-id.  */
5737   type = cp_parser_type_id (parser);
5738   /* Look for the `,'.  */
5739   cp_parser_require (parser, CPP_COMMA, "`,'");
5740
5741   /* Build the (type *)null that begins the traditional offsetof macro.  */
5742   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5743
5744   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5745   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5746                                                  true, &dummy);
5747   while (true)
5748     {
5749       cp_token *token = cp_lexer_peek_token (parser->lexer);
5750       switch (token->type)
5751         {
5752         case CPP_OPEN_SQUARE:
5753           /* offsetof-member-designator "[" expression "]" */
5754           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5755           break;
5756
5757         case CPP_DOT:
5758           /* offsetof-member-designator "." identifier */
5759           cp_lexer_consume_token (parser->lexer);
5760           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5761                                                          true, &dummy);
5762           break;
5763
5764         case CPP_CLOSE_PAREN:
5765           /* Consume the ")" token.  */
5766           cp_lexer_consume_token (parser->lexer);
5767           goto success;
5768
5769         default:
5770           /* Error.  We know the following require will fail, but
5771              that gives the proper error message.  */
5772           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5773           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5774           expr = error_mark_node;
5775           goto failure;
5776         }
5777     }
5778
5779  success:
5780   /* If we're processing a template, we can't finish the semantics yet.
5781      Otherwise we can fold the entire expression now.  */
5782   if (processing_template_decl)
5783     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5784   else
5785     expr = fold_offsetof (expr);
5786
5787  failure:
5788   parser->integral_constant_expression_p = save_ice_p;
5789   parser->non_integral_constant_expression_p = save_non_ice_p;
5790
5791   return expr;
5792 }
5793
5794 /* Statements [gram.stmt.stmt]  */
5795
5796 /* Parse a statement.
5797
5798    statement:
5799      labeled-statement
5800      expression-statement
5801      compound-statement
5802      selection-statement
5803      iteration-statement
5804      jump-statement
5805      declaration-statement
5806      try-block  */
5807
5808 static void
5809 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5810 {
5811   tree statement;
5812   cp_token *token;
5813   location_t statement_location;
5814
5815   /* There is no statement yet.  */
5816   statement = NULL_TREE;
5817   /* Peek at the next token.  */
5818   token = cp_lexer_peek_token (parser->lexer);
5819   /* Remember the location of the first token in the statement.  */
5820   statement_location = token->location;
5821   /* If this is a keyword, then that will often determine what kind of
5822      statement we have.  */
5823   if (token->type == CPP_KEYWORD)
5824     {
5825       enum rid keyword = token->keyword;
5826
5827       switch (keyword)
5828         {
5829         case RID_CASE:
5830         case RID_DEFAULT:
5831           statement = cp_parser_labeled_statement (parser,
5832                                                    in_statement_expr);
5833           break;
5834
5835         case RID_IF:
5836         case RID_SWITCH:
5837           statement = cp_parser_selection_statement (parser);
5838           break;
5839
5840         case RID_WHILE:
5841         case RID_DO:
5842         case RID_FOR:
5843           statement = cp_parser_iteration_statement (parser);
5844           break;
5845
5846         case RID_BREAK:
5847         case RID_CONTINUE:
5848         case RID_RETURN:
5849         case RID_GOTO:
5850           statement = cp_parser_jump_statement (parser);
5851           break;
5852
5853         case RID_TRY:
5854           statement = cp_parser_try_block (parser);
5855           break;
5856
5857         default:
5858           /* It might be a keyword like `int' that can start a
5859              declaration-statement.  */
5860           break;
5861         }
5862     }
5863   else if (token->type == CPP_NAME)
5864     {
5865       /* If the next token is a `:', then we are looking at a
5866          labeled-statement.  */
5867       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5868       if (token->type == CPP_COLON)
5869         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5870     }
5871   /* Anything that starts with a `{' must be a compound-statement.  */
5872   else if (token->type == CPP_OPEN_BRACE)
5873     statement = cp_parser_compound_statement (parser, NULL, false);
5874   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5875      a statement all its own.  */
5876   else if (token->type == CPP_PRAGMA)
5877     {
5878       cp_lexer_handle_pragma (parser->lexer);
5879       return;
5880     }
5881
5882   /* Everything else must be a declaration-statement or an
5883      expression-statement.  Try for the declaration-statement
5884      first, unless we are looking at a `;', in which case we know that
5885      we have an expression-statement.  */
5886   if (!statement)
5887     {
5888       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5889         {
5890           cp_parser_parse_tentatively (parser);
5891           /* Try to parse the declaration-statement.  */
5892           cp_parser_declaration_statement (parser);
5893           /* If that worked, we're done.  */
5894           if (cp_parser_parse_definitely (parser))
5895             return;
5896         }
5897       /* Look for an expression-statement instead.  */
5898       statement = cp_parser_expression_statement (parser, in_statement_expr);
5899     }
5900
5901   /* Set the line number for the statement.  */
5902   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5903     SET_EXPR_LOCATION (statement, statement_location);
5904 }
5905
5906 /* Parse a labeled-statement.
5907
5908    labeled-statement:
5909      identifier : statement
5910      case constant-expression : statement
5911      default : statement
5912
5913    GNU Extension:
5914
5915    labeled-statement:
5916      case constant-expression ... constant-expression : statement
5917
5918    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5919    For an ordinary label, returns a LABEL_EXPR.  */
5920
5921 static tree
5922 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5923 {
5924   cp_token *token;
5925   tree statement = error_mark_node;
5926
5927   /* The next token should be an identifier.  */
5928   token = cp_lexer_peek_token (parser->lexer);
5929   if (token->type != CPP_NAME
5930       && token->type != CPP_KEYWORD)
5931     {
5932       cp_parser_error (parser, "expected labeled-statement");
5933       return error_mark_node;
5934     }
5935
5936   switch (token->keyword)
5937     {
5938     case RID_CASE:
5939       {
5940         tree expr, expr_hi;
5941         cp_token *ellipsis;
5942
5943         /* Consume the `case' token.  */
5944         cp_lexer_consume_token (parser->lexer);
5945         /* Parse the constant-expression.  */
5946         expr = cp_parser_constant_expression (parser,
5947                                               /*allow_non_constant_p=*/false,
5948                                               NULL);
5949
5950         ellipsis = cp_lexer_peek_token (parser->lexer);
5951         if (ellipsis->type == CPP_ELLIPSIS)
5952           {
5953             /* Consume the `...' token.  */
5954             cp_lexer_consume_token (parser->lexer);
5955             expr_hi =
5956               cp_parser_constant_expression (parser,
5957                                              /*allow_non_constant_p=*/false,
5958                                              NULL);
5959             /* We don't need to emit warnings here, as the common code
5960                will do this for us.  */
5961           }
5962         else
5963           expr_hi = NULL_TREE;
5964
5965         if (!parser->in_switch_statement_p)
5966           error ("case label %qE not within a switch statement", expr);
5967         else
5968           statement = finish_case_label (expr, expr_hi);
5969       }
5970       break;
5971
5972     case RID_DEFAULT:
5973       /* Consume the `default' token.  */
5974       cp_lexer_consume_token (parser->lexer);
5975       if (!parser->in_switch_statement_p)
5976         error ("case label not within a switch statement");
5977       else
5978         statement = finish_case_label (NULL_TREE, NULL_TREE);
5979       break;
5980
5981     default:
5982       /* Anything else must be an ordinary label.  */
5983       statement = finish_label_stmt (cp_parser_identifier (parser));
5984       break;
5985     }
5986
5987   /* Require the `:' token.  */
5988   cp_parser_require (parser, CPP_COLON, "`:'");
5989   /* Parse the labeled statement.  */
5990   cp_parser_statement (parser, in_statement_expr);
5991
5992   /* Return the label, in the case of a `case' or `default' label.  */
5993   return statement;
5994 }
5995
5996 /* Parse an expression-statement.
5997
5998    expression-statement:
5999      expression [opt] ;
6000
6001    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6002    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6003    indicates whether this expression-statement is part of an
6004    expression statement.  */
6005
6006 static tree
6007 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6008 {
6009   tree statement = NULL_TREE;
6010
6011   /* If the next token is a ';', then there is no expression
6012      statement.  */
6013   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6014     statement = cp_parser_expression (parser);
6015
6016   /* Consume the final `;'.  */
6017   cp_parser_consume_semicolon_at_end_of_statement (parser);
6018
6019   if (in_statement_expr
6020       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6021     {
6022       /* This is the final expression statement of a statement
6023          expression.  */
6024       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6025     }
6026   else if (statement)
6027     statement = finish_expr_stmt (statement);
6028   else
6029     finish_stmt ();
6030
6031   return statement;
6032 }
6033
6034 /* Parse a compound-statement.
6035
6036    compound-statement:
6037      { statement-seq [opt] }
6038
6039    Returns a tree representing the statement.  */
6040
6041 static tree
6042 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6043                               bool in_try)
6044 {
6045   tree compound_stmt;
6046
6047   /* Consume the `{'.  */
6048   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6049     return error_mark_node;
6050   /* Begin the compound-statement.  */
6051   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6052   /* Parse an (optional) statement-seq.  */
6053   cp_parser_statement_seq_opt (parser, in_statement_expr);
6054   /* Finish the compound-statement.  */
6055   finish_compound_stmt (compound_stmt);
6056   /* Consume the `}'.  */
6057   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6058
6059   return compound_stmt;
6060 }
6061
6062 /* Parse an (optional) statement-seq.
6063
6064    statement-seq:
6065      statement
6066      statement-seq [opt] statement  */
6067
6068 static void
6069 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6070 {
6071   /* Scan statements until there aren't any more.  */
6072   while (true)
6073     {
6074       /* If we're looking at a `}', then we've run out of statements.  */
6075       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6076           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6077         break;
6078
6079       /* Parse the statement.  */
6080       cp_parser_statement (parser, in_statement_expr);
6081     }
6082 }
6083
6084 /* Parse a selection-statement.
6085
6086    selection-statement:
6087      if ( condition ) statement
6088      if ( condition ) statement else statement
6089      switch ( condition ) statement
6090
6091    Returns the new IF_STMT or SWITCH_STMT.  */
6092
6093 static tree
6094 cp_parser_selection_statement (cp_parser* parser)
6095 {
6096   cp_token *token;
6097   enum rid keyword;
6098
6099   /* Peek at the next token.  */
6100   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6101
6102   /* See what kind of keyword it is.  */
6103   keyword = token->keyword;
6104   switch (keyword)
6105     {
6106     case RID_IF:
6107     case RID_SWITCH:
6108       {
6109         tree statement;
6110         tree condition;
6111
6112         /* Look for the `('.  */
6113         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6114           {
6115             cp_parser_skip_to_end_of_statement (parser);
6116             return error_mark_node;
6117           }
6118
6119         /* Begin the selection-statement.  */
6120         if (keyword == RID_IF)
6121           statement = begin_if_stmt ();
6122         else
6123           statement = begin_switch_stmt ();
6124
6125         /* Parse the condition.  */
6126         condition = cp_parser_condition (parser);
6127         /* Look for the `)'.  */
6128         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6129           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6130                                                  /*consume_paren=*/true);
6131
6132         if (keyword == RID_IF)
6133           {
6134             /* Add the condition.  */
6135             finish_if_stmt_cond (condition, statement);
6136
6137             /* Parse the then-clause.  */
6138             cp_parser_implicitly_scoped_statement (parser);
6139             finish_then_clause (statement);
6140
6141             /* If the next token is `else', parse the else-clause.  */
6142             if (cp_lexer_next_token_is_keyword (parser->lexer,
6143                                                 RID_ELSE))
6144               {
6145                 /* Consume the `else' keyword.  */
6146                 cp_lexer_consume_token (parser->lexer);
6147                 begin_else_clause (statement);
6148                 /* Parse the else-clause.  */
6149                 cp_parser_implicitly_scoped_statement (parser);
6150                 finish_else_clause (statement);
6151               }
6152
6153             /* Now we're all done with the if-statement.  */
6154             finish_if_stmt (statement);
6155           }
6156         else
6157           {
6158             bool in_switch_statement_p;
6159
6160             /* Add the condition.  */
6161             finish_switch_cond (condition, statement);
6162
6163             /* Parse the body of the switch-statement.  */
6164             in_switch_statement_p = parser->in_switch_statement_p;
6165             parser->in_switch_statement_p = true;
6166             cp_parser_implicitly_scoped_statement (parser);
6167             parser->in_switch_statement_p = in_switch_statement_p;
6168
6169             /* Now we're all done with the switch-statement.  */
6170             finish_switch_stmt (statement);
6171           }
6172
6173         return statement;
6174       }
6175       break;
6176
6177     default:
6178       cp_parser_error (parser, "expected selection-statement");
6179       return error_mark_node;
6180     }
6181 }
6182
6183 /* Parse a condition.
6184
6185    condition:
6186      expression
6187      type-specifier-seq declarator = assignment-expression
6188
6189    GNU Extension:
6190
6191    condition:
6192      type-specifier-seq declarator asm-specification [opt]
6193        attributes [opt] = assignment-expression
6194
6195    Returns the expression that should be tested.  */
6196
6197 static tree
6198 cp_parser_condition (cp_parser* parser)
6199 {
6200   cp_decl_specifier_seq type_specifiers;
6201   const char *saved_message;
6202
6203   /* Try the declaration first.  */
6204   cp_parser_parse_tentatively (parser);
6205   /* New types are not allowed in the type-specifier-seq for a
6206      condition.  */
6207   saved_message = parser->type_definition_forbidden_message;
6208   parser->type_definition_forbidden_message
6209     = "types may not be defined in conditions";
6210   /* Parse the type-specifier-seq.  */
6211   cp_parser_type_specifier_seq (parser, &type_specifiers);
6212   /* Restore the saved message.  */
6213   parser->type_definition_forbidden_message = saved_message;
6214   /* If all is well, we might be looking at a declaration.  */
6215   if (!cp_parser_error_occurred (parser))
6216     {
6217       tree decl;
6218       tree asm_specification;
6219       tree attributes;
6220       cp_declarator *declarator;
6221       tree initializer = NULL_TREE;
6222
6223       /* Parse the declarator.  */
6224       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6225                                          /*ctor_dtor_or_conv_p=*/NULL,
6226                                          /*parenthesized_p=*/NULL,
6227                                          /*member_p=*/false);
6228       /* Parse the attributes.  */
6229       attributes = cp_parser_attributes_opt (parser);
6230       /* Parse the asm-specification.  */
6231       asm_specification = cp_parser_asm_specification_opt (parser);
6232       /* If the next token is not an `=', then we might still be
6233          looking at an expression.  For example:
6234
6235            if (A(a).x)
6236
6237          looks like a decl-specifier-seq and a declarator -- but then
6238          there is no `=', so this is an expression.  */
6239       cp_parser_require (parser, CPP_EQ, "`='");
6240       /* If we did see an `=', then we are looking at a declaration
6241          for sure.  */
6242       if (cp_parser_parse_definitely (parser))
6243         {
6244           bool pop_p;   
6245
6246           /* Create the declaration.  */
6247           decl = start_decl (declarator, &type_specifiers,
6248                              /*initialized_p=*/true,
6249                              attributes, /*prefix_attributes=*/NULL_TREE,
6250                              &pop_p);
6251           /* Parse the assignment-expression.  */
6252           initializer = cp_parser_assignment_expression (parser);
6253
6254           /* Process the initializer.  */
6255           cp_finish_decl (decl,
6256                           initializer,
6257                           asm_specification,
6258                           LOOKUP_ONLYCONVERTING);
6259
6260           if (pop_p)
6261             pop_scope (DECL_CONTEXT (decl));
6262
6263           return convert_from_reference (decl);
6264         }
6265     }
6266   /* If we didn't even get past the declarator successfully, we are
6267      definitely not looking at a declaration.  */
6268   else
6269     cp_parser_abort_tentative_parse (parser);
6270
6271   /* Otherwise, we are looking at an expression.  */
6272   return cp_parser_expression (parser);
6273 }
6274
6275 /* Parse an iteration-statement.
6276
6277    iteration-statement:
6278      while ( condition ) statement
6279      do statement while ( expression ) ;
6280      for ( for-init-statement condition [opt] ; expression [opt] )
6281        statement
6282
6283    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6284
6285 static tree
6286 cp_parser_iteration_statement (cp_parser* parser)
6287 {
6288   cp_token *token;
6289   enum rid keyword;
6290   tree statement;
6291   bool in_iteration_statement_p;
6292
6293
6294   /* Peek at the next token.  */
6295   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6296   if (!token)
6297     return error_mark_node;
6298
6299   /* Remember whether or not we are already within an iteration
6300      statement.  */
6301   in_iteration_statement_p = parser->in_iteration_statement_p;
6302
6303   /* See what kind of keyword it is.  */
6304   keyword = token->keyword;
6305   switch (keyword)
6306     {
6307     case RID_WHILE:
6308       {
6309         tree condition;
6310
6311         /* Begin the while-statement.  */
6312         statement = begin_while_stmt ();
6313         /* Look for the `('.  */
6314         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6315         /* Parse the condition.  */
6316         condition = cp_parser_condition (parser);
6317         finish_while_stmt_cond (condition, statement);
6318         /* Look for the `)'.  */
6319         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6320         /* Parse the dependent statement.  */
6321         parser->in_iteration_statement_p = true;
6322         cp_parser_already_scoped_statement (parser);
6323         parser->in_iteration_statement_p = in_iteration_statement_p;
6324         /* We're done with the while-statement.  */
6325         finish_while_stmt (statement);
6326       }
6327       break;
6328
6329     case RID_DO:
6330       {
6331         tree expression;
6332
6333         /* Begin the do-statement.  */
6334         statement = begin_do_stmt ();
6335         /* Parse the body of the do-statement.  */
6336         parser->in_iteration_statement_p = true;
6337         cp_parser_implicitly_scoped_statement (parser);
6338         parser->in_iteration_statement_p = in_iteration_statement_p;
6339         finish_do_body (statement);
6340         /* Look for the `while' keyword.  */
6341         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6342         /* Look for the `('.  */
6343         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6344         /* Parse the expression.  */
6345         expression = cp_parser_expression (parser);
6346         /* We're done with the do-statement.  */
6347         finish_do_stmt (expression, statement);
6348         /* Look for the `)'.  */
6349         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6350         /* Look for the `;'.  */
6351         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6352       }
6353       break;
6354
6355     case RID_FOR:
6356       {
6357         tree condition = NULL_TREE;
6358         tree expression = NULL_TREE;
6359
6360         /* Begin the for-statement.  */
6361         statement = begin_for_stmt ();
6362         /* Look for the `('.  */
6363         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6364         /* Parse the initialization.  */
6365         cp_parser_for_init_statement (parser);
6366         finish_for_init_stmt (statement);
6367
6368         /* If there's a condition, process it.  */
6369         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6370           condition = cp_parser_condition (parser);
6371         finish_for_cond (condition, statement);
6372         /* Look for the `;'.  */
6373         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6374
6375         /* If there's an expression, process it.  */
6376         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6377           expression = cp_parser_expression (parser);
6378         finish_for_expr (expression, statement);
6379         /* Look for the `)'.  */
6380         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6381
6382         /* Parse the body of the for-statement.  */
6383         parser->in_iteration_statement_p = true;
6384         cp_parser_already_scoped_statement (parser);
6385         parser->in_iteration_statement_p = in_iteration_statement_p;
6386
6387         /* We're done with the for-statement.  */
6388         finish_for_stmt (statement);
6389       }
6390       break;
6391
6392     default:
6393       cp_parser_error (parser, "expected iteration-statement");
6394       statement = error_mark_node;
6395       break;
6396     }
6397
6398   return statement;
6399 }
6400
6401 /* Parse a for-init-statement.
6402
6403    for-init-statement:
6404      expression-statement
6405      simple-declaration  */
6406
6407 static void
6408 cp_parser_for_init_statement (cp_parser* parser)
6409 {
6410   /* If the next token is a `;', then we have an empty
6411      expression-statement.  Grammatically, this is also a
6412      simple-declaration, but an invalid one, because it does not
6413      declare anything.  Therefore, if we did not handle this case
6414      specially, we would issue an error message about an invalid
6415      declaration.  */
6416   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6417     {
6418       /* We're going to speculatively look for a declaration, falling back
6419          to an expression, if necessary.  */
6420       cp_parser_parse_tentatively (parser);
6421       /* Parse the declaration.  */
6422       cp_parser_simple_declaration (parser,
6423                                     /*function_definition_allowed_p=*/false);
6424       /* If the tentative parse failed, then we shall need to look for an
6425          expression-statement.  */
6426       if (cp_parser_parse_definitely (parser))
6427         return;
6428     }
6429
6430   cp_parser_expression_statement (parser, false);
6431 }
6432
6433 /* Parse a jump-statement.
6434
6435    jump-statement:
6436      break ;
6437      continue ;
6438      return expression [opt] ;
6439      goto identifier ;
6440
6441    GNU extension:
6442
6443    jump-statement:
6444      goto * expression ;
6445
6446    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6447
6448 static tree
6449 cp_parser_jump_statement (cp_parser* parser)
6450 {
6451   tree statement = error_mark_node;
6452   cp_token *token;
6453   enum rid keyword;
6454
6455   /* Peek at the next token.  */
6456   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6457   if (!token)
6458     return error_mark_node;
6459
6460   /* See what kind of keyword it is.  */
6461   keyword = token->keyword;
6462   switch (keyword)
6463     {
6464     case RID_BREAK:
6465       if (!parser->in_switch_statement_p
6466           && !parser->in_iteration_statement_p)
6467         {
6468           error ("break statement not within loop or switch");
6469           statement = error_mark_node;
6470         }
6471       else
6472         statement = finish_break_stmt ();
6473       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6474       break;
6475
6476     case RID_CONTINUE:
6477       if (!parser->in_iteration_statement_p)
6478         {
6479           error ("continue statement not within a loop");
6480           statement = error_mark_node;
6481         }
6482       else
6483         statement = finish_continue_stmt ();
6484       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6485       break;
6486
6487     case RID_RETURN:
6488       {
6489         tree expr;
6490
6491         /* If the next token is a `;', then there is no
6492            expression.  */
6493         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6494           expr = cp_parser_expression (parser);
6495         else
6496           expr = NULL_TREE;
6497         /* Build the return-statement.  */
6498         statement = finish_return_stmt (expr);
6499         /* Look for the final `;'.  */
6500         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6501       }
6502       break;
6503
6504     case RID_GOTO:
6505       /* Create the goto-statement.  */
6506       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6507         {
6508           /* Issue a warning about this use of a GNU extension.  */
6509           if (pedantic)
6510             pedwarn ("ISO C++ forbids computed gotos");
6511           /* Consume the '*' token.  */
6512           cp_lexer_consume_token (parser->lexer);
6513           /* Parse the dependent expression.  */
6514           finish_goto_stmt (cp_parser_expression (parser));
6515         }
6516       else
6517         finish_goto_stmt (cp_parser_identifier (parser));
6518       /* Look for the final `;'.  */
6519       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6520       break;
6521
6522     default:
6523       cp_parser_error (parser, "expected jump-statement");
6524       break;
6525     }
6526
6527   return statement;
6528 }
6529
6530 /* Parse a declaration-statement.
6531
6532    declaration-statement:
6533      block-declaration  */
6534
6535 static void
6536 cp_parser_declaration_statement (cp_parser* parser)
6537 {
6538   void *p;
6539
6540   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6541   p = obstack_alloc (&declarator_obstack, 0);
6542
6543  /* Parse the block-declaration.  */
6544   cp_parser_block_declaration (parser, /*statement_p=*/true);
6545
6546   /* Free any declarators allocated.  */
6547   obstack_free (&declarator_obstack, p);
6548
6549   /* Finish off the statement.  */
6550   finish_stmt ();
6551 }
6552
6553 /* Some dependent statements (like `if (cond) statement'), are
6554    implicitly in their own scope.  In other words, if the statement is
6555    a single statement (as opposed to a compound-statement), it is
6556    none-the-less treated as if it were enclosed in braces.  Any
6557    declarations appearing in the dependent statement are out of scope
6558    after control passes that point.  This function parses a statement,
6559    but ensures that is in its own scope, even if it is not a
6560    compound-statement.
6561
6562    Returns the new statement.  */
6563
6564 static tree
6565 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6566 {
6567   tree statement;
6568
6569   /* If the token is not a `{', then we must take special action.  */
6570   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6571     {
6572       /* Create a compound-statement.  */
6573       statement = begin_compound_stmt (0);
6574       /* Parse the dependent-statement.  */
6575       cp_parser_statement (parser, false);
6576       /* Finish the dummy compound-statement.  */
6577       finish_compound_stmt (statement);
6578     }
6579   /* Otherwise, we simply parse the statement directly.  */
6580   else
6581     statement = cp_parser_compound_statement (parser, NULL, false);
6582
6583   /* Return the statement.  */
6584   return statement;
6585 }
6586
6587 /* For some dependent statements (like `while (cond) statement'), we
6588    have already created a scope.  Therefore, even if the dependent
6589    statement is a compound-statement, we do not want to create another
6590    scope.  */
6591
6592 static void
6593 cp_parser_already_scoped_statement (cp_parser* parser)
6594 {
6595   /* If the token is a `{', then we must take special action.  */
6596   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6597     cp_parser_statement (parser, false);
6598   else
6599     {
6600       /* Avoid calling cp_parser_compound_statement, so that we
6601          don't create a new scope.  Do everything else by hand.  */
6602       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6603       cp_parser_statement_seq_opt (parser, false);
6604       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6605     }
6606 }
6607
6608 /* Declarations [gram.dcl.dcl] */
6609
6610 /* Parse an optional declaration-sequence.
6611
6612    declaration-seq:
6613      declaration
6614      declaration-seq declaration  */
6615
6616 static void
6617 cp_parser_declaration_seq_opt (cp_parser* parser)
6618 {
6619   while (true)
6620     {
6621       cp_token *token;
6622
6623       token = cp_lexer_peek_token (parser->lexer);
6624
6625       if (token->type == CPP_CLOSE_BRACE
6626           || token->type == CPP_EOF)
6627         break;
6628
6629       if (token->type == CPP_SEMICOLON)
6630         {
6631           /* A declaration consisting of a single semicolon is
6632              invalid.  Allow it unless we're being pedantic.  */
6633           cp_lexer_consume_token (parser->lexer);
6634           if (pedantic && !in_system_header)
6635             pedwarn ("extra %<;%>");
6636           continue;
6637         }
6638
6639       /* If we're entering or exiting a region that's implicitly
6640          extern "C", modify the lang context appropriately. */
6641       if (!parser->implicit_extern_c && token->implicit_extern_c)
6642         {
6643           push_lang_context (lang_name_c);
6644           parser->implicit_extern_c = true;
6645         }
6646       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6647         {
6648           pop_lang_context ();
6649           parser->implicit_extern_c = false;
6650         }
6651
6652       if (token->type == CPP_PRAGMA)
6653         {
6654           /* A top-level declaration can consist solely of a #pragma.
6655              A nested declaration cannot, so this is done here and not
6656              in cp_parser_declaration.  (A #pragma at block scope is
6657              handled in cp_parser_statement.)  */
6658           cp_lexer_handle_pragma (parser->lexer);
6659           continue;
6660         }
6661
6662       /* Parse the declaration itself.  */
6663       cp_parser_declaration (parser);
6664     }
6665 }
6666
6667 /* Parse a declaration.
6668
6669    declaration:
6670      block-declaration
6671      function-definition
6672      template-declaration
6673      explicit-instantiation
6674      explicit-specialization
6675      linkage-specification
6676      namespace-definition
6677
6678    GNU extension:
6679
6680    declaration:
6681       __extension__ declaration */
6682
6683 static void
6684 cp_parser_declaration (cp_parser* parser)
6685 {
6686   cp_token token1;
6687   cp_token token2;
6688   int saved_pedantic;
6689   void *p;
6690
6691   /* Check for the `__extension__' keyword.  */
6692   if (cp_parser_extension_opt (parser, &saved_pedantic))
6693     {
6694       /* Parse the qualified declaration.  */
6695       cp_parser_declaration (parser);
6696       /* Restore the PEDANTIC flag.  */
6697       pedantic = saved_pedantic;
6698
6699       return;
6700     }
6701
6702   /* Try to figure out what kind of declaration is present.  */
6703   token1 = *cp_lexer_peek_token (parser->lexer);
6704
6705   if (token1.type != CPP_EOF)
6706     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6707
6708   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6709   p = obstack_alloc (&declarator_obstack, 0);
6710
6711   /* If the next token is `extern' and the following token is a string
6712      literal, then we have a linkage specification.  */
6713   if (token1.keyword == RID_EXTERN
6714       && cp_parser_is_string_literal (&token2))
6715     cp_parser_linkage_specification (parser);
6716   /* If the next token is `template', then we have either a template
6717      declaration, an explicit instantiation, or an explicit
6718      specialization.  */
6719   else if (token1.keyword == RID_TEMPLATE)
6720     {
6721       /* `template <>' indicates a template specialization.  */
6722       if (token2.type == CPP_LESS
6723           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6724         cp_parser_explicit_specialization (parser);
6725       /* `template <' indicates a template declaration.  */
6726       else if (token2.type == CPP_LESS)
6727         cp_parser_template_declaration (parser, /*member_p=*/false);
6728       /* Anything else must be an explicit instantiation.  */
6729       else
6730         cp_parser_explicit_instantiation (parser);
6731     }
6732   /* If the next token is `export', then we have a template
6733      declaration.  */
6734   else if (token1.keyword == RID_EXPORT)
6735     cp_parser_template_declaration (parser, /*member_p=*/false);
6736   /* If the next token is `extern', 'static' or 'inline' and the one
6737      after that is `template', we have a GNU extended explicit
6738      instantiation directive.  */
6739   else if (cp_parser_allow_gnu_extensions_p (parser)
6740            && (token1.keyword == RID_EXTERN
6741                || token1.keyword == RID_STATIC
6742                || token1.keyword == RID_INLINE)
6743            && token2.keyword == RID_TEMPLATE)
6744     cp_parser_explicit_instantiation (parser);
6745   /* If the next token is `namespace', check for a named or unnamed
6746      namespace definition.  */
6747   else if (token1.keyword == RID_NAMESPACE
6748            && (/* A named namespace definition.  */
6749                (token2.type == CPP_NAME
6750                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6751                     == CPP_OPEN_BRACE))
6752                /* An unnamed namespace definition.  */
6753                || token2.type == CPP_OPEN_BRACE))
6754     cp_parser_namespace_definition (parser);
6755   /* We must have either a block declaration or a function
6756      definition.  */
6757   else
6758     /* Try to parse a block-declaration, or a function-definition.  */
6759     cp_parser_block_declaration (parser, /*statement_p=*/false);
6760
6761   /* Free any declarators allocated.  */
6762   obstack_free (&declarator_obstack, p);
6763 }
6764
6765 /* Parse a block-declaration.
6766
6767    block-declaration:
6768      simple-declaration
6769      asm-definition
6770      namespace-alias-definition
6771      using-declaration
6772      using-directive
6773
6774    GNU Extension:
6775
6776    block-declaration:
6777      __extension__ block-declaration
6778      label-declaration
6779
6780    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6781    part of a declaration-statement.  */
6782
6783 static void
6784 cp_parser_block_declaration (cp_parser *parser,
6785                              bool      statement_p)
6786 {
6787   cp_token *token1;
6788   int saved_pedantic;
6789
6790   /* Check for the `__extension__' keyword.  */
6791   if (cp_parser_extension_opt (parser, &saved_pedantic))
6792     {
6793       /* Parse the qualified declaration.  */
6794       cp_parser_block_declaration (parser, statement_p);
6795       /* Restore the PEDANTIC flag.  */
6796       pedantic = saved_pedantic;
6797
6798       return;
6799     }
6800
6801   /* Peek at the next token to figure out which kind of declaration is
6802      present.  */
6803   token1 = cp_lexer_peek_token (parser->lexer);
6804
6805   /* If the next keyword is `asm', we have an asm-definition.  */
6806   if (token1->keyword == RID_ASM)
6807     {
6808       if (statement_p)
6809         cp_parser_commit_to_tentative_parse (parser);
6810       cp_parser_asm_definition (parser);
6811     }
6812   /* If the next keyword is `namespace', we have a
6813      namespace-alias-definition.  */
6814   else if (token1->keyword == RID_NAMESPACE)
6815     cp_parser_namespace_alias_definition (parser);
6816   /* If the next keyword is `using', we have either a
6817      using-declaration or a using-directive.  */
6818   else if (token1->keyword == RID_USING)
6819     {
6820       cp_token *token2;
6821
6822       if (statement_p)
6823         cp_parser_commit_to_tentative_parse (parser);
6824       /* If the token after `using' is `namespace', then we have a
6825          using-directive.  */
6826       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6827       if (token2->keyword == RID_NAMESPACE)
6828         cp_parser_using_directive (parser);
6829       /* Otherwise, it's a using-declaration.  */
6830       else
6831         cp_parser_using_declaration (parser);
6832     }
6833   /* If the next keyword is `__label__' we have a label declaration.  */
6834   else if (token1->keyword == RID_LABEL)
6835     {
6836       if (statement_p)
6837         cp_parser_commit_to_tentative_parse (parser);
6838       cp_parser_label_declaration (parser);
6839     }
6840   /* Anything else must be a simple-declaration.  */
6841   else
6842     cp_parser_simple_declaration (parser, !statement_p);
6843 }
6844
6845 /* Parse a simple-declaration.
6846
6847    simple-declaration:
6848      decl-specifier-seq [opt] init-declarator-list [opt] ;
6849
6850    init-declarator-list:
6851      init-declarator
6852      init-declarator-list , init-declarator
6853
6854    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6855    function-definition as a simple-declaration.  */
6856
6857 static void
6858 cp_parser_simple_declaration (cp_parser* parser,
6859                               bool function_definition_allowed_p)
6860 {
6861   cp_decl_specifier_seq decl_specifiers;
6862   int declares_class_or_enum;
6863   bool saw_declarator;
6864
6865   /* Defer access checks until we know what is being declared; the
6866      checks for names appearing in the decl-specifier-seq should be
6867      done as if we were in the scope of the thing being declared.  */
6868   push_deferring_access_checks (dk_deferred);
6869
6870   /* Parse the decl-specifier-seq.  We have to keep track of whether
6871      or not the decl-specifier-seq declares a named class or
6872      enumeration type, since that is the only case in which the
6873      init-declarator-list is allowed to be empty.
6874
6875      [dcl.dcl]
6876
6877      In a simple-declaration, the optional init-declarator-list can be
6878      omitted only when declaring a class or enumeration, that is when
6879      the decl-specifier-seq contains either a class-specifier, an
6880      elaborated-type-specifier, or an enum-specifier.  */
6881   cp_parser_decl_specifier_seq (parser,
6882                                 CP_PARSER_FLAGS_OPTIONAL,
6883                                 &decl_specifiers,
6884                                 &declares_class_or_enum);
6885   /* We no longer need to defer access checks.  */
6886   stop_deferring_access_checks ();
6887
6888   /* In a block scope, a valid declaration must always have a
6889      decl-specifier-seq.  By not trying to parse declarators, we can
6890      resolve the declaration/expression ambiguity more quickly.  */
6891   if (!function_definition_allowed_p
6892       && !decl_specifiers.any_specifiers_p)
6893     {
6894       cp_parser_error (parser, "expected declaration");
6895       goto done;
6896     }
6897
6898   /* If the next two tokens are both identifiers, the code is
6899      erroneous. The usual cause of this situation is code like:
6900
6901        T t;
6902
6903      where "T" should name a type -- but does not.  */
6904   if (!decl_specifiers.type
6905       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6906     {
6907       /* If parsing tentatively, we should commit; we really are
6908          looking at a declaration.  */
6909       cp_parser_commit_to_tentative_parse (parser);
6910       /* Give up.  */
6911       goto done;
6912     }
6913   
6914   /* If we have seen at least one decl-specifier, and the next token
6915      is not a parenthesis, then we must be looking at a declaration.
6916      (After "int (" we might be looking at a functional cast.)  */
6917   if (decl_specifiers.any_specifiers_p 
6918       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6919     cp_parser_commit_to_tentative_parse (parser);
6920
6921   /* Keep going until we hit the `;' at the end of the simple
6922      declaration.  */
6923   saw_declarator = false;
6924   while (cp_lexer_next_token_is_not (parser->lexer,
6925                                      CPP_SEMICOLON))
6926     {
6927       cp_token *token;
6928       bool function_definition_p;
6929       tree decl;
6930
6931       saw_declarator = true;
6932       /* Parse the init-declarator.  */
6933       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6934                                         function_definition_allowed_p,
6935                                         /*member_p=*/false,
6936                                         declares_class_or_enum,
6937                                         &function_definition_p);
6938       /* If an error occurred while parsing tentatively, exit quickly.
6939          (That usually happens when in the body of a function; each
6940          statement is treated as a declaration-statement until proven
6941          otherwise.)  */
6942       if (cp_parser_error_occurred (parser))
6943         goto done;
6944       /* Handle function definitions specially.  */
6945       if (function_definition_p)
6946         {
6947           /* If the next token is a `,', then we are probably
6948              processing something like:
6949
6950                void f() {}, *p;
6951
6952              which is erroneous.  */
6953           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6954             error ("mixing declarations and function-definitions is forbidden");
6955           /* Otherwise, we're done with the list of declarators.  */
6956           else
6957             {
6958               pop_deferring_access_checks ();
6959               return;
6960             }
6961         }
6962       /* The next token should be either a `,' or a `;'.  */
6963       token = cp_lexer_peek_token (parser->lexer);
6964       /* If it's a `,', there are more declarators to come.  */
6965       if (token->type == CPP_COMMA)
6966         cp_lexer_consume_token (parser->lexer);
6967       /* If it's a `;', we are done.  */
6968       else if (token->type == CPP_SEMICOLON)
6969         break;
6970       /* Anything else is an error.  */
6971       else
6972         {
6973           /* If we have already issued an error message we don't need
6974              to issue another one.  */
6975           if (decl != error_mark_node
6976               || (cp_parser_parsing_tentatively (parser)
6977                   && !cp_parser_committed_to_tentative_parse (parser)))
6978             cp_parser_error (parser, "expected %<,%> or %<;%>");
6979           /* Skip tokens until we reach the end of the statement.  */
6980           cp_parser_skip_to_end_of_statement (parser);
6981           /* If the next token is now a `;', consume it.  */
6982           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6983             cp_lexer_consume_token (parser->lexer);
6984           goto done;
6985         }
6986       /* After the first time around, a function-definition is not
6987          allowed -- even if it was OK at first.  For example:
6988
6989            int i, f() {}
6990
6991          is not valid.  */
6992       function_definition_allowed_p = false;
6993     }
6994
6995   /* Issue an error message if no declarators are present, and the
6996      decl-specifier-seq does not itself declare a class or
6997      enumeration.  */
6998   if (!saw_declarator)
6999     {
7000       if (cp_parser_declares_only_class_p (parser))
7001         shadow_tag (&decl_specifiers);
7002       /* Perform any deferred access checks.  */
7003       perform_deferred_access_checks ();
7004     }
7005
7006   /* Consume the `;'.  */
7007   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7008
7009  done:
7010   pop_deferring_access_checks ();
7011 }
7012
7013 /* Parse a decl-specifier-seq.
7014
7015    decl-specifier-seq:
7016      decl-specifier-seq [opt] decl-specifier
7017
7018    decl-specifier:
7019      storage-class-specifier
7020      type-specifier
7021      function-specifier
7022      friend
7023      typedef
7024
7025    GNU Extension:
7026
7027    decl-specifier:
7028      attributes
7029
7030    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7031
7032    The parser flags FLAGS is used to control type-specifier parsing.
7033
7034    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7035    flags:
7036
7037      1: one of the decl-specifiers is an elaborated-type-specifier
7038         (i.e., a type declaration)
7039      2: one of the decl-specifiers is an enum-specifier or a
7040         class-specifier (i.e., a type definition)
7041
7042    */
7043
7044 static void
7045 cp_parser_decl_specifier_seq (cp_parser* parser,
7046                               cp_parser_flags flags,
7047                               cp_decl_specifier_seq *decl_specs,
7048                               int* declares_class_or_enum)
7049 {
7050   bool constructor_possible_p = !parser->in_declarator_p;
7051
7052   /* Clear DECL_SPECS.  */
7053   clear_decl_specs (decl_specs);
7054
7055   /* Assume no class or enumeration type is declared.  */
7056   *declares_class_or_enum = 0;
7057
7058   /* Keep reading specifiers until there are no more to read.  */
7059   while (true)
7060     {
7061       bool constructor_p;
7062       bool found_decl_spec;
7063       cp_token *token;
7064
7065       /* Peek at the next token.  */
7066       token = cp_lexer_peek_token (parser->lexer);
7067       /* Handle attributes.  */
7068       if (token->keyword == RID_ATTRIBUTE)
7069         {
7070           /* Parse the attributes.  */
7071           decl_specs->attributes
7072             = chainon (decl_specs->attributes,
7073                        cp_parser_attributes_opt (parser));
7074           continue;
7075         }
7076       /* Assume we will find a decl-specifier keyword.  */
7077       found_decl_spec = true;
7078       /* If the next token is an appropriate keyword, we can simply
7079          add it to the list.  */
7080       switch (token->keyword)
7081         {
7082           /* decl-specifier:
7083                friend  */
7084         case RID_FRIEND:
7085           if (decl_specs->specs[(int) ds_friend]++)
7086             error ("duplicate %<friend%>");
7087           /* Consume the token.  */
7088           cp_lexer_consume_token (parser->lexer);
7089           break;
7090
7091           /* function-specifier:
7092                inline
7093                virtual
7094                explicit  */
7095         case RID_INLINE:
7096         case RID_VIRTUAL:
7097         case RID_EXPLICIT:
7098           cp_parser_function_specifier_opt (parser, decl_specs);
7099           break;
7100
7101           /* decl-specifier:
7102                typedef  */
7103         case RID_TYPEDEF:
7104           ++decl_specs->specs[(int) ds_typedef];
7105           /* Consume the token.  */
7106           cp_lexer_consume_token (parser->lexer);
7107           /* A constructor declarator cannot appear in a typedef.  */
7108           constructor_possible_p = false;
7109           /* The "typedef" keyword can only occur in a declaration; we
7110              may as well commit at this point.  */
7111           cp_parser_commit_to_tentative_parse (parser);
7112           break;
7113
7114           /* storage-class-specifier:
7115                auto
7116                register
7117                static
7118                extern
7119                mutable
7120
7121              GNU Extension:
7122                thread  */
7123         case RID_AUTO:
7124           /* Consume the token.  */
7125           cp_lexer_consume_token (parser->lexer);
7126           cp_parser_set_storage_class (decl_specs, sc_auto);
7127           break;
7128         case RID_REGISTER:
7129           /* Consume the token.  */
7130           cp_lexer_consume_token (parser->lexer);
7131           cp_parser_set_storage_class (decl_specs, sc_register);
7132           break;
7133         case RID_STATIC:
7134           /* Consume the token.  */
7135           cp_lexer_consume_token (parser->lexer);
7136           if (decl_specs->specs[(int) ds_thread])
7137             {
7138               error ("%<__thread%> before %<static%>");
7139               decl_specs->specs[(int) ds_thread] = 0;
7140             }
7141           cp_parser_set_storage_class (decl_specs, sc_static);
7142           break;
7143         case RID_EXTERN:
7144           /* Consume the token.  */
7145           cp_lexer_consume_token (parser->lexer);
7146           if (decl_specs->specs[(int) ds_thread])
7147             {
7148               error ("%<__thread%> before %<extern%>");
7149               decl_specs->specs[(int) ds_thread] = 0;
7150             }
7151           cp_parser_set_storage_class (decl_specs, sc_extern);
7152           break;
7153         case RID_MUTABLE:
7154           /* Consume the token.  */
7155           cp_lexer_consume_token (parser->lexer);
7156           cp_parser_set_storage_class (decl_specs, sc_mutable);
7157           break;
7158         case RID_THREAD:
7159           /* Consume the token.  */
7160           cp_lexer_consume_token (parser->lexer);
7161           ++decl_specs->specs[(int) ds_thread];
7162           break;
7163
7164         default:
7165           /* We did not yet find a decl-specifier yet.  */
7166           found_decl_spec = false;
7167           break;
7168         }
7169
7170       /* Constructors are a special case.  The `S' in `S()' is not a
7171          decl-specifier; it is the beginning of the declarator.  */
7172       constructor_p
7173         = (!found_decl_spec
7174            && constructor_possible_p
7175            && (cp_parser_constructor_declarator_p
7176                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7177
7178       /* If we don't have a DECL_SPEC yet, then we must be looking at
7179          a type-specifier.  */
7180       if (!found_decl_spec && !constructor_p)
7181         {
7182           int decl_spec_declares_class_or_enum;
7183           bool is_cv_qualifier;
7184           tree type_spec;
7185
7186           type_spec
7187             = cp_parser_type_specifier (parser, flags,
7188                                         decl_specs,
7189                                         /*is_declaration=*/true,
7190                                         &decl_spec_declares_class_or_enum,
7191                                         &is_cv_qualifier);
7192
7193           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7194
7195           /* If this type-specifier referenced a user-defined type
7196              (a typedef, class-name, etc.), then we can't allow any
7197              more such type-specifiers henceforth.
7198
7199              [dcl.spec]
7200
7201              The longest sequence of decl-specifiers that could
7202              possibly be a type name is taken as the
7203              decl-specifier-seq of a declaration.  The sequence shall
7204              be self-consistent as described below.
7205
7206              [dcl.type]
7207
7208              As a general rule, at most one type-specifier is allowed
7209              in the complete decl-specifier-seq of a declaration.  The
7210              only exceptions are the following:
7211
7212              -- const or volatile can be combined with any other
7213                 type-specifier.
7214
7215              -- signed or unsigned can be combined with char, long,
7216                 short, or int.
7217
7218              -- ..
7219
7220              Example:
7221
7222                typedef char* Pc;
7223                void g (const int Pc);
7224
7225              Here, Pc is *not* part of the decl-specifier seq; it's
7226              the declarator.  Therefore, once we see a type-specifier
7227              (other than a cv-qualifier), we forbid any additional
7228              user-defined types.  We *do* still allow things like `int
7229              int' to be considered a decl-specifier-seq, and issue the
7230              error message later.  */
7231           if (type_spec && !is_cv_qualifier)
7232             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7233           /* A constructor declarator cannot follow a type-specifier.  */
7234           if (type_spec)
7235             {
7236               constructor_possible_p = false;
7237               found_decl_spec = true;
7238             }
7239         }
7240
7241       /* If we still do not have a DECL_SPEC, then there are no more
7242          decl-specifiers.  */
7243       if (!found_decl_spec)
7244         break;
7245
7246       decl_specs->any_specifiers_p = true;
7247       /* After we see one decl-specifier, further decl-specifiers are
7248          always optional.  */
7249       flags |= CP_PARSER_FLAGS_OPTIONAL;
7250     }
7251
7252   /* Don't allow a friend specifier with a class definition.  */
7253   if (decl_specs->specs[(int) ds_friend] != 0
7254       && (*declares_class_or_enum & 2))
7255     error ("class definition may not be declared a friend");
7256 }
7257
7258 /* Parse an (optional) storage-class-specifier.
7259
7260    storage-class-specifier:
7261      auto
7262      register
7263      static
7264      extern
7265      mutable
7266
7267    GNU Extension:
7268
7269    storage-class-specifier:
7270      thread
7271
7272    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7273
7274 static tree
7275 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7276 {
7277   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7278     {
7279     case RID_AUTO:
7280     case RID_REGISTER:
7281     case RID_STATIC:
7282     case RID_EXTERN:
7283     case RID_MUTABLE:
7284     case RID_THREAD:
7285       /* Consume the token.  */
7286       return cp_lexer_consume_token (parser->lexer)->value;
7287
7288     default:
7289       return NULL_TREE;
7290     }
7291 }
7292
7293 /* Parse an (optional) function-specifier.
7294
7295    function-specifier:
7296      inline
7297      virtual
7298      explicit
7299
7300    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7301    Updates DECL_SPECS, if it is non-NULL.  */
7302
7303 static tree
7304 cp_parser_function_specifier_opt (cp_parser* parser,
7305                                   cp_decl_specifier_seq *decl_specs)
7306 {
7307   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7308     {
7309     case RID_INLINE:
7310       if (decl_specs)
7311         ++decl_specs->specs[(int) ds_inline];
7312       break;
7313
7314     case RID_VIRTUAL:
7315       if (decl_specs)
7316         ++decl_specs->specs[(int) ds_virtual];
7317       break;
7318
7319     case RID_EXPLICIT:
7320       if (decl_specs)
7321         ++decl_specs->specs[(int) ds_explicit];
7322       break;
7323
7324     default:
7325       return NULL_TREE;
7326     }
7327
7328   /* Consume the token.  */
7329   return cp_lexer_consume_token (parser->lexer)->value;
7330 }
7331
7332 /* Parse a linkage-specification.
7333
7334    linkage-specification:
7335      extern string-literal { declaration-seq [opt] }
7336      extern string-literal declaration  */
7337
7338 static void
7339 cp_parser_linkage_specification (cp_parser* parser)
7340 {
7341   tree linkage;
7342
7343   /* Look for the `extern' keyword.  */
7344   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7345
7346   /* Look for the string-literal.  */
7347   linkage = cp_parser_string_literal (parser, false, false);
7348
7349   /* Transform the literal into an identifier.  If the literal is a
7350      wide-character string, or contains embedded NULs, then we can't
7351      handle it as the user wants.  */
7352   if (strlen (TREE_STRING_POINTER (linkage))
7353       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7354     {
7355       cp_parser_error (parser, "invalid linkage-specification");
7356       /* Assume C++ linkage.  */
7357       linkage = lang_name_cplusplus;
7358     }
7359   else
7360     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7361
7362   /* We're now using the new linkage.  */
7363   push_lang_context (linkage);
7364
7365   /* If the next token is a `{', then we're using the first
7366      production.  */
7367   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7368     {
7369       /* Consume the `{' token.  */
7370       cp_lexer_consume_token (parser->lexer);
7371       /* Parse the declarations.  */
7372       cp_parser_declaration_seq_opt (parser);
7373       /* Look for the closing `}'.  */
7374       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7375     }
7376   /* Otherwise, there's just one declaration.  */
7377   else
7378     {
7379       bool saved_in_unbraced_linkage_specification_p;
7380
7381       saved_in_unbraced_linkage_specification_p
7382         = parser->in_unbraced_linkage_specification_p;
7383       parser->in_unbraced_linkage_specification_p = true;
7384       have_extern_spec = true;
7385       cp_parser_declaration (parser);
7386       have_extern_spec = false;
7387       parser->in_unbraced_linkage_specification_p
7388         = saved_in_unbraced_linkage_specification_p;
7389     }
7390
7391   /* We're done with the linkage-specification.  */
7392   pop_lang_context ();
7393 }
7394
7395 /* Special member functions [gram.special] */
7396
7397 /* Parse a conversion-function-id.
7398
7399    conversion-function-id:
7400      operator conversion-type-id
7401
7402    Returns an IDENTIFIER_NODE representing the operator.  */
7403
7404 static tree
7405 cp_parser_conversion_function_id (cp_parser* parser)
7406 {
7407   tree type;
7408   tree saved_scope;
7409   tree saved_qualifying_scope;
7410   tree saved_object_scope;
7411   bool pop_p = false;
7412
7413   /* Look for the `operator' token.  */
7414   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7415     return error_mark_node;
7416   /* When we parse the conversion-type-id, the current scope will be
7417      reset.  However, we need that information in able to look up the
7418      conversion function later, so we save it here.  */
7419   saved_scope = parser->scope;
7420   saved_qualifying_scope = parser->qualifying_scope;
7421   saved_object_scope = parser->object_scope;
7422   /* We must enter the scope of the class so that the names of
7423      entities declared within the class are available in the
7424      conversion-type-id.  For example, consider:
7425
7426        struct S {
7427          typedef int I;
7428          operator I();
7429        };
7430
7431        S::operator I() { ... }
7432
7433      In order to see that `I' is a type-name in the definition, we
7434      must be in the scope of `S'.  */
7435   if (saved_scope)
7436     pop_p = push_scope (saved_scope);
7437   /* Parse the conversion-type-id.  */
7438   type = cp_parser_conversion_type_id (parser);
7439   /* Leave the scope of the class, if any.  */
7440   if (pop_p)
7441     pop_scope (saved_scope);
7442   /* Restore the saved scope.  */
7443   parser->scope = saved_scope;
7444   parser->qualifying_scope = saved_qualifying_scope;
7445   parser->object_scope = saved_object_scope;
7446   /* If the TYPE is invalid, indicate failure.  */
7447   if (type == error_mark_node)
7448     return error_mark_node;
7449   return mangle_conv_op_name_for_type (type);
7450 }
7451
7452 /* Parse a conversion-type-id:
7453
7454    conversion-type-id:
7455      type-specifier-seq conversion-declarator [opt]
7456
7457    Returns the TYPE specified.  */
7458
7459 static tree
7460 cp_parser_conversion_type_id (cp_parser* parser)
7461 {
7462   tree attributes;
7463   cp_decl_specifier_seq type_specifiers;
7464   cp_declarator *declarator;
7465   tree type_specified;
7466
7467   /* Parse the attributes.  */
7468   attributes = cp_parser_attributes_opt (parser);
7469   /* Parse the type-specifiers.  */
7470   cp_parser_type_specifier_seq (parser, &type_specifiers);
7471   /* If that didn't work, stop.  */
7472   if (type_specifiers.type == error_mark_node)
7473     return error_mark_node;
7474   /* Parse the conversion-declarator.  */
7475   declarator = cp_parser_conversion_declarator_opt (parser);
7476
7477   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7478                                     /*initialized=*/0, &attributes);
7479   if (attributes)
7480     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7481   return type_specified;
7482 }
7483
7484 /* Parse an (optional) conversion-declarator.
7485
7486    conversion-declarator:
7487      ptr-operator conversion-declarator [opt]
7488
7489    */
7490
7491 static cp_declarator *
7492 cp_parser_conversion_declarator_opt (cp_parser* parser)
7493 {
7494   enum tree_code code;
7495   tree class_type;
7496   cp_cv_quals cv_quals;
7497
7498   /* We don't know if there's a ptr-operator next, or not.  */
7499   cp_parser_parse_tentatively (parser);
7500   /* Try the ptr-operator.  */
7501   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7502   /* If it worked, look for more conversion-declarators.  */
7503   if (cp_parser_parse_definitely (parser))
7504     {
7505       cp_declarator *declarator;
7506
7507       /* Parse another optional declarator.  */
7508       declarator = cp_parser_conversion_declarator_opt (parser);
7509
7510       /* Create the representation of the declarator.  */
7511       if (class_type)
7512         declarator = make_ptrmem_declarator (cv_quals, class_type,
7513                                              declarator);
7514       else if (code == INDIRECT_REF)
7515         declarator = make_pointer_declarator (cv_quals, declarator);
7516       else
7517         declarator = make_reference_declarator (cv_quals, declarator);
7518
7519       return declarator;
7520    }
7521
7522   return NULL;
7523 }
7524
7525 /* Parse an (optional) ctor-initializer.
7526
7527    ctor-initializer:
7528      : mem-initializer-list
7529
7530    Returns TRUE iff the ctor-initializer was actually present.  */
7531
7532 static bool
7533 cp_parser_ctor_initializer_opt (cp_parser* parser)
7534 {
7535   /* If the next token is not a `:', then there is no
7536      ctor-initializer.  */
7537   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7538     {
7539       /* Do default initialization of any bases and members.  */
7540       if (DECL_CONSTRUCTOR_P (current_function_decl))
7541         finish_mem_initializers (NULL_TREE);
7542
7543       return false;
7544     }
7545
7546   /* Consume the `:' token.  */
7547   cp_lexer_consume_token (parser->lexer);
7548   /* And the mem-initializer-list.  */
7549   cp_parser_mem_initializer_list (parser);
7550
7551   return true;
7552 }
7553
7554 /* Parse a mem-initializer-list.
7555
7556    mem-initializer-list:
7557      mem-initializer
7558      mem-initializer , mem-initializer-list  */
7559
7560 static void
7561 cp_parser_mem_initializer_list (cp_parser* parser)
7562 {
7563   tree mem_initializer_list = NULL_TREE;
7564
7565   /* Let the semantic analysis code know that we are starting the
7566      mem-initializer-list.  */
7567   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7568     error ("only constructors take base initializers");
7569
7570   /* Loop through the list.  */
7571   while (true)
7572     {
7573       tree mem_initializer;
7574
7575       /* Parse the mem-initializer.  */
7576       mem_initializer = cp_parser_mem_initializer (parser);
7577       /* Add it to the list, unless it was erroneous.  */
7578       if (mem_initializer)
7579         {
7580           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7581           mem_initializer_list = mem_initializer;
7582         }
7583       /* If the next token is not a `,', we're done.  */
7584       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7585         break;
7586       /* Consume the `,' token.  */
7587       cp_lexer_consume_token (parser->lexer);
7588     }
7589
7590   /* Perform semantic analysis.  */
7591   if (DECL_CONSTRUCTOR_P (current_function_decl))
7592     finish_mem_initializers (mem_initializer_list);
7593 }
7594
7595 /* Parse a mem-initializer.
7596
7597    mem-initializer:
7598      mem-initializer-id ( expression-list [opt] )
7599
7600    GNU extension:
7601
7602    mem-initializer:
7603      ( expression-list [opt] )
7604
7605    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7606    class) or FIELD_DECL (for a non-static data member) to initialize;
7607    the TREE_VALUE is the expression-list.  */
7608
7609 static tree
7610 cp_parser_mem_initializer (cp_parser* parser)
7611 {
7612   tree mem_initializer_id;
7613   tree expression_list;
7614   tree member;
7615
7616   /* Find out what is being initialized.  */
7617   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7618     {
7619       pedwarn ("anachronistic old-style base class initializer");
7620       mem_initializer_id = NULL_TREE;
7621     }
7622   else
7623     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7624   member = expand_member_init (mem_initializer_id);
7625   if (member && !DECL_P (member))
7626     in_base_initializer = 1;
7627
7628   expression_list
7629     = cp_parser_parenthesized_expression_list (parser, false,
7630                                                /*non_constant_p=*/NULL);
7631   if (!expression_list)
7632     expression_list = void_type_node;
7633
7634   in_base_initializer = 0;
7635
7636   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7637 }
7638
7639 /* Parse a mem-initializer-id.
7640
7641    mem-initializer-id:
7642      :: [opt] nested-name-specifier [opt] class-name
7643      identifier
7644
7645    Returns a TYPE indicating the class to be initializer for the first
7646    production.  Returns an IDENTIFIER_NODE indicating the data member
7647    to be initialized for the second production.  */
7648
7649 static tree
7650 cp_parser_mem_initializer_id (cp_parser* parser)
7651 {
7652   bool global_scope_p;
7653   bool nested_name_specifier_p;
7654   bool template_p = false;
7655   tree id;
7656
7657   /* `typename' is not allowed in this context ([temp.res]).  */
7658   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7659     {
7660       error ("keyword %<typename%> not allowed in this context (a qualified "
7661              "member initializer is implicitly a type)");
7662       cp_lexer_consume_token (parser->lexer);
7663     }
7664   /* Look for the optional `::' operator.  */
7665   global_scope_p
7666     = (cp_parser_global_scope_opt (parser,
7667                                    /*current_scope_valid_p=*/false)
7668        != NULL_TREE);
7669   /* Look for the optional nested-name-specifier.  The simplest way to
7670      implement:
7671
7672        [temp.res]
7673
7674        The keyword `typename' is not permitted in a base-specifier or
7675        mem-initializer; in these contexts a qualified name that
7676        depends on a template-parameter is implicitly assumed to be a
7677        type name.
7678
7679      is to assume that we have seen the `typename' keyword at this
7680      point.  */
7681   nested_name_specifier_p
7682     = (cp_parser_nested_name_specifier_opt (parser,
7683                                             /*typename_keyword_p=*/true,
7684                                             /*check_dependency_p=*/true,
7685                                             /*type_p=*/true,
7686                                             /*is_declaration=*/true)
7687        != NULL_TREE);
7688   if (nested_name_specifier_p)
7689     template_p = cp_parser_optional_template_keyword (parser);
7690   /* If there is a `::' operator or a nested-name-specifier, then we
7691      are definitely looking for a class-name.  */
7692   if (global_scope_p || nested_name_specifier_p)
7693     return cp_parser_class_name (parser,
7694                                  /*typename_keyword_p=*/true,
7695                                  /*template_keyword_p=*/template_p,
7696                                  /*type_p=*/false,
7697                                  /*check_dependency_p=*/true,
7698                                  /*class_head_p=*/false,
7699                                  /*is_declaration=*/true);
7700   /* Otherwise, we could also be looking for an ordinary identifier.  */
7701   cp_parser_parse_tentatively (parser);
7702   /* Try a class-name.  */
7703   id = cp_parser_class_name (parser,
7704                              /*typename_keyword_p=*/true,
7705                              /*template_keyword_p=*/false,
7706                              /*type_p=*/false,
7707                              /*check_dependency_p=*/true,
7708                              /*class_head_p=*/false,
7709                              /*is_declaration=*/true);
7710   /* If we found one, we're done.  */
7711   if (cp_parser_parse_definitely (parser))
7712     return id;
7713   /* Otherwise, look for an ordinary identifier.  */
7714   return cp_parser_identifier (parser);
7715 }
7716
7717 /* Overloading [gram.over] */
7718
7719 /* Parse an operator-function-id.
7720
7721    operator-function-id:
7722      operator operator
7723
7724    Returns an IDENTIFIER_NODE for the operator which is a
7725    human-readable spelling of the identifier, e.g., `operator +'.  */
7726
7727 static tree
7728 cp_parser_operator_function_id (cp_parser* parser)
7729 {
7730   /* Look for the `operator' keyword.  */
7731   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7732     return error_mark_node;
7733   /* And then the name of the operator itself.  */
7734   return cp_parser_operator (parser);
7735 }
7736
7737 /* Parse an operator.
7738
7739    operator:
7740      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7741      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7742      || ++ -- , ->* -> () []
7743
7744    GNU Extensions:
7745
7746    operator:
7747      <? >? <?= >?=
7748
7749    Returns an IDENTIFIER_NODE for the operator which is a
7750    human-readable spelling of the identifier, e.g., `operator +'.  */
7751
7752 static tree
7753 cp_parser_operator (cp_parser* parser)
7754 {
7755   tree id = NULL_TREE;
7756   cp_token *token;
7757
7758   /* Peek at the next token.  */
7759   token = cp_lexer_peek_token (parser->lexer);
7760   /* Figure out which operator we have.  */
7761   switch (token->type)
7762     {
7763     case CPP_KEYWORD:
7764       {
7765         enum tree_code op;
7766
7767         /* The keyword should be either `new' or `delete'.  */
7768         if (token->keyword == RID_NEW)
7769           op = NEW_EXPR;
7770         else if (token->keyword == RID_DELETE)
7771           op = DELETE_EXPR;
7772         else
7773           break;
7774
7775         /* Consume the `new' or `delete' token.  */
7776         cp_lexer_consume_token (parser->lexer);
7777
7778         /* Peek at the next token.  */
7779         token = cp_lexer_peek_token (parser->lexer);
7780         /* If it's a `[' token then this is the array variant of the
7781            operator.  */
7782         if (token->type == CPP_OPEN_SQUARE)
7783           {
7784             /* Consume the `[' token.  */
7785             cp_lexer_consume_token (parser->lexer);
7786             /* Look for the `]' token.  */
7787             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7788             id = ansi_opname (op == NEW_EXPR
7789                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7790           }
7791         /* Otherwise, we have the non-array variant.  */
7792         else
7793           id = ansi_opname (op);
7794
7795         return id;
7796       }
7797
7798     case CPP_PLUS:
7799       id = ansi_opname (PLUS_EXPR);
7800       break;
7801
7802     case CPP_MINUS:
7803       id = ansi_opname (MINUS_EXPR);
7804       break;
7805
7806     case CPP_MULT:
7807       id = ansi_opname (MULT_EXPR);
7808       break;
7809
7810     case CPP_DIV:
7811       id = ansi_opname (TRUNC_DIV_EXPR);
7812       break;
7813
7814     case CPP_MOD:
7815       id = ansi_opname (TRUNC_MOD_EXPR);
7816       break;
7817
7818     case CPP_XOR:
7819       id = ansi_opname (BIT_XOR_EXPR);
7820       break;
7821
7822     case CPP_AND:
7823       id = ansi_opname (BIT_AND_EXPR);
7824       break;
7825
7826     case CPP_OR:
7827       id = ansi_opname (BIT_IOR_EXPR);
7828       break;
7829
7830     case CPP_COMPL:
7831       id = ansi_opname (BIT_NOT_EXPR);
7832       break;
7833
7834     case CPP_NOT:
7835       id = ansi_opname (TRUTH_NOT_EXPR);
7836       break;
7837
7838     case CPP_EQ:
7839       id = ansi_assopname (NOP_EXPR);
7840       break;
7841
7842     case CPP_LESS:
7843       id = ansi_opname (LT_EXPR);
7844       break;
7845
7846     case CPP_GREATER:
7847       id = ansi_opname (GT_EXPR);
7848       break;
7849
7850     case CPP_PLUS_EQ:
7851       id = ansi_assopname (PLUS_EXPR);
7852       break;
7853
7854     case CPP_MINUS_EQ:
7855       id = ansi_assopname (MINUS_EXPR);
7856       break;
7857
7858     case CPP_MULT_EQ:
7859       id = ansi_assopname (MULT_EXPR);
7860       break;
7861
7862     case CPP_DIV_EQ:
7863       id = ansi_assopname (TRUNC_DIV_EXPR);
7864       break;
7865
7866     case CPP_MOD_EQ:
7867       id = ansi_assopname (TRUNC_MOD_EXPR);
7868       break;
7869
7870     case CPP_XOR_EQ:
7871       id = ansi_assopname (BIT_XOR_EXPR);
7872       break;
7873
7874     case CPP_AND_EQ:
7875       id = ansi_assopname (BIT_AND_EXPR);
7876       break;
7877
7878     case CPP_OR_EQ:
7879       id = ansi_assopname (BIT_IOR_EXPR);
7880       break;
7881
7882     case CPP_LSHIFT:
7883       id = ansi_opname (LSHIFT_EXPR);
7884       break;
7885
7886     case CPP_RSHIFT:
7887       id = ansi_opname (RSHIFT_EXPR);
7888       break;
7889
7890     case CPP_LSHIFT_EQ:
7891       id = ansi_assopname (LSHIFT_EXPR);
7892       break;
7893
7894     case CPP_RSHIFT_EQ:
7895       id = ansi_assopname (RSHIFT_EXPR);
7896       break;
7897
7898     case CPP_EQ_EQ:
7899       id = ansi_opname (EQ_EXPR);
7900       break;
7901
7902     case CPP_NOT_EQ:
7903       id = ansi_opname (NE_EXPR);
7904       break;
7905
7906     case CPP_LESS_EQ:
7907       id = ansi_opname (LE_EXPR);
7908       break;
7909
7910     case CPP_GREATER_EQ:
7911       id = ansi_opname (GE_EXPR);
7912       break;
7913
7914     case CPP_AND_AND:
7915       id = ansi_opname (TRUTH_ANDIF_EXPR);
7916       break;
7917
7918     case CPP_OR_OR:
7919       id = ansi_opname (TRUTH_ORIF_EXPR);
7920       break;
7921
7922     case CPP_PLUS_PLUS:
7923       id = ansi_opname (POSTINCREMENT_EXPR);
7924       break;
7925
7926     case CPP_MINUS_MINUS:
7927       id = ansi_opname (PREDECREMENT_EXPR);
7928       break;
7929
7930     case CPP_COMMA:
7931       id = ansi_opname (COMPOUND_EXPR);
7932       break;
7933
7934     case CPP_DEREF_STAR:
7935       id = ansi_opname (MEMBER_REF);
7936       break;
7937
7938     case CPP_DEREF:
7939       id = ansi_opname (COMPONENT_REF);
7940       break;
7941
7942     case CPP_OPEN_PAREN:
7943       /* Consume the `('.  */
7944       cp_lexer_consume_token (parser->lexer);
7945       /* Look for the matching `)'.  */
7946       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7947       return ansi_opname (CALL_EXPR);
7948
7949     case CPP_OPEN_SQUARE:
7950       /* Consume the `['.  */
7951       cp_lexer_consume_token (parser->lexer);
7952       /* Look for the matching `]'.  */
7953       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7954       return ansi_opname (ARRAY_REF);
7955
7956       /* Extensions.  */
7957     case CPP_MIN:
7958       id = ansi_opname (MIN_EXPR);
7959       break;
7960
7961     case CPP_MAX:
7962       id = ansi_opname (MAX_EXPR);
7963       break;
7964
7965     case CPP_MIN_EQ:
7966       id = ansi_assopname (MIN_EXPR);
7967       break;
7968
7969     case CPP_MAX_EQ:
7970       id = ansi_assopname (MAX_EXPR);
7971       break;
7972
7973     default:
7974       /* Anything else is an error.  */
7975       break;
7976     }
7977
7978   /* If we have selected an identifier, we need to consume the
7979      operator token.  */
7980   if (id)
7981     cp_lexer_consume_token (parser->lexer);
7982   /* Otherwise, no valid operator name was present.  */
7983   else
7984     {
7985       cp_parser_error (parser, "expected operator");
7986       id = error_mark_node;
7987     }
7988
7989   return id;
7990 }
7991
7992 /* Parse a template-declaration.
7993
7994    template-declaration:
7995      export [opt] template < template-parameter-list > declaration
7996
7997    If MEMBER_P is TRUE, this template-declaration occurs within a
7998    class-specifier.
7999
8000    The grammar rule given by the standard isn't correct.  What
8001    is really meant is:
8002
8003    template-declaration:
8004      export [opt] template-parameter-list-seq
8005        decl-specifier-seq [opt] init-declarator [opt] ;
8006      export [opt] template-parameter-list-seq
8007        function-definition
8008
8009    template-parameter-list-seq:
8010      template-parameter-list-seq [opt]
8011      template < template-parameter-list >  */
8012
8013 static void
8014 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8015 {
8016   /* Check for `export'.  */
8017   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8018     {
8019       /* Consume the `export' token.  */
8020       cp_lexer_consume_token (parser->lexer);
8021       /* Warn that we do not support `export'.  */
8022       warning ("keyword %<export%> not implemented, and will be ignored");
8023     }
8024
8025   cp_parser_template_declaration_after_export (parser, member_p);
8026 }
8027
8028 /* Parse a template-parameter-list.
8029
8030    template-parameter-list:
8031      template-parameter
8032      template-parameter-list , template-parameter
8033
8034    Returns a TREE_LIST.  Each node represents a template parameter.
8035    The nodes are connected via their TREE_CHAINs.  */
8036
8037 static tree
8038 cp_parser_template_parameter_list (cp_parser* parser)
8039 {
8040   tree parameter_list = NULL_TREE;
8041
8042   while (true)
8043     {
8044       tree parameter;
8045       cp_token *token;
8046       bool is_non_type;
8047
8048       /* Parse the template-parameter.  */
8049       parameter = cp_parser_template_parameter (parser, &is_non_type);
8050       /* Add it to the list.  */
8051       parameter_list = process_template_parm (parameter_list,
8052                                               parameter,
8053                                               is_non_type);
8054       /* Peek at the next token.  */
8055       token = cp_lexer_peek_token (parser->lexer);
8056       /* If it's not a `,', we're done.  */
8057       if (token->type != CPP_COMMA)
8058         break;
8059       /* Otherwise, consume the `,' token.  */
8060       cp_lexer_consume_token (parser->lexer);
8061     }
8062
8063   return parameter_list;
8064 }
8065
8066 /* Parse a template-parameter.
8067
8068    template-parameter:
8069      type-parameter
8070      parameter-declaration
8071
8072    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8073    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8074    true iff this parameter is a non-type parameter.  */
8075
8076 static tree
8077 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8078 {
8079   cp_token *token;
8080   cp_parameter_declarator *parameter_declarator;
8081
8082   /* Assume it is a type parameter or a template parameter.  */
8083   *is_non_type = false;
8084   /* Peek at the next token.  */
8085   token = cp_lexer_peek_token (parser->lexer);
8086   /* If it is `class' or `template', we have a type-parameter.  */
8087   if (token->keyword == RID_TEMPLATE)
8088     return cp_parser_type_parameter (parser);
8089   /* If it is `class' or `typename' we do not know yet whether it is a
8090      type parameter or a non-type parameter.  Consider:
8091
8092        template <typename T, typename T::X X> ...
8093
8094      or:
8095
8096        template <class C, class D*> ...
8097
8098      Here, the first parameter is a type parameter, and the second is
8099      a non-type parameter.  We can tell by looking at the token after
8100      the identifier -- if it is a `,', `=', or `>' then we have a type
8101      parameter.  */
8102   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8103     {
8104       /* Peek at the token after `class' or `typename'.  */
8105       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8106       /* If it's an identifier, skip it.  */
8107       if (token->type == CPP_NAME)
8108         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8109       /* Now, see if the token looks like the end of a template
8110          parameter.  */
8111       if (token->type == CPP_COMMA
8112           || token->type == CPP_EQ
8113           || token->type == CPP_GREATER)
8114         return cp_parser_type_parameter (parser);
8115     }
8116
8117   /* Otherwise, it is a non-type parameter.
8118
8119      [temp.param]
8120
8121      When parsing a default template-argument for a non-type
8122      template-parameter, the first non-nested `>' is taken as the end
8123      of the template parameter-list rather than a greater-than
8124      operator.  */
8125   *is_non_type = true;
8126   parameter_declarator
8127      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8128                                         /*parenthesized_p=*/NULL);
8129   return (build_tree_list
8130           (parameter_declarator->default_argument,
8131            grokdeclarator (parameter_declarator->declarator,
8132                            &parameter_declarator->decl_specifiers,
8133                            PARM, /*initialized=*/0,
8134                            /*attrlist=*/NULL)));
8135 }
8136
8137 /* Parse a type-parameter.
8138
8139    type-parameter:
8140      class identifier [opt]
8141      class identifier [opt] = type-id
8142      typename identifier [opt]
8143      typename identifier [opt] = type-id
8144      template < template-parameter-list > class identifier [opt]
8145      template < template-parameter-list > class identifier [opt]
8146        = id-expression
8147
8148    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8149    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8150    the declaration of the parameter.  */
8151
8152 static tree
8153 cp_parser_type_parameter (cp_parser* parser)
8154 {
8155   cp_token *token;
8156   tree parameter;
8157
8158   /* Look for a keyword to tell us what kind of parameter this is.  */
8159   token = cp_parser_require (parser, CPP_KEYWORD,
8160                              "`class', `typename', or `template'");
8161   if (!token)
8162     return error_mark_node;
8163
8164   switch (token->keyword)
8165     {
8166     case RID_CLASS:
8167     case RID_TYPENAME:
8168       {
8169         tree identifier;
8170         tree default_argument;
8171
8172         /* If the next token is an identifier, then it names the
8173            parameter.  */
8174         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8175           identifier = cp_parser_identifier (parser);
8176         else
8177           identifier = NULL_TREE;
8178
8179         /* Create the parameter.  */
8180         parameter = finish_template_type_parm (class_type_node, identifier);
8181
8182         /* If the next token is an `=', we have a default argument.  */
8183         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8184           {
8185             /* Consume the `=' token.  */
8186             cp_lexer_consume_token (parser->lexer);
8187             /* Parse the default-argument.  */
8188             default_argument = cp_parser_type_id (parser);
8189           }
8190         else
8191           default_argument = NULL_TREE;
8192
8193         /* Create the combined representation of the parameter and the
8194            default argument.  */
8195         parameter = build_tree_list (default_argument, parameter);
8196       }
8197       break;
8198
8199     case RID_TEMPLATE:
8200       {
8201         tree parameter_list;
8202         tree identifier;
8203         tree default_argument;
8204
8205         /* Look for the `<'.  */
8206         cp_parser_require (parser, CPP_LESS, "`<'");
8207         /* Parse the template-parameter-list.  */
8208         begin_template_parm_list ();
8209         parameter_list
8210           = cp_parser_template_parameter_list (parser);
8211         parameter_list = end_template_parm_list (parameter_list);
8212         /* Look for the `>'.  */
8213         cp_parser_require (parser, CPP_GREATER, "`>'");
8214         /* Look for the `class' keyword.  */
8215         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8216         /* If the next token is an `=', then there is a
8217            default-argument.  If the next token is a `>', we are at
8218            the end of the parameter-list.  If the next token is a `,',
8219            then we are at the end of this parameter.  */
8220         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8221             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8222             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8223           identifier = cp_parser_identifier (parser);
8224         else
8225           identifier = NULL_TREE;
8226         /* Create the template parameter.  */
8227         parameter = finish_template_template_parm (class_type_node,
8228                                                    identifier);
8229
8230         /* If the next token is an `=', then there is a
8231            default-argument.  */
8232         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8233           {
8234             bool is_template;
8235
8236             /* Consume the `='.  */
8237             cp_lexer_consume_token (parser->lexer);
8238             /* Parse the id-expression.  */
8239             default_argument
8240               = cp_parser_id_expression (parser,
8241                                          /*template_keyword_p=*/false,
8242                                          /*check_dependency_p=*/true,
8243                                          /*template_p=*/&is_template,
8244                                          /*declarator_p=*/false);
8245             if (TREE_CODE (default_argument) == TYPE_DECL)
8246               /* If the id-expression was a template-id that refers to
8247                  a template-class, we already have the declaration here,
8248                  so no further lookup is needed.  */
8249                  ;
8250             else
8251               /* Look up the name.  */
8252               default_argument
8253                 = cp_parser_lookup_name (parser, default_argument,
8254                                         /*is_type=*/false,
8255                                         /*is_template=*/is_template,
8256                                         /*is_namespace=*/false,
8257                                         /*check_dependency=*/true,
8258                                         /*ambiguous_p=*/NULL);
8259             /* See if the default argument is valid.  */
8260             default_argument
8261               = check_template_template_default_arg (default_argument);
8262           }
8263         else
8264           default_argument = NULL_TREE;
8265
8266         /* Create the combined representation of the parameter and the
8267            default argument.  */
8268         parameter =  build_tree_list (default_argument, parameter);
8269       }
8270       break;
8271
8272     default:
8273       /* Anything else is an error.  */
8274       cp_parser_error (parser,
8275                        "expected %<class%>, %<typename%>, or %<template%>");
8276       parameter = error_mark_node;
8277     }
8278
8279   return parameter;
8280 }
8281
8282 /* Parse a template-id.
8283
8284    template-id:
8285      template-name < template-argument-list [opt] >
8286
8287    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8288    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8289    returned.  Otherwise, if the template-name names a function, or set
8290    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8291    names a class, returns a TYPE_DECL for the specialization.
8292
8293    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8294    uninstantiated templates.  */
8295
8296 static tree
8297 cp_parser_template_id (cp_parser *parser,
8298                        bool template_keyword_p,
8299                        bool check_dependency_p,
8300                        bool is_declaration)
8301 {
8302   tree template;
8303   tree arguments;
8304   tree template_id;
8305   cp_token_position start_of_id = 0;
8306   tree access_check = NULL_TREE;
8307   cp_token *next_token, *next_token_2;
8308   bool is_identifier;
8309
8310   /* If the next token corresponds to a template-id, there is no need
8311      to reparse it.  */
8312   next_token = cp_lexer_peek_token (parser->lexer);
8313   if (next_token->type == CPP_TEMPLATE_ID)
8314     {
8315       tree value;
8316       tree check;
8317
8318       /* Get the stored value.  */
8319       value = cp_lexer_consume_token (parser->lexer)->value;
8320       /* Perform any access checks that were deferred.  */
8321       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8322         perform_or_defer_access_check (TREE_PURPOSE (check),
8323                                        TREE_VALUE (check));
8324       /* Return the stored value.  */
8325       return TREE_VALUE (value);
8326     }
8327
8328   /* Avoid performing name lookup if there is no possibility of
8329      finding a template-id.  */
8330   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8331       || (next_token->type == CPP_NAME
8332           && !cp_parser_nth_token_starts_template_argument_list_p
8333                (parser, 2)))
8334     {
8335       cp_parser_error (parser, "expected template-id");
8336       return error_mark_node;
8337     }
8338
8339   /* Remember where the template-id starts.  */
8340   if (cp_parser_parsing_tentatively (parser)
8341       && !cp_parser_committed_to_tentative_parse (parser))
8342     start_of_id = cp_lexer_token_position (parser->lexer, false);
8343
8344   push_deferring_access_checks (dk_deferred);
8345
8346   /* Parse the template-name.  */
8347   is_identifier = false;
8348   template = cp_parser_template_name (parser, template_keyword_p,
8349                                       check_dependency_p,
8350                                       is_declaration,
8351                                       &is_identifier);
8352   if (template == error_mark_node || is_identifier)
8353     {
8354       pop_deferring_access_checks ();
8355       return template;
8356     }
8357
8358   /* If we find the sequence `[:' after a template-name, it's probably
8359      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8360      parse correctly the argument list.  */
8361   next_token = cp_lexer_peek_token (parser->lexer);
8362   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8363   if (next_token->type == CPP_OPEN_SQUARE
8364       && next_token->flags & DIGRAPH
8365       && next_token_2->type == CPP_COLON
8366       && !(next_token_2->flags & PREV_WHITE))
8367     {
8368       cp_parser_parse_tentatively (parser);
8369       /* Change `:' into `::'.  */
8370       next_token_2->type = CPP_SCOPE;
8371       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8372          CPP_LESS.  */
8373       cp_lexer_consume_token (parser->lexer);
8374       /* Parse the arguments.  */
8375       arguments = cp_parser_enclosed_template_argument_list (parser);
8376       if (!cp_parser_parse_definitely (parser))
8377         {
8378           /* If we couldn't parse an argument list, then we revert our changes
8379              and return simply an error. Maybe this is not a template-id
8380              after all.  */
8381           next_token_2->type = CPP_COLON;
8382           cp_parser_error (parser, "expected %<<%>");
8383           pop_deferring_access_checks ();
8384           return error_mark_node;
8385         }
8386       /* Otherwise, emit an error about the invalid digraph, but continue
8387          parsing because we got our argument list.  */
8388       pedwarn ("%<<::%> cannot begin a template-argument list");
8389       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8390               "between %<<%> and %<::%>");
8391       if (!flag_permissive)
8392         {
8393           static bool hint;
8394           if (!hint)
8395             {
8396               inform ("(if you use -fpermissive G++ will accept your code)");
8397               hint = true;
8398             }
8399         }
8400     }
8401   else
8402     {
8403       /* Look for the `<' that starts the template-argument-list.  */
8404       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8405         {
8406           pop_deferring_access_checks ();
8407           return error_mark_node;
8408         }
8409       /* Parse the arguments.  */
8410       arguments = cp_parser_enclosed_template_argument_list (parser);
8411     }
8412
8413   /* Build a representation of the specialization.  */
8414   if (TREE_CODE (template) == IDENTIFIER_NODE)
8415     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8416   else if (DECL_CLASS_TEMPLATE_P (template)
8417            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8418     template_id
8419       = finish_template_type (template, arguments,
8420                               cp_lexer_next_token_is (parser->lexer,
8421                                                       CPP_SCOPE));
8422   else
8423     {
8424       /* If it's not a class-template or a template-template, it should be
8425          a function-template.  */
8426       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8427                    || TREE_CODE (template) == OVERLOAD
8428                    || BASELINK_P (template)));
8429
8430       template_id = lookup_template_function (template, arguments);
8431     }
8432
8433   /* Retrieve any deferred checks.  Do not pop this access checks yet
8434      so the memory will not be reclaimed during token replacing below.  */
8435   access_check = get_deferred_access_checks ();
8436
8437   /* If parsing tentatively, replace the sequence of tokens that makes
8438      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8439      should we re-parse the token stream, we will not have to repeat
8440      the effort required to do the parse, nor will we issue duplicate
8441      error messages about problems during instantiation of the
8442      template.  */
8443   if (start_of_id)
8444     {
8445       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8446       
8447       /* Reset the contents of the START_OF_ID token.  */
8448       token->type = CPP_TEMPLATE_ID;
8449       token->value = build_tree_list (access_check, template_id);
8450       token->keyword = RID_MAX;
8451       
8452       /* Purge all subsequent tokens.  */
8453       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8454     }
8455
8456   pop_deferring_access_checks ();
8457   return template_id;
8458 }
8459
8460 /* Parse a template-name.
8461
8462    template-name:
8463      identifier
8464
8465    The standard should actually say:
8466
8467    template-name:
8468      identifier
8469      operator-function-id
8470
8471    A defect report has been filed about this issue.
8472
8473    A conversion-function-id cannot be a template name because they cannot
8474    be part of a template-id. In fact, looking at this code:
8475
8476    a.operator K<int>()
8477
8478    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8479    It is impossible to call a templated conversion-function-id with an
8480    explicit argument list, since the only allowed template parameter is
8481    the type to which it is converting.
8482
8483    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8484    `template' keyword, in a construction like:
8485
8486      T::template f<3>()
8487
8488    In that case `f' is taken to be a template-name, even though there
8489    is no way of knowing for sure.
8490
8491    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8492    name refers to a set of overloaded functions, at least one of which
8493    is a template, or an IDENTIFIER_NODE with the name of the template,
8494    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8495    names are looked up inside uninstantiated templates.  */
8496
8497 static tree
8498 cp_parser_template_name (cp_parser* parser,
8499                          bool template_keyword_p,
8500                          bool check_dependency_p,
8501                          bool is_declaration,
8502                          bool *is_identifier)
8503 {
8504   tree identifier;
8505   tree decl;
8506   tree fns;
8507
8508   /* If the next token is `operator', then we have either an
8509      operator-function-id or a conversion-function-id.  */
8510   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8511     {
8512       /* We don't know whether we're looking at an
8513          operator-function-id or a conversion-function-id.  */
8514       cp_parser_parse_tentatively (parser);
8515       /* Try an operator-function-id.  */
8516       identifier = cp_parser_operator_function_id (parser);
8517       /* If that didn't work, try a conversion-function-id.  */
8518       if (!cp_parser_parse_definitely (parser))
8519         {
8520           cp_parser_error (parser, "expected template-name");
8521           return error_mark_node;
8522         }
8523     }
8524   /* Look for the identifier.  */
8525   else
8526     identifier = cp_parser_identifier (parser);
8527
8528   /* If we didn't find an identifier, we don't have a template-id.  */
8529   if (identifier == error_mark_node)
8530     return error_mark_node;
8531
8532   /* If the name immediately followed the `template' keyword, then it
8533      is a template-name.  However, if the next token is not `<', then
8534      we do not treat it as a template-name, since it is not being used
8535      as part of a template-id.  This enables us to handle constructs
8536      like:
8537
8538        template <typename T> struct S { S(); };
8539        template <typename T> S<T>::S();
8540
8541      correctly.  We would treat `S' as a template -- if it were `S<T>'
8542      -- but we do not if there is no `<'.  */
8543
8544   if (processing_template_decl
8545       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8546     {
8547       /* In a declaration, in a dependent context, we pretend that the
8548          "template" keyword was present in order to improve error
8549          recovery.  For example, given:
8550
8551            template <typename T> void f(T::X<int>);
8552
8553          we want to treat "X<int>" as a template-id.  */
8554       if (is_declaration
8555           && !template_keyword_p
8556           && parser->scope && TYPE_P (parser->scope)
8557           && check_dependency_p
8558           && dependent_type_p (parser->scope)
8559           /* Do not do this for dtors (or ctors), since they never
8560              need the template keyword before their name.  */
8561           && !constructor_name_p (identifier, parser->scope))
8562         {
8563           cp_token_position start = 0;
8564           
8565           /* Explain what went wrong.  */
8566           error ("non-template %qD used as template", identifier);
8567           inform ("use %<%T::template %D%> to indicate that it is a template",
8568                   parser->scope, identifier);
8569           /* If parsing tentatively, find the location of the "<"
8570              token.  */
8571           if (cp_parser_parsing_tentatively (parser)
8572               && !cp_parser_committed_to_tentative_parse (parser))
8573             {
8574               cp_parser_simulate_error (parser);
8575               start = cp_lexer_token_position (parser->lexer, true);
8576             }
8577           /* Parse the template arguments so that we can issue error
8578              messages about them.  */
8579           cp_lexer_consume_token (parser->lexer);
8580           cp_parser_enclosed_template_argument_list (parser);
8581           /* Skip tokens until we find a good place from which to
8582              continue parsing.  */
8583           cp_parser_skip_to_closing_parenthesis (parser,
8584                                                  /*recovering=*/true,
8585                                                  /*or_comma=*/true,
8586                                                  /*consume_paren=*/false);
8587           /* If parsing tentatively, permanently remove the
8588              template argument list.  That will prevent duplicate
8589              error messages from being issued about the missing
8590              "template" keyword.  */
8591           if (start)
8592             cp_lexer_purge_tokens_after (parser->lexer, start);
8593           if (is_identifier)
8594             *is_identifier = true;
8595           return identifier;
8596         }
8597
8598       /* If the "template" keyword is present, then there is generally
8599          no point in doing name-lookup, so we just return IDENTIFIER.
8600          But, if the qualifying scope is non-dependent then we can
8601          (and must) do name-lookup normally.  */
8602       if (template_keyword_p
8603           && (!parser->scope
8604               || (TYPE_P (parser->scope)
8605                   && dependent_type_p (parser->scope))))
8606         return identifier;
8607     }
8608
8609   /* Look up the name.  */
8610   decl = cp_parser_lookup_name (parser, identifier,
8611                                 /*is_type=*/false,
8612                                 /*is_template=*/false,
8613                                 /*is_namespace=*/false,
8614                                 check_dependency_p,
8615                                 /*ambiguous_p=*/NULL);
8616   decl = maybe_get_template_decl_from_type_decl (decl);
8617
8618   /* If DECL is a template, then the name was a template-name.  */
8619   if (TREE_CODE (decl) == TEMPLATE_DECL)
8620     ;
8621   else
8622     {
8623       /* The standard does not explicitly indicate whether a name that
8624          names a set of overloaded declarations, some of which are
8625          templates, is a template-name.  However, such a name should
8626          be a template-name; otherwise, there is no way to form a
8627          template-id for the overloaded templates.  */
8628       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8629       if (TREE_CODE (fns) == OVERLOAD)
8630         {
8631           tree fn;
8632
8633           for (fn = fns; fn; fn = OVL_NEXT (fn))
8634             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8635               break;
8636         }
8637       else
8638         {
8639           /* Otherwise, the name does not name a template.  */
8640           cp_parser_error (parser, "expected template-name");
8641           return error_mark_node;
8642         }
8643     }
8644
8645   /* If DECL is dependent, and refers to a function, then just return
8646      its name; we will look it up again during template instantiation.  */
8647   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8648     {
8649       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8650       if (TYPE_P (scope) && dependent_type_p (scope))
8651         return identifier;
8652     }
8653
8654   return decl;
8655 }
8656
8657 /* Parse a template-argument-list.
8658
8659    template-argument-list:
8660      template-argument
8661      template-argument-list , template-argument
8662
8663    Returns a TREE_VEC containing the arguments.  */
8664
8665 static tree
8666 cp_parser_template_argument_list (cp_parser* parser)
8667 {
8668   tree fixed_args[10];
8669   unsigned n_args = 0;
8670   unsigned alloced = 10;
8671   tree *arg_ary = fixed_args;
8672   tree vec;
8673   bool saved_in_template_argument_list_p;
8674
8675   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8676   parser->in_template_argument_list_p = true;
8677   do
8678     {
8679       tree argument;
8680
8681       if (n_args)
8682         /* Consume the comma.  */
8683         cp_lexer_consume_token (parser->lexer);
8684
8685       /* Parse the template-argument.  */
8686       argument = cp_parser_template_argument (parser);
8687       if (n_args == alloced)
8688         {
8689           alloced *= 2;
8690
8691           if (arg_ary == fixed_args)
8692             {
8693               arg_ary = xmalloc (sizeof (tree) * alloced);
8694               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8695             }
8696           else
8697             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8698         }
8699       arg_ary[n_args++] = argument;
8700     }
8701   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8702
8703   vec = make_tree_vec (n_args);
8704
8705   while (n_args--)
8706     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8707
8708   if (arg_ary != fixed_args)
8709     free (arg_ary);
8710   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8711   return vec;
8712 }
8713
8714 /* Parse a template-argument.
8715
8716    template-argument:
8717      assignment-expression
8718      type-id
8719      id-expression
8720
8721    The representation is that of an assignment-expression, type-id, or
8722    id-expression -- except that the qualified id-expression is
8723    evaluated, so that the value returned is either a DECL or an
8724    OVERLOAD.
8725
8726    Although the standard says "assignment-expression", it forbids
8727    throw-expressions or assignments in the template argument.
8728    Therefore, we use "conditional-expression" instead.  */
8729
8730 static tree
8731 cp_parser_template_argument (cp_parser* parser)
8732 {
8733   tree argument;
8734   bool template_p;
8735   bool address_p;
8736   bool maybe_type_id = false;
8737   cp_token *token;
8738   cp_id_kind idk;
8739   tree qualifying_class;
8740
8741   /* There's really no way to know what we're looking at, so we just
8742      try each alternative in order.
8743
8744        [temp.arg]
8745
8746        In a template-argument, an ambiguity between a type-id and an
8747        expression is resolved to a type-id, regardless of the form of
8748        the corresponding template-parameter.
8749
8750      Therefore, we try a type-id first.  */
8751   cp_parser_parse_tentatively (parser);
8752   argument = cp_parser_type_id (parser);
8753   /* If there was no error parsing the type-id but the next token is a '>>',
8754      we probably found a typo for '> >'. But there are type-id which are
8755      also valid expressions. For instance:
8756
8757      struct X { int operator >> (int); };
8758      template <int V> struct Foo {};
8759      Foo<X () >> 5> r;
8760
8761      Here 'X()' is a valid type-id of a function type, but the user just
8762      wanted to write the expression "X() >> 5". Thus, we remember that we
8763      found a valid type-id, but we still try to parse the argument as an
8764      expression to see what happens.  */
8765   if (!cp_parser_error_occurred (parser)
8766       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8767     {
8768       maybe_type_id = true;
8769       cp_parser_abort_tentative_parse (parser);
8770     }
8771   else
8772     {
8773       /* If the next token isn't a `,' or a `>', then this argument wasn't
8774       really finished. This means that the argument is not a valid
8775       type-id.  */
8776       if (!cp_parser_next_token_ends_template_argument_p (parser))
8777         cp_parser_error (parser, "expected template-argument");
8778       /* If that worked, we're done.  */
8779       if (cp_parser_parse_definitely (parser))
8780         return argument;
8781     }
8782   /* We're still not sure what the argument will be.  */
8783   cp_parser_parse_tentatively (parser);
8784   /* Try a template.  */
8785   argument = cp_parser_id_expression (parser,
8786                                       /*template_keyword_p=*/false,
8787                                       /*check_dependency_p=*/true,
8788                                       &template_p,
8789                                       /*declarator_p=*/false);
8790   /* If the next token isn't a `,' or a `>', then this argument wasn't
8791      really finished.  */
8792   if (!cp_parser_next_token_ends_template_argument_p (parser))
8793     cp_parser_error (parser, "expected template-argument");
8794   if (!cp_parser_error_occurred (parser))
8795     {
8796       /* Figure out what is being referred to.  If the id-expression
8797          was for a class template specialization, then we will have a
8798          TYPE_DECL at this point.  There is no need to do name lookup
8799          at this point in that case.  */
8800       if (TREE_CODE (argument) != TYPE_DECL)
8801         argument = cp_parser_lookup_name (parser, argument,
8802                                           /*is_type=*/false,
8803                                           /*is_template=*/template_p,
8804                                           /*is_namespace=*/false,
8805                                           /*check_dependency=*/true,
8806                                           /*ambiguous_p=*/NULL);
8807       if (TREE_CODE (argument) != TEMPLATE_DECL
8808           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8809         cp_parser_error (parser, "expected template-name");
8810     }
8811   if (cp_parser_parse_definitely (parser))
8812     return argument;
8813   /* It must be a non-type argument.  There permitted cases are given
8814      in [temp.arg.nontype]:
8815
8816      -- an integral constant-expression of integral or enumeration
8817         type; or
8818
8819      -- the name of a non-type template-parameter; or
8820
8821      -- the name of an object or function with external linkage...
8822
8823      -- the address of an object or function with external linkage...
8824
8825      -- a pointer to member...  */
8826   /* Look for a non-type template parameter.  */
8827   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8828     {
8829       cp_parser_parse_tentatively (parser);
8830       argument = cp_parser_primary_expression (parser,
8831                                                &idk,
8832                                                &qualifying_class);
8833       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8834           || !cp_parser_next_token_ends_template_argument_p (parser))
8835         cp_parser_simulate_error (parser);
8836       if (cp_parser_parse_definitely (parser))
8837         return argument;
8838     }
8839   /* If the next token is "&", the argument must be the address of an
8840      object or function with external linkage.  */
8841   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8842   if (address_p)
8843     cp_lexer_consume_token (parser->lexer);
8844   /* See if we might have an id-expression.  */
8845   token = cp_lexer_peek_token (parser->lexer);
8846   if (token->type == CPP_NAME
8847       || token->keyword == RID_OPERATOR
8848       || token->type == CPP_SCOPE
8849       || token->type == CPP_TEMPLATE_ID
8850       || token->type == CPP_NESTED_NAME_SPECIFIER)
8851     {
8852       cp_parser_parse_tentatively (parser);
8853       argument = cp_parser_primary_expression (parser,
8854                                                &idk,
8855                                                &qualifying_class);
8856       if (cp_parser_error_occurred (parser)
8857           || !cp_parser_next_token_ends_template_argument_p (parser))
8858         cp_parser_abort_tentative_parse (parser);
8859       else
8860         {
8861           if (qualifying_class)
8862             argument = finish_qualified_id_expr (qualifying_class,
8863                                                  argument,
8864                                                  /*done=*/true,
8865                                                  address_p);
8866           if (TREE_CODE (argument) == VAR_DECL)
8867             {
8868               /* A variable without external linkage might still be a
8869                  valid constant-expression, so no error is issued here
8870                  if the external-linkage check fails.  */
8871               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8872                 cp_parser_simulate_error (parser);
8873             }
8874           else if (is_overloaded_fn (argument))
8875             /* All overloaded functions are allowed; if the external
8876                linkage test does not pass, an error will be issued
8877                later.  */
8878             ;
8879           else if (address_p
8880                    && (TREE_CODE (argument) == OFFSET_REF
8881                        || TREE_CODE (argument) == SCOPE_REF))
8882             /* A pointer-to-member.  */
8883             ;
8884           else
8885             cp_parser_simulate_error (parser);
8886
8887           if (cp_parser_parse_definitely (parser))
8888             {
8889               if (address_p)
8890                 argument = build_x_unary_op (ADDR_EXPR, argument);
8891               return argument;
8892             }
8893         }
8894     }
8895   /* If the argument started with "&", there are no other valid
8896      alternatives at this point.  */
8897   if (address_p)
8898     {
8899       cp_parser_error (parser, "invalid non-type template argument");
8900       return error_mark_node;
8901     }
8902   /* If the argument wasn't successfully parsed as a type-id followed
8903      by '>>', the argument can only be a constant expression now.
8904      Otherwise, we try parsing the constant-expression tentatively,
8905      because the argument could really be a type-id.  */
8906   if (maybe_type_id)
8907     cp_parser_parse_tentatively (parser);
8908   argument = cp_parser_constant_expression (parser,
8909                                             /*allow_non_constant_p=*/false,
8910                                             /*non_constant_p=*/NULL);
8911   argument = fold_non_dependent_expr (argument);
8912   if (!maybe_type_id)
8913     return argument;
8914   if (!cp_parser_next_token_ends_template_argument_p (parser))
8915     cp_parser_error (parser, "expected template-argument");
8916   if (cp_parser_parse_definitely (parser))
8917     return argument;
8918   /* We did our best to parse the argument as a non type-id, but that
8919      was the only alternative that matched (albeit with a '>' after
8920      it). We can assume it's just a typo from the user, and a
8921      diagnostic will then be issued.  */
8922   return cp_parser_type_id (parser);
8923 }
8924
8925 /* Parse an explicit-instantiation.
8926
8927    explicit-instantiation:
8928      template declaration
8929
8930    Although the standard says `declaration', what it really means is:
8931
8932    explicit-instantiation:
8933      template decl-specifier-seq [opt] declarator [opt] ;
8934
8935    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8936    supposed to be allowed.  A defect report has been filed about this
8937    issue.
8938
8939    GNU Extension:
8940
8941    explicit-instantiation:
8942      storage-class-specifier template
8943        decl-specifier-seq [opt] declarator [opt] ;
8944      function-specifier template
8945        decl-specifier-seq [opt] declarator [opt] ;  */
8946
8947 static void
8948 cp_parser_explicit_instantiation (cp_parser* parser)
8949 {
8950   int declares_class_or_enum;
8951   cp_decl_specifier_seq decl_specifiers;
8952   tree extension_specifier = NULL_TREE;
8953
8954   /* Look for an (optional) storage-class-specifier or
8955      function-specifier.  */
8956   if (cp_parser_allow_gnu_extensions_p (parser))
8957     {
8958       extension_specifier
8959         = cp_parser_storage_class_specifier_opt (parser);
8960       if (!extension_specifier)
8961         extension_specifier
8962           = cp_parser_function_specifier_opt (parser,
8963                                               /*decl_specs=*/NULL);
8964     }
8965
8966   /* Look for the `template' keyword.  */
8967   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8968   /* Let the front end know that we are processing an explicit
8969      instantiation.  */
8970   begin_explicit_instantiation ();
8971   /* [temp.explicit] says that we are supposed to ignore access
8972      control while processing explicit instantiation directives.  */
8973   push_deferring_access_checks (dk_no_check);
8974   /* Parse a decl-specifier-seq.  */
8975   cp_parser_decl_specifier_seq (parser,
8976                                 CP_PARSER_FLAGS_OPTIONAL,
8977                                 &decl_specifiers,
8978                                 &declares_class_or_enum);
8979   /* If there was exactly one decl-specifier, and it declared a class,
8980      and there's no declarator, then we have an explicit type
8981      instantiation.  */
8982   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8983     {
8984       tree type;
8985
8986       type = check_tag_decl (&decl_specifiers);
8987       /* Turn access control back on for names used during
8988          template instantiation.  */
8989       pop_deferring_access_checks ();
8990       if (type)
8991         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8992     }
8993   else
8994     {
8995       cp_declarator *declarator;
8996       tree decl;
8997
8998       /* Parse the declarator.  */
8999       declarator
9000         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9001                                 /*ctor_dtor_or_conv_p=*/NULL,
9002                                 /*parenthesized_p=*/NULL,
9003                                 /*member_p=*/false);
9004       cp_parser_check_for_definition_in_return_type (declarator,
9005                                                      declares_class_or_enum);
9006       if (declarator != cp_error_declarator)
9007         {
9008           decl = grokdeclarator (declarator, &decl_specifiers,
9009                                  NORMAL, 0, NULL);
9010           /* Turn access control back on for names used during
9011              template instantiation.  */
9012           pop_deferring_access_checks ();
9013           /* Do the explicit instantiation.  */
9014           do_decl_instantiation (decl, extension_specifier);
9015         }
9016       else
9017         {
9018           pop_deferring_access_checks ();
9019           /* Skip the body of the explicit instantiation.  */
9020           cp_parser_skip_to_end_of_statement (parser);
9021         }
9022     }
9023   /* We're done with the instantiation.  */
9024   end_explicit_instantiation ();
9025
9026   cp_parser_consume_semicolon_at_end_of_statement (parser);
9027 }
9028
9029 /* Parse an explicit-specialization.
9030
9031    explicit-specialization:
9032      template < > declaration
9033
9034    Although the standard says `declaration', what it really means is:
9035
9036    explicit-specialization:
9037      template <> decl-specifier [opt] init-declarator [opt] ;
9038      template <> function-definition
9039      template <> explicit-specialization
9040      template <> template-declaration  */
9041
9042 static void
9043 cp_parser_explicit_specialization (cp_parser* parser)
9044 {
9045   /* Look for the `template' keyword.  */
9046   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9047   /* Look for the `<'.  */
9048   cp_parser_require (parser, CPP_LESS, "`<'");
9049   /* Look for the `>'.  */
9050   cp_parser_require (parser, CPP_GREATER, "`>'");
9051   /* We have processed another parameter list.  */
9052   ++parser->num_template_parameter_lists;
9053   /* Let the front end know that we are beginning a specialization.  */
9054   begin_specialization ();
9055
9056   /* If the next keyword is `template', we need to figure out whether
9057      or not we're looking a template-declaration.  */
9058   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9059     {
9060       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9061           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9062         cp_parser_template_declaration_after_export (parser,
9063                                                      /*member_p=*/false);
9064       else
9065         cp_parser_explicit_specialization (parser);
9066     }
9067   else
9068     /* Parse the dependent declaration.  */
9069     cp_parser_single_declaration (parser,
9070                                   /*member_p=*/false,
9071                                   /*friend_p=*/NULL);
9072
9073   /* We're done with the specialization.  */
9074   end_specialization ();
9075   /* We're done with this parameter list.  */
9076   --parser->num_template_parameter_lists;
9077 }
9078
9079 /* Parse a type-specifier.
9080
9081    type-specifier:
9082      simple-type-specifier
9083      class-specifier
9084      enum-specifier
9085      elaborated-type-specifier
9086      cv-qualifier
9087
9088    GNU Extension:
9089
9090    type-specifier:
9091      __complex__
9092
9093    Returns a representation of the type-specifier.  For a
9094    class-specifier, enum-specifier, or elaborated-type-specifier, a
9095    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9096
9097    The parser flags FLAGS is used to control type-specifier parsing.
9098
9099    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9100    in a decl-specifier-seq.
9101
9102    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9103    class-specifier, enum-specifier, or elaborated-type-specifier, then
9104    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9105    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9106    zero.
9107
9108    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9109    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9110    is set to FALSE.  */
9111
9112 static tree
9113 cp_parser_type_specifier (cp_parser* parser,
9114                           cp_parser_flags flags,
9115                           cp_decl_specifier_seq *decl_specs,
9116                           bool is_declaration,
9117                           int* declares_class_or_enum,
9118                           bool* is_cv_qualifier)
9119 {
9120   tree type_spec = NULL_TREE;
9121   cp_token *token;
9122   enum rid keyword;
9123   cp_decl_spec ds = ds_last;
9124
9125   /* Assume this type-specifier does not declare a new type.  */
9126   if (declares_class_or_enum)
9127     *declares_class_or_enum = 0;
9128   /* And that it does not specify a cv-qualifier.  */
9129   if (is_cv_qualifier)
9130     *is_cv_qualifier = false;
9131   /* Peek at the next token.  */
9132   token = cp_lexer_peek_token (parser->lexer);
9133
9134   /* If we're looking at a keyword, we can use that to guide the
9135      production we choose.  */
9136   keyword = token->keyword;
9137   switch (keyword)
9138     {
9139     case RID_ENUM:
9140       /* 'enum' [identifier] '{' introduces an enum-specifier;
9141          'enum' <anything else> introduces an elaborated-type-specifier.  */
9142       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9143           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9144               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9145                  == CPP_OPEN_BRACE))
9146         {
9147           type_spec = cp_parser_enum_specifier (parser);
9148           if (declares_class_or_enum)
9149             *declares_class_or_enum = 2;
9150           if (decl_specs)
9151             cp_parser_set_decl_spec_type (decl_specs,
9152                                           type_spec,
9153                                           /*user_defined_p=*/true);
9154           return type_spec;
9155         }
9156       else
9157         goto elaborated_type_specifier;
9158
9159       /* Any of these indicate either a class-specifier, or an
9160          elaborated-type-specifier.  */
9161     case RID_CLASS:
9162     case RID_STRUCT:
9163     case RID_UNION:
9164       /* Parse tentatively so that we can back up if we don't find a
9165          class-specifier.  */
9166       cp_parser_parse_tentatively (parser);
9167       /* Look for the class-specifier.  */
9168       type_spec = cp_parser_class_specifier (parser);
9169       /* If that worked, we're done.  */
9170       if (cp_parser_parse_definitely (parser))
9171         {
9172           if (declares_class_or_enum)
9173             *declares_class_or_enum = 2;
9174           if (decl_specs)
9175             cp_parser_set_decl_spec_type (decl_specs,
9176                                           type_spec,
9177                                           /*user_defined_p=*/true);
9178           return type_spec;
9179         }
9180
9181       /* Fall through.  */
9182     elaborated_type_specifier:
9183       /* We're declaring (not defining) a class or enum.  */
9184       if (declares_class_or_enum)
9185         *declares_class_or_enum = 1;
9186
9187       /* Fall through.  */
9188     case RID_TYPENAME:
9189       /* Look for an elaborated-type-specifier.  */
9190       type_spec
9191         = (cp_parser_elaborated_type_specifier
9192            (parser,
9193             decl_specs && decl_specs->specs[(int) ds_friend],
9194             is_declaration));
9195       if (decl_specs)
9196         cp_parser_set_decl_spec_type (decl_specs,
9197                                       type_spec,
9198                                       /*user_defined_p=*/true);
9199       return type_spec;
9200
9201     case RID_CONST:
9202       ds = ds_const;
9203       if (is_cv_qualifier)
9204         *is_cv_qualifier = true;
9205       break;
9206
9207     case RID_VOLATILE:
9208       ds = ds_volatile;
9209       if (is_cv_qualifier)
9210         *is_cv_qualifier = true;
9211       break;
9212
9213     case RID_RESTRICT:
9214       ds = ds_restrict;
9215       if (is_cv_qualifier)
9216         *is_cv_qualifier = true;
9217       break;
9218
9219     case RID_COMPLEX:
9220       /* The `__complex__' keyword is a GNU extension.  */
9221       ds = ds_complex;
9222       break;
9223
9224     default:
9225       break;
9226     }
9227
9228   /* Handle simple keywords.  */
9229   if (ds != ds_last)
9230     {
9231       if (decl_specs)
9232         {
9233           ++decl_specs->specs[(int)ds];
9234           decl_specs->any_specifiers_p = true;
9235         }
9236       return cp_lexer_consume_token (parser->lexer)->value;
9237     }
9238
9239   /* If we do not already have a type-specifier, assume we are looking
9240      at a simple-type-specifier.  */
9241   type_spec = cp_parser_simple_type_specifier (parser,
9242                                                decl_specs,
9243                                                flags);
9244
9245   /* If we didn't find a type-specifier, and a type-specifier was not
9246      optional in this context, issue an error message.  */
9247   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9248     {
9249       cp_parser_error (parser, "expected type specifier");
9250       return error_mark_node;
9251     }
9252
9253   return type_spec;
9254 }
9255
9256 /* Parse a simple-type-specifier.
9257
9258    simple-type-specifier:
9259      :: [opt] nested-name-specifier [opt] type-name
9260      :: [opt] nested-name-specifier template template-id
9261      char
9262      wchar_t
9263      bool
9264      short
9265      int
9266      long
9267      signed
9268      unsigned
9269      float
9270      double
9271      void
9272
9273    GNU Extension:
9274
9275    simple-type-specifier:
9276      __typeof__ unary-expression
9277      __typeof__ ( type-id )
9278
9279    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9280    appropriately updated.  */
9281
9282 static tree
9283 cp_parser_simple_type_specifier (cp_parser* parser,
9284                                  cp_decl_specifier_seq *decl_specs,
9285                                  cp_parser_flags flags)
9286 {
9287   tree type = NULL_TREE;
9288   cp_token *token;
9289
9290   /* Peek at the next token.  */
9291   token = cp_lexer_peek_token (parser->lexer);
9292
9293   /* If we're looking at a keyword, things are easy.  */
9294   switch (token->keyword)
9295     {
9296     case RID_CHAR:
9297       if (decl_specs)
9298         decl_specs->explicit_char_p = true;
9299       type = char_type_node;
9300       break;
9301     case RID_WCHAR:
9302       type = wchar_type_node;
9303       break;
9304     case RID_BOOL:
9305       type = boolean_type_node;
9306       break;
9307     case RID_SHORT:
9308       if (decl_specs)
9309         ++decl_specs->specs[(int) ds_short];
9310       type = short_integer_type_node;
9311       break;
9312     case RID_INT:
9313       if (decl_specs)
9314         decl_specs->explicit_int_p = true;
9315       type = integer_type_node;
9316       break;
9317     case RID_LONG:
9318       if (decl_specs)
9319         ++decl_specs->specs[(int) ds_long];
9320       type = long_integer_type_node;
9321       break;
9322     case RID_SIGNED:
9323       if (decl_specs)
9324         ++decl_specs->specs[(int) ds_signed];
9325       type = integer_type_node;
9326       break;
9327     case RID_UNSIGNED:
9328       if (decl_specs)
9329         ++decl_specs->specs[(int) ds_unsigned];
9330       type = unsigned_type_node;
9331       break;
9332     case RID_FLOAT:
9333       type = float_type_node;
9334       break;
9335     case RID_DOUBLE:
9336       type = double_type_node;
9337       break;
9338     case RID_VOID:
9339       type = void_type_node;
9340       break;
9341
9342     case RID_TYPEOF:
9343       /* Consume the `typeof' token.  */
9344       cp_lexer_consume_token (parser->lexer);
9345       /* Parse the operand to `typeof'.  */
9346       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9347       /* If it is not already a TYPE, take its type.  */
9348       if (!TYPE_P (type))
9349         type = finish_typeof (type);
9350
9351       if (decl_specs)
9352         cp_parser_set_decl_spec_type (decl_specs, type,
9353                                       /*user_defined_p=*/true);
9354
9355       return type;
9356
9357     default:
9358       break;
9359     }
9360
9361   /* If the type-specifier was for a built-in type, we're done.  */
9362   if (type)
9363     {
9364       tree id;
9365
9366       /* Record the type.  */
9367       if (decl_specs
9368           && (token->keyword != RID_SIGNED
9369               && token->keyword != RID_UNSIGNED
9370               && token->keyword != RID_SHORT
9371               && token->keyword != RID_LONG))
9372         cp_parser_set_decl_spec_type (decl_specs,
9373                                       type,
9374                                       /*user_defined=*/false);
9375       if (decl_specs)
9376         decl_specs->any_specifiers_p = true;
9377
9378       /* Consume the token.  */
9379       id = cp_lexer_consume_token (parser->lexer)->value;
9380
9381       /* There is no valid C++ program where a non-template type is
9382          followed by a "<".  That usually indicates that the user thought
9383          that the type was a template.  */
9384       cp_parser_check_for_invalid_template_id (parser, type);
9385
9386       return TYPE_NAME (type);
9387     }
9388
9389   /* The type-specifier must be a user-defined type.  */
9390   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9391     {
9392       bool qualified_p;
9393       bool global_p;
9394
9395       /* Don't gobble tokens or issue error messages if this is an
9396          optional type-specifier.  */
9397       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9398         cp_parser_parse_tentatively (parser);
9399
9400       /* Look for the optional `::' operator.  */
9401       global_p
9402         = (cp_parser_global_scope_opt (parser,
9403                                        /*current_scope_valid_p=*/false)
9404            != NULL_TREE);
9405       /* Look for the nested-name specifier.  */
9406       qualified_p
9407         = (cp_parser_nested_name_specifier_opt (parser,
9408                                                 /*typename_keyword_p=*/false,
9409                                                 /*check_dependency_p=*/true,
9410                                                 /*type_p=*/false,
9411                                                 /*is_declaration=*/false)
9412            != NULL_TREE);
9413       /* If we have seen a nested-name-specifier, and the next token
9414          is `template', then we are using the template-id production.  */
9415       if (parser->scope
9416           && cp_parser_optional_template_keyword (parser))
9417         {
9418           /* Look for the template-id.  */
9419           type = cp_parser_template_id (parser,
9420                                         /*template_keyword_p=*/true,
9421                                         /*check_dependency_p=*/true,
9422                                         /*is_declaration=*/false);
9423           /* If the template-id did not name a type, we are out of
9424              luck.  */
9425           if (TREE_CODE (type) != TYPE_DECL)
9426             {
9427               cp_parser_error (parser, "expected template-id for type");
9428               type = NULL_TREE;
9429             }
9430         }
9431       /* Otherwise, look for a type-name.  */
9432       else
9433         type = cp_parser_type_name (parser);
9434       /* Keep track of all name-lookups performed in class scopes.  */
9435       if (type
9436           && !global_p
9437           && !qualified_p
9438           && TREE_CODE (type) == TYPE_DECL
9439           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9440         maybe_note_name_used_in_class (DECL_NAME (type), type);
9441       /* If it didn't work out, we don't have a TYPE.  */
9442       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9443           && !cp_parser_parse_definitely (parser))
9444         type = NULL_TREE;
9445       if (type && decl_specs)
9446         cp_parser_set_decl_spec_type (decl_specs, type,
9447                                       /*user_defined=*/true);
9448     }
9449
9450   /* If we didn't get a type-name, issue an error message.  */
9451   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9452     {
9453       cp_parser_error (parser, "expected type-name");
9454       return error_mark_node;
9455     }
9456
9457   /* There is no valid C++ program where a non-template type is
9458      followed by a "<".  That usually indicates that the user thought
9459      that the type was a template.  */
9460   if (type && type != error_mark_node)
9461     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9462
9463   return type;
9464 }
9465
9466 /* Parse a type-name.
9467
9468    type-name:
9469      class-name
9470      enum-name
9471      typedef-name
9472
9473    enum-name:
9474      identifier
9475
9476    typedef-name:
9477      identifier
9478
9479    Returns a TYPE_DECL for the the type.  */
9480
9481 static tree
9482 cp_parser_type_name (cp_parser* parser)
9483 {
9484   tree type_decl;
9485   tree identifier;
9486
9487   /* We can't know yet whether it is a class-name or not.  */
9488   cp_parser_parse_tentatively (parser);
9489   /* Try a class-name.  */
9490   type_decl = cp_parser_class_name (parser,
9491                                     /*typename_keyword_p=*/false,
9492                                     /*template_keyword_p=*/false,
9493                                     /*type_p=*/false,
9494                                     /*check_dependency_p=*/true,
9495                                     /*class_head_p=*/false,
9496                                     /*is_declaration=*/false);
9497   /* If it's not a class-name, keep looking.  */
9498   if (!cp_parser_parse_definitely (parser))
9499     {
9500       /* It must be a typedef-name or an enum-name.  */
9501       identifier = cp_parser_identifier (parser);
9502       if (identifier == error_mark_node)
9503         return error_mark_node;
9504
9505       /* Look up the type-name.  */
9506       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9507       /* Issue an error if we did not find a type-name.  */
9508       if (TREE_CODE (type_decl) != TYPE_DECL)
9509         {
9510           if (!cp_parser_simulate_error (parser))
9511             cp_parser_name_lookup_error (parser, identifier, type_decl,
9512                                          "is not a type");
9513           type_decl = error_mark_node;
9514         }
9515       /* Remember that the name was used in the definition of the
9516          current class so that we can check later to see if the
9517          meaning would have been different after the class was
9518          entirely defined.  */
9519       else if (type_decl != error_mark_node
9520                && !parser->scope)
9521         maybe_note_name_used_in_class (identifier, type_decl);
9522     }
9523
9524   return type_decl;
9525 }
9526
9527
9528 /* Parse an elaborated-type-specifier.  Note that the grammar given
9529    here incorporates the resolution to DR68.
9530
9531    elaborated-type-specifier:
9532      class-key :: [opt] nested-name-specifier [opt] identifier
9533      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9534      enum :: [opt] nested-name-specifier [opt] identifier
9535      typename :: [opt] nested-name-specifier identifier
9536      typename :: [opt] nested-name-specifier template [opt]
9537        template-id
9538
9539    GNU extension:
9540
9541    elaborated-type-specifier:
9542      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9543      class-key attributes :: [opt] nested-name-specifier [opt]
9544                template [opt] template-id
9545      enum attributes :: [opt] nested-name-specifier [opt] identifier
9546
9547    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9548    declared `friend'.  If IS_DECLARATION is TRUE, then this
9549    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9550    something is being declared.
9551
9552    Returns the TYPE specified.  */
9553
9554 static tree
9555 cp_parser_elaborated_type_specifier (cp_parser* parser,
9556                                      bool is_friend,
9557                                      bool is_declaration)
9558 {
9559   enum tag_types tag_type;
9560   tree identifier;
9561   tree type = NULL_TREE;
9562   tree attributes = NULL_TREE;
9563
9564   /* See if we're looking at the `enum' keyword.  */
9565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9566     {
9567       /* Consume the `enum' token.  */
9568       cp_lexer_consume_token (parser->lexer);
9569       /* Remember that it's an enumeration type.  */
9570       tag_type = enum_type;
9571       /* Parse the attributes.  */
9572       attributes = cp_parser_attributes_opt (parser);
9573     }
9574   /* Or, it might be `typename'.  */
9575   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9576                                            RID_TYPENAME))
9577     {
9578       /* Consume the `typename' token.  */
9579       cp_lexer_consume_token (parser->lexer);
9580       /* Remember that it's a `typename' type.  */
9581       tag_type = typename_type;
9582       /* The `typename' keyword is only allowed in templates.  */
9583       if (!processing_template_decl)
9584         pedwarn ("using %<typename%> outside of template");
9585     }
9586   /* Otherwise it must be a class-key.  */
9587   else
9588     {
9589       tag_type = cp_parser_class_key (parser);
9590       if (tag_type == none_type)
9591         return error_mark_node;
9592       /* Parse the attributes.  */
9593       attributes = cp_parser_attributes_opt (parser);
9594     }
9595
9596   /* Look for the `::' operator.  */
9597   cp_parser_global_scope_opt (parser,
9598                               /*current_scope_valid_p=*/false);
9599   /* Look for the nested-name-specifier.  */
9600   if (tag_type == typename_type)
9601     {
9602       if (cp_parser_nested_name_specifier (parser,
9603                                            /*typename_keyword_p=*/true,
9604                                            /*check_dependency_p=*/true,
9605                                            /*type_p=*/true,
9606                                            is_declaration)
9607           == error_mark_node)
9608         return error_mark_node;
9609     }
9610   else
9611     /* Even though `typename' is not present, the proposed resolution
9612        to Core Issue 180 says that in `class A<T>::B', `B' should be
9613        considered a type-name, even if `A<T>' is dependent.  */
9614     cp_parser_nested_name_specifier_opt (parser,
9615                                          /*typename_keyword_p=*/true,
9616                                          /*check_dependency_p=*/true,
9617                                          /*type_p=*/true,
9618                                          is_declaration);
9619   /* For everything but enumeration types, consider a template-id.  */
9620   if (tag_type != enum_type)
9621     {
9622       bool template_p = false;
9623       tree decl;
9624
9625       /* Allow the `template' keyword.  */
9626       template_p = cp_parser_optional_template_keyword (parser);
9627       /* If we didn't see `template', we don't know if there's a
9628          template-id or not.  */
9629       if (!template_p)
9630         cp_parser_parse_tentatively (parser);
9631       /* Parse the template-id.  */
9632       decl = cp_parser_template_id (parser, template_p,
9633                                     /*check_dependency_p=*/true,
9634                                     is_declaration);
9635       /* If we didn't find a template-id, look for an ordinary
9636          identifier.  */
9637       if (!template_p && !cp_parser_parse_definitely (parser))
9638         ;
9639       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9640          in effect, then we must assume that, upon instantiation, the
9641          template will correspond to a class.  */
9642       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9643                && tag_type == typename_type)
9644         type = make_typename_type (parser->scope, decl,
9645                                    /*complain=*/1);
9646       else
9647         type = TREE_TYPE (decl);
9648     }
9649
9650   /* For an enumeration type, consider only a plain identifier.  */
9651   if (!type)
9652     {
9653       identifier = cp_parser_identifier (parser);
9654
9655       if (identifier == error_mark_node)
9656         {
9657           parser->scope = NULL_TREE;
9658           return error_mark_node;
9659         }
9660
9661       /* For a `typename', we needn't call xref_tag.  */
9662       if (tag_type == typename_type)
9663         return cp_parser_make_typename_type (parser, parser->scope,
9664                                              identifier);
9665       /* Look up a qualified name in the usual way.  */
9666       if (parser->scope)
9667         {
9668           tree decl;
9669
9670           /* In an elaborated-type-specifier, names are assumed to name
9671              types, so we set IS_TYPE to TRUE when calling
9672              cp_parser_lookup_name.  */
9673           decl = cp_parser_lookup_name (parser, identifier,
9674                                         /*is_type=*/true,
9675                                         /*is_template=*/false,
9676                                         /*is_namespace=*/false,
9677                                         /*check_dependency=*/true,
9678                                         /*ambiguous_p=*/NULL);
9679
9680           /* If we are parsing friend declaration, DECL may be a
9681              TEMPLATE_DECL tree node here.  However, we need to check
9682              whether this TEMPLATE_DECL results in valid code.  Consider
9683              the following example:
9684
9685                namespace N {
9686                  template <class T> class C {};
9687                }
9688                class X {
9689                  template <class T> friend class N::C; // #1, valid code
9690                };
9691                template <class T> class Y {
9692                  friend class N::C;                    // #2, invalid code
9693                };
9694
9695              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9696              name lookup of `N::C'.  We see that friend declaration must
9697              be template for the code to be valid.  Note that
9698              processing_template_decl does not work here since it is
9699              always 1 for the above two cases.  */
9700
9701           decl = (cp_parser_maybe_treat_template_as_class
9702                   (decl, /*tag_name_p=*/is_friend
9703                          && parser->num_template_parameter_lists));
9704
9705           if (TREE_CODE (decl) != TYPE_DECL)
9706             {
9707               error ("expected type-name");
9708               return error_mark_node;
9709             }
9710
9711           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9712             check_elaborated_type_specifier
9713               (tag_type, decl,
9714                (parser->num_template_parameter_lists
9715                 || DECL_SELF_REFERENCE_P (decl)));
9716
9717           type = TREE_TYPE (decl);
9718         }
9719       else
9720         {
9721           /* An elaborated-type-specifier sometimes introduces a new type and
9722              sometimes names an existing type.  Normally, the rule is that it
9723              introduces a new type only if there is not an existing type of
9724              the same name already in scope.  For example, given:
9725
9726                struct S {};
9727                void f() { struct S s; }
9728
9729              the `struct S' in the body of `f' is the same `struct S' as in
9730              the global scope; the existing definition is used.  However, if
9731              there were no global declaration, this would introduce a new
9732              local class named `S'.
9733
9734              An exception to this rule applies to the following code:
9735
9736                namespace N { struct S; }
9737
9738              Here, the elaborated-type-specifier names a new type
9739              unconditionally; even if there is already an `S' in the
9740              containing scope this declaration names a new type.
9741              This exception only applies if the elaborated-type-specifier
9742              forms the complete declaration:
9743
9744                [class.name]
9745
9746                A declaration consisting solely of `class-key identifier ;' is
9747                either a redeclaration of the name in the current scope or a
9748                forward declaration of the identifier as a class name.  It
9749                introduces the name into the current scope.
9750
9751              We are in this situation precisely when the next token is a `;'.
9752
9753              An exception to the exception is that a `friend' declaration does
9754              *not* name a new type; i.e., given:
9755
9756                struct S { friend struct T; };
9757
9758              `T' is not a new type in the scope of `S'.
9759
9760              Also, `new struct S' or `sizeof (struct S)' never results in the
9761              definition of a new type; a new type can only be declared in a
9762              declaration context.  */
9763
9764           /* Warn about attributes. They are ignored.  */
9765           if (attributes)
9766             warning ("type attributes are honored only at type definition");
9767
9768           type = xref_tag (tag_type, identifier,
9769                            (is_friend
9770                             || !is_declaration
9771                             || cp_lexer_next_token_is_not (parser->lexer,
9772                                                            CPP_SEMICOLON)),
9773                            parser->num_template_parameter_lists);
9774         }
9775     }
9776   if (tag_type != enum_type)
9777     cp_parser_check_class_key (tag_type, type);
9778
9779   /* A "<" cannot follow an elaborated type specifier.  If that
9780      happens, the user was probably trying to form a template-id.  */
9781   cp_parser_check_for_invalid_template_id (parser, type);
9782
9783   return type;
9784 }
9785
9786 /* Parse an enum-specifier.
9787
9788    enum-specifier:
9789      enum identifier [opt] { enumerator-list [opt] }
9790
9791    Returns an ENUM_TYPE representing the enumeration.  */
9792
9793 static tree
9794 cp_parser_enum_specifier (cp_parser* parser)
9795 {
9796   tree identifier;
9797   tree type;
9798
9799   /* Caller guarantees that the current token is 'enum', an identifier
9800      possibly follows, and the token after that is an opening brace.
9801      If we don't have an identifier, fabricate an anonymous name for
9802      the enumeration being defined.  */
9803   cp_lexer_consume_token (parser->lexer);
9804
9805   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9806     identifier = cp_parser_identifier (parser);
9807   else
9808     identifier = make_anon_name ();
9809
9810   /* Issue an error message if type-definitions are forbidden here.  */
9811   cp_parser_check_type_definition (parser);
9812
9813   /* Create the new type.  We do this before consuming the opening brace
9814      so the enum will be recorded as being on the line of its tag (or the
9815      'enum' keyword, if there is no tag).  */
9816   type = start_enum (identifier);
9817
9818   /* Consume the opening brace.  */
9819   cp_lexer_consume_token (parser->lexer);
9820
9821   /* If the next token is not '}', then there are some enumerators.  */
9822   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9823     cp_parser_enumerator_list (parser, type);
9824
9825   /* Consume the final '}'.  */
9826   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9827
9828   /* Finish up the enumeration.  */
9829   finish_enum (type);
9830
9831   return type;
9832 }
9833
9834 /* Parse an enumerator-list.  The enumerators all have the indicated
9835    TYPE.
9836
9837    enumerator-list:
9838      enumerator-definition
9839      enumerator-list , enumerator-definition  */
9840
9841 static void
9842 cp_parser_enumerator_list (cp_parser* parser, tree type)
9843 {
9844   while (true)
9845     {
9846       /* Parse an enumerator-definition.  */
9847       cp_parser_enumerator_definition (parser, type);
9848
9849       /* If the next token is not a ',', we've reached the end of
9850          the list.  */
9851       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9852         break;
9853       /* Otherwise, consume the `,' and keep going.  */
9854       cp_lexer_consume_token (parser->lexer);
9855       /* If the next token is a `}', there is a trailing comma.  */
9856       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9857         {
9858           if (pedantic && !in_system_header)
9859             pedwarn ("comma at end of enumerator list");
9860           break;
9861         }
9862     }
9863 }
9864
9865 /* Parse an enumerator-definition.  The enumerator has the indicated
9866    TYPE.
9867
9868    enumerator-definition:
9869      enumerator
9870      enumerator = constant-expression
9871
9872    enumerator:
9873      identifier  */
9874
9875 static void
9876 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9877 {
9878   tree identifier;
9879   tree value;
9880
9881   /* Look for the identifier.  */
9882   identifier = cp_parser_identifier (parser);
9883   if (identifier == error_mark_node)
9884     return;
9885
9886   /* If the next token is an '=', then there is an explicit value.  */
9887   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9888     {
9889       /* Consume the `=' token.  */
9890       cp_lexer_consume_token (parser->lexer);
9891       /* Parse the value.  */
9892       value = cp_parser_constant_expression (parser,
9893                                              /*allow_non_constant_p=*/false,
9894                                              NULL);
9895     }
9896   else
9897     value = NULL_TREE;
9898
9899   /* Create the enumerator.  */
9900   build_enumerator (identifier, value, type);
9901 }
9902
9903 /* Parse a namespace-name.
9904
9905    namespace-name:
9906      original-namespace-name
9907      namespace-alias
9908
9909    Returns the NAMESPACE_DECL for the namespace.  */
9910
9911 static tree
9912 cp_parser_namespace_name (cp_parser* parser)
9913 {
9914   tree identifier;
9915   tree namespace_decl;
9916
9917   /* Get the name of the namespace.  */
9918   identifier = cp_parser_identifier (parser);
9919   if (identifier == error_mark_node)
9920     return error_mark_node;
9921
9922   /* Look up the identifier in the currently active scope.  Look only
9923      for namespaces, due to:
9924
9925        [basic.lookup.udir]
9926
9927        When looking up a namespace-name in a using-directive or alias
9928        definition, only namespace names are considered.
9929
9930      And:
9931
9932        [basic.lookup.qual]
9933
9934        During the lookup of a name preceding the :: scope resolution
9935        operator, object, function, and enumerator names are ignored.
9936
9937      (Note that cp_parser_class_or_namespace_name only calls this
9938      function if the token after the name is the scope resolution
9939      operator.)  */
9940   namespace_decl = cp_parser_lookup_name (parser, identifier,
9941                                           /*is_type=*/false,
9942                                           /*is_template=*/false,
9943                                           /*is_namespace=*/true,
9944                                           /*check_dependency=*/true,
9945                                           /*ambiguous_p=*/NULL);
9946   /* If it's not a namespace, issue an error.  */
9947   if (namespace_decl == error_mark_node
9948       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9949     {
9950       cp_parser_error (parser, "expected namespace-name");
9951       namespace_decl = error_mark_node;
9952     }
9953
9954   return namespace_decl;
9955 }
9956
9957 /* Parse a namespace-definition.
9958
9959    namespace-definition:
9960      named-namespace-definition
9961      unnamed-namespace-definition
9962
9963    named-namespace-definition:
9964      original-namespace-definition
9965      extension-namespace-definition
9966
9967    original-namespace-definition:
9968      namespace identifier { namespace-body }
9969
9970    extension-namespace-definition:
9971      namespace original-namespace-name { namespace-body }
9972
9973    unnamed-namespace-definition:
9974      namespace { namespace-body } */
9975
9976 static void
9977 cp_parser_namespace_definition (cp_parser* parser)
9978 {
9979   tree identifier;
9980
9981   /* Look for the `namespace' keyword.  */
9982   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9983
9984   /* Get the name of the namespace.  We do not attempt to distinguish
9985      between an original-namespace-definition and an
9986      extension-namespace-definition at this point.  The semantic
9987      analysis routines are responsible for that.  */
9988   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9989     identifier = cp_parser_identifier (parser);
9990   else
9991     identifier = NULL_TREE;
9992
9993   /* Look for the `{' to start the namespace.  */
9994   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9995   /* Start the namespace.  */
9996   push_namespace (identifier);
9997   /* Parse the body of the namespace.  */
9998   cp_parser_namespace_body (parser);
9999   /* Finish the namespace.  */
10000   pop_namespace ();
10001   /* Look for the final `}'.  */
10002   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10003 }
10004
10005 /* Parse a namespace-body.
10006
10007    namespace-body:
10008      declaration-seq [opt]  */
10009
10010 static void
10011 cp_parser_namespace_body (cp_parser* parser)
10012 {
10013   cp_parser_declaration_seq_opt (parser);
10014 }
10015
10016 /* Parse a namespace-alias-definition.
10017
10018    namespace-alias-definition:
10019      namespace identifier = qualified-namespace-specifier ;  */
10020
10021 static void
10022 cp_parser_namespace_alias_definition (cp_parser* parser)
10023 {
10024   tree identifier;
10025   tree namespace_specifier;
10026
10027   /* Look for the `namespace' keyword.  */
10028   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10029   /* Look for the identifier.  */
10030   identifier = cp_parser_identifier (parser);
10031   if (identifier == error_mark_node)
10032     return;
10033   /* Look for the `=' token.  */
10034   cp_parser_require (parser, CPP_EQ, "`='");
10035   /* Look for the qualified-namespace-specifier.  */
10036   namespace_specifier
10037     = cp_parser_qualified_namespace_specifier (parser);
10038   /* Look for the `;' token.  */
10039   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10040
10041   /* Register the alias in the symbol table.  */
10042   do_namespace_alias (identifier, namespace_specifier);
10043 }
10044
10045 /* Parse a qualified-namespace-specifier.
10046
10047    qualified-namespace-specifier:
10048      :: [opt] nested-name-specifier [opt] namespace-name
10049
10050    Returns a NAMESPACE_DECL corresponding to the specified
10051    namespace.  */
10052
10053 static tree
10054 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10055 {
10056   /* Look for the optional `::'.  */
10057   cp_parser_global_scope_opt (parser,
10058                               /*current_scope_valid_p=*/false);
10059
10060   /* Look for the optional nested-name-specifier.  */
10061   cp_parser_nested_name_specifier_opt (parser,
10062                                        /*typename_keyword_p=*/false,
10063                                        /*check_dependency_p=*/true,
10064                                        /*type_p=*/false,
10065                                        /*is_declaration=*/true);
10066
10067   return cp_parser_namespace_name (parser);
10068 }
10069
10070 /* Parse a using-declaration.
10071
10072    using-declaration:
10073      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10074      using :: unqualified-id ;  */
10075
10076 static void
10077 cp_parser_using_declaration (cp_parser* parser)
10078 {
10079   cp_token *token;
10080   bool typename_p = false;
10081   bool global_scope_p;
10082   tree decl;
10083   tree identifier;
10084   tree scope;
10085   tree qscope;
10086
10087   /* Look for the `using' keyword.  */
10088   cp_parser_require_keyword (parser, RID_USING, "`using'");
10089
10090   /* Peek at the next token.  */
10091   token = cp_lexer_peek_token (parser->lexer);
10092   /* See if it's `typename'.  */
10093   if (token->keyword == RID_TYPENAME)
10094     {
10095       /* Remember that we've seen it.  */
10096       typename_p = true;
10097       /* Consume the `typename' token.  */
10098       cp_lexer_consume_token (parser->lexer);
10099     }
10100
10101   /* Look for the optional global scope qualification.  */
10102   global_scope_p
10103     = (cp_parser_global_scope_opt (parser,
10104                                    /*current_scope_valid_p=*/false)
10105        != NULL_TREE);
10106
10107   /* If we saw `typename', or didn't see `::', then there must be a
10108      nested-name-specifier present.  */
10109   if (typename_p || !global_scope_p)
10110     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10111                                               /*check_dependency_p=*/true,
10112                                               /*type_p=*/false,
10113                                               /*is_declaration=*/true);
10114   /* Otherwise, we could be in either of the two productions.  In that
10115      case, treat the nested-name-specifier as optional.  */
10116   else
10117     qscope = cp_parser_nested_name_specifier_opt (parser,
10118                                                   /*typename_keyword_p=*/false,
10119                                                   /*check_dependency_p=*/true,
10120                                                   /*type_p=*/false,
10121                                                   /*is_declaration=*/true);
10122   if (!qscope)
10123     qscope = global_namespace;
10124
10125   /* Parse the unqualified-id.  */
10126   identifier = cp_parser_unqualified_id (parser,
10127                                          /*template_keyword_p=*/false,
10128                                          /*check_dependency_p=*/true,
10129                                          /*declarator_p=*/true);
10130
10131   /* The function we call to handle a using-declaration is different
10132      depending on what scope we are in.  */
10133   if (identifier == error_mark_node)
10134     ;
10135   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10136            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10137     /* [namespace.udecl]
10138
10139        A using declaration shall not name a template-id.  */
10140     error ("a template-id may not appear in a using-declaration");
10141   else
10142     {
10143       scope = current_scope ();
10144       if (scope && TYPE_P (scope))
10145         {
10146           /* Create the USING_DECL.  */
10147           decl = do_class_using_decl (build_nt (SCOPE_REF,
10148                                                 parser->scope,
10149                                                 identifier));
10150           /* Add it to the list of members in this class.  */
10151           finish_member_declaration (decl);
10152         }
10153       else
10154         {
10155           decl = cp_parser_lookup_name_simple (parser, identifier);
10156           if (decl == error_mark_node)
10157             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10158           else if (scope)
10159             do_local_using_decl (decl, qscope, identifier);
10160           else
10161             do_toplevel_using_decl (decl, qscope, identifier);
10162         }
10163     }
10164
10165   /* Look for the final `;'.  */
10166   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10167 }
10168
10169 /* Parse a using-directive.
10170
10171    using-directive:
10172      using namespace :: [opt] nested-name-specifier [opt]
10173        namespace-name ;  */
10174
10175 static void
10176 cp_parser_using_directive (cp_parser* parser)
10177 {
10178   tree namespace_decl;
10179   tree attribs;
10180
10181   /* Look for the `using' keyword.  */
10182   cp_parser_require_keyword (parser, RID_USING, "`using'");
10183   /* And the `namespace' keyword.  */
10184   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10185   /* Look for the optional `::' operator.  */
10186   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10187   /* And the optional nested-name-specifier.  */
10188   cp_parser_nested_name_specifier_opt (parser,
10189                                        /*typename_keyword_p=*/false,
10190                                        /*check_dependency_p=*/true,
10191                                        /*type_p=*/false,
10192                                        /*is_declaration=*/true);
10193   /* Get the namespace being used.  */
10194   namespace_decl = cp_parser_namespace_name (parser);
10195   /* And any specified attributes.  */
10196   attribs = cp_parser_attributes_opt (parser);
10197   /* Update the symbol table.  */
10198   parse_using_directive (namespace_decl, attribs);
10199   /* Look for the final `;'.  */
10200   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10201 }
10202
10203 /* Parse an asm-definition.
10204
10205    asm-definition:
10206      asm ( string-literal ) ;
10207
10208    GNU Extension:
10209
10210    asm-definition:
10211      asm volatile [opt] ( string-literal ) ;
10212      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10213      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10214                           : asm-operand-list [opt] ) ;
10215      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10216                           : asm-operand-list [opt]
10217                           : asm-operand-list [opt] ) ;  */
10218
10219 static void
10220 cp_parser_asm_definition (cp_parser* parser)
10221 {
10222   tree string;
10223   tree outputs = NULL_TREE;
10224   tree inputs = NULL_TREE;
10225   tree clobbers = NULL_TREE;
10226   tree asm_stmt;
10227   bool volatile_p = false;
10228   bool extended_p = false;
10229
10230   /* Look for the `asm' keyword.  */
10231   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10232   /* See if the next token is `volatile'.  */
10233   if (cp_parser_allow_gnu_extensions_p (parser)
10234       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10235     {
10236       /* Remember that we saw the `volatile' keyword.  */
10237       volatile_p = true;
10238       /* Consume the token.  */
10239       cp_lexer_consume_token (parser->lexer);
10240     }
10241   /* Look for the opening `('.  */
10242   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10243     return;
10244   /* Look for the string.  */
10245   string = cp_parser_string_literal (parser, false, false);
10246   if (string == error_mark_node)
10247     {
10248       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10249                                              /*consume_paren=*/true);
10250       return;
10251     }
10252
10253   /* If we're allowing GNU extensions, check for the extended assembly
10254      syntax.  Unfortunately, the `:' tokens need not be separated by
10255      a space in C, and so, for compatibility, we tolerate that here
10256      too.  Doing that means that we have to treat the `::' operator as
10257      two `:' tokens.  */
10258   if (cp_parser_allow_gnu_extensions_p (parser)
10259       && at_function_scope_p ()
10260       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10261           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10262     {
10263       bool inputs_p = false;
10264       bool clobbers_p = false;
10265
10266       /* The extended syntax was used.  */
10267       extended_p = true;
10268
10269       /* Look for outputs.  */
10270       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10271         {
10272           /* Consume the `:'.  */
10273           cp_lexer_consume_token (parser->lexer);
10274           /* Parse the output-operands.  */
10275           if (cp_lexer_next_token_is_not (parser->lexer,
10276                                           CPP_COLON)
10277               && cp_lexer_next_token_is_not (parser->lexer,
10278                                              CPP_SCOPE)
10279               && cp_lexer_next_token_is_not (parser->lexer,
10280                                              CPP_CLOSE_PAREN))
10281             outputs = cp_parser_asm_operand_list (parser);
10282         }
10283       /* If the next token is `::', there are no outputs, and the
10284          next token is the beginning of the inputs.  */
10285       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10286         /* The inputs are coming next.  */
10287         inputs_p = true;
10288
10289       /* Look for inputs.  */
10290       if (inputs_p
10291           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10292         {
10293           /* Consume the `:' or `::'.  */
10294           cp_lexer_consume_token (parser->lexer);
10295           /* Parse the output-operands.  */
10296           if (cp_lexer_next_token_is_not (parser->lexer,
10297                                           CPP_COLON)
10298               && cp_lexer_next_token_is_not (parser->lexer,
10299                                              CPP_CLOSE_PAREN))
10300             inputs = cp_parser_asm_operand_list (parser);
10301         }
10302       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10303         /* The clobbers are coming next.  */
10304         clobbers_p = true;
10305
10306       /* Look for clobbers.  */
10307       if (clobbers_p
10308           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10309         {
10310           /* Consume the `:' or `::'.  */
10311           cp_lexer_consume_token (parser->lexer);
10312           /* Parse the clobbers.  */
10313           if (cp_lexer_next_token_is_not (parser->lexer,
10314                                           CPP_CLOSE_PAREN))
10315             clobbers = cp_parser_asm_clobber_list (parser);
10316         }
10317     }
10318   /* Look for the closing `)'.  */
10319   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10320     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10321                                            /*consume_paren=*/true);
10322   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10323
10324   /* Create the ASM_EXPR.  */
10325   if (at_function_scope_p ())
10326     {
10327       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10328                                   inputs, clobbers);
10329       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10330       if (!extended_p)
10331         {
10332           tree temp = asm_stmt;
10333           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10334             temp = TREE_OPERAND (temp, 0);
10335           
10336           ASM_INPUT_P (temp) = 1;
10337         }
10338     }
10339   else
10340     assemble_asm (string);
10341 }
10342
10343 /* Declarators [gram.dcl.decl] */
10344
10345 /* Parse an init-declarator.
10346
10347    init-declarator:
10348      declarator initializer [opt]
10349
10350    GNU Extension:
10351
10352    init-declarator:
10353      declarator asm-specification [opt] attributes [opt] initializer [opt]
10354
10355    function-definition:
10356      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10357        function-body
10358      decl-specifier-seq [opt] declarator function-try-block
10359
10360    GNU Extension:
10361
10362    function-definition:
10363      __extension__ function-definition
10364
10365    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10366    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10367    then this declarator appears in a class scope.  The new DECL created
10368    by this declarator is returned.
10369
10370    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10371    for a function-definition here as well.  If the declarator is a
10372    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10373    be TRUE upon return.  By that point, the function-definition will
10374    have been completely parsed.
10375
10376    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10377    is FALSE.  */
10378
10379 static tree
10380 cp_parser_init_declarator (cp_parser* parser,
10381                            cp_decl_specifier_seq *decl_specifiers,
10382                            bool function_definition_allowed_p,
10383                            bool member_p,
10384                            int declares_class_or_enum,
10385                            bool* function_definition_p)
10386 {
10387   cp_token *token;
10388   cp_declarator *declarator;
10389   tree prefix_attributes;
10390   tree attributes;
10391   tree asm_specification;
10392   tree initializer;
10393   tree decl = NULL_TREE;
10394   tree scope;
10395   bool is_initialized;
10396   bool is_parenthesized_init;
10397   bool is_non_constant_init;
10398   int ctor_dtor_or_conv_p;
10399   bool friend_p;
10400   bool pop_p = false;
10401
10402   /* Gather the attributes that were provided with the
10403      decl-specifiers.  */
10404   prefix_attributes = decl_specifiers->attributes;
10405
10406   /* Assume that this is not the declarator for a function
10407      definition.  */
10408   if (function_definition_p)
10409     *function_definition_p = false;
10410
10411   /* Defer access checks while parsing the declarator; we cannot know
10412      what names are accessible until we know what is being
10413      declared.  */
10414   resume_deferring_access_checks ();
10415
10416   /* Parse the declarator.  */
10417   declarator
10418     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10419                             &ctor_dtor_or_conv_p,
10420                             /*parenthesized_p=*/NULL,
10421                             /*member_p=*/false);
10422   /* Gather up the deferred checks.  */
10423   stop_deferring_access_checks ();
10424
10425   /* If the DECLARATOR was erroneous, there's no need to go
10426      further.  */
10427   if (declarator == cp_error_declarator)
10428     return error_mark_node;
10429
10430   cp_parser_check_for_definition_in_return_type (declarator,
10431                                                  declares_class_or_enum);
10432
10433   /* Figure out what scope the entity declared by the DECLARATOR is
10434      located in.  `grokdeclarator' sometimes changes the scope, so
10435      we compute it now.  */
10436   scope = get_scope_of_declarator (declarator);
10437
10438   /* If we're allowing GNU extensions, look for an asm-specification
10439      and attributes.  */
10440   if (cp_parser_allow_gnu_extensions_p (parser))
10441     {
10442       /* Look for an asm-specification.  */
10443       asm_specification = cp_parser_asm_specification_opt (parser);
10444       /* And attributes.  */
10445       attributes = cp_parser_attributes_opt (parser);
10446     }
10447   else
10448     {
10449       asm_specification = NULL_TREE;
10450       attributes = NULL_TREE;
10451     }
10452
10453   /* Peek at the next token.  */
10454   token = cp_lexer_peek_token (parser->lexer);
10455   /* Check to see if the token indicates the start of a
10456      function-definition.  */
10457   if (cp_parser_token_starts_function_definition_p (token))
10458     {
10459       if (!function_definition_allowed_p)
10460         {
10461           /* If a function-definition should not appear here, issue an
10462              error message.  */
10463           cp_parser_error (parser,
10464                            "a function-definition is not allowed here");
10465           return error_mark_node;
10466         }
10467       else
10468         {
10469           /* Neither attributes nor an asm-specification are allowed
10470              on a function-definition.  */
10471           if (asm_specification)
10472             error ("an asm-specification is not allowed on a function-definition");
10473           if (attributes)
10474             error ("attributes are not allowed on a function-definition");
10475           /* This is a function-definition.  */
10476           *function_definition_p = true;
10477
10478           /* Parse the function definition.  */
10479           if (member_p)
10480             decl = cp_parser_save_member_function_body (parser,
10481                                                         decl_specifiers,
10482                                                         declarator,
10483                                                         prefix_attributes);
10484           else
10485             decl
10486               = (cp_parser_function_definition_from_specifiers_and_declarator
10487                  (parser, decl_specifiers, prefix_attributes, declarator));
10488
10489           return decl;
10490         }
10491     }
10492
10493   /* [dcl.dcl]
10494
10495      Only in function declarations for constructors, destructors, and
10496      type conversions can the decl-specifier-seq be omitted.
10497
10498      We explicitly postpone this check past the point where we handle
10499      function-definitions because we tolerate function-definitions
10500      that are missing their return types in some modes.  */
10501   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10502     {
10503       cp_parser_error (parser,
10504                        "expected constructor, destructor, or type conversion");
10505       return error_mark_node;
10506     }
10507
10508   /* An `=' or an `(' indicates an initializer.  */
10509   is_initialized = (token->type == CPP_EQ
10510                      || token->type == CPP_OPEN_PAREN);
10511   /* If the init-declarator isn't initialized and isn't followed by a
10512      `,' or `;', it's not a valid init-declarator.  */
10513   if (!is_initialized
10514       && token->type != CPP_COMMA
10515       && token->type != CPP_SEMICOLON)
10516     {
10517       cp_parser_error (parser, "expected initializer");
10518       return error_mark_node;
10519     }
10520
10521   /* Because start_decl has side-effects, we should only call it if we
10522      know we're going ahead.  By this point, we know that we cannot
10523      possibly be looking at any other construct.  */
10524   cp_parser_commit_to_tentative_parse (parser);
10525
10526   /* If the decl specifiers were bad, issue an error now that we're
10527      sure this was intended to be a declarator.  Then continue
10528      declaring the variable(s), as int, to try to cut down on further
10529      errors.  */
10530   if (decl_specifiers->any_specifiers_p
10531       && decl_specifiers->type == error_mark_node)
10532     {
10533       cp_parser_error (parser, "invalid type in declaration");
10534       decl_specifiers->type = integer_type_node;
10535     }
10536
10537   /* Check to see whether or not this declaration is a friend.  */
10538   friend_p = cp_parser_friend_p (decl_specifiers);
10539
10540   /* Check that the number of template-parameter-lists is OK.  */
10541   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10542     return error_mark_node;
10543
10544   /* Enter the newly declared entry in the symbol table.  If we're
10545      processing a declaration in a class-specifier, we wait until
10546      after processing the initializer.  */
10547   if (!member_p)
10548     {
10549       if (parser->in_unbraced_linkage_specification_p)
10550         {
10551           decl_specifiers->storage_class = sc_extern;
10552           have_extern_spec = false;
10553         }
10554       decl = start_decl (declarator, decl_specifiers,
10555                          is_initialized, attributes, prefix_attributes,
10556                          &pop_p);
10557     }
10558   else if (scope)
10559     /* Enter the SCOPE.  That way unqualified names appearing in the
10560        initializer will be looked up in SCOPE.  */
10561     pop_p = push_scope (scope);
10562
10563   /* Perform deferred access control checks, now that we know in which
10564      SCOPE the declared entity resides.  */
10565   if (!member_p && decl)
10566     {
10567       tree saved_current_function_decl = NULL_TREE;
10568
10569       /* If the entity being declared is a function, pretend that we
10570          are in its scope.  If it is a `friend', it may have access to
10571          things that would not otherwise be accessible.  */
10572       if (TREE_CODE (decl) == FUNCTION_DECL)
10573         {
10574           saved_current_function_decl = current_function_decl;
10575           current_function_decl = decl;
10576         }
10577
10578       /* Perform the access control checks for the declarator and the
10579          the decl-specifiers.  */
10580       perform_deferred_access_checks ();
10581
10582       /* Restore the saved value.  */
10583       if (TREE_CODE (decl) == FUNCTION_DECL)
10584         current_function_decl = saved_current_function_decl;
10585     }
10586
10587   /* Parse the initializer.  */
10588   if (is_initialized)
10589     initializer = cp_parser_initializer (parser,
10590                                          &is_parenthesized_init,
10591                                          &is_non_constant_init);
10592   else
10593     {
10594       initializer = NULL_TREE;
10595       is_parenthesized_init = false;
10596       is_non_constant_init = true;
10597     }
10598
10599   /* The old parser allows attributes to appear after a parenthesized
10600      initializer.  Mark Mitchell proposed removing this functionality
10601      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10602      attributes -- but ignores them.  */
10603   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10604     if (cp_parser_attributes_opt (parser))
10605       warning ("attributes after parenthesized initializer ignored");
10606
10607   /* For an in-class declaration, use `grokfield' to create the
10608      declaration.  */
10609   if (member_p)
10610     {
10611       if (pop_p)
10612         pop_scope (scope);
10613       decl = grokfield (declarator, decl_specifiers,
10614                         initializer, /*asmspec=*/NULL_TREE,
10615                         /*attributes=*/NULL_TREE);
10616       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10617         cp_parser_save_default_args (parser, decl);
10618     }
10619
10620   /* Finish processing the declaration.  But, skip friend
10621      declarations.  */
10622   if (!friend_p && decl && decl != error_mark_node)
10623     {
10624       cp_finish_decl (decl,
10625                       initializer,
10626                       asm_specification,
10627                       /* If the initializer is in parentheses, then this is
10628                          a direct-initialization, which means that an
10629                          `explicit' constructor is OK.  Otherwise, an
10630                          `explicit' constructor cannot be used.  */
10631                       ((is_parenthesized_init || !is_initialized)
10632                      ? 0 : LOOKUP_ONLYCONVERTING));
10633       if (pop_p)
10634         pop_scope (DECL_CONTEXT (decl));
10635     }
10636
10637   /* Remember whether or not variables were initialized by
10638      constant-expressions.  */
10639   if (decl && TREE_CODE (decl) == VAR_DECL
10640       && is_initialized && !is_non_constant_init)
10641     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10642
10643   return decl;
10644 }
10645
10646 /* Parse a declarator.
10647
10648    declarator:
10649      direct-declarator
10650      ptr-operator declarator
10651
10652    abstract-declarator:
10653      ptr-operator abstract-declarator [opt]
10654      direct-abstract-declarator
10655
10656    GNU Extensions:
10657
10658    declarator:
10659      attributes [opt] direct-declarator
10660      attributes [opt] ptr-operator declarator
10661
10662    abstract-declarator:
10663      attributes [opt] ptr-operator abstract-declarator [opt]
10664      attributes [opt] direct-abstract-declarator
10665
10666    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10667    detect constructor, destructor or conversion operators. It is set
10668    to -1 if the declarator is a name, and +1 if it is a
10669    function. Otherwise it is set to zero. Usually you just want to
10670    test for >0, but internally the negative value is used.
10671
10672    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10673    a decl-specifier-seq unless it declares a constructor, destructor,
10674    or conversion.  It might seem that we could check this condition in
10675    semantic analysis, rather than parsing, but that makes it difficult
10676    to handle something like `f()'.  We want to notice that there are
10677    no decl-specifiers, and therefore realize that this is an
10678    expression, not a declaration.)
10679
10680    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10681    the declarator is a direct-declarator of the form "(...)".  
10682
10683    MEMBER_P is true iff this declarator is a member-declarator.  */
10684
10685 static cp_declarator *
10686 cp_parser_declarator (cp_parser* parser,
10687                       cp_parser_declarator_kind dcl_kind,
10688                       int* ctor_dtor_or_conv_p,
10689                       bool* parenthesized_p,
10690                       bool member_p)
10691 {
10692   cp_token *token;
10693   cp_declarator *declarator;
10694   enum tree_code code;
10695   cp_cv_quals cv_quals;
10696   tree class_type;
10697   tree attributes = NULL_TREE;
10698
10699   /* Assume this is not a constructor, destructor, or type-conversion
10700      operator.  */
10701   if (ctor_dtor_or_conv_p)
10702     *ctor_dtor_or_conv_p = 0;
10703
10704   if (cp_parser_allow_gnu_extensions_p (parser))
10705     attributes = cp_parser_attributes_opt (parser);
10706
10707   /* Peek at the next token.  */
10708   token = cp_lexer_peek_token (parser->lexer);
10709
10710   /* Check for the ptr-operator production.  */
10711   cp_parser_parse_tentatively (parser);
10712   /* Parse the ptr-operator.  */
10713   code = cp_parser_ptr_operator (parser,
10714                                  &class_type,
10715                                  &cv_quals);
10716   /* If that worked, then we have a ptr-operator.  */
10717   if (cp_parser_parse_definitely (parser))
10718     {
10719       /* If a ptr-operator was found, then this declarator was not
10720          parenthesized.  */
10721       if (parenthesized_p)
10722         *parenthesized_p = true;
10723       /* The dependent declarator is optional if we are parsing an
10724          abstract-declarator.  */
10725       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10726         cp_parser_parse_tentatively (parser);
10727
10728       /* Parse the dependent declarator.  */
10729       declarator = cp_parser_declarator (parser, dcl_kind,
10730                                          /*ctor_dtor_or_conv_p=*/NULL,
10731                                          /*parenthesized_p=*/NULL,
10732                                          /*member_p=*/false);
10733
10734       /* If we are parsing an abstract-declarator, we must handle the
10735          case where the dependent declarator is absent.  */
10736       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10737           && !cp_parser_parse_definitely (parser))
10738         declarator = NULL;
10739
10740       /* Build the representation of the ptr-operator.  */
10741       if (class_type)
10742         declarator = make_ptrmem_declarator (cv_quals,
10743                                              class_type,
10744                                              declarator);
10745       else if (code == INDIRECT_REF)
10746         declarator = make_pointer_declarator (cv_quals, declarator);
10747       else
10748         declarator = make_reference_declarator (cv_quals, declarator);
10749     }
10750   /* Everything else is a direct-declarator.  */
10751   else
10752     {
10753       if (parenthesized_p)
10754         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10755                                                    CPP_OPEN_PAREN);
10756       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10757                                                 ctor_dtor_or_conv_p,
10758                                                 member_p);
10759     }
10760
10761   if (attributes && declarator != cp_error_declarator)
10762     declarator->attributes = attributes;
10763
10764   return declarator;
10765 }
10766
10767 /* Parse a direct-declarator or direct-abstract-declarator.
10768
10769    direct-declarator:
10770      declarator-id
10771      direct-declarator ( parameter-declaration-clause )
10772        cv-qualifier-seq [opt]
10773        exception-specification [opt]
10774      direct-declarator [ constant-expression [opt] ]
10775      ( declarator )
10776
10777    direct-abstract-declarator:
10778      direct-abstract-declarator [opt]
10779        ( parameter-declaration-clause )
10780        cv-qualifier-seq [opt]
10781        exception-specification [opt]
10782      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10783      ( abstract-declarator )
10784
10785    Returns a representation of the declarator.  DCL_KIND is
10786    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10787    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10788    we are parsing a direct-declarator.  It is
10789    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10790    of ambiguity we prefer an abstract declarator, as per
10791    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10792    cp_parser_declarator.  */
10793
10794 static cp_declarator *
10795 cp_parser_direct_declarator (cp_parser* parser,
10796                              cp_parser_declarator_kind dcl_kind,
10797                              int* ctor_dtor_or_conv_p,
10798                              bool member_p)
10799 {
10800   cp_token *token;
10801   cp_declarator *declarator = NULL;
10802   tree scope = NULL_TREE;
10803   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10804   bool saved_in_declarator_p = parser->in_declarator_p;
10805   bool first = true;
10806   bool pop_p = false;
10807
10808   while (true)
10809     {
10810       /* Peek at the next token.  */
10811       token = cp_lexer_peek_token (parser->lexer);
10812       if (token->type == CPP_OPEN_PAREN)
10813         {
10814           /* This is either a parameter-declaration-clause, or a
10815              parenthesized declarator. When we know we are parsing a
10816              named declarator, it must be a parenthesized declarator
10817              if FIRST is true. For instance, `(int)' is a
10818              parameter-declaration-clause, with an omitted
10819              direct-abstract-declarator. But `((*))', is a
10820              parenthesized abstract declarator. Finally, when T is a
10821              template parameter `(T)' is a
10822              parameter-declaration-clause, and not a parenthesized
10823              named declarator.
10824
10825              We first try and parse a parameter-declaration-clause,
10826              and then try a nested declarator (if FIRST is true).
10827
10828              It is not an error for it not to be a
10829              parameter-declaration-clause, even when FIRST is
10830              false. Consider,
10831
10832                int i (int);
10833                int i (3);
10834
10835              The first is the declaration of a function while the
10836              second is a the definition of a variable, including its
10837              initializer.
10838
10839              Having seen only the parenthesis, we cannot know which of
10840              these two alternatives should be selected.  Even more
10841              complex are examples like:
10842
10843                int i (int (a));
10844                int i (int (3));
10845
10846              The former is a function-declaration; the latter is a
10847              variable initialization.
10848
10849              Thus again, we try a parameter-declaration-clause, and if
10850              that fails, we back out and return.  */
10851
10852           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10853             {
10854               cp_parameter_declarator *params;
10855               unsigned saved_num_template_parameter_lists;
10856
10857               /* In a member-declarator, the only valid interpretation
10858                  of a parenthesis is the start of a
10859                  parameter-declaration-clause.  (It is invalid to
10860                  initialize a static data member with a parenthesized
10861                  initializer; only the "=" form of initialization is
10862                  permitted.)  */
10863               if (!member_p)
10864                 cp_parser_parse_tentatively (parser);
10865
10866               /* Consume the `('.  */
10867               cp_lexer_consume_token (parser->lexer);
10868               if (first)
10869                 {
10870                   /* If this is going to be an abstract declarator, we're
10871                      in a declarator and we can't have default args.  */
10872                   parser->default_arg_ok_p = false;
10873                   parser->in_declarator_p = true;
10874                 }
10875
10876               /* Inside the function parameter list, surrounding
10877                  template-parameter-lists do not apply.  */
10878               saved_num_template_parameter_lists
10879                 = parser->num_template_parameter_lists;
10880               parser->num_template_parameter_lists = 0;
10881
10882               /* Parse the parameter-declaration-clause.  */
10883               params = cp_parser_parameter_declaration_clause (parser);
10884
10885               parser->num_template_parameter_lists
10886                 = saved_num_template_parameter_lists;
10887
10888               /* If all went well, parse the cv-qualifier-seq and the
10889                  exception-specification.  */
10890               if (member_p || cp_parser_parse_definitely (parser))
10891                 {
10892                   cp_cv_quals cv_quals;
10893                   tree exception_specification;
10894
10895                   if (ctor_dtor_or_conv_p)
10896                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10897                   first = false;
10898                   /* Consume the `)'.  */
10899                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10900
10901                   /* Parse the cv-qualifier-seq.  */
10902                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10903                   /* And the exception-specification.  */
10904                   exception_specification
10905                     = cp_parser_exception_specification_opt (parser);
10906
10907                   /* Create the function-declarator.  */
10908                   declarator = make_call_declarator (declarator,
10909                                                      params,
10910                                                      cv_quals,
10911                                                      exception_specification);
10912                   /* Any subsequent parameter lists are to do with
10913                      return type, so are not those of the declared
10914                      function.  */
10915                   parser->default_arg_ok_p = false;
10916
10917                   /* Repeat the main loop.  */
10918                   continue;
10919                 }
10920             }
10921
10922           /* If this is the first, we can try a parenthesized
10923              declarator.  */
10924           if (first)
10925             {
10926               bool saved_in_type_id_in_expr_p;
10927
10928               parser->default_arg_ok_p = saved_default_arg_ok_p;
10929               parser->in_declarator_p = saved_in_declarator_p;
10930
10931               /* Consume the `('.  */
10932               cp_lexer_consume_token (parser->lexer);
10933               /* Parse the nested declarator.  */
10934               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10935               parser->in_type_id_in_expr_p = true;
10936               declarator
10937                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10938                                         /*parenthesized_p=*/NULL,
10939                                         member_p);
10940               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10941               first = false;
10942               /* Expect a `)'.  */
10943               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10944                 declarator = cp_error_declarator;
10945               if (declarator == cp_error_declarator)
10946                 break;
10947
10948               goto handle_declarator;
10949             }
10950           /* Otherwise, we must be done.  */
10951           else
10952             break;
10953         }
10954       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10955                && token->type == CPP_OPEN_SQUARE)
10956         {
10957           /* Parse an array-declarator.  */
10958           tree bounds;
10959
10960           if (ctor_dtor_or_conv_p)
10961             *ctor_dtor_or_conv_p = 0;
10962
10963           first = false;
10964           parser->default_arg_ok_p = false;
10965           parser->in_declarator_p = true;
10966           /* Consume the `['.  */
10967           cp_lexer_consume_token (parser->lexer);
10968           /* Peek at the next token.  */
10969           token = cp_lexer_peek_token (parser->lexer);
10970           /* If the next token is `]', then there is no
10971              constant-expression.  */
10972           if (token->type != CPP_CLOSE_SQUARE)
10973             {
10974               bool non_constant_p;
10975
10976               bounds
10977                 = cp_parser_constant_expression (parser,
10978                                                  /*allow_non_constant=*/true,
10979                                                  &non_constant_p);
10980               if (!non_constant_p)
10981                 bounds = fold_non_dependent_expr (bounds);
10982             }
10983           else
10984             bounds = NULL_TREE;
10985           /* Look for the closing `]'.  */
10986           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10987             {
10988               declarator = cp_error_declarator;
10989               break;
10990             }
10991
10992           declarator = make_array_declarator (declarator, bounds);
10993         }
10994       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10995         {
10996           tree id;
10997
10998           /* Parse a declarator-id */
10999           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11000             cp_parser_parse_tentatively (parser);
11001           id = cp_parser_declarator_id (parser);
11002           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11003             {
11004               if (!cp_parser_parse_definitely (parser))
11005                 id = error_mark_node;
11006               else if (TREE_CODE (id) != IDENTIFIER_NODE)
11007                 {
11008                   cp_parser_error (parser, "expected unqualified-id");
11009                   id = error_mark_node;
11010                 }
11011             }
11012
11013           if (id == error_mark_node)
11014             {
11015               declarator = cp_error_declarator;
11016               break;
11017             }
11018
11019           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11020             {
11021               tree scope = TREE_OPERAND (id, 0);
11022
11023               /* In the declaration of a member of a template class
11024                  outside of the class itself, the SCOPE will sometimes
11025                  be a TYPENAME_TYPE.  For example, given:
11026
11027                  template <typename T>
11028                  int S<T>::R::i = 3;
11029
11030                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11031                  this context, we must resolve S<T>::R to an ordinary
11032                  type, rather than a typename type.
11033
11034                  The reason we normally avoid resolving TYPENAME_TYPEs
11035                  is that a specialization of `S' might render
11036                  `S<T>::R' not a type.  However, if `S' is
11037                  specialized, then this `i' will not be used, so there
11038                  is no harm in resolving the types here.  */
11039               if (TREE_CODE (scope) == TYPENAME_TYPE)
11040                 {
11041                   tree type;
11042
11043                   /* Resolve the TYPENAME_TYPE.  */
11044                   type = resolve_typename_type (scope,
11045                                                  /*only_current_p=*/false);
11046                   /* If that failed, the declarator is invalid.  */
11047                   if (type == error_mark_node)
11048                     error ("%<%T::%D%> is not a type",
11049                            TYPE_CONTEXT (scope),
11050                            TYPE_IDENTIFIER (scope));
11051                   /* Build a new DECLARATOR.  */
11052                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11053                 }
11054             }
11055
11056           declarator = make_id_declarator (id);
11057           if (id)
11058             {
11059               tree class_type;
11060               tree unqualified_name;
11061
11062               if (TREE_CODE (id) == SCOPE_REF
11063                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11064                 {
11065                   class_type = TREE_OPERAND (id, 0);
11066                   unqualified_name = TREE_OPERAND (id, 1);
11067                 }
11068               else
11069                 {
11070                   class_type = current_class_type;
11071                   unqualified_name = id;
11072                 }
11073
11074               if (class_type)
11075                 {
11076                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11077                     declarator->u.id.sfk = sfk_destructor;
11078                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11079                     declarator->u.id.sfk = sfk_conversion;
11080                   else if (constructor_name_p (unqualified_name,
11081                                                class_type)
11082                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11083                                && same_type_p (TREE_TYPE (unqualified_name),
11084                                                class_type)))
11085                     declarator->u.id.sfk = sfk_constructor;
11086
11087                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11088                     *ctor_dtor_or_conv_p = -1;
11089                   if (TREE_CODE (id) == SCOPE_REF
11090                       && TREE_CODE (unqualified_name) == TYPE_DECL
11091                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11092                     {
11093                       error ("invalid use of constructor as a template");
11094                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11095                               "the constructor in a qualified name",
11096                               class_type,
11097                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11098                               class_type, class_type);
11099                     }
11100                 }
11101             }
11102
11103         handle_declarator:;
11104           scope = get_scope_of_declarator (declarator);
11105           if (scope)
11106             /* Any names that appear after the declarator-id for a
11107                member are looked up in the containing scope.  */
11108             pop_p = push_scope (scope);
11109           parser->in_declarator_p = true;
11110           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11111               || (declarator && declarator->kind == cdk_id))
11112             /* Default args are only allowed on function
11113                declarations.  */
11114             parser->default_arg_ok_p = saved_default_arg_ok_p;
11115           else
11116             parser->default_arg_ok_p = false;
11117
11118           first = false;
11119         }
11120       /* We're done.  */
11121       else
11122         break;
11123     }
11124
11125   /* For an abstract declarator, we might wind up with nothing at this
11126      point.  That's an error; the declarator is not optional.  */
11127   if (!declarator)
11128     cp_parser_error (parser, "expected declarator");
11129
11130   /* If we entered a scope, we must exit it now.  */
11131   if (pop_p)
11132     pop_scope (scope);
11133
11134   parser->default_arg_ok_p = saved_default_arg_ok_p;
11135   parser->in_declarator_p = saved_in_declarator_p;
11136
11137   return declarator;
11138 }
11139
11140 /* Parse a ptr-operator.
11141
11142    ptr-operator:
11143      * cv-qualifier-seq [opt]
11144      &
11145      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11146
11147    GNU Extension:
11148
11149    ptr-operator:
11150      & cv-qualifier-seq [opt]
11151
11152    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11153    Returns ADDR_EXPR if a reference was used.  In the case of a
11154    pointer-to-member, *TYPE is filled in with the TYPE containing the
11155    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11156    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11157    ERROR_MARK if an error occurred.  */
11158
11159 static enum tree_code
11160 cp_parser_ptr_operator (cp_parser* parser,
11161                         tree* type,
11162                         cp_cv_quals *cv_quals)
11163 {
11164   enum tree_code code = ERROR_MARK;
11165   cp_token *token;
11166
11167   /* Assume that it's not a pointer-to-member.  */
11168   *type = NULL_TREE;
11169   /* And that there are no cv-qualifiers.  */
11170   *cv_quals = TYPE_UNQUALIFIED;
11171
11172   /* Peek at the next token.  */
11173   token = cp_lexer_peek_token (parser->lexer);
11174   /* If it's a `*' or `&' we have a pointer or reference.  */
11175   if (token->type == CPP_MULT || token->type == CPP_AND)
11176     {
11177       /* Remember which ptr-operator we were processing.  */
11178       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11179
11180       /* Consume the `*' or `&'.  */
11181       cp_lexer_consume_token (parser->lexer);
11182
11183       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11184          `&', if we are allowing GNU extensions.  (The only qualifier
11185          that can legally appear after `&' is `restrict', but that is
11186          enforced during semantic analysis.  */
11187       if (code == INDIRECT_REF
11188           || cp_parser_allow_gnu_extensions_p (parser))
11189         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11190     }
11191   else
11192     {
11193       /* Try the pointer-to-member case.  */
11194       cp_parser_parse_tentatively (parser);
11195       /* Look for the optional `::' operator.  */
11196       cp_parser_global_scope_opt (parser,
11197                                   /*current_scope_valid_p=*/false);
11198       /* Look for the nested-name specifier.  */
11199       cp_parser_nested_name_specifier (parser,
11200                                        /*typename_keyword_p=*/false,
11201                                        /*check_dependency_p=*/true,
11202                                        /*type_p=*/false,
11203                                        /*is_declaration=*/false);
11204       /* If we found it, and the next token is a `*', then we are
11205          indeed looking at a pointer-to-member operator.  */
11206       if (!cp_parser_error_occurred (parser)
11207           && cp_parser_require (parser, CPP_MULT, "`*'"))
11208         {
11209           /* The type of which the member is a member is given by the
11210              current SCOPE.  */
11211           *type = parser->scope;
11212           /* The next name will not be qualified.  */
11213           parser->scope = NULL_TREE;
11214           parser->qualifying_scope = NULL_TREE;
11215           parser->object_scope = NULL_TREE;
11216           /* Indicate that the `*' operator was used.  */
11217           code = INDIRECT_REF;
11218           /* Look for the optional cv-qualifier-seq.  */
11219           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11220         }
11221       /* If that didn't work we don't have a ptr-operator.  */
11222       if (!cp_parser_parse_definitely (parser))
11223         cp_parser_error (parser, "expected ptr-operator");
11224     }
11225
11226   return code;
11227 }
11228
11229 /* Parse an (optional) cv-qualifier-seq.
11230
11231    cv-qualifier-seq:
11232      cv-qualifier cv-qualifier-seq [opt]
11233
11234    cv-qualifier:
11235      const
11236      volatile
11237
11238    GNU Extension:
11239
11240    cv-qualifier:
11241      __restrict__
11242
11243    Returns a bitmask representing the cv-qualifiers.  */
11244
11245 static cp_cv_quals
11246 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11247 {
11248   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11249
11250   while (true)
11251     {
11252       cp_token *token;
11253       cp_cv_quals cv_qualifier;
11254
11255       /* Peek at the next token.  */
11256       token = cp_lexer_peek_token (parser->lexer);
11257       /* See if it's a cv-qualifier.  */
11258       switch (token->keyword)
11259         {
11260         case RID_CONST:
11261           cv_qualifier = TYPE_QUAL_CONST;
11262           break;
11263
11264         case RID_VOLATILE:
11265           cv_qualifier = TYPE_QUAL_VOLATILE;
11266           break;
11267
11268         case RID_RESTRICT:
11269           cv_qualifier = TYPE_QUAL_RESTRICT;
11270           break;
11271
11272         default:
11273           cv_qualifier = TYPE_UNQUALIFIED;
11274           break;
11275         }
11276
11277       if (!cv_qualifier)
11278         break;
11279
11280       if (cv_quals & cv_qualifier)
11281         {
11282           error ("duplicate cv-qualifier");
11283           cp_lexer_purge_token (parser->lexer);
11284         }
11285       else
11286         {
11287           cp_lexer_consume_token (parser->lexer);
11288           cv_quals |= cv_qualifier;
11289         }
11290     }
11291
11292   return cv_quals;
11293 }
11294
11295 /* Parse a declarator-id.
11296
11297    declarator-id:
11298      id-expression
11299      :: [opt] nested-name-specifier [opt] type-name
11300
11301    In the `id-expression' case, the value returned is as for
11302    cp_parser_id_expression if the id-expression was an unqualified-id.
11303    If the id-expression was a qualified-id, then a SCOPE_REF is
11304    returned.  The first operand is the scope (either a NAMESPACE_DECL
11305    or TREE_TYPE), but the second is still just a representation of an
11306    unqualified-id.  */
11307
11308 static tree
11309 cp_parser_declarator_id (cp_parser* parser)
11310 {
11311   tree id_expression;
11312
11313   /* The expression must be an id-expression.  Assume that qualified
11314      names are the names of types so that:
11315
11316        template <class T>
11317        int S<T>::R::i = 3;
11318
11319      will work; we must treat `S<T>::R' as the name of a type.
11320      Similarly, assume that qualified names are templates, where
11321      required, so that:
11322
11323        template <class T>
11324        int S<T>::R<T>::i = 3;
11325
11326      will work, too.  */
11327   id_expression = cp_parser_id_expression (parser,
11328                                            /*template_keyword_p=*/false,
11329                                            /*check_dependency_p=*/false,
11330                                            /*template_p=*/NULL,
11331                                            /*declarator_p=*/true);
11332   /* If the name was qualified, create a SCOPE_REF to represent
11333      that.  */
11334   if (parser->scope)
11335     {
11336       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11337       parser->scope = NULL_TREE;
11338     }
11339
11340   return id_expression;
11341 }
11342
11343 /* Parse a type-id.
11344
11345    type-id:
11346      type-specifier-seq abstract-declarator [opt]
11347
11348    Returns the TYPE specified.  */
11349
11350 static tree
11351 cp_parser_type_id (cp_parser* parser)
11352 {
11353   cp_decl_specifier_seq type_specifier_seq;
11354   cp_declarator *abstract_declarator;
11355
11356   /* Parse the type-specifier-seq.  */
11357   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11358   if (type_specifier_seq.type == error_mark_node)
11359     return error_mark_node;
11360
11361   /* There might or might not be an abstract declarator.  */
11362   cp_parser_parse_tentatively (parser);
11363   /* Look for the declarator.  */
11364   abstract_declarator
11365     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11366                             /*parenthesized_p=*/NULL,
11367                             /*member_p=*/false);
11368   /* Check to see if there really was a declarator.  */
11369   if (!cp_parser_parse_definitely (parser))
11370     abstract_declarator = NULL;
11371
11372   return groktypename (&type_specifier_seq, abstract_declarator);
11373 }
11374
11375 /* Parse a type-specifier-seq.
11376
11377    type-specifier-seq:
11378      type-specifier type-specifier-seq [opt]
11379
11380    GNU extension:
11381
11382    type-specifier-seq:
11383      attributes type-specifier-seq [opt]
11384
11385    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11386
11387 static void
11388 cp_parser_type_specifier_seq (cp_parser* parser,
11389                               cp_decl_specifier_seq *type_specifier_seq)
11390 {
11391   bool seen_type_specifier = false;
11392
11393   /* Clear the TYPE_SPECIFIER_SEQ.  */
11394   clear_decl_specs (type_specifier_seq);
11395
11396   /* Parse the type-specifiers and attributes.  */
11397   while (true)
11398     {
11399       tree type_specifier;
11400
11401       /* Check for attributes first.  */
11402       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11403         {
11404           type_specifier_seq->attributes =
11405             chainon (type_specifier_seq->attributes,
11406                      cp_parser_attributes_opt (parser));
11407           continue;
11408         }
11409
11410       /* Look for the type-specifier.  */
11411       type_specifier = cp_parser_type_specifier (parser,
11412                                                  CP_PARSER_FLAGS_OPTIONAL,
11413                                                  type_specifier_seq,
11414                                                  /*is_declaration=*/false,
11415                                                  NULL,
11416                                                  NULL);
11417       /* If the first type-specifier could not be found, this is not a
11418          type-specifier-seq at all.  */
11419       if (!seen_type_specifier && !type_specifier)
11420         {
11421           cp_parser_error (parser, "expected type-specifier");
11422           type_specifier_seq->type = error_mark_node;
11423           return;
11424         }
11425       /* If subsequent type-specifiers could not be found, the
11426          type-specifier-seq is complete.  */
11427       else if (seen_type_specifier && !type_specifier)
11428         break;
11429
11430       seen_type_specifier = true;
11431     }
11432
11433   return;
11434 }
11435
11436 /* Parse a parameter-declaration-clause.
11437
11438    parameter-declaration-clause:
11439      parameter-declaration-list [opt] ... [opt]
11440      parameter-declaration-list , ...
11441
11442    Returns a representation for the parameter declarations.  A return
11443    value of NULL indicates a parameter-declaration-clause consisting
11444    only of an ellipsis.  */
11445
11446 static cp_parameter_declarator *
11447 cp_parser_parameter_declaration_clause (cp_parser* parser)
11448 {
11449   cp_parameter_declarator *parameters;
11450   cp_token *token;
11451   bool ellipsis_p;
11452   bool is_error;
11453
11454   /* Peek at the next token.  */
11455   token = cp_lexer_peek_token (parser->lexer);
11456   /* Check for trivial parameter-declaration-clauses.  */
11457   if (token->type == CPP_ELLIPSIS)
11458     {
11459       /* Consume the `...' token.  */
11460       cp_lexer_consume_token (parser->lexer);
11461       return NULL;
11462     }
11463   else if (token->type == CPP_CLOSE_PAREN)
11464     /* There are no parameters.  */
11465     {
11466 #ifndef NO_IMPLICIT_EXTERN_C
11467       if (in_system_header && current_class_type == NULL
11468           && current_lang_name == lang_name_c)
11469         return NULL;
11470       else
11471 #endif
11472         return no_parameters;
11473     }
11474   /* Check for `(void)', too, which is a special case.  */
11475   else if (token->keyword == RID_VOID
11476            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11477                == CPP_CLOSE_PAREN))
11478     {
11479       /* Consume the `void' token.  */
11480       cp_lexer_consume_token (parser->lexer);
11481       /* There are no parameters.  */
11482       return no_parameters;
11483     }
11484
11485   /* Parse the parameter-declaration-list.  */
11486   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11487   /* If a parse error occurred while parsing the
11488      parameter-declaration-list, then the entire
11489      parameter-declaration-clause is erroneous.  */
11490   if (is_error)
11491     return NULL;
11492
11493   /* Peek at the next token.  */
11494   token = cp_lexer_peek_token (parser->lexer);
11495   /* If it's a `,', the clause should terminate with an ellipsis.  */
11496   if (token->type == CPP_COMMA)
11497     {
11498       /* Consume the `,'.  */
11499       cp_lexer_consume_token (parser->lexer);
11500       /* Expect an ellipsis.  */
11501       ellipsis_p
11502         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11503     }
11504   /* It might also be `...' if the optional trailing `,' was
11505      omitted.  */
11506   else if (token->type == CPP_ELLIPSIS)
11507     {
11508       /* Consume the `...' token.  */
11509       cp_lexer_consume_token (parser->lexer);
11510       /* And remember that we saw it.  */
11511       ellipsis_p = true;
11512     }
11513   else
11514     ellipsis_p = false;
11515
11516   /* Finish the parameter list.  */
11517   if (parameters && ellipsis_p)
11518     parameters->ellipsis_p = true;
11519
11520   return parameters;
11521 }
11522
11523 /* Parse a parameter-declaration-list.
11524
11525    parameter-declaration-list:
11526      parameter-declaration
11527      parameter-declaration-list , parameter-declaration
11528
11529    Returns a representation of the parameter-declaration-list, as for
11530    cp_parser_parameter_declaration_clause.  However, the
11531    `void_list_node' is never appended to the list.  Upon return,
11532    *IS_ERROR will be true iff an error occurred.  */
11533
11534 static cp_parameter_declarator *
11535 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11536 {
11537   cp_parameter_declarator *parameters = NULL;
11538   cp_parameter_declarator **tail = &parameters;
11539
11540   /* Assume all will go well.  */
11541   *is_error = false;
11542
11543   /* Look for more parameters.  */
11544   while (true)
11545     {
11546       cp_parameter_declarator *parameter;
11547       bool parenthesized_p;
11548       /* Parse the parameter.  */
11549       parameter
11550         = cp_parser_parameter_declaration (parser,
11551                                            /*template_parm_p=*/false,
11552                                            &parenthesized_p);
11553
11554       /* If a parse error occurred parsing the parameter declaration,
11555          then the entire parameter-declaration-list is erroneous.  */
11556       if (!parameter)
11557         {
11558           *is_error = true;
11559           parameters = NULL;
11560           break;
11561         }
11562       /* Add the new parameter to the list.  */
11563       *tail = parameter;
11564       tail = &parameter->next;
11565
11566       /* Peek at the next token.  */
11567       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11568           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11569         /* The parameter-declaration-list is complete.  */
11570         break;
11571       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11572         {
11573           cp_token *token;
11574
11575           /* Peek at the next token.  */
11576           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11577           /* If it's an ellipsis, then the list is complete.  */
11578           if (token->type == CPP_ELLIPSIS)
11579             break;
11580           /* Otherwise, there must be more parameters.  Consume the
11581              `,'.  */
11582           cp_lexer_consume_token (parser->lexer);
11583           /* When parsing something like:
11584
11585                 int i(float f, double d)
11586
11587              we can tell after seeing the declaration for "f" that we
11588              are not looking at an initialization of a variable "i",
11589              but rather at the declaration of a function "i".
11590
11591              Due to the fact that the parsing of template arguments
11592              (as specified to a template-id) requires backtracking we
11593              cannot use this technique when inside a template argument
11594              list.  */
11595           if (!parser->in_template_argument_list_p
11596               && !parser->in_type_id_in_expr_p
11597               && cp_parser_parsing_tentatively (parser)
11598               && !cp_parser_committed_to_tentative_parse (parser)
11599               /* However, a parameter-declaration of the form
11600                  "foat(f)" (which is a valid declaration of a
11601                  parameter "f") can also be interpreted as an
11602                  expression (the conversion of "f" to "float").  */
11603               && !parenthesized_p)
11604             cp_parser_commit_to_tentative_parse (parser);
11605         }
11606       else
11607         {
11608           cp_parser_error (parser, "expected %<,%> or %<...%>");
11609           if (!cp_parser_parsing_tentatively (parser)
11610               || cp_parser_committed_to_tentative_parse (parser))
11611             cp_parser_skip_to_closing_parenthesis (parser,
11612                                                    /*recovering=*/true,
11613                                                    /*or_comma=*/false,
11614                                                    /*consume_paren=*/false);
11615           break;
11616         }
11617     }
11618
11619   return parameters;
11620 }
11621
11622 /* Parse a parameter declaration.
11623
11624    parameter-declaration:
11625      decl-specifier-seq declarator
11626      decl-specifier-seq declarator = assignment-expression
11627      decl-specifier-seq abstract-declarator [opt]
11628      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11629
11630    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11631    declares a template parameter.  (In that case, a non-nested `>'
11632    token encountered during the parsing of the assignment-expression
11633    is not interpreted as a greater-than operator.)
11634
11635    Returns a representation of the parameter, or NULL if an error
11636    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11637    true iff the declarator is of the form "(p)".  */
11638
11639 static cp_parameter_declarator *
11640 cp_parser_parameter_declaration (cp_parser *parser,
11641                                  bool template_parm_p,
11642                                  bool *parenthesized_p)
11643 {
11644   int declares_class_or_enum;
11645   bool greater_than_is_operator_p;
11646   cp_decl_specifier_seq decl_specifiers;
11647   cp_declarator *declarator;
11648   tree default_argument;
11649   cp_token *token;
11650   const char *saved_message;
11651
11652   /* In a template parameter, `>' is not an operator.
11653
11654      [temp.param]
11655
11656      When parsing a default template-argument for a non-type
11657      template-parameter, the first non-nested `>' is taken as the end
11658      of the template parameter-list rather than a greater-than
11659      operator.  */
11660   greater_than_is_operator_p = !template_parm_p;
11661
11662   /* Type definitions may not appear in parameter types.  */
11663   saved_message = parser->type_definition_forbidden_message;
11664   parser->type_definition_forbidden_message
11665     = "types may not be defined in parameter types";
11666
11667   /* Parse the declaration-specifiers.  */
11668   cp_parser_decl_specifier_seq (parser,
11669                                 CP_PARSER_FLAGS_NONE,
11670                                 &decl_specifiers,
11671                                 &declares_class_or_enum);
11672   /* If an error occurred, there's no reason to attempt to parse the
11673      rest of the declaration.  */
11674   if (cp_parser_error_occurred (parser))
11675     {
11676       parser->type_definition_forbidden_message = saved_message;
11677       return NULL;
11678     }
11679
11680   /* Peek at the next token.  */
11681   token = cp_lexer_peek_token (parser->lexer);
11682   /* If the next token is a `)', `,', `=', `>', or `...', then there
11683      is no declarator.  */
11684   if (token->type == CPP_CLOSE_PAREN
11685       || token->type == CPP_COMMA
11686       || token->type == CPP_EQ
11687       || token->type == CPP_ELLIPSIS
11688       || token->type == CPP_GREATER)
11689     {
11690       declarator = NULL;
11691       if (parenthesized_p)
11692         *parenthesized_p = false;
11693     }
11694   /* Otherwise, there should be a declarator.  */
11695   else
11696     {
11697       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11698       parser->default_arg_ok_p = false;
11699
11700       /* After seeing a decl-specifier-seq, if the next token is not a
11701          "(", there is no possibility that the code is a valid
11702          expression.  Therefore, if parsing tentatively, we commit at
11703          this point.  */
11704       if (!parser->in_template_argument_list_p
11705           /* In an expression context, having seen:
11706
11707                (int((char ...
11708
11709              we cannot be sure whether we are looking at a
11710              function-type (taking a "char" as a parameter) or a cast
11711              of some object of type "char" to "int".  */
11712           && !parser->in_type_id_in_expr_p
11713           && cp_parser_parsing_tentatively (parser)
11714           && !cp_parser_committed_to_tentative_parse (parser)
11715           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11716         cp_parser_commit_to_tentative_parse (parser);
11717       /* Parse the declarator.  */
11718       declarator = cp_parser_declarator (parser,
11719                                          CP_PARSER_DECLARATOR_EITHER,
11720                                          /*ctor_dtor_or_conv_p=*/NULL,
11721                                          parenthesized_p,
11722                                          /*member_p=*/false);
11723       parser->default_arg_ok_p = saved_default_arg_ok_p;
11724       /* After the declarator, allow more attributes.  */
11725       decl_specifiers.attributes
11726         = chainon (decl_specifiers.attributes,
11727                    cp_parser_attributes_opt (parser));
11728     }
11729
11730   /* The restriction on defining new types applies only to the type
11731      of the parameter, not to the default argument.  */
11732   parser->type_definition_forbidden_message = saved_message;
11733
11734   /* If the next token is `=', then process a default argument.  */
11735   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11736     {
11737       bool saved_greater_than_is_operator_p;
11738       /* Consume the `='.  */
11739       cp_lexer_consume_token (parser->lexer);
11740
11741       /* If we are defining a class, then the tokens that make up the
11742          default argument must be saved and processed later.  */
11743       if (!template_parm_p && at_class_scope_p ()
11744           && TYPE_BEING_DEFINED (current_class_type))
11745         {
11746           unsigned depth = 0;
11747           cp_token *first_token;
11748           cp_token *token;
11749
11750           /* Add tokens until we have processed the entire default
11751              argument.  We add the range [first_token, token). */
11752           first_token = cp_lexer_peek_token (parser->lexer);
11753           while (true)
11754             {
11755               bool done = false;
11756
11757               /* Peek at the next token.  */
11758               token = cp_lexer_peek_token (parser->lexer);
11759               /* What we do depends on what token we have.  */
11760               switch (token->type)
11761                 {
11762                   /* In valid code, a default argument must be
11763                      immediately followed by a `,' `)', or `...'.  */
11764                 case CPP_COMMA:
11765                 case CPP_CLOSE_PAREN:
11766                 case CPP_ELLIPSIS:
11767                   /* If we run into a non-nested `;', `}', or `]',
11768                      then the code is invalid -- but the default
11769                      argument is certainly over.  */
11770                 case CPP_SEMICOLON:
11771                 case CPP_CLOSE_BRACE:
11772                 case CPP_CLOSE_SQUARE:
11773                   if (depth == 0)
11774                     done = true;
11775                   /* Update DEPTH, if necessary.  */
11776                   else if (token->type == CPP_CLOSE_PAREN
11777                            || token->type == CPP_CLOSE_BRACE
11778                            || token->type == CPP_CLOSE_SQUARE)
11779                     --depth;
11780                   break;
11781
11782                 case CPP_OPEN_PAREN:
11783                 case CPP_OPEN_SQUARE:
11784                 case CPP_OPEN_BRACE:
11785                   ++depth;
11786                   break;
11787
11788                 case CPP_GREATER:
11789                   /* If we see a non-nested `>', and `>' is not an
11790                      operator, then it marks the end of the default
11791                      argument.  */
11792                   if (!depth && !greater_than_is_operator_p)
11793                     done = true;
11794                   break;
11795
11796                   /* If we run out of tokens, issue an error message.  */
11797                 case CPP_EOF:
11798                   error ("file ends in default argument");
11799                   done = true;
11800                   break;
11801
11802                 case CPP_NAME:
11803                 case CPP_SCOPE:
11804                   /* In these cases, we should look for template-ids.
11805                      For example, if the default argument is
11806                      `X<int, double>()', we need to do name lookup to
11807                      figure out whether or not `X' is a template; if
11808                      so, the `,' does not end the default argument.
11809
11810                      That is not yet done.  */
11811                   break;
11812
11813                 default:
11814                   break;
11815                 }
11816
11817               /* If we've reached the end, stop.  */
11818               if (done)
11819                 break;
11820
11821               /* Add the token to the token block.  */
11822               token = cp_lexer_consume_token (parser->lexer);
11823             }
11824
11825           /* Create a DEFAULT_ARG to represented the unparsed default
11826              argument.  */
11827           default_argument = make_node (DEFAULT_ARG);
11828           DEFARG_TOKENS (default_argument)
11829             = cp_token_cache_new (first_token, token);  
11830         }
11831       /* Outside of a class definition, we can just parse the
11832          assignment-expression.  */
11833       else
11834         {
11835           bool saved_local_variables_forbidden_p;
11836
11837           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11838              set correctly.  */
11839           saved_greater_than_is_operator_p
11840             = parser->greater_than_is_operator_p;
11841           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11842           /* Local variable names (and the `this' keyword) may not
11843              appear in a default argument.  */
11844           saved_local_variables_forbidden_p
11845             = parser->local_variables_forbidden_p;
11846           parser->local_variables_forbidden_p = true;
11847           /* Parse the assignment-expression.  */
11848           default_argument = cp_parser_assignment_expression (parser);
11849           /* Restore saved state.  */
11850           parser->greater_than_is_operator_p
11851             = saved_greater_than_is_operator_p;
11852           parser->local_variables_forbidden_p
11853             = saved_local_variables_forbidden_p;
11854         }
11855       if (!parser->default_arg_ok_p)
11856         {
11857           if (!flag_pedantic_errors)
11858             warning ("deprecated use of default argument for parameter of non-function");
11859           else
11860             {
11861               error ("default arguments are only permitted for function parameters");
11862               default_argument = NULL_TREE;
11863             }
11864         }
11865     }
11866   else
11867     default_argument = NULL_TREE;
11868
11869   return make_parameter_declarator (&decl_specifiers,
11870                                     declarator,
11871                                     default_argument);
11872 }
11873
11874 /* Parse a function-body.
11875
11876    function-body:
11877      compound_statement  */
11878
11879 static void
11880 cp_parser_function_body (cp_parser *parser)
11881 {
11882   cp_parser_compound_statement (parser, NULL, false);
11883 }
11884
11885 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11886    true if a ctor-initializer was present.  */
11887
11888 static bool
11889 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11890 {
11891   tree body;
11892   bool ctor_initializer_p;
11893
11894   /* Begin the function body.  */
11895   body = begin_function_body ();
11896   /* Parse the optional ctor-initializer.  */
11897   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11898   /* Parse the function-body.  */
11899   cp_parser_function_body (parser);
11900   /* Finish the function body.  */
11901   finish_function_body (body);
11902
11903   return ctor_initializer_p;
11904 }
11905
11906 /* Parse an initializer.
11907
11908    initializer:
11909      = initializer-clause
11910      ( expression-list )
11911
11912    Returns a expression representing the initializer.  If no
11913    initializer is present, NULL_TREE is returned.
11914
11915    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11916    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11917    set to FALSE if there is no initializer present.  If there is an
11918    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11919    is set to true; otherwise it is set to false.  */
11920
11921 static tree
11922 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11923                        bool* non_constant_p)
11924 {
11925   cp_token *token;
11926   tree init;
11927
11928   /* Peek at the next token.  */
11929   token = cp_lexer_peek_token (parser->lexer);
11930
11931   /* Let our caller know whether or not this initializer was
11932      parenthesized.  */
11933   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11934   /* Assume that the initializer is constant.  */
11935   *non_constant_p = false;
11936
11937   if (token->type == CPP_EQ)
11938     {
11939       /* Consume the `='.  */
11940       cp_lexer_consume_token (parser->lexer);
11941       /* Parse the initializer-clause.  */
11942       init = cp_parser_initializer_clause (parser, non_constant_p);
11943     }
11944   else if (token->type == CPP_OPEN_PAREN)
11945     init = cp_parser_parenthesized_expression_list (parser, false,
11946                                                     non_constant_p);
11947   else
11948     {
11949       /* Anything else is an error.  */
11950       cp_parser_error (parser, "expected initializer");
11951       init = error_mark_node;
11952     }
11953
11954   return init;
11955 }
11956
11957 /* Parse an initializer-clause.
11958
11959    initializer-clause:
11960      assignment-expression
11961      { initializer-list , [opt] }
11962      { }
11963
11964    Returns an expression representing the initializer.
11965
11966    If the `assignment-expression' production is used the value
11967    returned is simply a representation for the expression.
11968
11969    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11970    the elements of the initializer-list (or NULL_TREE, if the last
11971    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11972    NULL_TREE.  There is no way to detect whether or not the optional
11973    trailing `,' was provided.  NON_CONSTANT_P is as for
11974    cp_parser_initializer.  */
11975
11976 static tree
11977 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11978 {
11979   tree initializer;
11980
11981   /* If it is not a `{', then we are looking at an
11982      assignment-expression.  */
11983   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11984     {
11985       initializer
11986         = cp_parser_constant_expression (parser,
11987                                         /*allow_non_constant_p=*/true,
11988                                         non_constant_p);
11989       if (!*non_constant_p)
11990         initializer = fold_non_dependent_expr (initializer);
11991     }
11992   else
11993     {
11994       /* Consume the `{' token.  */
11995       cp_lexer_consume_token (parser->lexer);
11996       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11997       initializer = make_node (CONSTRUCTOR);
11998       /* If it's not a `}', then there is a non-trivial initializer.  */
11999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12000         {
12001           /* Parse the initializer list.  */
12002           CONSTRUCTOR_ELTS (initializer)
12003             = cp_parser_initializer_list (parser, non_constant_p);
12004           /* A trailing `,' token is allowed.  */
12005           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12006             cp_lexer_consume_token (parser->lexer);
12007         }
12008       /* Now, there should be a trailing `}'.  */
12009       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12010     }
12011
12012   return initializer;
12013 }
12014
12015 /* Parse an initializer-list.
12016
12017    initializer-list:
12018      initializer-clause
12019      initializer-list , initializer-clause
12020
12021    GNU Extension:
12022
12023    initializer-list:
12024      identifier : initializer-clause
12025      initializer-list, identifier : initializer-clause
12026
12027    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12028    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12029    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12030    as for cp_parser_initializer.  */
12031
12032 static tree
12033 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12034 {
12035   tree initializers = NULL_TREE;
12036
12037   /* Assume all of the expressions are constant.  */
12038   *non_constant_p = false;
12039
12040   /* Parse the rest of the list.  */
12041   while (true)
12042     {
12043       cp_token *token;
12044       tree identifier;
12045       tree initializer;
12046       bool clause_non_constant_p;
12047
12048       /* If the next token is an identifier and the following one is a
12049          colon, we are looking at the GNU designated-initializer
12050          syntax.  */
12051       if (cp_parser_allow_gnu_extensions_p (parser)
12052           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12053           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12054         {
12055           /* Consume the identifier.  */
12056           identifier = cp_lexer_consume_token (parser->lexer)->value;
12057           /* Consume the `:'.  */
12058           cp_lexer_consume_token (parser->lexer);
12059         }
12060       else
12061         identifier = NULL_TREE;
12062
12063       /* Parse the initializer.  */
12064       initializer = cp_parser_initializer_clause (parser,
12065                                                   &clause_non_constant_p);
12066       /* If any clause is non-constant, so is the entire initializer.  */
12067       if (clause_non_constant_p)
12068         *non_constant_p = true;
12069       /* Add it to the list.  */
12070       initializers = tree_cons (identifier, initializer, initializers);
12071
12072       /* If the next token is not a comma, we have reached the end of
12073          the list.  */
12074       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12075         break;
12076
12077       /* Peek at the next token.  */
12078       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12079       /* If the next token is a `}', then we're still done.  An
12080          initializer-clause can have a trailing `,' after the
12081          initializer-list and before the closing `}'.  */
12082       if (token->type == CPP_CLOSE_BRACE)
12083         break;
12084
12085       /* Consume the `,' token.  */
12086       cp_lexer_consume_token (parser->lexer);
12087     }
12088
12089   /* The initializers were built up in reverse order, so we need to
12090      reverse them now.  */
12091   return nreverse (initializers);
12092 }
12093
12094 /* Classes [gram.class] */
12095
12096 /* Parse a class-name.
12097
12098    class-name:
12099      identifier
12100      template-id
12101
12102    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12103    to indicate that names looked up in dependent types should be
12104    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12105    keyword has been used to indicate that the name that appears next
12106    is a template.  TYPE_P is true iff the next name should be treated
12107    as class-name, even if it is declared to be some other kind of name
12108    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12109    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12110    being defined in a class-head.
12111
12112    Returns the TYPE_DECL representing the class.  */
12113
12114 static tree
12115 cp_parser_class_name (cp_parser *parser,
12116                       bool typename_keyword_p,
12117                       bool template_keyword_p,
12118                       bool type_p,
12119                       bool check_dependency_p,
12120                       bool class_head_p,
12121                       bool is_declaration)
12122 {
12123   tree decl;
12124   tree scope;
12125   bool typename_p;
12126   cp_token *token;
12127
12128   /* All class-names start with an identifier.  */
12129   token = cp_lexer_peek_token (parser->lexer);
12130   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12131     {
12132       cp_parser_error (parser, "expected class-name");
12133       return error_mark_node;
12134     }
12135
12136   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12137      to a template-id, so we save it here.  */
12138   scope = parser->scope;
12139   if (scope == error_mark_node)
12140     return error_mark_node;
12141
12142   /* Any name names a type if we're following the `typename' keyword
12143      in a qualified name where the enclosing scope is type-dependent.  */
12144   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12145                 && dependent_type_p (scope));
12146   /* Handle the common case (an identifier, but not a template-id)
12147      efficiently.  */
12148   if (token->type == CPP_NAME
12149       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12150     {
12151       tree identifier;
12152
12153       /* Look for the identifier.  */
12154       identifier = cp_parser_identifier (parser);
12155       /* If the next token isn't an identifier, we are certainly not
12156          looking at a class-name.  */
12157       if (identifier == error_mark_node)
12158         decl = error_mark_node;
12159       /* If we know this is a type-name, there's no need to look it
12160          up.  */
12161       else if (typename_p)
12162         decl = identifier;
12163       else
12164         {
12165           /* If the next token is a `::', then the name must be a type
12166              name.
12167
12168              [basic.lookup.qual]
12169
12170              During the lookup for a name preceding the :: scope
12171              resolution operator, object, function, and enumerator
12172              names are ignored.  */
12173           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12174             type_p = true;
12175           /* Look up the name.  */
12176           decl = cp_parser_lookup_name (parser, identifier,
12177                                         type_p,
12178                                         /*is_template=*/false,
12179                                         /*is_namespace=*/false,
12180                                         check_dependency_p,
12181                                         /*ambiguous_p=*/NULL);
12182         }
12183     }
12184   else
12185     {
12186       /* Try a template-id.  */
12187       decl = cp_parser_template_id (parser, template_keyword_p,
12188                                     check_dependency_p,
12189                                     is_declaration);
12190       if (decl == error_mark_node)
12191         return error_mark_node;
12192     }
12193
12194   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12195
12196   /* If this is a typename, create a TYPENAME_TYPE.  */
12197   if (typename_p && decl != error_mark_node)
12198     {
12199       decl = make_typename_type (scope, decl, /*complain=*/1);
12200       if (decl != error_mark_node)
12201         decl = TYPE_NAME (decl);
12202     }
12203
12204   /* Check to see that it is really the name of a class.  */
12205   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12206       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12207       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12208     /* Situations like this:
12209
12210          template <typename T> struct A {
12211            typename T::template X<int>::I i;
12212          };
12213
12214        are problematic.  Is `T::template X<int>' a class-name?  The
12215        standard does not seem to be definitive, but there is no other
12216        valid interpretation of the following `::'.  Therefore, those
12217        names are considered class-names.  */
12218     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12219   else if (decl == error_mark_node
12220            || TREE_CODE (decl) != TYPE_DECL
12221            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12222     {
12223       cp_parser_error (parser, "expected class-name");
12224       return error_mark_node;
12225     }
12226
12227   return decl;
12228 }
12229
12230 /* Parse a class-specifier.
12231
12232    class-specifier:
12233      class-head { member-specification [opt] }
12234
12235    Returns the TREE_TYPE representing the class.  */
12236
12237 static tree
12238 cp_parser_class_specifier (cp_parser* parser)
12239 {
12240   cp_token *token;
12241   tree type;
12242   tree attributes = NULL_TREE;
12243   int has_trailing_semicolon;
12244   bool nested_name_specifier_p;
12245   unsigned saved_num_template_parameter_lists;
12246   bool pop_p = false;
12247   tree scope = NULL_TREE;
12248
12249   push_deferring_access_checks (dk_no_deferred);
12250
12251   /* Parse the class-head.  */
12252   type = cp_parser_class_head (parser,
12253                                &nested_name_specifier_p,
12254                                &attributes);
12255   /* If the class-head was a semantic disaster, skip the entire body
12256      of the class.  */
12257   if (!type)
12258     {
12259       cp_parser_skip_to_end_of_block_or_statement (parser);
12260       pop_deferring_access_checks ();
12261       return error_mark_node;
12262     }
12263
12264   /* Look for the `{'.  */
12265   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12266     {
12267       pop_deferring_access_checks ();
12268       return error_mark_node;
12269     }
12270
12271   /* Issue an error message if type-definitions are forbidden here.  */
12272   cp_parser_check_type_definition (parser);
12273   /* Remember that we are defining one more class.  */
12274   ++parser->num_classes_being_defined;
12275   /* Inside the class, surrounding template-parameter-lists do not
12276      apply.  */
12277   saved_num_template_parameter_lists
12278     = parser->num_template_parameter_lists;
12279   parser->num_template_parameter_lists = 0;
12280
12281   /* Start the class.  */
12282   if (nested_name_specifier_p)
12283     {
12284       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12285       pop_p = push_scope (scope);
12286     }
12287   type = begin_class_definition (type);
12288
12289   if (type == error_mark_node)
12290     /* If the type is erroneous, skip the entire body of the class.  */
12291     cp_parser_skip_to_closing_brace (parser);
12292   else
12293     /* Parse the member-specification.  */
12294     cp_parser_member_specification_opt (parser);
12295
12296   /* Look for the trailing `}'.  */
12297   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12298   /* We get better error messages by noticing a common problem: a
12299      missing trailing `;'.  */
12300   token = cp_lexer_peek_token (parser->lexer);
12301   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12302   /* Look for trailing attributes to apply to this class.  */
12303   if (cp_parser_allow_gnu_extensions_p (parser))
12304     {
12305       tree sub_attr = cp_parser_attributes_opt (parser);
12306       attributes = chainon (attributes, sub_attr);
12307     }
12308   if (type != error_mark_node)
12309     type = finish_struct (type, attributes);
12310   if (pop_p)
12311     pop_scope (scope);
12312   /* If this class is not itself within the scope of another class,
12313      then we need to parse the bodies of all of the queued function
12314      definitions.  Note that the queued functions defined in a class
12315      are not always processed immediately following the
12316      class-specifier for that class.  Consider:
12317
12318        struct A {
12319          struct B { void f() { sizeof (A); } };
12320        };
12321
12322      If `f' were processed before the processing of `A' were
12323      completed, there would be no way to compute the size of `A'.
12324      Note that the nesting we are interested in here is lexical --
12325      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12326      for:
12327
12328        struct A { struct B; };
12329        struct A::B { void f() { } };
12330
12331      there is no need to delay the parsing of `A::B::f'.  */
12332   if (--parser->num_classes_being_defined == 0)
12333     {
12334       tree queue_entry;
12335       tree fn;
12336       tree class_type;
12337       bool pop_p;
12338
12339       /* In a first pass, parse default arguments to the functions.
12340          Then, in a second pass, parse the bodies of the functions.
12341          This two-phased approach handles cases like:
12342
12343             struct S {
12344               void f() { g(); }
12345               void g(int i = 3);
12346             };
12347
12348          */
12349       class_type = NULL_TREE;
12350       pop_p = false;
12351       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12352              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12353            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12354            TREE_PURPOSE (parser->unparsed_functions_queues)
12355              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12356         {
12357           fn = TREE_VALUE (queue_entry);
12358           /* If there are default arguments that have not yet been processed,
12359              take care of them now.  */
12360           if (class_type != TREE_PURPOSE (queue_entry))
12361             {
12362               if (pop_p)
12363                 pop_scope (class_type);
12364               class_type = TREE_PURPOSE (queue_entry);
12365               pop_p = push_scope (class_type);
12366             }
12367           /* Make sure that any template parameters are in scope.  */
12368           maybe_begin_member_template_processing (fn);
12369           /* Parse the default argument expressions.  */
12370           cp_parser_late_parsing_default_args (parser, fn);
12371           /* Remove any template parameters from the symbol table.  */
12372           maybe_end_member_template_processing ();
12373         }
12374       if (pop_p)
12375         pop_scope (class_type);
12376       /* Now parse the body of the functions.  */
12377       for (TREE_VALUE (parser->unparsed_functions_queues)
12378              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12379            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12380            TREE_VALUE (parser->unparsed_functions_queues)
12381              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12382         {
12383           /* Figure out which function we need to process.  */
12384           fn = TREE_VALUE (queue_entry);
12385
12386           /* A hack to prevent garbage collection.  */
12387           function_depth++;
12388
12389           /* Parse the function.  */
12390           cp_parser_late_parsing_for_member (parser, fn);
12391           function_depth--;
12392         }
12393     }
12394
12395   /* Put back any saved access checks.  */
12396   pop_deferring_access_checks ();
12397
12398   /* Restore the count of active template-parameter-lists.  */
12399   parser->num_template_parameter_lists
12400     = saved_num_template_parameter_lists;
12401
12402   return type;
12403 }
12404
12405 /* Parse a class-head.
12406
12407    class-head:
12408      class-key identifier [opt] base-clause [opt]
12409      class-key nested-name-specifier identifier base-clause [opt]
12410      class-key nested-name-specifier [opt] template-id
12411        base-clause [opt]
12412
12413    GNU Extensions:
12414      class-key attributes identifier [opt] base-clause [opt]
12415      class-key attributes nested-name-specifier identifier base-clause [opt]
12416      class-key attributes nested-name-specifier [opt] template-id
12417        base-clause [opt]
12418
12419    Returns the TYPE of the indicated class.  Sets
12420    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12421    involving a nested-name-specifier was used, and FALSE otherwise.
12422
12423    Returns NULL_TREE if the class-head is syntactically valid, but
12424    semantically invalid in a way that means we should skip the entire
12425    body of the class.  */
12426
12427 static tree
12428 cp_parser_class_head (cp_parser* parser,
12429                       bool* nested_name_specifier_p,
12430                       tree *attributes_p)
12431 {
12432   tree nested_name_specifier;
12433   enum tag_types class_key;
12434   tree id = NULL_TREE;
12435   tree type = NULL_TREE;
12436   tree attributes;
12437   bool template_id_p = false;
12438   bool qualified_p = false;
12439   bool invalid_nested_name_p = false;
12440   bool invalid_explicit_specialization_p = false;
12441   bool pop_p = false;
12442   unsigned num_templates;
12443   tree bases;
12444
12445   /* Assume no nested-name-specifier will be present.  */
12446   *nested_name_specifier_p = false;
12447   /* Assume no template parameter lists will be used in defining the
12448      type.  */
12449   num_templates = 0;
12450
12451   /* Look for the class-key.  */
12452   class_key = cp_parser_class_key (parser);
12453   if (class_key == none_type)
12454     return error_mark_node;
12455
12456   /* Parse the attributes.  */
12457   attributes = cp_parser_attributes_opt (parser);
12458
12459   /* If the next token is `::', that is invalid -- but sometimes
12460      people do try to write:
12461
12462        struct ::S {};
12463
12464      Handle this gracefully by accepting the extra qualifier, and then
12465      issuing an error about it later if this really is a
12466      class-head.  If it turns out just to be an elaborated type
12467      specifier, remain silent.  */
12468   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12469     qualified_p = true;
12470
12471   push_deferring_access_checks (dk_no_check);
12472
12473   /* Determine the name of the class.  Begin by looking for an
12474      optional nested-name-specifier.  */
12475   nested_name_specifier
12476     = cp_parser_nested_name_specifier_opt (parser,
12477                                            /*typename_keyword_p=*/false,
12478                                            /*check_dependency_p=*/false,
12479                                            /*type_p=*/false,
12480                                            /*is_declaration=*/false);
12481   /* If there was a nested-name-specifier, then there *must* be an
12482      identifier.  */
12483   if (nested_name_specifier)
12484     {
12485       /* Although the grammar says `identifier', it really means
12486          `class-name' or `template-name'.  You are only allowed to
12487          define a class that has already been declared with this
12488          syntax.
12489
12490          The proposed resolution for Core Issue 180 says that whever
12491          you see `class T::X' you should treat `X' as a type-name.
12492
12493          It is OK to define an inaccessible class; for example:
12494
12495            class A { class B; };
12496            class A::B {};
12497
12498          We do not know if we will see a class-name, or a
12499          template-name.  We look for a class-name first, in case the
12500          class-name is a template-id; if we looked for the
12501          template-name first we would stop after the template-name.  */
12502       cp_parser_parse_tentatively (parser);
12503       type = cp_parser_class_name (parser,
12504                                    /*typename_keyword_p=*/false,
12505                                    /*template_keyword_p=*/false,
12506                                    /*type_p=*/true,
12507                                    /*check_dependency_p=*/false,
12508                                    /*class_head_p=*/true,
12509                                    /*is_declaration=*/false);
12510       /* If that didn't work, ignore the nested-name-specifier.  */
12511       if (!cp_parser_parse_definitely (parser))
12512         {
12513           invalid_nested_name_p = true;
12514           id = cp_parser_identifier (parser);
12515           if (id == error_mark_node)
12516             id = NULL_TREE;
12517         }
12518       /* If we could not find a corresponding TYPE, treat this
12519          declaration like an unqualified declaration.  */
12520       if (type == error_mark_node)
12521         nested_name_specifier = NULL_TREE;
12522       /* Otherwise, count the number of templates used in TYPE and its
12523          containing scopes.  */
12524       else
12525         {
12526           tree scope;
12527
12528           for (scope = TREE_TYPE (type);
12529                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12530                scope = (TYPE_P (scope)
12531                         ? TYPE_CONTEXT (scope)
12532                         : DECL_CONTEXT (scope)))
12533             if (TYPE_P (scope)
12534                 && CLASS_TYPE_P (scope)
12535                 && CLASSTYPE_TEMPLATE_INFO (scope)
12536                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12537                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12538               ++num_templates;
12539         }
12540     }
12541   /* Otherwise, the identifier is optional.  */
12542   else
12543     {
12544       /* We don't know whether what comes next is a template-id,
12545          an identifier, or nothing at all.  */
12546       cp_parser_parse_tentatively (parser);
12547       /* Check for a template-id.  */
12548       id = cp_parser_template_id (parser,
12549                                   /*template_keyword_p=*/false,
12550                                   /*check_dependency_p=*/true,
12551                                   /*is_declaration=*/true);
12552       /* If that didn't work, it could still be an identifier.  */
12553       if (!cp_parser_parse_definitely (parser))
12554         {
12555           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12556             id = cp_parser_identifier (parser);
12557           else
12558             id = NULL_TREE;
12559         }
12560       else
12561         {
12562           template_id_p = true;
12563           ++num_templates;
12564         }
12565     }
12566
12567   pop_deferring_access_checks ();
12568
12569   if (id)
12570     cp_parser_check_for_invalid_template_id (parser, id);
12571
12572   /* If it's not a `:' or a `{' then we can't really be looking at a
12573      class-head, since a class-head only appears as part of a
12574      class-specifier.  We have to detect this situation before calling
12575      xref_tag, since that has irreversible side-effects.  */
12576   if (!cp_parser_next_token_starts_class_definition_p (parser))
12577     {
12578       cp_parser_error (parser, "expected %<{%> or %<:%>");
12579       return error_mark_node;
12580     }
12581
12582   /* At this point, we're going ahead with the class-specifier, even
12583      if some other problem occurs.  */
12584   cp_parser_commit_to_tentative_parse (parser);
12585   /* Issue the error about the overly-qualified name now.  */
12586   if (qualified_p)
12587     cp_parser_error (parser,
12588                      "global qualification of class name is invalid");
12589   else if (invalid_nested_name_p)
12590     cp_parser_error (parser,
12591                      "qualified name does not name a class");
12592   else if (nested_name_specifier)
12593     {
12594       tree scope;
12595       /* Figure out in what scope the declaration is being placed.  */
12596       scope = current_scope ();
12597       if (!scope)
12598         scope = current_namespace;
12599       /* If that scope does not contain the scope in which the
12600          class was originally declared, the program is invalid.  */
12601       if (scope && !is_ancestor (scope, nested_name_specifier))
12602         {
12603           error ("declaration of %qD in %qD which does not enclose %qD",
12604                  type, scope, nested_name_specifier);
12605           type = NULL_TREE;
12606           goto done;
12607         }
12608       /* [dcl.meaning]
12609
12610          A declarator-id shall not be qualified exception of the
12611          definition of a ... nested class outside of its class
12612          ... [or] a the definition or explicit instantiation of a
12613          class member of a namespace outside of its namespace.  */
12614       if (scope == nested_name_specifier)
12615         {
12616           pedwarn ("extra qualification ignored");
12617           nested_name_specifier = NULL_TREE;
12618           num_templates = 0;
12619         }
12620     }
12621   /* An explicit-specialization must be preceded by "template <>".  If
12622      it is not, try to recover gracefully.  */
12623   if (at_namespace_scope_p ()
12624       && parser->num_template_parameter_lists == 0
12625       && template_id_p)
12626     {
12627       error ("an explicit specialization must be preceded by %<template <>%>");
12628       invalid_explicit_specialization_p = true;
12629       /* Take the same action that would have been taken by
12630          cp_parser_explicit_specialization.  */
12631       ++parser->num_template_parameter_lists;
12632       begin_specialization ();
12633     }
12634   /* There must be no "return" statements between this point and the
12635      end of this function; set "type "to the correct return value and
12636      use "goto done;" to return.  */
12637   /* Make sure that the right number of template parameters were
12638      present.  */
12639   if (!cp_parser_check_template_parameters (parser, num_templates))
12640     {
12641       /* If something went wrong, there is no point in even trying to
12642          process the class-definition.  */
12643       type = NULL_TREE;
12644       goto done;
12645     }
12646
12647   /* Look up the type.  */
12648   if (template_id_p)
12649     {
12650       type = TREE_TYPE (id);
12651       maybe_process_partial_specialization (type);
12652     }
12653   else if (!nested_name_specifier)
12654     {
12655       /* If the class was unnamed, create a dummy name.  */
12656       if (!id)
12657         id = make_anon_name ();
12658       type = xref_tag (class_key, id, /*globalize=*/false,
12659                        parser->num_template_parameter_lists);
12660     }
12661   else
12662     {
12663       tree class_type;
12664       bool pop_p = false;
12665
12666       /* Given:
12667
12668             template <typename T> struct S { struct T };
12669             template <typename T> struct S<T>::T { };
12670
12671          we will get a TYPENAME_TYPE when processing the definition of
12672          `S::T'.  We need to resolve it to the actual type before we
12673          try to define it.  */
12674       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12675         {
12676           class_type = resolve_typename_type (TREE_TYPE (type),
12677                                               /*only_current_p=*/false);
12678           if (class_type != error_mark_node)
12679             type = TYPE_NAME (class_type);
12680           else
12681             {
12682               cp_parser_error (parser, "could not resolve typename type");
12683               type = error_mark_node;
12684             }
12685         }
12686
12687       maybe_process_partial_specialization (TREE_TYPE (type));
12688       class_type = current_class_type;
12689       /* Enter the scope indicated by the nested-name-specifier.  */
12690       if (nested_name_specifier)
12691         pop_p = push_scope (nested_name_specifier);
12692       /* Get the canonical version of this type.  */
12693       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12694       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12695           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12696         type = push_template_decl (type);
12697       type = TREE_TYPE (type);
12698       if (nested_name_specifier)
12699         {
12700           *nested_name_specifier_p = true;
12701           if (pop_p)
12702             pop_scope (nested_name_specifier);
12703         }
12704     }
12705   /* Indicate whether this class was declared as a `class' or as a
12706      `struct'.  */
12707   if (TREE_CODE (type) == RECORD_TYPE)
12708     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12709   cp_parser_check_class_key (class_key, type);
12710
12711   /* Enter the scope containing the class; the names of base classes
12712      should be looked up in that context.  For example, given:
12713
12714        struct A { struct B {}; struct C; };
12715        struct A::C : B {};
12716
12717      is valid.  */
12718   if (nested_name_specifier)
12719     pop_p = push_scope (nested_name_specifier);
12720
12721   bases = NULL_TREE;
12722
12723   /* Get the list of base-classes, if there is one.  */
12724   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12725     bases = cp_parser_base_clause (parser);
12726
12727   /* Process the base classes.  */
12728   xref_basetypes (type, bases);
12729
12730   /* Leave the scope given by the nested-name-specifier.  We will
12731      enter the class scope itself while processing the members.  */
12732   if (pop_p)
12733     pop_scope (nested_name_specifier);
12734
12735  done:
12736   if (invalid_explicit_specialization_p)
12737     {
12738       end_specialization ();
12739       --parser->num_template_parameter_lists;
12740     }
12741   *attributes_p = attributes;
12742   return type;
12743 }
12744
12745 /* Parse a class-key.
12746
12747    class-key:
12748      class
12749      struct
12750      union
12751
12752    Returns the kind of class-key specified, or none_type to indicate
12753    error.  */
12754
12755 static enum tag_types
12756 cp_parser_class_key (cp_parser* parser)
12757 {
12758   cp_token *token;
12759   enum tag_types tag_type;
12760
12761   /* Look for the class-key.  */
12762   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12763   if (!token)
12764     return none_type;
12765
12766   /* Check to see if the TOKEN is a class-key.  */
12767   tag_type = cp_parser_token_is_class_key (token);
12768   if (!tag_type)
12769     cp_parser_error (parser, "expected class-key");
12770   return tag_type;
12771 }
12772
12773 /* Parse an (optional) member-specification.
12774
12775    member-specification:
12776      member-declaration member-specification [opt]
12777      access-specifier : member-specification [opt]  */
12778
12779 static void
12780 cp_parser_member_specification_opt (cp_parser* parser)
12781 {
12782   while (true)
12783     {
12784       cp_token *token;
12785       enum rid keyword;
12786
12787       /* Peek at the next token.  */
12788       token = cp_lexer_peek_token (parser->lexer);
12789       /* If it's a `}', or EOF then we've seen all the members.  */
12790       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12791         break;
12792
12793       /* See if this token is a keyword.  */
12794       keyword = token->keyword;
12795       switch (keyword)
12796         {
12797         case RID_PUBLIC:
12798         case RID_PROTECTED:
12799         case RID_PRIVATE:
12800           /* Consume the access-specifier.  */
12801           cp_lexer_consume_token (parser->lexer);
12802           /* Remember which access-specifier is active.  */
12803           current_access_specifier = token->value;
12804           /* Look for the `:'.  */
12805           cp_parser_require (parser, CPP_COLON, "`:'");
12806           break;
12807
12808         default:
12809           /* Accept #pragmas at class scope.  */
12810           if (token->type == CPP_PRAGMA)
12811             {
12812               cp_lexer_handle_pragma (parser->lexer);
12813               break;
12814             }
12815
12816           /* Otherwise, the next construction must be a
12817              member-declaration.  */
12818           cp_parser_member_declaration (parser);
12819         }
12820     }
12821 }
12822
12823 /* Parse a member-declaration.
12824
12825    member-declaration:
12826      decl-specifier-seq [opt] member-declarator-list [opt] ;
12827      function-definition ; [opt]
12828      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12829      using-declaration
12830      template-declaration
12831
12832    member-declarator-list:
12833      member-declarator
12834      member-declarator-list , member-declarator
12835
12836    member-declarator:
12837      declarator pure-specifier [opt]
12838      declarator constant-initializer [opt]
12839      identifier [opt] : constant-expression
12840
12841    GNU Extensions:
12842
12843    member-declaration:
12844      __extension__ member-declaration
12845
12846    member-declarator:
12847      declarator attributes [opt] pure-specifier [opt]
12848      declarator attributes [opt] constant-initializer [opt]
12849      identifier [opt] attributes [opt] : constant-expression  */
12850
12851 static void
12852 cp_parser_member_declaration (cp_parser* parser)
12853 {
12854   cp_decl_specifier_seq decl_specifiers;
12855   tree prefix_attributes;
12856   tree decl;
12857   int declares_class_or_enum;
12858   bool friend_p;
12859   cp_token *token;
12860   int saved_pedantic;
12861
12862   /* Check for the `__extension__' keyword.  */
12863   if (cp_parser_extension_opt (parser, &saved_pedantic))
12864     {
12865       /* Recurse.  */
12866       cp_parser_member_declaration (parser);
12867       /* Restore the old value of the PEDANTIC flag.  */
12868       pedantic = saved_pedantic;
12869
12870       return;
12871     }
12872
12873   /* Check for a template-declaration.  */
12874   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12875     {
12876       /* Parse the template-declaration.  */
12877       cp_parser_template_declaration (parser, /*member_p=*/true);
12878
12879       return;
12880     }
12881
12882   /* Check for a using-declaration.  */
12883   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12884     {
12885       /* Parse the using-declaration.  */
12886       cp_parser_using_declaration (parser);
12887
12888       return;
12889     }
12890
12891   /* Parse the decl-specifier-seq.  */
12892   cp_parser_decl_specifier_seq (parser,
12893                                 CP_PARSER_FLAGS_OPTIONAL,
12894                                 &decl_specifiers,
12895                                 &declares_class_or_enum);
12896   prefix_attributes = decl_specifiers.attributes;
12897   decl_specifiers.attributes = NULL_TREE;
12898   /* Check for an invalid type-name.  */
12899   if (!decl_specifiers.type
12900       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12901     return;
12902   /* If there is no declarator, then the decl-specifier-seq should
12903      specify a type.  */
12904   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12905     {
12906       /* If there was no decl-specifier-seq, and the next token is a
12907          `;', then we have something like:
12908
12909            struct S { ; };
12910
12911          [class.mem]
12912
12913          Each member-declaration shall declare at least one member
12914          name of the class.  */
12915       if (!decl_specifiers.any_specifiers_p)
12916         {
12917           cp_token *token = cp_lexer_peek_token (parser->lexer);
12918           if (pedantic && !token->in_system_header)
12919             pedwarn ("%Hextra %<;%>", &token->location);
12920         }
12921       else
12922         {
12923           tree type;
12924
12925           /* See if this declaration is a friend.  */
12926           friend_p = cp_parser_friend_p (&decl_specifiers);
12927           /* If there were decl-specifiers, check to see if there was
12928              a class-declaration.  */
12929           type = check_tag_decl (&decl_specifiers);
12930           /* Nested classes have already been added to the class, but
12931              a `friend' needs to be explicitly registered.  */
12932           if (friend_p)
12933             {
12934               /* If the `friend' keyword was present, the friend must
12935                  be introduced with a class-key.  */
12936                if (!declares_class_or_enum)
12937                  error ("a class-key must be used when declaring a friend");
12938                /* In this case:
12939
12940                     template <typename T> struct A {
12941                       friend struct A<T>::B;
12942                     };
12943
12944                   A<T>::B will be represented by a TYPENAME_TYPE, and
12945                   therefore not recognized by check_tag_decl.  */
12946                if (!type
12947                    && decl_specifiers.type
12948                    && TYPE_P (decl_specifiers.type))
12949                  type = decl_specifiers.type;
12950                if (!type || !TYPE_P (type))
12951                  error ("friend declaration does not name a class or "
12952                         "function");
12953                else
12954                  make_friend_class (current_class_type, type,
12955                                     /*complain=*/true);
12956             }
12957           /* If there is no TYPE, an error message will already have
12958              been issued.  */
12959           else if (!type || type == error_mark_node)
12960             ;
12961           /* An anonymous aggregate has to be handled specially; such
12962              a declaration really declares a data member (with a
12963              particular type), as opposed to a nested class.  */
12964           else if (ANON_AGGR_TYPE_P (type))
12965             {
12966               /* Remove constructors and such from TYPE, now that we
12967                  know it is an anonymous aggregate.  */
12968               fixup_anonymous_aggr (type);
12969               /* And make the corresponding data member.  */
12970               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12971               /* Add it to the class.  */
12972               finish_member_declaration (decl);
12973             }
12974           else
12975             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12976         }
12977     }
12978   else
12979     {
12980       /* See if these declarations will be friends.  */
12981       friend_p = cp_parser_friend_p (&decl_specifiers);
12982
12983       /* Keep going until we hit the `;' at the end of the
12984          declaration.  */
12985       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12986         {
12987           tree attributes = NULL_TREE;
12988           tree first_attribute;
12989
12990           /* Peek at the next token.  */
12991           token = cp_lexer_peek_token (parser->lexer);
12992
12993           /* Check for a bitfield declaration.  */
12994           if (token->type == CPP_COLON
12995               || (token->type == CPP_NAME
12996                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12997                   == CPP_COLON))
12998             {
12999               tree identifier;
13000               tree width;
13001
13002               /* Get the name of the bitfield.  Note that we cannot just
13003                  check TOKEN here because it may have been invalidated by
13004                  the call to cp_lexer_peek_nth_token above.  */
13005               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13006                 identifier = cp_parser_identifier (parser);
13007               else
13008                 identifier = NULL_TREE;
13009
13010               /* Consume the `:' token.  */
13011               cp_lexer_consume_token (parser->lexer);
13012               /* Get the width of the bitfield.  */
13013               width
13014                 = cp_parser_constant_expression (parser,
13015                                                  /*allow_non_constant=*/false,
13016                                                  NULL);
13017
13018               /* Look for attributes that apply to the bitfield.  */
13019               attributes = cp_parser_attributes_opt (parser);
13020               /* Remember which attributes are prefix attributes and
13021                  which are not.  */
13022               first_attribute = attributes;
13023               /* Combine the attributes.  */
13024               attributes = chainon (prefix_attributes, attributes);
13025
13026               /* Create the bitfield declaration.  */
13027               decl = grokbitfield (identifier
13028                                    ? make_id_declarator (identifier)
13029                                    : NULL,
13030                                    &decl_specifiers,
13031                                    width);
13032               /* Apply the attributes.  */
13033               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13034             }
13035           else
13036             {
13037               cp_declarator *declarator;
13038               tree initializer;
13039               tree asm_specification;
13040               int ctor_dtor_or_conv_p;
13041
13042               /* Parse the declarator.  */
13043               declarator
13044                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13045                                         &ctor_dtor_or_conv_p,
13046                                         /*parenthesized_p=*/NULL,
13047                                         /*member_p=*/true);
13048
13049               /* If something went wrong parsing the declarator, make sure
13050                  that we at least consume some tokens.  */
13051               if (declarator == cp_error_declarator)
13052                 {
13053                   /* Skip to the end of the statement.  */
13054                   cp_parser_skip_to_end_of_statement (parser);
13055                   /* If the next token is not a semicolon, that is
13056                      probably because we just skipped over the body of
13057                      a function.  So, we consume a semicolon if
13058                      present, but do not issue an error message if it
13059                      is not present.  */
13060                   if (cp_lexer_next_token_is (parser->lexer,
13061                                               CPP_SEMICOLON))
13062                     cp_lexer_consume_token (parser->lexer);
13063                   return;
13064                 }
13065
13066               cp_parser_check_for_definition_in_return_type
13067                 (declarator, declares_class_or_enum);
13068
13069               /* Look for an asm-specification.  */
13070               asm_specification = cp_parser_asm_specification_opt (parser);
13071               /* Look for attributes that apply to the declaration.  */
13072               attributes = cp_parser_attributes_opt (parser);
13073               /* Remember which attributes are prefix attributes and
13074                  which are not.  */
13075               first_attribute = attributes;
13076               /* Combine the attributes.  */
13077               attributes = chainon (prefix_attributes, attributes);
13078
13079               /* If it's an `=', then we have a constant-initializer or a
13080                  pure-specifier.  It is not correct to parse the
13081                  initializer before registering the member declaration
13082                  since the member declaration should be in scope while
13083                  its initializer is processed.  However, the rest of the
13084                  front end does not yet provide an interface that allows
13085                  us to handle this correctly.  */
13086               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13087                 {
13088                   /* In [class.mem]:
13089
13090                      A pure-specifier shall be used only in the declaration of
13091                      a virtual function.
13092
13093                      A member-declarator can contain a constant-initializer
13094                      only if it declares a static member of integral or
13095                      enumeration type.
13096
13097                      Therefore, if the DECLARATOR is for a function, we look
13098                      for a pure-specifier; otherwise, we look for a
13099                      constant-initializer.  When we call `grokfield', it will
13100                      perform more stringent semantics checks.  */
13101                   if (declarator->kind == cdk_function)
13102                     initializer = cp_parser_pure_specifier (parser);
13103                   else
13104                     /* Parse the initializer.  */
13105                     initializer = cp_parser_constant_initializer (parser);
13106                 }
13107               /* Otherwise, there is no initializer.  */
13108               else
13109                 initializer = NULL_TREE;
13110
13111               /* See if we are probably looking at a function
13112                  definition.  We are certainly not looking at at a
13113                  member-declarator.  Calling `grokfield' has
13114                  side-effects, so we must not do it unless we are sure
13115                  that we are looking at a member-declarator.  */
13116               if (cp_parser_token_starts_function_definition_p
13117                   (cp_lexer_peek_token (parser->lexer)))
13118                 {
13119                   /* The grammar does not allow a pure-specifier to be
13120                      used when a member function is defined.  (It is
13121                      possible that this fact is an oversight in the
13122                      standard, since a pure function may be defined
13123                      outside of the class-specifier.  */
13124                   if (initializer)
13125                     error ("pure-specifier on function-definition");
13126                   decl = cp_parser_save_member_function_body (parser,
13127                                                               &decl_specifiers,
13128                                                               declarator,
13129                                                               attributes);
13130                   /* If the member was not a friend, declare it here.  */
13131                   if (!friend_p)
13132                     finish_member_declaration (decl);
13133                   /* Peek at the next token.  */
13134                   token = cp_lexer_peek_token (parser->lexer);
13135                   /* If the next token is a semicolon, consume it.  */
13136                   if (token->type == CPP_SEMICOLON)
13137                     cp_lexer_consume_token (parser->lexer);
13138                   return;
13139                 }
13140               else
13141                 {
13142                   /* Create the declaration.  */
13143                   decl = grokfield (declarator, &decl_specifiers,
13144                                     initializer, asm_specification,
13145                                     attributes);
13146                   /* Any initialization must have been from a
13147                      constant-expression.  */
13148                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13149                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13150                 }
13151             }
13152
13153           /* Reset PREFIX_ATTRIBUTES.  */
13154           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13155             attributes = TREE_CHAIN (attributes);
13156           if (attributes)
13157             TREE_CHAIN (attributes) = NULL_TREE;
13158
13159           /* If there is any qualification still in effect, clear it
13160              now; we will be starting fresh with the next declarator.  */
13161           parser->scope = NULL_TREE;
13162           parser->qualifying_scope = NULL_TREE;
13163           parser->object_scope = NULL_TREE;
13164           /* If it's a `,', then there are more declarators.  */
13165           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13166             cp_lexer_consume_token (parser->lexer);
13167           /* If the next token isn't a `;', then we have a parse error.  */
13168           else if (cp_lexer_next_token_is_not (parser->lexer,
13169                                                CPP_SEMICOLON))
13170             {
13171               cp_parser_error (parser, "expected %<;%>");
13172               /* Skip tokens until we find a `;'.  */
13173               cp_parser_skip_to_end_of_statement (parser);
13174
13175               break;
13176             }
13177
13178           if (decl)
13179             {
13180               /* Add DECL to the list of members.  */
13181               if (!friend_p)
13182                 finish_member_declaration (decl);
13183
13184               if (TREE_CODE (decl) == FUNCTION_DECL)
13185                 cp_parser_save_default_args (parser, decl);
13186             }
13187         }
13188     }
13189
13190   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13191 }
13192
13193 /* Parse a pure-specifier.
13194
13195    pure-specifier:
13196      = 0
13197
13198    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13199    Otherwise, ERROR_MARK_NODE is returned.  */
13200
13201 static tree
13202 cp_parser_pure_specifier (cp_parser* parser)
13203 {
13204   cp_token *token;
13205
13206   /* Look for the `=' token.  */
13207   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13208     return error_mark_node;
13209   /* Look for the `0' token.  */
13210   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13211   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13212      to get information from the lexer about how the number was
13213      spelled in order to fix this problem.  */
13214   if (!token || !integer_zerop (token->value))
13215     return error_mark_node;
13216
13217   return integer_zero_node;
13218 }
13219
13220 /* Parse a constant-initializer.
13221
13222    constant-initializer:
13223      = constant-expression
13224
13225    Returns a representation of the constant-expression.  */
13226
13227 static tree
13228 cp_parser_constant_initializer (cp_parser* parser)
13229 {
13230   /* Look for the `=' token.  */
13231   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13232     return error_mark_node;
13233
13234   /* It is invalid to write:
13235
13236        struct S { static const int i = { 7 }; };
13237
13238      */
13239   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13240     {
13241       cp_parser_error (parser,
13242                        "a brace-enclosed initializer is not allowed here");
13243       /* Consume the opening brace.  */
13244       cp_lexer_consume_token (parser->lexer);
13245       /* Skip the initializer.  */
13246       cp_parser_skip_to_closing_brace (parser);
13247       /* Look for the trailing `}'.  */
13248       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13249
13250       return error_mark_node;
13251     }
13252
13253   return cp_parser_constant_expression (parser,
13254                                         /*allow_non_constant=*/false,
13255                                         NULL);
13256 }
13257
13258 /* Derived classes [gram.class.derived] */
13259
13260 /* Parse a base-clause.
13261
13262    base-clause:
13263      : base-specifier-list
13264
13265    base-specifier-list:
13266      base-specifier
13267      base-specifier-list , base-specifier
13268
13269    Returns a TREE_LIST representing the base-classes, in the order in
13270    which they were declared.  The representation of each node is as
13271    described by cp_parser_base_specifier.
13272
13273    In the case that no bases are specified, this function will return
13274    NULL_TREE, not ERROR_MARK_NODE.  */
13275
13276 static tree
13277 cp_parser_base_clause (cp_parser* parser)
13278 {
13279   tree bases = NULL_TREE;
13280
13281   /* Look for the `:' that begins the list.  */
13282   cp_parser_require (parser, CPP_COLON, "`:'");
13283
13284   /* Scan the base-specifier-list.  */
13285   while (true)
13286     {
13287       cp_token *token;
13288       tree base;
13289
13290       /* Look for the base-specifier.  */
13291       base = cp_parser_base_specifier (parser);
13292       /* Add BASE to the front of the list.  */
13293       if (base != error_mark_node)
13294         {
13295           TREE_CHAIN (base) = bases;
13296           bases = base;
13297         }
13298       /* Peek at the next token.  */
13299       token = cp_lexer_peek_token (parser->lexer);
13300       /* If it's not a comma, then the list is complete.  */
13301       if (token->type != CPP_COMMA)
13302         break;
13303       /* Consume the `,'.  */
13304       cp_lexer_consume_token (parser->lexer);
13305     }
13306
13307   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13308      base class had a qualified name.  However, the next name that
13309      appears is certainly not qualified.  */
13310   parser->scope = NULL_TREE;
13311   parser->qualifying_scope = NULL_TREE;
13312   parser->object_scope = NULL_TREE;
13313
13314   return nreverse (bases);
13315 }
13316
13317 /* Parse a base-specifier.
13318
13319    base-specifier:
13320      :: [opt] nested-name-specifier [opt] class-name
13321      virtual access-specifier [opt] :: [opt] nested-name-specifier
13322        [opt] class-name
13323      access-specifier virtual [opt] :: [opt] nested-name-specifier
13324        [opt] class-name
13325
13326    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13327    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13328    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13329    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13330
13331 static tree
13332 cp_parser_base_specifier (cp_parser* parser)
13333 {
13334   cp_token *token;
13335   bool done = false;
13336   bool virtual_p = false;
13337   bool duplicate_virtual_error_issued_p = false;
13338   bool duplicate_access_error_issued_p = false;
13339   bool class_scope_p, template_p;
13340   tree access = access_default_node;
13341   tree type;
13342
13343   /* Process the optional `virtual' and `access-specifier'.  */
13344   while (!done)
13345     {
13346       /* Peek at the next token.  */
13347       token = cp_lexer_peek_token (parser->lexer);
13348       /* Process `virtual'.  */
13349       switch (token->keyword)
13350         {
13351         case RID_VIRTUAL:
13352           /* If `virtual' appears more than once, issue an error.  */
13353           if (virtual_p && !duplicate_virtual_error_issued_p)
13354             {
13355               cp_parser_error (parser,
13356                                "%<virtual%> specified more than once in base-specified");
13357               duplicate_virtual_error_issued_p = true;
13358             }
13359
13360           virtual_p = true;
13361
13362           /* Consume the `virtual' token.  */
13363           cp_lexer_consume_token (parser->lexer);
13364
13365           break;
13366
13367         case RID_PUBLIC:
13368         case RID_PROTECTED:
13369         case RID_PRIVATE:
13370           /* If more than one access specifier appears, issue an
13371              error.  */
13372           if (access != access_default_node
13373               && !duplicate_access_error_issued_p)
13374             {
13375               cp_parser_error (parser,
13376                                "more than one access specifier in base-specified");
13377               duplicate_access_error_issued_p = true;
13378             }
13379
13380           access = ridpointers[(int) token->keyword];
13381
13382           /* Consume the access-specifier.  */
13383           cp_lexer_consume_token (parser->lexer);
13384
13385           break;
13386
13387         default:
13388           done = true;
13389           break;
13390         }
13391     }
13392   /* It is not uncommon to see programs mechanically, erroneously, use
13393      the 'typename' keyword to denote (dependent) qualified types
13394      as base classes.  */
13395   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13396     {
13397       if (!processing_template_decl)
13398         error ("keyword %<typename%> not allowed outside of templates");
13399       else
13400         error ("keyword %<typename%> not allowed in this context "
13401                "(the base class is implicitly a type)");
13402       cp_lexer_consume_token (parser->lexer);
13403     }
13404
13405   /* Look for the optional `::' operator.  */
13406   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13407   /* Look for the nested-name-specifier.  The simplest way to
13408      implement:
13409
13410        [temp.res]
13411
13412        The keyword `typename' is not permitted in a base-specifier or
13413        mem-initializer; in these contexts a qualified name that
13414        depends on a template-parameter is implicitly assumed to be a
13415        type name.
13416
13417      is to pretend that we have seen the `typename' keyword at this
13418      point.  */
13419   cp_parser_nested_name_specifier_opt (parser,
13420                                        /*typename_keyword_p=*/true,
13421                                        /*check_dependency_p=*/true,
13422                                        /*type_p=*/true,
13423                                        /*is_declaration=*/true);
13424   /* If the base class is given by a qualified name, assume that names
13425      we see are type names or templates, as appropriate.  */
13426   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13427   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13428
13429   /* Finally, look for the class-name.  */
13430   type = cp_parser_class_name (parser,
13431                                class_scope_p,
13432                                template_p,
13433                                /*type_p=*/true,
13434                                /*check_dependency_p=*/true,
13435                                /*class_head_p=*/false,
13436                                /*is_declaration=*/true);
13437
13438   if (type == error_mark_node)
13439     return error_mark_node;
13440
13441   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13442 }
13443
13444 /* Exception handling [gram.exception] */
13445
13446 /* Parse an (optional) exception-specification.
13447
13448    exception-specification:
13449      throw ( type-id-list [opt] )
13450
13451    Returns a TREE_LIST representing the exception-specification.  The
13452    TREE_VALUE of each node is a type.  */
13453
13454 static tree
13455 cp_parser_exception_specification_opt (cp_parser* parser)
13456 {
13457   cp_token *token;
13458   tree type_id_list;
13459
13460   /* Peek at the next token.  */
13461   token = cp_lexer_peek_token (parser->lexer);
13462   /* If it's not `throw', then there's no exception-specification.  */
13463   if (!cp_parser_is_keyword (token, RID_THROW))
13464     return NULL_TREE;
13465
13466   /* Consume the `throw'.  */
13467   cp_lexer_consume_token (parser->lexer);
13468
13469   /* Look for the `('.  */
13470   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13471
13472   /* Peek at the next token.  */
13473   token = cp_lexer_peek_token (parser->lexer);
13474   /* If it's not a `)', then there is a type-id-list.  */
13475   if (token->type != CPP_CLOSE_PAREN)
13476     {
13477       const char *saved_message;
13478
13479       /* Types may not be defined in an exception-specification.  */
13480       saved_message = parser->type_definition_forbidden_message;
13481       parser->type_definition_forbidden_message
13482         = "types may not be defined in an exception-specification";
13483       /* Parse the type-id-list.  */
13484       type_id_list = cp_parser_type_id_list (parser);
13485       /* Restore the saved message.  */
13486       parser->type_definition_forbidden_message = saved_message;
13487     }
13488   else
13489     type_id_list = empty_except_spec;
13490
13491   /* Look for the `)'.  */
13492   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13493
13494   return type_id_list;
13495 }
13496
13497 /* Parse an (optional) type-id-list.
13498
13499    type-id-list:
13500      type-id
13501      type-id-list , type-id
13502
13503    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13504    in the order that the types were presented.  */
13505
13506 static tree
13507 cp_parser_type_id_list (cp_parser* parser)
13508 {
13509   tree types = NULL_TREE;
13510
13511   while (true)
13512     {
13513       cp_token *token;
13514       tree type;
13515
13516       /* Get the next type-id.  */
13517       type = cp_parser_type_id (parser);
13518       /* Add it to the list.  */
13519       types = add_exception_specifier (types, type, /*complain=*/1);
13520       /* Peek at the next token.  */
13521       token = cp_lexer_peek_token (parser->lexer);
13522       /* If it is not a `,', we are done.  */
13523       if (token->type != CPP_COMMA)
13524         break;
13525       /* Consume the `,'.  */
13526       cp_lexer_consume_token (parser->lexer);
13527     }
13528
13529   return nreverse (types);
13530 }
13531
13532 /* Parse a try-block.
13533
13534    try-block:
13535      try compound-statement handler-seq  */
13536
13537 static tree
13538 cp_parser_try_block (cp_parser* parser)
13539 {
13540   tree try_block;
13541
13542   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13543   try_block = begin_try_block ();
13544   cp_parser_compound_statement (parser, NULL, true);
13545   finish_try_block (try_block);
13546   cp_parser_handler_seq (parser);
13547   finish_handler_sequence (try_block);
13548
13549   return try_block;
13550 }
13551
13552 /* Parse a function-try-block.
13553
13554    function-try-block:
13555      try ctor-initializer [opt] function-body handler-seq  */
13556
13557 static bool
13558 cp_parser_function_try_block (cp_parser* parser)
13559 {
13560   tree try_block;
13561   bool ctor_initializer_p;
13562
13563   /* Look for the `try' keyword.  */
13564   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13565     return false;
13566   /* Let the rest of the front-end know where we are.  */
13567   try_block = begin_function_try_block ();
13568   /* Parse the function-body.  */
13569   ctor_initializer_p
13570     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13571   /* We're done with the `try' part.  */
13572   finish_function_try_block (try_block);
13573   /* Parse the handlers.  */
13574   cp_parser_handler_seq (parser);
13575   /* We're done with the handlers.  */
13576   finish_function_handler_sequence (try_block);
13577
13578   return ctor_initializer_p;
13579 }
13580
13581 /* Parse a handler-seq.
13582
13583    handler-seq:
13584      handler handler-seq [opt]  */
13585
13586 static void
13587 cp_parser_handler_seq (cp_parser* parser)
13588 {
13589   while (true)
13590     {
13591       cp_token *token;
13592
13593       /* Parse the handler.  */
13594       cp_parser_handler (parser);
13595       /* Peek at the next token.  */
13596       token = cp_lexer_peek_token (parser->lexer);
13597       /* If it's not `catch' then there are no more handlers.  */
13598       if (!cp_parser_is_keyword (token, RID_CATCH))
13599         break;
13600     }
13601 }
13602
13603 /* Parse a handler.
13604
13605    handler:
13606      catch ( exception-declaration ) compound-statement  */
13607
13608 static void
13609 cp_parser_handler (cp_parser* parser)
13610 {
13611   tree handler;
13612   tree declaration;
13613
13614   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13615   handler = begin_handler ();
13616   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13617   declaration = cp_parser_exception_declaration (parser);
13618   finish_handler_parms (declaration, handler);
13619   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13620   cp_parser_compound_statement (parser, NULL, false);
13621   finish_handler (handler);
13622 }
13623
13624 /* Parse an exception-declaration.
13625
13626    exception-declaration:
13627      type-specifier-seq declarator
13628      type-specifier-seq abstract-declarator
13629      type-specifier-seq
13630      ...
13631
13632    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13633    ellipsis variant is used.  */
13634
13635 static tree
13636 cp_parser_exception_declaration (cp_parser* parser)
13637 {
13638   tree decl;
13639   cp_decl_specifier_seq type_specifiers;
13640   cp_declarator *declarator;
13641   const char *saved_message;
13642
13643   /* If it's an ellipsis, it's easy to handle.  */
13644   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13645     {
13646       /* Consume the `...' token.  */
13647       cp_lexer_consume_token (parser->lexer);
13648       return NULL_TREE;
13649     }
13650
13651   /* Types may not be defined in exception-declarations.  */
13652   saved_message = parser->type_definition_forbidden_message;
13653   parser->type_definition_forbidden_message
13654     = "types may not be defined in exception-declarations";
13655
13656   /* Parse the type-specifier-seq.  */
13657   cp_parser_type_specifier_seq (parser, &type_specifiers);
13658   /* If it's a `)', then there is no declarator.  */
13659   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13660     declarator = NULL;
13661   else
13662     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13663                                        /*ctor_dtor_or_conv_p=*/NULL,
13664                                        /*parenthesized_p=*/NULL,
13665                                        /*member_p=*/false);
13666
13667   /* Restore the saved message.  */
13668   parser->type_definition_forbidden_message = saved_message;
13669
13670   if (type_specifiers.any_specifiers_p)
13671     {
13672       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13673       if (decl == NULL_TREE)
13674         error ("invalid catch parameter");
13675     }
13676   else
13677     decl = NULL_TREE;
13678
13679   return decl;
13680 }
13681
13682 /* Parse a throw-expression.
13683
13684    throw-expression:
13685      throw assignment-expression [opt]
13686
13687    Returns a THROW_EXPR representing the throw-expression.  */
13688
13689 static tree
13690 cp_parser_throw_expression (cp_parser* parser)
13691 {
13692   tree expression;
13693   cp_token* token;
13694
13695   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13696   token = cp_lexer_peek_token (parser->lexer);
13697   /* Figure out whether or not there is an assignment-expression
13698      following the "throw" keyword.  */
13699   if (token->type == CPP_COMMA
13700       || token->type == CPP_SEMICOLON
13701       || token->type == CPP_CLOSE_PAREN
13702       || token->type == CPP_CLOSE_SQUARE
13703       || token->type == CPP_CLOSE_BRACE
13704       || token->type == CPP_COLON)
13705     expression = NULL_TREE;
13706   else
13707     expression = cp_parser_assignment_expression (parser);
13708
13709   return build_throw (expression);
13710 }
13711
13712 /* GNU Extensions */
13713
13714 /* Parse an (optional) asm-specification.
13715
13716    asm-specification:
13717      asm ( string-literal )
13718
13719    If the asm-specification is present, returns a STRING_CST
13720    corresponding to the string-literal.  Otherwise, returns
13721    NULL_TREE.  */
13722
13723 static tree
13724 cp_parser_asm_specification_opt (cp_parser* parser)
13725 {
13726   cp_token *token;
13727   tree asm_specification;
13728
13729   /* Peek at the next token.  */
13730   token = cp_lexer_peek_token (parser->lexer);
13731   /* If the next token isn't the `asm' keyword, then there's no
13732      asm-specification.  */
13733   if (!cp_parser_is_keyword (token, RID_ASM))
13734     return NULL_TREE;
13735
13736   /* Consume the `asm' token.  */
13737   cp_lexer_consume_token (parser->lexer);
13738   /* Look for the `('.  */
13739   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13740
13741   /* Look for the string-literal.  */
13742   asm_specification = cp_parser_string_literal (parser, false, false);
13743
13744   /* Look for the `)'.  */
13745   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13746
13747   return asm_specification;
13748 }
13749
13750 /* Parse an asm-operand-list.
13751
13752    asm-operand-list:
13753      asm-operand
13754      asm-operand-list , asm-operand
13755
13756    asm-operand:
13757      string-literal ( expression )
13758      [ string-literal ] string-literal ( expression )
13759
13760    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13761    each node is the expression.  The TREE_PURPOSE is itself a
13762    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13763    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13764    is a STRING_CST for the string literal before the parenthesis.  */
13765
13766 static tree
13767 cp_parser_asm_operand_list (cp_parser* parser)
13768 {
13769   tree asm_operands = NULL_TREE;
13770
13771   while (true)
13772     {
13773       tree string_literal;
13774       tree expression;
13775       tree name;
13776
13777       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13778         {
13779           /* Consume the `[' token.  */
13780           cp_lexer_consume_token (parser->lexer);
13781           /* Read the operand name.  */
13782           name = cp_parser_identifier (parser);
13783           if (name != error_mark_node)
13784             name = build_string (IDENTIFIER_LENGTH (name),
13785                                  IDENTIFIER_POINTER (name));
13786           /* Look for the closing `]'.  */
13787           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13788         }
13789       else
13790         name = NULL_TREE;
13791       /* Look for the string-literal.  */
13792       string_literal = cp_parser_string_literal (parser, false, false);
13793
13794       /* Look for the `('.  */
13795       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13796       /* Parse the expression.  */
13797       expression = cp_parser_expression (parser);
13798       /* Look for the `)'.  */
13799       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13800
13801       /* Add this operand to the list.  */
13802       asm_operands = tree_cons (build_tree_list (name, string_literal),
13803                                 expression,
13804                                 asm_operands);
13805       /* If the next token is not a `,', there are no more
13806          operands.  */
13807       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13808         break;
13809       /* Consume the `,'.  */
13810       cp_lexer_consume_token (parser->lexer);
13811     }
13812
13813   return nreverse (asm_operands);
13814 }
13815
13816 /* Parse an asm-clobber-list.
13817
13818    asm-clobber-list:
13819      string-literal
13820      asm-clobber-list , string-literal
13821
13822    Returns a TREE_LIST, indicating the clobbers in the order that they
13823    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13824
13825 static tree
13826 cp_parser_asm_clobber_list (cp_parser* parser)
13827 {
13828   tree clobbers = NULL_TREE;
13829
13830   while (true)
13831     {
13832       tree string_literal;
13833
13834       /* Look for the string literal.  */
13835       string_literal = cp_parser_string_literal (parser, false, false);
13836       /* Add it to the list.  */
13837       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13838       /* If the next token is not a `,', then the list is
13839          complete.  */
13840       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13841         break;
13842       /* Consume the `,' token.  */
13843       cp_lexer_consume_token (parser->lexer);
13844     }
13845
13846   return clobbers;
13847 }
13848
13849 /* Parse an (optional) series of attributes.
13850
13851    attributes:
13852      attributes attribute
13853
13854    attribute:
13855      __attribute__ (( attribute-list [opt] ))
13856
13857    The return value is as for cp_parser_attribute_list.  */
13858
13859 static tree
13860 cp_parser_attributes_opt (cp_parser* parser)
13861 {
13862   tree attributes = NULL_TREE;
13863
13864   while (true)
13865     {
13866       cp_token *token;
13867       tree attribute_list;
13868
13869       /* Peek at the next token.  */
13870       token = cp_lexer_peek_token (parser->lexer);
13871       /* If it's not `__attribute__', then we're done.  */
13872       if (token->keyword != RID_ATTRIBUTE)
13873         break;
13874
13875       /* Consume the `__attribute__' keyword.  */
13876       cp_lexer_consume_token (parser->lexer);
13877       /* Look for the two `(' tokens.  */
13878       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13879       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13880
13881       /* Peek at the next token.  */
13882       token = cp_lexer_peek_token (parser->lexer);
13883       if (token->type != CPP_CLOSE_PAREN)
13884         /* Parse the attribute-list.  */
13885         attribute_list = cp_parser_attribute_list (parser);
13886       else
13887         /* If the next token is a `)', then there is no attribute
13888            list.  */
13889         attribute_list = NULL;
13890
13891       /* Look for the two `)' tokens.  */
13892       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13893       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13894
13895       /* Add these new attributes to the list.  */
13896       attributes = chainon (attributes, attribute_list);
13897     }
13898
13899   return attributes;
13900 }
13901
13902 /* Parse an attribute-list.
13903
13904    attribute-list:
13905      attribute
13906      attribute-list , attribute
13907
13908    attribute:
13909      identifier
13910      identifier ( identifier )
13911      identifier ( identifier , expression-list )
13912      identifier ( expression-list )
13913
13914    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13915    TREE_PURPOSE of each node is the identifier indicating which
13916    attribute is in use.  The TREE_VALUE represents the arguments, if
13917    any.  */
13918
13919 static tree
13920 cp_parser_attribute_list (cp_parser* parser)
13921 {
13922   tree attribute_list = NULL_TREE;
13923   bool save_translate_strings_p = parser->translate_strings_p;
13924
13925   parser->translate_strings_p = false;
13926   while (true)
13927     {
13928       cp_token *token;
13929       tree identifier;
13930       tree attribute;
13931
13932       /* Look for the identifier.  We also allow keywords here; for
13933          example `__attribute__ ((const))' is legal.  */
13934       token = cp_lexer_peek_token (parser->lexer);
13935       if (token->type != CPP_NAME
13936           && token->type != CPP_KEYWORD)
13937         return error_mark_node;
13938       /* Consume the token.  */
13939       token = cp_lexer_consume_token (parser->lexer);
13940
13941       /* Save away the identifier that indicates which attribute this is.  */
13942       identifier = token->value;
13943       attribute = build_tree_list (identifier, NULL_TREE);
13944
13945       /* Peek at the next token.  */
13946       token = cp_lexer_peek_token (parser->lexer);
13947       /* If it's an `(', then parse the attribute arguments.  */
13948       if (token->type == CPP_OPEN_PAREN)
13949         {
13950           tree arguments;
13951
13952           arguments = (cp_parser_parenthesized_expression_list
13953                        (parser, true, /*non_constant_p=*/NULL));
13954           /* Save the identifier and arguments away.  */
13955           TREE_VALUE (attribute) = arguments;
13956         }
13957
13958       /* Add this attribute to the list.  */
13959       TREE_CHAIN (attribute) = attribute_list;
13960       attribute_list = attribute;
13961
13962       /* Now, look for more attributes.  */
13963       token = cp_lexer_peek_token (parser->lexer);
13964       /* If the next token isn't a `,', we're done.  */
13965       if (token->type != CPP_COMMA)
13966         break;
13967
13968       /* Consume the comma and keep going.  */
13969       cp_lexer_consume_token (parser->lexer);
13970     }
13971   parser->translate_strings_p = save_translate_strings_p;
13972
13973   /* We built up the list in reverse order.  */
13974   return nreverse (attribute_list);
13975 }
13976
13977 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13978    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13979    current value of the PEDANTIC flag, regardless of whether or not
13980    the `__extension__' keyword is present.  The caller is responsible
13981    for restoring the value of the PEDANTIC flag.  */
13982
13983 static bool
13984 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13985 {
13986   /* Save the old value of the PEDANTIC flag.  */
13987   *saved_pedantic = pedantic;
13988
13989   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13990     {
13991       /* Consume the `__extension__' token.  */
13992       cp_lexer_consume_token (parser->lexer);
13993       /* We're not being pedantic while the `__extension__' keyword is
13994          in effect.  */
13995       pedantic = 0;
13996
13997       return true;
13998     }
13999
14000   return false;
14001 }
14002
14003 /* Parse a label declaration.
14004
14005    label-declaration:
14006      __label__ label-declarator-seq ;
14007
14008    label-declarator-seq:
14009      identifier , label-declarator-seq
14010      identifier  */
14011
14012 static void
14013 cp_parser_label_declaration (cp_parser* parser)
14014 {
14015   /* Look for the `__label__' keyword.  */
14016   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14017
14018   while (true)
14019     {
14020       tree identifier;
14021
14022       /* Look for an identifier.  */
14023       identifier = cp_parser_identifier (parser);
14024       /* Declare it as a lobel.  */
14025       finish_label_decl (identifier);
14026       /* If the next token is a `;', stop.  */
14027       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14028         break;
14029       /* Look for the `,' separating the label declarations.  */
14030       cp_parser_require (parser, CPP_COMMA, "`,'");
14031     }
14032
14033   /* Look for the final `;'.  */
14034   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14035 }
14036
14037 /* Support Functions */
14038
14039 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14040    NAME should have one of the representations used for an
14041    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14042    is returned.  If PARSER->SCOPE is a dependent type, then a
14043    SCOPE_REF is returned.
14044
14045    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14046    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14047    was formed.  Abstractly, such entities should not be passed to this
14048    function, because they do not need to be looked up, but it is
14049    simpler to check for this special case here, rather than at the
14050    call-sites.
14051
14052    In cases not explicitly covered above, this function returns a
14053    DECL, OVERLOAD, or baselink representing the result of the lookup.
14054    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14055    is returned.
14056
14057    If IS_TYPE is TRUE, bindings that do not refer to types are
14058    ignored.
14059
14060    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14061    ignored.
14062
14063    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14064    are ignored.
14065
14066    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14067    types.  
14068
14069    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14070    results in an ambiguity, and false otherwise.  */
14071
14072 static tree
14073 cp_parser_lookup_name (cp_parser *parser, tree name,
14074                        bool is_type, bool is_template, bool is_namespace,
14075                        bool check_dependency,
14076                        bool *ambiguous_p)
14077 {
14078   tree decl;
14079   tree object_type = parser->context->object_type;
14080
14081   /* Assume that the lookup will be unambiguous.  */
14082   if (ambiguous_p)
14083     *ambiguous_p = false;
14084
14085   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14086      no longer valid.  Note that if we are parsing tentatively, and
14087      the parse fails, OBJECT_TYPE will be automatically restored.  */
14088   parser->context->object_type = NULL_TREE;
14089
14090   if (name == error_mark_node)
14091     return error_mark_node;
14092
14093   /* A template-id has already been resolved; there is no lookup to
14094      do.  */
14095   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14096     return name;
14097   if (BASELINK_P (name))
14098     {
14099       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14100                   == TEMPLATE_ID_EXPR);
14101       return name;
14102     }
14103
14104   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14105      it should already have been checked to make sure that the name
14106      used matches the type being destroyed.  */
14107   if (TREE_CODE (name) == BIT_NOT_EXPR)
14108     {
14109       tree type;
14110
14111       /* Figure out to which type this destructor applies.  */
14112       if (parser->scope)
14113         type = parser->scope;
14114       else if (object_type)
14115         type = object_type;
14116       else
14117         type = current_class_type;
14118       /* If that's not a class type, there is no destructor.  */
14119       if (!type || !CLASS_TYPE_P (type))
14120         return error_mark_node;
14121       if (!CLASSTYPE_DESTRUCTORS (type))
14122           return error_mark_node;
14123       /* If it was a class type, return the destructor.  */
14124       return CLASSTYPE_DESTRUCTORS (type);
14125     }
14126
14127   /* By this point, the NAME should be an ordinary identifier.  If
14128      the id-expression was a qualified name, the qualifying scope is
14129      stored in PARSER->SCOPE at this point.  */
14130   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14131
14132   /* Perform the lookup.  */
14133   if (parser->scope)
14134     {
14135       bool dependent_p;
14136
14137       if (parser->scope == error_mark_node)
14138         return error_mark_node;
14139
14140       /* If the SCOPE is dependent, the lookup must be deferred until
14141          the template is instantiated -- unless we are explicitly
14142          looking up names in uninstantiated templates.  Even then, we
14143          cannot look up the name if the scope is not a class type; it
14144          might, for example, be a template type parameter.  */
14145       dependent_p = (TYPE_P (parser->scope)
14146                      && !(parser->in_declarator_p
14147                           && currently_open_class (parser->scope))
14148                      && dependent_type_p (parser->scope));
14149       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14150            && dependent_p)
14151         {
14152           if (is_type)
14153             /* The resolution to Core Issue 180 says that `struct A::B'
14154                should be considered a type-name, even if `A' is
14155                dependent.  */
14156             decl = TYPE_NAME (make_typename_type (parser->scope,
14157                                                   name,
14158                                                   /*complain=*/1));
14159           else if (is_template)
14160             decl = make_unbound_class_template (parser->scope,
14161                                                 name,
14162                                                 /*complain=*/1);
14163           else
14164             decl = build_nt (SCOPE_REF, parser->scope, name);
14165         }
14166       else
14167         {
14168           bool pop_p = false;
14169
14170           /* If PARSER->SCOPE is a dependent type, then it must be a
14171              class type, and we must not be checking dependencies;
14172              otherwise, we would have processed this lookup above.  So
14173              that PARSER->SCOPE is not considered a dependent base by
14174              lookup_member, we must enter the scope here.  */
14175           if (dependent_p)
14176             pop_p = push_scope (parser->scope);
14177           /* If the PARSER->SCOPE is a a template specialization, it
14178              may be instantiated during name lookup.  In that case,
14179              errors may be issued.  Even if we rollback the current
14180              tentative parse, those errors are valid.  */
14181           decl = lookup_qualified_name (parser->scope, name, is_type,
14182                                         /*complain=*/true);
14183           if (pop_p)
14184             pop_scope (parser->scope);
14185         }
14186       parser->qualifying_scope = parser->scope;
14187       parser->object_scope = NULL_TREE;
14188     }
14189   else if (object_type)
14190     {
14191       tree object_decl = NULL_TREE;
14192       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14193          OBJECT_TYPE is not a class.  */
14194       if (CLASS_TYPE_P (object_type))
14195         /* If the OBJECT_TYPE is a template specialization, it may
14196            be instantiated during name lookup.  In that case, errors
14197            may be issued.  Even if we rollback the current tentative
14198            parse, those errors are valid.  */
14199         object_decl = lookup_member (object_type,
14200                                      name,
14201                                      /*protect=*/0, is_type);
14202       /* Look it up in the enclosing context, too.  */
14203       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14204                                /*block_p=*/true, is_namespace,
14205                                /*flags=*/0);
14206       parser->object_scope = object_type;
14207       parser->qualifying_scope = NULL_TREE;
14208       if (object_decl)
14209         decl = object_decl;
14210     }
14211   else
14212     {
14213       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14214                                /*block_p=*/true, is_namespace,
14215                                /*flags=*/0);
14216       parser->qualifying_scope = NULL_TREE;
14217       parser->object_scope = NULL_TREE;
14218     }
14219
14220   /* If the lookup failed, let our caller know.  */
14221   if (!decl
14222       || decl == error_mark_node
14223       || (TREE_CODE (decl) == FUNCTION_DECL
14224           && DECL_ANTICIPATED (decl)))
14225     return error_mark_node;
14226
14227   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14228   if (TREE_CODE (decl) == TREE_LIST)
14229     {
14230       if (ambiguous_p)
14231         *ambiguous_p = true;
14232       /* The error message we have to print is too complicated for
14233          cp_parser_error, so we incorporate its actions directly.  */
14234       if (!cp_parser_simulate_error (parser))
14235         {
14236           error ("reference to %qD is ambiguous", name);
14237           print_candidates (decl);
14238         }
14239       return error_mark_node;
14240     }
14241
14242   gcc_assert (DECL_P (decl)
14243               || TREE_CODE (decl) == OVERLOAD
14244               || TREE_CODE (decl) == SCOPE_REF
14245               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14246               || BASELINK_P (decl));
14247
14248   /* If we have resolved the name of a member declaration, check to
14249      see if the declaration is accessible.  When the name resolves to
14250      set of overloaded functions, accessibility is checked when
14251      overload resolution is done.
14252
14253      During an explicit instantiation, access is not checked at all,
14254      as per [temp.explicit].  */
14255   if (DECL_P (decl))
14256     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14257
14258   return decl;
14259 }
14260
14261 /* Like cp_parser_lookup_name, but for use in the typical case where
14262    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14263    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14264
14265 static tree
14266 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14267 {
14268   return cp_parser_lookup_name (parser, name,
14269                                 /*is_type=*/false,
14270                                 /*is_template=*/false,
14271                                 /*is_namespace=*/false,
14272                                 /*check_dependency=*/true,
14273                                 /*ambiguous_p=*/NULL);
14274 }
14275
14276 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14277    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14278    true, the DECL indicates the class being defined in a class-head,
14279    or declared in an elaborated-type-specifier.
14280
14281    Otherwise, return DECL.  */
14282
14283 static tree
14284 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14285 {
14286   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14287      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14288
14289        struct A {
14290          template <typename T> struct B;
14291        };
14292
14293        template <typename T> struct A::B {};
14294
14295      Similarly, in a elaborated-type-specifier:
14296
14297        namespace N { struct X{}; }
14298
14299        struct A {
14300          template <typename T> friend struct N::X;
14301        };
14302
14303      However, if the DECL refers to a class type, and we are in
14304      the scope of the class, then the name lookup automatically
14305      finds the TYPE_DECL created by build_self_reference rather
14306      than a TEMPLATE_DECL.  For example, in:
14307
14308        template <class T> struct S {
14309          S s;
14310        };
14311
14312      there is no need to handle such case.  */
14313
14314   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14315     return DECL_TEMPLATE_RESULT (decl);
14316
14317   return decl;
14318 }
14319
14320 /* If too many, or too few, template-parameter lists apply to the
14321    declarator, issue an error message.  Returns TRUE if all went well,
14322    and FALSE otherwise.  */
14323
14324 static bool
14325 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14326                                                 cp_declarator *declarator)
14327 {
14328   unsigned num_templates;
14329
14330   /* We haven't seen any classes that involve template parameters yet.  */
14331   num_templates = 0;
14332
14333   switch (declarator->kind)
14334     {
14335     case cdk_id:
14336       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14337         {
14338           tree scope;
14339           tree member;
14340
14341           scope = TREE_OPERAND (declarator->u.id.name, 0);
14342           member = TREE_OPERAND (declarator->u.id.name, 1);
14343
14344           while (scope && CLASS_TYPE_P (scope))
14345             {
14346               /* You're supposed to have one `template <...>'
14347                  for every template class, but you don't need one
14348                  for a full specialization.  For example:
14349
14350                  template <class T> struct S{};
14351                  template <> struct S<int> { void f(); };
14352                  void S<int>::f () {}
14353
14354                  is correct; there shouldn't be a `template <>' for
14355                  the definition of `S<int>::f'.  */
14356               if (CLASSTYPE_TEMPLATE_INFO (scope)
14357                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14358                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14359                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14360                 ++num_templates;
14361
14362               scope = TYPE_CONTEXT (scope);
14363             }
14364         }
14365
14366       /* If the DECLARATOR has the form `X<y>' then it uses one
14367          additional level of template parameters.  */
14368       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14369         ++num_templates;
14370
14371       return cp_parser_check_template_parameters (parser,
14372                                                   num_templates);
14373
14374     case cdk_function:
14375     case cdk_array:
14376     case cdk_pointer:
14377     case cdk_reference:
14378     case cdk_ptrmem:
14379       return (cp_parser_check_declarator_template_parameters
14380               (parser, declarator->declarator));
14381
14382     case cdk_error:
14383       return true;
14384
14385     default:
14386       gcc_unreachable ();
14387     }
14388   return false;
14389 }
14390
14391 /* NUM_TEMPLATES were used in the current declaration.  If that is
14392    invalid, return FALSE and issue an error messages.  Otherwise,
14393    return TRUE.  */
14394
14395 static bool
14396 cp_parser_check_template_parameters (cp_parser* parser,
14397                                      unsigned num_templates)
14398 {
14399   /* If there are more template classes than parameter lists, we have
14400      something like:
14401
14402        template <class T> void S<T>::R<T>::f ();  */
14403   if (parser->num_template_parameter_lists < num_templates)
14404     {
14405       error ("too few template-parameter-lists");
14406       return false;
14407     }
14408   /* If there are the same number of template classes and parameter
14409      lists, that's OK.  */
14410   if (parser->num_template_parameter_lists == num_templates)
14411     return true;
14412   /* If there are more, but only one more, then we are referring to a
14413      member template.  That's OK too.  */
14414   if (parser->num_template_parameter_lists == num_templates + 1)
14415       return true;
14416   /* Otherwise, there are too many template parameter lists.  We have
14417      something like:
14418
14419      template <class T> template <class U> void S::f();  */
14420   error ("too many template-parameter-lists");
14421   return false;
14422 }
14423
14424 /* Parse an optional `::' token indicating that the following name is
14425    from the global namespace.  If so, PARSER->SCOPE is set to the
14426    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14427    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14428    Returns the new value of PARSER->SCOPE, if the `::' token is
14429    present, and NULL_TREE otherwise.  */
14430
14431 static tree
14432 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14433 {
14434   cp_token *token;
14435
14436   /* Peek at the next token.  */
14437   token = cp_lexer_peek_token (parser->lexer);
14438   /* If we're looking at a `::' token then we're starting from the
14439      global namespace, not our current location.  */
14440   if (token->type == CPP_SCOPE)
14441     {
14442       /* Consume the `::' token.  */
14443       cp_lexer_consume_token (parser->lexer);
14444       /* Set the SCOPE so that we know where to start the lookup.  */
14445       parser->scope = global_namespace;
14446       parser->qualifying_scope = global_namespace;
14447       parser->object_scope = NULL_TREE;
14448
14449       return parser->scope;
14450     }
14451   else if (!current_scope_valid_p)
14452     {
14453       parser->scope = NULL_TREE;
14454       parser->qualifying_scope = NULL_TREE;
14455       parser->object_scope = NULL_TREE;
14456     }
14457
14458   return NULL_TREE;
14459 }
14460
14461 /* Returns TRUE if the upcoming token sequence is the start of a
14462    constructor declarator.  If FRIEND_P is true, the declarator is
14463    preceded by the `friend' specifier.  */
14464
14465 static bool
14466 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14467 {
14468   bool constructor_p;
14469   tree type_decl = NULL_TREE;
14470   bool nested_name_p;
14471   cp_token *next_token;
14472
14473   /* The common case is that this is not a constructor declarator, so
14474      try to avoid doing lots of work if at all possible.  It's not
14475      valid declare a constructor at function scope.  */
14476   if (at_function_scope_p ())
14477     return false;
14478   /* And only certain tokens can begin a constructor declarator.  */
14479   next_token = cp_lexer_peek_token (parser->lexer);
14480   if (next_token->type != CPP_NAME
14481       && next_token->type != CPP_SCOPE
14482       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14483       && next_token->type != CPP_TEMPLATE_ID)
14484     return false;
14485
14486   /* Parse tentatively; we are going to roll back all of the tokens
14487      consumed here.  */
14488   cp_parser_parse_tentatively (parser);
14489   /* Assume that we are looking at a constructor declarator.  */
14490   constructor_p = true;
14491
14492   /* Look for the optional `::' operator.  */
14493   cp_parser_global_scope_opt (parser,
14494                               /*current_scope_valid_p=*/false);
14495   /* Look for the nested-name-specifier.  */
14496   nested_name_p
14497     = (cp_parser_nested_name_specifier_opt (parser,
14498                                             /*typename_keyword_p=*/false,
14499                                             /*check_dependency_p=*/false,
14500                                             /*type_p=*/false,
14501                                             /*is_declaration=*/false)
14502        != NULL_TREE);
14503   /* Outside of a class-specifier, there must be a
14504      nested-name-specifier.  */
14505   if (!nested_name_p &&
14506       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14507        || friend_p))
14508     constructor_p = false;
14509   /* If we still think that this might be a constructor-declarator,
14510      look for a class-name.  */
14511   if (constructor_p)
14512     {
14513       /* If we have:
14514
14515            template <typename T> struct S { S(); };
14516            template <typename T> S<T>::S ();
14517
14518          we must recognize that the nested `S' names a class.
14519          Similarly, for:
14520
14521            template <typename T> S<T>::S<T> ();
14522
14523          we must recognize that the nested `S' names a template.  */
14524       type_decl = cp_parser_class_name (parser,
14525                                         /*typename_keyword_p=*/false,
14526                                         /*template_keyword_p=*/false,
14527                                         /*type_p=*/false,
14528                                         /*check_dependency_p=*/false,
14529                                         /*class_head_p=*/false,
14530                                         /*is_declaration=*/false);
14531       /* If there was no class-name, then this is not a constructor.  */
14532       constructor_p = !cp_parser_error_occurred (parser);
14533     }
14534
14535   /* If we're still considering a constructor, we have to see a `(',
14536      to begin the parameter-declaration-clause, followed by either a
14537      `)', an `...', or a decl-specifier.  We need to check for a
14538      type-specifier to avoid being fooled into thinking that:
14539
14540        S::S (f) (int);
14541
14542      is a constructor.  (It is actually a function named `f' that
14543      takes one parameter (of type `int') and returns a value of type
14544      `S::S'.  */
14545   if (constructor_p
14546       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14547     {
14548       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14549           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14550           /* A parameter declaration begins with a decl-specifier,
14551              which is either the "attribute" keyword, a storage class
14552              specifier, or (usually) a type-specifier.  */
14553           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14554           && !cp_parser_storage_class_specifier_opt (parser))
14555         {
14556           tree type;
14557           bool pop_p = false;
14558           unsigned saved_num_template_parameter_lists;
14559
14560           /* Names appearing in the type-specifier should be looked up
14561              in the scope of the class.  */
14562           if (current_class_type)
14563             type = NULL_TREE;
14564           else
14565             {
14566               type = TREE_TYPE (type_decl);
14567               if (TREE_CODE (type) == TYPENAME_TYPE)
14568                 {
14569                   type = resolve_typename_type (type,
14570                                                 /*only_current_p=*/false);
14571                   if (type == error_mark_node)
14572                     {
14573                       cp_parser_abort_tentative_parse (parser);
14574                       return false;
14575                     }
14576                 }
14577               pop_p = push_scope (type);
14578             }
14579
14580           /* Inside the constructor parameter list, surrounding
14581              template-parameter-lists do not apply.  */
14582           saved_num_template_parameter_lists
14583             = parser->num_template_parameter_lists;
14584           parser->num_template_parameter_lists = 0;
14585
14586           /* Look for the type-specifier.  */
14587           cp_parser_type_specifier (parser,
14588                                     CP_PARSER_FLAGS_NONE,
14589                                     /*decl_specs=*/NULL,
14590                                     /*is_declarator=*/true,
14591                                     /*declares_class_or_enum=*/NULL,
14592                                     /*is_cv_qualifier=*/NULL);
14593
14594           parser->num_template_parameter_lists
14595             = saved_num_template_parameter_lists;
14596
14597           /* Leave the scope of the class.  */
14598           if (pop_p)
14599             pop_scope (type);
14600
14601           constructor_p = !cp_parser_error_occurred (parser);
14602         }
14603     }
14604   else
14605     constructor_p = false;
14606   /* We did not really want to consume any tokens.  */
14607   cp_parser_abort_tentative_parse (parser);
14608
14609   return constructor_p;
14610 }
14611
14612 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14613    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14614    they must be performed once we are in the scope of the function.
14615
14616    Returns the function defined.  */
14617
14618 static tree
14619 cp_parser_function_definition_from_specifiers_and_declarator
14620   (cp_parser* parser,
14621    cp_decl_specifier_seq *decl_specifiers,
14622    tree attributes,
14623    const cp_declarator *declarator)
14624 {
14625   tree fn;
14626   bool success_p;
14627
14628   /* Begin the function-definition.  */
14629   success_p = start_function (decl_specifiers, declarator, attributes);
14630
14631   /* The things we're about to see are not directly qualified by any
14632      template headers we've seen thus far.  */
14633   reset_specialization ();
14634
14635   /* If there were names looked up in the decl-specifier-seq that we
14636      did not check, check them now.  We must wait until we are in the
14637      scope of the function to perform the checks, since the function
14638      might be a friend.  */
14639   perform_deferred_access_checks ();
14640
14641   if (!success_p)
14642     {
14643       /* Skip the entire function.  */
14644       error ("invalid function declaration");
14645       cp_parser_skip_to_end_of_block_or_statement (parser);
14646       fn = error_mark_node;
14647     }
14648   else
14649     fn = cp_parser_function_definition_after_declarator (parser,
14650                                                          /*inline_p=*/false);
14651
14652   return fn;
14653 }
14654
14655 /* Parse the part of a function-definition that follows the
14656    declarator.  INLINE_P is TRUE iff this function is an inline
14657    function defined with a class-specifier.
14658
14659    Returns the function defined.  */
14660
14661 static tree
14662 cp_parser_function_definition_after_declarator (cp_parser* parser,
14663                                                 bool inline_p)
14664 {
14665   tree fn;
14666   bool ctor_initializer_p = false;
14667   bool saved_in_unbraced_linkage_specification_p;
14668   unsigned saved_num_template_parameter_lists;
14669
14670   /* If the next token is `return', then the code may be trying to
14671      make use of the "named return value" extension that G++ used to
14672      support.  */
14673   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14674     {
14675       /* Consume the `return' keyword.  */
14676       cp_lexer_consume_token (parser->lexer);
14677       /* Look for the identifier that indicates what value is to be
14678          returned.  */
14679       cp_parser_identifier (parser);
14680       /* Issue an error message.  */
14681       error ("named return values are no longer supported");
14682       /* Skip tokens until we reach the start of the function body.  */
14683       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14684              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14685         cp_lexer_consume_token (parser->lexer);
14686     }
14687   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14688      anything declared inside `f'.  */
14689   saved_in_unbraced_linkage_specification_p
14690     = parser->in_unbraced_linkage_specification_p;
14691   parser->in_unbraced_linkage_specification_p = false;
14692   /* Inside the function, surrounding template-parameter-lists do not
14693      apply.  */
14694   saved_num_template_parameter_lists
14695     = parser->num_template_parameter_lists;
14696   parser->num_template_parameter_lists = 0;
14697   /* If the next token is `try', then we are looking at a
14698      function-try-block.  */
14699   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14700     ctor_initializer_p = cp_parser_function_try_block (parser);
14701   /* A function-try-block includes the function-body, so we only do
14702      this next part if we're not processing a function-try-block.  */
14703   else
14704     ctor_initializer_p
14705       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14706
14707   /* Finish the function.  */
14708   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14709                         (inline_p ? 2 : 0));
14710   /* Generate code for it, if necessary.  */
14711   expand_or_defer_fn (fn);
14712   /* Restore the saved values.  */
14713   parser->in_unbraced_linkage_specification_p
14714     = saved_in_unbraced_linkage_specification_p;
14715   parser->num_template_parameter_lists
14716     = saved_num_template_parameter_lists;
14717
14718   return fn;
14719 }
14720
14721 /* Parse a template-declaration, assuming that the `export' (and
14722    `extern') keywords, if present, has already been scanned.  MEMBER_P
14723    is as for cp_parser_template_declaration.  */
14724
14725 static void
14726 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14727 {
14728   tree decl = NULL_TREE;
14729   tree parameter_list;
14730   bool friend_p = false;
14731
14732   /* Look for the `template' keyword.  */
14733   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14734     return;
14735
14736   /* And the `<'.  */
14737   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14738     return;
14739
14740   /* If the next token is `>', then we have an invalid
14741      specialization.  Rather than complain about an invalid template
14742      parameter, issue an error message here.  */
14743   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14744     {
14745       cp_parser_error (parser, "invalid explicit specialization");
14746       begin_specialization ();
14747       parameter_list = NULL_TREE;
14748     }
14749   else
14750     {
14751       /* Parse the template parameters.  */
14752       begin_template_parm_list ();
14753       parameter_list = cp_parser_template_parameter_list (parser);
14754       parameter_list = end_template_parm_list (parameter_list);
14755     }
14756
14757   /* Look for the `>'.  */
14758   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14759   /* We just processed one more parameter list.  */
14760   ++parser->num_template_parameter_lists;
14761   /* If the next token is `template', there are more template
14762      parameters.  */
14763   if (cp_lexer_next_token_is_keyword (parser->lexer,
14764                                       RID_TEMPLATE))
14765     cp_parser_template_declaration_after_export (parser, member_p);
14766   else
14767     {
14768       /* There are no access checks when parsing a template, as we do not
14769          know if a specialization will be a friend.  */
14770       push_deferring_access_checks (dk_no_check);
14771
14772       decl = cp_parser_single_declaration (parser,
14773                                            member_p,
14774                                            &friend_p);
14775
14776       pop_deferring_access_checks ();
14777
14778       /* If this is a member template declaration, let the front
14779          end know.  */
14780       if (member_p && !friend_p && decl)
14781         {
14782           if (TREE_CODE (decl) == TYPE_DECL)
14783             cp_parser_check_access_in_redeclaration (decl);
14784
14785           decl = finish_member_template_decl (decl);
14786         }
14787       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14788         make_friend_class (current_class_type, TREE_TYPE (decl),
14789                            /*complain=*/true);
14790     }
14791   /* We are done with the current parameter list.  */
14792   --parser->num_template_parameter_lists;
14793
14794   /* Finish up.  */
14795   finish_template_decl (parameter_list);
14796
14797   /* Register member declarations.  */
14798   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14799     finish_member_declaration (decl);
14800
14801   /* If DECL is a function template, we must return to parse it later.
14802      (Even though there is no definition, there might be default
14803      arguments that need handling.)  */
14804   if (member_p && decl
14805       && (TREE_CODE (decl) == FUNCTION_DECL
14806           || DECL_FUNCTION_TEMPLATE_P (decl)))
14807     TREE_VALUE (parser->unparsed_functions_queues)
14808       = tree_cons (NULL_TREE, decl,
14809                    TREE_VALUE (parser->unparsed_functions_queues));
14810 }
14811
14812 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14813    `function-definition' sequence.  MEMBER_P is true, this declaration
14814    appears in a class scope.
14815
14816    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14817    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14818
14819 static tree
14820 cp_parser_single_declaration (cp_parser* parser,
14821                               bool member_p,
14822                               bool* friend_p)
14823 {
14824   int declares_class_or_enum;
14825   tree decl = NULL_TREE;
14826   cp_decl_specifier_seq decl_specifiers;
14827   bool function_definition_p = false;
14828
14829   /* Defer access checks until we know what is being declared.  */
14830   push_deferring_access_checks (dk_deferred);
14831
14832   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14833      alternative.  */
14834   cp_parser_decl_specifier_seq (parser,
14835                                 CP_PARSER_FLAGS_OPTIONAL,
14836                                 &decl_specifiers,
14837                                 &declares_class_or_enum);
14838   if (friend_p)
14839     *friend_p = cp_parser_friend_p (&decl_specifiers);
14840   /* Gather up the access checks that occurred the
14841      decl-specifier-seq.  */
14842   stop_deferring_access_checks ();
14843
14844   /* Check for the declaration of a template class.  */
14845   if (declares_class_or_enum)
14846     {
14847       if (cp_parser_declares_only_class_p (parser))
14848         {
14849           decl = shadow_tag (&decl_specifiers);
14850           if (decl && decl != error_mark_node)
14851             decl = TYPE_NAME (decl);
14852           else
14853             decl = error_mark_node;
14854         }
14855     }
14856   else
14857     decl = NULL_TREE;
14858   /* If it's not a template class, try for a template function.  If
14859      the next token is a `;', then this declaration does not declare
14860      anything.  But, if there were errors in the decl-specifiers, then
14861      the error might well have come from an attempted class-specifier.
14862      In that case, there's no need to warn about a missing declarator.  */
14863   if (!decl
14864       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14865           || decl_specifiers.type != error_mark_node))
14866     decl = cp_parser_init_declarator (parser,
14867                                       &decl_specifiers,
14868                                       /*function_definition_allowed_p=*/true,
14869                                       member_p,
14870                                       declares_class_or_enum,
14871                                       &function_definition_p);
14872
14873   pop_deferring_access_checks ();
14874
14875   /* Clear any current qualification; whatever comes next is the start
14876      of something new.  */
14877   parser->scope = NULL_TREE;
14878   parser->qualifying_scope = NULL_TREE;
14879   parser->object_scope = NULL_TREE;
14880   /* Look for a trailing `;' after the declaration.  */
14881   if (!function_definition_p
14882       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14883     cp_parser_skip_to_end_of_block_or_statement (parser);
14884
14885   return decl;
14886 }
14887
14888 /* Parse a cast-expression that is not the operand of a unary "&".  */
14889
14890 static tree
14891 cp_parser_simple_cast_expression (cp_parser *parser)
14892 {
14893   return cp_parser_cast_expression (parser, /*address_p=*/false);
14894 }
14895
14896 /* Parse a functional cast to TYPE.  Returns an expression
14897    representing the cast.  */
14898
14899 static tree
14900 cp_parser_functional_cast (cp_parser* parser, tree type)
14901 {
14902   tree expression_list;
14903   tree cast;
14904
14905   expression_list
14906     = cp_parser_parenthesized_expression_list (parser, false,
14907                                                /*non_constant_p=*/NULL);
14908
14909   cast = build_functional_cast (type, expression_list);
14910   /* [expr.const]/1: In an integral constant expression "only type
14911      conversions to integral or enumeration type can be used".  */
14912   if (cast != error_mark_node && !type_dependent_expression_p (type)
14913       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14914     {
14915       if (cp_parser_non_integral_constant_expression
14916           (parser, "a call to a constructor"))
14917         return error_mark_node;
14918     }
14919   return cast;
14920 }
14921
14922 /* Save the tokens that make up the body of a member function defined
14923    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14924    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14925    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14926    for the member function.  */
14927
14928 static tree
14929 cp_parser_save_member_function_body (cp_parser* parser,
14930                                      cp_decl_specifier_seq *decl_specifiers,
14931                                      cp_declarator *declarator,
14932                                      tree attributes)
14933 {
14934   cp_token *first;
14935   cp_token *last;
14936   tree fn;
14937
14938   /* Create the function-declaration.  */
14939   fn = start_method (decl_specifiers, declarator, attributes);
14940   /* If something went badly wrong, bail out now.  */
14941   if (fn == error_mark_node)
14942     {
14943       /* If there's a function-body, skip it.  */
14944       if (cp_parser_token_starts_function_definition_p
14945           (cp_lexer_peek_token (parser->lexer)))
14946         cp_parser_skip_to_end_of_block_or_statement (parser);
14947       return error_mark_node;
14948     }
14949
14950   /* Remember it, if there default args to post process.  */
14951   cp_parser_save_default_args (parser, fn);
14952
14953   /* Save away the tokens that make up the body of the
14954      function.  */
14955   first = parser->lexer->next_token;
14956   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14957   /* Handle function try blocks.  */
14958   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14959     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14960   last = parser->lexer->next_token;
14961
14962   /* Save away the inline definition; we will process it when the
14963      class is complete.  */
14964   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14965   DECL_PENDING_INLINE_P (fn) = 1;
14966
14967   /* We need to know that this was defined in the class, so that
14968      friend templates are handled correctly.  */
14969   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14970
14971   /* We're done with the inline definition.  */
14972   finish_method (fn);
14973
14974   /* Add FN to the queue of functions to be parsed later.  */
14975   TREE_VALUE (parser->unparsed_functions_queues)
14976     = tree_cons (NULL_TREE, fn,
14977                  TREE_VALUE (parser->unparsed_functions_queues));
14978
14979   return fn;
14980 }
14981
14982 /* Parse a template-argument-list, as well as the trailing ">" (but
14983    not the opening ">").  See cp_parser_template_argument_list for the
14984    return value.  */
14985
14986 static tree
14987 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14988 {
14989   tree arguments;
14990   tree saved_scope;
14991   tree saved_qualifying_scope;
14992   tree saved_object_scope;
14993   bool saved_greater_than_is_operator_p;
14994
14995   /* [temp.names]
14996
14997      When parsing a template-id, the first non-nested `>' is taken as
14998      the end of the template-argument-list rather than a greater-than
14999      operator.  */
15000   saved_greater_than_is_operator_p
15001     = parser->greater_than_is_operator_p;
15002   parser->greater_than_is_operator_p = false;
15003   /* Parsing the argument list may modify SCOPE, so we save it
15004      here.  */
15005   saved_scope = parser->scope;
15006   saved_qualifying_scope = parser->qualifying_scope;
15007   saved_object_scope = parser->object_scope;
15008   /* Parse the template-argument-list itself.  */
15009   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15010     arguments = NULL_TREE;
15011   else
15012     arguments = cp_parser_template_argument_list (parser);
15013   /* Look for the `>' that ends the template-argument-list. If we find
15014      a '>>' instead, it's probably just a typo.  */
15015   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15016     {
15017       if (!saved_greater_than_is_operator_p)
15018         {
15019           /* If we're in a nested template argument list, the '>>' has
15020             to be a typo for '> >'. We emit the error message, but we
15021             continue parsing and we push a '>' as next token, so that
15022             the argument list will be parsed correctly.  Note that the
15023             global source location is still on the token before the
15024             '>>', so we need to say explicitly where we want it.  */
15025           cp_token *token = cp_lexer_peek_token (parser->lexer);
15026           error ("%H%<>>%> should be %<> >%> "
15027                  "within a nested template argument list",
15028                  &token->location);
15029
15030           /* ??? Proper recovery should terminate two levels of
15031              template argument list here.  */
15032           token->type = CPP_GREATER;
15033         }
15034       else
15035         {
15036           /* If this is not a nested template argument list, the '>>'
15037             is a typo for '>'. Emit an error message and continue.
15038             Same deal about the token location, but here we can get it
15039             right by consuming the '>>' before issuing the diagnostic.  */
15040           cp_lexer_consume_token (parser->lexer);
15041           error ("spurious %<>>%>, use %<>%> to terminate "
15042                  "a template argument list");
15043         }
15044     }
15045   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15046     error ("missing %<>%> to terminate the template argument list");
15047   else
15048     /* It's what we want, a '>'; consume it.  */
15049     cp_lexer_consume_token (parser->lexer);
15050   /* The `>' token might be a greater-than operator again now.  */
15051   parser->greater_than_is_operator_p
15052     = saved_greater_than_is_operator_p;
15053   /* Restore the SAVED_SCOPE.  */
15054   parser->scope = saved_scope;
15055   parser->qualifying_scope = saved_qualifying_scope;
15056   parser->object_scope = saved_object_scope;
15057
15058   return arguments;
15059 }
15060
15061 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15062    arguments, or the body of the function have not yet been parsed,
15063    parse them now.  */
15064
15065 static void
15066 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15067 {
15068   /* If this member is a template, get the underlying
15069      FUNCTION_DECL.  */
15070   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15071     member_function = DECL_TEMPLATE_RESULT (member_function);
15072
15073   /* There should not be any class definitions in progress at this
15074      point; the bodies of members are only parsed outside of all class
15075      definitions.  */
15076   gcc_assert (parser->num_classes_being_defined == 0);
15077   /* While we're parsing the member functions we might encounter more
15078      classes.  We want to handle them right away, but we don't want
15079      them getting mixed up with functions that are currently in the
15080      queue.  */
15081   parser->unparsed_functions_queues
15082     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15083
15084   /* Make sure that any template parameters are in scope.  */
15085   maybe_begin_member_template_processing (member_function);
15086
15087   /* If the body of the function has not yet been parsed, parse it
15088      now.  */
15089   if (DECL_PENDING_INLINE_P (member_function))
15090     {
15091       tree function_scope;
15092       cp_token_cache *tokens;
15093
15094       /* The function is no longer pending; we are processing it.  */
15095       tokens = DECL_PENDING_INLINE_INFO (member_function);
15096       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15097       DECL_PENDING_INLINE_P (member_function) = 0;
15098       /* If this was an inline function in a local class, enter the scope
15099          of the containing function.  */
15100       function_scope = decl_function_context (member_function);
15101       if (function_scope)
15102         push_function_context_to (function_scope);
15103
15104       /* Push the body of the function onto the lexer stack.  */
15105       cp_parser_push_lexer_for_tokens (parser, tokens);
15106
15107       /* Let the front end know that we going to be defining this
15108          function.  */
15109       start_preparsed_function (member_function, NULL_TREE,
15110                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15111
15112       /* Now, parse the body of the function.  */
15113       cp_parser_function_definition_after_declarator (parser,
15114                                                       /*inline_p=*/true);
15115
15116       /* Leave the scope of the containing function.  */
15117       if (function_scope)
15118         pop_function_context_from (function_scope);
15119       cp_parser_pop_lexer (parser);
15120     }
15121
15122   /* Remove any template parameters from the symbol table.  */
15123   maybe_end_member_template_processing ();
15124
15125   /* Restore the queue.  */
15126   parser->unparsed_functions_queues
15127     = TREE_CHAIN (parser->unparsed_functions_queues);
15128 }
15129
15130 /* If DECL contains any default args, remember it on the unparsed
15131    functions queue.  */
15132
15133 static void
15134 cp_parser_save_default_args (cp_parser* parser, tree decl)
15135 {
15136   tree probe;
15137
15138   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15139        probe;
15140        probe = TREE_CHAIN (probe))
15141     if (TREE_PURPOSE (probe))
15142       {
15143         TREE_PURPOSE (parser->unparsed_functions_queues)
15144           = tree_cons (current_class_type, decl,
15145                        TREE_PURPOSE (parser->unparsed_functions_queues));
15146         break;
15147       }
15148   return;
15149 }
15150
15151 /* FN is a FUNCTION_DECL which may contains a parameter with an
15152    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15153    assumes that the current scope is the scope in which the default
15154    argument should be processed.  */
15155
15156 static void
15157 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15158 {
15159   bool saved_local_variables_forbidden_p;
15160   tree parm;
15161
15162   /* While we're parsing the default args, we might (due to the
15163      statement expression extension) encounter more classes.  We want
15164      to handle them right away, but we don't want them getting mixed
15165      up with default args that are currently in the queue.  */
15166   parser->unparsed_functions_queues
15167     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15168
15169   /* Local variable names (and the `this' keyword) may not appear
15170      in a default argument.  */
15171   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15172   parser->local_variables_forbidden_p = true;
15173
15174   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15175        parm;
15176        parm = TREE_CHAIN (parm))
15177     {
15178       cp_token_cache *tokens;
15179
15180       if (!TREE_PURPOSE (parm)
15181           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15182         continue;
15183
15184        /* Push the saved tokens for the default argument onto the parser's
15185           lexer stack.  */
15186       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15187       cp_parser_push_lexer_for_tokens (parser, tokens);
15188
15189       /* Parse the assignment-expression.  */
15190       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15191
15192       /* If the token stream has not been completely used up, then
15193          there was extra junk after the end of the default
15194          argument.  */
15195       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15196         cp_parser_error (parser, "expected %<,%>");
15197
15198       /* Revert to the main lexer.  */
15199       cp_parser_pop_lexer (parser);
15200     }
15201
15202   /* Restore the state of local_variables_forbidden_p.  */
15203   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15204
15205   /* Restore the queue.  */
15206   parser->unparsed_functions_queues
15207     = TREE_CHAIN (parser->unparsed_functions_queues);
15208 }
15209
15210 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15211    either a TYPE or an expression, depending on the form of the
15212    input.  The KEYWORD indicates which kind of expression we have
15213    encountered.  */
15214
15215 static tree
15216 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15217 {
15218   static const char *format;
15219   tree expr = NULL_TREE;
15220   const char *saved_message;
15221   bool saved_integral_constant_expression_p;
15222
15223   /* Initialize FORMAT the first time we get here.  */
15224   if (!format)
15225     format = "types may not be defined in `%s' expressions";
15226
15227   /* Types cannot be defined in a `sizeof' expression.  Save away the
15228      old message.  */
15229   saved_message = parser->type_definition_forbidden_message;
15230   /* And create the new one.  */
15231   parser->type_definition_forbidden_message
15232     = xmalloc (strlen (format)
15233                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15234                + 1 /* `\0' */);
15235   sprintf ((char *) parser->type_definition_forbidden_message,
15236            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15237
15238   /* The restrictions on constant-expressions do not apply inside
15239      sizeof expressions.  */
15240   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15241   parser->integral_constant_expression_p = false;
15242
15243   /* Do not actually evaluate the expression.  */
15244   ++skip_evaluation;
15245   /* If it's a `(', then we might be looking at the type-id
15246      construction.  */
15247   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15248     {
15249       tree type;
15250       bool saved_in_type_id_in_expr_p;
15251
15252       /* We can't be sure yet whether we're looking at a type-id or an
15253          expression.  */
15254       cp_parser_parse_tentatively (parser);
15255       /* Consume the `('.  */
15256       cp_lexer_consume_token (parser->lexer);
15257       /* Parse the type-id.  */
15258       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15259       parser->in_type_id_in_expr_p = true;
15260       type = cp_parser_type_id (parser);
15261       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15262       /* Now, look for the trailing `)'.  */
15263       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15264       /* If all went well, then we're done.  */
15265       if (cp_parser_parse_definitely (parser))
15266         {
15267           cp_decl_specifier_seq decl_specs;
15268
15269           /* Build a trivial decl-specifier-seq.  */
15270           clear_decl_specs (&decl_specs);
15271           decl_specs.type = type;
15272
15273           /* Call grokdeclarator to figure out what type this is.  */
15274           expr = grokdeclarator (NULL,
15275                                  &decl_specs,
15276                                  TYPENAME,
15277                                  /*initialized=*/0,
15278                                  /*attrlist=*/NULL);
15279         }
15280     }
15281
15282   /* If the type-id production did not work out, then we must be
15283      looking at the unary-expression production.  */
15284   if (!expr)
15285     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15286   /* Go back to evaluating expressions.  */
15287   --skip_evaluation;
15288
15289   /* Free the message we created.  */
15290   free ((char *) parser->type_definition_forbidden_message);
15291   /* And restore the old one.  */
15292   parser->type_definition_forbidden_message = saved_message;
15293   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15294
15295   return expr;
15296 }
15297
15298 /* If the current declaration has no declarator, return true.  */
15299
15300 static bool
15301 cp_parser_declares_only_class_p (cp_parser *parser)
15302 {
15303   /* If the next token is a `;' or a `,' then there is no
15304      declarator.  */
15305   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15306           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15307 }
15308
15309 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15310
15311 static void
15312 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15313                              cp_storage_class storage_class)
15314 {
15315   if (decl_specs->storage_class != sc_none)
15316     decl_specs->multiple_storage_classes_p = true;
15317   else
15318     decl_specs->storage_class = storage_class;
15319 }
15320
15321 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15322    is true, the type is a user-defined type; otherwise it is a
15323    built-in type specified by a keyword.  */
15324
15325 static void
15326 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15327                               tree type_spec,
15328                               bool user_defined_p)
15329 {
15330   decl_specs->any_specifiers_p = true;
15331
15332   /* If the user tries to redeclare a built-in type (with, for example,
15333      in "typedef int wchar_t;") we remember that this is what
15334      happened.  In system headers, we ignore these declarations so
15335      that G++ can work with system headers that are not C++-safe.  */
15336   if (decl_specs->specs[(int) ds_typedef]
15337       && !user_defined_p
15338       && (decl_specs->type
15339           || decl_specs->specs[(int) ds_long]
15340           || decl_specs->specs[(int) ds_short]
15341           || decl_specs->specs[(int) ds_unsigned]
15342           || decl_specs->specs[(int) ds_signed]))
15343     {
15344       decl_specs->redefined_builtin_type = type_spec;
15345       if (!decl_specs->type)
15346         {
15347           decl_specs->type = type_spec;
15348           decl_specs->user_defined_type_p = false;
15349         }
15350     }
15351   else if (decl_specs->type)
15352     decl_specs->multiple_types_p = true;
15353   else
15354     {
15355       decl_specs->type = type_spec;
15356       decl_specs->user_defined_type_p = user_defined_p;
15357       decl_specs->redefined_builtin_type = NULL_TREE;
15358     }
15359 }
15360
15361 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15362    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15363
15364 static bool
15365 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15366 {
15367   return decl_specifiers->specs[(int) ds_friend] != 0;
15368 }
15369
15370 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15371    issue an error message indicating that TOKEN_DESC was expected.
15372
15373    Returns the token consumed, if the token had the appropriate type.
15374    Otherwise, returns NULL.  */
15375
15376 static cp_token *
15377 cp_parser_require (cp_parser* parser,
15378                    enum cpp_ttype type,
15379                    const char* token_desc)
15380 {
15381   if (cp_lexer_next_token_is (parser->lexer, type))
15382     return cp_lexer_consume_token (parser->lexer);
15383   else
15384     {
15385       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15386       if (!cp_parser_simulate_error (parser))
15387         {
15388           char *message = concat ("expected ", token_desc, NULL);
15389           cp_parser_error (parser, message);
15390           free (message);
15391         }
15392       return NULL;
15393     }
15394 }
15395
15396 /* Like cp_parser_require, except that tokens will be skipped until
15397    the desired token is found.  An error message is still produced if
15398    the next token is not as expected.  */
15399
15400 static void
15401 cp_parser_skip_until_found (cp_parser* parser,
15402                             enum cpp_ttype type,
15403                             const char* token_desc)
15404 {
15405   cp_token *token;
15406   unsigned nesting_depth = 0;
15407
15408   if (cp_parser_require (parser, type, token_desc))
15409     return;
15410
15411   /* Skip tokens until the desired token is found.  */
15412   while (true)
15413     {
15414       /* Peek at the next token.  */
15415       token = cp_lexer_peek_token (parser->lexer);
15416       /* If we've reached the token we want, consume it and
15417          stop.  */
15418       if (token->type == type && !nesting_depth)
15419         {
15420           cp_lexer_consume_token (parser->lexer);
15421           return;
15422         }
15423       /* If we've run out of tokens, stop.  */
15424       if (token->type == CPP_EOF)
15425         return;
15426       if (token->type == CPP_OPEN_BRACE
15427           || token->type == CPP_OPEN_PAREN
15428           || token->type == CPP_OPEN_SQUARE)
15429         ++nesting_depth;
15430       else if (token->type == CPP_CLOSE_BRACE
15431                || token->type == CPP_CLOSE_PAREN
15432                || token->type == CPP_CLOSE_SQUARE)
15433         {
15434           if (nesting_depth-- == 0)
15435             return;
15436         }
15437       /* Consume this token.  */
15438       cp_lexer_consume_token (parser->lexer);
15439     }
15440 }
15441
15442 /* If the next token is the indicated keyword, consume it.  Otherwise,
15443    issue an error message indicating that TOKEN_DESC was expected.
15444
15445    Returns the token consumed, if the token had the appropriate type.
15446    Otherwise, returns NULL.  */
15447
15448 static cp_token *
15449 cp_parser_require_keyword (cp_parser* parser,
15450                            enum rid keyword,
15451                            const char* token_desc)
15452 {
15453   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15454
15455   if (token && token->keyword != keyword)
15456     {
15457       dyn_string_t error_msg;
15458
15459       /* Format the error message.  */
15460       error_msg = dyn_string_new (0);
15461       dyn_string_append_cstr (error_msg, "expected ");
15462       dyn_string_append_cstr (error_msg, token_desc);
15463       cp_parser_error (parser, error_msg->s);
15464       dyn_string_delete (error_msg);
15465       return NULL;
15466     }
15467
15468   return token;
15469 }
15470
15471 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15472    function-definition.  */
15473
15474 static bool
15475 cp_parser_token_starts_function_definition_p (cp_token* token)
15476 {
15477   return (/* An ordinary function-body begins with an `{'.  */
15478           token->type == CPP_OPEN_BRACE
15479           /* A ctor-initializer begins with a `:'.  */
15480           || token->type == CPP_COLON
15481           /* A function-try-block begins with `try'.  */
15482           || token->keyword == RID_TRY
15483           /* The named return value extension begins with `return'.  */
15484           || token->keyword == RID_RETURN);
15485 }
15486
15487 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15488    definition.  */
15489
15490 static bool
15491 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15492 {
15493   cp_token *token;
15494
15495   token = cp_lexer_peek_token (parser->lexer);
15496   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15497 }
15498
15499 /* Returns TRUE iff the next token is the "," or ">" ending a
15500    template-argument. ">>" is also accepted (after the full
15501    argument was parsed) because it's probably a typo for "> >",
15502    and there is a specific diagnostic for this.  */
15503
15504 static bool
15505 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15506 {
15507   cp_token *token;
15508
15509   token = cp_lexer_peek_token (parser->lexer);
15510   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15511           || token->type == CPP_RSHIFT);
15512 }
15513
15514 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15515    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15516
15517 static bool
15518 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15519                                                      size_t n)
15520 {
15521   cp_token *token;
15522
15523   token = cp_lexer_peek_nth_token (parser->lexer, n);
15524   if (token->type == CPP_LESS)
15525     return true;
15526   /* Check for the sequence `<::' in the original code. It would be lexed as
15527      `[:', where `[' is a digraph, and there is no whitespace before
15528      `:'.  */
15529   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15530     {
15531       cp_token *token2;
15532       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15533       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15534         return true;
15535     }
15536   return false;
15537 }
15538
15539 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15540    or none_type otherwise.  */
15541
15542 static enum tag_types
15543 cp_parser_token_is_class_key (cp_token* token)
15544 {
15545   switch (token->keyword)
15546     {
15547     case RID_CLASS:
15548       return class_type;
15549     case RID_STRUCT:
15550       return record_type;
15551     case RID_UNION:
15552       return union_type;
15553
15554     default:
15555       return none_type;
15556     }
15557 }
15558
15559 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15560
15561 static void
15562 cp_parser_check_class_key (enum tag_types class_key, tree type)
15563 {
15564   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15565     pedwarn ("%qs tag used in naming %q#T",
15566             class_key == union_type ? "union"
15567              : class_key == record_type ? "struct" : "class",
15568              type);
15569 }
15570
15571 /* Issue an error message if DECL is redeclared with different
15572    access than its original declaration [class.access.spec/3].
15573    This applies to nested classes and nested class templates.
15574    [class.mem/1].  */
15575
15576 static void
15577 cp_parser_check_access_in_redeclaration (tree decl)
15578 {
15579   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15580     return;
15581
15582   if ((TREE_PRIVATE (decl)
15583        != (current_access_specifier == access_private_node))
15584       || (TREE_PROTECTED (decl)
15585           != (current_access_specifier == access_protected_node)))
15586     error ("%qD redeclared with different access", decl);
15587 }
15588
15589 /* Look for the `template' keyword, as a syntactic disambiguator.
15590    Return TRUE iff it is present, in which case it will be
15591    consumed.  */
15592
15593 static bool
15594 cp_parser_optional_template_keyword (cp_parser *parser)
15595 {
15596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15597     {
15598       /* The `template' keyword can only be used within templates;
15599          outside templates the parser can always figure out what is a
15600          template and what is not.  */
15601       if (!processing_template_decl)
15602         {
15603           error ("%<template%> (as a disambiguator) is only allowed "
15604                  "within templates");
15605           /* If this part of the token stream is rescanned, the same
15606              error message would be generated.  So, we purge the token
15607              from the stream.  */
15608           cp_lexer_purge_token (parser->lexer);
15609           return false;
15610         }
15611       else
15612         {
15613           /* Consume the `template' keyword.  */
15614           cp_lexer_consume_token (parser->lexer);
15615           return true;
15616         }
15617     }
15618
15619   return false;
15620 }
15621
15622 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15623    set PARSER->SCOPE, and perform other related actions.  */
15624
15625 static void
15626 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15627 {
15628   tree value;
15629   tree check;
15630
15631   /* Get the stored value.  */
15632   value = cp_lexer_consume_token (parser->lexer)->value;
15633   /* Perform any access checks that were deferred.  */
15634   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15635     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15636   /* Set the scope from the stored value.  */
15637   parser->scope = TREE_VALUE (value);
15638   parser->qualifying_scope = TREE_TYPE (value);
15639   parser->object_scope = NULL_TREE;
15640 }
15641
15642 /* Consume tokens up through a non-nested END token. */
15643
15644 static void
15645 cp_parser_cache_group (cp_parser *parser,
15646                        enum cpp_ttype end,
15647                        unsigned depth)
15648 {
15649   while (true)
15650     {
15651       cp_token *token;
15652
15653       /* Abort a parenthesized expression if we encounter a brace.  */
15654       if ((end == CPP_CLOSE_PAREN || depth == 0)
15655           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15656         return;
15657       /* If we've reached the end of the file, stop.  */
15658       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15659         return;
15660       /* Consume the next token.  */
15661       token = cp_lexer_consume_token (parser->lexer);
15662       /* See if it starts a new group.  */
15663       if (token->type == CPP_OPEN_BRACE)
15664         {
15665           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15666           if (depth == 0)
15667             return;
15668         }
15669       else if (token->type == CPP_OPEN_PAREN)
15670         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15671       else if (token->type == end)
15672         return;
15673     }
15674 }
15675
15676 /* Begin parsing tentatively.  We always save tokens while parsing
15677    tentatively so that if the tentative parsing fails we can restore the
15678    tokens.  */
15679
15680 static void
15681 cp_parser_parse_tentatively (cp_parser* parser)
15682 {
15683   /* Enter a new parsing context.  */
15684   parser->context = cp_parser_context_new (parser->context);
15685   /* Begin saving tokens.  */
15686   cp_lexer_save_tokens (parser->lexer);
15687   /* In order to avoid repetitive access control error messages,
15688      access checks are queued up until we are no longer parsing
15689      tentatively.  */
15690   push_deferring_access_checks (dk_deferred);
15691 }
15692
15693 /* Commit to the currently active tentative parse.  */
15694
15695 static void
15696 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15697 {
15698   cp_parser_context *context;
15699   cp_lexer *lexer;
15700
15701   /* Mark all of the levels as committed.  */
15702   lexer = parser->lexer;
15703   for (context = parser->context; context->next; context = context->next)
15704     {
15705       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15706         break;
15707       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15708       while (!cp_lexer_saving_tokens (lexer))
15709         lexer = lexer->next;
15710       cp_lexer_commit_tokens (lexer);
15711     }
15712 }
15713
15714 /* Abort the currently active tentative parse.  All consumed tokens
15715    will be rolled back, and no diagnostics will be issued.  */
15716
15717 static void
15718 cp_parser_abort_tentative_parse (cp_parser* parser)
15719 {
15720   cp_parser_simulate_error (parser);
15721   /* Now, pretend that we want to see if the construct was
15722      successfully parsed.  */
15723   cp_parser_parse_definitely (parser);
15724 }
15725
15726 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15727    token stream.  Otherwise, commit to the tokens we have consumed.
15728    Returns true if no error occurred; false otherwise.  */
15729
15730 static bool
15731 cp_parser_parse_definitely (cp_parser* parser)
15732 {
15733   bool error_occurred;
15734   cp_parser_context *context;
15735
15736   /* Remember whether or not an error occurred, since we are about to
15737      destroy that information.  */
15738   error_occurred = cp_parser_error_occurred (parser);
15739   /* Remove the topmost context from the stack.  */
15740   context = parser->context;
15741   parser->context = context->next;
15742   /* If no parse errors occurred, commit to the tentative parse.  */
15743   if (!error_occurred)
15744     {
15745       /* Commit to the tokens read tentatively, unless that was
15746          already done.  */
15747       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15748         cp_lexer_commit_tokens (parser->lexer);
15749
15750       pop_to_parent_deferring_access_checks ();
15751     }
15752   /* Otherwise, if errors occurred, roll back our state so that things
15753      are just as they were before we began the tentative parse.  */
15754   else
15755     {
15756       cp_lexer_rollback_tokens (parser->lexer);
15757       pop_deferring_access_checks ();
15758     }
15759   /* Add the context to the front of the free list.  */
15760   context->next = cp_parser_context_free_list;
15761   cp_parser_context_free_list = context;
15762
15763   return !error_occurred;
15764 }
15765
15766 /* Returns true if we are parsing tentatively -- but have decided that
15767    we will stick with this tentative parse, even if errors occur.  */
15768
15769 static bool
15770 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15771 {
15772   return (cp_parser_parsing_tentatively (parser)
15773           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15774 }
15775
15776 /* Returns nonzero iff an error has occurred during the most recent
15777    tentative parse.  */
15778
15779 static bool
15780 cp_parser_error_occurred (cp_parser* parser)
15781 {
15782   return (cp_parser_parsing_tentatively (parser)
15783           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15784 }
15785
15786 /* Returns nonzero if GNU extensions are allowed.  */
15787
15788 static bool
15789 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15790 {
15791   return parser->allow_gnu_extensions_p;
15792 }
15793
15794 \f
15795 /* The parser.  */
15796
15797 static GTY (()) cp_parser *the_parser;
15798
15799 /* External interface.  */
15800
15801 /* Parse one entire translation unit.  */
15802
15803 void
15804 c_parse_file (void)
15805 {
15806   bool error_occurred;
15807   static bool already_called = false;
15808
15809   if (already_called)
15810     {
15811       sorry ("inter-module optimizations not implemented for C++");
15812       return;
15813     }
15814   already_called = true;
15815
15816   the_parser = cp_parser_new ();
15817   push_deferring_access_checks (flag_access_control
15818                                 ? dk_no_deferred : dk_no_check);
15819   error_occurred = cp_parser_translation_unit (the_parser);
15820   the_parser = NULL;
15821 }
15822
15823 /* This variable must be provided by every front end.  */
15824
15825 int yydebug;
15826
15827 #include "gt-cp-parser.h"