OSDN Git Service

* class.c (layout_class_type): Do not issue C++ ABI warnings
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* The value associated with this token, if any.  */
63   tree value;
64   /* The location at which this token was found.  */
65   location_t location;
66 } cp_token;
67
68 /* We use a stack of token pointer for saving token sets.  */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
72
73 static const cp_token eof_token =
74 {
75   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
77   0
78 #else
79   {0, 0}
80 #endif
81 };
82
83 /* The cp_lexer structure represents the C++ lexer.  It is responsible
84    for managing the token stream from the preprocessor and supplying
85    it to the parser.  Tokens are never added to the cp_lexer after
86    it is created.  */
87
88 typedef struct cp_lexer GTY (())
89 {
90   /* The memory allocated for the buffer.  NULL if this lexer does not
91      own the token buffer.  */
92   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93   /* If the lexer owns the buffer, this is the number of tokens in the
94      buffer.  */
95   size_t buffer_length;
96   
97   /* A pointer just past the last available token.  The tokens
98      in this lexer are [buffer, last_token).  */
99   cp_token_position GTY ((skip)) last_token;
100
101   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
102      no more available tokens.  */
103   cp_token_position GTY ((skip)) next_token;
104
105   /* A stack indicating positions at which cp_lexer_save_tokens was
106      called.  The top entry is the most recent position at which we
107      began saving tokens.  If the stack is non-empty, we are saving
108      tokens.  */
109   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
110
111   /* True if we should output debugging information.  */
112   bool debugging_p;
113
114   /* The next lexer in a linked list of lexers.  */
115   struct cp_lexer *next;
116 } cp_lexer;
117
118 /* cp_token_cache is a range of tokens.  There is no need to represent
119    allocate heap memory for it, since tokens are never removed from the
120    lexer's array.  There is also no need for the GC to walk through
121    a cp_token_cache, since everything in here is referenced through
122    a lexer.  */
123
124 typedef struct cp_token_cache GTY(())
125 {
126   /* The beginning of the token range.  */
127   cp_token * GTY((skip)) first;
128
129   /* Points immediately after the last token in the range.  */
130   cp_token * GTY ((skip)) last;
131 } cp_token_cache;
132
133 /* Prototypes.  */
134
135 static cp_lexer *cp_lexer_new_main
136   (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138   (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140   (cp_lexer *);
141 static int cp_lexer_saving_tokens
142   (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144   (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146   (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148   (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150   (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152   (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156   (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158   (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160   (cp_lexer *);
161 static void cp_lexer_purge_token
162   (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164   (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166   (cp_lexer *);
167 static void cp_lexer_save_tokens
168   (cp_lexer *);
169 static void cp_lexer_commit_tokens
170   (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172   (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175   (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177   (cp_lexer *);
178 static void cp_lexer_start_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181   (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184    about passing NULL to functions that require non-NULL arguments
185    (fputs, fprintf).  It will never be used, so all we need is a value
186    of the right type that's guaranteed not to be NULL.  */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
191
192 static cp_token_cache *cp_token_cache_new
193   (cp_token *, cp_token *);
194
195 /* Manifest constants.  */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
198
199 /* A token type for keywords, as opposed to ordinary identifiers.  */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202 /* A token type for template-ids.  If a template-id is processed while
203    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204    the value of the CPP_TEMPLATE_ID is whatever was returned by
205    cp_parser_template_id.  */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208 /* A token type for nested-name-specifiers.  If a
209    nested-name-specifier is processed while parsing tentatively, it is
210    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212    cp_parser_nested_name_specifier_opt.  */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215 /* A token type for tokens that are not tokens at all; these are used
216    to represent slots in the array where there used to be a token
217    that has now been deleted.  */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220 /* The number of token types, including C++-specific ones.  */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222
223 /* Variables.  */
224
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written.  */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
229
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231    preprocessor.  */
232
233 static cp_lexer *
234 cp_lexer_new_main (void)
235 {
236   cp_token first_token;
237   cp_lexer *lexer;
238   cp_token *pos;
239   size_t alloc;
240   size_t space;
241   cp_token *buffer;
242
243   /* It's possible that lexing the first token will load a PCH file,
244      which is a GC collection point.  So we have to grab the first
245      token before allocating any memory.  Pragmas must not be deferred
246      as -fpch-preprocess can generate a pragma to load the PCH file in
247      the preprocessed output used by -save-temps.  */
248   cp_lexer_get_preprocessor_token (NULL, &first_token);
249
250   /* Tell cpplib we want CPP_PRAGMA tokens.  */
251   cpp_get_options (parse_in)->defer_pragmas = true;
252
253   /* Tell c_lex not to merge string constants.  */
254   c_lex_return_raw_strings = true;
255
256   c_common_no_more_pch ();
257
258   /* Allocate the memory.  */
259   lexer = GGC_CNEW (cp_lexer);
260
261 #ifdef ENABLE_CHECKING  
262   /* Initially we are not debugging.  */
263   lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266                                    CP_SAVED_TOKEN_STACK);
267          
268   /* Create the buffer.  */
269   alloc = CP_LEXER_BUFFER_SIZE;
270   buffer = ggc_alloc (alloc * sizeof (cp_token));
271
272   /* Put the first token in the buffer.  */
273   space = alloc;
274   pos = buffer;
275   *pos = first_token;
276   
277   /* Get the remaining tokens from the preprocessor.  */
278   while (pos->type != CPP_EOF)
279     {
280       pos++;
281       if (!--space)
282         {
283           space = alloc;
284           alloc *= 2;
285           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286           pos = buffer + space;
287         }
288       cp_lexer_get_preprocessor_token (lexer, pos);
289     }
290   lexer->buffer = buffer;
291   lexer->buffer_length = alloc - space;
292   lexer->last_token = pos;
293   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
294
295   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296      direct calls to c_lex.  Those callers all expect c_lex to do
297      string constant concatenation.  */
298   c_lex_return_raw_strings = false;
299
300   gcc_assert (lexer->next_token->type != CPP_PURGED);
301   return lexer;
302 }
303
304 /* Create a new lexer whose token stream is primed with the tokens in
305    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
306
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
309 {
310   cp_token *first = cache->first;
311   cp_token *last = cache->last;
312   cp_lexer *lexer = GGC_CNEW (cp_lexer);
313
314   /* We do not own the buffer.  */
315   lexer->buffer = NULL;
316   lexer->buffer_length = 0;
317   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318   lexer->last_token = last;
319   
320   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321                                    CP_SAVED_TOKEN_STACK);
322
323 #ifdef ENABLE_CHECKING
324   /* Initially we are not debugging.  */
325   lexer->debugging_p = false;
326 #endif
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Frees all resources associated with LEXER.  */
333
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
336 {
337   if (lexer->buffer)
338     ggc_free (lexer->buffer);
339   VEC_free (cp_token_position, heap, lexer->saved_tokens);
340   ggc_free (lexer);
341 }
342
343 /* Returns nonzero if debugging information should be output.  */
344
345 #ifdef ENABLE_CHECKING
346
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
349 {
350   return lexer->debugging_p;
351 }
352
353 #endif /* ENABLE_CHECKING */
354
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
357 {
358   gcc_assert (!previous_p || lexer->next_token != &eof_token);
359   
360   return lexer->next_token - previous_p;
361 }
362
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 {
366   return pos;
367 }
368
369 /* nonzero if we are presently saving tokens.  */
370
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
373 {
374   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
375 }
376
377 /* Store the next token from the preprocessor in *TOKEN.  Return true
378    if we reach EOF.  */
379
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382                                  cp_token *token)
383 {
384   static int is_extern_c = 0;
385
386    /* Get a new token from the preprocessor.  */
387   token->type
388     = c_lex_with_flags (&token->value, &token->location, &token->flags);
389   token->in_system_header = in_system_header;
390
391   /* On some systems, some header files are surrounded by an 
392      implicit extern "C" block.  Set a flag in the token if it
393      comes from such a header.  */
394   is_extern_c += pending_lang_change;
395   pending_lang_change = 0;
396   token->implicit_extern_c = is_extern_c > 0;
397
398   /* Check to see if this token is a keyword.  */
399   if (token->type == CPP_NAME
400       && C_IS_RESERVED_WORD (token->value))
401     {
402       /* Mark this token as a keyword.  */
403       token->type = CPP_KEYWORD;
404       /* Record which keyword.  */
405       token->keyword = C_RID_CODE (token->value);
406       /* Update the value.  Some keywords are mapped to particular
407          entities, rather than simply having the value of the
408          corresponding IDENTIFIER_NODE.  For example, `__const' is
409          mapped to `const'.  */
410       token->value = ridpointers[token->keyword];
411     }
412   /* Handle Objective-C++ keywords.  */
413   else if (token->type == CPP_AT_NAME)
414     {
415       token->type = CPP_KEYWORD;
416       switch (C_RID_CODE (token->value))
417         {
418         /* Map 'class' to '@class', 'private' to '@private', etc.  */
419         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423         case RID_THROW: token->keyword = RID_AT_THROW; break;
424         case RID_TRY: token->keyword = RID_AT_TRY; break;
425         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426         default: token->keyword = C_RID_CODE (token->value);
427         }
428     }
429   else
430     token->keyword = RID_MAX;
431 }
432
433 /* Update the globals input_location and in_system_header from TOKEN.  */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
436 {
437   if (token->type != CPP_EOF)
438     {
439       input_location = token->location;
440       in_system_header = token->in_system_header;
441     }
442 }
443
444 /* Return a pointer to the next token in the token stream, but do not
445    consume it.  */
446
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
449 {
450   if (cp_lexer_debugging_p (lexer))
451     {
452       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454       putc ('\n', cp_lexer_debug_stream);
455     }
456   return lexer->next_token;
457 }
458
459 /* Return true if the next token has the indicated TYPE.  */
460
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
463 {
464   return cp_lexer_peek_token (lexer)->type == type;
465 }
466
467 /* Return true if the next token does not have the indicated TYPE.  */
468
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
471 {
472   return !cp_lexer_next_token_is (lexer, type);
473 }
474
475 /* Return true if the next token is the indicated KEYWORD.  */
476
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
479 {
480   cp_token *token;
481
482   /* Peek at the next token.  */
483   token = cp_lexer_peek_token (lexer);
484   /* Check to see if it is the indicated keyword.  */
485   return token->keyword == keyword;
486 }
487
488 /* Return a pointer to the Nth token in the token stream.  If N is 1,
489    then this is precisely equivalent to cp_lexer_peek_token (except
490    that it is not inline).  One would like to disallow that case, but
491    there is one case (cp_parser_nth_token_starts_template_id) where
492    the caller passes a variable for N and it might be 1.  */
493
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
496 {
497   cp_token *token;
498
499   /* N is 1-based, not zero-based.  */
500   gcc_assert (n > 0 && lexer->next_token != &eof_token);
501
502   if (cp_lexer_debugging_p (lexer))
503     fprintf (cp_lexer_debug_stream,
504              "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
506   --n;
507   token = lexer->next_token;
508   while (n != 0)
509     {
510       ++token;
511       if (token == lexer->last_token)
512         {
513           token = (cp_token *)&eof_token;
514           break;
515         }
516       
517       if (token->type != CPP_PURGED)
518         --n;
519     }
520
521   if (cp_lexer_debugging_p (lexer))
522     {
523       cp_lexer_print_token (cp_lexer_debug_stream, token);
524       putc ('\n', cp_lexer_debug_stream);
525     }
526
527   return token;
528 }
529
530 /* Return the next token, and advance the lexer's next_token pointer
531    to point to the next non-purged token.  */
532
533 static cp_token *
534 cp_lexer_consume_token (cp_lexer* lexer)
535 {
536   cp_token *token = lexer->next_token;
537
538   gcc_assert (token != &eof_token);
539   
540   do
541     {
542       lexer->next_token++;
543       if (lexer->next_token == lexer->last_token)
544         {
545           lexer->next_token = (cp_token *)&eof_token;
546           break;
547         }
548       
549     }
550   while (lexer->next_token->type == CPP_PURGED);
551   
552   cp_lexer_set_source_position_from_token (token);
553   
554   /* Provide debugging output.  */
555   if (cp_lexer_debugging_p (lexer))
556     {
557       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558       cp_lexer_print_token (cp_lexer_debug_stream, token);
559       putc ('\n', cp_lexer_debug_stream);
560     }
561   
562   return token;
563 }
564
565 /* Permanently remove the next token from the token stream, and
566    advance the next_token pointer to refer to the next non-purged
567    token.  */
568
569 static void
570 cp_lexer_purge_token (cp_lexer *lexer)
571 {
572   cp_token *tok = lexer->next_token;
573   
574   gcc_assert (tok != &eof_token);
575   tok->type = CPP_PURGED;
576   tok->location = UNKNOWN_LOCATION;
577   tok->value = NULL_TREE;
578   tok->keyword = RID_MAX;
579
580   do
581     {
582       tok++;
583       if (tok == lexer->last_token)
584         {
585           tok = (cp_token *)&eof_token;
586           break;
587         }
588     }
589   while (tok->type == CPP_PURGED);
590   lexer->next_token = tok;
591 }
592
593 /* Permanently remove all tokens after TOK, up to, but not
594    including, the token that will be returned next by
595    cp_lexer_peek_token.  */
596
597 static void
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
599 {
600   cp_token *peek = lexer->next_token;
601
602   if (peek == &eof_token)
603     peek = lexer->last_token;
604   
605   gcc_assert (tok < peek);
606
607   for ( tok += 1; tok != peek; tok += 1)
608     {
609       tok->type = CPP_PURGED;
610       tok->location = UNKNOWN_LOCATION;
611       tok->value = NULL_TREE;
612       tok->keyword = RID_MAX;
613     }
614 }
615
616 /* Consume and handle a pragma token.  */
617 static void
618 cp_lexer_handle_pragma (cp_lexer *lexer)
619 {
620   cpp_string s;
621   cp_token *token = cp_lexer_consume_token (lexer);
622   gcc_assert (token->type == CPP_PRAGMA);
623   gcc_assert (token->value);
624
625   s.len = TREE_STRING_LENGTH (token->value);
626   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
627
628   cpp_handle_deferred_pragma (parse_in, &s);
629
630   /* Clearing token->value here means that we will get an ICE if we
631      try to process this #pragma again (which should be impossible).  */
632   token->value = NULL;
633 }
634
635 /* Begin saving tokens.  All tokens consumed after this point will be
636    preserved.  */
637
638 static void
639 cp_lexer_save_tokens (cp_lexer* lexer)
640 {
641   /* Provide debugging output.  */
642   if (cp_lexer_debugging_p (lexer))
643     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
644
645   VEC_safe_push (cp_token_position, heap,
646                  lexer->saved_tokens, lexer->next_token);
647 }
648
649 /* Commit to the portion of the token stream most recently saved.  */
650
651 static void
652 cp_lexer_commit_tokens (cp_lexer* lexer)
653 {
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
657
658   VEC_pop (cp_token_position, lexer->saved_tokens);
659 }
660
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662    to the token stream.  Stop saving tokens.  */
663
664 static void
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
666 {
667   /* Provide debugging output.  */
668   if (cp_lexer_debugging_p (lexer))
669     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
670
671   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 }
673
674 /* Print a representation of the TOKEN on the STREAM.  */
675
676 #ifdef ENABLE_CHECKING
677
678 static void
679 cp_lexer_print_token (FILE * stream, cp_token *token)
680 {
681   /* We don't use cpp_type2name here because the parser defines
682      a few tokens of its own.  */
683   static const char *const token_names[] = {
684     /* cpplib-defined token types */
685 #define OP(e, s) #e,
686 #define TK(e, s) #e,
687     TTYPE_TABLE
688 #undef OP
689 #undef TK
690     /* C++ parser token types - see "Manifest constants", above.  */
691     "KEYWORD",
692     "TEMPLATE_ID",
693     "NESTED_NAME_SPECIFIER",
694     "PURGED"
695   };
696   
697   /* If we have a name for the token, print it out.  Otherwise, we
698      simply give the numeric code.  */
699   gcc_assert (token->type < ARRAY_SIZE(token_names));
700   fputs (token_names[token->type], stream);
701
702   /* For some tokens, print the associated data.  */
703   switch (token->type)
704     {
705     case CPP_KEYWORD:
706       /* Some keywords have a value that is not an IDENTIFIER_NODE.
707          For example, `struct' is mapped to an INTEGER_CST.  */
708       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
709         break;
710       /* else fall through */
711     case CPP_NAME:
712       fputs (IDENTIFIER_POINTER (token->value), stream);
713       break;
714
715     case CPP_STRING:
716     case CPP_WSTRING:
717     case CPP_PRAGMA:
718       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
719       break;
720
721     default:
722       break;
723     }
724 }
725
726 /* Start emitting debugging information.  */
727
728 static void
729 cp_lexer_start_debugging (cp_lexer* lexer)
730 {
731   lexer->debugging_p = true;
732 }
733
734 /* Stop emitting debugging information.  */
735
736 static void
737 cp_lexer_stop_debugging (cp_lexer* lexer)
738 {
739   lexer->debugging_p = false;
740 }
741
742 #endif /* ENABLE_CHECKING */
743
744 /* Create a new cp_token_cache, representing a range of tokens.  */
745
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
748 {
749   cp_token_cache *cache = GGC_NEW (cp_token_cache);
750   cache->first = first;
751   cache->last = last;
752   return cache;
753 }
754
755 \f
756 /* Decl-specifiers.  */
757
758 static void clear_decl_specs
759   (cp_decl_specifier_seq *);
760
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
762
763 static void
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 {
766   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
767 }
768
769 /* Declarators.  */
770
771 /* Nothing other than the parser should be creating declarators;
772    declarators are a semi-syntactic representation of C++ entities.
773    Other parts of the front end that need to create entities (like
774    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
775
776 static cp_declarator *make_call_declarator
777   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779   (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781   (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783   (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785   (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787   (cp_cv_quals, tree, cp_declarator *);
788
789 cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If non-NULL, the
820    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821    just UNQUALIFIED_NAME.  */
822
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
825 {
826   cp_declarator *declarator;
827
828   /* It is valid to write:
829
830        class C { void f(); };
831        typedef C D;
832        void D::f();
833
834      The standard is not clear about whether `typedef const C D' is
835      legal; as of 2002-09-15 the committee is considering that
836      question.  EDG 3.0 allows that syntax.  Therefore, we do as
837      well.  */
838   if (qualifying_scope && TYPE_P (qualifying_scope))
839     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
840
841   declarator = make_declarator (cdk_id);
842   declarator->u.id.qualifying_scope = qualifying_scope;
843   declarator->u.id.unqualified_name = unqualified_name;
844   declarator->u.id.sfk = sfk_none;
845
846   return declarator;
847 }
848
849 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
850    of modifiers such as const or volatile to apply to the pointer
851    type, represented as identifiers.  */
852
853 cp_declarator *
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
855 {
856   cp_declarator *declarator;
857
858   declarator = make_declarator (cdk_pointer);
859   declarator->declarator = target;
860   declarator->u.pointer.qualifiers = cv_qualifiers;
861   declarator->u.pointer.class_type = NULL_TREE;
862
863   return declarator;
864 }
865
866 /* Like make_pointer_declarator -- but for references.  */
867
868 cp_declarator *
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
870 {
871   cp_declarator *declarator;
872
873   declarator = make_declarator (cdk_reference);
874   declarator->declarator = target;
875   declarator->u.pointer.qualifiers = cv_qualifiers;
876   declarator->u.pointer.class_type = NULL_TREE;
877
878   return declarator;
879 }
880
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882    member of CLASS_TYPE.  */
883
884 cp_declarator *
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886                         cp_declarator *pointee)
887 {
888   cp_declarator *declarator;
889
890   declarator = make_declarator (cdk_ptrmem);
891   declarator->declarator = pointee;
892   declarator->u.pointer.qualifiers = cv_qualifiers;
893   declarator->u.pointer.class_type = class_type;
894
895   return declarator;
896 }
897
898 /* Make a declarator for the function given by TARGET, with the
899    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
900    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
901    indicates what exceptions can be thrown.  */
902
903 cp_declarator *
904 make_call_declarator (cp_declarator *target,
905                       cp_parameter_declarator *parms,
906                       cp_cv_quals cv_qualifiers,
907                       tree exception_specification)
908 {
909   cp_declarator *declarator;
910
911   declarator = make_declarator (cdk_function);
912   declarator->declarator = target;
913   declarator->u.function.parameters = parms;
914   declarator->u.function.qualifiers = cv_qualifiers;
915   declarator->u.function.exception_specification = exception_specification;
916
917   return declarator;
918 }
919
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921    defined by ELEMENT.  */
922
923 cp_declarator *
924 make_array_declarator (cp_declarator *element, tree bounds)
925 {
926   cp_declarator *declarator;
927
928   declarator = make_declarator (cdk_array);
929   declarator->declarator = element;
930   declarator->u.array.bounds = bounds;
931
932   return declarator;
933 }
934
935 cp_parameter_declarator *no_parameters;
936
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938    DECLARATOR and DEFAULT_ARGUMENT.  */
939
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942                            cp_declarator *declarator,
943                            tree default_argument)
944 {
945   cp_parameter_declarator *parameter;
946
947   parameter = ((cp_parameter_declarator *)
948                alloc_declarator (sizeof (cp_parameter_declarator)));
949   parameter->next = NULL;
950   if (decl_specifiers)
951     parameter->decl_specifiers = *decl_specifiers;
952   else
953     clear_decl_specs (&parameter->decl_specifiers);
954   parameter->declarator = declarator;
955   parameter->default_argument = default_argument;
956   parameter->ellipsis_p = false;
957
958   return parameter;
959 }
960
961 /* The parser.  */
962
963 /* Overview
964    --------
965
966    A cp_parser parses the token stream as specified by the C++
967    grammar.  Its job is purely parsing, not semantic analysis.  For
968    example, the parser breaks the token stream into declarators,
969    expressions, statements, and other similar syntactic constructs.
970    It does not check that the types of the expressions on either side
971    of an assignment-statement are compatible, or that a function is
972    not declared with a parameter of type `void'.
973
974    The parser invokes routines elsewhere in the compiler to perform
975    semantic analysis and to build up the abstract syntax tree for the
976    code processed.
977
978    The parser (and the template instantiation code, which is, in a
979    way, a close relative of parsing) are the only parts of the
980    compiler that should be calling push_scope and pop_scope, or
981    related functions.  The parser (and template instantiation code)
982    keeps track of what scope is presently active; everything else
983    should simply honor that.  (The code that generates static
984    initializers may also need to set the scope, in order to check
985    access control correctly when emitting the initializers.)
986
987    Methodology
988    -----------
989
990    The parser is of the standard recursive-descent variety.  Upcoming
991    tokens in the token stream are examined in order to determine which
992    production to use when parsing a non-terminal.  Some C++ constructs
993    require arbitrary look ahead to disambiguate.  For example, it is
994    impossible, in the general case, to tell whether a statement is an
995    expression or declaration without scanning the entire statement.
996    Therefore, the parser is capable of "parsing tentatively."  When the
997    parser is not sure what construct comes next, it enters this mode.
998    Then, while we attempt to parse the construct, the parser queues up
999    error messages, rather than issuing them immediately, and saves the
1000    tokens it consumes.  If the construct is parsed successfully, the
1001    parser "commits", i.e., it issues any queued error messages and
1002    the tokens that were being preserved are permanently discarded.
1003    If, however, the construct is not parsed successfully, the parser
1004    rolls back its state completely so that it can resume parsing using
1005    a different alternative.
1006
1007    Future Improvements
1008    -------------------
1009
1010    The performance of the parser could probably be improved substantially.
1011    We could often eliminate the need to parse tentatively by looking ahead
1012    a little bit.  In some places, this approach might not entirely eliminate
1013    the need to parse tentatively, but it might still speed up the average
1014    case.  */
1015
1016 /* Flags that are passed to some parsing functions.  These values can
1017    be bitwise-ored together.  */
1018
1019 typedef enum cp_parser_flags
1020 {
1021   /* No flags.  */
1022   CP_PARSER_FLAGS_NONE = 0x0,
1023   /* The construct is optional.  If it is not present, then no error
1024      should be issued.  */
1025   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026   /* When parsing a type-specifier, do not allow user-defined types.  */
1027   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1028 } cp_parser_flags;
1029
1030 /* The different kinds of declarators we want to parse.  */
1031
1032 typedef enum cp_parser_declarator_kind
1033 {
1034   /* We want an abstract declarator.  */
1035   CP_PARSER_DECLARATOR_ABSTRACT,
1036   /* We want a named declarator.  */
1037   CP_PARSER_DECLARATOR_NAMED,
1038   /* We don't mind, but the name must be an unqualified-id.  */
1039   CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1041
1042 /* The precedence values used to parse binary expressions.  The minimum value
1043    of PREC must be 1, because zero is reserved to quickly discriminate
1044    binary operators from other tokens.  */
1045
1046 enum cp_parser_prec
1047 {
1048   PREC_NOT_OPERATOR,
1049   PREC_LOGICAL_OR_EXPRESSION,
1050   PREC_LOGICAL_AND_EXPRESSION,
1051   PREC_INCLUSIVE_OR_EXPRESSION,
1052   PREC_EXCLUSIVE_OR_EXPRESSION,
1053   PREC_AND_EXPRESSION,
1054   PREC_EQUALITY_EXPRESSION,
1055   PREC_RELATIONAL_EXPRESSION,
1056   PREC_SHIFT_EXPRESSION,
1057   PREC_ADDITIVE_EXPRESSION,
1058   PREC_MULTIPLICATIVE_EXPRESSION,
1059   PREC_PM_EXPRESSION,
1060   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1061 };
1062
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064    precedence value.  */
1065
1066 typedef struct cp_parser_binary_operations_map_node
1067 {
1068   /* The token type.  */
1069   enum cpp_ttype token_type;
1070   /* The corresponding tree code.  */
1071   enum tree_code tree_type;
1072   /* The precedence of this operator.  */
1073   enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1075
1076 /* The status of a tentative parse.  */
1077
1078 typedef enum cp_parser_status_kind
1079 {
1080   /* No errors have occurred.  */
1081   CP_PARSER_STATUS_KIND_NO_ERROR,
1082   /* An error has occurred.  */
1083   CP_PARSER_STATUS_KIND_ERROR,
1084   /* We are committed to this tentative parse, whether or not an error
1085      has occurred.  */
1086   CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1088
1089 typedef struct cp_parser_expression_stack_entry
1090 {
1091   tree lhs;
1092   enum tree_code tree_type;
1093   int prec;
1094 } cp_parser_expression_stack_entry;
1095
1096 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1097    entries because precedence levels on the stack are monotonically
1098    increasing.  */
1099 typedef struct cp_parser_expression_stack_entry
1100   cp_parser_expression_stack[NUM_PREC_VALUES];
1101
1102 /* Context that is saved and restored when parsing tentatively.  */
1103 typedef struct cp_parser_context GTY (())
1104 {
1105   /* If this is a tentative parsing context, the status of the
1106      tentative parse.  */
1107   enum cp_parser_status_kind status;
1108   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1109      that are looked up in this context must be looked up both in the
1110      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111      the context of the containing expression.  */
1112   tree object_type;
1113
1114   /* The next parsing context in the stack.  */
1115   struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes.  */
1119
1120 /* Constructors and destructors.  */
1121
1122 static cp_parser_context *cp_parser_context_new
1123   (cp_parser_context *);
1124
1125 /* Class variables.  */
1126
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130    Transformed into an associative array (binops_by_token) by
1131    cp_parser_new.  */
1132
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1136
1137   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140
1141   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143
1144   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146
1147   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1153
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.
1221
1222      This value is not cleared automatically after a name is looked
1223      up, so we must be careful to clear it before starting a new look
1224      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1225      will look up `Z' in the scope of `X', rather than the current
1226      scope.)  Unfortunately, it is difficult to tell when name lookup
1227      is complete, because we sometimes peek at a token, look it up,
1228      and then decide not to consume it.  */
1229   tree scope;
1230
1231   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1232      last lookup took place.  OBJECT_SCOPE is used if an expression
1233      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1234      respectively.  QUALIFYING_SCOPE is used for an expression of the
1235      form "X::Y"; it refers to X.  */
1236   tree object_scope;
1237   tree qualifying_scope;
1238
1239   /* A stack of parsing contexts.  All but the bottom entry on the
1240      stack will be tentative contexts.
1241
1242      We parse tentatively in order to determine which construct is in
1243      use in some situations.  For example, in order to determine
1244      whether a statement is an expression-statement or a
1245      declaration-statement we parse it tentatively as a
1246      declaration-statement.  If that fails, we then reparse the same
1247      token stream as an expression-statement.  */
1248   cp_parser_context *context;
1249
1250   /* True if we are parsing GNU C++.  If this flag is not set, then
1251      GNU extensions are not recognized.  */
1252   bool allow_gnu_extensions_p;
1253
1254   /* TRUE if the `>' token should be interpreted as the greater-than
1255      operator.  FALSE if it is the end of a template-id or
1256      template-parameter-list.  */
1257   bool greater_than_is_operator_p;
1258
1259   /* TRUE if default arguments are allowed within a parameter list
1260      that starts at this point. FALSE if only a gnu extension makes
1261      them permissible.  */
1262   bool default_arg_ok_p;
1263
1264   /* TRUE if we are parsing an integral constant-expression.  See
1265      [expr.const] for a precise definition.  */
1266   bool integral_constant_expression_p;
1267
1268   /* TRUE if we are parsing an integral constant-expression -- but a
1269      non-constant expression should be permitted as well.  This flag
1270      is used when parsing an array bound so that GNU variable-length
1271      arrays are tolerated.  */
1272   bool allow_non_integral_constant_expression_p;
1273
1274   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1275      been seen that makes the expression non-constant.  */
1276   bool non_integral_constant_expression_p;
1277
1278   /* TRUE if local variable names and `this' are forbidden in the
1279      current context.  */
1280   bool local_variables_forbidden_p;
1281
1282   /* TRUE if the declaration we are parsing is part of a
1283      linkage-specification of the form `extern string-literal
1284      declaration'.  */
1285   bool in_unbraced_linkage_specification_p;
1286
1287   /* TRUE if we are presently parsing a declarator, after the
1288      direct-declarator.  */
1289   bool in_declarator_p;
1290
1291   /* TRUE if we are presently parsing a template-argument-list.  */
1292   bool in_template_argument_list_p;
1293
1294   /* TRUE if we are presently parsing the body of an
1295      iteration-statement.  */
1296   bool in_iteration_statement_p;
1297
1298   /* TRUE if we are presently parsing the body of a switch
1299      statement.  */
1300   bool in_switch_statement_p;
1301
1302   /* TRUE if we are parsing a type-id in an expression context.  In
1303      such a situation, both "type (expr)" and "type (type)" are valid
1304      alternatives.  */
1305   bool in_type_id_in_expr_p;
1306
1307   /* TRUE if we are currently in a header file where declarations are
1308      implicitly extern "C".  */
1309   bool implicit_extern_c;
1310
1311   /* TRUE if strings in expressions should be translated to the execution
1312      character set.  */
1313   bool translate_strings_p;
1314
1315   /* If non-NULL, then we are parsing a construct where new type
1316      definitions are not permitted.  The string stored here will be
1317      issued as an error message if a type is defined.  */
1318   const char *type_definition_forbidden_message;
1319
1320   /* A list of lists. The outer list is a stack, used for member
1321      functions of local classes. At each level there are two sub-list,
1322      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1323      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1324      TREE_VALUE's. The functions are chained in reverse declaration
1325      order.
1326
1327      The TREE_PURPOSE sublist contains those functions with default
1328      arguments that need post processing, and the TREE_VALUE sublist
1329      contains those functions with definitions that need post
1330      processing.
1331
1332      These lists can only be processed once the outermost class being
1333      defined is complete.  */
1334   tree unparsed_functions_queues;
1335
1336   /* The number of classes whose definitions are currently in
1337      progress.  */
1338   unsigned num_classes_being_defined;
1339
1340   /* The number of template parameter lists that apply directly to the
1341      current declaration.  */
1342   unsigned num_template_parameter_lists;
1343 } cp_parser;
1344
1345 /* The type of a function that parses some kind of expression.  */
1346 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1347
1348 /* Prototypes.  */
1349
1350 /* Constructors and destructors.  */
1351
1352 static cp_parser *cp_parser_new
1353   (void);
1354
1355 /* Routines to parse various constructs.
1356
1357    Those that return `tree' will return the error_mark_node (rather
1358    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1359    Sometimes, they will return an ordinary node if error-recovery was
1360    attempted, even though a parse error occurred.  So, to check
1361    whether or not a parse error occurred, you should always use
1362    cp_parser_error_occurred.  If the construct is optional (indicated
1363    either by an `_opt' in the name of the function that does the
1364    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1365    the construct is not present.  */
1366
1367 /* Lexical conventions [gram.lex]  */
1368
1369 static tree cp_parser_identifier
1370   (cp_parser *);
1371 static tree cp_parser_string_literal
1372   (cp_parser *, bool, bool);
1373
1374 /* Basic concepts [gram.basic]  */
1375
1376 static bool cp_parser_translation_unit
1377   (cp_parser *);
1378
1379 /* Expressions [gram.expr]  */
1380
1381 static tree cp_parser_primary_expression
1382   (cp_parser *, bool, cp_id_kind *, tree *);
1383 static tree cp_parser_id_expression
1384   (cp_parser *, bool, bool, bool *, bool);
1385 static tree cp_parser_unqualified_id
1386   (cp_parser *, bool, bool, bool);
1387 static tree cp_parser_nested_name_specifier_opt
1388   (cp_parser *, bool, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier
1390   (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_class_or_namespace_name
1392   (cp_parser *, bool, bool, bool, bool, bool);
1393 static tree cp_parser_postfix_expression
1394   (cp_parser *, bool, bool);
1395 static tree cp_parser_postfix_open_square_expression
1396   (cp_parser *, tree, bool);
1397 static tree cp_parser_postfix_dot_deref_expression
1398   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1399 static tree cp_parser_parenthesized_expression_list
1400   (cp_parser *, bool, bool, bool *);
1401 static void cp_parser_pseudo_destructor_name
1402   (cp_parser *, tree *, tree *);
1403 static tree cp_parser_unary_expression
1404   (cp_parser *, bool, bool);
1405 static enum tree_code cp_parser_unary_operator
1406   (cp_token *);
1407 static tree cp_parser_new_expression
1408   (cp_parser *);
1409 static tree cp_parser_new_placement
1410   (cp_parser *);
1411 static tree cp_parser_new_type_id
1412   (cp_parser *, tree *);
1413 static cp_declarator *cp_parser_new_declarator_opt
1414   (cp_parser *);
1415 static cp_declarator *cp_parser_direct_new_declarator
1416   (cp_parser *);
1417 static tree cp_parser_new_initializer
1418   (cp_parser *);
1419 static tree cp_parser_delete_expression
1420   (cp_parser *);
1421 static tree cp_parser_cast_expression
1422   (cp_parser *, bool, bool);
1423 static tree cp_parser_binary_expression
1424   (cp_parser *, bool);
1425 static tree cp_parser_question_colon_clause
1426   (cp_parser *, tree);
1427 static tree cp_parser_assignment_expression
1428   (cp_parser *, bool);
1429 static enum tree_code cp_parser_assignment_operator_opt
1430   (cp_parser *);
1431 static tree cp_parser_expression
1432   (cp_parser *, bool);
1433 static tree cp_parser_constant_expression
1434   (cp_parser *, bool, bool *);
1435 static tree cp_parser_builtin_offsetof
1436   (cp_parser *);
1437
1438 /* Statements [gram.stmt.stmt]  */
1439
1440 static void cp_parser_statement
1441   (cp_parser *, tree);
1442 static tree cp_parser_labeled_statement
1443   (cp_parser *, tree);
1444 static tree cp_parser_expression_statement
1445   (cp_parser *, tree);
1446 static tree cp_parser_compound_statement
1447   (cp_parser *, tree, bool);
1448 static void cp_parser_statement_seq_opt
1449   (cp_parser *, tree);
1450 static tree cp_parser_selection_statement
1451   (cp_parser *);
1452 static tree cp_parser_condition
1453   (cp_parser *);
1454 static tree cp_parser_iteration_statement
1455   (cp_parser *);
1456 static void cp_parser_for_init_statement
1457   (cp_parser *);
1458 static tree cp_parser_jump_statement
1459   (cp_parser *);
1460 static void cp_parser_declaration_statement
1461   (cp_parser *);
1462
1463 static tree cp_parser_implicitly_scoped_statement
1464   (cp_parser *);
1465 static void cp_parser_already_scoped_statement
1466   (cp_parser *);
1467
1468 /* Declarations [gram.dcl.dcl] */
1469
1470 static void cp_parser_declaration_seq_opt
1471   (cp_parser *);
1472 static void cp_parser_declaration
1473   (cp_parser *);
1474 static void cp_parser_block_declaration
1475   (cp_parser *, bool);
1476 static void cp_parser_simple_declaration
1477   (cp_parser *, bool);
1478 static void cp_parser_decl_specifier_seq
1479   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1480 static tree cp_parser_storage_class_specifier_opt
1481   (cp_parser *);
1482 static tree cp_parser_function_specifier_opt
1483   (cp_parser *, cp_decl_specifier_seq *);
1484 static tree cp_parser_type_specifier
1485   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1486    int *, bool *);
1487 static tree cp_parser_simple_type_specifier
1488   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1489 static tree cp_parser_type_name
1490   (cp_parser *);
1491 static tree cp_parser_elaborated_type_specifier
1492   (cp_parser *, bool, bool);
1493 static tree cp_parser_enum_specifier
1494   (cp_parser *);
1495 static void cp_parser_enumerator_list
1496   (cp_parser *, tree);
1497 static void cp_parser_enumerator_definition
1498   (cp_parser *, tree);
1499 static tree cp_parser_namespace_name
1500   (cp_parser *);
1501 static void cp_parser_namespace_definition
1502   (cp_parser *);
1503 static void cp_parser_namespace_body
1504   (cp_parser *);
1505 static tree cp_parser_qualified_namespace_specifier
1506   (cp_parser *);
1507 static void cp_parser_namespace_alias_definition
1508   (cp_parser *);
1509 static void cp_parser_using_declaration
1510   (cp_parser *);
1511 static void cp_parser_using_directive
1512   (cp_parser *);
1513 static void cp_parser_asm_definition
1514   (cp_parser *);
1515 static void cp_parser_linkage_specification
1516   (cp_parser *);
1517
1518 /* Declarators [gram.dcl.decl] */
1519
1520 static tree cp_parser_init_declarator
1521   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1522 static cp_declarator *cp_parser_declarator
1523   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1524 static cp_declarator *cp_parser_direct_declarator
1525   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1526 static enum tree_code cp_parser_ptr_operator
1527   (cp_parser *, tree *, cp_cv_quals *);
1528 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1529   (cp_parser *);
1530 static tree cp_parser_declarator_id
1531   (cp_parser *);
1532 static tree cp_parser_type_id
1533   (cp_parser *);
1534 static void cp_parser_type_specifier_seq
1535   (cp_parser *, bool, cp_decl_specifier_seq *);
1536 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1537   (cp_parser *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1539   (cp_parser *, bool *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration
1541   (cp_parser *, bool, bool *);
1542 static void cp_parser_function_body
1543   (cp_parser *);
1544 static tree cp_parser_initializer
1545   (cp_parser *, bool *, bool *);
1546 static tree cp_parser_initializer_clause
1547   (cp_parser *, bool *);
1548 static tree cp_parser_initializer_list
1549   (cp_parser *, bool *);
1550
1551 static bool cp_parser_ctor_initializer_opt_and_function_body
1552   (cp_parser *);
1553
1554 /* Classes [gram.class] */
1555
1556 static tree cp_parser_class_name
1557   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1558 static tree cp_parser_class_specifier
1559   (cp_parser *);
1560 static tree cp_parser_class_head
1561   (cp_parser *, bool *, tree *);
1562 static enum tag_types cp_parser_class_key
1563   (cp_parser *);
1564 static void cp_parser_member_specification_opt
1565   (cp_parser *);
1566 static void cp_parser_member_declaration
1567   (cp_parser *);
1568 static tree cp_parser_pure_specifier
1569   (cp_parser *);
1570 static tree cp_parser_constant_initializer
1571   (cp_parser *);
1572
1573 /* Derived classes [gram.class.derived] */
1574
1575 static tree cp_parser_base_clause
1576   (cp_parser *);
1577 static tree cp_parser_base_specifier
1578   (cp_parser *);
1579
1580 /* Special member functions [gram.special] */
1581
1582 static tree cp_parser_conversion_function_id
1583   (cp_parser *);
1584 static tree cp_parser_conversion_type_id
1585   (cp_parser *);
1586 static cp_declarator *cp_parser_conversion_declarator_opt
1587   (cp_parser *);
1588 static bool cp_parser_ctor_initializer_opt
1589   (cp_parser *);
1590 static void cp_parser_mem_initializer_list
1591   (cp_parser *);
1592 static tree cp_parser_mem_initializer
1593   (cp_parser *);
1594 static tree cp_parser_mem_initializer_id
1595   (cp_parser *);
1596
1597 /* Overloading [gram.over] */
1598
1599 static tree cp_parser_operator_function_id
1600   (cp_parser *);
1601 static tree cp_parser_operator
1602   (cp_parser *);
1603
1604 /* Templates [gram.temp] */
1605
1606 static void cp_parser_template_declaration
1607   (cp_parser *, bool);
1608 static tree cp_parser_template_parameter_list
1609   (cp_parser *);
1610 static tree cp_parser_template_parameter
1611   (cp_parser *, bool *);
1612 static tree cp_parser_type_parameter
1613   (cp_parser *);
1614 static tree cp_parser_template_id
1615   (cp_parser *, bool, bool, bool);
1616 static tree cp_parser_template_name
1617   (cp_parser *, bool, bool, bool, bool *);
1618 static tree cp_parser_template_argument_list
1619   (cp_parser *);
1620 static tree cp_parser_template_argument
1621   (cp_parser *);
1622 static void cp_parser_explicit_instantiation
1623   (cp_parser *);
1624 static void cp_parser_explicit_specialization
1625   (cp_parser *);
1626
1627 /* Exception handling [gram.exception] */
1628
1629 static tree cp_parser_try_block
1630   (cp_parser *);
1631 static bool cp_parser_function_try_block
1632   (cp_parser *);
1633 static void cp_parser_handler_seq
1634   (cp_parser *);
1635 static void cp_parser_handler
1636   (cp_parser *);
1637 static tree cp_parser_exception_declaration
1638   (cp_parser *);
1639 static tree cp_parser_throw_expression
1640   (cp_parser *);
1641 static tree cp_parser_exception_specification_opt
1642   (cp_parser *);
1643 static tree cp_parser_type_id_list
1644   (cp_parser *);
1645
1646 /* GNU Extensions */
1647
1648 static tree cp_parser_asm_specification_opt
1649   (cp_parser *);
1650 static tree cp_parser_asm_operand_list
1651   (cp_parser *);
1652 static tree cp_parser_asm_clobber_list
1653   (cp_parser *);
1654 static tree cp_parser_attributes_opt
1655   (cp_parser *);
1656 static tree cp_parser_attribute_list
1657   (cp_parser *);
1658 static bool cp_parser_extension_opt
1659   (cp_parser *, int *);
1660 static void cp_parser_label_declaration
1661   (cp_parser *);
1662
1663 /* Objective-C++ Productions */
1664
1665 static tree cp_parser_objc_message_receiver
1666   (cp_parser *);
1667 static tree cp_parser_objc_message_args
1668   (cp_parser *);
1669 static tree cp_parser_objc_message_expression
1670   (cp_parser *);
1671 static tree cp_parser_objc_encode_expression
1672   (cp_parser *);
1673 static tree cp_parser_objc_defs_expression 
1674   (cp_parser *);
1675 static tree cp_parser_objc_protocol_expression
1676   (cp_parser *);
1677 static tree cp_parser_objc_selector_expression
1678   (cp_parser *);
1679 static tree cp_parser_objc_expression
1680   (cp_parser *);
1681 static bool cp_parser_objc_selector_p
1682   (enum cpp_ttype);
1683 static tree cp_parser_objc_selector
1684   (cp_parser *);
1685 static tree cp_parser_objc_protocol_refs_opt
1686   (cp_parser *);
1687 static void cp_parser_objc_declaration
1688   (cp_parser *);
1689 static tree cp_parser_objc_statement
1690   (cp_parser *);
1691
1692 /* Utility Routines */
1693
1694 static tree cp_parser_lookup_name
1695   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1696 static tree cp_parser_lookup_name_simple
1697   (cp_parser *, tree);
1698 static tree cp_parser_maybe_treat_template_as_class
1699   (tree, bool);
1700 static bool cp_parser_check_declarator_template_parameters
1701   (cp_parser *, cp_declarator *);
1702 static bool cp_parser_check_template_parameters
1703   (cp_parser *, unsigned);
1704 static tree cp_parser_simple_cast_expression
1705   (cp_parser *);
1706 static tree cp_parser_global_scope_opt
1707   (cp_parser *, bool);
1708 static bool cp_parser_constructor_declarator_p
1709   (cp_parser *, bool);
1710 static tree cp_parser_function_definition_from_specifiers_and_declarator
1711   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1712 static tree cp_parser_function_definition_after_declarator
1713   (cp_parser *, bool);
1714 static void cp_parser_template_declaration_after_export
1715   (cp_parser *, bool);
1716 static tree cp_parser_single_declaration
1717   (cp_parser *, bool, bool *);
1718 static tree cp_parser_functional_cast
1719   (cp_parser *, tree);
1720 static tree cp_parser_save_member_function_body
1721   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1722 static tree cp_parser_enclosed_template_argument_list
1723   (cp_parser *);
1724 static void cp_parser_save_default_args
1725   (cp_parser *, tree);
1726 static void cp_parser_late_parsing_for_member
1727   (cp_parser *, tree);
1728 static void cp_parser_late_parsing_default_args
1729   (cp_parser *, tree);
1730 static tree cp_parser_sizeof_operand
1731   (cp_parser *, enum rid);
1732 static bool cp_parser_declares_only_class_p
1733   (cp_parser *);
1734 static void cp_parser_set_storage_class
1735   (cp_decl_specifier_seq *, cp_storage_class);
1736 static void cp_parser_set_decl_spec_type
1737   (cp_decl_specifier_seq *, tree, bool);
1738 static bool cp_parser_friend_p
1739   (const cp_decl_specifier_seq *);
1740 static cp_token *cp_parser_require
1741   (cp_parser *, enum cpp_ttype, const char *);
1742 static cp_token *cp_parser_require_keyword
1743   (cp_parser *, enum rid, const char *);
1744 static bool cp_parser_token_starts_function_definition_p
1745   (cp_token *);
1746 static bool cp_parser_next_token_starts_class_definition_p
1747   (cp_parser *);
1748 static bool cp_parser_next_token_ends_template_argument_p
1749   (cp_parser *);
1750 static bool cp_parser_nth_token_starts_template_argument_list_p
1751   (cp_parser *, size_t);
1752 static enum tag_types cp_parser_token_is_class_key
1753   (cp_token *);
1754 static void cp_parser_check_class_key
1755   (enum tag_types, tree type);
1756 static void cp_parser_check_access_in_redeclaration
1757   (tree type);
1758 static bool cp_parser_optional_template_keyword
1759   (cp_parser *);
1760 static void cp_parser_pre_parsed_nested_name_specifier
1761   (cp_parser *);
1762 static void cp_parser_cache_group
1763   (cp_parser *, enum cpp_ttype, unsigned);
1764 static void cp_parser_parse_tentatively
1765   (cp_parser *);
1766 static void cp_parser_commit_to_tentative_parse
1767   (cp_parser *);
1768 static void cp_parser_abort_tentative_parse
1769   (cp_parser *);
1770 static bool cp_parser_parse_definitely
1771   (cp_parser *);
1772 static inline bool cp_parser_parsing_tentatively
1773   (cp_parser *);
1774 static bool cp_parser_uncommitted_to_tentative_parse_p
1775   (cp_parser *);
1776 static void cp_parser_error
1777   (cp_parser *, const char *);
1778 static void cp_parser_name_lookup_error
1779   (cp_parser *, tree, tree, const char *);
1780 static bool cp_parser_simulate_error
1781   (cp_parser *);
1782 static void cp_parser_check_type_definition
1783   (cp_parser *);
1784 static void cp_parser_check_for_definition_in_return_type
1785   (cp_declarator *, tree);
1786 static void cp_parser_check_for_invalid_template_id
1787   (cp_parser *, tree);
1788 static bool cp_parser_non_integral_constant_expression
1789   (cp_parser *, const char *);
1790 static void cp_parser_diagnose_invalid_type_name
1791   (cp_parser *, tree, tree);
1792 static bool cp_parser_parse_and_diagnose_invalid_type_name
1793   (cp_parser *);
1794 static int cp_parser_skip_to_closing_parenthesis
1795   (cp_parser *, bool, bool, bool);
1796 static void cp_parser_skip_to_end_of_statement
1797   (cp_parser *);
1798 static void cp_parser_consume_semicolon_at_end_of_statement
1799   (cp_parser *);
1800 static void cp_parser_skip_to_end_of_block_or_statement
1801   (cp_parser *);
1802 static void cp_parser_skip_to_closing_brace
1803   (cp_parser *);
1804 static void cp_parser_skip_until_found
1805   (cp_parser *, enum cpp_ttype, const char *);
1806 static bool cp_parser_error_occurred
1807   (cp_parser *);
1808 static bool cp_parser_allow_gnu_extensions_p
1809   (cp_parser *);
1810 static bool cp_parser_is_string_literal
1811   (cp_token *);
1812 static bool cp_parser_is_keyword
1813   (cp_token *, enum rid);
1814 static tree cp_parser_make_typename_type
1815   (cp_parser *, tree, tree);
1816
1817 /* Returns nonzero if we are parsing tentatively.  */
1818
1819 static inline bool
1820 cp_parser_parsing_tentatively (cp_parser* parser)
1821 {
1822   return parser->context->next != NULL;
1823 }
1824
1825 /* Returns nonzero if TOKEN is a string literal.  */
1826
1827 static bool
1828 cp_parser_is_string_literal (cp_token* token)
1829 {
1830   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1831 }
1832
1833 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1834
1835 static bool
1836 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1837 {
1838   return token->keyword == keyword;
1839 }
1840
1841 /* A minimum or maximum operator has been seen.  As these are
1842    deprecated, issue a warning.  */
1843
1844 static inline void
1845 cp_parser_warn_min_max (void)
1846 {
1847   if (warn_deprecated && !in_system_header)
1848     warning (0, "minimum/maximum operators are deprecated");
1849 }
1850
1851 /* If not parsing tentatively, issue a diagnostic of the form
1852       FILE:LINE: MESSAGE before TOKEN
1853    where TOKEN is the next token in the input stream.  MESSAGE
1854    (specified by the caller) is usually of the form "expected
1855    OTHER-TOKEN".  */
1856
1857 static void
1858 cp_parser_error (cp_parser* parser, const char* message)
1859 {
1860   if (!cp_parser_simulate_error (parser))
1861     {
1862       cp_token *token = cp_lexer_peek_token (parser->lexer);
1863       /* This diagnostic makes more sense if it is tagged to the line
1864          of the token we just peeked at.  */
1865       cp_lexer_set_source_position_from_token (token);
1866       if (token->type == CPP_PRAGMA)
1867         {
1868           error ("%<#pragma%> is not allowed here"); 
1869           cp_lexer_purge_token (parser->lexer);
1870           return;
1871         }
1872       c_parse_error (message,
1873                      /* Because c_parser_error does not understand
1874                         CPP_KEYWORD, keywords are treated like
1875                         identifiers.  */
1876                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1877                      token->value);
1878     }
1879 }
1880
1881 /* Issue an error about name-lookup failing.  NAME is the
1882    IDENTIFIER_NODE DECL is the result of
1883    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1884    the thing that we hoped to find.  */
1885
1886 static void
1887 cp_parser_name_lookup_error (cp_parser* parser,
1888                              tree name,
1889                              tree decl,
1890                              const char* desired)
1891 {
1892   /* If name lookup completely failed, tell the user that NAME was not
1893      declared.  */
1894   if (decl == error_mark_node)
1895     {
1896       if (parser->scope && parser->scope != global_namespace)
1897         error ("%<%D::%D%> has not been declared",
1898                parser->scope, name);
1899       else if (parser->scope == global_namespace)
1900         error ("%<::%D%> has not been declared", name);
1901       else if (parser->object_scope 
1902                && !CLASS_TYPE_P (parser->object_scope))
1903         error ("request for member %qD in non-class type %qT",
1904                name, parser->object_scope);
1905       else if (parser->object_scope)
1906         error ("%<%T::%D%> has not been declared", 
1907                parser->object_scope, name);
1908       else
1909         error ("%qD has not been declared", name);
1910     }
1911   else if (parser->scope && parser->scope != global_namespace)
1912     error ("%<%D::%D%> %s", parser->scope, name, desired);
1913   else if (parser->scope == global_namespace)
1914     error ("%<::%D%> %s", name, desired);
1915   else
1916     error ("%qD %s", name, desired);
1917 }
1918
1919 /* If we are parsing tentatively, remember that an error has occurred
1920    during this tentative parse.  Returns true if the error was
1921    simulated; false if a message should be issued by the caller.  */
1922
1923 static bool
1924 cp_parser_simulate_error (cp_parser* parser)
1925 {
1926   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1927     {
1928       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1929       return true;
1930     }
1931   return false;
1932 }
1933
1934 /* This function is called when a type is defined.  If type
1935    definitions are forbidden at this point, an error message is
1936    issued.  */
1937
1938 static void
1939 cp_parser_check_type_definition (cp_parser* parser)
1940 {
1941   /* If types are forbidden here, issue a message.  */
1942   if (parser->type_definition_forbidden_message)
1943     /* Use `%s' to print the string in case there are any escape
1944        characters in the message.  */
1945     error ("%s", parser->type_definition_forbidden_message);
1946 }
1947
1948 /* This function is called when the DECLARATOR is processed.  The TYPE
1949    was a type defined in the decl-specifiers.  If it is invalid to
1950    define a type in the decl-specifiers for DECLARATOR, an error is
1951    issued.  */
1952
1953 static void
1954 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1955                                                tree type)
1956 {
1957   /* [dcl.fct] forbids type definitions in return types.
1958      Unfortunately, it's not easy to know whether or not we are
1959      processing a return type until after the fact.  */
1960   while (declarator
1961          && (declarator->kind == cdk_pointer
1962              || declarator->kind == cdk_reference
1963              || declarator->kind == cdk_ptrmem))
1964     declarator = declarator->declarator;
1965   if (declarator
1966       && declarator->kind == cdk_function)
1967     {
1968       error ("new types may not be defined in a return type");
1969       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1970               type);
1971     }
1972 }
1973
1974 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1975    "<" in any valid C++ program.  If the next token is indeed "<",
1976    issue a message warning the user about what appears to be an
1977    invalid attempt to form a template-id.  */
1978
1979 static void
1980 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1981                                          tree type)
1982 {
1983   cp_token_position start = 0;
1984
1985   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1986     {
1987       if (TYPE_P (type))
1988         error ("%qT is not a template", type);
1989       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1990         error ("%qE is not a template", type);
1991       else
1992         error ("invalid template-id");
1993       /* Remember the location of the invalid "<".  */
1994       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1995         start = cp_lexer_token_position (parser->lexer, true);
1996       /* Consume the "<".  */
1997       cp_lexer_consume_token (parser->lexer);
1998       /* Parse the template arguments.  */
1999       cp_parser_enclosed_template_argument_list (parser);
2000       /* Permanently remove the invalid template arguments so that
2001          this error message is not issued again.  */
2002       if (start)
2003         cp_lexer_purge_tokens_after (parser->lexer, start);
2004     }
2005 }
2006
2007 /* If parsing an integral constant-expression, issue an error message
2008    about the fact that THING appeared and return true.  Otherwise,
2009    return false.  In either case, set
2010    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */ 
2011
2012 static bool
2013 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2014                                             const char *thing)
2015 {
2016   parser->non_integral_constant_expression_p = true;
2017   if (parser->integral_constant_expression_p)
2018     {
2019       if (!parser->allow_non_integral_constant_expression_p)
2020         {
2021           error ("%s cannot appear in a constant-expression", thing);
2022           return true;
2023         }
2024     }
2025   return false;
2026 }
2027
2028 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2029    qualifying scope (or NULL, if none) for ID.  This function commits
2030    to the current active tentative parse, if any.  (Otherwise, the
2031    problematic construct might be encountered again later, resulting
2032    in duplicate error messages.)  */
2033
2034 static void
2035 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2036 {
2037   tree decl, old_scope;
2038   /* Try to lookup the identifier.  */
2039   old_scope = parser->scope;
2040   parser->scope = scope;
2041   decl = cp_parser_lookup_name_simple (parser, id);
2042   parser->scope = old_scope;
2043   /* If the lookup found a template-name, it means that the user forgot
2044   to specify an argument list. Emit an useful error message.  */
2045   if (TREE_CODE (decl) == TEMPLATE_DECL)
2046     error ("invalid use of template-name %qE without an argument list",
2047       decl);
2048   else if (!parser->scope)
2049     {
2050       /* Issue an error message.  */
2051       error ("%qE does not name a type", id);
2052       /* If we're in a template class, it's possible that the user was
2053          referring to a type from a base class.  For example:
2054
2055            template <typename T> struct A { typedef T X; };
2056            template <typename T> struct B : public A<T> { X x; };
2057
2058          The user should have said "typename A<T>::X".  */
2059       if (processing_template_decl && current_class_type
2060           && TYPE_BINFO (current_class_type))
2061         {
2062           tree b;
2063
2064           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2065                b;
2066                b = TREE_CHAIN (b))
2067             {
2068               tree base_type = BINFO_TYPE (b);
2069               if (CLASS_TYPE_P (base_type)
2070                   && dependent_type_p (base_type))
2071                 {
2072                   tree field;
2073                   /* Go from a particular instantiation of the
2074                      template (which will have an empty TYPE_FIELDs),
2075                      to the main version.  */
2076                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2077                   for (field = TYPE_FIELDS (base_type);
2078                        field;
2079                        field = TREE_CHAIN (field))
2080                     if (TREE_CODE (field) == TYPE_DECL
2081                         && DECL_NAME (field) == id)
2082                       {
2083                         inform ("(perhaps %<typename %T::%E%> was intended)",
2084                                 BINFO_TYPE (b), id);
2085                         break;
2086                       }
2087                   if (field)
2088                     break;
2089                 }
2090             }
2091         }
2092     }
2093   /* Here we diagnose qualified-ids where the scope is actually correct,
2094      but the identifier does not resolve to a valid type name.  */
2095   else
2096     {
2097       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2098         error ("%qE in namespace %qE does not name a type",
2099                id, parser->scope);
2100       else if (TYPE_P (parser->scope))
2101         error ("%qE in class %qT does not name a type", id, parser->scope);
2102       else
2103         gcc_unreachable ();
2104     }
2105   cp_parser_commit_to_tentative_parse (parser);
2106 }
2107
2108 /* Check for a common situation where a type-name should be present,
2109    but is not, and issue a sensible error message.  Returns true if an
2110    invalid type-name was detected.
2111
2112    The situation handled by this function are variable declarations of the
2113    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2114    Usually, `ID' should name a type, but if we got here it means that it
2115    does not. We try to emit the best possible error message depending on
2116    how exactly the id-expression looks like.
2117 */
2118
2119 static bool
2120 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2121 {
2122   tree id;
2123
2124   cp_parser_parse_tentatively (parser);
2125   id = cp_parser_id_expression (parser,
2126                                 /*template_keyword_p=*/false,
2127                                 /*check_dependency_p=*/true,
2128                                 /*template_p=*/NULL,
2129                                 /*declarator_p=*/true);
2130   /* After the id-expression, there should be a plain identifier,
2131      otherwise this is not a simple variable declaration. Also, if
2132      the scope is dependent, we cannot do much.  */
2133   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2134       || (parser->scope && TYPE_P (parser->scope)
2135           && dependent_type_p (parser->scope)))
2136     {
2137       cp_parser_abort_tentative_parse (parser);
2138       return false;
2139     }
2140   if (!cp_parser_parse_definitely (parser)
2141       || TREE_CODE (id) != IDENTIFIER_NODE)
2142     return false;
2143
2144   /* Emit a diagnostic for the invalid type.  */
2145   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2146   /* Skip to the end of the declaration; there's no point in
2147      trying to process it.  */
2148   cp_parser_skip_to_end_of_block_or_statement (parser);
2149   return true;
2150 }
2151
2152 /* Consume tokens up to, and including, the next non-nested closing `)'.
2153    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2154    are doing error recovery. Returns -1 if OR_COMMA is true and we
2155    found an unnested comma.  */
2156
2157 static int
2158 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2159                                        bool recovering,
2160                                        bool or_comma,
2161                                        bool consume_paren)
2162 {
2163   unsigned paren_depth = 0;
2164   unsigned brace_depth = 0;
2165   int result;
2166
2167   if (recovering && !or_comma
2168       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2169     return 0;
2170
2171   while (true)
2172     {
2173       cp_token *token;
2174
2175       /* If we've run out of tokens, then there is no closing `)'.  */
2176       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2177         {
2178           result = 0;
2179           break;
2180         }
2181
2182       token = cp_lexer_peek_token (parser->lexer);
2183
2184       /* This matches the processing in skip_to_end_of_statement.  */
2185       if (token->type == CPP_SEMICOLON && !brace_depth)
2186         {
2187           result = 0;
2188           break;
2189         }
2190       if (token->type == CPP_OPEN_BRACE)
2191         ++brace_depth;
2192       if (token->type == CPP_CLOSE_BRACE)
2193         {
2194           if (!brace_depth--)
2195             {
2196               result = 0;
2197               break;
2198             }
2199         }
2200       if (recovering && or_comma && token->type == CPP_COMMA
2201           && !brace_depth && !paren_depth)
2202         {
2203           result = -1;
2204           break;
2205         }
2206
2207       if (!brace_depth)
2208         {
2209           /* If it is an `(', we have entered another level of nesting.  */
2210           if (token->type == CPP_OPEN_PAREN)
2211             ++paren_depth;
2212           /* If it is a `)', then we might be done.  */
2213           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2214             {
2215               if (consume_paren)
2216                 cp_lexer_consume_token (parser->lexer);
2217               {
2218                 result = 1;
2219                 break;
2220               }
2221             }
2222         }
2223
2224       /* Consume the token.  */
2225       cp_lexer_consume_token (parser->lexer);
2226     }
2227
2228   return result;
2229 }
2230
2231 /* Consume tokens until we reach the end of the current statement.
2232    Normally, that will be just before consuming a `;'.  However, if a
2233    non-nested `}' comes first, then we stop before consuming that.  */
2234
2235 static void
2236 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2237 {
2238   unsigned nesting_depth = 0;
2239
2240   while (true)
2241     {
2242       cp_token *token;
2243
2244       /* Peek at the next token.  */
2245       token = cp_lexer_peek_token (parser->lexer);
2246       /* If we've run out of tokens, stop.  */
2247       if (token->type == CPP_EOF)
2248         break;
2249       /* If the next token is a `;', we have reached the end of the
2250          statement.  */
2251       if (token->type == CPP_SEMICOLON && !nesting_depth)
2252         break;
2253       /* If the next token is a non-nested `}', then we have reached
2254          the end of the current block.  */
2255       if (token->type == CPP_CLOSE_BRACE)
2256         {
2257           /* If this is a non-nested `}', stop before consuming it.
2258              That way, when confronted with something like:
2259
2260                { 3 + }
2261
2262              we stop before consuming the closing `}', even though we
2263              have not yet reached a `;'.  */
2264           if (nesting_depth == 0)
2265             break;
2266           /* If it is the closing `}' for a block that we have
2267              scanned, stop -- but only after consuming the token.
2268              That way given:
2269
2270                 void f g () { ... }
2271                 typedef int I;
2272
2273              we will stop after the body of the erroneously declared
2274              function, but before consuming the following `typedef'
2275              declaration.  */
2276           if (--nesting_depth == 0)
2277             {
2278               cp_lexer_consume_token (parser->lexer);
2279               break;
2280             }
2281         }
2282       /* If it the next token is a `{', then we are entering a new
2283          block.  Consume the entire block.  */
2284       else if (token->type == CPP_OPEN_BRACE)
2285         ++nesting_depth;
2286       /* Consume the token.  */
2287       cp_lexer_consume_token (parser->lexer);
2288     }
2289 }
2290
2291 /* This function is called at the end of a statement or declaration.
2292    If the next token is a semicolon, it is consumed; otherwise, error
2293    recovery is attempted.  */
2294
2295 static void
2296 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2297 {
2298   /* Look for the trailing `;'.  */
2299   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2300     {
2301       /* If there is additional (erroneous) input, skip to the end of
2302          the statement.  */
2303       cp_parser_skip_to_end_of_statement (parser);
2304       /* If the next token is now a `;', consume it.  */
2305       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2306         cp_lexer_consume_token (parser->lexer);
2307     }
2308 }
2309
2310 /* Skip tokens until we have consumed an entire block, or until we
2311    have consumed a non-nested `;'.  */
2312
2313 static void
2314 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2315 {
2316   unsigned nesting_depth = 0;
2317
2318   while (true)
2319     {
2320       cp_token *token;
2321
2322       /* Peek at the next token.  */
2323       token = cp_lexer_peek_token (parser->lexer);
2324       /* If we've run out of tokens, stop.  */
2325       if (token->type == CPP_EOF)
2326         break;
2327       /* If the next token is a `;', we have reached the end of the
2328          statement.  */
2329       if (token->type == CPP_SEMICOLON && !nesting_depth)
2330         {
2331           /* Consume the `;'.  */
2332           cp_lexer_consume_token (parser->lexer);
2333           break;
2334         }
2335       /* Consume the token.  */
2336       token = cp_lexer_consume_token (parser->lexer);
2337       /* If the next token is a non-nested `}', then we have reached
2338          the end of the current block.  */
2339       if (token->type == CPP_CLOSE_BRACE
2340           && (nesting_depth == 0 || --nesting_depth == 0))
2341         break;
2342       /* If it the next token is a `{', then we are entering a new
2343          block.  Consume the entire block.  */
2344       if (token->type == CPP_OPEN_BRACE)
2345         ++nesting_depth;
2346     }
2347 }
2348
2349 /* Skip tokens until a non-nested closing curly brace is the next
2350    token.  */
2351
2352 static void
2353 cp_parser_skip_to_closing_brace (cp_parser *parser)
2354 {
2355   unsigned nesting_depth = 0;
2356
2357   while (true)
2358     {
2359       cp_token *token;
2360
2361       /* Peek at the next token.  */
2362       token = cp_lexer_peek_token (parser->lexer);
2363       /* If we've run out of tokens, stop.  */
2364       if (token->type == CPP_EOF)
2365         break;
2366       /* If the next token is a non-nested `}', then we have reached
2367          the end of the current block.  */
2368       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2369         break;
2370       /* If it the next token is a `{', then we are entering a new
2371          block.  Consume the entire block.  */
2372       else if (token->type == CPP_OPEN_BRACE)
2373         ++nesting_depth;
2374       /* Consume the token.  */
2375       cp_lexer_consume_token (parser->lexer);
2376     }
2377 }
2378
2379 /* This is a simple wrapper around make_typename_type. When the id is
2380    an unresolved identifier node, we can provide a superior diagnostic
2381    using cp_parser_diagnose_invalid_type_name.  */
2382
2383 static tree
2384 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2385 {
2386   tree result;
2387   if (TREE_CODE (id) == IDENTIFIER_NODE)
2388     {
2389       result = make_typename_type (scope, id, typename_type,
2390                                    /*complain=*/0);
2391       if (result == error_mark_node)
2392         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2393       return result;
2394     }
2395   return make_typename_type (scope, id, typename_type, tf_error);
2396 }
2397
2398
2399 /* Create a new C++ parser.  */
2400
2401 static cp_parser *
2402 cp_parser_new (void)
2403 {
2404   cp_parser *parser;
2405   cp_lexer *lexer;
2406   unsigned i;
2407
2408   /* cp_lexer_new_main is called before calling ggc_alloc because
2409      cp_lexer_new_main might load a PCH file.  */
2410   lexer = cp_lexer_new_main ();
2411
2412   /* Initialize the binops_by_token so that we can get the tree
2413      directly from the token.  */
2414   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2415     binops_by_token[binops[i].token_type] = binops[i];
2416
2417   parser = GGC_CNEW (cp_parser);
2418   parser->lexer = lexer;
2419   parser->context = cp_parser_context_new (NULL);
2420
2421   /* For now, we always accept GNU extensions.  */
2422   parser->allow_gnu_extensions_p = 1;
2423
2424   /* The `>' token is a greater-than operator, not the end of a
2425      template-id.  */
2426   parser->greater_than_is_operator_p = true;
2427
2428   parser->default_arg_ok_p = true;
2429
2430   /* We are not parsing a constant-expression.  */
2431   parser->integral_constant_expression_p = false;
2432   parser->allow_non_integral_constant_expression_p = false;
2433   parser->non_integral_constant_expression_p = false;
2434
2435   /* Local variable names are not forbidden.  */
2436   parser->local_variables_forbidden_p = false;
2437
2438   /* We are not processing an `extern "C"' declaration.  */
2439   parser->in_unbraced_linkage_specification_p = false;
2440
2441   /* We are not processing a declarator.  */
2442   parser->in_declarator_p = false;
2443
2444   /* We are not processing a template-argument-list.  */
2445   parser->in_template_argument_list_p = false;
2446
2447   /* We are not in an iteration statement.  */
2448   parser->in_iteration_statement_p = false;
2449
2450   /* We are not in a switch statement.  */
2451   parser->in_switch_statement_p = false;
2452
2453   /* We are not parsing a type-id inside an expression.  */
2454   parser->in_type_id_in_expr_p = false;
2455
2456   /* Declarations aren't implicitly extern "C".  */
2457   parser->implicit_extern_c = false;
2458
2459   /* String literals should be translated to the execution character set.  */
2460   parser->translate_strings_p = true;
2461
2462   /* The unparsed function queue is empty.  */
2463   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2464
2465   /* There are no classes being defined.  */
2466   parser->num_classes_being_defined = 0;
2467
2468   /* No template parameters apply.  */
2469   parser->num_template_parameter_lists = 0;
2470
2471   return parser;
2472 }
2473
2474 /* Create a cp_lexer structure which will emit the tokens in CACHE
2475    and push it onto the parser's lexer stack.  This is used for delayed
2476    parsing of in-class method bodies and default arguments, and should
2477    not be confused with tentative parsing.  */
2478 static void
2479 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2480 {
2481   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2482   lexer->next = parser->lexer;
2483   parser->lexer = lexer;
2484
2485   /* Move the current source position to that of the first token in the
2486      new lexer.  */
2487   cp_lexer_set_source_position_from_token (lexer->next_token);
2488 }
2489
2490 /* Pop the top lexer off the parser stack.  This is never used for the
2491    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2492 static void
2493 cp_parser_pop_lexer (cp_parser *parser)
2494 {
2495   cp_lexer *lexer = parser->lexer;
2496   parser->lexer = lexer->next;
2497   cp_lexer_destroy (lexer);
2498
2499   /* Put the current source position back where it was before this
2500      lexer was pushed.  */
2501   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2502 }
2503
2504 /* Lexical conventions [gram.lex]  */
2505
2506 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2507    identifier.  */
2508
2509 static tree
2510 cp_parser_identifier (cp_parser* parser)
2511 {
2512   cp_token *token;
2513
2514   /* Look for the identifier.  */
2515   token = cp_parser_require (parser, CPP_NAME, "identifier");
2516   /* Return the value.  */
2517   return token ? token->value : error_mark_node;
2518 }
2519
2520 /* Parse a sequence of adjacent string constants.  Returns a
2521    TREE_STRING representing the combined, nul-terminated string
2522    constant.  If TRANSLATE is true, translate the string to the
2523    execution character set.  If WIDE_OK is true, a wide string is
2524    invalid here.
2525
2526    C++98 [lex.string] says that if a narrow string literal token is
2527    adjacent to a wide string literal token, the behavior is undefined.
2528    However, C99 6.4.5p4 says that this results in a wide string literal.
2529    We follow C99 here, for consistency with the C front end.
2530
2531    This code is largely lifted from lex_string() in c-lex.c.
2532
2533    FUTURE: ObjC++ will need to handle @-strings here.  */
2534 static tree
2535 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2536 {
2537   tree value;
2538   bool wide = false;
2539   size_t count;
2540   struct obstack str_ob;
2541   cpp_string str, istr, *strs;
2542   cp_token *tok;
2543
2544   tok = cp_lexer_peek_token (parser->lexer);
2545   if (!cp_parser_is_string_literal (tok))
2546     {
2547       cp_parser_error (parser, "expected string-literal");
2548       return error_mark_node;
2549     }
2550
2551   /* Try to avoid the overhead of creating and destroying an obstack
2552      for the common case of just one string.  */
2553   if (!cp_parser_is_string_literal
2554       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2555     {
2556       cp_lexer_consume_token (parser->lexer);
2557
2558       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2559       str.len = TREE_STRING_LENGTH (tok->value);
2560       count = 1;
2561       if (tok->type == CPP_WSTRING)
2562         wide = true;
2563
2564       strs = &str;
2565     }
2566   else
2567     {
2568       gcc_obstack_init (&str_ob);
2569       count = 0;
2570
2571       do
2572         {
2573           cp_lexer_consume_token (parser->lexer);
2574           count++;
2575           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2576           str.len = TREE_STRING_LENGTH (tok->value);
2577           if (tok->type == CPP_WSTRING)
2578             wide = true;
2579
2580           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2581
2582           tok = cp_lexer_peek_token (parser->lexer);
2583         }
2584       while (cp_parser_is_string_literal (tok));
2585
2586       strs = (cpp_string *) obstack_finish (&str_ob);
2587     }
2588
2589   if (wide && !wide_ok)
2590     {
2591       cp_parser_error (parser, "a wide string is invalid in this context");
2592       wide = false;
2593     }
2594
2595   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2596       (parse_in, strs, count, &istr, wide))
2597     {
2598       value = build_string (istr.len, (char *)istr.text);
2599       free ((void *)istr.text);
2600
2601       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2602       value = fix_string_type (value);
2603     }
2604   else
2605     /* cpp_interpret_string has issued an error.  */
2606     value = error_mark_node;
2607
2608   if (count > 1)
2609     obstack_free (&str_ob, 0);
2610
2611   return value;
2612 }
2613
2614
2615 /* Basic concepts [gram.basic]  */
2616
2617 /* Parse a translation-unit.
2618
2619    translation-unit:
2620      declaration-seq [opt]
2621
2622    Returns TRUE if all went well.  */
2623
2624 static bool
2625 cp_parser_translation_unit (cp_parser* parser)
2626 {
2627   /* The address of the first non-permanent object on the declarator
2628      obstack.  */
2629   static void *declarator_obstack_base;
2630
2631   bool success;
2632
2633   /* Create the declarator obstack, if necessary.  */
2634   if (!cp_error_declarator)
2635     {
2636       gcc_obstack_init (&declarator_obstack);
2637       /* Create the error declarator.  */
2638       cp_error_declarator = make_declarator (cdk_error);
2639       /* Create the empty parameter list.  */
2640       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2641       /* Remember where the base of the declarator obstack lies.  */
2642       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2643     }
2644
2645   while (true)
2646     {
2647       cp_parser_declaration_seq_opt (parser);
2648
2649       /* If there are no tokens left then all went well.  */
2650       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2651         {
2652           /* Get rid of the token array; we don't need it any more.  */
2653           cp_lexer_destroy (parser->lexer);
2654           parser->lexer = NULL;
2655
2656           /* This file might have been a context that's implicitly extern
2657              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2658           if (parser->implicit_extern_c)
2659             {
2660               pop_lang_context ();
2661               parser->implicit_extern_c = false;
2662             }
2663
2664           /* Finish up.  */
2665           finish_translation_unit ();
2666
2667           success = true;
2668           break;
2669         }
2670       else
2671         {
2672           cp_parser_error (parser, "expected declaration");
2673           success = false;
2674           break;
2675         }
2676     }
2677
2678   /* Make sure the declarator obstack was fully cleaned up.  */
2679   gcc_assert (obstack_next_free (&declarator_obstack)
2680               == declarator_obstack_base);
2681
2682   /* All went well.  */
2683   return success;
2684 }
2685
2686 /* Expressions [gram.expr] */
2687
2688 /* Parse a primary-expression.
2689
2690    primary-expression:
2691      literal
2692      this
2693      ( expression )
2694      id-expression
2695
2696    GNU Extensions:
2697
2698    primary-expression:
2699      ( compound-statement )
2700      __builtin_va_arg ( assignment-expression , type-id )
2701
2702    Objective-C++ Extension:
2703
2704    primary-expression:
2705      objc-expression
2706
2707    literal:
2708      __null
2709
2710    CAST_P is true if this primary expression is the target of a cast.
2711
2712    Returns a representation of the expression.
2713
2714    *IDK indicates what kind of id-expression (if any) was present.
2715
2716    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2717    used as the operand of a pointer-to-member.  In that case,
2718    *QUALIFYING_CLASS gives the class that is used as the qualifying
2719    class in the pointer-to-member.  */
2720
2721 static tree
2722 cp_parser_primary_expression (cp_parser *parser,
2723                               bool cast_p,
2724                               cp_id_kind *idk,
2725                               tree *qualifying_class)
2726 {
2727   cp_token *token;
2728
2729   /* Assume the primary expression is not an id-expression.  */
2730   *idk = CP_ID_KIND_NONE;
2731   /* And that it cannot be used as pointer-to-member.  */
2732   *qualifying_class = NULL_TREE;
2733
2734   /* Peek at the next token.  */
2735   token = cp_lexer_peek_token (parser->lexer);
2736   switch (token->type)
2737     {
2738       /* literal:
2739            integer-literal
2740            character-literal
2741            floating-literal
2742            string-literal
2743            boolean-literal  */
2744     case CPP_CHAR:
2745     case CPP_WCHAR:
2746     case CPP_NUMBER:
2747       token = cp_lexer_consume_token (parser->lexer);
2748       /* Floating-point literals are only allowed in an integral
2749          constant expression if they are cast to an integral or
2750          enumeration type.  */
2751       if (TREE_CODE (token->value) == REAL_CST
2752           && parser->integral_constant_expression_p
2753           && pedantic)
2754         {
2755           /* CAST_P will be set even in invalid code like "int(2.7 +
2756              ...)".   Therefore, we have to check that the next token
2757              is sure to end the cast.  */
2758           if (cast_p)
2759             {
2760               cp_token *next_token;
2761
2762               next_token = cp_lexer_peek_token (parser->lexer);
2763               if (/* The comma at the end of an
2764                      enumerator-definition.  */
2765                   next_token->type != CPP_COMMA
2766                   /* The curly brace at the end of an enum-specifier.  */
2767                   && next_token->type != CPP_CLOSE_BRACE
2768                   /* The end of a statement.  */
2769                   && next_token->type != CPP_SEMICOLON
2770                   /* The end of the cast-expression.  */
2771                   && next_token->type != CPP_CLOSE_PAREN
2772                   /* The end of an array bound.  */
2773                   && next_token->type != CPP_CLOSE_SQUARE)
2774                 cast_p = false;
2775             }
2776
2777           /* If we are within a cast, then the constraint that the
2778              cast is to an integral or enumeration type will be
2779              checked at that point.  If we are not within a cast, then
2780              this code is invalid.  */
2781           if (!cast_p)
2782             cp_parser_non_integral_constant_expression 
2783               (parser, "floating-point literal");
2784         }
2785       return token->value;
2786
2787     case CPP_STRING:
2788     case CPP_WSTRING:
2789       /* ??? Should wide strings be allowed when parser->translate_strings_p
2790          is false (i.e. in attributes)?  If not, we can kill the third
2791          argument to cp_parser_string_literal.  */
2792       return cp_parser_string_literal (parser,
2793                                        parser->translate_strings_p,
2794                                        true);
2795
2796     case CPP_OPEN_PAREN:
2797       {
2798         tree expr;
2799         bool saved_greater_than_is_operator_p;
2800
2801         /* Consume the `('.  */
2802         cp_lexer_consume_token (parser->lexer);
2803         /* Within a parenthesized expression, a `>' token is always
2804            the greater-than operator.  */
2805         saved_greater_than_is_operator_p
2806           = parser->greater_than_is_operator_p;
2807         parser->greater_than_is_operator_p = true;
2808         /* If we see `( { ' then we are looking at the beginning of
2809            a GNU statement-expression.  */
2810         if (cp_parser_allow_gnu_extensions_p (parser)
2811             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2812           {
2813             /* Statement-expressions are not allowed by the standard.  */
2814             if (pedantic)
2815               pedwarn ("ISO C++ forbids braced-groups within expressions");
2816
2817             /* And they're not allowed outside of a function-body; you
2818                cannot, for example, write:
2819
2820                  int i = ({ int j = 3; j + 1; });
2821
2822                at class or namespace scope.  */
2823             if (!at_function_scope_p ())
2824               error ("statement-expressions are allowed only inside functions");
2825             /* Start the statement-expression.  */
2826             expr = begin_stmt_expr ();
2827             /* Parse the compound-statement.  */
2828             cp_parser_compound_statement (parser, expr, false);
2829             /* Finish up.  */
2830             expr = finish_stmt_expr (expr, false);
2831           }
2832         else
2833           {
2834             /* Parse the parenthesized expression.  */
2835             expr = cp_parser_expression (parser, cast_p);
2836             /* Let the front end know that this expression was
2837                enclosed in parentheses. This matters in case, for
2838                example, the expression is of the form `A::B', since
2839                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2840                not.  */
2841             finish_parenthesized_expr (expr);
2842           }
2843         /* The `>' token might be the end of a template-id or
2844            template-parameter-list now.  */
2845         parser->greater_than_is_operator_p
2846           = saved_greater_than_is_operator_p;
2847         /* Consume the `)'.  */
2848         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2849           cp_parser_skip_to_end_of_statement (parser);
2850
2851         return expr;
2852       }
2853
2854     case CPP_KEYWORD:
2855       switch (token->keyword)
2856         {
2857           /* These two are the boolean literals.  */
2858         case RID_TRUE:
2859           cp_lexer_consume_token (parser->lexer);
2860           return boolean_true_node;
2861         case RID_FALSE:
2862           cp_lexer_consume_token (parser->lexer);
2863           return boolean_false_node;
2864
2865           /* The `__null' literal.  */
2866         case RID_NULL:
2867           cp_lexer_consume_token (parser->lexer);
2868           return null_node;
2869
2870           /* Recognize the `this' keyword.  */
2871         case RID_THIS:
2872           cp_lexer_consume_token (parser->lexer);
2873           if (parser->local_variables_forbidden_p)
2874             {
2875               error ("%<this%> may not be used in this context");
2876               return error_mark_node;
2877             }
2878           /* Pointers cannot appear in constant-expressions.  */
2879           if (cp_parser_non_integral_constant_expression (parser,
2880                                                           "`this'"))
2881             return error_mark_node;
2882           return finish_this_expr ();
2883
2884           /* The `operator' keyword can be the beginning of an
2885              id-expression.  */
2886         case RID_OPERATOR:
2887           goto id_expression;
2888
2889         case RID_FUNCTION_NAME:
2890         case RID_PRETTY_FUNCTION_NAME:
2891         case RID_C99_FUNCTION_NAME:
2892           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2893              __func__ are the names of variables -- but they are
2894              treated specially.  Therefore, they are handled here,
2895              rather than relying on the generic id-expression logic
2896              below.  Grammatically, these names are id-expressions.
2897
2898              Consume the token.  */
2899           token = cp_lexer_consume_token (parser->lexer);
2900           /* Look up the name.  */
2901           return finish_fname (token->value);
2902
2903         case RID_VA_ARG:
2904           {
2905             tree expression;
2906             tree type;
2907
2908             /* The `__builtin_va_arg' construct is used to handle
2909                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2910             cp_lexer_consume_token (parser->lexer);
2911             /* Look for the opening `('.  */
2912             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2913             /* Now, parse the assignment-expression.  */
2914             expression = cp_parser_assignment_expression (parser,
2915                                                           /*cast_p=*/false);
2916             /* Look for the `,'.  */
2917             cp_parser_require (parser, CPP_COMMA, "`,'");
2918             /* Parse the type-id.  */
2919             type = cp_parser_type_id (parser);
2920             /* Look for the closing `)'.  */
2921             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2922             /* Using `va_arg' in a constant-expression is not
2923                allowed.  */
2924             if (cp_parser_non_integral_constant_expression (parser,
2925                                                             "`va_arg'"))
2926               return error_mark_node;
2927             return build_x_va_arg (expression, type);
2928           }
2929
2930         case RID_OFFSETOF:
2931           return cp_parser_builtin_offsetof (parser);
2932
2933           /* Objective-C++ expressions.  */
2934         case RID_AT_ENCODE:
2935         case RID_AT_PROTOCOL:
2936         case RID_AT_SELECTOR:
2937           return cp_parser_objc_expression (parser);
2938
2939         default:
2940           cp_parser_error (parser, "expected primary-expression");
2941           return error_mark_node;
2942         }
2943
2944       /* An id-expression can start with either an identifier, a
2945          `::' as the beginning of a qualified-id, or the "operator"
2946          keyword.  */
2947     case CPP_NAME:
2948     case CPP_SCOPE:
2949     case CPP_TEMPLATE_ID:
2950     case CPP_NESTED_NAME_SPECIFIER:
2951       {
2952         tree id_expression;
2953         tree decl;
2954         const char *error_msg;
2955
2956       id_expression:
2957         /* Parse the id-expression.  */
2958         id_expression
2959           = cp_parser_id_expression (parser,
2960                                      /*template_keyword_p=*/false,
2961                                      /*check_dependency_p=*/true,
2962                                      /*template_p=*/NULL,
2963                                      /*declarator_p=*/false);
2964         if (id_expression == error_mark_node)
2965           return error_mark_node;
2966         /* If we have a template-id, then no further lookup is
2967            required.  If the template-id was for a template-class, we
2968            will sometimes have a TYPE_DECL at this point.  */
2969         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2970             || TREE_CODE (id_expression) == TYPE_DECL)
2971           decl = id_expression;
2972         /* Look up the name.  */
2973         else
2974           {
2975             bool ambiguous_p;
2976
2977             decl = cp_parser_lookup_name (parser, id_expression,
2978                                           none_type,
2979                                           /*is_template=*/false,
2980                                           /*is_namespace=*/false,
2981                                           /*check_dependency=*/true,
2982                                           &ambiguous_p);
2983             /* If the lookup was ambiguous, an error will already have
2984                been issued.  */
2985             if (ambiguous_p)
2986               return error_mark_node;
2987
2988             /* In Objective-C++, an instance variable (ivar) may be preferred
2989                to whatever cp_parser_lookup_name() found.  */
2990             decl = objc_lookup_ivar (decl, id_expression);
2991
2992             /* If name lookup gives us a SCOPE_REF, then the
2993                qualifying scope was dependent.  Just propagate the
2994                name.  */
2995             if (TREE_CODE (decl) == SCOPE_REF)
2996               {
2997                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2998                   *qualifying_class = TREE_OPERAND (decl, 0);
2999                 return decl;
3000               }
3001             /* Check to see if DECL is a local variable in a context
3002                where that is forbidden.  */
3003             if (parser->local_variables_forbidden_p
3004                 && local_variable_p (decl))
3005               {
3006                 /* It might be that we only found DECL because we are
3007                    trying to be generous with pre-ISO scoping rules.
3008                    For example, consider:
3009
3010                      int i;
3011                      void g() {
3012                        for (int i = 0; i < 10; ++i) {}
3013                        extern void f(int j = i);
3014                      }
3015
3016                    Here, name look up will originally find the out
3017                    of scope `i'.  We need to issue a warning message,
3018                    but then use the global `i'.  */
3019                 decl = check_for_out_of_scope_variable (decl);
3020                 if (local_variable_p (decl))
3021                   {
3022                     error ("local variable %qD may not appear in this context",
3023                            decl);
3024                     return error_mark_node;
3025                   }
3026               }
3027           }
3028
3029         decl = finish_id_expression (id_expression, decl, parser->scope,
3030                                      idk, qualifying_class,
3031                                      parser->integral_constant_expression_p,
3032                                      parser->allow_non_integral_constant_expression_p,
3033                                      &parser->non_integral_constant_expression_p,
3034                                      &error_msg);
3035         if (error_msg)
3036           cp_parser_error (parser, error_msg);
3037         return decl;
3038       }
3039
3040       /* Anything else is an error.  */
3041     default:
3042       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3043       if (c_dialect_objc () 
3044           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3045         return cp_parser_objc_expression (parser);
3046
3047       cp_parser_error (parser, "expected primary-expression");
3048       return error_mark_node;
3049     }
3050 }
3051
3052 /* Parse an id-expression.
3053
3054    id-expression:
3055      unqualified-id
3056      qualified-id
3057
3058    qualified-id:
3059      :: [opt] nested-name-specifier template [opt] unqualified-id
3060      :: identifier
3061      :: operator-function-id
3062      :: template-id
3063
3064    Return a representation of the unqualified portion of the
3065    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3066    a `::' or nested-name-specifier.
3067
3068    Often, if the id-expression was a qualified-id, the caller will
3069    want to make a SCOPE_REF to represent the qualified-id.  This
3070    function does not do this in order to avoid wastefully creating
3071    SCOPE_REFs when they are not required.
3072
3073    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3074    `template' keyword.
3075
3076    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3077    uninstantiated templates.
3078
3079    If *TEMPLATE_P is non-NULL, it is set to true iff the
3080    `template' keyword is used to explicitly indicate that the entity
3081    named is a template.
3082
3083    If DECLARATOR_P is true, the id-expression is appearing as part of
3084    a declarator, rather than as part of an expression.  */
3085
3086 static tree
3087 cp_parser_id_expression (cp_parser *parser,
3088                          bool template_keyword_p,
3089                          bool check_dependency_p,
3090                          bool *template_p,
3091                          bool declarator_p)
3092 {
3093   bool global_scope_p;
3094   bool nested_name_specifier_p;
3095
3096   /* Assume the `template' keyword was not used.  */
3097   if (template_p)
3098     *template_p = false;
3099
3100   /* Look for the optional `::' operator.  */
3101   global_scope_p
3102     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3103        != NULL_TREE);
3104   /* Look for the optional nested-name-specifier.  */
3105   nested_name_specifier_p
3106     = (cp_parser_nested_name_specifier_opt (parser,
3107                                             /*typename_keyword_p=*/false,
3108                                             check_dependency_p,
3109                                             /*type_p=*/false,
3110                                             declarator_p)
3111        != NULL_TREE);
3112   /* If there is a nested-name-specifier, then we are looking at
3113      the first qualified-id production.  */
3114   if (nested_name_specifier_p)
3115     {
3116       tree saved_scope;
3117       tree saved_object_scope;
3118       tree saved_qualifying_scope;
3119       tree unqualified_id;
3120       bool is_template;
3121
3122       /* See if the next token is the `template' keyword.  */
3123       if (!template_p)
3124         template_p = &is_template;
3125       *template_p = cp_parser_optional_template_keyword (parser);
3126       /* Name lookup we do during the processing of the
3127          unqualified-id might obliterate SCOPE.  */
3128       saved_scope = parser->scope;
3129       saved_object_scope = parser->object_scope;
3130       saved_qualifying_scope = parser->qualifying_scope;
3131       /* Process the final unqualified-id.  */
3132       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3133                                                  check_dependency_p,
3134                                                  declarator_p);
3135       /* Restore the SAVED_SCOPE for our caller.  */
3136       parser->scope = saved_scope;
3137       parser->object_scope = saved_object_scope;
3138       parser->qualifying_scope = saved_qualifying_scope;
3139
3140       return unqualified_id;
3141     }
3142   /* Otherwise, if we are in global scope, then we are looking at one
3143      of the other qualified-id productions.  */
3144   else if (global_scope_p)
3145     {
3146       cp_token *token;
3147       tree id;
3148
3149       /* Peek at the next token.  */
3150       token = cp_lexer_peek_token (parser->lexer);
3151
3152       /* If it's an identifier, and the next token is not a "<", then
3153          we can avoid the template-id case.  This is an optimization
3154          for this common case.  */
3155       if (token->type == CPP_NAME
3156           && !cp_parser_nth_token_starts_template_argument_list_p
3157                (parser, 2))
3158         return cp_parser_identifier (parser);
3159
3160       cp_parser_parse_tentatively (parser);
3161       /* Try a template-id.  */
3162       id = cp_parser_template_id (parser,
3163                                   /*template_keyword_p=*/false,
3164                                   /*check_dependency_p=*/true,
3165                                   declarator_p);
3166       /* If that worked, we're done.  */
3167       if (cp_parser_parse_definitely (parser))
3168         return id;
3169
3170       /* Peek at the next token.  (Changes in the token buffer may
3171          have invalidated the pointer obtained above.)  */
3172       token = cp_lexer_peek_token (parser->lexer);
3173
3174       switch (token->type)
3175         {
3176         case CPP_NAME:
3177           return cp_parser_identifier (parser);
3178
3179         case CPP_KEYWORD:
3180           if (token->keyword == RID_OPERATOR)
3181             return cp_parser_operator_function_id (parser);
3182           /* Fall through.  */
3183
3184         default:
3185           cp_parser_error (parser, "expected id-expression");
3186           return error_mark_node;
3187         }
3188     }
3189   else
3190     return cp_parser_unqualified_id (parser, template_keyword_p,
3191                                      /*check_dependency_p=*/true,
3192                                      declarator_p);
3193 }
3194
3195 /* Parse an unqualified-id.
3196
3197    unqualified-id:
3198      identifier
3199      operator-function-id
3200      conversion-function-id
3201      ~ class-name
3202      template-id
3203
3204    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3205    keyword, in a construct like `A::template ...'.
3206
3207    Returns a representation of unqualified-id.  For the `identifier'
3208    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3209    production a BIT_NOT_EXPR is returned; the operand of the
3210    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3211    other productions, see the documentation accompanying the
3212    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3213    names are looked up in uninstantiated templates.  If DECLARATOR_P
3214    is true, the unqualified-id is appearing as part of a declarator,
3215    rather than as part of an expression.  */
3216
3217 static tree
3218 cp_parser_unqualified_id (cp_parser* parser,
3219                           bool template_keyword_p,
3220                           bool check_dependency_p,
3221                           bool declarator_p)
3222 {
3223   cp_token *token;
3224
3225   /* Peek at the next token.  */
3226   token = cp_lexer_peek_token (parser->lexer);
3227
3228   switch (token->type)
3229     {
3230     case CPP_NAME:
3231       {
3232         tree id;
3233
3234         /* We don't know yet whether or not this will be a
3235            template-id.  */
3236         cp_parser_parse_tentatively (parser);
3237         /* Try a template-id.  */
3238         id = cp_parser_template_id (parser, template_keyword_p,
3239                                     check_dependency_p,
3240                                     declarator_p);
3241         /* If it worked, we're done.  */
3242         if (cp_parser_parse_definitely (parser))
3243           return id;
3244         /* Otherwise, it's an ordinary identifier.  */
3245         return cp_parser_identifier (parser);
3246       }
3247
3248     case CPP_TEMPLATE_ID:
3249       return cp_parser_template_id (parser, template_keyword_p,
3250                                     check_dependency_p,
3251                                     declarator_p);
3252
3253     case CPP_COMPL:
3254       {
3255         tree type_decl;
3256         tree qualifying_scope;
3257         tree object_scope;
3258         tree scope;
3259         bool done;
3260
3261         /* Consume the `~' token.  */
3262         cp_lexer_consume_token (parser->lexer);
3263         /* Parse the class-name.  The standard, as written, seems to
3264            say that:
3265
3266              template <typename T> struct S { ~S (); };
3267              template <typename T> S<T>::~S() {}
3268
3269            is invalid, since `~' must be followed by a class-name, but
3270            `S<T>' is dependent, and so not known to be a class.
3271            That's not right; we need to look in uninstantiated
3272            templates.  A further complication arises from:
3273
3274              template <typename T> void f(T t) {
3275                t.T::~T();
3276              }
3277
3278            Here, it is not possible to look up `T' in the scope of `T'
3279            itself.  We must look in both the current scope, and the
3280            scope of the containing complete expression.
3281
3282            Yet another issue is:
3283
3284              struct S {
3285                int S;
3286                ~S();
3287              };
3288
3289              S::~S() {}
3290
3291            The standard does not seem to say that the `S' in `~S'
3292            should refer to the type `S' and not the data member
3293            `S::S'.  */
3294
3295         /* DR 244 says that we look up the name after the "~" in the
3296            same scope as we looked up the qualifying name.  That idea
3297            isn't fully worked out; it's more complicated than that.  */
3298         scope = parser->scope;
3299         object_scope = parser->object_scope;
3300         qualifying_scope = parser->qualifying_scope;
3301
3302         /* If the name is of the form "X::~X" it's OK.  */
3303         if (scope && TYPE_P (scope)
3304             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3305             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3306                 == CPP_OPEN_PAREN)
3307             && (cp_lexer_peek_token (parser->lexer)->value
3308                 == TYPE_IDENTIFIER (scope)))
3309           {
3310             cp_lexer_consume_token (parser->lexer);
3311             return build_nt (BIT_NOT_EXPR, scope);
3312           }
3313
3314         /* If there was an explicit qualification (S::~T), first look
3315            in the scope given by the qualification (i.e., S).  */
3316         done = false;
3317         type_decl = NULL_TREE;
3318         if (scope)
3319           {
3320             cp_parser_parse_tentatively (parser);
3321             type_decl = cp_parser_class_name (parser,
3322                                               /*typename_keyword_p=*/false,
3323                                               /*template_keyword_p=*/false,
3324                                               none_type,
3325                                               /*check_dependency=*/false,
3326                                               /*class_head_p=*/false,
3327                                               declarator_p);
3328             if (cp_parser_parse_definitely (parser))
3329               done = true;
3330           }
3331         /* In "N::S::~S", look in "N" as well.  */
3332         if (!done && scope && qualifying_scope)
3333           {
3334             cp_parser_parse_tentatively (parser);
3335             parser->scope = qualifying_scope;
3336             parser->object_scope = NULL_TREE;
3337             parser->qualifying_scope = NULL_TREE;
3338             type_decl
3339               = cp_parser_class_name (parser,
3340                                       /*typename_keyword_p=*/false,
3341                                       /*template_keyword_p=*/false,
3342                                       none_type,
3343                                       /*check_dependency=*/false,
3344                                       /*class_head_p=*/false,
3345                                       declarator_p);
3346             if (cp_parser_parse_definitely (parser))
3347               done = true;
3348           }
3349         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3350         else if (!done && object_scope)
3351           {
3352             cp_parser_parse_tentatively (parser);
3353             parser->scope = object_scope;
3354             parser->object_scope = NULL_TREE;
3355             parser->qualifying_scope = NULL_TREE;
3356             type_decl
3357               = cp_parser_class_name (parser,
3358                                       /*typename_keyword_p=*/false,
3359                                       /*template_keyword_p=*/false,
3360                                       none_type,
3361                                       /*check_dependency=*/false,
3362                                       /*class_head_p=*/false,
3363                                       declarator_p);
3364             if (cp_parser_parse_definitely (parser))
3365               done = true;
3366           }
3367         /* Look in the surrounding context.  */
3368         if (!done)
3369           {
3370             parser->scope = NULL_TREE;
3371             parser->object_scope = NULL_TREE;
3372             parser->qualifying_scope = NULL_TREE;
3373             type_decl
3374               = cp_parser_class_name (parser,
3375                                       /*typename_keyword_p=*/false,
3376                                       /*template_keyword_p=*/false,
3377                                       none_type,
3378                                       /*check_dependency=*/false,
3379                                       /*class_head_p=*/false,
3380                                       declarator_p);
3381           }
3382         /* If an error occurred, assume that the name of the
3383            destructor is the same as the name of the qualifying
3384            class.  That allows us to keep parsing after running
3385            into ill-formed destructor names.  */
3386         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3387           return build_nt (BIT_NOT_EXPR, scope);
3388         else if (type_decl == error_mark_node)
3389           return error_mark_node;
3390
3391         /* [class.dtor]
3392
3393            A typedef-name that names a class shall not be used as the
3394            identifier in the declarator for a destructor declaration.  */
3395         if (declarator_p
3396             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3397             && !DECL_SELF_REFERENCE_P (type_decl)
3398             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3399           error ("typedef-name %qD used as destructor declarator",
3400                  type_decl);
3401
3402         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3403       }
3404
3405     case CPP_KEYWORD:
3406       if (token->keyword == RID_OPERATOR)
3407         {
3408           tree id;
3409
3410           /* This could be a template-id, so we try that first.  */
3411           cp_parser_parse_tentatively (parser);
3412           /* Try a template-id.  */
3413           id = cp_parser_template_id (parser, template_keyword_p,
3414                                       /*check_dependency_p=*/true,
3415                                       declarator_p);
3416           /* If that worked, we're done.  */
3417           if (cp_parser_parse_definitely (parser))
3418             return id;
3419           /* We still don't know whether we're looking at an
3420              operator-function-id or a conversion-function-id.  */
3421           cp_parser_parse_tentatively (parser);
3422           /* Try an operator-function-id.  */
3423           id = cp_parser_operator_function_id (parser);
3424           /* If that didn't work, try a conversion-function-id.  */
3425           if (!cp_parser_parse_definitely (parser))
3426             id = cp_parser_conversion_function_id (parser);
3427
3428           return id;
3429         }
3430       /* Fall through.  */
3431
3432     default:
3433       cp_parser_error (parser, "expected unqualified-id");
3434       return error_mark_node;
3435     }
3436 }
3437
3438 /* Parse an (optional) nested-name-specifier.
3439
3440    nested-name-specifier:
3441      class-or-namespace-name :: nested-name-specifier [opt]
3442      class-or-namespace-name :: template nested-name-specifier [opt]
3443
3444    PARSER->SCOPE should be set appropriately before this function is
3445    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3446    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3447    in name lookups.
3448
3449    Sets PARSER->SCOPE to the class (TYPE) or namespace
3450    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3451    it unchanged if there is no nested-name-specifier.  Returns the new
3452    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3453
3454    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3455    part of a declaration and/or decl-specifier.  */
3456
3457 static tree
3458 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3459                                      bool typename_keyword_p,
3460                                      bool check_dependency_p,
3461                                      bool type_p,
3462                                      bool is_declaration)
3463 {
3464   bool success = false;
3465   tree access_check = NULL_TREE;
3466   cp_token_position start = 0;
3467   cp_token *token;
3468
3469   /* If the next token corresponds to a nested name specifier, there
3470      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3471      false, it may have been true before, in which case something
3472      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3473      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3474      CHECK_DEPENDENCY_P is false, we have to fall through into the
3475      main loop.  */
3476   if (check_dependency_p
3477       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3478     {
3479       cp_parser_pre_parsed_nested_name_specifier (parser);
3480       return parser->scope;
3481     }
3482
3483   /* Remember where the nested-name-specifier starts.  */
3484   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3485     start = cp_lexer_token_position (parser->lexer, false);
3486
3487   push_deferring_access_checks (dk_deferred);
3488
3489   while (true)
3490     {
3491       tree new_scope;
3492       tree old_scope;
3493       tree saved_qualifying_scope;
3494       bool template_keyword_p;
3495
3496       /* Spot cases that cannot be the beginning of a
3497          nested-name-specifier.  */
3498       token = cp_lexer_peek_token (parser->lexer);
3499
3500       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3501          the already parsed nested-name-specifier.  */
3502       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3503         {
3504           /* Grab the nested-name-specifier and continue the loop.  */
3505           cp_parser_pre_parsed_nested_name_specifier (parser);
3506           success = true;
3507           continue;
3508         }
3509
3510       /* Spot cases that cannot be the beginning of a
3511          nested-name-specifier.  On the second and subsequent times
3512          through the loop, we look for the `template' keyword.  */
3513       if (success && token->keyword == RID_TEMPLATE)
3514         ;
3515       /* A template-id can start a nested-name-specifier.  */
3516       else if (token->type == CPP_TEMPLATE_ID)
3517         ;
3518       else
3519         {
3520           /* If the next token is not an identifier, then it is
3521              definitely not a class-or-namespace-name.  */
3522           if (token->type != CPP_NAME)
3523             break;
3524           /* If the following token is neither a `<' (to begin a
3525              template-id), nor a `::', then we are not looking at a
3526              nested-name-specifier.  */
3527           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3528           if (token->type != CPP_SCOPE
3529               && !cp_parser_nth_token_starts_template_argument_list_p
3530                   (parser, 2))
3531             break;
3532         }
3533
3534       /* The nested-name-specifier is optional, so we parse
3535          tentatively.  */
3536       cp_parser_parse_tentatively (parser);
3537
3538       /* Look for the optional `template' keyword, if this isn't the
3539          first time through the loop.  */
3540       if (success)
3541         template_keyword_p = cp_parser_optional_template_keyword (parser);
3542       else
3543         template_keyword_p = false;
3544
3545       /* Save the old scope since the name lookup we are about to do
3546          might destroy it.  */
3547       old_scope = parser->scope;
3548       saved_qualifying_scope = parser->qualifying_scope;
3549       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3550          look up names in "X<T>::I" in order to determine that "Y" is
3551          a template.  So, if we have a typename at this point, we make
3552          an effort to look through it.  */
3553       if (is_declaration 
3554           && !typename_keyword_p
3555           && parser->scope 
3556           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3557         parser->scope = resolve_typename_type (parser->scope, 
3558                                                /*only_current_p=*/false);
3559       /* Parse the qualifying entity.  */
3560       new_scope
3561         = cp_parser_class_or_namespace_name (parser,
3562                                              typename_keyword_p,
3563                                              template_keyword_p,
3564                                              check_dependency_p,
3565                                              type_p,
3566                                              is_declaration);
3567       /* Look for the `::' token.  */
3568       cp_parser_require (parser, CPP_SCOPE, "`::'");
3569
3570       /* If we found what we wanted, we keep going; otherwise, we're
3571          done.  */
3572       if (!cp_parser_parse_definitely (parser))
3573         {
3574           bool error_p = false;
3575
3576           /* Restore the OLD_SCOPE since it was valid before the
3577              failed attempt at finding the last
3578              class-or-namespace-name.  */
3579           parser->scope = old_scope;
3580           parser->qualifying_scope = saved_qualifying_scope;
3581           /* If the next token is an identifier, and the one after
3582              that is a `::', then any valid interpretation would have
3583              found a class-or-namespace-name.  */
3584           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3585                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3586                      == CPP_SCOPE)
3587                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3588                      != CPP_COMPL))
3589             {
3590               token = cp_lexer_consume_token (parser->lexer);
3591               if (!error_p)
3592                 {
3593                   tree decl;
3594
3595                   decl = cp_parser_lookup_name_simple (parser, token->value);
3596                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3597                     error ("%qD used without template parameters", decl);
3598                   else
3599                     cp_parser_name_lookup_error
3600                       (parser, token->value, decl,
3601                        "is not a class or namespace");
3602                   parser->scope = NULL_TREE;
3603                   error_p = true;
3604                   /* Treat this as a successful nested-name-specifier
3605                      due to:
3606
3607                      [basic.lookup.qual]
3608
3609                      If the name found is not a class-name (clause
3610                      _class_) or namespace-name (_namespace.def_), the
3611                      program is ill-formed.  */
3612                   success = true;
3613                 }
3614               cp_lexer_consume_token (parser->lexer);
3615             }
3616           break;
3617         }
3618
3619       /* We've found one valid nested-name-specifier.  */
3620       success = true;
3621       /* Make sure we look in the right scope the next time through
3622          the loop.  */
3623       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3624                        ? TREE_TYPE (new_scope)
3625                        : new_scope);
3626       /* If it is a class scope, try to complete it; we are about to
3627          be looking up names inside the class.  */
3628       if (TYPE_P (parser->scope)
3629           /* Since checking types for dependency can be expensive,
3630              avoid doing it if the type is already complete.  */
3631           && !COMPLETE_TYPE_P (parser->scope)
3632           /* Do not try to complete dependent types.  */
3633           && !dependent_type_p (parser->scope))
3634         complete_type (parser->scope);
3635     }
3636
3637   /* Retrieve any deferred checks.  Do not pop this access checks yet
3638      so the memory will not be reclaimed during token replacing below.  */
3639   access_check = get_deferred_access_checks ();
3640
3641   /* If parsing tentatively, replace the sequence of tokens that makes
3642      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3643      token.  That way, should we re-parse the token stream, we will
3644      not have to repeat the effort required to do the parse, nor will
3645      we issue duplicate error messages.  */
3646   if (success && start)
3647     {
3648       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3649       
3650       /* Reset the contents of the START token.  */
3651       token->type = CPP_NESTED_NAME_SPECIFIER;
3652       token->value = build_tree_list (access_check, parser->scope);
3653       TREE_TYPE (token->value) = parser->qualifying_scope;
3654       token->keyword = RID_MAX;
3655       
3656       /* Purge all subsequent tokens.  */
3657       cp_lexer_purge_tokens_after (parser->lexer, start);
3658     }
3659
3660   pop_deferring_access_checks ();
3661   return success ? parser->scope : NULL_TREE;
3662 }
3663
3664 /* Parse a nested-name-specifier.  See
3665    cp_parser_nested_name_specifier_opt for details.  This function
3666    behaves identically, except that it will an issue an error if no
3667    nested-name-specifier is present, and it will return
3668    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3669    is present.  */
3670
3671 static tree
3672 cp_parser_nested_name_specifier (cp_parser *parser,
3673                                  bool typename_keyword_p,
3674                                  bool check_dependency_p,
3675                                  bool type_p,
3676                                  bool is_declaration)
3677 {
3678   tree scope;
3679
3680   /* Look for the nested-name-specifier.  */
3681   scope = cp_parser_nested_name_specifier_opt (parser,
3682                                                typename_keyword_p,
3683                                                check_dependency_p,
3684                                                type_p,
3685                                                is_declaration);
3686   /* If it was not present, issue an error message.  */
3687   if (!scope)
3688     {
3689       cp_parser_error (parser, "expected nested-name-specifier");
3690       parser->scope = NULL_TREE;
3691       return error_mark_node;
3692     }
3693
3694   return scope;
3695 }
3696
3697 /* Parse a class-or-namespace-name.
3698
3699    class-or-namespace-name:
3700      class-name
3701      namespace-name
3702
3703    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3704    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3705    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3706    TYPE_P is TRUE iff the next name should be taken as a class-name,
3707    even the same name is declared to be another entity in the same
3708    scope.
3709
3710    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3711    specified by the class-or-namespace-name.  If neither is found the
3712    ERROR_MARK_NODE is returned.  */
3713
3714 static tree
3715 cp_parser_class_or_namespace_name (cp_parser *parser,
3716                                    bool typename_keyword_p,
3717                                    bool template_keyword_p,
3718                                    bool check_dependency_p,
3719                                    bool type_p,
3720                                    bool is_declaration)
3721 {
3722   tree saved_scope;
3723   tree saved_qualifying_scope;
3724   tree saved_object_scope;
3725   tree scope;
3726   bool only_class_p;
3727
3728   /* Before we try to parse the class-name, we must save away the
3729      current PARSER->SCOPE since cp_parser_class_name will destroy
3730      it.  */
3731   saved_scope = parser->scope;
3732   saved_qualifying_scope = parser->qualifying_scope;
3733   saved_object_scope = parser->object_scope;
3734   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3735      there is no need to look for a namespace-name.  */
3736   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3737   if (!only_class_p)
3738     cp_parser_parse_tentatively (parser);
3739   scope = cp_parser_class_name (parser,
3740                                 typename_keyword_p,
3741                                 template_keyword_p,
3742                                 type_p ? class_type : none_type,
3743                                 check_dependency_p,
3744                                 /*class_head_p=*/false,
3745                                 is_declaration);
3746   /* If that didn't work, try for a namespace-name.  */
3747   if (!only_class_p && !cp_parser_parse_definitely (parser))
3748     {
3749       /* Restore the saved scope.  */
3750       parser->scope = saved_scope;
3751       parser->qualifying_scope = saved_qualifying_scope;
3752       parser->object_scope = saved_object_scope;
3753       /* If we are not looking at an identifier followed by the scope
3754          resolution operator, then this is not part of a
3755          nested-name-specifier.  (Note that this function is only used
3756          to parse the components of a nested-name-specifier.)  */
3757       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3758           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3759         return error_mark_node;
3760       scope = cp_parser_namespace_name (parser);
3761     }
3762
3763   return scope;
3764 }
3765
3766 /* Parse a postfix-expression.
3767
3768    postfix-expression:
3769      primary-expression
3770      postfix-expression [ expression ]
3771      postfix-expression ( expression-list [opt] )
3772      simple-type-specifier ( expression-list [opt] )
3773      typename :: [opt] nested-name-specifier identifier
3774        ( expression-list [opt] )
3775      typename :: [opt] nested-name-specifier template [opt] template-id
3776        ( expression-list [opt] )
3777      postfix-expression . template [opt] id-expression
3778      postfix-expression -> template [opt] id-expression
3779      postfix-expression . pseudo-destructor-name
3780      postfix-expression -> pseudo-destructor-name
3781      postfix-expression ++
3782      postfix-expression --
3783      dynamic_cast < type-id > ( expression )
3784      static_cast < type-id > ( expression )
3785      reinterpret_cast < type-id > ( expression )
3786      const_cast < type-id > ( expression )
3787      typeid ( expression )
3788      typeid ( type-id )
3789
3790    GNU Extension:
3791
3792    postfix-expression:
3793      ( type-id ) { initializer-list , [opt] }
3794
3795    This extension is a GNU version of the C99 compound-literal
3796    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3797    but they are essentially the same concept.)
3798
3799    If ADDRESS_P is true, the postfix expression is the operand of the
3800    `&' operator.  CAST_P is true if this expression is the target of a
3801    cast. 
3802
3803    Returns a representation of the expression.  */
3804
3805 static tree
3806 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p)
3807 {
3808   cp_token *token;
3809   enum rid keyword;
3810   cp_id_kind idk = CP_ID_KIND_NONE;
3811   tree postfix_expression = NULL_TREE;
3812   /* Non-NULL only if the current postfix-expression can be used to
3813      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3814      class used to qualify the member.  */
3815   tree qualifying_class = NULL_TREE;
3816
3817   /* Peek at the next token.  */
3818   token = cp_lexer_peek_token (parser->lexer);
3819   /* Some of the productions are determined by keywords.  */
3820   keyword = token->keyword;
3821   switch (keyword)
3822     {
3823     case RID_DYNCAST:
3824     case RID_STATCAST:
3825     case RID_REINTCAST:
3826     case RID_CONSTCAST:
3827       {
3828         tree type;
3829         tree expression;
3830         const char *saved_message;
3831
3832         /* All of these can be handled in the same way from the point
3833            of view of parsing.  Begin by consuming the token
3834            identifying the cast.  */
3835         cp_lexer_consume_token (parser->lexer);
3836
3837         /* New types cannot be defined in the cast.  */
3838         saved_message = parser->type_definition_forbidden_message;
3839         parser->type_definition_forbidden_message
3840           = "types may not be defined in casts";
3841
3842         /* Look for the opening `<'.  */
3843         cp_parser_require (parser, CPP_LESS, "`<'");
3844         /* Parse the type to which we are casting.  */
3845         type = cp_parser_type_id (parser);
3846         /* Look for the closing `>'.  */
3847         cp_parser_require (parser, CPP_GREATER, "`>'");
3848         /* Restore the old message.  */
3849         parser->type_definition_forbidden_message = saved_message;
3850
3851         /* And the expression which is being cast.  */
3852         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3853         expression = cp_parser_expression (parser, /*cast_p=*/true);
3854         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3855
3856         /* Only type conversions to integral or enumeration types
3857            can be used in constant-expressions.  */
3858         if (parser->integral_constant_expression_p
3859             && !dependent_type_p (type)
3860             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3861             && (cp_parser_non_integral_constant_expression
3862                 (parser,
3863                  "a cast to a type other than an integral or "
3864                  "enumeration type")))
3865           return error_mark_node;
3866
3867         switch (keyword)
3868           {
3869           case RID_DYNCAST:
3870             postfix_expression
3871               = build_dynamic_cast (type, expression);
3872             break;
3873           case RID_STATCAST:
3874             postfix_expression
3875               = build_static_cast (type, expression);
3876             break;
3877           case RID_REINTCAST:
3878             postfix_expression
3879               = build_reinterpret_cast (type, expression);
3880             break;
3881           case RID_CONSTCAST:
3882             postfix_expression
3883               = build_const_cast (type, expression);
3884             break;
3885           default:
3886             gcc_unreachable ();
3887           }
3888       }
3889       break;
3890
3891     case RID_TYPEID:
3892       {
3893         tree type;
3894         const char *saved_message;
3895         bool saved_in_type_id_in_expr_p;
3896
3897         /* Consume the `typeid' token.  */
3898         cp_lexer_consume_token (parser->lexer);
3899         /* Look for the `(' token.  */
3900         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3901         /* Types cannot be defined in a `typeid' expression.  */
3902         saved_message = parser->type_definition_forbidden_message;
3903         parser->type_definition_forbidden_message
3904           = "types may not be defined in a `typeid\' expression";
3905         /* We can't be sure yet whether we're looking at a type-id or an
3906            expression.  */
3907         cp_parser_parse_tentatively (parser);
3908         /* Try a type-id first.  */
3909         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3910         parser->in_type_id_in_expr_p = true;
3911         type = cp_parser_type_id (parser);
3912         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3913         /* Look for the `)' token.  Otherwise, we can't be sure that
3914            we're not looking at an expression: consider `typeid (int
3915            (3))', for example.  */
3916         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3917         /* If all went well, simply lookup the type-id.  */
3918         if (cp_parser_parse_definitely (parser))
3919           postfix_expression = get_typeid (type);
3920         /* Otherwise, fall back to the expression variant.  */
3921         else
3922           {
3923             tree expression;
3924
3925             /* Look for an expression.  */
3926             expression = cp_parser_expression (parser, /*cast_p=*/false);
3927             /* Compute its typeid.  */
3928             postfix_expression = build_typeid (expression);
3929             /* Look for the `)' token.  */
3930             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3931           }
3932         /* `typeid' may not appear in an integral constant expression.  */
3933         if (cp_parser_non_integral_constant_expression(parser,
3934                                                        "`typeid' operator"))
3935           return error_mark_node;
3936         /* Restore the saved message.  */
3937         parser->type_definition_forbidden_message = saved_message;
3938       }
3939       break;
3940
3941     case RID_TYPENAME:
3942       {
3943         bool template_p = false;
3944         tree id;
3945         tree type;
3946         tree scope;
3947
3948         /* Consume the `typename' token.  */
3949         cp_lexer_consume_token (parser->lexer);
3950         /* Look for the optional `::' operator.  */
3951         cp_parser_global_scope_opt (parser,
3952                                     /*current_scope_valid_p=*/false);
3953         /* Look for the nested-name-specifier.  In case of error here,
3954            consume the trailing id to avoid subsequent error messages
3955            for usual cases.  */
3956         scope = cp_parser_nested_name_specifier (parser,
3957                                                  /*typename_keyword_p=*/true,
3958                                                  /*check_dependency_p=*/true,
3959                                                  /*type_p=*/true,
3960                                                  /*is_declaration=*/true);
3961
3962         /* Look for the optional `template' keyword.  */
3963         template_p = cp_parser_optional_template_keyword (parser);
3964         /* We don't know whether we're looking at a template-id or an
3965            identifier.  */
3966         cp_parser_parse_tentatively (parser);
3967         /* Try a template-id.  */
3968         id = cp_parser_template_id (parser, template_p,
3969                                     /*check_dependency_p=*/true,
3970                                     /*is_declaration=*/true);
3971         /* If that didn't work, try an identifier.  */
3972         if (!cp_parser_parse_definitely (parser))
3973           id = cp_parser_identifier (parser);
3974
3975         /* Don't process id if nested name specifier is invalid.  */
3976         if (scope == error_mark_node)
3977           return error_mark_node;
3978         /* If we look up a template-id in a non-dependent qualifying
3979            scope, there's no need to create a dependent type.  */
3980         else if (TREE_CODE (id) == TYPE_DECL
3981             && !dependent_type_p (parser->scope))
3982           type = TREE_TYPE (id);
3983         /* Create a TYPENAME_TYPE to represent the type to which the
3984            functional cast is being performed.  */
3985         else
3986           type = make_typename_type (parser->scope, id,
3987                                      typename_type,
3988                                      /*complain=*/1);
3989
3990         postfix_expression = cp_parser_functional_cast (parser, type);
3991       }
3992       break;
3993
3994     default:
3995       {
3996         tree type;
3997
3998         /* If the next thing is a simple-type-specifier, we may be
3999            looking at a functional cast.  We could also be looking at
4000            an id-expression.  So, we try the functional cast, and if
4001            that doesn't work we fall back to the primary-expression.  */
4002         cp_parser_parse_tentatively (parser);
4003         /* Look for the simple-type-specifier.  */
4004         type = cp_parser_simple_type_specifier (parser,
4005                                                 /*decl_specs=*/NULL,
4006                                                 CP_PARSER_FLAGS_NONE);
4007         /* Parse the cast itself.  */
4008         if (!cp_parser_error_occurred (parser))
4009           postfix_expression
4010             = cp_parser_functional_cast (parser, type);
4011         /* If that worked, we're done.  */
4012         if (cp_parser_parse_definitely (parser))
4013           break;
4014
4015         /* If the functional-cast didn't work out, try a
4016            compound-literal.  */
4017         if (cp_parser_allow_gnu_extensions_p (parser)
4018             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4019           {
4020             tree initializer_list = NULL_TREE;
4021             bool saved_in_type_id_in_expr_p;
4022
4023             cp_parser_parse_tentatively (parser);
4024             /* Consume the `('.  */
4025             cp_lexer_consume_token (parser->lexer);
4026             /* Parse the type.  */
4027             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4028             parser->in_type_id_in_expr_p = true;
4029             type = cp_parser_type_id (parser);
4030             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4031             /* Look for the `)'.  */
4032             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4033             /* Look for the `{'.  */
4034             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
4035             /* If things aren't going well, there's no need to
4036                keep going.  */
4037             if (!cp_parser_error_occurred (parser))
4038               {
4039                 bool non_constant_p;
4040                 /* Parse the initializer-list.  */
4041                 initializer_list
4042                   = cp_parser_initializer_list (parser, &non_constant_p);
4043                 /* Allow a trailing `,'.  */
4044                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4045                   cp_lexer_consume_token (parser->lexer);
4046                 /* Look for the final `}'.  */
4047                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
4048               }
4049             /* If that worked, we're definitely looking at a
4050                compound-literal expression.  */
4051             if (cp_parser_parse_definitely (parser))
4052               {
4053                 /* Warn the user that a compound literal is not
4054                    allowed in standard C++.  */
4055                 if (pedantic)
4056                   pedwarn ("ISO C++ forbids compound-literals");
4057                 /* Form the representation of the compound-literal.  */
4058                 postfix_expression
4059                   = finish_compound_literal (type, initializer_list);
4060                 break;
4061               }
4062           }
4063
4064         /* It must be a primary-expression.  */
4065         postfix_expression = cp_parser_primary_expression (parser,
4066                                                            cast_p,
4067                                                            &idk,
4068                                                            &qualifying_class);
4069       }
4070       break;
4071     }
4072
4073   /* If we were avoiding committing to the processing of a
4074      qualified-id until we knew whether or not we had a
4075      pointer-to-member, we now know.  */
4076   if (qualifying_class)
4077     {
4078       bool done;
4079
4080       /* Peek at the next token.  */
4081       token = cp_lexer_peek_token (parser->lexer);
4082       done = (token->type != CPP_OPEN_SQUARE
4083               && token->type != CPP_OPEN_PAREN
4084               && token->type != CPP_DOT
4085               && token->type != CPP_DEREF
4086               && token->type != CPP_PLUS_PLUS
4087               && token->type != CPP_MINUS_MINUS);
4088
4089       postfix_expression = finish_qualified_id_expr (qualifying_class,
4090                                                      postfix_expression,
4091                                                      done,
4092                                                      address_p);
4093       if (done)
4094         return postfix_expression;
4095     }
4096
4097   /* Keep looping until the postfix-expression is complete.  */
4098   while (true)
4099     {
4100       if (idk == CP_ID_KIND_UNQUALIFIED
4101           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4102           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4103         /* It is not a Koenig lookup function call.  */
4104         postfix_expression
4105           = unqualified_name_lookup_error (postfix_expression);
4106
4107       /* Peek at the next token.  */
4108       token = cp_lexer_peek_token (parser->lexer);
4109
4110       switch (token->type)
4111         {
4112         case CPP_OPEN_SQUARE:
4113           postfix_expression
4114             = cp_parser_postfix_open_square_expression (parser,
4115                                                         postfix_expression,
4116                                                         false);
4117           idk = CP_ID_KIND_NONE;
4118           break;
4119
4120         case CPP_OPEN_PAREN:
4121           /* postfix-expression ( expression-list [opt] ) */
4122           {
4123             bool koenig_p;
4124             tree args = (cp_parser_parenthesized_expression_list
4125                          (parser, false, 
4126                           /*cast_p=*/false,
4127                           /*non_constant_p=*/NULL));
4128
4129             if (args == error_mark_node)
4130               {
4131                 postfix_expression = error_mark_node;
4132                 break;
4133               }
4134
4135             /* Function calls are not permitted in
4136                constant-expressions.  */
4137             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4138                 && cp_parser_non_integral_constant_expression (parser,
4139                                                                "a function call"))
4140               {
4141                 postfix_expression = error_mark_node;
4142                 break;
4143               }
4144
4145             koenig_p = false;
4146             if (idk == CP_ID_KIND_UNQUALIFIED)
4147               {
4148                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4149                   {
4150                     if (args)
4151                       {
4152                         koenig_p = true;
4153                         postfix_expression
4154                           = perform_koenig_lookup (postfix_expression, args);
4155                       }
4156                     else
4157                       postfix_expression
4158                         = unqualified_fn_lookup_error (postfix_expression);
4159                   }
4160                 /* We do not perform argument-dependent lookup if
4161                    normal lookup finds a non-function, in accordance
4162                    with the expected resolution of DR 218.  */
4163                 else if (args && is_overloaded_fn (postfix_expression))
4164                   {
4165                     tree fn = get_first_fn (postfix_expression);
4166
4167                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4168                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4169
4170                     /* Only do argument dependent lookup if regular
4171                        lookup does not find a set of member functions.
4172                        [basic.lookup.koenig]/2a  */
4173                     if (!DECL_FUNCTION_MEMBER_P (fn))
4174                       {
4175                         koenig_p = true;
4176                         postfix_expression
4177                           = perform_koenig_lookup (postfix_expression, args);
4178                       }
4179                   }
4180               }
4181
4182             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4183               {
4184                 tree instance = TREE_OPERAND (postfix_expression, 0);
4185                 tree fn = TREE_OPERAND (postfix_expression, 1);
4186
4187                 if (processing_template_decl
4188                     && (type_dependent_expression_p (instance)
4189                         || (!BASELINK_P (fn)
4190                             && TREE_CODE (fn) != FIELD_DECL)
4191                         || type_dependent_expression_p (fn)
4192                         || any_type_dependent_arguments_p (args)))
4193                   {
4194                     postfix_expression
4195                       = build_min_nt (CALL_EXPR, postfix_expression,
4196                                       args, NULL_TREE);
4197                     break;
4198                   }
4199
4200                 if (BASELINK_P (fn))
4201                   postfix_expression
4202                     = (build_new_method_call
4203                        (instance, fn, args, NULL_TREE,
4204                         (idk == CP_ID_KIND_QUALIFIED
4205                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4206                 else
4207                   postfix_expression
4208                     = finish_call_expr (postfix_expression, args,
4209                                         /*disallow_virtual=*/false,
4210                                         /*koenig_p=*/false);
4211               }
4212             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4213                      || TREE_CODE (postfix_expression) == MEMBER_REF
4214                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4215               postfix_expression = (build_offset_ref_call_from_tree
4216                                     (postfix_expression, args));
4217             else if (idk == CP_ID_KIND_QUALIFIED)
4218               /* A call to a static class member, or a namespace-scope
4219                  function.  */
4220               postfix_expression
4221                 = finish_call_expr (postfix_expression, args,
4222                                     /*disallow_virtual=*/true,
4223                                     koenig_p);
4224             else
4225               /* All other function calls.  */
4226               postfix_expression
4227                 = finish_call_expr (postfix_expression, args,
4228                                     /*disallow_virtual=*/false,
4229                                     koenig_p);
4230
4231             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4232             idk = CP_ID_KIND_NONE;
4233           }
4234           break;
4235
4236         case CPP_DOT:
4237         case CPP_DEREF:
4238           /* postfix-expression . template [opt] id-expression
4239              postfix-expression . pseudo-destructor-name
4240              postfix-expression -> template [opt] id-expression
4241              postfix-expression -> pseudo-destructor-name */
4242
4243           /* Consume the `.' or `->' operator.  */
4244           cp_lexer_consume_token (parser->lexer);
4245
4246           postfix_expression
4247             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4248                                                       postfix_expression,
4249                                                       false, &idk);
4250           break;
4251
4252         case CPP_PLUS_PLUS:
4253           /* postfix-expression ++  */
4254           /* Consume the `++' token.  */
4255           cp_lexer_consume_token (parser->lexer);
4256           /* Generate a representation for the complete expression.  */
4257           postfix_expression
4258             = finish_increment_expr (postfix_expression,
4259                                      POSTINCREMENT_EXPR);
4260           /* Increments may not appear in constant-expressions.  */
4261           if (cp_parser_non_integral_constant_expression (parser,
4262                                                           "an increment"))
4263             postfix_expression = error_mark_node;
4264           idk = CP_ID_KIND_NONE;
4265           break;
4266
4267         case CPP_MINUS_MINUS:
4268           /* postfix-expression -- */
4269           /* Consume the `--' token.  */
4270           cp_lexer_consume_token (parser->lexer);
4271           /* Generate a representation for the complete expression.  */
4272           postfix_expression
4273             = finish_increment_expr (postfix_expression,
4274                                      POSTDECREMENT_EXPR);
4275           /* Decrements may not appear in constant-expressions.  */
4276           if (cp_parser_non_integral_constant_expression (parser,
4277                                                           "a decrement"))
4278             postfix_expression = error_mark_node;
4279           idk = CP_ID_KIND_NONE;
4280           break;
4281
4282         default:
4283           return postfix_expression;
4284         }
4285     }
4286
4287   /* We should never get here.  */
4288   gcc_unreachable ();
4289   return error_mark_node;
4290 }
4291
4292 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4293    by cp_parser_builtin_offsetof.  We're looking for
4294
4295      postfix-expression [ expression ]
4296
4297    FOR_OFFSETOF is set if we're being called in that context, which
4298    changes how we deal with integer constant expressions.  */
4299
4300 static tree
4301 cp_parser_postfix_open_square_expression (cp_parser *parser,
4302                                           tree postfix_expression,
4303                                           bool for_offsetof)
4304 {
4305   tree index;
4306
4307   /* Consume the `[' token.  */
4308   cp_lexer_consume_token (parser->lexer);
4309
4310   /* Parse the index expression.  */
4311   /* ??? For offsetof, there is a question of what to allow here.  If
4312      offsetof is not being used in an integral constant expression context,
4313      then we *could* get the right answer by computing the value at runtime.
4314      If we are in an integral constant expression context, then we might
4315      could accept any constant expression; hard to say without analysis.
4316      Rather than open the barn door too wide right away, allow only integer
4317      constant expressions here.  */
4318   if (for_offsetof)
4319     index = cp_parser_constant_expression (parser, false, NULL);
4320   else
4321     index = cp_parser_expression (parser, /*cast_p=*/false);
4322
4323   /* Look for the closing `]'.  */
4324   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4325
4326   /* Build the ARRAY_REF.  */
4327   postfix_expression = grok_array_decl (postfix_expression, index);
4328
4329   /* When not doing offsetof, array references are not permitted in
4330      constant-expressions.  */
4331   if (!for_offsetof
4332       && (cp_parser_non_integral_constant_expression
4333           (parser, "an array reference")))
4334     postfix_expression = error_mark_node;
4335
4336   return postfix_expression;
4337 }
4338
4339 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4340    by cp_parser_builtin_offsetof.  We're looking for
4341
4342      postfix-expression . template [opt] id-expression
4343      postfix-expression . pseudo-destructor-name
4344      postfix-expression -> template [opt] id-expression
4345      postfix-expression -> pseudo-destructor-name
4346
4347    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4348    limits what of the above we'll actually accept, but nevermind.
4349    TOKEN_TYPE is the "." or "->" token, which will already have been
4350    removed from the stream.  */
4351
4352 static tree
4353 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4354                                         enum cpp_ttype token_type,
4355                                         tree postfix_expression,
4356                                         bool for_offsetof, cp_id_kind *idk)
4357 {
4358   tree name;
4359   bool dependent_p;
4360   bool template_p;
4361   bool pseudo_destructor_p;
4362   tree scope = NULL_TREE;
4363
4364   /* If this is a `->' operator, dereference the pointer.  */
4365   if (token_type == CPP_DEREF)
4366     postfix_expression = build_x_arrow (postfix_expression);
4367   /* Check to see whether or not the expression is type-dependent.  */
4368   dependent_p = type_dependent_expression_p (postfix_expression);
4369   /* The identifier following the `->' or `.' is not qualified.  */
4370   parser->scope = NULL_TREE;
4371   parser->qualifying_scope = NULL_TREE;
4372   parser->object_scope = NULL_TREE;
4373   *idk = CP_ID_KIND_NONE;
4374   /* Enter the scope corresponding to the type of the object
4375      given by the POSTFIX_EXPRESSION.  */
4376   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4377     {
4378       scope = TREE_TYPE (postfix_expression);
4379       /* According to the standard, no expression should ever have
4380          reference type.  Unfortunately, we do not currently match
4381          the standard in this respect in that our internal representation
4382          of an expression may have reference type even when the standard
4383          says it does not.  Therefore, we have to manually obtain the
4384          underlying type here.  */
4385       scope = non_reference (scope);
4386       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4387       scope = complete_type_or_else (scope, NULL_TREE);
4388       /* Let the name lookup machinery know that we are processing a
4389          class member access expression.  */
4390       parser->context->object_type = scope;
4391       /* If something went wrong, we want to be able to discern that case,
4392          as opposed to the case where there was no SCOPE due to the type
4393          of expression being dependent.  */
4394       if (!scope)
4395         scope = error_mark_node;
4396       /* If the SCOPE was erroneous, make the various semantic analysis
4397          functions exit quickly -- and without issuing additional error
4398          messages.  */
4399       if (scope == error_mark_node)
4400         postfix_expression = error_mark_node;
4401     }
4402
4403   /* Assume this expression is not a pseudo-destructor access.  */
4404   pseudo_destructor_p = false;
4405
4406   /* If the SCOPE is a scalar type, then, if this is a valid program,
4407      we must be looking at a pseudo-destructor-name.  */
4408   if (scope && SCALAR_TYPE_P (scope))
4409     {
4410       tree s;
4411       tree type;
4412
4413       cp_parser_parse_tentatively (parser);
4414       /* Parse the pseudo-destructor-name.  */
4415       s = NULL_TREE;
4416       cp_parser_pseudo_destructor_name (parser, &s, &type);
4417       if (cp_parser_parse_definitely (parser))
4418         {
4419           pseudo_destructor_p = true;
4420           postfix_expression
4421             = finish_pseudo_destructor_expr (postfix_expression,
4422                                              s, TREE_TYPE (type));
4423         }
4424     }
4425
4426   if (!pseudo_destructor_p)
4427     {
4428       /* If the SCOPE is not a scalar type, we are looking at an
4429          ordinary class member access expression, rather than a
4430          pseudo-destructor-name.  */
4431       template_p = cp_parser_optional_template_keyword (parser);
4432       /* Parse the id-expression.  */
4433       name = cp_parser_id_expression (parser, template_p,
4434                                       /*check_dependency_p=*/true,
4435                                       /*template_p=*/NULL,
4436                                       /*declarator_p=*/false);
4437       /* In general, build a SCOPE_REF if the member name is qualified.
4438          However, if the name was not dependent and has already been
4439          resolved; there is no need to build the SCOPE_REF.  For example;
4440
4441              struct X { void f(); };
4442              template <typename T> void f(T* t) { t->X::f(); }
4443
4444          Even though "t" is dependent, "X::f" is not and has been resolved
4445          to a BASELINK; there is no need to include scope information.  */
4446
4447       /* But we do need to remember that there was an explicit scope for
4448          virtual function calls.  */
4449       if (parser->scope)
4450         *idk = CP_ID_KIND_QUALIFIED;
4451
4452       /* If the name is a template-id that names a type, we will get a
4453          TYPE_DECL here.  That is invalid code.  */
4454       if (TREE_CODE (name) == TYPE_DECL)
4455         {
4456           error ("invalid use of %qD", name);
4457           postfix_expression = error_mark_node;
4458         }
4459       else
4460         {
4461           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4462             {
4463               name = build_nt (SCOPE_REF, parser->scope, name);
4464               parser->scope = NULL_TREE;
4465               parser->qualifying_scope = NULL_TREE;
4466               parser->object_scope = NULL_TREE;
4467             }
4468           if (scope && name && BASELINK_P (name))
4469             adjust_result_of_qualified_name_lookup
4470               (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4471           postfix_expression
4472             = finish_class_member_access_expr (postfix_expression, name);
4473         }
4474     }
4475
4476   /* We no longer need to look up names in the scope of the object on
4477      the left-hand side of the `.' or `->' operator.  */
4478   parser->context->object_type = NULL_TREE;
4479
4480   /* Outside of offsetof, these operators may not appear in
4481      constant-expressions.  */
4482   if (!for_offsetof
4483       && (cp_parser_non_integral_constant_expression
4484           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4485     postfix_expression = error_mark_node;
4486
4487   return postfix_expression;
4488 }
4489
4490 /* Parse a parenthesized expression-list.
4491
4492    expression-list:
4493      assignment-expression
4494      expression-list, assignment-expression
4495
4496    attribute-list:
4497      expression-list
4498      identifier
4499      identifier, expression-list
4500
4501    CAST_P is true if this expression is the target of a cast.
4502
4503    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4504    representation of an assignment-expression.  Note that a TREE_LIST
4505    is returned even if there is only a single expression in the list.
4506    error_mark_node is returned if the ( and or ) are
4507    missing. NULL_TREE is returned on no expressions. The parentheses
4508    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4509    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4510    indicates whether or not all of the expressions in the list were
4511    constant.  */
4512
4513 static tree
4514 cp_parser_parenthesized_expression_list (cp_parser* parser,
4515                                          bool is_attribute_list,
4516                                          bool cast_p,
4517                                          bool *non_constant_p)
4518 {
4519   tree expression_list = NULL_TREE;
4520   bool fold_expr_p = is_attribute_list;
4521   tree identifier = NULL_TREE;
4522
4523   /* Assume all the expressions will be constant.  */
4524   if (non_constant_p)
4525     *non_constant_p = false;
4526
4527   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4528     return error_mark_node;
4529
4530   /* Consume expressions until there are no more.  */
4531   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4532     while (true)
4533       {
4534         tree expr;
4535
4536         /* At the beginning of attribute lists, check to see if the
4537            next token is an identifier.  */
4538         if (is_attribute_list
4539             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4540           {
4541             cp_token *token;
4542
4543             /* Consume the identifier.  */
4544             token = cp_lexer_consume_token (parser->lexer);
4545             /* Save the identifier.  */
4546             identifier = token->value;
4547           }
4548         else
4549           {
4550             /* Parse the next assignment-expression.  */
4551             if (non_constant_p)
4552               {
4553                 bool expr_non_constant_p;
4554                 expr = (cp_parser_constant_expression
4555                         (parser, /*allow_non_constant_p=*/true,
4556                          &expr_non_constant_p));
4557                 if (expr_non_constant_p)
4558                   *non_constant_p = true;
4559               }
4560             else
4561               expr = cp_parser_assignment_expression (parser, cast_p);
4562
4563             if (fold_expr_p)
4564               expr = fold_non_dependent_expr (expr);
4565
4566              /* Add it to the list.  We add error_mark_node
4567                 expressions to the list, so that we can still tell if
4568                 the correct form for a parenthesized expression-list
4569                 is found. That gives better errors.  */
4570             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4571
4572             if (expr == error_mark_node)
4573               goto skip_comma;
4574           }
4575
4576         /* After the first item, attribute lists look the same as
4577            expression lists.  */
4578         is_attribute_list = false;
4579
4580       get_comma:;
4581         /* If the next token isn't a `,', then we are done.  */
4582         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4583           break;
4584
4585         /* Otherwise, consume the `,' and keep going.  */
4586         cp_lexer_consume_token (parser->lexer);
4587       }
4588
4589   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4590     {
4591       int ending;
4592
4593     skip_comma:;
4594       /* We try and resync to an unnested comma, as that will give the
4595          user better diagnostics.  */
4596       ending = cp_parser_skip_to_closing_parenthesis (parser,
4597                                                       /*recovering=*/true,
4598                                                       /*or_comma=*/true,
4599                                                       /*consume_paren=*/true);
4600       if (ending < 0)
4601         goto get_comma;
4602       if (!ending)
4603         return error_mark_node;
4604     }
4605
4606   /* We built up the list in reverse order so we must reverse it now.  */
4607   expression_list = nreverse (expression_list);
4608   if (identifier)
4609     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4610
4611   return expression_list;
4612 }
4613
4614 /* Parse a pseudo-destructor-name.
4615
4616    pseudo-destructor-name:
4617      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4618      :: [opt] nested-name-specifier template template-id :: ~ type-name
4619      :: [opt] nested-name-specifier [opt] ~ type-name
4620
4621    If either of the first two productions is used, sets *SCOPE to the
4622    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4623    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4624    or ERROR_MARK_NODE if the parse fails.  */
4625
4626 static void
4627 cp_parser_pseudo_destructor_name (cp_parser* parser,
4628                                   tree* scope,
4629                                   tree* type)
4630 {
4631   bool nested_name_specifier_p;
4632
4633   /* Assume that things will not work out.  */
4634   *type = error_mark_node;
4635
4636   /* Look for the optional `::' operator.  */
4637   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4638   /* Look for the optional nested-name-specifier.  */
4639   nested_name_specifier_p
4640     = (cp_parser_nested_name_specifier_opt (parser,
4641                                             /*typename_keyword_p=*/false,
4642                                             /*check_dependency_p=*/true,
4643                                             /*type_p=*/false,
4644                                             /*is_declaration=*/true)
4645        != NULL_TREE);
4646   /* Now, if we saw a nested-name-specifier, we might be doing the
4647      second production.  */
4648   if (nested_name_specifier_p
4649       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4650     {
4651       /* Consume the `template' keyword.  */
4652       cp_lexer_consume_token (parser->lexer);
4653       /* Parse the template-id.  */
4654       cp_parser_template_id (parser,
4655                              /*template_keyword_p=*/true,
4656                              /*check_dependency_p=*/false,
4657                              /*is_declaration=*/true);
4658       /* Look for the `::' token.  */
4659       cp_parser_require (parser, CPP_SCOPE, "`::'");
4660     }
4661   /* If the next token is not a `~', then there might be some
4662      additional qualification.  */
4663   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4664     {
4665       /* Look for the type-name.  */
4666       *scope = TREE_TYPE (cp_parser_type_name (parser));
4667
4668       if (*scope == error_mark_node)
4669         return;
4670
4671       /* If we don't have ::~, then something has gone wrong.  Since
4672          the only caller of this function is looking for something
4673          after `.' or `->' after a scalar type, most likely the
4674          program is trying to get a member of a non-aggregate
4675          type.  */
4676       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4677           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4678         {
4679           cp_parser_error (parser, "request for member of non-aggregate type");
4680           return;
4681         }
4682
4683       /* Look for the `::' token.  */
4684       cp_parser_require (parser, CPP_SCOPE, "`::'");
4685     }
4686   else
4687     *scope = NULL_TREE;
4688
4689   /* Look for the `~'.  */
4690   cp_parser_require (parser, CPP_COMPL, "`~'");
4691   /* Look for the type-name again.  We are not responsible for
4692      checking that it matches the first type-name.  */
4693   *type = cp_parser_type_name (parser);
4694 }
4695
4696 /* Parse a unary-expression.
4697
4698    unary-expression:
4699      postfix-expression
4700      ++ cast-expression
4701      -- cast-expression
4702      unary-operator cast-expression
4703      sizeof unary-expression
4704      sizeof ( type-id )
4705      new-expression
4706      delete-expression
4707
4708    GNU Extensions:
4709
4710    unary-expression:
4711      __extension__ cast-expression
4712      __alignof__ unary-expression
4713      __alignof__ ( type-id )
4714      __real__ cast-expression
4715      __imag__ cast-expression
4716      && identifier
4717
4718    ADDRESS_P is true iff the unary-expression is appearing as the
4719    operand of the `&' operator.   CAST_P is true if this expression is
4720    the target of a cast.
4721
4722    Returns a representation of the expression.  */
4723
4724 static tree
4725 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p)
4726 {
4727   cp_token *token;
4728   enum tree_code unary_operator;
4729
4730   /* Peek at the next token.  */
4731   token = cp_lexer_peek_token (parser->lexer);
4732   /* Some keywords give away the kind of expression.  */
4733   if (token->type == CPP_KEYWORD)
4734     {
4735       enum rid keyword = token->keyword;
4736
4737       switch (keyword)
4738         {
4739         case RID_ALIGNOF:
4740         case RID_SIZEOF:
4741           {
4742             tree operand;
4743             enum tree_code op;
4744
4745             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4746             /* Consume the token.  */
4747             cp_lexer_consume_token (parser->lexer);
4748             /* Parse the operand.  */
4749             operand = cp_parser_sizeof_operand (parser, keyword);
4750
4751             if (TYPE_P (operand))
4752               return cxx_sizeof_or_alignof_type (operand, op, true);
4753             else
4754               return cxx_sizeof_or_alignof_expr (operand, op);
4755           }
4756
4757         case RID_NEW:
4758           return cp_parser_new_expression (parser);
4759
4760         case RID_DELETE:
4761           return cp_parser_delete_expression (parser);
4762
4763         case RID_EXTENSION:
4764           {
4765             /* The saved value of the PEDANTIC flag.  */
4766             int saved_pedantic;
4767             tree expr;
4768
4769             /* Save away the PEDANTIC flag.  */
4770             cp_parser_extension_opt (parser, &saved_pedantic);
4771             /* Parse the cast-expression.  */
4772             expr = cp_parser_simple_cast_expression (parser);
4773             /* Restore the PEDANTIC flag.  */
4774             pedantic = saved_pedantic;
4775
4776             return expr;
4777           }
4778
4779         case RID_REALPART:
4780         case RID_IMAGPART:
4781           {
4782             tree expression;
4783
4784             /* Consume the `__real__' or `__imag__' token.  */
4785             cp_lexer_consume_token (parser->lexer);
4786             /* Parse the cast-expression.  */
4787             expression = cp_parser_simple_cast_expression (parser);
4788             /* Create the complete representation.  */
4789             return build_x_unary_op ((keyword == RID_REALPART
4790                                       ? REALPART_EXPR : IMAGPART_EXPR),
4791                                      expression);
4792           }
4793           break;
4794
4795         default:
4796           break;
4797         }
4798     }
4799
4800   /* Look for the `:: new' and `:: delete', which also signal the
4801      beginning of a new-expression, or delete-expression,
4802      respectively.  If the next token is `::', then it might be one of
4803      these.  */
4804   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4805     {
4806       enum rid keyword;
4807
4808       /* See if the token after the `::' is one of the keywords in
4809          which we're interested.  */
4810       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4811       /* If it's `new', we have a new-expression.  */
4812       if (keyword == RID_NEW)
4813         return cp_parser_new_expression (parser);
4814       /* Similarly, for `delete'.  */
4815       else if (keyword == RID_DELETE)
4816         return cp_parser_delete_expression (parser);
4817     }
4818
4819   /* Look for a unary operator.  */
4820   unary_operator = cp_parser_unary_operator (token);
4821   /* The `++' and `--' operators can be handled similarly, even though
4822      they are not technically unary-operators in the grammar.  */
4823   if (unary_operator == ERROR_MARK)
4824     {
4825       if (token->type == CPP_PLUS_PLUS)
4826         unary_operator = PREINCREMENT_EXPR;
4827       else if (token->type == CPP_MINUS_MINUS)
4828         unary_operator = PREDECREMENT_EXPR;
4829       /* Handle the GNU address-of-label extension.  */
4830       else if (cp_parser_allow_gnu_extensions_p (parser)
4831                && token->type == CPP_AND_AND)
4832         {
4833           tree identifier;
4834
4835           /* Consume the '&&' token.  */
4836           cp_lexer_consume_token (parser->lexer);
4837           /* Look for the identifier.  */
4838           identifier = cp_parser_identifier (parser);
4839           /* Create an expression representing the address.  */
4840           return finish_label_address_expr (identifier);
4841         }
4842     }
4843   if (unary_operator != ERROR_MARK)
4844     {
4845       tree cast_expression;
4846       tree expression = error_mark_node;
4847       const char *non_constant_p = NULL;
4848
4849       /* Consume the operator token.  */
4850       token = cp_lexer_consume_token (parser->lexer);
4851       /* Parse the cast-expression.  */
4852       cast_expression
4853         = cp_parser_cast_expression (parser, 
4854                                      unary_operator == ADDR_EXPR,
4855                                      /*cast_p=*/false);
4856       /* Now, build an appropriate representation.  */
4857       switch (unary_operator)
4858         {
4859         case INDIRECT_REF:
4860           non_constant_p = "`*'";
4861           expression = build_x_indirect_ref (cast_expression, "unary *");
4862           break;
4863
4864         case ADDR_EXPR:
4865           non_constant_p = "`&'";
4866           /* Fall through.  */
4867         case BIT_NOT_EXPR:
4868           expression = build_x_unary_op (unary_operator, cast_expression);
4869           break;
4870
4871         case PREINCREMENT_EXPR:
4872         case PREDECREMENT_EXPR:
4873           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4874                             ? "`++'" : "`--'");
4875           /* Fall through.  */
4876         case CONVERT_EXPR:
4877         case NEGATE_EXPR:
4878         case TRUTH_NOT_EXPR:
4879           expression = finish_unary_op_expr (unary_operator, cast_expression);
4880           break;
4881
4882         default:
4883           gcc_unreachable ();
4884         }
4885
4886       if (non_constant_p
4887           && cp_parser_non_integral_constant_expression (parser,
4888                                                          non_constant_p))
4889         expression = error_mark_node;
4890
4891       return expression;
4892     }
4893
4894   return cp_parser_postfix_expression (parser, address_p, cast_p);
4895 }
4896
4897 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4898    unary-operator, the corresponding tree code is returned.  */
4899
4900 static enum tree_code
4901 cp_parser_unary_operator (cp_token* token)
4902 {
4903   switch (token->type)
4904     {
4905     case CPP_MULT:
4906       return INDIRECT_REF;
4907
4908     case CPP_AND:
4909       return ADDR_EXPR;
4910
4911     case CPP_PLUS:
4912       return CONVERT_EXPR;
4913
4914     case CPP_MINUS:
4915       return NEGATE_EXPR;
4916
4917     case CPP_NOT:
4918       return TRUTH_NOT_EXPR;
4919
4920     case CPP_COMPL:
4921       return BIT_NOT_EXPR;
4922
4923     default:
4924       return ERROR_MARK;
4925     }
4926 }
4927
4928 /* Parse a new-expression.
4929
4930    new-expression:
4931      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4932      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4933
4934    Returns a representation of the expression.  */
4935
4936 static tree
4937 cp_parser_new_expression (cp_parser* parser)
4938 {
4939   bool global_scope_p;
4940   tree placement;
4941   tree type;
4942   tree initializer;
4943   tree nelts;
4944
4945   /* Look for the optional `::' operator.  */
4946   global_scope_p
4947     = (cp_parser_global_scope_opt (parser,
4948                                    /*current_scope_valid_p=*/false)
4949        != NULL_TREE);
4950   /* Look for the `new' operator.  */
4951   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4952   /* There's no easy way to tell a new-placement from the
4953      `( type-id )' construct.  */
4954   cp_parser_parse_tentatively (parser);
4955   /* Look for a new-placement.  */
4956   placement = cp_parser_new_placement (parser);
4957   /* If that didn't work out, there's no new-placement.  */
4958   if (!cp_parser_parse_definitely (parser))
4959     placement = NULL_TREE;
4960
4961   /* If the next token is a `(', then we have a parenthesized
4962      type-id.  */
4963   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4964     {
4965       /* Consume the `('.  */
4966       cp_lexer_consume_token (parser->lexer);
4967       /* Parse the type-id.  */
4968       type = cp_parser_type_id (parser);
4969       /* Look for the closing `)'.  */
4970       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4971       /* There should not be a direct-new-declarator in this production,
4972          but GCC used to allowed this, so we check and emit a sensible error
4973          message for this case.  */
4974       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4975         {
4976           error ("array bound forbidden after parenthesized type-id");
4977           inform ("try removing the parentheses around the type-id");
4978           cp_parser_direct_new_declarator (parser);
4979         }
4980       nelts = NULL_TREE;
4981     }
4982   /* Otherwise, there must be a new-type-id.  */
4983   else
4984     type = cp_parser_new_type_id (parser, &nelts);
4985
4986   /* If the next token is a `(', then we have a new-initializer.  */
4987   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4988     initializer = cp_parser_new_initializer (parser);
4989   else
4990     initializer = NULL_TREE;
4991
4992   /* A new-expression may not appear in an integral constant
4993      expression.  */
4994   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4995     return error_mark_node;
4996
4997   /* Create a representation of the new-expression.  */
4998   return build_new (placement, type, nelts, initializer, global_scope_p);
4999 }
5000
5001 /* Parse a new-placement.
5002
5003    new-placement:
5004      ( expression-list )
5005
5006    Returns the same representation as for an expression-list.  */
5007
5008 static tree
5009 cp_parser_new_placement (cp_parser* parser)
5010 {
5011   tree expression_list;
5012
5013   /* Parse the expression-list.  */
5014   expression_list = (cp_parser_parenthesized_expression_list
5015                      (parser, false, /*cast_p=*/false,
5016                       /*non_constant_p=*/NULL));
5017
5018   return expression_list;
5019 }
5020
5021 /* Parse a new-type-id.
5022
5023    new-type-id:
5024      type-specifier-seq new-declarator [opt]
5025
5026    Returns the TYPE allocated.  If the new-type-id indicates an array
5027    type, *NELTS is set to the number of elements in the last array
5028    bound; the TYPE will not include the last array bound.  */
5029
5030 static tree
5031 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5032 {
5033   cp_decl_specifier_seq type_specifier_seq;
5034   cp_declarator *new_declarator;
5035   cp_declarator *declarator;
5036   cp_declarator *outer_declarator;
5037   const char *saved_message;
5038   tree type;
5039
5040   /* The type-specifier sequence must not contain type definitions.
5041      (It cannot contain declarations of new types either, but if they
5042      are not definitions we will catch that because they are not
5043      complete.)  */
5044   saved_message = parser->type_definition_forbidden_message;
5045   parser->type_definition_forbidden_message
5046     = "types may not be defined in a new-type-id";
5047   /* Parse the type-specifier-seq.  */
5048   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5049                                 &type_specifier_seq);
5050   /* Restore the old message.  */
5051   parser->type_definition_forbidden_message = saved_message;
5052   /* Parse the new-declarator.  */
5053   new_declarator = cp_parser_new_declarator_opt (parser);
5054
5055   /* Determine the number of elements in the last array dimension, if
5056      any.  */
5057   *nelts = NULL_TREE;
5058   /* Skip down to the last array dimension.  */
5059   declarator = new_declarator;
5060   outer_declarator = NULL;
5061   while (declarator && (declarator->kind == cdk_pointer
5062                         || declarator->kind == cdk_ptrmem))
5063     {
5064       outer_declarator = declarator;
5065       declarator = declarator->declarator;
5066     }
5067   while (declarator
5068          && declarator->kind == cdk_array
5069          && declarator->declarator
5070          && declarator->declarator->kind == cdk_array)
5071     {
5072       outer_declarator = declarator;
5073       declarator = declarator->declarator;
5074     }
5075
5076   if (declarator && declarator->kind == cdk_array)
5077     {
5078       *nelts = declarator->u.array.bounds;
5079       if (*nelts == error_mark_node)
5080         *nelts = integer_one_node;
5081       
5082       if (outer_declarator)
5083         outer_declarator->declarator = declarator->declarator;
5084       else
5085         new_declarator = NULL;
5086     }
5087
5088   type = groktypename (&type_specifier_seq, new_declarator);
5089   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
5090     {
5091       *nelts = array_type_nelts_top (type);
5092       type = TREE_TYPE (type);
5093     }
5094   return type;
5095 }
5096
5097 /* Parse an (optional) new-declarator.
5098
5099    new-declarator:
5100      ptr-operator new-declarator [opt]
5101      direct-new-declarator
5102
5103    Returns the declarator.  */
5104
5105 static cp_declarator *
5106 cp_parser_new_declarator_opt (cp_parser* parser)
5107 {
5108   enum tree_code code;
5109   tree type;
5110   cp_cv_quals cv_quals;
5111
5112   /* We don't know if there's a ptr-operator next, or not.  */
5113   cp_parser_parse_tentatively (parser);
5114   /* Look for a ptr-operator.  */
5115   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5116   /* If that worked, look for more new-declarators.  */
5117   if (cp_parser_parse_definitely (parser))
5118     {
5119       cp_declarator *declarator;
5120
5121       /* Parse another optional declarator.  */
5122       declarator = cp_parser_new_declarator_opt (parser);
5123
5124       /* Create the representation of the declarator.  */
5125       if (type)
5126         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
5127       else if (code == INDIRECT_REF)
5128         declarator = make_pointer_declarator (cv_quals, declarator);
5129       else
5130         declarator = make_reference_declarator (cv_quals, declarator);
5131
5132       return declarator;
5133     }
5134
5135   /* If the next token is a `[', there is a direct-new-declarator.  */
5136   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5137     return cp_parser_direct_new_declarator (parser);
5138
5139   return NULL;
5140 }
5141
5142 /* Parse a direct-new-declarator.
5143
5144    direct-new-declarator:
5145      [ expression ]
5146      direct-new-declarator [constant-expression]
5147
5148    */
5149
5150 static cp_declarator *
5151 cp_parser_direct_new_declarator (cp_parser* parser)
5152 {
5153   cp_declarator *declarator = NULL;
5154
5155   while (true)
5156     {
5157       tree expression;
5158
5159       /* Look for the opening `['.  */
5160       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
5161       /* The first expression is not required to be constant.  */
5162       if (!declarator)
5163         {
5164           expression = cp_parser_expression (parser, /*cast_p=*/false);
5165           /* The standard requires that the expression have integral
5166              type.  DR 74 adds enumeration types.  We believe that the
5167              real intent is that these expressions be handled like the
5168              expression in a `switch' condition, which also allows
5169              classes with a single conversion to integral or
5170              enumeration type.  */
5171           if (!processing_template_decl)
5172             {
5173               expression
5174                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5175                                               expression,
5176                                               /*complain=*/true);
5177               if (!expression)
5178                 {
5179                   error ("expression in new-declarator must have integral "
5180                          "or enumeration type");
5181                   expression = error_mark_node;
5182                 }
5183             }
5184         }
5185       /* But all the other expressions must be.  */
5186       else
5187         expression
5188           = cp_parser_constant_expression (parser,
5189                                            /*allow_non_constant=*/false,
5190                                            NULL);
5191       /* Look for the closing `]'.  */
5192       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5193
5194       /* Add this bound to the declarator.  */
5195       declarator = make_array_declarator (declarator, expression);
5196
5197       /* If the next token is not a `[', then there are no more
5198          bounds.  */
5199       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5200         break;
5201     }
5202
5203   return declarator;
5204 }
5205
5206 /* Parse a new-initializer.
5207
5208    new-initializer:
5209      ( expression-list [opt] )
5210
5211    Returns a representation of the expression-list.  If there is no
5212    expression-list, VOID_ZERO_NODE is returned.  */
5213
5214 static tree
5215 cp_parser_new_initializer (cp_parser* parser)
5216 {
5217   tree expression_list;
5218
5219   expression_list = (cp_parser_parenthesized_expression_list
5220                      (parser, false, /*cast_p=*/false,
5221                       /*non_constant_p=*/NULL));
5222   if (!expression_list)
5223     expression_list = void_zero_node;
5224
5225   return expression_list;
5226 }
5227
5228 /* Parse a delete-expression.
5229
5230    delete-expression:
5231      :: [opt] delete cast-expression
5232      :: [opt] delete [ ] cast-expression
5233
5234    Returns a representation of the expression.  */
5235
5236 static tree
5237 cp_parser_delete_expression (cp_parser* parser)
5238 {
5239   bool global_scope_p;
5240   bool array_p;
5241   tree expression;
5242
5243   /* Look for the optional `::' operator.  */
5244   global_scope_p
5245     = (cp_parser_global_scope_opt (parser,
5246                                    /*current_scope_valid_p=*/false)
5247        != NULL_TREE);
5248   /* Look for the `delete' keyword.  */
5249   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5250   /* See if the array syntax is in use.  */
5251   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5252     {
5253       /* Consume the `[' token.  */
5254       cp_lexer_consume_token (parser->lexer);
5255       /* Look for the `]' token.  */
5256       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5257       /* Remember that this is the `[]' construct.  */
5258       array_p = true;
5259     }
5260   else
5261     array_p = false;
5262
5263   /* Parse the cast-expression.  */
5264   expression = cp_parser_simple_cast_expression (parser);
5265
5266   /* A delete-expression may not appear in an integral constant
5267      expression.  */
5268   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5269     return error_mark_node;
5270
5271   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5272 }
5273
5274 /* Parse a cast-expression.
5275
5276    cast-expression:
5277      unary-expression
5278      ( type-id ) cast-expression
5279
5280    ADDRESS_P is true iff the unary-expression is appearing as the
5281    operand of the `&' operator.   CAST_P is true if this expression is
5282    the target of a cast.
5283
5284    Returns a representation of the expression.  */
5285
5286 static tree
5287 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p)
5288 {
5289   /* If it's a `(', then we might be looking at a cast.  */
5290   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5291     {
5292       tree type = NULL_TREE;
5293       tree expr = NULL_TREE;
5294       bool compound_literal_p;
5295       const char *saved_message;
5296
5297       /* There's no way to know yet whether or not this is a cast.
5298          For example, `(int (3))' is a unary-expression, while `(int)
5299          3' is a cast.  So, we resort to parsing tentatively.  */
5300       cp_parser_parse_tentatively (parser);
5301       /* Types may not be defined in a cast.  */
5302       saved_message = parser->type_definition_forbidden_message;
5303       parser->type_definition_forbidden_message
5304         = "types may not be defined in casts";
5305       /* Consume the `('.  */
5306       cp_lexer_consume_token (parser->lexer);
5307       /* A very tricky bit is that `(struct S) { 3 }' is a
5308          compound-literal (which we permit in C++ as an extension).
5309          But, that construct is not a cast-expression -- it is a
5310          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5311          is legal; if the compound-literal were a cast-expression,
5312          you'd need an extra set of parentheses.)  But, if we parse
5313          the type-id, and it happens to be a class-specifier, then we
5314          will commit to the parse at that point, because we cannot
5315          undo the action that is done when creating a new class.  So,
5316          then we cannot back up and do a postfix-expression.
5317
5318          Therefore, we scan ahead to the closing `)', and check to see
5319          if the token after the `)' is a `{'.  If so, we are not
5320          looking at a cast-expression.
5321
5322          Save tokens so that we can put them back.  */
5323       cp_lexer_save_tokens (parser->lexer);
5324       /* Skip tokens until the next token is a closing parenthesis.
5325          If we find the closing `)', and the next token is a `{', then
5326          we are looking at a compound-literal.  */
5327       compound_literal_p
5328         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5329                                                   /*consume_paren=*/true)
5330            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5331       /* Roll back the tokens we skipped.  */
5332       cp_lexer_rollback_tokens (parser->lexer);
5333       /* If we were looking at a compound-literal, simulate an error
5334          so that the call to cp_parser_parse_definitely below will
5335          fail.  */
5336       if (compound_literal_p)
5337         cp_parser_simulate_error (parser);
5338       else
5339         {
5340           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5341           parser->in_type_id_in_expr_p = true;
5342           /* Look for the type-id.  */
5343           type = cp_parser_type_id (parser);
5344           /* Look for the closing `)'.  */
5345           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5346           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5347         }
5348
5349       /* Restore the saved message.  */
5350       parser->type_definition_forbidden_message = saved_message;
5351
5352       /* If ok so far, parse the dependent expression. We cannot be
5353          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5354          ctor of T, but looks like a cast to function returning T
5355          without a dependent expression.  */
5356       if (!cp_parser_error_occurred (parser))
5357         expr = cp_parser_cast_expression (parser, 
5358                                           /*address_p=*/false,
5359                                           /*cast_p=*/true);
5360
5361       if (cp_parser_parse_definitely (parser))
5362         {
5363           /* Warn about old-style casts, if so requested.  */
5364           if (warn_old_style_cast
5365               && !in_system_header
5366               && !VOID_TYPE_P (type)
5367               && current_lang_name != lang_name_c)
5368             warning (0, "use of old-style cast");
5369
5370           /* Only type conversions to integral or enumeration types
5371              can be used in constant-expressions.  */
5372           if (parser->integral_constant_expression_p
5373               && !dependent_type_p (type)
5374               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5375               && (cp_parser_non_integral_constant_expression
5376                   (parser,
5377                    "a cast to a type other than an integral or "
5378                    "enumeration type")))
5379             return error_mark_node;
5380
5381           /* Perform the cast.  */
5382           expr = build_c_cast (type, expr);
5383           return expr;
5384         }
5385     }
5386
5387   /* If we get here, then it's not a cast, so it must be a
5388      unary-expression.  */
5389   return cp_parser_unary_expression (parser, address_p, cast_p);
5390 }
5391
5392 /* Parse a binary expression of the general form:
5393
5394    pm-expression:
5395      cast-expression
5396      pm-expression .* cast-expression
5397      pm-expression ->* cast-expression
5398
5399    multiplicative-expression:
5400      pm-expression
5401      multiplicative-expression * pm-expression
5402      multiplicative-expression / pm-expression
5403      multiplicative-expression % pm-expression
5404
5405    additive-expression:
5406      multiplicative-expression
5407      additive-expression + multiplicative-expression
5408      additive-expression - multiplicative-expression
5409
5410    shift-expression:
5411      additive-expression
5412      shift-expression << additive-expression
5413      shift-expression >> additive-expression
5414
5415    relational-expression:
5416      shift-expression
5417      relational-expression < shift-expression
5418      relational-expression > shift-expression
5419      relational-expression <= shift-expression
5420      relational-expression >= shift-expression
5421
5422   GNU Extension:
5423   
5424    relational-expression:
5425      relational-expression <? shift-expression
5426      relational-expression >? shift-expression
5427
5428    equality-expression:
5429      relational-expression
5430      equality-expression == relational-expression
5431      equality-expression != relational-expression
5432
5433    and-expression:
5434      equality-expression
5435      and-expression & equality-expression
5436
5437    exclusive-or-expression:
5438      and-expression
5439      exclusive-or-expression ^ and-expression
5440
5441    inclusive-or-expression:
5442      exclusive-or-expression
5443      inclusive-or-expression | exclusive-or-expression
5444
5445    logical-and-expression:
5446      inclusive-or-expression
5447      logical-and-expression && inclusive-or-expression
5448
5449    logical-or-expression:
5450      logical-and-expression
5451      logical-or-expression || logical-and-expression
5452
5453    All these are implemented with a single function like:
5454
5455    binary-expression:
5456      simple-cast-expression
5457      binary-expression <token> binary-expression
5458
5459    CAST_P is true if this expression is the target of a cast.
5460
5461    The binops_by_token map is used to get the tree codes for each <token> type.
5462    binary-expressions are associated according to a precedence table.  */
5463
5464 #define TOKEN_PRECEDENCE(token) \
5465   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5466    ? PREC_NOT_OPERATOR \
5467    : binops_by_token[token->type].prec)
5468
5469 static tree
5470 cp_parser_binary_expression (cp_parser* parser, bool cast_p)
5471 {
5472   cp_parser_expression_stack stack;
5473   cp_parser_expression_stack_entry *sp = &stack[0];
5474   tree lhs, rhs;
5475   cp_token *token;
5476   enum tree_code tree_type;
5477   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5478   bool overloaded_p;
5479
5480   /* Parse the first expression.  */
5481   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p);
5482
5483   for (;;)
5484     {
5485       /* Get an operator token.  */
5486       token = cp_lexer_peek_token (parser->lexer);
5487       if (token->type == CPP_MIN || token->type == CPP_MAX)
5488         cp_parser_warn_min_max ();
5489
5490       new_prec = TOKEN_PRECEDENCE (token);
5491
5492       /* Popping an entry off the stack means we completed a subexpression:
5493          - either we found a token which is not an operator (`>' where it is not
5494            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5495            will happen repeatedly;
5496          - or, we found an operator which has lower priority.  This is the case 
5497            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5498            parsing `3 * 4'.  */
5499       if (new_prec <= prec)
5500         {
5501           if (sp == stack)
5502             break;
5503           else
5504             goto pop;
5505         }
5506
5507      get_rhs:
5508       tree_type = binops_by_token[token->type].tree_type;
5509
5510       /* We used the operator token.  */
5511       cp_lexer_consume_token (parser->lexer);
5512
5513       /* Extract another operand.  It may be the RHS of this expression
5514          or the LHS of a new, higher priority expression.  */
5515       rhs = cp_parser_simple_cast_expression (parser);
5516
5517       /* Get another operator token.  Look up its precedence to avoid
5518          building a useless (immediately popped) stack entry for common
5519          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
5520       token = cp_lexer_peek_token (parser->lexer);
5521       lookahead_prec = TOKEN_PRECEDENCE (token);
5522       if (lookahead_prec > new_prec)
5523         {
5524           /* ... and prepare to parse the RHS of the new, higher priority
5525              expression.  Since precedence levels on the stack are
5526              monotonically increasing, we do not have to care about
5527              stack overflows.  */
5528           sp->prec = prec;
5529           sp->tree_type = tree_type;
5530           sp->lhs = lhs;
5531           sp++;
5532           lhs = rhs;
5533           prec = new_prec;
5534           new_prec = lookahead_prec;
5535           goto get_rhs;
5536
5537          pop:
5538           /* If the stack is not empty, we have parsed into LHS the right side
5539              (`4' in the example above) of an expression we had suspended.
5540              We can use the information on the stack to recover the LHS (`3') 
5541              from the stack together with the tree code (`MULT_EXPR'), and
5542              the precedence of the higher level subexpression
5543              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5544              which will be used to actually build the additive expression.  */
5545           --sp;
5546           prec = sp->prec;
5547           tree_type = sp->tree_type;
5548           rhs = lhs;
5549           lhs = sp->lhs;
5550         }
5551
5552       overloaded_p = false;
5553       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5554
5555       /* If the binary operator required the use of an overloaded operator,
5556          then this expression cannot be an integral constant-expression.
5557          An overloaded operator can be used even if both operands are
5558          otherwise permissible in an integral constant-expression if at
5559          least one of the operands is of enumeration type.  */
5560
5561       if (overloaded_p
5562           && (cp_parser_non_integral_constant_expression 
5563               (parser, "calls to overloaded operators")))
5564         return error_mark_node;
5565     }
5566
5567   return lhs;
5568 }
5569
5570
5571 /* Parse the `? expression : assignment-expression' part of a
5572    conditional-expression.  The LOGICAL_OR_EXPR is the
5573    logical-or-expression that started the conditional-expression.
5574    Returns a representation of the entire conditional-expression.
5575
5576    This routine is used by cp_parser_assignment_expression.
5577
5578      ? expression : assignment-expression
5579
5580    GNU Extensions:
5581
5582      ? : assignment-expression */
5583
5584 static tree
5585 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5586 {
5587   tree expr;
5588   tree assignment_expr;
5589
5590   /* Consume the `?' token.  */
5591   cp_lexer_consume_token (parser->lexer);
5592   if (cp_parser_allow_gnu_extensions_p (parser)
5593       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5594     /* Implicit true clause.  */
5595     expr = NULL_TREE;
5596   else
5597     /* Parse the expression.  */
5598     expr = cp_parser_expression (parser, /*cast_p=*/false);
5599
5600   /* The next token should be a `:'.  */
5601   cp_parser_require (parser, CPP_COLON, "`:'");
5602   /* Parse the assignment-expression.  */
5603   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5604
5605   /* Build the conditional-expression.  */
5606   return build_x_conditional_expr (logical_or_expr,
5607                                    expr,
5608                                    assignment_expr);
5609 }
5610
5611 /* Parse an assignment-expression.
5612
5613    assignment-expression:
5614      conditional-expression
5615      logical-or-expression assignment-operator assignment_expression
5616      throw-expression
5617
5618    CAST_P is true if this expression is the target of a cast.
5619
5620    Returns a representation for the expression.  */
5621
5622 static tree
5623 cp_parser_assignment_expression (cp_parser* parser, bool cast_p)
5624 {
5625   tree expr;
5626
5627   /* If the next token is the `throw' keyword, then we're looking at
5628      a throw-expression.  */
5629   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5630     expr = cp_parser_throw_expression (parser);
5631   /* Otherwise, it must be that we are looking at a
5632      logical-or-expression.  */
5633   else
5634     {
5635       /* Parse the binary expressions (logical-or-expression).  */
5636       expr = cp_parser_binary_expression (parser, cast_p);
5637       /* If the next token is a `?' then we're actually looking at a
5638          conditional-expression.  */
5639       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5640         return cp_parser_question_colon_clause (parser, expr);
5641       else
5642         {
5643           enum tree_code assignment_operator;
5644
5645           /* If it's an assignment-operator, we're using the second
5646              production.  */
5647           assignment_operator
5648             = cp_parser_assignment_operator_opt (parser);
5649           if (assignment_operator != ERROR_MARK)
5650             {
5651               tree rhs;
5652
5653               /* Parse the right-hand side of the assignment.  */
5654               rhs = cp_parser_assignment_expression (parser, cast_p);
5655               /* An assignment may not appear in a
5656                  constant-expression.  */
5657               if (cp_parser_non_integral_constant_expression (parser,
5658                                                               "an assignment"))
5659                 return error_mark_node;
5660               /* Build the assignment expression.  */
5661               expr = build_x_modify_expr (expr,
5662                                           assignment_operator,
5663                                           rhs);
5664             }
5665         }
5666     }
5667
5668   return expr;
5669 }
5670
5671 /* Parse an (optional) assignment-operator.
5672
5673    assignment-operator: one of
5674      = *= /= %= += -= >>= <<= &= ^= |=
5675
5676    GNU Extension:
5677
5678    assignment-operator: one of
5679      <?= >?=
5680
5681    If the next token is an assignment operator, the corresponding tree
5682    code is returned, and the token is consumed.  For example, for
5683    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5684    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5685    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5686    operator, ERROR_MARK is returned.  */
5687
5688 static enum tree_code
5689 cp_parser_assignment_operator_opt (cp_parser* parser)
5690 {
5691   enum tree_code op;
5692   cp_token *token;
5693
5694   /* Peek at the next toen.  */
5695   token = cp_lexer_peek_token (parser->lexer);
5696
5697   switch (token->type)
5698     {
5699     case CPP_EQ:
5700       op = NOP_EXPR;
5701       break;
5702
5703     case CPP_MULT_EQ:
5704       op = MULT_EXPR;
5705       break;
5706
5707     case CPP_DIV_EQ:
5708       op = TRUNC_DIV_EXPR;
5709       break;
5710
5711     case CPP_MOD_EQ:
5712       op = TRUNC_MOD_EXPR;
5713       break;
5714
5715     case CPP_PLUS_EQ:
5716       op = PLUS_EXPR;
5717       break;
5718
5719     case CPP_MINUS_EQ:
5720       op = MINUS_EXPR;
5721       break;
5722
5723     case CPP_RSHIFT_EQ:
5724       op = RSHIFT_EXPR;
5725       break;
5726
5727     case CPP_LSHIFT_EQ:
5728       op = LSHIFT_EXPR;
5729       break;
5730
5731     case CPP_AND_EQ:
5732       op = BIT_AND_EXPR;
5733       break;
5734
5735     case CPP_XOR_EQ:
5736       op = BIT_XOR_EXPR;
5737       break;
5738
5739     case CPP_OR_EQ:
5740       op = BIT_IOR_EXPR;
5741       break;
5742
5743     case CPP_MIN_EQ:
5744       op = MIN_EXPR;
5745       cp_parser_warn_min_max ();
5746       break;
5747
5748     case CPP_MAX_EQ:
5749       op = MAX_EXPR;
5750       cp_parser_warn_min_max ();
5751       break;
5752
5753     default:
5754       /* Nothing else is an assignment operator.  */
5755       op = ERROR_MARK;
5756     }
5757
5758   /* If it was an assignment operator, consume it.  */
5759   if (op != ERROR_MARK)
5760     cp_lexer_consume_token (parser->lexer);
5761
5762   return op;
5763 }
5764
5765 /* Parse an expression.
5766
5767    expression:
5768      assignment-expression
5769      expression , assignment-expression
5770
5771    CAST_P is true if this expression is the target of a cast.
5772
5773    Returns a representation of the expression.  */
5774
5775 static tree
5776 cp_parser_expression (cp_parser* parser, bool cast_p)
5777 {
5778   tree expression = NULL_TREE;
5779
5780   while (true)
5781     {
5782       tree assignment_expression;
5783
5784       /* Parse the next assignment-expression.  */
5785       assignment_expression
5786         = cp_parser_assignment_expression (parser, cast_p);
5787       /* If this is the first assignment-expression, we can just
5788          save it away.  */
5789       if (!expression)
5790         expression = assignment_expression;
5791       else
5792         expression = build_x_compound_expr (expression,
5793                                             assignment_expression);
5794       /* If the next token is not a comma, then we are done with the
5795          expression.  */
5796       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5797         break;
5798       /* Consume the `,'.  */
5799       cp_lexer_consume_token (parser->lexer);
5800       /* A comma operator cannot appear in a constant-expression.  */
5801       if (cp_parser_non_integral_constant_expression (parser,
5802                                                       "a comma operator"))
5803         expression = error_mark_node;
5804     }
5805
5806   return expression;
5807 }
5808
5809 /* Parse a constant-expression.
5810
5811    constant-expression:
5812      conditional-expression
5813
5814   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5815   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5816   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5817   is false, NON_CONSTANT_P should be NULL.  */
5818
5819 static tree
5820 cp_parser_constant_expression (cp_parser* parser,
5821                                bool allow_non_constant_p,
5822                                bool *non_constant_p)
5823 {
5824   bool saved_integral_constant_expression_p;
5825   bool saved_allow_non_integral_constant_expression_p;
5826   bool saved_non_integral_constant_expression_p;
5827   tree expression;
5828
5829   /* It might seem that we could simply parse the
5830      conditional-expression, and then check to see if it were
5831      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5832      one that the compiler can figure out is constant, possibly after
5833      doing some simplifications or optimizations.  The standard has a
5834      precise definition of constant-expression, and we must honor
5835      that, even though it is somewhat more restrictive.
5836
5837      For example:
5838
5839        int i[(2, 3)];
5840
5841      is not a legal declaration, because `(2, 3)' is not a
5842      constant-expression.  The `,' operator is forbidden in a
5843      constant-expression.  However, GCC's constant-folding machinery
5844      will fold this operation to an INTEGER_CST for `3'.  */
5845
5846   /* Save the old settings.  */
5847   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5848   saved_allow_non_integral_constant_expression_p
5849     = parser->allow_non_integral_constant_expression_p;
5850   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5851   /* We are now parsing a constant-expression.  */
5852   parser->integral_constant_expression_p = true;
5853   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5854   parser->non_integral_constant_expression_p = false;
5855   /* Although the grammar says "conditional-expression", we parse an
5856      "assignment-expression", which also permits "throw-expression"
5857      and the use of assignment operators.  In the case that
5858      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5859      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5860      actually essential that we look for an assignment-expression.
5861      For example, cp_parser_initializer_clauses uses this function to
5862      determine whether a particular assignment-expression is in fact
5863      constant.  */
5864   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false);
5865   /* Restore the old settings.  */
5866   parser->integral_constant_expression_p 
5867     = saved_integral_constant_expression_p;
5868   parser->allow_non_integral_constant_expression_p
5869     = saved_allow_non_integral_constant_expression_p;
5870   if (allow_non_constant_p)
5871     *non_constant_p = parser->non_integral_constant_expression_p;
5872   else if (parser->non_integral_constant_expression_p)
5873     expression = error_mark_node;
5874   parser->non_integral_constant_expression_p 
5875     = saved_non_integral_constant_expression_p;
5876
5877   return expression;
5878 }
5879
5880 /* Parse __builtin_offsetof.
5881
5882    offsetof-expression:
5883      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5884
5885    offsetof-member-designator:
5886      id-expression
5887      | offsetof-member-designator "." id-expression
5888      | offsetof-member-designator "[" expression "]"
5889 */
5890
5891 static tree
5892 cp_parser_builtin_offsetof (cp_parser *parser)
5893 {
5894   int save_ice_p, save_non_ice_p;
5895   tree type, expr;
5896   cp_id_kind dummy;
5897
5898   /* We're about to accept non-integral-constant things, but will
5899      definitely yield an integral constant expression.  Save and
5900      restore these values around our local parsing.  */
5901   save_ice_p = parser->integral_constant_expression_p;
5902   save_non_ice_p = parser->non_integral_constant_expression_p;
5903
5904   /* Consume the "__builtin_offsetof" token.  */
5905   cp_lexer_consume_token (parser->lexer);
5906   /* Consume the opening `('.  */
5907   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5908   /* Parse the type-id.  */
5909   type = cp_parser_type_id (parser);
5910   /* Look for the `,'.  */
5911   cp_parser_require (parser, CPP_COMMA, "`,'");
5912
5913   /* Build the (type *)null that begins the traditional offsetof macro.  */
5914   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5915
5916   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5917   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5918                                                  true, &dummy);
5919   while (true)
5920     {
5921       cp_token *token = cp_lexer_peek_token (parser->lexer);
5922       switch (token->type)
5923         {
5924         case CPP_OPEN_SQUARE:
5925           /* offsetof-member-designator "[" expression "]" */
5926           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5927           break;
5928
5929         case CPP_DOT:
5930           /* offsetof-member-designator "." identifier */
5931           cp_lexer_consume_token (parser->lexer);
5932           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5933                                                          true, &dummy);
5934           break;
5935
5936         case CPP_CLOSE_PAREN:
5937           /* Consume the ")" token.  */
5938           cp_lexer_consume_token (parser->lexer);
5939           goto success;
5940
5941         default:
5942           /* Error.  We know the following require will fail, but
5943              that gives the proper error message.  */
5944           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5945           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5946           expr = error_mark_node;
5947           goto failure;
5948         }
5949     }
5950
5951  success:
5952   /* If we're processing a template, we can't finish the semantics yet.
5953      Otherwise we can fold the entire expression now.  */
5954   if (processing_template_decl)
5955     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5956   else
5957     expr = fold_offsetof (expr);
5958
5959  failure:
5960   parser->integral_constant_expression_p = save_ice_p;
5961   parser->non_integral_constant_expression_p = save_non_ice_p;
5962
5963   return expr;
5964 }
5965
5966 /* Statements [gram.stmt.stmt]  */
5967
5968 /* Parse a statement.
5969
5970    statement:
5971      labeled-statement
5972      expression-statement
5973      compound-statement
5974      selection-statement
5975      iteration-statement
5976      jump-statement
5977      declaration-statement
5978      try-block  */
5979
5980 static void
5981 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5982 {
5983   tree statement;
5984   cp_token *token;
5985   location_t statement_location;
5986
5987   /* There is no statement yet.  */
5988   statement = NULL_TREE;
5989   /* Peek at the next token.  */
5990   token = cp_lexer_peek_token (parser->lexer);
5991   /* Remember the location of the first token in the statement.  */
5992   statement_location = token->location;
5993   /* If this is a keyword, then that will often determine what kind of
5994      statement we have.  */
5995   if (token->type == CPP_KEYWORD)
5996     {
5997       enum rid keyword = token->keyword;
5998
5999       switch (keyword)
6000         {
6001         case RID_CASE:
6002         case RID_DEFAULT:
6003           statement = cp_parser_labeled_statement (parser,
6004                                                    in_statement_expr);
6005           break;
6006
6007         case RID_IF:
6008         case RID_SWITCH:
6009           statement = cp_parser_selection_statement (parser);
6010           break;
6011
6012         case RID_WHILE:
6013         case RID_DO:
6014         case RID_FOR:
6015           statement = cp_parser_iteration_statement (parser);
6016           break;
6017
6018         case RID_BREAK:
6019         case RID_CONTINUE:
6020         case RID_RETURN:
6021         case RID_GOTO:
6022           statement = cp_parser_jump_statement (parser);
6023           break;
6024
6025           /* Objective-C++ exception-handling constructs.  */
6026         case RID_AT_TRY:
6027         case RID_AT_CATCH:
6028         case RID_AT_FINALLY:
6029         case RID_AT_SYNCHRONIZED:
6030         case RID_AT_THROW:
6031           statement = cp_parser_objc_statement (parser);
6032           break;
6033
6034         case RID_TRY:
6035           statement = cp_parser_try_block (parser);
6036           break;
6037
6038         default:
6039           /* It might be a keyword like `int' that can start a
6040              declaration-statement.  */
6041           break;
6042         }
6043     }
6044   else if (token->type == CPP_NAME)
6045     {
6046       /* If the next token is a `:', then we are looking at a
6047          labeled-statement.  */
6048       token = cp_lexer_peek_nth_token (parser->lexer, 2);
6049       if (token->type == CPP_COLON)
6050         statement = cp_parser_labeled_statement (parser, in_statement_expr);
6051     }
6052   /* Anything that starts with a `{' must be a compound-statement.  */
6053   else if (token->type == CPP_OPEN_BRACE)
6054     statement = cp_parser_compound_statement (parser, NULL, false);
6055   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
6056      a statement all its own.  */
6057   else if (token->type == CPP_PRAGMA)
6058     {
6059       cp_lexer_handle_pragma (parser->lexer);
6060       return;
6061     }
6062
6063   /* Everything else must be a declaration-statement or an
6064      expression-statement.  Try for the declaration-statement
6065      first, unless we are looking at a `;', in which case we know that
6066      we have an expression-statement.  */
6067   if (!statement)
6068     {
6069       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6070         {
6071           cp_parser_parse_tentatively (parser);
6072           /* Try to parse the declaration-statement.  */
6073           cp_parser_declaration_statement (parser);
6074           /* If that worked, we're done.  */
6075           if (cp_parser_parse_definitely (parser))
6076             return;
6077         }
6078       /* Look for an expression-statement instead.  */
6079       statement = cp_parser_expression_statement (parser, in_statement_expr);
6080     }
6081
6082   /* Set the line number for the statement.  */
6083   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
6084     SET_EXPR_LOCATION (statement, statement_location);
6085 }
6086
6087 /* Parse a labeled-statement.
6088
6089    labeled-statement:
6090      identifier : statement
6091      case constant-expression : statement
6092      default : statement
6093
6094    GNU Extension:
6095
6096    labeled-statement:
6097      case constant-expression ... constant-expression : statement
6098
6099    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
6100    For an ordinary label, returns a LABEL_EXPR.  */
6101
6102 static tree
6103 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
6104 {
6105   cp_token *token;
6106   tree statement = error_mark_node;
6107
6108   /* The next token should be an identifier.  */
6109   token = cp_lexer_peek_token (parser->lexer);
6110   if (token->type != CPP_NAME
6111       && token->type != CPP_KEYWORD)
6112     {
6113       cp_parser_error (parser, "expected labeled-statement");
6114       return error_mark_node;
6115     }
6116
6117   switch (token->keyword)
6118     {
6119     case RID_CASE:
6120       {
6121         tree expr, expr_hi;
6122         cp_token *ellipsis;
6123
6124         /* Consume the `case' token.  */
6125         cp_lexer_consume_token (parser->lexer);
6126         /* Parse the constant-expression.  */
6127         expr = cp_parser_constant_expression (parser,
6128                                               /*allow_non_constant_p=*/false,
6129                                               NULL);
6130
6131         ellipsis = cp_lexer_peek_token (parser->lexer);
6132         if (ellipsis->type == CPP_ELLIPSIS)
6133           {
6134             /* Consume the `...' token.  */
6135             cp_lexer_consume_token (parser->lexer);
6136             expr_hi =
6137               cp_parser_constant_expression (parser,
6138                                              /*allow_non_constant_p=*/false,
6139                                              NULL);
6140             /* We don't need to emit warnings here, as the common code
6141                will do this for us.  */
6142           }
6143         else
6144           expr_hi = NULL_TREE;
6145
6146         if (!parser->in_switch_statement_p)
6147           error ("case label %qE not within a switch statement", expr);
6148         else
6149           statement = finish_case_label (expr, expr_hi);
6150       }
6151       break;
6152
6153     case RID_DEFAULT:
6154       /* Consume the `default' token.  */
6155       cp_lexer_consume_token (parser->lexer);
6156       if (!parser->in_switch_statement_p)
6157         error ("case label not within a switch statement");
6158       else
6159         statement = finish_case_label (NULL_TREE, NULL_TREE);
6160       break;
6161
6162     default:
6163       /* Anything else must be an ordinary label.  */
6164       statement = finish_label_stmt (cp_parser_identifier (parser));
6165       break;
6166     }
6167
6168   /* Require the `:' token.  */
6169   cp_parser_require (parser, CPP_COLON, "`:'");
6170   /* Parse the labeled statement.  */
6171   cp_parser_statement (parser, in_statement_expr);
6172
6173   /* Return the label, in the case of a `case' or `default' label.  */
6174   return statement;
6175 }
6176
6177 /* Parse an expression-statement.
6178
6179    expression-statement:
6180      expression [opt] ;
6181
6182    Returns the new EXPR_STMT -- or NULL_TREE if the expression
6183    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
6184    indicates whether this expression-statement is part of an
6185    expression statement.  */
6186
6187 static tree
6188 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
6189 {
6190   tree statement = NULL_TREE;
6191
6192   /* If the next token is a ';', then there is no expression
6193      statement.  */
6194   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6195     statement = cp_parser_expression (parser, /*cast_p=*/false);
6196
6197   /* Consume the final `;'.  */
6198   cp_parser_consume_semicolon_at_end_of_statement (parser);
6199
6200   if (in_statement_expr
6201       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6202     /* This is the final expression statement of a statement
6203        expression.  */
6204     statement = finish_stmt_expr_expr (statement, in_statement_expr);
6205   else if (statement)
6206     statement = finish_expr_stmt (statement);
6207   else
6208     finish_stmt ();
6209
6210   return statement;
6211 }
6212
6213 /* Parse a compound-statement.
6214
6215    compound-statement:
6216      { statement-seq [opt] }
6217
6218    Returns a tree representing the statement.  */
6219
6220 static tree
6221 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6222                               bool in_try)
6223 {
6224   tree compound_stmt;
6225
6226   /* Consume the `{'.  */
6227   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6228     return error_mark_node;
6229   /* Begin the compound-statement.  */
6230   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6231   /* Parse an (optional) statement-seq.  */
6232   cp_parser_statement_seq_opt (parser, in_statement_expr);
6233   /* Finish the compound-statement.  */
6234   finish_compound_stmt (compound_stmt);
6235   /* Consume the `}'.  */
6236   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6237
6238   return compound_stmt;
6239 }
6240
6241 /* Parse an (optional) statement-seq.
6242
6243    statement-seq:
6244      statement
6245      statement-seq [opt] statement  */
6246
6247 static void
6248 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6249 {
6250   /* Scan statements until there aren't any more.  */
6251   while (true)
6252     {
6253       /* If we're looking at a `}', then we've run out of statements.  */
6254       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6255           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6256         break;
6257
6258       /* Parse the statement.  */
6259       cp_parser_statement (parser, in_statement_expr);
6260     }
6261 }
6262
6263 /* Parse a selection-statement.
6264
6265    selection-statement:
6266      if ( condition ) statement
6267      if ( condition ) statement else statement
6268      switch ( condition ) statement
6269
6270    Returns the new IF_STMT or SWITCH_STMT.  */
6271
6272 static tree
6273 cp_parser_selection_statement (cp_parser* parser)
6274 {
6275   cp_token *token;
6276   enum rid keyword;
6277
6278   /* Peek at the next token.  */
6279   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6280
6281   /* See what kind of keyword it is.  */
6282   keyword = token->keyword;
6283   switch (keyword)
6284     {
6285     case RID_IF:
6286     case RID_SWITCH:
6287       {
6288         tree statement;
6289         tree condition;
6290
6291         /* Look for the `('.  */
6292         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6293           {
6294             cp_parser_skip_to_end_of_statement (parser);
6295             return error_mark_node;
6296           }
6297
6298         /* Begin the selection-statement.  */
6299         if (keyword == RID_IF)
6300           statement = begin_if_stmt ();
6301         else
6302           statement = begin_switch_stmt ();
6303
6304         /* Parse the condition.  */
6305         condition = cp_parser_condition (parser);
6306         /* Look for the `)'.  */
6307         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6308           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6309                                                  /*consume_paren=*/true);
6310
6311         if (keyword == RID_IF)
6312           {
6313             /* Add the condition.  */
6314             finish_if_stmt_cond (condition, statement);
6315
6316             /* Parse the then-clause.  */
6317             cp_parser_implicitly_scoped_statement (parser);
6318             finish_then_clause (statement);
6319
6320             /* If the next token is `else', parse the else-clause.  */
6321             if (cp_lexer_next_token_is_keyword (parser->lexer,
6322                                                 RID_ELSE))
6323               {
6324                 /* Consume the `else' keyword.  */
6325                 cp_lexer_consume_token (parser->lexer);
6326                 begin_else_clause (statement);
6327                 /* Parse the else-clause.  */
6328                 cp_parser_implicitly_scoped_statement (parser);
6329                 finish_else_clause (statement);
6330               }
6331
6332             /* Now we're all done with the if-statement.  */
6333             finish_if_stmt (statement);
6334           }
6335         else
6336           {
6337             bool in_switch_statement_p;
6338
6339             /* Add the condition.  */
6340             finish_switch_cond (condition, statement);
6341
6342             /* Parse the body of the switch-statement.  */
6343             in_switch_statement_p = parser->in_switch_statement_p;
6344             parser->in_switch_statement_p = true;
6345             cp_parser_implicitly_scoped_statement (parser);
6346             parser->in_switch_statement_p = in_switch_statement_p;
6347
6348             /* Now we're all done with the switch-statement.  */
6349             finish_switch_stmt (statement);
6350           }
6351
6352         return statement;
6353       }
6354       break;
6355
6356     default:
6357       cp_parser_error (parser, "expected selection-statement");
6358       return error_mark_node;
6359     }
6360 }
6361
6362 /* Parse a condition.
6363
6364    condition:
6365      expression
6366      type-specifier-seq declarator = assignment-expression
6367
6368    GNU Extension:
6369
6370    condition:
6371      type-specifier-seq declarator asm-specification [opt]
6372        attributes [opt] = assignment-expression
6373
6374    Returns the expression that should be tested.  */
6375
6376 static tree
6377 cp_parser_condition (cp_parser* parser)
6378 {
6379   cp_decl_specifier_seq type_specifiers;
6380   const char *saved_message;
6381
6382   /* Try the declaration first.  */
6383   cp_parser_parse_tentatively (parser);
6384   /* New types are not allowed in the type-specifier-seq for a
6385      condition.  */
6386   saved_message = parser->type_definition_forbidden_message;
6387   parser->type_definition_forbidden_message
6388     = "types may not be defined in conditions";
6389   /* Parse the type-specifier-seq.  */
6390   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
6391                                 &type_specifiers);
6392   /* Restore the saved message.  */
6393   parser->type_definition_forbidden_message = saved_message;
6394   /* If all is well, we might be looking at a declaration.  */
6395   if (!cp_parser_error_occurred (parser))
6396     {
6397       tree decl;
6398       tree asm_specification;
6399       tree attributes;
6400       cp_declarator *declarator;
6401       tree initializer = NULL_TREE;
6402
6403       /* Parse the declarator.  */
6404       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6405                                          /*ctor_dtor_or_conv_p=*/NULL,
6406                                          /*parenthesized_p=*/NULL,
6407                                          /*member_p=*/false);
6408       /* Parse the attributes.  */
6409       attributes = cp_parser_attributes_opt (parser);
6410       /* Parse the asm-specification.  */
6411       asm_specification = cp_parser_asm_specification_opt (parser);
6412       /* If the next token is not an `=', then we might still be
6413          looking at an expression.  For example:
6414
6415            if (A(a).x)
6416
6417          looks like a decl-specifier-seq and a declarator -- but then
6418          there is no `=', so this is an expression.  */
6419       cp_parser_require (parser, CPP_EQ, "`='");
6420       /* If we did see an `=', then we are looking at a declaration
6421          for sure.  */
6422       if (cp_parser_parse_definitely (parser))
6423         {
6424           tree pushed_scope;    
6425
6426           /* Create the declaration.  */
6427           decl = start_decl (declarator, &type_specifiers,
6428                              /*initialized_p=*/true,
6429                              attributes, /*prefix_attributes=*/NULL_TREE,
6430                              &pushed_scope);
6431           /* Parse the assignment-expression.  */
6432           initializer = cp_parser_assignment_expression (parser,
6433                                                          /*cast_p=*/false);
6434
6435           /* Process the initializer.  */
6436           cp_finish_decl (decl,
6437                           initializer,
6438                           asm_specification,
6439                           LOOKUP_ONLYCONVERTING);
6440
6441           if (pushed_scope)
6442             pop_scope (pushed_scope);
6443
6444           return convert_from_reference (decl);
6445         }
6446     }
6447   /* If we didn't even get past the declarator successfully, we are
6448      definitely not looking at a declaration.  */
6449   else
6450     cp_parser_abort_tentative_parse (parser);
6451
6452   /* Otherwise, we are looking at an expression.  */
6453   return cp_parser_expression (parser, /*cast_p=*/false);
6454 }
6455
6456 /* Parse an iteration-statement.
6457
6458    iteration-statement:
6459      while ( condition ) statement
6460      do statement while ( expression ) ;
6461      for ( for-init-statement condition [opt] ; expression [opt] )
6462        statement
6463
6464    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6465
6466 static tree
6467 cp_parser_iteration_statement (cp_parser* parser)
6468 {
6469   cp_token *token;
6470   enum rid keyword;
6471   tree statement;
6472   bool in_iteration_statement_p;
6473
6474
6475   /* Peek at the next token.  */
6476   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6477   if (!token)
6478     return error_mark_node;
6479
6480   /* Remember whether or not we are already within an iteration
6481      statement.  */
6482   in_iteration_statement_p = parser->in_iteration_statement_p;
6483
6484   /* See what kind of keyword it is.  */
6485   keyword = token->keyword;
6486   switch (keyword)
6487     {
6488     case RID_WHILE:
6489       {
6490         tree condition;
6491
6492         /* Begin the while-statement.  */
6493         statement = begin_while_stmt ();
6494         /* Look for the `('.  */
6495         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6496         /* Parse the condition.  */
6497         condition = cp_parser_condition (parser);
6498         finish_while_stmt_cond (condition, statement);
6499         /* Look for the `)'.  */
6500         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6501         /* Parse the dependent statement.  */
6502         parser->in_iteration_statement_p = true;
6503         cp_parser_already_scoped_statement (parser);
6504         parser->in_iteration_statement_p = in_iteration_statement_p;
6505         /* We're done with the while-statement.  */
6506         finish_while_stmt (statement);
6507       }
6508       break;
6509
6510     case RID_DO:
6511       {
6512         tree expression;
6513
6514         /* Begin the do-statement.  */
6515         statement = begin_do_stmt ();
6516         /* Parse the body of the do-statement.  */
6517         parser->in_iteration_statement_p = true;
6518         cp_parser_implicitly_scoped_statement (parser);
6519         parser->in_iteration_statement_p = in_iteration_statement_p;
6520         finish_do_body (statement);
6521         /* Look for the `while' keyword.  */
6522         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6523         /* Look for the `('.  */
6524         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6525         /* Parse the expression.  */
6526         expression = cp_parser_expression (parser, /*cast_p=*/false);
6527         /* We're done with the do-statement.  */
6528         finish_do_stmt (expression, statement);
6529         /* Look for the `)'.  */
6530         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6531         /* Look for the `;'.  */
6532         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6533       }
6534       break;
6535
6536     case RID_FOR:
6537       {
6538         tree condition = NULL_TREE;
6539         tree expression = NULL_TREE;
6540
6541         /* Begin the for-statement.  */
6542         statement = begin_for_stmt ();
6543         /* Look for the `('.  */
6544         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6545         /* Parse the initialization.  */
6546         cp_parser_for_init_statement (parser);
6547         finish_for_init_stmt (statement);
6548
6549         /* If there's a condition, process it.  */
6550         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6551           condition = cp_parser_condition (parser);
6552         finish_for_cond (condition, statement);
6553         /* Look for the `;'.  */
6554         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6555
6556         /* If there's an expression, process it.  */
6557         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6558           expression = cp_parser_expression (parser, /*cast_p=*/false);
6559         finish_for_expr (expression, statement);
6560         /* Look for the `)'.  */
6561         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6562
6563         /* Parse the body of the for-statement.  */
6564         parser->in_iteration_statement_p = true;
6565         cp_parser_already_scoped_statement (parser);
6566         parser->in_iteration_statement_p = in_iteration_statement_p;
6567
6568         /* We're done with the for-statement.  */
6569         finish_for_stmt (statement);
6570       }
6571       break;
6572
6573     default:
6574       cp_parser_error (parser, "expected iteration-statement");
6575       statement = error_mark_node;
6576       break;
6577     }
6578
6579   return statement;
6580 }
6581
6582 /* Parse a for-init-statement.
6583
6584    for-init-statement:
6585      expression-statement
6586      simple-declaration  */
6587
6588 static void
6589 cp_parser_for_init_statement (cp_parser* parser)
6590 {
6591   /* If the next token is a `;', then we have an empty
6592      expression-statement.  Grammatically, this is also a
6593      simple-declaration, but an invalid one, because it does not
6594      declare anything.  Therefore, if we did not handle this case
6595      specially, we would issue an error message about an invalid
6596      declaration.  */
6597   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6598     {
6599       /* We're going to speculatively look for a declaration, falling back
6600          to an expression, if necessary.  */
6601       cp_parser_parse_tentatively (parser);
6602       /* Parse the declaration.  */
6603       cp_parser_simple_declaration (parser,
6604                                     /*function_definition_allowed_p=*/false);
6605       /* If the tentative parse failed, then we shall need to look for an
6606          expression-statement.  */
6607       if (cp_parser_parse_definitely (parser))
6608         return;
6609     }
6610
6611   cp_parser_expression_statement (parser, false);
6612 }
6613
6614 /* Parse a jump-statement.
6615
6616    jump-statement:
6617      break ;
6618      continue ;
6619      return expression [opt] ;
6620      goto identifier ;
6621
6622    GNU extension:
6623
6624    jump-statement:
6625      goto * expression ;
6626
6627    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6628
6629 static tree
6630 cp_parser_jump_statement (cp_parser* parser)
6631 {
6632   tree statement = error_mark_node;
6633   cp_token *token;
6634   enum rid keyword;
6635
6636   /* Peek at the next token.  */
6637   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6638   if (!token)
6639     return error_mark_node;
6640
6641   /* See what kind of keyword it is.  */
6642   keyword = token->keyword;
6643   switch (keyword)
6644     {
6645     case RID_BREAK:
6646       if (!parser->in_switch_statement_p
6647           && !parser->in_iteration_statement_p)
6648         {
6649           error ("break statement not within loop or switch");
6650           statement = error_mark_node;
6651         }
6652       else
6653         statement = finish_break_stmt ();
6654       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6655       break;
6656
6657     case RID_CONTINUE:
6658       if (!parser->in_iteration_statement_p)
6659         {
6660           error ("continue statement not within a loop");
6661           statement = error_mark_node;
6662         }
6663       else
6664         statement = finish_continue_stmt ();
6665       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6666       break;
6667
6668     case RID_RETURN:
6669       {
6670         tree expr;
6671
6672         /* If the next token is a `;', then there is no
6673            expression.  */
6674         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6675           expr = cp_parser_expression (parser, /*cast_p=*/false);
6676         else
6677           expr = NULL_TREE;
6678         /* Build the return-statement.  */
6679         statement = finish_return_stmt (expr);
6680         /* Look for the final `;'.  */
6681         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6682       }
6683       break;
6684
6685     case RID_GOTO:
6686       /* Create the goto-statement.  */
6687       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6688         {
6689           /* Issue a warning about this use of a GNU extension.  */
6690           if (pedantic)
6691             pedwarn ("ISO C++ forbids computed gotos");
6692           /* Consume the '*' token.  */
6693           cp_lexer_consume_token (parser->lexer);
6694           /* Parse the dependent expression.  */
6695           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false));
6696         }
6697       else
6698         finish_goto_stmt (cp_parser_identifier (parser));
6699       /* Look for the final `;'.  */
6700       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6701       break;
6702
6703     default:
6704       cp_parser_error (parser, "expected jump-statement");
6705       break;
6706     }
6707
6708   return statement;
6709 }
6710
6711 /* Parse a declaration-statement.
6712
6713    declaration-statement:
6714      block-declaration  */
6715
6716 static void
6717 cp_parser_declaration_statement (cp_parser* parser)
6718 {
6719   void *p;
6720
6721   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6722   p = obstack_alloc (&declarator_obstack, 0);
6723
6724  /* Parse the block-declaration.  */
6725   cp_parser_block_declaration (parser, /*statement_p=*/true);
6726
6727   /* Free any declarators allocated.  */
6728   obstack_free (&declarator_obstack, p);
6729
6730   /* Finish off the statement.  */
6731   finish_stmt ();
6732 }
6733
6734 /* Some dependent statements (like `if (cond) statement'), are
6735    implicitly in their own scope.  In other words, if the statement is
6736    a single statement (as opposed to a compound-statement), it is
6737    none-the-less treated as if it were enclosed in braces.  Any
6738    declarations appearing in the dependent statement are out of scope
6739    after control passes that point.  This function parses a statement,
6740    but ensures that is in its own scope, even if it is not a
6741    compound-statement.
6742
6743    Returns the new statement.  */
6744
6745 static tree
6746 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6747 {
6748   tree statement;
6749
6750   /* If the token is not a `{', then we must take special action.  */
6751   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6752     {
6753       /* Create a compound-statement.  */
6754       statement = begin_compound_stmt (0);
6755       /* Parse the dependent-statement.  */
6756       cp_parser_statement (parser, false);
6757       /* Finish the dummy compound-statement.  */
6758       finish_compound_stmt (statement);
6759     }
6760   /* Otherwise, we simply parse the statement directly.  */
6761   else
6762     statement = cp_parser_compound_statement (parser, NULL, false);
6763
6764   /* Return the statement.  */
6765   return statement;
6766 }
6767
6768 /* For some dependent statements (like `while (cond) statement'), we
6769    have already created a scope.  Therefore, even if the dependent
6770    statement is a compound-statement, we do not want to create another
6771    scope.  */
6772
6773 static void
6774 cp_parser_already_scoped_statement (cp_parser* parser)
6775 {
6776   /* If the token is a `{', then we must take special action.  */
6777   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6778     cp_parser_statement (parser, false);
6779   else
6780     {
6781       /* Avoid calling cp_parser_compound_statement, so that we
6782          don't create a new scope.  Do everything else by hand.  */
6783       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6784       cp_parser_statement_seq_opt (parser, false);
6785       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6786     }
6787 }
6788
6789 /* Declarations [gram.dcl.dcl] */
6790
6791 /* Parse an optional declaration-sequence.
6792
6793    declaration-seq:
6794      declaration
6795      declaration-seq declaration  */
6796
6797 static void
6798 cp_parser_declaration_seq_opt (cp_parser* parser)
6799 {
6800   while (true)
6801     {
6802       cp_token *token;
6803
6804       token = cp_lexer_peek_token (parser->lexer);
6805
6806       if (token->type == CPP_CLOSE_BRACE
6807           || token->type == CPP_EOF)
6808         break;
6809
6810       if (token->type == CPP_SEMICOLON)
6811         {
6812           /* A declaration consisting of a single semicolon is
6813              invalid.  Allow it unless we're being pedantic.  */
6814           cp_lexer_consume_token (parser->lexer);
6815           if (pedantic && !in_system_header)
6816             pedwarn ("extra %<;%>");
6817           continue;
6818         }
6819
6820       /* If we're entering or exiting a region that's implicitly
6821          extern "C", modify the lang context appropriately.  */
6822       if (!parser->implicit_extern_c && token->implicit_extern_c)
6823         {
6824           push_lang_context (lang_name_c);
6825           parser->implicit_extern_c = true;
6826         }
6827       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6828         {
6829           pop_lang_context ();
6830           parser->implicit_extern_c = false;
6831         }
6832
6833       if (token->type == CPP_PRAGMA)
6834         {
6835           /* A top-level declaration can consist solely of a #pragma.
6836              A nested declaration cannot, so this is done here and not
6837              in cp_parser_declaration.  (A #pragma at block scope is
6838              handled in cp_parser_statement.)  */
6839           cp_lexer_handle_pragma (parser->lexer);
6840           continue;
6841         }
6842
6843       /* Parse the declaration itself.  */
6844       cp_parser_declaration (parser);
6845     }
6846 }
6847
6848 /* Parse a declaration.
6849
6850    declaration:
6851      block-declaration
6852      function-definition
6853      template-declaration
6854      explicit-instantiation
6855      explicit-specialization
6856      linkage-specification
6857      namespace-definition
6858
6859    GNU extension:
6860
6861    declaration:
6862       __extension__ declaration */
6863
6864 static void
6865 cp_parser_declaration (cp_parser* parser)
6866 {
6867   cp_token token1;
6868   cp_token token2;
6869   int saved_pedantic;
6870   void *p;
6871
6872   /* Check for the `__extension__' keyword.  */
6873   if (cp_parser_extension_opt (parser, &saved_pedantic))
6874     {
6875       /* Parse the qualified declaration.  */
6876       cp_parser_declaration (parser);
6877       /* Restore the PEDANTIC flag.  */
6878       pedantic = saved_pedantic;
6879
6880       return;
6881     }
6882
6883   /* Try to figure out what kind of declaration is present.  */
6884   token1 = *cp_lexer_peek_token (parser->lexer);
6885
6886   if (token1.type != CPP_EOF)
6887     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6888
6889   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6890   p = obstack_alloc (&declarator_obstack, 0);
6891
6892   /* If the next token is `extern' and the following token is a string
6893      literal, then we have a linkage specification.  */
6894   if (token1.keyword == RID_EXTERN
6895       && cp_parser_is_string_literal (&token2))
6896     cp_parser_linkage_specification (parser);
6897   /* If the next token is `template', then we have either a template
6898      declaration, an explicit instantiation, or an explicit
6899      specialization.  */
6900   else if (token1.keyword == RID_TEMPLATE)
6901     {
6902       /* `template <>' indicates a template specialization.  */
6903       if (token2.type == CPP_LESS
6904           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6905         cp_parser_explicit_specialization (parser);
6906       /* `template <' indicates a template declaration.  */
6907       else if (token2.type == CPP_LESS)
6908         cp_parser_template_declaration (parser, /*member_p=*/false);
6909       /* Anything else must be an explicit instantiation.  */
6910       else
6911         cp_parser_explicit_instantiation (parser);
6912     }
6913   /* If the next token is `export', then we have a template
6914      declaration.  */
6915   else if (token1.keyword == RID_EXPORT)
6916     cp_parser_template_declaration (parser, /*member_p=*/false);
6917   /* If the next token is `extern', 'static' or 'inline' and the one
6918      after that is `template', we have a GNU extended explicit
6919      instantiation directive.  */
6920   else if (cp_parser_allow_gnu_extensions_p (parser)
6921            && (token1.keyword == RID_EXTERN
6922                || token1.keyword == RID_STATIC
6923                || token1.keyword == RID_INLINE)
6924            && token2.keyword == RID_TEMPLATE)
6925     cp_parser_explicit_instantiation (parser);
6926   /* If the next token is `namespace', check for a named or unnamed
6927      namespace definition.  */
6928   else if (token1.keyword == RID_NAMESPACE
6929            && (/* A named namespace definition.  */
6930                (token2.type == CPP_NAME
6931                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6932                     == CPP_OPEN_BRACE))
6933                /* An unnamed namespace definition.  */
6934                || token2.type == CPP_OPEN_BRACE))
6935     cp_parser_namespace_definition (parser);
6936   /* Objective-C++ declaration/definition.  */
6937   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
6938     cp_parser_objc_declaration (parser);
6939   /* We must have either a block declaration or a function
6940      definition.  */
6941   else
6942     /* Try to parse a block-declaration, or a function-definition.  */
6943     cp_parser_block_declaration (parser, /*statement_p=*/false);
6944
6945   /* Free any declarators allocated.  */
6946   obstack_free (&declarator_obstack, p);
6947 }
6948
6949 /* Parse a block-declaration.
6950
6951    block-declaration:
6952      simple-declaration
6953      asm-definition
6954      namespace-alias-definition
6955      using-declaration
6956      using-directive
6957
6958    GNU Extension:
6959
6960    block-declaration:
6961      __extension__ block-declaration
6962      label-declaration
6963
6964    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6965    part of a declaration-statement.  */
6966
6967 static void
6968 cp_parser_block_declaration (cp_parser *parser,
6969                              bool      statement_p)
6970 {
6971   cp_token *token1;
6972   int saved_pedantic;
6973
6974   /* Check for the `__extension__' keyword.  */
6975   if (cp_parser_extension_opt (parser, &saved_pedantic))
6976     {
6977       /* Parse the qualified declaration.  */
6978       cp_parser_block_declaration (parser, statement_p);
6979       /* Restore the PEDANTIC flag.  */
6980       pedantic = saved_pedantic;
6981
6982       return;
6983     }
6984
6985   /* Peek at the next token to figure out which kind of declaration is
6986      present.  */
6987   token1 = cp_lexer_peek_token (parser->lexer);
6988
6989   /* If the next keyword is `asm', we have an asm-definition.  */
6990   if (token1->keyword == RID_ASM)
6991     {
6992       if (statement_p)
6993         cp_parser_commit_to_tentative_parse (parser);
6994       cp_parser_asm_definition (parser);
6995     }
6996   /* If the next keyword is `namespace', we have a
6997      namespace-alias-definition.  */
6998   else if (token1->keyword == RID_NAMESPACE)
6999     cp_parser_namespace_alias_definition (parser);
7000   /* If the next keyword is `using', we have either a
7001      using-declaration or a using-directive.  */
7002   else if (token1->keyword == RID_USING)
7003     {
7004       cp_token *token2;
7005
7006       if (statement_p)
7007         cp_parser_commit_to_tentative_parse (parser);
7008       /* If the token after `using' is `namespace', then we have a
7009          using-directive.  */
7010       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
7011       if (token2->keyword == RID_NAMESPACE)
7012         cp_parser_using_directive (parser);
7013       /* Otherwise, it's a using-declaration.  */
7014       else
7015         cp_parser_using_declaration (parser);
7016     }
7017   /* If the next keyword is `__label__' we have a label declaration.  */
7018   else if (token1->keyword == RID_LABEL)
7019     {
7020       if (statement_p)
7021         cp_parser_commit_to_tentative_parse (parser);
7022       cp_parser_label_declaration (parser);
7023     }
7024   /* Anything else must be a simple-declaration.  */
7025   else
7026     cp_parser_simple_declaration (parser, !statement_p);
7027 }
7028
7029 /* Parse a simple-declaration.
7030
7031    simple-declaration:
7032      decl-specifier-seq [opt] init-declarator-list [opt] ;
7033
7034    init-declarator-list:
7035      init-declarator
7036      init-declarator-list , init-declarator
7037
7038    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
7039    function-definition as a simple-declaration.  */
7040
7041 static void
7042 cp_parser_simple_declaration (cp_parser* parser,
7043                               bool function_definition_allowed_p)
7044 {
7045   cp_decl_specifier_seq decl_specifiers;
7046   int declares_class_or_enum;
7047   bool saw_declarator;
7048
7049   /* Defer access checks until we know what is being declared; the
7050      checks for names appearing in the decl-specifier-seq should be
7051      done as if we were in the scope of the thing being declared.  */
7052   push_deferring_access_checks (dk_deferred);
7053
7054   /* Parse the decl-specifier-seq.  We have to keep track of whether
7055      or not the decl-specifier-seq declares a named class or
7056      enumeration type, since that is the only case in which the
7057      init-declarator-list is allowed to be empty.
7058
7059      [dcl.dcl]
7060
7061      In a simple-declaration, the optional init-declarator-list can be
7062      omitted only when declaring a class or enumeration, that is when
7063      the decl-specifier-seq contains either a class-specifier, an
7064      elaborated-type-specifier, or an enum-specifier.  */
7065   cp_parser_decl_specifier_seq (parser,
7066                                 CP_PARSER_FLAGS_OPTIONAL,
7067                                 &decl_specifiers,
7068                                 &declares_class_or_enum);
7069   /* We no longer need to defer access checks.  */
7070   stop_deferring_access_checks ();
7071
7072   /* In a block scope, a valid declaration must always have a
7073      decl-specifier-seq.  By not trying to parse declarators, we can
7074      resolve the declaration/expression ambiguity more quickly.  */
7075   if (!function_definition_allowed_p
7076       && !decl_specifiers.any_specifiers_p)
7077     {
7078       cp_parser_error (parser, "expected declaration");
7079       goto done;
7080     }
7081
7082   /* If the next two tokens are both identifiers, the code is
7083      erroneous. The usual cause of this situation is code like:
7084
7085        T t;
7086
7087      where "T" should name a type -- but does not.  */
7088   if (!decl_specifiers.type
7089       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
7090     {
7091       /* If parsing tentatively, we should commit; we really are
7092          looking at a declaration.  */
7093       cp_parser_commit_to_tentative_parse (parser);
7094       /* Give up.  */
7095       goto done;
7096     }
7097   
7098   /* If we have seen at least one decl-specifier, and the next token
7099      is not a parenthesis, then we must be looking at a declaration.
7100      (After "int (" we might be looking at a functional cast.)  */
7101   if (decl_specifiers.any_specifiers_p 
7102       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
7103     cp_parser_commit_to_tentative_parse (parser);
7104
7105   /* Keep going until we hit the `;' at the end of the simple
7106      declaration.  */
7107   saw_declarator = false;
7108   while (cp_lexer_next_token_is_not (parser->lexer,
7109                                      CPP_SEMICOLON))
7110     {
7111       cp_token *token;
7112       bool function_definition_p;
7113       tree decl;
7114
7115       saw_declarator = true;
7116       /* Parse the init-declarator.  */
7117       decl = cp_parser_init_declarator (parser, &decl_specifiers,
7118                                         function_definition_allowed_p,
7119                                         /*member_p=*/false,
7120                                         declares_class_or_enum,
7121                                         &function_definition_p);
7122       /* If an error occurred while parsing tentatively, exit quickly.
7123          (That usually happens when in the body of a function; each
7124          statement is treated as a declaration-statement until proven
7125          otherwise.)  */
7126       if (cp_parser_error_occurred (parser))
7127         goto done;
7128       /* Handle function definitions specially.  */
7129       if (function_definition_p)
7130         {
7131           /* If the next token is a `,', then we are probably
7132              processing something like:
7133
7134                void f() {}, *p;
7135
7136              which is erroneous.  */
7137           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
7138             error ("mixing declarations and function-definitions is forbidden");
7139           /* Otherwise, we're done with the list of declarators.  */
7140           else
7141             {
7142               pop_deferring_access_checks ();
7143               return;
7144             }
7145         }
7146       /* The next token should be either a `,' or a `;'.  */
7147       token = cp_lexer_peek_token (parser->lexer);
7148       /* If it's a `,', there are more declarators to come.  */
7149       if (token->type == CPP_COMMA)
7150         cp_lexer_consume_token (parser->lexer);
7151       /* If it's a `;', we are done.  */
7152       else if (token->type == CPP_SEMICOLON)
7153         break;
7154       /* Anything else is an error.  */
7155       else
7156         {
7157           /* If we have already issued an error message we don't need
7158              to issue another one.  */
7159           if (decl != error_mark_node
7160               || cp_parser_uncommitted_to_tentative_parse_p (parser))
7161             cp_parser_error (parser, "expected %<,%> or %<;%>");
7162           /* Skip tokens until we reach the end of the statement.  */
7163           cp_parser_skip_to_end_of_statement (parser);
7164           /* If the next token is now a `;', consume it.  */
7165           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7166             cp_lexer_consume_token (parser->lexer);
7167           goto done;
7168         }
7169       /* After the first time around, a function-definition is not
7170          allowed -- even if it was OK at first.  For example:
7171
7172            int i, f() {}
7173
7174          is not valid.  */
7175       function_definition_allowed_p = false;
7176     }
7177
7178   /* Issue an error message if no declarators are present, and the
7179      decl-specifier-seq does not itself declare a class or
7180      enumeration.  */
7181   if (!saw_declarator)
7182     {
7183       if (cp_parser_declares_only_class_p (parser))
7184         shadow_tag (&decl_specifiers);
7185       /* Perform any deferred access checks.  */
7186       perform_deferred_access_checks ();
7187     }
7188
7189   /* Consume the `;'.  */
7190   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
7191
7192  done:
7193   pop_deferring_access_checks ();
7194 }
7195
7196 /* Parse a decl-specifier-seq.
7197
7198    decl-specifier-seq:
7199      decl-specifier-seq [opt] decl-specifier
7200
7201    decl-specifier:
7202      storage-class-specifier
7203      type-specifier
7204      function-specifier
7205      friend
7206      typedef
7207
7208    GNU Extension:
7209
7210    decl-specifier:
7211      attributes
7212
7213    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7214
7215    The parser flags FLAGS is used to control type-specifier parsing.
7216
7217    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7218    flags:
7219
7220      1: one of the decl-specifiers is an elaborated-type-specifier
7221         (i.e., a type declaration)
7222      2: one of the decl-specifiers is an enum-specifier or a
7223         class-specifier (i.e., a type definition)
7224
7225    */
7226
7227 static void
7228 cp_parser_decl_specifier_seq (cp_parser* parser,
7229                               cp_parser_flags flags,
7230                               cp_decl_specifier_seq *decl_specs,
7231                               int* declares_class_or_enum)
7232 {
7233   bool constructor_possible_p = !parser->in_declarator_p;
7234
7235   /* Clear DECL_SPECS.  */
7236   clear_decl_specs (decl_specs);
7237
7238   /* Assume no class or enumeration type is declared.  */
7239   *declares_class_or_enum = 0;
7240
7241   /* Keep reading specifiers until there are no more to read.  */
7242   while (true)
7243     {
7244       bool constructor_p;
7245       bool found_decl_spec;
7246       cp_token *token;
7247
7248       /* Peek at the next token.  */
7249       token = cp_lexer_peek_token (parser->lexer);
7250       /* Handle attributes.  */
7251       if (token->keyword == RID_ATTRIBUTE)
7252         {
7253           /* Parse the attributes.  */
7254           decl_specs->attributes
7255             = chainon (decl_specs->attributes,
7256                        cp_parser_attributes_opt (parser));
7257           continue;
7258         }
7259       /* Assume we will find a decl-specifier keyword.  */
7260       found_decl_spec = true;
7261       /* If the next token is an appropriate keyword, we can simply
7262          add it to the list.  */
7263       switch (token->keyword)
7264         {
7265           /* decl-specifier:
7266                friend  */
7267         case RID_FRIEND:
7268           if (decl_specs->specs[(int) ds_friend]++)
7269             error ("duplicate %<friend%>");
7270           /* Consume the token.  */
7271           cp_lexer_consume_token (parser->lexer);
7272           break;
7273
7274           /* function-specifier:
7275                inline
7276                virtual
7277                explicit  */
7278         case RID_INLINE:
7279         case RID_VIRTUAL:
7280         case RID_EXPLICIT:
7281           cp_parser_function_specifier_opt (parser, decl_specs);
7282           break;
7283
7284           /* decl-specifier:
7285                typedef  */
7286         case RID_TYPEDEF:
7287           ++decl_specs->specs[(int) ds_typedef];
7288           /* Consume the token.  */
7289           cp_lexer_consume_token (parser->lexer);
7290           /* A constructor declarator cannot appear in a typedef.  */
7291           constructor_possible_p = false;
7292           /* The "typedef" keyword can only occur in a declaration; we
7293              may as well commit at this point.  */
7294           cp_parser_commit_to_tentative_parse (parser);
7295           break;
7296
7297           /* storage-class-specifier:
7298                auto
7299                register
7300                static
7301                extern
7302                mutable
7303
7304              GNU Extension:
7305                thread  */
7306         case RID_AUTO:
7307           /* Consume the token.  */
7308           cp_lexer_consume_token (parser->lexer);
7309           cp_parser_set_storage_class (decl_specs, sc_auto);
7310           break;
7311         case RID_REGISTER:
7312           /* Consume the token.  */
7313           cp_lexer_consume_token (parser->lexer);
7314           cp_parser_set_storage_class (decl_specs, sc_register);
7315           break;
7316         case RID_STATIC:
7317           /* Consume the token.  */
7318           cp_lexer_consume_token (parser->lexer);
7319           if (decl_specs->specs[(int) ds_thread])
7320             {
7321               error ("%<__thread%> before %<static%>");
7322               decl_specs->specs[(int) ds_thread] = 0;
7323             }
7324           cp_parser_set_storage_class (decl_specs, sc_static);
7325           break;
7326         case RID_EXTERN:
7327           /* Consume the token.  */
7328           cp_lexer_consume_token (parser->lexer);
7329           if (decl_specs->specs[(int) ds_thread])
7330             {
7331               error ("%<__thread%> before %<extern%>");
7332               decl_specs->specs[(int) ds_thread] = 0;
7333             }
7334           cp_parser_set_storage_class (decl_specs, sc_extern);
7335           break;
7336         case RID_MUTABLE:
7337           /* Consume the token.  */
7338           cp_lexer_consume_token (parser->lexer);
7339           cp_parser_set_storage_class (decl_specs, sc_mutable);
7340           break;
7341         case RID_THREAD:
7342           /* Consume the token.  */
7343           cp_lexer_consume_token (parser->lexer);
7344           ++decl_specs->specs[(int) ds_thread];
7345           break;
7346
7347         default:
7348           /* We did not yet find a decl-specifier yet.  */
7349           found_decl_spec = false;
7350           break;
7351         }
7352
7353       /* Constructors are a special case.  The `S' in `S()' is not a
7354          decl-specifier; it is the beginning of the declarator.  */
7355       constructor_p
7356         = (!found_decl_spec
7357            && constructor_possible_p
7358            && (cp_parser_constructor_declarator_p
7359                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7360
7361       /* If we don't have a DECL_SPEC yet, then we must be looking at
7362          a type-specifier.  */
7363       if (!found_decl_spec && !constructor_p)
7364         {
7365           int decl_spec_declares_class_or_enum;
7366           bool is_cv_qualifier;
7367           tree type_spec;
7368
7369           type_spec
7370             = cp_parser_type_specifier (parser, flags,
7371                                         decl_specs,
7372                                         /*is_declaration=*/true,
7373                                         &decl_spec_declares_class_or_enum,
7374                                         &is_cv_qualifier);
7375
7376           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7377
7378           /* If this type-specifier referenced a user-defined type
7379              (a typedef, class-name, etc.), then we can't allow any
7380              more such type-specifiers henceforth.
7381
7382              [dcl.spec]
7383
7384              The longest sequence of decl-specifiers that could
7385              possibly be a type name is taken as the
7386              decl-specifier-seq of a declaration.  The sequence shall
7387              be self-consistent as described below.
7388
7389              [dcl.type]
7390
7391              As a general rule, at most one type-specifier is allowed
7392              in the complete decl-specifier-seq of a declaration.  The
7393              only exceptions are the following:
7394
7395              -- const or volatile can be combined with any other
7396                 type-specifier.
7397
7398              -- signed or unsigned can be combined with char, long,
7399                 short, or int.
7400
7401              -- ..
7402
7403              Example:
7404
7405                typedef char* Pc;
7406                void g (const int Pc);
7407
7408              Here, Pc is *not* part of the decl-specifier seq; it's
7409              the declarator.  Therefore, once we see a type-specifier
7410              (other than a cv-qualifier), we forbid any additional
7411              user-defined types.  We *do* still allow things like `int
7412              int' to be considered a decl-specifier-seq, and issue the
7413              error message later.  */
7414           if (type_spec && !is_cv_qualifier)
7415             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7416           /* A constructor declarator cannot follow a type-specifier.  */
7417           if (type_spec)
7418             {
7419               constructor_possible_p = false;
7420               found_decl_spec = true;
7421             }
7422         }
7423
7424       /* If we still do not have a DECL_SPEC, then there are no more
7425          decl-specifiers.  */
7426       if (!found_decl_spec)
7427         break;
7428
7429       decl_specs->any_specifiers_p = true;
7430       /* After we see one decl-specifier, further decl-specifiers are
7431          always optional.  */
7432       flags |= CP_PARSER_FLAGS_OPTIONAL;
7433     }
7434
7435   /* Don't allow a friend specifier with a class definition.  */
7436   if (decl_specs->specs[(int) ds_friend] != 0
7437       && (*declares_class_or_enum & 2))
7438     error ("class definition may not be declared a friend");
7439 }
7440
7441 /* Parse an (optional) storage-class-specifier.
7442
7443    storage-class-specifier:
7444      auto
7445      register
7446      static
7447      extern
7448      mutable
7449
7450    GNU Extension:
7451
7452    storage-class-specifier:
7453      thread
7454
7455    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7456
7457 static tree
7458 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7459 {
7460   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7461     {
7462     case RID_AUTO:
7463     case RID_REGISTER:
7464     case RID_STATIC:
7465     case RID_EXTERN:
7466     case RID_MUTABLE:
7467     case RID_THREAD:
7468       /* Consume the token.  */
7469       return cp_lexer_consume_token (parser->lexer)->value;
7470
7471     default:
7472       return NULL_TREE;
7473     }
7474 }
7475
7476 /* Parse an (optional) function-specifier.
7477
7478    function-specifier:
7479      inline
7480      virtual
7481      explicit
7482
7483    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7484    Updates DECL_SPECS, if it is non-NULL.  */
7485
7486 static tree
7487 cp_parser_function_specifier_opt (cp_parser* parser,
7488                                   cp_decl_specifier_seq *decl_specs)
7489 {
7490   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7491     {
7492     case RID_INLINE:
7493       if (decl_specs)
7494         ++decl_specs->specs[(int) ds_inline];
7495       break;
7496
7497     case RID_VIRTUAL:
7498       if (decl_specs)
7499         ++decl_specs->specs[(int) ds_virtual];
7500       break;
7501
7502     case RID_EXPLICIT:
7503       if (decl_specs)
7504         ++decl_specs->specs[(int) ds_explicit];
7505       break;
7506
7507     default:
7508       return NULL_TREE;
7509     }
7510
7511   /* Consume the token.  */
7512   return cp_lexer_consume_token (parser->lexer)->value;
7513 }
7514
7515 /* Parse a linkage-specification.
7516
7517    linkage-specification:
7518      extern string-literal { declaration-seq [opt] }
7519      extern string-literal declaration  */
7520
7521 static void
7522 cp_parser_linkage_specification (cp_parser* parser)
7523 {
7524   tree linkage;
7525
7526   /* Look for the `extern' keyword.  */
7527   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7528
7529   /* Look for the string-literal.  */
7530   linkage = cp_parser_string_literal (parser, false, false);
7531
7532   /* Transform the literal into an identifier.  If the literal is a
7533      wide-character string, or contains embedded NULs, then we can't
7534      handle it as the user wants.  */
7535   if (strlen (TREE_STRING_POINTER (linkage))
7536       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7537     {
7538       cp_parser_error (parser, "invalid linkage-specification");
7539       /* Assume C++ linkage.  */
7540       linkage = lang_name_cplusplus;
7541     }
7542   else
7543     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7544
7545   /* We're now using the new linkage.  */
7546   push_lang_context (linkage);
7547
7548   /* If the next token is a `{', then we're using the first
7549      production.  */
7550   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7551     {
7552       /* Consume the `{' token.  */
7553       cp_lexer_consume_token (parser->lexer);
7554       /* Parse the declarations.  */
7555       cp_parser_declaration_seq_opt (parser);
7556       /* Look for the closing `}'.  */
7557       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7558     }
7559   /* Otherwise, there's just one declaration.  */
7560   else
7561     {
7562       bool saved_in_unbraced_linkage_specification_p;
7563
7564       saved_in_unbraced_linkage_specification_p
7565         = parser->in_unbraced_linkage_specification_p;
7566       parser->in_unbraced_linkage_specification_p = true;
7567       have_extern_spec = true;
7568       cp_parser_declaration (parser);
7569       have_extern_spec = false;
7570       parser->in_unbraced_linkage_specification_p
7571         = saved_in_unbraced_linkage_specification_p;
7572     }
7573
7574   /* We're done with the linkage-specification.  */
7575   pop_lang_context ();
7576 }
7577
7578 /* Special member functions [gram.special] */
7579
7580 /* Parse a conversion-function-id.
7581
7582    conversion-function-id:
7583      operator conversion-type-id
7584
7585    Returns an IDENTIFIER_NODE representing the operator.  */
7586
7587 static tree
7588 cp_parser_conversion_function_id (cp_parser* parser)
7589 {
7590   tree type;
7591   tree saved_scope;
7592   tree saved_qualifying_scope;
7593   tree saved_object_scope;
7594   tree pushed_scope = NULL_TREE;
7595
7596   /* Look for the `operator' token.  */
7597   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7598     return error_mark_node;
7599   /* When we parse the conversion-type-id, the current scope will be
7600      reset.  However, we need that information in able to look up the
7601      conversion function later, so we save it here.  */
7602   saved_scope = parser->scope;
7603   saved_qualifying_scope = parser->qualifying_scope;
7604   saved_object_scope = parser->object_scope;
7605   /* We must enter the scope of the class so that the names of
7606      entities declared within the class are available in the
7607      conversion-type-id.  For example, consider:
7608
7609        struct S {
7610          typedef int I;
7611          operator I();
7612        };
7613
7614        S::operator I() { ... }
7615
7616      In order to see that `I' is a type-name in the definition, we
7617      must be in the scope of `S'.  */
7618   if (saved_scope)
7619     pushed_scope = push_scope (saved_scope);
7620   /* Parse the conversion-type-id.  */
7621   type = cp_parser_conversion_type_id (parser);
7622   /* Leave the scope of the class, if any.  */
7623   if (pushed_scope)
7624     pop_scope (pushed_scope);
7625   /* Restore the saved scope.  */
7626   parser->scope = saved_scope;
7627   parser->qualifying_scope = saved_qualifying_scope;
7628   parser->object_scope = saved_object_scope;
7629   /* If the TYPE is invalid, indicate failure.  */
7630   if (type == error_mark_node)
7631     return error_mark_node;
7632   return mangle_conv_op_name_for_type (type);
7633 }
7634
7635 /* Parse a conversion-type-id:
7636
7637    conversion-type-id:
7638      type-specifier-seq conversion-declarator [opt]
7639
7640    Returns the TYPE specified.  */
7641
7642 static tree
7643 cp_parser_conversion_type_id (cp_parser* parser)
7644 {
7645   tree attributes;
7646   cp_decl_specifier_seq type_specifiers;
7647   cp_declarator *declarator;
7648   tree type_specified;
7649
7650   /* Parse the attributes.  */
7651   attributes = cp_parser_attributes_opt (parser);
7652   /* Parse the type-specifiers.  */
7653   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
7654                                 &type_specifiers);
7655   /* If that didn't work, stop.  */
7656   if (type_specifiers.type == error_mark_node)
7657     return error_mark_node;
7658   /* Parse the conversion-declarator.  */
7659   declarator = cp_parser_conversion_declarator_opt (parser);
7660
7661   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7662                                     /*initialized=*/0, &attributes);
7663   if (attributes)
7664     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7665   return type_specified;
7666 }
7667
7668 /* Parse an (optional) conversion-declarator.
7669
7670    conversion-declarator:
7671      ptr-operator conversion-declarator [opt]
7672
7673    */
7674
7675 static cp_declarator *
7676 cp_parser_conversion_declarator_opt (cp_parser* parser)
7677 {
7678   enum tree_code code;
7679   tree class_type;
7680   cp_cv_quals cv_quals;
7681
7682   /* We don't know if there's a ptr-operator next, or not.  */
7683   cp_parser_parse_tentatively (parser);
7684   /* Try the ptr-operator.  */
7685   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7686   /* If it worked, look for more conversion-declarators.  */
7687   if (cp_parser_parse_definitely (parser))
7688     {
7689       cp_declarator *declarator;
7690
7691       /* Parse another optional declarator.  */
7692       declarator = cp_parser_conversion_declarator_opt (parser);
7693
7694       /* Create the representation of the declarator.  */
7695       if (class_type)
7696         declarator = make_ptrmem_declarator (cv_quals, class_type,
7697                                              declarator);
7698       else if (code == INDIRECT_REF)
7699         declarator = make_pointer_declarator (cv_quals, declarator);
7700       else
7701         declarator = make_reference_declarator (cv_quals, declarator);
7702
7703       return declarator;
7704    }
7705
7706   return NULL;
7707 }
7708
7709 /* Parse an (optional) ctor-initializer.
7710
7711    ctor-initializer:
7712      : mem-initializer-list
7713
7714    Returns TRUE iff the ctor-initializer was actually present.  */
7715
7716 static bool
7717 cp_parser_ctor_initializer_opt (cp_parser* parser)
7718 {
7719   /* If the next token is not a `:', then there is no
7720      ctor-initializer.  */
7721   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7722     {
7723       /* Do default initialization of any bases and members.  */
7724       if (DECL_CONSTRUCTOR_P (current_function_decl))
7725         finish_mem_initializers (NULL_TREE);
7726
7727       return false;
7728     }
7729
7730   /* Consume the `:' token.  */
7731   cp_lexer_consume_token (parser->lexer);
7732   /* And the mem-initializer-list.  */
7733   cp_parser_mem_initializer_list (parser);
7734
7735   return true;
7736 }
7737
7738 /* Parse a mem-initializer-list.
7739
7740    mem-initializer-list:
7741      mem-initializer
7742      mem-initializer , mem-initializer-list  */
7743
7744 static void
7745 cp_parser_mem_initializer_list (cp_parser* parser)
7746 {
7747   tree mem_initializer_list = NULL_TREE;
7748
7749   /* Let the semantic analysis code know that we are starting the
7750      mem-initializer-list.  */
7751   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7752     error ("only constructors take base initializers");
7753
7754   /* Loop through the list.  */
7755   while (true)
7756     {
7757       tree mem_initializer;
7758
7759       /* Parse the mem-initializer.  */
7760       mem_initializer = cp_parser_mem_initializer (parser);
7761       /* Add it to the list, unless it was erroneous.  */
7762       if (mem_initializer)
7763         {
7764           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7765           mem_initializer_list = mem_initializer;
7766         }
7767       /* If the next token is not a `,', we're done.  */
7768       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7769         break;
7770       /* Consume the `,' token.  */
7771       cp_lexer_consume_token (parser->lexer);
7772     }
7773
7774   /* Perform semantic analysis.  */
7775   if (DECL_CONSTRUCTOR_P (current_function_decl))
7776     finish_mem_initializers (mem_initializer_list);
7777 }
7778
7779 /* Parse a mem-initializer.
7780
7781    mem-initializer:
7782      mem-initializer-id ( expression-list [opt] )
7783
7784    GNU extension:
7785
7786    mem-initializer:
7787      ( expression-list [opt] )
7788
7789    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7790    class) or FIELD_DECL (for a non-static data member) to initialize;
7791    the TREE_VALUE is the expression-list.  */
7792
7793 static tree
7794 cp_parser_mem_initializer (cp_parser* parser)
7795 {
7796   tree mem_initializer_id;
7797   tree expression_list;
7798   tree member;
7799
7800   /* Find out what is being initialized.  */
7801   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7802     {
7803       pedwarn ("anachronistic old-style base class initializer");
7804       mem_initializer_id = NULL_TREE;
7805     }
7806   else
7807     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7808   member = expand_member_init (mem_initializer_id);
7809   if (member && !DECL_P (member))
7810     in_base_initializer = 1;
7811
7812   expression_list
7813     = cp_parser_parenthesized_expression_list (parser, false,
7814                                                /*cast_p=*/false,
7815                                                /*non_constant_p=*/NULL);
7816   if (!expression_list)
7817     expression_list = void_type_node;
7818
7819   in_base_initializer = 0;
7820
7821   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7822 }
7823
7824 /* Parse a mem-initializer-id.
7825
7826    mem-initializer-id:
7827      :: [opt] nested-name-specifier [opt] class-name
7828      identifier
7829
7830    Returns a TYPE indicating the class to be initializer for the first
7831    production.  Returns an IDENTIFIER_NODE indicating the data member
7832    to be initialized for the second production.  */
7833
7834 static tree
7835 cp_parser_mem_initializer_id (cp_parser* parser)
7836 {
7837   bool global_scope_p;
7838   bool nested_name_specifier_p;
7839   bool template_p = false;
7840   tree id;
7841
7842   /* `typename' is not allowed in this context ([temp.res]).  */
7843   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7844     {
7845       error ("keyword %<typename%> not allowed in this context (a qualified "
7846              "member initializer is implicitly a type)");
7847       cp_lexer_consume_token (parser->lexer);
7848     }
7849   /* Look for the optional `::' operator.  */
7850   global_scope_p
7851     = (cp_parser_global_scope_opt (parser,
7852                                    /*current_scope_valid_p=*/false)
7853        != NULL_TREE);
7854   /* Look for the optional nested-name-specifier.  The simplest way to
7855      implement:
7856
7857        [temp.res]
7858
7859        The keyword `typename' is not permitted in a base-specifier or
7860        mem-initializer; in these contexts a qualified name that
7861        depends on a template-parameter is implicitly assumed to be a
7862        type name.
7863
7864      is to assume that we have seen the `typename' keyword at this
7865      point.  */
7866   nested_name_specifier_p
7867     = (cp_parser_nested_name_specifier_opt (parser,
7868                                             /*typename_keyword_p=*/true,
7869                                             /*check_dependency_p=*/true,
7870                                             /*type_p=*/true,
7871                                             /*is_declaration=*/true)
7872        != NULL_TREE);
7873   if (nested_name_specifier_p)
7874     template_p = cp_parser_optional_template_keyword (parser);
7875   /* If there is a `::' operator or a nested-name-specifier, then we
7876      are definitely looking for a class-name.  */
7877   if (global_scope_p || nested_name_specifier_p)
7878     return cp_parser_class_name (parser,
7879                                  /*typename_keyword_p=*/true,
7880                                  /*template_keyword_p=*/template_p,
7881                                  none_type,
7882                                  /*check_dependency_p=*/true,
7883                                  /*class_head_p=*/false,
7884                                  /*is_declaration=*/true);
7885   /* Otherwise, we could also be looking for an ordinary identifier.  */
7886   cp_parser_parse_tentatively (parser);
7887   /* Try a class-name.  */
7888   id = cp_parser_class_name (parser,
7889                              /*typename_keyword_p=*/true,
7890                              /*template_keyword_p=*/false,
7891                              none_type,
7892                              /*check_dependency_p=*/true,
7893                              /*class_head_p=*/false,
7894                              /*is_declaration=*/true);
7895   /* If we found one, we're done.  */
7896   if (cp_parser_parse_definitely (parser))
7897     return id;
7898   /* Otherwise, look for an ordinary identifier.  */
7899   return cp_parser_identifier (parser);
7900 }
7901
7902 /* Overloading [gram.over] */
7903
7904 /* Parse an operator-function-id.
7905
7906    operator-function-id:
7907      operator operator
7908
7909    Returns an IDENTIFIER_NODE for the operator which is a
7910    human-readable spelling of the identifier, e.g., `operator +'.  */
7911
7912 static tree
7913 cp_parser_operator_function_id (cp_parser* parser)
7914 {
7915   /* Look for the `operator' keyword.  */
7916   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7917     return error_mark_node;
7918   /* And then the name of the operator itself.  */
7919   return cp_parser_operator (parser);
7920 }
7921
7922 /* Parse an operator.
7923
7924    operator:
7925      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7926      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7927      || ++ -- , ->* -> () []
7928
7929    GNU Extensions:
7930
7931    operator:
7932      <? >? <?= >?=
7933
7934    Returns an IDENTIFIER_NODE for the operator which is a
7935    human-readable spelling of the identifier, e.g., `operator +'.  */
7936
7937 static tree
7938 cp_parser_operator (cp_parser* parser)
7939 {
7940   tree id = NULL_TREE;
7941   cp_token *token;
7942
7943   /* Peek at the next token.  */
7944   token = cp_lexer_peek_token (parser->lexer);
7945   /* Figure out which operator we have.  */
7946   switch (token->type)
7947     {
7948     case CPP_KEYWORD:
7949       {
7950         enum tree_code op;
7951
7952         /* The keyword should be either `new' or `delete'.  */
7953         if (token->keyword == RID_NEW)
7954           op = NEW_EXPR;
7955         else if (token->keyword == RID_DELETE)
7956           op = DELETE_EXPR;
7957         else
7958           break;
7959
7960         /* Consume the `new' or `delete' token.  */
7961         cp_lexer_consume_token (parser->lexer);
7962
7963         /* Peek at the next token.  */
7964         token = cp_lexer_peek_token (parser->lexer);
7965         /* If it's a `[' token then this is the array variant of the
7966            operator.  */
7967         if (token->type == CPP_OPEN_SQUARE)
7968           {
7969             /* Consume the `[' token.  */
7970             cp_lexer_consume_token (parser->lexer);
7971             /* Look for the `]' token.  */
7972             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7973             id = ansi_opname (op == NEW_EXPR
7974                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7975           }
7976         /* Otherwise, we have the non-array variant.  */
7977         else
7978           id = ansi_opname (op);
7979
7980         return id;
7981       }
7982
7983     case CPP_PLUS:
7984       id = ansi_opname (PLUS_EXPR);
7985       break;
7986
7987     case CPP_MINUS:
7988       id = ansi_opname (MINUS_EXPR);
7989       break;
7990
7991     case CPP_MULT:
7992       id = ansi_opname (MULT_EXPR);
7993       break;
7994
7995     case CPP_DIV:
7996       id = ansi_opname (TRUNC_DIV_EXPR);
7997       break;
7998
7999     case CPP_MOD:
8000       id = ansi_opname (TRUNC_MOD_EXPR);
8001       break;
8002
8003     case CPP_XOR:
8004       id = ansi_opname (BIT_XOR_EXPR);
8005       break;
8006
8007     case CPP_AND:
8008       id = ansi_opname (BIT_AND_EXPR);
8009       break;
8010
8011     case CPP_OR:
8012       id = ansi_opname (BIT_IOR_EXPR);
8013       break;
8014
8015     case CPP_COMPL:
8016       id = ansi_opname (BIT_NOT_EXPR);
8017       break;
8018
8019     case CPP_NOT:
8020       id = ansi_opname (TRUTH_NOT_EXPR);
8021       break;
8022
8023     case CPP_EQ:
8024       id = ansi_assopname (NOP_EXPR);
8025       break;
8026
8027     case CPP_LESS:
8028       id = ansi_opname (LT_EXPR);
8029       break;
8030
8031     case CPP_GREATER:
8032       id = ansi_opname (GT_EXPR);
8033       break;
8034
8035     case CPP_PLUS_EQ:
8036       id = ansi_assopname (PLUS_EXPR);
8037       break;
8038
8039     case CPP_MINUS_EQ:
8040       id = ansi_assopname (MINUS_EXPR);
8041       break;
8042
8043     case CPP_MULT_EQ:
8044       id = ansi_assopname (MULT_EXPR);
8045       break;
8046
8047     case CPP_DIV_EQ:
8048       id = ansi_assopname (TRUNC_DIV_EXPR);
8049       break;
8050
8051     case CPP_MOD_EQ:
8052       id = ansi_assopname (TRUNC_MOD_EXPR);
8053       break;
8054
8055     case CPP_XOR_EQ:
8056       id = ansi_assopname (BIT_XOR_EXPR);
8057       break;
8058
8059     case CPP_AND_EQ:
8060       id = ansi_assopname (BIT_AND_EXPR);
8061       break;
8062
8063     case CPP_OR_EQ:
8064       id = ansi_assopname (BIT_IOR_EXPR);
8065       break;
8066
8067     case CPP_LSHIFT:
8068       id = ansi_opname (LSHIFT_EXPR);
8069       break;
8070
8071     case CPP_RSHIFT:
8072       id = ansi_opname (RSHIFT_EXPR);
8073       break;
8074
8075     case CPP_LSHIFT_EQ:
8076       id = ansi_assopname (LSHIFT_EXPR);
8077       break;
8078
8079     case CPP_RSHIFT_EQ:
8080       id = ansi_assopname (RSHIFT_EXPR);
8081       break;
8082
8083     case CPP_EQ_EQ:
8084       id = ansi_opname (EQ_EXPR);
8085       break;
8086
8087     case CPP_NOT_EQ:
8088       id = ansi_opname (NE_EXPR);
8089       break;
8090
8091     case CPP_LESS_EQ:
8092       id = ansi_opname (LE_EXPR);
8093       break;
8094
8095     case CPP_GREATER_EQ:
8096       id = ansi_opname (GE_EXPR);
8097       break;
8098
8099     case CPP_AND_AND:
8100       id = ansi_opname (TRUTH_ANDIF_EXPR);
8101       break;
8102
8103     case CPP_OR_OR:
8104       id = ansi_opname (TRUTH_ORIF_EXPR);
8105       break;
8106
8107     case CPP_PLUS_PLUS:
8108       id = ansi_opname (POSTINCREMENT_EXPR);
8109       break;
8110
8111     case CPP_MINUS_MINUS:
8112       id = ansi_opname (PREDECREMENT_EXPR);
8113       break;
8114
8115     case CPP_COMMA:
8116       id = ansi_opname (COMPOUND_EXPR);
8117       break;
8118
8119     case CPP_DEREF_STAR:
8120       id = ansi_opname (MEMBER_REF);
8121       break;
8122
8123     case CPP_DEREF:
8124       id = ansi_opname (COMPONENT_REF);
8125       break;
8126
8127     case CPP_OPEN_PAREN:
8128       /* Consume the `('.  */
8129       cp_lexer_consume_token (parser->lexer);
8130       /* Look for the matching `)'.  */
8131       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
8132       return ansi_opname (CALL_EXPR);
8133
8134     case CPP_OPEN_SQUARE:
8135       /* Consume the `['.  */
8136       cp_lexer_consume_token (parser->lexer);
8137       /* Look for the matching `]'.  */
8138       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
8139       return ansi_opname (ARRAY_REF);
8140
8141       /* Extensions.  */
8142     case CPP_MIN:
8143       id = ansi_opname (MIN_EXPR);
8144       cp_parser_warn_min_max ();
8145       break;
8146
8147     case CPP_MAX:
8148       id = ansi_opname (MAX_EXPR);
8149       cp_parser_warn_min_max ();
8150       break;
8151
8152     case CPP_MIN_EQ:
8153       id = ansi_assopname (MIN_EXPR);
8154       cp_parser_warn_min_max ();
8155       break;
8156
8157     case CPP_MAX_EQ:
8158       id = ansi_assopname (MAX_EXPR);
8159       cp_parser_warn_min_max ();
8160       break;
8161
8162     default:
8163       /* Anything else is an error.  */
8164       break;
8165     }
8166
8167   /* If we have selected an identifier, we need to consume the
8168      operator token.  */
8169   if (id)
8170     cp_lexer_consume_token (parser->lexer);
8171   /* Otherwise, no valid operator name was present.  */
8172   else
8173     {
8174       cp_parser_error (parser, "expected operator");
8175       id = error_mark_node;
8176     }
8177
8178   return id;
8179 }
8180
8181 /* Parse a template-declaration.
8182
8183    template-declaration:
8184      export [opt] template < template-parameter-list > declaration
8185
8186    If MEMBER_P is TRUE, this template-declaration occurs within a
8187    class-specifier.
8188
8189    The grammar rule given by the standard isn't correct.  What
8190    is really meant is:
8191
8192    template-declaration:
8193      export [opt] template-parameter-list-seq
8194        decl-specifier-seq [opt] init-declarator [opt] ;
8195      export [opt] template-parameter-list-seq
8196        function-definition
8197
8198    template-parameter-list-seq:
8199      template-parameter-list-seq [opt]
8200      template < template-parameter-list >  */
8201
8202 static void
8203 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8204 {
8205   /* Check for `export'.  */
8206   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8207     {
8208       /* Consume the `export' token.  */
8209       cp_lexer_consume_token (parser->lexer);
8210       /* Warn that we do not support `export'.  */
8211       warning (0, "keyword %<export%> not implemented, and will be ignored");
8212     }
8213
8214   cp_parser_template_declaration_after_export (parser, member_p);
8215 }
8216
8217 /* Parse a template-parameter-list.
8218
8219    template-parameter-list:
8220      template-parameter
8221      template-parameter-list , template-parameter
8222
8223    Returns a TREE_LIST.  Each node represents a template parameter.
8224    The nodes are connected via their TREE_CHAINs.  */
8225
8226 static tree
8227 cp_parser_template_parameter_list (cp_parser* parser)
8228 {
8229   tree parameter_list = NULL_TREE;
8230
8231   while (true)
8232     {
8233       tree parameter;
8234       cp_token *token;
8235       bool is_non_type;
8236
8237       /* Parse the template-parameter.  */
8238       parameter = cp_parser_template_parameter (parser, &is_non_type);
8239       /* Add it to the list.  */
8240       if (parameter != error_mark_node)
8241         parameter_list = process_template_parm (parameter_list,
8242                                                 parameter,
8243                                                 is_non_type);
8244       /* Peek at the next token.  */
8245       token = cp_lexer_peek_token (parser->lexer);
8246       /* If it's not a `,', we're done.  */
8247       if (token->type != CPP_COMMA)
8248         break;
8249       /* Otherwise, consume the `,' token.  */
8250       cp_lexer_consume_token (parser->lexer);
8251     }
8252
8253   return parameter_list;
8254 }
8255
8256 /* Parse a template-parameter.
8257
8258    template-parameter:
8259      type-parameter
8260      parameter-declaration
8261
8262    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
8263    the parameter.  The TREE_PURPOSE is the default value, if any.
8264    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
8265    iff this parameter is a non-type parameter.  */
8266
8267 static tree
8268 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8269 {
8270   cp_token *token;
8271   cp_parameter_declarator *parameter_declarator;
8272   tree parm;
8273
8274   /* Assume it is a type parameter or a template parameter.  */
8275   *is_non_type = false;
8276   /* Peek at the next token.  */
8277   token = cp_lexer_peek_token (parser->lexer);
8278   /* If it is `class' or `template', we have a type-parameter.  */
8279   if (token->keyword == RID_TEMPLATE)
8280     return cp_parser_type_parameter (parser);
8281   /* If it is `class' or `typename' we do not know yet whether it is a
8282      type parameter or a non-type parameter.  Consider:
8283
8284        template <typename T, typename T::X X> ...
8285
8286      or:
8287
8288        template <class C, class D*> ...
8289
8290      Here, the first parameter is a type parameter, and the second is
8291      a non-type parameter.  We can tell by looking at the token after
8292      the identifier -- if it is a `,', `=', or `>' then we have a type
8293      parameter.  */
8294   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8295     {
8296       /* Peek at the token after `class' or `typename'.  */
8297       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8298       /* If it's an identifier, skip it.  */
8299       if (token->type == CPP_NAME)
8300         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8301       /* Now, see if the token looks like the end of a template
8302          parameter.  */
8303       if (token->type == CPP_COMMA
8304           || token->type == CPP_EQ
8305           || token->type == CPP_GREATER)
8306         return cp_parser_type_parameter (parser);
8307     }
8308
8309   /* Otherwise, it is a non-type parameter.
8310
8311      [temp.param]
8312
8313      When parsing a default template-argument for a non-type
8314      template-parameter, the first non-nested `>' is taken as the end
8315      of the template parameter-list rather than a greater-than
8316      operator.  */
8317   *is_non_type = true;
8318   parameter_declarator
8319      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8320                                         /*parenthesized_p=*/NULL);
8321   parm = grokdeclarator (parameter_declarator->declarator,
8322                          &parameter_declarator->decl_specifiers,
8323                          PARM, /*initialized=*/0,
8324                          /*attrlist=*/NULL);
8325   if (parm == error_mark_node)
8326     return error_mark_node;
8327   return build_tree_list (parameter_declarator->default_argument, parm);
8328 }
8329
8330 /* Parse a type-parameter.
8331
8332    type-parameter:
8333      class identifier [opt]
8334      class identifier [opt] = type-id
8335      typename identifier [opt]
8336      typename identifier [opt] = type-id
8337      template < template-parameter-list > class identifier [opt]
8338      template < template-parameter-list > class identifier [opt]
8339        = id-expression
8340
8341    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8342    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8343    the declaration of the parameter.  */
8344
8345 static tree
8346 cp_parser_type_parameter (cp_parser* parser)
8347 {
8348   cp_token *token;
8349   tree parameter;
8350
8351   /* Look for a keyword to tell us what kind of parameter this is.  */
8352   token = cp_parser_require (parser, CPP_KEYWORD,
8353                              "`class', `typename', or `template'");
8354   if (!token)
8355     return error_mark_node;
8356
8357   switch (token->keyword)
8358     {
8359     case RID_CLASS:
8360     case RID_TYPENAME:
8361       {
8362         tree identifier;
8363         tree default_argument;
8364
8365         /* If the next token is an identifier, then it names the
8366            parameter.  */
8367         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8368           identifier = cp_parser_identifier (parser);
8369         else
8370           identifier = NULL_TREE;
8371
8372         /* Create the parameter.  */
8373         parameter = finish_template_type_parm (class_type_node, identifier);
8374
8375         /* If the next token is an `=', we have a default argument.  */
8376         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8377           {
8378             /* Consume the `=' token.  */
8379             cp_lexer_consume_token (parser->lexer);
8380             /* Parse the default-argument.  */
8381             default_argument = cp_parser_type_id (parser);
8382           }
8383         else
8384           default_argument = NULL_TREE;
8385
8386         /* Create the combined representation of the parameter and the
8387            default argument.  */
8388         parameter = build_tree_list (default_argument, parameter);
8389       }
8390       break;
8391
8392     case RID_TEMPLATE:
8393       {
8394         tree parameter_list;
8395         tree identifier;
8396         tree default_argument;
8397
8398         /* Look for the `<'.  */
8399         cp_parser_require (parser, CPP_LESS, "`<'");
8400         /* Parse the template-parameter-list.  */
8401         begin_template_parm_list ();
8402         parameter_list
8403           = cp_parser_template_parameter_list (parser);
8404         parameter_list = end_template_parm_list (parameter_list);
8405         /* Look for the `>'.  */
8406         cp_parser_require (parser, CPP_GREATER, "`>'");
8407         /* Look for the `class' keyword.  */
8408         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8409         /* If the next token is an `=', then there is a
8410            default-argument.  If the next token is a `>', we are at
8411            the end of the parameter-list.  If the next token is a `,',
8412            then we are at the end of this parameter.  */
8413         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8414             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8415             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8416           {
8417             identifier = cp_parser_identifier (parser);
8418             /* Treat invalid names as if the parameter were nameless.  */
8419             if (identifier == error_mark_node)
8420               identifier = NULL_TREE;
8421           }
8422         else
8423           identifier = NULL_TREE;
8424
8425         /* Create the template parameter.  */
8426         parameter = finish_template_template_parm (class_type_node,
8427                                                    identifier);
8428
8429         /* If the next token is an `=', then there is a
8430            default-argument.  */
8431         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8432           {
8433             bool is_template;
8434
8435             /* Consume the `='.  */
8436             cp_lexer_consume_token (parser->lexer);
8437             /* Parse the id-expression.  */
8438             default_argument
8439               = cp_parser_id_expression (parser,
8440                                          /*template_keyword_p=*/false,
8441                                          /*check_dependency_p=*/true,
8442                                          /*template_p=*/&is_template,
8443                                          /*declarator_p=*/false);
8444             if (TREE_CODE (default_argument) == TYPE_DECL)
8445               /* If the id-expression was a template-id that refers to
8446                  a template-class, we already have the declaration here,
8447                  so no further lookup is needed.  */
8448                  ;
8449             else
8450               /* Look up the name.  */
8451               default_argument
8452                 = cp_parser_lookup_name (parser, default_argument,
8453                                          none_type,
8454                                          /*is_template=*/is_template,
8455                                          /*is_namespace=*/false,
8456                                          /*check_dependency=*/true,
8457                                          /*ambiguous_p=*/NULL);
8458             /* See if the default argument is valid.  */
8459             default_argument
8460               = check_template_template_default_arg (default_argument);
8461           }
8462         else
8463           default_argument = NULL_TREE;
8464
8465         /* Create the combined representation of the parameter and the
8466            default argument.  */
8467         parameter = build_tree_list (default_argument, parameter);
8468       }
8469       break;
8470
8471     default:
8472       gcc_unreachable ();
8473       break;
8474     }
8475
8476   return parameter;
8477 }
8478
8479 /* Parse a template-id.
8480
8481    template-id:
8482      template-name < template-argument-list [opt] >
8483
8484    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8485    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8486    returned.  Otherwise, if the template-name names a function, or set
8487    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8488    names a class, returns a TYPE_DECL for the specialization.
8489
8490    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8491    uninstantiated templates.  */
8492
8493 static tree
8494 cp_parser_template_id (cp_parser *parser,
8495                        bool template_keyword_p,
8496                        bool check_dependency_p,
8497                        bool is_declaration)
8498 {
8499   tree template;
8500   tree arguments;
8501   tree template_id;
8502   cp_token_position start_of_id = 0;
8503   tree access_check = NULL_TREE;
8504   cp_token *next_token, *next_token_2;
8505   bool is_identifier;
8506
8507   /* If the next token corresponds to a template-id, there is no need
8508      to reparse it.  */
8509   next_token = cp_lexer_peek_token (parser->lexer);
8510   if (next_token->type == CPP_TEMPLATE_ID)
8511     {
8512       tree value;
8513       tree check;
8514
8515       /* Get the stored value.  */
8516       value = cp_lexer_consume_token (parser->lexer)->value;
8517       /* Perform any access checks that were deferred.  */
8518       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8519         perform_or_defer_access_check (TREE_PURPOSE (check),
8520                                        TREE_VALUE (check));
8521       /* Return the stored value.  */
8522       return TREE_VALUE (value);
8523     }
8524
8525   /* Avoid performing name lookup if there is no possibility of
8526      finding a template-id.  */
8527   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8528       || (next_token->type == CPP_NAME
8529           && !cp_parser_nth_token_starts_template_argument_list_p
8530                (parser, 2)))
8531     {
8532       cp_parser_error (parser, "expected template-id");
8533       return error_mark_node;
8534     }
8535
8536   /* Remember where the template-id starts.  */
8537   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
8538     start_of_id = cp_lexer_token_position (parser->lexer, false);
8539
8540   push_deferring_access_checks (dk_deferred);
8541
8542   /* Parse the template-name.  */
8543   is_identifier = false;
8544   template = cp_parser_template_name (parser, template_keyword_p,
8545                                       check_dependency_p,
8546                                       is_declaration,
8547                                       &is_identifier);
8548   if (template == error_mark_node || is_identifier)
8549     {
8550       pop_deferring_access_checks ();
8551       return template;
8552     }
8553
8554   /* If we find the sequence `[:' after a template-name, it's probably
8555      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8556      parse correctly the argument list.  */
8557   next_token = cp_lexer_peek_token (parser->lexer);
8558   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8559   if (next_token->type == CPP_OPEN_SQUARE
8560       && next_token->flags & DIGRAPH
8561       && next_token_2->type == CPP_COLON
8562       && !(next_token_2->flags & PREV_WHITE))
8563     {
8564       cp_parser_parse_tentatively (parser);
8565       /* Change `:' into `::'.  */
8566       next_token_2->type = CPP_SCOPE;
8567       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8568          CPP_LESS.  */
8569       cp_lexer_consume_token (parser->lexer);
8570       /* Parse the arguments.  */
8571       arguments = cp_parser_enclosed_template_argument_list (parser);
8572       if (!cp_parser_parse_definitely (parser))
8573         {
8574           /* If we couldn't parse an argument list, then we revert our changes
8575              and return simply an error. Maybe this is not a template-id
8576              after all.  */
8577           next_token_2->type = CPP_COLON;
8578           cp_parser_error (parser, "expected %<<%>");
8579           pop_deferring_access_checks ();
8580           return error_mark_node;
8581         }
8582       /* Otherwise, emit an error about the invalid digraph, but continue
8583          parsing because we got our argument list.  */
8584       pedwarn ("%<<::%> cannot begin a template-argument list");
8585       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8586               "between %<<%> and %<::%>");
8587       if (!flag_permissive)
8588         {
8589           static bool hint;
8590           if (!hint)
8591             {
8592               inform ("(if you use -fpermissive G++ will accept your code)");
8593               hint = true;
8594             }
8595         }
8596     }
8597   else
8598     {
8599       /* Look for the `<' that starts the template-argument-list.  */
8600       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8601         {
8602           pop_deferring_access_checks ();
8603           return error_mark_node;
8604         }
8605       /* Parse the arguments.  */
8606       arguments = cp_parser_enclosed_template_argument_list (parser);
8607     }
8608
8609   /* Build a representation of the specialization.  */
8610   if (TREE_CODE (template) == IDENTIFIER_NODE)
8611     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8612   else if (DECL_CLASS_TEMPLATE_P (template)
8613            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8614     template_id
8615       = finish_template_type (template, arguments,
8616                               cp_lexer_next_token_is (parser->lexer,
8617                                                       CPP_SCOPE));
8618   else
8619     {
8620       /* If it's not a class-template or a template-template, it should be
8621          a function-template.  */
8622       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8623                    || TREE_CODE (template) == OVERLOAD
8624                    || BASELINK_P (template)));
8625
8626       template_id = lookup_template_function (template, arguments);
8627     }
8628
8629   /* Retrieve any deferred checks.  Do not pop this access checks yet
8630      so the memory will not be reclaimed during token replacing below.  */
8631   access_check = get_deferred_access_checks ();
8632
8633   /* If parsing tentatively, replace the sequence of tokens that makes
8634      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8635      should we re-parse the token stream, we will not have to repeat
8636      the effort required to do the parse, nor will we issue duplicate
8637      error messages about problems during instantiation of the
8638      template.  */
8639   if (start_of_id)
8640     {
8641       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8642       
8643       /* Reset the contents of the START_OF_ID token.  */
8644       token->type = CPP_TEMPLATE_ID;
8645       token->value = build_tree_list (access_check, template_id);
8646       token->keyword = RID_MAX;
8647       
8648       /* Purge all subsequent tokens.  */
8649       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8650
8651       /* ??? Can we actually assume that, if template_id ==
8652          error_mark_node, we will have issued a diagnostic to the
8653          user, as opposed to simply marking the tentative parse as
8654          failed?  */
8655       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
8656         error ("parse error in template argument list");
8657     }
8658
8659   pop_deferring_access_checks ();
8660   return template_id;
8661 }
8662
8663 /* Parse a template-name.
8664
8665    template-name:
8666      identifier
8667
8668    The standard should actually say:
8669
8670    template-name:
8671      identifier
8672      operator-function-id
8673
8674    A defect report has been filed about this issue.
8675
8676    A conversion-function-id cannot be a template name because they cannot
8677    be part of a template-id. In fact, looking at this code:
8678
8679    a.operator K<int>()
8680
8681    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8682    It is impossible to call a templated conversion-function-id with an
8683    explicit argument list, since the only allowed template parameter is
8684    the type to which it is converting.
8685
8686    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8687    `template' keyword, in a construction like:
8688
8689      T::template f<3>()
8690
8691    In that case `f' is taken to be a template-name, even though there
8692    is no way of knowing for sure.
8693
8694    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8695    name refers to a set of overloaded functions, at least one of which
8696    is a template, or an IDENTIFIER_NODE with the name of the template,
8697    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8698    names are looked up inside uninstantiated templates.  */
8699
8700 static tree
8701 cp_parser_template_name (cp_parser* parser,
8702                          bool template_keyword_p,
8703                          bool check_dependency_p,
8704                          bool is_declaration,
8705                          bool *is_identifier)
8706 {
8707   tree identifier;
8708   tree decl;
8709   tree fns;
8710
8711   /* If the next token is `operator', then we have either an
8712      operator-function-id or a conversion-function-id.  */
8713   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8714     {
8715       /* We don't know whether we're looking at an
8716          operator-function-id or a conversion-function-id.  */
8717       cp_parser_parse_tentatively (parser);
8718       /* Try an operator-function-id.  */
8719       identifier = cp_parser_operator_function_id (parser);
8720       /* If that didn't work, try a conversion-function-id.  */
8721       if (!cp_parser_parse_definitely (parser))
8722         {
8723           cp_parser_error (parser, "expected template-name");
8724           return error_mark_node;
8725         }
8726     }
8727   /* Look for the identifier.  */
8728   else
8729     identifier = cp_parser_identifier (parser);
8730
8731   /* If we didn't find an identifier, we don't have a template-id.  */
8732   if (identifier == error_mark_node)
8733     return error_mark_node;
8734
8735   /* If the name immediately followed the `template' keyword, then it
8736      is a template-name.  However, if the next token is not `<', then
8737      we do not treat it as a template-name, since it is not being used
8738      as part of a template-id.  This enables us to handle constructs
8739      like:
8740
8741        template <typename T> struct S { S(); };
8742        template <typename T> S<T>::S();
8743
8744      correctly.  We would treat `S' as a template -- if it were `S<T>'
8745      -- but we do not if there is no `<'.  */
8746
8747   if (processing_template_decl
8748       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8749     {
8750       /* In a declaration, in a dependent context, we pretend that the
8751          "template" keyword was present in order to improve error
8752          recovery.  For example, given:
8753
8754            template <typename T> void f(T::X<int>);
8755
8756          we want to treat "X<int>" as a template-id.  */
8757       if (is_declaration
8758           && !template_keyword_p
8759           && parser->scope && TYPE_P (parser->scope)
8760           && check_dependency_p
8761           && dependent_type_p (parser->scope)
8762           /* Do not do this for dtors (or ctors), since they never
8763              need the template keyword before their name.  */
8764           && !constructor_name_p (identifier, parser->scope))
8765         {
8766           cp_token_position start = 0;
8767           
8768           /* Explain what went wrong.  */
8769           error ("non-template %qD used as template", identifier);
8770           inform ("use %<%T::template %D%> to indicate that it is a template",
8771                   parser->scope, identifier);
8772           /* If parsing tentatively, find the location of the "<" token.  */
8773           if (cp_parser_simulate_error (parser))
8774             start = cp_lexer_token_position (parser->lexer, true);
8775           /* Parse the template arguments so that we can issue error
8776              messages about them.  */
8777           cp_lexer_consume_token (parser->lexer);
8778           cp_parser_enclosed_template_argument_list (parser);
8779           /* Skip tokens until we find a good place from which to
8780              continue parsing.  */
8781           cp_parser_skip_to_closing_parenthesis (parser,
8782                                                  /*recovering=*/true,
8783                                                  /*or_comma=*/true,
8784                                                  /*consume_paren=*/false);
8785           /* If parsing tentatively, permanently remove the
8786              template argument list.  That will prevent duplicate
8787              error messages from being issued about the missing
8788              "template" keyword.  */
8789           if (start)
8790             cp_lexer_purge_tokens_after (parser->lexer, start);
8791           if (is_identifier)
8792             *is_identifier = true;
8793           return identifier;
8794         }
8795
8796       /* If the "template" keyword is present, then there is generally
8797          no point in doing name-lookup, so we just return IDENTIFIER.
8798          But, if the qualifying scope is non-dependent then we can
8799          (and must) do name-lookup normally.  */
8800       if (template_keyword_p
8801           && (!parser->scope
8802               || (TYPE_P (parser->scope)
8803                   && dependent_type_p (parser->scope))))
8804         return identifier;
8805     }
8806
8807   /* Look up the name.  */
8808   decl = cp_parser_lookup_name (parser, identifier,
8809                                 none_type,
8810                                 /*is_template=*/false,
8811                                 /*is_namespace=*/false,
8812                                 check_dependency_p,
8813                                 /*ambiguous_p=*/NULL);
8814   decl = maybe_get_template_decl_from_type_decl (decl);
8815
8816   /* If DECL is a template, then the name was a template-name.  */
8817   if (TREE_CODE (decl) == TEMPLATE_DECL)
8818     ;
8819   else
8820     {
8821       tree fn = NULL_TREE;
8822
8823       /* The standard does not explicitly indicate whether a name that
8824          names a set of overloaded declarations, some of which are
8825          templates, is a template-name.  However, such a name should
8826          be a template-name; otherwise, there is no way to form a
8827          template-id for the overloaded templates.  */
8828       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8829       if (TREE_CODE (fns) == OVERLOAD)
8830         for (fn = fns; fn; fn = OVL_NEXT (fn))
8831           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8832             break;
8833
8834       if (!fn)
8835         {
8836           /* The name does not name a template.  */
8837           cp_parser_error (parser, "expected template-name");
8838           return error_mark_node;
8839         }
8840     }
8841
8842   /* If DECL is dependent, and refers to a function, then just return
8843      its name; we will look it up again during template instantiation.  */
8844   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8845     {
8846       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8847       if (TYPE_P (scope) && dependent_type_p (scope))
8848         return identifier;
8849     }
8850
8851   return decl;
8852 }
8853
8854 /* Parse a template-argument-list.
8855
8856    template-argument-list:
8857      template-argument
8858      template-argument-list , template-argument
8859
8860    Returns a TREE_VEC containing the arguments.  */
8861
8862 static tree
8863 cp_parser_template_argument_list (cp_parser* parser)
8864 {
8865   tree fixed_args[10];
8866   unsigned n_args = 0;
8867   unsigned alloced = 10;
8868   tree *arg_ary = fixed_args;
8869   tree vec;
8870   bool saved_in_template_argument_list_p;
8871
8872   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8873   parser->in_template_argument_list_p = true;
8874   do
8875     {
8876       tree argument;
8877
8878       if (n_args)
8879         /* Consume the comma.  */
8880         cp_lexer_consume_token (parser->lexer);
8881
8882       /* Parse the template-argument.  */
8883       argument = cp_parser_template_argument (parser);
8884       if (n_args == alloced)
8885         {
8886           alloced *= 2;
8887
8888           if (arg_ary == fixed_args)
8889             {
8890               arg_ary = xmalloc (sizeof (tree) * alloced);
8891               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8892             }
8893           else
8894             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8895         }
8896       arg_ary[n_args++] = argument;
8897     }
8898   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8899
8900   vec = make_tree_vec (n_args);
8901
8902   while (n_args--)
8903     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8904
8905   if (arg_ary != fixed_args)
8906     free (arg_ary);
8907   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8908   return vec;
8909 }
8910
8911 /* Parse a template-argument.
8912
8913    template-argument:
8914      assignment-expression
8915      type-id
8916      id-expression
8917
8918    The representation is that of an assignment-expression, type-id, or
8919    id-expression -- except that the qualified id-expression is
8920    evaluated, so that the value returned is either a DECL or an
8921    OVERLOAD.
8922
8923    Although the standard says "assignment-expression", it forbids
8924    throw-expressions or assignments in the template argument.
8925    Therefore, we use "conditional-expression" instead.  */
8926
8927 static tree
8928 cp_parser_template_argument (cp_parser* parser)
8929 {
8930   tree argument;
8931   bool template_p;
8932   bool address_p;
8933   bool maybe_type_id = false;
8934   cp_token *token;
8935   cp_id_kind idk;
8936   tree qualifying_class;
8937
8938   /* There's really no way to know what we're looking at, so we just
8939      try each alternative in order.
8940
8941        [temp.arg]
8942
8943        In a template-argument, an ambiguity between a type-id and an
8944        expression is resolved to a type-id, regardless of the form of
8945        the corresponding template-parameter.
8946
8947      Therefore, we try a type-id first.  */
8948   cp_parser_parse_tentatively (parser);
8949   argument = cp_parser_type_id (parser);
8950   /* If there was no error parsing the type-id but the next token is a '>>',
8951      we probably found a typo for '> >'. But there are type-id which are
8952      also valid expressions. For instance:
8953
8954      struct X { int operator >> (int); };
8955      template <int V> struct Foo {};
8956      Foo<X () >> 5> r;
8957
8958      Here 'X()' is a valid type-id of a function type, but the user just
8959      wanted to write the expression "X() >> 5". Thus, we remember that we
8960      found a valid type-id, but we still try to parse the argument as an
8961      expression to see what happens.  */
8962   if (!cp_parser_error_occurred (parser)
8963       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8964     {
8965       maybe_type_id = true;
8966       cp_parser_abort_tentative_parse (parser);
8967     }
8968   else
8969     {
8970       /* If the next token isn't a `,' or a `>', then this argument wasn't
8971       really finished. This means that the argument is not a valid
8972       type-id.  */
8973       if (!cp_parser_next_token_ends_template_argument_p (parser))
8974         cp_parser_error (parser, "expected template-argument");
8975       /* If that worked, we're done.  */
8976       if (cp_parser_parse_definitely (parser))
8977         return argument;
8978     }
8979   /* We're still not sure what the argument will be.  */
8980   cp_parser_parse_tentatively (parser);
8981   /* Try a template.  */
8982   argument = cp_parser_id_expression (parser,
8983                                       /*template_keyword_p=*/false,
8984                                       /*check_dependency_p=*/true,
8985                                       &template_p,
8986                                       /*declarator_p=*/false);
8987   /* If the next token isn't a `,' or a `>', then this argument wasn't
8988      really finished.  */
8989   if (!cp_parser_next_token_ends_template_argument_p (parser))
8990     cp_parser_error (parser, "expected template-argument");
8991   if (!cp_parser_error_occurred (parser))
8992     {
8993       /* Figure out what is being referred to.  If the id-expression
8994          was for a class template specialization, then we will have a
8995          TYPE_DECL at this point.  There is no need to do name lookup
8996          at this point in that case.  */
8997       if (TREE_CODE (argument) != TYPE_DECL)
8998         argument = cp_parser_lookup_name (parser, argument,
8999                                           none_type,
9000                                           /*is_template=*/template_p,
9001                                           /*is_namespace=*/false,
9002                                           /*check_dependency=*/true,
9003                                           /*ambiguous_p=*/NULL);
9004       if (TREE_CODE (argument) != TEMPLATE_DECL
9005           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
9006         cp_parser_error (parser, "expected template-name");
9007     }
9008   if (cp_parser_parse_definitely (parser))
9009     return argument;
9010   /* It must be a non-type argument.  There permitted cases are given
9011      in [temp.arg.nontype]:
9012
9013      -- an integral constant-expression of integral or enumeration
9014         type; or
9015
9016      -- the name of a non-type template-parameter; or
9017
9018      -- the name of an object or function with external linkage...
9019
9020      -- the address of an object or function with external linkage...
9021
9022      -- a pointer to member...  */
9023   /* Look for a non-type template parameter.  */
9024   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9025     {
9026       cp_parser_parse_tentatively (parser);
9027       argument = cp_parser_primary_expression (parser,
9028                                                /*cast_p=*/false,
9029                                                &idk,
9030                                                &qualifying_class);
9031       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
9032           || !cp_parser_next_token_ends_template_argument_p (parser))
9033         cp_parser_simulate_error (parser);
9034       if (cp_parser_parse_definitely (parser))
9035         return argument;
9036     }
9037
9038   /* If the next token is "&", the argument must be the address of an
9039      object or function with external linkage.  */
9040   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
9041   if (address_p)
9042     cp_lexer_consume_token (parser->lexer);
9043   /* See if we might have an id-expression.  */
9044   token = cp_lexer_peek_token (parser->lexer);
9045   if (token->type == CPP_NAME
9046       || token->keyword == RID_OPERATOR
9047       || token->type == CPP_SCOPE
9048       || token->type == CPP_TEMPLATE_ID
9049       || token->type == CPP_NESTED_NAME_SPECIFIER)
9050     {
9051       cp_parser_parse_tentatively (parser);
9052       argument = cp_parser_primary_expression (parser,
9053                                                /*cast_p=*/false,
9054                                                &idk,
9055                                                &qualifying_class);
9056       if (cp_parser_error_occurred (parser)
9057           || !cp_parser_next_token_ends_template_argument_p (parser))
9058         cp_parser_abort_tentative_parse (parser);
9059       else
9060         {
9061           if (TREE_CODE (argument) == INDIRECT_REF)
9062             {
9063               gcc_assert (REFERENCE_REF_P (argument));
9064               argument = TREE_OPERAND (argument, 0);
9065             }
9066           
9067           if (qualifying_class)
9068             argument = finish_qualified_id_expr (qualifying_class,
9069                                                  argument,
9070                                                  /*done=*/true,
9071                                                  address_p);
9072           if (TREE_CODE (argument) == VAR_DECL)
9073             {
9074               /* A variable without external linkage might still be a
9075                  valid constant-expression, so no error is issued here
9076                  if the external-linkage check fails.  */
9077               if (!DECL_EXTERNAL_LINKAGE_P (argument))
9078                 cp_parser_simulate_error (parser);
9079             }
9080           else if (is_overloaded_fn (argument))
9081             /* All overloaded functions are allowed; if the external
9082                linkage test does not pass, an error will be issued
9083                later.  */
9084             ;
9085           else if (address_p
9086                    && (TREE_CODE (argument) == OFFSET_REF
9087                        || TREE_CODE (argument) == SCOPE_REF))
9088             /* A pointer-to-member.  */
9089             ;
9090           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
9091             ;
9092           else
9093             cp_parser_simulate_error (parser);
9094
9095           if (cp_parser_parse_definitely (parser))
9096             {
9097               if (address_p)
9098                 argument = build_x_unary_op (ADDR_EXPR, argument);
9099               return argument;
9100             }
9101         }
9102     }
9103   /* If the argument started with "&", there are no other valid
9104      alternatives at this point.  */
9105   if (address_p)
9106     {
9107       cp_parser_error (parser, "invalid non-type template argument");
9108       return error_mark_node;
9109     }
9110
9111   /* If the argument wasn't successfully parsed as a type-id followed
9112      by '>>', the argument can only be a constant expression now.
9113      Otherwise, we try parsing the constant-expression tentatively,
9114      because the argument could really be a type-id.  */
9115   if (maybe_type_id)
9116     cp_parser_parse_tentatively (parser);
9117   argument = cp_parser_constant_expression (parser,
9118                                             /*allow_non_constant_p=*/false,
9119                                             /*non_constant_p=*/NULL);
9120   argument = fold_non_dependent_expr (argument);
9121   if (!maybe_type_id)
9122     return argument;
9123   if (!cp_parser_next_token_ends_template_argument_p (parser))
9124     cp_parser_error (parser, "expected template-argument");
9125   if (cp_parser_parse_definitely (parser))
9126     return argument;
9127   /* We did our best to parse the argument as a non type-id, but that
9128      was the only alternative that matched (albeit with a '>' after
9129      it). We can assume it's just a typo from the user, and a
9130      diagnostic will then be issued.  */
9131   return cp_parser_type_id (parser);
9132 }
9133
9134 /* Parse an explicit-instantiation.
9135
9136    explicit-instantiation:
9137      template declaration
9138
9139    Although the standard says `declaration', what it really means is:
9140
9141    explicit-instantiation:
9142      template decl-specifier-seq [opt] declarator [opt] ;
9143
9144    Things like `template int S<int>::i = 5, int S<double>::j;' are not
9145    supposed to be allowed.  A defect report has been filed about this
9146    issue.
9147
9148    GNU Extension:
9149
9150    explicit-instantiation:
9151      storage-class-specifier template
9152        decl-specifier-seq [opt] declarator [opt] ;
9153      function-specifier template
9154        decl-specifier-seq [opt] declarator [opt] ;  */
9155
9156 static void
9157 cp_parser_explicit_instantiation (cp_parser* parser)
9158 {
9159   int declares_class_or_enum;
9160   cp_decl_specifier_seq decl_specifiers;
9161   tree extension_specifier = NULL_TREE;
9162
9163   /* Look for an (optional) storage-class-specifier or
9164      function-specifier.  */
9165   if (cp_parser_allow_gnu_extensions_p (parser))
9166     {
9167       extension_specifier
9168         = cp_parser_storage_class_specifier_opt (parser);
9169       if (!extension_specifier)
9170         extension_specifier
9171           = cp_parser_function_specifier_opt (parser,
9172                                               /*decl_specs=*/NULL);
9173     }
9174
9175   /* Look for the `template' keyword.  */
9176   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9177   /* Let the front end know that we are processing an explicit
9178      instantiation.  */
9179   begin_explicit_instantiation ();
9180   /* [temp.explicit] says that we are supposed to ignore access
9181      control while processing explicit instantiation directives.  */
9182   push_deferring_access_checks (dk_no_check);
9183   /* Parse a decl-specifier-seq.  */
9184   cp_parser_decl_specifier_seq (parser,
9185                                 CP_PARSER_FLAGS_OPTIONAL,
9186                                 &decl_specifiers,
9187                                 &declares_class_or_enum);
9188   /* If there was exactly one decl-specifier, and it declared a class,
9189      and there's no declarator, then we have an explicit type
9190      instantiation.  */
9191   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
9192     {
9193       tree type;
9194
9195       type = check_tag_decl (&decl_specifiers);
9196       /* Turn access control back on for names used during
9197          template instantiation.  */
9198       pop_deferring_access_checks ();
9199       if (type)
9200         do_type_instantiation (type, extension_specifier, /*complain=*/1);
9201     }
9202   else
9203     {
9204       cp_declarator *declarator;
9205       tree decl;
9206
9207       /* Parse the declarator.  */
9208       declarator
9209         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9210                                 /*ctor_dtor_or_conv_p=*/NULL,
9211                                 /*parenthesized_p=*/NULL,
9212                                 /*member_p=*/false);
9213       if (declares_class_or_enum & 2)
9214         cp_parser_check_for_definition_in_return_type (declarator,
9215                                                        decl_specifiers.type);
9216       if (declarator != cp_error_declarator)
9217         {
9218           decl = grokdeclarator (declarator, &decl_specifiers,
9219                                  NORMAL, 0, NULL);
9220           /* Turn access control back on for names used during
9221              template instantiation.  */
9222           pop_deferring_access_checks ();
9223           /* Do the explicit instantiation.  */
9224           do_decl_instantiation (decl, extension_specifier);
9225         }
9226       else
9227         {
9228           pop_deferring_access_checks ();
9229           /* Skip the body of the explicit instantiation.  */
9230           cp_parser_skip_to_end_of_statement (parser);
9231         }
9232     }
9233   /* We're done with the instantiation.  */
9234   end_explicit_instantiation ();
9235
9236   cp_parser_consume_semicolon_at_end_of_statement (parser);
9237 }
9238
9239 /* Parse an explicit-specialization.
9240
9241    explicit-specialization:
9242      template < > declaration
9243
9244    Although the standard says `declaration', what it really means is:
9245
9246    explicit-specialization:
9247      template <> decl-specifier [opt] init-declarator [opt] ;
9248      template <> function-definition
9249      template <> explicit-specialization
9250      template <> template-declaration  */
9251
9252 static void
9253 cp_parser_explicit_specialization (cp_parser* parser)
9254 {
9255   /* Look for the `template' keyword.  */
9256   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9257   /* Look for the `<'.  */
9258   cp_parser_require (parser, CPP_LESS, "`<'");
9259   /* Look for the `>'.  */
9260   cp_parser_require (parser, CPP_GREATER, "`>'");
9261   /* We have processed another parameter list.  */
9262   ++parser->num_template_parameter_lists;
9263   /* Let the front end know that we are beginning a specialization.  */
9264   begin_specialization ();
9265
9266   /* If the next keyword is `template', we need to figure out whether
9267      or not we're looking a template-declaration.  */
9268   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9269     {
9270       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9271           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9272         cp_parser_template_declaration_after_export (parser,
9273                                                      /*member_p=*/false);
9274       else
9275         cp_parser_explicit_specialization (parser);
9276     }
9277   else
9278     /* Parse the dependent declaration.  */
9279     cp_parser_single_declaration (parser,
9280                                   /*member_p=*/false,
9281                                   /*friend_p=*/NULL);
9282
9283   /* We're done with the specialization.  */
9284   end_specialization ();
9285   /* We're done with this parameter list.  */
9286   --parser->num_template_parameter_lists;
9287 }
9288
9289 /* Parse a type-specifier.
9290
9291    type-specifier:
9292      simple-type-specifier
9293      class-specifier
9294      enum-specifier
9295      elaborated-type-specifier
9296      cv-qualifier
9297
9298    GNU Extension:
9299
9300    type-specifier:
9301      __complex__
9302
9303    Returns a representation of the type-specifier.  For a
9304    class-specifier, enum-specifier, or elaborated-type-specifier, a
9305    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9306
9307    The parser flags FLAGS is used to control type-specifier parsing.
9308
9309    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9310    in a decl-specifier-seq.
9311
9312    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9313    class-specifier, enum-specifier, or elaborated-type-specifier, then
9314    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9315    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9316    zero.
9317
9318    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9319    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9320    is set to FALSE.  */
9321
9322 static tree
9323 cp_parser_type_specifier (cp_parser* parser,
9324                           cp_parser_flags flags,
9325                           cp_decl_specifier_seq *decl_specs,
9326                           bool is_declaration,
9327                           int* declares_class_or_enum,
9328                           bool* is_cv_qualifier)
9329 {
9330   tree type_spec = NULL_TREE;
9331   cp_token *token;
9332   enum rid keyword;
9333   cp_decl_spec ds = ds_last;
9334
9335   /* Assume this type-specifier does not declare a new type.  */
9336   if (declares_class_or_enum)
9337     *declares_class_or_enum = 0;
9338   /* And that it does not specify a cv-qualifier.  */
9339   if (is_cv_qualifier)
9340     *is_cv_qualifier = false;
9341   /* Peek at the next token.  */
9342   token = cp_lexer_peek_token (parser->lexer);
9343
9344   /* If we're looking at a keyword, we can use that to guide the
9345      production we choose.  */
9346   keyword = token->keyword;
9347   switch (keyword)
9348     {
9349     case RID_ENUM:
9350       /* 'enum' [identifier] '{' introduces an enum-specifier;
9351          'enum' <anything else> introduces an elaborated-type-specifier.  */
9352       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9353           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9354               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9355                  == CPP_OPEN_BRACE))
9356         {
9357           if (parser->num_template_parameter_lists)
9358             {
9359               error ("template declaration of %qs", "enum");
9360               cp_parser_skip_to_end_of_block_or_statement (parser);
9361               type_spec = error_mark_node;
9362             }
9363           else
9364             type_spec = cp_parser_enum_specifier (parser);
9365
9366           if (declares_class_or_enum)
9367             *declares_class_or_enum = 2;
9368           if (decl_specs)
9369             cp_parser_set_decl_spec_type (decl_specs,
9370                                           type_spec,
9371                                           /*user_defined_p=*/true);
9372           return type_spec;
9373         }
9374       else
9375         goto elaborated_type_specifier;
9376
9377       /* Any of these indicate either a class-specifier, or an
9378          elaborated-type-specifier.  */
9379     case RID_CLASS:
9380     case RID_STRUCT:
9381     case RID_UNION:
9382       /* Parse tentatively so that we can back up if we don't find a
9383          class-specifier.  */
9384       cp_parser_parse_tentatively (parser);
9385       /* Look for the class-specifier.  */
9386       type_spec = cp_parser_class_specifier (parser);
9387       /* If that worked, we're done.  */
9388       if (cp_parser_parse_definitely (parser))
9389         {
9390           if (declares_class_or_enum)
9391             *declares_class_or_enum = 2;
9392           if (decl_specs)
9393             cp_parser_set_decl_spec_type (decl_specs,
9394                                           type_spec,
9395                                           /*user_defined_p=*/true);
9396           return type_spec;
9397         }
9398
9399       /* Fall through.  */
9400     elaborated_type_specifier:
9401       /* We're declaring (not defining) a class or enum.  */
9402       if (declares_class_or_enum)
9403         *declares_class_or_enum = 1;
9404
9405       /* Fall through.  */
9406     case RID_TYPENAME:
9407       /* Look for an elaborated-type-specifier.  */
9408       type_spec
9409         = (cp_parser_elaborated_type_specifier
9410            (parser,
9411             decl_specs && decl_specs->specs[(int) ds_friend],
9412             is_declaration));
9413       if (decl_specs)
9414         cp_parser_set_decl_spec_type (decl_specs,
9415                                       type_spec,
9416                                       /*user_defined_p=*/true);
9417       return type_spec;
9418
9419     case RID_CONST:
9420       ds = ds_const;
9421       if (is_cv_qualifier)
9422         *is_cv_qualifier = true;
9423       break;
9424
9425     case RID_VOLATILE:
9426       ds = ds_volatile;
9427       if (is_cv_qualifier)
9428         *is_cv_qualifier = true;
9429       break;
9430
9431     case RID_RESTRICT:
9432       ds = ds_restrict;
9433       if (is_cv_qualifier)
9434         *is_cv_qualifier = true;
9435       break;
9436
9437     case RID_COMPLEX:
9438       /* The `__complex__' keyword is a GNU extension.  */
9439       ds = ds_complex;
9440       break;
9441
9442     default:
9443       break;
9444     }
9445
9446   /* Handle simple keywords.  */
9447   if (ds != ds_last)
9448     {
9449       if (decl_specs)
9450         {
9451           ++decl_specs->specs[(int)ds];
9452           decl_specs->any_specifiers_p = true;
9453         }
9454       return cp_lexer_consume_token (parser->lexer)->value;
9455     }
9456
9457   /* If we do not already have a type-specifier, assume we are looking
9458      at a simple-type-specifier.  */
9459   type_spec = cp_parser_simple_type_specifier (parser,
9460                                                decl_specs,
9461                                                flags);
9462
9463   /* If we didn't find a type-specifier, and a type-specifier was not
9464      optional in this context, issue an error message.  */
9465   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9466     {
9467       cp_parser_error (parser, "expected type specifier");
9468       return error_mark_node;
9469     }
9470
9471   return type_spec;
9472 }
9473
9474 /* Parse a simple-type-specifier.
9475
9476    simple-type-specifier:
9477      :: [opt] nested-name-specifier [opt] type-name
9478      :: [opt] nested-name-specifier template template-id
9479      char
9480      wchar_t
9481      bool
9482      short
9483      int
9484      long
9485      signed
9486      unsigned
9487      float
9488      double
9489      void
9490
9491    GNU Extension:
9492
9493    simple-type-specifier:
9494      __typeof__ unary-expression
9495      __typeof__ ( type-id )
9496
9497    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9498    appropriately updated.  */
9499
9500 static tree
9501 cp_parser_simple_type_specifier (cp_parser* parser,
9502                                  cp_decl_specifier_seq *decl_specs,
9503                                  cp_parser_flags flags)
9504 {
9505   tree type = NULL_TREE;
9506   cp_token *token;
9507
9508   /* Peek at the next token.  */
9509   token = cp_lexer_peek_token (parser->lexer);
9510
9511   /* If we're looking at a keyword, things are easy.  */
9512   switch (token->keyword)
9513     {
9514     case RID_CHAR:
9515       if (decl_specs)
9516         decl_specs->explicit_char_p = true;
9517       type = char_type_node;
9518       break;
9519     case RID_WCHAR:
9520       type = wchar_type_node;
9521       break;
9522     case RID_BOOL:
9523       type = boolean_type_node;
9524       break;
9525     case RID_SHORT:
9526       if (decl_specs)
9527         ++decl_specs->specs[(int) ds_short];
9528       type = short_integer_type_node;
9529       break;
9530     case RID_INT:
9531       if (decl_specs)
9532         decl_specs->explicit_int_p = true;
9533       type = integer_type_node;
9534       break;
9535     case RID_LONG:
9536       if (decl_specs)
9537         ++decl_specs->specs[(int) ds_long];
9538       type = long_integer_type_node;
9539       break;
9540     case RID_SIGNED:
9541       if (decl_specs)
9542         ++decl_specs->specs[(int) ds_signed];
9543       type = integer_type_node;
9544       break;
9545     case RID_UNSIGNED:
9546       if (decl_specs)
9547         ++decl_specs->specs[(int) ds_unsigned];
9548       type = unsigned_type_node;
9549       break;
9550     case RID_FLOAT:
9551       type = float_type_node;
9552       break;
9553     case RID_DOUBLE:
9554       type = double_type_node;
9555       break;
9556     case RID_VOID:
9557       type = void_type_node;
9558       break;
9559
9560     case RID_TYPEOF:
9561       /* Consume the `typeof' token.  */
9562       cp_lexer_consume_token (parser->lexer);
9563       /* Parse the operand to `typeof'.  */
9564       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9565       /* If it is not already a TYPE, take its type.  */
9566       if (!TYPE_P (type))
9567         type = finish_typeof (type);
9568
9569       if (decl_specs)
9570         cp_parser_set_decl_spec_type (decl_specs, type,
9571                                       /*user_defined_p=*/true);
9572
9573       return type;
9574
9575     default:
9576       break;
9577     }
9578
9579   /* If the type-specifier was for a built-in type, we're done.  */
9580   if (type)
9581     {
9582       tree id;
9583
9584       /* Record the type.  */
9585       if (decl_specs
9586           && (token->keyword != RID_SIGNED
9587               && token->keyword != RID_UNSIGNED
9588               && token->keyword != RID_SHORT
9589               && token->keyword != RID_LONG))
9590         cp_parser_set_decl_spec_type (decl_specs,
9591                                       type,
9592                                       /*user_defined=*/false);
9593       if (decl_specs)
9594         decl_specs->any_specifiers_p = true;
9595
9596       /* Consume the token.  */
9597       id = cp_lexer_consume_token (parser->lexer)->value;
9598
9599       /* There is no valid C++ program where a non-template type is
9600          followed by a "<".  That usually indicates that the user thought
9601          that the type was a template.  */
9602       cp_parser_check_for_invalid_template_id (parser, type);
9603
9604       return TYPE_NAME (type);
9605     }
9606
9607   /* The type-specifier must be a user-defined type.  */
9608   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9609     {
9610       bool qualified_p;
9611       bool global_p;
9612
9613       /* Don't gobble tokens or issue error messages if this is an
9614          optional type-specifier.  */
9615       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9616         cp_parser_parse_tentatively (parser);
9617
9618       /* Look for the optional `::' operator.  */
9619       global_p
9620         = (cp_parser_global_scope_opt (parser,
9621                                        /*current_scope_valid_p=*/false)
9622            != NULL_TREE);
9623       /* Look for the nested-name specifier.  */
9624       qualified_p
9625         = (cp_parser_nested_name_specifier_opt (parser,
9626                                                 /*typename_keyword_p=*/false,
9627                                                 /*check_dependency_p=*/true,
9628                                                 /*type_p=*/false,
9629                                                 /*is_declaration=*/false)
9630            != NULL_TREE);
9631       /* If we have seen a nested-name-specifier, and the next token
9632          is `template', then we are using the template-id production.  */
9633       if (parser->scope
9634           && cp_parser_optional_template_keyword (parser))
9635         {
9636           /* Look for the template-id.  */
9637           type = cp_parser_template_id (parser,
9638                                         /*template_keyword_p=*/true,
9639                                         /*check_dependency_p=*/true,
9640                                         /*is_declaration=*/false);
9641           /* If the template-id did not name a type, we are out of
9642              luck.  */
9643           if (TREE_CODE (type) != TYPE_DECL)
9644             {
9645               cp_parser_error (parser, "expected template-id for type");
9646               type = NULL_TREE;
9647             }
9648         }
9649       /* Otherwise, look for a type-name.  */
9650       else
9651         type = cp_parser_type_name (parser);
9652       /* Keep track of all name-lookups performed in class scopes.  */
9653       if (type
9654           && !global_p
9655           && !qualified_p
9656           && TREE_CODE (type) == TYPE_DECL
9657           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9658         maybe_note_name_used_in_class (DECL_NAME (type), type);
9659       /* If it didn't work out, we don't have a TYPE.  */
9660       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9661           && !cp_parser_parse_definitely (parser))
9662         type = NULL_TREE;
9663       if (type && decl_specs)
9664         cp_parser_set_decl_spec_type (decl_specs, type,
9665                                       /*user_defined=*/true);
9666     }
9667
9668   /* If we didn't get a type-name, issue an error message.  */
9669   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9670     {
9671       cp_parser_error (parser, "expected type-name");
9672       return error_mark_node;
9673     }
9674
9675   /* There is no valid C++ program where a non-template type is
9676      followed by a "<".  That usually indicates that the user thought
9677      that the type was a template.  */
9678   if (type && type != error_mark_node)
9679     {
9680       /* As a last-ditch effort, see if TYPE is an Objective-C type.
9681          If it is, then the '<'...'>' enclose protocol names rather than
9682          template arguments, and so everything is fine.  */
9683       if (c_dialect_objc ()
9684           && (objc_is_id (type) || objc_is_class_name (type)))
9685         {
9686           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9687           tree qual_type = objc_get_protocol_qualified_type (type, protos);
9688
9689           /* Clobber the "unqualified" type previously entered into
9690              DECL_SPECS with the new, improved protocol-qualified version.  */
9691           if (decl_specs)
9692             decl_specs->type = qual_type;
9693
9694           return qual_type;
9695         }
9696
9697       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9698     } 
9699
9700   return type;
9701 }
9702
9703 /* Parse a type-name.
9704
9705    type-name:
9706      class-name
9707      enum-name
9708      typedef-name
9709
9710    enum-name:
9711      identifier
9712
9713    typedef-name:
9714      identifier
9715
9716    Returns a TYPE_DECL for the type.  */
9717
9718 static tree
9719 cp_parser_type_name (cp_parser* parser)
9720 {
9721   tree type_decl;
9722   tree identifier;
9723
9724   /* We can't know yet whether it is a class-name or not.  */
9725   cp_parser_parse_tentatively (parser);
9726   /* Try a class-name.  */
9727   type_decl = cp_parser_class_name (parser,
9728                                     /*typename_keyword_p=*/false,
9729                                     /*template_keyword_p=*/false,
9730                                     none_type,
9731                                     /*check_dependency_p=*/true,
9732                                     /*class_head_p=*/false,
9733                                     /*is_declaration=*/false);
9734   /* If it's not a class-name, keep looking.  */
9735   if (!cp_parser_parse_definitely (parser))
9736     {
9737       /* It must be a typedef-name or an enum-name.  */
9738       identifier = cp_parser_identifier (parser);
9739       if (identifier == error_mark_node)
9740         return error_mark_node;
9741
9742       /* Look up the type-name.  */
9743       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9744
9745       if (TREE_CODE (type_decl) != TYPE_DECL
9746           && (objc_is_id (identifier) || objc_is_class_name (identifier)))
9747         {
9748           /* See if this is an Objective-C type.  */
9749           tree protos = cp_parser_objc_protocol_refs_opt (parser);
9750           tree type = objc_get_protocol_qualified_type (identifier, protos);
9751           if (type) 
9752             type_decl = TYPE_NAME (type);
9753         }
9754
9755       /* Issue an error if we did not find a type-name.  */
9756       if (TREE_CODE (type_decl) != TYPE_DECL)
9757         {
9758           if (!cp_parser_simulate_error (parser))
9759             cp_parser_name_lookup_error (parser, identifier, type_decl,
9760                                          "is not a type");
9761           type_decl = error_mark_node;
9762         }
9763       /* Remember that the name was used in the definition of the
9764          current class so that we can check later to see if the
9765          meaning would have been different after the class was
9766          entirely defined.  */
9767       else if (type_decl != error_mark_node
9768                && !parser->scope)
9769         maybe_note_name_used_in_class (identifier, type_decl);
9770     }
9771
9772   return type_decl;
9773 }
9774
9775
9776 /* Parse an elaborated-type-specifier.  Note that the grammar given
9777    here incorporates the resolution to DR68.
9778
9779    elaborated-type-specifier:
9780      class-key :: [opt] nested-name-specifier [opt] identifier
9781      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9782      enum :: [opt] nested-name-specifier [opt] identifier
9783      typename :: [opt] nested-name-specifier identifier
9784      typename :: [opt] nested-name-specifier template [opt]
9785        template-id
9786
9787    GNU extension:
9788
9789    elaborated-type-specifier:
9790      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9791      class-key attributes :: [opt] nested-name-specifier [opt]
9792                template [opt] template-id
9793      enum attributes :: [opt] nested-name-specifier [opt] identifier
9794
9795    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9796    declared `friend'.  If IS_DECLARATION is TRUE, then this
9797    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9798    something is being declared.
9799
9800    Returns the TYPE specified.  */
9801
9802 static tree
9803 cp_parser_elaborated_type_specifier (cp_parser* parser,
9804                                      bool is_friend,
9805                                      bool is_declaration)
9806 {
9807   enum tag_types tag_type;
9808   tree identifier;
9809   tree type = NULL_TREE;
9810   tree attributes = NULL_TREE;
9811
9812   /* See if we're looking at the `enum' keyword.  */
9813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9814     {
9815       /* Consume the `enum' token.  */
9816       cp_lexer_consume_token (parser->lexer);
9817       /* Remember that it's an enumeration type.  */
9818       tag_type = enum_type;
9819       /* Parse the attributes.  */
9820       attributes = cp_parser_attributes_opt (parser);
9821     }
9822   /* Or, it might be `typename'.  */
9823   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9824                                            RID_TYPENAME))
9825     {
9826       /* Consume the `typename' token.  */
9827       cp_lexer_consume_token (parser->lexer);
9828       /* Remember that it's a `typename' type.  */
9829       tag_type = typename_type;
9830       /* The `typename' keyword is only allowed in templates.  */
9831       if (!processing_template_decl)
9832         pedwarn ("using %<typename%> outside of template");
9833     }
9834   /* Otherwise it must be a class-key.  */
9835   else
9836     {
9837       tag_type = cp_parser_class_key (parser);
9838       if (tag_type == none_type)
9839         return error_mark_node;
9840       /* Parse the attributes.  */
9841       attributes = cp_parser_attributes_opt (parser);
9842     }
9843
9844   /* Look for the `::' operator.  */
9845   cp_parser_global_scope_opt (parser,
9846                               /*current_scope_valid_p=*/false);
9847   /* Look for the nested-name-specifier.  */
9848   if (tag_type == typename_type)
9849     {
9850       if (cp_parser_nested_name_specifier (parser,
9851                                            /*typename_keyword_p=*/true,
9852                                            /*check_dependency_p=*/true,
9853                                            /*type_p=*/true,
9854                                            is_declaration)
9855           == error_mark_node)
9856         return error_mark_node;
9857     }
9858   else
9859     /* Even though `typename' is not present, the proposed resolution
9860        to Core Issue 180 says that in `class A<T>::B', `B' should be
9861        considered a type-name, even if `A<T>' is dependent.  */
9862     cp_parser_nested_name_specifier_opt (parser,
9863                                          /*typename_keyword_p=*/true,
9864                                          /*check_dependency_p=*/true,
9865                                          /*type_p=*/true,
9866                                          is_declaration);
9867   /* For everything but enumeration types, consider a template-id.  */
9868   if (tag_type != enum_type)
9869     {
9870       bool template_p = false;
9871       tree decl;
9872
9873       /* Allow the `template' keyword.  */
9874       template_p = cp_parser_optional_template_keyword (parser);
9875       /* If we didn't see `template', we don't know if there's a
9876          template-id or not.  */
9877       if (!template_p)
9878         cp_parser_parse_tentatively (parser);
9879       /* Parse the template-id.  */
9880       decl = cp_parser_template_id (parser, template_p,
9881                                     /*check_dependency_p=*/true,
9882                                     is_declaration);
9883       /* If we didn't find a template-id, look for an ordinary
9884          identifier.  */
9885       if (!template_p && !cp_parser_parse_definitely (parser))
9886         ;
9887       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9888          in effect, then we must assume that, upon instantiation, the
9889          template will correspond to a class.  */
9890       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9891                && tag_type == typename_type)
9892         type = make_typename_type (parser->scope, decl,
9893                                    typename_type,
9894                                    /*complain=*/1);
9895       else
9896         type = TREE_TYPE (decl);
9897     }
9898
9899   /* For an enumeration type, consider only a plain identifier.  */
9900   if (!type)
9901     {
9902       identifier = cp_parser_identifier (parser);
9903
9904       if (identifier == error_mark_node)
9905         {
9906           parser->scope = NULL_TREE;
9907           return error_mark_node;
9908         }
9909
9910       /* For a `typename', we needn't call xref_tag.  */
9911       if (tag_type == typename_type 
9912           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
9913         return cp_parser_make_typename_type (parser, parser->scope,
9914                                              identifier);
9915       /* Look up a qualified name in the usual way.  */
9916       if (parser->scope)
9917         {
9918           tree decl;
9919
9920           decl = cp_parser_lookup_name (parser, identifier,
9921                                         tag_type,
9922                                         /*is_template=*/false,
9923                                         /*is_namespace=*/false,
9924                                         /*check_dependency=*/true,
9925                                         /*ambiguous_p=*/NULL);
9926
9927           /* If we are parsing friend declaration, DECL may be a
9928              TEMPLATE_DECL tree node here.  However, we need to check
9929              whether this TEMPLATE_DECL results in valid code.  Consider
9930              the following example:
9931
9932                namespace N {
9933                  template <class T> class C {};
9934                }
9935                class X {
9936                  template <class T> friend class N::C; // #1, valid code
9937                };
9938                template <class T> class Y {
9939                  friend class N::C;                    // #2, invalid code
9940                };
9941
9942              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9943              name lookup of `N::C'.  We see that friend declaration must
9944              be template for the code to be valid.  Note that
9945              processing_template_decl does not work here since it is
9946              always 1 for the above two cases.  */
9947
9948           decl = (cp_parser_maybe_treat_template_as_class
9949                   (decl, /*tag_name_p=*/is_friend
9950                          && parser->num_template_parameter_lists));
9951
9952           if (TREE_CODE (decl) != TYPE_DECL)
9953             {
9954               cp_parser_diagnose_invalid_type_name (parser, 
9955                                                     parser->scope,
9956                                                     identifier);
9957               return error_mark_node;
9958             }
9959
9960           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9961             check_elaborated_type_specifier
9962               (tag_type, decl,
9963                (parser->num_template_parameter_lists
9964                 || DECL_SELF_REFERENCE_P (decl)));
9965
9966           type = TREE_TYPE (decl);
9967         }
9968       else
9969         {
9970           /* An elaborated-type-specifier sometimes introduces a new type and
9971              sometimes names an existing type.  Normally, the rule is that it
9972              introduces a new type only if there is not an existing type of
9973              the same name already in scope.  For example, given:
9974
9975                struct S {};
9976                void f() { struct S s; }
9977
9978              the `struct S' in the body of `f' is the same `struct S' as in
9979              the global scope; the existing definition is used.  However, if
9980              there were no global declaration, this would introduce a new
9981              local class named `S'.
9982
9983              An exception to this rule applies to the following code:
9984
9985                namespace N { struct S; }
9986
9987              Here, the elaborated-type-specifier names a new type
9988              unconditionally; even if there is already an `S' in the
9989              containing scope this declaration names a new type.
9990              This exception only applies if the elaborated-type-specifier
9991              forms the complete declaration:
9992
9993                [class.name]
9994
9995                A declaration consisting solely of `class-key identifier ;' is
9996                either a redeclaration of the name in the current scope or a
9997                forward declaration of the identifier as a class name.  It
9998                introduces the name into the current scope.
9999
10000              We are in this situation precisely when the next token is a `;'.
10001
10002              An exception to the exception is that a `friend' declaration does
10003              *not* name a new type; i.e., given:
10004
10005                struct S { friend struct T; };
10006
10007              `T' is not a new type in the scope of `S'.
10008
10009              Also, `new struct S' or `sizeof (struct S)' never results in the
10010              definition of a new type; a new type can only be declared in a
10011              declaration context.  */
10012
10013           tag_scope ts;
10014           if (is_friend)
10015             /* Friends have special name lookup rules.  */
10016             ts = ts_within_enclosing_non_class;
10017           else if (is_declaration
10018                    && cp_lexer_next_token_is (parser->lexer,
10019                                               CPP_SEMICOLON))
10020             /* This is a `class-key identifier ;' */
10021             ts = ts_current;
10022           else
10023             ts = ts_global;
10024
10025           /* Warn about attributes. They are ignored.  */
10026           if (attributes)
10027             warning (0, "type attributes are honored only at type definition");
10028
10029           type = xref_tag (tag_type, identifier, ts,
10030                            parser->num_template_parameter_lists);
10031         }
10032     }
10033   if (tag_type != enum_type)
10034     cp_parser_check_class_key (tag_type, type);
10035
10036   /* A "<" cannot follow an elaborated type specifier.  If that
10037      happens, the user was probably trying to form a template-id.  */
10038   cp_parser_check_for_invalid_template_id (parser, type);
10039
10040   return type;
10041 }
10042
10043 /* Parse an enum-specifier.
10044
10045    enum-specifier:
10046      enum identifier [opt] { enumerator-list [opt] }
10047
10048    GNU Extensions:
10049      enum identifier [opt] { enumerator-list [opt] } attributes
10050
10051    Returns an ENUM_TYPE representing the enumeration.  */
10052
10053 static tree
10054 cp_parser_enum_specifier (cp_parser* parser)
10055 {
10056   tree identifier;
10057   tree type;
10058
10059   /* Caller guarantees that the current token is 'enum', an identifier
10060      possibly follows, and the token after that is an opening brace.
10061      If we don't have an identifier, fabricate an anonymous name for
10062      the enumeration being defined.  */
10063   cp_lexer_consume_token (parser->lexer);
10064
10065   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10066     identifier = cp_parser_identifier (parser);
10067   else
10068     identifier = make_anon_name ();
10069
10070   /* Issue an error message if type-definitions are forbidden here.  */
10071   cp_parser_check_type_definition (parser);
10072
10073   /* Create the new type.  We do this before consuming the opening brace
10074      so the enum will be recorded as being on the line of its tag (or the
10075      'enum' keyword, if there is no tag).  */
10076   type = start_enum (identifier);
10077
10078   /* Consume the opening brace.  */
10079   cp_lexer_consume_token (parser->lexer);
10080
10081   /* If the next token is not '}', then there are some enumerators.  */
10082   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
10083     cp_parser_enumerator_list (parser, type);
10084
10085   /* Consume the final '}'.  */
10086   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10087
10088   /* Look for trailing attributes to apply to this enumeration, and
10089      apply them if appropriate.  */
10090   if (cp_parser_allow_gnu_extensions_p (parser))
10091     {
10092       tree trailing_attr = cp_parser_attributes_opt (parser);
10093       cplus_decl_attributes (&type,
10094                              trailing_attr,
10095                              (int) ATTR_FLAG_TYPE_IN_PLACE);
10096     }
10097
10098   /* Finish up the enumeration.  */
10099   finish_enum (type);
10100
10101   return type;
10102 }
10103
10104 /* Parse an enumerator-list.  The enumerators all have the indicated
10105    TYPE.
10106
10107    enumerator-list:
10108      enumerator-definition
10109      enumerator-list , enumerator-definition  */
10110
10111 static void
10112 cp_parser_enumerator_list (cp_parser* parser, tree type)
10113 {
10114   while (true)
10115     {
10116       /* Parse an enumerator-definition.  */
10117       cp_parser_enumerator_definition (parser, type);
10118
10119       /* If the next token is not a ',', we've reached the end of
10120          the list.  */
10121       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10122         break;
10123       /* Otherwise, consume the `,' and keep going.  */
10124       cp_lexer_consume_token (parser->lexer);
10125       /* If the next token is a `}', there is a trailing comma.  */
10126       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
10127         {
10128           if (pedantic && !in_system_header)
10129             pedwarn ("comma at end of enumerator list");
10130           break;
10131         }
10132     }
10133 }
10134
10135 /* Parse an enumerator-definition.  The enumerator has the indicated
10136    TYPE.
10137
10138    enumerator-definition:
10139      enumerator
10140      enumerator = constant-expression
10141
10142    enumerator:
10143      identifier  */
10144
10145 static void
10146 cp_parser_enumerator_definition (cp_parser* parser, tree type)
10147 {
10148   tree identifier;
10149   tree value;
10150
10151   /* Look for the identifier.  */
10152   identifier = cp_parser_identifier (parser);
10153   if (identifier == error_mark_node)
10154     return;
10155
10156   /* If the next token is an '=', then there is an explicit value.  */
10157   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10158     {
10159       /* Consume the `=' token.  */
10160       cp_lexer_consume_token (parser->lexer);
10161       /* Parse the value.  */
10162       value = cp_parser_constant_expression (parser,
10163                                              /*allow_non_constant_p=*/false,
10164                                              NULL);
10165     }
10166   else
10167     value = NULL_TREE;
10168
10169   /* Create the enumerator.  */
10170   build_enumerator (identifier, value, type);
10171 }
10172
10173 /* Parse a namespace-name.
10174
10175    namespace-name:
10176      original-namespace-name
10177      namespace-alias
10178
10179    Returns the NAMESPACE_DECL for the namespace.  */
10180
10181 static tree
10182 cp_parser_namespace_name (cp_parser* parser)
10183 {
10184   tree identifier;
10185   tree namespace_decl;
10186
10187   /* Get the name of the namespace.  */
10188   identifier = cp_parser_identifier (parser);
10189   if (identifier == error_mark_node)
10190     return error_mark_node;
10191
10192   /* Look up the identifier in the currently active scope.  Look only
10193      for namespaces, due to:
10194
10195        [basic.lookup.udir]
10196
10197        When looking up a namespace-name in a using-directive or alias
10198        definition, only namespace names are considered.
10199
10200      And:
10201
10202        [basic.lookup.qual]
10203
10204        During the lookup of a name preceding the :: scope resolution
10205        operator, object, function, and enumerator names are ignored.
10206
10207      (Note that cp_parser_class_or_namespace_name only calls this
10208      function if the token after the name is the scope resolution
10209      operator.)  */
10210   namespace_decl = cp_parser_lookup_name (parser, identifier,
10211                                           none_type,
10212                                           /*is_template=*/false,
10213                                           /*is_namespace=*/true,
10214                                           /*check_dependency=*/true,
10215                                           /*ambiguous_p=*/NULL);
10216   /* If it's not a namespace, issue an error.  */
10217   if (namespace_decl == error_mark_node
10218       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
10219     {
10220       cp_parser_error (parser, "expected namespace-name");
10221       namespace_decl = error_mark_node;
10222     }
10223
10224   return namespace_decl;
10225 }
10226
10227 /* Parse a namespace-definition.
10228
10229    namespace-definition:
10230      named-namespace-definition
10231      unnamed-namespace-definition
10232
10233    named-namespace-definition:
10234      original-namespace-definition
10235      extension-namespace-definition
10236
10237    original-namespace-definition:
10238      namespace identifier { namespace-body }
10239
10240    extension-namespace-definition:
10241      namespace original-namespace-name { namespace-body }
10242
10243    unnamed-namespace-definition:
10244      namespace { namespace-body } */
10245
10246 static void
10247 cp_parser_namespace_definition (cp_parser* parser)
10248 {
10249   tree identifier;
10250
10251   /* Look for the `namespace' keyword.  */
10252   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10253
10254   /* Get the name of the namespace.  We do not attempt to distinguish
10255      between an original-namespace-definition and an
10256      extension-namespace-definition at this point.  The semantic
10257      analysis routines are responsible for that.  */
10258   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10259     identifier = cp_parser_identifier (parser);
10260   else
10261     identifier = NULL_TREE;
10262
10263   /* Look for the `{' to start the namespace.  */
10264   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
10265   /* Start the namespace.  */
10266   push_namespace (identifier);
10267   /* Parse the body of the namespace.  */
10268   cp_parser_namespace_body (parser);
10269   /* Finish the namespace.  */
10270   pop_namespace ();
10271   /* Look for the final `}'.  */
10272   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
10273 }
10274
10275 /* Parse a namespace-body.
10276
10277    namespace-body:
10278      declaration-seq [opt]  */
10279
10280 static void
10281 cp_parser_namespace_body (cp_parser* parser)
10282 {
10283   cp_parser_declaration_seq_opt (parser);
10284 }
10285
10286 /* Parse a namespace-alias-definition.
10287
10288    namespace-alias-definition:
10289      namespace identifier = qualified-namespace-specifier ;  */
10290
10291 static void
10292 cp_parser_namespace_alias_definition (cp_parser* parser)
10293 {
10294   tree identifier;
10295   tree namespace_specifier;
10296
10297   /* Look for the `namespace' keyword.  */
10298   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10299   /* Look for the identifier.  */
10300   identifier = cp_parser_identifier (parser);
10301   if (identifier == error_mark_node)
10302     return;
10303   /* Look for the `=' token.  */
10304   cp_parser_require (parser, CPP_EQ, "`='");
10305   /* Look for the qualified-namespace-specifier.  */
10306   namespace_specifier
10307     = cp_parser_qualified_namespace_specifier (parser);
10308   /* Look for the `;' token.  */
10309   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10310
10311   /* Register the alias in the symbol table.  */
10312   do_namespace_alias (identifier, namespace_specifier);
10313 }
10314
10315 /* Parse a qualified-namespace-specifier.
10316
10317    qualified-namespace-specifier:
10318      :: [opt] nested-name-specifier [opt] namespace-name
10319
10320    Returns a NAMESPACE_DECL corresponding to the specified
10321    namespace.  */
10322
10323 static tree
10324 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10325 {
10326   /* Look for the optional `::'.  */
10327   cp_parser_global_scope_opt (parser,
10328                               /*current_scope_valid_p=*/false);
10329
10330   /* Look for the optional nested-name-specifier.  */
10331   cp_parser_nested_name_specifier_opt (parser,
10332                                        /*typename_keyword_p=*/false,
10333                                        /*check_dependency_p=*/true,
10334                                        /*type_p=*/false,
10335                                        /*is_declaration=*/true);
10336
10337   return cp_parser_namespace_name (parser);
10338 }
10339
10340 /* Parse a using-declaration.
10341
10342    using-declaration:
10343      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10344      using :: unqualified-id ;  */
10345
10346 static void
10347 cp_parser_using_declaration (cp_parser* parser)
10348 {
10349   cp_token *token;
10350   bool typename_p = false;
10351   bool global_scope_p;
10352   tree decl;
10353   tree identifier;
10354   tree qscope;
10355
10356   /* Look for the `using' keyword.  */
10357   cp_parser_require_keyword (parser, RID_USING, "`using'");
10358
10359   /* Peek at the next token.  */
10360   token = cp_lexer_peek_token (parser->lexer);
10361   /* See if it's `typename'.  */
10362   if (token->keyword == RID_TYPENAME)
10363     {
10364       /* Remember that we've seen it.  */
10365       typename_p = true;
10366       /* Consume the `typename' token.  */
10367       cp_lexer_consume_token (parser->lexer);
10368     }
10369
10370   /* Look for the optional global scope qualification.  */
10371   global_scope_p
10372     = (cp_parser_global_scope_opt (parser,
10373                                    /*current_scope_valid_p=*/false)
10374        != NULL_TREE);
10375
10376   /* If we saw `typename', or didn't see `::', then there must be a
10377      nested-name-specifier present.  */
10378   if (typename_p || !global_scope_p)
10379     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10380                                               /*check_dependency_p=*/true,
10381                                               /*type_p=*/false,
10382                                               /*is_declaration=*/true);
10383   /* Otherwise, we could be in either of the two productions.  In that
10384      case, treat the nested-name-specifier as optional.  */
10385   else
10386     qscope = cp_parser_nested_name_specifier_opt (parser,
10387                                                   /*typename_keyword_p=*/false,
10388                                                   /*check_dependency_p=*/true,
10389                                                   /*type_p=*/false,
10390                                                   /*is_declaration=*/true);
10391   if (!qscope)
10392     qscope = global_namespace;
10393
10394   /* Parse the unqualified-id.  */
10395   identifier = cp_parser_unqualified_id (parser,
10396                                          /*template_keyword_p=*/false,
10397                                          /*check_dependency_p=*/true,
10398                                          /*declarator_p=*/true);
10399
10400   /* The function we call to handle a using-declaration is different
10401      depending on what scope we are in.  */
10402   if (identifier == error_mark_node)
10403     ;
10404   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10405            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10406     /* [namespace.udecl]
10407
10408        A using declaration shall not name a template-id.  */
10409     error ("a template-id may not appear in a using-declaration");
10410   else
10411     {
10412       if (at_class_scope_p ())
10413         {
10414           /* Create the USING_DECL.  */
10415           decl = do_class_using_decl (parser->scope, identifier);
10416           /* Add it to the list of members in this class.  */
10417           finish_member_declaration (decl);
10418         }
10419       else
10420         {
10421           decl = cp_parser_lookup_name_simple (parser, identifier);
10422           if (decl == error_mark_node)
10423             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10424           else if (!at_namespace_scope_p ())
10425             do_local_using_decl (decl, qscope, identifier);
10426           else
10427             do_toplevel_using_decl (decl, qscope, identifier);
10428         }
10429     }
10430
10431   /* Look for the final `;'.  */
10432   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10433 }
10434
10435 /* Parse a using-directive.
10436
10437    using-directive:
10438      using namespace :: [opt] nested-name-specifier [opt]
10439        namespace-name ;  */
10440
10441 static void
10442 cp_parser_using_directive (cp_parser* parser)
10443 {
10444   tree namespace_decl;
10445   tree attribs;
10446
10447   /* Look for the `using' keyword.  */
10448   cp_parser_require_keyword (parser, RID_USING, "`using'");
10449   /* And the `namespace' keyword.  */
10450   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10451   /* Look for the optional `::' operator.  */
10452   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10453   /* And the optional nested-name-specifier.  */
10454   cp_parser_nested_name_specifier_opt (parser,
10455                                        /*typename_keyword_p=*/false,
10456                                        /*check_dependency_p=*/true,
10457                                        /*type_p=*/false,
10458                                        /*is_declaration=*/true);
10459   /* Get the namespace being used.  */
10460   namespace_decl = cp_parser_namespace_name (parser);
10461   /* And any specified attributes.  */
10462   attribs = cp_parser_attributes_opt (parser);
10463   /* Update the symbol table.  */
10464   parse_using_directive (namespace_decl, attribs);
10465   /* Look for the final `;'.  */
10466   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10467 }
10468
10469 /* Parse an asm-definition.
10470
10471    asm-definition:
10472      asm ( string-literal ) ;
10473
10474    GNU Extension:
10475
10476    asm-definition:
10477      asm volatile [opt] ( string-literal ) ;
10478      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10479      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10480                           : asm-operand-list [opt] ) ;
10481      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10482                           : asm-operand-list [opt]
10483                           : asm-operand-list [opt] ) ;  */
10484
10485 static void
10486 cp_parser_asm_definition (cp_parser* parser)
10487 {
10488   tree string;
10489   tree outputs = NULL_TREE;
10490   tree inputs = NULL_TREE;
10491   tree clobbers = NULL_TREE;
10492   tree asm_stmt;
10493   bool volatile_p = false;
10494   bool extended_p = false;
10495
10496   /* Look for the `asm' keyword.  */
10497   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10498   /* See if the next token is `volatile'.  */
10499   if (cp_parser_allow_gnu_extensions_p (parser)
10500       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10501     {
10502       /* Remember that we saw the `volatile' keyword.  */
10503       volatile_p = true;
10504       /* Consume the token.  */
10505       cp_lexer_consume_token (parser->lexer);
10506     }
10507   /* Look for the opening `('.  */
10508   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10509     return;
10510   /* Look for the string.  */
10511   string = cp_parser_string_literal (parser, false, false);
10512   if (string == error_mark_node)
10513     {
10514       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10515                                              /*consume_paren=*/true);
10516       return;
10517     }
10518
10519   /* If we're allowing GNU extensions, check for the extended assembly
10520      syntax.  Unfortunately, the `:' tokens need not be separated by
10521      a space in C, and so, for compatibility, we tolerate that here
10522      too.  Doing that means that we have to treat the `::' operator as
10523      two `:' tokens.  */
10524   if (cp_parser_allow_gnu_extensions_p (parser)
10525       && at_function_scope_p ()
10526       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10527           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10528     {
10529       bool inputs_p = false;
10530       bool clobbers_p = false;
10531
10532       /* The extended syntax was used.  */
10533       extended_p = true;
10534
10535       /* Look for outputs.  */
10536       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10537         {
10538           /* Consume the `:'.  */
10539           cp_lexer_consume_token (parser->lexer);
10540           /* Parse the output-operands.  */
10541           if (cp_lexer_next_token_is_not (parser->lexer,
10542                                           CPP_COLON)
10543               && cp_lexer_next_token_is_not (parser->lexer,
10544                                              CPP_SCOPE)
10545               && cp_lexer_next_token_is_not (parser->lexer,
10546                                              CPP_CLOSE_PAREN))
10547             outputs = cp_parser_asm_operand_list (parser);
10548         }
10549       /* If the next token is `::', there are no outputs, and the
10550          next token is the beginning of the inputs.  */
10551       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10552         /* The inputs are coming next.  */
10553         inputs_p = true;
10554
10555       /* Look for inputs.  */
10556       if (inputs_p
10557           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10558         {
10559           /* Consume the `:' or `::'.  */
10560           cp_lexer_consume_token (parser->lexer);
10561           /* Parse the output-operands.  */
10562           if (cp_lexer_next_token_is_not (parser->lexer,
10563                                           CPP_COLON)
10564               && cp_lexer_next_token_is_not (parser->lexer,
10565                                              CPP_CLOSE_PAREN))
10566             inputs = cp_parser_asm_operand_list (parser);
10567         }
10568       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10569         /* The clobbers are coming next.  */
10570         clobbers_p = true;
10571
10572       /* Look for clobbers.  */
10573       if (clobbers_p
10574           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10575         {
10576           /* Consume the `:' or `::'.  */
10577           cp_lexer_consume_token (parser->lexer);
10578           /* Parse the clobbers.  */
10579           if (cp_lexer_next_token_is_not (parser->lexer,
10580                                           CPP_CLOSE_PAREN))
10581             clobbers = cp_parser_asm_clobber_list (parser);
10582         }
10583     }
10584   /* Look for the closing `)'.  */
10585   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10586     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10587                                            /*consume_paren=*/true);
10588   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10589
10590   /* Create the ASM_EXPR.  */
10591   if (at_function_scope_p ())
10592     {
10593       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10594                                   inputs, clobbers);
10595       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10596       if (!extended_p)
10597         {
10598           tree temp = asm_stmt;
10599           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10600             temp = TREE_OPERAND (temp, 0);
10601           
10602           ASM_INPUT_P (temp) = 1;
10603         }
10604     }
10605   else
10606     assemble_asm (string);
10607 }
10608
10609 /* Declarators [gram.dcl.decl] */
10610
10611 /* Parse an init-declarator.
10612
10613    init-declarator:
10614      declarator initializer [opt]
10615
10616    GNU Extension:
10617
10618    init-declarator:
10619      declarator asm-specification [opt] attributes [opt] initializer [opt]
10620
10621    function-definition:
10622      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10623        function-body
10624      decl-specifier-seq [opt] declarator function-try-block
10625
10626    GNU Extension:
10627
10628    function-definition:
10629      __extension__ function-definition
10630
10631    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10632    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10633    then this declarator appears in a class scope.  The new DECL created
10634    by this declarator is returned.
10635
10636    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10637    for a function-definition here as well.  If the declarator is a
10638    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10639    be TRUE upon return.  By that point, the function-definition will
10640    have been completely parsed.
10641
10642    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10643    is FALSE.  */
10644
10645 static tree
10646 cp_parser_init_declarator (cp_parser* parser,
10647                            cp_decl_specifier_seq *decl_specifiers,
10648                            bool function_definition_allowed_p,
10649                            bool member_p,
10650                            int declares_class_or_enum,
10651                            bool* function_definition_p)
10652 {
10653   cp_token *token;
10654   cp_declarator *declarator;
10655   tree prefix_attributes;
10656   tree attributes;
10657   tree asm_specification;
10658   tree initializer;
10659   tree decl = NULL_TREE;
10660   tree scope;
10661   bool is_initialized;
10662   bool is_parenthesized_init;
10663   bool is_non_constant_init;
10664   int ctor_dtor_or_conv_p;
10665   bool friend_p;
10666   tree pushed_scope = NULL;
10667
10668   /* Gather the attributes that were provided with the
10669      decl-specifiers.  */
10670   prefix_attributes = decl_specifiers->attributes;
10671
10672   /* Assume that this is not the declarator for a function
10673      definition.  */
10674   if (function_definition_p)
10675     *function_definition_p = false;
10676
10677   /* Defer access checks while parsing the declarator; we cannot know
10678      what names are accessible until we know what is being
10679      declared.  */
10680   resume_deferring_access_checks ();
10681
10682   /* Parse the declarator.  */
10683   declarator
10684     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10685                             &ctor_dtor_or_conv_p,
10686                             /*parenthesized_p=*/NULL,
10687                             /*member_p=*/false);
10688   /* Gather up the deferred checks.  */
10689   stop_deferring_access_checks ();
10690
10691   /* If the DECLARATOR was erroneous, there's no need to go
10692      further.  */
10693   if (declarator == cp_error_declarator)
10694     return error_mark_node;
10695
10696   if (declares_class_or_enum & 2)
10697     cp_parser_check_for_definition_in_return_type (declarator,
10698                                                    decl_specifiers->type);
10699
10700   /* Figure out what scope the entity declared by the DECLARATOR is
10701      located in.  `grokdeclarator' sometimes changes the scope, so
10702      we compute it now.  */
10703   scope = get_scope_of_declarator (declarator);
10704
10705   /* If we're allowing GNU extensions, look for an asm-specification
10706      and attributes.  */
10707   if (cp_parser_allow_gnu_extensions_p (parser))
10708     {
10709       /* Look for an asm-specification.  */
10710       asm_specification = cp_parser_asm_specification_opt (parser);
10711       /* And attributes.  */
10712       attributes = cp_parser_attributes_opt (parser);
10713     }
10714   else
10715     {
10716       asm_specification = NULL_TREE;
10717       attributes = NULL_TREE;
10718     }
10719
10720   /* Peek at the next token.  */
10721   token = cp_lexer_peek_token (parser->lexer);
10722   /* Check to see if the token indicates the start of a
10723      function-definition.  */
10724   if (cp_parser_token_starts_function_definition_p (token))
10725     {
10726       if (!function_definition_allowed_p)
10727         {
10728           /* If a function-definition should not appear here, issue an
10729              error message.  */
10730           cp_parser_error (parser,
10731                            "a function-definition is not allowed here");
10732           return error_mark_node;
10733         }
10734       else
10735         {
10736           /* Neither attributes nor an asm-specification are allowed
10737              on a function-definition.  */
10738           if (asm_specification)
10739             error ("an asm-specification is not allowed on a function-definition");
10740           if (attributes)
10741             error ("attributes are not allowed on a function-definition");
10742           /* This is a function-definition.  */
10743           *function_definition_p = true;
10744
10745           /* Parse the function definition.  */
10746           if (member_p)
10747             decl = cp_parser_save_member_function_body (parser,
10748                                                         decl_specifiers,
10749                                                         declarator,
10750                                                         prefix_attributes);
10751           else
10752             decl
10753               = (cp_parser_function_definition_from_specifiers_and_declarator
10754                  (parser, decl_specifiers, prefix_attributes, declarator));
10755
10756           return decl;
10757         }
10758     }
10759
10760   /* [dcl.dcl]
10761
10762      Only in function declarations for constructors, destructors, and
10763      type conversions can the decl-specifier-seq be omitted.
10764
10765      We explicitly postpone this check past the point where we handle
10766      function-definitions because we tolerate function-definitions
10767      that are missing their return types in some modes.  */
10768   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10769     {
10770       cp_parser_error (parser,
10771                        "expected constructor, destructor, or type conversion");
10772       return error_mark_node;
10773     }
10774
10775   /* An `=' or an `(' indicates an initializer.  */
10776   is_initialized = (token->type == CPP_EQ
10777                      || token->type == CPP_OPEN_PAREN);
10778   /* If the init-declarator isn't initialized and isn't followed by a
10779      `,' or `;', it's not a valid init-declarator.  */
10780   if (!is_initialized
10781       && token->type != CPP_COMMA
10782       && token->type != CPP_SEMICOLON)
10783     {
10784       cp_parser_error (parser, "expected initializer");
10785       return error_mark_node;
10786     }
10787
10788   /* Because start_decl has side-effects, we should only call it if we
10789      know we're going ahead.  By this point, we know that we cannot
10790      possibly be looking at any other construct.  */
10791   cp_parser_commit_to_tentative_parse (parser);
10792
10793   /* If the decl specifiers were bad, issue an error now that we're
10794      sure this was intended to be a declarator.  Then continue
10795      declaring the variable(s), as int, to try to cut down on further
10796      errors.  */
10797   if (decl_specifiers->any_specifiers_p
10798       && decl_specifiers->type == error_mark_node)
10799     {
10800       cp_parser_error (parser, "invalid type in declaration");
10801       decl_specifiers->type = integer_type_node;
10802     }
10803
10804   /* Check to see whether or not this declaration is a friend.  */
10805   friend_p = cp_parser_friend_p (decl_specifiers);
10806
10807   /* Check that the number of template-parameter-lists is OK.  */
10808   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10809     return error_mark_node;
10810
10811   /* Enter the newly declared entry in the symbol table.  If we're
10812      processing a declaration in a class-specifier, we wait until
10813      after processing the initializer.  */
10814   if (!member_p)
10815     {
10816       if (parser->in_unbraced_linkage_specification_p)
10817         {
10818           decl_specifiers->storage_class = sc_extern;
10819           have_extern_spec = false;
10820         }
10821       decl = start_decl (declarator, decl_specifiers,
10822                          is_initialized, attributes, prefix_attributes,
10823                          &pushed_scope);
10824     }
10825   else if (scope)
10826     /* Enter the SCOPE.  That way unqualified names appearing in the
10827        initializer will be looked up in SCOPE.  */
10828     pushed_scope = push_scope (scope);
10829
10830   /* Perform deferred access control checks, now that we know in which
10831      SCOPE the declared entity resides.  */
10832   if (!member_p && decl)
10833     {
10834       tree saved_current_function_decl = NULL_TREE;
10835
10836       /* If the entity being declared is a function, pretend that we
10837          are in its scope.  If it is a `friend', it may have access to
10838          things that would not otherwise be accessible.  */
10839       if (TREE_CODE (decl) == FUNCTION_DECL)
10840         {
10841           saved_current_function_decl = current_function_decl;
10842           current_function_decl = decl;
10843         }
10844
10845       /* Perform the access control checks for the declarator and the
10846          the decl-specifiers.  */
10847       perform_deferred_access_checks ();
10848
10849       /* Restore the saved value.  */
10850       if (TREE_CODE (decl) == FUNCTION_DECL)
10851         current_function_decl = saved_current_function_decl;
10852     }
10853
10854   /* Parse the initializer.  */
10855   if (is_initialized)
10856     initializer = cp_parser_initializer (parser,
10857                                          &is_parenthesized_init,
10858                                          &is_non_constant_init);
10859   else
10860     {
10861       initializer = NULL_TREE;
10862       is_parenthesized_init = false;
10863       is_non_constant_init = true;
10864     }
10865
10866   /* The old parser allows attributes to appear after a parenthesized
10867      initializer.  Mark Mitchell proposed removing this functionality
10868      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10869      attributes -- but ignores them.  */
10870   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10871     if (cp_parser_attributes_opt (parser))
10872       warning (0, "attributes after parenthesized initializer ignored");
10873
10874   /* For an in-class declaration, use `grokfield' to create the
10875      declaration.  */
10876   if (member_p)
10877     {
10878       if (pushed_scope)
10879         {
10880           pop_scope (pushed_scope);
10881           pushed_scope = false;
10882         }
10883       decl = grokfield (declarator, decl_specifiers,
10884                         initializer, /*asmspec=*/NULL_TREE,
10885                         /*attributes=*/NULL_TREE);
10886       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10887         cp_parser_save_default_args (parser, decl);
10888     }
10889
10890   /* Finish processing the declaration.  But, skip friend
10891      declarations.  */
10892   if (!friend_p && decl && decl != error_mark_node)
10893     {
10894       cp_finish_decl (decl,
10895                       initializer,
10896                       asm_specification,
10897                       /* If the initializer is in parentheses, then this is
10898                          a direct-initialization, which means that an
10899                          `explicit' constructor is OK.  Otherwise, an
10900                          `explicit' constructor cannot be used.  */
10901                       ((is_parenthesized_init || !is_initialized)
10902                      ? 0 : LOOKUP_ONLYCONVERTING));
10903     }
10904   if (!friend_p && pushed_scope)
10905     pop_scope (pushed_scope);
10906
10907   /* Remember whether or not variables were initialized by
10908      constant-expressions.  */
10909   if (decl && TREE_CODE (decl) == VAR_DECL
10910       && is_initialized && !is_non_constant_init)
10911     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10912
10913   return decl;
10914 }
10915
10916 /* Parse a declarator.
10917
10918    declarator:
10919      direct-declarator
10920      ptr-operator declarator
10921
10922    abstract-declarator:
10923      ptr-operator abstract-declarator [opt]
10924      direct-abstract-declarator
10925
10926    GNU Extensions:
10927
10928    declarator:
10929      attributes [opt] direct-declarator
10930      attributes [opt] ptr-operator declarator
10931
10932    abstract-declarator:
10933      attributes [opt] ptr-operator abstract-declarator [opt]
10934      attributes [opt] direct-abstract-declarator
10935
10936    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10937    detect constructor, destructor or conversion operators. It is set
10938    to -1 if the declarator is a name, and +1 if it is a
10939    function. Otherwise it is set to zero. Usually you just want to
10940    test for >0, but internally the negative value is used.
10941
10942    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10943    a decl-specifier-seq unless it declares a constructor, destructor,
10944    or conversion.  It might seem that we could check this condition in
10945    semantic analysis, rather than parsing, but that makes it difficult
10946    to handle something like `f()'.  We want to notice that there are
10947    no decl-specifiers, and therefore realize that this is an
10948    expression, not a declaration.)
10949
10950    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10951    the declarator is a direct-declarator of the form "(...)".  
10952
10953    MEMBER_P is true iff this declarator is a member-declarator.  */
10954
10955 static cp_declarator *
10956 cp_parser_declarator (cp_parser* parser,
10957                       cp_parser_declarator_kind dcl_kind,
10958                       int* ctor_dtor_or_conv_p,
10959                       bool* parenthesized_p,
10960                       bool member_p)
10961 {
10962   cp_token *token;
10963   cp_declarator *declarator;
10964   enum tree_code code;
10965   cp_cv_quals cv_quals;
10966   tree class_type;
10967   tree attributes = NULL_TREE;
10968
10969   /* Assume this is not a constructor, destructor, or type-conversion
10970      operator.  */
10971   if (ctor_dtor_or_conv_p)
10972     *ctor_dtor_or_conv_p = 0;
10973
10974   if (cp_parser_allow_gnu_extensions_p (parser))
10975     attributes = cp_parser_attributes_opt (parser);
10976
10977   /* Peek at the next token.  */
10978   token = cp_lexer_peek_token (parser->lexer);
10979
10980   /* Check for the ptr-operator production.  */
10981   cp_parser_parse_tentatively (parser);
10982   /* Parse the ptr-operator.  */
10983   code = cp_parser_ptr_operator (parser,
10984                                  &class_type,
10985                                  &cv_quals);
10986   /* If that worked, then we have a ptr-operator.  */
10987   if (cp_parser_parse_definitely (parser))
10988     {
10989       /* If a ptr-operator was found, then this declarator was not
10990          parenthesized.  */
10991       if (parenthesized_p)
10992         *parenthesized_p = true;
10993       /* The dependent declarator is optional if we are parsing an
10994          abstract-declarator.  */
10995       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10996         cp_parser_parse_tentatively (parser);
10997
10998       /* Parse the dependent declarator.  */
10999       declarator = cp_parser_declarator (parser, dcl_kind,
11000                                          /*ctor_dtor_or_conv_p=*/NULL,
11001                                          /*parenthesized_p=*/NULL,
11002                                          /*member_p=*/false);
11003
11004       /* If we are parsing an abstract-declarator, we must handle the
11005          case where the dependent declarator is absent.  */
11006       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
11007           && !cp_parser_parse_definitely (parser))
11008         declarator = NULL;
11009
11010       /* Build the representation of the ptr-operator.  */
11011       if (class_type)
11012         declarator = make_ptrmem_declarator (cv_quals,
11013                                              class_type,
11014                                              declarator);
11015       else if (code == INDIRECT_REF)
11016         declarator = make_pointer_declarator (cv_quals, declarator);
11017       else
11018         declarator = make_reference_declarator (cv_quals, declarator);
11019     }
11020   /* Everything else is a direct-declarator.  */
11021   else
11022     {
11023       if (parenthesized_p)
11024         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
11025                                                    CPP_OPEN_PAREN);
11026       declarator = cp_parser_direct_declarator (parser, dcl_kind,
11027                                                 ctor_dtor_or_conv_p,
11028                                                 member_p);
11029     }
11030
11031   if (attributes && declarator != cp_error_declarator)
11032     declarator->attributes = attributes;
11033
11034   return declarator;
11035 }
11036
11037 /* Parse a direct-declarator or direct-abstract-declarator.
11038
11039    direct-declarator:
11040      declarator-id
11041      direct-declarator ( parameter-declaration-clause )
11042        cv-qualifier-seq [opt]
11043        exception-specification [opt]
11044      direct-declarator [ constant-expression [opt] ]
11045      ( declarator )
11046
11047    direct-abstract-declarator:
11048      direct-abstract-declarator [opt]
11049        ( parameter-declaration-clause )
11050        cv-qualifier-seq [opt]
11051        exception-specification [opt]
11052      direct-abstract-declarator [opt] [ constant-expression [opt] ]
11053      ( abstract-declarator )
11054
11055    Returns a representation of the declarator.  DCL_KIND is
11056    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
11057    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
11058    we are parsing a direct-declarator.  It is
11059    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
11060    of ambiguity we prefer an abstract declarator, as per
11061    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
11062    cp_parser_declarator.  */
11063
11064 static cp_declarator *
11065 cp_parser_direct_declarator (cp_parser* parser,
11066                              cp_parser_declarator_kind dcl_kind,
11067                              int* ctor_dtor_or_conv_p,
11068                              bool member_p)
11069 {
11070   cp_token *token;
11071   cp_declarator *declarator = NULL;
11072   tree scope = NULL_TREE;
11073   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11074   bool saved_in_declarator_p = parser->in_declarator_p;
11075   bool first = true;
11076   tree pushed_scope = NULL_TREE;
11077
11078   while (true)
11079     {
11080       /* Peek at the next token.  */
11081       token = cp_lexer_peek_token (parser->lexer);
11082       if (token->type == CPP_OPEN_PAREN)
11083         {
11084           /* This is either a parameter-declaration-clause, or a
11085              parenthesized declarator. When we know we are parsing a
11086              named declarator, it must be a parenthesized declarator
11087              if FIRST is true. For instance, `(int)' is a
11088              parameter-declaration-clause, with an omitted
11089              direct-abstract-declarator. But `((*))', is a
11090              parenthesized abstract declarator. Finally, when T is a
11091              template parameter `(T)' is a
11092              parameter-declaration-clause, and not a parenthesized
11093              named declarator.
11094
11095              We first try and parse a parameter-declaration-clause,
11096              and then try a nested declarator (if FIRST is true).
11097
11098              It is not an error for it not to be a
11099              parameter-declaration-clause, even when FIRST is
11100              false. Consider,
11101
11102                int i (int);
11103                int i (3);
11104
11105              The first is the declaration of a function while the
11106              second is a the definition of a variable, including its
11107              initializer.
11108
11109              Having seen only the parenthesis, we cannot know which of
11110              these two alternatives should be selected.  Even more
11111              complex are examples like:
11112
11113                int i (int (a));
11114                int i (int (3));
11115
11116              The former is a function-declaration; the latter is a
11117              variable initialization.
11118
11119              Thus again, we try a parameter-declaration-clause, and if
11120              that fails, we back out and return.  */
11121
11122           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11123             {
11124               cp_parameter_declarator *params;
11125               unsigned saved_num_template_parameter_lists;
11126
11127               /* In a member-declarator, the only valid interpretation
11128                  of a parenthesis is the start of a
11129                  parameter-declaration-clause.  (It is invalid to
11130                  initialize a static data member with a parenthesized
11131                  initializer; only the "=" form of initialization is
11132                  permitted.)  */
11133               if (!member_p)
11134                 cp_parser_parse_tentatively (parser);
11135
11136               /* Consume the `('.  */
11137               cp_lexer_consume_token (parser->lexer);
11138               if (first)
11139                 {
11140                   /* If this is going to be an abstract declarator, we're
11141                      in a declarator and we can't have default args.  */
11142                   parser->default_arg_ok_p = false;
11143                   parser->in_declarator_p = true;
11144                 }
11145
11146               /* Inside the function parameter list, surrounding
11147                  template-parameter-lists do not apply.  */
11148               saved_num_template_parameter_lists
11149                 = parser->num_template_parameter_lists;
11150               parser->num_template_parameter_lists = 0;
11151
11152               /* Parse the parameter-declaration-clause.  */
11153               params = cp_parser_parameter_declaration_clause (parser);
11154
11155               parser->num_template_parameter_lists
11156                 = saved_num_template_parameter_lists;
11157
11158               /* If all went well, parse the cv-qualifier-seq and the
11159                  exception-specification.  */
11160               if (member_p || cp_parser_parse_definitely (parser))
11161                 {
11162                   cp_cv_quals cv_quals;
11163                   tree exception_specification;
11164
11165                   if (ctor_dtor_or_conv_p)
11166                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
11167                   first = false;
11168                   /* Consume the `)'.  */
11169                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
11170
11171                   /* Parse the cv-qualifier-seq.  */
11172                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11173                   /* And the exception-specification.  */
11174                   exception_specification
11175                     = cp_parser_exception_specification_opt (parser);
11176
11177                   /* Create the function-declarator.  */
11178                   declarator = make_call_declarator (declarator,
11179                                                      params,
11180                                                      cv_quals,
11181                                                      exception_specification);
11182                   /* Any subsequent parameter lists are to do with
11183                      return type, so are not those of the declared
11184                      function.  */
11185                   parser->default_arg_ok_p = false;
11186
11187                   /* Repeat the main loop.  */
11188                   continue;
11189                 }
11190             }
11191
11192           /* If this is the first, we can try a parenthesized
11193              declarator.  */
11194           if (first)
11195             {
11196               bool saved_in_type_id_in_expr_p;
11197
11198               parser->default_arg_ok_p = saved_default_arg_ok_p;
11199               parser->in_declarator_p = saved_in_declarator_p;
11200
11201               /* Consume the `('.  */
11202               cp_lexer_consume_token (parser->lexer);
11203               /* Parse the nested declarator.  */
11204               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
11205               parser->in_type_id_in_expr_p = true;
11206               declarator
11207                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
11208                                         /*parenthesized_p=*/NULL,
11209                                         member_p);
11210               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
11211               first = false;
11212               /* Expect a `)'.  */
11213               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
11214                 declarator = cp_error_declarator;
11215               if (declarator == cp_error_declarator)
11216                 break;
11217
11218               goto handle_declarator;
11219             }
11220           /* Otherwise, we must be done.  */
11221           else
11222             break;
11223         }
11224       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
11225                && token->type == CPP_OPEN_SQUARE)
11226         {
11227           /* Parse an array-declarator.  */
11228           tree bounds;
11229
11230           if (ctor_dtor_or_conv_p)
11231             *ctor_dtor_or_conv_p = 0;
11232
11233           first = false;
11234           parser->default_arg_ok_p = false;
11235           parser->in_declarator_p = true;
11236           /* Consume the `['.  */
11237           cp_lexer_consume_token (parser->lexer);
11238           /* Peek at the next token.  */
11239           token = cp_lexer_peek_token (parser->lexer);
11240           /* If the next token is `]', then there is no
11241              constant-expression.  */
11242           if (token->type != CPP_CLOSE_SQUARE)
11243             {
11244               bool non_constant_p;
11245
11246               bounds
11247                 = cp_parser_constant_expression (parser,
11248                                                  /*allow_non_constant=*/true,
11249                                                  &non_constant_p);
11250               if (!non_constant_p)
11251                 bounds = fold_non_dependent_expr (bounds);
11252               /* Normally, the array bound must be an integral constant
11253                  expression.  However, as an extension, we allow VLAs
11254                  in function scopes.  */  
11255               else if (!at_function_scope_p ())
11256                 {
11257                   error ("array bound is not an integer constant");
11258                   bounds = error_mark_node;
11259                 }
11260             }
11261           else
11262             bounds = NULL_TREE;
11263           /* Look for the closing `]'.  */
11264           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
11265             {
11266               declarator = cp_error_declarator;
11267               break;
11268             }
11269
11270           declarator = make_array_declarator (declarator, bounds);
11271         }
11272       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
11273         {
11274           tree qualifying_scope;
11275           tree unqualified_name;
11276
11277           /* Parse a declarator-id */
11278           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11279             cp_parser_parse_tentatively (parser);
11280           unqualified_name = cp_parser_declarator_id (parser);
11281           qualifying_scope = parser->scope;
11282           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
11283             {
11284               if (!cp_parser_parse_definitely (parser))
11285                 unqualified_name = error_mark_node;
11286               else if (qualifying_scope
11287                        || (TREE_CODE (unqualified_name) 
11288                            != IDENTIFIER_NODE))
11289                 {
11290                   cp_parser_error (parser, "expected unqualified-id");
11291                   unqualified_name = error_mark_node;
11292                 }
11293             }
11294
11295           if (unqualified_name == error_mark_node)
11296             {
11297               declarator = cp_error_declarator;
11298               break;
11299             }
11300
11301           if (qualifying_scope && at_namespace_scope_p ()
11302               && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
11303             {
11304               /* In the declaration of a member of a template class
11305                  outside of the class itself, the SCOPE will sometimes
11306                  be a TYPENAME_TYPE.  For example, given:
11307
11308                  template <typename T>
11309                  int S<T>::R::i = 3;
11310
11311                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11312                  this context, we must resolve S<T>::R to an ordinary
11313                  type, rather than a typename type.
11314
11315                  The reason we normally avoid resolving TYPENAME_TYPEs
11316                  is that a specialization of `S' might render
11317                  `S<T>::R' not a type.  However, if `S' is
11318                  specialized, then this `i' will not be used, so there
11319                  is no harm in resolving the types here.  */
11320               tree type;
11321               
11322               /* Resolve the TYPENAME_TYPE.  */
11323               type = resolve_typename_type (qualifying_scope,
11324                                             /*only_current_p=*/false);
11325               /* If that failed, the declarator is invalid.  */
11326               if (type == error_mark_node)
11327                 error ("%<%T::%D%> is not a type",
11328                        TYPE_CONTEXT (qualifying_scope),
11329                        TYPE_IDENTIFIER (qualifying_scope));
11330               qualifying_scope = type;
11331             }
11332
11333           declarator = make_id_declarator (qualifying_scope, 
11334                                            unqualified_name);
11335           declarator->id_loc = token->location;
11336           if (unqualified_name)
11337             {
11338               tree class_type;
11339
11340               if (qualifying_scope
11341                   && CLASS_TYPE_P (qualifying_scope))
11342                 class_type = qualifying_scope;
11343               else
11344                 class_type = current_class_type;
11345
11346               if (class_type)
11347                 {
11348                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11349                     declarator->u.id.sfk = sfk_destructor;
11350                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11351                     declarator->u.id.sfk = sfk_conversion;
11352                   else if (/* There's no way to declare a constructor
11353                               for an anonymous type, even if the type
11354                               got a name for linkage purposes.  */
11355                            !TYPE_WAS_ANONYMOUS (class_type)
11356                            && (constructor_name_p (unqualified_name,
11357                                                    class_type)
11358                                || (TREE_CODE (unqualified_name) == TYPE_DECL
11359                                    && (same_type_p 
11360                                        (TREE_TYPE (unqualified_name),
11361                                         class_type)))))
11362                     declarator->u.id.sfk = sfk_constructor;
11363
11364                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11365                     *ctor_dtor_or_conv_p = -1;
11366                   if (qualifying_scope
11367                       && TREE_CODE (unqualified_name) == TYPE_DECL
11368                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11369                     {
11370                       error ("invalid use of constructor as a template");
11371                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11372                               "the constructor in a qualified name",
11373                               class_type,
11374                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11375                               class_type, class_type);
11376                     }
11377                 }
11378             }
11379
11380         handle_declarator:;
11381           scope = get_scope_of_declarator (declarator);
11382           if (scope)
11383             /* Any names that appear after the declarator-id for a
11384                member are looked up in the containing scope.  */
11385             pushed_scope = push_scope (scope);
11386           parser->in_declarator_p = true;
11387           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11388               || (declarator && declarator->kind == cdk_id))
11389             /* Default args are only allowed on function
11390                declarations.  */
11391             parser->default_arg_ok_p = saved_default_arg_ok_p;
11392           else
11393             parser->default_arg_ok_p = false;
11394
11395           first = false;
11396         }
11397       /* We're done.  */
11398       else
11399         break;
11400     }
11401
11402   /* For an abstract declarator, we might wind up with nothing at this
11403      point.  That's an error; the declarator is not optional.  */
11404   if (!declarator)
11405     cp_parser_error (parser, "expected declarator");
11406
11407   /* If we entered a scope, we must exit it now.  */
11408   if (pushed_scope)
11409     pop_scope (pushed_scope);
11410
11411   parser->default_arg_ok_p = saved_default_arg_ok_p;
11412   parser->in_declarator_p = saved_in_declarator_p;
11413
11414   return declarator;
11415 }
11416
11417 /* Parse a ptr-operator.
11418
11419    ptr-operator:
11420      * cv-qualifier-seq [opt]
11421      &
11422      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11423
11424    GNU Extension:
11425
11426    ptr-operator:
11427      & cv-qualifier-seq [opt]
11428
11429    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11430    Returns ADDR_EXPR if a reference was used.  In the case of a
11431    pointer-to-member, *TYPE is filled in with the TYPE containing the
11432    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11433    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11434    ERROR_MARK if an error occurred.  */
11435
11436 static enum tree_code
11437 cp_parser_ptr_operator (cp_parser* parser,
11438                         tree* type,
11439                         cp_cv_quals *cv_quals)
11440 {
11441   enum tree_code code = ERROR_MARK;
11442   cp_token *token;
11443
11444   /* Assume that it's not a pointer-to-member.  */
11445   *type = NULL_TREE;
11446   /* And that there are no cv-qualifiers.  */
11447   *cv_quals = TYPE_UNQUALIFIED;
11448
11449   /* Peek at the next token.  */
11450   token = cp_lexer_peek_token (parser->lexer);
11451   /* If it's a `*' or `&' we have a pointer or reference.  */
11452   if (token->type == CPP_MULT || token->type == CPP_AND)
11453     {
11454       /* Remember which ptr-operator we were processing.  */
11455       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11456
11457       /* Consume the `*' or `&'.  */
11458       cp_lexer_consume_token (parser->lexer);
11459
11460       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11461          `&', if we are allowing GNU extensions.  (The only qualifier
11462          that can legally appear after `&' is `restrict', but that is
11463          enforced during semantic analysis.  */
11464       if (code == INDIRECT_REF
11465           || cp_parser_allow_gnu_extensions_p (parser))
11466         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11467     }
11468   else
11469     {
11470       /* Try the pointer-to-member case.  */
11471       cp_parser_parse_tentatively (parser);
11472       /* Look for the optional `::' operator.  */
11473       cp_parser_global_scope_opt (parser,
11474                                   /*current_scope_valid_p=*/false);
11475       /* Look for the nested-name specifier.  */
11476       cp_parser_nested_name_specifier (parser,
11477                                        /*typename_keyword_p=*/false,
11478                                        /*check_dependency_p=*/true,
11479                                        /*type_p=*/false,
11480                                        /*is_declaration=*/false);
11481       /* If we found it, and the next token is a `*', then we are
11482          indeed looking at a pointer-to-member operator.  */
11483       if (!cp_parser_error_occurred (parser)
11484           && cp_parser_require (parser, CPP_MULT, "`*'"))
11485         {
11486           /* The type of which the member is a member is given by the
11487              current SCOPE.  */
11488           *type = parser->scope;
11489           /* The next name will not be qualified.  */
11490           parser->scope = NULL_TREE;
11491           parser->qualifying_scope = NULL_TREE;
11492           parser->object_scope = NULL_TREE;
11493           /* Indicate that the `*' operator was used.  */
11494           code = INDIRECT_REF;
11495           /* Look for the optional cv-qualifier-seq.  */
11496           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11497         }
11498       /* If that didn't work we don't have a ptr-operator.  */
11499       if (!cp_parser_parse_definitely (parser))
11500         cp_parser_error (parser, "expected ptr-operator");
11501     }
11502
11503   return code;
11504 }
11505
11506 /* Parse an (optional) cv-qualifier-seq.
11507
11508    cv-qualifier-seq:
11509      cv-qualifier cv-qualifier-seq [opt]
11510
11511    cv-qualifier:
11512      const
11513      volatile
11514
11515    GNU Extension:
11516
11517    cv-qualifier:
11518      __restrict__
11519
11520    Returns a bitmask representing the cv-qualifiers.  */
11521
11522 static cp_cv_quals
11523 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11524 {
11525   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11526
11527   while (true)
11528     {
11529       cp_token *token;
11530       cp_cv_quals cv_qualifier;
11531
11532       /* Peek at the next token.  */
11533       token = cp_lexer_peek_token (parser->lexer);
11534       /* See if it's a cv-qualifier.  */
11535       switch (token->keyword)
11536         {
11537         case RID_CONST:
11538           cv_qualifier = TYPE_QUAL_CONST;
11539           break;
11540
11541         case RID_VOLATILE:
11542           cv_qualifier = TYPE_QUAL_VOLATILE;
11543           break;
11544
11545         case RID_RESTRICT:
11546           cv_qualifier = TYPE_QUAL_RESTRICT;
11547           break;
11548
11549         default:
11550           cv_qualifier = TYPE_UNQUALIFIED;
11551           break;
11552         }
11553
11554       if (!cv_qualifier)
11555         break;
11556
11557       if (cv_quals & cv_qualifier)
11558         {
11559           error ("duplicate cv-qualifier");
11560           cp_lexer_purge_token (parser->lexer);
11561         }
11562       else
11563         {
11564           cp_lexer_consume_token (parser->lexer);
11565           cv_quals |= cv_qualifier;
11566         }
11567     }
11568
11569   return cv_quals;
11570 }
11571
11572 /* Parse a declarator-id.
11573
11574    declarator-id:
11575      id-expression
11576      :: [opt] nested-name-specifier [opt] type-name
11577
11578    In the `id-expression' case, the value returned is as for
11579    cp_parser_id_expression if the id-expression was an unqualified-id.
11580    If the id-expression was a qualified-id, then a SCOPE_REF is
11581    returned.  The first operand is the scope (either a NAMESPACE_DECL
11582    or TREE_TYPE), but the second is still just a representation of an
11583    unqualified-id.  */
11584
11585 static tree
11586 cp_parser_declarator_id (cp_parser* parser)
11587 {
11588   /* The expression must be an id-expression.  Assume that qualified
11589      names are the names of types so that:
11590
11591        template <class T>
11592        int S<T>::R::i = 3;
11593
11594      will work; we must treat `S<T>::R' as the name of a type.
11595      Similarly, assume that qualified names are templates, where
11596      required, so that:
11597
11598        template <class T>
11599        int S<T>::R<T>::i = 3;
11600
11601      will work, too.  */
11602   return cp_parser_id_expression (parser,
11603                                   /*template_keyword_p=*/false,
11604                                   /*check_dependency_p=*/false,
11605                                   /*template_p=*/NULL,
11606                                   /*declarator_p=*/true);
11607 }
11608
11609 /* Parse a type-id.
11610
11611    type-id:
11612      type-specifier-seq abstract-declarator [opt]
11613
11614    Returns the TYPE specified.  */
11615
11616 static tree
11617 cp_parser_type_id (cp_parser* parser)
11618 {
11619   cp_decl_specifier_seq type_specifier_seq;
11620   cp_declarator *abstract_declarator;
11621
11622   /* Parse the type-specifier-seq.  */
11623   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
11624                                 &type_specifier_seq);
11625   if (type_specifier_seq.type == error_mark_node)
11626     return error_mark_node;
11627
11628   /* There might or might not be an abstract declarator.  */
11629   cp_parser_parse_tentatively (parser);
11630   /* Look for the declarator.  */
11631   abstract_declarator
11632     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11633                             /*parenthesized_p=*/NULL,
11634                             /*member_p=*/false);
11635   /* Check to see if there really was a declarator.  */
11636   if (!cp_parser_parse_definitely (parser))
11637     abstract_declarator = NULL;
11638
11639   return groktypename (&type_specifier_seq, abstract_declarator);
11640 }
11641
11642 /* Parse a type-specifier-seq.
11643
11644    type-specifier-seq:
11645      type-specifier type-specifier-seq [opt]
11646
11647    GNU extension:
11648
11649    type-specifier-seq:
11650      attributes type-specifier-seq [opt]
11651
11652    If IS_CONDITION is true, we are at the start of a "condition",
11653    e.g., we've just seen "if (".
11654
11655    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11656
11657 static void
11658 cp_parser_type_specifier_seq (cp_parser* parser,
11659                               bool is_condition,
11660                               cp_decl_specifier_seq *type_specifier_seq)
11661 {
11662   bool seen_type_specifier = false;
11663   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
11664
11665   /* Clear the TYPE_SPECIFIER_SEQ.  */
11666   clear_decl_specs (type_specifier_seq);
11667
11668   /* Parse the type-specifiers and attributes.  */
11669   while (true)
11670     {
11671       tree type_specifier;
11672       bool is_cv_qualifier;
11673
11674       /* Check for attributes first.  */
11675       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11676         {
11677           type_specifier_seq->attributes =
11678             chainon (type_specifier_seq->attributes,
11679                      cp_parser_attributes_opt (parser));
11680           continue;
11681         }
11682
11683       /* Look for the type-specifier.  */
11684       type_specifier = cp_parser_type_specifier (parser,
11685                                                  flags,
11686                                                  type_specifier_seq,
11687                                                  /*is_declaration=*/false,
11688                                                  NULL,
11689                                                  &is_cv_qualifier);
11690       if (!type_specifier)
11691         {
11692           /* If the first type-specifier could not be found, this is not a
11693              type-specifier-seq at all.  */
11694           if (!seen_type_specifier)
11695             {
11696               cp_parser_error (parser, "expected type-specifier");
11697               type_specifier_seq->type = error_mark_node;
11698               return;
11699             }
11700           /* If subsequent type-specifiers could not be found, the
11701              type-specifier-seq is complete.  */
11702           break;
11703         }
11704
11705       seen_type_specifier = true;
11706       /* The standard says that a condition can be:
11707
11708             type-specifier-seq declarator = assignment-expression
11709       
11710          However, given:
11711
11712            struct S {};
11713            if (int S = ...)
11714
11715          we should treat the "S" as a declarator, not as a
11716          type-specifier.  The standard doesn't say that explicitly for
11717          type-specifier-seq, but it does say that for
11718          decl-specifier-seq in an ordinary declaration.  Perhaps it
11719          would be clearer just to allow a decl-specifier-seq here, and
11720          then add a semantic restriction that if any decl-specifiers
11721          that are not type-specifiers appear, the program is invalid.  */
11722       if (is_condition && !is_cv_qualifier)
11723         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES; 
11724     }
11725
11726   return;
11727 }
11728
11729 /* Parse a parameter-declaration-clause.
11730
11731    parameter-declaration-clause:
11732      parameter-declaration-list [opt] ... [opt]
11733      parameter-declaration-list , ...
11734
11735    Returns a representation for the parameter declarations.  A return
11736    value of NULL indicates a parameter-declaration-clause consisting
11737    only of an ellipsis.  */
11738
11739 static cp_parameter_declarator *
11740 cp_parser_parameter_declaration_clause (cp_parser* parser)
11741 {
11742   cp_parameter_declarator *parameters;
11743   cp_token *token;
11744   bool ellipsis_p;
11745   bool is_error;
11746
11747   /* Peek at the next token.  */
11748   token = cp_lexer_peek_token (parser->lexer);
11749   /* Check for trivial parameter-declaration-clauses.  */
11750   if (token->type == CPP_ELLIPSIS)
11751     {
11752       /* Consume the `...' token.  */
11753       cp_lexer_consume_token (parser->lexer);
11754       return NULL;
11755     }
11756   else if (token->type == CPP_CLOSE_PAREN)
11757     /* There are no parameters.  */
11758     {
11759 #ifndef NO_IMPLICIT_EXTERN_C
11760       if (in_system_header && current_class_type == NULL
11761           && current_lang_name == lang_name_c)
11762         return NULL;
11763       else
11764 #endif
11765         return no_parameters;
11766     }
11767   /* Check for `(void)', too, which is a special case.  */
11768   else if (token->keyword == RID_VOID
11769            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11770                == CPP_CLOSE_PAREN))
11771     {
11772       /* Consume the `void' token.  */
11773       cp_lexer_consume_token (parser->lexer);
11774       /* There are no parameters.  */
11775       return no_parameters;
11776     }
11777
11778   /* Parse the parameter-declaration-list.  */
11779   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11780   /* If a parse error occurred while parsing the
11781      parameter-declaration-list, then the entire
11782      parameter-declaration-clause is erroneous.  */
11783   if (is_error)
11784     return NULL;
11785
11786   /* Peek at the next token.  */
11787   token = cp_lexer_peek_token (parser->lexer);
11788   /* If it's a `,', the clause should terminate with an ellipsis.  */
11789   if (token->type == CPP_COMMA)
11790     {
11791       /* Consume the `,'.  */
11792       cp_lexer_consume_token (parser->lexer);
11793       /* Expect an ellipsis.  */
11794       ellipsis_p
11795         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11796     }
11797   /* It might also be `...' if the optional trailing `,' was
11798      omitted.  */
11799   else if (token->type == CPP_ELLIPSIS)
11800     {
11801       /* Consume the `...' token.  */
11802       cp_lexer_consume_token (parser->lexer);
11803       /* And remember that we saw it.  */
11804       ellipsis_p = true;
11805     }
11806   else
11807     ellipsis_p = false;
11808
11809   /* Finish the parameter list.  */
11810   if (parameters && ellipsis_p)
11811     parameters->ellipsis_p = true;
11812
11813   return parameters;
11814 }
11815
11816 /* Parse a parameter-declaration-list.
11817
11818    parameter-declaration-list:
11819      parameter-declaration
11820      parameter-declaration-list , parameter-declaration
11821
11822    Returns a representation of the parameter-declaration-list, as for
11823    cp_parser_parameter_declaration_clause.  However, the
11824    `void_list_node' is never appended to the list.  Upon return,
11825    *IS_ERROR will be true iff an error occurred.  */
11826
11827 static cp_parameter_declarator *
11828 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11829 {
11830   cp_parameter_declarator *parameters = NULL;
11831   cp_parameter_declarator **tail = &parameters;
11832
11833   /* Assume all will go well.  */
11834   *is_error = false;
11835
11836   /* Look for more parameters.  */
11837   while (true)
11838     {
11839       cp_parameter_declarator *parameter;
11840       bool parenthesized_p;
11841       /* Parse the parameter.  */
11842       parameter
11843         = cp_parser_parameter_declaration (parser,
11844                                            /*template_parm_p=*/false,
11845                                            &parenthesized_p);
11846
11847       /* If a parse error occurred parsing the parameter declaration,
11848          then the entire parameter-declaration-list is erroneous.  */
11849       if (!parameter)
11850         {
11851           *is_error = true;
11852           parameters = NULL;
11853           break;
11854         }
11855       /* Add the new parameter to the list.  */
11856       *tail = parameter;
11857       tail = &parameter->next;
11858
11859       /* Peek at the next token.  */
11860       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11861           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11862           /* These are for Objective-C++ */
11863           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
11864           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11865         /* The parameter-declaration-list is complete.  */
11866         break;
11867       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11868         {
11869           cp_token *token;
11870
11871           /* Peek at the next token.  */
11872           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11873           /* If it's an ellipsis, then the list is complete.  */
11874           if (token->type == CPP_ELLIPSIS)
11875             break;
11876           /* Otherwise, there must be more parameters.  Consume the
11877              `,'.  */
11878           cp_lexer_consume_token (parser->lexer);
11879           /* When parsing something like:
11880
11881                 int i(float f, double d)
11882
11883              we can tell after seeing the declaration for "f" that we
11884              are not looking at an initialization of a variable "i",
11885              but rather at the declaration of a function "i".
11886
11887              Due to the fact that the parsing of template arguments
11888              (as specified to a template-id) requires backtracking we
11889              cannot use this technique when inside a template argument
11890              list.  */
11891           if (!parser->in_template_argument_list_p
11892               && !parser->in_type_id_in_expr_p
11893               && cp_parser_uncommitted_to_tentative_parse_p (parser)
11894               /* However, a parameter-declaration of the form
11895                  "foat(f)" (which is a valid declaration of a
11896                  parameter "f") can also be interpreted as an
11897                  expression (the conversion of "f" to "float").  */
11898               && !parenthesized_p)
11899             cp_parser_commit_to_tentative_parse (parser);
11900         }
11901       else
11902         {
11903           cp_parser_error (parser, "expected %<,%> or %<...%>");
11904           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
11905             cp_parser_skip_to_closing_parenthesis (parser,
11906                                                    /*recovering=*/true,
11907                                                    /*or_comma=*/false,
11908                                                    /*consume_paren=*/false);
11909           break;
11910         }
11911     }
11912
11913   return parameters;
11914 }
11915
11916 /* Parse a parameter declaration.
11917
11918    parameter-declaration:
11919      decl-specifier-seq declarator
11920      decl-specifier-seq declarator = assignment-expression
11921      decl-specifier-seq abstract-declarator [opt]
11922      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11923
11924    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11925    declares a template parameter.  (In that case, a non-nested `>'
11926    token encountered during the parsing of the assignment-expression
11927    is not interpreted as a greater-than operator.)
11928
11929    Returns a representation of the parameter, or NULL if an error
11930    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11931    true iff the declarator is of the form "(p)".  */
11932
11933 static cp_parameter_declarator *
11934 cp_parser_parameter_declaration (cp_parser *parser,
11935                                  bool template_parm_p,
11936                                  bool *parenthesized_p)
11937 {
11938   int declares_class_or_enum;
11939   bool greater_than_is_operator_p;
11940   cp_decl_specifier_seq decl_specifiers;
11941   cp_declarator *declarator;
11942   tree default_argument;
11943   cp_token *token;
11944   const char *saved_message;
11945
11946   /* In a template parameter, `>' is not an operator.
11947
11948      [temp.param]
11949
11950      When parsing a default template-argument for a non-type
11951      template-parameter, the first non-nested `>' is taken as the end
11952      of the template parameter-list rather than a greater-than
11953      operator.  */
11954   greater_than_is_operator_p = !template_parm_p;
11955
11956   /* Type definitions may not appear in parameter types.  */
11957   saved_message = parser->type_definition_forbidden_message;
11958   parser->type_definition_forbidden_message
11959     = "types may not be defined in parameter types";
11960
11961   /* Parse the declaration-specifiers.  */
11962   cp_parser_decl_specifier_seq (parser,
11963                                 CP_PARSER_FLAGS_NONE,
11964                                 &decl_specifiers,
11965                                 &declares_class_or_enum);
11966   /* If an error occurred, there's no reason to attempt to parse the
11967      rest of the declaration.  */
11968   if (cp_parser_error_occurred (parser))
11969     {
11970       parser->type_definition_forbidden_message = saved_message;
11971       return NULL;
11972     }
11973
11974   /* Peek at the next token.  */
11975   token = cp_lexer_peek_token (parser->lexer);
11976   /* If the next token is a `)', `,', `=', `>', or `...', then there
11977      is no declarator.  */
11978   if (token->type == CPP_CLOSE_PAREN
11979       || token->type == CPP_COMMA
11980       || token->type == CPP_EQ
11981       || token->type == CPP_ELLIPSIS
11982       || token->type == CPP_GREATER)
11983     {
11984       declarator = NULL;
11985       if (parenthesized_p)
11986         *parenthesized_p = false;
11987     }
11988   /* Otherwise, there should be a declarator.  */
11989   else
11990     {
11991       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11992       parser->default_arg_ok_p = false;
11993
11994       /* After seeing a decl-specifier-seq, if the next token is not a
11995          "(", there is no possibility that the code is a valid
11996          expression.  Therefore, if parsing tentatively, we commit at
11997          this point.  */
11998       if (!parser->in_template_argument_list_p
11999           /* In an expression context, having seen:
12000
12001                (int((char ...
12002
12003              we cannot be sure whether we are looking at a
12004              function-type (taking a "char" as a parameter) or a cast
12005              of some object of type "char" to "int".  */
12006           && !parser->in_type_id_in_expr_p
12007           && cp_parser_uncommitted_to_tentative_parse_p (parser)
12008           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
12009         cp_parser_commit_to_tentative_parse (parser);
12010       /* Parse the declarator.  */
12011       declarator = cp_parser_declarator (parser,
12012                                          CP_PARSER_DECLARATOR_EITHER,
12013                                          /*ctor_dtor_or_conv_p=*/NULL,
12014                                          parenthesized_p,
12015                                          /*member_p=*/false);
12016       parser->default_arg_ok_p = saved_default_arg_ok_p;
12017       /* After the declarator, allow more attributes.  */
12018       decl_specifiers.attributes
12019         = chainon (decl_specifiers.attributes,
12020                    cp_parser_attributes_opt (parser));
12021     }
12022
12023   /* The restriction on defining new types applies only to the type
12024      of the parameter, not to the default argument.  */
12025   parser->type_definition_forbidden_message = saved_message;
12026
12027   /* If the next token is `=', then process a default argument.  */
12028   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12029     {
12030       bool saved_greater_than_is_operator_p;
12031       /* Consume the `='.  */
12032       cp_lexer_consume_token (parser->lexer);
12033
12034       /* If we are defining a class, then the tokens that make up the
12035          default argument must be saved and processed later.  */
12036       if (!template_parm_p && at_class_scope_p ()
12037           && TYPE_BEING_DEFINED (current_class_type))
12038         {
12039           unsigned depth = 0;
12040           cp_token *first_token;
12041           cp_token *token;
12042
12043           /* Add tokens until we have processed the entire default
12044              argument.  We add the range [first_token, token).  */
12045           first_token = cp_lexer_peek_token (parser->lexer);
12046           while (true)
12047             {
12048               bool done = false;
12049
12050               /* Peek at the next token.  */
12051               token = cp_lexer_peek_token (parser->lexer);
12052               /* What we do depends on what token we have.  */
12053               switch (token->type)
12054                 {
12055                   /* In valid code, a default argument must be
12056                      immediately followed by a `,' `)', or `...'.  */
12057                 case CPP_COMMA:
12058                 case CPP_CLOSE_PAREN:
12059                 case CPP_ELLIPSIS:
12060                   /* If we run into a non-nested `;', `}', or `]',
12061                      then the code is invalid -- but the default
12062                      argument is certainly over.  */
12063                 case CPP_SEMICOLON:
12064                 case CPP_CLOSE_BRACE:
12065                 case CPP_CLOSE_SQUARE:
12066                   if (depth == 0)
12067                     done = true;
12068                   /* Update DEPTH, if necessary.  */
12069                   else if (token->type == CPP_CLOSE_PAREN
12070                            || token->type == CPP_CLOSE_BRACE
12071                            || token->type == CPP_CLOSE_SQUARE)
12072                     --depth;
12073                   break;
12074
12075                 case CPP_OPEN_PAREN:
12076                 case CPP_OPEN_SQUARE:
12077                 case CPP_OPEN_BRACE:
12078                   ++depth;
12079                   break;
12080
12081                 case CPP_GREATER:
12082                   /* If we see a non-nested `>', and `>' is not an
12083                      operator, then it marks the end of the default
12084                      argument.  */
12085                   if (!depth && !greater_than_is_operator_p)
12086                     done = true;
12087                   break;
12088
12089                   /* If we run out of tokens, issue an error message.  */
12090                 case CPP_EOF:
12091                   error ("file ends in default argument");
12092                   done = true;
12093                   break;
12094
12095                 case CPP_NAME:
12096                 case CPP_SCOPE:
12097                   /* In these cases, we should look for template-ids.
12098                      For example, if the default argument is
12099                      `X<int, double>()', we need to do name lookup to
12100                      figure out whether or not `X' is a template; if
12101                      so, the `,' does not end the default argument.
12102
12103                      That is not yet done.  */
12104                   break;
12105
12106                 default:
12107                   break;
12108                 }
12109
12110               /* If we've reached the end, stop.  */
12111               if (done)
12112                 break;
12113
12114               /* Add the token to the token block.  */
12115               token = cp_lexer_consume_token (parser->lexer);
12116             }
12117
12118           /* Create a DEFAULT_ARG to represented the unparsed default
12119              argument.  */
12120           default_argument = make_node (DEFAULT_ARG);
12121           DEFARG_TOKENS (default_argument)
12122             = cp_token_cache_new (first_token, token);  
12123         }
12124       /* Outside of a class definition, we can just parse the
12125          assignment-expression.  */
12126       else
12127         {
12128           bool saved_local_variables_forbidden_p;
12129
12130           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
12131              set correctly.  */
12132           saved_greater_than_is_operator_p
12133             = parser->greater_than_is_operator_p;
12134           parser->greater_than_is_operator_p = greater_than_is_operator_p;
12135           /* Local variable names (and the `this' keyword) may not
12136              appear in a default argument.  */
12137           saved_local_variables_forbidden_p
12138             = parser->local_variables_forbidden_p;
12139           parser->local_variables_forbidden_p = true;
12140           /* Parse the assignment-expression.  */
12141           default_argument 
12142             = cp_parser_assignment_expression (parser, /*cast_p=*/false);
12143           /* Restore saved state.  */
12144           parser->greater_than_is_operator_p
12145             = saved_greater_than_is_operator_p;
12146           parser->local_variables_forbidden_p
12147             = saved_local_variables_forbidden_p;
12148         }
12149       if (!parser->default_arg_ok_p)
12150         {
12151           if (!flag_pedantic_errors)
12152             warning (0, "deprecated use of default argument for parameter of non-function");
12153           else
12154             {
12155               error ("default arguments are only permitted for function parameters");
12156               default_argument = NULL_TREE;
12157             }
12158         }
12159     }
12160   else
12161     default_argument = NULL_TREE;
12162
12163   return make_parameter_declarator (&decl_specifiers,
12164                                     declarator,
12165                                     default_argument);
12166 }
12167
12168 /* Parse a function-body.
12169
12170    function-body:
12171      compound_statement  */
12172
12173 static void
12174 cp_parser_function_body (cp_parser *parser)
12175 {
12176   cp_parser_compound_statement (parser, NULL, false);
12177 }
12178
12179 /* Parse a ctor-initializer-opt followed by a function-body.  Return
12180    true if a ctor-initializer was present.  */
12181
12182 static bool
12183 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
12184 {
12185   tree body;
12186   bool ctor_initializer_p;
12187
12188   /* Begin the function body.  */
12189   body = begin_function_body ();
12190   /* Parse the optional ctor-initializer.  */
12191   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
12192   /* Parse the function-body.  */
12193   cp_parser_function_body (parser);
12194   /* Finish the function body.  */
12195   finish_function_body (body);
12196
12197   return ctor_initializer_p;
12198 }
12199
12200 /* Parse an initializer.
12201
12202    initializer:
12203      = initializer-clause
12204      ( expression-list )
12205
12206    Returns a expression representing the initializer.  If no
12207    initializer is present, NULL_TREE is returned.
12208
12209    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
12210    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
12211    set to FALSE if there is no initializer present.  If there is an
12212    initializer, and it is not a constant-expression, *NON_CONSTANT_P
12213    is set to true; otherwise it is set to false.  */
12214
12215 static tree
12216 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
12217                        bool* non_constant_p)
12218 {
12219   cp_token *token;
12220   tree init;
12221
12222   /* Peek at the next token.  */
12223   token = cp_lexer_peek_token (parser->lexer);
12224
12225   /* Let our caller know whether or not this initializer was
12226      parenthesized.  */
12227   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
12228   /* Assume that the initializer is constant.  */
12229   *non_constant_p = false;
12230
12231   if (token->type == CPP_EQ)
12232     {
12233       /* Consume the `='.  */
12234       cp_lexer_consume_token (parser->lexer);
12235       /* Parse the initializer-clause.  */
12236       init = cp_parser_initializer_clause (parser, non_constant_p);
12237     }
12238   else if (token->type == CPP_OPEN_PAREN)
12239     init = cp_parser_parenthesized_expression_list (parser, false,
12240                                                     /*cast_p=*/false,
12241                                                     non_constant_p);
12242   else
12243     {
12244       /* Anything else is an error.  */
12245       cp_parser_error (parser, "expected initializer");
12246       init = error_mark_node;
12247     }
12248
12249   return init;
12250 }
12251
12252 /* Parse an initializer-clause.
12253
12254    initializer-clause:
12255      assignment-expression
12256      { initializer-list , [opt] }
12257      { }
12258
12259    Returns an expression representing the initializer.
12260
12261    If the `assignment-expression' production is used the value
12262    returned is simply a representation for the expression.
12263
12264    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
12265    the elements of the initializer-list (or NULL_TREE, if the last
12266    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
12267    NULL_TREE.  There is no way to detect whether or not the optional
12268    trailing `,' was provided.  NON_CONSTANT_P is as for
12269    cp_parser_initializer.  */
12270
12271 static tree
12272 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
12273 {
12274   tree initializer;
12275
12276   /* Assume the expression is constant.  */
12277   *non_constant_p = false;
12278
12279   /* If it is not a `{', then we are looking at an
12280      assignment-expression.  */
12281   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
12282     {
12283       initializer
12284         = cp_parser_constant_expression (parser,
12285                                         /*allow_non_constant_p=*/true,
12286                                         non_constant_p);
12287       if (!*non_constant_p)
12288         initializer = fold_non_dependent_expr (initializer);
12289     }
12290   else
12291     {
12292       /* Consume the `{' token.  */
12293       cp_lexer_consume_token (parser->lexer);
12294       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
12295       initializer = make_node (CONSTRUCTOR);
12296       /* If it's not a `}', then there is a non-trivial initializer.  */
12297       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12298         {
12299           /* Parse the initializer list.  */
12300           CONSTRUCTOR_ELTS (initializer)
12301             = cp_parser_initializer_list (parser, non_constant_p);
12302           /* A trailing `,' token is allowed.  */
12303           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
12304             cp_lexer_consume_token (parser->lexer);
12305         }
12306       /* Now, there should be a trailing `}'.  */
12307       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12308     }
12309
12310   return initializer;
12311 }
12312
12313 /* Parse an initializer-list.
12314
12315    initializer-list:
12316      initializer-clause
12317      initializer-list , initializer-clause
12318
12319    GNU Extension:
12320
12321    initializer-list:
12322      identifier : initializer-clause
12323      initializer-list, identifier : initializer-clause
12324
12325    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12326    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12327    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12328    as for cp_parser_initializer.  */
12329
12330 static tree
12331 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12332 {
12333   tree initializers = NULL_TREE;
12334
12335   /* Assume all of the expressions are constant.  */
12336   *non_constant_p = false;
12337
12338   /* Parse the rest of the list.  */
12339   while (true)
12340     {
12341       cp_token *token;
12342       tree identifier;
12343       tree initializer;
12344       bool clause_non_constant_p;
12345
12346       /* If the next token is an identifier and the following one is a
12347          colon, we are looking at the GNU designated-initializer
12348          syntax.  */
12349       if (cp_parser_allow_gnu_extensions_p (parser)
12350           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12351           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12352         {
12353           /* Consume the identifier.  */
12354           identifier = cp_lexer_consume_token (parser->lexer)->value;
12355           /* Consume the `:'.  */
12356           cp_lexer_consume_token (parser->lexer);
12357         }
12358       else
12359         identifier = NULL_TREE;
12360
12361       /* Parse the initializer.  */
12362       initializer = cp_parser_initializer_clause (parser,
12363                                                   &clause_non_constant_p);
12364       /* If any clause is non-constant, so is the entire initializer.  */
12365       if (clause_non_constant_p)
12366         *non_constant_p = true;
12367       /* Add it to the list.  */
12368       initializers = tree_cons (identifier, initializer, initializers);
12369
12370       /* If the next token is not a comma, we have reached the end of
12371          the list.  */
12372       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12373         break;
12374
12375       /* Peek at the next token.  */
12376       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12377       /* If the next token is a `}', then we're still done.  An
12378          initializer-clause can have a trailing `,' after the
12379          initializer-list and before the closing `}'.  */
12380       if (token->type == CPP_CLOSE_BRACE)
12381         break;
12382
12383       /* Consume the `,' token.  */
12384       cp_lexer_consume_token (parser->lexer);
12385     }
12386
12387   /* The initializers were built up in reverse order, so we need to
12388      reverse them now.  */
12389   return nreverse (initializers);
12390 }
12391
12392 /* Classes [gram.class] */
12393
12394 /* Parse a class-name.
12395
12396    class-name:
12397      identifier
12398      template-id
12399
12400    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12401    to indicate that names looked up in dependent types should be
12402    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12403    keyword has been used to indicate that the name that appears next
12404    is a template.  TAG_TYPE indicates the explicit tag given before
12405    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
12406    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
12407    is the class being defined in a class-head.
12408
12409    Returns the TYPE_DECL representing the class.  */
12410
12411 static tree
12412 cp_parser_class_name (cp_parser *parser,
12413                       bool typename_keyword_p,
12414                       bool template_keyword_p,
12415                       enum tag_types tag_type,
12416                       bool check_dependency_p,
12417                       bool class_head_p,
12418                       bool is_declaration)
12419 {
12420   tree decl;
12421   tree scope;
12422   bool typename_p;
12423   cp_token *token;
12424
12425   /* All class-names start with an identifier.  */
12426   token = cp_lexer_peek_token (parser->lexer);
12427   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12428     {
12429       cp_parser_error (parser, "expected class-name");
12430       return error_mark_node;
12431     }
12432
12433   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12434      to a template-id, so we save it here.  */
12435   scope = parser->scope;
12436   if (scope == error_mark_node)
12437     return error_mark_node;
12438
12439   /* Any name names a type if we're following the `typename' keyword
12440      in a qualified name where the enclosing scope is type-dependent.  */
12441   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12442                 && dependent_type_p (scope));
12443   /* Handle the common case (an identifier, but not a template-id)
12444      efficiently.  */
12445   if (token->type == CPP_NAME
12446       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12447     {
12448       tree identifier;
12449
12450       /* Look for the identifier.  */
12451       identifier = cp_parser_identifier (parser);
12452       /* If the next token isn't an identifier, we are certainly not
12453          looking at a class-name.  */
12454       if (identifier == error_mark_node)
12455         decl = error_mark_node;
12456       /* If we know this is a type-name, there's no need to look it
12457          up.  */
12458       else if (typename_p)
12459         decl = identifier;
12460       else
12461         {
12462           /* If the next token is a `::', then the name must be a type
12463              name.
12464
12465              [basic.lookup.qual]
12466
12467              During the lookup for a name preceding the :: scope
12468              resolution operator, object, function, and enumerator
12469              names are ignored.  */
12470           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12471             tag_type = typename_type;
12472           /* Look up the name.  */
12473           decl = cp_parser_lookup_name (parser, identifier,
12474                                         tag_type,
12475                                         /*is_template=*/false,
12476                                         /*is_namespace=*/false,
12477                                         check_dependency_p,
12478                                         /*ambiguous_p=*/NULL);
12479         }
12480     }
12481   else
12482     {
12483       /* Try a template-id.  */
12484       decl = cp_parser_template_id (parser, template_keyword_p,
12485                                     check_dependency_p,
12486                                     is_declaration);
12487       if (decl == error_mark_node)
12488         return error_mark_node;
12489     }
12490
12491   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12492
12493   /* If this is a typename, create a TYPENAME_TYPE.  */
12494   if (typename_p && decl != error_mark_node)
12495     {
12496       decl = make_typename_type (scope, decl, typename_type, /*complain=*/1);
12497       if (decl != error_mark_node)
12498         decl = TYPE_NAME (decl);
12499     }
12500
12501   /* Check to see that it is really the name of a class.  */
12502   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12503       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12504       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12505     /* Situations like this:
12506
12507          template <typename T> struct A {
12508            typename T::template X<int>::I i;
12509          };
12510
12511        are problematic.  Is `T::template X<int>' a class-name?  The
12512        standard does not seem to be definitive, but there is no other
12513        valid interpretation of the following `::'.  Therefore, those
12514        names are considered class-names.  */
12515     decl = TYPE_NAME (make_typename_type (scope, decl, tag_type, tf_error));
12516   else if (decl == error_mark_node
12517            || TREE_CODE (decl) != TYPE_DECL
12518            || TREE_TYPE (decl) == error_mark_node
12519            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12520     {
12521       cp_parser_error (parser, "expected class-name");
12522       return error_mark_node;
12523     }
12524
12525   return decl;
12526 }
12527
12528 /* Parse a class-specifier.
12529
12530    class-specifier:
12531      class-head { member-specification [opt] }
12532
12533    Returns the TREE_TYPE representing the class.  */
12534
12535 static tree
12536 cp_parser_class_specifier (cp_parser* parser)
12537 {
12538   cp_token *token;
12539   tree type;
12540   tree attributes = NULL_TREE;
12541   int has_trailing_semicolon;
12542   bool nested_name_specifier_p;
12543   unsigned saved_num_template_parameter_lists;
12544   tree old_scope = NULL_TREE;
12545   tree scope = NULL_TREE;
12546
12547   push_deferring_access_checks (dk_no_deferred);
12548
12549   /* Parse the class-head.  */
12550   type = cp_parser_class_head (parser,
12551                                &nested_name_specifier_p,
12552                                &attributes);
12553   /* If the class-head was a semantic disaster, skip the entire body
12554      of the class.  */
12555   if (!type)
12556     {
12557       cp_parser_skip_to_end_of_block_or_statement (parser);
12558       pop_deferring_access_checks ();
12559       return error_mark_node;
12560     }
12561
12562   /* Look for the `{'.  */
12563   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12564     {
12565       pop_deferring_access_checks ();
12566       return error_mark_node;
12567     }
12568
12569   /* Issue an error message if type-definitions are forbidden here.  */
12570   cp_parser_check_type_definition (parser);
12571   /* Remember that we are defining one more class.  */
12572   ++parser->num_classes_being_defined;
12573   /* Inside the class, surrounding template-parameter-lists do not
12574      apply.  */
12575   saved_num_template_parameter_lists
12576     = parser->num_template_parameter_lists;
12577   parser->num_template_parameter_lists = 0;
12578
12579   /* Start the class.  */
12580   if (nested_name_specifier_p)
12581     {
12582       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12583       old_scope = push_inner_scope (scope);
12584     }
12585   type = begin_class_definition (type);
12586
12587   if (type == error_mark_node)
12588     /* If the type is erroneous, skip the entire body of the class.  */
12589     cp_parser_skip_to_closing_brace (parser);
12590   else
12591     /* Parse the member-specification.  */
12592     cp_parser_member_specification_opt (parser);
12593
12594   /* Look for the trailing `}'.  */
12595   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12596   /* We get better error messages by noticing a common problem: a
12597      missing trailing `;'.  */
12598   token = cp_lexer_peek_token (parser->lexer);
12599   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12600   /* Look for trailing attributes to apply to this class.  */
12601   if (cp_parser_allow_gnu_extensions_p (parser))
12602     {
12603       tree sub_attr = cp_parser_attributes_opt (parser);
12604       attributes = chainon (attributes, sub_attr);
12605     }
12606   if (type != error_mark_node)
12607     type = finish_struct (type, attributes);
12608   if (nested_name_specifier_p)
12609     pop_inner_scope (old_scope, scope);
12610   /* If this class is not itself within the scope of another class,
12611      then we need to parse the bodies of all of the queued function
12612      definitions.  Note that the queued functions defined in a class
12613      are not always processed immediately following the
12614      class-specifier for that class.  Consider:
12615
12616        struct A {
12617          struct B { void f() { sizeof (A); } };
12618        };
12619
12620      If `f' were processed before the processing of `A' were
12621      completed, there would be no way to compute the size of `A'.
12622      Note that the nesting we are interested in here is lexical --
12623      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12624      for:
12625
12626        struct A { struct B; };
12627        struct A::B { void f() { } };
12628
12629      there is no need to delay the parsing of `A::B::f'.  */
12630   if (--parser->num_classes_being_defined == 0)
12631     {
12632       tree queue_entry;
12633       tree fn;
12634       tree class_type = NULL_TREE;
12635       tree pushed_scope = NULL_TREE;
12636
12637       /* In a first pass, parse default arguments to the functions.
12638          Then, in a second pass, parse the bodies of the functions.
12639          This two-phased approach handles cases like:
12640
12641             struct S {
12642               void f() { g(); }
12643               void g(int i = 3);
12644             };
12645
12646          */
12647       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12648              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12649            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12650            TREE_PURPOSE (parser->unparsed_functions_queues)
12651              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12652         {
12653           fn = TREE_VALUE (queue_entry);
12654           /* If there are default arguments that have not yet been processed,
12655              take care of them now.  */
12656           if (class_type != TREE_PURPOSE (queue_entry))
12657             {
12658               if (pushed_scope)
12659                 pop_scope (pushed_scope);
12660               class_type = TREE_PURPOSE (queue_entry);
12661               pushed_scope = push_scope (class_type);
12662             }
12663           /* Make sure that any template parameters are in scope.  */
12664           maybe_begin_member_template_processing (fn);
12665           /* Parse the default argument expressions.  */
12666           cp_parser_late_parsing_default_args (parser, fn);
12667           /* Remove any template parameters from the symbol table.  */
12668           maybe_end_member_template_processing ();
12669         }
12670       if (pushed_scope)
12671         pop_scope (pushed_scope);
12672       /* Now parse the body of the functions.  */
12673       for (TREE_VALUE (parser->unparsed_functions_queues)
12674              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12675            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12676            TREE_VALUE (parser->unparsed_functions_queues)
12677              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12678         {
12679           /* Figure out which function we need to process.  */
12680           fn = TREE_VALUE (queue_entry);
12681
12682           /* A hack to prevent garbage collection.  */
12683           function_depth++;
12684
12685           /* Parse the function.  */
12686           cp_parser_late_parsing_for_member (parser, fn);
12687           function_depth--;
12688         }
12689     }
12690
12691   /* Put back any saved access checks.  */
12692   pop_deferring_access_checks ();
12693
12694   /* Restore the count of active template-parameter-lists.  */
12695   parser->num_template_parameter_lists
12696     = saved_num_template_parameter_lists;
12697
12698   return type;
12699 }
12700
12701 /* Parse a class-head.
12702
12703    class-head:
12704      class-key identifier [opt] base-clause [opt]
12705      class-key nested-name-specifier identifier base-clause [opt]
12706      class-key nested-name-specifier [opt] template-id
12707        base-clause [opt]
12708
12709    GNU Extensions:
12710      class-key attributes identifier [opt] base-clause [opt]
12711      class-key attributes nested-name-specifier identifier base-clause [opt]
12712      class-key attributes nested-name-specifier [opt] template-id
12713        base-clause [opt]
12714
12715    Returns the TYPE of the indicated class.  Sets
12716    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12717    involving a nested-name-specifier was used, and FALSE otherwise.
12718
12719    Returns error_mark_node if this is not a class-head.
12720    
12721    Returns NULL_TREE if the class-head is syntactically valid, but
12722    semantically invalid in a way that means we should skip the entire
12723    body of the class.  */
12724
12725 static tree
12726 cp_parser_class_head (cp_parser* parser,
12727                       bool* nested_name_specifier_p,
12728                       tree *attributes_p)
12729 {
12730   tree nested_name_specifier;
12731   enum tag_types class_key;
12732   tree id = NULL_TREE;
12733   tree type = NULL_TREE;
12734   tree attributes;
12735   bool template_id_p = false;
12736   bool qualified_p = false;
12737   bool invalid_nested_name_p = false;
12738   bool invalid_explicit_specialization_p = false;
12739   tree pushed_scope = NULL_TREE;
12740   unsigned num_templates;
12741   tree bases;
12742
12743   /* Assume no nested-name-specifier will be present.  */
12744   *nested_name_specifier_p = false;
12745   /* Assume no template parameter lists will be used in defining the
12746      type.  */
12747   num_templates = 0;
12748
12749   /* Look for the class-key.  */
12750   class_key = cp_parser_class_key (parser);
12751   if (class_key == none_type)
12752     return error_mark_node;
12753
12754   /* Parse the attributes.  */
12755   attributes = cp_parser_attributes_opt (parser);
12756
12757   /* If the next token is `::', that is invalid -- but sometimes
12758      people do try to write:
12759
12760        struct ::S {};
12761
12762      Handle this gracefully by accepting the extra qualifier, and then
12763      issuing an error about it later if this really is a
12764      class-head.  If it turns out just to be an elaborated type
12765      specifier, remain silent.  */
12766   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12767     qualified_p = true;
12768
12769   push_deferring_access_checks (dk_no_check);
12770
12771   /* Determine the name of the class.  Begin by looking for an
12772      optional nested-name-specifier.  */
12773   nested_name_specifier
12774     = cp_parser_nested_name_specifier_opt (parser,
12775                                            /*typename_keyword_p=*/false,
12776                                            /*check_dependency_p=*/false,
12777                                            /*type_p=*/false,
12778                                            /*is_declaration=*/false);
12779   /* If there was a nested-name-specifier, then there *must* be an
12780      identifier.  */
12781   if (nested_name_specifier)
12782     {
12783       /* Although the grammar says `identifier', it really means
12784          `class-name' or `template-name'.  You are only allowed to
12785          define a class that has already been declared with this
12786          syntax.
12787
12788          The proposed resolution for Core Issue 180 says that whever
12789          you see `class T::X' you should treat `X' as a type-name.
12790
12791          It is OK to define an inaccessible class; for example:
12792
12793            class A { class B; };
12794            class A::B {};
12795
12796          We do not know if we will see a class-name, or a
12797          template-name.  We look for a class-name first, in case the
12798          class-name is a template-id; if we looked for the
12799          template-name first we would stop after the template-name.  */
12800       cp_parser_parse_tentatively (parser);
12801       type = cp_parser_class_name (parser,
12802                                    /*typename_keyword_p=*/false,
12803                                    /*template_keyword_p=*/false,
12804                                    class_type,
12805                                    /*check_dependency_p=*/false,
12806                                    /*class_head_p=*/true,
12807                                    /*is_declaration=*/false);
12808       /* If that didn't work, ignore the nested-name-specifier.  */
12809       if (!cp_parser_parse_definitely (parser))
12810         {
12811           invalid_nested_name_p = true;
12812           id = cp_parser_identifier (parser);
12813           if (id == error_mark_node)
12814             id = NULL_TREE;
12815         }
12816       /* If we could not find a corresponding TYPE, treat this
12817          declaration like an unqualified declaration.  */
12818       if (type == error_mark_node)
12819         nested_name_specifier = NULL_TREE;
12820       /* Otherwise, count the number of templates used in TYPE and its
12821          containing scopes.  */
12822       else
12823         {
12824           tree scope;
12825
12826           for (scope = TREE_TYPE (type);
12827                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12828                scope = (TYPE_P (scope)
12829                         ? TYPE_CONTEXT (scope)
12830                         : DECL_CONTEXT (scope)))
12831             if (TYPE_P (scope)
12832                 && CLASS_TYPE_P (scope)
12833                 && CLASSTYPE_TEMPLATE_INFO (scope)
12834                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12835                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12836               ++num_templates;
12837         }
12838     }
12839   /* Otherwise, the identifier is optional.  */
12840   else
12841     {
12842       /* We don't know whether what comes next is a template-id,
12843          an identifier, or nothing at all.  */
12844       cp_parser_parse_tentatively (parser);
12845       /* Check for a template-id.  */
12846       id = cp_parser_template_id (parser,
12847                                   /*template_keyword_p=*/false,
12848                                   /*check_dependency_p=*/true,
12849                                   /*is_declaration=*/true);
12850       /* If that didn't work, it could still be an identifier.  */
12851       if (!cp_parser_parse_definitely (parser))
12852         {
12853           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12854             id = cp_parser_identifier (parser);
12855           else
12856             id = NULL_TREE;
12857         }
12858       else
12859         {
12860           template_id_p = true;
12861           ++num_templates;
12862         }
12863     }
12864
12865   pop_deferring_access_checks ();
12866
12867   if (id)
12868     cp_parser_check_for_invalid_template_id (parser, id);
12869
12870   /* If it's not a `:' or a `{' then we can't really be looking at a
12871      class-head, since a class-head only appears as part of a
12872      class-specifier.  We have to detect this situation before calling
12873      xref_tag, since that has irreversible side-effects.  */
12874   if (!cp_parser_next_token_starts_class_definition_p (parser))
12875     {
12876       cp_parser_error (parser, "expected %<{%> or %<:%>");
12877       return error_mark_node;
12878     }
12879
12880   /* At this point, we're going ahead with the class-specifier, even
12881      if some other problem occurs.  */
12882   cp_parser_commit_to_tentative_parse (parser);
12883   /* Issue the error about the overly-qualified name now.  */
12884   if (qualified_p)
12885     cp_parser_error (parser,
12886                      "global qualification of class name is invalid");
12887   else if (invalid_nested_name_p)
12888     cp_parser_error (parser,
12889                      "qualified name does not name a class");
12890   else if (nested_name_specifier)
12891     {
12892       tree scope;
12893
12894       /* Reject typedef-names in class heads.  */
12895       if (!DECL_IMPLICIT_TYPEDEF_P (type))
12896         {
12897           error ("invalid class name in declaration of %qD", type);
12898           type = NULL_TREE;
12899           goto done;
12900         }
12901
12902       /* Figure out in what scope the declaration is being placed.  */
12903       scope = current_scope ();
12904       /* If that scope does not contain the scope in which the
12905          class was originally declared, the program is invalid.  */
12906       if (scope && !is_ancestor (scope, nested_name_specifier))
12907         {
12908           error ("declaration of %qD in %qD which does not enclose %qD",
12909                  type, scope, nested_name_specifier);
12910           type = NULL_TREE;
12911           goto done;
12912         }
12913       /* [dcl.meaning]
12914
12915          A declarator-id shall not be qualified exception of the
12916          definition of a ... nested class outside of its class
12917          ... [or] a the definition or explicit instantiation of a
12918          class member of a namespace outside of its namespace.  */
12919       if (scope == nested_name_specifier)
12920         {
12921           pedwarn ("extra qualification ignored");
12922           nested_name_specifier = NULL_TREE;
12923           num_templates = 0;
12924         }
12925     }
12926   /* An explicit-specialization must be preceded by "template <>".  If
12927      it is not, try to recover gracefully.  */
12928   if (at_namespace_scope_p ()
12929       && parser->num_template_parameter_lists == 0
12930       && template_id_p)
12931     {
12932       error ("an explicit specialization must be preceded by %<template <>%>");
12933       invalid_explicit_specialization_p = true;
12934       /* Take the same action that would have been taken by
12935          cp_parser_explicit_specialization.  */
12936       ++parser->num_template_parameter_lists;
12937       begin_specialization ();
12938     }
12939   /* There must be no "return" statements between this point and the
12940      end of this function; set "type "to the correct return value and
12941      use "goto done;" to return.  */
12942   /* Make sure that the right number of template parameters were
12943      present.  */
12944   if (!cp_parser_check_template_parameters (parser, num_templates))
12945     {
12946       /* If something went wrong, there is no point in even trying to
12947          process the class-definition.  */
12948       type = NULL_TREE;
12949       goto done;
12950     }
12951
12952   /* Look up the type.  */
12953   if (template_id_p)
12954     {
12955       type = TREE_TYPE (id);
12956       maybe_process_partial_specialization (type);
12957       if (nested_name_specifier)
12958         pushed_scope = push_scope (nested_name_specifier);
12959     }
12960   else if (nested_name_specifier)
12961     {
12962       tree class_type;
12963
12964       /* Given:
12965
12966             template <typename T> struct S { struct T };
12967             template <typename T> struct S<T>::T { };
12968
12969          we will get a TYPENAME_TYPE when processing the definition of
12970          `S::T'.  We need to resolve it to the actual type before we
12971          try to define it.  */
12972       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12973         {
12974           class_type = resolve_typename_type (TREE_TYPE (type),
12975                                               /*only_current_p=*/false);
12976           if (class_type != error_mark_node)
12977             type = TYPE_NAME (class_type);
12978           else
12979             {
12980               cp_parser_error (parser, "could not resolve typename type");
12981               type = error_mark_node;
12982             }
12983         }
12984
12985       maybe_process_partial_specialization (TREE_TYPE (type));
12986       class_type = current_class_type;
12987       /* Enter the scope indicated by the nested-name-specifier.  */
12988       pushed_scope = push_scope (nested_name_specifier);
12989       /* Get the canonical version of this type.  */
12990       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12991       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12992           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12993         {
12994           type = push_template_decl (type);
12995           if (type == error_mark_node)
12996             {
12997               type = NULL_TREE;
12998               goto done;
12999             }
13000         }
13001       
13002       type = TREE_TYPE (type);
13003       *nested_name_specifier_p = true;
13004     }
13005   else      /* The name is not a nested name.  */
13006     {
13007       /* If the class was unnamed, create a dummy name.  */
13008       if (!id)
13009         id = make_anon_name ();
13010       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
13011                        parser->num_template_parameter_lists);
13012     }
13013
13014   /* Indicate whether this class was declared as a `class' or as a
13015      `struct'.  */
13016   if (TREE_CODE (type) == RECORD_TYPE)
13017     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
13018   cp_parser_check_class_key (class_key, type);
13019
13020   /* If this type was already complete, and we see another definition,
13021      that's an error.  */
13022   if (type != error_mark_node && COMPLETE_TYPE_P (type))
13023     {
13024       error ("redefinition of %q#T", type);
13025       cp_error_at ("previous definition of %q#T", type);
13026       type = NULL_TREE;
13027       goto done;
13028     }
13029
13030   /* We will have entered the scope containing the class; the names of
13031      base classes should be looked up in that context.  For example:
13032
13033        struct A { struct B {}; struct C; };
13034        struct A::C : B {};
13035
13036      is valid.  */
13037   bases = NULL_TREE;
13038
13039   /* Get the list of base-classes, if there is one.  */
13040   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13041     bases = cp_parser_base_clause (parser);
13042
13043   /* Process the base classes.  */
13044   xref_basetypes (type, bases);
13045
13046  done:
13047   /* Leave the scope given by the nested-name-specifier.  We will
13048      enter the class scope itself while processing the members.  */
13049   if (pushed_scope)
13050     pop_scope (pushed_scope);
13051
13052   if (invalid_explicit_specialization_p)
13053     {
13054       end_specialization ();
13055       --parser->num_template_parameter_lists;
13056     }
13057   *attributes_p = attributes;
13058   return type;
13059 }
13060
13061 /* Parse a class-key.
13062
13063    class-key:
13064      class
13065      struct
13066      union
13067
13068    Returns the kind of class-key specified, or none_type to indicate
13069    error.  */
13070
13071 static enum tag_types
13072 cp_parser_class_key (cp_parser* parser)
13073 {
13074   cp_token *token;
13075   enum tag_types tag_type;
13076
13077   /* Look for the class-key.  */
13078   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
13079   if (!token)
13080     return none_type;
13081
13082   /* Check to see if the TOKEN is a class-key.  */
13083   tag_type = cp_parser_token_is_class_key (token);
13084   if (!tag_type)
13085     cp_parser_error (parser, "expected class-key");
13086   return tag_type;
13087 }
13088
13089 /* Parse an (optional) member-specification.
13090
13091    member-specification:
13092      member-declaration member-specification [opt]
13093      access-specifier : member-specification [opt]  */
13094
13095 static void
13096 cp_parser_member_specification_opt (cp_parser* parser)
13097 {
13098   while (true)
13099     {
13100       cp_token *token;
13101       enum rid keyword;
13102
13103       /* Peek at the next token.  */
13104       token = cp_lexer_peek_token (parser->lexer);
13105       /* If it's a `}', or EOF then we've seen all the members.  */
13106       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
13107         break;
13108
13109       /* See if this token is a keyword.  */
13110       keyword = token->keyword;
13111       switch (keyword)
13112         {
13113         case RID_PUBLIC:
13114         case RID_PROTECTED:
13115         case RID_PRIVATE:
13116           /* Consume the access-specifier.  */
13117           cp_lexer_consume_token (parser->lexer);
13118           /* Remember which access-specifier is active.  */
13119           current_access_specifier = token->value;
13120           /* Look for the `:'.  */
13121           cp_parser_require (parser, CPP_COLON, "`:'");
13122           break;
13123
13124         default:
13125           /* Accept #pragmas at class scope.  */
13126           if (token->type == CPP_PRAGMA)
13127             {
13128               cp_lexer_handle_pragma (parser->lexer);
13129               break;
13130             }
13131
13132           /* Otherwise, the next construction must be a
13133              member-declaration.  */
13134           cp_parser_member_declaration (parser);
13135         }
13136     }
13137 }
13138
13139 /* Parse a member-declaration.
13140
13141    member-declaration:
13142      decl-specifier-seq [opt] member-declarator-list [opt] ;
13143      function-definition ; [opt]
13144      :: [opt] nested-name-specifier template [opt] unqualified-id ;
13145      using-declaration
13146      template-declaration
13147
13148    member-declarator-list:
13149      member-declarator
13150      member-declarator-list , member-declarator
13151
13152    member-declarator:
13153      declarator pure-specifier [opt]
13154      declarator constant-initializer [opt]
13155      identifier [opt] : constant-expression
13156
13157    GNU Extensions:
13158
13159    member-declaration:
13160      __extension__ member-declaration
13161
13162    member-declarator:
13163      declarator attributes [opt] pure-specifier [opt]
13164      declarator attributes [opt] constant-initializer [opt]
13165      identifier [opt] attributes [opt] : constant-expression  */
13166
13167 static void
13168 cp_parser_member_declaration (cp_parser* parser)
13169 {
13170   cp_decl_specifier_seq decl_specifiers;
13171   tree prefix_attributes;
13172   tree decl;
13173   int declares_class_or_enum;
13174   bool friend_p;
13175   cp_token *token;
13176   int saved_pedantic;
13177
13178   /* Check for the `__extension__' keyword.  */
13179   if (cp_parser_extension_opt (parser, &saved_pedantic))
13180     {
13181       /* Recurse.  */
13182       cp_parser_member_declaration (parser);
13183       /* Restore the old value of the PEDANTIC flag.  */
13184       pedantic = saved_pedantic;
13185
13186       return;
13187     }
13188
13189   /* Check for a template-declaration.  */
13190   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13191     {
13192       /* Parse the template-declaration.  */
13193       cp_parser_template_declaration (parser, /*member_p=*/true);
13194
13195       return;
13196     }
13197
13198   /* Check for a using-declaration.  */
13199   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
13200     {
13201       /* Parse the using-declaration.  */
13202       cp_parser_using_declaration (parser);
13203
13204       return;
13205     }
13206
13207   /* Check for @defs.  */
13208   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
13209     {
13210       tree ivar, member;
13211       tree ivar_chains = cp_parser_objc_defs_expression (parser);
13212       ivar = ivar_chains;
13213       while (ivar)
13214         {
13215           member = ivar;
13216           ivar = TREE_CHAIN (member);
13217           TREE_CHAIN (member) = NULL_TREE;
13218           finish_member_declaration (member);
13219         }
13220       return;
13221     }
13222
13223   /* Parse the decl-specifier-seq.  */
13224   cp_parser_decl_specifier_seq (parser,
13225                                 CP_PARSER_FLAGS_OPTIONAL,
13226                                 &decl_specifiers,
13227                                 &declares_class_or_enum);
13228   prefix_attributes = decl_specifiers.attributes;
13229   decl_specifiers.attributes = NULL_TREE;
13230   /* Check for an invalid type-name.  */
13231   if (!decl_specifiers.type
13232       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
13233     return;
13234   /* If there is no declarator, then the decl-specifier-seq should
13235      specify a type.  */
13236   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13237     {
13238       /* If there was no decl-specifier-seq, and the next token is a
13239          `;', then we have something like:
13240
13241            struct S { ; };
13242
13243          [class.mem]
13244
13245          Each member-declaration shall declare at least one member
13246          name of the class.  */
13247       if (!decl_specifiers.any_specifiers_p)
13248         {
13249           cp_token *token = cp_lexer_peek_token (parser->lexer);
13250           if (pedantic && !token->in_system_header)
13251             pedwarn ("%Hextra %<;%>", &token->location);
13252         }
13253       else
13254         {
13255           tree type;
13256
13257           /* See if this declaration is a friend.  */
13258           friend_p = cp_parser_friend_p (&decl_specifiers);
13259           /* If there were decl-specifiers, check to see if there was
13260              a class-declaration.  */
13261           type = check_tag_decl (&decl_specifiers);
13262           /* Nested classes have already been added to the class, but
13263              a `friend' needs to be explicitly registered.  */
13264           if (friend_p)
13265             {
13266               /* If the `friend' keyword was present, the friend must
13267                  be introduced with a class-key.  */
13268                if (!declares_class_or_enum)
13269                  error ("a class-key must be used when declaring a friend");
13270                /* In this case:
13271
13272                     template <typename T> struct A {
13273                       friend struct A<T>::B;
13274                     };
13275
13276                   A<T>::B will be represented by a TYPENAME_TYPE, and
13277                   therefore not recognized by check_tag_decl.  */
13278                if (!type
13279                    && decl_specifiers.type
13280                    && TYPE_P (decl_specifiers.type))
13281                  type = decl_specifiers.type;
13282                if (!type || !TYPE_P (type))
13283                  error ("friend declaration does not name a class or "
13284                         "function");
13285                else
13286                  make_friend_class (current_class_type, type,
13287                                     /*complain=*/true);
13288             }
13289           /* If there is no TYPE, an error message will already have
13290              been issued.  */
13291           else if (!type || type == error_mark_node)
13292             ;
13293           /* An anonymous aggregate has to be handled specially; such
13294              a declaration really declares a data member (with a
13295              particular type), as opposed to a nested class.  */
13296           else if (ANON_AGGR_TYPE_P (type))
13297             {
13298               /* Remove constructors and such from TYPE, now that we
13299                  know it is an anonymous aggregate.  */
13300               fixup_anonymous_aggr (type);
13301               /* And make the corresponding data member.  */
13302               decl = build_decl (FIELD_DECL, NULL_TREE, type);
13303               /* Add it to the class.  */
13304               finish_member_declaration (decl);
13305             }
13306           else
13307             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
13308         }
13309     }
13310   else
13311     {
13312       /* See if these declarations will be friends.  */
13313       friend_p = cp_parser_friend_p (&decl_specifiers);
13314
13315       /* Keep going until we hit the `;' at the end of the
13316          declaration.  */
13317       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13318         {
13319           tree attributes = NULL_TREE;
13320           tree first_attribute;
13321
13322           /* Peek at the next token.  */
13323           token = cp_lexer_peek_token (parser->lexer);
13324
13325           /* Check for a bitfield declaration.  */
13326           if (token->type == CPP_COLON
13327               || (token->type == CPP_NAME
13328                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
13329                   == CPP_COLON))
13330             {
13331               tree identifier;
13332               tree width;
13333
13334               /* Get the name of the bitfield.  Note that we cannot just
13335                  check TOKEN here because it may have been invalidated by
13336                  the call to cp_lexer_peek_nth_token above.  */
13337               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
13338                 identifier = cp_parser_identifier (parser);
13339               else
13340                 identifier = NULL_TREE;
13341
13342               /* Consume the `:' token.  */
13343               cp_lexer_consume_token (parser->lexer);
13344               /* Get the width of the bitfield.  */
13345               width
13346                 = cp_parser_constant_expression (parser,
13347                                                  /*allow_non_constant=*/false,
13348                                                  NULL);
13349
13350               /* Look for attributes that apply to the bitfield.  */
13351               attributes = cp_parser_attributes_opt (parser);
13352               /* Remember which attributes are prefix attributes and
13353                  which are not.  */
13354               first_attribute = attributes;
13355               /* Combine the attributes.  */
13356               attributes = chainon (prefix_attributes, attributes);
13357
13358               /* Create the bitfield declaration.  */
13359               decl = grokbitfield (identifier
13360                                    ? make_id_declarator (NULL_TREE,
13361                                                          identifier)
13362                                    : NULL,
13363                                    &decl_specifiers,
13364                                    width);
13365               /* Apply the attributes.  */
13366               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13367             }
13368           else
13369             {
13370               cp_declarator *declarator;
13371               tree initializer;
13372               tree asm_specification;
13373               int ctor_dtor_or_conv_p;
13374
13375               /* Parse the declarator.  */
13376               declarator
13377                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13378                                         &ctor_dtor_or_conv_p,
13379                                         /*parenthesized_p=*/NULL,
13380                                         /*member_p=*/true);
13381
13382               /* If something went wrong parsing the declarator, make sure
13383                  that we at least consume some tokens.  */
13384               if (declarator == cp_error_declarator)
13385                 {
13386                   /* Skip to the end of the statement.  */
13387                   cp_parser_skip_to_end_of_statement (parser);
13388                   /* If the next token is not a semicolon, that is
13389                      probably because we just skipped over the body of
13390                      a function.  So, we consume a semicolon if
13391                      present, but do not issue an error message if it
13392                      is not present.  */
13393                   if (cp_lexer_next_token_is (parser->lexer,
13394                                               CPP_SEMICOLON))
13395                     cp_lexer_consume_token (parser->lexer);
13396                   return;
13397                 }
13398
13399               if (declares_class_or_enum & 2)
13400                 cp_parser_check_for_definition_in_return_type
13401                   (declarator, decl_specifiers.type);
13402
13403               /* Look for an asm-specification.  */
13404               asm_specification = cp_parser_asm_specification_opt (parser);
13405               /* Look for attributes that apply to the declaration.  */
13406               attributes = cp_parser_attributes_opt (parser);
13407               /* Remember which attributes are prefix attributes and
13408                  which are not.  */
13409               first_attribute = attributes;
13410               /* Combine the attributes.  */
13411               attributes = chainon (prefix_attributes, attributes);
13412
13413               /* If it's an `=', then we have a constant-initializer or a
13414                  pure-specifier.  It is not correct to parse the
13415                  initializer before registering the member declaration
13416                  since the member declaration should be in scope while
13417                  its initializer is processed.  However, the rest of the
13418                  front end does not yet provide an interface that allows
13419                  us to handle this correctly.  */
13420               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13421                 {
13422                   /* In [class.mem]:
13423
13424                      A pure-specifier shall be used only in the declaration of
13425                      a virtual function.
13426
13427                      A member-declarator can contain a constant-initializer
13428                      only if it declares a static member of integral or
13429                      enumeration type.
13430
13431                      Therefore, if the DECLARATOR is for a function, we look
13432                      for a pure-specifier; otherwise, we look for a
13433                      constant-initializer.  When we call `grokfield', it will
13434                      perform more stringent semantics checks.  */
13435                   if (declarator->kind == cdk_function)
13436                     initializer = cp_parser_pure_specifier (parser);
13437                   else
13438                     /* Parse the initializer.  */
13439                     initializer = cp_parser_constant_initializer (parser);
13440                 }
13441               /* Otherwise, there is no initializer.  */
13442               else
13443                 initializer = NULL_TREE;
13444
13445               /* See if we are probably looking at a function
13446                  definition.  We are certainly not looking at a
13447                  member-declarator.  Calling `grokfield' has
13448                  side-effects, so we must not do it unless we are sure
13449                  that we are looking at a member-declarator.  */
13450               if (cp_parser_token_starts_function_definition_p
13451                   (cp_lexer_peek_token (parser->lexer)))
13452                 {
13453                   /* The grammar does not allow a pure-specifier to be
13454                      used when a member function is defined.  (It is
13455                      possible that this fact is an oversight in the
13456                      standard, since a pure function may be defined
13457                      outside of the class-specifier.  */
13458                   if (initializer)
13459                     error ("pure-specifier on function-definition");
13460                   decl = cp_parser_save_member_function_body (parser,
13461                                                               &decl_specifiers,
13462                                                               declarator,
13463                                                               attributes);
13464                   /* If the member was not a friend, declare it here.  */
13465                   if (!friend_p)
13466                     finish_member_declaration (decl);
13467                   /* Peek at the next token.  */
13468                   token = cp_lexer_peek_token (parser->lexer);
13469                   /* If the next token is a semicolon, consume it.  */
13470                   if (token->type == CPP_SEMICOLON)
13471                     cp_lexer_consume_token (parser->lexer);
13472                   return;
13473                 }
13474               else
13475                 {
13476                   /* Create the declaration.  */
13477                   decl = grokfield (declarator, &decl_specifiers,
13478                                     initializer, asm_specification,
13479                                     attributes);
13480                   /* Any initialization must have been from a
13481                      constant-expression.  */
13482                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13483                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13484                 }
13485             }
13486
13487           /* Reset PREFIX_ATTRIBUTES.  */
13488           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13489             attributes = TREE_CHAIN (attributes);
13490           if (attributes)
13491             TREE_CHAIN (attributes) = NULL_TREE;
13492
13493           /* If there is any qualification still in effect, clear it
13494              now; we will be starting fresh with the next declarator.  */
13495           parser->scope = NULL_TREE;
13496           parser->qualifying_scope = NULL_TREE;
13497           parser->object_scope = NULL_TREE;
13498           /* If it's a `,', then there are more declarators.  */
13499           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13500             cp_lexer_consume_token (parser->lexer);
13501           /* If the next token isn't a `;', then we have a parse error.  */
13502           else if (cp_lexer_next_token_is_not (parser->lexer,
13503                                                CPP_SEMICOLON))
13504             {
13505               cp_parser_error (parser, "expected %<;%>");
13506               /* Skip tokens until we find a `;'.  */
13507               cp_parser_skip_to_end_of_statement (parser);
13508
13509               break;
13510             }
13511
13512           if (decl)
13513             {
13514               /* Add DECL to the list of members.  */
13515               if (!friend_p)
13516                 finish_member_declaration (decl);
13517
13518               if (TREE_CODE (decl) == FUNCTION_DECL)
13519                 cp_parser_save_default_args (parser, decl);
13520             }
13521         }
13522     }
13523
13524   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13525 }
13526
13527 /* Parse a pure-specifier.
13528
13529    pure-specifier:
13530      = 0
13531
13532    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13533    Otherwise, ERROR_MARK_NODE is returned.  */
13534
13535 static tree
13536 cp_parser_pure_specifier (cp_parser* parser)
13537 {
13538   cp_token *token;
13539
13540   /* Look for the `=' token.  */
13541   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13542     return error_mark_node;
13543   /* Look for the `0' token.  */
13544   token = cp_lexer_consume_token (parser->lexer);
13545   if (token->type != CPP_NUMBER || !integer_zerop (token->value))
13546     {
13547       cp_parser_error (parser,
13548                        "invalid pure specifier (only `= 0' is allowed)");
13549       cp_parser_skip_to_end_of_statement (parser);
13550       return error_mark_node;
13551     }
13552
13553   /* FIXME: Unfortunately, this will accept `0L' and `0x00' as well.
13554      We need to get information from the lexer about how the number
13555      was spelled in order to fix this problem.  */
13556   return integer_zero_node;
13557 }
13558
13559 /* Parse a constant-initializer.
13560
13561    constant-initializer:
13562      = constant-expression
13563
13564    Returns a representation of the constant-expression.  */
13565
13566 static tree
13567 cp_parser_constant_initializer (cp_parser* parser)
13568 {
13569   /* Look for the `=' token.  */
13570   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13571     return error_mark_node;
13572
13573   /* It is invalid to write:
13574
13575        struct S { static const int i = { 7 }; };
13576
13577      */
13578   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13579     {
13580       cp_parser_error (parser,
13581                        "a brace-enclosed initializer is not allowed here");
13582       /* Consume the opening brace.  */
13583       cp_lexer_consume_token (parser->lexer);
13584       /* Skip the initializer.  */
13585       cp_parser_skip_to_closing_brace (parser);
13586       /* Look for the trailing `}'.  */
13587       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13588
13589       return error_mark_node;
13590     }
13591
13592   return cp_parser_constant_expression (parser,
13593                                         /*allow_non_constant=*/false,
13594                                         NULL);
13595 }
13596
13597 /* Derived classes [gram.class.derived] */
13598
13599 /* Parse a base-clause.
13600
13601    base-clause:
13602      : base-specifier-list
13603
13604    base-specifier-list:
13605      base-specifier
13606      base-specifier-list , base-specifier
13607
13608    Returns a TREE_LIST representing the base-classes, in the order in
13609    which they were declared.  The representation of each node is as
13610    described by cp_parser_base_specifier.
13611
13612    In the case that no bases are specified, this function will return
13613    NULL_TREE, not ERROR_MARK_NODE.  */
13614
13615 static tree
13616 cp_parser_base_clause (cp_parser* parser)
13617 {
13618   tree bases = NULL_TREE;
13619
13620   /* Look for the `:' that begins the list.  */
13621   cp_parser_require (parser, CPP_COLON, "`:'");
13622
13623   /* Scan the base-specifier-list.  */
13624   while (true)
13625     {
13626       cp_token *token;
13627       tree base;
13628
13629       /* Look for the base-specifier.  */
13630       base = cp_parser_base_specifier (parser);
13631       /* Add BASE to the front of the list.  */
13632       if (base != error_mark_node)
13633         {
13634           TREE_CHAIN (base) = bases;
13635           bases = base;
13636         }
13637       /* Peek at the next token.  */
13638       token = cp_lexer_peek_token (parser->lexer);
13639       /* If it's not a comma, then the list is complete.  */
13640       if (token->type != CPP_COMMA)
13641         break;
13642       /* Consume the `,'.  */
13643       cp_lexer_consume_token (parser->lexer);
13644     }
13645
13646   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13647      base class had a qualified name.  However, the next name that
13648      appears is certainly not qualified.  */
13649   parser->scope = NULL_TREE;
13650   parser->qualifying_scope = NULL_TREE;
13651   parser->object_scope = NULL_TREE;
13652
13653   return nreverse (bases);
13654 }
13655
13656 /* Parse a base-specifier.
13657
13658    base-specifier:
13659      :: [opt] nested-name-specifier [opt] class-name
13660      virtual access-specifier [opt] :: [opt] nested-name-specifier
13661        [opt] class-name
13662      access-specifier virtual [opt] :: [opt] nested-name-specifier
13663        [opt] class-name
13664
13665    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13666    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13667    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13668    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13669
13670 static tree
13671 cp_parser_base_specifier (cp_parser* parser)
13672 {
13673   cp_token *token;
13674   bool done = false;
13675   bool virtual_p = false;
13676   bool duplicate_virtual_error_issued_p = false;
13677   bool duplicate_access_error_issued_p = false;
13678   bool class_scope_p, template_p;
13679   tree access = access_default_node;
13680   tree type;
13681
13682   /* Process the optional `virtual' and `access-specifier'.  */
13683   while (!done)
13684     {
13685       /* Peek at the next token.  */
13686       token = cp_lexer_peek_token (parser->lexer);
13687       /* Process `virtual'.  */
13688       switch (token->keyword)
13689         {
13690         case RID_VIRTUAL:
13691           /* If `virtual' appears more than once, issue an error.  */
13692           if (virtual_p && !duplicate_virtual_error_issued_p)
13693             {
13694               cp_parser_error (parser,
13695                                "%<virtual%> specified more than once in base-specified");
13696               duplicate_virtual_error_issued_p = true;
13697             }
13698
13699           virtual_p = true;
13700
13701           /* Consume the `virtual' token.  */
13702           cp_lexer_consume_token (parser->lexer);
13703
13704           break;
13705
13706         case RID_PUBLIC:
13707         case RID_PROTECTED:
13708         case RID_PRIVATE:
13709           /* If more than one access specifier appears, issue an
13710              error.  */
13711           if (access != access_default_node
13712               && !duplicate_access_error_issued_p)
13713             {
13714               cp_parser_error (parser,
13715                                "more than one access specifier in base-specified");
13716               duplicate_access_error_issued_p = true;
13717             }
13718
13719           access = ridpointers[(int) token->keyword];
13720
13721           /* Consume the access-specifier.  */
13722           cp_lexer_consume_token (parser->lexer);
13723
13724           break;
13725
13726         default:
13727           done = true;
13728           break;
13729         }
13730     }
13731   /* It is not uncommon to see programs mechanically, erroneously, use
13732      the 'typename' keyword to denote (dependent) qualified types
13733      as base classes.  */
13734   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13735     {
13736       if (!processing_template_decl)
13737         error ("keyword %<typename%> not allowed outside of templates");
13738       else
13739         error ("keyword %<typename%> not allowed in this context "
13740                "(the base class is implicitly a type)");
13741       cp_lexer_consume_token (parser->lexer);
13742     }
13743
13744   /* Look for the optional `::' operator.  */
13745   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13746   /* Look for the nested-name-specifier.  The simplest way to
13747      implement:
13748
13749        [temp.res]
13750
13751        The keyword `typename' is not permitted in a base-specifier or
13752        mem-initializer; in these contexts a qualified name that
13753        depends on a template-parameter is implicitly assumed to be a
13754        type name.
13755
13756      is to pretend that we have seen the `typename' keyword at this
13757      point.  */
13758   cp_parser_nested_name_specifier_opt (parser,
13759                                        /*typename_keyword_p=*/true,
13760                                        /*check_dependency_p=*/true,
13761                                        typename_type,
13762                                        /*is_declaration=*/true);
13763   /* If the base class is given by a qualified name, assume that names
13764      we see are type names or templates, as appropriate.  */
13765   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13766   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13767
13768   /* Finally, look for the class-name.  */
13769   type = cp_parser_class_name (parser,
13770                                class_scope_p,
13771                                template_p,
13772                                typename_type,
13773                                /*check_dependency_p=*/true,
13774                                /*class_head_p=*/false,
13775                                /*is_declaration=*/true);
13776
13777   if (type == error_mark_node)
13778     return error_mark_node;
13779
13780   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13781 }
13782
13783 /* Exception handling [gram.exception] */
13784
13785 /* Parse an (optional) exception-specification.
13786
13787    exception-specification:
13788      throw ( type-id-list [opt] )
13789
13790    Returns a TREE_LIST representing the exception-specification.  The
13791    TREE_VALUE of each node is a type.  */
13792
13793 static tree
13794 cp_parser_exception_specification_opt (cp_parser* parser)
13795 {
13796   cp_token *token;
13797   tree type_id_list;
13798
13799   /* Peek at the next token.  */
13800   token = cp_lexer_peek_token (parser->lexer);
13801   /* If it's not `throw', then there's no exception-specification.  */
13802   if (!cp_parser_is_keyword (token, RID_THROW))
13803     return NULL_TREE;
13804
13805   /* Consume the `throw'.  */
13806   cp_lexer_consume_token (parser->lexer);
13807
13808   /* Look for the `('.  */
13809   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13810
13811   /* Peek at the next token.  */
13812   token = cp_lexer_peek_token (parser->lexer);
13813   /* If it's not a `)', then there is a type-id-list.  */
13814   if (token->type != CPP_CLOSE_PAREN)
13815     {
13816       const char *saved_message;
13817
13818       /* Types may not be defined in an exception-specification.  */
13819       saved_message = parser->type_definition_forbidden_message;
13820       parser->type_definition_forbidden_message
13821         = "types may not be defined in an exception-specification";
13822       /* Parse the type-id-list.  */
13823       type_id_list = cp_parser_type_id_list (parser);
13824       /* Restore the saved message.  */
13825       parser->type_definition_forbidden_message = saved_message;
13826     }
13827   else
13828     type_id_list = empty_except_spec;
13829
13830   /* Look for the `)'.  */
13831   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13832
13833   return type_id_list;
13834 }
13835
13836 /* Parse an (optional) type-id-list.
13837
13838    type-id-list:
13839      type-id
13840      type-id-list , type-id
13841
13842    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13843    in the order that the types were presented.  */
13844
13845 static tree
13846 cp_parser_type_id_list (cp_parser* parser)
13847 {
13848   tree types = NULL_TREE;
13849
13850   while (true)
13851     {
13852       cp_token *token;
13853       tree type;
13854
13855       /* Get the next type-id.  */
13856       type = cp_parser_type_id (parser);
13857       /* Add it to the list.  */
13858       types = add_exception_specifier (types, type, /*complain=*/1);
13859       /* Peek at the next token.  */
13860       token = cp_lexer_peek_token (parser->lexer);
13861       /* If it is not a `,', we are done.  */
13862       if (token->type != CPP_COMMA)
13863         break;
13864       /* Consume the `,'.  */
13865       cp_lexer_consume_token (parser->lexer);
13866     }
13867
13868   return nreverse (types);
13869 }
13870
13871 /* Parse a try-block.
13872
13873    try-block:
13874      try compound-statement handler-seq  */
13875
13876 static tree
13877 cp_parser_try_block (cp_parser* parser)
13878 {
13879   tree try_block;
13880
13881   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13882   try_block = begin_try_block ();
13883   cp_parser_compound_statement (parser, NULL, true);
13884   finish_try_block (try_block);
13885   cp_parser_handler_seq (parser);
13886   finish_handler_sequence (try_block);
13887
13888   return try_block;
13889 }
13890
13891 /* Parse a function-try-block.
13892
13893    function-try-block:
13894      try ctor-initializer [opt] function-body handler-seq  */
13895
13896 static bool
13897 cp_parser_function_try_block (cp_parser* parser)
13898 {
13899   tree try_block;
13900   bool ctor_initializer_p;
13901
13902   /* Look for the `try' keyword.  */
13903   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13904     return false;
13905   /* Let the rest of the front-end know where we are.  */
13906   try_block = begin_function_try_block ();
13907   /* Parse the function-body.  */
13908   ctor_initializer_p
13909     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13910   /* We're done with the `try' part.  */
13911   finish_function_try_block (try_block);
13912   /* Parse the handlers.  */
13913   cp_parser_handler_seq (parser);
13914   /* We're done with the handlers.  */
13915   finish_function_handler_sequence (try_block);
13916
13917   return ctor_initializer_p;
13918 }
13919
13920 /* Parse a handler-seq.
13921
13922    handler-seq:
13923      handler handler-seq [opt]  */
13924
13925 static void
13926 cp_parser_handler_seq (cp_parser* parser)
13927 {
13928   while (true)
13929     {
13930       cp_token *token;
13931
13932       /* Parse the handler.  */
13933       cp_parser_handler (parser);
13934       /* Peek at the next token.  */
13935       token = cp_lexer_peek_token (parser->lexer);
13936       /* If it's not `catch' then there are no more handlers.  */
13937       if (!cp_parser_is_keyword (token, RID_CATCH))
13938         break;
13939     }
13940 }
13941
13942 /* Parse a handler.
13943
13944    handler:
13945      catch ( exception-declaration ) compound-statement  */
13946
13947 static void
13948 cp_parser_handler (cp_parser* parser)
13949 {
13950   tree handler;
13951   tree declaration;
13952
13953   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13954   handler = begin_handler ();
13955   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13956   declaration = cp_parser_exception_declaration (parser);
13957   finish_handler_parms (declaration, handler);
13958   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13959   cp_parser_compound_statement (parser, NULL, false);
13960   finish_handler (handler);
13961 }
13962
13963 /* Parse an exception-declaration.
13964
13965    exception-declaration:
13966      type-specifier-seq declarator
13967      type-specifier-seq abstract-declarator
13968      type-specifier-seq
13969      ...
13970
13971    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13972    ellipsis variant is used.  */
13973
13974 static tree
13975 cp_parser_exception_declaration (cp_parser* parser)
13976 {
13977   tree decl;
13978   cp_decl_specifier_seq type_specifiers;
13979   cp_declarator *declarator;
13980   const char *saved_message;
13981
13982   /* If it's an ellipsis, it's easy to handle.  */
13983   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13984     {
13985       /* Consume the `...' token.  */
13986       cp_lexer_consume_token (parser->lexer);
13987       return NULL_TREE;
13988     }
13989
13990   /* Types may not be defined in exception-declarations.  */
13991   saved_message = parser->type_definition_forbidden_message;
13992   parser->type_definition_forbidden_message
13993     = "types may not be defined in exception-declarations";
13994
13995   /* Parse the type-specifier-seq.  */
13996   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
13997                                 &type_specifiers);
13998   /* If it's a `)', then there is no declarator.  */
13999   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
14000     declarator = NULL;
14001   else
14002     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
14003                                        /*ctor_dtor_or_conv_p=*/NULL,
14004                                        /*parenthesized_p=*/NULL,
14005                                        /*member_p=*/false);
14006
14007   /* Restore the saved message.  */
14008   parser->type_definition_forbidden_message = saved_message;
14009
14010   if (type_specifiers.any_specifiers_p)
14011     {
14012       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
14013       if (decl == NULL_TREE)
14014         error ("invalid catch parameter");
14015     }
14016   else
14017     decl = NULL_TREE;
14018
14019   return decl;
14020 }
14021
14022 /* Parse a throw-expression.
14023
14024    throw-expression:
14025      throw assignment-expression [opt]
14026
14027    Returns a THROW_EXPR representing the throw-expression.  */
14028
14029 static tree
14030 cp_parser_throw_expression (cp_parser* parser)
14031 {
14032   tree expression;
14033   cp_token* token;
14034
14035   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
14036   token = cp_lexer_peek_token (parser->lexer);
14037   /* Figure out whether or not there is an assignment-expression
14038      following the "throw" keyword.  */
14039   if (token->type == CPP_COMMA
14040       || token->type == CPP_SEMICOLON
14041       || token->type == CPP_CLOSE_PAREN
14042       || token->type == CPP_CLOSE_SQUARE
14043       || token->type == CPP_CLOSE_BRACE
14044       || token->type == CPP_COLON)
14045     expression = NULL_TREE;
14046   else
14047     expression = cp_parser_assignment_expression (parser,
14048                                                   /*cast_p=*/false);
14049
14050   return build_throw (expression);
14051 }
14052
14053 /* GNU Extensions */
14054
14055 /* Parse an (optional) asm-specification.
14056
14057    asm-specification:
14058      asm ( string-literal )
14059
14060    If the asm-specification is present, returns a STRING_CST
14061    corresponding to the string-literal.  Otherwise, returns
14062    NULL_TREE.  */
14063
14064 static tree
14065 cp_parser_asm_specification_opt (cp_parser* parser)
14066 {
14067   cp_token *token;
14068   tree asm_specification;
14069
14070   /* Peek at the next token.  */
14071   token = cp_lexer_peek_token (parser->lexer);
14072   /* If the next token isn't the `asm' keyword, then there's no
14073      asm-specification.  */
14074   if (!cp_parser_is_keyword (token, RID_ASM))
14075     return NULL_TREE;
14076
14077   /* Consume the `asm' token.  */
14078   cp_lexer_consume_token (parser->lexer);
14079   /* Look for the `('.  */
14080   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14081
14082   /* Look for the string-literal.  */
14083   asm_specification = cp_parser_string_literal (parser, false, false);
14084
14085   /* Look for the `)'.  */
14086   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
14087
14088   return asm_specification;
14089 }
14090
14091 /* Parse an asm-operand-list.
14092
14093    asm-operand-list:
14094      asm-operand
14095      asm-operand-list , asm-operand
14096
14097    asm-operand:
14098      string-literal ( expression )
14099      [ string-literal ] string-literal ( expression )
14100
14101    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
14102    each node is the expression.  The TREE_PURPOSE is itself a
14103    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
14104    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
14105    is a STRING_CST for the string literal before the parenthesis.  */
14106
14107 static tree
14108 cp_parser_asm_operand_list (cp_parser* parser)
14109 {
14110   tree asm_operands = NULL_TREE;
14111
14112   while (true)
14113     {
14114       tree string_literal;
14115       tree expression;
14116       tree name;
14117
14118       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
14119         {
14120           /* Consume the `[' token.  */
14121           cp_lexer_consume_token (parser->lexer);
14122           /* Read the operand name.  */
14123           name = cp_parser_identifier (parser);
14124           if (name != error_mark_node)
14125             name = build_string (IDENTIFIER_LENGTH (name),
14126                                  IDENTIFIER_POINTER (name));
14127           /* Look for the closing `]'.  */
14128           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
14129         }
14130       else
14131         name = NULL_TREE;
14132       /* Look for the string-literal.  */
14133       string_literal = cp_parser_string_literal (parser, false, false);
14134
14135       /* Look for the `('.  */
14136       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14137       /* Parse the expression.  */
14138       expression = cp_parser_expression (parser, /*cast_p=*/false);
14139       /* Look for the `)'.  */
14140       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14141
14142       /* Add this operand to the list.  */
14143       asm_operands = tree_cons (build_tree_list (name, string_literal),
14144                                 expression,
14145                                 asm_operands);
14146       /* If the next token is not a `,', there are no more
14147          operands.  */
14148       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14149         break;
14150       /* Consume the `,'.  */
14151       cp_lexer_consume_token (parser->lexer);
14152     }
14153
14154   return nreverse (asm_operands);
14155 }
14156
14157 /* Parse an asm-clobber-list.
14158
14159    asm-clobber-list:
14160      string-literal
14161      asm-clobber-list , string-literal
14162
14163    Returns a TREE_LIST, indicating the clobbers in the order that they
14164    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
14165
14166 static tree
14167 cp_parser_asm_clobber_list (cp_parser* parser)
14168 {
14169   tree clobbers = NULL_TREE;
14170
14171   while (true)
14172     {
14173       tree string_literal;
14174
14175       /* Look for the string literal.  */
14176       string_literal = cp_parser_string_literal (parser, false, false);
14177       /* Add it to the list.  */
14178       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
14179       /* If the next token is not a `,', then the list is
14180          complete.  */
14181       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14182         break;
14183       /* Consume the `,' token.  */
14184       cp_lexer_consume_token (parser->lexer);
14185     }
14186
14187   return clobbers;
14188 }
14189
14190 /* Parse an (optional) series of attributes.
14191
14192    attributes:
14193      attributes attribute
14194
14195    attribute:
14196      __attribute__ (( attribute-list [opt] ))
14197
14198    The return value is as for cp_parser_attribute_list.  */
14199
14200 static tree
14201 cp_parser_attributes_opt (cp_parser* parser)
14202 {
14203   tree attributes = NULL_TREE;
14204
14205   while (true)
14206     {
14207       cp_token *token;
14208       tree attribute_list;
14209
14210       /* Peek at the next token.  */
14211       token = cp_lexer_peek_token (parser->lexer);
14212       /* If it's not `__attribute__', then we're done.  */
14213       if (token->keyword != RID_ATTRIBUTE)
14214         break;
14215
14216       /* Consume the `__attribute__' keyword.  */
14217       cp_lexer_consume_token (parser->lexer);
14218       /* Look for the two `(' tokens.  */
14219       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14220       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
14221
14222       /* Peek at the next token.  */
14223       token = cp_lexer_peek_token (parser->lexer);
14224       if (token->type != CPP_CLOSE_PAREN)
14225         /* Parse the attribute-list.  */
14226         attribute_list = cp_parser_attribute_list (parser);
14227       else
14228         /* If the next token is a `)', then there is no attribute
14229            list.  */
14230         attribute_list = NULL;
14231
14232       /* Look for the two `)' tokens.  */
14233       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14234       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
14235
14236       /* Add these new attributes to the list.  */
14237       attributes = chainon (attributes, attribute_list);
14238     }
14239
14240   return attributes;
14241 }
14242
14243 /* Parse an attribute-list.
14244
14245    attribute-list:
14246      attribute
14247      attribute-list , attribute
14248
14249    attribute:
14250      identifier
14251      identifier ( identifier )
14252      identifier ( identifier , expression-list )
14253      identifier ( expression-list )
14254
14255    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
14256    to an attribute.  The TREE_PURPOSE of each node is the identifier
14257    indicating which attribute is in use.  The TREE_VALUE represents
14258    the arguments, if any.  */
14259
14260 static tree
14261 cp_parser_attribute_list (cp_parser* parser)
14262 {
14263   tree attribute_list = NULL_TREE;
14264   bool save_translate_strings_p = parser->translate_strings_p;
14265
14266   parser->translate_strings_p = false;
14267   while (true)
14268     {
14269       cp_token *token;
14270       tree identifier;
14271       tree attribute;
14272
14273       /* Look for the identifier.  We also allow keywords here; for
14274          example `__attribute__ ((const))' is legal.  */
14275       token = cp_lexer_peek_token (parser->lexer);
14276       if (token->type == CPP_NAME
14277           || token->type == CPP_KEYWORD)
14278         {
14279           /* Consume the token.  */
14280           token = cp_lexer_consume_token (parser->lexer);
14281
14282           /* Save away the identifier that indicates which attribute
14283              this is.  */ 
14284           identifier = token->value;
14285           attribute = build_tree_list (identifier, NULL_TREE);
14286
14287           /* Peek at the next token.  */
14288           token = cp_lexer_peek_token (parser->lexer);
14289           /* If it's an `(', then parse the attribute arguments.  */
14290           if (token->type == CPP_OPEN_PAREN)
14291             {
14292               tree arguments;
14293
14294               arguments = (cp_parser_parenthesized_expression_list
14295                            (parser, true, /*cast_p=*/false, 
14296                             /*non_constant_p=*/NULL));
14297               /* Save the identifier and arguments away.  */
14298               TREE_VALUE (attribute) = arguments;
14299             }
14300
14301           /* Add this attribute to the list.  */
14302           TREE_CHAIN (attribute) = attribute_list;
14303           attribute_list = attribute;
14304
14305           token = cp_lexer_peek_token (parser->lexer);
14306         }
14307       /* Now, look for more attributes.  If the next token isn't a
14308          `,', we're done.  */
14309       if (token->type != CPP_COMMA)
14310         break;
14311
14312       /* Consume the comma and keep going.  */
14313       cp_lexer_consume_token (parser->lexer);
14314     }
14315   parser->translate_strings_p = save_translate_strings_p;
14316
14317   /* We built up the list in reverse order.  */
14318   return nreverse (attribute_list);
14319 }
14320
14321 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
14322    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
14323    current value of the PEDANTIC flag, regardless of whether or not
14324    the `__extension__' keyword is present.  The caller is responsible
14325    for restoring the value of the PEDANTIC flag.  */
14326
14327 static bool
14328 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
14329 {
14330   /* Save the old value of the PEDANTIC flag.  */
14331   *saved_pedantic = pedantic;
14332
14333   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
14334     {
14335       /* Consume the `__extension__' token.  */
14336       cp_lexer_consume_token (parser->lexer);
14337       /* We're not being pedantic while the `__extension__' keyword is
14338          in effect.  */
14339       pedantic = 0;
14340
14341       return true;
14342     }
14343
14344   return false;
14345 }
14346
14347 /* Parse a label declaration.
14348
14349    label-declaration:
14350      __label__ label-declarator-seq ;
14351
14352    label-declarator-seq:
14353      identifier , label-declarator-seq
14354      identifier  */
14355
14356 static void
14357 cp_parser_label_declaration (cp_parser* parser)
14358 {
14359   /* Look for the `__label__' keyword.  */
14360   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14361
14362   while (true)
14363     {
14364       tree identifier;
14365
14366       /* Look for an identifier.  */
14367       identifier = cp_parser_identifier (parser);
14368       /* Declare it as a lobel.  */
14369       finish_label_decl (identifier);
14370       /* If the next token is a `;', stop.  */
14371       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14372         break;
14373       /* Look for the `,' separating the label declarations.  */
14374       cp_parser_require (parser, CPP_COMMA, "`,'");
14375     }
14376
14377   /* Look for the final `;'.  */
14378   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14379 }
14380
14381 /* Support Functions */
14382
14383 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14384    NAME should have one of the representations used for an
14385    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14386    is returned.  If PARSER->SCOPE is a dependent type, then a
14387    SCOPE_REF is returned.
14388
14389    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14390    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14391    was formed.  Abstractly, such entities should not be passed to this
14392    function, because they do not need to be looked up, but it is
14393    simpler to check for this special case here, rather than at the
14394    call-sites.
14395
14396    In cases not explicitly covered above, this function returns a
14397    DECL, OVERLOAD, or baselink representing the result of the lookup.
14398    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14399    is returned.
14400
14401    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
14402    (e.g., "struct") that was used.  In that case bindings that do not
14403    refer to types are ignored.
14404
14405    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14406    ignored.
14407
14408    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14409    are ignored.
14410
14411    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14412    types.  
14413
14414    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14415    results in an ambiguity, and false otherwise.  */
14416
14417 static tree
14418 cp_parser_lookup_name (cp_parser *parser, tree name,
14419                        enum tag_types tag_type,
14420                        bool is_template, bool is_namespace,
14421                        bool check_dependency,
14422                        bool *ambiguous_p)
14423 {
14424   tree decl;
14425   tree object_type = parser->context->object_type;
14426
14427   /* Assume that the lookup will be unambiguous.  */
14428   if (ambiguous_p)
14429     *ambiguous_p = false;
14430
14431   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14432      no longer valid.  Note that if we are parsing tentatively, and
14433      the parse fails, OBJECT_TYPE will be automatically restored.  */
14434   parser->context->object_type = NULL_TREE;
14435
14436   if (name == error_mark_node)
14437     return error_mark_node;
14438
14439   /* A template-id has already been resolved; there is no lookup to
14440      do.  */
14441   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14442     return name;
14443   if (BASELINK_P (name))
14444     {
14445       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14446                   == TEMPLATE_ID_EXPR);
14447       return name;
14448     }
14449
14450   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14451      it should already have been checked to make sure that the name
14452      used matches the type being destroyed.  */
14453   if (TREE_CODE (name) == BIT_NOT_EXPR)
14454     {
14455       tree type;
14456
14457       /* Figure out to which type this destructor applies.  */
14458       if (parser->scope)
14459         type = parser->scope;
14460       else if (object_type)
14461         type = object_type;
14462       else
14463         type = current_class_type;
14464       /* If that's not a class type, there is no destructor.  */
14465       if (!type || !CLASS_TYPE_P (type))
14466         return error_mark_node;
14467       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
14468         lazily_declare_fn (sfk_destructor, type);
14469       if (!CLASSTYPE_DESTRUCTORS (type))
14470           return error_mark_node;
14471       /* If it was a class type, return the destructor.  */
14472       return CLASSTYPE_DESTRUCTORS (type);
14473     }
14474
14475   /* By this point, the NAME should be an ordinary identifier.  If
14476      the id-expression was a qualified name, the qualifying scope is
14477      stored in PARSER->SCOPE at this point.  */
14478   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14479
14480   /* Perform the lookup.  */
14481   if (parser->scope)
14482     {
14483       bool dependent_p;
14484
14485       if (parser->scope == error_mark_node)
14486         return error_mark_node;
14487
14488       /* If the SCOPE is dependent, the lookup must be deferred until
14489          the template is instantiated -- unless we are explicitly
14490          looking up names in uninstantiated templates.  Even then, we
14491          cannot look up the name if the scope is not a class type; it
14492          might, for example, be a template type parameter.  */
14493       dependent_p = (TYPE_P (parser->scope)
14494                      && !(parser->in_declarator_p
14495                           && currently_open_class (parser->scope))
14496                      && dependent_type_p (parser->scope));
14497       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14498            && dependent_p)
14499         {
14500           if (tag_type)
14501             {
14502               tree type;
14503
14504               /* The resolution to Core Issue 180 says that `struct
14505                  A::B' should be considered a type-name, even if `A'
14506                  is dependent.  */
14507               type = make_typename_type (parser->scope, name, tag_type,
14508                                          /*complain=*/1);
14509               decl = TYPE_NAME (type);
14510             }
14511           else if (is_template)
14512             decl = make_unbound_class_template (parser->scope,
14513                                                 name, NULL_TREE,
14514                                                 /*complain=*/1);
14515           else
14516             decl = build_nt (SCOPE_REF, parser->scope, name);
14517         }
14518       else
14519         {
14520           tree pushed_scope = NULL_TREE;
14521
14522           /* If PARSER->SCOPE is a dependent type, then it must be a
14523              class type, and we must not be checking dependencies;
14524              otherwise, we would have processed this lookup above.  So
14525              that PARSER->SCOPE is not considered a dependent base by
14526              lookup_member, we must enter the scope here.  */
14527           if (dependent_p)
14528             pushed_scope = push_scope (parser->scope);
14529           /* If the PARSER->SCOPE is a template specialization, it
14530              may be instantiated during name lookup.  In that case,
14531              errors may be issued.  Even if we rollback the current
14532              tentative parse, those errors are valid.  */
14533           decl = lookup_qualified_name (parser->scope, name, 
14534                                         tag_type != none_type, 
14535                                         /*complain=*/true);
14536           if (pushed_scope)
14537             pop_scope (pushed_scope);
14538         }
14539       parser->qualifying_scope = parser->scope;
14540       parser->object_scope = NULL_TREE;
14541     }
14542   else if (object_type)
14543     {
14544       tree object_decl = NULL_TREE;
14545       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14546          OBJECT_TYPE is not a class.  */
14547       if (CLASS_TYPE_P (object_type))
14548         /* If the OBJECT_TYPE is a template specialization, it may
14549            be instantiated during name lookup.  In that case, errors
14550            may be issued.  Even if we rollback the current tentative
14551            parse, those errors are valid.  */
14552         object_decl = lookup_member (object_type,
14553                                      name,
14554                                      /*protect=*/0, 
14555                                      tag_type != none_type);
14556       /* Look it up in the enclosing context, too.  */
14557       decl = lookup_name_real (name, tag_type != none_type, 
14558                                /*nonclass=*/0,
14559                                /*block_p=*/true, is_namespace,
14560                                /*flags=*/0);
14561       parser->object_scope = object_type;
14562       parser->qualifying_scope = NULL_TREE;
14563       if (object_decl)
14564         decl = object_decl;
14565     }
14566   else
14567     {
14568       decl = lookup_name_real (name, tag_type != none_type, 
14569                                /*nonclass=*/0,
14570                                /*block_p=*/true, is_namespace,
14571                                /*flags=*/0);
14572       parser->qualifying_scope = NULL_TREE;
14573       parser->object_scope = NULL_TREE;
14574     }
14575
14576   /* If the lookup failed, let our caller know.  */
14577   if (!decl || decl == error_mark_node)
14578     return error_mark_node;
14579
14580   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14581   if (TREE_CODE (decl) == TREE_LIST)
14582     {
14583       if (ambiguous_p)
14584         *ambiguous_p = true;
14585       /* The error message we have to print is too complicated for
14586          cp_parser_error, so we incorporate its actions directly.  */
14587       if (!cp_parser_simulate_error (parser))
14588         {
14589           error ("reference to %qD is ambiguous", name);
14590           print_candidates (decl);
14591         }
14592       return error_mark_node;
14593     }
14594
14595   gcc_assert (DECL_P (decl)
14596               || TREE_CODE (decl) == OVERLOAD
14597               || TREE_CODE (decl) == SCOPE_REF
14598               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14599               || BASELINK_P (decl));
14600
14601   /* If we have resolved the name of a member declaration, check to
14602      see if the declaration is accessible.  When the name resolves to
14603      set of overloaded functions, accessibility is checked when
14604      overload resolution is done.
14605
14606      During an explicit instantiation, access is not checked at all,
14607      as per [temp.explicit].  */
14608   if (DECL_P (decl))
14609     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14610
14611   return decl;
14612 }
14613
14614 /* Like cp_parser_lookup_name, but for use in the typical case where
14615    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14616    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14617
14618 static tree
14619 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14620 {
14621   return cp_parser_lookup_name (parser, name,
14622                                 none_type,
14623                                 /*is_template=*/false,
14624                                 /*is_namespace=*/false,
14625                                 /*check_dependency=*/true,
14626                                 /*ambiguous_p=*/NULL);
14627 }
14628
14629 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14630    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14631    true, the DECL indicates the class being defined in a class-head,
14632    or declared in an elaborated-type-specifier.
14633
14634    Otherwise, return DECL.  */
14635
14636 static tree
14637 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14638 {
14639   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14640      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14641
14642        struct A {
14643          template <typename T> struct B;
14644        };
14645
14646        template <typename T> struct A::B {};
14647
14648      Similarly, in a elaborated-type-specifier:
14649
14650        namespace N { struct X{}; }
14651
14652        struct A {
14653          template <typename T> friend struct N::X;
14654        };
14655
14656      However, if the DECL refers to a class type, and we are in
14657      the scope of the class, then the name lookup automatically
14658      finds the TYPE_DECL created by build_self_reference rather
14659      than a TEMPLATE_DECL.  For example, in:
14660
14661        template <class T> struct S {
14662          S s;
14663        };
14664
14665      there is no need to handle such case.  */
14666
14667   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14668     return DECL_TEMPLATE_RESULT (decl);
14669
14670   return decl;
14671 }
14672
14673 /* If too many, or too few, template-parameter lists apply to the
14674    declarator, issue an error message.  Returns TRUE if all went well,
14675    and FALSE otherwise.  */
14676
14677 static bool
14678 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14679                                                 cp_declarator *declarator)
14680 {
14681   unsigned num_templates;
14682
14683   /* We haven't seen any classes that involve template parameters yet.  */
14684   num_templates = 0;
14685
14686   switch (declarator->kind)
14687     {
14688     case cdk_id:
14689       if (declarator->u.id.qualifying_scope)
14690         {
14691           tree scope;
14692           tree member;
14693
14694           scope = declarator->u.id.qualifying_scope;
14695           member = declarator->u.id.unqualified_name;
14696
14697           while (scope && CLASS_TYPE_P (scope))
14698             {
14699               /* You're supposed to have one `template <...>'
14700                  for every template class, but you don't need one
14701                  for a full specialization.  For example:
14702
14703                  template <class T> struct S{};
14704                  template <> struct S<int> { void f(); };
14705                  void S<int>::f () {}
14706
14707                  is correct; there shouldn't be a `template <>' for
14708                  the definition of `S<int>::f'.  */
14709               if (CLASSTYPE_TEMPLATE_INFO (scope)
14710                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14711                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14712                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14713                 ++num_templates;
14714
14715               scope = TYPE_CONTEXT (scope);
14716             }
14717         }
14718       else if (TREE_CODE (declarator->u.id.unqualified_name) 
14719                == TEMPLATE_ID_EXPR)
14720         /* If the DECLARATOR has the form `X<y>' then it uses one
14721            additional level of template parameters.  */
14722         ++num_templates;
14723
14724       return cp_parser_check_template_parameters (parser,
14725                                                   num_templates);
14726
14727     case cdk_function:
14728     case cdk_array:
14729     case cdk_pointer:
14730     case cdk_reference:
14731     case cdk_ptrmem:
14732       return (cp_parser_check_declarator_template_parameters
14733               (parser, declarator->declarator));
14734
14735     case cdk_error:
14736       return true;
14737
14738     default:
14739       gcc_unreachable ();
14740     }
14741   return false;
14742 }
14743
14744 /* NUM_TEMPLATES were used in the current declaration.  If that is
14745    invalid, return FALSE and issue an error messages.  Otherwise,
14746    return TRUE.  */
14747
14748 static bool
14749 cp_parser_check_template_parameters (cp_parser* parser,
14750                                      unsigned num_templates)
14751 {
14752   /* If there are more template classes than parameter lists, we have
14753      something like:
14754
14755        template <class T> void S<T>::R<T>::f ();  */
14756   if (parser->num_template_parameter_lists < num_templates)
14757     {
14758       error ("too few template-parameter-lists");
14759       return false;
14760     }
14761   /* If there are the same number of template classes and parameter
14762      lists, that's OK.  */
14763   if (parser->num_template_parameter_lists == num_templates)
14764     return true;
14765   /* If there are more, but only one more, then we are referring to a
14766      member template.  That's OK too.  */
14767   if (parser->num_template_parameter_lists == num_templates + 1)
14768       return true;
14769   /* Otherwise, there are too many template parameter lists.  We have
14770      something like:
14771
14772      template <class T> template <class U> void S::f();  */
14773   error ("too many template-parameter-lists");
14774   return false;
14775 }
14776
14777 /* Parse an optional `::' token indicating that the following name is
14778    from the global namespace.  If so, PARSER->SCOPE is set to the
14779    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14780    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14781    Returns the new value of PARSER->SCOPE, if the `::' token is
14782    present, and NULL_TREE otherwise.  */
14783
14784 static tree
14785 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14786 {
14787   cp_token *token;
14788
14789   /* Peek at the next token.  */
14790   token = cp_lexer_peek_token (parser->lexer);
14791   /* If we're looking at a `::' token then we're starting from the
14792      global namespace, not our current location.  */
14793   if (token->type == CPP_SCOPE)
14794     {
14795       /* Consume the `::' token.  */
14796       cp_lexer_consume_token (parser->lexer);
14797       /* Set the SCOPE so that we know where to start the lookup.  */
14798       parser->scope = global_namespace;
14799       parser->qualifying_scope = global_namespace;
14800       parser->object_scope = NULL_TREE;
14801
14802       return parser->scope;
14803     }
14804   else if (!current_scope_valid_p)
14805     {
14806       parser->scope = NULL_TREE;
14807       parser->qualifying_scope = NULL_TREE;
14808       parser->object_scope = NULL_TREE;
14809     }
14810
14811   return NULL_TREE;
14812 }
14813
14814 /* Returns TRUE if the upcoming token sequence is the start of a
14815    constructor declarator.  If FRIEND_P is true, the declarator is
14816    preceded by the `friend' specifier.  */
14817
14818 static bool
14819 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14820 {
14821   bool constructor_p;
14822   tree type_decl = NULL_TREE;
14823   bool nested_name_p;
14824   cp_token *next_token;
14825
14826   /* The common case is that this is not a constructor declarator, so
14827      try to avoid doing lots of work if at all possible.  It's not
14828      valid declare a constructor at function scope.  */
14829   if (at_function_scope_p ())
14830     return false;
14831   /* And only certain tokens can begin a constructor declarator.  */
14832   next_token = cp_lexer_peek_token (parser->lexer);
14833   if (next_token->type != CPP_NAME
14834       && next_token->type != CPP_SCOPE
14835       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14836       && next_token->type != CPP_TEMPLATE_ID)
14837     return false;
14838
14839   /* Parse tentatively; we are going to roll back all of the tokens
14840      consumed here.  */
14841   cp_parser_parse_tentatively (parser);
14842   /* Assume that we are looking at a constructor declarator.  */
14843   constructor_p = true;
14844
14845   /* Look for the optional `::' operator.  */
14846   cp_parser_global_scope_opt (parser,
14847                               /*current_scope_valid_p=*/false);
14848   /* Look for the nested-name-specifier.  */
14849   nested_name_p
14850     = (cp_parser_nested_name_specifier_opt (parser,
14851                                             /*typename_keyword_p=*/false,
14852                                             /*check_dependency_p=*/false,
14853                                             /*type_p=*/false,
14854                                             /*is_declaration=*/false)
14855        != NULL_TREE);
14856   /* Outside of a class-specifier, there must be a
14857      nested-name-specifier.  */
14858   if (!nested_name_p &&
14859       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14860        || friend_p))
14861     constructor_p = false;
14862   /* If we still think that this might be a constructor-declarator,
14863      look for a class-name.  */
14864   if (constructor_p)
14865     {
14866       /* If we have:
14867
14868            template <typename T> struct S { S(); };
14869            template <typename T> S<T>::S ();
14870
14871          we must recognize that the nested `S' names a class.
14872          Similarly, for:
14873
14874            template <typename T> S<T>::S<T> ();
14875
14876          we must recognize that the nested `S' names a template.  */
14877       type_decl = cp_parser_class_name (parser,
14878                                         /*typename_keyword_p=*/false,
14879                                         /*template_keyword_p=*/false,
14880                                         none_type,
14881                                         /*check_dependency_p=*/false,
14882                                         /*class_head_p=*/false,
14883                                         /*is_declaration=*/false);
14884       /* If there was no class-name, then this is not a constructor.  */
14885       constructor_p = !cp_parser_error_occurred (parser);
14886     }
14887
14888   /* If we're still considering a constructor, we have to see a `(',
14889      to begin the parameter-declaration-clause, followed by either a
14890      `)', an `...', or a decl-specifier.  We need to check for a
14891      type-specifier to avoid being fooled into thinking that:
14892
14893        S::S (f) (int);
14894
14895      is a constructor.  (It is actually a function named `f' that
14896      takes one parameter (of type `int') and returns a value of type
14897      `S::S'.  */
14898   if (constructor_p
14899       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14900     {
14901       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14902           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14903           /* A parameter declaration begins with a decl-specifier,
14904              which is either the "attribute" keyword, a storage class
14905              specifier, or (usually) a type-specifier.  */
14906           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14907           && !cp_parser_storage_class_specifier_opt (parser))
14908         {
14909           tree type;
14910           tree pushed_scope = NULL_TREE;
14911           unsigned saved_num_template_parameter_lists;
14912
14913           /* Names appearing in the type-specifier should be looked up
14914              in the scope of the class.  */
14915           if (current_class_type)
14916             type = NULL_TREE;
14917           else
14918             {
14919               type = TREE_TYPE (type_decl);
14920               if (TREE_CODE (type) == TYPENAME_TYPE)
14921                 {
14922                   type = resolve_typename_type (type,
14923                                                 /*only_current_p=*/false);
14924                   if (type == error_mark_node)
14925                     {
14926                       cp_parser_abort_tentative_parse (parser);
14927                       return false;
14928                     }
14929                 }
14930               pushed_scope = push_scope (type);
14931             }
14932
14933           /* Inside the constructor parameter list, surrounding
14934              template-parameter-lists do not apply.  */
14935           saved_num_template_parameter_lists
14936             = parser->num_template_parameter_lists;
14937           parser->num_template_parameter_lists = 0;
14938
14939           /* Look for the type-specifier.  */
14940           cp_parser_type_specifier (parser,
14941                                     CP_PARSER_FLAGS_NONE,
14942                                     /*decl_specs=*/NULL,
14943                                     /*is_declarator=*/true,
14944                                     /*declares_class_or_enum=*/NULL,
14945                                     /*is_cv_qualifier=*/NULL);
14946
14947           parser->num_template_parameter_lists
14948             = saved_num_template_parameter_lists;
14949
14950           /* Leave the scope of the class.  */
14951           if (pushed_scope)
14952             pop_scope (pushed_scope);
14953
14954           constructor_p = !cp_parser_error_occurred (parser);
14955         }
14956     }
14957   else
14958     constructor_p = false;
14959   /* We did not really want to consume any tokens.  */
14960   cp_parser_abort_tentative_parse (parser);
14961
14962   return constructor_p;
14963 }
14964
14965 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14966    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14967    they must be performed once we are in the scope of the function.
14968
14969    Returns the function defined.  */
14970
14971 static tree
14972 cp_parser_function_definition_from_specifiers_and_declarator
14973   (cp_parser* parser,
14974    cp_decl_specifier_seq *decl_specifiers,
14975    tree attributes,
14976    const cp_declarator *declarator)
14977 {
14978   tree fn;
14979   bool success_p;
14980
14981   /* Begin the function-definition.  */
14982   success_p = start_function (decl_specifiers, declarator, attributes);
14983
14984   /* The things we're about to see are not directly qualified by any
14985      template headers we've seen thus far.  */
14986   reset_specialization ();
14987
14988   /* If there were names looked up in the decl-specifier-seq that we
14989      did not check, check them now.  We must wait until we are in the
14990      scope of the function to perform the checks, since the function
14991      might be a friend.  */
14992   perform_deferred_access_checks ();
14993
14994   if (!success_p)
14995     {
14996       /* Skip the entire function.  */
14997       error ("invalid function declaration");
14998       cp_parser_skip_to_end_of_block_or_statement (parser);
14999       fn = error_mark_node;
15000     }
15001   else
15002     fn = cp_parser_function_definition_after_declarator (parser,
15003                                                          /*inline_p=*/false);
15004
15005   return fn;
15006 }
15007
15008 /* Parse the part of a function-definition that follows the
15009    declarator.  INLINE_P is TRUE iff this function is an inline
15010    function defined with a class-specifier.
15011
15012    Returns the function defined.  */
15013
15014 static tree
15015 cp_parser_function_definition_after_declarator (cp_parser* parser,
15016                                                 bool inline_p)
15017 {
15018   tree fn;
15019   bool ctor_initializer_p = false;
15020   bool saved_in_unbraced_linkage_specification_p;
15021   unsigned saved_num_template_parameter_lists;
15022
15023   /* If the next token is `return', then the code may be trying to
15024      make use of the "named return value" extension that G++ used to
15025      support.  */
15026   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
15027     {
15028       /* Consume the `return' keyword.  */
15029       cp_lexer_consume_token (parser->lexer);
15030       /* Look for the identifier that indicates what value is to be
15031          returned.  */
15032       cp_parser_identifier (parser);
15033       /* Issue an error message.  */
15034       error ("named return values are no longer supported");
15035       /* Skip tokens until we reach the start of the function body.  */
15036       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
15037              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
15038         cp_lexer_consume_token (parser->lexer);
15039     }
15040   /* The `extern' in `extern "C" void f () { ... }' does not apply to
15041      anything declared inside `f'.  */
15042   saved_in_unbraced_linkage_specification_p
15043     = parser->in_unbraced_linkage_specification_p;
15044   parser->in_unbraced_linkage_specification_p = false;
15045   /* Inside the function, surrounding template-parameter-lists do not
15046      apply.  */
15047   saved_num_template_parameter_lists
15048     = parser->num_template_parameter_lists;
15049   parser->num_template_parameter_lists = 0;
15050   /* If the next token is `try', then we are looking at a
15051      function-try-block.  */
15052   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
15053     ctor_initializer_p = cp_parser_function_try_block (parser);
15054   /* A function-try-block includes the function-body, so we only do
15055      this next part if we're not processing a function-try-block.  */
15056   else
15057     ctor_initializer_p
15058       = cp_parser_ctor_initializer_opt_and_function_body (parser);
15059
15060   /* Finish the function.  */
15061   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
15062                         (inline_p ? 2 : 0));
15063   /* Generate code for it, if necessary.  */
15064   expand_or_defer_fn (fn);
15065   /* Restore the saved values.  */
15066   parser->in_unbraced_linkage_specification_p
15067     = saved_in_unbraced_linkage_specification_p;
15068   parser->num_template_parameter_lists
15069     = saved_num_template_parameter_lists;
15070
15071   return fn;
15072 }
15073
15074 /* Parse a template-declaration, assuming that the `export' (and
15075    `extern') keywords, if present, has already been scanned.  MEMBER_P
15076    is as for cp_parser_template_declaration.  */
15077
15078 static void
15079 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
15080 {
15081   tree decl = NULL_TREE;
15082   tree parameter_list;
15083   bool friend_p = false;
15084
15085   /* Look for the `template' keyword.  */
15086   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
15087     return;
15088
15089   /* And the `<'.  */
15090   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
15091     return;
15092
15093   /* If the next token is `>', then we have an invalid
15094      specialization.  Rather than complain about an invalid template
15095      parameter, issue an error message here.  */
15096   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15097     {
15098       cp_parser_error (parser, "invalid explicit specialization");
15099       begin_specialization ();
15100       parameter_list = NULL_TREE;
15101     }
15102   else
15103     {
15104       /* Parse the template parameters.  */
15105       begin_template_parm_list ();
15106       parameter_list = cp_parser_template_parameter_list (parser);
15107       parameter_list = end_template_parm_list (parameter_list);
15108     }
15109
15110   /* Look for the `>'.  */
15111   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
15112   /* We just processed one more parameter list.  */
15113   ++parser->num_template_parameter_lists;
15114   /* If the next token is `template', there are more template
15115      parameters.  */
15116   if (cp_lexer_next_token_is_keyword (parser->lexer,
15117                                       RID_TEMPLATE))
15118     cp_parser_template_declaration_after_export (parser, member_p);
15119   else
15120     {
15121       /* There are no access checks when parsing a template, as we do not
15122          know if a specialization will be a friend.  */
15123       push_deferring_access_checks (dk_no_check);
15124
15125       decl = cp_parser_single_declaration (parser,
15126                                            member_p,
15127                                            &friend_p);
15128
15129       pop_deferring_access_checks ();
15130
15131       /* If this is a member template declaration, let the front
15132          end know.  */
15133       if (member_p && !friend_p && decl)
15134         {
15135           if (TREE_CODE (decl) == TYPE_DECL)
15136             cp_parser_check_access_in_redeclaration (decl);
15137
15138           decl = finish_member_template_decl (decl);
15139         }
15140       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
15141         make_friend_class (current_class_type, TREE_TYPE (decl),
15142                            /*complain=*/true);
15143     }
15144   /* We are done with the current parameter list.  */
15145   --parser->num_template_parameter_lists;
15146
15147   /* Finish up.  */
15148   finish_template_decl (parameter_list);
15149
15150   /* Register member declarations.  */
15151   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
15152     finish_member_declaration (decl);
15153
15154   /* If DECL is a function template, we must return to parse it later.
15155      (Even though there is no definition, there might be default
15156      arguments that need handling.)  */
15157   if (member_p && decl
15158       && (TREE_CODE (decl) == FUNCTION_DECL
15159           || DECL_FUNCTION_TEMPLATE_P (decl)))
15160     TREE_VALUE (parser->unparsed_functions_queues)
15161       = tree_cons (NULL_TREE, decl,
15162                    TREE_VALUE (parser->unparsed_functions_queues));
15163 }
15164
15165 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
15166    `function-definition' sequence.  MEMBER_P is true, this declaration
15167    appears in a class scope.
15168
15169    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
15170    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
15171
15172 static tree
15173 cp_parser_single_declaration (cp_parser* parser,
15174                               bool member_p,
15175                               bool* friend_p)
15176 {
15177   int declares_class_or_enum;
15178   tree decl = NULL_TREE;
15179   cp_decl_specifier_seq decl_specifiers;
15180   bool function_definition_p = false;
15181
15182   /* This function is only used when processing a template
15183      declaration.  */
15184   gcc_assert (innermost_scope_kind () == sk_template_parms
15185               || innermost_scope_kind () == sk_template_spec);
15186
15187   /* Defer access checks until we know what is being declared.  */
15188   push_deferring_access_checks (dk_deferred);
15189
15190   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
15191      alternative.  */
15192   cp_parser_decl_specifier_seq (parser,
15193                                 CP_PARSER_FLAGS_OPTIONAL,
15194                                 &decl_specifiers,
15195                                 &declares_class_or_enum);
15196   if (friend_p)
15197     *friend_p = cp_parser_friend_p (&decl_specifiers);
15198
15199   /* There are no template typedefs.  */
15200   if (decl_specifiers.specs[(int) ds_typedef])
15201     {
15202       error ("template declaration of %qs", "typedef");
15203       decl = error_mark_node;
15204     }
15205
15206   /* Gather up the access checks that occurred the
15207      decl-specifier-seq.  */
15208   stop_deferring_access_checks ();
15209
15210   /* Check for the declaration of a template class.  */
15211   if (declares_class_or_enum)
15212     {
15213       if (cp_parser_declares_only_class_p (parser))
15214         {
15215           decl = shadow_tag (&decl_specifiers);
15216
15217           /* In this case:
15218
15219                struct C {
15220                  friend template <typename T> struct A<T>::B;
15221                };
15222
15223              A<T>::B will be represented by a TYPENAME_TYPE, and
15224              therefore not recognized by shadow_tag.  */
15225           if (friend_p && *friend_p
15226               && !decl
15227               && decl_specifiers.type
15228               && TYPE_P (decl_specifiers.type))
15229             decl = decl_specifiers.type;
15230
15231           if (decl && decl != error_mark_node)
15232             decl = TYPE_NAME (decl);
15233           else
15234             decl = error_mark_node;
15235         }
15236     }
15237   /* If it's not a template class, try for a template function.  If
15238      the next token is a `;', then this declaration does not declare
15239      anything.  But, if there were errors in the decl-specifiers, then
15240      the error might well have come from an attempted class-specifier.
15241      In that case, there's no need to warn about a missing declarator.  */
15242   if (!decl
15243       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
15244           || decl_specifiers.type != error_mark_node))
15245     decl = cp_parser_init_declarator (parser,
15246                                       &decl_specifiers,
15247                                       /*function_definition_allowed_p=*/true,
15248                                       member_p,
15249                                       declares_class_or_enum,
15250                                       &function_definition_p);
15251
15252   pop_deferring_access_checks ();
15253
15254   /* Clear any current qualification; whatever comes next is the start
15255      of something new.  */
15256   parser->scope = NULL_TREE;
15257   parser->qualifying_scope = NULL_TREE;
15258   parser->object_scope = NULL_TREE;
15259   /* Look for a trailing `;' after the declaration.  */
15260   if (!function_definition_p
15261       && (decl == error_mark_node
15262           || !cp_parser_require (parser, CPP_SEMICOLON, "`;'")))
15263     cp_parser_skip_to_end_of_block_or_statement (parser);
15264
15265   return decl;
15266 }
15267
15268 /* Parse a cast-expression that is not the operand of a unary "&".  */
15269
15270 static tree
15271 cp_parser_simple_cast_expression (cp_parser *parser)
15272 {
15273   return cp_parser_cast_expression (parser, /*address_p=*/false,
15274                                     /*cast_p=*/false);
15275 }
15276
15277 /* Parse a functional cast to TYPE.  Returns an expression
15278    representing the cast.  */
15279
15280 static tree
15281 cp_parser_functional_cast (cp_parser* parser, tree type)
15282 {
15283   tree expression_list;
15284   tree cast;
15285
15286   expression_list
15287     = cp_parser_parenthesized_expression_list (parser, false,
15288                                                /*cast_p=*/true,
15289                                                /*non_constant_p=*/NULL);
15290
15291   cast = build_functional_cast (type, expression_list);
15292   /* [expr.const]/1: In an integral constant expression "only type
15293      conversions to integral or enumeration type can be used".  */
15294   if (cast != error_mark_node && !type_dependent_expression_p (type)
15295       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
15296     {
15297       if (cp_parser_non_integral_constant_expression
15298           (parser, "a call to a constructor"))
15299         return error_mark_node;
15300     }
15301   return cast;
15302 }
15303
15304 /* Save the tokens that make up the body of a member function defined
15305    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
15306    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
15307    specifiers applied to the declaration.  Returns the FUNCTION_DECL
15308    for the member function.  */
15309
15310 static tree
15311 cp_parser_save_member_function_body (cp_parser* parser,
15312                                      cp_decl_specifier_seq *decl_specifiers,
15313                                      cp_declarator *declarator,
15314                                      tree attributes)
15315 {
15316   cp_token *first;
15317   cp_token *last;
15318   tree fn;
15319
15320   /* Create the function-declaration.  */
15321   fn = start_method (decl_specifiers, declarator, attributes);
15322   /* If something went badly wrong, bail out now.  */
15323   if (fn == error_mark_node)
15324     {
15325       /* If there's a function-body, skip it.  */
15326       if (cp_parser_token_starts_function_definition_p
15327           (cp_lexer_peek_token (parser->lexer)))
15328         cp_parser_skip_to_end_of_block_or_statement (parser);
15329       return error_mark_node;
15330     }
15331
15332   /* Remember it, if there default args to post process.  */
15333   cp_parser_save_default_args (parser, fn);
15334
15335   /* Save away the tokens that make up the body of the
15336      function.  */
15337   first = parser->lexer->next_token;
15338   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15339   /* Handle function try blocks.  */
15340   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
15341     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
15342   last = parser->lexer->next_token;
15343
15344   /* Save away the inline definition; we will process it when the
15345      class is complete.  */
15346   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
15347   DECL_PENDING_INLINE_P (fn) = 1;
15348
15349   /* We need to know that this was defined in the class, so that
15350      friend templates are handled correctly.  */
15351   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
15352
15353   /* We're done with the inline definition.  */
15354   finish_method (fn);
15355
15356   /* Add FN to the queue of functions to be parsed later.  */
15357   TREE_VALUE (parser->unparsed_functions_queues)
15358     = tree_cons (NULL_TREE, fn,
15359                  TREE_VALUE (parser->unparsed_functions_queues));
15360
15361   return fn;
15362 }
15363
15364 /* Parse a template-argument-list, as well as the trailing ">" (but
15365    not the opening ">").  See cp_parser_template_argument_list for the
15366    return value.  */
15367
15368 static tree
15369 cp_parser_enclosed_template_argument_list (cp_parser* parser)
15370 {
15371   tree arguments;
15372   tree saved_scope;
15373   tree saved_qualifying_scope;
15374   tree saved_object_scope;
15375   bool saved_greater_than_is_operator_p;
15376
15377   /* [temp.names]
15378
15379      When parsing a template-id, the first non-nested `>' is taken as
15380      the end of the template-argument-list rather than a greater-than
15381      operator.  */
15382   saved_greater_than_is_operator_p
15383     = parser->greater_than_is_operator_p;
15384   parser->greater_than_is_operator_p = false;
15385   /* Parsing the argument list may modify SCOPE, so we save it
15386      here.  */
15387   saved_scope = parser->scope;
15388   saved_qualifying_scope = parser->qualifying_scope;
15389   saved_object_scope = parser->object_scope;
15390   /* Parse the template-argument-list itself.  */
15391   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15392     arguments = NULL_TREE;
15393   else
15394     arguments = cp_parser_template_argument_list (parser);
15395   /* Look for the `>' that ends the template-argument-list. If we find
15396      a '>>' instead, it's probably just a typo.  */
15397   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15398     {
15399       if (!saved_greater_than_is_operator_p)
15400         {
15401           /* If we're in a nested template argument list, the '>>' has
15402             to be a typo for '> >'. We emit the error message, but we
15403             continue parsing and we push a '>' as next token, so that
15404             the argument list will be parsed correctly.  Note that the
15405             global source location is still on the token before the
15406             '>>', so we need to say explicitly where we want it.  */
15407           cp_token *token = cp_lexer_peek_token (parser->lexer);
15408           error ("%H%<>>%> should be %<> >%> "
15409                  "within a nested template argument list",
15410                  &token->location);
15411
15412           /* ??? Proper recovery should terminate two levels of
15413              template argument list here.  */
15414           token->type = CPP_GREATER;
15415         }
15416       else
15417         {
15418           /* If this is not a nested template argument list, the '>>'
15419             is a typo for '>'. Emit an error message and continue.
15420             Same deal about the token location, but here we can get it
15421             right by consuming the '>>' before issuing the diagnostic.  */
15422           cp_lexer_consume_token (parser->lexer);
15423           error ("spurious %<>>%>, use %<>%> to terminate "
15424                  "a template argument list");
15425         }
15426     }
15427   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15428     error ("missing %<>%> to terminate the template argument list");
15429   else
15430     /* It's what we want, a '>'; consume it.  */
15431     cp_lexer_consume_token (parser->lexer);
15432   /* The `>' token might be a greater-than operator again now.  */
15433   parser->greater_than_is_operator_p
15434     = saved_greater_than_is_operator_p;
15435   /* Restore the SAVED_SCOPE.  */
15436   parser->scope = saved_scope;
15437   parser->qualifying_scope = saved_qualifying_scope;
15438   parser->object_scope = saved_object_scope;
15439
15440   return arguments;
15441 }
15442
15443 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15444    arguments, or the body of the function have not yet been parsed,
15445    parse them now.  */
15446
15447 static void
15448 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15449 {
15450   /* If this member is a template, get the underlying
15451      FUNCTION_DECL.  */
15452   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15453     member_function = DECL_TEMPLATE_RESULT (member_function);
15454
15455   /* There should not be any class definitions in progress at this
15456      point; the bodies of members are only parsed outside of all class
15457      definitions.  */
15458   gcc_assert (parser->num_classes_being_defined == 0);
15459   /* While we're parsing the member functions we might encounter more
15460      classes.  We want to handle them right away, but we don't want
15461      them getting mixed up with functions that are currently in the
15462      queue.  */
15463   parser->unparsed_functions_queues
15464     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15465
15466   /* Make sure that any template parameters are in scope.  */
15467   maybe_begin_member_template_processing (member_function);
15468
15469   /* If the body of the function has not yet been parsed, parse it
15470      now.  */
15471   if (DECL_PENDING_INLINE_P (member_function))
15472     {
15473       tree function_scope;
15474       cp_token_cache *tokens;
15475
15476       /* The function is no longer pending; we are processing it.  */
15477       tokens = DECL_PENDING_INLINE_INFO (member_function);
15478       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15479       DECL_PENDING_INLINE_P (member_function) = 0;
15480       
15481       /* If this is a local class, enter the scope of the containing
15482          function.  */
15483       function_scope = current_function_decl;
15484       if (function_scope)
15485         push_function_context_to (function_scope);
15486
15487       /* Push the body of the function onto the lexer stack.  */
15488       cp_parser_push_lexer_for_tokens (parser, tokens);
15489
15490       /* Let the front end know that we going to be defining this
15491          function.  */
15492       start_preparsed_function (member_function, NULL_TREE,
15493                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15494
15495       /* Now, parse the body of the function.  */
15496       cp_parser_function_definition_after_declarator (parser,
15497                                                       /*inline_p=*/true);
15498
15499       /* Leave the scope of the containing function.  */
15500       if (function_scope)
15501         pop_function_context_from (function_scope);
15502       cp_parser_pop_lexer (parser);
15503     }
15504
15505   /* Remove any template parameters from the symbol table.  */
15506   maybe_end_member_template_processing ();
15507
15508   /* Restore the queue.  */
15509   parser->unparsed_functions_queues
15510     = TREE_CHAIN (parser->unparsed_functions_queues);
15511 }
15512
15513 /* If DECL contains any default args, remember it on the unparsed
15514    functions queue.  */
15515
15516 static void
15517 cp_parser_save_default_args (cp_parser* parser, tree decl)
15518 {
15519   tree probe;
15520
15521   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15522        probe;
15523        probe = TREE_CHAIN (probe))
15524     if (TREE_PURPOSE (probe))
15525       {
15526         TREE_PURPOSE (parser->unparsed_functions_queues)
15527           = tree_cons (current_class_type, decl,
15528                        TREE_PURPOSE (parser->unparsed_functions_queues));
15529         break;
15530       }
15531   return;
15532 }
15533
15534 /* FN is a FUNCTION_DECL which may contains a parameter with an
15535    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15536    assumes that the current scope is the scope in which the default
15537    argument should be processed.  */
15538
15539 static void
15540 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15541 {
15542   bool saved_local_variables_forbidden_p;
15543   tree parm;
15544
15545   /* While we're parsing the default args, we might (due to the
15546      statement expression extension) encounter more classes.  We want
15547      to handle them right away, but we don't want them getting mixed
15548      up with default args that are currently in the queue.  */
15549   parser->unparsed_functions_queues
15550     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15551
15552   /* Local variable names (and the `this' keyword) may not appear
15553      in a default argument.  */
15554   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15555   parser->local_variables_forbidden_p = true;
15556
15557   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15558        parm;
15559        parm = TREE_CHAIN (parm))
15560     {
15561       cp_token_cache *tokens;
15562
15563       if (!TREE_PURPOSE (parm)
15564           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15565         continue;
15566
15567        /* Push the saved tokens for the default argument onto the parser's
15568           lexer stack.  */
15569       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15570       cp_parser_push_lexer_for_tokens (parser, tokens);
15571
15572       /* Parse the assignment-expression.  */
15573       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser,
15574                                                              /*cast_p=*/false);
15575
15576       /* If the token stream has not been completely used up, then
15577          there was extra junk after the end of the default
15578          argument.  */
15579       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15580         cp_parser_error (parser, "expected %<,%>");
15581
15582       /* Revert to the main lexer.  */
15583       cp_parser_pop_lexer (parser);
15584     }
15585
15586   /* Restore the state of local_variables_forbidden_p.  */
15587   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15588
15589   /* Restore the queue.  */
15590   parser->unparsed_functions_queues
15591     = TREE_CHAIN (parser->unparsed_functions_queues);
15592 }
15593
15594 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15595    either a TYPE or an expression, depending on the form of the
15596    input.  The KEYWORD indicates which kind of expression we have
15597    encountered.  */
15598
15599 static tree
15600 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15601 {
15602   static const char *format;
15603   tree expr = NULL_TREE;
15604   const char *saved_message;
15605   bool saved_integral_constant_expression_p;
15606   bool saved_non_integral_constant_expression_p;
15607
15608   /* Initialize FORMAT the first time we get here.  */
15609   if (!format)
15610     format = "types may not be defined in '%s' expressions";
15611
15612   /* Types cannot be defined in a `sizeof' expression.  Save away the
15613      old message.  */
15614   saved_message = parser->type_definition_forbidden_message;
15615   /* And create the new one.  */
15616   parser->type_definition_forbidden_message
15617     = xmalloc (strlen (format)
15618                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15619                + 1 /* `\0' */);
15620   sprintf ((char *) parser->type_definition_forbidden_message,
15621            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15622
15623   /* The restrictions on constant-expressions do not apply inside
15624      sizeof expressions.  */
15625   saved_integral_constant_expression_p 
15626     = parser->integral_constant_expression_p;
15627   saved_non_integral_constant_expression_p
15628     = parser->non_integral_constant_expression_p;
15629   parser->integral_constant_expression_p = false;
15630
15631   /* Do not actually evaluate the expression.  */
15632   ++skip_evaluation;
15633   /* If it's a `(', then we might be looking at the type-id
15634      construction.  */
15635   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15636     {
15637       tree type;
15638       bool saved_in_type_id_in_expr_p;
15639
15640       /* We can't be sure yet whether we're looking at a type-id or an
15641          expression.  */
15642       cp_parser_parse_tentatively (parser);
15643       /* Consume the `('.  */
15644       cp_lexer_consume_token (parser->lexer);
15645       /* Parse the type-id.  */
15646       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15647       parser->in_type_id_in_expr_p = true;
15648       type = cp_parser_type_id (parser);
15649       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15650       /* Now, look for the trailing `)'.  */
15651       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
15652       /* If all went well, then we're done.  */
15653       if (cp_parser_parse_definitely (parser))
15654         {
15655           cp_decl_specifier_seq decl_specs;
15656
15657           /* Build a trivial decl-specifier-seq.  */
15658           clear_decl_specs (&decl_specs);
15659           decl_specs.type = type;
15660
15661           /* Call grokdeclarator to figure out what type this is.  */
15662           expr = grokdeclarator (NULL,
15663                                  &decl_specs,
15664                                  TYPENAME,
15665                                  /*initialized=*/0,
15666                                  /*attrlist=*/NULL);
15667         }
15668     }
15669
15670   /* If the type-id production did not work out, then we must be
15671      looking at the unary-expression production.  */
15672   if (!expr)
15673     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
15674                                        /*cast_p=*/false);
15675   /* Go back to evaluating expressions.  */
15676   --skip_evaluation;
15677
15678   /* Free the message we created.  */
15679   free ((char *) parser->type_definition_forbidden_message);
15680   /* And restore the old one.  */
15681   parser->type_definition_forbidden_message = saved_message;
15682   parser->integral_constant_expression_p 
15683     = saved_integral_constant_expression_p;
15684   parser->non_integral_constant_expression_p
15685     = saved_non_integral_constant_expression_p;
15686
15687   return expr;
15688 }
15689
15690 /* If the current declaration has no declarator, return true.  */
15691
15692 static bool
15693 cp_parser_declares_only_class_p (cp_parser *parser)
15694 {
15695   /* If the next token is a `;' or a `,' then there is no
15696      declarator.  */
15697   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15698           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15699 }
15700
15701 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15702
15703 static void
15704 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15705                              cp_storage_class storage_class)
15706 {
15707   if (decl_specs->storage_class != sc_none)
15708     decl_specs->multiple_storage_classes_p = true;
15709   else
15710     decl_specs->storage_class = storage_class;
15711 }
15712
15713 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15714    is true, the type is a user-defined type; otherwise it is a
15715    built-in type specified by a keyword.  */
15716
15717 static void
15718 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15719                               tree type_spec,
15720                               bool user_defined_p)
15721 {
15722   decl_specs->any_specifiers_p = true;
15723
15724   /* If the user tries to redeclare bool or wchar_t (with, for
15725      example, in "typedef int wchar_t;") we remember that this is what
15726      happened.  In system headers, we ignore these declarations so
15727      that G++ can work with system headers that are not C++-safe.  */
15728   if (decl_specs->specs[(int) ds_typedef]
15729       && !user_defined_p
15730       && (type_spec == boolean_type_node
15731           || type_spec == wchar_type_node)
15732       && (decl_specs->type
15733           || decl_specs->specs[(int) ds_long]
15734           || decl_specs->specs[(int) ds_short]
15735           || decl_specs->specs[(int) ds_unsigned]
15736           || decl_specs->specs[(int) ds_signed]))
15737     {
15738       decl_specs->redefined_builtin_type = type_spec;
15739       if (!decl_specs->type)
15740         {
15741           decl_specs->type = type_spec;
15742           decl_specs->user_defined_type_p = false;
15743         }
15744     }
15745   else if (decl_specs->type)
15746     decl_specs->multiple_types_p = true;
15747   else
15748     {
15749       decl_specs->type = type_spec;
15750       decl_specs->user_defined_type_p = user_defined_p;
15751       decl_specs->redefined_builtin_type = NULL_TREE;
15752     }
15753 }
15754
15755 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15756    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15757
15758 static bool
15759 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15760 {
15761   return decl_specifiers->specs[(int) ds_friend] != 0;
15762 }
15763
15764 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15765    issue an error message indicating that TOKEN_DESC was expected.
15766
15767    Returns the token consumed, if the token had the appropriate type.
15768    Otherwise, returns NULL.  */
15769
15770 static cp_token *
15771 cp_parser_require (cp_parser* parser,
15772                    enum cpp_ttype type,
15773                    const char* token_desc)
15774 {
15775   if (cp_lexer_next_token_is (parser->lexer, type))
15776     return cp_lexer_consume_token (parser->lexer);
15777   else
15778     {
15779       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15780       if (!cp_parser_simulate_error (parser))
15781         {
15782           char *message = concat ("expected ", token_desc, NULL);
15783           cp_parser_error (parser, message);
15784           free (message);
15785         }
15786       return NULL;
15787     }
15788 }
15789
15790 /* Like cp_parser_require, except that tokens will be skipped until
15791    the desired token is found.  An error message is still produced if
15792    the next token is not as expected.  */
15793
15794 static void
15795 cp_parser_skip_until_found (cp_parser* parser,
15796                             enum cpp_ttype type,
15797                             const char* token_desc)
15798 {
15799   cp_token *token;
15800   unsigned nesting_depth = 0;
15801
15802   if (cp_parser_require (parser, type, token_desc))
15803     return;
15804
15805   /* Skip tokens until the desired token is found.  */
15806   while (true)
15807     {
15808       /* Peek at the next token.  */
15809       token = cp_lexer_peek_token (parser->lexer);
15810       /* If we've reached the token we want, consume it and
15811          stop.  */
15812       if (token->type == type && !nesting_depth)
15813         {
15814           cp_lexer_consume_token (parser->lexer);
15815           return;
15816         }
15817       /* If we've run out of tokens, stop.  */
15818       if (token->type == CPP_EOF)
15819         return;
15820       if (token->type == CPP_OPEN_BRACE
15821           || token->type == CPP_OPEN_PAREN
15822           || token->type == CPP_OPEN_SQUARE)
15823         ++nesting_depth;
15824       else if (token->type == CPP_CLOSE_BRACE
15825                || token->type == CPP_CLOSE_PAREN
15826                || token->type == CPP_CLOSE_SQUARE)
15827         {
15828           if (nesting_depth-- == 0)
15829             return;
15830         }
15831       /* Consume this token.  */
15832       cp_lexer_consume_token (parser->lexer);
15833     }
15834 }
15835
15836 /* If the next token is the indicated keyword, consume it.  Otherwise,
15837    issue an error message indicating that TOKEN_DESC was expected.
15838
15839    Returns the token consumed, if the token had the appropriate type.
15840    Otherwise, returns NULL.  */
15841
15842 static cp_token *
15843 cp_parser_require_keyword (cp_parser* parser,
15844                            enum rid keyword,
15845                            const char* token_desc)
15846 {
15847   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15848
15849   if (token && token->keyword != keyword)
15850     {
15851       dyn_string_t error_msg;
15852
15853       /* Format the error message.  */
15854       error_msg = dyn_string_new (0);
15855       dyn_string_append_cstr (error_msg, "expected ");
15856       dyn_string_append_cstr (error_msg, token_desc);
15857       cp_parser_error (parser, error_msg->s);
15858       dyn_string_delete (error_msg);
15859       return NULL;
15860     }
15861
15862   return token;
15863 }
15864
15865 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15866    function-definition.  */
15867
15868 static bool
15869 cp_parser_token_starts_function_definition_p (cp_token* token)
15870 {
15871   return (/* An ordinary function-body begins with an `{'.  */
15872           token->type == CPP_OPEN_BRACE
15873           /* A ctor-initializer begins with a `:'.  */
15874           || token->type == CPP_COLON
15875           /* A function-try-block begins with `try'.  */
15876           || token->keyword == RID_TRY
15877           /* The named return value extension begins with `return'.  */
15878           || token->keyword == RID_RETURN);
15879 }
15880
15881 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15882    definition.  */
15883
15884 static bool
15885 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15886 {
15887   cp_token *token;
15888
15889   token = cp_lexer_peek_token (parser->lexer);
15890   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15891 }
15892
15893 /* Returns TRUE iff the next token is the "," or ">" ending a
15894    template-argument.  */
15895
15896 static bool
15897 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15898 {
15899   cp_token *token;
15900
15901   token = cp_lexer_peek_token (parser->lexer);
15902   return (token->type == CPP_COMMA || token->type == CPP_GREATER);
15903 }
15904
15905 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15906    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15907
15908 static bool
15909 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15910                                                      size_t n)
15911 {
15912   cp_token *token;
15913
15914   token = cp_lexer_peek_nth_token (parser->lexer, n);
15915   if (token->type == CPP_LESS)
15916     return true;
15917   /* Check for the sequence `<::' in the original code. It would be lexed as
15918      `[:', where `[' is a digraph, and there is no whitespace before
15919      `:'.  */
15920   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15921     {
15922       cp_token *token2;
15923       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15924       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15925         return true;
15926     }
15927   return false;
15928 }
15929
15930 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15931    or none_type otherwise.  */
15932
15933 static enum tag_types
15934 cp_parser_token_is_class_key (cp_token* token)
15935 {
15936   switch (token->keyword)
15937     {
15938     case RID_CLASS:
15939       return class_type;
15940     case RID_STRUCT:
15941       return record_type;
15942     case RID_UNION:
15943       return union_type;
15944
15945     default:
15946       return none_type;
15947     }
15948 }
15949
15950 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15951
15952 static void
15953 cp_parser_check_class_key (enum tag_types class_key, tree type)
15954 {
15955   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15956     pedwarn ("%qs tag used in naming %q#T",
15957             class_key == union_type ? "union"
15958              : class_key == record_type ? "struct" : "class",
15959              type);
15960 }
15961
15962 /* Issue an error message if DECL is redeclared with different
15963    access than its original declaration [class.access.spec/3].
15964    This applies to nested classes and nested class templates.
15965    [class.mem/1].  */
15966
15967 static void
15968 cp_parser_check_access_in_redeclaration (tree decl)
15969 {
15970   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15971     return;
15972
15973   if ((TREE_PRIVATE (decl)
15974        != (current_access_specifier == access_private_node))
15975       || (TREE_PROTECTED (decl)
15976           != (current_access_specifier == access_protected_node)))
15977     error ("%qD redeclared with different access", decl);
15978 }
15979
15980 /* Look for the `template' keyword, as a syntactic disambiguator.
15981    Return TRUE iff it is present, in which case it will be
15982    consumed.  */
15983
15984 static bool
15985 cp_parser_optional_template_keyword (cp_parser *parser)
15986 {
15987   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15988     {
15989       /* The `template' keyword can only be used within templates;
15990          outside templates the parser can always figure out what is a
15991          template and what is not.  */
15992       if (!processing_template_decl)
15993         {
15994           error ("%<template%> (as a disambiguator) is only allowed "
15995                  "within templates");
15996           /* If this part of the token stream is rescanned, the same
15997              error message would be generated.  So, we purge the token
15998              from the stream.  */
15999           cp_lexer_purge_token (parser->lexer);
16000           return false;
16001         }
16002       else
16003         {
16004           /* Consume the `template' keyword.  */
16005           cp_lexer_consume_token (parser->lexer);
16006           return true;
16007         }
16008     }
16009
16010   return false;
16011 }
16012
16013 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
16014    set PARSER->SCOPE, and perform other related actions.  */
16015
16016 static void
16017 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
16018 {
16019   tree value;
16020   tree check;
16021
16022   /* Get the stored value.  */
16023   value = cp_lexer_consume_token (parser->lexer)->value;
16024   /* Perform any access checks that were deferred.  */
16025   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
16026     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
16027   /* Set the scope from the stored value.  */
16028   parser->scope = TREE_VALUE (value);
16029   parser->qualifying_scope = TREE_TYPE (value);
16030   parser->object_scope = NULL_TREE;
16031 }
16032
16033 /* Consume tokens up through a non-nested END token.  */
16034
16035 static void
16036 cp_parser_cache_group (cp_parser *parser,
16037                        enum cpp_ttype end,
16038                        unsigned depth)
16039 {
16040   while (true)
16041     {
16042       cp_token *token;
16043
16044       /* Abort a parenthesized expression if we encounter a brace.  */
16045       if ((end == CPP_CLOSE_PAREN || depth == 0)
16046           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16047         return;
16048       /* If we've reached the end of the file, stop.  */
16049       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
16050         return;
16051       /* Consume the next token.  */
16052       token = cp_lexer_consume_token (parser->lexer);
16053       /* See if it starts a new group.  */
16054       if (token->type == CPP_OPEN_BRACE)
16055         {
16056           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
16057           if (depth == 0)
16058             return;
16059         }
16060       else if (token->type == CPP_OPEN_PAREN)
16061         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
16062       else if (token->type == end)
16063         return;
16064     }
16065 }
16066
16067 /* Begin parsing tentatively.  We always save tokens while parsing
16068    tentatively so that if the tentative parsing fails we can restore the
16069    tokens.  */
16070
16071 static void
16072 cp_parser_parse_tentatively (cp_parser* parser)
16073 {
16074   /* Enter a new parsing context.  */
16075   parser->context = cp_parser_context_new (parser->context);
16076   /* Begin saving tokens.  */
16077   cp_lexer_save_tokens (parser->lexer);
16078   /* In order to avoid repetitive access control error messages,
16079      access checks are queued up until we are no longer parsing
16080      tentatively.  */
16081   push_deferring_access_checks (dk_deferred);
16082 }
16083
16084 /* Commit to the currently active tentative parse.  */
16085
16086 static void
16087 cp_parser_commit_to_tentative_parse (cp_parser* parser)
16088 {
16089   cp_parser_context *context;
16090   cp_lexer *lexer;
16091
16092   /* Mark all of the levels as committed.  */
16093   lexer = parser->lexer;
16094   for (context = parser->context; context->next; context = context->next)
16095     {
16096       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
16097         break;
16098       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
16099       while (!cp_lexer_saving_tokens (lexer))
16100         lexer = lexer->next;
16101       cp_lexer_commit_tokens (lexer);
16102     }
16103 }
16104
16105 /* Abort the currently active tentative parse.  All consumed tokens
16106    will be rolled back, and no diagnostics will be issued.  */
16107
16108 static void
16109 cp_parser_abort_tentative_parse (cp_parser* parser)
16110 {
16111   cp_parser_simulate_error (parser);
16112   /* Now, pretend that we want to see if the construct was
16113      successfully parsed.  */
16114   cp_parser_parse_definitely (parser);
16115 }
16116
16117 /* Stop parsing tentatively.  If a parse error has occurred, restore the
16118    token stream.  Otherwise, commit to the tokens we have consumed.
16119    Returns true if no error occurred; false otherwise.  */
16120
16121 static bool
16122 cp_parser_parse_definitely (cp_parser* parser)
16123 {
16124   bool error_occurred;
16125   cp_parser_context *context;
16126
16127   /* Remember whether or not an error occurred, since we are about to
16128      destroy that information.  */
16129   error_occurred = cp_parser_error_occurred (parser);
16130   /* Remove the topmost context from the stack.  */
16131   context = parser->context;
16132   parser->context = context->next;
16133   /* If no parse errors occurred, commit to the tentative parse.  */
16134   if (!error_occurred)
16135     {
16136       /* Commit to the tokens read tentatively, unless that was
16137          already done.  */
16138       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
16139         cp_lexer_commit_tokens (parser->lexer);
16140
16141       pop_to_parent_deferring_access_checks ();
16142     }
16143   /* Otherwise, if errors occurred, roll back our state so that things
16144      are just as they were before we began the tentative parse.  */
16145   else
16146     {
16147       cp_lexer_rollback_tokens (parser->lexer);
16148       pop_deferring_access_checks ();
16149     }
16150   /* Add the context to the front of the free list.  */
16151   context->next = cp_parser_context_free_list;
16152   cp_parser_context_free_list = context;
16153
16154   return !error_occurred;
16155 }
16156
16157 /* Returns true if we are parsing tentatively and are not committed to
16158    this tentative parse.  */
16159
16160 static bool
16161 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
16162 {
16163   return (cp_parser_parsing_tentatively (parser)
16164           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
16165 }
16166
16167 /* Returns nonzero iff an error has occurred during the most recent
16168    tentative parse.  */
16169
16170 static bool
16171 cp_parser_error_occurred (cp_parser* parser)
16172 {
16173   return (cp_parser_parsing_tentatively (parser)
16174           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
16175 }
16176
16177 /* Returns nonzero if GNU extensions are allowed.  */
16178
16179 static bool
16180 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
16181 {
16182   return parser->allow_gnu_extensions_p;
16183 }
16184 \f
16185 /* Objective-C++ Productions */
16186
16187
16188 /* Parse an Objective-C expression, which feeds into a primary-expression
16189    above.
16190
16191    objc-expression:
16192      objc-message-expression
16193      objc-string-literal
16194      objc-encode-expression
16195      objc-protocol-expression
16196      objc-selector-expression
16197
16198   Returns a tree representation of the expression.  */
16199
16200 static tree
16201 cp_parser_objc_expression (cp_parser* parser)
16202 {
16203   /* Try to figure out what kind of declaration is present.  */
16204   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
16205
16206   switch (kwd->type)
16207     {
16208     case CPP_OPEN_SQUARE:
16209       return cp_parser_objc_message_expression (parser);
16210
16211     case CPP_OBJC_STRING:
16212       kwd = cp_lexer_consume_token (parser->lexer);
16213       return objc_build_string_object (kwd->value);
16214
16215     case CPP_KEYWORD:
16216       switch (kwd->keyword)
16217         {
16218         case RID_AT_ENCODE:
16219           return cp_parser_objc_encode_expression (parser);
16220
16221         case RID_AT_PROTOCOL:
16222           return cp_parser_objc_protocol_expression (parser);
16223
16224         case RID_AT_SELECTOR:
16225           return cp_parser_objc_selector_expression (parser);
16226
16227         default:
16228           break;
16229         }
16230     default:
16231       error ("misplaced `@%D' Objective-C++ construct", kwd->value);
16232       cp_parser_skip_to_end_of_block_or_statement (parser);
16233     }
16234
16235   return error_mark_node;
16236 }
16237
16238 /* Parse an Objective-C message expression.
16239
16240    objc-message-expression:
16241      [ objc-message-receiver objc-message-args ]
16242
16243    Returns a representation of an Objective-C message.  */
16244
16245 static tree
16246 cp_parser_objc_message_expression (cp_parser* parser)
16247 {
16248   tree receiver, messageargs;
16249
16250   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
16251   receiver = cp_parser_objc_message_receiver (parser);
16252   messageargs = cp_parser_objc_message_args (parser);
16253   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
16254
16255   return objc_build_message_expr (build_tree_list (receiver, messageargs));
16256 }
16257
16258 /* Parse an objc-message-receiver.
16259
16260    objc-message-receiver:
16261      expression
16262      simple-type-specifier
16263
16264   Returns a representation of the type or expression.  */
16265
16266 static tree
16267 cp_parser_objc_message_receiver (cp_parser* parser)
16268 {
16269   tree rcv;
16270
16271   /* An Objective-C message receiver may be either (1) a type
16272      or (2) an expression.  */
16273   cp_parser_parse_tentatively (parser);
16274   rcv = cp_parser_expression (parser, false);
16275
16276   if (cp_parser_parse_definitely (parser))
16277     return rcv;
16278
16279   rcv = cp_parser_simple_type_specifier (parser,
16280                                          /*decl_specs=*/NULL,
16281                                          CP_PARSER_FLAGS_NONE);
16282
16283   return objc_get_class_reference (rcv);
16284 }
16285
16286 /* Parse the arguments and selectors comprising an Objective-C message.
16287
16288    objc-message-args:
16289      objc-selector
16290      objc-selector-args
16291      objc-selector-args , objc-comma-args
16292
16293    objc-selector-args:
16294      objc-selector [opt] : assignment-expression
16295      objc-selector-args objc-selector [opt] : assignment-expression
16296
16297    objc-comma-args:
16298      assignment-expression
16299      objc-comma-args , assignment-expression
16300
16301    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
16302    selector arguments and TREE_VALUE containing a list of comma
16303    arguments.  */
16304
16305 static tree
16306 cp_parser_objc_message_args (cp_parser* parser)
16307 {
16308   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
16309   bool maybe_unary_selector_p = true;
16310   cp_token *token = cp_lexer_peek_token (parser->lexer);
16311
16312   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16313     {
16314       tree selector = NULL_TREE, arg;
16315
16316       if (token->type != CPP_COLON)
16317         selector = cp_parser_objc_selector (parser);
16318
16319       /* Detect if we have a unary selector.  */
16320       if (maybe_unary_selector_p
16321           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16322         return build_tree_list (selector, NULL_TREE);
16323
16324       maybe_unary_selector_p = false;
16325       cp_parser_require (parser, CPP_COLON, "`:'");
16326       arg = cp_parser_assignment_expression (parser, false);
16327
16328       sel_args
16329         = chainon (sel_args,
16330                    build_tree_list (selector, arg));
16331
16332       token = cp_lexer_peek_token (parser->lexer);
16333     }
16334
16335   /* Handle non-selector arguments, if any. */
16336   while (token->type == CPP_COMMA)
16337     {
16338       tree arg;
16339
16340       cp_lexer_consume_token (parser->lexer);
16341       arg = cp_parser_assignment_expression (parser, false);
16342
16343       addl_args
16344         = chainon (addl_args,
16345                    build_tree_list (NULL_TREE, arg));
16346
16347       token = cp_lexer_peek_token (parser->lexer);
16348     }
16349
16350   return build_tree_list (sel_args, addl_args);
16351 }
16352
16353 /* Parse an Objective-C encode expression.
16354
16355    objc-encode-expression:
16356      @encode objc-typename
16357      
16358    Returns an encoded representation of the type argument.  */
16359
16360 static tree
16361 cp_parser_objc_encode_expression (cp_parser* parser)
16362 {
16363   tree type;
16364
16365   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
16366   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16367   type = complete_type (cp_parser_type_id (parser));
16368   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16369
16370   if (!type)
16371     {
16372       error ("`@encode' must specify a type as an argument");
16373       return error_mark_node;
16374     }
16375
16376   return objc_build_encode_expr (type);
16377 }
16378
16379 /* Parse an Objective-C @defs expression.  */
16380
16381 static tree
16382 cp_parser_objc_defs_expression (cp_parser *parser)
16383 {
16384   tree name;
16385
16386   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
16387   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16388   name = cp_parser_identifier (parser);
16389   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16390
16391   return objc_get_class_ivars (name);
16392 }
16393
16394 /* Parse an Objective-C protocol expression.
16395
16396   objc-protocol-expression:
16397     @protocol ( identifier )
16398
16399   Returns a representation of the protocol expression.  */
16400
16401 static tree
16402 cp_parser_objc_protocol_expression (cp_parser* parser)
16403 {
16404   tree proto;
16405
16406   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16407   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16408   proto = cp_parser_identifier (parser);
16409   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16410
16411   return objc_build_protocol_expr (proto);
16412 }
16413
16414 /* Parse an Objective-C selector expression.
16415
16416    objc-selector-expression:
16417      @selector ( objc-method-signature )
16418
16419    objc-method-signature:
16420      objc-selector
16421      objc-selector-seq
16422
16423    objc-selector-seq:
16424      objc-selector :
16425      objc-selector-seq objc-selector :
16426
16427   Returns a representation of the method selector.  */
16428
16429 static tree
16430 cp_parser_objc_selector_expression (cp_parser* parser)
16431 {
16432   tree sel_seq = NULL_TREE;
16433   bool maybe_unary_selector_p = true;
16434   cp_token *token;
16435
16436   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
16437   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
16438   token = cp_lexer_peek_token (parser->lexer);
16439
16440   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16441     {
16442       tree selector = NULL_TREE;
16443
16444       if (token->type != CPP_COLON)
16445         selector = cp_parser_objc_selector (parser);
16446
16447       /* Detect if we have a unary selector.  */
16448       if (maybe_unary_selector_p
16449           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16450         {
16451           sel_seq = selector;
16452           goto finish_selector;
16453         }
16454
16455       maybe_unary_selector_p = false;
16456       cp_parser_require (parser, CPP_COLON, "`:'");
16457
16458       sel_seq
16459         = chainon (sel_seq,
16460                    build_tree_list (selector, NULL_TREE));
16461
16462       token = cp_lexer_peek_token (parser->lexer);
16463     }
16464
16465  finish_selector:
16466   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16467
16468   return objc_build_selector_expr (sel_seq);
16469 }
16470
16471 /* Parse a list of identifiers.
16472
16473    objc-identifier-list:
16474      identifier
16475      objc-identifier-list , identifier
16476
16477    Returns a TREE_LIST of identifier nodes.  */
16478
16479 static tree
16480 cp_parser_objc_identifier_list (cp_parser* parser)
16481 {
16482   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
16483   cp_token *sep = cp_lexer_peek_token (parser->lexer);
16484
16485   while (sep->type == CPP_COMMA)
16486     {
16487       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16488       list = chainon (list, 
16489                       build_tree_list (NULL_TREE,
16490                                        cp_parser_identifier (parser)));
16491       sep = cp_lexer_peek_token (parser->lexer);
16492     }
16493     
16494   return list;
16495 }
16496
16497 /* Parse an Objective-C alias declaration.
16498
16499    objc-alias-declaration:
16500      @compatibility_alias identifier identifier ;
16501
16502    This function registers the alias mapping with the Objective-C front-end.
16503    It returns nothing.  */
16504
16505 static void
16506 cp_parser_objc_alias_declaration (cp_parser* parser)
16507 {
16508   tree alias, orig;
16509
16510   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
16511   alias = cp_parser_identifier (parser);
16512   orig = cp_parser_identifier (parser);
16513   objc_declare_alias (alias, orig);
16514   cp_parser_consume_semicolon_at_end_of_statement (parser);
16515 }
16516
16517 /* Parse an Objective-C class forward-declaration.
16518
16519    objc-class-declaration:
16520      @class objc-identifier-list ;
16521
16522    The function registers the forward declarations with the Objective-C
16523    front-end.  It returns nothing.  */
16524
16525 static void
16526 cp_parser_objc_class_declaration (cp_parser* parser)
16527 {
16528   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
16529   objc_declare_class (cp_parser_objc_identifier_list (parser));
16530   cp_parser_consume_semicolon_at_end_of_statement (parser);
16531 }
16532
16533 /* Parse a list of Objective-C protocol references.
16534
16535    objc-protocol-refs-opt:
16536      objc-protocol-refs [opt]
16537
16538    objc-protocol-refs:
16539      < objc-identifier-list >
16540
16541    Returns a TREE_LIST of identifiers, if any.  */
16542
16543 static tree
16544 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
16545 {
16546   tree protorefs = NULL_TREE;
16547
16548   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
16549     {
16550       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
16551       protorefs = cp_parser_objc_identifier_list (parser);
16552       cp_parser_require (parser, CPP_GREATER, "`>'");
16553     }
16554
16555   return protorefs;
16556 }
16557
16558 /* Parse a Objective-C visibility specification.  */
16559
16560 static void
16561 cp_parser_objc_visibility_spec (cp_parser* parser)
16562 {
16563   cp_token *vis = cp_lexer_peek_token (parser->lexer);
16564
16565   switch (vis->keyword)
16566     {
16567     case RID_AT_PRIVATE:
16568       objc_set_visibility (2);
16569       break;
16570     case RID_AT_PROTECTED:
16571       objc_set_visibility (0);
16572       break;
16573     case RID_AT_PUBLIC:
16574       objc_set_visibility (1);
16575       break;
16576     default:
16577       return;
16578     }
16579
16580   /* Eat '@private'/'@protected'/'@public'.  */
16581   cp_lexer_consume_token (parser->lexer);
16582 }
16583
16584 /* Parse an Objective-C method type.  */
16585
16586 static void
16587 cp_parser_objc_method_type (cp_parser* parser)
16588 {
16589   objc_set_method_type
16590    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
16591     ? PLUS_EXPR
16592     : MINUS_EXPR);
16593 }
16594
16595 /* Parse an Objective-C protocol qualifier.  */
16596
16597 static tree
16598 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
16599 {
16600   tree quals = NULL_TREE, node;
16601   cp_token *token = cp_lexer_peek_token (parser->lexer);
16602
16603   node = token->value;
16604
16605   while (node && TREE_CODE (node) == IDENTIFIER_NODE
16606          && (node == ridpointers [(int) RID_IN]
16607              || node == ridpointers [(int) RID_OUT]
16608              || node == ridpointers [(int) RID_INOUT]
16609              || node == ridpointers [(int) RID_BYCOPY]
16610              || node == ridpointers [(int) RID_BYREF]
16611              || node == ridpointers [(int) RID_ONEWAY]))
16612     {
16613       quals = tree_cons (NULL_TREE, node, quals);
16614       cp_lexer_consume_token (parser->lexer);
16615       token = cp_lexer_peek_token (parser->lexer);
16616       node = token->value;
16617     }
16618
16619   return quals;
16620 }
16621
16622 /* Parse an Objective-C typename.  */
16623
16624 static tree
16625 cp_parser_objc_typename (cp_parser* parser)
16626 {
16627   tree typename = NULL_TREE;
16628
16629   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
16630     {
16631       tree proto_quals, cp_type = NULL_TREE;
16632
16633       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
16634       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
16635
16636       /* An ObjC type name may consist of just protocol qualifiers, in which
16637          case the type shall default to 'id'.  */
16638       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
16639         cp_type = cp_parser_type_id (parser);
16640
16641       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
16642       typename = build_tree_list (proto_quals, cp_type);
16643     }
16644
16645   return typename;
16646 }
16647
16648 /* Check to see if TYPE refers to an Objective-C selector name.  */
16649
16650 static bool
16651 cp_parser_objc_selector_p (enum cpp_ttype type)
16652 {
16653   return (type == CPP_NAME || type == CPP_KEYWORD
16654           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
16655           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
16656           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
16657           || type == CPP_XOR || type == CPP_XOR_EQ);
16658 }
16659
16660 /* Parse an Objective-C selector.  */
16661
16662 static tree
16663 cp_parser_objc_selector (cp_parser* parser)
16664 {
16665   cp_token *token = cp_lexer_consume_token (parser->lexer);
16666   
16667   if (!cp_parser_objc_selector_p (token->type))
16668     {
16669       error ("invalid Objective-C++ selector name");
16670       return error_mark_node;
16671     }
16672
16673   /* C++ operator names are allowed to appear in ObjC selectors.  */
16674   switch (token->type)
16675     {
16676     case CPP_AND_AND: return get_identifier ("and");
16677     case CPP_AND_EQ: return get_identifier ("and_eq");
16678     case CPP_AND: return get_identifier ("bitand");
16679     case CPP_OR: return get_identifier ("bitor");
16680     case CPP_COMPL: return get_identifier ("compl");
16681     case CPP_NOT: return get_identifier ("not");
16682     case CPP_NOT_EQ: return get_identifier ("not_eq");
16683     case CPP_OR_OR: return get_identifier ("or");
16684     case CPP_OR_EQ: return get_identifier ("or_eq");
16685     case CPP_XOR: return get_identifier ("xor");
16686     case CPP_XOR_EQ: return get_identifier ("xor_eq");
16687     default: return token->value;
16688     }
16689 }
16690
16691 /* Parse an Objective-C params list.  */
16692
16693 static tree
16694 cp_parser_objc_method_keyword_params (cp_parser* parser)
16695 {
16696   tree params = NULL_TREE;
16697   bool maybe_unary_selector_p = true;
16698   cp_token *token = cp_lexer_peek_token (parser->lexer);
16699
16700   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
16701     {
16702       tree selector = NULL_TREE, typename, identifier;
16703
16704       if (token->type != CPP_COLON)
16705         selector = cp_parser_objc_selector (parser);
16706
16707       /* Detect if we have a unary selector.  */
16708       if (maybe_unary_selector_p
16709           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
16710         return selector;
16711
16712       maybe_unary_selector_p = false;
16713       cp_parser_require (parser, CPP_COLON, "`:'");
16714       typename = cp_parser_objc_typename (parser);
16715       identifier = cp_parser_identifier (parser);
16716
16717       params
16718         = chainon (params,
16719                    objc_build_keyword_decl (selector, 
16720                                             typename,
16721                                             identifier));
16722
16723       token = cp_lexer_peek_token (parser->lexer);
16724     }
16725
16726   return params;
16727 }
16728
16729 /* Parse the non-keyword Objective-C params.  */
16730
16731 static tree
16732 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
16733 {
16734   tree params = make_node (TREE_LIST);
16735   cp_token *token = cp_lexer_peek_token (parser->lexer);
16736   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
16737
16738   while (token->type == CPP_COMMA)
16739     {
16740       cp_parameter_declarator *parmdecl;
16741       tree parm;
16742
16743       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16744       token = cp_lexer_peek_token (parser->lexer);
16745
16746       if (token->type == CPP_ELLIPSIS)
16747         {
16748           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
16749           *ellipsisp = true;
16750           break;
16751         }
16752
16753       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
16754       parm = grokdeclarator (parmdecl->declarator,
16755                              &parmdecl->decl_specifiers,
16756                              PARM, /*initialized=*/0, 
16757                              /*attrlist=*/NULL);
16758
16759       chainon (params, build_tree_list (NULL_TREE, parm));
16760       token = cp_lexer_peek_token (parser->lexer);
16761     }
16762
16763   return params;
16764 }
16765
16766 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
16767
16768 static void
16769 cp_parser_objc_interstitial_code (cp_parser* parser)
16770 {
16771   cp_token *token = cp_lexer_peek_token (parser->lexer);
16772
16773   /* If the next token is `extern' and the following token is a string
16774      literal, then we have a linkage specification.  */
16775   if (token->keyword == RID_EXTERN
16776       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
16777     cp_parser_linkage_specification (parser);
16778   /* Handle #pragma, if any.  */
16779   else if (token->type == CPP_PRAGMA)
16780     cp_lexer_handle_pragma (parser->lexer);
16781   /* Allow stray semicolons.  */
16782   else if (token->type == CPP_SEMICOLON)
16783     cp_lexer_consume_token (parser->lexer);
16784   /* Finally, try to parse a block-declaration, or a function-definition.  */
16785   else
16786     cp_parser_block_declaration (parser, /*statement_p=*/false);
16787 }
16788
16789 /* Parse a method signature.  */
16790
16791 static tree
16792 cp_parser_objc_method_signature (cp_parser* parser)
16793 {
16794   tree rettype, kwdparms, optparms;
16795   bool ellipsis = false;
16796
16797   cp_parser_objc_method_type (parser);
16798   rettype = cp_parser_objc_typename (parser);
16799   kwdparms = cp_parser_objc_method_keyword_params (parser);
16800   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
16801
16802   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
16803 }
16804
16805 /* Pars an Objective-C method prototype list.  */
16806
16807 static void
16808 cp_parser_objc_method_prototype_list (cp_parser* parser)
16809 {
16810   cp_token *token = cp_lexer_peek_token (parser->lexer);
16811
16812   while (token->keyword != RID_AT_END)
16813     {
16814       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16815         {
16816           objc_add_method_declaration
16817            (cp_parser_objc_method_signature (parser));
16818           cp_parser_consume_semicolon_at_end_of_statement (parser);
16819         }
16820       else
16821         /* Allow for interspersed non-ObjC++ code.  */
16822         cp_parser_objc_interstitial_code (parser);
16823
16824       token = cp_lexer_peek_token (parser->lexer);
16825     }
16826
16827   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16828   objc_finish_interface ();
16829 }
16830
16831 /* Parse an Objective-C method definition list.  */
16832
16833 static void
16834 cp_parser_objc_method_definition_list (cp_parser* parser)
16835 {
16836   cp_token *token = cp_lexer_peek_token (parser->lexer);
16837
16838   while (token->keyword != RID_AT_END)
16839     {
16840       tree meth;
16841
16842       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
16843         {
16844           push_deferring_access_checks (dk_deferred);
16845           objc_start_method_definition
16846            (cp_parser_objc_method_signature (parser));
16847
16848           /* For historical reasons, we accept an optional semicolon.  */
16849           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16850             cp_lexer_consume_token (parser->lexer);
16851
16852           perform_deferred_access_checks ();
16853           stop_deferring_access_checks ();
16854           meth = cp_parser_function_definition_after_declarator (parser,
16855                                                                  false);
16856           pop_deferring_access_checks ();
16857           objc_finish_method_definition (meth);
16858         }
16859       else
16860         /* Allow for interspersed non-ObjC++ code.  */
16861         cp_parser_objc_interstitial_code (parser);
16862
16863       token = cp_lexer_peek_token (parser->lexer);
16864     }
16865
16866   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
16867   objc_finish_implementation ();
16868 }
16869
16870 /* Parse Objective-C ivars.  */
16871
16872 static void
16873 cp_parser_objc_class_ivars (cp_parser* parser)
16874 {
16875   cp_token *token = cp_lexer_peek_token (parser->lexer);
16876
16877   if (token->type != CPP_OPEN_BRACE)
16878     return;     /* No ivars specified.  */
16879
16880   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
16881   token = cp_lexer_peek_token (parser->lexer);
16882
16883   while (token->type != CPP_CLOSE_BRACE)
16884     {
16885       cp_decl_specifier_seq declspecs;
16886       int decl_class_or_enum_p;
16887       tree prefix_attributes;
16888
16889       cp_parser_objc_visibility_spec (parser);
16890
16891       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
16892         break;
16893
16894       cp_parser_decl_specifier_seq (parser,
16895                                     CP_PARSER_FLAGS_OPTIONAL,
16896                                     &declspecs,
16897                                     &decl_class_or_enum_p);
16898       prefix_attributes = declspecs.attributes;
16899       declspecs.attributes = NULL_TREE;
16900
16901       /* Keep going until we hit the `;' at the end of the
16902          declaration.  */
16903       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16904         {
16905           tree width = NULL_TREE, attributes, first_attribute, decl;
16906           cp_declarator *declarator = NULL;
16907           int ctor_dtor_or_conv_p;
16908
16909           /* Check for a (possibly unnamed) bitfield declaration.  */
16910           token = cp_lexer_peek_token (parser->lexer);
16911           if (token->type == CPP_COLON)
16912             goto eat_colon;
16913
16914           if (token->type == CPP_NAME
16915               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16916                   == CPP_COLON))
16917             {
16918               /* Get the name of the bitfield.  */
16919               declarator = make_id_declarator (NULL_TREE,
16920                                                cp_parser_identifier (parser));
16921
16922              eat_colon:
16923               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
16924               /* Get the width of the bitfield.  */
16925               width
16926                 = cp_parser_constant_expression (parser,
16927                                                  /*allow_non_constant=*/false,
16928                                                  NULL);
16929             }
16930           else
16931             {
16932               /* Parse the declarator.  */
16933               declarator 
16934                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16935                                         &ctor_dtor_or_conv_p,
16936                                         /*parenthesized_p=*/NULL,
16937                                         /*member_p=*/false);
16938             }
16939
16940           /* Look for attributes that apply to the ivar.  */
16941           attributes = cp_parser_attributes_opt (parser);
16942           /* Remember which attributes are prefix attributes and
16943              which are not.  */
16944           first_attribute = attributes;
16945           /* Combine the attributes.  */
16946           attributes = chainon (prefix_attributes, attributes);
16947
16948           if (width)
16949             {
16950               /* Create the bitfield declaration.  */
16951               decl = grokbitfield (declarator, &declspecs, width);
16952               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
16953             }
16954           else
16955             decl = grokfield (declarator, &declspecs, NULL_TREE,
16956                               NULL_TREE, attributes);
16957           
16958           /* Add the instance variable.  */
16959           objc_add_instance_variable (decl);
16960
16961           /* Reset PREFIX_ATTRIBUTES.  */
16962           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16963             attributes = TREE_CHAIN (attributes);
16964           if (attributes)
16965             TREE_CHAIN (attributes) = NULL_TREE;
16966
16967           token = cp_lexer_peek_token (parser->lexer);
16968
16969           if (token->type == CPP_COMMA)
16970             {
16971               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
16972               continue;
16973             }
16974           break;
16975         }
16976
16977       cp_parser_consume_semicolon_at_end_of_statement (parser);
16978       token = cp_lexer_peek_token (parser->lexer);
16979     }
16980
16981   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
16982   /* For historical reasons, we accept an optional semicolon.  */
16983   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16984     cp_lexer_consume_token (parser->lexer);
16985 }
16986
16987 /* Parse an Objective-C protocol declaration.  */
16988
16989 static void
16990 cp_parser_objc_protocol_declaration (cp_parser* parser)
16991 {
16992   tree proto, protorefs;
16993   cp_token *tok;
16994
16995   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
16996   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
16997     {
16998       error ("identifier expected after `@protocol'");
16999       goto finish;
17000     }
17001
17002   /* See if we have a forward declaration or a definition.  */
17003   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
17004   
17005   /* Try a forward declaration first.  */
17006   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
17007     {
17008       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
17009      finish: 
17010       cp_parser_consume_semicolon_at_end_of_statement (parser);
17011     } 
17012
17013   /* Ok, we got a full-fledged definition (or at least should).  */
17014   else
17015     {
17016       proto = cp_parser_identifier (parser);
17017       protorefs = cp_parser_objc_protocol_refs_opt (parser);
17018       objc_start_protocol (proto, protorefs);
17019       cp_parser_objc_method_prototype_list (parser);
17020     }
17021 }
17022
17023 /* Parse an Objective-C superclass or category.  */
17024
17025 static void
17026 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
17027                                                           tree *categ)
17028 {
17029   cp_token *next = cp_lexer_peek_token (parser->lexer);
17030
17031   *super = *categ = NULL_TREE;
17032   if (next->type == CPP_COLON)
17033     {
17034       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
17035       *super = cp_parser_identifier (parser);
17036     }
17037   else if (next->type == CPP_OPEN_PAREN)
17038     {
17039       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
17040       *categ = cp_parser_identifier (parser);
17041       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17042     }
17043 }
17044
17045 /* Parse an Objective-C class interface.  */
17046
17047 static void
17048 cp_parser_objc_class_interface (cp_parser* parser)
17049 {
17050   tree name, super, categ, protos;
17051
17052   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
17053   name = cp_parser_identifier (parser);
17054   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17055   protos = cp_parser_objc_protocol_refs_opt (parser);
17056
17057   /* We have either a class or a category on our hands.  */
17058   if (categ)
17059     objc_start_category_interface (name, categ, protos);
17060   else
17061     {
17062       objc_start_class_interface (name, super, protos);
17063       /* Handle instance variable declarations, if any.  */
17064       cp_parser_objc_class_ivars (parser);
17065       objc_continue_interface ();
17066     }
17067
17068   cp_parser_objc_method_prototype_list (parser);
17069 }
17070
17071 /* Parse an Objective-C class implementation.  */
17072
17073 static void
17074 cp_parser_objc_class_implementation (cp_parser* parser)
17075 {
17076   tree name, super, categ;
17077
17078   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
17079   name = cp_parser_identifier (parser);
17080   cp_parser_objc_superclass_or_category (parser, &super, &categ);
17081
17082   /* We have either a class or a category on our hands.  */
17083   if (categ)
17084     objc_start_category_implementation (name, categ);
17085   else
17086     {
17087       objc_start_class_implementation (name, super);
17088       /* Handle instance variable declarations, if any.  */
17089       cp_parser_objc_class_ivars (parser);
17090       objc_continue_implementation ();
17091     }
17092
17093   cp_parser_objc_method_definition_list (parser);
17094 }
17095
17096 /* Consume the @end token and finish off the implementation.  */
17097
17098 static void
17099 cp_parser_objc_end_implementation (cp_parser* parser)
17100 {
17101   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
17102   objc_finish_implementation ();
17103 }
17104
17105 /* Parse an Objective-C declaration.  */
17106
17107 static void
17108 cp_parser_objc_declaration (cp_parser* parser)
17109 {
17110   /* Try to figure out what kind of declaration is present.  */
17111   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17112
17113   switch (kwd->keyword)
17114     {
17115     case RID_AT_ALIAS:
17116       cp_parser_objc_alias_declaration (parser);
17117       break;
17118     case RID_AT_CLASS:
17119       cp_parser_objc_class_declaration (parser);
17120       break;
17121     case RID_AT_PROTOCOL:
17122       cp_parser_objc_protocol_declaration (parser);
17123       break;
17124     case RID_AT_INTERFACE:
17125       cp_parser_objc_class_interface (parser);
17126       break;
17127     case RID_AT_IMPLEMENTATION:
17128       cp_parser_objc_class_implementation (parser);
17129       break;
17130     case RID_AT_END:
17131       cp_parser_objc_end_implementation (parser);
17132       break;
17133     default:
17134       error ("misplaced `@%D' Objective-C++ construct", kwd->value);
17135       cp_parser_skip_to_end_of_block_or_statement (parser);
17136     }
17137 }
17138
17139 /* Parse an Objective-C try-catch-finally statement.
17140
17141    objc-try-catch-finally-stmt:
17142      @try compound-statement objc-catch-clause-seq [opt]
17143        objc-finally-clause [opt]
17144
17145    objc-catch-clause-seq:
17146      objc-catch-clause objc-catch-clause-seq [opt]
17147
17148    objc-catch-clause:
17149      @catch ( exception-declaration ) compound-statement
17150
17151    objc-finally-clause
17152      @finally compound-statement
17153
17154    Returns NULL_TREE.  */
17155
17156 static tree
17157 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
17158   location_t location;
17159   tree stmt;
17160
17161   cp_parser_require_keyword (parser, RID_AT_TRY, "`@try'");
17162   location = cp_lexer_peek_token (parser->lexer)->location;
17163   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
17164      node, lest it get absorbed into the surrounding block.  */
17165   stmt = push_stmt_list ();
17166   cp_parser_compound_statement (parser, NULL, false);
17167   objc_begin_try_stmt (location, pop_stmt_list (stmt));
17168   
17169   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
17170     {
17171       cp_parameter_declarator *parmdecl;
17172       tree parm;
17173
17174       cp_lexer_consume_token (parser->lexer);
17175       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17176       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
17177       parm = grokdeclarator (parmdecl->declarator,
17178                              &parmdecl->decl_specifiers,
17179                              PARM, /*initialized=*/0, 
17180                              /*attrlist=*/NULL);
17181       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17182       objc_begin_catch_clause (parm);
17183       cp_parser_compound_statement (parser, NULL, false);
17184       objc_finish_catch_clause ();
17185     }
17186
17187   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
17188     {
17189       cp_lexer_consume_token (parser->lexer);
17190       location = cp_lexer_peek_token (parser->lexer)->location;
17191       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
17192          node, lest it get absorbed into the surrounding block.  */
17193       stmt = push_stmt_list ();
17194       cp_parser_compound_statement (parser, NULL, false);
17195       objc_build_finally_clause (location, pop_stmt_list (stmt));
17196     }
17197
17198   return objc_finish_try_stmt ();
17199 }
17200
17201 /* Parse an Objective-C synchronized statement.
17202
17203    objc-synchronized-stmt:
17204      @synchronized ( expression ) compound-statement
17205
17206    Returns NULL_TREE.  */
17207
17208 static tree
17209 cp_parser_objc_synchronized_statement (cp_parser *parser) {
17210   location_t location;
17211   tree lock, stmt;
17212
17213   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "`@synchronized'");
17214
17215   location = cp_lexer_peek_token (parser->lexer)->location;
17216   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
17217   lock = cp_parser_expression (parser, false);
17218   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
17219
17220   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
17221      node, lest it get absorbed into the surrounding block.  */
17222   stmt = push_stmt_list ();
17223   cp_parser_compound_statement (parser, NULL, false);
17224
17225   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
17226 }
17227
17228 /* Parse an Objective-C throw statement.
17229
17230    objc-throw-stmt:
17231      @throw assignment-expression [opt] ;
17232
17233    Returns a constructed '@throw' statement.  */
17234
17235 static tree
17236 cp_parser_objc_throw_statement (cp_parser *parser) {
17237   tree expr = NULL_TREE;
17238
17239   cp_parser_require_keyword (parser, RID_AT_THROW, "`@throw'");
17240
17241   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17242     expr = cp_parser_assignment_expression (parser, false);
17243
17244   cp_parser_consume_semicolon_at_end_of_statement (parser);
17245
17246   return objc_build_throw_stmt (expr);
17247 }
17248
17249 /* Parse an Objective-C statement.  */
17250
17251 static tree
17252 cp_parser_objc_statement (cp_parser * parser) {
17253   /* Try to figure out what kind of declaration is present.  */
17254   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
17255
17256   switch (kwd->keyword)
17257     {
17258     case RID_AT_TRY:
17259       return cp_parser_objc_try_catch_finally_statement (parser);
17260     case RID_AT_SYNCHRONIZED:
17261       return cp_parser_objc_synchronized_statement (parser);
17262     case RID_AT_THROW:
17263       return cp_parser_objc_throw_statement (parser);
17264     default:
17265       error ("misplaced `@%D' Objective-C++ construct", kwd->value);
17266       cp_parser_skip_to_end_of_block_or_statement (parser);
17267     }
17268
17269   return error_mark_node;
17270 }
17271 \f
17272 /* The parser.  */
17273
17274 static GTY (()) cp_parser *the_parser;
17275
17276 /* External interface.  */
17277
17278 /* Parse one entire translation unit.  */
17279
17280 void
17281 c_parse_file (void)
17282 {
17283   bool error_occurred;
17284   static bool already_called = false;
17285
17286   if (already_called)
17287     {
17288       sorry ("inter-module optimizations not implemented for C++");
17289       return;
17290     }
17291   already_called = true;
17292
17293   the_parser = cp_parser_new ();
17294   push_deferring_access_checks (flag_access_control
17295                                 ? dk_no_deferred : dk_no_check);
17296   error_occurred = cp_parser_translation_unit (the_parser);
17297   the_parser = NULL;
17298 }
17299
17300 /* This variable must be provided by every front end.  */
17301
17302 int yydebug;
17303
17304 #include "gt-cp-parser.h"