OSDN Git Service

cp:
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
3    Written by Mark Mitchell <mark@codesourcery.com>.
4
5    This file is part of GCC.
6
7    GCC is free software; you can redistribute it and/or modify it
8    under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2, or (at your option)
10    any later version.
11
12    GCC is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU General Public License
18    along with GCC; see the file COPYING.  If not, write to the Free
19    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20    02111-1307, USA.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "dyn-string.h"
27 #include "varray.h"
28 #include "cpplib.h"
29 #include "tree.h"
30 #include "cp-tree.h"
31 #include "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38
39 \f
40 /* The lexer.  */
41
42 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
43    and c-lex.c) and the C++ parser.  */
44
45 /* A C++ token.  */
46
47 typedef struct cp_token GTY (())
48 {
49   /* The kind of token.  */
50   ENUM_BITFIELD (cpp_ttype) type : 8;
51   /* If this token is a keyword, this value indicates which keyword.
52      Otherwise, this value is RID_MAX.  */
53   ENUM_BITFIELD (rid) keyword : 8;
54   /* Token flags.  */
55   unsigned char flags;
56   /* True if this token is from a system header. */
57   BOOL_BITFIELD in_system_header : 1;
58   /* True if this token is from a context where it is implicitly extern "C" */
59   BOOL_BITFIELD implicit_extern_c : 1;
60   /* The value associated with this token, if any.  */
61   tree value;
62   /* The location at which this token was found.  */
63   location_t location;
64 } cp_token;
65
66 /* We use a stack of token pointer for saving token sets.  */
67 typedef struct cp_token *cp_token_position;
68 DEF_VEC_MALLOC_P (cp_token_position);
69
70 static const cp_token eof_token =
71 {
72   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
73 #if USE_MAPPED_LOCATION
74   0
75 #else
76   {0, 0}
77 #endif
78 };
79
80 /* The cp_lexer structure represents the C++ lexer.  It is responsible
81    for managing the token stream from the preprocessor and supplying
82    it to the parser.  Tokens are never added to the cp_lexer after
83    it is created. */
84
85 typedef struct cp_lexer GTY (())
86 {
87   /* The memory allocated for the buffer.  NULL if this lexer does not
88      own the token buffer.  */
89   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
90   /* If the lexer owns the buffer, this is the number of tokens in the
91      buffer.  */
92   size_t buffer_length;
93   
94   /* A pointer just past the last available token.  The tokens
95      in this lexer are [buffer, last_token). */
96   cp_token_position GTY ((skip)) last_token;
97
98   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
99      no more available tokens.  */
100   cp_token_position GTY ((skip)) next_token;
101
102   /* A stack indicating positions at which cp_lexer_save_tokens was
103      called.  The top entry is the most recent position at which we
104      began saving tokens.  If the stack is non-empty, we are saving
105      tokens.  */
106   VEC (cp_token_position) *GTY ((skip)) saved_tokens;
107
108   /* True if we should output debugging information.  */
109   bool debugging_p;
110
111   /* The next lexer in a linked list of lexers.  */
112   struct cp_lexer *next;
113 } cp_lexer;
114
115 /* cp_token_cache is a range of tokens.  There is no need to represent
116    allocate heap memory for it, since tokens are never removed from the
117    lexer's array.  There is also no need for the GC to walk through
118    a cp_token_cache, since everything in here is referenced through
119    a lexer. */
120
121 typedef struct cp_token_cache GTY(())
122 {
123   /* The beginning of the token range. */
124   cp_token * GTY((skip)) first;
125
126   /* Points immediately after the last token in the range. */
127   cp_token * GTY ((skip)) last;
128 } cp_token_cache;
129
130 /* Prototypes.  */
131
132 static cp_lexer *cp_lexer_new_main
133   (void);
134 static cp_lexer *cp_lexer_new_from_tokens
135   (cp_token_cache *tokens);
136 static void cp_lexer_destroy
137   (cp_lexer *);
138 static int cp_lexer_saving_tokens
139   (const cp_lexer *);
140 static cp_token_position cp_lexer_token_position
141   (cp_lexer *, bool);
142 static cp_token *cp_lexer_token_at
143   (cp_lexer *, cp_token_position);
144 static void cp_lexer_get_preprocessor_token
145   (cp_lexer *, cp_token *);
146 static inline cp_token *cp_lexer_peek_token
147   (cp_lexer *);
148 static cp_token *cp_lexer_peek_nth_token
149   (cp_lexer *, size_t);
150 static inline bool cp_lexer_next_token_is
151   (cp_lexer *, enum cpp_ttype);
152 static bool cp_lexer_next_token_is_not
153   (cp_lexer *, enum cpp_ttype);
154 static bool cp_lexer_next_token_is_keyword
155   (cp_lexer *, enum rid);
156 static cp_token *cp_lexer_consume_token
157   (cp_lexer *);
158 static void cp_lexer_purge_token
159   (cp_lexer *);
160 static void cp_lexer_purge_tokens_after
161   (cp_lexer *, cp_token_position);
162 static void cp_lexer_handle_pragma
163   (cp_lexer *);
164 static void cp_lexer_save_tokens
165   (cp_lexer *);
166 static void cp_lexer_commit_tokens
167   (cp_lexer *);
168 static void cp_lexer_rollback_tokens
169   (cp_lexer *);
170 #ifdef ENABLE_CHECKING
171 static void cp_lexer_print_token
172   (FILE *, cp_token *);
173 static inline bool cp_lexer_debugging_p
174   (cp_lexer *);
175 static void cp_lexer_start_debugging
176   (cp_lexer *) ATTRIBUTE_UNUSED;
177 static void cp_lexer_stop_debugging
178   (cp_lexer *) ATTRIBUTE_UNUSED;
179 #else
180 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
181    about passing NULL to functions that require non-NULL arguments
182    (fputs, fprintf).  It will never be used, so all we need is a value
183    of the right type that's guaranteed not to be NULL.  */
184 #define cp_lexer_debug_stream stdout
185 #define cp_lexer_print_token(str, tok) (void) 0
186 #define cp_lexer_debugging_p(lexer) 0
187 #endif /* ENABLE_CHECKING */
188
189 static cp_token_cache *cp_token_cache_new
190   (cp_token *, cp_token *);
191
192 /* Manifest constants.  */
193 #define CP_LEXER_BUFFER_SIZE 10000
194 #define CP_SAVED_TOKEN_STACK 5
195
196 /* A token type for keywords, as opposed to ordinary identifiers.  */
197 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
198
199 /* A token type for template-ids.  If a template-id is processed while
200    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
201    the value of the CPP_TEMPLATE_ID is whatever was returned by
202    cp_parser_template_id.  */
203 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
204
205 /* A token type for nested-name-specifiers.  If a
206    nested-name-specifier is processed while parsing tentatively, it is
207    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
208    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
209    cp_parser_nested_name_specifier_opt.  */
210 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
211
212 /* A token type for tokens that are not tokens at all; these are used
213    to represent slots in the array where there used to be a token
214    that has now been deleted. */
215 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
216
217 /* The number of token types, including C++-specific ones.  */
218 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
219
220 /* Variables.  */
221
222 #ifdef ENABLE_CHECKING
223 /* The stream to which debugging output should be written.  */
224 static FILE *cp_lexer_debug_stream;
225 #endif /* ENABLE_CHECKING */
226
227 /* Create a new main C++ lexer, the lexer that gets tokens from the
228    preprocessor.  */
229
230 static cp_lexer *
231 cp_lexer_new_main (void)
232 {
233   cp_token first_token;
234   cp_lexer *lexer;
235   cp_token *pos;
236   size_t alloc;
237   size_t space;
238   cp_token *buffer;
239
240   /* Tell cpplib we want CPP_PRAGMA tokens. */
241   cpp_get_options (parse_in)->defer_pragmas = true;
242
243   /* Tell c_lex not to merge string constants.  */
244   c_lex_return_raw_strings = true;
245
246   /* It's possible that lexing the first token will load a PCH file,
247      which is a GC collection point.  So we have to grab the first
248      token before allocating any memory.  */
249   cp_lexer_get_preprocessor_token (NULL, &first_token);
250   c_common_no_more_pch ();
251
252   /* Allocate the memory.  */
253   lexer = GGC_CNEW (cp_lexer);
254
255 #ifdef ENABLE_CHECKING  
256   /* Initially we are not debugging.  */
257   lexer->debugging_p = false;
258 #endif /* ENABLE_CHECKING */
259   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
260          
261   /* Create the buffer.  */
262   alloc = CP_LEXER_BUFFER_SIZE;
263   buffer = ggc_alloc (alloc * sizeof (cp_token));
264
265   /* Put the first token in the buffer.  */
266   space = alloc;
267   pos = buffer;
268   *pos = first_token;
269   
270   /* Get the remaining tokens from the preprocessor. */
271   while (pos->type != CPP_EOF)
272     {
273       pos++;
274       if (!--space)
275         {
276           space = alloc;
277           alloc *= 2;
278           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
279           pos = buffer + space;
280         }
281       cp_lexer_get_preprocessor_token (lexer, pos);
282     }
283   lexer->buffer = buffer;
284   lexer->buffer_length = alloc - space;
285   lexer->last_token = pos;
286   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
287
288   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
289      direct calls to c_lex.  Those callers all expect c_lex to do
290      string constant concatenation.  */
291   c_lex_return_raw_strings = false;
292
293   gcc_assert (lexer->next_token->type != CPP_PURGED);
294   return lexer;
295 }
296
297 /* Create a new lexer whose token stream is primed with the tokens in
298    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
299
300 static cp_lexer *
301 cp_lexer_new_from_tokens (cp_token_cache *cache)
302 {
303   cp_token *first = cache->first;
304   cp_token *last = cache->last;
305   cp_lexer *lexer = GGC_CNEW (cp_lexer);
306
307   /* We do not own the buffer.  */
308   lexer->buffer = NULL;
309   lexer->buffer_length = 0;
310   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
311   lexer->last_token = last;
312   
313   lexer->saved_tokens = VEC_alloc (cp_token_position, CP_SAVED_TOKEN_STACK);
314
315 #ifdef ENABLE_CHECKING
316   /* Initially we are not debugging.  */
317   lexer->debugging_p = false;
318 #endif
319
320   gcc_assert (lexer->next_token->type != CPP_PURGED);
321   return lexer;
322 }
323
324 /* Frees all resources associated with LEXER. */
325
326 static void
327 cp_lexer_destroy (cp_lexer *lexer)
328 {
329   if (lexer->buffer)
330     ggc_free (lexer->buffer);
331   VEC_free (cp_token_position, lexer->saved_tokens);
332   ggc_free (lexer);
333 }
334
335 /* Returns nonzero if debugging information should be output.  */
336
337 #ifdef ENABLE_CHECKING
338
339 static inline bool
340 cp_lexer_debugging_p (cp_lexer *lexer)
341 {
342   return lexer->debugging_p;
343 }
344
345 #endif /* ENABLE_CHECKING */
346
347 static inline cp_token_position
348 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
349 {
350   gcc_assert (!previous_p || lexer->next_token != &eof_token);
351   
352   return lexer->next_token - previous_p;
353 }
354
355 static inline cp_token *
356 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
357 {
358   return pos;
359 }
360
361 /* nonzero if we are presently saving tokens.  */
362
363 static inline int
364 cp_lexer_saving_tokens (const cp_lexer* lexer)
365 {
366   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
367 }
368
369 /* Store the next token from the preprocessor in *TOKEN.  Return true
370    if we reach EOF.  */
371
372 static void
373 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
374                                  cp_token *token)
375 {
376   static int is_extern_c = 0;
377   bool done;
378
379   done = false;
380   /* Keep going until we get a token we like.  */
381   while (!done)
382     {
383       /* Get a new token from the preprocessor.  */
384       token->type = c_lex_with_flags (&token->value, &token->flags);
385       /* Issue messages about tokens we cannot process.  */
386       switch (token->type)
387         {
388         case CPP_ATSIGN:
389         case CPP_HASH:
390         case CPP_PASTE:
391           error ("invalid token");
392           break;
393
394         default:
395           /* This is a good token, so we exit the loop.  */
396           done = true;
397           break;
398         }
399     }
400   /* Now we've got our token.  */
401   token->location = input_location;
402   token->in_system_header = in_system_header;
403
404   /* On some systems, some header files are surrounded by an 
405      implicit extern "C" block.  Set a flag in the token if it
406      comes from such a header. */
407   is_extern_c += pending_lang_change;
408   pending_lang_change = 0;
409   token->implicit_extern_c = is_extern_c > 0;
410
411   /* Check to see if this token is a keyword.  */
412   if (token->type == CPP_NAME
413       && C_IS_RESERVED_WORD (token->value))
414     {
415       /* Mark this token as a keyword.  */
416       token->type = CPP_KEYWORD;
417       /* Record which keyword.  */
418       token->keyword = C_RID_CODE (token->value);
419       /* Update the value.  Some keywords are mapped to particular
420          entities, rather than simply having the value of the
421          corresponding IDENTIFIER_NODE.  For example, `__const' is
422          mapped to `const'.  */
423       token->value = ridpointers[token->keyword];
424     }
425   else
426     token->keyword = RID_MAX;
427 }
428
429 /* Update the globals input_location and in_system_header from TOKEN.   */
430 static inline void
431 cp_lexer_set_source_position_from_token (cp_token *token)
432 {
433   if (token->type != CPP_EOF)
434     {
435       input_location = token->location;
436       in_system_header = token->in_system_header;
437     }
438 }
439
440 /* Return a pointer to the next token in the token stream, but do not
441    consume it.  */
442
443 static inline cp_token *
444 cp_lexer_peek_token (cp_lexer *lexer)
445 {
446   if (cp_lexer_debugging_p (lexer))
447     {
448       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
449       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
450       putc ('\n', cp_lexer_debug_stream);
451     }
452   return lexer->next_token;
453 }
454
455 /* Return true if the next token has the indicated TYPE.  */
456
457 static inline bool
458 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
459 {
460   return cp_lexer_peek_token (lexer)->type == type;
461 }
462
463 /* Return true if the next token does not have the indicated TYPE.  */
464
465 static inline bool
466 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
467 {
468   return !cp_lexer_next_token_is (lexer, type);
469 }
470
471 /* Return true if the next token is the indicated KEYWORD.  */
472
473 static inline bool
474 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
475 {
476   cp_token *token;
477
478   /* Peek at the next token.  */
479   token = cp_lexer_peek_token (lexer);
480   /* Check to see if it is the indicated keyword.  */
481   return token->keyword == keyword;
482 }
483
484 /* Return a pointer to the Nth token in the token stream.  If N is 1,
485    then this is precisely equivalent to cp_lexer_peek_token (except
486    that it is not inline).  One would like to disallow that case, but
487    there is one case (cp_parser_nth_token_starts_template_id) where
488    the caller passes a variable for N and it might be 1.  */
489
490 static cp_token *
491 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
492 {
493   cp_token *token;
494
495   /* N is 1-based, not zero-based.  */
496   gcc_assert (n > 0 && lexer->next_token != &eof_token);
497
498   if (cp_lexer_debugging_p (lexer))
499     fprintf (cp_lexer_debug_stream,
500              "cp_lexer: peeking ahead %ld at token: ", (long)n);
501
502   --n;
503   token = lexer->next_token;
504   while (n != 0)
505     {
506       ++token;
507       if (token == lexer->last_token)
508         {
509           token = (cp_token *)&eof_token;
510           break;
511         }
512       
513       if (token->type != CPP_PURGED)
514         --n;
515     }
516
517   if (cp_lexer_debugging_p (lexer))
518     {
519       cp_lexer_print_token (cp_lexer_debug_stream, token);
520       putc ('\n', cp_lexer_debug_stream);
521     }
522
523   return token;
524 }
525
526 /* Return the next token, and advance the lexer's next_token pointer
527    to point to the next non-purged token.  */
528
529 static cp_token *
530 cp_lexer_consume_token (cp_lexer* lexer)
531 {
532   cp_token *token = lexer->next_token;
533
534   gcc_assert (token != &eof_token);
535   
536   do
537     {
538       lexer->next_token++;
539       if (lexer->next_token == lexer->last_token)
540         {
541           lexer->next_token = (cp_token *)&eof_token;
542           break;
543         }
544       
545     }
546   while (lexer->next_token->type == CPP_PURGED);
547   
548   cp_lexer_set_source_position_from_token (token);
549   
550   /* Provide debugging output.  */
551   if (cp_lexer_debugging_p (lexer))
552     {
553       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
554       cp_lexer_print_token (cp_lexer_debug_stream, token);
555       putc ('\n', cp_lexer_debug_stream);
556     }
557   
558   return token;
559 }
560
561 /* Permanently remove the next token from the token stream, and
562    advance the next_token pointer to refer to the next non-purged
563    token.  */
564
565 static void
566 cp_lexer_purge_token (cp_lexer *lexer)
567 {
568   cp_token *tok = lexer->next_token;
569   
570   gcc_assert (tok != &eof_token);
571   tok->type = CPP_PURGED;
572   tok->location = UNKNOWN_LOCATION;
573   tok->value = NULL_TREE;
574   tok->keyword = RID_MAX;
575
576   do
577     {
578       tok++;
579       if (tok == lexer->last_token)
580         {
581           tok = (cp_token *)&eof_token;
582           break;
583         }
584     }
585   while (tok->type == CPP_PURGED);
586   lexer->next_token = tok;
587 }
588
589 /* Permanently remove all tokens after TOK, up to, but not
590    including, the token that will be returned next by
591    cp_lexer_peek_token.  */
592
593 static void
594 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
595 {
596   cp_token *peek = lexer->next_token;
597
598   if (peek == &eof_token)
599     peek = lexer->last_token;
600   
601   gcc_assert (tok < peek);
602
603   for ( tok += 1; tok != peek; tok += 1)
604     {
605       tok->type = CPP_PURGED;
606       tok->location = UNKNOWN_LOCATION;
607       tok->value = NULL_TREE;
608       tok->keyword = RID_MAX;
609     }
610 }
611
612 /* Consume and handle a pragma token.   */
613 static void
614 cp_lexer_handle_pragma (cp_lexer *lexer)
615 {
616   cpp_string s;
617   cp_token *token = cp_lexer_consume_token (lexer);
618   gcc_assert (token->type == CPP_PRAGMA);
619   gcc_assert (token->value);
620
621   s.len = TREE_STRING_LENGTH (token->value);
622   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
623
624   cpp_handle_deferred_pragma (parse_in, &s);
625
626   /* Clearing token->value here means that we will get an ICE if we
627      try to process this #pragma again (which should be impossible).  */
628   token->value = NULL;
629 }
630
631 /* Begin saving tokens.  All tokens consumed after this point will be
632    preserved.  */
633
634 static void
635 cp_lexer_save_tokens (cp_lexer* lexer)
636 {
637   /* Provide debugging output.  */
638   if (cp_lexer_debugging_p (lexer))
639     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
640
641   VEC_safe_push (cp_token_position, lexer->saved_tokens, lexer->next_token);
642 }
643
644 /* Commit to the portion of the token stream most recently saved.  */
645
646 static void
647 cp_lexer_commit_tokens (cp_lexer* lexer)
648 {
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
652
653   VEC_pop (cp_token_position, lexer->saved_tokens);
654 }
655
656 /* Return all tokens saved since the last call to cp_lexer_save_tokens
657    to the token stream.  Stop saving tokens.  */
658
659 static void
660 cp_lexer_rollback_tokens (cp_lexer* lexer)
661 {
662   /* Provide debugging output.  */
663   if (cp_lexer_debugging_p (lexer))
664     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
665
666   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
667 }
668
669 /* Print a representation of the TOKEN on the STREAM.  */
670
671 #ifdef ENABLE_CHECKING
672
673 static void
674 cp_lexer_print_token (FILE * stream, cp_token *token)
675 {
676   /* We don't use cpp_type2name here because the parser defines
677      a few tokens of its own.  */
678   static const char *const token_names[] = {
679     /* cpplib-defined token types */
680 #define OP(e, s) #e,
681 #define TK(e, s) #e,
682     TTYPE_TABLE
683 #undef OP
684 #undef TK
685     /* C++ parser token types - see "Manifest constants", above.  */
686     "KEYWORD",
687     "TEMPLATE_ID",
688     "NESTED_NAME_SPECIFIER",
689     "PURGED"
690   };
691   
692   /* If we have a name for the token, print it out.  Otherwise, we
693      simply give the numeric code.  */
694   gcc_assert (token->type < ARRAY_SIZE(token_names));
695   fputs (token_names[token->type], stream);
696
697   /* For some tokens, print the associated data.  */
698   switch (token->type)
699     {
700     case CPP_KEYWORD:
701       /* Some keywords have a value that is not an IDENTIFIER_NODE.
702          For example, `struct' is mapped to an INTEGER_CST.  */
703       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
704         break;
705       /* else fall through */
706     case CPP_NAME:
707       fputs (IDENTIFIER_POINTER (token->value), stream);
708       break;
709
710     case CPP_STRING:
711     case CPP_WSTRING:
712     case CPP_PRAGMA:
713       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
714       break;
715
716     default:
717       break;
718     }
719 }
720
721 /* Start emitting debugging information.  */
722
723 static void
724 cp_lexer_start_debugging (cp_lexer* lexer)
725 {
726   ++lexer->debugging_p;
727 }
728
729 /* Stop emitting debugging information.  */
730
731 static void
732 cp_lexer_stop_debugging (cp_lexer* lexer)
733 {
734   --lexer->debugging_p;
735 }
736
737 #endif /* ENABLE_CHECKING */
738
739 /* Create a new cp_token_cache, representing a range of tokens. */
740
741 static cp_token_cache *
742 cp_token_cache_new (cp_token *first, cp_token *last)
743 {
744   cp_token_cache *cache = GGC_NEW (cp_token_cache);
745   cache->first = first;
746   cache->last = last;
747   return cache;
748 }
749
750 \f
751 /* Decl-specifiers.  */
752
753 static void clear_decl_specs
754   (cp_decl_specifier_seq *);
755
756 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
757
758 static void
759 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
760 {
761   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
762 }
763
764 /* Declarators.  */
765
766 /* Nothing other than the parser should be creating declarators;
767    declarators are a semi-syntactic representation of C++ entities.
768    Other parts of the front end that need to create entities (like
769    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
770
771 static cp_declarator *make_id_declarator
772   (tree);
773 static cp_declarator *make_call_declarator
774   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
775 static cp_declarator *make_array_declarator
776   (cp_declarator *, tree);
777 static cp_declarator *make_pointer_declarator
778   (cp_cv_quals, cp_declarator *);
779 static cp_declarator *make_reference_declarator
780   (cp_cv_quals, cp_declarator *);
781 static cp_parameter_declarator *make_parameter_declarator
782   (cp_decl_specifier_seq *, cp_declarator *, tree);
783 static cp_declarator *make_ptrmem_declarator
784   (cp_cv_quals, tree, cp_declarator *);
785
786 cp_declarator *cp_error_declarator;
787
788 /* The obstack on which declarators and related data structures are
789    allocated.  */
790 static struct obstack declarator_obstack;
791
792 /* Alloc BYTES from the declarator memory pool.  */
793
794 static inline void *
795 alloc_declarator (size_t bytes)
796 {
797   return obstack_alloc (&declarator_obstack, bytes);
798 }
799
800 /* Allocate a declarator of the indicated KIND.  Clear fields that are
801    common to all declarators.  */
802
803 static cp_declarator *
804 make_declarator (cp_declarator_kind kind)
805 {
806   cp_declarator *declarator;
807
808   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
809   declarator->kind = kind;
810   declarator->attributes = NULL_TREE;
811   declarator->declarator = NULL;
812
813   return declarator;
814 }
815
816 /* Make a declarator for a generalized identifier.  */
817
818 cp_declarator *
819 make_id_declarator (tree id)
820 {
821   cp_declarator *declarator;
822
823   declarator = make_declarator (cdk_id);
824   declarator->u.id.name = id;
825   declarator->u.id.sfk = sfk_none;
826
827   return declarator;
828 }
829
830 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
831    of modifiers such as const or volatile to apply to the pointer
832    type, represented as identifiers.  */
833
834 cp_declarator *
835 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
836 {
837   cp_declarator *declarator;
838
839   declarator = make_declarator (cdk_pointer);
840   declarator->declarator = target;
841   declarator->u.pointer.qualifiers = cv_qualifiers;
842   declarator->u.pointer.class_type = NULL_TREE;
843
844   return declarator;
845 }
846
847 /* Like make_pointer_declarator -- but for references.  */
848
849 cp_declarator *
850 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
851 {
852   cp_declarator *declarator;
853
854   declarator = make_declarator (cdk_reference);
855   declarator->declarator = target;
856   declarator->u.pointer.qualifiers = cv_qualifiers;
857   declarator->u.pointer.class_type = NULL_TREE;
858
859   return declarator;
860 }
861
862 /* Like make_pointer_declarator -- but for a pointer to a non-static
863    member of CLASS_TYPE.  */
864
865 cp_declarator *
866 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
867                         cp_declarator *pointee)
868 {
869   cp_declarator *declarator;
870
871   declarator = make_declarator (cdk_ptrmem);
872   declarator->declarator = pointee;
873   declarator->u.pointer.qualifiers = cv_qualifiers;
874   declarator->u.pointer.class_type = class_type;
875
876   return declarator;
877 }
878
879 /* Make a declarator for the function given by TARGET, with the
880    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
881    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
882    indicates what exceptions can be thrown.  */
883
884 cp_declarator *
885 make_call_declarator (cp_declarator *target,
886                       cp_parameter_declarator *parms,
887                       cp_cv_quals cv_qualifiers,
888                       tree exception_specification)
889 {
890   cp_declarator *declarator;
891
892   declarator = make_declarator (cdk_function);
893   declarator->declarator = target;
894   declarator->u.function.parameters = parms;
895   declarator->u.function.qualifiers = cv_qualifiers;
896   declarator->u.function.exception_specification = exception_specification;
897
898   return declarator;
899 }
900
901 /* Make a declarator for an array of BOUNDS elements, each of which is
902    defined by ELEMENT.  */
903
904 cp_declarator *
905 make_array_declarator (cp_declarator *element, tree bounds)
906 {
907   cp_declarator *declarator;
908
909   declarator = make_declarator (cdk_array);
910   declarator->declarator = element;
911   declarator->u.array.bounds = bounds;
912
913   return declarator;
914 }
915
916 cp_parameter_declarator *no_parameters;
917
918 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
919    DECLARATOR and DEFAULT_ARGUMENT.  */
920
921 cp_parameter_declarator *
922 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
923                            cp_declarator *declarator,
924                            tree default_argument)
925 {
926   cp_parameter_declarator *parameter;
927
928   parameter = ((cp_parameter_declarator *)
929                alloc_declarator (sizeof (cp_parameter_declarator)));
930   parameter->next = NULL;
931   if (decl_specifiers)
932     parameter->decl_specifiers = *decl_specifiers;
933   else
934     clear_decl_specs (&parameter->decl_specifiers);
935   parameter->declarator = declarator;
936   parameter->default_argument = default_argument;
937   parameter->ellipsis_p = false;
938
939   return parameter;
940 }
941
942 /* The parser.  */
943
944 /* Overview
945    --------
946
947    A cp_parser parses the token stream as specified by the C++
948    grammar.  Its job is purely parsing, not semantic analysis.  For
949    example, the parser breaks the token stream into declarators,
950    expressions, statements, and other similar syntactic constructs.
951    It does not check that the types of the expressions on either side
952    of an assignment-statement are compatible, or that a function is
953    not declared with a parameter of type `void'.
954
955    The parser invokes routines elsewhere in the compiler to perform
956    semantic analysis and to build up the abstract syntax tree for the
957    code processed.
958
959    The parser (and the template instantiation code, which is, in a
960    way, a close relative of parsing) are the only parts of the
961    compiler that should be calling push_scope and pop_scope, or
962    related functions.  The parser (and template instantiation code)
963    keeps track of what scope is presently active; everything else
964    should simply honor that.  (The code that generates static
965    initializers may also need to set the scope, in order to check
966    access control correctly when emitting the initializers.)
967
968    Methodology
969    -----------
970
971    The parser is of the standard recursive-descent variety.  Upcoming
972    tokens in the token stream are examined in order to determine which
973    production to use when parsing a non-terminal.  Some C++ constructs
974    require arbitrary look ahead to disambiguate.  For example, it is
975    impossible, in the general case, to tell whether a statement is an
976    expression or declaration without scanning the entire statement.
977    Therefore, the parser is capable of "parsing tentatively."  When the
978    parser is not sure what construct comes next, it enters this mode.
979    Then, while we attempt to parse the construct, the parser queues up
980    error messages, rather than issuing them immediately, and saves the
981    tokens it consumes.  If the construct is parsed successfully, the
982    parser "commits", i.e., it issues any queued error messages and
983    the tokens that were being preserved are permanently discarded.
984    If, however, the construct is not parsed successfully, the parser
985    rolls back its state completely so that it can resume parsing using
986    a different alternative.
987
988    Future Improvements
989    -------------------
990
991    The performance of the parser could probably be improved substantially.
992    We could often eliminate the need to parse tentatively by looking ahead
993    a little bit.  In some places, this approach might not entirely eliminate
994    the need to parse tentatively, but it might still speed up the average
995    case.  */
996
997 /* Flags that are passed to some parsing functions.  These values can
998    be bitwise-ored together.  */
999
1000 typedef enum cp_parser_flags
1001 {
1002   /* No flags.  */
1003   CP_PARSER_FLAGS_NONE = 0x0,
1004   /* The construct is optional.  If it is not present, then no error
1005      should be issued.  */
1006   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1007   /* When parsing a type-specifier, do not allow user-defined types.  */
1008   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1009 } cp_parser_flags;
1010
1011 /* The different kinds of declarators we want to parse.  */
1012
1013 typedef enum cp_parser_declarator_kind
1014 {
1015   /* We want an abstract declarator.  */
1016   CP_PARSER_DECLARATOR_ABSTRACT,
1017   /* We want a named declarator.  */
1018   CP_PARSER_DECLARATOR_NAMED,
1019   /* We don't mind, but the name must be an unqualified-id.  */
1020   CP_PARSER_DECLARATOR_EITHER
1021 } cp_parser_declarator_kind;
1022
1023 /* The precedence values used to parse binary expressions.  The minimum value
1024    of PREC must be 1, because zero is reserved to quickly discriminate
1025    binary operators from other tokens.  */
1026
1027 enum cp_parser_prec
1028 {
1029   PREC_NOT_OPERATOR,
1030   PREC_LOGICAL_OR_EXPRESSION,
1031   PREC_LOGICAL_AND_EXPRESSION,
1032   PREC_INCLUSIVE_OR_EXPRESSION,
1033   PREC_EXCLUSIVE_OR_EXPRESSION,
1034   PREC_AND_EXPRESSION,
1035   PREC_EQUALITY_EXPRESSION,
1036   PREC_RELATIONAL_EXPRESSION,
1037   PREC_SHIFT_EXPRESSION,
1038   PREC_ADDITIVE_EXPRESSION,
1039   PREC_MULTIPLICATIVE_EXPRESSION,
1040   PREC_PM_EXPRESSION,
1041   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1042 };
1043
1044 /* A mapping from a token type to a corresponding tree node type, with a
1045    precedence value.  */
1046
1047 typedef struct cp_parser_binary_operations_map_node
1048 {
1049   /* The token type.  */
1050   enum cpp_ttype token_type;
1051   /* The corresponding tree code.  */
1052   enum tree_code tree_type;
1053   /* The precedence of this operator.  */
1054   enum cp_parser_prec prec;
1055 } cp_parser_binary_operations_map_node;
1056
1057 /* The status of a tentative parse.  */
1058
1059 typedef enum cp_parser_status_kind
1060 {
1061   /* No errors have occurred.  */
1062   CP_PARSER_STATUS_KIND_NO_ERROR,
1063   /* An error has occurred.  */
1064   CP_PARSER_STATUS_KIND_ERROR,
1065   /* We are committed to this tentative parse, whether or not an error
1066      has occurred.  */
1067   CP_PARSER_STATUS_KIND_COMMITTED
1068 } cp_parser_status_kind;
1069
1070 typedef struct cp_parser_expression_stack_entry
1071 {
1072   tree lhs;
1073   enum tree_code tree_type;
1074   int prec;
1075 } cp_parser_expression_stack_entry;
1076
1077 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1078    entries because precedence levels on the stack are monotonically
1079    increasing.  */
1080 typedef struct cp_parser_expression_stack_entry
1081   cp_parser_expression_stack[NUM_PREC_VALUES];
1082
1083 /* Context that is saved and restored when parsing tentatively.  */
1084 typedef struct cp_parser_context GTY (())
1085 {
1086   /* If this is a tentative parsing context, the status of the
1087      tentative parse.  */
1088   enum cp_parser_status_kind status;
1089   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1090      that are looked up in this context must be looked up both in the
1091      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1092      the context of the containing expression.  */
1093   tree object_type;
1094
1095   /* The next parsing context in the stack.  */
1096   struct cp_parser_context *next;
1097 } cp_parser_context;
1098
1099 /* Prototypes.  */
1100
1101 /* Constructors and destructors.  */
1102
1103 static cp_parser_context *cp_parser_context_new
1104   (cp_parser_context *);
1105
1106 /* Class variables.  */
1107
1108 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1109
1110 /* The operator-precedence table used by cp_parser_binary_expression.
1111    Transformed into an associative array (binops_by_token) by
1112    cp_parser_new.  */
1113
1114 static const cp_parser_binary_operations_map_node binops[] = {
1115   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1116   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1117
1118   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1119   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1120   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1121
1122   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1123   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1124
1125   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1126   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1127
1128   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1129   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1130   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1131   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1132   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1133   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1134
1135   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1136   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1137
1138   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1139
1140   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1141
1142   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1143
1144   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1145
1146   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1147 };
1148
1149 /* The same as binops, but initialized by cp_parser_new so that
1150    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1151    for speed.  */
1152 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1153
1154 /* Constructors and destructors.  */
1155
1156 /* Construct a new context.  The context below this one on the stack
1157    is given by NEXT.  */
1158
1159 static cp_parser_context *
1160 cp_parser_context_new (cp_parser_context* next)
1161 {
1162   cp_parser_context *context;
1163
1164   /* Allocate the storage.  */
1165   if (cp_parser_context_free_list != NULL)
1166     {
1167       /* Pull the first entry from the free list.  */
1168       context = cp_parser_context_free_list;
1169       cp_parser_context_free_list = context->next;
1170       memset (context, 0, sizeof (*context));
1171     }
1172   else
1173     context = GGC_CNEW (cp_parser_context);
1174
1175   /* No errors have occurred yet in this context.  */
1176   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1177   /* If this is not the bottomost context, copy information that we
1178      need from the previous context.  */
1179   if (next)
1180     {
1181       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1182          expression, then we are parsing one in this context, too.  */
1183       context->object_type = next->object_type;
1184       /* Thread the stack.  */
1185       context->next = next;
1186     }
1187
1188   return context;
1189 }
1190
1191 /* The cp_parser structure represents the C++ parser.  */
1192
1193 typedef struct cp_parser GTY(())
1194 {
1195   /* The lexer from which we are obtaining tokens.  */
1196   cp_lexer *lexer;
1197
1198   /* The scope in which names should be looked up.  If NULL_TREE, then
1199      we look up names in the scope that is currently open in the
1200      source program.  If non-NULL, this is either a TYPE or
1201      NAMESPACE_DECL for the scope in which we should look.
1202
1203      This value is not cleared automatically after a name is looked
1204      up, so we must be careful to clear it before starting a new look
1205      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1206      will look up `Z' in the scope of `X', rather than the current
1207      scope.)  Unfortunately, it is difficult to tell when name lookup
1208      is complete, because we sometimes peek at a token, look it up,
1209      and then decide not to consume it.  */
1210   tree scope;
1211
1212   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1213      last lookup took place.  OBJECT_SCOPE is used if an expression
1214      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1215      respectively.  QUALIFYING_SCOPE is used for an expression of the
1216      form "X::Y"; it refers to X.  */
1217   tree object_scope;
1218   tree qualifying_scope;
1219
1220   /* A stack of parsing contexts.  All but the bottom entry on the
1221      stack will be tentative contexts.
1222
1223      We parse tentatively in order to determine which construct is in
1224      use in some situations.  For example, in order to determine
1225      whether a statement is an expression-statement or a
1226      declaration-statement we parse it tentatively as a
1227      declaration-statement.  If that fails, we then reparse the same
1228      token stream as an expression-statement.  */
1229   cp_parser_context *context;
1230
1231   /* True if we are parsing GNU C++.  If this flag is not set, then
1232      GNU extensions are not recognized.  */
1233   bool allow_gnu_extensions_p;
1234
1235   /* TRUE if the `>' token should be interpreted as the greater-than
1236      operator.  FALSE if it is the end of a template-id or
1237      template-parameter-list.  */
1238   bool greater_than_is_operator_p;
1239
1240   /* TRUE if default arguments are allowed within a parameter list
1241      that starts at this point. FALSE if only a gnu extension makes
1242      them permissible.  */
1243   bool default_arg_ok_p;
1244
1245   /* TRUE if we are parsing an integral constant-expression.  See
1246      [expr.const] for a precise definition.  */
1247   bool integral_constant_expression_p;
1248
1249   /* TRUE if we are parsing an integral constant-expression -- but a
1250      non-constant expression should be permitted as well.  This flag
1251      is used when parsing an array bound so that GNU variable-length
1252      arrays are tolerated.  */
1253   bool allow_non_integral_constant_expression_p;
1254
1255   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1256      been seen that makes the expression non-constant.  */
1257   bool non_integral_constant_expression_p;
1258
1259   /* TRUE if local variable names and `this' are forbidden in the
1260      current context.  */
1261   bool local_variables_forbidden_p;
1262
1263   /* TRUE if the declaration we are parsing is part of a
1264      linkage-specification of the form `extern string-literal
1265      declaration'.  */
1266   bool in_unbraced_linkage_specification_p;
1267
1268   /* TRUE if we are presently parsing a declarator, after the
1269      direct-declarator.  */
1270   bool in_declarator_p;
1271
1272   /* TRUE if we are presently parsing a template-argument-list.  */
1273   bool in_template_argument_list_p;
1274
1275   /* TRUE if we are presently parsing the body of an
1276      iteration-statement.  */
1277   bool in_iteration_statement_p;
1278
1279   /* TRUE if we are presently parsing the body of a switch
1280      statement.  */
1281   bool in_switch_statement_p;
1282
1283   /* TRUE if we are parsing a type-id in an expression context.  In
1284      such a situation, both "type (expr)" and "type (type)" are valid
1285      alternatives.  */
1286   bool in_type_id_in_expr_p;
1287
1288   /* TRUE if we are currently in a header file where declarations are
1289      implicitly extern "C". */
1290   bool implicit_extern_c;
1291
1292   /* TRUE if strings in expressions should be translated to the execution
1293      character set.  */
1294   bool translate_strings_p;
1295
1296   /* If non-NULL, then we are parsing a construct where new type
1297      definitions are not permitted.  The string stored here will be
1298      issued as an error message if a type is defined.  */
1299   const char *type_definition_forbidden_message;
1300
1301   /* A list of lists. The outer list is a stack, used for member
1302      functions of local classes. At each level there are two sub-list,
1303      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1304      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1305      TREE_VALUE's. The functions are chained in reverse declaration
1306      order.
1307
1308      The TREE_PURPOSE sublist contains those functions with default
1309      arguments that need post processing, and the TREE_VALUE sublist
1310      contains those functions with definitions that need post
1311      processing.
1312
1313      These lists can only be processed once the outermost class being
1314      defined is complete.  */
1315   tree unparsed_functions_queues;
1316
1317   /* The number of classes whose definitions are currently in
1318      progress.  */
1319   unsigned num_classes_being_defined;
1320
1321   /* The number of template parameter lists that apply directly to the
1322      current declaration.  */
1323   unsigned num_template_parameter_lists;
1324 } cp_parser;
1325
1326 /* The type of a function that parses some kind of expression.  */
1327 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1328
1329 /* Prototypes.  */
1330
1331 /* Constructors and destructors.  */
1332
1333 static cp_parser *cp_parser_new
1334   (void);
1335
1336 /* Routines to parse various constructs.
1337
1338    Those that return `tree' will return the error_mark_node (rather
1339    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1340    Sometimes, they will return an ordinary node if error-recovery was
1341    attempted, even though a parse error occurred.  So, to check
1342    whether or not a parse error occurred, you should always use
1343    cp_parser_error_occurred.  If the construct is optional (indicated
1344    either by an `_opt' in the name of the function that does the
1345    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1346    the construct is not present.  */
1347
1348 /* Lexical conventions [gram.lex]  */
1349
1350 static tree cp_parser_identifier
1351   (cp_parser *);
1352 static tree cp_parser_string_literal
1353   (cp_parser *, bool, bool);
1354
1355 /* Basic concepts [gram.basic]  */
1356
1357 static bool cp_parser_translation_unit
1358   (cp_parser *);
1359
1360 /* Expressions [gram.expr]  */
1361
1362 static tree cp_parser_primary_expression
1363   (cp_parser *, cp_id_kind *, tree *);
1364 static tree cp_parser_id_expression
1365   (cp_parser *, bool, bool, bool *, bool);
1366 static tree cp_parser_unqualified_id
1367   (cp_parser *, bool, bool, bool);
1368 static tree cp_parser_nested_name_specifier_opt
1369   (cp_parser *, bool, bool, bool, bool);
1370 static tree cp_parser_nested_name_specifier
1371   (cp_parser *, bool, bool, bool, bool);
1372 static tree cp_parser_class_or_namespace_name
1373   (cp_parser *, bool, bool, bool, bool, bool);
1374 static tree cp_parser_postfix_expression
1375   (cp_parser *, bool);
1376 static tree cp_parser_postfix_open_square_expression
1377   (cp_parser *, tree, bool);
1378 static tree cp_parser_postfix_dot_deref_expression
1379   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1380 static tree cp_parser_parenthesized_expression_list
1381   (cp_parser *, bool, bool *);
1382 static void cp_parser_pseudo_destructor_name
1383   (cp_parser *, tree *, tree *);
1384 static tree cp_parser_unary_expression
1385   (cp_parser *, bool);
1386 static enum tree_code cp_parser_unary_operator
1387   (cp_token *);
1388 static tree cp_parser_new_expression
1389   (cp_parser *);
1390 static tree cp_parser_new_placement
1391   (cp_parser *);
1392 static tree cp_parser_new_type_id
1393   (cp_parser *, tree *);
1394 static cp_declarator *cp_parser_new_declarator_opt
1395   (cp_parser *);
1396 static cp_declarator *cp_parser_direct_new_declarator
1397   (cp_parser *);
1398 static tree cp_parser_new_initializer
1399   (cp_parser *);
1400 static tree cp_parser_delete_expression
1401   (cp_parser *);
1402 static tree cp_parser_cast_expression
1403   (cp_parser *, bool);
1404 static tree cp_parser_binary_expression
1405   (cp_parser *);
1406 static tree cp_parser_question_colon_clause
1407   (cp_parser *, tree);
1408 static tree cp_parser_assignment_expression
1409   (cp_parser *);
1410 static enum tree_code cp_parser_assignment_operator_opt
1411   (cp_parser *);
1412 static tree cp_parser_expression
1413   (cp_parser *);
1414 static tree cp_parser_constant_expression
1415   (cp_parser *, bool, bool *);
1416 static tree cp_parser_builtin_offsetof
1417   (cp_parser *);
1418
1419 /* Statements [gram.stmt.stmt]  */
1420
1421 static void cp_parser_statement
1422   (cp_parser *, tree);
1423 static tree cp_parser_labeled_statement
1424   (cp_parser *, tree);
1425 static tree cp_parser_expression_statement
1426   (cp_parser *, tree);
1427 static tree cp_parser_compound_statement
1428   (cp_parser *, tree, bool);
1429 static void cp_parser_statement_seq_opt
1430   (cp_parser *, tree);
1431 static tree cp_parser_selection_statement
1432   (cp_parser *);
1433 static tree cp_parser_condition
1434   (cp_parser *);
1435 static tree cp_parser_iteration_statement
1436   (cp_parser *);
1437 static void cp_parser_for_init_statement
1438   (cp_parser *);
1439 static tree cp_parser_jump_statement
1440   (cp_parser *);
1441 static void cp_parser_declaration_statement
1442   (cp_parser *);
1443
1444 static tree cp_parser_implicitly_scoped_statement
1445   (cp_parser *);
1446 static void cp_parser_already_scoped_statement
1447   (cp_parser *);
1448
1449 /* Declarations [gram.dcl.dcl] */
1450
1451 static void cp_parser_declaration_seq_opt
1452   (cp_parser *);
1453 static void cp_parser_declaration
1454   (cp_parser *);
1455 static void cp_parser_block_declaration
1456   (cp_parser *, bool);
1457 static void cp_parser_simple_declaration
1458   (cp_parser *, bool);
1459 static void cp_parser_decl_specifier_seq
1460   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1461 static tree cp_parser_storage_class_specifier_opt
1462   (cp_parser *);
1463 static tree cp_parser_function_specifier_opt
1464   (cp_parser *, cp_decl_specifier_seq *);
1465 static tree cp_parser_type_specifier
1466   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1467    int *, bool *);
1468 static tree cp_parser_simple_type_specifier
1469   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1470 static tree cp_parser_type_name
1471   (cp_parser *);
1472 static tree cp_parser_elaborated_type_specifier
1473   (cp_parser *, bool, bool);
1474 static tree cp_parser_enum_specifier
1475   (cp_parser *);
1476 static void cp_parser_enumerator_list
1477   (cp_parser *, tree);
1478 static void cp_parser_enumerator_definition
1479   (cp_parser *, tree);
1480 static tree cp_parser_namespace_name
1481   (cp_parser *);
1482 static void cp_parser_namespace_definition
1483   (cp_parser *);
1484 static void cp_parser_namespace_body
1485   (cp_parser *);
1486 static tree cp_parser_qualified_namespace_specifier
1487   (cp_parser *);
1488 static void cp_parser_namespace_alias_definition
1489   (cp_parser *);
1490 static void cp_parser_using_declaration
1491   (cp_parser *);
1492 static void cp_parser_using_directive
1493   (cp_parser *);
1494 static void cp_parser_asm_definition
1495   (cp_parser *);
1496 static void cp_parser_linkage_specification
1497   (cp_parser *);
1498
1499 /* Declarators [gram.dcl.decl] */
1500
1501 static tree cp_parser_init_declarator
1502   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1503 static cp_declarator *cp_parser_declarator
1504   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1505 static cp_declarator *cp_parser_direct_declarator
1506   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1507 static enum tree_code cp_parser_ptr_operator
1508   (cp_parser *, tree *, cp_cv_quals *);
1509 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1510   (cp_parser *);
1511 static tree cp_parser_declarator_id
1512   (cp_parser *);
1513 static tree cp_parser_type_id
1514   (cp_parser *);
1515 static void cp_parser_type_specifier_seq
1516   (cp_parser *, cp_decl_specifier_seq *);
1517 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1518   (cp_parser *);
1519 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1520   (cp_parser *, bool *);
1521 static cp_parameter_declarator *cp_parser_parameter_declaration
1522   (cp_parser *, bool, bool *);
1523 static void cp_parser_function_body
1524   (cp_parser *);
1525 static tree cp_parser_initializer
1526   (cp_parser *, bool *, bool *);
1527 static tree cp_parser_initializer_clause
1528   (cp_parser *, bool *);
1529 static tree cp_parser_initializer_list
1530   (cp_parser *, bool *);
1531
1532 static bool cp_parser_ctor_initializer_opt_and_function_body
1533   (cp_parser *);
1534
1535 /* Classes [gram.class] */
1536
1537 static tree cp_parser_class_name
1538   (cp_parser *, bool, bool, bool, bool, bool, bool);
1539 static tree cp_parser_class_specifier
1540   (cp_parser *);
1541 static tree cp_parser_class_head
1542   (cp_parser *, bool *, tree *);
1543 static enum tag_types cp_parser_class_key
1544   (cp_parser *);
1545 static void cp_parser_member_specification_opt
1546   (cp_parser *);
1547 static void cp_parser_member_declaration
1548   (cp_parser *);
1549 static tree cp_parser_pure_specifier
1550   (cp_parser *);
1551 static tree cp_parser_constant_initializer
1552   (cp_parser *);
1553
1554 /* Derived classes [gram.class.derived] */
1555
1556 static tree cp_parser_base_clause
1557   (cp_parser *);
1558 static tree cp_parser_base_specifier
1559   (cp_parser *);
1560
1561 /* Special member functions [gram.special] */
1562
1563 static tree cp_parser_conversion_function_id
1564   (cp_parser *);
1565 static tree cp_parser_conversion_type_id
1566   (cp_parser *);
1567 static cp_declarator *cp_parser_conversion_declarator_opt
1568   (cp_parser *);
1569 static bool cp_parser_ctor_initializer_opt
1570   (cp_parser *);
1571 static void cp_parser_mem_initializer_list
1572   (cp_parser *);
1573 static tree cp_parser_mem_initializer
1574   (cp_parser *);
1575 static tree cp_parser_mem_initializer_id
1576   (cp_parser *);
1577
1578 /* Overloading [gram.over] */
1579
1580 static tree cp_parser_operator_function_id
1581   (cp_parser *);
1582 static tree cp_parser_operator
1583   (cp_parser *);
1584
1585 /* Templates [gram.temp] */
1586
1587 static void cp_parser_template_declaration
1588   (cp_parser *, bool);
1589 static tree cp_parser_template_parameter_list
1590   (cp_parser *);
1591 static tree cp_parser_template_parameter
1592   (cp_parser *, bool *);
1593 static tree cp_parser_type_parameter
1594   (cp_parser *);
1595 static tree cp_parser_template_id
1596   (cp_parser *, bool, bool, bool);
1597 static tree cp_parser_template_name
1598   (cp_parser *, bool, bool, bool, bool *);
1599 static tree cp_parser_template_argument_list
1600   (cp_parser *);
1601 static tree cp_parser_template_argument
1602   (cp_parser *);
1603 static void cp_parser_explicit_instantiation
1604   (cp_parser *);
1605 static void cp_parser_explicit_specialization
1606   (cp_parser *);
1607
1608 /* Exception handling [gram.exception] */
1609
1610 static tree cp_parser_try_block
1611   (cp_parser *);
1612 static bool cp_parser_function_try_block
1613   (cp_parser *);
1614 static void cp_parser_handler_seq
1615   (cp_parser *);
1616 static void cp_parser_handler
1617   (cp_parser *);
1618 static tree cp_parser_exception_declaration
1619   (cp_parser *);
1620 static tree cp_parser_throw_expression
1621   (cp_parser *);
1622 static tree cp_parser_exception_specification_opt
1623   (cp_parser *);
1624 static tree cp_parser_type_id_list
1625   (cp_parser *);
1626
1627 /* GNU Extensions */
1628
1629 static tree cp_parser_asm_specification_opt
1630   (cp_parser *);
1631 static tree cp_parser_asm_operand_list
1632   (cp_parser *);
1633 static tree cp_parser_asm_clobber_list
1634   (cp_parser *);
1635 static tree cp_parser_attributes_opt
1636   (cp_parser *);
1637 static tree cp_parser_attribute_list
1638   (cp_parser *);
1639 static bool cp_parser_extension_opt
1640   (cp_parser *, int *);
1641 static void cp_parser_label_declaration
1642   (cp_parser *);
1643
1644 /* Utility Routines */
1645
1646 static tree cp_parser_lookup_name
1647   (cp_parser *, tree, bool, bool, bool, bool, bool *);
1648 static tree cp_parser_lookup_name_simple
1649   (cp_parser *, tree);
1650 static tree cp_parser_maybe_treat_template_as_class
1651   (tree, bool);
1652 static bool cp_parser_check_declarator_template_parameters
1653   (cp_parser *, cp_declarator *);
1654 static bool cp_parser_check_template_parameters
1655   (cp_parser *, unsigned);
1656 static tree cp_parser_simple_cast_expression
1657   (cp_parser *);
1658 static tree cp_parser_global_scope_opt
1659   (cp_parser *, bool);
1660 static bool cp_parser_constructor_declarator_p
1661   (cp_parser *, bool);
1662 static tree cp_parser_function_definition_from_specifiers_and_declarator
1663   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1664 static tree cp_parser_function_definition_after_declarator
1665   (cp_parser *, bool);
1666 static void cp_parser_template_declaration_after_export
1667   (cp_parser *, bool);
1668 static tree cp_parser_single_declaration
1669   (cp_parser *, bool, bool *);
1670 static tree cp_parser_functional_cast
1671   (cp_parser *, tree);
1672 static tree cp_parser_save_member_function_body
1673   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1674 static tree cp_parser_enclosed_template_argument_list
1675   (cp_parser *);
1676 static void cp_parser_save_default_args
1677   (cp_parser *, tree);
1678 static void cp_parser_late_parsing_for_member
1679   (cp_parser *, tree);
1680 static void cp_parser_late_parsing_default_args
1681   (cp_parser *, tree);
1682 static tree cp_parser_sizeof_operand
1683   (cp_parser *, enum rid);
1684 static bool cp_parser_declares_only_class_p
1685   (cp_parser *);
1686 static void cp_parser_set_storage_class
1687   (cp_decl_specifier_seq *, cp_storage_class);
1688 static void cp_parser_set_decl_spec_type
1689   (cp_decl_specifier_seq *, tree, bool);
1690 static bool cp_parser_friend_p
1691   (const cp_decl_specifier_seq *);
1692 static cp_token *cp_parser_require
1693   (cp_parser *, enum cpp_ttype, const char *);
1694 static cp_token *cp_parser_require_keyword
1695   (cp_parser *, enum rid, const char *);
1696 static bool cp_parser_token_starts_function_definition_p
1697   (cp_token *);
1698 static bool cp_parser_next_token_starts_class_definition_p
1699   (cp_parser *);
1700 static bool cp_parser_next_token_ends_template_argument_p
1701   (cp_parser *);
1702 static bool cp_parser_nth_token_starts_template_argument_list_p
1703   (cp_parser *, size_t);
1704 static enum tag_types cp_parser_token_is_class_key
1705   (cp_token *);
1706 static void cp_parser_check_class_key
1707   (enum tag_types, tree type);
1708 static void cp_parser_check_access_in_redeclaration
1709   (tree type);
1710 static bool cp_parser_optional_template_keyword
1711   (cp_parser *);
1712 static void cp_parser_pre_parsed_nested_name_specifier
1713   (cp_parser *);
1714 static void cp_parser_cache_group
1715   (cp_parser *, enum cpp_ttype, unsigned);
1716 static void cp_parser_parse_tentatively
1717   (cp_parser *);
1718 static void cp_parser_commit_to_tentative_parse
1719   (cp_parser *);
1720 static void cp_parser_abort_tentative_parse
1721   (cp_parser *);
1722 static bool cp_parser_parse_definitely
1723   (cp_parser *);
1724 static inline bool cp_parser_parsing_tentatively
1725   (cp_parser *);
1726 static bool cp_parser_committed_to_tentative_parse
1727   (cp_parser *);
1728 static void cp_parser_error
1729   (cp_parser *, const char *);
1730 static void cp_parser_name_lookup_error
1731   (cp_parser *, tree, tree, const char *);
1732 static bool cp_parser_simulate_error
1733   (cp_parser *);
1734 static void cp_parser_check_type_definition
1735   (cp_parser *);
1736 static void cp_parser_check_for_definition_in_return_type
1737   (cp_declarator *, int);
1738 static void cp_parser_check_for_invalid_template_id
1739   (cp_parser *, tree);
1740 static bool cp_parser_non_integral_constant_expression
1741   (cp_parser *, const char *);
1742 static void cp_parser_diagnose_invalid_type_name
1743   (cp_parser *, tree, tree);
1744 static bool cp_parser_parse_and_diagnose_invalid_type_name
1745   (cp_parser *);
1746 static int cp_parser_skip_to_closing_parenthesis
1747   (cp_parser *, bool, bool, bool);
1748 static void cp_parser_skip_to_end_of_statement
1749   (cp_parser *);
1750 static void cp_parser_consume_semicolon_at_end_of_statement
1751   (cp_parser *);
1752 static void cp_parser_skip_to_end_of_block_or_statement
1753   (cp_parser *);
1754 static void cp_parser_skip_to_closing_brace
1755   (cp_parser *);
1756 static void cp_parser_skip_until_found
1757   (cp_parser *, enum cpp_ttype, const char *);
1758 static bool cp_parser_error_occurred
1759   (cp_parser *);
1760 static bool cp_parser_allow_gnu_extensions_p
1761   (cp_parser *);
1762 static bool cp_parser_is_string_literal
1763   (cp_token *);
1764 static bool cp_parser_is_keyword
1765   (cp_token *, enum rid);
1766 static tree cp_parser_make_typename_type
1767   (cp_parser *, tree, tree);
1768
1769 /* Returns nonzero if we are parsing tentatively.  */
1770
1771 static inline bool
1772 cp_parser_parsing_tentatively (cp_parser* parser)
1773 {
1774   return parser->context->next != NULL;
1775 }
1776
1777 /* Returns nonzero if TOKEN is a string literal.  */
1778
1779 static bool
1780 cp_parser_is_string_literal (cp_token* token)
1781 {
1782   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1783 }
1784
1785 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1786
1787 static bool
1788 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1789 {
1790   return token->keyword == keyword;
1791 }
1792
1793 /* If not parsing tentatively, issue a diagnostic of the form
1794       FILE:LINE: MESSAGE before TOKEN
1795    where TOKEN is the next token in the input stream.  MESSAGE
1796    (specified by the caller) is usually of the form "expected
1797    OTHER-TOKEN".  */
1798
1799 static void
1800 cp_parser_error (cp_parser* parser, const char* message)
1801 {
1802   if (!cp_parser_simulate_error (parser))
1803     {
1804       cp_token *token = cp_lexer_peek_token (parser->lexer);
1805       /* This diagnostic makes more sense if it is tagged to the line
1806          of the token we just peeked at.  */
1807       cp_lexer_set_source_position_from_token (token);
1808       c_parse_error (message,
1809                      /* Because c_parser_error does not understand
1810                         CPP_KEYWORD, keywords are treated like
1811                         identifiers.  */
1812                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1813                      token->value);
1814     }
1815 }
1816
1817 /* Issue an error about name-lookup failing.  NAME is the
1818    IDENTIFIER_NODE DECL is the result of
1819    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1820    the thing that we hoped to find.  */
1821
1822 static void
1823 cp_parser_name_lookup_error (cp_parser* parser,
1824                              tree name,
1825                              tree decl,
1826                              const char* desired)
1827 {
1828   /* If name lookup completely failed, tell the user that NAME was not
1829      declared.  */
1830   if (decl == error_mark_node)
1831     {
1832       if (parser->scope && parser->scope != global_namespace)
1833         error ("%<%D::%D%> has not been declared",
1834                parser->scope, name);
1835       else if (parser->scope == global_namespace)
1836         error ("%<::%D%> has not been declared", name);
1837       else if (parser->object_scope 
1838                && !CLASS_TYPE_P (parser->object_scope))
1839         error ("request for member %qD in non-class type %qT",
1840                name, parser->object_scope);
1841       else if (parser->object_scope)
1842         error ("%<%T::%D%> has not been declared", 
1843                parser->object_scope, name);
1844       else
1845         error ("`%D' has not been declared", name);
1846     }
1847   else if (parser->scope && parser->scope != global_namespace)
1848     error ("%<%D::%D%> %s", parser->scope, name, desired);
1849   else if (parser->scope == global_namespace)
1850     error ("%<::%D%> %s", name, desired);
1851   else
1852     error ("%qD %s", name, desired);
1853 }
1854
1855 /* If we are parsing tentatively, remember that an error has occurred
1856    during this tentative parse.  Returns true if the error was
1857    simulated; false if a message should be issued by the caller.  */
1858
1859 static bool
1860 cp_parser_simulate_error (cp_parser* parser)
1861 {
1862   if (cp_parser_parsing_tentatively (parser)
1863       && !cp_parser_committed_to_tentative_parse (parser))
1864     {
1865       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1866       return true;
1867     }
1868   return false;
1869 }
1870
1871 /* This function is called when a type is defined.  If type
1872    definitions are forbidden at this point, an error message is
1873    issued.  */
1874
1875 static void
1876 cp_parser_check_type_definition (cp_parser* parser)
1877 {
1878   /* If types are forbidden here, issue a message.  */
1879   if (parser->type_definition_forbidden_message)
1880     /* Use `%s' to print the string in case there are any escape
1881        characters in the message.  */
1882     error ("%s", parser->type_definition_forbidden_message);
1883 }
1884
1885 /* This function is called when a declaration is parsed.  If
1886    DECLARATOR is a function declarator and DECLARES_CLASS_OR_ENUM
1887    indicates that a type was defined in the decl-specifiers for DECL,
1888    then an error is issued.  */
1889
1890 static void
1891 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1892                                                int declares_class_or_enum)
1893 {
1894   /* [dcl.fct] forbids type definitions in return types.
1895      Unfortunately, it's not easy to know whether or not we are
1896      processing a return type until after the fact.  */
1897   while (declarator
1898          && (declarator->kind == cdk_pointer
1899              || declarator->kind == cdk_reference
1900              || declarator->kind == cdk_ptrmem))
1901     declarator = declarator->declarator;
1902   if (declarator
1903       && declarator->kind == cdk_function
1904       && declares_class_or_enum & 2)
1905     error ("new types may not be defined in a return type");
1906 }
1907
1908 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1909    "<" in any valid C++ program.  If the next token is indeed "<",
1910    issue a message warning the user about what appears to be an
1911    invalid attempt to form a template-id.  */
1912
1913 static void
1914 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1915                                          tree type)
1916 {
1917   cp_token_position start = 0;
1918
1919   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1920     {
1921       if (TYPE_P (type))
1922         error ("%qT is not a template", type);
1923       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1924         error ("%qE is not a template", type);
1925       else
1926         error ("invalid template-id");
1927       /* Remember the location of the invalid "<".  */
1928       if (cp_parser_parsing_tentatively (parser)
1929           && !cp_parser_committed_to_tentative_parse (parser))
1930         start = cp_lexer_token_position (parser->lexer, true);
1931       /* Consume the "<".  */
1932       cp_lexer_consume_token (parser->lexer);
1933       /* Parse the template arguments.  */
1934       cp_parser_enclosed_template_argument_list (parser);
1935       /* Permanently remove the invalid template arguments so that
1936          this error message is not issued again.  */
1937       if (start)
1938         cp_lexer_purge_tokens_after (parser->lexer, start);
1939     }
1940 }
1941
1942 /* If parsing an integral constant-expression, issue an error message
1943    about the fact that THING appeared and return true.  Otherwise,
1944    return false, marking the current expression as non-constant.  */
1945
1946 static bool
1947 cp_parser_non_integral_constant_expression (cp_parser  *parser,
1948                                             const char *thing)
1949 {
1950   if (parser->integral_constant_expression_p)
1951     {
1952       if (!parser->allow_non_integral_constant_expression_p)
1953         {
1954           error ("%s cannot appear in a constant-expression", thing);
1955           return true;
1956         }
1957       parser->non_integral_constant_expression_p = true;
1958     }
1959   return false;
1960 }
1961
1962 /* Emit a diagnostic for an invalid type name. Consider also if it is
1963    qualified or not and the result of a lookup, to provide a better
1964    message.  */
1965
1966 static void
1967 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
1968 {
1969   tree decl, old_scope;
1970   /* Try to lookup the identifier.  */
1971   old_scope = parser->scope;
1972   parser->scope = scope;
1973   decl = cp_parser_lookup_name_simple (parser, id);
1974   parser->scope = old_scope;
1975   /* If the lookup found a template-name, it means that the user forgot
1976   to specify an argument list. Emit an useful error message.  */
1977   if (TREE_CODE (decl) == TEMPLATE_DECL)
1978     error ("invalid use of template-name %qE without an argument list",
1979       decl);
1980   else if (!parser->scope)
1981     {
1982       /* Issue an error message.  */
1983       error ("%qE does not name a type", id);
1984       /* If we're in a template class, it's possible that the user was
1985          referring to a type from a base class.  For example:
1986
1987            template <typename T> struct A { typedef T X; };
1988            template <typename T> struct B : public A<T> { X x; };
1989
1990          The user should have said "typename A<T>::X".  */
1991       if (processing_template_decl && current_class_type)
1992         {
1993           tree b;
1994
1995           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
1996                b;
1997                b = TREE_CHAIN (b))
1998             {
1999               tree base_type = BINFO_TYPE (b);
2000               if (CLASS_TYPE_P (base_type)
2001                   && dependent_type_p (base_type))
2002                 {
2003                   tree field;
2004                   /* Go from a particular instantiation of the
2005                      template (which will have an empty TYPE_FIELDs),
2006                      to the main version.  */
2007                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2008                   for (field = TYPE_FIELDS (base_type);
2009                        field;
2010                        field = TREE_CHAIN (field))
2011                     if (TREE_CODE (field) == TYPE_DECL
2012                         && DECL_NAME (field) == id)
2013                       {
2014                         inform ("(perhaps `typename %T::%E' was intended)",
2015                                 BINFO_TYPE (b), id);
2016                         break;
2017                       }
2018                   if (field)
2019                     break;
2020                 }
2021             }
2022         }
2023     }
2024   /* Here we diagnose qualified-ids where the scope is actually correct,
2025      but the identifier does not resolve to a valid type name.  */
2026   else
2027     {
2028       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2029         error ("%qE in namespace %qE does not name a type",
2030                id, parser->scope);
2031       else if (TYPE_P (parser->scope))
2032         error ("q%E in class %qT does not name a type", id, parser->scope);
2033       else
2034         gcc_unreachable ();
2035     }
2036 }
2037
2038 /* Check for a common situation where a type-name should be present,
2039    but is not, and issue a sensible error message.  Returns true if an
2040    invalid type-name was detected.
2041
2042    The situation handled by this function are variable declarations of the
2043    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2044    Usually, `ID' should name a type, but if we got here it means that it
2045    does not. We try to emit the best possible error message depending on
2046    how exactly the id-expression looks like.
2047 */
2048
2049 static bool
2050 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2051 {
2052   tree id;
2053
2054   cp_parser_parse_tentatively (parser);
2055   id = cp_parser_id_expression (parser,
2056                                 /*template_keyword_p=*/false,
2057                                 /*check_dependency_p=*/true,
2058                                 /*template_p=*/NULL,
2059                                 /*declarator_p=*/true);
2060   /* After the id-expression, there should be a plain identifier,
2061      otherwise this is not a simple variable declaration. Also, if
2062      the scope is dependent, we cannot do much.  */
2063   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2064       || (parser->scope && TYPE_P (parser->scope)
2065           && dependent_type_p (parser->scope)))
2066     {
2067       cp_parser_abort_tentative_parse (parser);
2068       return false;
2069     }
2070   if (!cp_parser_parse_definitely (parser)
2071       || TREE_CODE (id) != IDENTIFIER_NODE)
2072     return false;
2073
2074   /* Emit a diagnostic for the invalid type.  */
2075   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2076   /* Skip to the end of the declaration; there's no point in
2077      trying to process it.  */
2078   cp_parser_skip_to_end_of_block_or_statement (parser);
2079   return true;
2080 }
2081
2082 /* Consume tokens up to, and including, the next non-nested closing `)'.
2083    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2084    are doing error recovery. Returns -1 if OR_COMMA is true and we
2085    found an unnested comma.  */
2086
2087 static int
2088 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2089                                        bool recovering,
2090                                        bool or_comma,
2091                                        bool consume_paren)
2092 {
2093   unsigned paren_depth = 0;
2094   unsigned brace_depth = 0;
2095   int result;
2096
2097   if (recovering && !or_comma && cp_parser_parsing_tentatively (parser)
2098       && !cp_parser_committed_to_tentative_parse (parser))
2099     return 0;
2100
2101   while (true)
2102     {
2103       cp_token *token;
2104
2105       /* If we've run out of tokens, then there is no closing `)'.  */
2106       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2107         {
2108           result = 0;
2109           break;
2110         }
2111
2112       token = cp_lexer_peek_token (parser->lexer);
2113
2114       /* This matches the processing in skip_to_end_of_statement.  */
2115       if (token->type == CPP_SEMICOLON && !brace_depth)
2116         {
2117           result = 0;
2118           break;
2119         }
2120       if (token->type == CPP_OPEN_BRACE)
2121         ++brace_depth;
2122       if (token->type == CPP_CLOSE_BRACE)
2123         {
2124           if (!brace_depth--)
2125             {
2126               result = 0;
2127               break;
2128             }
2129         }
2130       if (recovering && or_comma && token->type == CPP_COMMA
2131           && !brace_depth && !paren_depth)
2132         {
2133           result = -1;
2134           break;
2135         }
2136
2137       if (!brace_depth)
2138         {
2139           /* If it is an `(', we have entered another level of nesting.  */
2140           if (token->type == CPP_OPEN_PAREN)
2141             ++paren_depth;
2142           /* If it is a `)', then we might be done.  */
2143           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2144             {
2145               if (consume_paren)
2146                 cp_lexer_consume_token (parser->lexer);
2147               {
2148                 result = 1;
2149                 break;
2150               }
2151             }
2152         }
2153
2154       /* Consume the token.  */
2155       cp_lexer_consume_token (parser->lexer);
2156     }
2157
2158   return result;
2159 }
2160
2161 /* Consume tokens until we reach the end of the current statement.
2162    Normally, that will be just before consuming a `;'.  However, if a
2163    non-nested `}' comes first, then we stop before consuming that.  */
2164
2165 static void
2166 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2167 {
2168   unsigned nesting_depth = 0;
2169
2170   while (true)
2171     {
2172       cp_token *token;
2173
2174       /* Peek at the next token.  */
2175       token = cp_lexer_peek_token (parser->lexer);
2176       /* If we've run out of tokens, stop.  */
2177       if (token->type == CPP_EOF)
2178         break;
2179       /* If the next token is a `;', we have reached the end of the
2180          statement.  */
2181       if (token->type == CPP_SEMICOLON && !nesting_depth)
2182         break;
2183       /* If the next token is a non-nested `}', then we have reached
2184          the end of the current block.  */
2185       if (token->type == CPP_CLOSE_BRACE)
2186         {
2187           /* If this is a non-nested `}', stop before consuming it.
2188              That way, when confronted with something like:
2189
2190                { 3 + }
2191
2192              we stop before consuming the closing `}', even though we
2193              have not yet reached a `;'.  */
2194           if (nesting_depth == 0)
2195             break;
2196           /* If it is the closing `}' for a block that we have
2197              scanned, stop -- but only after consuming the token.
2198              That way given:
2199
2200                 void f g () { ... }
2201                 typedef int I;
2202
2203              we will stop after the body of the erroneously declared
2204              function, but before consuming the following `typedef'
2205              declaration.  */
2206           if (--nesting_depth == 0)
2207             {
2208               cp_lexer_consume_token (parser->lexer);
2209               break;
2210             }
2211         }
2212       /* If it the next token is a `{', then we are entering a new
2213          block.  Consume the entire block.  */
2214       else if (token->type == CPP_OPEN_BRACE)
2215         ++nesting_depth;
2216       /* Consume the token.  */
2217       cp_lexer_consume_token (parser->lexer);
2218     }
2219 }
2220
2221 /* This function is called at the end of a statement or declaration.
2222    If the next token is a semicolon, it is consumed; otherwise, error
2223    recovery is attempted.  */
2224
2225 static void
2226 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2227 {
2228   /* Look for the trailing `;'.  */
2229   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2230     {
2231       /* If there is additional (erroneous) input, skip to the end of
2232          the statement.  */
2233       cp_parser_skip_to_end_of_statement (parser);
2234       /* If the next token is now a `;', consume it.  */
2235       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2236         cp_lexer_consume_token (parser->lexer);
2237     }
2238 }
2239
2240 /* Skip tokens until we have consumed an entire block, or until we
2241    have consumed a non-nested `;'.  */
2242
2243 static void
2244 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2245 {
2246   unsigned nesting_depth = 0;
2247
2248   while (true)
2249     {
2250       cp_token *token;
2251
2252       /* Peek at the next token.  */
2253       token = cp_lexer_peek_token (parser->lexer);
2254       /* If we've run out of tokens, stop.  */
2255       if (token->type == CPP_EOF)
2256         break;
2257       /* If the next token is a `;', we have reached the end of the
2258          statement.  */
2259       if (token->type == CPP_SEMICOLON && !nesting_depth)
2260         {
2261           /* Consume the `;'.  */
2262           cp_lexer_consume_token (parser->lexer);
2263           break;
2264         }
2265       /* Consume the token.  */
2266       token = cp_lexer_consume_token (parser->lexer);
2267       /* If the next token is a non-nested `}', then we have reached
2268          the end of the current block.  */
2269       if (token->type == CPP_CLOSE_BRACE
2270           && (nesting_depth == 0 || --nesting_depth == 0))
2271         break;
2272       /* If it the next token is a `{', then we are entering a new
2273          block.  Consume the entire block.  */
2274       if (token->type == CPP_OPEN_BRACE)
2275         ++nesting_depth;
2276     }
2277 }
2278
2279 /* Skip tokens until a non-nested closing curly brace is the next
2280    token.  */
2281
2282 static void
2283 cp_parser_skip_to_closing_brace (cp_parser *parser)
2284 {
2285   unsigned nesting_depth = 0;
2286
2287   while (true)
2288     {
2289       cp_token *token;
2290
2291       /* Peek at the next token.  */
2292       token = cp_lexer_peek_token (parser->lexer);
2293       /* If we've run out of tokens, stop.  */
2294       if (token->type == CPP_EOF)
2295         break;
2296       /* If the next token is a non-nested `}', then we have reached
2297          the end of the current block.  */
2298       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2299         break;
2300       /* If it the next token is a `{', then we are entering a new
2301          block.  Consume the entire block.  */
2302       else if (token->type == CPP_OPEN_BRACE)
2303         ++nesting_depth;
2304       /* Consume the token.  */
2305       cp_lexer_consume_token (parser->lexer);
2306     }
2307 }
2308
2309 /* This is a simple wrapper around make_typename_type. When the id is
2310    an unresolved identifier node, we can provide a superior diagnostic
2311    using cp_parser_diagnose_invalid_type_name.  */
2312
2313 static tree
2314 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2315 {
2316   tree result;
2317   if (TREE_CODE (id) == IDENTIFIER_NODE)
2318     {
2319       result = make_typename_type (scope, id, /*complain=*/0);
2320       if (result == error_mark_node)
2321         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2322       return result;
2323     }
2324   return make_typename_type (scope, id, tf_error);
2325 }
2326
2327
2328 /* Create a new C++ parser.  */
2329
2330 static cp_parser *
2331 cp_parser_new (void)
2332 {
2333   cp_parser *parser;
2334   cp_lexer *lexer;
2335   unsigned i;
2336
2337   /* cp_lexer_new_main is called before calling ggc_alloc because
2338      cp_lexer_new_main might load a PCH file.  */
2339   lexer = cp_lexer_new_main ();
2340
2341   /* Initialize the binops_by_token so that we can get the tree
2342      directly from the token.  */
2343   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2344     binops_by_token[binops[i].token_type] = binops[i];
2345
2346   parser = GGC_CNEW (cp_parser);
2347   parser->lexer = lexer;
2348   parser->context = cp_parser_context_new (NULL);
2349
2350   /* For now, we always accept GNU extensions.  */
2351   parser->allow_gnu_extensions_p = 1;
2352
2353   /* The `>' token is a greater-than operator, not the end of a
2354      template-id.  */
2355   parser->greater_than_is_operator_p = true;
2356
2357   parser->default_arg_ok_p = true;
2358
2359   /* We are not parsing a constant-expression.  */
2360   parser->integral_constant_expression_p = false;
2361   parser->allow_non_integral_constant_expression_p = false;
2362   parser->non_integral_constant_expression_p = false;
2363
2364   /* Local variable names are not forbidden.  */
2365   parser->local_variables_forbidden_p = false;
2366
2367   /* We are not processing an `extern "C"' declaration.  */
2368   parser->in_unbraced_linkage_specification_p = false;
2369
2370   /* We are not processing a declarator.  */
2371   parser->in_declarator_p = false;
2372
2373   /* We are not processing a template-argument-list.  */
2374   parser->in_template_argument_list_p = false;
2375
2376   /* We are not in an iteration statement.  */
2377   parser->in_iteration_statement_p = false;
2378
2379   /* We are not in a switch statement.  */
2380   parser->in_switch_statement_p = false;
2381
2382   /* We are not parsing a type-id inside an expression.  */
2383   parser->in_type_id_in_expr_p = false;
2384
2385   /* Declarations aren't implicitly extern "C". */
2386   parser->implicit_extern_c = false;
2387
2388   /* String literals should be translated to the execution character set.  */
2389   parser->translate_strings_p = true;
2390
2391   /* The unparsed function queue is empty.  */
2392   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2393
2394   /* There are no classes being defined.  */
2395   parser->num_classes_being_defined = 0;
2396
2397   /* No template parameters apply.  */
2398   parser->num_template_parameter_lists = 0;
2399
2400   return parser;
2401 }
2402
2403 /* Create a cp_lexer structure which will emit the tokens in CACHE
2404    and push it onto the parser's lexer stack.  This is used for delayed
2405    parsing of in-class method bodies and default arguments, and should
2406    not be confused with tentative parsing.  */
2407 static void
2408 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2409 {
2410   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2411   lexer->next = parser->lexer;
2412   parser->lexer = lexer;
2413
2414   /* Move the current source position to that of the first token in the
2415      new lexer.  */
2416   cp_lexer_set_source_position_from_token (lexer->next_token);
2417 }
2418
2419 /* Pop the top lexer off the parser stack.  This is never used for the
2420    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2421 static void
2422 cp_parser_pop_lexer (cp_parser *parser)
2423 {
2424   cp_lexer *lexer = parser->lexer;
2425   parser->lexer = lexer->next;
2426   cp_lexer_destroy (lexer);
2427
2428   /* Put the current source position back where it was before this
2429      lexer was pushed.  */
2430   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2431 }
2432
2433 /* Lexical conventions [gram.lex]  */
2434
2435 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2436    identifier.  */
2437
2438 static tree
2439 cp_parser_identifier (cp_parser* parser)
2440 {
2441   cp_token *token;
2442
2443   /* Look for the identifier.  */
2444   token = cp_parser_require (parser, CPP_NAME, "identifier");
2445   /* Return the value.  */
2446   return token ? token->value : error_mark_node;
2447 }
2448
2449 /* Parse a sequence of adjacent string constants.  Returns a
2450    TREE_STRING representing the combined, nul-terminated string
2451    constant.  If TRANSLATE is true, translate the string to the
2452    execution character set.  If WIDE_OK is true, a wide string is
2453    invalid here.
2454
2455    C++98 [lex.string] says that if a narrow string literal token is
2456    adjacent to a wide string literal token, the behavior is undefined.
2457    However, C99 6.4.5p4 says that this results in a wide string literal.
2458    We follow C99 here, for consistency with the C front end.
2459
2460    This code is largely lifted from lex_string() in c-lex.c.
2461
2462    FUTURE: ObjC++ will need to handle @-strings here.  */
2463 static tree
2464 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2465 {
2466   tree value;
2467   bool wide = false;
2468   size_t count;
2469   struct obstack str_ob;
2470   cpp_string str, istr, *strs;
2471   cp_token *tok;
2472
2473   tok = cp_lexer_peek_token (parser->lexer);
2474   if (!cp_parser_is_string_literal (tok))
2475     {
2476       cp_parser_error (parser, "expected string-literal");
2477       return error_mark_node;
2478     }
2479
2480   /* Try to avoid the overhead of creating and destroying an obstack
2481      for the common case of just one string.  */
2482   if (!cp_parser_is_string_literal
2483       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2484     {
2485       cp_lexer_consume_token (parser->lexer);
2486
2487       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2488       str.len = TREE_STRING_LENGTH (tok->value);
2489       count = 1;
2490       if (tok->type == CPP_WSTRING)
2491         wide = true;
2492
2493       strs = &str;
2494     }
2495   else
2496     {
2497       gcc_obstack_init (&str_ob);
2498       count = 0;
2499
2500       do
2501         {
2502           cp_lexer_consume_token (parser->lexer);
2503           count++;
2504           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2505           str.len = TREE_STRING_LENGTH (tok->value);
2506           if (tok->type == CPP_WSTRING)
2507             wide = true;
2508
2509           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2510
2511           tok = cp_lexer_peek_token (parser->lexer);
2512         }
2513       while (cp_parser_is_string_literal (tok));
2514
2515       strs = (cpp_string *) obstack_finish (&str_ob);
2516     }
2517
2518   if (wide && !wide_ok)
2519     {
2520       cp_parser_error (parser, "a wide string is invalid in this context");
2521       wide = false;
2522     }
2523
2524   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2525       (parse_in, strs, count, &istr, wide))
2526     {
2527       value = build_string (istr.len, (char *)istr.text);
2528       free ((void *)istr.text);
2529
2530       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2531       value = fix_string_type (value);
2532     }
2533   else
2534     /* cpp_interpret_string has issued an error.  */
2535     value = error_mark_node;
2536
2537   if (count > 1)
2538     obstack_free (&str_ob, 0);
2539
2540   return value;
2541 }
2542
2543
2544 /* Basic concepts [gram.basic]  */
2545
2546 /* Parse a translation-unit.
2547
2548    translation-unit:
2549      declaration-seq [opt]
2550
2551    Returns TRUE if all went well.  */
2552
2553 static bool
2554 cp_parser_translation_unit (cp_parser* parser)
2555 {
2556   /* The address of the first non-permanent object on the declarator
2557      obstack.  */
2558   static void *declarator_obstack_base;
2559
2560   bool success;
2561
2562   /* Create the declarator obstack, if necessary.  */
2563   if (!cp_error_declarator)
2564     {
2565       gcc_obstack_init (&declarator_obstack);
2566       /* Create the error declarator.  */
2567       cp_error_declarator = make_declarator (cdk_error);
2568       /* Create the empty parameter list.  */
2569       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2570       /* Remember where the base of the declarator obstack lies.  */
2571       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2572     }
2573
2574   while (true)
2575     {
2576       cp_parser_declaration_seq_opt (parser);
2577
2578       /* If there are no tokens left then all went well.  */
2579       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2580         {
2581           /* Get rid of the token array; we don't need it any more. */
2582           cp_lexer_destroy (parser->lexer);
2583           parser->lexer = NULL;
2584
2585           /* This file might have been a context that's implicitly extern
2586              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2587           if (parser->implicit_extern_c)
2588             {
2589               pop_lang_context ();
2590               parser->implicit_extern_c = false;
2591             }
2592
2593           /* Finish up.  */
2594           finish_translation_unit ();
2595
2596           success = true;
2597           break;
2598         }
2599       else
2600         {
2601           cp_parser_error (parser, "expected declaration");
2602           success = false;
2603           break;
2604         }
2605     }
2606
2607   /* Make sure the declarator obstack was fully cleaned up.  */
2608   gcc_assert (obstack_next_free (&declarator_obstack)
2609               == declarator_obstack_base);
2610
2611   /* All went well.  */
2612   return success;
2613 }
2614
2615 /* Expressions [gram.expr] */
2616
2617 /* Parse a primary-expression.
2618
2619    primary-expression:
2620      literal
2621      this
2622      ( expression )
2623      id-expression
2624
2625    GNU Extensions:
2626
2627    primary-expression:
2628      ( compound-statement )
2629      __builtin_va_arg ( assignment-expression , type-id )
2630
2631    literal:
2632      __null
2633
2634    Returns a representation of the expression.
2635
2636    *IDK indicates what kind of id-expression (if any) was present.
2637
2638    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2639    used as the operand of a pointer-to-member.  In that case,
2640    *QUALIFYING_CLASS gives the class that is used as the qualifying
2641    class in the pointer-to-member.  */
2642
2643 static tree
2644 cp_parser_primary_expression (cp_parser *parser,
2645                               cp_id_kind *idk,
2646                               tree *qualifying_class)
2647 {
2648   cp_token *token;
2649
2650   /* Assume the primary expression is not an id-expression.  */
2651   *idk = CP_ID_KIND_NONE;
2652   /* And that it cannot be used as pointer-to-member.  */
2653   *qualifying_class = NULL_TREE;
2654
2655   /* Peek at the next token.  */
2656   token = cp_lexer_peek_token (parser->lexer);
2657   switch (token->type)
2658     {
2659       /* literal:
2660            integer-literal
2661            character-literal
2662            floating-literal
2663            string-literal
2664            boolean-literal  */
2665     case CPP_CHAR:
2666     case CPP_WCHAR:
2667     case CPP_NUMBER:
2668       token = cp_lexer_consume_token (parser->lexer);
2669       return token->value;
2670
2671     case CPP_STRING:
2672     case CPP_WSTRING:
2673       /* ??? Should wide strings be allowed when parser->translate_strings_p
2674          is false (i.e. in attributes)?  If not, we can kill the third
2675          argument to cp_parser_string_literal.  */
2676       return cp_parser_string_literal (parser,
2677                                        parser->translate_strings_p,
2678                                        true);
2679
2680     case CPP_OPEN_PAREN:
2681       {
2682         tree expr;
2683         bool saved_greater_than_is_operator_p;
2684
2685         /* Consume the `('.  */
2686         cp_lexer_consume_token (parser->lexer);
2687         /* Within a parenthesized expression, a `>' token is always
2688            the greater-than operator.  */
2689         saved_greater_than_is_operator_p
2690           = parser->greater_than_is_operator_p;
2691         parser->greater_than_is_operator_p = true;
2692         /* If we see `( { ' then we are looking at the beginning of
2693            a GNU statement-expression.  */
2694         if (cp_parser_allow_gnu_extensions_p (parser)
2695             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2696           {
2697             /* Statement-expressions are not allowed by the standard.  */
2698             if (pedantic)
2699               pedwarn ("ISO C++ forbids braced-groups within expressions");
2700
2701             /* And they're not allowed outside of a function-body; you
2702                cannot, for example, write:
2703
2704                  int i = ({ int j = 3; j + 1; });
2705
2706                at class or namespace scope.  */
2707             if (!at_function_scope_p ())
2708               error ("statement-expressions are allowed only inside functions");
2709             /* Start the statement-expression.  */
2710             expr = begin_stmt_expr ();
2711             /* Parse the compound-statement.  */
2712             cp_parser_compound_statement (parser, expr, false);
2713             /* Finish up.  */
2714             expr = finish_stmt_expr (expr, false);
2715           }
2716         else
2717           {
2718             /* Parse the parenthesized expression.  */
2719             expr = cp_parser_expression (parser);
2720             /* Let the front end know that this expression was
2721                enclosed in parentheses. This matters in case, for
2722                example, the expression is of the form `A::B', since
2723                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2724                not.  */
2725             finish_parenthesized_expr (expr);
2726           }
2727         /* The `>' token might be the end of a template-id or
2728            template-parameter-list now.  */
2729         parser->greater_than_is_operator_p
2730           = saved_greater_than_is_operator_p;
2731         /* Consume the `)'.  */
2732         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2733           cp_parser_skip_to_end_of_statement (parser);
2734
2735         return expr;
2736       }
2737
2738     case CPP_KEYWORD:
2739       switch (token->keyword)
2740         {
2741           /* These two are the boolean literals.  */
2742         case RID_TRUE:
2743           cp_lexer_consume_token (parser->lexer);
2744           return boolean_true_node;
2745         case RID_FALSE:
2746           cp_lexer_consume_token (parser->lexer);
2747           return boolean_false_node;
2748
2749           /* The `__null' literal.  */
2750         case RID_NULL:
2751           cp_lexer_consume_token (parser->lexer);
2752           return null_node;
2753
2754           /* Recognize the `this' keyword.  */
2755         case RID_THIS:
2756           cp_lexer_consume_token (parser->lexer);
2757           if (parser->local_variables_forbidden_p)
2758             {
2759               error ("%<this%> may not be used in this context");
2760               return error_mark_node;
2761             }
2762           /* Pointers cannot appear in constant-expressions.  */
2763           if (cp_parser_non_integral_constant_expression (parser,
2764                                                           "`this'"))
2765             return error_mark_node;
2766           return finish_this_expr ();
2767
2768           /* The `operator' keyword can be the beginning of an
2769              id-expression.  */
2770         case RID_OPERATOR:
2771           goto id_expression;
2772
2773         case RID_FUNCTION_NAME:
2774         case RID_PRETTY_FUNCTION_NAME:
2775         case RID_C99_FUNCTION_NAME:
2776           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2777              __func__ are the names of variables -- but they are
2778              treated specially.  Therefore, they are handled here,
2779              rather than relying on the generic id-expression logic
2780              below.  Grammatically, these names are id-expressions.
2781
2782              Consume the token.  */
2783           token = cp_lexer_consume_token (parser->lexer);
2784           /* Look up the name.  */
2785           return finish_fname (token->value);
2786
2787         case RID_VA_ARG:
2788           {
2789             tree expression;
2790             tree type;
2791
2792             /* The `__builtin_va_arg' construct is used to handle
2793                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2794             cp_lexer_consume_token (parser->lexer);
2795             /* Look for the opening `('.  */
2796             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2797             /* Now, parse the assignment-expression.  */
2798             expression = cp_parser_assignment_expression (parser);
2799             /* Look for the `,'.  */
2800             cp_parser_require (parser, CPP_COMMA, "`,'");
2801             /* Parse the type-id.  */
2802             type = cp_parser_type_id (parser);
2803             /* Look for the closing `)'.  */
2804             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2805             /* Using `va_arg' in a constant-expression is not
2806                allowed.  */
2807             if (cp_parser_non_integral_constant_expression (parser,
2808                                                             "`va_arg'"))
2809               return error_mark_node;
2810             return build_x_va_arg (expression, type);
2811           }
2812
2813         case RID_OFFSETOF:
2814           return cp_parser_builtin_offsetof (parser);
2815
2816         default:
2817           cp_parser_error (parser, "expected primary-expression");
2818           return error_mark_node;
2819         }
2820
2821       /* An id-expression can start with either an identifier, a
2822          `::' as the beginning of a qualified-id, or the "operator"
2823          keyword.  */
2824     case CPP_NAME:
2825     case CPP_SCOPE:
2826     case CPP_TEMPLATE_ID:
2827     case CPP_NESTED_NAME_SPECIFIER:
2828       {
2829         tree id_expression;
2830         tree decl;
2831         const char *error_msg;
2832
2833       id_expression:
2834         /* Parse the id-expression.  */
2835         id_expression
2836           = cp_parser_id_expression (parser,
2837                                      /*template_keyword_p=*/false,
2838                                      /*check_dependency_p=*/true,
2839                                      /*template_p=*/NULL,
2840                                      /*declarator_p=*/false);
2841         if (id_expression == error_mark_node)
2842           return error_mark_node;
2843         /* If we have a template-id, then no further lookup is
2844            required.  If the template-id was for a template-class, we
2845            will sometimes have a TYPE_DECL at this point.  */
2846         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2847             || TREE_CODE (id_expression) == TYPE_DECL)
2848           decl = id_expression;
2849         /* Look up the name.  */
2850         else
2851           {
2852             bool ambiguous_p;
2853
2854             decl = cp_parser_lookup_name (parser, id_expression,
2855                                           /*is_type=*/false,
2856                                           /*is_template=*/false,
2857                                           /*is_namespace=*/false,
2858                                           /*check_dependency=*/true,
2859                                           &ambiguous_p);
2860             /* If the lookup was ambiguous, an error will already have
2861                been issued.  */
2862             if (ambiguous_p)
2863               return error_mark_node;
2864             /* If name lookup gives us a SCOPE_REF, then the
2865                qualifying scope was dependent.  Just propagate the
2866                name.  */
2867             if (TREE_CODE (decl) == SCOPE_REF)
2868               {
2869                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2870                   *qualifying_class = TREE_OPERAND (decl, 0);
2871                 return decl;
2872               }
2873             /* Check to see if DECL is a local variable in a context
2874                where that is forbidden.  */
2875             if (parser->local_variables_forbidden_p
2876                 && local_variable_p (decl))
2877               {
2878                 /* It might be that we only found DECL because we are
2879                    trying to be generous with pre-ISO scoping rules.
2880                    For example, consider:
2881
2882                      int i;
2883                      void g() {
2884                        for (int i = 0; i < 10; ++i) {}
2885                        extern void f(int j = i);
2886                      }
2887
2888                    Here, name look up will originally find the out
2889                    of scope `i'.  We need to issue a warning message,
2890                    but then use the global `i'.  */
2891                 decl = check_for_out_of_scope_variable (decl);
2892                 if (local_variable_p (decl))
2893                   {
2894                     error ("local variable %qD may not appear in this context",
2895                            decl);
2896                     return error_mark_node;
2897                   }
2898               }
2899           }
2900
2901         decl = finish_id_expression (id_expression, decl, parser->scope,
2902                                      idk, qualifying_class,
2903                                      parser->integral_constant_expression_p,
2904                                      parser->allow_non_integral_constant_expression_p,
2905                                      &parser->non_integral_constant_expression_p,
2906                                      &error_msg);
2907         if (error_msg)
2908           cp_parser_error (parser, error_msg);
2909         return decl;
2910       }
2911
2912       /* Anything else is an error.  */
2913     default:
2914       cp_parser_error (parser, "expected primary-expression");
2915       return error_mark_node;
2916     }
2917 }
2918
2919 /* Parse an id-expression.
2920
2921    id-expression:
2922      unqualified-id
2923      qualified-id
2924
2925    qualified-id:
2926      :: [opt] nested-name-specifier template [opt] unqualified-id
2927      :: identifier
2928      :: operator-function-id
2929      :: template-id
2930
2931    Return a representation of the unqualified portion of the
2932    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
2933    a `::' or nested-name-specifier.
2934
2935    Often, if the id-expression was a qualified-id, the caller will
2936    want to make a SCOPE_REF to represent the qualified-id.  This
2937    function does not do this in order to avoid wastefully creating
2938    SCOPE_REFs when they are not required.
2939
2940    If TEMPLATE_KEYWORD_P is true, then we have just seen the
2941    `template' keyword.
2942
2943    If CHECK_DEPENDENCY_P is false, then names are looked up inside
2944    uninstantiated templates.
2945
2946    If *TEMPLATE_P is non-NULL, it is set to true iff the
2947    `template' keyword is used to explicitly indicate that the entity
2948    named is a template.
2949
2950    If DECLARATOR_P is true, the id-expression is appearing as part of
2951    a declarator, rather than as part of an expression.  */
2952
2953 static tree
2954 cp_parser_id_expression (cp_parser *parser,
2955                          bool template_keyword_p,
2956                          bool check_dependency_p,
2957                          bool *template_p,
2958                          bool declarator_p)
2959 {
2960   bool global_scope_p;
2961   bool nested_name_specifier_p;
2962
2963   /* Assume the `template' keyword was not used.  */
2964   if (template_p)
2965     *template_p = false;
2966
2967   /* Look for the optional `::' operator.  */
2968   global_scope_p
2969     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
2970        != NULL_TREE);
2971   /* Look for the optional nested-name-specifier.  */
2972   nested_name_specifier_p
2973     = (cp_parser_nested_name_specifier_opt (parser,
2974                                             /*typename_keyword_p=*/false,
2975                                             check_dependency_p,
2976                                             /*type_p=*/false,
2977                                             declarator_p)
2978        != NULL_TREE);
2979   /* If there is a nested-name-specifier, then we are looking at
2980      the first qualified-id production.  */
2981   if (nested_name_specifier_p)
2982     {
2983       tree saved_scope;
2984       tree saved_object_scope;
2985       tree saved_qualifying_scope;
2986       tree unqualified_id;
2987       bool is_template;
2988
2989       /* See if the next token is the `template' keyword.  */
2990       if (!template_p)
2991         template_p = &is_template;
2992       *template_p = cp_parser_optional_template_keyword (parser);
2993       /* Name lookup we do during the processing of the
2994          unqualified-id might obliterate SCOPE.  */
2995       saved_scope = parser->scope;
2996       saved_object_scope = parser->object_scope;
2997       saved_qualifying_scope = parser->qualifying_scope;
2998       /* Process the final unqualified-id.  */
2999       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3000                                                  check_dependency_p,
3001                                                  declarator_p);
3002       /* Restore the SAVED_SCOPE for our caller.  */
3003       parser->scope = saved_scope;
3004       parser->object_scope = saved_object_scope;
3005       parser->qualifying_scope = saved_qualifying_scope;
3006
3007       return unqualified_id;
3008     }
3009   /* Otherwise, if we are in global scope, then we are looking at one
3010      of the other qualified-id productions.  */
3011   else if (global_scope_p)
3012     {
3013       cp_token *token;
3014       tree id;
3015
3016       /* Peek at the next token.  */
3017       token = cp_lexer_peek_token (parser->lexer);
3018
3019       /* If it's an identifier, and the next token is not a "<", then
3020          we can avoid the template-id case.  This is an optimization
3021          for this common case.  */
3022       if (token->type == CPP_NAME
3023           && !cp_parser_nth_token_starts_template_argument_list_p
3024                (parser, 2))
3025         return cp_parser_identifier (parser);
3026
3027       cp_parser_parse_tentatively (parser);
3028       /* Try a template-id.  */
3029       id = cp_parser_template_id (parser,
3030                                   /*template_keyword_p=*/false,
3031                                   /*check_dependency_p=*/true,
3032                                   declarator_p);
3033       /* If that worked, we're done.  */
3034       if (cp_parser_parse_definitely (parser))
3035         return id;
3036
3037       /* Peek at the next token.  (Changes in the token buffer may
3038          have invalidated the pointer obtained above.)  */
3039       token = cp_lexer_peek_token (parser->lexer);
3040
3041       switch (token->type)
3042         {
3043         case CPP_NAME:
3044           return cp_parser_identifier (parser);
3045
3046         case CPP_KEYWORD:
3047           if (token->keyword == RID_OPERATOR)
3048             return cp_parser_operator_function_id (parser);
3049           /* Fall through.  */
3050
3051         default:
3052           cp_parser_error (parser, "expected id-expression");
3053           return error_mark_node;
3054         }
3055     }
3056   else
3057     return cp_parser_unqualified_id (parser, template_keyword_p,
3058                                      /*check_dependency_p=*/true,
3059                                      declarator_p);
3060 }
3061
3062 /* Parse an unqualified-id.
3063
3064    unqualified-id:
3065      identifier
3066      operator-function-id
3067      conversion-function-id
3068      ~ class-name
3069      template-id
3070
3071    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3072    keyword, in a construct like `A::template ...'.
3073
3074    Returns a representation of unqualified-id.  For the `identifier'
3075    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3076    production a BIT_NOT_EXPR is returned; the operand of the
3077    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3078    other productions, see the documentation accompanying the
3079    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3080    names are looked up in uninstantiated templates.  If DECLARATOR_P
3081    is true, the unqualified-id is appearing as part of a declarator,
3082    rather than as part of an expression.  */
3083
3084 static tree
3085 cp_parser_unqualified_id (cp_parser* parser,
3086                           bool template_keyword_p,
3087                           bool check_dependency_p,
3088                           bool declarator_p)
3089 {
3090   cp_token *token;
3091
3092   /* Peek at the next token.  */
3093   token = cp_lexer_peek_token (parser->lexer);
3094
3095   switch (token->type)
3096     {
3097     case CPP_NAME:
3098       {
3099         tree id;
3100
3101         /* We don't know yet whether or not this will be a
3102            template-id.  */
3103         cp_parser_parse_tentatively (parser);
3104         /* Try a template-id.  */
3105         id = cp_parser_template_id (parser, template_keyword_p,
3106                                     check_dependency_p,
3107                                     declarator_p);
3108         /* If it worked, we're done.  */
3109         if (cp_parser_parse_definitely (parser))
3110           return id;
3111         /* Otherwise, it's an ordinary identifier.  */
3112         return cp_parser_identifier (parser);
3113       }
3114
3115     case CPP_TEMPLATE_ID:
3116       return cp_parser_template_id (parser, template_keyword_p,
3117                                     check_dependency_p,
3118                                     declarator_p);
3119
3120     case CPP_COMPL:
3121       {
3122         tree type_decl;
3123         tree qualifying_scope;
3124         tree object_scope;
3125         tree scope;
3126
3127         /* Consume the `~' token.  */
3128         cp_lexer_consume_token (parser->lexer);
3129         /* Parse the class-name.  The standard, as written, seems to
3130            say that:
3131
3132              template <typename T> struct S { ~S (); };
3133              template <typename T> S<T>::~S() {}
3134
3135            is invalid, since `~' must be followed by a class-name, but
3136            `S<T>' is dependent, and so not known to be a class.
3137            That's not right; we need to look in uninstantiated
3138            templates.  A further complication arises from:
3139
3140              template <typename T> void f(T t) {
3141                t.T::~T();
3142              }
3143
3144            Here, it is not possible to look up `T' in the scope of `T'
3145            itself.  We must look in both the current scope, and the
3146            scope of the containing complete expression.
3147
3148            Yet another issue is:
3149
3150              struct S {
3151                int S;
3152                ~S();
3153              };
3154
3155              S::~S() {}
3156
3157            The standard does not seem to say that the `S' in `~S'
3158            should refer to the type `S' and not the data member
3159            `S::S'.  */
3160
3161         /* DR 244 says that we look up the name after the "~" in the
3162            same scope as we looked up the qualifying name.  That idea
3163            isn't fully worked out; it's more complicated than that.  */
3164         scope = parser->scope;
3165         object_scope = parser->object_scope;
3166         qualifying_scope = parser->qualifying_scope;
3167
3168         /* If the name is of the form "X::~X" it's OK.  */
3169         if (scope && TYPE_P (scope)
3170             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3171             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3172                 == CPP_OPEN_PAREN)
3173             && (cp_lexer_peek_token (parser->lexer)->value
3174                 == TYPE_IDENTIFIER (scope)))
3175           {
3176             cp_lexer_consume_token (parser->lexer);
3177             return build_nt (BIT_NOT_EXPR, scope);
3178           }
3179
3180         /* If there was an explicit qualification (S::~T), first look
3181            in the scope given by the qualification (i.e., S).  */
3182         if (scope)
3183           {
3184             cp_parser_parse_tentatively (parser);
3185             type_decl = cp_parser_class_name (parser,
3186                                               /*typename_keyword_p=*/false,
3187                                               /*template_keyword_p=*/false,
3188                                               /*type_p=*/false,
3189                                               /*check_dependency=*/false,
3190                                               /*class_head_p=*/false,
3191                                               declarator_p);
3192             if (cp_parser_parse_definitely (parser))
3193               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3194           }
3195         /* In "N::S::~S", look in "N" as well.  */
3196         if (scope && qualifying_scope)
3197           {
3198             cp_parser_parse_tentatively (parser);
3199             parser->scope = qualifying_scope;
3200             parser->object_scope = NULL_TREE;
3201             parser->qualifying_scope = NULL_TREE;
3202             type_decl
3203               = cp_parser_class_name (parser,
3204                                       /*typename_keyword_p=*/false,
3205                                       /*template_keyword_p=*/false,
3206                                       /*type_p=*/false,
3207                                       /*check_dependency=*/false,
3208                                       /*class_head_p=*/false,
3209                                       declarator_p);
3210             if (cp_parser_parse_definitely (parser))
3211               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3212           }
3213         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3214         else if (object_scope)
3215           {
3216             cp_parser_parse_tentatively (parser);
3217             parser->scope = object_scope;
3218             parser->object_scope = NULL_TREE;
3219             parser->qualifying_scope = NULL_TREE;
3220             type_decl
3221               = cp_parser_class_name (parser,
3222                                       /*typename_keyword_p=*/false,
3223                                       /*template_keyword_p=*/false,
3224                                       /*type_p=*/false,
3225                                       /*check_dependency=*/false,
3226                                       /*class_head_p=*/false,
3227                                       declarator_p);
3228             if (cp_parser_parse_definitely (parser))
3229               return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3230           }
3231         /* Look in the surrounding context.  */
3232         parser->scope = NULL_TREE;
3233         parser->object_scope = NULL_TREE;
3234         parser->qualifying_scope = NULL_TREE;
3235         type_decl
3236           = cp_parser_class_name (parser,
3237                                   /*typename_keyword_p=*/false,
3238                                   /*template_keyword_p=*/false,
3239                                   /*type_p=*/false,
3240                                   /*check_dependency=*/false,
3241                                   /*class_head_p=*/false,
3242                                   declarator_p);
3243         /* If an error occurred, assume that the name of the
3244            destructor is the same as the name of the qualifying
3245            class.  That allows us to keep parsing after running
3246            into ill-formed destructor names.  */
3247         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3248           return build_nt (BIT_NOT_EXPR, scope);
3249         else if (type_decl == error_mark_node)
3250           return error_mark_node;
3251
3252         /* [class.dtor]
3253
3254            A typedef-name that names a class shall not be used as the
3255            identifier in the declarator for a destructor declaration.  */
3256         if (declarator_p
3257             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3258             && !DECL_SELF_REFERENCE_P (type_decl))
3259           error ("typedef-name %qD used as destructor declarator",
3260                  type_decl);
3261
3262         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3263       }
3264
3265     case CPP_KEYWORD:
3266       if (token->keyword == RID_OPERATOR)
3267         {
3268           tree id;
3269
3270           /* This could be a template-id, so we try that first.  */
3271           cp_parser_parse_tentatively (parser);
3272           /* Try a template-id.  */
3273           id = cp_parser_template_id (parser, template_keyword_p,
3274                                       /*check_dependency_p=*/true,
3275                                       declarator_p);
3276           /* If that worked, we're done.  */
3277           if (cp_parser_parse_definitely (parser))
3278             return id;
3279           /* We still don't know whether we're looking at an
3280              operator-function-id or a conversion-function-id.  */
3281           cp_parser_parse_tentatively (parser);
3282           /* Try an operator-function-id.  */
3283           id = cp_parser_operator_function_id (parser);
3284           /* If that didn't work, try a conversion-function-id.  */
3285           if (!cp_parser_parse_definitely (parser))
3286             id = cp_parser_conversion_function_id (parser);
3287
3288           return id;
3289         }
3290       /* Fall through.  */
3291
3292     default:
3293       cp_parser_error (parser, "expected unqualified-id");
3294       return error_mark_node;
3295     }
3296 }
3297
3298 /* Parse an (optional) nested-name-specifier.
3299
3300    nested-name-specifier:
3301      class-or-namespace-name :: nested-name-specifier [opt]
3302      class-or-namespace-name :: template nested-name-specifier [opt]
3303
3304    PARSER->SCOPE should be set appropriately before this function is
3305    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3306    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3307    in name lookups.
3308
3309    Sets PARSER->SCOPE to the class (TYPE) or namespace
3310    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3311    it unchanged if there is no nested-name-specifier.  Returns the new
3312    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3313
3314    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3315    part of a declaration and/or decl-specifier.  */
3316
3317 static tree
3318 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3319                                      bool typename_keyword_p,
3320                                      bool check_dependency_p,
3321                                      bool type_p,
3322                                      bool is_declaration)
3323 {
3324   bool success = false;
3325   tree access_check = NULL_TREE;
3326   cp_token_position start = 0;
3327   cp_token *token;
3328
3329   /* If the next token corresponds to a nested name specifier, there
3330      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3331      false, it may have been true before, in which case something
3332      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3333      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3334      CHECK_DEPENDENCY_P is false, we have to fall through into the
3335      main loop.  */
3336   if (check_dependency_p
3337       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3338     {
3339       cp_parser_pre_parsed_nested_name_specifier (parser);
3340       return parser->scope;
3341     }
3342
3343   /* Remember where the nested-name-specifier starts.  */
3344   if (cp_parser_parsing_tentatively (parser)
3345       && !cp_parser_committed_to_tentative_parse (parser))
3346     start = cp_lexer_token_position (parser->lexer, false);
3347
3348   push_deferring_access_checks (dk_deferred);
3349
3350   while (true)
3351     {
3352       tree new_scope;
3353       tree old_scope;
3354       tree saved_qualifying_scope;
3355       bool template_keyword_p;
3356
3357       /* Spot cases that cannot be the beginning of a
3358          nested-name-specifier.  */
3359       token = cp_lexer_peek_token (parser->lexer);
3360
3361       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3362          the already parsed nested-name-specifier.  */
3363       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3364         {
3365           /* Grab the nested-name-specifier and continue the loop.  */
3366           cp_parser_pre_parsed_nested_name_specifier (parser);
3367           success = true;
3368           continue;
3369         }
3370
3371       /* Spot cases that cannot be the beginning of a
3372          nested-name-specifier.  On the second and subsequent times
3373          through the loop, we look for the `template' keyword.  */
3374       if (success && token->keyword == RID_TEMPLATE)
3375         ;
3376       /* A template-id can start a nested-name-specifier.  */
3377       else if (token->type == CPP_TEMPLATE_ID)
3378         ;
3379       else
3380         {
3381           /* If the next token is not an identifier, then it is
3382              definitely not a class-or-namespace-name.  */
3383           if (token->type != CPP_NAME)
3384             break;
3385           /* If the following token is neither a `<' (to begin a
3386              template-id), nor a `::', then we are not looking at a
3387              nested-name-specifier.  */
3388           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3389           if (token->type != CPP_SCOPE
3390               && !cp_parser_nth_token_starts_template_argument_list_p
3391                   (parser, 2))
3392             break;
3393         }
3394
3395       /* The nested-name-specifier is optional, so we parse
3396          tentatively.  */
3397       cp_parser_parse_tentatively (parser);
3398
3399       /* Look for the optional `template' keyword, if this isn't the
3400          first time through the loop.  */
3401       if (success)
3402         template_keyword_p = cp_parser_optional_template_keyword (parser);
3403       else
3404         template_keyword_p = false;
3405
3406       /* Save the old scope since the name lookup we are about to do
3407          might destroy it.  */
3408       old_scope = parser->scope;
3409       saved_qualifying_scope = parser->qualifying_scope;
3410       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3411          look up names in "X<T>::I" in order to determine that "Y" is
3412          a template.  So, if we have a typename at this point, we make
3413          an effort to look through it.  */
3414       if (is_declaration 
3415           && !typename_keyword_p
3416           && parser->scope 
3417           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3418         parser->scope = resolve_typename_type (parser->scope, 
3419                                                /*only_current_p=*/false);
3420       /* Parse the qualifying entity.  */
3421       new_scope
3422         = cp_parser_class_or_namespace_name (parser,
3423                                              typename_keyword_p,
3424                                              template_keyword_p,
3425                                              check_dependency_p,
3426                                              type_p,
3427                                              is_declaration);
3428       /* Look for the `::' token.  */
3429       cp_parser_require (parser, CPP_SCOPE, "`::'");
3430
3431       /* If we found what we wanted, we keep going; otherwise, we're
3432          done.  */
3433       if (!cp_parser_parse_definitely (parser))
3434         {
3435           bool error_p = false;
3436
3437           /* Restore the OLD_SCOPE since it was valid before the
3438              failed attempt at finding the last
3439              class-or-namespace-name.  */
3440           parser->scope = old_scope;
3441           parser->qualifying_scope = saved_qualifying_scope;
3442           /* If the next token is an identifier, and the one after
3443              that is a `::', then any valid interpretation would have
3444              found a class-or-namespace-name.  */
3445           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3446                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3447                      == CPP_SCOPE)
3448                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
3449                      != CPP_COMPL))
3450             {
3451               token = cp_lexer_consume_token (parser->lexer);
3452               if (!error_p)
3453                 {
3454                   tree decl;
3455
3456                   decl = cp_parser_lookup_name_simple (parser, token->value);
3457                   if (TREE_CODE (decl) == TEMPLATE_DECL)
3458                     error ("%qD used without template parameters", decl);
3459                   else
3460                     cp_parser_name_lookup_error
3461                       (parser, token->value, decl,
3462                        "is not a class or namespace");
3463                   parser->scope = NULL_TREE;
3464                   error_p = true;
3465                   /* Treat this as a successful nested-name-specifier
3466                      due to:
3467
3468                      [basic.lookup.qual]
3469
3470                      If the name found is not a class-name (clause
3471                      _class_) or namespace-name (_namespace.def_), the
3472                      program is ill-formed.  */
3473                   success = true;
3474                 }
3475               cp_lexer_consume_token (parser->lexer);
3476             }
3477           break;
3478         }
3479
3480       /* We've found one valid nested-name-specifier.  */
3481       success = true;
3482       /* Make sure we look in the right scope the next time through
3483          the loop.  */
3484       parser->scope = (TREE_CODE (new_scope) == TYPE_DECL
3485                        ? TREE_TYPE (new_scope)
3486                        : new_scope);
3487       /* If it is a class scope, try to complete it; we are about to
3488          be looking up names inside the class.  */
3489       if (TYPE_P (parser->scope)
3490           /* Since checking types for dependency can be expensive,
3491              avoid doing it if the type is already complete.  */
3492           && !COMPLETE_TYPE_P (parser->scope)
3493           /* Do not try to complete dependent types.  */
3494           && !dependent_type_p (parser->scope))
3495         complete_type (parser->scope);
3496     }
3497
3498   /* Retrieve any deferred checks.  Do not pop this access checks yet
3499      so the memory will not be reclaimed during token replacing below.  */
3500   access_check = get_deferred_access_checks ();
3501
3502   /* If parsing tentatively, replace the sequence of tokens that makes
3503      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
3504      token.  That way, should we re-parse the token stream, we will
3505      not have to repeat the effort required to do the parse, nor will
3506      we issue duplicate error messages.  */
3507   if (success && start)
3508     {
3509       cp_token *token = cp_lexer_token_at (parser->lexer, start);
3510       
3511       /* Reset the contents of the START token.  */
3512       token->type = CPP_NESTED_NAME_SPECIFIER;
3513       token->value = build_tree_list (access_check, parser->scope);
3514       TREE_TYPE (token->value) = parser->qualifying_scope;
3515       token->keyword = RID_MAX;
3516       
3517       /* Purge all subsequent tokens.  */
3518       cp_lexer_purge_tokens_after (parser->lexer, start);
3519     }
3520
3521   pop_deferring_access_checks ();
3522   return success ? parser->scope : NULL_TREE;
3523 }
3524
3525 /* Parse a nested-name-specifier.  See
3526    cp_parser_nested_name_specifier_opt for details.  This function
3527    behaves identically, except that it will an issue an error if no
3528    nested-name-specifier is present, and it will return
3529    ERROR_MARK_NODE, rather than NULL_TREE, if no nested-name-specifier
3530    is present.  */
3531
3532 static tree
3533 cp_parser_nested_name_specifier (cp_parser *parser,
3534                                  bool typename_keyword_p,
3535                                  bool check_dependency_p,
3536                                  bool type_p,
3537                                  bool is_declaration)
3538 {
3539   tree scope;
3540
3541   /* Look for the nested-name-specifier.  */
3542   scope = cp_parser_nested_name_specifier_opt (parser,
3543                                                typename_keyword_p,
3544                                                check_dependency_p,
3545                                                type_p,
3546                                                is_declaration);
3547   /* If it was not present, issue an error message.  */
3548   if (!scope)
3549     {
3550       cp_parser_error (parser, "expected nested-name-specifier");
3551       parser->scope = NULL_TREE;
3552       return error_mark_node;
3553     }
3554
3555   return scope;
3556 }
3557
3558 /* Parse a class-or-namespace-name.
3559
3560    class-or-namespace-name:
3561      class-name
3562      namespace-name
3563
3564    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
3565    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
3566    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
3567    TYPE_P is TRUE iff the next name should be taken as a class-name,
3568    even the same name is declared to be another entity in the same
3569    scope.
3570
3571    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
3572    specified by the class-or-namespace-name.  If neither is found the
3573    ERROR_MARK_NODE is returned.  */
3574
3575 static tree
3576 cp_parser_class_or_namespace_name (cp_parser *parser,
3577                                    bool typename_keyword_p,
3578                                    bool template_keyword_p,
3579                                    bool check_dependency_p,
3580                                    bool type_p,
3581                                    bool is_declaration)
3582 {
3583   tree saved_scope;
3584   tree saved_qualifying_scope;
3585   tree saved_object_scope;
3586   tree scope;
3587   bool only_class_p;
3588
3589   /* Before we try to parse the class-name, we must save away the
3590      current PARSER->SCOPE since cp_parser_class_name will destroy
3591      it.  */
3592   saved_scope = parser->scope;
3593   saved_qualifying_scope = parser->qualifying_scope;
3594   saved_object_scope = parser->object_scope;
3595   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
3596      there is no need to look for a namespace-name.  */
3597   only_class_p = template_keyword_p || (saved_scope && TYPE_P (saved_scope));
3598   if (!only_class_p)
3599     cp_parser_parse_tentatively (parser);
3600   scope = cp_parser_class_name (parser,
3601                                 typename_keyword_p,
3602                                 template_keyword_p,
3603                                 type_p,
3604                                 check_dependency_p,
3605                                 /*class_head_p=*/false,
3606                                 is_declaration);
3607   /* If that didn't work, try for a namespace-name.  */
3608   if (!only_class_p && !cp_parser_parse_definitely (parser))
3609     {
3610       /* Restore the saved scope.  */
3611       parser->scope = saved_scope;
3612       parser->qualifying_scope = saved_qualifying_scope;
3613       parser->object_scope = saved_object_scope;
3614       /* If we are not looking at an identifier followed by the scope
3615          resolution operator, then this is not part of a
3616          nested-name-specifier.  (Note that this function is only used
3617          to parse the components of a nested-name-specifier.)  */
3618       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
3619           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
3620         return error_mark_node;
3621       scope = cp_parser_namespace_name (parser);
3622     }
3623
3624   return scope;
3625 }
3626
3627 /* Parse a postfix-expression.
3628
3629    postfix-expression:
3630      primary-expression
3631      postfix-expression [ expression ]
3632      postfix-expression ( expression-list [opt] )
3633      simple-type-specifier ( expression-list [opt] )
3634      typename :: [opt] nested-name-specifier identifier
3635        ( expression-list [opt] )
3636      typename :: [opt] nested-name-specifier template [opt] template-id
3637        ( expression-list [opt] )
3638      postfix-expression . template [opt] id-expression
3639      postfix-expression -> template [opt] id-expression
3640      postfix-expression . pseudo-destructor-name
3641      postfix-expression -> pseudo-destructor-name
3642      postfix-expression ++
3643      postfix-expression --
3644      dynamic_cast < type-id > ( expression )
3645      static_cast < type-id > ( expression )
3646      reinterpret_cast < type-id > ( expression )
3647      const_cast < type-id > ( expression )
3648      typeid ( expression )
3649      typeid ( type-id )
3650
3651    GNU Extension:
3652
3653    postfix-expression:
3654      ( type-id ) { initializer-list , [opt] }
3655
3656    This extension is a GNU version of the C99 compound-literal
3657    construct.  (The C99 grammar uses `type-name' instead of `type-id',
3658    but they are essentially the same concept.)
3659
3660    If ADDRESS_P is true, the postfix expression is the operand of the
3661    `&' operator.
3662
3663    Returns a representation of the expression.  */
3664
3665 static tree
3666 cp_parser_postfix_expression (cp_parser *parser, bool address_p)
3667 {
3668   cp_token *token;
3669   enum rid keyword;
3670   cp_id_kind idk = CP_ID_KIND_NONE;
3671   tree postfix_expression = NULL_TREE;
3672   /* Non-NULL only if the current postfix-expression can be used to
3673      form a pointer-to-member.  In that case, QUALIFYING_CLASS is the
3674      class used to qualify the member.  */
3675   tree qualifying_class = NULL_TREE;
3676
3677   /* Peek at the next token.  */
3678   token = cp_lexer_peek_token (parser->lexer);
3679   /* Some of the productions are determined by keywords.  */
3680   keyword = token->keyword;
3681   switch (keyword)
3682     {
3683     case RID_DYNCAST:
3684     case RID_STATCAST:
3685     case RID_REINTCAST:
3686     case RID_CONSTCAST:
3687       {
3688         tree type;
3689         tree expression;
3690         const char *saved_message;
3691
3692         /* All of these can be handled in the same way from the point
3693            of view of parsing.  Begin by consuming the token
3694            identifying the cast.  */
3695         cp_lexer_consume_token (parser->lexer);
3696
3697         /* New types cannot be defined in the cast.  */
3698         saved_message = parser->type_definition_forbidden_message;
3699         parser->type_definition_forbidden_message
3700           = "types may not be defined in casts";
3701
3702         /* Look for the opening `<'.  */
3703         cp_parser_require (parser, CPP_LESS, "`<'");
3704         /* Parse the type to which we are casting.  */
3705         type = cp_parser_type_id (parser);
3706         /* Look for the closing `>'.  */
3707         cp_parser_require (parser, CPP_GREATER, "`>'");
3708         /* Restore the old message.  */
3709         parser->type_definition_forbidden_message = saved_message;
3710
3711         /* And the expression which is being cast.  */
3712         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3713         expression = cp_parser_expression (parser);
3714         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3715
3716         /* Only type conversions to integral or enumeration types
3717            can be used in constant-expressions.  */
3718         if (parser->integral_constant_expression_p
3719             && !dependent_type_p (type)
3720             && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
3721             && (cp_parser_non_integral_constant_expression
3722                 (parser,
3723                  "a cast to a type other than an integral or "
3724                  "enumeration type")))
3725           return error_mark_node;
3726
3727         switch (keyword)
3728           {
3729           case RID_DYNCAST:
3730             postfix_expression
3731               = build_dynamic_cast (type, expression);
3732             break;
3733           case RID_STATCAST:
3734             postfix_expression
3735               = build_static_cast (type, expression);
3736             break;
3737           case RID_REINTCAST:
3738             postfix_expression
3739               = build_reinterpret_cast (type, expression);
3740             break;
3741           case RID_CONSTCAST:
3742             postfix_expression
3743               = build_const_cast (type, expression);
3744             break;
3745           default:
3746             gcc_unreachable ();
3747           }
3748       }
3749       break;
3750
3751     case RID_TYPEID:
3752       {
3753         tree type;
3754         const char *saved_message;
3755         bool saved_in_type_id_in_expr_p;
3756
3757         /* Consume the `typeid' token.  */
3758         cp_lexer_consume_token (parser->lexer);
3759         /* Look for the `(' token.  */
3760         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
3761         /* Types cannot be defined in a `typeid' expression.  */
3762         saved_message = parser->type_definition_forbidden_message;
3763         parser->type_definition_forbidden_message
3764           = "types may not be defined in a `typeid\' expression";
3765         /* We can't be sure yet whether we're looking at a type-id or an
3766            expression.  */
3767         cp_parser_parse_tentatively (parser);
3768         /* Try a type-id first.  */
3769         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3770         parser->in_type_id_in_expr_p = true;
3771         type = cp_parser_type_id (parser);
3772         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3773         /* Look for the `)' token.  Otherwise, we can't be sure that
3774            we're not looking at an expression: consider `typeid (int
3775            (3))', for example.  */
3776         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3777         /* If all went well, simply lookup the type-id.  */
3778         if (cp_parser_parse_definitely (parser))
3779           postfix_expression = get_typeid (type);
3780         /* Otherwise, fall back to the expression variant.  */
3781         else
3782           {
3783             tree expression;
3784
3785             /* Look for an expression.  */
3786             expression = cp_parser_expression (parser);
3787             /* Compute its typeid.  */
3788             postfix_expression = build_typeid (expression);
3789             /* Look for the `)' token.  */
3790             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3791           }
3792         /* `typeid' may not appear in an integral constant expression.  */
3793         if (cp_parser_non_integral_constant_expression(parser,
3794                                                        "`typeid' operator"))
3795           return error_mark_node;
3796         /* Restore the saved message.  */
3797         parser->type_definition_forbidden_message = saved_message;
3798       }
3799       break;
3800
3801     case RID_TYPENAME:
3802       {
3803         bool template_p = false;
3804         tree id;
3805         tree type;
3806
3807         /* Consume the `typename' token.  */
3808         cp_lexer_consume_token (parser->lexer);
3809         /* Look for the optional `::' operator.  */
3810         cp_parser_global_scope_opt (parser,
3811                                     /*current_scope_valid_p=*/false);
3812         /* Look for the nested-name-specifier.  */
3813         cp_parser_nested_name_specifier (parser,
3814                                          /*typename_keyword_p=*/true,
3815                                          /*check_dependency_p=*/true,
3816                                          /*type_p=*/true,
3817                                          /*is_declaration=*/true);
3818         /* Look for the optional `template' keyword.  */
3819         template_p = cp_parser_optional_template_keyword (parser);
3820         /* We don't know whether we're looking at a template-id or an
3821            identifier.  */
3822         cp_parser_parse_tentatively (parser);
3823         /* Try a template-id.  */
3824         id = cp_parser_template_id (parser, template_p,
3825                                     /*check_dependency_p=*/true,
3826                                     /*is_declaration=*/true);
3827         /* If that didn't work, try an identifier.  */
3828         if (!cp_parser_parse_definitely (parser))
3829           id = cp_parser_identifier (parser);
3830         /* If we look up a template-id in a non-dependent qualifying
3831            scope, there's no need to create a dependent type.  */
3832         if (TREE_CODE (id) == TYPE_DECL
3833             && !dependent_type_p (parser->scope))
3834           type = TREE_TYPE (id);
3835         /* Create a TYPENAME_TYPE to represent the type to which the
3836            functional cast is being performed.  */
3837         else
3838           type = make_typename_type (parser->scope, id,
3839                                      /*complain=*/1);
3840
3841         postfix_expression = cp_parser_functional_cast (parser, type);
3842       }
3843       break;
3844
3845     default:
3846       {
3847         tree type;
3848
3849         /* If the next thing is a simple-type-specifier, we may be
3850            looking at a functional cast.  We could also be looking at
3851            an id-expression.  So, we try the functional cast, and if
3852            that doesn't work we fall back to the primary-expression.  */
3853         cp_parser_parse_tentatively (parser);
3854         /* Look for the simple-type-specifier.  */
3855         type = cp_parser_simple_type_specifier (parser,
3856                                                 /*decl_specs=*/NULL,
3857                                                 CP_PARSER_FLAGS_NONE);
3858         /* Parse the cast itself.  */
3859         if (!cp_parser_error_occurred (parser))
3860           postfix_expression
3861             = cp_parser_functional_cast (parser, type);
3862         /* If that worked, we're done.  */
3863         if (cp_parser_parse_definitely (parser))
3864           break;
3865
3866         /* If the functional-cast didn't work out, try a
3867            compound-literal.  */
3868         if (cp_parser_allow_gnu_extensions_p (parser)
3869             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
3870           {
3871             tree initializer_list = NULL_TREE;
3872             bool saved_in_type_id_in_expr_p;
3873
3874             cp_parser_parse_tentatively (parser);
3875             /* Consume the `('.  */
3876             cp_lexer_consume_token (parser->lexer);
3877             /* Parse the type.  */
3878             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
3879             parser->in_type_id_in_expr_p = true;
3880             type = cp_parser_type_id (parser);
3881             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
3882             /* Look for the `)'.  */
3883             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
3884             /* Look for the `{'.  */
3885             cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
3886             /* If things aren't going well, there's no need to
3887                keep going.  */
3888             if (!cp_parser_error_occurred (parser))
3889               {
3890                 bool non_constant_p;
3891                 /* Parse the initializer-list.  */
3892                 initializer_list
3893                   = cp_parser_initializer_list (parser, &non_constant_p);
3894                 /* Allow a trailing `,'.  */
3895                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
3896                   cp_lexer_consume_token (parser->lexer);
3897                 /* Look for the final `}'.  */
3898                 cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
3899               }
3900             /* If that worked, we're definitely looking at a
3901                compound-literal expression.  */
3902             if (cp_parser_parse_definitely (parser))
3903               {
3904                 /* Warn the user that a compound literal is not
3905                    allowed in standard C++.  */
3906                 if (pedantic)
3907                   pedwarn ("ISO C++ forbids compound-literals");
3908                 /* Form the representation of the compound-literal.  */
3909                 postfix_expression
3910                   = finish_compound_literal (type, initializer_list);
3911                 break;
3912               }
3913           }
3914
3915         /* It must be a primary-expression.  */
3916         postfix_expression = cp_parser_primary_expression (parser,
3917                                                            &idk,
3918                                                            &qualifying_class);
3919       }
3920       break;
3921     }
3922
3923   /* If we were avoiding committing to the processing of a
3924      qualified-id until we knew whether or not we had a
3925      pointer-to-member, we now know.  */
3926   if (qualifying_class)
3927     {
3928       bool done;
3929
3930       /* Peek at the next token.  */
3931       token = cp_lexer_peek_token (parser->lexer);
3932       done = (token->type != CPP_OPEN_SQUARE
3933               && token->type != CPP_OPEN_PAREN
3934               && token->type != CPP_DOT
3935               && token->type != CPP_DEREF
3936               && token->type != CPP_PLUS_PLUS
3937               && token->type != CPP_MINUS_MINUS);
3938
3939       postfix_expression = finish_qualified_id_expr (qualifying_class,
3940                                                      postfix_expression,
3941                                                      done,
3942                                                      address_p);
3943       if (done)
3944         return postfix_expression;
3945     }
3946
3947   /* Keep looping until the postfix-expression is complete.  */
3948   while (true)
3949     {
3950       if (idk == CP_ID_KIND_UNQUALIFIED
3951           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
3952           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
3953         /* It is not a Koenig lookup function call.  */
3954         postfix_expression
3955           = unqualified_name_lookup_error (postfix_expression);
3956
3957       /* Peek at the next token.  */
3958       token = cp_lexer_peek_token (parser->lexer);
3959
3960       switch (token->type)
3961         {
3962         case CPP_OPEN_SQUARE:
3963           postfix_expression
3964             = cp_parser_postfix_open_square_expression (parser,
3965                                                         postfix_expression,
3966                                                         false);
3967           idk = CP_ID_KIND_NONE;
3968           break;
3969
3970         case CPP_OPEN_PAREN:
3971           /* postfix-expression ( expression-list [opt] ) */
3972           {
3973             bool koenig_p;
3974             tree args = (cp_parser_parenthesized_expression_list
3975                          (parser, false, /*non_constant_p=*/NULL));
3976
3977             if (args == error_mark_node)
3978               {
3979                 postfix_expression = error_mark_node;
3980                 break;
3981               }
3982
3983             /* Function calls are not permitted in
3984                constant-expressions.  */
3985             if (cp_parser_non_integral_constant_expression (parser,
3986                                                             "a function call"))
3987               {
3988                 postfix_expression = error_mark_node;
3989                 break;
3990               }
3991
3992             koenig_p = false;
3993             if (idk == CP_ID_KIND_UNQUALIFIED)
3994               {
3995                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
3996                   {
3997                     if (args)
3998                       {
3999                         koenig_p = true;
4000                         postfix_expression
4001                           = perform_koenig_lookup (postfix_expression, args);
4002                       }
4003                     else
4004                       postfix_expression
4005                         = unqualified_fn_lookup_error (postfix_expression);
4006                   }
4007                 /* We do not perform argument-dependent lookup if
4008                    normal lookup finds a non-function, in accordance
4009                    with the expected resolution of DR 218.  */
4010                 else if (args && is_overloaded_fn (postfix_expression))
4011                   {
4012                     tree fn = get_first_fn (postfix_expression);
4013
4014                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4015                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4016
4017                     /* Only do argument dependent lookup if regular
4018                        lookup does not find a set of member functions.
4019                        [basic.lookup.koenig]/2a  */
4020                     if (!DECL_FUNCTION_MEMBER_P (fn))
4021                       {
4022                         koenig_p = true;
4023                         postfix_expression
4024                           = perform_koenig_lookup (postfix_expression, args);
4025                       }
4026                   }
4027               }
4028
4029             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4030               {
4031                 tree instance = TREE_OPERAND (postfix_expression, 0);
4032                 tree fn = TREE_OPERAND (postfix_expression, 1);
4033
4034                 if (processing_template_decl
4035                     && (type_dependent_expression_p (instance)
4036                         || (!BASELINK_P (fn)
4037                             && TREE_CODE (fn) != FIELD_DECL)
4038                         || type_dependent_expression_p (fn)
4039                         || any_type_dependent_arguments_p (args)))
4040                   {
4041                     postfix_expression
4042                       = build_min_nt (CALL_EXPR, postfix_expression,
4043                                       args, NULL_TREE);
4044                     break;
4045                   }
4046
4047                 if (BASELINK_P (fn))
4048                   postfix_expression
4049                     = (build_new_method_call
4050                        (instance, fn, args, NULL_TREE,
4051                         (idk == CP_ID_KIND_QUALIFIED
4052                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL)));
4053                 else
4054                   postfix_expression
4055                     = finish_call_expr (postfix_expression, args,
4056                                         /*disallow_virtual=*/false,
4057                                         /*koenig_p=*/false);
4058               }
4059             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4060                      || TREE_CODE (postfix_expression) == MEMBER_REF
4061                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4062               postfix_expression = (build_offset_ref_call_from_tree
4063                                     (postfix_expression, args));
4064             else if (idk == CP_ID_KIND_QUALIFIED)
4065               /* A call to a static class member, or a namespace-scope
4066                  function.  */
4067               postfix_expression
4068                 = finish_call_expr (postfix_expression, args,
4069                                     /*disallow_virtual=*/true,
4070                                     koenig_p);
4071             else
4072               /* All other function calls.  */
4073               postfix_expression
4074                 = finish_call_expr (postfix_expression, args,
4075                                     /*disallow_virtual=*/false,
4076                                     koenig_p);
4077
4078             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4079             idk = CP_ID_KIND_NONE;
4080           }
4081           break;
4082
4083         case CPP_DOT:
4084         case CPP_DEREF:
4085           /* postfix-expression . template [opt] id-expression
4086              postfix-expression . pseudo-destructor-name
4087              postfix-expression -> template [opt] id-expression
4088              postfix-expression -> pseudo-destructor-name */
4089
4090           /* Consume the `.' or `->' operator.  */
4091           cp_lexer_consume_token (parser->lexer);
4092
4093           postfix_expression
4094             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4095                                                       postfix_expression,
4096                                                       false, &idk);
4097           break;
4098
4099         case CPP_PLUS_PLUS:
4100           /* postfix-expression ++  */
4101           /* Consume the `++' token.  */
4102           cp_lexer_consume_token (parser->lexer);
4103           /* Generate a representation for the complete expression.  */
4104           postfix_expression
4105             = finish_increment_expr (postfix_expression,
4106                                      POSTINCREMENT_EXPR);
4107           /* Increments may not appear in constant-expressions.  */
4108           if (cp_parser_non_integral_constant_expression (parser,
4109                                                           "an increment"))
4110             postfix_expression = error_mark_node;
4111           idk = CP_ID_KIND_NONE;
4112           break;
4113
4114         case CPP_MINUS_MINUS:
4115           /* postfix-expression -- */
4116           /* Consume the `--' token.  */
4117           cp_lexer_consume_token (parser->lexer);
4118           /* Generate a representation for the complete expression.  */
4119           postfix_expression
4120             = finish_increment_expr (postfix_expression,
4121                                      POSTDECREMENT_EXPR);
4122           /* Decrements may not appear in constant-expressions.  */
4123           if (cp_parser_non_integral_constant_expression (parser,
4124                                                           "a decrement"))
4125             postfix_expression = error_mark_node;
4126           idk = CP_ID_KIND_NONE;
4127           break;
4128
4129         default:
4130           return postfix_expression;
4131         }
4132     }
4133
4134   /* We should never get here.  */
4135   gcc_unreachable ();
4136   return error_mark_node;
4137 }
4138
4139 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4140    by cp_parser_builtin_offsetof.  We're looking for
4141
4142      postfix-expression [ expression ]
4143
4144    FOR_OFFSETOF is set if we're being called in that context, which
4145    changes how we deal with integer constant expressions.  */
4146
4147 static tree
4148 cp_parser_postfix_open_square_expression (cp_parser *parser,
4149                                           tree postfix_expression,
4150                                           bool for_offsetof)
4151 {
4152   tree index;
4153
4154   /* Consume the `[' token.  */
4155   cp_lexer_consume_token (parser->lexer);
4156
4157   /* Parse the index expression.  */
4158   /* ??? For offsetof, there is a question of what to allow here.  If
4159      offsetof is not being used in an integral constant expression context,
4160      then we *could* get the right answer by computing the value at runtime.
4161      If we are in an integral constant expression context, then we might
4162      could accept any constant expression; hard to say without analysis.
4163      Rather than open the barn door too wide right away, allow only integer
4164      constant expressions here.  */
4165   if (for_offsetof)
4166     index = cp_parser_constant_expression (parser, false, NULL);
4167   else
4168     index = cp_parser_expression (parser);
4169
4170   /* Look for the closing `]'.  */
4171   cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
4172
4173   /* Build the ARRAY_REF.  */
4174   postfix_expression = grok_array_decl (postfix_expression, index);
4175
4176   /* When not doing offsetof, array references are not permitted in
4177      constant-expressions.  */
4178   if (!for_offsetof
4179       && (cp_parser_non_integral_constant_expression
4180           (parser, "an array reference")))
4181     postfix_expression = error_mark_node;
4182
4183   return postfix_expression;
4184 }
4185
4186 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4187    by cp_parser_builtin_offsetof.  We're looking for
4188
4189      postfix-expression . template [opt] id-expression
4190      postfix-expression . pseudo-destructor-name
4191      postfix-expression -> template [opt] id-expression
4192      postfix-expression -> pseudo-destructor-name
4193
4194    FOR_OFFSETOF is set if we're being called in that context.  That sorta
4195    limits what of the above we'll actually accept, but nevermind.
4196    TOKEN_TYPE is the "." or "->" token, which will already have been
4197    removed from the stream.  */
4198
4199 static tree
4200 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
4201                                         enum cpp_ttype token_type,
4202                                         tree postfix_expression,
4203                                         bool for_offsetof, cp_id_kind *idk)
4204 {
4205   tree name;
4206   bool dependent_p;
4207   bool template_p;
4208   bool pseudo_destructor_p;
4209   tree scope = NULL_TREE;
4210
4211   /* If this is a `->' operator, dereference the pointer.  */
4212   if (token_type == CPP_DEREF)
4213     postfix_expression = build_x_arrow (postfix_expression);
4214   /* Check to see whether or not the expression is type-dependent.  */
4215   dependent_p = type_dependent_expression_p (postfix_expression);
4216   /* The identifier following the `->' or `.' is not qualified.  */
4217   parser->scope = NULL_TREE;
4218   parser->qualifying_scope = NULL_TREE;
4219   parser->object_scope = NULL_TREE;
4220   *idk = CP_ID_KIND_NONE;
4221   /* Enter the scope corresponding to the type of the object
4222      given by the POSTFIX_EXPRESSION.  */
4223   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
4224     {
4225       scope = TREE_TYPE (postfix_expression);
4226       /* According to the standard, no expression should ever have
4227          reference type.  Unfortunately, we do not currently match
4228          the standard in this respect in that our internal representation
4229          of an expression may have reference type even when the standard
4230          says it does not.  Therefore, we have to manually obtain the
4231          underlying type here.  */
4232       scope = non_reference (scope);
4233       /* The type of the POSTFIX_EXPRESSION must be complete.  */
4234       scope = complete_type_or_else (scope, NULL_TREE);
4235       /* Let the name lookup machinery know that we are processing a
4236          class member access expression.  */
4237       parser->context->object_type = scope;
4238       /* If something went wrong, we want to be able to discern that case,
4239          as opposed to the case where there was no SCOPE due to the type
4240          of expression being dependent.  */
4241       if (!scope)
4242         scope = error_mark_node;
4243       /* If the SCOPE was erroneous, make the various semantic analysis
4244          functions exit quickly -- and without issuing additional error
4245          messages.  */
4246       if (scope == error_mark_node)
4247         postfix_expression = error_mark_node;
4248     }
4249
4250   /* Assume this expression is not a pseudo-destructor access.  */
4251   pseudo_destructor_p = false;
4252
4253   /* If the SCOPE is a scalar type, then, if this is a valid program,
4254      we must be looking at a pseudo-destructor-name.  */
4255   if (scope && SCALAR_TYPE_P (scope))
4256     {
4257       tree s;
4258       tree type;
4259
4260       cp_parser_parse_tentatively (parser);
4261       /* Parse the pseudo-destructor-name.  */
4262       s = NULL_TREE;
4263       cp_parser_pseudo_destructor_name (parser, &s, &type);
4264       if (cp_parser_parse_definitely (parser))
4265         {
4266           pseudo_destructor_p = true;
4267           postfix_expression
4268             = finish_pseudo_destructor_expr (postfix_expression,
4269                                              s, TREE_TYPE (type));
4270         }
4271     }
4272
4273   if (!pseudo_destructor_p)
4274     {
4275       /* If the SCOPE is not a scalar type, we are looking at an
4276          ordinary class member access expression, rather than a
4277          pseudo-destructor-name.  */
4278       template_p = cp_parser_optional_template_keyword (parser);
4279       /* Parse the id-expression.  */
4280       name = cp_parser_id_expression (parser, template_p,
4281                                       /*check_dependency_p=*/true,
4282                                       /*template_p=*/NULL,
4283                                       /*declarator_p=*/false);
4284       /* In general, build a SCOPE_REF if the member name is qualified.
4285          However, if the name was not dependent and has already been
4286          resolved; there is no need to build the SCOPE_REF.  For example;
4287
4288              struct X { void f(); };
4289              template <typename T> void f(T* t) { t->X::f(); }
4290
4291          Even though "t" is dependent, "X::f" is not and has been resolved
4292          to a BASELINK; there is no need to include scope information.  */
4293
4294       /* But we do need to remember that there was an explicit scope for
4295          virtual function calls.  */
4296       if (parser->scope)
4297         *idk = CP_ID_KIND_QUALIFIED;
4298
4299       if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
4300         {
4301           name = build_nt (SCOPE_REF, parser->scope, name);
4302           parser->scope = NULL_TREE;
4303           parser->qualifying_scope = NULL_TREE;
4304           parser->object_scope = NULL_TREE;
4305         }
4306       if (scope && name && BASELINK_P (name))
4307         adjust_result_of_qualified_name_lookup
4308           (name, BINFO_TYPE (BASELINK_BINFO (name)), scope);
4309       postfix_expression
4310         = finish_class_member_access_expr (postfix_expression, name);
4311     }
4312
4313   /* We no longer need to look up names in the scope of the object on
4314      the left-hand side of the `.' or `->' operator.  */
4315   parser->context->object_type = NULL_TREE;
4316
4317   /* Outside of offsetof, these operators may not appear in
4318      constant-expressions.  */
4319   if (!for_offsetof
4320       && (cp_parser_non_integral_constant_expression
4321           (parser, token_type == CPP_DEREF ? "'->'" : "`.'")))
4322     postfix_expression = error_mark_node;
4323
4324   return postfix_expression;
4325 }
4326
4327 /* Parse a parenthesized expression-list.
4328
4329    expression-list:
4330      assignment-expression
4331      expression-list, assignment-expression
4332
4333    attribute-list:
4334      expression-list
4335      identifier
4336      identifier, expression-list
4337
4338    Returns a TREE_LIST.  The TREE_VALUE of each node is a
4339    representation of an assignment-expression.  Note that a TREE_LIST
4340    is returned even if there is only a single expression in the list.
4341    error_mark_node is returned if the ( and or ) are
4342    missing. NULL_TREE is returned on no expressions. The parentheses
4343    are eaten. IS_ATTRIBUTE_LIST is true if this is really an attribute
4344    list being parsed.  If NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P
4345    indicates whether or not all of the expressions in the list were
4346    constant.  */
4347
4348 static tree
4349 cp_parser_parenthesized_expression_list (cp_parser* parser,
4350                                          bool is_attribute_list,
4351                                          bool *non_constant_p)
4352 {
4353   tree expression_list = NULL_TREE;
4354   bool fold_expr_p = is_attribute_list;
4355   tree identifier = NULL_TREE;
4356
4357   /* Assume all the expressions will be constant.  */
4358   if (non_constant_p)
4359     *non_constant_p = false;
4360
4361   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
4362     return error_mark_node;
4363
4364   /* Consume expressions until there are no more.  */
4365   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
4366     while (true)
4367       {
4368         tree expr;
4369
4370         /* At the beginning of attribute lists, check to see if the
4371            next token is an identifier.  */
4372         if (is_attribute_list
4373             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
4374           {
4375             cp_token *token;
4376
4377             /* Consume the identifier.  */
4378             token = cp_lexer_consume_token (parser->lexer);
4379             /* Save the identifier.  */
4380             identifier = token->value;
4381           }
4382         else
4383           {
4384             /* Parse the next assignment-expression.  */
4385             if (non_constant_p)
4386               {
4387                 bool expr_non_constant_p;
4388                 expr = (cp_parser_constant_expression
4389                         (parser, /*allow_non_constant_p=*/true,
4390                          &expr_non_constant_p));
4391                 if (expr_non_constant_p)
4392                   *non_constant_p = true;
4393               }
4394             else
4395               expr = cp_parser_assignment_expression (parser);
4396
4397             if (fold_expr_p)
4398               expr = fold_non_dependent_expr (expr);
4399
4400              /* Add it to the list.  We add error_mark_node
4401                 expressions to the list, so that we can still tell if
4402                 the correct form for a parenthesized expression-list
4403                 is found. That gives better errors.  */
4404             expression_list = tree_cons (NULL_TREE, expr, expression_list);
4405
4406             if (expr == error_mark_node)
4407               goto skip_comma;
4408           }
4409
4410         /* After the first item, attribute lists look the same as
4411            expression lists.  */
4412         is_attribute_list = false;
4413
4414       get_comma:;
4415         /* If the next token isn't a `,', then we are done.  */
4416         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
4417           break;
4418
4419         /* Otherwise, consume the `,' and keep going.  */
4420         cp_lexer_consume_token (parser->lexer);
4421       }
4422
4423   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
4424     {
4425       int ending;
4426
4427     skip_comma:;
4428       /* We try and resync to an unnested comma, as that will give the
4429          user better diagnostics.  */
4430       ending = cp_parser_skip_to_closing_parenthesis (parser,
4431                                                       /*recovering=*/true,
4432                                                       /*or_comma=*/true,
4433                                                       /*consume_paren=*/true);
4434       if (ending < 0)
4435         goto get_comma;
4436       if (!ending)
4437         return error_mark_node;
4438     }
4439
4440   /* We built up the list in reverse order so we must reverse it now.  */
4441   expression_list = nreverse (expression_list);
4442   if (identifier)
4443     expression_list = tree_cons (NULL_TREE, identifier, expression_list);
4444
4445   return expression_list;
4446 }
4447
4448 /* Parse a pseudo-destructor-name.
4449
4450    pseudo-destructor-name:
4451      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
4452      :: [opt] nested-name-specifier template template-id :: ~ type-name
4453      :: [opt] nested-name-specifier [opt] ~ type-name
4454
4455    If either of the first two productions is used, sets *SCOPE to the
4456    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
4457    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
4458    or ERROR_MARK_NODE if the parse fails.  */
4459
4460 static void
4461 cp_parser_pseudo_destructor_name (cp_parser* parser,
4462                                   tree* scope,
4463                                   tree* type)
4464 {
4465   bool nested_name_specifier_p;
4466
4467   /* Assume that things will not work out.  */
4468   *type = error_mark_node;
4469
4470   /* Look for the optional `::' operator.  */
4471   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
4472   /* Look for the optional nested-name-specifier.  */
4473   nested_name_specifier_p
4474     = (cp_parser_nested_name_specifier_opt (parser,
4475                                             /*typename_keyword_p=*/false,
4476                                             /*check_dependency_p=*/true,
4477                                             /*type_p=*/false,
4478                                             /*is_declaration=*/true)
4479        != NULL_TREE);
4480   /* Now, if we saw a nested-name-specifier, we might be doing the
4481      second production.  */
4482   if (nested_name_specifier_p
4483       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
4484     {
4485       /* Consume the `template' keyword.  */
4486       cp_lexer_consume_token (parser->lexer);
4487       /* Parse the template-id.  */
4488       cp_parser_template_id (parser,
4489                              /*template_keyword_p=*/true,
4490                              /*check_dependency_p=*/false,
4491                              /*is_declaration=*/true);
4492       /* Look for the `::' token.  */
4493       cp_parser_require (parser, CPP_SCOPE, "`::'");
4494     }
4495   /* If the next token is not a `~', then there might be some
4496      additional qualification.  */
4497   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
4498     {
4499       /* Look for the type-name.  */
4500       *scope = TREE_TYPE (cp_parser_type_name (parser));
4501
4502       if (*scope == error_mark_node)
4503         return;
4504
4505       /* If we don't have ::~, then something has gone wrong.  Since
4506          the only caller of this function is looking for something
4507          after `.' or `->' after a scalar type, most likely the
4508          program is trying to get a member of a non-aggregate
4509          type.  */
4510       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE)
4511           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_COMPL)
4512         {
4513           cp_parser_error (parser, "request for member of non-aggregate type");
4514           return;
4515         }
4516
4517       /* Look for the `::' token.  */
4518       cp_parser_require (parser, CPP_SCOPE, "`::'");
4519     }
4520   else
4521     *scope = NULL_TREE;
4522
4523   /* Look for the `~'.  */
4524   cp_parser_require (parser, CPP_COMPL, "`~'");
4525   /* Look for the type-name again.  We are not responsible for
4526      checking that it matches the first type-name.  */
4527   *type = cp_parser_type_name (parser);
4528 }
4529
4530 /* Parse a unary-expression.
4531
4532    unary-expression:
4533      postfix-expression
4534      ++ cast-expression
4535      -- cast-expression
4536      unary-operator cast-expression
4537      sizeof unary-expression
4538      sizeof ( type-id )
4539      new-expression
4540      delete-expression
4541
4542    GNU Extensions:
4543
4544    unary-expression:
4545      __extension__ cast-expression
4546      __alignof__ unary-expression
4547      __alignof__ ( type-id )
4548      __real__ cast-expression
4549      __imag__ cast-expression
4550      && identifier
4551
4552    ADDRESS_P is true iff the unary-expression is appearing as the
4553    operand of the `&' operator.
4554
4555    Returns a representation of the expression.  */
4556
4557 static tree
4558 cp_parser_unary_expression (cp_parser *parser, bool address_p)
4559 {
4560   cp_token *token;
4561   enum tree_code unary_operator;
4562
4563   /* Peek at the next token.  */
4564   token = cp_lexer_peek_token (parser->lexer);
4565   /* Some keywords give away the kind of expression.  */
4566   if (token->type == CPP_KEYWORD)
4567     {
4568       enum rid keyword = token->keyword;
4569
4570       switch (keyword)
4571         {
4572         case RID_ALIGNOF:
4573         case RID_SIZEOF:
4574           {
4575             tree operand;
4576             enum tree_code op;
4577
4578             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
4579             /* Consume the token.  */
4580             cp_lexer_consume_token (parser->lexer);
4581             /* Parse the operand.  */
4582             operand = cp_parser_sizeof_operand (parser, keyword);
4583
4584             if (TYPE_P (operand))
4585               return cxx_sizeof_or_alignof_type (operand, op, true);
4586             else
4587               return cxx_sizeof_or_alignof_expr (operand, op);
4588           }
4589
4590         case RID_NEW:
4591           return cp_parser_new_expression (parser);
4592
4593         case RID_DELETE:
4594           return cp_parser_delete_expression (parser);
4595
4596         case RID_EXTENSION:
4597           {
4598             /* The saved value of the PEDANTIC flag.  */
4599             int saved_pedantic;
4600             tree expr;
4601
4602             /* Save away the PEDANTIC flag.  */
4603             cp_parser_extension_opt (parser, &saved_pedantic);
4604             /* Parse the cast-expression.  */
4605             expr = cp_parser_simple_cast_expression (parser);
4606             /* Restore the PEDANTIC flag.  */
4607             pedantic = saved_pedantic;
4608
4609             return expr;
4610           }
4611
4612         case RID_REALPART:
4613         case RID_IMAGPART:
4614           {
4615             tree expression;
4616
4617             /* Consume the `__real__' or `__imag__' token.  */
4618             cp_lexer_consume_token (parser->lexer);
4619             /* Parse the cast-expression.  */
4620             expression = cp_parser_simple_cast_expression (parser);
4621             /* Create the complete representation.  */
4622             return build_x_unary_op ((keyword == RID_REALPART
4623                                       ? REALPART_EXPR : IMAGPART_EXPR),
4624                                      expression);
4625           }
4626           break;
4627
4628         default:
4629           break;
4630         }
4631     }
4632
4633   /* Look for the `:: new' and `:: delete', which also signal the
4634      beginning of a new-expression, or delete-expression,
4635      respectively.  If the next token is `::', then it might be one of
4636      these.  */
4637   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
4638     {
4639       enum rid keyword;
4640
4641       /* See if the token after the `::' is one of the keywords in
4642          which we're interested.  */
4643       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
4644       /* If it's `new', we have a new-expression.  */
4645       if (keyword == RID_NEW)
4646         return cp_parser_new_expression (parser);
4647       /* Similarly, for `delete'.  */
4648       else if (keyword == RID_DELETE)
4649         return cp_parser_delete_expression (parser);
4650     }
4651
4652   /* Look for a unary operator.  */
4653   unary_operator = cp_parser_unary_operator (token);
4654   /* The `++' and `--' operators can be handled similarly, even though
4655      they are not technically unary-operators in the grammar.  */
4656   if (unary_operator == ERROR_MARK)
4657     {
4658       if (token->type == CPP_PLUS_PLUS)
4659         unary_operator = PREINCREMENT_EXPR;
4660       else if (token->type == CPP_MINUS_MINUS)
4661         unary_operator = PREDECREMENT_EXPR;
4662       /* Handle the GNU address-of-label extension.  */
4663       else if (cp_parser_allow_gnu_extensions_p (parser)
4664                && token->type == CPP_AND_AND)
4665         {
4666           tree identifier;
4667
4668           /* Consume the '&&' token.  */
4669           cp_lexer_consume_token (parser->lexer);
4670           /* Look for the identifier.  */
4671           identifier = cp_parser_identifier (parser);
4672           /* Create an expression representing the address.  */
4673           return finish_label_address_expr (identifier);
4674         }
4675     }
4676   if (unary_operator != ERROR_MARK)
4677     {
4678       tree cast_expression;
4679       tree expression = error_mark_node;
4680       const char *non_constant_p = NULL;
4681
4682       /* Consume the operator token.  */
4683       token = cp_lexer_consume_token (parser->lexer);
4684       /* Parse the cast-expression.  */
4685       cast_expression
4686         = cp_parser_cast_expression (parser, unary_operator == ADDR_EXPR);
4687       /* Now, build an appropriate representation.  */
4688       switch (unary_operator)
4689         {
4690         case INDIRECT_REF:
4691           non_constant_p = "`*'";
4692           expression = build_x_indirect_ref (cast_expression, "unary *");
4693           break;
4694
4695         case ADDR_EXPR:
4696           non_constant_p = "`&'";
4697           /* Fall through.  */
4698         case BIT_NOT_EXPR:
4699           expression = build_x_unary_op (unary_operator, cast_expression);
4700           break;
4701
4702         case PREINCREMENT_EXPR:
4703         case PREDECREMENT_EXPR:
4704           non_constant_p = (unary_operator == PREINCREMENT_EXPR
4705                             ? "`++'" : "`--'");
4706           /* Fall through.  */
4707         case CONVERT_EXPR:
4708         case NEGATE_EXPR:
4709         case TRUTH_NOT_EXPR:
4710           expression = finish_unary_op_expr (unary_operator, cast_expression);
4711           break;
4712
4713         default:
4714           gcc_unreachable ();
4715         }
4716
4717       if (non_constant_p
4718           && cp_parser_non_integral_constant_expression (parser,
4719                                                          non_constant_p))
4720         expression = error_mark_node;
4721
4722       return expression;
4723     }
4724
4725   return cp_parser_postfix_expression (parser, address_p);
4726 }
4727
4728 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
4729    unary-operator, the corresponding tree code is returned.  */
4730
4731 static enum tree_code
4732 cp_parser_unary_operator (cp_token* token)
4733 {
4734   switch (token->type)
4735     {
4736     case CPP_MULT:
4737       return INDIRECT_REF;
4738
4739     case CPP_AND:
4740       return ADDR_EXPR;
4741
4742     case CPP_PLUS:
4743       return CONVERT_EXPR;
4744
4745     case CPP_MINUS:
4746       return NEGATE_EXPR;
4747
4748     case CPP_NOT:
4749       return TRUTH_NOT_EXPR;
4750
4751     case CPP_COMPL:
4752       return BIT_NOT_EXPR;
4753
4754     default:
4755       return ERROR_MARK;
4756     }
4757 }
4758
4759 /* Parse a new-expression.
4760
4761    new-expression:
4762      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
4763      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
4764
4765    Returns a representation of the expression.  */
4766
4767 static tree
4768 cp_parser_new_expression (cp_parser* parser)
4769 {
4770   bool global_scope_p;
4771   tree placement;
4772   tree type;
4773   tree initializer;
4774   tree nelts;
4775
4776   /* Look for the optional `::' operator.  */
4777   global_scope_p
4778     = (cp_parser_global_scope_opt (parser,
4779                                    /*current_scope_valid_p=*/false)
4780        != NULL_TREE);
4781   /* Look for the `new' operator.  */
4782   cp_parser_require_keyword (parser, RID_NEW, "`new'");
4783   /* There's no easy way to tell a new-placement from the
4784      `( type-id )' construct.  */
4785   cp_parser_parse_tentatively (parser);
4786   /* Look for a new-placement.  */
4787   placement = cp_parser_new_placement (parser);
4788   /* If that didn't work out, there's no new-placement.  */
4789   if (!cp_parser_parse_definitely (parser))
4790     placement = NULL_TREE;
4791
4792   /* If the next token is a `(', then we have a parenthesized
4793      type-id.  */
4794   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4795     {
4796       /* Consume the `('.  */
4797       cp_lexer_consume_token (parser->lexer);
4798       /* Parse the type-id.  */
4799       type = cp_parser_type_id (parser);
4800       /* Look for the closing `)'.  */
4801       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
4802       /* There should not be a direct-new-declarator in this production,
4803          but GCC used to allowed this, so we check and emit a sensible error
4804          message for this case.  */
4805       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4806         {
4807           error ("array bound forbidden after parenthesized type-id");
4808           inform ("try removing the parentheses around the type-id");
4809           cp_parser_direct_new_declarator (parser);
4810         }
4811       nelts = NULL_TREE;
4812     }
4813   /* Otherwise, there must be a new-type-id.  */
4814   else
4815     type = cp_parser_new_type_id (parser, &nelts);
4816
4817   /* If the next token is a `(', then we have a new-initializer.  */
4818   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4819     initializer = cp_parser_new_initializer (parser);
4820   else
4821     initializer = NULL_TREE;
4822
4823   /* A new-expression may not appear in an integral constant
4824      expression.  */
4825   if (cp_parser_non_integral_constant_expression (parser, "`new'"))
4826     return error_mark_node;
4827
4828   /* Create a representation of the new-expression.  */
4829   return build_new (placement, type, nelts, initializer, global_scope_p);
4830 }
4831
4832 /* Parse a new-placement.
4833
4834    new-placement:
4835      ( expression-list )
4836
4837    Returns the same representation as for an expression-list.  */
4838
4839 static tree
4840 cp_parser_new_placement (cp_parser* parser)
4841 {
4842   tree expression_list;
4843
4844   /* Parse the expression-list.  */
4845   expression_list = (cp_parser_parenthesized_expression_list
4846                      (parser, false, /*non_constant_p=*/NULL));
4847
4848   return expression_list;
4849 }
4850
4851 /* Parse a new-type-id.
4852
4853    new-type-id:
4854      type-specifier-seq new-declarator [opt]
4855
4856    Returns the TYPE allocated.  If the new-type-id indicates an array
4857    type, *NELTS is set to the number of elements in the last array
4858    bound; the TYPE will not include the last array bound.  */
4859
4860 static tree
4861 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
4862 {
4863   cp_decl_specifier_seq type_specifier_seq;
4864   cp_declarator *new_declarator;
4865   cp_declarator *declarator;
4866   cp_declarator *outer_declarator;
4867   const char *saved_message;
4868   tree type;
4869
4870   /* The type-specifier sequence must not contain type definitions.
4871      (It cannot contain declarations of new types either, but if they
4872      are not definitions we will catch that because they are not
4873      complete.)  */
4874   saved_message = parser->type_definition_forbidden_message;
4875   parser->type_definition_forbidden_message
4876     = "types may not be defined in a new-type-id";
4877   /* Parse the type-specifier-seq.  */
4878   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
4879   /* Restore the old message.  */
4880   parser->type_definition_forbidden_message = saved_message;
4881   /* Parse the new-declarator.  */
4882   new_declarator = cp_parser_new_declarator_opt (parser);
4883
4884   /* Determine the number of elements in the last array dimension, if
4885      any.  */
4886   *nelts = NULL_TREE;
4887   /* Skip down to the last array dimension.  */
4888   declarator = new_declarator;
4889   outer_declarator = NULL;
4890   while (declarator && (declarator->kind == cdk_pointer
4891                         || declarator->kind == cdk_ptrmem))
4892     {
4893       outer_declarator = declarator;
4894       declarator = declarator->declarator;
4895     }
4896   while (declarator
4897          && declarator->kind == cdk_array
4898          && declarator->declarator
4899          && declarator->declarator->kind == cdk_array)
4900     {
4901       outer_declarator = declarator;
4902       declarator = declarator->declarator;
4903     }
4904
4905   if (declarator && declarator->kind == cdk_array)
4906     {
4907       *nelts = declarator->u.array.bounds;
4908       if (*nelts == error_mark_node)
4909         *nelts = integer_one_node;
4910       else if (!processing_template_decl)
4911         {
4912           if (!build_expr_type_conversion (WANT_INT | WANT_ENUM, *nelts,
4913                                            false))
4914             pedwarn ("size in array new must have integral type");
4915           *nelts = save_expr (cp_convert (sizetype, *nelts));
4916           if (*nelts == integer_zero_node)
4917             warning ("zero size array reserves no space");
4918         }
4919       if (outer_declarator)
4920         outer_declarator->declarator = declarator->declarator;
4921       else
4922         new_declarator = NULL;
4923     }
4924
4925   type = groktypename (&type_specifier_seq, new_declarator);
4926   if (TREE_CODE (type) == ARRAY_TYPE && *nelts == NULL_TREE)
4927     {
4928       *nelts = array_type_nelts_top (type);
4929       type = TREE_TYPE (type);
4930     }
4931   return type;
4932 }
4933
4934 /* Parse an (optional) new-declarator.
4935
4936    new-declarator:
4937      ptr-operator new-declarator [opt]
4938      direct-new-declarator
4939
4940    Returns the declarator.  */
4941
4942 static cp_declarator *
4943 cp_parser_new_declarator_opt (cp_parser* parser)
4944 {
4945   enum tree_code code;
4946   tree type;
4947   cp_cv_quals cv_quals;
4948
4949   /* We don't know if there's a ptr-operator next, or not.  */
4950   cp_parser_parse_tentatively (parser);
4951   /* Look for a ptr-operator.  */
4952   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
4953   /* If that worked, look for more new-declarators.  */
4954   if (cp_parser_parse_definitely (parser))
4955     {
4956       cp_declarator *declarator;
4957
4958       /* Parse another optional declarator.  */
4959       declarator = cp_parser_new_declarator_opt (parser);
4960
4961       /* Create the representation of the declarator.  */
4962       if (type)
4963         declarator = make_ptrmem_declarator (cv_quals, type, declarator);
4964       else if (code == INDIRECT_REF)
4965         declarator = make_pointer_declarator (cv_quals, declarator);
4966       else
4967         declarator = make_reference_declarator (cv_quals, declarator);
4968
4969       return declarator;
4970     }
4971
4972   /* If the next token is a `[', there is a direct-new-declarator.  */
4973   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
4974     return cp_parser_direct_new_declarator (parser);
4975
4976   return NULL;
4977 }
4978
4979 /* Parse a direct-new-declarator.
4980
4981    direct-new-declarator:
4982      [ expression ]
4983      direct-new-declarator [constant-expression]
4984
4985    */
4986
4987 static cp_declarator *
4988 cp_parser_direct_new_declarator (cp_parser* parser)
4989 {
4990   cp_declarator *declarator = NULL;
4991
4992   while (true)
4993     {
4994       tree expression;
4995
4996       /* Look for the opening `['.  */
4997       cp_parser_require (parser, CPP_OPEN_SQUARE, "`['");
4998       /* The first expression is not required to be constant.  */
4999       if (!declarator)
5000         {
5001           expression = cp_parser_expression (parser);
5002           /* The standard requires that the expression have integral
5003              type.  DR 74 adds enumeration types.  We believe that the
5004              real intent is that these expressions be handled like the
5005              expression in a `switch' condition, which also allows
5006              classes with a single conversion to integral or
5007              enumeration type.  */
5008           if (!processing_template_decl)
5009             {
5010               expression
5011                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5012                                               expression,
5013                                               /*complain=*/true);
5014               if (!expression)
5015                 {
5016                   error ("expression in new-declarator must have integral "
5017                          "or enumeration type");
5018                   expression = error_mark_node;
5019                 }
5020             }
5021         }
5022       /* But all the other expressions must be.  */
5023       else
5024         expression
5025           = cp_parser_constant_expression (parser,
5026                                            /*allow_non_constant=*/false,
5027                                            NULL);
5028       /* Look for the closing `]'.  */
5029       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5030
5031       /* Add this bound to the declarator.  */
5032       declarator = make_array_declarator (declarator, expression);
5033
5034       /* If the next token is not a `[', then there are no more
5035          bounds.  */
5036       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5037         break;
5038     }
5039
5040   return declarator;
5041 }
5042
5043 /* Parse a new-initializer.
5044
5045    new-initializer:
5046      ( expression-list [opt] )
5047
5048    Returns a representation of the expression-list.  If there is no
5049    expression-list, VOID_ZERO_NODE is returned.  */
5050
5051 static tree
5052 cp_parser_new_initializer (cp_parser* parser)
5053 {
5054   tree expression_list;
5055
5056   expression_list = (cp_parser_parenthesized_expression_list
5057                      (parser, false, /*non_constant_p=*/NULL));
5058   if (!expression_list)
5059     expression_list = void_zero_node;
5060
5061   return expression_list;
5062 }
5063
5064 /* Parse a delete-expression.
5065
5066    delete-expression:
5067      :: [opt] delete cast-expression
5068      :: [opt] delete [ ] cast-expression
5069
5070    Returns a representation of the expression.  */
5071
5072 static tree
5073 cp_parser_delete_expression (cp_parser* parser)
5074 {
5075   bool global_scope_p;
5076   bool array_p;
5077   tree expression;
5078
5079   /* Look for the optional `::' operator.  */
5080   global_scope_p
5081     = (cp_parser_global_scope_opt (parser,
5082                                    /*current_scope_valid_p=*/false)
5083        != NULL_TREE);
5084   /* Look for the `delete' keyword.  */
5085   cp_parser_require_keyword (parser, RID_DELETE, "`delete'");
5086   /* See if the array syntax is in use.  */
5087   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5088     {
5089       /* Consume the `[' token.  */
5090       cp_lexer_consume_token (parser->lexer);
5091       /* Look for the `]' token.  */
5092       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
5093       /* Remember that this is the `[]' construct.  */
5094       array_p = true;
5095     }
5096   else
5097     array_p = false;
5098
5099   /* Parse the cast-expression.  */
5100   expression = cp_parser_simple_cast_expression (parser);
5101
5102   /* A delete-expression may not appear in an integral constant
5103      expression.  */
5104   if (cp_parser_non_integral_constant_expression (parser, "`delete'"))
5105     return error_mark_node;
5106
5107   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
5108 }
5109
5110 /* Parse a cast-expression.
5111
5112    cast-expression:
5113      unary-expression
5114      ( type-id ) cast-expression
5115
5116    Returns a representation of the expression.  */
5117
5118 static tree
5119 cp_parser_cast_expression (cp_parser *parser, bool address_p)
5120 {
5121   /* If it's a `(', then we might be looking at a cast.  */
5122   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5123     {
5124       tree type = NULL_TREE;
5125       tree expr = NULL_TREE;
5126       bool compound_literal_p;
5127       const char *saved_message;
5128
5129       /* There's no way to know yet whether or not this is a cast.
5130          For example, `(int (3))' is a unary-expression, while `(int)
5131          3' is a cast.  So, we resort to parsing tentatively.  */
5132       cp_parser_parse_tentatively (parser);
5133       /* Types may not be defined in a cast.  */
5134       saved_message = parser->type_definition_forbidden_message;
5135       parser->type_definition_forbidden_message
5136         = "types may not be defined in casts";
5137       /* Consume the `('.  */
5138       cp_lexer_consume_token (parser->lexer);
5139       /* A very tricky bit is that `(struct S) { 3 }' is a
5140          compound-literal (which we permit in C++ as an extension).
5141          But, that construct is not a cast-expression -- it is a
5142          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
5143          is legal; if the compound-literal were a cast-expression,
5144          you'd need an extra set of parentheses.)  But, if we parse
5145          the type-id, and it happens to be a class-specifier, then we
5146          will commit to the parse at that point, because we cannot
5147          undo the action that is done when creating a new class.  So,
5148          then we cannot back up and do a postfix-expression.
5149
5150          Therefore, we scan ahead to the closing `)', and check to see
5151          if the token after the `)' is a `{'.  If so, we are not
5152          looking at a cast-expression.
5153
5154          Save tokens so that we can put them back.  */
5155       cp_lexer_save_tokens (parser->lexer);
5156       /* Skip tokens until the next token is a closing parenthesis.
5157          If we find the closing `)', and the next token is a `{', then
5158          we are looking at a compound-literal.  */
5159       compound_literal_p
5160         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
5161                                                   /*consume_paren=*/true)
5162            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
5163       /* Roll back the tokens we skipped.  */
5164       cp_lexer_rollback_tokens (parser->lexer);
5165       /* If we were looking at a compound-literal, simulate an error
5166          so that the call to cp_parser_parse_definitely below will
5167          fail.  */
5168       if (compound_literal_p)
5169         cp_parser_simulate_error (parser);
5170       else
5171         {
5172           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5173           parser->in_type_id_in_expr_p = true;
5174           /* Look for the type-id.  */
5175           type = cp_parser_type_id (parser);
5176           /* Look for the closing `)'.  */
5177           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5178           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5179         }
5180
5181       /* Restore the saved message.  */
5182       parser->type_definition_forbidden_message = saved_message;
5183
5184       /* If ok so far, parse the dependent expression. We cannot be
5185          sure it is a cast. Consider `(T ())'.  It is a parenthesized
5186          ctor of T, but looks like a cast to function returning T
5187          without a dependent expression.  */
5188       if (!cp_parser_error_occurred (parser))
5189         expr = cp_parser_simple_cast_expression (parser);
5190
5191       if (cp_parser_parse_definitely (parser))
5192         {
5193           /* Warn about old-style casts, if so requested.  */
5194           if (warn_old_style_cast
5195               && !in_system_header
5196               && !VOID_TYPE_P (type)
5197               && current_lang_name != lang_name_c)
5198             warning ("use of old-style cast");
5199
5200           /* Only type conversions to integral or enumeration types
5201              can be used in constant-expressions.  */
5202           if (parser->integral_constant_expression_p
5203               && !dependent_type_p (type)
5204               && !INTEGRAL_OR_ENUMERATION_TYPE_P (type)
5205               && (cp_parser_non_integral_constant_expression
5206                   (parser,
5207                    "a cast to a type other than an integral or "
5208                    "enumeration type")))
5209             return error_mark_node;
5210
5211           /* Perform the cast.  */
5212           expr = build_c_cast (type, expr);
5213           return expr;
5214         }
5215     }
5216
5217   /* If we get here, then it's not a cast, so it must be a
5218      unary-expression.  */
5219   return cp_parser_unary_expression (parser, address_p);
5220 }
5221
5222 /* Parse a binary expression of the general form:
5223
5224    pm-expression:
5225      cast-expression
5226      pm-expression .* cast-expression
5227      pm-expression ->* cast-expression
5228
5229    multiplicative-expression:
5230      pm-expression
5231      multiplicative-expression * pm-expression
5232      multiplicative-expression / pm-expression
5233      multiplicative-expression % pm-expression
5234
5235    additive-expression:
5236      multiplicative-expression
5237      additive-expression + multiplicative-expression
5238      additive-expression - multiplicative-expression
5239
5240    shift-expression:
5241      additive-expression
5242      shift-expression << additive-expression
5243      shift-expression >> additive-expression
5244
5245    relational-expression:
5246      shift-expression
5247      relational-expression < shift-expression
5248      relational-expression > shift-expression
5249      relational-expression <= shift-expression
5250      relational-expression >= shift-expression
5251
5252   GNU Extension:
5253   
5254    relational-expression:
5255      relational-expression <? shift-expression
5256      relational-expression >? shift-expression
5257
5258    equality-expression:
5259      relational-expression
5260      equality-expression == relational-expression
5261      equality-expression != relational-expression
5262
5263    and-expression:
5264      equality-expression
5265      and-expression & equality-expression
5266
5267    exclusive-or-expression:
5268      and-expression
5269      exclusive-or-expression ^ and-expression
5270
5271    inclusive-or-expression:
5272      exclusive-or-expression
5273      inclusive-or-expression | exclusive-or-expression
5274
5275    logical-and-expression:
5276      inclusive-or-expression
5277      logical-and-expression && inclusive-or-expression
5278
5279    logical-or-expression:
5280      logical-and-expression
5281      logical-or-expression || logical-and-expression
5282
5283    All these are implemented with a single function like:
5284
5285    binary-expression:
5286      simple-cast-expression
5287      binary-expression <token> binary-expression
5288
5289    The binops_by_token map is used to get the tree codes for each <token> type.
5290    binary-expressions are associated according to a precedence table.  */
5291
5292 #define TOKEN_PRECEDENCE(token) \
5293   ((token->type == CPP_GREATER && !parser->greater_than_is_operator_p) \
5294    ? PREC_NOT_OPERATOR \
5295    : binops_by_token[token->type].prec)
5296
5297 static tree
5298 cp_parser_binary_expression (cp_parser* parser)
5299 {
5300   cp_parser_expression_stack stack;
5301   cp_parser_expression_stack_entry *sp = &stack[0];
5302   tree lhs, rhs;
5303   cp_token *token;
5304   enum tree_code tree_type;
5305   enum cp_parser_prec prec = PREC_NOT_OPERATOR, new_prec, lookahead_prec;
5306   bool overloaded_p;
5307
5308   /* Parse the first expression.  */
5309   lhs = cp_parser_simple_cast_expression (parser);
5310
5311   for (;;)
5312     {
5313       /* Get an operator token.  */
5314       token = cp_lexer_peek_token (parser->lexer);
5315       new_prec = TOKEN_PRECEDENCE (token);
5316
5317       /* Popping an entry off the stack means we completed a subexpression:
5318          - either we found a token which is not an operator (`>' where it is not
5319            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
5320            will happen repeatedly;
5321          - or, we found an operator which has lower priority.  This is the case 
5322            where the recursive descent *ascends*, as in `3 * 4 + 5' after
5323            parsing `3 * 4'. */
5324       if (new_prec <= prec)
5325         {
5326           if (sp == stack)
5327             break;
5328           else
5329             goto pop;
5330         }
5331
5332      get_rhs:
5333       tree_type = binops_by_token[token->type].tree_type;
5334
5335       /* We used the operator token. */
5336       cp_lexer_consume_token (parser->lexer);
5337
5338       /* Extract another operand.  It may be the RHS of this expression
5339          or the LHS of a new, higher priority expression.  */
5340       rhs = cp_parser_simple_cast_expression (parser);
5341
5342       /* Get another operator token.  Look up its precedence to avoid
5343          building a useless (immediately popped) stack entry for common
5344          cases such as 3 + 4 + 5 or 3 * 4 + 5.   */
5345       token = cp_lexer_peek_token (parser->lexer);
5346       lookahead_prec = TOKEN_PRECEDENCE (token);
5347       if (lookahead_prec > new_prec)
5348         {
5349           /* ... and prepare to parse the RHS of the new, higher priority
5350              expression.  Since precedence levels on the stack are
5351              monotonically increasing, we do not have to care about
5352              stack overflows.  */
5353           sp->prec = prec;
5354           sp->tree_type = tree_type;
5355           sp->lhs = lhs;
5356           sp++;
5357           lhs = rhs;
5358           prec = new_prec;
5359           new_prec = lookahead_prec;
5360           goto get_rhs;
5361
5362          pop:
5363           /* If the stack is not empty, we have parsed into LHS the right side
5364              (`4' in the example above) of an expression we had suspended.
5365              We can use the information on the stack to recover the LHS (`3') 
5366              from the stack together with the tree code (`MULT_EXPR'), and
5367              the precedence of the higher level subexpression
5368              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
5369              which will be used to actually build the additive expression.  */
5370           --sp;
5371           prec = sp->prec;
5372           tree_type = sp->tree_type;
5373           rhs = lhs;
5374           lhs = sp->lhs;
5375         }
5376
5377       overloaded_p = false;
5378       lhs = build_x_binary_op (tree_type, lhs, rhs, &overloaded_p);
5379
5380       /* If the binary operator required the use of an overloaded operator,
5381          then this expression cannot be an integral constant-expression.
5382          An overloaded operator can be used even if both operands are
5383          otherwise permissible in an integral constant-expression if at
5384          least one of the operands is of enumeration type.  */
5385
5386       if (overloaded_p
5387           && (cp_parser_non_integral_constant_expression 
5388               (parser, "calls to overloaded operators")))
5389         return error_mark_node;
5390     }
5391
5392   return lhs;
5393 }
5394
5395
5396 /* Parse the `? expression : assignment-expression' part of a
5397    conditional-expression.  The LOGICAL_OR_EXPR is the
5398    logical-or-expression that started the conditional-expression.
5399    Returns a representation of the entire conditional-expression.
5400
5401    This routine is used by cp_parser_assignment_expression.
5402
5403      ? expression : assignment-expression
5404
5405    GNU Extensions:
5406
5407      ? : assignment-expression */
5408
5409 static tree
5410 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
5411 {
5412   tree expr;
5413   tree assignment_expr;
5414
5415   /* Consume the `?' token.  */
5416   cp_lexer_consume_token (parser->lexer);
5417   if (cp_parser_allow_gnu_extensions_p (parser)
5418       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
5419     /* Implicit true clause.  */
5420     expr = NULL_TREE;
5421   else
5422     /* Parse the expression.  */
5423     expr = cp_parser_expression (parser);
5424
5425   /* The next token should be a `:'.  */
5426   cp_parser_require (parser, CPP_COLON, "`:'");
5427   /* Parse the assignment-expression.  */
5428   assignment_expr = cp_parser_assignment_expression (parser);
5429
5430   /* Build the conditional-expression.  */
5431   return build_x_conditional_expr (logical_or_expr,
5432                                    expr,
5433                                    assignment_expr);
5434 }
5435
5436 /* Parse an assignment-expression.
5437
5438    assignment-expression:
5439      conditional-expression
5440      logical-or-expression assignment-operator assignment_expression
5441      throw-expression
5442
5443    Returns a representation for the expression.  */
5444
5445 static tree
5446 cp_parser_assignment_expression (cp_parser* parser)
5447 {
5448   tree expr;
5449
5450   /* If the next token is the `throw' keyword, then we're looking at
5451      a throw-expression.  */
5452   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
5453     expr = cp_parser_throw_expression (parser);
5454   /* Otherwise, it must be that we are looking at a
5455      logical-or-expression.  */
5456   else
5457     {
5458       /* Parse the binary expressions (logical-or-expression).  */
5459       expr = cp_parser_binary_expression (parser);
5460       /* If the next token is a `?' then we're actually looking at a
5461          conditional-expression.  */
5462       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
5463         return cp_parser_question_colon_clause (parser, expr);
5464       else
5465         {
5466           enum tree_code assignment_operator;
5467
5468           /* If it's an assignment-operator, we're using the second
5469              production.  */
5470           assignment_operator
5471             = cp_parser_assignment_operator_opt (parser);
5472           if (assignment_operator != ERROR_MARK)
5473             {
5474               tree rhs;
5475
5476               /* Parse the right-hand side of the assignment.  */
5477               rhs = cp_parser_assignment_expression (parser);
5478               /* An assignment may not appear in a
5479                  constant-expression.  */
5480               if (cp_parser_non_integral_constant_expression (parser,
5481                                                               "an assignment"))
5482                 return error_mark_node;
5483               /* Build the assignment expression.  */
5484               expr = build_x_modify_expr (expr,
5485                                           assignment_operator,
5486                                           rhs);
5487             }
5488         }
5489     }
5490
5491   return expr;
5492 }
5493
5494 /* Parse an (optional) assignment-operator.
5495
5496    assignment-operator: one of
5497      = *= /= %= += -= >>= <<= &= ^= |=
5498
5499    GNU Extension:
5500
5501    assignment-operator: one of
5502      <?= >?=
5503
5504    If the next token is an assignment operator, the corresponding tree
5505    code is returned, and the token is consumed.  For example, for
5506    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
5507    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
5508    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
5509    operator, ERROR_MARK is returned.  */
5510
5511 static enum tree_code
5512 cp_parser_assignment_operator_opt (cp_parser* parser)
5513 {
5514   enum tree_code op;
5515   cp_token *token;
5516
5517   /* Peek at the next toen.  */
5518   token = cp_lexer_peek_token (parser->lexer);
5519
5520   switch (token->type)
5521     {
5522     case CPP_EQ:
5523       op = NOP_EXPR;
5524       break;
5525
5526     case CPP_MULT_EQ:
5527       op = MULT_EXPR;
5528       break;
5529
5530     case CPP_DIV_EQ:
5531       op = TRUNC_DIV_EXPR;
5532       break;
5533
5534     case CPP_MOD_EQ:
5535       op = TRUNC_MOD_EXPR;
5536       break;
5537
5538     case CPP_PLUS_EQ:
5539       op = PLUS_EXPR;
5540       break;
5541
5542     case CPP_MINUS_EQ:
5543       op = MINUS_EXPR;
5544       break;
5545
5546     case CPP_RSHIFT_EQ:
5547       op = RSHIFT_EXPR;
5548       break;
5549
5550     case CPP_LSHIFT_EQ:
5551       op = LSHIFT_EXPR;
5552       break;
5553
5554     case CPP_AND_EQ:
5555       op = BIT_AND_EXPR;
5556       break;
5557
5558     case CPP_XOR_EQ:
5559       op = BIT_XOR_EXPR;
5560       break;
5561
5562     case CPP_OR_EQ:
5563       op = BIT_IOR_EXPR;
5564       break;
5565
5566     case CPP_MIN_EQ:
5567       op = MIN_EXPR;
5568       break;
5569
5570     case CPP_MAX_EQ:
5571       op = MAX_EXPR;
5572       break;
5573
5574     default:
5575       /* Nothing else is an assignment operator.  */
5576       op = ERROR_MARK;
5577     }
5578
5579   /* If it was an assignment operator, consume it.  */
5580   if (op != ERROR_MARK)
5581     cp_lexer_consume_token (parser->lexer);
5582
5583   return op;
5584 }
5585
5586 /* Parse an expression.
5587
5588    expression:
5589      assignment-expression
5590      expression , assignment-expression
5591
5592    Returns a representation of the expression.  */
5593
5594 static tree
5595 cp_parser_expression (cp_parser* parser)
5596 {
5597   tree expression = NULL_TREE;
5598
5599   while (true)
5600     {
5601       tree assignment_expression;
5602
5603       /* Parse the next assignment-expression.  */
5604       assignment_expression
5605         = cp_parser_assignment_expression (parser);
5606       /* If this is the first assignment-expression, we can just
5607          save it away.  */
5608       if (!expression)
5609         expression = assignment_expression;
5610       else
5611         expression = build_x_compound_expr (expression,
5612                                             assignment_expression);
5613       /* If the next token is not a comma, then we are done with the
5614          expression.  */
5615       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5616         break;
5617       /* Consume the `,'.  */
5618       cp_lexer_consume_token (parser->lexer);
5619       /* A comma operator cannot appear in a constant-expression.  */
5620       if (cp_parser_non_integral_constant_expression (parser,
5621                                                       "a comma operator"))
5622         expression = error_mark_node;
5623     }
5624
5625   return expression;
5626 }
5627
5628 /* Parse a constant-expression.
5629
5630    constant-expression:
5631      conditional-expression
5632
5633   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
5634   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
5635   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
5636   is false, NON_CONSTANT_P should be NULL.  */
5637
5638 static tree
5639 cp_parser_constant_expression (cp_parser* parser,
5640                                bool allow_non_constant_p,
5641                                bool *non_constant_p)
5642 {
5643   bool saved_integral_constant_expression_p;
5644   bool saved_allow_non_integral_constant_expression_p;
5645   bool saved_non_integral_constant_expression_p;
5646   tree expression;
5647
5648   /* It might seem that we could simply parse the
5649      conditional-expression, and then check to see if it were
5650      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
5651      one that the compiler can figure out is constant, possibly after
5652      doing some simplifications or optimizations.  The standard has a
5653      precise definition of constant-expression, and we must honor
5654      that, even though it is somewhat more restrictive.
5655
5656      For example:
5657
5658        int i[(2, 3)];
5659
5660      is not a legal declaration, because `(2, 3)' is not a
5661      constant-expression.  The `,' operator is forbidden in a
5662      constant-expression.  However, GCC's constant-folding machinery
5663      will fold this operation to an INTEGER_CST for `3'.  */
5664
5665   /* Save the old settings.  */
5666   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
5667   saved_allow_non_integral_constant_expression_p
5668     = parser->allow_non_integral_constant_expression_p;
5669   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
5670   /* We are now parsing a constant-expression.  */
5671   parser->integral_constant_expression_p = true;
5672   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
5673   parser->non_integral_constant_expression_p = false;
5674   /* Although the grammar says "conditional-expression", we parse an
5675      "assignment-expression", which also permits "throw-expression"
5676      and the use of assignment operators.  In the case that
5677      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
5678      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
5679      actually essential that we look for an assignment-expression.
5680      For example, cp_parser_initializer_clauses uses this function to
5681      determine whether a particular assignment-expression is in fact
5682      constant.  */
5683   expression = cp_parser_assignment_expression (parser);
5684   /* Restore the old settings.  */
5685   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
5686   parser->allow_non_integral_constant_expression_p
5687     = saved_allow_non_integral_constant_expression_p;
5688   if (allow_non_constant_p)
5689     *non_constant_p = parser->non_integral_constant_expression_p;
5690   parser->non_integral_constant_expression_p = saved_non_integral_constant_expression_p;
5691
5692   return expression;
5693 }
5694
5695 /* Parse __builtin_offsetof.
5696
5697    offsetof-expression:
5698      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
5699
5700    offsetof-member-designator:
5701      id-expression
5702      | offsetof-member-designator "." id-expression
5703      | offsetof-member-designator "[" expression "]"
5704 */
5705
5706 static tree
5707 cp_parser_builtin_offsetof (cp_parser *parser)
5708 {
5709   int save_ice_p, save_non_ice_p;
5710   tree type, expr;
5711   cp_id_kind dummy;
5712
5713   /* We're about to accept non-integral-constant things, but will
5714      definitely yield an integral constant expression.  Save and
5715      restore these values around our local parsing.  */
5716   save_ice_p = parser->integral_constant_expression_p;
5717   save_non_ice_p = parser->non_integral_constant_expression_p;
5718
5719   /* Consume the "__builtin_offsetof" token.  */
5720   cp_lexer_consume_token (parser->lexer);
5721   /* Consume the opening `('.  */
5722   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
5723   /* Parse the type-id.  */
5724   type = cp_parser_type_id (parser);
5725   /* Look for the `,'.  */
5726   cp_parser_require (parser, CPP_COMMA, "`,'");
5727
5728   /* Build the (type *)null that begins the traditional offsetof macro.  */
5729   expr = build_static_cast (build_pointer_type (type), null_pointer_node);
5730
5731   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
5732   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
5733                                                  true, &dummy);
5734   while (true)
5735     {
5736       cp_token *token = cp_lexer_peek_token (parser->lexer);
5737       switch (token->type)
5738         {
5739         case CPP_OPEN_SQUARE:
5740           /* offsetof-member-designator "[" expression "]" */
5741           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
5742           break;
5743
5744         case CPP_DOT:
5745           /* offsetof-member-designator "." identifier */
5746           cp_lexer_consume_token (parser->lexer);
5747           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT, expr,
5748                                                          true, &dummy);
5749           break;
5750
5751         case CPP_CLOSE_PAREN:
5752           /* Consume the ")" token.  */
5753           cp_lexer_consume_token (parser->lexer);
5754           goto success;
5755
5756         default:
5757           /* Error.  We know the following require will fail, but
5758              that gives the proper error message.  */
5759           cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
5760           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
5761           expr = error_mark_node;
5762           goto failure;
5763         }
5764     }
5765
5766  success:
5767   /* If we're processing a template, we can't finish the semantics yet.
5768      Otherwise we can fold the entire expression now.  */
5769   if (processing_template_decl)
5770     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
5771   else
5772     expr = fold_offsetof (expr);
5773
5774  failure:
5775   parser->integral_constant_expression_p = save_ice_p;
5776   parser->non_integral_constant_expression_p = save_non_ice_p;
5777
5778   return expr;
5779 }
5780
5781 /* Statements [gram.stmt.stmt]  */
5782
5783 /* Parse a statement.
5784
5785    statement:
5786      labeled-statement
5787      expression-statement
5788      compound-statement
5789      selection-statement
5790      iteration-statement
5791      jump-statement
5792      declaration-statement
5793      try-block  */
5794
5795 static void
5796 cp_parser_statement (cp_parser* parser, tree in_statement_expr)
5797 {
5798   tree statement;
5799   cp_token *token;
5800   location_t statement_location;
5801
5802   /* There is no statement yet.  */
5803   statement = NULL_TREE;
5804   /* Peek at the next token.  */
5805   token = cp_lexer_peek_token (parser->lexer);
5806   /* Remember the location of the first token in the statement.  */
5807   statement_location = token->location;
5808   /* If this is a keyword, then that will often determine what kind of
5809      statement we have.  */
5810   if (token->type == CPP_KEYWORD)
5811     {
5812       enum rid keyword = token->keyword;
5813
5814       switch (keyword)
5815         {
5816         case RID_CASE:
5817         case RID_DEFAULT:
5818           statement = cp_parser_labeled_statement (parser,
5819                                                    in_statement_expr);
5820           break;
5821
5822         case RID_IF:
5823         case RID_SWITCH:
5824           statement = cp_parser_selection_statement (parser);
5825           break;
5826
5827         case RID_WHILE:
5828         case RID_DO:
5829         case RID_FOR:
5830           statement = cp_parser_iteration_statement (parser);
5831           break;
5832
5833         case RID_BREAK:
5834         case RID_CONTINUE:
5835         case RID_RETURN:
5836         case RID_GOTO:
5837           statement = cp_parser_jump_statement (parser);
5838           break;
5839
5840         case RID_TRY:
5841           statement = cp_parser_try_block (parser);
5842           break;
5843
5844         default:
5845           /* It might be a keyword like `int' that can start a
5846              declaration-statement.  */
5847           break;
5848         }
5849     }
5850   else if (token->type == CPP_NAME)
5851     {
5852       /* If the next token is a `:', then we are looking at a
5853          labeled-statement.  */
5854       token = cp_lexer_peek_nth_token (parser->lexer, 2);
5855       if (token->type == CPP_COLON)
5856         statement = cp_parser_labeled_statement (parser, in_statement_expr);
5857     }
5858   /* Anything that starts with a `{' must be a compound-statement.  */
5859   else if (token->type == CPP_OPEN_BRACE)
5860     statement = cp_parser_compound_statement (parser, NULL, false);
5861   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
5862      a statement all its own.  */
5863   else if (token->type == CPP_PRAGMA)
5864     {
5865       cp_lexer_handle_pragma (parser->lexer);
5866       return;
5867     }
5868
5869   /* Everything else must be a declaration-statement or an
5870      expression-statement.  Try for the declaration-statement
5871      first, unless we are looking at a `;', in which case we know that
5872      we have an expression-statement.  */
5873   if (!statement)
5874     {
5875       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
5876         {
5877           cp_parser_parse_tentatively (parser);
5878           /* Try to parse the declaration-statement.  */
5879           cp_parser_declaration_statement (parser);
5880           /* If that worked, we're done.  */
5881           if (cp_parser_parse_definitely (parser))
5882             return;
5883         }
5884       /* Look for an expression-statement instead.  */
5885       statement = cp_parser_expression_statement (parser, in_statement_expr);
5886     }
5887
5888   /* Set the line number for the statement.  */
5889   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
5890     SET_EXPR_LOCATION (statement, statement_location);
5891 }
5892
5893 /* Parse a labeled-statement.
5894
5895    labeled-statement:
5896      identifier : statement
5897      case constant-expression : statement
5898      default : statement
5899
5900    GNU Extension:
5901
5902    labeled-statement:
5903      case constant-expression ... constant-expression : statement
5904
5905    Returns the new CASE_LABEL_EXPR, for a `case' or `default' label.
5906    For an ordinary label, returns a LABEL_EXPR.  */
5907
5908 static tree
5909 cp_parser_labeled_statement (cp_parser* parser, tree in_statement_expr)
5910 {
5911   cp_token *token;
5912   tree statement = error_mark_node;
5913
5914   /* The next token should be an identifier.  */
5915   token = cp_lexer_peek_token (parser->lexer);
5916   if (token->type != CPP_NAME
5917       && token->type != CPP_KEYWORD)
5918     {
5919       cp_parser_error (parser, "expected labeled-statement");
5920       return error_mark_node;
5921     }
5922
5923   switch (token->keyword)
5924     {
5925     case RID_CASE:
5926       {
5927         tree expr, expr_hi;
5928         cp_token *ellipsis;
5929
5930         /* Consume the `case' token.  */
5931         cp_lexer_consume_token (parser->lexer);
5932         /* Parse the constant-expression.  */
5933         expr = cp_parser_constant_expression (parser,
5934                                               /*allow_non_constant_p=*/false,
5935                                               NULL);
5936
5937         ellipsis = cp_lexer_peek_token (parser->lexer);
5938         if (ellipsis->type == CPP_ELLIPSIS)
5939           {
5940             /* Consume the `...' token.  */
5941             cp_lexer_consume_token (parser->lexer);
5942             expr_hi =
5943               cp_parser_constant_expression (parser,
5944                                              /*allow_non_constant_p=*/false,
5945                                              NULL);
5946             /* We don't need to emit warnings here, as the common code
5947                will do this for us.  */
5948           }
5949         else
5950           expr_hi = NULL_TREE;
5951
5952         if (!parser->in_switch_statement_p)
5953           error ("case label %qE not within a switch statement", expr);
5954         else
5955           statement = finish_case_label (expr, expr_hi);
5956       }
5957       break;
5958
5959     case RID_DEFAULT:
5960       /* Consume the `default' token.  */
5961       cp_lexer_consume_token (parser->lexer);
5962       if (!parser->in_switch_statement_p)
5963         error ("case label not within a switch statement");
5964       else
5965         statement = finish_case_label (NULL_TREE, NULL_TREE);
5966       break;
5967
5968     default:
5969       /* Anything else must be an ordinary label.  */
5970       statement = finish_label_stmt (cp_parser_identifier (parser));
5971       break;
5972     }
5973
5974   /* Require the `:' token.  */
5975   cp_parser_require (parser, CPP_COLON, "`:'");
5976   /* Parse the labeled statement.  */
5977   cp_parser_statement (parser, in_statement_expr);
5978
5979   /* Return the label, in the case of a `case' or `default' label.  */
5980   return statement;
5981 }
5982
5983 /* Parse an expression-statement.
5984
5985    expression-statement:
5986      expression [opt] ;
5987
5988    Returns the new EXPR_STMT -- or NULL_TREE if the expression
5989    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
5990    indicates whether this expression-statement is part of an
5991    expression statement.  */
5992
5993 static tree
5994 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
5995 {
5996   tree statement = NULL_TREE;
5997
5998   /* If the next token is a ';', then there is no expression
5999      statement.  */
6000   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6001     statement = cp_parser_expression (parser);
6002
6003   /* Consume the final `;'.  */
6004   cp_parser_consume_semicolon_at_end_of_statement (parser);
6005
6006   if (in_statement_expr
6007       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
6008     {
6009       /* This is the final expression statement of a statement
6010          expression.  */
6011       statement = finish_stmt_expr_expr (statement, in_statement_expr);
6012     }
6013   else if (statement)
6014     statement = finish_expr_stmt (statement);
6015   else
6016     finish_stmt ();
6017
6018   return statement;
6019 }
6020
6021 /* Parse a compound-statement.
6022
6023    compound-statement:
6024      { statement-seq [opt] }
6025
6026    Returns a tree representing the statement.  */
6027
6028 static tree
6029 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
6030                               bool in_try)
6031 {
6032   tree compound_stmt;
6033
6034   /* Consume the `{'.  */
6035   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
6036     return error_mark_node;
6037   /* Begin the compound-statement.  */
6038   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
6039   /* Parse an (optional) statement-seq.  */
6040   cp_parser_statement_seq_opt (parser, in_statement_expr);
6041   /* Finish the compound-statement.  */
6042   finish_compound_stmt (compound_stmt);
6043   /* Consume the `}'.  */
6044   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6045
6046   return compound_stmt;
6047 }
6048
6049 /* Parse an (optional) statement-seq.
6050
6051    statement-seq:
6052      statement
6053      statement-seq [opt] statement  */
6054
6055 static void
6056 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
6057 {
6058   /* Scan statements until there aren't any more.  */
6059   while (true)
6060     {
6061       /* If we're looking at a `}', then we've run out of statements.  */
6062       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE)
6063           || cp_lexer_next_token_is (parser->lexer, CPP_EOF))
6064         break;
6065
6066       /* Parse the statement.  */
6067       cp_parser_statement (parser, in_statement_expr);
6068     }
6069 }
6070
6071 /* Parse a selection-statement.
6072
6073    selection-statement:
6074      if ( condition ) statement
6075      if ( condition ) statement else statement
6076      switch ( condition ) statement
6077
6078    Returns the new IF_STMT or SWITCH_STMT.  */
6079
6080 static tree
6081 cp_parser_selection_statement (cp_parser* parser)
6082 {
6083   cp_token *token;
6084   enum rid keyword;
6085
6086   /* Peek at the next token.  */
6087   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
6088
6089   /* See what kind of keyword it is.  */
6090   keyword = token->keyword;
6091   switch (keyword)
6092     {
6093     case RID_IF:
6094     case RID_SWITCH:
6095       {
6096         tree statement;
6097         tree condition;
6098
6099         /* Look for the `('.  */
6100         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
6101           {
6102             cp_parser_skip_to_end_of_statement (parser);
6103             return error_mark_node;
6104           }
6105
6106         /* Begin the selection-statement.  */
6107         if (keyword == RID_IF)
6108           statement = begin_if_stmt ();
6109         else
6110           statement = begin_switch_stmt ();
6111
6112         /* Parse the condition.  */
6113         condition = cp_parser_condition (parser);
6114         /* Look for the `)'.  */
6115         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
6116           cp_parser_skip_to_closing_parenthesis (parser, true, false,
6117                                                  /*consume_paren=*/true);
6118
6119         if (keyword == RID_IF)
6120           {
6121             /* Add the condition.  */
6122             finish_if_stmt_cond (condition, statement);
6123
6124             /* Parse the then-clause.  */
6125             cp_parser_implicitly_scoped_statement (parser);
6126             finish_then_clause (statement);
6127
6128             /* If the next token is `else', parse the else-clause.  */
6129             if (cp_lexer_next_token_is_keyword (parser->lexer,
6130                                                 RID_ELSE))
6131               {
6132                 /* Consume the `else' keyword.  */
6133                 cp_lexer_consume_token (parser->lexer);
6134                 begin_else_clause (statement);
6135                 /* Parse the else-clause.  */
6136                 cp_parser_implicitly_scoped_statement (parser);
6137                 finish_else_clause (statement);
6138               }
6139
6140             /* Now we're all done with the if-statement.  */
6141             finish_if_stmt (statement);
6142           }
6143         else
6144           {
6145             bool in_switch_statement_p;
6146
6147             /* Add the condition.  */
6148             finish_switch_cond (condition, statement);
6149
6150             /* Parse the body of the switch-statement.  */
6151             in_switch_statement_p = parser->in_switch_statement_p;
6152             parser->in_switch_statement_p = true;
6153             cp_parser_implicitly_scoped_statement (parser);
6154             parser->in_switch_statement_p = in_switch_statement_p;
6155
6156             /* Now we're all done with the switch-statement.  */
6157             finish_switch_stmt (statement);
6158           }
6159
6160         return statement;
6161       }
6162       break;
6163
6164     default:
6165       cp_parser_error (parser, "expected selection-statement");
6166       return error_mark_node;
6167     }
6168 }
6169
6170 /* Parse a condition.
6171
6172    condition:
6173      expression
6174      type-specifier-seq declarator = assignment-expression
6175
6176    GNU Extension:
6177
6178    condition:
6179      type-specifier-seq declarator asm-specification [opt]
6180        attributes [opt] = assignment-expression
6181
6182    Returns the expression that should be tested.  */
6183
6184 static tree
6185 cp_parser_condition (cp_parser* parser)
6186 {
6187   cp_decl_specifier_seq type_specifiers;
6188   const char *saved_message;
6189
6190   /* Try the declaration first.  */
6191   cp_parser_parse_tentatively (parser);
6192   /* New types are not allowed in the type-specifier-seq for a
6193      condition.  */
6194   saved_message = parser->type_definition_forbidden_message;
6195   parser->type_definition_forbidden_message
6196     = "types may not be defined in conditions";
6197   /* Parse the type-specifier-seq.  */
6198   cp_parser_type_specifier_seq (parser, &type_specifiers);
6199   /* Restore the saved message.  */
6200   parser->type_definition_forbidden_message = saved_message;
6201   /* If all is well, we might be looking at a declaration.  */
6202   if (!cp_parser_error_occurred (parser))
6203     {
6204       tree decl;
6205       tree asm_specification;
6206       tree attributes;
6207       cp_declarator *declarator;
6208       tree initializer = NULL_TREE;
6209
6210       /* Parse the declarator.  */
6211       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
6212                                          /*ctor_dtor_or_conv_p=*/NULL,
6213                                          /*parenthesized_p=*/NULL,
6214                                          /*member_p=*/false);
6215       /* Parse the attributes.  */
6216       attributes = cp_parser_attributes_opt (parser);
6217       /* Parse the asm-specification.  */
6218       asm_specification = cp_parser_asm_specification_opt (parser);
6219       /* If the next token is not an `=', then we might still be
6220          looking at an expression.  For example:
6221
6222            if (A(a).x)
6223
6224          looks like a decl-specifier-seq and a declarator -- but then
6225          there is no `=', so this is an expression.  */
6226       cp_parser_require (parser, CPP_EQ, "`='");
6227       /* If we did see an `=', then we are looking at a declaration
6228          for sure.  */
6229       if (cp_parser_parse_definitely (parser))
6230         {
6231           bool pop_p;   
6232
6233           /* Create the declaration.  */
6234           decl = start_decl (declarator, &type_specifiers,
6235                              /*initialized_p=*/true,
6236                              attributes, /*prefix_attributes=*/NULL_TREE,
6237                              &pop_p);
6238           /* Parse the assignment-expression.  */
6239           initializer = cp_parser_assignment_expression (parser);
6240
6241           /* Process the initializer.  */
6242           cp_finish_decl (decl,
6243                           initializer,
6244                           asm_specification,
6245                           LOOKUP_ONLYCONVERTING);
6246
6247           if (pop_p)
6248             pop_scope (DECL_CONTEXT (decl));
6249
6250           return convert_from_reference (decl);
6251         }
6252     }
6253   /* If we didn't even get past the declarator successfully, we are
6254      definitely not looking at a declaration.  */
6255   else
6256     cp_parser_abort_tentative_parse (parser);
6257
6258   /* Otherwise, we are looking at an expression.  */
6259   return cp_parser_expression (parser);
6260 }
6261
6262 /* Parse an iteration-statement.
6263
6264    iteration-statement:
6265      while ( condition ) statement
6266      do statement while ( expression ) ;
6267      for ( for-init-statement condition [opt] ; expression [opt] )
6268        statement
6269
6270    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
6271
6272 static tree
6273 cp_parser_iteration_statement (cp_parser* parser)
6274 {
6275   cp_token *token;
6276   enum rid keyword;
6277   tree statement;
6278   bool in_iteration_statement_p;
6279
6280
6281   /* Peek at the next token.  */
6282   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
6283   if (!token)
6284     return error_mark_node;
6285
6286   /* Remember whether or not we are already within an iteration
6287      statement.  */
6288   in_iteration_statement_p = parser->in_iteration_statement_p;
6289
6290   /* See what kind of keyword it is.  */
6291   keyword = token->keyword;
6292   switch (keyword)
6293     {
6294     case RID_WHILE:
6295       {
6296         tree condition;
6297
6298         /* Begin the while-statement.  */
6299         statement = begin_while_stmt ();
6300         /* Look for the `('.  */
6301         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6302         /* Parse the condition.  */
6303         condition = cp_parser_condition (parser);
6304         finish_while_stmt_cond (condition, statement);
6305         /* Look for the `)'.  */
6306         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6307         /* Parse the dependent statement.  */
6308         parser->in_iteration_statement_p = true;
6309         cp_parser_already_scoped_statement (parser);
6310         parser->in_iteration_statement_p = in_iteration_statement_p;
6311         /* We're done with the while-statement.  */
6312         finish_while_stmt (statement);
6313       }
6314       break;
6315
6316     case RID_DO:
6317       {
6318         tree expression;
6319
6320         /* Begin the do-statement.  */
6321         statement = begin_do_stmt ();
6322         /* Parse the body of the do-statement.  */
6323         parser->in_iteration_statement_p = true;
6324         cp_parser_implicitly_scoped_statement (parser);
6325         parser->in_iteration_statement_p = in_iteration_statement_p;
6326         finish_do_body (statement);
6327         /* Look for the `while' keyword.  */
6328         cp_parser_require_keyword (parser, RID_WHILE, "`while'");
6329         /* Look for the `('.  */
6330         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6331         /* Parse the expression.  */
6332         expression = cp_parser_expression (parser);
6333         /* We're done with the do-statement.  */
6334         finish_do_stmt (expression, statement);
6335         /* Look for the `)'.  */
6336         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6337         /* Look for the `;'.  */
6338         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6339       }
6340       break;
6341
6342     case RID_FOR:
6343       {
6344         tree condition = NULL_TREE;
6345         tree expression = NULL_TREE;
6346
6347         /* Begin the for-statement.  */
6348         statement = begin_for_stmt ();
6349         /* Look for the `('.  */
6350         cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
6351         /* Parse the initialization.  */
6352         cp_parser_for_init_statement (parser);
6353         finish_for_init_stmt (statement);
6354
6355         /* If there's a condition, process it.  */
6356         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6357           condition = cp_parser_condition (parser);
6358         finish_for_cond (condition, statement);
6359         /* Look for the `;'.  */
6360         cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6361
6362         /* If there's an expression, process it.  */
6363         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6364           expression = cp_parser_expression (parser);
6365         finish_for_expr (expression, statement);
6366         /* Look for the `)'.  */
6367         cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
6368
6369         /* Parse the body of the for-statement.  */
6370         parser->in_iteration_statement_p = true;
6371         cp_parser_already_scoped_statement (parser);
6372         parser->in_iteration_statement_p = in_iteration_statement_p;
6373
6374         /* We're done with the for-statement.  */
6375         finish_for_stmt (statement);
6376       }
6377       break;
6378
6379     default:
6380       cp_parser_error (parser, "expected iteration-statement");
6381       statement = error_mark_node;
6382       break;
6383     }
6384
6385   return statement;
6386 }
6387
6388 /* Parse a for-init-statement.
6389
6390    for-init-statement:
6391      expression-statement
6392      simple-declaration  */
6393
6394 static void
6395 cp_parser_for_init_statement (cp_parser* parser)
6396 {
6397   /* If the next token is a `;', then we have an empty
6398      expression-statement.  Grammatically, this is also a
6399      simple-declaration, but an invalid one, because it does not
6400      declare anything.  Therefore, if we did not handle this case
6401      specially, we would issue an error message about an invalid
6402      declaration.  */
6403   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6404     {
6405       /* We're going to speculatively look for a declaration, falling back
6406          to an expression, if necessary.  */
6407       cp_parser_parse_tentatively (parser);
6408       /* Parse the declaration.  */
6409       cp_parser_simple_declaration (parser,
6410                                     /*function_definition_allowed_p=*/false);
6411       /* If the tentative parse failed, then we shall need to look for an
6412          expression-statement.  */
6413       if (cp_parser_parse_definitely (parser))
6414         return;
6415     }
6416
6417   cp_parser_expression_statement (parser, false);
6418 }
6419
6420 /* Parse a jump-statement.
6421
6422    jump-statement:
6423      break ;
6424      continue ;
6425      return expression [opt] ;
6426      goto identifier ;
6427
6428    GNU extension:
6429
6430    jump-statement:
6431      goto * expression ;
6432
6433    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
6434
6435 static tree
6436 cp_parser_jump_statement (cp_parser* parser)
6437 {
6438   tree statement = error_mark_node;
6439   cp_token *token;
6440   enum rid keyword;
6441
6442   /* Peek at the next token.  */
6443   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
6444   if (!token)
6445     return error_mark_node;
6446
6447   /* See what kind of keyword it is.  */
6448   keyword = token->keyword;
6449   switch (keyword)
6450     {
6451     case RID_BREAK:
6452       if (!parser->in_switch_statement_p
6453           && !parser->in_iteration_statement_p)
6454         {
6455           error ("break statement not within loop or switch");
6456           statement = error_mark_node;
6457         }
6458       else
6459         statement = finish_break_stmt ();
6460       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6461       break;
6462
6463     case RID_CONTINUE:
6464       if (!parser->in_iteration_statement_p)
6465         {
6466           error ("continue statement not within a loop");
6467           statement = error_mark_node;
6468         }
6469       else
6470         statement = finish_continue_stmt ();
6471       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6472       break;
6473
6474     case RID_RETURN:
6475       {
6476         tree expr;
6477
6478         /* If the next token is a `;', then there is no
6479            expression.  */
6480         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
6481           expr = cp_parser_expression (parser);
6482         else
6483           expr = NULL_TREE;
6484         /* Build the return-statement.  */
6485         statement = finish_return_stmt (expr);
6486         /* Look for the final `;'.  */
6487         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6488       }
6489       break;
6490
6491     case RID_GOTO:
6492       /* Create the goto-statement.  */
6493       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
6494         {
6495           /* Issue a warning about this use of a GNU extension.  */
6496           if (pedantic)
6497             pedwarn ("ISO C++ forbids computed gotos");
6498           /* Consume the '*' token.  */
6499           cp_lexer_consume_token (parser->lexer);
6500           /* Parse the dependent expression.  */
6501           finish_goto_stmt (cp_parser_expression (parser));
6502         }
6503       else
6504         finish_goto_stmt (cp_parser_identifier (parser));
6505       /* Look for the final `;'.  */
6506       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
6507       break;
6508
6509     default:
6510       cp_parser_error (parser, "expected jump-statement");
6511       break;
6512     }
6513
6514   return statement;
6515 }
6516
6517 /* Parse a declaration-statement.
6518
6519    declaration-statement:
6520      block-declaration  */
6521
6522 static void
6523 cp_parser_declaration_statement (cp_parser* parser)
6524 {
6525   void *p;
6526
6527   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6528   p = obstack_alloc (&declarator_obstack, 0);
6529
6530  /* Parse the block-declaration.  */
6531   cp_parser_block_declaration (parser, /*statement_p=*/true);
6532
6533   /* Free any declarators allocated.  */
6534   obstack_free (&declarator_obstack, p);
6535
6536   /* Finish off the statement.  */
6537   finish_stmt ();
6538 }
6539
6540 /* Some dependent statements (like `if (cond) statement'), are
6541    implicitly in their own scope.  In other words, if the statement is
6542    a single statement (as opposed to a compound-statement), it is
6543    none-the-less treated as if it were enclosed in braces.  Any
6544    declarations appearing in the dependent statement are out of scope
6545    after control passes that point.  This function parses a statement,
6546    but ensures that is in its own scope, even if it is not a
6547    compound-statement.
6548
6549    Returns the new statement.  */
6550
6551 static tree
6552 cp_parser_implicitly_scoped_statement (cp_parser* parser)
6553 {
6554   tree statement;
6555
6556   /* If the token is not a `{', then we must take special action.  */
6557   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6558     {
6559       /* Create a compound-statement.  */
6560       statement = begin_compound_stmt (0);
6561       /* Parse the dependent-statement.  */
6562       cp_parser_statement (parser, false);
6563       /* Finish the dummy compound-statement.  */
6564       finish_compound_stmt (statement);
6565     }
6566   /* Otherwise, we simply parse the statement directly.  */
6567   else
6568     statement = cp_parser_compound_statement (parser, NULL, false);
6569
6570   /* Return the statement.  */
6571   return statement;
6572 }
6573
6574 /* For some dependent statements (like `while (cond) statement'), we
6575    have already created a scope.  Therefore, even if the dependent
6576    statement is a compound-statement, we do not want to create another
6577    scope.  */
6578
6579 static void
6580 cp_parser_already_scoped_statement (cp_parser* parser)
6581 {
6582   /* If the token is a `{', then we must take special action.  */
6583   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
6584     cp_parser_statement (parser, false);
6585   else
6586     {
6587       /* Avoid calling cp_parser_compound_statement, so that we
6588          don't create a new scope.  Do everything else by hand.  */
6589       cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
6590       cp_parser_statement_seq_opt (parser, false);
6591       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
6592     }
6593 }
6594
6595 /* Declarations [gram.dcl.dcl] */
6596
6597 /* Parse an optional declaration-sequence.
6598
6599    declaration-seq:
6600      declaration
6601      declaration-seq declaration  */
6602
6603 static void
6604 cp_parser_declaration_seq_opt (cp_parser* parser)
6605 {
6606   while (true)
6607     {
6608       cp_token *token;
6609
6610       token = cp_lexer_peek_token (parser->lexer);
6611
6612       if (token->type == CPP_CLOSE_BRACE
6613           || token->type == CPP_EOF)
6614         break;
6615
6616       if (token->type == CPP_SEMICOLON)
6617         {
6618           /* A declaration consisting of a single semicolon is
6619              invalid.  Allow it unless we're being pedantic.  */
6620           cp_lexer_consume_token (parser->lexer);
6621           if (pedantic && !in_system_header)
6622             pedwarn ("extra %<;%>");
6623           continue;
6624         }
6625
6626       /* If we're entering or exiting a region that's implicitly
6627          extern "C", modify the lang context appropriately. */
6628       if (!parser->implicit_extern_c && token->implicit_extern_c)
6629         {
6630           push_lang_context (lang_name_c);
6631           parser->implicit_extern_c = true;
6632         }
6633       else if (parser->implicit_extern_c && !token->implicit_extern_c)
6634         {
6635           pop_lang_context ();
6636           parser->implicit_extern_c = false;
6637         }
6638
6639       if (token->type == CPP_PRAGMA)
6640         {
6641           /* A top-level declaration can consist solely of a #pragma.
6642              A nested declaration cannot, so this is done here and not
6643              in cp_parser_declaration.  (A #pragma at block scope is
6644              handled in cp_parser_statement.)  */
6645           cp_lexer_handle_pragma (parser->lexer);
6646           continue;
6647         }
6648
6649       /* Parse the declaration itself.  */
6650       cp_parser_declaration (parser);
6651     }
6652 }
6653
6654 /* Parse a declaration.
6655
6656    declaration:
6657      block-declaration
6658      function-definition
6659      template-declaration
6660      explicit-instantiation
6661      explicit-specialization
6662      linkage-specification
6663      namespace-definition
6664
6665    GNU extension:
6666
6667    declaration:
6668       __extension__ declaration */
6669
6670 static void
6671 cp_parser_declaration (cp_parser* parser)
6672 {
6673   cp_token token1;
6674   cp_token token2;
6675   int saved_pedantic;
6676   void *p;
6677
6678   /* Check for the `__extension__' keyword.  */
6679   if (cp_parser_extension_opt (parser, &saved_pedantic))
6680     {
6681       /* Parse the qualified declaration.  */
6682       cp_parser_declaration (parser);
6683       /* Restore the PEDANTIC flag.  */
6684       pedantic = saved_pedantic;
6685
6686       return;
6687     }
6688
6689   /* Try to figure out what kind of declaration is present.  */
6690   token1 = *cp_lexer_peek_token (parser->lexer);
6691
6692   if (token1.type != CPP_EOF)
6693     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
6694
6695   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
6696   p = obstack_alloc (&declarator_obstack, 0);
6697
6698   /* If the next token is `extern' and the following token is a string
6699      literal, then we have a linkage specification.  */
6700   if (token1.keyword == RID_EXTERN
6701       && cp_parser_is_string_literal (&token2))
6702     cp_parser_linkage_specification (parser);
6703   /* If the next token is `template', then we have either a template
6704      declaration, an explicit instantiation, or an explicit
6705      specialization.  */
6706   else if (token1.keyword == RID_TEMPLATE)
6707     {
6708       /* `template <>' indicates a template specialization.  */
6709       if (token2.type == CPP_LESS
6710           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
6711         cp_parser_explicit_specialization (parser);
6712       /* `template <' indicates a template declaration.  */
6713       else if (token2.type == CPP_LESS)
6714         cp_parser_template_declaration (parser, /*member_p=*/false);
6715       /* Anything else must be an explicit instantiation.  */
6716       else
6717         cp_parser_explicit_instantiation (parser);
6718     }
6719   /* If the next token is `export', then we have a template
6720      declaration.  */
6721   else if (token1.keyword == RID_EXPORT)
6722     cp_parser_template_declaration (parser, /*member_p=*/false);
6723   /* If the next token is `extern', 'static' or 'inline' and the one
6724      after that is `template', we have a GNU extended explicit
6725      instantiation directive.  */
6726   else if (cp_parser_allow_gnu_extensions_p (parser)
6727            && (token1.keyword == RID_EXTERN
6728                || token1.keyword == RID_STATIC
6729                || token1.keyword == RID_INLINE)
6730            && token2.keyword == RID_TEMPLATE)
6731     cp_parser_explicit_instantiation (parser);
6732   /* If the next token is `namespace', check for a named or unnamed
6733      namespace definition.  */
6734   else if (token1.keyword == RID_NAMESPACE
6735            && (/* A named namespace definition.  */
6736                (token2.type == CPP_NAME
6737                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
6738                     == CPP_OPEN_BRACE))
6739                /* An unnamed namespace definition.  */
6740                || token2.type == CPP_OPEN_BRACE))
6741     cp_parser_namespace_definition (parser);
6742   /* We must have either a block declaration or a function
6743      definition.  */
6744   else
6745     /* Try to parse a block-declaration, or a function-definition.  */
6746     cp_parser_block_declaration (parser, /*statement_p=*/false);
6747
6748   /* Free any declarators allocated.  */
6749   obstack_free (&declarator_obstack, p);
6750 }
6751
6752 /* Parse a block-declaration.
6753
6754    block-declaration:
6755      simple-declaration
6756      asm-definition
6757      namespace-alias-definition
6758      using-declaration
6759      using-directive
6760
6761    GNU Extension:
6762
6763    block-declaration:
6764      __extension__ block-declaration
6765      label-declaration
6766
6767    If STATEMENT_P is TRUE, then this block-declaration is occurring as
6768    part of a declaration-statement.  */
6769
6770 static void
6771 cp_parser_block_declaration (cp_parser *parser,
6772                              bool      statement_p)
6773 {
6774   cp_token *token1;
6775   int saved_pedantic;
6776
6777   /* Check for the `__extension__' keyword.  */
6778   if (cp_parser_extension_opt (parser, &saved_pedantic))
6779     {
6780       /* Parse the qualified declaration.  */
6781       cp_parser_block_declaration (parser, statement_p);
6782       /* Restore the PEDANTIC flag.  */
6783       pedantic = saved_pedantic;
6784
6785       return;
6786     }
6787
6788   /* Peek at the next token to figure out which kind of declaration is
6789      present.  */
6790   token1 = cp_lexer_peek_token (parser->lexer);
6791
6792   /* If the next keyword is `asm', we have an asm-definition.  */
6793   if (token1->keyword == RID_ASM)
6794     {
6795       if (statement_p)
6796         cp_parser_commit_to_tentative_parse (parser);
6797       cp_parser_asm_definition (parser);
6798     }
6799   /* If the next keyword is `namespace', we have a
6800      namespace-alias-definition.  */
6801   else if (token1->keyword == RID_NAMESPACE)
6802     cp_parser_namespace_alias_definition (parser);
6803   /* If the next keyword is `using', we have either a
6804      using-declaration or a using-directive.  */
6805   else if (token1->keyword == RID_USING)
6806     {
6807       cp_token *token2;
6808
6809       if (statement_p)
6810         cp_parser_commit_to_tentative_parse (parser);
6811       /* If the token after `using' is `namespace', then we have a
6812          using-directive.  */
6813       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
6814       if (token2->keyword == RID_NAMESPACE)
6815         cp_parser_using_directive (parser);
6816       /* Otherwise, it's a using-declaration.  */
6817       else
6818         cp_parser_using_declaration (parser);
6819     }
6820   /* If the next keyword is `__label__' we have a label declaration.  */
6821   else if (token1->keyword == RID_LABEL)
6822     {
6823       if (statement_p)
6824         cp_parser_commit_to_tentative_parse (parser);
6825       cp_parser_label_declaration (parser);
6826     }
6827   /* Anything else must be a simple-declaration.  */
6828   else
6829     cp_parser_simple_declaration (parser, !statement_p);
6830 }
6831
6832 /* Parse a simple-declaration.
6833
6834    simple-declaration:
6835      decl-specifier-seq [opt] init-declarator-list [opt] ;
6836
6837    init-declarator-list:
6838      init-declarator
6839      init-declarator-list , init-declarator
6840
6841    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
6842    function-definition as a simple-declaration.  */
6843
6844 static void
6845 cp_parser_simple_declaration (cp_parser* parser,
6846                               bool function_definition_allowed_p)
6847 {
6848   cp_decl_specifier_seq decl_specifiers;
6849   int declares_class_or_enum;
6850   bool saw_declarator;
6851
6852   /* Defer access checks until we know what is being declared; the
6853      checks for names appearing in the decl-specifier-seq should be
6854      done as if we were in the scope of the thing being declared.  */
6855   push_deferring_access_checks (dk_deferred);
6856
6857   /* Parse the decl-specifier-seq.  We have to keep track of whether
6858      or not the decl-specifier-seq declares a named class or
6859      enumeration type, since that is the only case in which the
6860      init-declarator-list is allowed to be empty.
6861
6862      [dcl.dcl]
6863
6864      In a simple-declaration, the optional init-declarator-list can be
6865      omitted only when declaring a class or enumeration, that is when
6866      the decl-specifier-seq contains either a class-specifier, an
6867      elaborated-type-specifier, or an enum-specifier.  */
6868   cp_parser_decl_specifier_seq (parser,
6869                                 CP_PARSER_FLAGS_OPTIONAL,
6870                                 &decl_specifiers,
6871                                 &declares_class_or_enum);
6872   /* We no longer need to defer access checks.  */
6873   stop_deferring_access_checks ();
6874
6875   /* In a block scope, a valid declaration must always have a
6876      decl-specifier-seq.  By not trying to parse declarators, we can
6877      resolve the declaration/expression ambiguity more quickly.  */
6878   if (!function_definition_allowed_p
6879       && !decl_specifiers.any_specifiers_p)
6880     {
6881       cp_parser_error (parser, "expected declaration");
6882       goto done;
6883     }
6884
6885   /* If the next two tokens are both identifiers, the code is
6886      erroneous. The usual cause of this situation is code like:
6887
6888        T t;
6889
6890      where "T" should name a type -- but does not.  */
6891   if (!decl_specifiers.type
6892       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
6893     {
6894       /* If parsing tentatively, we should commit; we really are
6895          looking at a declaration.  */
6896       cp_parser_commit_to_tentative_parse (parser);
6897       /* Give up.  */
6898       goto done;
6899     }
6900   
6901   /* If we have seen at least one decl-specifier, and the next token
6902      is not a parenthesis, then we must be looking at a declaration.
6903      (After "int (" we might be looking at a functional cast.)  */
6904   if (decl_specifiers.any_specifiers_p 
6905       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
6906     cp_parser_commit_to_tentative_parse (parser);
6907
6908   /* Keep going until we hit the `;' at the end of the simple
6909      declaration.  */
6910   saw_declarator = false;
6911   while (cp_lexer_next_token_is_not (parser->lexer,
6912                                      CPP_SEMICOLON))
6913     {
6914       cp_token *token;
6915       bool function_definition_p;
6916       tree decl;
6917
6918       saw_declarator = true;
6919       /* Parse the init-declarator.  */
6920       decl = cp_parser_init_declarator (parser, &decl_specifiers,
6921                                         function_definition_allowed_p,
6922                                         /*member_p=*/false,
6923                                         declares_class_or_enum,
6924                                         &function_definition_p);
6925       /* If an error occurred while parsing tentatively, exit quickly.
6926          (That usually happens when in the body of a function; each
6927          statement is treated as a declaration-statement until proven
6928          otherwise.)  */
6929       if (cp_parser_error_occurred (parser))
6930         goto done;
6931       /* Handle function definitions specially.  */
6932       if (function_definition_p)
6933         {
6934           /* If the next token is a `,', then we are probably
6935              processing something like:
6936
6937                void f() {}, *p;
6938
6939              which is erroneous.  */
6940           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
6941             error ("mixing declarations and function-definitions is forbidden");
6942           /* Otherwise, we're done with the list of declarators.  */
6943           else
6944             {
6945               pop_deferring_access_checks ();
6946               return;
6947             }
6948         }
6949       /* The next token should be either a `,' or a `;'.  */
6950       token = cp_lexer_peek_token (parser->lexer);
6951       /* If it's a `,', there are more declarators to come.  */
6952       if (token->type == CPP_COMMA)
6953         cp_lexer_consume_token (parser->lexer);
6954       /* If it's a `;', we are done.  */
6955       else if (token->type == CPP_SEMICOLON)
6956         break;
6957       /* Anything else is an error.  */
6958       else
6959         {
6960           /* If we have already issued an error message we don't need
6961              to issue another one.  */
6962           if (decl != error_mark_node
6963               || (cp_parser_parsing_tentatively (parser)
6964                   && !cp_parser_committed_to_tentative_parse (parser)))
6965             cp_parser_error (parser, "expected %<,%> or %<;%>");
6966           /* Skip tokens until we reach the end of the statement.  */
6967           cp_parser_skip_to_end_of_statement (parser);
6968           /* If the next token is now a `;', consume it.  */
6969           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
6970             cp_lexer_consume_token (parser->lexer);
6971           goto done;
6972         }
6973       /* After the first time around, a function-definition is not
6974          allowed -- even if it was OK at first.  For example:
6975
6976            int i, f() {}
6977
6978          is not valid.  */
6979       function_definition_allowed_p = false;
6980     }
6981
6982   /* Issue an error message if no declarators are present, and the
6983      decl-specifier-seq does not itself declare a class or
6984      enumeration.  */
6985   if (!saw_declarator)
6986     {
6987       if (cp_parser_declares_only_class_p (parser))
6988         shadow_tag (&decl_specifiers);
6989       /* Perform any deferred access checks.  */
6990       perform_deferred_access_checks ();
6991     }
6992
6993   /* Consume the `;'.  */
6994   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
6995
6996  done:
6997   pop_deferring_access_checks ();
6998 }
6999
7000 /* Parse a decl-specifier-seq.
7001
7002    decl-specifier-seq:
7003      decl-specifier-seq [opt] decl-specifier
7004
7005    decl-specifier:
7006      storage-class-specifier
7007      type-specifier
7008      function-specifier
7009      friend
7010      typedef
7011
7012    GNU Extension:
7013
7014    decl-specifier:
7015      attributes
7016
7017    Set *DECL_SPECS to a representation of the decl-specifier-seq.
7018
7019    The parser flags FLAGS is used to control type-specifier parsing.
7020
7021    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
7022    flags:
7023
7024      1: one of the decl-specifiers is an elaborated-type-specifier
7025         (i.e., a type declaration)
7026      2: one of the decl-specifiers is an enum-specifier or a
7027         class-specifier (i.e., a type definition)
7028
7029    */
7030
7031 static void
7032 cp_parser_decl_specifier_seq (cp_parser* parser,
7033                               cp_parser_flags flags,
7034                               cp_decl_specifier_seq *decl_specs,
7035                               int* declares_class_or_enum)
7036 {
7037   bool constructor_possible_p = !parser->in_declarator_p;
7038
7039   /* Clear DECL_SPECS.  */
7040   clear_decl_specs (decl_specs);
7041
7042   /* Assume no class or enumeration type is declared.  */
7043   *declares_class_or_enum = 0;
7044
7045   /* Keep reading specifiers until there are no more to read.  */
7046   while (true)
7047     {
7048       bool constructor_p;
7049       bool found_decl_spec;
7050       cp_token *token;
7051
7052       /* Peek at the next token.  */
7053       token = cp_lexer_peek_token (parser->lexer);
7054       /* Handle attributes.  */
7055       if (token->keyword == RID_ATTRIBUTE)
7056         {
7057           /* Parse the attributes.  */
7058           decl_specs->attributes
7059             = chainon (decl_specs->attributes,
7060                        cp_parser_attributes_opt (parser));
7061           continue;
7062         }
7063       /* Assume we will find a decl-specifier keyword.  */
7064       found_decl_spec = true;
7065       /* If the next token is an appropriate keyword, we can simply
7066          add it to the list.  */
7067       switch (token->keyword)
7068         {
7069           /* decl-specifier:
7070                friend  */
7071         case RID_FRIEND:
7072           if (decl_specs->specs[(int) ds_friend]++)
7073             error ("duplicate %<friend%>");
7074           /* Consume the token.  */
7075           cp_lexer_consume_token (parser->lexer);
7076           break;
7077
7078           /* function-specifier:
7079                inline
7080                virtual
7081                explicit  */
7082         case RID_INLINE:
7083         case RID_VIRTUAL:
7084         case RID_EXPLICIT:
7085           cp_parser_function_specifier_opt (parser, decl_specs);
7086           break;
7087
7088           /* decl-specifier:
7089                typedef  */
7090         case RID_TYPEDEF:
7091           ++decl_specs->specs[(int) ds_typedef];
7092           /* Consume the token.  */
7093           cp_lexer_consume_token (parser->lexer);
7094           /* A constructor declarator cannot appear in a typedef.  */
7095           constructor_possible_p = false;
7096           /* The "typedef" keyword can only occur in a declaration; we
7097              may as well commit at this point.  */
7098           cp_parser_commit_to_tentative_parse (parser);
7099           break;
7100
7101           /* storage-class-specifier:
7102                auto
7103                register
7104                static
7105                extern
7106                mutable
7107
7108              GNU Extension:
7109                thread  */
7110         case RID_AUTO:
7111           /* Consume the token.  */
7112           cp_lexer_consume_token (parser->lexer);
7113           cp_parser_set_storage_class (decl_specs, sc_auto);
7114           break;
7115         case RID_REGISTER:
7116           /* Consume the token.  */
7117           cp_lexer_consume_token (parser->lexer);
7118           cp_parser_set_storage_class (decl_specs, sc_register);
7119           break;
7120         case RID_STATIC:
7121           /* Consume the token.  */
7122           cp_lexer_consume_token (parser->lexer);
7123           if (decl_specs->specs[(int) ds_thread])
7124             {
7125               error ("%<__thread%> before %<static%>");
7126               decl_specs->specs[(int) ds_thread] = 0;
7127             }
7128           cp_parser_set_storage_class (decl_specs, sc_static);
7129           break;
7130         case RID_EXTERN:
7131           /* Consume the token.  */
7132           cp_lexer_consume_token (parser->lexer);
7133           if (decl_specs->specs[(int) ds_thread])
7134             {
7135               error ("%<__thread%> before %<extern%>");
7136               decl_specs->specs[(int) ds_thread] = 0;
7137             }
7138           cp_parser_set_storage_class (decl_specs, sc_extern);
7139           break;
7140         case RID_MUTABLE:
7141           /* Consume the token.  */
7142           cp_lexer_consume_token (parser->lexer);
7143           cp_parser_set_storage_class (decl_specs, sc_mutable);
7144           break;
7145         case RID_THREAD:
7146           /* Consume the token.  */
7147           cp_lexer_consume_token (parser->lexer);
7148           ++decl_specs->specs[(int) ds_thread];
7149           break;
7150
7151         default:
7152           /* We did not yet find a decl-specifier yet.  */
7153           found_decl_spec = false;
7154           break;
7155         }
7156
7157       /* Constructors are a special case.  The `S' in `S()' is not a
7158          decl-specifier; it is the beginning of the declarator.  */
7159       constructor_p
7160         = (!found_decl_spec
7161            && constructor_possible_p
7162            && (cp_parser_constructor_declarator_p
7163                (parser, decl_specs->specs[(int) ds_friend] != 0)));
7164
7165       /* If we don't have a DECL_SPEC yet, then we must be looking at
7166          a type-specifier.  */
7167       if (!found_decl_spec && !constructor_p)
7168         {
7169           int decl_spec_declares_class_or_enum;
7170           bool is_cv_qualifier;
7171           tree type_spec;
7172
7173           type_spec
7174             = cp_parser_type_specifier (parser, flags,
7175                                         decl_specs,
7176                                         /*is_declaration=*/true,
7177                                         &decl_spec_declares_class_or_enum,
7178                                         &is_cv_qualifier);
7179
7180           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
7181
7182           /* If this type-specifier referenced a user-defined type
7183              (a typedef, class-name, etc.), then we can't allow any
7184              more such type-specifiers henceforth.
7185
7186              [dcl.spec]
7187
7188              The longest sequence of decl-specifiers that could
7189              possibly be a type name is taken as the
7190              decl-specifier-seq of a declaration.  The sequence shall
7191              be self-consistent as described below.
7192
7193              [dcl.type]
7194
7195              As a general rule, at most one type-specifier is allowed
7196              in the complete decl-specifier-seq of a declaration.  The
7197              only exceptions are the following:
7198
7199              -- const or volatile can be combined with any other
7200                 type-specifier.
7201
7202              -- signed or unsigned can be combined with char, long,
7203                 short, or int.
7204
7205              -- ..
7206
7207              Example:
7208
7209                typedef char* Pc;
7210                void g (const int Pc);
7211
7212              Here, Pc is *not* part of the decl-specifier seq; it's
7213              the declarator.  Therefore, once we see a type-specifier
7214              (other than a cv-qualifier), we forbid any additional
7215              user-defined types.  We *do* still allow things like `int
7216              int' to be considered a decl-specifier-seq, and issue the
7217              error message later.  */
7218           if (type_spec && !is_cv_qualifier)
7219             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
7220           /* A constructor declarator cannot follow a type-specifier.  */
7221           if (type_spec)
7222             {
7223               constructor_possible_p = false;
7224               found_decl_spec = true;
7225             }
7226         }
7227
7228       /* If we still do not have a DECL_SPEC, then there are no more
7229          decl-specifiers.  */
7230       if (!found_decl_spec)
7231         break;
7232
7233       decl_specs->any_specifiers_p = true;
7234       /* After we see one decl-specifier, further decl-specifiers are
7235          always optional.  */
7236       flags |= CP_PARSER_FLAGS_OPTIONAL;
7237     }
7238
7239   /* Don't allow a friend specifier with a class definition.  */
7240   if (decl_specs->specs[(int) ds_friend] != 0
7241       && (*declares_class_or_enum & 2))
7242     error ("class definition may not be declared a friend");
7243 }
7244
7245 /* Parse an (optional) storage-class-specifier.
7246
7247    storage-class-specifier:
7248      auto
7249      register
7250      static
7251      extern
7252      mutable
7253
7254    GNU Extension:
7255
7256    storage-class-specifier:
7257      thread
7258
7259    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
7260
7261 static tree
7262 cp_parser_storage_class_specifier_opt (cp_parser* parser)
7263 {
7264   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7265     {
7266     case RID_AUTO:
7267     case RID_REGISTER:
7268     case RID_STATIC:
7269     case RID_EXTERN:
7270     case RID_MUTABLE:
7271     case RID_THREAD:
7272       /* Consume the token.  */
7273       return cp_lexer_consume_token (parser->lexer)->value;
7274
7275     default:
7276       return NULL_TREE;
7277     }
7278 }
7279
7280 /* Parse an (optional) function-specifier.
7281
7282    function-specifier:
7283      inline
7284      virtual
7285      explicit
7286
7287    Returns an IDENTIFIER_NODE corresponding to the keyword used.
7288    Updates DECL_SPECS, if it is non-NULL.  */
7289
7290 static tree
7291 cp_parser_function_specifier_opt (cp_parser* parser,
7292                                   cp_decl_specifier_seq *decl_specs)
7293 {
7294   switch (cp_lexer_peek_token (parser->lexer)->keyword)
7295     {
7296     case RID_INLINE:
7297       if (decl_specs)
7298         ++decl_specs->specs[(int) ds_inline];
7299       break;
7300
7301     case RID_VIRTUAL:
7302       if (decl_specs)
7303         ++decl_specs->specs[(int) ds_virtual];
7304       break;
7305
7306     case RID_EXPLICIT:
7307       if (decl_specs)
7308         ++decl_specs->specs[(int) ds_explicit];
7309       break;
7310
7311     default:
7312       return NULL_TREE;
7313     }
7314
7315   /* Consume the token.  */
7316   return cp_lexer_consume_token (parser->lexer)->value;
7317 }
7318
7319 /* Parse a linkage-specification.
7320
7321    linkage-specification:
7322      extern string-literal { declaration-seq [opt] }
7323      extern string-literal declaration  */
7324
7325 static void
7326 cp_parser_linkage_specification (cp_parser* parser)
7327 {
7328   tree linkage;
7329
7330   /* Look for the `extern' keyword.  */
7331   cp_parser_require_keyword (parser, RID_EXTERN, "`extern'");
7332
7333   /* Look for the string-literal.  */
7334   linkage = cp_parser_string_literal (parser, false, false);
7335
7336   /* Transform the literal into an identifier.  If the literal is a
7337      wide-character string, or contains embedded NULs, then we can't
7338      handle it as the user wants.  */
7339   if (strlen (TREE_STRING_POINTER (linkage))
7340       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
7341     {
7342       cp_parser_error (parser, "invalid linkage-specification");
7343       /* Assume C++ linkage.  */
7344       linkage = lang_name_cplusplus;
7345     }
7346   else
7347     linkage = get_identifier (TREE_STRING_POINTER (linkage));
7348
7349   /* We're now using the new linkage.  */
7350   push_lang_context (linkage);
7351
7352   /* If the next token is a `{', then we're using the first
7353      production.  */
7354   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
7355     {
7356       /* Consume the `{' token.  */
7357       cp_lexer_consume_token (parser->lexer);
7358       /* Parse the declarations.  */
7359       cp_parser_declaration_seq_opt (parser);
7360       /* Look for the closing `}'.  */
7361       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
7362     }
7363   /* Otherwise, there's just one declaration.  */
7364   else
7365     {
7366       bool saved_in_unbraced_linkage_specification_p;
7367
7368       saved_in_unbraced_linkage_specification_p
7369         = parser->in_unbraced_linkage_specification_p;
7370       parser->in_unbraced_linkage_specification_p = true;
7371       have_extern_spec = true;
7372       cp_parser_declaration (parser);
7373       have_extern_spec = false;
7374       parser->in_unbraced_linkage_specification_p
7375         = saved_in_unbraced_linkage_specification_p;
7376     }
7377
7378   /* We're done with the linkage-specification.  */
7379   pop_lang_context ();
7380 }
7381
7382 /* Special member functions [gram.special] */
7383
7384 /* Parse a conversion-function-id.
7385
7386    conversion-function-id:
7387      operator conversion-type-id
7388
7389    Returns an IDENTIFIER_NODE representing the operator.  */
7390
7391 static tree
7392 cp_parser_conversion_function_id (cp_parser* parser)
7393 {
7394   tree type;
7395   tree saved_scope;
7396   tree saved_qualifying_scope;
7397   tree saved_object_scope;
7398   bool pop_p = false;
7399
7400   /* Look for the `operator' token.  */
7401   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7402     return error_mark_node;
7403   /* When we parse the conversion-type-id, the current scope will be
7404      reset.  However, we need that information in able to look up the
7405      conversion function later, so we save it here.  */
7406   saved_scope = parser->scope;
7407   saved_qualifying_scope = parser->qualifying_scope;
7408   saved_object_scope = parser->object_scope;
7409   /* We must enter the scope of the class so that the names of
7410      entities declared within the class are available in the
7411      conversion-type-id.  For example, consider:
7412
7413        struct S {
7414          typedef int I;
7415          operator I();
7416        };
7417
7418        S::operator I() { ... }
7419
7420      In order to see that `I' is a type-name in the definition, we
7421      must be in the scope of `S'.  */
7422   if (saved_scope)
7423     pop_p = push_scope (saved_scope);
7424   /* Parse the conversion-type-id.  */
7425   type = cp_parser_conversion_type_id (parser);
7426   /* Leave the scope of the class, if any.  */
7427   if (pop_p)
7428     pop_scope (saved_scope);
7429   /* Restore the saved scope.  */
7430   parser->scope = saved_scope;
7431   parser->qualifying_scope = saved_qualifying_scope;
7432   parser->object_scope = saved_object_scope;
7433   /* If the TYPE is invalid, indicate failure.  */
7434   if (type == error_mark_node)
7435     return error_mark_node;
7436   return mangle_conv_op_name_for_type (type);
7437 }
7438
7439 /* Parse a conversion-type-id:
7440
7441    conversion-type-id:
7442      type-specifier-seq conversion-declarator [opt]
7443
7444    Returns the TYPE specified.  */
7445
7446 static tree
7447 cp_parser_conversion_type_id (cp_parser* parser)
7448 {
7449   tree attributes;
7450   cp_decl_specifier_seq type_specifiers;
7451   cp_declarator *declarator;
7452   tree type_specified;
7453
7454   /* Parse the attributes.  */
7455   attributes = cp_parser_attributes_opt (parser);
7456   /* Parse the type-specifiers.  */
7457   cp_parser_type_specifier_seq (parser, &type_specifiers);
7458   /* If that didn't work, stop.  */
7459   if (type_specifiers.type == error_mark_node)
7460     return error_mark_node;
7461   /* Parse the conversion-declarator.  */
7462   declarator = cp_parser_conversion_declarator_opt (parser);
7463
7464   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
7465                                     /*initialized=*/0, &attributes);
7466   if (attributes)
7467     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
7468   return type_specified;
7469 }
7470
7471 /* Parse an (optional) conversion-declarator.
7472
7473    conversion-declarator:
7474      ptr-operator conversion-declarator [opt]
7475
7476    */
7477
7478 static cp_declarator *
7479 cp_parser_conversion_declarator_opt (cp_parser* parser)
7480 {
7481   enum tree_code code;
7482   tree class_type;
7483   cp_cv_quals cv_quals;
7484
7485   /* We don't know if there's a ptr-operator next, or not.  */
7486   cp_parser_parse_tentatively (parser);
7487   /* Try the ptr-operator.  */
7488   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
7489   /* If it worked, look for more conversion-declarators.  */
7490   if (cp_parser_parse_definitely (parser))
7491     {
7492       cp_declarator *declarator;
7493
7494       /* Parse another optional declarator.  */
7495       declarator = cp_parser_conversion_declarator_opt (parser);
7496
7497       /* Create the representation of the declarator.  */
7498       if (class_type)
7499         declarator = make_ptrmem_declarator (cv_quals, class_type,
7500                                              declarator);
7501       else if (code == INDIRECT_REF)
7502         declarator = make_pointer_declarator (cv_quals, declarator);
7503       else
7504         declarator = make_reference_declarator (cv_quals, declarator);
7505
7506       return declarator;
7507    }
7508
7509   return NULL;
7510 }
7511
7512 /* Parse an (optional) ctor-initializer.
7513
7514    ctor-initializer:
7515      : mem-initializer-list
7516
7517    Returns TRUE iff the ctor-initializer was actually present.  */
7518
7519 static bool
7520 cp_parser_ctor_initializer_opt (cp_parser* parser)
7521 {
7522   /* If the next token is not a `:', then there is no
7523      ctor-initializer.  */
7524   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
7525     {
7526       /* Do default initialization of any bases and members.  */
7527       if (DECL_CONSTRUCTOR_P (current_function_decl))
7528         finish_mem_initializers (NULL_TREE);
7529
7530       return false;
7531     }
7532
7533   /* Consume the `:' token.  */
7534   cp_lexer_consume_token (parser->lexer);
7535   /* And the mem-initializer-list.  */
7536   cp_parser_mem_initializer_list (parser);
7537
7538   return true;
7539 }
7540
7541 /* Parse a mem-initializer-list.
7542
7543    mem-initializer-list:
7544      mem-initializer
7545      mem-initializer , mem-initializer-list  */
7546
7547 static void
7548 cp_parser_mem_initializer_list (cp_parser* parser)
7549 {
7550   tree mem_initializer_list = NULL_TREE;
7551
7552   /* Let the semantic analysis code know that we are starting the
7553      mem-initializer-list.  */
7554   if (!DECL_CONSTRUCTOR_P (current_function_decl))
7555     error ("only constructors take base initializers");
7556
7557   /* Loop through the list.  */
7558   while (true)
7559     {
7560       tree mem_initializer;
7561
7562       /* Parse the mem-initializer.  */
7563       mem_initializer = cp_parser_mem_initializer (parser);
7564       /* Add it to the list, unless it was erroneous.  */
7565       if (mem_initializer)
7566         {
7567           TREE_CHAIN (mem_initializer) = mem_initializer_list;
7568           mem_initializer_list = mem_initializer;
7569         }
7570       /* If the next token is not a `,', we're done.  */
7571       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7572         break;
7573       /* Consume the `,' token.  */
7574       cp_lexer_consume_token (parser->lexer);
7575     }
7576
7577   /* Perform semantic analysis.  */
7578   if (DECL_CONSTRUCTOR_P (current_function_decl))
7579     finish_mem_initializers (mem_initializer_list);
7580 }
7581
7582 /* Parse a mem-initializer.
7583
7584    mem-initializer:
7585      mem-initializer-id ( expression-list [opt] )
7586
7587    GNU extension:
7588
7589    mem-initializer:
7590      ( expression-list [opt] )
7591
7592    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
7593    class) or FIELD_DECL (for a non-static data member) to initialize;
7594    the TREE_VALUE is the expression-list.  */
7595
7596 static tree
7597 cp_parser_mem_initializer (cp_parser* parser)
7598 {
7599   tree mem_initializer_id;
7600   tree expression_list;
7601   tree member;
7602
7603   /* Find out what is being initialized.  */
7604   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7605     {
7606       pedwarn ("anachronistic old-style base class initializer");
7607       mem_initializer_id = NULL_TREE;
7608     }
7609   else
7610     mem_initializer_id = cp_parser_mem_initializer_id (parser);
7611   member = expand_member_init (mem_initializer_id);
7612   if (member && !DECL_P (member))
7613     in_base_initializer = 1;
7614
7615   expression_list
7616     = cp_parser_parenthesized_expression_list (parser, false,
7617                                                /*non_constant_p=*/NULL);
7618   if (!expression_list)
7619     expression_list = void_type_node;
7620
7621   in_base_initializer = 0;
7622
7623   return member ? build_tree_list (member, expression_list) : NULL_TREE;
7624 }
7625
7626 /* Parse a mem-initializer-id.
7627
7628    mem-initializer-id:
7629      :: [opt] nested-name-specifier [opt] class-name
7630      identifier
7631
7632    Returns a TYPE indicating the class to be initializer for the first
7633    production.  Returns an IDENTIFIER_NODE indicating the data member
7634    to be initialized for the second production.  */
7635
7636 static tree
7637 cp_parser_mem_initializer_id (cp_parser* parser)
7638 {
7639   bool global_scope_p;
7640   bool nested_name_specifier_p;
7641   bool template_p = false;
7642   tree id;
7643
7644   /* `typename' is not allowed in this context ([temp.res]).  */
7645   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
7646     {
7647       error ("keyword %<typename%> not allowed in this context (a qualified "
7648              "member initializer is implicitly a type)");
7649       cp_lexer_consume_token (parser->lexer);
7650     }
7651   /* Look for the optional `::' operator.  */
7652   global_scope_p
7653     = (cp_parser_global_scope_opt (parser,
7654                                    /*current_scope_valid_p=*/false)
7655        != NULL_TREE);
7656   /* Look for the optional nested-name-specifier.  The simplest way to
7657      implement:
7658
7659        [temp.res]
7660
7661        The keyword `typename' is not permitted in a base-specifier or
7662        mem-initializer; in these contexts a qualified name that
7663        depends on a template-parameter is implicitly assumed to be a
7664        type name.
7665
7666      is to assume that we have seen the `typename' keyword at this
7667      point.  */
7668   nested_name_specifier_p
7669     = (cp_parser_nested_name_specifier_opt (parser,
7670                                             /*typename_keyword_p=*/true,
7671                                             /*check_dependency_p=*/true,
7672                                             /*type_p=*/true,
7673                                             /*is_declaration=*/true)
7674        != NULL_TREE);
7675   if (nested_name_specifier_p)
7676     template_p = cp_parser_optional_template_keyword (parser);
7677   /* If there is a `::' operator or a nested-name-specifier, then we
7678      are definitely looking for a class-name.  */
7679   if (global_scope_p || nested_name_specifier_p)
7680     return cp_parser_class_name (parser,
7681                                  /*typename_keyword_p=*/true,
7682                                  /*template_keyword_p=*/template_p,
7683                                  /*type_p=*/false,
7684                                  /*check_dependency_p=*/true,
7685                                  /*class_head_p=*/false,
7686                                  /*is_declaration=*/true);
7687   /* Otherwise, we could also be looking for an ordinary identifier.  */
7688   cp_parser_parse_tentatively (parser);
7689   /* Try a class-name.  */
7690   id = cp_parser_class_name (parser,
7691                              /*typename_keyword_p=*/true,
7692                              /*template_keyword_p=*/false,
7693                              /*type_p=*/false,
7694                              /*check_dependency_p=*/true,
7695                              /*class_head_p=*/false,
7696                              /*is_declaration=*/true);
7697   /* If we found one, we're done.  */
7698   if (cp_parser_parse_definitely (parser))
7699     return id;
7700   /* Otherwise, look for an ordinary identifier.  */
7701   return cp_parser_identifier (parser);
7702 }
7703
7704 /* Overloading [gram.over] */
7705
7706 /* Parse an operator-function-id.
7707
7708    operator-function-id:
7709      operator operator
7710
7711    Returns an IDENTIFIER_NODE for the operator which is a
7712    human-readable spelling of the identifier, e.g., `operator +'.  */
7713
7714 static tree
7715 cp_parser_operator_function_id (cp_parser* parser)
7716 {
7717   /* Look for the `operator' keyword.  */
7718   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "`operator'"))
7719     return error_mark_node;
7720   /* And then the name of the operator itself.  */
7721   return cp_parser_operator (parser);
7722 }
7723
7724 /* Parse an operator.
7725
7726    operator:
7727      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
7728      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
7729      || ++ -- , ->* -> () []
7730
7731    GNU Extensions:
7732
7733    operator:
7734      <? >? <?= >?=
7735
7736    Returns an IDENTIFIER_NODE for the operator which is a
7737    human-readable spelling of the identifier, e.g., `operator +'.  */
7738
7739 static tree
7740 cp_parser_operator (cp_parser* parser)
7741 {
7742   tree id = NULL_TREE;
7743   cp_token *token;
7744
7745   /* Peek at the next token.  */
7746   token = cp_lexer_peek_token (parser->lexer);
7747   /* Figure out which operator we have.  */
7748   switch (token->type)
7749     {
7750     case CPP_KEYWORD:
7751       {
7752         enum tree_code op;
7753
7754         /* The keyword should be either `new' or `delete'.  */
7755         if (token->keyword == RID_NEW)
7756           op = NEW_EXPR;
7757         else if (token->keyword == RID_DELETE)
7758           op = DELETE_EXPR;
7759         else
7760           break;
7761
7762         /* Consume the `new' or `delete' token.  */
7763         cp_lexer_consume_token (parser->lexer);
7764
7765         /* Peek at the next token.  */
7766         token = cp_lexer_peek_token (parser->lexer);
7767         /* If it's a `[' token then this is the array variant of the
7768            operator.  */
7769         if (token->type == CPP_OPEN_SQUARE)
7770           {
7771             /* Consume the `[' token.  */
7772             cp_lexer_consume_token (parser->lexer);
7773             /* Look for the `]' token.  */
7774             cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7775             id = ansi_opname (op == NEW_EXPR
7776                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
7777           }
7778         /* Otherwise, we have the non-array variant.  */
7779         else
7780           id = ansi_opname (op);
7781
7782         return id;
7783       }
7784
7785     case CPP_PLUS:
7786       id = ansi_opname (PLUS_EXPR);
7787       break;
7788
7789     case CPP_MINUS:
7790       id = ansi_opname (MINUS_EXPR);
7791       break;
7792
7793     case CPP_MULT:
7794       id = ansi_opname (MULT_EXPR);
7795       break;
7796
7797     case CPP_DIV:
7798       id = ansi_opname (TRUNC_DIV_EXPR);
7799       break;
7800
7801     case CPP_MOD:
7802       id = ansi_opname (TRUNC_MOD_EXPR);
7803       break;
7804
7805     case CPP_XOR:
7806       id = ansi_opname (BIT_XOR_EXPR);
7807       break;
7808
7809     case CPP_AND:
7810       id = ansi_opname (BIT_AND_EXPR);
7811       break;
7812
7813     case CPP_OR:
7814       id = ansi_opname (BIT_IOR_EXPR);
7815       break;
7816
7817     case CPP_COMPL:
7818       id = ansi_opname (BIT_NOT_EXPR);
7819       break;
7820
7821     case CPP_NOT:
7822       id = ansi_opname (TRUTH_NOT_EXPR);
7823       break;
7824
7825     case CPP_EQ:
7826       id = ansi_assopname (NOP_EXPR);
7827       break;
7828
7829     case CPP_LESS:
7830       id = ansi_opname (LT_EXPR);
7831       break;
7832
7833     case CPP_GREATER:
7834       id = ansi_opname (GT_EXPR);
7835       break;
7836
7837     case CPP_PLUS_EQ:
7838       id = ansi_assopname (PLUS_EXPR);
7839       break;
7840
7841     case CPP_MINUS_EQ:
7842       id = ansi_assopname (MINUS_EXPR);
7843       break;
7844
7845     case CPP_MULT_EQ:
7846       id = ansi_assopname (MULT_EXPR);
7847       break;
7848
7849     case CPP_DIV_EQ:
7850       id = ansi_assopname (TRUNC_DIV_EXPR);
7851       break;
7852
7853     case CPP_MOD_EQ:
7854       id = ansi_assopname (TRUNC_MOD_EXPR);
7855       break;
7856
7857     case CPP_XOR_EQ:
7858       id = ansi_assopname (BIT_XOR_EXPR);
7859       break;
7860
7861     case CPP_AND_EQ:
7862       id = ansi_assopname (BIT_AND_EXPR);
7863       break;
7864
7865     case CPP_OR_EQ:
7866       id = ansi_assopname (BIT_IOR_EXPR);
7867       break;
7868
7869     case CPP_LSHIFT:
7870       id = ansi_opname (LSHIFT_EXPR);
7871       break;
7872
7873     case CPP_RSHIFT:
7874       id = ansi_opname (RSHIFT_EXPR);
7875       break;
7876
7877     case CPP_LSHIFT_EQ:
7878       id = ansi_assopname (LSHIFT_EXPR);
7879       break;
7880
7881     case CPP_RSHIFT_EQ:
7882       id = ansi_assopname (RSHIFT_EXPR);
7883       break;
7884
7885     case CPP_EQ_EQ:
7886       id = ansi_opname (EQ_EXPR);
7887       break;
7888
7889     case CPP_NOT_EQ:
7890       id = ansi_opname (NE_EXPR);
7891       break;
7892
7893     case CPP_LESS_EQ:
7894       id = ansi_opname (LE_EXPR);
7895       break;
7896
7897     case CPP_GREATER_EQ:
7898       id = ansi_opname (GE_EXPR);
7899       break;
7900
7901     case CPP_AND_AND:
7902       id = ansi_opname (TRUTH_ANDIF_EXPR);
7903       break;
7904
7905     case CPP_OR_OR:
7906       id = ansi_opname (TRUTH_ORIF_EXPR);
7907       break;
7908
7909     case CPP_PLUS_PLUS:
7910       id = ansi_opname (POSTINCREMENT_EXPR);
7911       break;
7912
7913     case CPP_MINUS_MINUS:
7914       id = ansi_opname (PREDECREMENT_EXPR);
7915       break;
7916
7917     case CPP_COMMA:
7918       id = ansi_opname (COMPOUND_EXPR);
7919       break;
7920
7921     case CPP_DEREF_STAR:
7922       id = ansi_opname (MEMBER_REF);
7923       break;
7924
7925     case CPP_DEREF:
7926       id = ansi_opname (COMPONENT_REF);
7927       break;
7928
7929     case CPP_OPEN_PAREN:
7930       /* Consume the `('.  */
7931       cp_lexer_consume_token (parser->lexer);
7932       /* Look for the matching `)'.  */
7933       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
7934       return ansi_opname (CALL_EXPR);
7935
7936     case CPP_OPEN_SQUARE:
7937       /* Consume the `['.  */
7938       cp_lexer_consume_token (parser->lexer);
7939       /* Look for the matching `]'.  */
7940       cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
7941       return ansi_opname (ARRAY_REF);
7942
7943       /* Extensions.  */
7944     case CPP_MIN:
7945       id = ansi_opname (MIN_EXPR);
7946       break;
7947
7948     case CPP_MAX:
7949       id = ansi_opname (MAX_EXPR);
7950       break;
7951
7952     case CPP_MIN_EQ:
7953       id = ansi_assopname (MIN_EXPR);
7954       break;
7955
7956     case CPP_MAX_EQ:
7957       id = ansi_assopname (MAX_EXPR);
7958       break;
7959
7960     default:
7961       /* Anything else is an error.  */
7962       break;
7963     }
7964
7965   /* If we have selected an identifier, we need to consume the
7966      operator token.  */
7967   if (id)
7968     cp_lexer_consume_token (parser->lexer);
7969   /* Otherwise, no valid operator name was present.  */
7970   else
7971     {
7972       cp_parser_error (parser, "expected operator");
7973       id = error_mark_node;
7974     }
7975
7976   return id;
7977 }
7978
7979 /* Parse a template-declaration.
7980
7981    template-declaration:
7982      export [opt] template < template-parameter-list > declaration
7983
7984    If MEMBER_P is TRUE, this template-declaration occurs within a
7985    class-specifier.
7986
7987    The grammar rule given by the standard isn't correct.  What
7988    is really meant is:
7989
7990    template-declaration:
7991      export [opt] template-parameter-list-seq
7992        decl-specifier-seq [opt] init-declarator [opt] ;
7993      export [opt] template-parameter-list-seq
7994        function-definition
7995
7996    template-parameter-list-seq:
7997      template-parameter-list-seq [opt]
7998      template < template-parameter-list >  */
7999
8000 static void
8001 cp_parser_template_declaration (cp_parser* parser, bool member_p)
8002 {
8003   /* Check for `export'.  */
8004   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
8005     {
8006       /* Consume the `export' token.  */
8007       cp_lexer_consume_token (parser->lexer);
8008       /* Warn that we do not support `export'.  */
8009       warning ("keyword %<export%> not implemented, and will be ignored");
8010     }
8011
8012   cp_parser_template_declaration_after_export (parser, member_p);
8013 }
8014
8015 /* Parse a template-parameter-list.
8016
8017    template-parameter-list:
8018      template-parameter
8019      template-parameter-list , template-parameter
8020
8021    Returns a TREE_LIST.  Each node represents a template parameter.
8022    The nodes are connected via their TREE_CHAINs.  */
8023
8024 static tree
8025 cp_parser_template_parameter_list (cp_parser* parser)
8026 {
8027   tree parameter_list = NULL_TREE;
8028
8029   while (true)
8030     {
8031       tree parameter;
8032       cp_token *token;
8033       bool is_non_type;
8034
8035       /* Parse the template-parameter.  */
8036       parameter = cp_parser_template_parameter (parser, &is_non_type);
8037       /* Add it to the list.  */
8038       parameter_list = process_template_parm (parameter_list,
8039                                               parameter,
8040                                               is_non_type);
8041       /* Peek at the next token.  */
8042       token = cp_lexer_peek_token (parser->lexer);
8043       /* If it's not a `,', we're done.  */
8044       if (token->type != CPP_COMMA)
8045         break;
8046       /* Otherwise, consume the `,' token.  */
8047       cp_lexer_consume_token (parser->lexer);
8048     }
8049
8050   return parameter_list;
8051 }
8052
8053 /* Parse a template-parameter.
8054
8055    template-parameter:
8056      type-parameter
8057      parameter-declaration
8058
8059    Returns a TREE_LIST.  The TREE_VALUE represents the parameter.  The
8060    TREE_PURPOSE is the default value, if any.  *IS_NON_TYPE is set to
8061    true iff this parameter is a non-type parameter.  */
8062
8063 static tree
8064 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type)
8065 {
8066   cp_token *token;
8067   cp_parameter_declarator *parameter_declarator;
8068
8069   /* Assume it is a type parameter or a template parameter.  */
8070   *is_non_type = false;
8071   /* Peek at the next token.  */
8072   token = cp_lexer_peek_token (parser->lexer);
8073   /* If it is `class' or `template', we have a type-parameter.  */
8074   if (token->keyword == RID_TEMPLATE)
8075     return cp_parser_type_parameter (parser);
8076   /* If it is `class' or `typename' we do not know yet whether it is a
8077      type parameter or a non-type parameter.  Consider:
8078
8079        template <typename T, typename T::X X> ...
8080
8081      or:
8082
8083        template <class C, class D*> ...
8084
8085      Here, the first parameter is a type parameter, and the second is
8086      a non-type parameter.  We can tell by looking at the token after
8087      the identifier -- if it is a `,', `=', or `>' then we have a type
8088      parameter.  */
8089   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
8090     {
8091       /* Peek at the token after `class' or `typename'.  */
8092       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8093       /* If it's an identifier, skip it.  */
8094       if (token->type == CPP_NAME)
8095         token = cp_lexer_peek_nth_token (parser->lexer, 3);
8096       /* Now, see if the token looks like the end of a template
8097          parameter.  */
8098       if (token->type == CPP_COMMA
8099           || token->type == CPP_EQ
8100           || token->type == CPP_GREATER)
8101         return cp_parser_type_parameter (parser);
8102     }
8103
8104   /* Otherwise, it is a non-type parameter.
8105
8106      [temp.param]
8107
8108      When parsing a default template-argument for a non-type
8109      template-parameter, the first non-nested `>' is taken as the end
8110      of the template parameter-list rather than a greater-than
8111      operator.  */
8112   *is_non_type = true;
8113   parameter_declarator
8114      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
8115                                         /*parenthesized_p=*/NULL);
8116   return (build_tree_list
8117           (parameter_declarator->default_argument,
8118            grokdeclarator (parameter_declarator->declarator,
8119                            &parameter_declarator->decl_specifiers,
8120                            PARM, /*initialized=*/0,
8121                            /*attrlist=*/NULL)));
8122 }
8123
8124 /* Parse a type-parameter.
8125
8126    type-parameter:
8127      class identifier [opt]
8128      class identifier [opt] = type-id
8129      typename identifier [opt]
8130      typename identifier [opt] = type-id
8131      template < template-parameter-list > class identifier [opt]
8132      template < template-parameter-list > class identifier [opt]
8133        = id-expression
8134
8135    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
8136    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
8137    the declaration of the parameter.  */
8138
8139 static tree
8140 cp_parser_type_parameter (cp_parser* parser)
8141 {
8142   cp_token *token;
8143   tree parameter;
8144
8145   /* Look for a keyword to tell us what kind of parameter this is.  */
8146   token = cp_parser_require (parser, CPP_KEYWORD,
8147                              "`class', `typename', or `template'");
8148   if (!token)
8149     return error_mark_node;
8150
8151   switch (token->keyword)
8152     {
8153     case RID_CLASS:
8154     case RID_TYPENAME:
8155       {
8156         tree identifier;
8157         tree default_argument;
8158
8159         /* If the next token is an identifier, then it names the
8160            parameter.  */
8161         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8162           identifier = cp_parser_identifier (parser);
8163         else
8164           identifier = NULL_TREE;
8165
8166         /* Create the parameter.  */
8167         parameter = finish_template_type_parm (class_type_node, identifier);
8168
8169         /* If the next token is an `=', we have a default argument.  */
8170         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8171           {
8172             /* Consume the `=' token.  */
8173             cp_lexer_consume_token (parser->lexer);
8174             /* Parse the default-argument.  */
8175             default_argument = cp_parser_type_id (parser);
8176           }
8177         else
8178           default_argument = NULL_TREE;
8179
8180         /* Create the combined representation of the parameter and the
8181            default argument.  */
8182         parameter = build_tree_list (default_argument, parameter);
8183       }
8184       break;
8185
8186     case RID_TEMPLATE:
8187       {
8188         tree parameter_list;
8189         tree identifier;
8190         tree default_argument;
8191
8192         /* Look for the `<'.  */
8193         cp_parser_require (parser, CPP_LESS, "`<'");
8194         /* Parse the template-parameter-list.  */
8195         begin_template_parm_list ();
8196         parameter_list
8197           = cp_parser_template_parameter_list (parser);
8198         parameter_list = end_template_parm_list (parameter_list);
8199         /* Look for the `>'.  */
8200         cp_parser_require (parser, CPP_GREATER, "`>'");
8201         /* Look for the `class' keyword.  */
8202         cp_parser_require_keyword (parser, RID_CLASS, "`class'");
8203         /* If the next token is an `=', then there is a
8204            default-argument.  If the next token is a `>', we are at
8205            the end of the parameter-list.  If the next token is a `,',
8206            then we are at the end of this parameter.  */
8207         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8208             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
8209             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
8210           identifier = cp_parser_identifier (parser);
8211         else
8212           identifier = NULL_TREE;
8213         /* Create the template parameter.  */
8214         parameter = finish_template_template_parm (class_type_node,
8215                                                    identifier);
8216
8217         /* If the next token is an `=', then there is a
8218            default-argument.  */
8219         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8220           {
8221             bool is_template;
8222
8223             /* Consume the `='.  */
8224             cp_lexer_consume_token (parser->lexer);
8225             /* Parse the id-expression.  */
8226             default_argument
8227               = cp_parser_id_expression (parser,
8228                                          /*template_keyword_p=*/false,
8229                                          /*check_dependency_p=*/true,
8230                                          /*template_p=*/&is_template,
8231                                          /*declarator_p=*/false);
8232             if (TREE_CODE (default_argument) == TYPE_DECL)
8233               /* If the id-expression was a template-id that refers to
8234                  a template-class, we already have the declaration here,
8235                  so no further lookup is needed.  */
8236                  ;
8237             else
8238               /* Look up the name.  */
8239               default_argument
8240                 = cp_parser_lookup_name (parser, default_argument,
8241                                         /*is_type=*/false,
8242                                         /*is_template=*/is_template,
8243                                         /*is_namespace=*/false,
8244                                         /*check_dependency=*/true,
8245                                         /*ambiguous_p=*/NULL);
8246             /* See if the default argument is valid.  */
8247             default_argument
8248               = check_template_template_default_arg (default_argument);
8249           }
8250         else
8251           default_argument = NULL_TREE;
8252
8253         /* Create the combined representation of the parameter and the
8254            default argument.  */
8255         parameter =  build_tree_list (default_argument, parameter);
8256       }
8257       break;
8258
8259     default:
8260       /* Anything else is an error.  */
8261       cp_parser_error (parser,
8262                        "expected %<class%>, %<typename%>, or %<template%>");
8263       parameter = error_mark_node;
8264     }
8265
8266   return parameter;
8267 }
8268
8269 /* Parse a template-id.
8270
8271    template-id:
8272      template-name < template-argument-list [opt] >
8273
8274    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
8275    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
8276    returned.  Otherwise, if the template-name names a function, or set
8277    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
8278    names a class, returns a TYPE_DECL for the specialization.
8279
8280    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
8281    uninstantiated templates.  */
8282
8283 static tree
8284 cp_parser_template_id (cp_parser *parser,
8285                        bool template_keyword_p,
8286                        bool check_dependency_p,
8287                        bool is_declaration)
8288 {
8289   tree template;
8290   tree arguments;
8291   tree template_id;
8292   cp_token_position start_of_id = 0;
8293   tree access_check = NULL_TREE;
8294   cp_token *next_token, *next_token_2;
8295   bool is_identifier;
8296
8297   /* If the next token corresponds to a template-id, there is no need
8298      to reparse it.  */
8299   next_token = cp_lexer_peek_token (parser->lexer);
8300   if (next_token->type == CPP_TEMPLATE_ID)
8301     {
8302       tree value;
8303       tree check;
8304
8305       /* Get the stored value.  */
8306       value = cp_lexer_consume_token (parser->lexer)->value;
8307       /* Perform any access checks that were deferred.  */
8308       for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
8309         perform_or_defer_access_check (TREE_PURPOSE (check),
8310                                        TREE_VALUE (check));
8311       /* Return the stored value.  */
8312       return TREE_VALUE (value);
8313     }
8314
8315   /* Avoid performing name lookup if there is no possibility of
8316      finding a template-id.  */
8317   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
8318       || (next_token->type == CPP_NAME
8319           && !cp_parser_nth_token_starts_template_argument_list_p
8320                (parser, 2)))
8321     {
8322       cp_parser_error (parser, "expected template-id");
8323       return error_mark_node;
8324     }
8325
8326   /* Remember where the template-id starts.  */
8327   if (cp_parser_parsing_tentatively (parser)
8328       && !cp_parser_committed_to_tentative_parse (parser))
8329     start_of_id = cp_lexer_token_position (parser->lexer, false);
8330
8331   push_deferring_access_checks (dk_deferred);
8332
8333   /* Parse the template-name.  */
8334   is_identifier = false;
8335   template = cp_parser_template_name (parser, template_keyword_p,
8336                                       check_dependency_p,
8337                                       is_declaration,
8338                                       &is_identifier);
8339   if (template == error_mark_node || is_identifier)
8340     {
8341       pop_deferring_access_checks ();
8342       return template;
8343     }
8344
8345   /* If we find the sequence `[:' after a template-name, it's probably
8346      a digraph-typo for `< ::'. Substitute the tokens and check if we can
8347      parse correctly the argument list.  */
8348   next_token = cp_lexer_peek_token (parser->lexer);
8349   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8350   if (next_token->type == CPP_OPEN_SQUARE
8351       && next_token->flags & DIGRAPH
8352       && next_token_2->type == CPP_COLON
8353       && !(next_token_2->flags & PREV_WHITE))
8354     {
8355       cp_parser_parse_tentatively (parser);
8356       /* Change `:' into `::'.  */
8357       next_token_2->type = CPP_SCOPE;
8358       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
8359          CPP_LESS.  */
8360       cp_lexer_consume_token (parser->lexer);
8361       /* Parse the arguments.  */
8362       arguments = cp_parser_enclosed_template_argument_list (parser);
8363       if (!cp_parser_parse_definitely (parser))
8364         {
8365           /* If we couldn't parse an argument list, then we revert our changes
8366              and return simply an error. Maybe this is not a template-id
8367              after all.  */
8368           next_token_2->type = CPP_COLON;
8369           cp_parser_error (parser, "expected %<<%>");
8370           pop_deferring_access_checks ();
8371           return error_mark_node;
8372         }
8373       /* Otherwise, emit an error about the invalid digraph, but continue
8374          parsing because we got our argument list.  */
8375       pedwarn ("%<<::%> cannot begin a template-argument list");
8376       inform ("%<<:%> is an alternate spelling for %<[%>. Insert whitespace "
8377               "between %<<%> and %<::%>");
8378       if (!flag_permissive)
8379         {
8380           static bool hint;
8381           if (!hint)
8382             {
8383               inform ("(if you use -fpermissive G++ will accept your code)");
8384               hint = true;
8385             }
8386         }
8387     }
8388   else
8389     {
8390       /* Look for the `<' that starts the template-argument-list.  */
8391       if (!cp_parser_require (parser, CPP_LESS, "`<'"))
8392         {
8393           pop_deferring_access_checks ();
8394           return error_mark_node;
8395         }
8396       /* Parse the arguments.  */
8397       arguments = cp_parser_enclosed_template_argument_list (parser);
8398     }
8399
8400   /* Build a representation of the specialization.  */
8401   if (TREE_CODE (template) == IDENTIFIER_NODE)
8402     template_id = build_min_nt (TEMPLATE_ID_EXPR, template, arguments);
8403   else if (DECL_CLASS_TEMPLATE_P (template)
8404            || DECL_TEMPLATE_TEMPLATE_PARM_P (template))
8405     template_id
8406       = finish_template_type (template, arguments,
8407                               cp_lexer_next_token_is (parser->lexer,
8408                                                       CPP_SCOPE));
8409   else
8410     {
8411       /* If it's not a class-template or a template-template, it should be
8412          a function-template.  */
8413       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (template)
8414                    || TREE_CODE (template) == OVERLOAD
8415                    || BASELINK_P (template)));
8416
8417       template_id = lookup_template_function (template, arguments);
8418     }
8419
8420   /* Retrieve any deferred checks.  Do not pop this access checks yet
8421      so the memory will not be reclaimed during token replacing below.  */
8422   access_check = get_deferred_access_checks ();
8423
8424   /* If parsing tentatively, replace the sequence of tokens that makes
8425      up the template-id with a CPP_TEMPLATE_ID token.  That way,
8426      should we re-parse the token stream, we will not have to repeat
8427      the effort required to do the parse, nor will we issue duplicate
8428      error messages about problems during instantiation of the
8429      template.  */
8430   if (start_of_id)
8431     {
8432       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
8433       
8434       /* Reset the contents of the START_OF_ID token.  */
8435       token->type = CPP_TEMPLATE_ID;
8436       token->value = build_tree_list (access_check, template_id);
8437       token->keyword = RID_MAX;
8438       
8439       /* Purge all subsequent tokens.  */
8440       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
8441     }
8442
8443   pop_deferring_access_checks ();
8444   return template_id;
8445 }
8446
8447 /* Parse a template-name.
8448
8449    template-name:
8450      identifier
8451
8452    The standard should actually say:
8453
8454    template-name:
8455      identifier
8456      operator-function-id
8457
8458    A defect report has been filed about this issue.
8459
8460    A conversion-function-id cannot be a template name because they cannot
8461    be part of a template-id. In fact, looking at this code:
8462
8463    a.operator K<int>()
8464
8465    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
8466    It is impossible to call a templated conversion-function-id with an
8467    explicit argument list, since the only allowed template parameter is
8468    the type to which it is converting.
8469
8470    If TEMPLATE_KEYWORD_P is true, then we have just seen the
8471    `template' keyword, in a construction like:
8472
8473      T::template f<3>()
8474
8475    In that case `f' is taken to be a template-name, even though there
8476    is no way of knowing for sure.
8477
8478    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
8479    name refers to a set of overloaded functions, at least one of which
8480    is a template, or an IDENTIFIER_NODE with the name of the template,
8481    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
8482    names are looked up inside uninstantiated templates.  */
8483
8484 static tree
8485 cp_parser_template_name (cp_parser* parser,
8486                          bool template_keyword_p,
8487                          bool check_dependency_p,
8488                          bool is_declaration,
8489                          bool *is_identifier)
8490 {
8491   tree identifier;
8492   tree decl;
8493   tree fns;
8494
8495   /* If the next token is `operator', then we have either an
8496      operator-function-id or a conversion-function-id.  */
8497   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
8498     {
8499       /* We don't know whether we're looking at an
8500          operator-function-id or a conversion-function-id.  */
8501       cp_parser_parse_tentatively (parser);
8502       /* Try an operator-function-id.  */
8503       identifier = cp_parser_operator_function_id (parser);
8504       /* If that didn't work, try a conversion-function-id.  */
8505       if (!cp_parser_parse_definitely (parser))
8506         {
8507           cp_parser_error (parser, "expected template-name");
8508           return error_mark_node;
8509         }
8510     }
8511   /* Look for the identifier.  */
8512   else
8513     identifier = cp_parser_identifier (parser);
8514
8515   /* If we didn't find an identifier, we don't have a template-id.  */
8516   if (identifier == error_mark_node)
8517     return error_mark_node;
8518
8519   /* If the name immediately followed the `template' keyword, then it
8520      is a template-name.  However, if the next token is not `<', then
8521      we do not treat it as a template-name, since it is not being used
8522      as part of a template-id.  This enables us to handle constructs
8523      like:
8524
8525        template <typename T> struct S { S(); };
8526        template <typename T> S<T>::S();
8527
8528      correctly.  We would treat `S' as a template -- if it were `S<T>'
8529      -- but we do not if there is no `<'.  */
8530
8531   if (processing_template_decl
8532       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
8533     {
8534       /* In a declaration, in a dependent context, we pretend that the
8535          "template" keyword was present in order to improve error
8536          recovery.  For example, given:
8537
8538            template <typename T> void f(T::X<int>);
8539
8540          we want to treat "X<int>" as a template-id.  */
8541       if (is_declaration
8542           && !template_keyword_p
8543           && parser->scope && TYPE_P (parser->scope)
8544           && check_dependency_p
8545           && dependent_type_p (parser->scope)
8546           /* Do not do this for dtors (or ctors), since they never
8547              need the template keyword before their name.  */
8548           && !constructor_name_p (identifier, parser->scope))
8549         {
8550           cp_token_position start = 0;
8551           
8552           /* Explain what went wrong.  */
8553           error ("non-template %qD used as template", identifier);
8554           inform ("use %<%T::template %D%> to indicate that it is a template",
8555                   parser->scope, identifier);
8556           /* If parsing tentatively, find the location of the "<"
8557              token.  */
8558           if (cp_parser_parsing_tentatively (parser)
8559               && !cp_parser_committed_to_tentative_parse (parser))
8560             {
8561               cp_parser_simulate_error (parser);
8562               start = cp_lexer_token_position (parser->lexer, true);
8563             }
8564           /* Parse the template arguments so that we can issue error
8565              messages about them.  */
8566           cp_lexer_consume_token (parser->lexer);
8567           cp_parser_enclosed_template_argument_list (parser);
8568           /* Skip tokens until we find a good place from which to
8569              continue parsing.  */
8570           cp_parser_skip_to_closing_parenthesis (parser,
8571                                                  /*recovering=*/true,
8572                                                  /*or_comma=*/true,
8573                                                  /*consume_paren=*/false);
8574           /* If parsing tentatively, permanently remove the
8575              template argument list.  That will prevent duplicate
8576              error messages from being issued about the missing
8577              "template" keyword.  */
8578           if (start)
8579             cp_lexer_purge_tokens_after (parser->lexer, start);
8580           if (is_identifier)
8581             *is_identifier = true;
8582           return identifier;
8583         }
8584
8585       /* If the "template" keyword is present, then there is generally
8586          no point in doing name-lookup, so we just return IDENTIFIER.
8587          But, if the qualifying scope is non-dependent then we can
8588          (and must) do name-lookup normally.  */
8589       if (template_keyword_p
8590           && (!parser->scope
8591               || (TYPE_P (parser->scope)
8592                   && dependent_type_p (parser->scope))))
8593         return identifier;
8594     }
8595
8596   /* Look up the name.  */
8597   decl = cp_parser_lookup_name (parser, identifier,
8598                                 /*is_type=*/false,
8599                                 /*is_template=*/false,
8600                                 /*is_namespace=*/false,
8601                                 check_dependency_p,
8602                                 /*ambiguous_p=*/NULL);
8603   decl = maybe_get_template_decl_from_type_decl (decl);
8604
8605   /* If DECL is a template, then the name was a template-name.  */
8606   if (TREE_CODE (decl) == TEMPLATE_DECL)
8607     ;
8608   else
8609     {
8610       /* The standard does not explicitly indicate whether a name that
8611          names a set of overloaded declarations, some of which are
8612          templates, is a template-name.  However, such a name should
8613          be a template-name; otherwise, there is no way to form a
8614          template-id for the overloaded templates.  */
8615       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
8616       if (TREE_CODE (fns) == OVERLOAD)
8617         {
8618           tree fn;
8619
8620           for (fn = fns; fn; fn = OVL_NEXT (fn))
8621             if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
8622               break;
8623         }
8624       else
8625         {
8626           /* Otherwise, the name does not name a template.  */
8627           cp_parser_error (parser, "expected template-name");
8628           return error_mark_node;
8629         }
8630     }
8631
8632   /* If DECL is dependent, and refers to a function, then just return
8633      its name; we will look it up again during template instantiation.  */
8634   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
8635     {
8636       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
8637       if (TYPE_P (scope) && dependent_type_p (scope))
8638         return identifier;
8639     }
8640
8641   return decl;
8642 }
8643
8644 /* Parse a template-argument-list.
8645
8646    template-argument-list:
8647      template-argument
8648      template-argument-list , template-argument
8649
8650    Returns a TREE_VEC containing the arguments.  */
8651
8652 static tree
8653 cp_parser_template_argument_list (cp_parser* parser)
8654 {
8655   tree fixed_args[10];
8656   unsigned n_args = 0;
8657   unsigned alloced = 10;
8658   tree *arg_ary = fixed_args;
8659   tree vec;
8660   bool saved_in_template_argument_list_p;
8661
8662   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
8663   parser->in_template_argument_list_p = true;
8664   do
8665     {
8666       tree argument;
8667
8668       if (n_args)
8669         /* Consume the comma.  */
8670         cp_lexer_consume_token (parser->lexer);
8671
8672       /* Parse the template-argument.  */
8673       argument = cp_parser_template_argument (parser);
8674       if (n_args == alloced)
8675         {
8676           alloced *= 2;
8677
8678           if (arg_ary == fixed_args)
8679             {
8680               arg_ary = xmalloc (sizeof (tree) * alloced);
8681               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
8682             }
8683           else
8684             arg_ary = xrealloc (arg_ary, sizeof (tree) * alloced);
8685         }
8686       arg_ary[n_args++] = argument;
8687     }
8688   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
8689
8690   vec = make_tree_vec (n_args);
8691
8692   while (n_args--)
8693     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
8694
8695   if (arg_ary != fixed_args)
8696     free (arg_ary);
8697   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
8698   return vec;
8699 }
8700
8701 /* Parse a template-argument.
8702
8703    template-argument:
8704      assignment-expression
8705      type-id
8706      id-expression
8707
8708    The representation is that of an assignment-expression, type-id, or
8709    id-expression -- except that the qualified id-expression is
8710    evaluated, so that the value returned is either a DECL or an
8711    OVERLOAD.
8712
8713    Although the standard says "assignment-expression", it forbids
8714    throw-expressions or assignments in the template argument.
8715    Therefore, we use "conditional-expression" instead.  */
8716
8717 static tree
8718 cp_parser_template_argument (cp_parser* parser)
8719 {
8720   tree argument;
8721   bool template_p;
8722   bool address_p;
8723   bool maybe_type_id = false;
8724   cp_token *token;
8725   cp_id_kind idk;
8726   tree qualifying_class;
8727
8728   /* There's really no way to know what we're looking at, so we just
8729      try each alternative in order.
8730
8731        [temp.arg]
8732
8733        In a template-argument, an ambiguity between a type-id and an
8734        expression is resolved to a type-id, regardless of the form of
8735        the corresponding template-parameter.
8736
8737      Therefore, we try a type-id first.  */
8738   cp_parser_parse_tentatively (parser);
8739   argument = cp_parser_type_id (parser);
8740   /* If there was no error parsing the type-id but the next token is a '>>',
8741      we probably found a typo for '> >'. But there are type-id which are
8742      also valid expressions. For instance:
8743
8744      struct X { int operator >> (int); };
8745      template <int V> struct Foo {};
8746      Foo<X () >> 5> r;
8747
8748      Here 'X()' is a valid type-id of a function type, but the user just
8749      wanted to write the expression "X() >> 5". Thus, we remember that we
8750      found a valid type-id, but we still try to parse the argument as an
8751      expression to see what happens.  */
8752   if (!cp_parser_error_occurred (parser)
8753       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
8754     {
8755       maybe_type_id = true;
8756       cp_parser_abort_tentative_parse (parser);
8757     }
8758   else
8759     {
8760       /* If the next token isn't a `,' or a `>', then this argument wasn't
8761       really finished. This means that the argument is not a valid
8762       type-id.  */
8763       if (!cp_parser_next_token_ends_template_argument_p (parser))
8764         cp_parser_error (parser, "expected template-argument");
8765       /* If that worked, we're done.  */
8766       if (cp_parser_parse_definitely (parser))
8767         return argument;
8768     }
8769   /* We're still not sure what the argument will be.  */
8770   cp_parser_parse_tentatively (parser);
8771   /* Try a template.  */
8772   argument = cp_parser_id_expression (parser,
8773                                       /*template_keyword_p=*/false,
8774                                       /*check_dependency_p=*/true,
8775                                       &template_p,
8776                                       /*declarator_p=*/false);
8777   /* If the next token isn't a `,' or a `>', then this argument wasn't
8778      really finished.  */
8779   if (!cp_parser_next_token_ends_template_argument_p (parser))
8780     cp_parser_error (parser, "expected template-argument");
8781   if (!cp_parser_error_occurred (parser))
8782     {
8783       /* Figure out what is being referred to.  If the id-expression
8784          was for a class template specialization, then we will have a
8785          TYPE_DECL at this point.  There is no need to do name lookup
8786          at this point in that case.  */
8787       if (TREE_CODE (argument) != TYPE_DECL)
8788         argument = cp_parser_lookup_name (parser, argument,
8789                                           /*is_type=*/false,
8790                                           /*is_template=*/template_p,
8791                                           /*is_namespace=*/false,
8792                                           /*check_dependency=*/true,
8793                                           /*ambiguous_p=*/NULL);
8794       if (TREE_CODE (argument) != TEMPLATE_DECL
8795           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
8796         cp_parser_error (parser, "expected template-name");
8797     }
8798   if (cp_parser_parse_definitely (parser))
8799     return argument;
8800   /* It must be a non-type argument.  There permitted cases are given
8801      in [temp.arg.nontype]:
8802
8803      -- an integral constant-expression of integral or enumeration
8804         type; or
8805
8806      -- the name of a non-type template-parameter; or
8807
8808      -- the name of an object or function with external linkage...
8809
8810      -- the address of an object or function with external linkage...
8811
8812      -- a pointer to member...  */
8813   /* Look for a non-type template parameter.  */
8814   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
8815     {
8816       cp_parser_parse_tentatively (parser);
8817       argument = cp_parser_primary_expression (parser,
8818                                                &idk,
8819                                                &qualifying_class);
8820       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
8821           || !cp_parser_next_token_ends_template_argument_p (parser))
8822         cp_parser_simulate_error (parser);
8823       if (cp_parser_parse_definitely (parser))
8824         return argument;
8825     }
8826   /* If the next token is "&", the argument must be the address of an
8827      object or function with external linkage.  */
8828   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
8829   if (address_p)
8830     cp_lexer_consume_token (parser->lexer);
8831   /* See if we might have an id-expression.  */
8832   token = cp_lexer_peek_token (parser->lexer);
8833   if (token->type == CPP_NAME
8834       || token->keyword == RID_OPERATOR
8835       || token->type == CPP_SCOPE
8836       || token->type == CPP_TEMPLATE_ID
8837       || token->type == CPP_NESTED_NAME_SPECIFIER)
8838     {
8839       cp_parser_parse_tentatively (parser);
8840       argument = cp_parser_primary_expression (parser,
8841                                                &idk,
8842                                                &qualifying_class);
8843       if (cp_parser_error_occurred (parser)
8844           || !cp_parser_next_token_ends_template_argument_p (parser))
8845         cp_parser_abort_tentative_parse (parser);
8846       else
8847         {
8848           if (qualifying_class)
8849             argument = finish_qualified_id_expr (qualifying_class,
8850                                                  argument,
8851                                                  /*done=*/true,
8852                                                  address_p);
8853           if (TREE_CODE (argument) == VAR_DECL)
8854             {
8855               /* A variable without external linkage might still be a
8856                  valid constant-expression, so no error is issued here
8857                  if the external-linkage check fails.  */
8858               if (!DECL_EXTERNAL_LINKAGE_P (argument))
8859                 cp_parser_simulate_error (parser);
8860             }
8861           else if (is_overloaded_fn (argument))
8862             /* All overloaded functions are allowed; if the external
8863                linkage test does not pass, an error will be issued
8864                later.  */
8865             ;
8866           else if (address_p
8867                    && (TREE_CODE (argument) == OFFSET_REF
8868                        || TREE_CODE (argument) == SCOPE_REF))
8869             /* A pointer-to-member.  */
8870             ;
8871           else
8872             cp_parser_simulate_error (parser);
8873
8874           if (cp_parser_parse_definitely (parser))
8875             {
8876               if (address_p)
8877                 argument = build_x_unary_op (ADDR_EXPR, argument);
8878               return argument;
8879             }
8880         }
8881     }
8882   /* If the argument started with "&", there are no other valid
8883      alternatives at this point.  */
8884   if (address_p)
8885     {
8886       cp_parser_error (parser, "invalid non-type template argument");
8887       return error_mark_node;
8888     }
8889   /* If the argument wasn't successfully parsed as a type-id followed
8890      by '>>', the argument can only be a constant expression now.
8891      Otherwise, we try parsing the constant-expression tentatively,
8892      because the argument could really be a type-id.  */
8893   if (maybe_type_id)
8894     cp_parser_parse_tentatively (parser);
8895   argument = cp_parser_constant_expression (parser,
8896                                             /*allow_non_constant_p=*/false,
8897                                             /*non_constant_p=*/NULL);
8898   argument = fold_non_dependent_expr (argument);
8899   if (!maybe_type_id)
8900     return argument;
8901   if (!cp_parser_next_token_ends_template_argument_p (parser))
8902     cp_parser_error (parser, "expected template-argument");
8903   if (cp_parser_parse_definitely (parser))
8904     return argument;
8905   /* We did our best to parse the argument as a non type-id, but that
8906      was the only alternative that matched (albeit with a '>' after
8907      it). We can assume it's just a typo from the user, and a
8908      diagnostic will then be issued.  */
8909   return cp_parser_type_id (parser);
8910 }
8911
8912 /* Parse an explicit-instantiation.
8913
8914    explicit-instantiation:
8915      template declaration
8916
8917    Although the standard says `declaration', what it really means is:
8918
8919    explicit-instantiation:
8920      template decl-specifier-seq [opt] declarator [opt] ;
8921
8922    Things like `template int S<int>::i = 5, int S<double>::j;' are not
8923    supposed to be allowed.  A defect report has been filed about this
8924    issue.
8925
8926    GNU Extension:
8927
8928    explicit-instantiation:
8929      storage-class-specifier template
8930        decl-specifier-seq [opt] declarator [opt] ;
8931      function-specifier template
8932        decl-specifier-seq [opt] declarator [opt] ;  */
8933
8934 static void
8935 cp_parser_explicit_instantiation (cp_parser* parser)
8936 {
8937   int declares_class_or_enum;
8938   cp_decl_specifier_seq decl_specifiers;
8939   tree extension_specifier = NULL_TREE;
8940
8941   /* Look for an (optional) storage-class-specifier or
8942      function-specifier.  */
8943   if (cp_parser_allow_gnu_extensions_p (parser))
8944     {
8945       extension_specifier
8946         = cp_parser_storage_class_specifier_opt (parser);
8947       if (!extension_specifier)
8948         extension_specifier
8949           = cp_parser_function_specifier_opt (parser,
8950                                               /*decl_specs=*/NULL);
8951     }
8952
8953   /* Look for the `template' keyword.  */
8954   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
8955   /* Let the front end know that we are processing an explicit
8956      instantiation.  */
8957   begin_explicit_instantiation ();
8958   /* [temp.explicit] says that we are supposed to ignore access
8959      control while processing explicit instantiation directives.  */
8960   push_deferring_access_checks (dk_no_check);
8961   /* Parse a decl-specifier-seq.  */
8962   cp_parser_decl_specifier_seq (parser,
8963                                 CP_PARSER_FLAGS_OPTIONAL,
8964                                 &decl_specifiers,
8965                                 &declares_class_or_enum);
8966   /* If there was exactly one decl-specifier, and it declared a class,
8967      and there's no declarator, then we have an explicit type
8968      instantiation.  */
8969   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
8970     {
8971       tree type;
8972
8973       type = check_tag_decl (&decl_specifiers);
8974       /* Turn access control back on for names used during
8975          template instantiation.  */
8976       pop_deferring_access_checks ();
8977       if (type)
8978         do_type_instantiation (type, extension_specifier, /*complain=*/1);
8979     }
8980   else
8981     {
8982       cp_declarator *declarator;
8983       tree decl;
8984
8985       /* Parse the declarator.  */
8986       declarator
8987         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8988                                 /*ctor_dtor_or_conv_p=*/NULL,
8989                                 /*parenthesized_p=*/NULL,
8990                                 /*member_p=*/false);
8991       cp_parser_check_for_definition_in_return_type (declarator,
8992                                                      declares_class_or_enum);
8993       if (declarator != cp_error_declarator)
8994         {
8995           decl = grokdeclarator (declarator, &decl_specifiers,
8996                                  NORMAL, 0, NULL);
8997           /* Turn access control back on for names used during
8998              template instantiation.  */
8999           pop_deferring_access_checks ();
9000           /* Do the explicit instantiation.  */
9001           do_decl_instantiation (decl, extension_specifier);
9002         }
9003       else
9004         {
9005           pop_deferring_access_checks ();
9006           /* Skip the body of the explicit instantiation.  */
9007           cp_parser_skip_to_end_of_statement (parser);
9008         }
9009     }
9010   /* We're done with the instantiation.  */
9011   end_explicit_instantiation ();
9012
9013   cp_parser_consume_semicolon_at_end_of_statement (parser);
9014 }
9015
9016 /* Parse an explicit-specialization.
9017
9018    explicit-specialization:
9019      template < > declaration
9020
9021    Although the standard says `declaration', what it really means is:
9022
9023    explicit-specialization:
9024      template <> decl-specifier [opt] init-declarator [opt] ;
9025      template <> function-definition
9026      template <> explicit-specialization
9027      template <> template-declaration  */
9028
9029 static void
9030 cp_parser_explicit_specialization (cp_parser* parser)
9031 {
9032   /* Look for the `template' keyword.  */
9033   cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'");
9034   /* Look for the `<'.  */
9035   cp_parser_require (parser, CPP_LESS, "`<'");
9036   /* Look for the `>'.  */
9037   cp_parser_require (parser, CPP_GREATER, "`>'");
9038   /* We have processed another parameter list.  */
9039   ++parser->num_template_parameter_lists;
9040   /* Let the front end know that we are beginning a specialization.  */
9041   begin_specialization ();
9042
9043   /* If the next keyword is `template', we need to figure out whether
9044      or not we're looking a template-declaration.  */
9045   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
9046     {
9047       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
9048           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
9049         cp_parser_template_declaration_after_export (parser,
9050                                                      /*member_p=*/false);
9051       else
9052         cp_parser_explicit_specialization (parser);
9053     }
9054   else
9055     /* Parse the dependent declaration.  */
9056     cp_parser_single_declaration (parser,
9057                                   /*member_p=*/false,
9058                                   /*friend_p=*/NULL);
9059
9060   /* We're done with the specialization.  */
9061   end_specialization ();
9062   /* We're done with this parameter list.  */
9063   --parser->num_template_parameter_lists;
9064 }
9065
9066 /* Parse a type-specifier.
9067
9068    type-specifier:
9069      simple-type-specifier
9070      class-specifier
9071      enum-specifier
9072      elaborated-type-specifier
9073      cv-qualifier
9074
9075    GNU Extension:
9076
9077    type-specifier:
9078      __complex__
9079
9080    Returns a representation of the type-specifier.  For a
9081    class-specifier, enum-specifier, or elaborated-type-specifier, a
9082    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
9083
9084    The parser flags FLAGS is used to control type-specifier parsing.
9085
9086    If IS_DECLARATION is TRUE, then this type-specifier is appearing
9087    in a decl-specifier-seq.
9088
9089    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
9090    class-specifier, enum-specifier, or elaborated-type-specifier, then
9091    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
9092    if a type is declared; 2 if it is defined.  Otherwise, it is set to
9093    zero.
9094
9095    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
9096    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
9097    is set to FALSE.  */
9098
9099 static tree
9100 cp_parser_type_specifier (cp_parser* parser,
9101                           cp_parser_flags flags,
9102                           cp_decl_specifier_seq *decl_specs,
9103                           bool is_declaration,
9104                           int* declares_class_or_enum,
9105                           bool* is_cv_qualifier)
9106 {
9107   tree type_spec = NULL_TREE;
9108   cp_token *token;
9109   enum rid keyword;
9110   cp_decl_spec ds = ds_last;
9111
9112   /* Assume this type-specifier does not declare a new type.  */
9113   if (declares_class_or_enum)
9114     *declares_class_or_enum = 0;
9115   /* And that it does not specify a cv-qualifier.  */
9116   if (is_cv_qualifier)
9117     *is_cv_qualifier = false;
9118   /* Peek at the next token.  */
9119   token = cp_lexer_peek_token (parser->lexer);
9120
9121   /* If we're looking at a keyword, we can use that to guide the
9122      production we choose.  */
9123   keyword = token->keyword;
9124   switch (keyword)
9125     {
9126     case RID_ENUM:
9127       /* 'enum' [identifier] '{' introduces an enum-specifier;
9128          'enum' <anything else> introduces an elaborated-type-specifier.  */
9129       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_OPEN_BRACE
9130           || (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
9131               && cp_lexer_peek_nth_token (parser->lexer, 3)->type
9132                  == CPP_OPEN_BRACE))
9133         {
9134           type_spec = cp_parser_enum_specifier (parser);
9135           if (declares_class_or_enum)
9136             *declares_class_or_enum = 2;
9137           if (decl_specs)
9138             cp_parser_set_decl_spec_type (decl_specs,
9139                                           type_spec,
9140                                           /*user_defined_p=*/true);
9141           return type_spec;
9142         }
9143       else
9144         goto elaborated_type_specifier;
9145
9146       /* Any of these indicate either a class-specifier, or an
9147          elaborated-type-specifier.  */
9148     case RID_CLASS:
9149     case RID_STRUCT:
9150     case RID_UNION:
9151       /* Parse tentatively so that we can back up if we don't find a
9152          class-specifier.  */
9153       cp_parser_parse_tentatively (parser);
9154       /* Look for the class-specifier.  */
9155       type_spec = cp_parser_class_specifier (parser);
9156       /* If that worked, we're done.  */
9157       if (cp_parser_parse_definitely (parser))
9158         {
9159           if (declares_class_or_enum)
9160             *declares_class_or_enum = 2;
9161           if (decl_specs)
9162             cp_parser_set_decl_spec_type (decl_specs,
9163                                           type_spec,
9164                                           /*user_defined_p=*/true);
9165           return type_spec;
9166         }
9167
9168       /* Fall through.  */
9169     elaborated_type_specifier:
9170       /* We're declaring (not defining) a class or enum.  */
9171       if (declares_class_or_enum)
9172         *declares_class_or_enum = 1;
9173
9174       /* Fall through.  */
9175     case RID_TYPENAME:
9176       /* Look for an elaborated-type-specifier.  */
9177       type_spec
9178         = (cp_parser_elaborated_type_specifier
9179            (parser,
9180             decl_specs && decl_specs->specs[(int) ds_friend],
9181             is_declaration));
9182       if (decl_specs)
9183         cp_parser_set_decl_spec_type (decl_specs,
9184                                       type_spec,
9185                                       /*user_defined_p=*/true);
9186       return type_spec;
9187
9188     case RID_CONST:
9189       ds = ds_const;
9190       if (is_cv_qualifier)
9191         *is_cv_qualifier = true;
9192       break;
9193
9194     case RID_VOLATILE:
9195       ds = ds_volatile;
9196       if (is_cv_qualifier)
9197         *is_cv_qualifier = true;
9198       break;
9199
9200     case RID_RESTRICT:
9201       ds = ds_restrict;
9202       if (is_cv_qualifier)
9203         *is_cv_qualifier = true;
9204       break;
9205
9206     case RID_COMPLEX:
9207       /* The `__complex__' keyword is a GNU extension.  */
9208       ds = ds_complex;
9209       break;
9210
9211     default:
9212       break;
9213     }
9214
9215   /* Handle simple keywords.  */
9216   if (ds != ds_last)
9217     {
9218       if (decl_specs)
9219         {
9220           ++decl_specs->specs[(int)ds];
9221           decl_specs->any_specifiers_p = true;
9222         }
9223       return cp_lexer_consume_token (parser->lexer)->value;
9224     }
9225
9226   /* If we do not already have a type-specifier, assume we are looking
9227      at a simple-type-specifier.  */
9228   type_spec = cp_parser_simple_type_specifier (parser,
9229                                                decl_specs,
9230                                                flags);
9231
9232   /* If we didn't find a type-specifier, and a type-specifier was not
9233      optional in this context, issue an error message.  */
9234   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9235     {
9236       cp_parser_error (parser, "expected type specifier");
9237       return error_mark_node;
9238     }
9239
9240   return type_spec;
9241 }
9242
9243 /* Parse a simple-type-specifier.
9244
9245    simple-type-specifier:
9246      :: [opt] nested-name-specifier [opt] type-name
9247      :: [opt] nested-name-specifier template template-id
9248      char
9249      wchar_t
9250      bool
9251      short
9252      int
9253      long
9254      signed
9255      unsigned
9256      float
9257      double
9258      void
9259
9260    GNU Extension:
9261
9262    simple-type-specifier:
9263      __typeof__ unary-expression
9264      __typeof__ ( type-id )
9265
9266    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
9267    appropriately updated.  */
9268
9269 static tree
9270 cp_parser_simple_type_specifier (cp_parser* parser,
9271                                  cp_decl_specifier_seq *decl_specs,
9272                                  cp_parser_flags flags)
9273 {
9274   tree type = NULL_TREE;
9275   cp_token *token;
9276
9277   /* Peek at the next token.  */
9278   token = cp_lexer_peek_token (parser->lexer);
9279
9280   /* If we're looking at a keyword, things are easy.  */
9281   switch (token->keyword)
9282     {
9283     case RID_CHAR:
9284       if (decl_specs)
9285         decl_specs->explicit_char_p = true;
9286       type = char_type_node;
9287       break;
9288     case RID_WCHAR:
9289       type = wchar_type_node;
9290       break;
9291     case RID_BOOL:
9292       type = boolean_type_node;
9293       break;
9294     case RID_SHORT:
9295       if (decl_specs)
9296         ++decl_specs->specs[(int) ds_short];
9297       type = short_integer_type_node;
9298       break;
9299     case RID_INT:
9300       if (decl_specs)
9301         decl_specs->explicit_int_p = true;
9302       type = integer_type_node;
9303       break;
9304     case RID_LONG:
9305       if (decl_specs)
9306         ++decl_specs->specs[(int) ds_long];
9307       type = long_integer_type_node;
9308       break;
9309     case RID_SIGNED:
9310       if (decl_specs)
9311         ++decl_specs->specs[(int) ds_signed];
9312       type = integer_type_node;
9313       break;
9314     case RID_UNSIGNED:
9315       if (decl_specs)
9316         ++decl_specs->specs[(int) ds_unsigned];
9317       type = unsigned_type_node;
9318       break;
9319     case RID_FLOAT:
9320       type = float_type_node;
9321       break;
9322     case RID_DOUBLE:
9323       type = double_type_node;
9324       break;
9325     case RID_VOID:
9326       type = void_type_node;
9327       break;
9328
9329     case RID_TYPEOF:
9330       /* Consume the `typeof' token.  */
9331       cp_lexer_consume_token (parser->lexer);
9332       /* Parse the operand to `typeof'.  */
9333       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
9334       /* If it is not already a TYPE, take its type.  */
9335       if (!TYPE_P (type))
9336         type = finish_typeof (type);
9337
9338       if (decl_specs)
9339         cp_parser_set_decl_spec_type (decl_specs, type,
9340                                       /*user_defined_p=*/true);
9341
9342       return type;
9343
9344     default:
9345       break;
9346     }
9347
9348   /* If the type-specifier was for a built-in type, we're done.  */
9349   if (type)
9350     {
9351       tree id;
9352
9353       /* Record the type.  */
9354       if (decl_specs
9355           && (token->keyword != RID_SIGNED
9356               && token->keyword != RID_UNSIGNED
9357               && token->keyword != RID_SHORT
9358               && token->keyword != RID_LONG))
9359         cp_parser_set_decl_spec_type (decl_specs,
9360                                       type,
9361                                       /*user_defined=*/false);
9362       if (decl_specs)
9363         decl_specs->any_specifiers_p = true;
9364
9365       /* Consume the token.  */
9366       id = cp_lexer_consume_token (parser->lexer)->value;
9367
9368       /* There is no valid C++ program where a non-template type is
9369          followed by a "<".  That usually indicates that the user thought
9370          that the type was a template.  */
9371       cp_parser_check_for_invalid_template_id (parser, type);
9372
9373       return TYPE_NAME (type);
9374     }
9375
9376   /* The type-specifier must be a user-defined type.  */
9377   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
9378     {
9379       bool qualified_p;
9380       bool global_p;
9381
9382       /* Don't gobble tokens or issue error messages if this is an
9383          optional type-specifier.  */
9384       if (flags & CP_PARSER_FLAGS_OPTIONAL)
9385         cp_parser_parse_tentatively (parser);
9386
9387       /* Look for the optional `::' operator.  */
9388       global_p
9389         = (cp_parser_global_scope_opt (parser,
9390                                        /*current_scope_valid_p=*/false)
9391            != NULL_TREE);
9392       /* Look for the nested-name specifier.  */
9393       qualified_p
9394         = (cp_parser_nested_name_specifier_opt (parser,
9395                                                 /*typename_keyword_p=*/false,
9396                                                 /*check_dependency_p=*/true,
9397                                                 /*type_p=*/false,
9398                                                 /*is_declaration=*/false)
9399            != NULL_TREE);
9400       /* If we have seen a nested-name-specifier, and the next token
9401          is `template', then we are using the template-id production.  */
9402       if (parser->scope
9403           && cp_parser_optional_template_keyword (parser))
9404         {
9405           /* Look for the template-id.  */
9406           type = cp_parser_template_id (parser,
9407                                         /*template_keyword_p=*/true,
9408                                         /*check_dependency_p=*/true,
9409                                         /*is_declaration=*/false);
9410           /* If the template-id did not name a type, we are out of
9411              luck.  */
9412           if (TREE_CODE (type) != TYPE_DECL)
9413             {
9414               cp_parser_error (parser, "expected template-id for type");
9415               type = NULL_TREE;
9416             }
9417         }
9418       /* Otherwise, look for a type-name.  */
9419       else
9420         type = cp_parser_type_name (parser);
9421       /* Keep track of all name-lookups performed in class scopes.  */
9422       if (type
9423           && !global_p
9424           && !qualified_p
9425           && TREE_CODE (type) == TYPE_DECL
9426           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
9427         maybe_note_name_used_in_class (DECL_NAME (type), type);
9428       /* If it didn't work out, we don't have a TYPE.  */
9429       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
9430           && !cp_parser_parse_definitely (parser))
9431         type = NULL_TREE;
9432       if (type && decl_specs)
9433         cp_parser_set_decl_spec_type (decl_specs, type,
9434                                       /*user_defined=*/true);
9435     }
9436
9437   /* If we didn't get a type-name, issue an error message.  */
9438   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
9439     {
9440       cp_parser_error (parser, "expected type-name");
9441       return error_mark_node;
9442     }
9443
9444   /* There is no valid C++ program where a non-template type is
9445      followed by a "<".  That usually indicates that the user thought
9446      that the type was a template.  */
9447   if (type && type != error_mark_node)
9448     cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type));
9449
9450   return type;
9451 }
9452
9453 /* Parse a type-name.
9454
9455    type-name:
9456      class-name
9457      enum-name
9458      typedef-name
9459
9460    enum-name:
9461      identifier
9462
9463    typedef-name:
9464      identifier
9465
9466    Returns a TYPE_DECL for the the type.  */
9467
9468 static tree
9469 cp_parser_type_name (cp_parser* parser)
9470 {
9471   tree type_decl;
9472   tree identifier;
9473
9474   /* We can't know yet whether it is a class-name or not.  */
9475   cp_parser_parse_tentatively (parser);
9476   /* Try a class-name.  */
9477   type_decl = cp_parser_class_name (parser,
9478                                     /*typename_keyword_p=*/false,
9479                                     /*template_keyword_p=*/false,
9480                                     /*type_p=*/false,
9481                                     /*check_dependency_p=*/true,
9482                                     /*class_head_p=*/false,
9483                                     /*is_declaration=*/false);
9484   /* If it's not a class-name, keep looking.  */
9485   if (!cp_parser_parse_definitely (parser))
9486     {
9487       /* It must be a typedef-name or an enum-name.  */
9488       identifier = cp_parser_identifier (parser);
9489       if (identifier == error_mark_node)
9490         return error_mark_node;
9491
9492       /* Look up the type-name.  */
9493       type_decl = cp_parser_lookup_name_simple (parser, identifier);
9494       /* Issue an error if we did not find a type-name.  */
9495       if (TREE_CODE (type_decl) != TYPE_DECL)
9496         {
9497           if (!cp_parser_simulate_error (parser))
9498             cp_parser_name_lookup_error (parser, identifier, type_decl,
9499                                          "is not a type");
9500           type_decl = error_mark_node;
9501         }
9502       /* Remember that the name was used in the definition of the
9503          current class so that we can check later to see if the
9504          meaning would have been different after the class was
9505          entirely defined.  */
9506       else if (type_decl != error_mark_node
9507                && !parser->scope)
9508         maybe_note_name_used_in_class (identifier, type_decl);
9509     }
9510
9511   return type_decl;
9512 }
9513
9514
9515 /* Parse an elaborated-type-specifier.  Note that the grammar given
9516    here incorporates the resolution to DR68.
9517
9518    elaborated-type-specifier:
9519      class-key :: [opt] nested-name-specifier [opt] identifier
9520      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
9521      enum :: [opt] nested-name-specifier [opt] identifier
9522      typename :: [opt] nested-name-specifier identifier
9523      typename :: [opt] nested-name-specifier template [opt]
9524        template-id
9525
9526    GNU extension:
9527
9528    elaborated-type-specifier:
9529      class-key attributes :: [opt] nested-name-specifier [opt] identifier
9530      class-key attributes :: [opt] nested-name-specifier [opt]
9531                template [opt] template-id
9532      enum attributes :: [opt] nested-name-specifier [opt] identifier
9533
9534    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
9535    declared `friend'.  If IS_DECLARATION is TRUE, then this
9536    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
9537    something is being declared.
9538
9539    Returns the TYPE specified.  */
9540
9541 static tree
9542 cp_parser_elaborated_type_specifier (cp_parser* parser,
9543                                      bool is_friend,
9544                                      bool is_declaration)
9545 {
9546   enum tag_types tag_type;
9547   tree identifier;
9548   tree type = NULL_TREE;
9549   tree attributes = NULL_TREE;
9550
9551   /* See if we're looking at the `enum' keyword.  */
9552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
9553     {
9554       /* Consume the `enum' token.  */
9555       cp_lexer_consume_token (parser->lexer);
9556       /* Remember that it's an enumeration type.  */
9557       tag_type = enum_type;
9558       /* Parse the attributes.  */
9559       attributes = cp_parser_attributes_opt (parser);
9560     }
9561   /* Or, it might be `typename'.  */
9562   else if (cp_lexer_next_token_is_keyword (parser->lexer,
9563                                            RID_TYPENAME))
9564     {
9565       /* Consume the `typename' token.  */
9566       cp_lexer_consume_token (parser->lexer);
9567       /* Remember that it's a `typename' type.  */
9568       tag_type = typename_type;
9569       /* The `typename' keyword is only allowed in templates.  */
9570       if (!processing_template_decl)
9571         pedwarn ("using %<typename%> outside of template");
9572     }
9573   /* Otherwise it must be a class-key.  */
9574   else
9575     {
9576       tag_type = cp_parser_class_key (parser);
9577       if (tag_type == none_type)
9578         return error_mark_node;
9579       /* Parse the attributes.  */
9580       attributes = cp_parser_attributes_opt (parser);
9581     }
9582
9583   /* Look for the `::' operator.  */
9584   cp_parser_global_scope_opt (parser,
9585                               /*current_scope_valid_p=*/false);
9586   /* Look for the nested-name-specifier.  */
9587   if (tag_type == typename_type)
9588     {
9589       if (cp_parser_nested_name_specifier (parser,
9590                                            /*typename_keyword_p=*/true,
9591                                            /*check_dependency_p=*/true,
9592                                            /*type_p=*/true,
9593                                            is_declaration)
9594           == error_mark_node)
9595         return error_mark_node;
9596     }
9597   else
9598     /* Even though `typename' is not present, the proposed resolution
9599        to Core Issue 180 says that in `class A<T>::B', `B' should be
9600        considered a type-name, even if `A<T>' is dependent.  */
9601     cp_parser_nested_name_specifier_opt (parser,
9602                                          /*typename_keyword_p=*/true,
9603                                          /*check_dependency_p=*/true,
9604                                          /*type_p=*/true,
9605                                          is_declaration);
9606   /* For everything but enumeration types, consider a template-id.  */
9607   if (tag_type != enum_type)
9608     {
9609       bool template_p = false;
9610       tree decl;
9611
9612       /* Allow the `template' keyword.  */
9613       template_p = cp_parser_optional_template_keyword (parser);
9614       /* If we didn't see `template', we don't know if there's a
9615          template-id or not.  */
9616       if (!template_p)
9617         cp_parser_parse_tentatively (parser);
9618       /* Parse the template-id.  */
9619       decl = cp_parser_template_id (parser, template_p,
9620                                     /*check_dependency_p=*/true,
9621                                     is_declaration);
9622       /* If we didn't find a template-id, look for an ordinary
9623          identifier.  */
9624       if (!template_p && !cp_parser_parse_definitely (parser))
9625         ;
9626       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
9627          in effect, then we must assume that, upon instantiation, the
9628          template will correspond to a class.  */
9629       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
9630                && tag_type == typename_type)
9631         type = make_typename_type (parser->scope, decl,
9632                                    /*complain=*/1);
9633       else
9634         type = TREE_TYPE (decl);
9635     }
9636
9637   /* For an enumeration type, consider only a plain identifier.  */
9638   if (!type)
9639     {
9640       identifier = cp_parser_identifier (parser);
9641
9642       if (identifier == error_mark_node)
9643         {
9644           parser->scope = NULL_TREE;
9645           return error_mark_node;
9646         }
9647
9648       /* For a `typename', we needn't call xref_tag.  */
9649       if (tag_type == typename_type)
9650         return cp_parser_make_typename_type (parser, parser->scope,
9651                                              identifier);
9652       /* Look up a qualified name in the usual way.  */
9653       if (parser->scope)
9654         {
9655           tree decl;
9656
9657           /* In an elaborated-type-specifier, names are assumed to name
9658              types, so we set IS_TYPE to TRUE when calling
9659              cp_parser_lookup_name.  */
9660           decl = cp_parser_lookup_name (parser, identifier,
9661                                         /*is_type=*/true,
9662                                         /*is_template=*/false,
9663                                         /*is_namespace=*/false,
9664                                         /*check_dependency=*/true,
9665                                         /*ambiguous_p=*/NULL);
9666
9667           /* If we are parsing friend declaration, DECL may be a
9668              TEMPLATE_DECL tree node here.  However, we need to check
9669              whether this TEMPLATE_DECL results in valid code.  Consider
9670              the following example:
9671
9672                namespace N {
9673                  template <class T> class C {};
9674                }
9675                class X {
9676                  template <class T> friend class N::C; // #1, valid code
9677                };
9678                template <class T> class Y {
9679                  friend class N::C;                    // #2, invalid code
9680                };
9681
9682              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
9683              name lookup of `N::C'.  We see that friend declaration must
9684              be template for the code to be valid.  Note that
9685              processing_template_decl does not work here since it is
9686              always 1 for the above two cases.  */
9687
9688           decl = (cp_parser_maybe_treat_template_as_class
9689                   (decl, /*tag_name_p=*/is_friend
9690                          && parser->num_template_parameter_lists));
9691
9692           if (TREE_CODE (decl) != TYPE_DECL)
9693             {
9694               error ("expected type-name");
9695               return error_mark_node;
9696             }
9697
9698           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
9699             check_elaborated_type_specifier
9700               (tag_type, decl,
9701                (parser->num_template_parameter_lists
9702                 || DECL_SELF_REFERENCE_P (decl)));
9703
9704           type = TREE_TYPE (decl);
9705         }
9706       else
9707         {
9708           /* An elaborated-type-specifier sometimes introduces a new type and
9709              sometimes names an existing type.  Normally, the rule is that it
9710              introduces a new type only if there is not an existing type of
9711              the same name already in scope.  For example, given:
9712
9713                struct S {};
9714                void f() { struct S s; }
9715
9716              the `struct S' in the body of `f' is the same `struct S' as in
9717              the global scope; the existing definition is used.  However, if
9718              there were no global declaration, this would introduce a new
9719              local class named `S'.
9720
9721              An exception to this rule applies to the following code:
9722
9723                namespace N { struct S; }
9724
9725              Here, the elaborated-type-specifier names a new type
9726              unconditionally; even if there is already an `S' in the
9727              containing scope this declaration names a new type.
9728              This exception only applies if the elaborated-type-specifier
9729              forms the complete declaration:
9730
9731                [class.name]
9732
9733                A declaration consisting solely of `class-key identifier ;' is
9734                either a redeclaration of the name in the current scope or a
9735                forward declaration of the identifier as a class name.  It
9736                introduces the name into the current scope.
9737
9738              We are in this situation precisely when the next token is a `;'.
9739
9740              An exception to the exception is that a `friend' declaration does
9741              *not* name a new type; i.e., given:
9742
9743                struct S { friend struct T; };
9744
9745              `T' is not a new type in the scope of `S'.
9746
9747              Also, `new struct S' or `sizeof (struct S)' never results in the
9748              definition of a new type; a new type can only be declared in a
9749              declaration context.  */
9750
9751           /* Warn about attributes. They are ignored.  */
9752           if (attributes)
9753             warning ("type attributes are honored only at type definition");
9754
9755           type = xref_tag (tag_type, identifier,
9756                            (is_friend
9757                             || !is_declaration
9758                             || cp_lexer_next_token_is_not (parser->lexer,
9759                                                            CPP_SEMICOLON)),
9760                            parser->num_template_parameter_lists);
9761         }
9762     }
9763   if (tag_type != enum_type)
9764     cp_parser_check_class_key (tag_type, type);
9765
9766   /* A "<" cannot follow an elaborated type specifier.  If that
9767      happens, the user was probably trying to form a template-id.  */
9768   cp_parser_check_for_invalid_template_id (parser, type);
9769
9770   return type;
9771 }
9772
9773 /* Parse an enum-specifier.
9774
9775    enum-specifier:
9776      enum identifier [opt] { enumerator-list [opt] }
9777
9778    Returns an ENUM_TYPE representing the enumeration.  */
9779
9780 static tree
9781 cp_parser_enum_specifier (cp_parser* parser)
9782 {
9783   tree identifier;
9784   tree type;
9785
9786   /* Caller guarantees that the current token is 'enum', an identifier
9787      possibly follows, and the token after that is an opening brace.
9788      If we don't have an identifier, fabricate an anonymous name for
9789      the enumeration being defined.  */
9790   cp_lexer_consume_token (parser->lexer);
9791
9792   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9793     identifier = cp_parser_identifier (parser);
9794   else
9795     identifier = make_anon_name ();
9796
9797   /* Issue an error message if type-definitions are forbidden here.  */
9798   cp_parser_check_type_definition (parser);
9799
9800   /* Create the new type.  We do this before consuming the opening brace
9801      so the enum will be recorded as being on the line of its tag (or the
9802      'enum' keyword, if there is no tag).  */
9803   type = start_enum (identifier);
9804
9805   /* Consume the opening brace.  */
9806   cp_lexer_consume_token (parser->lexer);
9807
9808   /* If the next token is not '}', then there are some enumerators.  */
9809   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
9810     cp_parser_enumerator_list (parser, type);
9811
9812   /* Consume the final '}'.  */
9813   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9814
9815   /* Finish up the enumeration.  */
9816   finish_enum (type);
9817
9818   return type;
9819 }
9820
9821 /* Parse an enumerator-list.  The enumerators all have the indicated
9822    TYPE.
9823
9824    enumerator-list:
9825      enumerator-definition
9826      enumerator-list , enumerator-definition  */
9827
9828 static void
9829 cp_parser_enumerator_list (cp_parser* parser, tree type)
9830 {
9831   while (true)
9832     {
9833       /* Parse an enumerator-definition.  */
9834       cp_parser_enumerator_definition (parser, type);
9835
9836       /* If the next token is not a ',', we've reached the end of
9837          the list.  */
9838       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9839         break;
9840       /* Otherwise, consume the `,' and keep going.  */
9841       cp_lexer_consume_token (parser->lexer);
9842       /* If the next token is a `}', there is a trailing comma.  */
9843       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
9844         {
9845           if (pedantic && !in_system_header)
9846             pedwarn ("comma at end of enumerator list");
9847           break;
9848         }
9849     }
9850 }
9851
9852 /* Parse an enumerator-definition.  The enumerator has the indicated
9853    TYPE.
9854
9855    enumerator-definition:
9856      enumerator
9857      enumerator = constant-expression
9858
9859    enumerator:
9860      identifier  */
9861
9862 static void
9863 cp_parser_enumerator_definition (cp_parser* parser, tree type)
9864 {
9865   tree identifier;
9866   tree value;
9867
9868   /* Look for the identifier.  */
9869   identifier = cp_parser_identifier (parser);
9870   if (identifier == error_mark_node)
9871     return;
9872
9873   /* If the next token is an '=', then there is an explicit value.  */
9874   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
9875     {
9876       /* Consume the `=' token.  */
9877       cp_lexer_consume_token (parser->lexer);
9878       /* Parse the value.  */
9879       value = cp_parser_constant_expression (parser,
9880                                              /*allow_non_constant_p=*/false,
9881                                              NULL);
9882     }
9883   else
9884     value = NULL_TREE;
9885
9886   /* Create the enumerator.  */
9887   build_enumerator (identifier, value, type);
9888 }
9889
9890 /* Parse a namespace-name.
9891
9892    namespace-name:
9893      original-namespace-name
9894      namespace-alias
9895
9896    Returns the NAMESPACE_DECL for the namespace.  */
9897
9898 static tree
9899 cp_parser_namespace_name (cp_parser* parser)
9900 {
9901   tree identifier;
9902   tree namespace_decl;
9903
9904   /* Get the name of the namespace.  */
9905   identifier = cp_parser_identifier (parser);
9906   if (identifier == error_mark_node)
9907     return error_mark_node;
9908
9909   /* Look up the identifier in the currently active scope.  Look only
9910      for namespaces, due to:
9911
9912        [basic.lookup.udir]
9913
9914        When looking up a namespace-name in a using-directive or alias
9915        definition, only namespace names are considered.
9916
9917      And:
9918
9919        [basic.lookup.qual]
9920
9921        During the lookup of a name preceding the :: scope resolution
9922        operator, object, function, and enumerator names are ignored.
9923
9924      (Note that cp_parser_class_or_namespace_name only calls this
9925      function if the token after the name is the scope resolution
9926      operator.)  */
9927   namespace_decl = cp_parser_lookup_name (parser, identifier,
9928                                           /*is_type=*/false,
9929                                           /*is_template=*/false,
9930                                           /*is_namespace=*/true,
9931                                           /*check_dependency=*/true,
9932                                           /*ambiguous_p=*/NULL);
9933   /* If it's not a namespace, issue an error.  */
9934   if (namespace_decl == error_mark_node
9935       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
9936     {
9937       cp_parser_error (parser, "expected namespace-name");
9938       namespace_decl = error_mark_node;
9939     }
9940
9941   return namespace_decl;
9942 }
9943
9944 /* Parse a namespace-definition.
9945
9946    namespace-definition:
9947      named-namespace-definition
9948      unnamed-namespace-definition
9949
9950    named-namespace-definition:
9951      original-namespace-definition
9952      extension-namespace-definition
9953
9954    original-namespace-definition:
9955      namespace identifier { namespace-body }
9956
9957    extension-namespace-definition:
9958      namespace original-namespace-name { namespace-body }
9959
9960    unnamed-namespace-definition:
9961      namespace { namespace-body } */
9962
9963 static void
9964 cp_parser_namespace_definition (cp_parser* parser)
9965 {
9966   tree identifier;
9967
9968   /* Look for the `namespace' keyword.  */
9969   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
9970
9971   /* Get the name of the namespace.  We do not attempt to distinguish
9972      between an original-namespace-definition and an
9973      extension-namespace-definition at this point.  The semantic
9974      analysis routines are responsible for that.  */
9975   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
9976     identifier = cp_parser_identifier (parser);
9977   else
9978     identifier = NULL_TREE;
9979
9980   /* Look for the `{' to start the namespace.  */
9981   cp_parser_require (parser, CPP_OPEN_BRACE, "`{'");
9982   /* Start the namespace.  */
9983   push_namespace (identifier);
9984   /* Parse the body of the namespace.  */
9985   cp_parser_namespace_body (parser);
9986   /* Finish the namespace.  */
9987   pop_namespace ();
9988   /* Look for the final `}'.  */
9989   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
9990 }
9991
9992 /* Parse a namespace-body.
9993
9994    namespace-body:
9995      declaration-seq [opt]  */
9996
9997 static void
9998 cp_parser_namespace_body (cp_parser* parser)
9999 {
10000   cp_parser_declaration_seq_opt (parser);
10001 }
10002
10003 /* Parse a namespace-alias-definition.
10004
10005    namespace-alias-definition:
10006      namespace identifier = qualified-namespace-specifier ;  */
10007
10008 static void
10009 cp_parser_namespace_alias_definition (cp_parser* parser)
10010 {
10011   tree identifier;
10012   tree namespace_specifier;
10013
10014   /* Look for the `namespace' keyword.  */
10015   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10016   /* Look for the identifier.  */
10017   identifier = cp_parser_identifier (parser);
10018   if (identifier == error_mark_node)
10019     return;
10020   /* Look for the `=' token.  */
10021   cp_parser_require (parser, CPP_EQ, "`='");
10022   /* Look for the qualified-namespace-specifier.  */
10023   namespace_specifier
10024     = cp_parser_qualified_namespace_specifier (parser);
10025   /* Look for the `;' token.  */
10026   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10027
10028   /* Register the alias in the symbol table.  */
10029   do_namespace_alias (identifier, namespace_specifier);
10030 }
10031
10032 /* Parse a qualified-namespace-specifier.
10033
10034    qualified-namespace-specifier:
10035      :: [opt] nested-name-specifier [opt] namespace-name
10036
10037    Returns a NAMESPACE_DECL corresponding to the specified
10038    namespace.  */
10039
10040 static tree
10041 cp_parser_qualified_namespace_specifier (cp_parser* parser)
10042 {
10043   /* Look for the optional `::'.  */
10044   cp_parser_global_scope_opt (parser,
10045                               /*current_scope_valid_p=*/false);
10046
10047   /* Look for the optional nested-name-specifier.  */
10048   cp_parser_nested_name_specifier_opt (parser,
10049                                        /*typename_keyword_p=*/false,
10050                                        /*check_dependency_p=*/true,
10051                                        /*type_p=*/false,
10052                                        /*is_declaration=*/true);
10053
10054   return cp_parser_namespace_name (parser);
10055 }
10056
10057 /* Parse a using-declaration.
10058
10059    using-declaration:
10060      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
10061      using :: unqualified-id ;  */
10062
10063 static void
10064 cp_parser_using_declaration (cp_parser* parser)
10065 {
10066   cp_token *token;
10067   bool typename_p = false;
10068   bool global_scope_p;
10069   tree decl;
10070   tree identifier;
10071   tree scope;
10072   tree qscope;
10073
10074   /* Look for the `using' keyword.  */
10075   cp_parser_require_keyword (parser, RID_USING, "`using'");
10076
10077   /* Peek at the next token.  */
10078   token = cp_lexer_peek_token (parser->lexer);
10079   /* See if it's `typename'.  */
10080   if (token->keyword == RID_TYPENAME)
10081     {
10082       /* Remember that we've seen it.  */
10083       typename_p = true;
10084       /* Consume the `typename' token.  */
10085       cp_lexer_consume_token (parser->lexer);
10086     }
10087
10088   /* Look for the optional global scope qualification.  */
10089   global_scope_p
10090     = (cp_parser_global_scope_opt (parser,
10091                                    /*current_scope_valid_p=*/false)
10092        != NULL_TREE);
10093
10094   /* If we saw `typename', or didn't see `::', then there must be a
10095      nested-name-specifier present.  */
10096   if (typename_p || !global_scope_p)
10097     qscope = cp_parser_nested_name_specifier (parser, typename_p,
10098                                               /*check_dependency_p=*/true,
10099                                               /*type_p=*/false,
10100                                               /*is_declaration=*/true);
10101   /* Otherwise, we could be in either of the two productions.  In that
10102      case, treat the nested-name-specifier as optional.  */
10103   else
10104     qscope = cp_parser_nested_name_specifier_opt (parser,
10105                                                   /*typename_keyword_p=*/false,
10106                                                   /*check_dependency_p=*/true,
10107                                                   /*type_p=*/false,
10108                                                   /*is_declaration=*/true);
10109   if (!qscope)
10110     qscope = global_namespace;
10111
10112   /* Parse the unqualified-id.  */
10113   identifier = cp_parser_unqualified_id (parser,
10114                                          /*template_keyword_p=*/false,
10115                                          /*check_dependency_p=*/true,
10116                                          /*declarator_p=*/true);
10117
10118   /* The function we call to handle a using-declaration is different
10119      depending on what scope we are in.  */
10120   if (identifier == error_mark_node)
10121     ;
10122   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
10123            && TREE_CODE (identifier) != BIT_NOT_EXPR)
10124     /* [namespace.udecl]
10125
10126        A using declaration shall not name a template-id.  */
10127     error ("a template-id may not appear in a using-declaration");
10128   else
10129     {
10130       scope = current_scope ();
10131       if (scope && TYPE_P (scope))
10132         {
10133           /* Create the USING_DECL.  */
10134           decl = do_class_using_decl (build_nt (SCOPE_REF,
10135                                                 parser->scope,
10136                                                 identifier));
10137           /* Add it to the list of members in this class.  */
10138           finish_member_declaration (decl);
10139         }
10140       else
10141         {
10142           decl = cp_parser_lookup_name_simple (parser, identifier);
10143           if (decl == error_mark_node)
10144             cp_parser_name_lookup_error (parser, identifier, decl, NULL);
10145           else if (scope)
10146             do_local_using_decl (decl, qscope, identifier);
10147           else
10148             do_toplevel_using_decl (decl, qscope, identifier);
10149         }
10150     }
10151
10152   /* Look for the final `;'.  */
10153   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10154 }
10155
10156 /* Parse a using-directive.
10157
10158    using-directive:
10159      using namespace :: [opt] nested-name-specifier [opt]
10160        namespace-name ;  */
10161
10162 static void
10163 cp_parser_using_directive (cp_parser* parser)
10164 {
10165   tree namespace_decl;
10166   tree attribs;
10167
10168   /* Look for the `using' keyword.  */
10169   cp_parser_require_keyword (parser, RID_USING, "`using'");
10170   /* And the `namespace' keyword.  */
10171   cp_parser_require_keyword (parser, RID_NAMESPACE, "`namespace'");
10172   /* Look for the optional `::' operator.  */
10173   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
10174   /* And the optional nested-name-specifier.  */
10175   cp_parser_nested_name_specifier_opt (parser,
10176                                        /*typename_keyword_p=*/false,
10177                                        /*check_dependency_p=*/true,
10178                                        /*type_p=*/false,
10179                                        /*is_declaration=*/true);
10180   /* Get the namespace being used.  */
10181   namespace_decl = cp_parser_namespace_name (parser);
10182   /* And any specified attributes.  */
10183   attribs = cp_parser_attributes_opt (parser);
10184   /* Update the symbol table.  */
10185   parse_using_directive (namespace_decl, attribs);
10186   /* Look for the final `;'.  */
10187   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10188 }
10189
10190 /* Parse an asm-definition.
10191
10192    asm-definition:
10193      asm ( string-literal ) ;
10194
10195    GNU Extension:
10196
10197    asm-definition:
10198      asm volatile [opt] ( string-literal ) ;
10199      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
10200      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10201                           : asm-operand-list [opt] ) ;
10202      asm volatile [opt] ( string-literal : asm-operand-list [opt]
10203                           : asm-operand-list [opt]
10204                           : asm-operand-list [opt] ) ;  */
10205
10206 static void
10207 cp_parser_asm_definition (cp_parser* parser)
10208 {
10209   tree string;
10210   tree outputs = NULL_TREE;
10211   tree inputs = NULL_TREE;
10212   tree clobbers = NULL_TREE;
10213   tree asm_stmt;
10214   bool volatile_p = false;
10215   bool extended_p = false;
10216
10217   /* Look for the `asm' keyword.  */
10218   cp_parser_require_keyword (parser, RID_ASM, "`asm'");
10219   /* See if the next token is `volatile'.  */
10220   if (cp_parser_allow_gnu_extensions_p (parser)
10221       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
10222     {
10223       /* Remember that we saw the `volatile' keyword.  */
10224       volatile_p = true;
10225       /* Consume the token.  */
10226       cp_lexer_consume_token (parser->lexer);
10227     }
10228   /* Look for the opening `('.  */
10229   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
10230     return;
10231   /* Look for the string.  */
10232   string = cp_parser_string_literal (parser, false, false);
10233   if (string == error_mark_node)
10234     {
10235       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10236                                              /*consume_paren=*/true);
10237       return;
10238     }
10239
10240   /* If we're allowing GNU extensions, check for the extended assembly
10241      syntax.  Unfortunately, the `:' tokens need not be separated by
10242      a space in C, and so, for compatibility, we tolerate that here
10243      too.  Doing that means that we have to treat the `::' operator as
10244      two `:' tokens.  */
10245   if (cp_parser_allow_gnu_extensions_p (parser)
10246       && at_function_scope_p ()
10247       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
10248           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
10249     {
10250       bool inputs_p = false;
10251       bool clobbers_p = false;
10252
10253       /* The extended syntax was used.  */
10254       extended_p = true;
10255
10256       /* Look for outputs.  */
10257       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10258         {
10259           /* Consume the `:'.  */
10260           cp_lexer_consume_token (parser->lexer);
10261           /* Parse the output-operands.  */
10262           if (cp_lexer_next_token_is_not (parser->lexer,
10263                                           CPP_COLON)
10264               && cp_lexer_next_token_is_not (parser->lexer,
10265                                              CPP_SCOPE)
10266               && cp_lexer_next_token_is_not (parser->lexer,
10267                                              CPP_CLOSE_PAREN))
10268             outputs = cp_parser_asm_operand_list (parser);
10269         }
10270       /* If the next token is `::', there are no outputs, and the
10271          next token is the beginning of the inputs.  */
10272       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10273         /* The inputs are coming next.  */
10274         inputs_p = true;
10275
10276       /* Look for inputs.  */
10277       if (inputs_p
10278           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10279         {
10280           /* Consume the `:' or `::'.  */
10281           cp_lexer_consume_token (parser->lexer);
10282           /* Parse the output-operands.  */
10283           if (cp_lexer_next_token_is_not (parser->lexer,
10284                                           CPP_COLON)
10285               && cp_lexer_next_token_is_not (parser->lexer,
10286                                              CPP_CLOSE_PAREN))
10287             inputs = cp_parser_asm_operand_list (parser);
10288         }
10289       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
10290         /* The clobbers are coming next.  */
10291         clobbers_p = true;
10292
10293       /* Look for clobbers.  */
10294       if (clobbers_p
10295           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
10296         {
10297           /* Consume the `:' or `::'.  */
10298           cp_lexer_consume_token (parser->lexer);
10299           /* Parse the clobbers.  */
10300           if (cp_lexer_next_token_is_not (parser->lexer,
10301                                           CPP_CLOSE_PAREN))
10302             clobbers = cp_parser_asm_clobber_list (parser);
10303         }
10304     }
10305   /* Look for the closing `)'.  */
10306   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10307     cp_parser_skip_to_closing_parenthesis (parser, true, false,
10308                                            /*consume_paren=*/true);
10309   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
10310
10311   /* Create the ASM_EXPR.  */
10312   if (at_function_scope_p ())
10313     {
10314       asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
10315                                   inputs, clobbers);
10316       /* If the extended syntax was not used, mark the ASM_EXPR.  */
10317       if (!extended_p)
10318         {
10319           tree temp = asm_stmt;
10320           if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
10321             temp = TREE_OPERAND (temp, 0);
10322           
10323           ASM_INPUT_P (temp) = 1;
10324         }
10325     }
10326   else
10327     assemble_asm (string);
10328 }
10329
10330 /* Declarators [gram.dcl.decl] */
10331
10332 /* Parse an init-declarator.
10333
10334    init-declarator:
10335      declarator initializer [opt]
10336
10337    GNU Extension:
10338
10339    init-declarator:
10340      declarator asm-specification [opt] attributes [opt] initializer [opt]
10341
10342    function-definition:
10343      decl-specifier-seq [opt] declarator ctor-initializer [opt]
10344        function-body
10345      decl-specifier-seq [opt] declarator function-try-block
10346
10347    GNU Extension:
10348
10349    function-definition:
10350      __extension__ function-definition
10351
10352    The DECL_SPECIFIERS and PREFIX_ATTRIBUTES apply to this declarator.
10353    Returns a representation of the entity declared.  If MEMBER_P is TRUE,
10354    then this declarator appears in a class scope.  The new DECL created
10355    by this declarator is returned.
10356
10357    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
10358    for a function-definition here as well.  If the declarator is a
10359    declarator for a function-definition, *FUNCTION_DEFINITION_P will
10360    be TRUE upon return.  By that point, the function-definition will
10361    have been completely parsed.
10362
10363    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
10364    is FALSE.  */
10365
10366 static tree
10367 cp_parser_init_declarator (cp_parser* parser,
10368                            cp_decl_specifier_seq *decl_specifiers,
10369                            bool function_definition_allowed_p,
10370                            bool member_p,
10371                            int declares_class_or_enum,
10372                            bool* function_definition_p)
10373 {
10374   cp_token *token;
10375   cp_declarator *declarator;
10376   tree prefix_attributes;
10377   tree attributes;
10378   tree asm_specification;
10379   tree initializer;
10380   tree decl = NULL_TREE;
10381   tree scope;
10382   bool is_initialized;
10383   bool is_parenthesized_init;
10384   bool is_non_constant_init;
10385   int ctor_dtor_or_conv_p;
10386   bool friend_p;
10387   bool pop_p = false;
10388
10389   /* Gather the attributes that were provided with the
10390      decl-specifiers.  */
10391   prefix_attributes = decl_specifiers->attributes;
10392
10393   /* Assume that this is not the declarator for a function
10394      definition.  */
10395   if (function_definition_p)
10396     *function_definition_p = false;
10397
10398   /* Defer access checks while parsing the declarator; we cannot know
10399      what names are accessible until we know what is being
10400      declared.  */
10401   resume_deferring_access_checks ();
10402
10403   /* Parse the declarator.  */
10404   declarator
10405     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
10406                             &ctor_dtor_or_conv_p,
10407                             /*parenthesized_p=*/NULL,
10408                             /*member_p=*/false);
10409   /* Gather up the deferred checks.  */
10410   stop_deferring_access_checks ();
10411
10412   /* If the DECLARATOR was erroneous, there's no need to go
10413      further.  */
10414   if (declarator == cp_error_declarator)
10415     return error_mark_node;
10416
10417   cp_parser_check_for_definition_in_return_type (declarator,
10418                                                  declares_class_or_enum);
10419
10420   /* Figure out what scope the entity declared by the DECLARATOR is
10421      located in.  `grokdeclarator' sometimes changes the scope, so
10422      we compute it now.  */
10423   scope = get_scope_of_declarator (declarator);
10424
10425   /* If we're allowing GNU extensions, look for an asm-specification
10426      and attributes.  */
10427   if (cp_parser_allow_gnu_extensions_p (parser))
10428     {
10429       /* Look for an asm-specification.  */
10430       asm_specification = cp_parser_asm_specification_opt (parser);
10431       /* And attributes.  */
10432       attributes = cp_parser_attributes_opt (parser);
10433     }
10434   else
10435     {
10436       asm_specification = NULL_TREE;
10437       attributes = NULL_TREE;
10438     }
10439
10440   /* Peek at the next token.  */
10441   token = cp_lexer_peek_token (parser->lexer);
10442   /* Check to see if the token indicates the start of a
10443      function-definition.  */
10444   if (cp_parser_token_starts_function_definition_p (token))
10445     {
10446       if (!function_definition_allowed_p)
10447         {
10448           /* If a function-definition should not appear here, issue an
10449              error message.  */
10450           cp_parser_error (parser,
10451                            "a function-definition is not allowed here");
10452           return error_mark_node;
10453         }
10454       else
10455         {
10456           /* Neither attributes nor an asm-specification are allowed
10457              on a function-definition.  */
10458           if (asm_specification)
10459             error ("an asm-specification is not allowed on a function-definition");
10460           if (attributes)
10461             error ("attributes are not allowed on a function-definition");
10462           /* This is a function-definition.  */
10463           *function_definition_p = true;
10464
10465           /* Parse the function definition.  */
10466           if (member_p)
10467             decl = cp_parser_save_member_function_body (parser,
10468                                                         decl_specifiers,
10469                                                         declarator,
10470                                                         prefix_attributes);
10471           else
10472             decl
10473               = (cp_parser_function_definition_from_specifiers_and_declarator
10474                  (parser, decl_specifiers, prefix_attributes, declarator));
10475
10476           return decl;
10477         }
10478     }
10479
10480   /* [dcl.dcl]
10481
10482      Only in function declarations for constructors, destructors, and
10483      type conversions can the decl-specifier-seq be omitted.
10484
10485      We explicitly postpone this check past the point where we handle
10486      function-definitions because we tolerate function-definitions
10487      that are missing their return types in some modes.  */
10488   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
10489     {
10490       cp_parser_error (parser,
10491                        "expected constructor, destructor, or type conversion");
10492       return error_mark_node;
10493     }
10494
10495   /* An `=' or an `(' indicates an initializer.  */
10496   is_initialized = (token->type == CPP_EQ
10497                      || token->type == CPP_OPEN_PAREN);
10498   /* If the init-declarator isn't initialized and isn't followed by a
10499      `,' or `;', it's not a valid init-declarator.  */
10500   if (!is_initialized
10501       && token->type != CPP_COMMA
10502       && token->type != CPP_SEMICOLON)
10503     {
10504       cp_parser_error (parser, "expected initializer");
10505       return error_mark_node;
10506     }
10507
10508   /* Because start_decl has side-effects, we should only call it if we
10509      know we're going ahead.  By this point, we know that we cannot
10510      possibly be looking at any other construct.  */
10511   cp_parser_commit_to_tentative_parse (parser);
10512
10513   /* If the decl specifiers were bad, issue an error now that we're
10514      sure this was intended to be a declarator.  Then continue
10515      declaring the variable(s), as int, to try to cut down on further
10516      errors.  */
10517   if (decl_specifiers->any_specifiers_p
10518       && decl_specifiers->type == error_mark_node)
10519     {
10520       cp_parser_error (parser, "invalid type in declaration");
10521       decl_specifiers->type = integer_type_node;
10522     }
10523
10524   /* Check to see whether or not this declaration is a friend.  */
10525   friend_p = cp_parser_friend_p (decl_specifiers);
10526
10527   /* Check that the number of template-parameter-lists is OK.  */
10528   if (!cp_parser_check_declarator_template_parameters (parser, declarator))
10529     return error_mark_node;
10530
10531   /* Enter the newly declared entry in the symbol table.  If we're
10532      processing a declaration in a class-specifier, we wait until
10533      after processing the initializer.  */
10534   if (!member_p)
10535     {
10536       if (parser->in_unbraced_linkage_specification_p)
10537         {
10538           decl_specifiers->storage_class = sc_extern;
10539           have_extern_spec = false;
10540         }
10541       decl = start_decl (declarator, decl_specifiers,
10542                          is_initialized, attributes, prefix_attributes,
10543                          &pop_p);
10544     }
10545   else if (scope)
10546     /* Enter the SCOPE.  That way unqualified names appearing in the
10547        initializer will be looked up in SCOPE.  */
10548     pop_p = push_scope (scope);
10549
10550   /* Perform deferred access control checks, now that we know in which
10551      SCOPE the declared entity resides.  */
10552   if (!member_p && decl)
10553     {
10554       tree saved_current_function_decl = NULL_TREE;
10555
10556       /* If the entity being declared is a function, pretend that we
10557          are in its scope.  If it is a `friend', it may have access to
10558          things that would not otherwise be accessible.  */
10559       if (TREE_CODE (decl) == FUNCTION_DECL)
10560         {
10561           saved_current_function_decl = current_function_decl;
10562           current_function_decl = decl;
10563         }
10564
10565       /* Perform the access control checks for the declarator and the
10566          the decl-specifiers.  */
10567       perform_deferred_access_checks ();
10568
10569       /* Restore the saved value.  */
10570       if (TREE_CODE (decl) == FUNCTION_DECL)
10571         current_function_decl = saved_current_function_decl;
10572     }
10573
10574   /* Parse the initializer.  */
10575   if (is_initialized)
10576     initializer = cp_parser_initializer (parser,
10577                                          &is_parenthesized_init,
10578                                          &is_non_constant_init);
10579   else
10580     {
10581       initializer = NULL_TREE;
10582       is_parenthesized_init = false;
10583       is_non_constant_init = true;
10584     }
10585
10586   /* The old parser allows attributes to appear after a parenthesized
10587      initializer.  Mark Mitchell proposed removing this functionality
10588      on the GCC mailing lists on 2002-08-13.  This parser accepts the
10589      attributes -- but ignores them.  */
10590   if (cp_parser_allow_gnu_extensions_p (parser) && is_parenthesized_init)
10591     if (cp_parser_attributes_opt (parser))
10592       warning ("attributes after parenthesized initializer ignored");
10593
10594   /* For an in-class declaration, use `grokfield' to create the
10595      declaration.  */
10596   if (member_p)
10597     {
10598       if (pop_p)
10599         pop_scope (scope);
10600       decl = grokfield (declarator, decl_specifiers,
10601                         initializer, /*asmspec=*/NULL_TREE,
10602                         /*attributes=*/NULL_TREE);
10603       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
10604         cp_parser_save_default_args (parser, decl);
10605     }
10606
10607   /* Finish processing the declaration.  But, skip friend
10608      declarations.  */
10609   if (!friend_p && decl && decl != error_mark_node)
10610     {
10611       cp_finish_decl (decl,
10612                       initializer,
10613                       asm_specification,
10614                       /* If the initializer is in parentheses, then this is
10615                          a direct-initialization, which means that an
10616                          `explicit' constructor is OK.  Otherwise, an
10617                          `explicit' constructor cannot be used.  */
10618                       ((is_parenthesized_init || !is_initialized)
10619                      ? 0 : LOOKUP_ONLYCONVERTING));
10620       if (pop_p)
10621         pop_scope (DECL_CONTEXT (decl));
10622     }
10623
10624   /* Remember whether or not variables were initialized by
10625      constant-expressions.  */
10626   if (decl && TREE_CODE (decl) == VAR_DECL
10627       && is_initialized && !is_non_constant_init)
10628     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = true;
10629
10630   return decl;
10631 }
10632
10633 /* Parse a declarator.
10634
10635    declarator:
10636      direct-declarator
10637      ptr-operator declarator
10638
10639    abstract-declarator:
10640      ptr-operator abstract-declarator [opt]
10641      direct-abstract-declarator
10642
10643    GNU Extensions:
10644
10645    declarator:
10646      attributes [opt] direct-declarator
10647      attributes [opt] ptr-operator declarator
10648
10649    abstract-declarator:
10650      attributes [opt] ptr-operator abstract-declarator [opt]
10651      attributes [opt] direct-abstract-declarator
10652
10653    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
10654    detect constructor, destructor or conversion operators. It is set
10655    to -1 if the declarator is a name, and +1 if it is a
10656    function. Otherwise it is set to zero. Usually you just want to
10657    test for >0, but internally the negative value is used.
10658
10659    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
10660    a decl-specifier-seq unless it declares a constructor, destructor,
10661    or conversion.  It might seem that we could check this condition in
10662    semantic analysis, rather than parsing, but that makes it difficult
10663    to handle something like `f()'.  We want to notice that there are
10664    no decl-specifiers, and therefore realize that this is an
10665    expression, not a declaration.)
10666
10667    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
10668    the declarator is a direct-declarator of the form "(...)".  
10669
10670    MEMBER_P is true iff this declarator is a member-declarator.  */
10671
10672 static cp_declarator *
10673 cp_parser_declarator (cp_parser* parser,
10674                       cp_parser_declarator_kind dcl_kind,
10675                       int* ctor_dtor_or_conv_p,
10676                       bool* parenthesized_p,
10677                       bool member_p)
10678 {
10679   cp_token *token;
10680   cp_declarator *declarator;
10681   enum tree_code code;
10682   cp_cv_quals cv_quals;
10683   tree class_type;
10684   tree attributes = NULL_TREE;
10685
10686   /* Assume this is not a constructor, destructor, or type-conversion
10687      operator.  */
10688   if (ctor_dtor_or_conv_p)
10689     *ctor_dtor_or_conv_p = 0;
10690
10691   if (cp_parser_allow_gnu_extensions_p (parser))
10692     attributes = cp_parser_attributes_opt (parser);
10693
10694   /* Peek at the next token.  */
10695   token = cp_lexer_peek_token (parser->lexer);
10696
10697   /* Check for the ptr-operator production.  */
10698   cp_parser_parse_tentatively (parser);
10699   /* Parse the ptr-operator.  */
10700   code = cp_parser_ptr_operator (parser,
10701                                  &class_type,
10702                                  &cv_quals);
10703   /* If that worked, then we have a ptr-operator.  */
10704   if (cp_parser_parse_definitely (parser))
10705     {
10706       /* If a ptr-operator was found, then this declarator was not
10707          parenthesized.  */
10708       if (parenthesized_p)
10709         *parenthesized_p = true;
10710       /* The dependent declarator is optional if we are parsing an
10711          abstract-declarator.  */
10712       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10713         cp_parser_parse_tentatively (parser);
10714
10715       /* Parse the dependent declarator.  */
10716       declarator = cp_parser_declarator (parser, dcl_kind,
10717                                          /*ctor_dtor_or_conv_p=*/NULL,
10718                                          /*parenthesized_p=*/NULL,
10719                                          /*member_p=*/false);
10720
10721       /* If we are parsing an abstract-declarator, we must handle the
10722          case where the dependent declarator is absent.  */
10723       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
10724           && !cp_parser_parse_definitely (parser))
10725         declarator = NULL;
10726
10727       /* Build the representation of the ptr-operator.  */
10728       if (class_type)
10729         declarator = make_ptrmem_declarator (cv_quals,
10730                                              class_type,
10731                                              declarator);
10732       else if (code == INDIRECT_REF)
10733         declarator = make_pointer_declarator (cv_quals, declarator);
10734       else
10735         declarator = make_reference_declarator (cv_quals, declarator);
10736     }
10737   /* Everything else is a direct-declarator.  */
10738   else
10739     {
10740       if (parenthesized_p)
10741         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
10742                                                    CPP_OPEN_PAREN);
10743       declarator = cp_parser_direct_declarator (parser, dcl_kind,
10744                                                 ctor_dtor_or_conv_p,
10745                                                 member_p);
10746     }
10747
10748   if (attributes && declarator != cp_error_declarator)
10749     declarator->attributes = attributes;
10750
10751   return declarator;
10752 }
10753
10754 /* Parse a direct-declarator or direct-abstract-declarator.
10755
10756    direct-declarator:
10757      declarator-id
10758      direct-declarator ( parameter-declaration-clause )
10759        cv-qualifier-seq [opt]
10760        exception-specification [opt]
10761      direct-declarator [ constant-expression [opt] ]
10762      ( declarator )
10763
10764    direct-abstract-declarator:
10765      direct-abstract-declarator [opt]
10766        ( parameter-declaration-clause )
10767        cv-qualifier-seq [opt]
10768        exception-specification [opt]
10769      direct-abstract-declarator [opt] [ constant-expression [opt] ]
10770      ( abstract-declarator )
10771
10772    Returns a representation of the declarator.  DCL_KIND is
10773    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
10774    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
10775    we are parsing a direct-declarator.  It is
10776    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
10777    of ambiguity we prefer an abstract declarator, as per
10778    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
10779    cp_parser_declarator.  */
10780
10781 static cp_declarator *
10782 cp_parser_direct_declarator (cp_parser* parser,
10783                              cp_parser_declarator_kind dcl_kind,
10784                              int* ctor_dtor_or_conv_p,
10785                              bool member_p)
10786 {
10787   cp_token *token;
10788   cp_declarator *declarator = NULL;
10789   tree scope = NULL_TREE;
10790   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
10791   bool saved_in_declarator_p = parser->in_declarator_p;
10792   bool first = true;
10793   bool pop_p = false;
10794
10795   while (true)
10796     {
10797       /* Peek at the next token.  */
10798       token = cp_lexer_peek_token (parser->lexer);
10799       if (token->type == CPP_OPEN_PAREN)
10800         {
10801           /* This is either a parameter-declaration-clause, or a
10802              parenthesized declarator. When we know we are parsing a
10803              named declarator, it must be a parenthesized declarator
10804              if FIRST is true. For instance, `(int)' is a
10805              parameter-declaration-clause, with an omitted
10806              direct-abstract-declarator. But `((*))', is a
10807              parenthesized abstract declarator. Finally, when T is a
10808              template parameter `(T)' is a
10809              parameter-declaration-clause, and not a parenthesized
10810              named declarator.
10811
10812              We first try and parse a parameter-declaration-clause,
10813              and then try a nested declarator (if FIRST is true).
10814
10815              It is not an error for it not to be a
10816              parameter-declaration-clause, even when FIRST is
10817              false. Consider,
10818
10819                int i (int);
10820                int i (3);
10821
10822              The first is the declaration of a function while the
10823              second is a the definition of a variable, including its
10824              initializer.
10825
10826              Having seen only the parenthesis, we cannot know which of
10827              these two alternatives should be selected.  Even more
10828              complex are examples like:
10829
10830                int i (int (a));
10831                int i (int (3));
10832
10833              The former is a function-declaration; the latter is a
10834              variable initialization.
10835
10836              Thus again, we try a parameter-declaration-clause, and if
10837              that fails, we back out and return.  */
10838
10839           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10840             {
10841               cp_parameter_declarator *params;
10842               unsigned saved_num_template_parameter_lists;
10843
10844               /* In a member-declarator, the only valid interpretation
10845                  of a parenthesis is the start of a
10846                  parameter-declaration-clause.  (It is invalid to
10847                  initialize a static data member with a parenthesized
10848                  initializer; only the "=" form of initialization is
10849                  permitted.)  */
10850               if (!member_p)
10851                 cp_parser_parse_tentatively (parser);
10852
10853               /* Consume the `('.  */
10854               cp_lexer_consume_token (parser->lexer);
10855               if (first)
10856                 {
10857                   /* If this is going to be an abstract declarator, we're
10858                      in a declarator and we can't have default args.  */
10859                   parser->default_arg_ok_p = false;
10860                   parser->in_declarator_p = true;
10861                 }
10862
10863               /* Inside the function parameter list, surrounding
10864                  template-parameter-lists do not apply.  */
10865               saved_num_template_parameter_lists
10866                 = parser->num_template_parameter_lists;
10867               parser->num_template_parameter_lists = 0;
10868
10869               /* Parse the parameter-declaration-clause.  */
10870               params = cp_parser_parameter_declaration_clause (parser);
10871
10872               parser->num_template_parameter_lists
10873                 = saved_num_template_parameter_lists;
10874
10875               /* If all went well, parse the cv-qualifier-seq and the
10876                  exception-specification.  */
10877               if (member_p || cp_parser_parse_definitely (parser))
10878                 {
10879                   cp_cv_quals cv_quals;
10880                   tree exception_specification;
10881
10882                   if (ctor_dtor_or_conv_p)
10883                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
10884                   first = false;
10885                   /* Consume the `)'.  */
10886                   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
10887
10888                   /* Parse the cv-qualifier-seq.  */
10889                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
10890                   /* And the exception-specification.  */
10891                   exception_specification
10892                     = cp_parser_exception_specification_opt (parser);
10893
10894                   /* Create the function-declarator.  */
10895                   declarator = make_call_declarator (declarator,
10896                                                      params,
10897                                                      cv_quals,
10898                                                      exception_specification);
10899                   /* Any subsequent parameter lists are to do with
10900                      return type, so are not those of the declared
10901                      function.  */
10902                   parser->default_arg_ok_p = false;
10903
10904                   /* Repeat the main loop.  */
10905                   continue;
10906                 }
10907             }
10908
10909           /* If this is the first, we can try a parenthesized
10910              declarator.  */
10911           if (first)
10912             {
10913               bool saved_in_type_id_in_expr_p;
10914
10915               parser->default_arg_ok_p = saved_default_arg_ok_p;
10916               parser->in_declarator_p = saved_in_declarator_p;
10917
10918               /* Consume the `('.  */
10919               cp_lexer_consume_token (parser->lexer);
10920               /* Parse the nested declarator.  */
10921               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
10922               parser->in_type_id_in_expr_p = true;
10923               declarator
10924                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
10925                                         /*parenthesized_p=*/NULL,
10926                                         member_p);
10927               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
10928               first = false;
10929               /* Expect a `)'.  */
10930               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
10931                 declarator = cp_error_declarator;
10932               if (declarator == cp_error_declarator)
10933                 break;
10934
10935               goto handle_declarator;
10936             }
10937           /* Otherwise, we must be done.  */
10938           else
10939             break;
10940         }
10941       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
10942                && token->type == CPP_OPEN_SQUARE)
10943         {
10944           /* Parse an array-declarator.  */
10945           tree bounds;
10946
10947           if (ctor_dtor_or_conv_p)
10948             *ctor_dtor_or_conv_p = 0;
10949
10950           first = false;
10951           parser->default_arg_ok_p = false;
10952           parser->in_declarator_p = true;
10953           /* Consume the `['.  */
10954           cp_lexer_consume_token (parser->lexer);
10955           /* Peek at the next token.  */
10956           token = cp_lexer_peek_token (parser->lexer);
10957           /* If the next token is `]', then there is no
10958              constant-expression.  */
10959           if (token->type != CPP_CLOSE_SQUARE)
10960             {
10961               bool non_constant_p;
10962
10963               bounds
10964                 = cp_parser_constant_expression (parser,
10965                                                  /*allow_non_constant=*/true,
10966                                                  &non_constant_p);
10967               if (!non_constant_p)
10968                 bounds = fold_non_dependent_expr (bounds);
10969             }
10970           else
10971             bounds = NULL_TREE;
10972           /* Look for the closing `]'.  */
10973           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'"))
10974             {
10975               declarator = cp_error_declarator;
10976               break;
10977             }
10978
10979           declarator = make_array_declarator (declarator, bounds);
10980         }
10981       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
10982         {
10983           tree id;
10984
10985           /* Parse a declarator-id */
10986           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10987             cp_parser_parse_tentatively (parser);
10988           id = cp_parser_declarator_id (parser);
10989           if (dcl_kind == CP_PARSER_DECLARATOR_EITHER)
10990             {
10991               if (!cp_parser_parse_definitely (parser))
10992                 id = error_mark_node;
10993               else if (TREE_CODE (id) != IDENTIFIER_NODE)
10994                 {
10995                   cp_parser_error (parser, "expected unqualified-id");
10996                   id = error_mark_node;
10997                 }
10998             }
10999
11000           if (id == error_mark_node)
11001             {
11002               declarator = cp_error_declarator;
11003               break;
11004             }
11005
11006           if (TREE_CODE (id) == SCOPE_REF && !current_scope ())
11007             {
11008               tree scope = TREE_OPERAND (id, 0);
11009
11010               /* In the declaration of a member of a template class
11011                  outside of the class itself, the SCOPE will sometimes
11012                  be a TYPENAME_TYPE.  For example, given:
11013
11014                  template <typename T>
11015                  int S<T>::R::i = 3;
11016
11017                  the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
11018                  this context, we must resolve S<T>::R to an ordinary
11019                  type, rather than a typename type.
11020
11021                  The reason we normally avoid resolving TYPENAME_TYPEs
11022                  is that a specialization of `S' might render
11023                  `S<T>::R' not a type.  However, if `S' is
11024                  specialized, then this `i' will not be used, so there
11025                  is no harm in resolving the types here.  */
11026               if (TREE_CODE (scope) == TYPENAME_TYPE)
11027                 {
11028                   tree type;
11029
11030                   /* Resolve the TYPENAME_TYPE.  */
11031                   type = resolve_typename_type (scope,
11032                                                  /*only_current_p=*/false);
11033                   /* If that failed, the declarator is invalid.  */
11034                   if (type == error_mark_node)
11035                     error ("%<%T::%D%> is not a type",
11036                            TYPE_CONTEXT (scope),
11037                            TYPE_IDENTIFIER (scope));
11038                   /* Build a new DECLARATOR.  */
11039                   id = build_nt (SCOPE_REF, type, TREE_OPERAND (id, 1));
11040                 }
11041             }
11042
11043           declarator = make_id_declarator (id);
11044           if (id)
11045             {
11046               tree class_type;
11047               tree unqualified_name;
11048
11049               if (TREE_CODE (id) == SCOPE_REF
11050                   && CLASS_TYPE_P (TREE_OPERAND (id, 0)))
11051                 {
11052                   class_type = TREE_OPERAND (id, 0);
11053                   unqualified_name = TREE_OPERAND (id, 1);
11054                 }
11055               else
11056                 {
11057                   class_type = current_class_type;
11058                   unqualified_name = id;
11059                 }
11060
11061               if (class_type)
11062                 {
11063                   if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
11064                     declarator->u.id.sfk = sfk_destructor;
11065                   else if (IDENTIFIER_TYPENAME_P (unqualified_name))
11066                     declarator->u.id.sfk = sfk_conversion;
11067                   else if (constructor_name_p (unqualified_name,
11068                                                class_type)
11069                            || (TREE_CODE (unqualified_name) == TYPE_DECL
11070                                && same_type_p (TREE_TYPE (unqualified_name),
11071                                                class_type)))
11072                     declarator->u.id.sfk = sfk_constructor;
11073
11074                   if (ctor_dtor_or_conv_p && declarator->u.id.sfk != sfk_none)
11075                     *ctor_dtor_or_conv_p = -1;
11076                   if (TREE_CODE (id) == SCOPE_REF
11077                       && TREE_CODE (unqualified_name) == TYPE_DECL
11078                       && CLASSTYPE_USE_TEMPLATE (TREE_TYPE (unqualified_name)))
11079                     {
11080                       error ("invalid use of constructor as a template");
11081                       inform ("use %<%T::%D%> instead of %<%T::%T%> to name "
11082                               "the constructor in a qualified name",
11083                               class_type,
11084                               DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
11085                               class_type, class_type);
11086                     }
11087                 }
11088             }
11089
11090         handle_declarator:;
11091           scope = get_scope_of_declarator (declarator);
11092           if (scope)
11093             /* Any names that appear after the declarator-id for a
11094                member are looked up in the containing scope.  */
11095             pop_p = push_scope (scope);
11096           parser->in_declarator_p = true;
11097           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
11098               || (declarator && declarator->kind == cdk_id))
11099             /* Default args are only allowed on function
11100                declarations.  */
11101             parser->default_arg_ok_p = saved_default_arg_ok_p;
11102           else
11103             parser->default_arg_ok_p = false;
11104
11105           first = false;
11106         }
11107       /* We're done.  */
11108       else
11109         break;
11110     }
11111
11112   /* For an abstract declarator, we might wind up with nothing at this
11113      point.  That's an error; the declarator is not optional.  */
11114   if (!declarator)
11115     cp_parser_error (parser, "expected declarator");
11116
11117   /* If we entered a scope, we must exit it now.  */
11118   if (pop_p)
11119     pop_scope (scope);
11120
11121   parser->default_arg_ok_p = saved_default_arg_ok_p;
11122   parser->in_declarator_p = saved_in_declarator_p;
11123
11124   return declarator;
11125 }
11126
11127 /* Parse a ptr-operator.
11128
11129    ptr-operator:
11130      * cv-qualifier-seq [opt]
11131      &
11132      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
11133
11134    GNU Extension:
11135
11136    ptr-operator:
11137      & cv-qualifier-seq [opt]
11138
11139    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
11140    Returns ADDR_EXPR if a reference was used.  In the case of a
11141    pointer-to-member, *TYPE is filled in with the TYPE containing the
11142    member.  *CV_QUALS is filled in with the cv-qualifier-seq, or
11143    TYPE_UNQUALIFIED, if there are no cv-qualifiers.  Returns
11144    ERROR_MARK if an error occurred.  */
11145
11146 static enum tree_code
11147 cp_parser_ptr_operator (cp_parser* parser,
11148                         tree* type,
11149                         cp_cv_quals *cv_quals)
11150 {
11151   enum tree_code code = ERROR_MARK;
11152   cp_token *token;
11153
11154   /* Assume that it's not a pointer-to-member.  */
11155   *type = NULL_TREE;
11156   /* And that there are no cv-qualifiers.  */
11157   *cv_quals = TYPE_UNQUALIFIED;
11158
11159   /* Peek at the next token.  */
11160   token = cp_lexer_peek_token (parser->lexer);
11161   /* If it's a `*' or `&' we have a pointer or reference.  */
11162   if (token->type == CPP_MULT || token->type == CPP_AND)
11163     {
11164       /* Remember which ptr-operator we were processing.  */
11165       code = (token->type == CPP_AND ? ADDR_EXPR : INDIRECT_REF);
11166
11167       /* Consume the `*' or `&'.  */
11168       cp_lexer_consume_token (parser->lexer);
11169
11170       /* A `*' can be followed by a cv-qualifier-seq, and so can a
11171          `&', if we are allowing GNU extensions.  (The only qualifier
11172          that can legally appear after `&' is `restrict', but that is
11173          enforced during semantic analysis.  */
11174       if (code == INDIRECT_REF
11175           || cp_parser_allow_gnu_extensions_p (parser))
11176         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11177     }
11178   else
11179     {
11180       /* Try the pointer-to-member case.  */
11181       cp_parser_parse_tentatively (parser);
11182       /* Look for the optional `::' operator.  */
11183       cp_parser_global_scope_opt (parser,
11184                                   /*current_scope_valid_p=*/false);
11185       /* Look for the nested-name specifier.  */
11186       cp_parser_nested_name_specifier (parser,
11187                                        /*typename_keyword_p=*/false,
11188                                        /*check_dependency_p=*/true,
11189                                        /*type_p=*/false,
11190                                        /*is_declaration=*/false);
11191       /* If we found it, and the next token is a `*', then we are
11192          indeed looking at a pointer-to-member operator.  */
11193       if (!cp_parser_error_occurred (parser)
11194           && cp_parser_require (parser, CPP_MULT, "`*'"))
11195         {
11196           /* The type of which the member is a member is given by the
11197              current SCOPE.  */
11198           *type = parser->scope;
11199           /* The next name will not be qualified.  */
11200           parser->scope = NULL_TREE;
11201           parser->qualifying_scope = NULL_TREE;
11202           parser->object_scope = NULL_TREE;
11203           /* Indicate that the `*' operator was used.  */
11204           code = INDIRECT_REF;
11205           /* Look for the optional cv-qualifier-seq.  */
11206           *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
11207         }
11208       /* If that didn't work we don't have a ptr-operator.  */
11209       if (!cp_parser_parse_definitely (parser))
11210         cp_parser_error (parser, "expected ptr-operator");
11211     }
11212
11213   return code;
11214 }
11215
11216 /* Parse an (optional) cv-qualifier-seq.
11217
11218    cv-qualifier-seq:
11219      cv-qualifier cv-qualifier-seq [opt]
11220
11221    cv-qualifier:
11222      const
11223      volatile
11224
11225    GNU Extension:
11226
11227    cv-qualifier:
11228      __restrict__
11229
11230    Returns a bitmask representing the cv-qualifiers.  */
11231
11232 static cp_cv_quals
11233 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
11234 {
11235   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
11236
11237   while (true)
11238     {
11239       cp_token *token;
11240       cp_cv_quals cv_qualifier;
11241
11242       /* Peek at the next token.  */
11243       token = cp_lexer_peek_token (parser->lexer);
11244       /* See if it's a cv-qualifier.  */
11245       switch (token->keyword)
11246         {
11247         case RID_CONST:
11248           cv_qualifier = TYPE_QUAL_CONST;
11249           break;
11250
11251         case RID_VOLATILE:
11252           cv_qualifier = TYPE_QUAL_VOLATILE;
11253           break;
11254
11255         case RID_RESTRICT:
11256           cv_qualifier = TYPE_QUAL_RESTRICT;
11257           break;
11258
11259         default:
11260           cv_qualifier = TYPE_UNQUALIFIED;
11261           break;
11262         }
11263
11264       if (!cv_qualifier)
11265         break;
11266
11267       if (cv_quals & cv_qualifier)
11268         {
11269           error ("duplicate cv-qualifier");
11270           cp_lexer_purge_token (parser->lexer);
11271         }
11272       else
11273         {
11274           cp_lexer_consume_token (parser->lexer);
11275           cv_quals |= cv_qualifier;
11276         }
11277     }
11278
11279   return cv_quals;
11280 }
11281
11282 /* Parse a declarator-id.
11283
11284    declarator-id:
11285      id-expression
11286      :: [opt] nested-name-specifier [opt] type-name
11287
11288    In the `id-expression' case, the value returned is as for
11289    cp_parser_id_expression if the id-expression was an unqualified-id.
11290    If the id-expression was a qualified-id, then a SCOPE_REF is
11291    returned.  The first operand is the scope (either a NAMESPACE_DECL
11292    or TREE_TYPE), but the second is still just a representation of an
11293    unqualified-id.  */
11294
11295 static tree
11296 cp_parser_declarator_id (cp_parser* parser)
11297 {
11298   tree id_expression;
11299
11300   /* The expression must be an id-expression.  Assume that qualified
11301      names are the names of types so that:
11302
11303        template <class T>
11304        int S<T>::R::i = 3;
11305
11306      will work; we must treat `S<T>::R' as the name of a type.
11307      Similarly, assume that qualified names are templates, where
11308      required, so that:
11309
11310        template <class T>
11311        int S<T>::R<T>::i = 3;
11312
11313      will work, too.  */
11314   id_expression = cp_parser_id_expression (parser,
11315                                            /*template_keyword_p=*/false,
11316                                            /*check_dependency_p=*/false,
11317                                            /*template_p=*/NULL,
11318                                            /*declarator_p=*/true);
11319   /* If the name was qualified, create a SCOPE_REF to represent
11320      that.  */
11321   if (parser->scope)
11322     {
11323       id_expression = build_nt (SCOPE_REF, parser->scope, id_expression);
11324       parser->scope = NULL_TREE;
11325     }
11326
11327   return id_expression;
11328 }
11329
11330 /* Parse a type-id.
11331
11332    type-id:
11333      type-specifier-seq abstract-declarator [opt]
11334
11335    Returns the TYPE specified.  */
11336
11337 static tree
11338 cp_parser_type_id (cp_parser* parser)
11339 {
11340   cp_decl_specifier_seq type_specifier_seq;
11341   cp_declarator *abstract_declarator;
11342
11343   /* Parse the type-specifier-seq.  */
11344   cp_parser_type_specifier_seq (parser, &type_specifier_seq);
11345   if (type_specifier_seq.type == error_mark_node)
11346     return error_mark_node;
11347
11348   /* There might or might not be an abstract declarator.  */
11349   cp_parser_parse_tentatively (parser);
11350   /* Look for the declarator.  */
11351   abstract_declarator
11352     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
11353                             /*parenthesized_p=*/NULL,
11354                             /*member_p=*/false);
11355   /* Check to see if there really was a declarator.  */
11356   if (!cp_parser_parse_definitely (parser))
11357     abstract_declarator = NULL;
11358
11359   return groktypename (&type_specifier_seq, abstract_declarator);
11360 }
11361
11362 /* Parse a type-specifier-seq.
11363
11364    type-specifier-seq:
11365      type-specifier type-specifier-seq [opt]
11366
11367    GNU extension:
11368
11369    type-specifier-seq:
11370      attributes type-specifier-seq [opt]
11371
11372    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
11373
11374 static void
11375 cp_parser_type_specifier_seq (cp_parser* parser,
11376                               cp_decl_specifier_seq *type_specifier_seq)
11377 {
11378   bool seen_type_specifier = false;
11379
11380   /* Clear the TYPE_SPECIFIER_SEQ.  */
11381   clear_decl_specs (type_specifier_seq);
11382
11383   /* Parse the type-specifiers and attributes.  */
11384   while (true)
11385     {
11386       tree type_specifier;
11387
11388       /* Check for attributes first.  */
11389       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
11390         {
11391           type_specifier_seq->attributes =
11392             chainon (type_specifier_seq->attributes,
11393                      cp_parser_attributes_opt (parser));
11394           continue;
11395         }
11396
11397       /* Look for the type-specifier.  */
11398       type_specifier = cp_parser_type_specifier (parser,
11399                                                  CP_PARSER_FLAGS_OPTIONAL,
11400                                                  type_specifier_seq,
11401                                                  /*is_declaration=*/false,
11402                                                  NULL,
11403                                                  NULL);
11404       /* If the first type-specifier could not be found, this is not a
11405          type-specifier-seq at all.  */
11406       if (!seen_type_specifier && !type_specifier)
11407         {
11408           cp_parser_error (parser, "expected type-specifier");
11409           type_specifier_seq->type = error_mark_node;
11410           return;
11411         }
11412       /* If subsequent type-specifiers could not be found, the
11413          type-specifier-seq is complete.  */
11414       else if (seen_type_specifier && !type_specifier)
11415         break;
11416
11417       seen_type_specifier = true;
11418     }
11419
11420   return;
11421 }
11422
11423 /* Parse a parameter-declaration-clause.
11424
11425    parameter-declaration-clause:
11426      parameter-declaration-list [opt] ... [opt]
11427      parameter-declaration-list , ...
11428
11429    Returns a representation for the parameter declarations.  A return
11430    value of NULL indicates a parameter-declaration-clause consisting
11431    only of an ellipsis.  */
11432
11433 static cp_parameter_declarator *
11434 cp_parser_parameter_declaration_clause (cp_parser* parser)
11435 {
11436   cp_parameter_declarator *parameters;
11437   cp_token *token;
11438   bool ellipsis_p;
11439   bool is_error;
11440
11441   /* Peek at the next token.  */
11442   token = cp_lexer_peek_token (parser->lexer);
11443   /* Check for trivial parameter-declaration-clauses.  */
11444   if (token->type == CPP_ELLIPSIS)
11445     {
11446       /* Consume the `...' token.  */
11447       cp_lexer_consume_token (parser->lexer);
11448       return NULL;
11449     }
11450   else if (token->type == CPP_CLOSE_PAREN)
11451     /* There are no parameters.  */
11452     {
11453 #ifndef NO_IMPLICIT_EXTERN_C
11454       if (in_system_header && current_class_type == NULL
11455           && current_lang_name == lang_name_c)
11456         return NULL;
11457       else
11458 #endif
11459         return no_parameters;
11460     }
11461   /* Check for `(void)', too, which is a special case.  */
11462   else if (token->keyword == RID_VOID
11463            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
11464                == CPP_CLOSE_PAREN))
11465     {
11466       /* Consume the `void' token.  */
11467       cp_lexer_consume_token (parser->lexer);
11468       /* There are no parameters.  */
11469       return no_parameters;
11470     }
11471
11472   /* Parse the parameter-declaration-list.  */
11473   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
11474   /* If a parse error occurred while parsing the
11475      parameter-declaration-list, then the entire
11476      parameter-declaration-clause is erroneous.  */
11477   if (is_error)
11478     return NULL;
11479
11480   /* Peek at the next token.  */
11481   token = cp_lexer_peek_token (parser->lexer);
11482   /* If it's a `,', the clause should terminate with an ellipsis.  */
11483   if (token->type == CPP_COMMA)
11484     {
11485       /* Consume the `,'.  */
11486       cp_lexer_consume_token (parser->lexer);
11487       /* Expect an ellipsis.  */
11488       ellipsis_p
11489         = (cp_parser_require (parser, CPP_ELLIPSIS, "`...'") != NULL);
11490     }
11491   /* It might also be `...' if the optional trailing `,' was
11492      omitted.  */
11493   else if (token->type == CPP_ELLIPSIS)
11494     {
11495       /* Consume the `...' token.  */
11496       cp_lexer_consume_token (parser->lexer);
11497       /* And remember that we saw it.  */
11498       ellipsis_p = true;
11499     }
11500   else
11501     ellipsis_p = false;
11502
11503   /* Finish the parameter list.  */
11504   if (parameters && ellipsis_p)
11505     parameters->ellipsis_p = true;
11506
11507   return parameters;
11508 }
11509
11510 /* Parse a parameter-declaration-list.
11511
11512    parameter-declaration-list:
11513      parameter-declaration
11514      parameter-declaration-list , parameter-declaration
11515
11516    Returns a representation of the parameter-declaration-list, as for
11517    cp_parser_parameter_declaration_clause.  However, the
11518    `void_list_node' is never appended to the list.  Upon return,
11519    *IS_ERROR will be true iff an error occurred.  */
11520
11521 static cp_parameter_declarator *
11522 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
11523 {
11524   cp_parameter_declarator *parameters = NULL;
11525   cp_parameter_declarator **tail = &parameters;
11526
11527   /* Assume all will go well.  */
11528   *is_error = false;
11529
11530   /* Look for more parameters.  */
11531   while (true)
11532     {
11533       cp_parameter_declarator *parameter;
11534       bool parenthesized_p;
11535       /* Parse the parameter.  */
11536       parameter
11537         = cp_parser_parameter_declaration (parser,
11538                                            /*template_parm_p=*/false,
11539                                            &parenthesized_p);
11540
11541       /* If a parse error occurred parsing the parameter declaration,
11542          then the entire parameter-declaration-list is erroneous.  */
11543       if (!parameter)
11544         {
11545           *is_error = true;
11546           parameters = NULL;
11547           break;
11548         }
11549       /* Add the new parameter to the list.  */
11550       *tail = parameter;
11551       tail = &parameter->next;
11552
11553       /* Peek at the next token.  */
11554       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
11555           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11556         /* The parameter-declaration-list is complete.  */
11557         break;
11558       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11559         {
11560           cp_token *token;
11561
11562           /* Peek at the next token.  */
11563           token = cp_lexer_peek_nth_token (parser->lexer, 2);
11564           /* If it's an ellipsis, then the list is complete.  */
11565           if (token->type == CPP_ELLIPSIS)
11566             break;
11567           /* Otherwise, there must be more parameters.  Consume the
11568              `,'.  */
11569           cp_lexer_consume_token (parser->lexer);
11570           /* When parsing something like:
11571
11572                 int i(float f, double d)
11573
11574              we can tell after seeing the declaration for "f" that we
11575              are not looking at an initialization of a variable "i",
11576              but rather at the declaration of a function "i".
11577
11578              Due to the fact that the parsing of template arguments
11579              (as specified to a template-id) requires backtracking we
11580              cannot use this technique when inside a template argument
11581              list.  */
11582           if (!parser->in_template_argument_list_p
11583               && !parser->in_type_id_in_expr_p
11584               && cp_parser_parsing_tentatively (parser)
11585               && !cp_parser_committed_to_tentative_parse (parser)
11586               /* However, a parameter-declaration of the form
11587                  "foat(f)" (which is a valid declaration of a
11588                  parameter "f") can also be interpreted as an
11589                  expression (the conversion of "f" to "float").  */
11590               && !parenthesized_p)
11591             cp_parser_commit_to_tentative_parse (parser);
11592         }
11593       else
11594         {
11595           cp_parser_error (parser, "expected %<,%> or %<...%>");
11596           if (!cp_parser_parsing_tentatively (parser)
11597               || cp_parser_committed_to_tentative_parse (parser))
11598             cp_parser_skip_to_closing_parenthesis (parser,
11599                                                    /*recovering=*/true,
11600                                                    /*or_comma=*/false,
11601                                                    /*consume_paren=*/false);
11602           break;
11603         }
11604     }
11605
11606   return parameters;
11607 }
11608
11609 /* Parse a parameter declaration.
11610
11611    parameter-declaration:
11612      decl-specifier-seq declarator
11613      decl-specifier-seq declarator = assignment-expression
11614      decl-specifier-seq abstract-declarator [opt]
11615      decl-specifier-seq abstract-declarator [opt] = assignment-expression
11616
11617    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
11618    declares a template parameter.  (In that case, a non-nested `>'
11619    token encountered during the parsing of the assignment-expression
11620    is not interpreted as a greater-than operator.)
11621
11622    Returns a representation of the parameter, or NULL if an error
11623    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
11624    true iff the declarator is of the form "(p)".  */
11625
11626 static cp_parameter_declarator *
11627 cp_parser_parameter_declaration (cp_parser *parser,
11628                                  bool template_parm_p,
11629                                  bool *parenthesized_p)
11630 {
11631   int declares_class_or_enum;
11632   bool greater_than_is_operator_p;
11633   cp_decl_specifier_seq decl_specifiers;
11634   cp_declarator *declarator;
11635   tree default_argument;
11636   cp_token *token;
11637   const char *saved_message;
11638
11639   /* In a template parameter, `>' is not an operator.
11640
11641      [temp.param]
11642
11643      When parsing a default template-argument for a non-type
11644      template-parameter, the first non-nested `>' is taken as the end
11645      of the template parameter-list rather than a greater-than
11646      operator.  */
11647   greater_than_is_operator_p = !template_parm_p;
11648
11649   /* Type definitions may not appear in parameter types.  */
11650   saved_message = parser->type_definition_forbidden_message;
11651   parser->type_definition_forbidden_message
11652     = "types may not be defined in parameter types";
11653
11654   /* Parse the declaration-specifiers.  */
11655   cp_parser_decl_specifier_seq (parser,
11656                                 CP_PARSER_FLAGS_NONE,
11657                                 &decl_specifiers,
11658                                 &declares_class_or_enum);
11659   /* If an error occurred, there's no reason to attempt to parse the
11660      rest of the declaration.  */
11661   if (cp_parser_error_occurred (parser))
11662     {
11663       parser->type_definition_forbidden_message = saved_message;
11664       return NULL;
11665     }
11666
11667   /* Peek at the next token.  */
11668   token = cp_lexer_peek_token (parser->lexer);
11669   /* If the next token is a `)', `,', `=', `>', or `...', then there
11670      is no declarator.  */
11671   if (token->type == CPP_CLOSE_PAREN
11672       || token->type == CPP_COMMA
11673       || token->type == CPP_EQ
11674       || token->type == CPP_ELLIPSIS
11675       || token->type == CPP_GREATER)
11676     {
11677       declarator = NULL;
11678       if (parenthesized_p)
11679         *parenthesized_p = false;
11680     }
11681   /* Otherwise, there should be a declarator.  */
11682   else
11683     {
11684       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
11685       parser->default_arg_ok_p = false;
11686
11687       /* After seeing a decl-specifier-seq, if the next token is not a
11688          "(", there is no possibility that the code is a valid
11689          expression.  Therefore, if parsing tentatively, we commit at
11690          this point.  */
11691       if (!parser->in_template_argument_list_p
11692           /* In an expression context, having seen:
11693
11694                (int((char ...
11695
11696              we cannot be sure whether we are looking at a
11697              function-type (taking a "char" as a parameter) or a cast
11698              of some object of type "char" to "int".  */
11699           && !parser->in_type_id_in_expr_p
11700           && cp_parser_parsing_tentatively (parser)
11701           && !cp_parser_committed_to_tentative_parse (parser)
11702           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
11703         cp_parser_commit_to_tentative_parse (parser);
11704       /* Parse the declarator.  */
11705       declarator = cp_parser_declarator (parser,
11706                                          CP_PARSER_DECLARATOR_EITHER,
11707                                          /*ctor_dtor_or_conv_p=*/NULL,
11708                                          parenthesized_p,
11709                                          /*member_p=*/false);
11710       parser->default_arg_ok_p = saved_default_arg_ok_p;
11711       /* After the declarator, allow more attributes.  */
11712       decl_specifiers.attributes
11713         = chainon (decl_specifiers.attributes,
11714                    cp_parser_attributes_opt (parser));
11715     }
11716
11717   /* The restriction on defining new types applies only to the type
11718      of the parameter, not to the default argument.  */
11719   parser->type_definition_forbidden_message = saved_message;
11720
11721   /* If the next token is `=', then process a default argument.  */
11722   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11723     {
11724       bool saved_greater_than_is_operator_p;
11725       /* Consume the `='.  */
11726       cp_lexer_consume_token (parser->lexer);
11727
11728       /* If we are defining a class, then the tokens that make up the
11729          default argument must be saved and processed later.  */
11730       if (!template_parm_p && at_class_scope_p ()
11731           && TYPE_BEING_DEFINED (current_class_type))
11732         {
11733           unsigned depth = 0;
11734           cp_token *first_token;
11735           cp_token *token;
11736
11737           /* Add tokens until we have processed the entire default
11738              argument.  We add the range [first_token, token). */
11739           first_token = cp_lexer_peek_token (parser->lexer);
11740           while (true)
11741             {
11742               bool done = false;
11743
11744               /* Peek at the next token.  */
11745               token = cp_lexer_peek_token (parser->lexer);
11746               /* What we do depends on what token we have.  */
11747               switch (token->type)
11748                 {
11749                   /* In valid code, a default argument must be
11750                      immediately followed by a `,' `)', or `...'.  */
11751                 case CPP_COMMA:
11752                 case CPP_CLOSE_PAREN:
11753                 case CPP_ELLIPSIS:
11754                   /* If we run into a non-nested `;', `}', or `]',
11755                      then the code is invalid -- but the default
11756                      argument is certainly over.  */
11757                 case CPP_SEMICOLON:
11758                 case CPP_CLOSE_BRACE:
11759                 case CPP_CLOSE_SQUARE:
11760                   if (depth == 0)
11761                     done = true;
11762                   /* Update DEPTH, if necessary.  */
11763                   else if (token->type == CPP_CLOSE_PAREN
11764                            || token->type == CPP_CLOSE_BRACE
11765                            || token->type == CPP_CLOSE_SQUARE)
11766                     --depth;
11767                   break;
11768
11769                 case CPP_OPEN_PAREN:
11770                 case CPP_OPEN_SQUARE:
11771                 case CPP_OPEN_BRACE:
11772                   ++depth;
11773                   break;
11774
11775                 case CPP_GREATER:
11776                   /* If we see a non-nested `>', and `>' is not an
11777                      operator, then it marks the end of the default
11778                      argument.  */
11779                   if (!depth && !greater_than_is_operator_p)
11780                     done = true;
11781                   break;
11782
11783                   /* If we run out of tokens, issue an error message.  */
11784                 case CPP_EOF:
11785                   error ("file ends in default argument");
11786                   done = true;
11787                   break;
11788
11789                 case CPP_NAME:
11790                 case CPP_SCOPE:
11791                   /* In these cases, we should look for template-ids.
11792                      For example, if the default argument is
11793                      `X<int, double>()', we need to do name lookup to
11794                      figure out whether or not `X' is a template; if
11795                      so, the `,' does not end the default argument.
11796
11797                      That is not yet done.  */
11798                   break;
11799
11800                 default:
11801                   break;
11802                 }
11803
11804               /* If we've reached the end, stop.  */
11805               if (done)
11806                 break;
11807
11808               /* Add the token to the token block.  */
11809               token = cp_lexer_consume_token (parser->lexer);
11810             }
11811
11812           /* Create a DEFAULT_ARG to represented the unparsed default
11813              argument.  */
11814           default_argument = make_node (DEFAULT_ARG);
11815           DEFARG_TOKENS (default_argument)
11816             = cp_token_cache_new (first_token, token);  
11817         }
11818       /* Outside of a class definition, we can just parse the
11819          assignment-expression.  */
11820       else
11821         {
11822           bool saved_local_variables_forbidden_p;
11823
11824           /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
11825              set correctly.  */
11826           saved_greater_than_is_operator_p
11827             = parser->greater_than_is_operator_p;
11828           parser->greater_than_is_operator_p = greater_than_is_operator_p;
11829           /* Local variable names (and the `this' keyword) may not
11830              appear in a default argument.  */
11831           saved_local_variables_forbidden_p
11832             = parser->local_variables_forbidden_p;
11833           parser->local_variables_forbidden_p = true;
11834           /* Parse the assignment-expression.  */
11835           default_argument = cp_parser_assignment_expression (parser);
11836           /* Restore saved state.  */
11837           parser->greater_than_is_operator_p
11838             = saved_greater_than_is_operator_p;
11839           parser->local_variables_forbidden_p
11840             = saved_local_variables_forbidden_p;
11841         }
11842       if (!parser->default_arg_ok_p)
11843         {
11844           if (!flag_pedantic_errors)
11845             warning ("deprecated use of default argument for parameter of non-function");
11846           else
11847             {
11848               error ("default arguments are only permitted for function parameters");
11849               default_argument = NULL_TREE;
11850             }
11851         }
11852     }
11853   else
11854     default_argument = NULL_TREE;
11855
11856   return make_parameter_declarator (&decl_specifiers,
11857                                     declarator,
11858                                     default_argument);
11859 }
11860
11861 /* Parse a function-body.
11862
11863    function-body:
11864      compound_statement  */
11865
11866 static void
11867 cp_parser_function_body (cp_parser *parser)
11868 {
11869   cp_parser_compound_statement (parser, NULL, false);
11870 }
11871
11872 /* Parse a ctor-initializer-opt followed by a function-body.  Return
11873    true if a ctor-initializer was present.  */
11874
11875 static bool
11876 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
11877 {
11878   tree body;
11879   bool ctor_initializer_p;
11880
11881   /* Begin the function body.  */
11882   body = begin_function_body ();
11883   /* Parse the optional ctor-initializer.  */
11884   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
11885   /* Parse the function-body.  */
11886   cp_parser_function_body (parser);
11887   /* Finish the function body.  */
11888   finish_function_body (body);
11889
11890   return ctor_initializer_p;
11891 }
11892
11893 /* Parse an initializer.
11894
11895    initializer:
11896      = initializer-clause
11897      ( expression-list )
11898
11899    Returns a expression representing the initializer.  If no
11900    initializer is present, NULL_TREE is returned.
11901
11902    *IS_PARENTHESIZED_INIT is set to TRUE if the `( expression-list )'
11903    production is used, and zero otherwise.  *IS_PARENTHESIZED_INIT is
11904    set to FALSE if there is no initializer present.  If there is an
11905    initializer, and it is not a constant-expression, *NON_CONSTANT_P
11906    is set to true; otherwise it is set to false.  */
11907
11908 static tree
11909 cp_parser_initializer (cp_parser* parser, bool* is_parenthesized_init,
11910                        bool* non_constant_p)
11911 {
11912   cp_token *token;
11913   tree init;
11914
11915   /* Peek at the next token.  */
11916   token = cp_lexer_peek_token (parser->lexer);
11917
11918   /* Let our caller know whether or not this initializer was
11919      parenthesized.  */
11920   *is_parenthesized_init = (token->type == CPP_OPEN_PAREN);
11921   /* Assume that the initializer is constant.  */
11922   *non_constant_p = false;
11923
11924   if (token->type == CPP_EQ)
11925     {
11926       /* Consume the `='.  */
11927       cp_lexer_consume_token (parser->lexer);
11928       /* Parse the initializer-clause.  */
11929       init = cp_parser_initializer_clause (parser, non_constant_p);
11930     }
11931   else if (token->type == CPP_OPEN_PAREN)
11932     init = cp_parser_parenthesized_expression_list (parser, false,
11933                                                     non_constant_p);
11934   else
11935     {
11936       /* Anything else is an error.  */
11937       cp_parser_error (parser, "expected initializer");
11938       init = error_mark_node;
11939     }
11940
11941   return init;
11942 }
11943
11944 /* Parse an initializer-clause.
11945
11946    initializer-clause:
11947      assignment-expression
11948      { initializer-list , [opt] }
11949      { }
11950
11951    Returns an expression representing the initializer.
11952
11953    If the `assignment-expression' production is used the value
11954    returned is simply a representation for the expression.
11955
11956    Otherwise, a CONSTRUCTOR is returned.  The CONSTRUCTOR_ELTS will be
11957    the elements of the initializer-list (or NULL_TREE, if the last
11958    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
11959    NULL_TREE.  There is no way to detect whether or not the optional
11960    trailing `,' was provided.  NON_CONSTANT_P is as for
11961    cp_parser_initializer.  */
11962
11963 static tree
11964 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
11965 {
11966   tree initializer;
11967
11968   /* If it is not a `{', then we are looking at an
11969      assignment-expression.  */
11970   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
11971     {
11972       initializer
11973         = cp_parser_constant_expression (parser,
11974                                         /*allow_non_constant_p=*/true,
11975                                         non_constant_p);
11976       if (!*non_constant_p)
11977         initializer = fold_non_dependent_expr (initializer);
11978     }
11979   else
11980     {
11981       /* Consume the `{' token.  */
11982       cp_lexer_consume_token (parser->lexer);
11983       /* Create a CONSTRUCTOR to represent the braced-initializer.  */
11984       initializer = make_node (CONSTRUCTOR);
11985       /* If it's not a `}', then there is a non-trivial initializer.  */
11986       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
11987         {
11988           /* Parse the initializer list.  */
11989           CONSTRUCTOR_ELTS (initializer)
11990             = cp_parser_initializer_list (parser, non_constant_p);
11991           /* A trailing `,' token is allowed.  */
11992           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
11993             cp_lexer_consume_token (parser->lexer);
11994         }
11995       /* Now, there should be a trailing `}'.  */
11996       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
11997     }
11998
11999   return initializer;
12000 }
12001
12002 /* Parse an initializer-list.
12003
12004    initializer-list:
12005      initializer-clause
12006      initializer-list , initializer-clause
12007
12008    GNU Extension:
12009
12010    initializer-list:
12011      identifier : initializer-clause
12012      initializer-list, identifier : initializer-clause
12013
12014    Returns a TREE_LIST.  The TREE_VALUE of each node is an expression
12015    for the initializer.  If the TREE_PURPOSE is non-NULL, it is the
12016    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
12017    as for cp_parser_initializer.  */
12018
12019 static tree
12020 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
12021 {
12022   tree initializers = NULL_TREE;
12023
12024   /* Assume all of the expressions are constant.  */
12025   *non_constant_p = false;
12026
12027   /* Parse the rest of the list.  */
12028   while (true)
12029     {
12030       cp_token *token;
12031       tree identifier;
12032       tree initializer;
12033       bool clause_non_constant_p;
12034
12035       /* If the next token is an identifier and the following one is a
12036          colon, we are looking at the GNU designated-initializer
12037          syntax.  */
12038       if (cp_parser_allow_gnu_extensions_p (parser)
12039           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
12040           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
12041         {
12042           /* Consume the identifier.  */
12043           identifier = cp_lexer_consume_token (parser->lexer)->value;
12044           /* Consume the `:'.  */
12045           cp_lexer_consume_token (parser->lexer);
12046         }
12047       else
12048         identifier = NULL_TREE;
12049
12050       /* Parse the initializer.  */
12051       initializer = cp_parser_initializer_clause (parser,
12052                                                   &clause_non_constant_p);
12053       /* If any clause is non-constant, so is the entire initializer.  */
12054       if (clause_non_constant_p)
12055         *non_constant_p = true;
12056       /* Add it to the list.  */
12057       initializers = tree_cons (identifier, initializer, initializers);
12058
12059       /* If the next token is not a comma, we have reached the end of
12060          the list.  */
12061       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12062         break;
12063
12064       /* Peek at the next token.  */
12065       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12066       /* If the next token is a `}', then we're still done.  An
12067          initializer-clause can have a trailing `,' after the
12068          initializer-list and before the closing `}'.  */
12069       if (token->type == CPP_CLOSE_BRACE)
12070         break;
12071
12072       /* Consume the `,' token.  */
12073       cp_lexer_consume_token (parser->lexer);
12074     }
12075
12076   /* The initializers were built up in reverse order, so we need to
12077      reverse them now.  */
12078   return nreverse (initializers);
12079 }
12080
12081 /* Classes [gram.class] */
12082
12083 /* Parse a class-name.
12084
12085    class-name:
12086      identifier
12087      template-id
12088
12089    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
12090    to indicate that names looked up in dependent types should be
12091    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
12092    keyword has been used to indicate that the name that appears next
12093    is a template.  TYPE_P is true iff the next name should be treated
12094    as class-name, even if it is declared to be some other kind of name
12095    as well.  If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12096    dependent scopes.  If CLASS_HEAD_P is TRUE, this class is the class
12097    being defined in a class-head.
12098
12099    Returns the TYPE_DECL representing the class.  */
12100
12101 static tree
12102 cp_parser_class_name (cp_parser *parser,
12103                       bool typename_keyword_p,
12104                       bool template_keyword_p,
12105                       bool type_p,
12106                       bool check_dependency_p,
12107                       bool class_head_p,
12108                       bool is_declaration)
12109 {
12110   tree decl;
12111   tree scope;
12112   bool typename_p;
12113   cp_token *token;
12114
12115   /* All class-names start with an identifier.  */
12116   token = cp_lexer_peek_token (parser->lexer);
12117   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
12118     {
12119       cp_parser_error (parser, "expected class-name");
12120       return error_mark_node;
12121     }
12122
12123   /* PARSER->SCOPE can be cleared when parsing the template-arguments
12124      to a template-id, so we save it here.  */
12125   scope = parser->scope;
12126   if (scope == error_mark_node)
12127     return error_mark_node;
12128
12129   /* Any name names a type if we're following the `typename' keyword
12130      in a qualified name where the enclosing scope is type-dependent.  */
12131   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
12132                 && dependent_type_p (scope));
12133   /* Handle the common case (an identifier, but not a template-id)
12134      efficiently.  */
12135   if (token->type == CPP_NAME
12136       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
12137     {
12138       tree identifier;
12139
12140       /* Look for the identifier.  */
12141       identifier = cp_parser_identifier (parser);
12142       /* If the next token isn't an identifier, we are certainly not
12143          looking at a class-name.  */
12144       if (identifier == error_mark_node)
12145         decl = error_mark_node;
12146       /* If we know this is a type-name, there's no need to look it
12147          up.  */
12148       else if (typename_p)
12149         decl = identifier;
12150       else
12151         {
12152           /* If the next token is a `::', then the name must be a type
12153              name.
12154
12155              [basic.lookup.qual]
12156
12157              During the lookup for a name preceding the :: scope
12158              resolution operator, object, function, and enumerator
12159              names are ignored.  */
12160           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12161             type_p = true;
12162           /* Look up the name.  */
12163           decl = cp_parser_lookup_name (parser, identifier,
12164                                         type_p,
12165                                         /*is_template=*/false,
12166                                         /*is_namespace=*/false,
12167                                         check_dependency_p,
12168                                         /*ambiguous_p=*/NULL);
12169         }
12170     }
12171   else
12172     {
12173       /* Try a template-id.  */
12174       decl = cp_parser_template_id (parser, template_keyword_p,
12175                                     check_dependency_p,
12176                                     is_declaration);
12177       if (decl == error_mark_node)
12178         return error_mark_node;
12179     }
12180
12181   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
12182
12183   /* If this is a typename, create a TYPENAME_TYPE.  */
12184   if (typename_p && decl != error_mark_node)
12185     {
12186       decl = make_typename_type (scope, decl, /*complain=*/1);
12187       if (decl != error_mark_node)
12188         decl = TYPE_NAME (decl);
12189     }
12190
12191   /* Check to see that it is really the name of a class.  */
12192   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12193       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
12194       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
12195     /* Situations like this:
12196
12197          template <typename T> struct A {
12198            typename T::template X<int>::I i;
12199          };
12200
12201        are problematic.  Is `T::template X<int>' a class-name?  The
12202        standard does not seem to be definitive, but there is no other
12203        valid interpretation of the following `::'.  Therefore, those
12204        names are considered class-names.  */
12205     decl = TYPE_NAME (make_typename_type (scope, decl, tf_error));
12206   else if (decl == error_mark_node
12207            || TREE_CODE (decl) != TYPE_DECL
12208            || !IS_AGGR_TYPE (TREE_TYPE (decl)))
12209     {
12210       cp_parser_error (parser, "expected class-name");
12211       return error_mark_node;
12212     }
12213
12214   return decl;
12215 }
12216
12217 /* Parse a class-specifier.
12218
12219    class-specifier:
12220      class-head { member-specification [opt] }
12221
12222    Returns the TREE_TYPE representing the class.  */
12223
12224 static tree
12225 cp_parser_class_specifier (cp_parser* parser)
12226 {
12227   cp_token *token;
12228   tree type;
12229   tree attributes = NULL_TREE;
12230   int has_trailing_semicolon;
12231   bool nested_name_specifier_p;
12232   unsigned saved_num_template_parameter_lists;
12233   bool pop_p = false;
12234   tree scope = NULL_TREE;
12235
12236   push_deferring_access_checks (dk_no_deferred);
12237
12238   /* Parse the class-head.  */
12239   type = cp_parser_class_head (parser,
12240                                &nested_name_specifier_p,
12241                                &attributes);
12242   /* If the class-head was a semantic disaster, skip the entire body
12243      of the class.  */
12244   if (!type)
12245     {
12246       cp_parser_skip_to_end_of_block_or_statement (parser);
12247       pop_deferring_access_checks ();
12248       return error_mark_node;
12249     }
12250
12251   /* Look for the `{'.  */
12252   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "`{'"))
12253     {
12254       pop_deferring_access_checks ();
12255       return error_mark_node;
12256     }
12257
12258   /* Issue an error message if type-definitions are forbidden here.  */
12259   cp_parser_check_type_definition (parser);
12260   /* Remember that we are defining one more class.  */
12261   ++parser->num_classes_being_defined;
12262   /* Inside the class, surrounding template-parameter-lists do not
12263      apply.  */
12264   saved_num_template_parameter_lists
12265     = parser->num_template_parameter_lists;
12266   parser->num_template_parameter_lists = 0;
12267
12268   /* Start the class.  */
12269   if (nested_name_specifier_p)
12270     {
12271       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
12272       pop_p = push_scope (scope);
12273     }
12274   type = begin_class_definition (type);
12275
12276   if (type == error_mark_node)
12277     /* If the type is erroneous, skip the entire body of the class.  */
12278     cp_parser_skip_to_closing_brace (parser);
12279   else
12280     /* Parse the member-specification.  */
12281     cp_parser_member_specification_opt (parser);
12282
12283   /* Look for the trailing `}'.  */
12284   cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
12285   /* We get better error messages by noticing a common problem: a
12286      missing trailing `;'.  */
12287   token = cp_lexer_peek_token (parser->lexer);
12288   has_trailing_semicolon = (token->type == CPP_SEMICOLON);
12289   /* Look for trailing attributes to apply to this class.  */
12290   if (cp_parser_allow_gnu_extensions_p (parser))
12291     {
12292       tree sub_attr = cp_parser_attributes_opt (parser);
12293       attributes = chainon (attributes, sub_attr);
12294     }
12295   if (type != error_mark_node)
12296     type = finish_struct (type, attributes);
12297   if (pop_p)
12298     pop_scope (scope);
12299   /* If this class is not itself within the scope of another class,
12300      then we need to parse the bodies of all of the queued function
12301      definitions.  Note that the queued functions defined in a class
12302      are not always processed immediately following the
12303      class-specifier for that class.  Consider:
12304
12305        struct A {
12306          struct B { void f() { sizeof (A); } };
12307        };
12308
12309      If `f' were processed before the processing of `A' were
12310      completed, there would be no way to compute the size of `A'.
12311      Note that the nesting we are interested in here is lexical --
12312      not the semantic nesting given by TYPE_CONTEXT.  In particular,
12313      for:
12314
12315        struct A { struct B; };
12316        struct A::B { void f() { } };
12317
12318      there is no need to delay the parsing of `A::B::f'.  */
12319   if (--parser->num_classes_being_defined == 0)
12320     {
12321       tree queue_entry;
12322       tree fn;
12323       tree class_type;
12324       bool pop_p;
12325
12326       /* In a first pass, parse default arguments to the functions.
12327          Then, in a second pass, parse the bodies of the functions.
12328          This two-phased approach handles cases like:
12329
12330             struct S {
12331               void f() { g(); }
12332               void g(int i = 3);
12333             };
12334
12335          */
12336       class_type = NULL_TREE;
12337       pop_p = false;
12338       for (TREE_PURPOSE (parser->unparsed_functions_queues)
12339              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
12340            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
12341            TREE_PURPOSE (parser->unparsed_functions_queues)
12342              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
12343         {
12344           fn = TREE_VALUE (queue_entry);
12345           /* If there are default arguments that have not yet been processed,
12346              take care of them now.  */
12347           if (class_type != TREE_PURPOSE (queue_entry))
12348             {
12349               if (pop_p)
12350                 pop_scope (class_type);
12351               class_type = TREE_PURPOSE (queue_entry);
12352               pop_p = push_scope (class_type);
12353             }
12354           /* Make sure that any template parameters are in scope.  */
12355           maybe_begin_member_template_processing (fn);
12356           /* Parse the default argument expressions.  */
12357           cp_parser_late_parsing_default_args (parser, fn);
12358           /* Remove any template parameters from the symbol table.  */
12359           maybe_end_member_template_processing ();
12360         }
12361       if (pop_p)
12362         pop_scope (class_type);
12363       /* Now parse the body of the functions.  */
12364       for (TREE_VALUE (parser->unparsed_functions_queues)
12365              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
12366            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
12367            TREE_VALUE (parser->unparsed_functions_queues)
12368              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
12369         {
12370           /* Figure out which function we need to process.  */
12371           fn = TREE_VALUE (queue_entry);
12372
12373           /* A hack to prevent garbage collection.  */
12374           function_depth++;
12375
12376           /* Parse the function.  */
12377           cp_parser_late_parsing_for_member (parser, fn);
12378           function_depth--;
12379         }
12380     }
12381
12382   /* Put back any saved access checks.  */
12383   pop_deferring_access_checks ();
12384
12385   /* Restore the count of active template-parameter-lists.  */
12386   parser->num_template_parameter_lists
12387     = saved_num_template_parameter_lists;
12388
12389   return type;
12390 }
12391
12392 /* Parse a class-head.
12393
12394    class-head:
12395      class-key identifier [opt] base-clause [opt]
12396      class-key nested-name-specifier identifier base-clause [opt]
12397      class-key nested-name-specifier [opt] template-id
12398        base-clause [opt]
12399
12400    GNU Extensions:
12401      class-key attributes identifier [opt] base-clause [opt]
12402      class-key attributes nested-name-specifier identifier base-clause [opt]
12403      class-key attributes nested-name-specifier [opt] template-id
12404        base-clause [opt]
12405
12406    Returns the TYPE of the indicated class.  Sets
12407    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
12408    involving a nested-name-specifier was used, and FALSE otherwise.
12409
12410    Returns NULL_TREE if the class-head is syntactically valid, but
12411    semantically invalid in a way that means we should skip the entire
12412    body of the class.  */
12413
12414 static tree
12415 cp_parser_class_head (cp_parser* parser,
12416                       bool* nested_name_specifier_p,
12417                       tree *attributes_p)
12418 {
12419   tree nested_name_specifier;
12420   enum tag_types class_key;
12421   tree id = NULL_TREE;
12422   tree type = NULL_TREE;
12423   tree attributes;
12424   bool template_id_p = false;
12425   bool qualified_p = false;
12426   bool invalid_nested_name_p = false;
12427   bool invalid_explicit_specialization_p = false;
12428   bool pop_p = false;
12429   unsigned num_templates;
12430   tree bases;
12431
12432   /* Assume no nested-name-specifier will be present.  */
12433   *nested_name_specifier_p = false;
12434   /* Assume no template parameter lists will be used in defining the
12435      type.  */
12436   num_templates = 0;
12437
12438   /* Look for the class-key.  */
12439   class_key = cp_parser_class_key (parser);
12440   if (class_key == none_type)
12441     return error_mark_node;
12442
12443   /* Parse the attributes.  */
12444   attributes = cp_parser_attributes_opt (parser);
12445
12446   /* If the next token is `::', that is invalid -- but sometimes
12447      people do try to write:
12448
12449        struct ::S {};
12450
12451      Handle this gracefully by accepting the extra qualifier, and then
12452      issuing an error about it later if this really is a
12453      class-head.  If it turns out just to be an elaborated type
12454      specifier, remain silent.  */
12455   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
12456     qualified_p = true;
12457
12458   push_deferring_access_checks (dk_no_check);
12459
12460   /* Determine the name of the class.  Begin by looking for an
12461      optional nested-name-specifier.  */
12462   nested_name_specifier
12463     = cp_parser_nested_name_specifier_opt (parser,
12464                                            /*typename_keyword_p=*/false,
12465                                            /*check_dependency_p=*/false,
12466                                            /*type_p=*/false,
12467                                            /*is_declaration=*/false);
12468   /* If there was a nested-name-specifier, then there *must* be an
12469      identifier.  */
12470   if (nested_name_specifier)
12471     {
12472       /* Although the grammar says `identifier', it really means
12473          `class-name' or `template-name'.  You are only allowed to
12474          define a class that has already been declared with this
12475          syntax.
12476
12477          The proposed resolution for Core Issue 180 says that whever
12478          you see `class T::X' you should treat `X' as a type-name.
12479
12480          It is OK to define an inaccessible class; for example:
12481
12482            class A { class B; };
12483            class A::B {};
12484
12485          We do not know if we will see a class-name, or a
12486          template-name.  We look for a class-name first, in case the
12487          class-name is a template-id; if we looked for the
12488          template-name first we would stop after the template-name.  */
12489       cp_parser_parse_tentatively (parser);
12490       type = cp_parser_class_name (parser,
12491                                    /*typename_keyword_p=*/false,
12492                                    /*template_keyword_p=*/false,
12493                                    /*type_p=*/true,
12494                                    /*check_dependency_p=*/false,
12495                                    /*class_head_p=*/true,
12496                                    /*is_declaration=*/false);
12497       /* If that didn't work, ignore the nested-name-specifier.  */
12498       if (!cp_parser_parse_definitely (parser))
12499         {
12500           invalid_nested_name_p = true;
12501           id = cp_parser_identifier (parser);
12502           if (id == error_mark_node)
12503             id = NULL_TREE;
12504         }
12505       /* If we could not find a corresponding TYPE, treat this
12506          declaration like an unqualified declaration.  */
12507       if (type == error_mark_node)
12508         nested_name_specifier = NULL_TREE;
12509       /* Otherwise, count the number of templates used in TYPE and its
12510          containing scopes.  */
12511       else
12512         {
12513           tree scope;
12514
12515           for (scope = TREE_TYPE (type);
12516                scope && TREE_CODE (scope) != NAMESPACE_DECL;
12517                scope = (TYPE_P (scope)
12518                         ? TYPE_CONTEXT (scope)
12519                         : DECL_CONTEXT (scope)))
12520             if (TYPE_P (scope)
12521                 && CLASS_TYPE_P (scope)
12522                 && CLASSTYPE_TEMPLATE_INFO (scope)
12523                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
12524                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
12525               ++num_templates;
12526         }
12527     }
12528   /* Otherwise, the identifier is optional.  */
12529   else
12530     {
12531       /* We don't know whether what comes next is a template-id,
12532          an identifier, or nothing at all.  */
12533       cp_parser_parse_tentatively (parser);
12534       /* Check for a template-id.  */
12535       id = cp_parser_template_id (parser,
12536                                   /*template_keyword_p=*/false,
12537                                   /*check_dependency_p=*/true,
12538                                   /*is_declaration=*/true);
12539       /* If that didn't work, it could still be an identifier.  */
12540       if (!cp_parser_parse_definitely (parser))
12541         {
12542           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12543             id = cp_parser_identifier (parser);
12544           else
12545             id = NULL_TREE;
12546         }
12547       else
12548         {
12549           template_id_p = true;
12550           ++num_templates;
12551         }
12552     }
12553
12554   pop_deferring_access_checks ();
12555
12556   if (id)
12557     cp_parser_check_for_invalid_template_id (parser, id);
12558
12559   /* If it's not a `:' or a `{' then we can't really be looking at a
12560      class-head, since a class-head only appears as part of a
12561      class-specifier.  We have to detect this situation before calling
12562      xref_tag, since that has irreversible side-effects.  */
12563   if (!cp_parser_next_token_starts_class_definition_p (parser))
12564     {
12565       cp_parser_error (parser, "expected %<{%> or %<:%>");
12566       return error_mark_node;
12567     }
12568
12569   /* At this point, we're going ahead with the class-specifier, even
12570      if some other problem occurs.  */
12571   cp_parser_commit_to_tentative_parse (parser);
12572   /* Issue the error about the overly-qualified name now.  */
12573   if (qualified_p)
12574     cp_parser_error (parser,
12575                      "global qualification of class name is invalid");
12576   else if (invalid_nested_name_p)
12577     cp_parser_error (parser,
12578                      "qualified name does not name a class");
12579   else if (nested_name_specifier)
12580     {
12581       tree scope;
12582       /* Figure out in what scope the declaration is being placed.  */
12583       scope = current_scope ();
12584       if (!scope)
12585         scope = current_namespace;
12586       /* If that scope does not contain the scope in which the
12587          class was originally declared, the program is invalid.  */
12588       if (scope && !is_ancestor (scope, nested_name_specifier))
12589         {
12590           error ("declaration of %qD in %qD which does not enclose %qD",
12591                  type, scope, nested_name_specifier);
12592           type = NULL_TREE;
12593           goto done;
12594         }
12595       /* [dcl.meaning]
12596
12597          A declarator-id shall not be qualified exception of the
12598          definition of a ... nested class outside of its class
12599          ... [or] a the definition or explicit instantiation of a
12600          class member of a namespace outside of its namespace.  */
12601       if (scope == nested_name_specifier)
12602         {
12603           pedwarn ("extra qualification ignored");
12604           nested_name_specifier = NULL_TREE;
12605           num_templates = 0;
12606         }
12607     }
12608   /* An explicit-specialization must be preceded by "template <>".  If
12609      it is not, try to recover gracefully.  */
12610   if (at_namespace_scope_p ()
12611       && parser->num_template_parameter_lists == 0
12612       && template_id_p)
12613     {
12614       error ("an explicit specialization must be preceded by %<template <>%>");
12615       invalid_explicit_specialization_p = true;
12616       /* Take the same action that would have been taken by
12617          cp_parser_explicit_specialization.  */
12618       ++parser->num_template_parameter_lists;
12619       begin_specialization ();
12620     }
12621   /* There must be no "return" statements between this point and the
12622      end of this function; set "type "to the correct return value and
12623      use "goto done;" to return.  */
12624   /* Make sure that the right number of template parameters were
12625      present.  */
12626   if (!cp_parser_check_template_parameters (parser, num_templates))
12627     {
12628       /* If something went wrong, there is no point in even trying to
12629          process the class-definition.  */
12630       type = NULL_TREE;
12631       goto done;
12632     }
12633
12634   /* Look up the type.  */
12635   if (template_id_p)
12636     {
12637       type = TREE_TYPE (id);
12638       maybe_process_partial_specialization (type);
12639     }
12640   else if (!nested_name_specifier)
12641     {
12642       /* If the class was unnamed, create a dummy name.  */
12643       if (!id)
12644         id = make_anon_name ();
12645       type = xref_tag (class_key, id, /*globalize=*/false,
12646                        parser->num_template_parameter_lists);
12647     }
12648   else
12649     {
12650       tree class_type;
12651       bool pop_p = false;
12652
12653       /* Given:
12654
12655             template <typename T> struct S { struct T };
12656             template <typename T> struct S<T>::T { };
12657
12658          we will get a TYPENAME_TYPE when processing the definition of
12659          `S::T'.  We need to resolve it to the actual type before we
12660          try to define it.  */
12661       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
12662         {
12663           class_type = resolve_typename_type (TREE_TYPE (type),
12664                                               /*only_current_p=*/false);
12665           if (class_type != error_mark_node)
12666             type = TYPE_NAME (class_type);
12667           else
12668             {
12669               cp_parser_error (parser, "could not resolve typename type");
12670               type = error_mark_node;
12671             }
12672         }
12673
12674       maybe_process_partial_specialization (TREE_TYPE (type));
12675       class_type = current_class_type;
12676       /* Enter the scope indicated by the nested-name-specifier.  */
12677       if (nested_name_specifier)
12678         pop_p = push_scope (nested_name_specifier);
12679       /* Get the canonical version of this type.  */
12680       type = TYPE_MAIN_DECL (TREE_TYPE (type));
12681       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
12682           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
12683         type = push_template_decl (type);
12684       type = TREE_TYPE (type);
12685       if (nested_name_specifier)
12686         {
12687           *nested_name_specifier_p = true;
12688           if (pop_p)
12689             pop_scope (nested_name_specifier);
12690         }
12691     }
12692   /* Indicate whether this class was declared as a `class' or as a
12693      `struct'.  */
12694   if (TREE_CODE (type) == RECORD_TYPE)
12695     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
12696   cp_parser_check_class_key (class_key, type);
12697
12698   /* Enter the scope containing the class; the names of base classes
12699      should be looked up in that context.  For example, given:
12700
12701        struct A { struct B {}; struct C; };
12702        struct A::C : B {};
12703
12704      is valid.  */
12705   if (nested_name_specifier)
12706     pop_p = push_scope (nested_name_specifier);
12707
12708   bases = NULL_TREE;
12709
12710   /* Get the list of base-classes, if there is one.  */
12711   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12712     bases = cp_parser_base_clause (parser);
12713
12714   /* Process the base classes.  */
12715   xref_basetypes (type, bases);
12716
12717   /* Leave the scope given by the nested-name-specifier.  We will
12718      enter the class scope itself while processing the members.  */
12719   if (pop_p)
12720     pop_scope (nested_name_specifier);
12721
12722  done:
12723   if (invalid_explicit_specialization_p)
12724     {
12725       end_specialization ();
12726       --parser->num_template_parameter_lists;
12727     }
12728   *attributes_p = attributes;
12729   return type;
12730 }
12731
12732 /* Parse a class-key.
12733
12734    class-key:
12735      class
12736      struct
12737      union
12738
12739    Returns the kind of class-key specified, or none_type to indicate
12740    error.  */
12741
12742 static enum tag_types
12743 cp_parser_class_key (cp_parser* parser)
12744 {
12745   cp_token *token;
12746   enum tag_types tag_type;
12747
12748   /* Look for the class-key.  */
12749   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
12750   if (!token)
12751     return none_type;
12752
12753   /* Check to see if the TOKEN is a class-key.  */
12754   tag_type = cp_parser_token_is_class_key (token);
12755   if (!tag_type)
12756     cp_parser_error (parser, "expected class-key");
12757   return tag_type;
12758 }
12759
12760 /* Parse an (optional) member-specification.
12761
12762    member-specification:
12763      member-declaration member-specification [opt]
12764      access-specifier : member-specification [opt]  */
12765
12766 static void
12767 cp_parser_member_specification_opt (cp_parser* parser)
12768 {
12769   while (true)
12770     {
12771       cp_token *token;
12772       enum rid keyword;
12773
12774       /* Peek at the next token.  */
12775       token = cp_lexer_peek_token (parser->lexer);
12776       /* If it's a `}', or EOF then we've seen all the members.  */
12777       if (token->type == CPP_CLOSE_BRACE || token->type == CPP_EOF)
12778         break;
12779
12780       /* See if this token is a keyword.  */
12781       keyword = token->keyword;
12782       switch (keyword)
12783         {
12784         case RID_PUBLIC:
12785         case RID_PROTECTED:
12786         case RID_PRIVATE:
12787           /* Consume the access-specifier.  */
12788           cp_lexer_consume_token (parser->lexer);
12789           /* Remember which access-specifier is active.  */
12790           current_access_specifier = token->value;
12791           /* Look for the `:'.  */
12792           cp_parser_require (parser, CPP_COLON, "`:'");
12793           break;
12794
12795         default:
12796           /* Accept #pragmas at class scope.  */
12797           if (token->type == CPP_PRAGMA)
12798             {
12799               cp_lexer_handle_pragma (parser->lexer);
12800               break;
12801             }
12802
12803           /* Otherwise, the next construction must be a
12804              member-declaration.  */
12805           cp_parser_member_declaration (parser);
12806         }
12807     }
12808 }
12809
12810 /* Parse a member-declaration.
12811
12812    member-declaration:
12813      decl-specifier-seq [opt] member-declarator-list [opt] ;
12814      function-definition ; [opt]
12815      :: [opt] nested-name-specifier template [opt] unqualified-id ;
12816      using-declaration
12817      template-declaration
12818
12819    member-declarator-list:
12820      member-declarator
12821      member-declarator-list , member-declarator
12822
12823    member-declarator:
12824      declarator pure-specifier [opt]
12825      declarator constant-initializer [opt]
12826      identifier [opt] : constant-expression
12827
12828    GNU Extensions:
12829
12830    member-declaration:
12831      __extension__ member-declaration
12832
12833    member-declarator:
12834      declarator attributes [opt] pure-specifier [opt]
12835      declarator attributes [opt] constant-initializer [opt]
12836      identifier [opt] attributes [opt] : constant-expression  */
12837
12838 static void
12839 cp_parser_member_declaration (cp_parser* parser)
12840 {
12841   cp_decl_specifier_seq decl_specifiers;
12842   tree prefix_attributes;
12843   tree decl;
12844   int declares_class_or_enum;
12845   bool friend_p;
12846   cp_token *token;
12847   int saved_pedantic;
12848
12849   /* Check for the `__extension__' keyword.  */
12850   if (cp_parser_extension_opt (parser, &saved_pedantic))
12851     {
12852       /* Recurse.  */
12853       cp_parser_member_declaration (parser);
12854       /* Restore the old value of the PEDANTIC flag.  */
12855       pedantic = saved_pedantic;
12856
12857       return;
12858     }
12859
12860   /* Check for a template-declaration.  */
12861   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12862     {
12863       /* Parse the template-declaration.  */
12864       cp_parser_template_declaration (parser, /*member_p=*/true);
12865
12866       return;
12867     }
12868
12869   /* Check for a using-declaration.  */
12870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
12871     {
12872       /* Parse the using-declaration.  */
12873       cp_parser_using_declaration (parser);
12874
12875       return;
12876     }
12877
12878   /* Parse the decl-specifier-seq.  */
12879   cp_parser_decl_specifier_seq (parser,
12880                                 CP_PARSER_FLAGS_OPTIONAL,
12881                                 &decl_specifiers,
12882                                 &declares_class_or_enum);
12883   prefix_attributes = decl_specifiers.attributes;
12884   decl_specifiers.attributes = NULL_TREE;
12885   /* Check for an invalid type-name.  */
12886   if (!decl_specifiers.type
12887       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
12888     return;
12889   /* If there is no declarator, then the decl-specifier-seq should
12890      specify a type.  */
12891   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
12892     {
12893       /* If there was no decl-specifier-seq, and the next token is a
12894          `;', then we have something like:
12895
12896            struct S { ; };
12897
12898          [class.mem]
12899
12900          Each member-declaration shall declare at least one member
12901          name of the class.  */
12902       if (!decl_specifiers.any_specifiers_p)
12903         {
12904           cp_token *token = cp_lexer_peek_token (parser->lexer);
12905           if (pedantic && !token->in_system_header)
12906             pedwarn ("%Hextra %<;%>", &token->location);
12907         }
12908       else
12909         {
12910           tree type;
12911
12912           /* See if this declaration is a friend.  */
12913           friend_p = cp_parser_friend_p (&decl_specifiers);
12914           /* If there were decl-specifiers, check to see if there was
12915              a class-declaration.  */
12916           type = check_tag_decl (&decl_specifiers);
12917           /* Nested classes have already been added to the class, but
12918              a `friend' needs to be explicitly registered.  */
12919           if (friend_p)
12920             {
12921               /* If the `friend' keyword was present, the friend must
12922                  be introduced with a class-key.  */
12923                if (!declares_class_or_enum)
12924                  error ("a class-key must be used when declaring a friend");
12925                /* In this case:
12926
12927                     template <typename T> struct A {
12928                       friend struct A<T>::B;
12929                     };
12930
12931                   A<T>::B will be represented by a TYPENAME_TYPE, and
12932                   therefore not recognized by check_tag_decl.  */
12933                if (!type
12934                    && decl_specifiers.type
12935                    && TYPE_P (decl_specifiers.type))
12936                  type = decl_specifiers.type;
12937                if (!type || !TYPE_P (type))
12938                  error ("friend declaration does not name a class or "
12939                         "function");
12940                else
12941                  make_friend_class (current_class_type, type,
12942                                     /*complain=*/true);
12943             }
12944           /* If there is no TYPE, an error message will already have
12945              been issued.  */
12946           else if (!type || type == error_mark_node)
12947             ;
12948           /* An anonymous aggregate has to be handled specially; such
12949              a declaration really declares a data member (with a
12950              particular type), as opposed to a nested class.  */
12951           else if (ANON_AGGR_TYPE_P (type))
12952             {
12953               /* Remove constructors and such from TYPE, now that we
12954                  know it is an anonymous aggregate.  */
12955               fixup_anonymous_aggr (type);
12956               /* And make the corresponding data member.  */
12957               decl = build_decl (FIELD_DECL, NULL_TREE, type);
12958               /* Add it to the class.  */
12959               finish_member_declaration (decl);
12960             }
12961           else
12962             cp_parser_check_access_in_redeclaration (TYPE_NAME (type));
12963         }
12964     }
12965   else
12966     {
12967       /* See if these declarations will be friends.  */
12968       friend_p = cp_parser_friend_p (&decl_specifiers);
12969
12970       /* Keep going until we hit the `;' at the end of the
12971          declaration.  */
12972       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
12973         {
12974           tree attributes = NULL_TREE;
12975           tree first_attribute;
12976
12977           /* Peek at the next token.  */
12978           token = cp_lexer_peek_token (parser->lexer);
12979
12980           /* Check for a bitfield declaration.  */
12981           if (token->type == CPP_COLON
12982               || (token->type == CPP_NAME
12983                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
12984                   == CPP_COLON))
12985             {
12986               tree identifier;
12987               tree width;
12988
12989               /* Get the name of the bitfield.  Note that we cannot just
12990                  check TOKEN here because it may have been invalidated by
12991                  the call to cp_lexer_peek_nth_token above.  */
12992               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
12993                 identifier = cp_parser_identifier (parser);
12994               else
12995                 identifier = NULL_TREE;
12996
12997               /* Consume the `:' token.  */
12998               cp_lexer_consume_token (parser->lexer);
12999               /* Get the width of the bitfield.  */
13000               width
13001                 = cp_parser_constant_expression (parser,
13002                                                  /*allow_non_constant=*/false,
13003                                                  NULL);
13004
13005               /* Look for attributes that apply to the bitfield.  */
13006               attributes = cp_parser_attributes_opt (parser);
13007               /* Remember which attributes are prefix attributes and
13008                  which are not.  */
13009               first_attribute = attributes;
13010               /* Combine the attributes.  */
13011               attributes = chainon (prefix_attributes, attributes);
13012
13013               /* Create the bitfield declaration.  */
13014               decl = grokbitfield (identifier
13015                                    ? make_id_declarator (identifier)
13016                                    : NULL,
13017                                    &decl_specifiers,
13018                                    width);
13019               /* Apply the attributes.  */
13020               cplus_decl_attributes (&decl, attributes, /*flags=*/0);
13021             }
13022           else
13023             {
13024               cp_declarator *declarator;
13025               tree initializer;
13026               tree asm_specification;
13027               int ctor_dtor_or_conv_p;
13028
13029               /* Parse the declarator.  */
13030               declarator
13031                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13032                                         &ctor_dtor_or_conv_p,
13033                                         /*parenthesized_p=*/NULL,
13034                                         /*member_p=*/true);
13035
13036               /* If something went wrong parsing the declarator, make sure
13037                  that we at least consume some tokens.  */
13038               if (declarator == cp_error_declarator)
13039                 {
13040                   /* Skip to the end of the statement.  */
13041                   cp_parser_skip_to_end_of_statement (parser);
13042                   /* If the next token is not a semicolon, that is
13043                      probably because we just skipped over the body of
13044                      a function.  So, we consume a semicolon if
13045                      present, but do not issue an error message if it
13046                      is not present.  */
13047                   if (cp_lexer_next_token_is (parser->lexer,
13048                                               CPP_SEMICOLON))
13049                     cp_lexer_consume_token (parser->lexer);
13050                   return;
13051                 }
13052
13053               cp_parser_check_for_definition_in_return_type
13054                 (declarator, declares_class_or_enum);
13055
13056               /* Look for an asm-specification.  */
13057               asm_specification = cp_parser_asm_specification_opt (parser);
13058               /* Look for attributes that apply to the declaration.  */
13059               attributes = cp_parser_attributes_opt (parser);
13060               /* Remember which attributes are prefix attributes and
13061                  which are not.  */
13062               first_attribute = attributes;
13063               /* Combine the attributes.  */
13064               attributes = chainon (prefix_attributes, attributes);
13065
13066               /* If it's an `=', then we have a constant-initializer or a
13067                  pure-specifier.  It is not correct to parse the
13068                  initializer before registering the member declaration
13069                  since the member declaration should be in scope while
13070                  its initializer is processed.  However, the rest of the
13071                  front end does not yet provide an interface that allows
13072                  us to handle this correctly.  */
13073               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13074                 {
13075                   /* In [class.mem]:
13076
13077                      A pure-specifier shall be used only in the declaration of
13078                      a virtual function.
13079
13080                      A member-declarator can contain a constant-initializer
13081                      only if it declares a static member of integral or
13082                      enumeration type.
13083
13084                      Therefore, if the DECLARATOR is for a function, we look
13085                      for a pure-specifier; otherwise, we look for a
13086                      constant-initializer.  When we call `grokfield', it will
13087                      perform more stringent semantics checks.  */
13088                   if (declarator->kind == cdk_function)
13089                     initializer = cp_parser_pure_specifier (parser);
13090                   else
13091                     /* Parse the initializer.  */
13092                     initializer = cp_parser_constant_initializer (parser);
13093                 }
13094               /* Otherwise, there is no initializer.  */
13095               else
13096                 initializer = NULL_TREE;
13097
13098               /* See if we are probably looking at a function
13099                  definition.  We are certainly not looking at at a
13100                  member-declarator.  Calling `grokfield' has
13101                  side-effects, so we must not do it unless we are sure
13102                  that we are looking at a member-declarator.  */
13103               if (cp_parser_token_starts_function_definition_p
13104                   (cp_lexer_peek_token (parser->lexer)))
13105                 {
13106                   /* The grammar does not allow a pure-specifier to be
13107                      used when a member function is defined.  (It is
13108                      possible that this fact is an oversight in the
13109                      standard, since a pure function may be defined
13110                      outside of the class-specifier.  */
13111                   if (initializer)
13112                     error ("pure-specifier on function-definition");
13113                   decl = cp_parser_save_member_function_body (parser,
13114                                                               &decl_specifiers,
13115                                                               declarator,
13116                                                               attributes);
13117                   /* If the member was not a friend, declare it here.  */
13118                   if (!friend_p)
13119                     finish_member_declaration (decl);
13120                   /* Peek at the next token.  */
13121                   token = cp_lexer_peek_token (parser->lexer);
13122                   /* If the next token is a semicolon, consume it.  */
13123                   if (token->type == CPP_SEMICOLON)
13124                     cp_lexer_consume_token (parser->lexer);
13125                   return;
13126                 }
13127               else
13128                 {
13129                   /* Create the declaration.  */
13130                   decl = grokfield (declarator, &decl_specifiers,
13131                                     initializer, asm_specification,
13132                                     attributes);
13133                   /* Any initialization must have been from a
13134                      constant-expression.  */
13135                   if (decl && TREE_CODE (decl) == VAR_DECL && initializer)
13136                     DECL_INITIALIZED_BY_CONSTANT_EXPRESSION_P (decl) = 1;
13137                 }
13138             }
13139
13140           /* Reset PREFIX_ATTRIBUTES.  */
13141           while (attributes && TREE_CHAIN (attributes) != first_attribute)
13142             attributes = TREE_CHAIN (attributes);
13143           if (attributes)
13144             TREE_CHAIN (attributes) = NULL_TREE;
13145
13146           /* If there is any qualification still in effect, clear it
13147              now; we will be starting fresh with the next declarator.  */
13148           parser->scope = NULL_TREE;
13149           parser->qualifying_scope = NULL_TREE;
13150           parser->object_scope = NULL_TREE;
13151           /* If it's a `,', then there are more declarators.  */
13152           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
13153             cp_lexer_consume_token (parser->lexer);
13154           /* If the next token isn't a `;', then we have a parse error.  */
13155           else if (cp_lexer_next_token_is_not (parser->lexer,
13156                                                CPP_SEMICOLON))
13157             {
13158               cp_parser_error (parser, "expected %<;%>");
13159               /* Skip tokens until we find a `;'.  */
13160               cp_parser_skip_to_end_of_statement (parser);
13161
13162               break;
13163             }
13164
13165           if (decl)
13166             {
13167               /* Add DECL to the list of members.  */
13168               if (!friend_p)
13169                 finish_member_declaration (decl);
13170
13171               if (TREE_CODE (decl) == FUNCTION_DECL)
13172                 cp_parser_save_default_args (parser, decl);
13173             }
13174         }
13175     }
13176
13177   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
13178 }
13179
13180 /* Parse a pure-specifier.
13181
13182    pure-specifier:
13183      = 0
13184
13185    Returns INTEGER_ZERO_NODE if a pure specifier is found.
13186    Otherwise, ERROR_MARK_NODE is returned.  */
13187
13188 static tree
13189 cp_parser_pure_specifier (cp_parser* parser)
13190 {
13191   cp_token *token;
13192
13193   /* Look for the `=' token.  */
13194   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13195     return error_mark_node;
13196   /* Look for the `0' token.  */
13197   token = cp_parser_require (parser, CPP_NUMBER, "`0'");
13198   /* Unfortunately, this will accept `0L' and `0x00' as well.  We need
13199      to get information from the lexer about how the number was
13200      spelled in order to fix this problem.  */
13201   if (!token || !integer_zerop (token->value))
13202     return error_mark_node;
13203
13204   return integer_zero_node;
13205 }
13206
13207 /* Parse a constant-initializer.
13208
13209    constant-initializer:
13210      = constant-expression
13211
13212    Returns a representation of the constant-expression.  */
13213
13214 static tree
13215 cp_parser_constant_initializer (cp_parser* parser)
13216 {
13217   /* Look for the `=' token.  */
13218   if (!cp_parser_require (parser, CPP_EQ, "`='"))
13219     return error_mark_node;
13220
13221   /* It is invalid to write:
13222
13223        struct S { static const int i = { 7 }; };
13224
13225      */
13226   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13227     {
13228       cp_parser_error (parser,
13229                        "a brace-enclosed initializer is not allowed here");
13230       /* Consume the opening brace.  */
13231       cp_lexer_consume_token (parser->lexer);
13232       /* Skip the initializer.  */
13233       cp_parser_skip_to_closing_brace (parser);
13234       /* Look for the trailing `}'.  */
13235       cp_parser_require (parser, CPP_CLOSE_BRACE, "`}'");
13236
13237       return error_mark_node;
13238     }
13239
13240   return cp_parser_constant_expression (parser,
13241                                         /*allow_non_constant=*/false,
13242                                         NULL);
13243 }
13244
13245 /* Derived classes [gram.class.derived] */
13246
13247 /* Parse a base-clause.
13248
13249    base-clause:
13250      : base-specifier-list
13251
13252    base-specifier-list:
13253      base-specifier
13254      base-specifier-list , base-specifier
13255
13256    Returns a TREE_LIST representing the base-classes, in the order in
13257    which they were declared.  The representation of each node is as
13258    described by cp_parser_base_specifier.
13259
13260    In the case that no bases are specified, this function will return
13261    NULL_TREE, not ERROR_MARK_NODE.  */
13262
13263 static tree
13264 cp_parser_base_clause (cp_parser* parser)
13265 {
13266   tree bases = NULL_TREE;
13267
13268   /* Look for the `:' that begins the list.  */
13269   cp_parser_require (parser, CPP_COLON, "`:'");
13270
13271   /* Scan the base-specifier-list.  */
13272   while (true)
13273     {
13274       cp_token *token;
13275       tree base;
13276
13277       /* Look for the base-specifier.  */
13278       base = cp_parser_base_specifier (parser);
13279       /* Add BASE to the front of the list.  */
13280       if (base != error_mark_node)
13281         {
13282           TREE_CHAIN (base) = bases;
13283           bases = base;
13284         }
13285       /* Peek at the next token.  */
13286       token = cp_lexer_peek_token (parser->lexer);
13287       /* If it's not a comma, then the list is complete.  */
13288       if (token->type != CPP_COMMA)
13289         break;
13290       /* Consume the `,'.  */
13291       cp_lexer_consume_token (parser->lexer);
13292     }
13293
13294   /* PARSER->SCOPE may still be non-NULL at this point, if the last
13295      base class had a qualified name.  However, the next name that
13296      appears is certainly not qualified.  */
13297   parser->scope = NULL_TREE;
13298   parser->qualifying_scope = NULL_TREE;
13299   parser->object_scope = NULL_TREE;
13300
13301   return nreverse (bases);
13302 }
13303
13304 /* Parse a base-specifier.
13305
13306    base-specifier:
13307      :: [opt] nested-name-specifier [opt] class-name
13308      virtual access-specifier [opt] :: [opt] nested-name-specifier
13309        [opt] class-name
13310      access-specifier virtual [opt] :: [opt] nested-name-specifier
13311        [opt] class-name
13312
13313    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
13314    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
13315    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
13316    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
13317
13318 static tree
13319 cp_parser_base_specifier (cp_parser* parser)
13320 {
13321   cp_token *token;
13322   bool done = false;
13323   bool virtual_p = false;
13324   bool duplicate_virtual_error_issued_p = false;
13325   bool duplicate_access_error_issued_p = false;
13326   bool class_scope_p, template_p;
13327   tree access = access_default_node;
13328   tree type;
13329
13330   /* Process the optional `virtual' and `access-specifier'.  */
13331   while (!done)
13332     {
13333       /* Peek at the next token.  */
13334       token = cp_lexer_peek_token (parser->lexer);
13335       /* Process `virtual'.  */
13336       switch (token->keyword)
13337         {
13338         case RID_VIRTUAL:
13339           /* If `virtual' appears more than once, issue an error.  */
13340           if (virtual_p && !duplicate_virtual_error_issued_p)
13341             {
13342               cp_parser_error (parser,
13343                                "%<virtual%> specified more than once in base-specified");
13344               duplicate_virtual_error_issued_p = true;
13345             }
13346
13347           virtual_p = true;
13348
13349           /* Consume the `virtual' token.  */
13350           cp_lexer_consume_token (parser->lexer);
13351
13352           break;
13353
13354         case RID_PUBLIC:
13355         case RID_PROTECTED:
13356         case RID_PRIVATE:
13357           /* If more than one access specifier appears, issue an
13358              error.  */
13359           if (access != access_default_node
13360               && !duplicate_access_error_issued_p)
13361             {
13362               cp_parser_error (parser,
13363                                "more than one access specifier in base-specified");
13364               duplicate_access_error_issued_p = true;
13365             }
13366
13367           access = ridpointers[(int) token->keyword];
13368
13369           /* Consume the access-specifier.  */
13370           cp_lexer_consume_token (parser->lexer);
13371
13372           break;
13373
13374         default:
13375           done = true;
13376           break;
13377         }
13378     }
13379   /* It is not uncommon to see programs mechanically, erroneously, use
13380      the 'typename' keyword to denote (dependent) qualified types
13381      as base classes.  */
13382   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
13383     {
13384       if (!processing_template_decl)
13385         error ("keyword %<typename%> not allowed outside of templates");
13386       else
13387         error ("keyword %<typename%> not allowed in this context "
13388                "(the base class is implicitly a type)");
13389       cp_lexer_consume_token (parser->lexer);
13390     }
13391
13392   /* Look for the optional `::' operator.  */
13393   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13394   /* Look for the nested-name-specifier.  The simplest way to
13395      implement:
13396
13397        [temp.res]
13398
13399        The keyword `typename' is not permitted in a base-specifier or
13400        mem-initializer; in these contexts a qualified name that
13401        depends on a template-parameter is implicitly assumed to be a
13402        type name.
13403
13404      is to pretend that we have seen the `typename' keyword at this
13405      point.  */
13406   cp_parser_nested_name_specifier_opt (parser,
13407                                        /*typename_keyword_p=*/true,
13408                                        /*check_dependency_p=*/true,
13409                                        /*type_p=*/true,
13410                                        /*is_declaration=*/true);
13411   /* If the base class is given by a qualified name, assume that names
13412      we see are type names or templates, as appropriate.  */
13413   class_scope_p = (parser->scope && TYPE_P (parser->scope));
13414   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
13415
13416   /* Finally, look for the class-name.  */
13417   type = cp_parser_class_name (parser,
13418                                class_scope_p,
13419                                template_p,
13420                                /*type_p=*/true,
13421                                /*check_dependency_p=*/true,
13422                                /*class_head_p=*/false,
13423                                /*is_declaration=*/true);
13424
13425   if (type == error_mark_node)
13426     return error_mark_node;
13427
13428   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
13429 }
13430
13431 /* Exception handling [gram.exception] */
13432
13433 /* Parse an (optional) exception-specification.
13434
13435    exception-specification:
13436      throw ( type-id-list [opt] )
13437
13438    Returns a TREE_LIST representing the exception-specification.  The
13439    TREE_VALUE of each node is a type.  */
13440
13441 static tree
13442 cp_parser_exception_specification_opt (cp_parser* parser)
13443 {
13444   cp_token *token;
13445   tree type_id_list;
13446
13447   /* Peek at the next token.  */
13448   token = cp_lexer_peek_token (parser->lexer);
13449   /* If it's not `throw', then there's no exception-specification.  */
13450   if (!cp_parser_is_keyword (token, RID_THROW))
13451     return NULL_TREE;
13452
13453   /* Consume the `throw'.  */
13454   cp_lexer_consume_token (parser->lexer);
13455
13456   /* Look for the `('.  */
13457   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13458
13459   /* Peek at the next token.  */
13460   token = cp_lexer_peek_token (parser->lexer);
13461   /* If it's not a `)', then there is a type-id-list.  */
13462   if (token->type != CPP_CLOSE_PAREN)
13463     {
13464       const char *saved_message;
13465
13466       /* Types may not be defined in an exception-specification.  */
13467       saved_message = parser->type_definition_forbidden_message;
13468       parser->type_definition_forbidden_message
13469         = "types may not be defined in an exception-specification";
13470       /* Parse the type-id-list.  */
13471       type_id_list = cp_parser_type_id_list (parser);
13472       /* Restore the saved message.  */
13473       parser->type_definition_forbidden_message = saved_message;
13474     }
13475   else
13476     type_id_list = empty_except_spec;
13477
13478   /* Look for the `)'.  */
13479   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13480
13481   return type_id_list;
13482 }
13483
13484 /* Parse an (optional) type-id-list.
13485
13486    type-id-list:
13487      type-id
13488      type-id-list , type-id
13489
13490    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
13491    in the order that the types were presented.  */
13492
13493 static tree
13494 cp_parser_type_id_list (cp_parser* parser)
13495 {
13496   tree types = NULL_TREE;
13497
13498   while (true)
13499     {
13500       cp_token *token;
13501       tree type;
13502
13503       /* Get the next type-id.  */
13504       type = cp_parser_type_id (parser);
13505       /* Add it to the list.  */
13506       types = add_exception_specifier (types, type, /*complain=*/1);
13507       /* Peek at the next token.  */
13508       token = cp_lexer_peek_token (parser->lexer);
13509       /* If it is not a `,', we are done.  */
13510       if (token->type != CPP_COMMA)
13511         break;
13512       /* Consume the `,'.  */
13513       cp_lexer_consume_token (parser->lexer);
13514     }
13515
13516   return nreverse (types);
13517 }
13518
13519 /* Parse a try-block.
13520
13521    try-block:
13522      try compound-statement handler-seq  */
13523
13524 static tree
13525 cp_parser_try_block (cp_parser* parser)
13526 {
13527   tree try_block;
13528
13529   cp_parser_require_keyword (parser, RID_TRY, "`try'");
13530   try_block = begin_try_block ();
13531   cp_parser_compound_statement (parser, NULL, true);
13532   finish_try_block (try_block);
13533   cp_parser_handler_seq (parser);
13534   finish_handler_sequence (try_block);
13535
13536   return try_block;
13537 }
13538
13539 /* Parse a function-try-block.
13540
13541    function-try-block:
13542      try ctor-initializer [opt] function-body handler-seq  */
13543
13544 static bool
13545 cp_parser_function_try_block (cp_parser* parser)
13546 {
13547   tree try_block;
13548   bool ctor_initializer_p;
13549
13550   /* Look for the `try' keyword.  */
13551   if (!cp_parser_require_keyword (parser, RID_TRY, "`try'"))
13552     return false;
13553   /* Let the rest of the front-end know where we are.  */
13554   try_block = begin_function_try_block ();
13555   /* Parse the function-body.  */
13556   ctor_initializer_p
13557     = cp_parser_ctor_initializer_opt_and_function_body (parser);
13558   /* We're done with the `try' part.  */
13559   finish_function_try_block (try_block);
13560   /* Parse the handlers.  */
13561   cp_parser_handler_seq (parser);
13562   /* We're done with the handlers.  */
13563   finish_function_handler_sequence (try_block);
13564
13565   return ctor_initializer_p;
13566 }
13567
13568 /* Parse a handler-seq.
13569
13570    handler-seq:
13571      handler handler-seq [opt]  */
13572
13573 static void
13574 cp_parser_handler_seq (cp_parser* parser)
13575 {
13576   while (true)
13577     {
13578       cp_token *token;
13579
13580       /* Parse the handler.  */
13581       cp_parser_handler (parser);
13582       /* Peek at the next token.  */
13583       token = cp_lexer_peek_token (parser->lexer);
13584       /* If it's not `catch' then there are no more handlers.  */
13585       if (!cp_parser_is_keyword (token, RID_CATCH))
13586         break;
13587     }
13588 }
13589
13590 /* Parse a handler.
13591
13592    handler:
13593      catch ( exception-declaration ) compound-statement  */
13594
13595 static void
13596 cp_parser_handler (cp_parser* parser)
13597 {
13598   tree handler;
13599   tree declaration;
13600
13601   cp_parser_require_keyword (parser, RID_CATCH, "`catch'");
13602   handler = begin_handler ();
13603   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13604   declaration = cp_parser_exception_declaration (parser);
13605   finish_handler_parms (declaration, handler);
13606   cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13607   cp_parser_compound_statement (parser, NULL, false);
13608   finish_handler (handler);
13609 }
13610
13611 /* Parse an exception-declaration.
13612
13613    exception-declaration:
13614      type-specifier-seq declarator
13615      type-specifier-seq abstract-declarator
13616      type-specifier-seq
13617      ...
13618
13619    Returns a VAR_DECL for the declaration, or NULL_TREE if the
13620    ellipsis variant is used.  */
13621
13622 static tree
13623 cp_parser_exception_declaration (cp_parser* parser)
13624 {
13625   tree decl;
13626   cp_decl_specifier_seq type_specifiers;
13627   cp_declarator *declarator;
13628   const char *saved_message;
13629
13630   /* If it's an ellipsis, it's easy to handle.  */
13631   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
13632     {
13633       /* Consume the `...' token.  */
13634       cp_lexer_consume_token (parser->lexer);
13635       return NULL_TREE;
13636     }
13637
13638   /* Types may not be defined in exception-declarations.  */
13639   saved_message = parser->type_definition_forbidden_message;
13640   parser->type_definition_forbidden_message
13641     = "types may not be defined in exception-declarations";
13642
13643   /* Parse the type-specifier-seq.  */
13644   cp_parser_type_specifier_seq (parser, &type_specifiers);
13645   /* If it's a `)', then there is no declarator.  */
13646   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
13647     declarator = NULL;
13648   else
13649     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
13650                                        /*ctor_dtor_or_conv_p=*/NULL,
13651                                        /*parenthesized_p=*/NULL,
13652                                        /*member_p=*/false);
13653
13654   /* Restore the saved message.  */
13655   parser->type_definition_forbidden_message = saved_message;
13656
13657   if (type_specifiers.any_specifiers_p)
13658     {
13659       decl = grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
13660       if (decl == NULL_TREE)
13661         error ("invalid catch parameter");
13662     }
13663   else
13664     decl = NULL_TREE;
13665
13666   return decl;
13667 }
13668
13669 /* Parse a throw-expression.
13670
13671    throw-expression:
13672      throw assignment-expression [opt]
13673
13674    Returns a THROW_EXPR representing the throw-expression.  */
13675
13676 static tree
13677 cp_parser_throw_expression (cp_parser* parser)
13678 {
13679   tree expression;
13680   cp_token* token;
13681
13682   cp_parser_require_keyword (parser, RID_THROW, "`throw'");
13683   token = cp_lexer_peek_token (parser->lexer);
13684   /* Figure out whether or not there is an assignment-expression
13685      following the "throw" keyword.  */
13686   if (token->type == CPP_COMMA
13687       || token->type == CPP_SEMICOLON
13688       || token->type == CPP_CLOSE_PAREN
13689       || token->type == CPP_CLOSE_SQUARE
13690       || token->type == CPP_CLOSE_BRACE
13691       || token->type == CPP_COLON)
13692     expression = NULL_TREE;
13693   else
13694     expression = cp_parser_assignment_expression (parser);
13695
13696   return build_throw (expression);
13697 }
13698
13699 /* GNU Extensions */
13700
13701 /* Parse an (optional) asm-specification.
13702
13703    asm-specification:
13704      asm ( string-literal )
13705
13706    If the asm-specification is present, returns a STRING_CST
13707    corresponding to the string-literal.  Otherwise, returns
13708    NULL_TREE.  */
13709
13710 static tree
13711 cp_parser_asm_specification_opt (cp_parser* parser)
13712 {
13713   cp_token *token;
13714   tree asm_specification;
13715
13716   /* Peek at the next token.  */
13717   token = cp_lexer_peek_token (parser->lexer);
13718   /* If the next token isn't the `asm' keyword, then there's no
13719      asm-specification.  */
13720   if (!cp_parser_is_keyword (token, RID_ASM))
13721     return NULL_TREE;
13722
13723   /* Consume the `asm' token.  */
13724   cp_lexer_consume_token (parser->lexer);
13725   /* Look for the `('.  */
13726   cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13727
13728   /* Look for the string-literal.  */
13729   asm_specification = cp_parser_string_literal (parser, false, false);
13730
13731   /* Look for the `)'.  */
13732   cp_parser_require (parser, CPP_CLOSE_PAREN, "`('");
13733
13734   return asm_specification;
13735 }
13736
13737 /* Parse an asm-operand-list.
13738
13739    asm-operand-list:
13740      asm-operand
13741      asm-operand-list , asm-operand
13742
13743    asm-operand:
13744      string-literal ( expression )
13745      [ string-literal ] string-literal ( expression )
13746
13747    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
13748    each node is the expression.  The TREE_PURPOSE is itself a
13749    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
13750    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
13751    is a STRING_CST for the string literal before the parenthesis.  */
13752
13753 static tree
13754 cp_parser_asm_operand_list (cp_parser* parser)
13755 {
13756   tree asm_operands = NULL_TREE;
13757
13758   while (true)
13759     {
13760       tree string_literal;
13761       tree expression;
13762       tree name;
13763
13764       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
13765         {
13766           /* Consume the `[' token.  */
13767           cp_lexer_consume_token (parser->lexer);
13768           /* Read the operand name.  */
13769           name = cp_parser_identifier (parser);
13770           if (name != error_mark_node)
13771             name = build_string (IDENTIFIER_LENGTH (name),
13772                                  IDENTIFIER_POINTER (name));
13773           /* Look for the closing `]'.  */
13774           cp_parser_require (parser, CPP_CLOSE_SQUARE, "`]'");
13775         }
13776       else
13777         name = NULL_TREE;
13778       /* Look for the string-literal.  */
13779       string_literal = cp_parser_string_literal (parser, false, false);
13780
13781       /* Look for the `('.  */
13782       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13783       /* Parse the expression.  */
13784       expression = cp_parser_expression (parser);
13785       /* Look for the `)'.  */
13786       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13787
13788       /* Add this operand to the list.  */
13789       asm_operands = tree_cons (build_tree_list (name, string_literal),
13790                                 expression,
13791                                 asm_operands);
13792       /* If the next token is not a `,', there are no more
13793          operands.  */
13794       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13795         break;
13796       /* Consume the `,'.  */
13797       cp_lexer_consume_token (parser->lexer);
13798     }
13799
13800   return nreverse (asm_operands);
13801 }
13802
13803 /* Parse an asm-clobber-list.
13804
13805    asm-clobber-list:
13806      string-literal
13807      asm-clobber-list , string-literal
13808
13809    Returns a TREE_LIST, indicating the clobbers in the order that they
13810    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
13811
13812 static tree
13813 cp_parser_asm_clobber_list (cp_parser* parser)
13814 {
13815   tree clobbers = NULL_TREE;
13816
13817   while (true)
13818     {
13819       tree string_literal;
13820
13821       /* Look for the string literal.  */
13822       string_literal = cp_parser_string_literal (parser, false, false);
13823       /* Add it to the list.  */
13824       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
13825       /* If the next token is not a `,', then the list is
13826          complete.  */
13827       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13828         break;
13829       /* Consume the `,' token.  */
13830       cp_lexer_consume_token (parser->lexer);
13831     }
13832
13833   return clobbers;
13834 }
13835
13836 /* Parse an (optional) series of attributes.
13837
13838    attributes:
13839      attributes attribute
13840
13841    attribute:
13842      __attribute__ (( attribute-list [opt] ))
13843
13844    The return value is as for cp_parser_attribute_list.  */
13845
13846 static tree
13847 cp_parser_attributes_opt (cp_parser* parser)
13848 {
13849   tree attributes = NULL_TREE;
13850
13851   while (true)
13852     {
13853       cp_token *token;
13854       tree attribute_list;
13855
13856       /* Peek at the next token.  */
13857       token = cp_lexer_peek_token (parser->lexer);
13858       /* If it's not `__attribute__', then we're done.  */
13859       if (token->keyword != RID_ATTRIBUTE)
13860         break;
13861
13862       /* Consume the `__attribute__' keyword.  */
13863       cp_lexer_consume_token (parser->lexer);
13864       /* Look for the two `(' tokens.  */
13865       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13866       cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
13867
13868       /* Peek at the next token.  */
13869       token = cp_lexer_peek_token (parser->lexer);
13870       if (token->type != CPP_CLOSE_PAREN)
13871         /* Parse the attribute-list.  */
13872         attribute_list = cp_parser_attribute_list (parser);
13873       else
13874         /* If the next token is a `)', then there is no attribute
13875            list.  */
13876         attribute_list = NULL;
13877
13878       /* Look for the two `)' tokens.  */
13879       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13880       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
13881
13882       /* Add these new attributes to the list.  */
13883       attributes = chainon (attributes, attribute_list);
13884     }
13885
13886   return attributes;
13887 }
13888
13889 /* Parse an attribute-list.
13890
13891    attribute-list:
13892      attribute
13893      attribute-list , attribute
13894
13895    attribute:
13896      identifier
13897      identifier ( identifier )
13898      identifier ( identifier , expression-list )
13899      identifier ( expression-list )
13900
13901    Returns a TREE_LIST.  Each node corresponds to an attribute.  THe
13902    TREE_PURPOSE of each node is the identifier indicating which
13903    attribute is in use.  The TREE_VALUE represents the arguments, if
13904    any.  */
13905
13906 static tree
13907 cp_parser_attribute_list (cp_parser* parser)
13908 {
13909   tree attribute_list = NULL_TREE;
13910   bool save_translate_strings_p = parser->translate_strings_p;
13911
13912   parser->translate_strings_p = false;
13913   while (true)
13914     {
13915       cp_token *token;
13916       tree identifier;
13917       tree attribute;
13918
13919       /* Look for the identifier.  We also allow keywords here; for
13920          example `__attribute__ ((const))' is legal.  */
13921       token = cp_lexer_peek_token (parser->lexer);
13922       if (token->type != CPP_NAME
13923           && token->type != CPP_KEYWORD)
13924         return error_mark_node;
13925       /* Consume the token.  */
13926       token = cp_lexer_consume_token (parser->lexer);
13927
13928       /* Save away the identifier that indicates which attribute this is.  */
13929       identifier = token->value;
13930       attribute = build_tree_list (identifier, NULL_TREE);
13931
13932       /* Peek at the next token.  */
13933       token = cp_lexer_peek_token (parser->lexer);
13934       /* If it's an `(', then parse the attribute arguments.  */
13935       if (token->type == CPP_OPEN_PAREN)
13936         {
13937           tree arguments;
13938
13939           arguments = (cp_parser_parenthesized_expression_list
13940                        (parser, true, /*non_constant_p=*/NULL));
13941           /* Save the identifier and arguments away.  */
13942           TREE_VALUE (attribute) = arguments;
13943         }
13944
13945       /* Add this attribute to the list.  */
13946       TREE_CHAIN (attribute) = attribute_list;
13947       attribute_list = attribute;
13948
13949       /* Now, look for more attributes.  */
13950       token = cp_lexer_peek_token (parser->lexer);
13951       /* If the next token isn't a `,', we're done.  */
13952       if (token->type != CPP_COMMA)
13953         break;
13954
13955       /* Consume the comma and keep going.  */
13956       cp_lexer_consume_token (parser->lexer);
13957     }
13958   parser->translate_strings_p = save_translate_strings_p;
13959
13960   /* We built up the list in reverse order.  */
13961   return nreverse (attribute_list);
13962 }
13963
13964 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
13965    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
13966    current value of the PEDANTIC flag, regardless of whether or not
13967    the `__extension__' keyword is present.  The caller is responsible
13968    for restoring the value of the PEDANTIC flag.  */
13969
13970 static bool
13971 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
13972 {
13973   /* Save the old value of the PEDANTIC flag.  */
13974   *saved_pedantic = pedantic;
13975
13976   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
13977     {
13978       /* Consume the `__extension__' token.  */
13979       cp_lexer_consume_token (parser->lexer);
13980       /* We're not being pedantic while the `__extension__' keyword is
13981          in effect.  */
13982       pedantic = 0;
13983
13984       return true;
13985     }
13986
13987   return false;
13988 }
13989
13990 /* Parse a label declaration.
13991
13992    label-declaration:
13993      __label__ label-declarator-seq ;
13994
13995    label-declarator-seq:
13996      identifier , label-declarator-seq
13997      identifier  */
13998
13999 static void
14000 cp_parser_label_declaration (cp_parser* parser)
14001 {
14002   /* Look for the `__label__' keyword.  */
14003   cp_parser_require_keyword (parser, RID_LABEL, "`__label__'");
14004
14005   while (true)
14006     {
14007       tree identifier;
14008
14009       /* Look for an identifier.  */
14010       identifier = cp_parser_identifier (parser);
14011       /* Declare it as a lobel.  */
14012       finish_label_decl (identifier);
14013       /* If the next token is a `;', stop.  */
14014       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14015         break;
14016       /* Look for the `,' separating the label declarations.  */
14017       cp_parser_require (parser, CPP_COMMA, "`,'");
14018     }
14019
14020   /* Look for the final `;'.  */
14021   cp_parser_require (parser, CPP_SEMICOLON, "`;'");
14022 }
14023
14024 /* Support Functions */
14025
14026 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
14027    NAME should have one of the representations used for an
14028    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
14029    is returned.  If PARSER->SCOPE is a dependent type, then a
14030    SCOPE_REF is returned.
14031
14032    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
14033    returned; the name was already resolved when the TEMPLATE_ID_EXPR
14034    was formed.  Abstractly, such entities should not be passed to this
14035    function, because they do not need to be looked up, but it is
14036    simpler to check for this special case here, rather than at the
14037    call-sites.
14038
14039    In cases not explicitly covered above, this function returns a
14040    DECL, OVERLOAD, or baselink representing the result of the lookup.
14041    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
14042    is returned.
14043
14044    If IS_TYPE is TRUE, bindings that do not refer to types are
14045    ignored.
14046
14047    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
14048    ignored.
14049
14050    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
14051    are ignored.
14052
14053    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
14054    types.  
14055
14056    If AMBIGUOUS_P is non-NULL, it is set to true if name-lookup
14057    results in an ambiguity, and false otherwise.  */
14058
14059 static tree
14060 cp_parser_lookup_name (cp_parser *parser, tree name,
14061                        bool is_type, bool is_template, bool is_namespace,
14062                        bool check_dependency,
14063                        bool *ambiguous_p)
14064 {
14065   tree decl;
14066   tree object_type = parser->context->object_type;
14067
14068   /* Assume that the lookup will be unambiguous.  */
14069   if (ambiguous_p)
14070     *ambiguous_p = false;
14071
14072   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
14073      no longer valid.  Note that if we are parsing tentatively, and
14074      the parse fails, OBJECT_TYPE will be automatically restored.  */
14075   parser->context->object_type = NULL_TREE;
14076
14077   if (name == error_mark_node)
14078     return error_mark_node;
14079
14080   /* A template-id has already been resolved; there is no lookup to
14081      do.  */
14082   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
14083     return name;
14084   if (BASELINK_P (name))
14085     {
14086       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
14087                   == TEMPLATE_ID_EXPR);
14088       return name;
14089     }
14090
14091   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
14092      it should already have been checked to make sure that the name
14093      used matches the type being destroyed.  */
14094   if (TREE_CODE (name) == BIT_NOT_EXPR)
14095     {
14096       tree type;
14097
14098       /* Figure out to which type this destructor applies.  */
14099       if (parser->scope)
14100         type = parser->scope;
14101       else if (object_type)
14102         type = object_type;
14103       else
14104         type = current_class_type;
14105       /* If that's not a class type, there is no destructor.  */
14106       if (!type || !CLASS_TYPE_P (type))
14107         return error_mark_node;
14108       if (!CLASSTYPE_DESTRUCTORS (type))
14109           return error_mark_node;
14110       /* If it was a class type, return the destructor.  */
14111       return CLASSTYPE_DESTRUCTORS (type);
14112     }
14113
14114   /* By this point, the NAME should be an ordinary identifier.  If
14115      the id-expression was a qualified name, the qualifying scope is
14116      stored in PARSER->SCOPE at this point.  */
14117   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
14118
14119   /* Perform the lookup.  */
14120   if (parser->scope)
14121     {
14122       bool dependent_p;
14123
14124       if (parser->scope == error_mark_node)
14125         return error_mark_node;
14126
14127       /* If the SCOPE is dependent, the lookup must be deferred until
14128          the template is instantiated -- unless we are explicitly
14129          looking up names in uninstantiated templates.  Even then, we
14130          cannot look up the name if the scope is not a class type; it
14131          might, for example, be a template type parameter.  */
14132       dependent_p = (TYPE_P (parser->scope)
14133                      && !(parser->in_declarator_p
14134                           && currently_open_class (parser->scope))
14135                      && dependent_type_p (parser->scope));
14136       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
14137            && dependent_p)
14138         {
14139           if (is_type)
14140             /* The resolution to Core Issue 180 says that `struct A::B'
14141                should be considered a type-name, even if `A' is
14142                dependent.  */
14143             decl = TYPE_NAME (make_typename_type (parser->scope,
14144                                                   name,
14145                                                   /*complain=*/1));
14146           else if (is_template)
14147             decl = make_unbound_class_template (parser->scope,
14148                                                 name, NULL_TREE,
14149                                                 /*complain=*/1);
14150           else
14151             decl = build_nt (SCOPE_REF, parser->scope, name);
14152         }
14153       else
14154         {
14155           bool pop_p = false;
14156
14157           /* If PARSER->SCOPE is a dependent type, then it must be a
14158              class type, and we must not be checking dependencies;
14159              otherwise, we would have processed this lookup above.  So
14160              that PARSER->SCOPE is not considered a dependent base by
14161              lookup_member, we must enter the scope here.  */
14162           if (dependent_p)
14163             pop_p = push_scope (parser->scope);
14164           /* If the PARSER->SCOPE is a a template specialization, it
14165              may be instantiated during name lookup.  In that case,
14166              errors may be issued.  Even if we rollback the current
14167              tentative parse, those errors are valid.  */
14168           decl = lookup_qualified_name (parser->scope, name, is_type,
14169                                         /*complain=*/true);
14170           if (pop_p)
14171             pop_scope (parser->scope);
14172         }
14173       parser->qualifying_scope = parser->scope;
14174       parser->object_scope = NULL_TREE;
14175     }
14176   else if (object_type)
14177     {
14178       tree object_decl = NULL_TREE;
14179       /* Look up the name in the scope of the OBJECT_TYPE, unless the
14180          OBJECT_TYPE is not a class.  */
14181       if (CLASS_TYPE_P (object_type))
14182         /* If the OBJECT_TYPE is a template specialization, it may
14183            be instantiated during name lookup.  In that case, errors
14184            may be issued.  Even if we rollback the current tentative
14185            parse, those errors are valid.  */
14186         object_decl = lookup_member (object_type,
14187                                      name,
14188                                      /*protect=*/0, is_type);
14189       /* Look it up in the enclosing context, too.  */
14190       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14191                                /*block_p=*/true, is_namespace,
14192                                /*flags=*/0);
14193       parser->object_scope = object_type;
14194       parser->qualifying_scope = NULL_TREE;
14195       if (object_decl)
14196         decl = object_decl;
14197     }
14198   else
14199     {
14200       decl = lookup_name_real (name, is_type, /*nonclass=*/0,
14201                                /*block_p=*/true, is_namespace,
14202                                /*flags=*/0);
14203       parser->qualifying_scope = NULL_TREE;
14204       parser->object_scope = NULL_TREE;
14205     }
14206
14207   /* If the lookup failed, let our caller know.  */
14208   if (!decl
14209       || decl == error_mark_node
14210       || (TREE_CODE (decl) == FUNCTION_DECL
14211           && DECL_ANTICIPATED (decl)))
14212     return error_mark_node;
14213
14214   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
14215   if (TREE_CODE (decl) == TREE_LIST)
14216     {
14217       if (ambiguous_p)
14218         *ambiguous_p = true;
14219       /* The error message we have to print is too complicated for
14220          cp_parser_error, so we incorporate its actions directly.  */
14221       if (!cp_parser_simulate_error (parser))
14222         {
14223           error ("reference to %qD is ambiguous", name);
14224           print_candidates (decl);
14225         }
14226       return error_mark_node;
14227     }
14228
14229   gcc_assert (DECL_P (decl)
14230               || TREE_CODE (decl) == OVERLOAD
14231               || TREE_CODE (decl) == SCOPE_REF
14232               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
14233               || BASELINK_P (decl));
14234
14235   /* If we have resolved the name of a member declaration, check to
14236      see if the declaration is accessible.  When the name resolves to
14237      set of overloaded functions, accessibility is checked when
14238      overload resolution is done.
14239
14240      During an explicit instantiation, access is not checked at all,
14241      as per [temp.explicit].  */
14242   if (DECL_P (decl))
14243     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
14244
14245   return decl;
14246 }
14247
14248 /* Like cp_parser_lookup_name, but for use in the typical case where
14249    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
14250    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
14251
14252 static tree
14253 cp_parser_lookup_name_simple (cp_parser* parser, tree name)
14254 {
14255   return cp_parser_lookup_name (parser, name,
14256                                 /*is_type=*/false,
14257                                 /*is_template=*/false,
14258                                 /*is_namespace=*/false,
14259                                 /*check_dependency=*/true,
14260                                 /*ambiguous_p=*/NULL);
14261 }
14262
14263 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
14264    the current context, return the TYPE_DECL.  If TAG_NAME_P is
14265    true, the DECL indicates the class being defined in a class-head,
14266    or declared in an elaborated-type-specifier.
14267
14268    Otherwise, return DECL.  */
14269
14270 static tree
14271 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
14272 {
14273   /* If the TEMPLATE_DECL is being declared as part of a class-head,
14274      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
14275
14276        struct A {
14277          template <typename T> struct B;
14278        };
14279
14280        template <typename T> struct A::B {};
14281
14282      Similarly, in a elaborated-type-specifier:
14283
14284        namespace N { struct X{}; }
14285
14286        struct A {
14287          template <typename T> friend struct N::X;
14288        };
14289
14290      However, if the DECL refers to a class type, and we are in
14291      the scope of the class, then the name lookup automatically
14292      finds the TYPE_DECL created by build_self_reference rather
14293      than a TEMPLATE_DECL.  For example, in:
14294
14295        template <class T> struct S {
14296          S s;
14297        };
14298
14299      there is no need to handle such case.  */
14300
14301   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
14302     return DECL_TEMPLATE_RESULT (decl);
14303
14304   return decl;
14305 }
14306
14307 /* If too many, or too few, template-parameter lists apply to the
14308    declarator, issue an error message.  Returns TRUE if all went well,
14309    and FALSE otherwise.  */
14310
14311 static bool
14312 cp_parser_check_declarator_template_parameters (cp_parser* parser,
14313                                                 cp_declarator *declarator)
14314 {
14315   unsigned num_templates;
14316
14317   /* We haven't seen any classes that involve template parameters yet.  */
14318   num_templates = 0;
14319
14320   switch (declarator->kind)
14321     {
14322     case cdk_id:
14323       if (TREE_CODE (declarator->u.id.name) == SCOPE_REF)
14324         {
14325           tree scope;
14326           tree member;
14327
14328           scope = TREE_OPERAND (declarator->u.id.name, 0);
14329           member = TREE_OPERAND (declarator->u.id.name, 1);
14330
14331           while (scope && CLASS_TYPE_P (scope))
14332             {
14333               /* You're supposed to have one `template <...>'
14334                  for every template class, but you don't need one
14335                  for a full specialization.  For example:
14336
14337                  template <class T> struct S{};
14338                  template <> struct S<int> { void f(); };
14339                  void S<int>::f () {}
14340
14341                  is correct; there shouldn't be a `template <>' for
14342                  the definition of `S<int>::f'.  */
14343               if (CLASSTYPE_TEMPLATE_INFO (scope)
14344                   && (CLASSTYPE_TEMPLATE_INSTANTIATION (scope)
14345                       || uses_template_parms (CLASSTYPE_TI_ARGS (scope)))
14346                   && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
14347                 ++num_templates;
14348
14349               scope = TYPE_CONTEXT (scope);
14350             }
14351         }
14352
14353       /* If the DECLARATOR has the form `X<y>' then it uses one
14354          additional level of template parameters.  */
14355       if (TREE_CODE (declarator->u.id.name) == TEMPLATE_ID_EXPR)
14356         ++num_templates;
14357
14358       return cp_parser_check_template_parameters (parser,
14359                                                   num_templates);
14360
14361     case cdk_function:
14362     case cdk_array:
14363     case cdk_pointer:
14364     case cdk_reference:
14365     case cdk_ptrmem:
14366       return (cp_parser_check_declarator_template_parameters
14367               (parser, declarator->declarator));
14368
14369     case cdk_error:
14370       return true;
14371
14372     default:
14373       gcc_unreachable ();
14374     }
14375   return false;
14376 }
14377
14378 /* NUM_TEMPLATES were used in the current declaration.  If that is
14379    invalid, return FALSE and issue an error messages.  Otherwise,
14380    return TRUE.  */
14381
14382 static bool
14383 cp_parser_check_template_parameters (cp_parser* parser,
14384                                      unsigned num_templates)
14385 {
14386   /* If there are more template classes than parameter lists, we have
14387      something like:
14388
14389        template <class T> void S<T>::R<T>::f ();  */
14390   if (parser->num_template_parameter_lists < num_templates)
14391     {
14392       error ("too few template-parameter-lists");
14393       return false;
14394     }
14395   /* If there are the same number of template classes and parameter
14396      lists, that's OK.  */
14397   if (parser->num_template_parameter_lists == num_templates)
14398     return true;
14399   /* If there are more, but only one more, then we are referring to a
14400      member template.  That's OK too.  */
14401   if (parser->num_template_parameter_lists == num_templates + 1)
14402       return true;
14403   /* Otherwise, there are too many template parameter lists.  We have
14404      something like:
14405
14406      template <class T> template <class U> void S::f();  */
14407   error ("too many template-parameter-lists");
14408   return false;
14409 }
14410
14411 /* Parse an optional `::' token indicating that the following name is
14412    from the global namespace.  If so, PARSER->SCOPE is set to the
14413    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
14414    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
14415    Returns the new value of PARSER->SCOPE, if the `::' token is
14416    present, and NULL_TREE otherwise.  */
14417
14418 static tree
14419 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
14420 {
14421   cp_token *token;
14422
14423   /* Peek at the next token.  */
14424   token = cp_lexer_peek_token (parser->lexer);
14425   /* If we're looking at a `::' token then we're starting from the
14426      global namespace, not our current location.  */
14427   if (token->type == CPP_SCOPE)
14428     {
14429       /* Consume the `::' token.  */
14430       cp_lexer_consume_token (parser->lexer);
14431       /* Set the SCOPE so that we know where to start the lookup.  */
14432       parser->scope = global_namespace;
14433       parser->qualifying_scope = global_namespace;
14434       parser->object_scope = NULL_TREE;
14435
14436       return parser->scope;
14437     }
14438   else if (!current_scope_valid_p)
14439     {
14440       parser->scope = NULL_TREE;
14441       parser->qualifying_scope = NULL_TREE;
14442       parser->object_scope = NULL_TREE;
14443     }
14444
14445   return NULL_TREE;
14446 }
14447
14448 /* Returns TRUE if the upcoming token sequence is the start of a
14449    constructor declarator.  If FRIEND_P is true, the declarator is
14450    preceded by the `friend' specifier.  */
14451
14452 static bool
14453 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
14454 {
14455   bool constructor_p;
14456   tree type_decl = NULL_TREE;
14457   bool nested_name_p;
14458   cp_token *next_token;
14459
14460   /* The common case is that this is not a constructor declarator, so
14461      try to avoid doing lots of work if at all possible.  It's not
14462      valid declare a constructor at function scope.  */
14463   if (at_function_scope_p ())
14464     return false;
14465   /* And only certain tokens can begin a constructor declarator.  */
14466   next_token = cp_lexer_peek_token (parser->lexer);
14467   if (next_token->type != CPP_NAME
14468       && next_token->type != CPP_SCOPE
14469       && next_token->type != CPP_NESTED_NAME_SPECIFIER
14470       && next_token->type != CPP_TEMPLATE_ID)
14471     return false;
14472
14473   /* Parse tentatively; we are going to roll back all of the tokens
14474      consumed here.  */
14475   cp_parser_parse_tentatively (parser);
14476   /* Assume that we are looking at a constructor declarator.  */
14477   constructor_p = true;
14478
14479   /* Look for the optional `::' operator.  */
14480   cp_parser_global_scope_opt (parser,
14481                               /*current_scope_valid_p=*/false);
14482   /* Look for the nested-name-specifier.  */
14483   nested_name_p
14484     = (cp_parser_nested_name_specifier_opt (parser,
14485                                             /*typename_keyword_p=*/false,
14486                                             /*check_dependency_p=*/false,
14487                                             /*type_p=*/false,
14488                                             /*is_declaration=*/false)
14489        != NULL_TREE);
14490   /* Outside of a class-specifier, there must be a
14491      nested-name-specifier.  */
14492   if (!nested_name_p &&
14493       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
14494        || friend_p))
14495     constructor_p = false;
14496   /* If we still think that this might be a constructor-declarator,
14497      look for a class-name.  */
14498   if (constructor_p)
14499     {
14500       /* If we have:
14501
14502            template <typename T> struct S { S(); };
14503            template <typename T> S<T>::S ();
14504
14505          we must recognize that the nested `S' names a class.
14506          Similarly, for:
14507
14508            template <typename T> S<T>::S<T> ();
14509
14510          we must recognize that the nested `S' names a template.  */
14511       type_decl = cp_parser_class_name (parser,
14512                                         /*typename_keyword_p=*/false,
14513                                         /*template_keyword_p=*/false,
14514                                         /*type_p=*/false,
14515                                         /*check_dependency_p=*/false,
14516                                         /*class_head_p=*/false,
14517                                         /*is_declaration=*/false);
14518       /* If there was no class-name, then this is not a constructor.  */
14519       constructor_p = !cp_parser_error_occurred (parser);
14520     }
14521
14522   /* If we're still considering a constructor, we have to see a `(',
14523      to begin the parameter-declaration-clause, followed by either a
14524      `)', an `...', or a decl-specifier.  We need to check for a
14525      type-specifier to avoid being fooled into thinking that:
14526
14527        S::S (f) (int);
14528
14529      is a constructor.  (It is actually a function named `f' that
14530      takes one parameter (of type `int') and returns a value of type
14531      `S::S'.  */
14532   if (constructor_p
14533       && cp_parser_require (parser, CPP_OPEN_PAREN, "`('"))
14534     {
14535       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
14536           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
14537           /* A parameter declaration begins with a decl-specifier,
14538              which is either the "attribute" keyword, a storage class
14539              specifier, or (usually) a type-specifier.  */
14540           && !cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE)
14541           && !cp_parser_storage_class_specifier_opt (parser))
14542         {
14543           tree type;
14544           bool pop_p = false;
14545           unsigned saved_num_template_parameter_lists;
14546
14547           /* Names appearing in the type-specifier should be looked up
14548              in the scope of the class.  */
14549           if (current_class_type)
14550             type = NULL_TREE;
14551           else
14552             {
14553               type = TREE_TYPE (type_decl);
14554               if (TREE_CODE (type) == TYPENAME_TYPE)
14555                 {
14556                   type = resolve_typename_type (type,
14557                                                 /*only_current_p=*/false);
14558                   if (type == error_mark_node)
14559                     {
14560                       cp_parser_abort_tentative_parse (parser);
14561                       return false;
14562                     }
14563                 }
14564               pop_p = push_scope (type);
14565             }
14566
14567           /* Inside the constructor parameter list, surrounding
14568              template-parameter-lists do not apply.  */
14569           saved_num_template_parameter_lists
14570             = parser->num_template_parameter_lists;
14571           parser->num_template_parameter_lists = 0;
14572
14573           /* Look for the type-specifier.  */
14574           cp_parser_type_specifier (parser,
14575                                     CP_PARSER_FLAGS_NONE,
14576                                     /*decl_specs=*/NULL,
14577                                     /*is_declarator=*/true,
14578                                     /*declares_class_or_enum=*/NULL,
14579                                     /*is_cv_qualifier=*/NULL);
14580
14581           parser->num_template_parameter_lists
14582             = saved_num_template_parameter_lists;
14583
14584           /* Leave the scope of the class.  */
14585           if (pop_p)
14586             pop_scope (type);
14587
14588           constructor_p = !cp_parser_error_occurred (parser);
14589         }
14590     }
14591   else
14592     constructor_p = false;
14593   /* We did not really want to consume any tokens.  */
14594   cp_parser_abort_tentative_parse (parser);
14595
14596   return constructor_p;
14597 }
14598
14599 /* Parse the definition of the function given by the DECL_SPECIFIERS,
14600    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
14601    they must be performed once we are in the scope of the function.
14602
14603    Returns the function defined.  */
14604
14605 static tree
14606 cp_parser_function_definition_from_specifiers_and_declarator
14607   (cp_parser* parser,
14608    cp_decl_specifier_seq *decl_specifiers,
14609    tree attributes,
14610    const cp_declarator *declarator)
14611 {
14612   tree fn;
14613   bool success_p;
14614
14615   /* Begin the function-definition.  */
14616   success_p = start_function (decl_specifiers, declarator, attributes);
14617
14618   /* The things we're about to see are not directly qualified by any
14619      template headers we've seen thus far.  */
14620   reset_specialization ();
14621
14622   /* If there were names looked up in the decl-specifier-seq that we
14623      did not check, check them now.  We must wait until we are in the
14624      scope of the function to perform the checks, since the function
14625      might be a friend.  */
14626   perform_deferred_access_checks ();
14627
14628   if (!success_p)
14629     {
14630       /* Skip the entire function.  */
14631       error ("invalid function declaration");
14632       cp_parser_skip_to_end_of_block_or_statement (parser);
14633       fn = error_mark_node;
14634     }
14635   else
14636     fn = cp_parser_function_definition_after_declarator (parser,
14637                                                          /*inline_p=*/false);
14638
14639   return fn;
14640 }
14641
14642 /* Parse the part of a function-definition that follows the
14643    declarator.  INLINE_P is TRUE iff this function is an inline
14644    function defined with a class-specifier.
14645
14646    Returns the function defined.  */
14647
14648 static tree
14649 cp_parser_function_definition_after_declarator (cp_parser* parser,
14650                                                 bool inline_p)
14651 {
14652   tree fn;
14653   bool ctor_initializer_p = false;
14654   bool saved_in_unbraced_linkage_specification_p;
14655   unsigned saved_num_template_parameter_lists;
14656
14657   /* If the next token is `return', then the code may be trying to
14658      make use of the "named return value" extension that G++ used to
14659      support.  */
14660   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
14661     {
14662       /* Consume the `return' keyword.  */
14663       cp_lexer_consume_token (parser->lexer);
14664       /* Look for the identifier that indicates what value is to be
14665          returned.  */
14666       cp_parser_identifier (parser);
14667       /* Issue an error message.  */
14668       error ("named return values are no longer supported");
14669       /* Skip tokens until we reach the start of the function body.  */
14670       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
14671              && cp_lexer_next_token_is_not (parser->lexer, CPP_EOF))
14672         cp_lexer_consume_token (parser->lexer);
14673     }
14674   /* The `extern' in `extern "C" void f () { ... }' does not apply to
14675      anything declared inside `f'.  */
14676   saved_in_unbraced_linkage_specification_p
14677     = parser->in_unbraced_linkage_specification_p;
14678   parser->in_unbraced_linkage_specification_p = false;
14679   /* Inside the function, surrounding template-parameter-lists do not
14680      apply.  */
14681   saved_num_template_parameter_lists
14682     = parser->num_template_parameter_lists;
14683   parser->num_template_parameter_lists = 0;
14684   /* If the next token is `try', then we are looking at a
14685      function-try-block.  */
14686   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
14687     ctor_initializer_p = cp_parser_function_try_block (parser);
14688   /* A function-try-block includes the function-body, so we only do
14689      this next part if we're not processing a function-try-block.  */
14690   else
14691     ctor_initializer_p
14692       = cp_parser_ctor_initializer_opt_and_function_body (parser);
14693
14694   /* Finish the function.  */
14695   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
14696                         (inline_p ? 2 : 0));
14697   /* Generate code for it, if necessary.  */
14698   expand_or_defer_fn (fn);
14699   /* Restore the saved values.  */
14700   parser->in_unbraced_linkage_specification_p
14701     = saved_in_unbraced_linkage_specification_p;
14702   parser->num_template_parameter_lists
14703     = saved_num_template_parameter_lists;
14704
14705   return fn;
14706 }
14707
14708 /* Parse a template-declaration, assuming that the `export' (and
14709    `extern') keywords, if present, has already been scanned.  MEMBER_P
14710    is as for cp_parser_template_declaration.  */
14711
14712 static void
14713 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
14714 {
14715   tree decl = NULL_TREE;
14716   tree parameter_list;
14717   bool friend_p = false;
14718
14719   /* Look for the `template' keyword.  */
14720   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "`template'"))
14721     return;
14722
14723   /* And the `<'.  */
14724   if (!cp_parser_require (parser, CPP_LESS, "`<'"))
14725     return;
14726
14727   /* If the next token is `>', then we have an invalid
14728      specialization.  Rather than complain about an invalid template
14729      parameter, issue an error message here.  */
14730   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
14731     {
14732       cp_parser_error (parser, "invalid explicit specialization");
14733       begin_specialization ();
14734       parameter_list = NULL_TREE;
14735     }
14736   else
14737     {
14738       /* Parse the template parameters.  */
14739       begin_template_parm_list ();
14740       parameter_list = cp_parser_template_parameter_list (parser);
14741       parameter_list = end_template_parm_list (parameter_list);
14742     }
14743
14744   /* Look for the `>'.  */
14745   cp_parser_skip_until_found (parser, CPP_GREATER, "`>'");
14746   /* We just processed one more parameter list.  */
14747   ++parser->num_template_parameter_lists;
14748   /* If the next token is `template', there are more template
14749      parameters.  */
14750   if (cp_lexer_next_token_is_keyword (parser->lexer,
14751                                       RID_TEMPLATE))
14752     cp_parser_template_declaration_after_export (parser, member_p);
14753   else
14754     {
14755       /* There are no access checks when parsing a template, as we do not
14756          know if a specialization will be a friend.  */
14757       push_deferring_access_checks (dk_no_check);
14758
14759       decl = cp_parser_single_declaration (parser,
14760                                            member_p,
14761                                            &friend_p);
14762
14763       pop_deferring_access_checks ();
14764
14765       /* If this is a member template declaration, let the front
14766          end know.  */
14767       if (member_p && !friend_p && decl)
14768         {
14769           if (TREE_CODE (decl) == TYPE_DECL)
14770             cp_parser_check_access_in_redeclaration (decl);
14771
14772           decl = finish_member_template_decl (decl);
14773         }
14774       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
14775         make_friend_class (current_class_type, TREE_TYPE (decl),
14776                            /*complain=*/true);
14777     }
14778   /* We are done with the current parameter list.  */
14779   --parser->num_template_parameter_lists;
14780
14781   /* Finish up.  */
14782   finish_template_decl (parameter_list);
14783
14784   /* Register member declarations.  */
14785   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
14786     finish_member_declaration (decl);
14787
14788   /* If DECL is a function template, we must return to parse it later.
14789      (Even though there is no definition, there might be default
14790      arguments that need handling.)  */
14791   if (member_p && decl
14792       && (TREE_CODE (decl) == FUNCTION_DECL
14793           || DECL_FUNCTION_TEMPLATE_P (decl)))
14794     TREE_VALUE (parser->unparsed_functions_queues)
14795       = tree_cons (NULL_TREE, decl,
14796                    TREE_VALUE (parser->unparsed_functions_queues));
14797 }
14798
14799 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
14800    `function-definition' sequence.  MEMBER_P is true, this declaration
14801    appears in a class scope.
14802
14803    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
14804    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
14805
14806 static tree
14807 cp_parser_single_declaration (cp_parser* parser,
14808                               bool member_p,
14809                               bool* friend_p)
14810 {
14811   int declares_class_or_enum;
14812   tree decl = NULL_TREE;
14813   cp_decl_specifier_seq decl_specifiers;
14814   bool function_definition_p = false;
14815
14816   /* Defer access checks until we know what is being declared.  */
14817   push_deferring_access_checks (dk_deferred);
14818
14819   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
14820      alternative.  */
14821   cp_parser_decl_specifier_seq (parser,
14822                                 CP_PARSER_FLAGS_OPTIONAL,
14823                                 &decl_specifiers,
14824                                 &declares_class_or_enum);
14825   if (friend_p)
14826     *friend_p = cp_parser_friend_p (&decl_specifiers);
14827   /* Gather up the access checks that occurred the
14828      decl-specifier-seq.  */
14829   stop_deferring_access_checks ();
14830
14831   /* Check for the declaration of a template class.  */
14832   if (declares_class_or_enum)
14833     {
14834       if (cp_parser_declares_only_class_p (parser))
14835         {
14836           decl = shadow_tag (&decl_specifiers);
14837
14838           /* In this case:
14839
14840                struct C {
14841                  friend template <typename T> struct A<T>::B;
14842                };
14843
14844              A<T>::B will be represented by a TYPENAME_TYPE, and
14845              therefore not recognized by shadow_tag.  */
14846           if (friend_p && *friend_p
14847               && !decl
14848               && decl_specifiers.type
14849               && TYPE_P (decl_specifiers.type))
14850             decl = decl_specifiers.type;
14851
14852           if (decl && decl != error_mark_node)
14853             decl = TYPE_NAME (decl);
14854           else
14855             decl = error_mark_node;
14856         }
14857     }
14858   else
14859     decl = NULL_TREE;
14860   /* If it's not a template class, try for a template function.  If
14861      the next token is a `;', then this declaration does not declare
14862      anything.  But, if there were errors in the decl-specifiers, then
14863      the error might well have come from an attempted class-specifier.
14864      In that case, there's no need to warn about a missing declarator.  */
14865   if (!decl
14866       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
14867           || decl_specifiers.type != error_mark_node))
14868     decl = cp_parser_init_declarator (parser,
14869                                       &decl_specifiers,
14870                                       /*function_definition_allowed_p=*/true,
14871                                       member_p,
14872                                       declares_class_or_enum,
14873                                       &function_definition_p);
14874
14875   pop_deferring_access_checks ();
14876
14877   /* Clear any current qualification; whatever comes next is the start
14878      of something new.  */
14879   parser->scope = NULL_TREE;
14880   parser->qualifying_scope = NULL_TREE;
14881   parser->object_scope = NULL_TREE;
14882   /* Look for a trailing `;' after the declaration.  */
14883   if (!function_definition_p
14884       && !cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
14885     cp_parser_skip_to_end_of_block_or_statement (parser);
14886
14887   return decl;
14888 }
14889
14890 /* Parse a cast-expression that is not the operand of a unary "&".  */
14891
14892 static tree
14893 cp_parser_simple_cast_expression (cp_parser *parser)
14894 {
14895   return cp_parser_cast_expression (parser, /*address_p=*/false);
14896 }
14897
14898 /* Parse a functional cast to TYPE.  Returns an expression
14899    representing the cast.  */
14900
14901 static tree
14902 cp_parser_functional_cast (cp_parser* parser, tree type)
14903 {
14904   tree expression_list;
14905   tree cast;
14906
14907   expression_list
14908     = cp_parser_parenthesized_expression_list (parser, false,
14909                                                /*non_constant_p=*/NULL);
14910
14911   cast = build_functional_cast (type, expression_list);
14912   /* [expr.const]/1: In an integral constant expression "only type
14913      conversions to integral or enumeration type can be used".  */
14914   if (cast != error_mark_node && !type_dependent_expression_p (type)
14915       && !INTEGRAL_OR_ENUMERATION_TYPE_P (TREE_TYPE (type)))
14916     {
14917       if (cp_parser_non_integral_constant_expression
14918           (parser, "a call to a constructor"))
14919         return error_mark_node;
14920     }
14921   return cast;
14922 }
14923
14924 /* Save the tokens that make up the body of a member function defined
14925    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
14926    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
14927    specifiers applied to the declaration.  Returns the FUNCTION_DECL
14928    for the member function.  */
14929
14930 static tree
14931 cp_parser_save_member_function_body (cp_parser* parser,
14932                                      cp_decl_specifier_seq *decl_specifiers,
14933                                      cp_declarator *declarator,
14934                                      tree attributes)
14935 {
14936   cp_token *first;
14937   cp_token *last;
14938   tree fn;
14939
14940   /* Create the function-declaration.  */
14941   fn = start_method (decl_specifiers, declarator, attributes);
14942   /* If something went badly wrong, bail out now.  */
14943   if (fn == error_mark_node)
14944     {
14945       /* If there's a function-body, skip it.  */
14946       if (cp_parser_token_starts_function_definition_p
14947           (cp_lexer_peek_token (parser->lexer)))
14948         cp_parser_skip_to_end_of_block_or_statement (parser);
14949       return error_mark_node;
14950     }
14951
14952   /* Remember it, if there default args to post process.  */
14953   cp_parser_save_default_args (parser, fn);
14954
14955   /* Save away the tokens that make up the body of the
14956      function.  */
14957   first = parser->lexer->next_token;
14958   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14959   /* Handle function try blocks.  */
14960   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
14961     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
14962   last = parser->lexer->next_token;
14963
14964   /* Save away the inline definition; we will process it when the
14965      class is complete.  */
14966   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
14967   DECL_PENDING_INLINE_P (fn) = 1;
14968
14969   /* We need to know that this was defined in the class, so that
14970      friend templates are handled correctly.  */
14971   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
14972
14973   /* We're done with the inline definition.  */
14974   finish_method (fn);
14975
14976   /* Add FN to the queue of functions to be parsed later.  */
14977   TREE_VALUE (parser->unparsed_functions_queues)
14978     = tree_cons (NULL_TREE, fn,
14979                  TREE_VALUE (parser->unparsed_functions_queues));
14980
14981   return fn;
14982 }
14983
14984 /* Parse a template-argument-list, as well as the trailing ">" (but
14985    not the opening ">").  See cp_parser_template_argument_list for the
14986    return value.  */
14987
14988 static tree
14989 cp_parser_enclosed_template_argument_list (cp_parser* parser)
14990 {
14991   tree arguments;
14992   tree saved_scope;
14993   tree saved_qualifying_scope;
14994   tree saved_object_scope;
14995   bool saved_greater_than_is_operator_p;
14996
14997   /* [temp.names]
14998
14999      When parsing a template-id, the first non-nested `>' is taken as
15000      the end of the template-argument-list rather than a greater-than
15001      operator.  */
15002   saved_greater_than_is_operator_p
15003     = parser->greater_than_is_operator_p;
15004   parser->greater_than_is_operator_p = false;
15005   /* Parsing the argument list may modify SCOPE, so we save it
15006      here.  */
15007   saved_scope = parser->scope;
15008   saved_qualifying_scope = parser->qualifying_scope;
15009   saved_object_scope = parser->object_scope;
15010   /* Parse the template-argument-list itself.  */
15011   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15012     arguments = NULL_TREE;
15013   else
15014     arguments = cp_parser_template_argument_list (parser);
15015   /* Look for the `>' that ends the template-argument-list. If we find
15016      a '>>' instead, it's probably just a typo.  */
15017   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
15018     {
15019       if (!saved_greater_than_is_operator_p)
15020         {
15021           /* If we're in a nested template argument list, the '>>' has
15022             to be a typo for '> >'. We emit the error message, but we
15023             continue parsing and we push a '>' as next token, so that
15024             the argument list will be parsed correctly.  Note that the
15025             global source location is still on the token before the
15026             '>>', so we need to say explicitly where we want it.  */
15027           cp_token *token = cp_lexer_peek_token (parser->lexer);
15028           error ("%H%<>>%> should be %<> >%> "
15029                  "within a nested template argument list",
15030                  &token->location);
15031
15032           /* ??? Proper recovery should terminate two levels of
15033              template argument list here.  */
15034           token->type = CPP_GREATER;
15035         }
15036       else
15037         {
15038           /* If this is not a nested template argument list, the '>>'
15039             is a typo for '>'. Emit an error message and continue.
15040             Same deal about the token location, but here we can get it
15041             right by consuming the '>>' before issuing the diagnostic.  */
15042           cp_lexer_consume_token (parser->lexer);
15043           error ("spurious %<>>%>, use %<>%> to terminate "
15044                  "a template argument list");
15045         }
15046     }
15047   else if (!cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
15048     error ("missing %<>%> to terminate the template argument list");
15049   else
15050     /* It's what we want, a '>'; consume it.  */
15051     cp_lexer_consume_token (parser->lexer);
15052   /* The `>' token might be a greater-than operator again now.  */
15053   parser->greater_than_is_operator_p
15054     = saved_greater_than_is_operator_p;
15055   /* Restore the SAVED_SCOPE.  */
15056   parser->scope = saved_scope;
15057   parser->qualifying_scope = saved_qualifying_scope;
15058   parser->object_scope = saved_object_scope;
15059
15060   return arguments;
15061 }
15062
15063 /* MEMBER_FUNCTION is a member function, or a friend.  If default
15064    arguments, or the body of the function have not yet been parsed,
15065    parse them now.  */
15066
15067 static void
15068 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
15069 {
15070   /* If this member is a template, get the underlying
15071      FUNCTION_DECL.  */
15072   if (DECL_FUNCTION_TEMPLATE_P (member_function))
15073     member_function = DECL_TEMPLATE_RESULT (member_function);
15074
15075   /* There should not be any class definitions in progress at this
15076      point; the bodies of members are only parsed outside of all class
15077      definitions.  */
15078   gcc_assert (parser->num_classes_being_defined == 0);
15079   /* While we're parsing the member functions we might encounter more
15080      classes.  We want to handle them right away, but we don't want
15081      them getting mixed up with functions that are currently in the
15082      queue.  */
15083   parser->unparsed_functions_queues
15084     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15085
15086   /* Make sure that any template parameters are in scope.  */
15087   maybe_begin_member_template_processing (member_function);
15088
15089   /* If the body of the function has not yet been parsed, parse it
15090      now.  */
15091   if (DECL_PENDING_INLINE_P (member_function))
15092     {
15093       tree function_scope;
15094       cp_token_cache *tokens;
15095
15096       /* The function is no longer pending; we are processing it.  */
15097       tokens = DECL_PENDING_INLINE_INFO (member_function);
15098       DECL_PENDING_INLINE_INFO (member_function) = NULL;
15099       DECL_PENDING_INLINE_P (member_function) = 0;
15100       /* If this was an inline function in a local class, enter the scope
15101          of the containing function.  */
15102       function_scope = decl_function_context (member_function);
15103       if (function_scope)
15104         push_function_context_to (function_scope);
15105
15106       /* Push the body of the function onto the lexer stack.  */
15107       cp_parser_push_lexer_for_tokens (parser, tokens);
15108
15109       /* Let the front end know that we going to be defining this
15110          function.  */
15111       start_preparsed_function (member_function, NULL_TREE,
15112                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
15113
15114       /* Now, parse the body of the function.  */
15115       cp_parser_function_definition_after_declarator (parser,
15116                                                       /*inline_p=*/true);
15117
15118       /* Leave the scope of the containing function.  */
15119       if (function_scope)
15120         pop_function_context_from (function_scope);
15121       cp_parser_pop_lexer (parser);
15122     }
15123
15124   /* Remove any template parameters from the symbol table.  */
15125   maybe_end_member_template_processing ();
15126
15127   /* Restore the queue.  */
15128   parser->unparsed_functions_queues
15129     = TREE_CHAIN (parser->unparsed_functions_queues);
15130 }
15131
15132 /* If DECL contains any default args, remember it on the unparsed
15133    functions queue.  */
15134
15135 static void
15136 cp_parser_save_default_args (cp_parser* parser, tree decl)
15137 {
15138   tree probe;
15139
15140   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
15141        probe;
15142        probe = TREE_CHAIN (probe))
15143     if (TREE_PURPOSE (probe))
15144       {
15145         TREE_PURPOSE (parser->unparsed_functions_queues)
15146           = tree_cons (current_class_type, decl,
15147                        TREE_PURPOSE (parser->unparsed_functions_queues));
15148         break;
15149       }
15150   return;
15151 }
15152
15153 /* FN is a FUNCTION_DECL which may contains a parameter with an
15154    unparsed DEFAULT_ARG.  Parse the default args now.  This function
15155    assumes that the current scope is the scope in which the default
15156    argument should be processed.  */
15157
15158 static void
15159 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
15160 {
15161   bool saved_local_variables_forbidden_p;
15162   tree parm;
15163
15164   /* While we're parsing the default args, we might (due to the
15165      statement expression extension) encounter more classes.  We want
15166      to handle them right away, but we don't want them getting mixed
15167      up with default args that are currently in the queue.  */
15168   parser->unparsed_functions_queues
15169     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
15170
15171   /* Local variable names (and the `this' keyword) may not appear
15172      in a default argument.  */
15173   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15174   parser->local_variables_forbidden_p = true;
15175
15176   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn));
15177        parm;
15178        parm = TREE_CHAIN (parm))
15179     {
15180       cp_token_cache *tokens;
15181
15182       if (!TREE_PURPOSE (parm)
15183           || TREE_CODE (TREE_PURPOSE (parm)) != DEFAULT_ARG)
15184         continue;
15185
15186        /* Push the saved tokens for the default argument onto the parser's
15187           lexer stack.  */
15188       tokens = DEFARG_TOKENS (TREE_PURPOSE (parm));
15189       cp_parser_push_lexer_for_tokens (parser, tokens);
15190
15191       /* Parse the assignment-expression.  */
15192       TREE_PURPOSE (parm) = cp_parser_assignment_expression (parser);
15193
15194       /* If the token stream has not been completely used up, then
15195          there was extra junk after the end of the default
15196          argument.  */
15197       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15198         cp_parser_error (parser, "expected %<,%>");
15199
15200       /* Revert to the main lexer.  */
15201       cp_parser_pop_lexer (parser);
15202     }
15203
15204   /* Restore the state of local_variables_forbidden_p.  */
15205   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15206
15207   /* Restore the queue.  */
15208   parser->unparsed_functions_queues
15209     = TREE_CHAIN (parser->unparsed_functions_queues);
15210 }
15211
15212 /* Parse the operand of `sizeof' (or a similar operator).  Returns
15213    either a TYPE or an expression, depending on the form of the
15214    input.  The KEYWORD indicates which kind of expression we have
15215    encountered.  */
15216
15217 static tree
15218 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
15219 {
15220   static const char *format;
15221   tree expr = NULL_TREE;
15222   const char *saved_message;
15223   bool saved_integral_constant_expression_p;
15224
15225   /* Initialize FORMAT the first time we get here.  */
15226   if (!format)
15227     format = "types may not be defined in `%s' expressions";
15228
15229   /* Types cannot be defined in a `sizeof' expression.  Save away the
15230      old message.  */
15231   saved_message = parser->type_definition_forbidden_message;
15232   /* And create the new one.  */
15233   parser->type_definition_forbidden_message
15234     = xmalloc (strlen (format)
15235                + strlen (IDENTIFIER_POINTER (ridpointers[keyword]))
15236                + 1 /* `\0' */);
15237   sprintf ((char *) parser->type_definition_forbidden_message,
15238            format, IDENTIFIER_POINTER (ridpointers[keyword]));
15239
15240   /* The restrictions on constant-expressions do not apply inside
15241      sizeof expressions.  */
15242   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
15243   parser->integral_constant_expression_p = false;
15244
15245   /* Do not actually evaluate the expression.  */
15246   ++skip_evaluation;
15247   /* If it's a `(', then we might be looking at the type-id
15248      construction.  */
15249   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
15250     {
15251       tree type;
15252       bool saved_in_type_id_in_expr_p;
15253
15254       /* We can't be sure yet whether we're looking at a type-id or an
15255          expression.  */
15256       cp_parser_parse_tentatively (parser);
15257       /* Consume the `('.  */
15258       cp_lexer_consume_token (parser->lexer);
15259       /* Parse the type-id.  */
15260       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15261       parser->in_type_id_in_expr_p = true;
15262       type = cp_parser_type_id (parser);
15263       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15264       /* Now, look for the trailing `)'.  */
15265       cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
15266       /* If all went well, then we're done.  */
15267       if (cp_parser_parse_definitely (parser))
15268         {
15269           cp_decl_specifier_seq decl_specs;
15270
15271           /* Build a trivial decl-specifier-seq.  */
15272           clear_decl_specs (&decl_specs);
15273           decl_specs.type = type;
15274
15275           /* Call grokdeclarator to figure out what type this is.  */
15276           expr = grokdeclarator (NULL,
15277                                  &decl_specs,
15278                                  TYPENAME,
15279                                  /*initialized=*/0,
15280                                  /*attrlist=*/NULL);
15281         }
15282     }
15283
15284   /* If the type-id production did not work out, then we must be
15285      looking at the unary-expression production.  */
15286   if (!expr)
15287     expr = cp_parser_unary_expression (parser, /*address_p=*/false);
15288   /* Go back to evaluating expressions.  */
15289   --skip_evaluation;
15290
15291   /* Free the message we created.  */
15292   free ((char *) parser->type_definition_forbidden_message);
15293   /* And restore the old one.  */
15294   parser->type_definition_forbidden_message = saved_message;
15295   parser->integral_constant_expression_p = saved_integral_constant_expression_p;
15296
15297   return expr;
15298 }
15299
15300 /* If the current declaration has no declarator, return true.  */
15301
15302 static bool
15303 cp_parser_declares_only_class_p (cp_parser *parser)
15304 {
15305   /* If the next token is a `;' or a `,' then there is no
15306      declarator.  */
15307   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15308           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
15309 }
15310
15311 /* Update the DECL_SPECS to reflect the STORAGE_CLASS.  */
15312
15313 static void
15314 cp_parser_set_storage_class (cp_decl_specifier_seq *decl_specs,
15315                              cp_storage_class storage_class)
15316 {
15317   if (decl_specs->storage_class != sc_none)
15318     decl_specs->multiple_storage_classes_p = true;
15319   else
15320     decl_specs->storage_class = storage_class;
15321 }
15322
15323 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
15324    is true, the type is a user-defined type; otherwise it is a
15325    built-in type specified by a keyword.  */
15326
15327 static void
15328 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
15329                               tree type_spec,
15330                               bool user_defined_p)
15331 {
15332   decl_specs->any_specifiers_p = true;
15333
15334   /* If the user tries to redeclare a built-in type (with, for example,
15335      in "typedef int wchar_t;") we remember that this is what
15336      happened.  In system headers, we ignore these declarations so
15337      that G++ can work with system headers that are not C++-safe.  */
15338   if (decl_specs->specs[(int) ds_typedef]
15339       && !user_defined_p
15340       && (decl_specs->type
15341           || decl_specs->specs[(int) ds_long]
15342           || decl_specs->specs[(int) ds_short]
15343           || decl_specs->specs[(int) ds_unsigned]
15344           || decl_specs->specs[(int) ds_signed]))
15345     {
15346       decl_specs->redefined_builtin_type = type_spec;
15347       if (!decl_specs->type)
15348         {
15349           decl_specs->type = type_spec;
15350           decl_specs->user_defined_type_p = false;
15351         }
15352     }
15353   else if (decl_specs->type)
15354     decl_specs->multiple_types_p = true;
15355   else
15356     {
15357       decl_specs->type = type_spec;
15358       decl_specs->user_defined_type_p = user_defined_p;
15359       decl_specs->redefined_builtin_type = NULL_TREE;
15360     }
15361 }
15362
15363 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
15364    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
15365
15366 static bool
15367 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
15368 {
15369   return decl_specifiers->specs[(int) ds_friend] != 0;
15370 }
15371
15372 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
15373    issue an error message indicating that TOKEN_DESC was expected.
15374
15375    Returns the token consumed, if the token had the appropriate type.
15376    Otherwise, returns NULL.  */
15377
15378 static cp_token *
15379 cp_parser_require (cp_parser* parser,
15380                    enum cpp_ttype type,
15381                    const char* token_desc)
15382 {
15383   if (cp_lexer_next_token_is (parser->lexer, type))
15384     return cp_lexer_consume_token (parser->lexer);
15385   else
15386     {
15387       /* Output the MESSAGE -- unless we're parsing tentatively.  */
15388       if (!cp_parser_simulate_error (parser))
15389         {
15390           char *message = concat ("expected ", token_desc, NULL);
15391           cp_parser_error (parser, message);
15392           free (message);
15393         }
15394       return NULL;
15395     }
15396 }
15397
15398 /* Like cp_parser_require, except that tokens will be skipped until
15399    the desired token is found.  An error message is still produced if
15400    the next token is not as expected.  */
15401
15402 static void
15403 cp_parser_skip_until_found (cp_parser* parser,
15404                             enum cpp_ttype type,
15405                             const char* token_desc)
15406 {
15407   cp_token *token;
15408   unsigned nesting_depth = 0;
15409
15410   if (cp_parser_require (parser, type, token_desc))
15411     return;
15412
15413   /* Skip tokens until the desired token is found.  */
15414   while (true)
15415     {
15416       /* Peek at the next token.  */
15417       token = cp_lexer_peek_token (parser->lexer);
15418       /* If we've reached the token we want, consume it and
15419          stop.  */
15420       if (token->type == type && !nesting_depth)
15421         {
15422           cp_lexer_consume_token (parser->lexer);
15423           return;
15424         }
15425       /* If we've run out of tokens, stop.  */
15426       if (token->type == CPP_EOF)
15427         return;
15428       if (token->type == CPP_OPEN_BRACE
15429           || token->type == CPP_OPEN_PAREN
15430           || token->type == CPP_OPEN_SQUARE)
15431         ++nesting_depth;
15432       else if (token->type == CPP_CLOSE_BRACE
15433                || token->type == CPP_CLOSE_PAREN
15434                || token->type == CPP_CLOSE_SQUARE)
15435         {
15436           if (nesting_depth-- == 0)
15437             return;
15438         }
15439       /* Consume this token.  */
15440       cp_lexer_consume_token (parser->lexer);
15441     }
15442 }
15443
15444 /* If the next token is the indicated keyword, consume it.  Otherwise,
15445    issue an error message indicating that TOKEN_DESC was expected.
15446
15447    Returns the token consumed, if the token had the appropriate type.
15448    Otherwise, returns NULL.  */
15449
15450 static cp_token *
15451 cp_parser_require_keyword (cp_parser* parser,
15452                            enum rid keyword,
15453                            const char* token_desc)
15454 {
15455   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
15456
15457   if (token && token->keyword != keyword)
15458     {
15459       dyn_string_t error_msg;
15460
15461       /* Format the error message.  */
15462       error_msg = dyn_string_new (0);
15463       dyn_string_append_cstr (error_msg, "expected ");
15464       dyn_string_append_cstr (error_msg, token_desc);
15465       cp_parser_error (parser, error_msg->s);
15466       dyn_string_delete (error_msg);
15467       return NULL;
15468     }
15469
15470   return token;
15471 }
15472
15473 /* Returns TRUE iff TOKEN is a token that can begin the body of a
15474    function-definition.  */
15475
15476 static bool
15477 cp_parser_token_starts_function_definition_p (cp_token* token)
15478 {
15479   return (/* An ordinary function-body begins with an `{'.  */
15480           token->type == CPP_OPEN_BRACE
15481           /* A ctor-initializer begins with a `:'.  */
15482           || token->type == CPP_COLON
15483           /* A function-try-block begins with `try'.  */
15484           || token->keyword == RID_TRY
15485           /* The named return value extension begins with `return'.  */
15486           || token->keyword == RID_RETURN);
15487 }
15488
15489 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
15490    definition.  */
15491
15492 static bool
15493 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
15494 {
15495   cp_token *token;
15496
15497   token = cp_lexer_peek_token (parser->lexer);
15498   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
15499 }
15500
15501 /* Returns TRUE iff the next token is the "," or ">" ending a
15502    template-argument. ">>" is also accepted (after the full
15503    argument was parsed) because it's probably a typo for "> >",
15504    and there is a specific diagnostic for this.  */
15505
15506 static bool
15507 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
15508 {
15509   cp_token *token;
15510
15511   token = cp_lexer_peek_token (parser->lexer);
15512   return (token->type == CPP_COMMA || token->type == CPP_GREATER
15513           || token->type == CPP_RSHIFT);
15514 }
15515
15516 /* Returns TRUE iff the n-th token is a ">", or the n-th is a "[" and the
15517    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
15518
15519 static bool
15520 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
15521                                                      size_t n)
15522 {
15523   cp_token *token;
15524
15525   token = cp_lexer_peek_nth_token (parser->lexer, n);
15526   if (token->type == CPP_LESS)
15527     return true;
15528   /* Check for the sequence `<::' in the original code. It would be lexed as
15529      `[:', where `[' is a digraph, and there is no whitespace before
15530      `:'.  */
15531   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
15532     {
15533       cp_token *token2;
15534       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
15535       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
15536         return true;
15537     }
15538   return false;
15539 }
15540
15541 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
15542    or none_type otherwise.  */
15543
15544 static enum tag_types
15545 cp_parser_token_is_class_key (cp_token* token)
15546 {
15547   switch (token->keyword)
15548     {
15549     case RID_CLASS:
15550       return class_type;
15551     case RID_STRUCT:
15552       return record_type;
15553     case RID_UNION:
15554       return union_type;
15555
15556     default:
15557       return none_type;
15558     }
15559 }
15560
15561 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
15562
15563 static void
15564 cp_parser_check_class_key (enum tag_types class_key, tree type)
15565 {
15566   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
15567     pedwarn ("%qs tag used in naming %q#T",
15568             class_key == union_type ? "union"
15569              : class_key == record_type ? "struct" : "class",
15570              type);
15571 }
15572
15573 /* Issue an error message if DECL is redeclared with different
15574    access than its original declaration [class.access.spec/3].
15575    This applies to nested classes and nested class templates.
15576    [class.mem/1].  */
15577
15578 static void
15579 cp_parser_check_access_in_redeclaration (tree decl)
15580 {
15581   if (!CLASS_TYPE_P (TREE_TYPE (decl)))
15582     return;
15583
15584   if ((TREE_PRIVATE (decl)
15585        != (current_access_specifier == access_private_node))
15586       || (TREE_PROTECTED (decl)
15587           != (current_access_specifier == access_protected_node)))
15588     error ("%qD redeclared with different access", decl);
15589 }
15590
15591 /* Look for the `template' keyword, as a syntactic disambiguator.
15592    Return TRUE iff it is present, in which case it will be
15593    consumed.  */
15594
15595 static bool
15596 cp_parser_optional_template_keyword (cp_parser *parser)
15597 {
15598   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
15599     {
15600       /* The `template' keyword can only be used within templates;
15601          outside templates the parser can always figure out what is a
15602          template and what is not.  */
15603       if (!processing_template_decl)
15604         {
15605           error ("%<template%> (as a disambiguator) is only allowed "
15606                  "within templates");
15607           /* If this part of the token stream is rescanned, the same
15608              error message would be generated.  So, we purge the token
15609              from the stream.  */
15610           cp_lexer_purge_token (parser->lexer);
15611           return false;
15612         }
15613       else
15614         {
15615           /* Consume the `template' keyword.  */
15616           cp_lexer_consume_token (parser->lexer);
15617           return true;
15618         }
15619     }
15620
15621   return false;
15622 }
15623
15624 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
15625    set PARSER->SCOPE, and perform other related actions.  */
15626
15627 static void
15628 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
15629 {
15630   tree value;
15631   tree check;
15632
15633   /* Get the stored value.  */
15634   value = cp_lexer_consume_token (parser->lexer)->value;
15635   /* Perform any access checks that were deferred.  */
15636   for (check = TREE_PURPOSE (value); check; check = TREE_CHAIN (check))
15637     perform_or_defer_access_check (TREE_PURPOSE (check), TREE_VALUE (check));
15638   /* Set the scope from the stored value.  */
15639   parser->scope = TREE_VALUE (value);
15640   parser->qualifying_scope = TREE_TYPE (value);
15641   parser->object_scope = NULL_TREE;
15642 }
15643
15644 /* Consume tokens up through a non-nested END token. */
15645
15646 static void
15647 cp_parser_cache_group (cp_parser *parser,
15648                        enum cpp_ttype end,
15649                        unsigned depth)
15650 {
15651   while (true)
15652     {
15653       cp_token *token;
15654
15655       /* Abort a parenthesized expression if we encounter a brace.  */
15656       if ((end == CPP_CLOSE_PAREN || depth == 0)
15657           && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
15658         return;
15659       /* If we've reached the end of the file, stop.  */
15660       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
15661         return;
15662       /* Consume the next token.  */
15663       token = cp_lexer_consume_token (parser->lexer);
15664       /* See if it starts a new group.  */
15665       if (token->type == CPP_OPEN_BRACE)
15666         {
15667           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
15668           if (depth == 0)
15669             return;
15670         }
15671       else if (token->type == CPP_OPEN_PAREN)
15672         cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
15673       else if (token->type == end)
15674         return;
15675     }
15676 }
15677
15678 /* Begin parsing tentatively.  We always save tokens while parsing
15679    tentatively so that if the tentative parsing fails we can restore the
15680    tokens.  */
15681
15682 static void
15683 cp_parser_parse_tentatively (cp_parser* parser)
15684 {
15685   /* Enter a new parsing context.  */
15686   parser->context = cp_parser_context_new (parser->context);
15687   /* Begin saving tokens.  */
15688   cp_lexer_save_tokens (parser->lexer);
15689   /* In order to avoid repetitive access control error messages,
15690      access checks are queued up until we are no longer parsing
15691      tentatively.  */
15692   push_deferring_access_checks (dk_deferred);
15693 }
15694
15695 /* Commit to the currently active tentative parse.  */
15696
15697 static void
15698 cp_parser_commit_to_tentative_parse (cp_parser* parser)
15699 {
15700   cp_parser_context *context;
15701   cp_lexer *lexer;
15702
15703   /* Mark all of the levels as committed.  */
15704   lexer = parser->lexer;
15705   for (context = parser->context; context->next; context = context->next)
15706     {
15707       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
15708         break;
15709       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
15710       while (!cp_lexer_saving_tokens (lexer))
15711         lexer = lexer->next;
15712       cp_lexer_commit_tokens (lexer);
15713     }
15714 }
15715
15716 /* Abort the currently active tentative parse.  All consumed tokens
15717    will be rolled back, and no diagnostics will be issued.  */
15718
15719 static void
15720 cp_parser_abort_tentative_parse (cp_parser* parser)
15721 {
15722   cp_parser_simulate_error (parser);
15723   /* Now, pretend that we want to see if the construct was
15724      successfully parsed.  */
15725   cp_parser_parse_definitely (parser);
15726 }
15727
15728 /* Stop parsing tentatively.  If a parse error has occurred, restore the
15729    token stream.  Otherwise, commit to the tokens we have consumed.
15730    Returns true if no error occurred; false otherwise.  */
15731
15732 static bool
15733 cp_parser_parse_definitely (cp_parser* parser)
15734 {
15735   bool error_occurred;
15736   cp_parser_context *context;
15737
15738   /* Remember whether or not an error occurred, since we are about to
15739      destroy that information.  */
15740   error_occurred = cp_parser_error_occurred (parser);
15741   /* Remove the topmost context from the stack.  */
15742   context = parser->context;
15743   parser->context = context->next;
15744   /* If no parse errors occurred, commit to the tentative parse.  */
15745   if (!error_occurred)
15746     {
15747       /* Commit to the tokens read tentatively, unless that was
15748          already done.  */
15749       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
15750         cp_lexer_commit_tokens (parser->lexer);
15751
15752       pop_to_parent_deferring_access_checks ();
15753     }
15754   /* Otherwise, if errors occurred, roll back our state so that things
15755      are just as they were before we began the tentative parse.  */
15756   else
15757     {
15758       cp_lexer_rollback_tokens (parser->lexer);
15759       pop_deferring_access_checks ();
15760     }
15761   /* Add the context to the front of the free list.  */
15762   context->next = cp_parser_context_free_list;
15763   cp_parser_context_free_list = context;
15764
15765   return !error_occurred;
15766 }
15767
15768 /* Returns true if we are parsing tentatively -- but have decided that
15769    we will stick with this tentative parse, even if errors occur.  */
15770
15771 static bool
15772 cp_parser_committed_to_tentative_parse (cp_parser* parser)
15773 {
15774   return (cp_parser_parsing_tentatively (parser)
15775           && parser->context->status == CP_PARSER_STATUS_KIND_COMMITTED);
15776 }
15777
15778 /* Returns nonzero iff an error has occurred during the most recent
15779    tentative parse.  */
15780
15781 static bool
15782 cp_parser_error_occurred (cp_parser* parser)
15783 {
15784   return (cp_parser_parsing_tentatively (parser)
15785           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
15786 }
15787
15788 /* Returns nonzero if GNU extensions are allowed.  */
15789
15790 static bool
15791 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
15792 {
15793   return parser->allow_gnu_extensions_p;
15794 }
15795
15796 \f
15797 /* The parser.  */
15798
15799 static GTY (()) cp_parser *the_parser;
15800
15801 /* External interface.  */
15802
15803 /* Parse one entire translation unit.  */
15804
15805 void
15806 c_parse_file (void)
15807 {
15808   bool error_occurred;
15809   static bool already_called = false;
15810
15811   if (already_called)
15812     {
15813       sorry ("inter-module optimizations not implemented for C++");
15814       return;
15815     }
15816   already_called = true;
15817
15818   the_parser = cp_parser_new ();
15819   push_deferring_access_checks (flag_access_control
15820                                 ? dk_no_deferred : dk_no_check);
15821   error_occurred = cp_parser_translation_unit (the_parser);
15822   the_parser = NULL;
15823 }
15824
15825 /* This variable must be provided by every front end.  */
15826
15827 int yydebug;
15828
15829 #include "gt-cp-parser.h"