OSDN Git Service

* class.c (layout_class_type): Do not issue C++ ABI warnings
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18    You should have received a copy of the GNU General Public License
19    along with GCC; see the file COPYING.  If not, write to the Free
20    Software Foundation, 59 Temple Place - Suite 330, Boston, MA
21    02111-1307, USA.  */
22
23 #include "config.h"
24 #include "system.h"
25 #include "coretypes.h"
26 #include "tm.h"
27 #include "dyn-string.h"
28 #include "varray.h"
29 #include "cpplib.h"
30 #include "tree.h"
31 #include "cp-tree.h"
32 #include "c-pragma.h"
33 #include "decl.h"
34 #include "flags.h"
35 #include "diagnostic.h"
36 #include "toplev.h"
37 #include "output.h"
38 #include "target.h"
39 #include "c-common.h"
40
41 \f
42 /* The lexer.  */
43
44 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
45    and c-lex.c) and the C++ parser.  */
46
47 /* A C++ token.  */
48
49 typedef struct cp_token GTY (())
50 {
51   /* The kind of token.  */
52   ENUM_BITFIELD (cpp_ttype) type : 8;
53   /* If this token is a keyword, this value indicates which keyword.
54      Otherwise, this value is RID_MAX.  */
55   ENUM_BITFIELD (rid) keyword : 8;
56   /* Token flags.  */
57   unsigned char flags;
58   /* True if this token is from a system header.  */
59   BOOL_BITFIELD in_system_header : 1;
60   /* True if this token is from a context where it is implicitly extern "C" */
61   BOOL_BITFIELD implicit_extern_c : 1;
62   /* The value associated with this token, if any.  */
63   tree value;
64   /* The location at which this token was found.  */
65   location_t location;
66 } cp_token;
67
68 /* We use a stack of token pointer for saving token sets.  */
69 typedef struct cp_token *cp_token_position;
70 DEF_VEC_P (cp_token_position);
71 DEF_VEC_ALLOC_P (cp_token_position,heap);
72
73 static const cp_token eof_token =
74 {
75   CPP_EOF, RID_MAX, 0, 0, 0, NULL_TREE,
76 #if USE_MAPPED_LOCATION
77   0
78 #else
79   {0, 0}
80 #endif
81 };
82
83 /* The cp_lexer structure represents the C++ lexer.  It is responsible
84    for managing the token stream from the preprocessor and supplying
85    it to the parser.  Tokens are never added to the cp_lexer after
86    it is created.  */
87
88 typedef struct cp_lexer GTY (())
89 {
90   /* The memory allocated for the buffer.  NULL if this lexer does not
91      own the token buffer.  */
92   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
93   /* If the lexer owns the buffer, this is the number of tokens in the
94      buffer.  */
95   size_t buffer_length;
96   
97   /* A pointer just past the last available token.  The tokens
98      in this lexer are [buffer, last_token).  */
99   cp_token_position GTY ((skip)) last_token;
100
101   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
102      no more available tokens.  */
103   cp_token_position GTY ((skip)) next_token;
104
105   /* A stack indicating positions at which cp_lexer_save_tokens was
106      called.  The top entry is the most recent position at which we
107      began saving tokens.  If the stack is non-empty, we are saving
108      tokens.  */
109   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
110
111   /* True if we should output debugging information.  */
112   bool debugging_p;
113
114   /* The next lexer in a linked list of lexers.  */
115   struct cp_lexer *next;
116 } cp_lexer;
117
118 /* cp_token_cache is a range of tokens.  There is no need to represent
119    allocate heap memory for it, since tokens are never removed from the
120    lexer's array.  There is also no need for the GC to walk through
121    a cp_token_cache, since everything in here is referenced through
122    a lexer.  */
123
124 typedef struct cp_token_cache GTY(())
125 {
126   /* The beginning of the token range.  */
127   cp_token * GTY((skip)) first;
128
129   /* Points immediately after the last token in the range.  */
130   cp_token * GTY ((skip)) last;
131 } cp_token_cache;
132
133 /* Prototypes.  */
134
135 static cp_lexer *cp_lexer_new_main
136   (void);
137 static cp_lexer *cp_lexer_new_from_tokens
138   (cp_token_cache *tokens);
139 static void cp_lexer_destroy
140   (cp_lexer *);
141 static int cp_lexer_saving_tokens
142   (const cp_lexer *);
143 static cp_token_position cp_lexer_token_position
144   (cp_lexer *, bool);
145 static cp_token *cp_lexer_token_at
146   (cp_lexer *, cp_token_position);
147 static void cp_lexer_get_preprocessor_token
148   (cp_lexer *, cp_token *);
149 static inline cp_token *cp_lexer_peek_token
150   (cp_lexer *);
151 static cp_token *cp_lexer_peek_nth_token
152   (cp_lexer *, size_t);
153 static inline bool cp_lexer_next_token_is
154   (cp_lexer *, enum cpp_ttype);
155 static bool cp_lexer_next_token_is_not
156   (cp_lexer *, enum cpp_ttype);
157 static bool cp_lexer_next_token_is_keyword
158   (cp_lexer *, enum rid);
159 static cp_token *cp_lexer_consume_token
160   (cp_lexer *);
161 static void cp_lexer_purge_token
162   (cp_lexer *);
163 static void cp_lexer_purge_tokens_after
164   (cp_lexer *, cp_token_position);
165 static void cp_lexer_handle_pragma
166   (cp_lexer *);
167 static void cp_lexer_save_tokens
168   (cp_lexer *);
169 static void cp_lexer_commit_tokens
170   (cp_lexer *);
171 static void cp_lexer_rollback_tokens
172   (cp_lexer *);
173 #ifdef ENABLE_CHECKING
174 static void cp_lexer_print_token
175   (FILE *, cp_token *);
176 static inline bool cp_lexer_debugging_p
177   (cp_lexer *);
178 static void cp_lexer_start_debugging
179   (cp_lexer *) ATTRIBUTE_UNUSED;
180 static void cp_lexer_stop_debugging
181   (cp_lexer *) ATTRIBUTE_UNUSED;
182 #else
183 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
184    about passing NULL to functions that require non-NULL arguments
185    (fputs, fprintf).  It will never be used, so all we need is a value
186    of the right type that's guaranteed not to be NULL.  */
187 #define cp_lexer_debug_stream stdout
188 #define cp_lexer_print_token(str, tok) (void) 0
189 #define cp_lexer_debugging_p(lexer) 0
190 #endif /* ENABLE_CHECKING */
191
192 static cp_token_cache *cp_token_cache_new
193   (cp_token *, cp_token *);
194
195 /* Manifest constants.  */
196 #define CP_LEXER_BUFFER_SIZE 10000
197 #define CP_SAVED_TOKEN_STACK 5
198
199 /* A token type for keywords, as opposed to ordinary identifiers.  */
200 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
201
202 /* A token type for template-ids.  If a template-id is processed while
203    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
204    the value of the CPP_TEMPLATE_ID is whatever was returned by
205    cp_parser_template_id.  */
206 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
207
208 /* A token type for nested-name-specifiers.  If a
209    nested-name-specifier is processed while parsing tentatively, it is
210    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
211    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
212    cp_parser_nested_name_specifier_opt.  */
213 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
214
215 /* A token type for tokens that are not tokens at all; these are used
216    to represent slots in the array where there used to be a token
217    that has now been deleted.  */
218 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
219
220 /* The number of token types, including C++-specific ones.  */
221 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
222
223 /* Variables.  */
224
225 #ifdef ENABLE_CHECKING
226 /* The stream to which debugging output should be written.  */
227 static FILE *cp_lexer_debug_stream;
228 #endif /* ENABLE_CHECKING */
229
230 /* Create a new main C++ lexer, the lexer that gets tokens from the
231    preprocessor.  */
232
233 static cp_lexer *
234 cp_lexer_new_main (void)
235 {
236   cp_token first_token;
237   cp_lexer *lexer;
238   cp_token *pos;
239   size_t alloc;
240   size_t space;
241   cp_token *buffer;
242
243   /* It's possible that lexing the first token will load a PCH file,
244      which is a GC collection point.  So we have to grab the first
245      token before allocating any memory.  Pragmas must not be deferred
246      as -fpch-preprocess can generate a pragma to load the PCH file in
247      the preprocessed output used by -save-temps.  */
248   cp_lexer_get_preprocessor_token (NULL, &first_token);
249
250   /* Tell cpplib we want CPP_PRAGMA tokens.  */
251   cpp_get_options (parse_in)->defer_pragmas = true;
252
253   /* Tell c_lex not to merge string constants.  */
254   c_lex_return_raw_strings = true;
255
256   c_common_no_more_pch ();
257
258   /* Allocate the memory.  */
259   lexer = GGC_CNEW (cp_lexer);
260
261 #ifdef ENABLE_CHECKING  
262   /* Initially we are not debugging.  */
263   lexer->debugging_p = false;
264 #endif /* ENABLE_CHECKING */
265   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
266                                    CP_SAVED_TOKEN_STACK);
267          
268   /* Create the buffer.  */
269   alloc = CP_LEXER_BUFFER_SIZE;
270   buffer = ggc_alloc (alloc * sizeof (cp_token));
271
272   /* Put the first token in the buffer.  */
273   space = alloc;
274   pos = buffer;
275   *pos = first_token;
276   
277   /* Get the remaining tokens from the preprocessor.  */
278   while (pos->type != CPP_EOF)
279     {
280       pos++;
281       if (!--space)
282         {
283           space = alloc;
284           alloc *= 2;
285           buffer = ggc_realloc (buffer, alloc * sizeof (cp_token));
286           pos = buffer + space;
287         }
288       cp_lexer_get_preprocessor_token (lexer, pos);
289     }
290   lexer->buffer = buffer;
291   lexer->buffer_length = alloc - space;
292   lexer->last_token = pos;
293   lexer->next_token = lexer->buffer_length ? buffer : (cp_token *)&eof_token;
294
295   /* Pragma processing (via cpp_handle_deferred_pragma) may result in
296      direct calls to c_lex.  Those callers all expect c_lex to do
297      string constant concatenation.  */
298   c_lex_return_raw_strings = false;
299
300   gcc_assert (lexer->next_token->type != CPP_PURGED);
301   return lexer;
302 }
303
304 /* Create a new lexer whose token stream is primed with the tokens in
305    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
306
307 static cp_lexer *
308 cp_lexer_new_from_tokens (cp_token_cache *cache)
309 {
310   cp_token *first = cache->first;
311   cp_token *last = cache->last;
312   cp_lexer *lexer = GGC_CNEW (cp_lexer);
313
314   /* We do not own the buffer.  */
315   lexer->buffer = NULL;
316   lexer->buffer_length = 0;
317   lexer->next_token = first == last ? (cp_token *)&eof_token : first;
318   lexer->last_token = last;
319   
320   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
321                                    CP_SAVED_TOKEN_STACK);
322
323 #ifdef ENABLE_CHECKING
324   /* Initially we are not debugging.  */
325   lexer->debugging_p = false;
326 #endif
327
328   gcc_assert (lexer->next_token->type != CPP_PURGED);
329   return lexer;
330 }
331
332 /* Frees all resources associated with LEXER.  */
333
334 static void
335 cp_lexer_destroy (cp_lexer *lexer)
336 {
337   if (lexer->buffer)
338     ggc_free (lexer->buffer);
339   VEC_free (cp_token_position, heap, lexer->saved_tokens);
340   ggc_free (lexer);
341 }
342
343 /* Returns nonzero if debugging information should be output.  */
344
345 #ifdef ENABLE_CHECKING
346
347 static inline bool
348 cp_lexer_debugging_p (cp_lexer *lexer)
349 {
350   return lexer->debugging_p;
351 }
352
353 #endif /* ENABLE_CHECKING */
354
355 static inline cp_token_position
356 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
357 {
358   gcc_assert (!previous_p || lexer->next_token != &eof_token);
359   
360   return lexer->next_token - previous_p;
361 }
362
363 static inline cp_token *
364 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
365 {
366   return pos;
367 }
368
369 /* nonzero if we are presently saving tokens.  */
370
371 static inline int
372 cp_lexer_saving_tokens (const cp_lexer* lexer)
373 {
374   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
375 }
376
377 /* Store the next token from the preprocessor in *TOKEN.  Return true
378    if we reach EOF.  */
379
380 static void
381 cp_lexer_get_preprocessor_token (cp_lexer *lexer ATTRIBUTE_UNUSED ,
382                                  cp_token *token)
383 {
384   static int is_extern_c = 0;
385
386    /* Get a new token from the preprocessor.  */
387   token->type
388     = c_lex_with_flags (&token->value, &token->location, &token->flags);
389   token->in_system_header = in_system_header;
390
391   /* On some systems, some header files are surrounded by an 
392      implicit extern "C" block.  Set a flag in the token if it
393      comes from such a header.  */
394   is_extern_c += pending_lang_change;
395   pending_lang_change = 0;
396   token->implicit_extern_c = is_extern_c > 0;
397
398   /* Check to see if this token is a keyword.  */
399   if (token->type == CPP_NAME
400       && C_IS_RESERVED_WORD (token->value))
401     {
402       /* Mark this token as a keyword.  */
403       token->type = CPP_KEYWORD;
404       /* Record which keyword.  */
405       token->keyword = C_RID_CODE (token->value);
406       /* Update the value.  Some keywords are mapped to particular
407          entities, rather than simply having the value of the
408          corresponding IDENTIFIER_NODE.  For example, `__const' is
409          mapped to `const'.  */
410       token->value = ridpointers[token->keyword];
411     }
412   /* Handle Objective-C++ keywords.  */
413   else if (token->type == CPP_AT_NAME)
414     {
415       token->type = CPP_KEYWORD;
416       switch (C_RID_CODE (token->value))
417         {
418         /* Map 'class' to '@class', 'private' to '@private', etc.  */
419         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
420         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
421         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
422         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
423         case RID_THROW: token->keyword = RID_AT_THROW; break;
424         case RID_TRY: token->keyword = RID_AT_TRY; break;
425         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
426         default: token->keyword = C_RID_CODE (token->value);
427         }
428     }
429   else
430     token->keyword = RID_MAX;
431 }
432
433 /* Update the globals input_location and in_system_header from TOKEN.  */
434 static inline void
435 cp_lexer_set_source_position_from_token (cp_token *token)
436 {
437   if (token->type != CPP_EOF)
438     {
439       input_location = token->location;
440       in_system_header = token->in_system_header;
441     }
442 }
443
444 /* Return a pointer to the next token in the token stream, but do not
445    consume it.  */
446
447 static inline cp_token *
448 cp_lexer_peek_token (cp_lexer *lexer)
449 {
450   if (cp_lexer_debugging_p (lexer))
451     {
452       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
453       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
454       putc ('\n', cp_lexer_debug_stream);
455     }
456   return lexer->next_token;
457 }
458
459 /* Return true if the next token has the indicated TYPE.  */
460
461 static inline bool
462 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
463 {
464   return cp_lexer_peek_token (lexer)->type == type;
465 }
466
467 /* Return true if the next token does not have the indicated TYPE.  */
468
469 static inline bool
470 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
471 {
472   return !cp_lexer_next_token_is (lexer, type);
473 }
474
475 /* Return true if the next token is the indicated KEYWORD.  */
476
477 static inline bool
478 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
479 {
480   cp_token *token;
481
482   /* Peek at the next token.  */
483   token = cp_lexer_peek_token (lexer);
484   /* Check to see if it is the indicated keyword.  */
485   return token->keyword == keyword;
486 }
487
488 /* Return a pointer to the Nth token in the token stream.  If N is 1,
489    then this is precisely equivalent to cp_lexer_peek_token (except
490    that it is not inline).  One would like to disallow that case, but
491    there is one case (cp_parser_nth_token_starts_template_id) where
492    the caller passes a variable for N and it might be 1.  */
493
494 static cp_token *
495 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
496 {
497   cp_token *token;
498
499   /* N is 1-based, not zero-based.  */
500   gcc_assert (n > 0 && lexer->next_token != &eof_token);
501
502   if (cp_lexer_debugging_p (lexer))
503     fprintf (cp_lexer_debug_stream,
504              "cp_lexer: peeking ahead %ld at token: ", (long)n);
505
506   --n;
507   token = lexer->next_token;
508   while (n != 0)
509     {
510       ++token;
511       if (token == lexer->last_token)
512         {
513           token = (cp_token *)&eof_token;
514           break;
515         }
516       
517       if (token->type != CPP_PURGED)
518         --n;
519     }
520
521   if (cp_lexer_debugging_p (lexer))
522     {
523       cp_lexer_print_token (cp_lexer_debug_stream, token);
524       putc ('\n', cp_lexer_debug_stream);
525     }
526
527   return token;
528 }
529
530 /* Return the next token, and advance the lexer's next_token pointer
531    to point to the next non-purged token.  */
532
533 static cp_token *
534 cp_lexer_consume_token (cp_lexer* lexer)
535 {
536   cp_token *token = lexer->next_token;
537
538   gcc_assert (token != &eof_token);
539   
540   do
541     {
542       lexer->next_token++;
543       if (lexer->next_token == lexer->last_token)
544         {
545           lexer->next_token = (cp_token *)&eof_token;
546           break;
547         }
548       
549     }
550   while (lexer->next_token->type == CPP_PURGED);
551   
552   cp_lexer_set_source_position_from_token (token);
553   
554   /* Provide debugging output.  */
555   if (cp_lexer_debugging_p (lexer))
556     {
557       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
558       cp_lexer_print_token (cp_lexer_debug_stream, token);
559       putc ('\n', cp_lexer_debug_stream);
560     }
561   
562   return token;
563 }
564
565 /* Permanently remove the next token from the token stream, and
566    advance the next_token pointer to refer to the next non-purged
567    token.  */
568
569 static void
570 cp_lexer_purge_token (cp_lexer *lexer)
571 {
572   cp_token *tok = lexer->next_token;
573   
574   gcc_assert (tok != &eof_token);
575   tok->type = CPP_PURGED;
576   tok->location = UNKNOWN_LOCATION;
577   tok->value = NULL_TREE;
578   tok->keyword = RID_MAX;
579
580   do
581     {
582       tok++;
583       if (tok == lexer->last_token)
584         {
585           tok = (cp_token *)&eof_token;
586           break;
587         }
588     }
589   while (tok->type == CPP_PURGED);
590   lexer->next_token = tok;
591 }
592
593 /* Permanently remove all tokens after TOK, up to, but not
594    including, the token that will be returned next by
595    cp_lexer_peek_token.  */
596
597 static void
598 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
599 {
600   cp_token *peek = lexer->next_token;
601
602   if (peek == &eof_token)
603     peek = lexer->last_token;
604   
605   gcc_assert (tok < peek);
606
607   for ( tok += 1; tok != peek; tok += 1)
608     {
609       tok->type = CPP_PURGED;
610       tok->location = UNKNOWN_LOCATION;
611       tok->value = NULL_TREE;
612       tok->keyword = RID_MAX;
613     }
614 }
615
616 /* Consume and handle a pragma token.  */
617 static void
618 cp_lexer_handle_pragma (cp_lexer *lexer)
619 {
620   cpp_string s;
621   cp_token *token = cp_lexer_consume_token (lexer);
622   gcc_assert (token->type == CPP_PRAGMA);
623   gcc_assert (token->value);
624
625   s.len = TREE_STRING_LENGTH (token->value);
626   s.text = (const unsigned char *) TREE_STRING_POINTER (token->value);
627
628   cpp_handle_deferred_pragma (parse_in, &s);
629
630   /* Clearing token->value here means that we will get an ICE if we
631      try to process this #pragma again (which should be impossible).  */
632   token->value = NULL;
633 }
634
635 /* Begin saving tokens.  All tokens consumed after this point will be
636    preserved.  */
637
638 static void
639 cp_lexer_save_tokens (cp_lexer* lexer)
640 {
641   /* Provide debugging output.  */
642   if (cp_lexer_debugging_p (lexer))
643     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
644
645   VEC_safe_push (cp_token_position, heap,
646                  lexer->saved_tokens, lexer->next_token);
647 }
648
649 /* Commit to the portion of the token stream most recently saved.  */
650
651 static void
652 cp_lexer_commit_tokens (cp_lexer* lexer)
653 {
654   /* Provide debugging output.  */
655   if (cp_lexer_debugging_p (lexer))
656     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
657
658   VEC_pop (cp_token_position, lexer->saved_tokens);
659 }
660
661 /* Return all tokens saved since the last call to cp_lexer_save_tokens
662    to the token stream.  Stop saving tokens.  */
663
664 static void
665 cp_lexer_rollback_tokens (cp_lexer* lexer)
666 {
667   /* Provide debugging output.  */
668   if (cp_lexer_debugging_p (lexer))
669     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
670
671   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
672 }
673
674 /* Print a representation of the TOKEN on the STREAM.  */
675
676 #ifdef ENABLE_CHECKING
677
678 static void
679 cp_lexer_print_token (FILE * stream, cp_token *token)
680 {
681   /* We don't use cpp_type2name here because the parser defines
682      a few tokens of its own.  */
683   static const char *const token_names[] = {
684     /* cpplib-defined token types */
685 #define OP(e, s) #e,
686 #define TK(e, s) #e,
687     TTYPE_TABLE
688 #undef OP
689 #undef TK
690     /* C++ parser token types - see "Manifest constants", above.  */
691     "KEYWORD",
692     "TEMPLATE_ID",
693     "NESTED_NAME_SPECIFIER",
694     "PURGED"
695   };
696   
697   /* If we have a name for the token, print it out.  Otherwise, we
698      simply give the numeric code.  */
699   gcc_assert (token->type < ARRAY_SIZE(token_names));
700   fputs (token_names[token->type], stream);
701
702   /* For some tokens, print the associated data.  */
703   switch (token->type)
704     {
705     case CPP_KEYWORD:
706       /* Some keywords have a value that is not an IDENTIFIER_NODE.
707          For example, `struct' is mapped to an INTEGER_CST.  */
708       if (TREE_CODE (token->value) != IDENTIFIER_NODE)
709         break;
710       /* else fall through */
711     case CPP_NAME:
712       fputs (IDENTIFIER_POINTER (token->value), stream);
713       break;
714
715     case CPP_STRING:
716     case CPP_WSTRING:
717     case CPP_PRAGMA:
718       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->value));
719       break;
720
721     default:
722       break;
723     }
724 }
725
726 /* Start emitting debugging information.  */
727
728 static void
729 cp_lexer_start_debugging (cp_lexer* lexer)
730 {
731   lexer->debugging_p = true;
732 }
733
734 /* Stop emitting debugging information.  */
735
736 static void
737 cp_lexer_stop_debugging (cp_lexer* lexer)
738 {
739   lexer->debugging_p = false;
740 }
741
742 #endif /* ENABLE_CHECKING */
743
744 /* Create a new cp_token_cache, representing a range of tokens.  */
745
746 static cp_token_cache *
747 cp_token_cache_new (cp_token *first, cp_token *last)
748 {
749   cp_token_cache *cache = GGC_NEW (cp_token_cache);
750   cache->first = first;
751   cache->last = last;
752   return cache;
753 }
754
755 \f
756 /* Decl-specifiers.  */
757
758 static void clear_decl_specs
759   (cp_decl_specifier_seq *);
760
761 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
762
763 static void
764 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
765 {
766   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
767 }
768
769 /* Declarators.  */
770
771 /* Nothing other than the parser should be creating declarators;
772    declarators are a semi-syntactic representation of C++ entities.
773    Other parts of the front end that need to create entities (like
774    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
775
776 static cp_declarator *make_call_declarator
777   (cp_declarator *, cp_parameter_declarator *, cp_cv_quals, tree);
778 static cp_declarator *make_array_declarator
779   (cp_declarator *, tree);
780 static cp_declarator *make_pointer_declarator
781   (cp_cv_quals, cp_declarator *);
782 static cp_declarator *make_reference_declarator
783   (cp_cv_quals, cp_declarator *);
784 static cp_parameter_declarator *make_parameter_declarator
785   (cp_decl_specifier_seq *, cp_declarator *, tree);
786 static cp_declarator *make_ptrmem_declarator
787   (cp_cv_quals, tree, cp_declarator *);
788
789 cp_declarator *cp_error_declarator;
790
791 /* The obstack on which declarators and related data structures are
792    allocated.  */
793 static struct obstack declarator_obstack;
794
795 /* Alloc BYTES from the declarator memory pool.  */
796
797 static inline void *
798 alloc_declarator (size_t bytes)
799 {
800   return obstack_alloc (&declarator_obstack, bytes);
801 }
802
803 /* Allocate a declarator of the indicated KIND.  Clear fields that are
804    common to all declarators.  */
805
806 static cp_declarator *
807 make_declarator (cp_declarator_kind kind)
808 {
809   cp_declarator *declarator;
810
811   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
812   declarator->kind = kind;
813   declarator->attributes = NULL_TREE;
814   declarator->declarator = NULL;
815
816   return declarator;
817 }
818
819 /* Make a declarator for a generalized identifier.  If non-NULL, the
820    identifier is QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is
821    just UNQUALIFIED_NAME.  */
822
823 static cp_declarator *
824 make_id_declarator (tree qualifying_scope, tree unqualified_name)
825 {
826   cp_declarator *declarator;
827
828   /* It is valid to write:
829
830        class C { void f(); };
831        typedef C D;
832        void D::f();
833
834      The standard is not clear about whether `typedef const C D' is
835      legal; as of 2002-09-15 the committee is considering that
836      question.  EDG 3.0 allows that syntax.  Therefore, we do as
837      well.  */
838   if (qualifying_scope && TYPE_P (qualifying_scope))
839     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
840
841   declarator = make_declarator (cdk_id);
842   declarator->u.id.qualifying_scope = qualifying_scope;
843   declarator->u.id.unqualified_name = unqualified_name;
844   declarator->u.id.sfk = sfk_none;
845
846   return declarator;
847 }
848
849 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
850    of modifiers such as const or volatile to apply to the pointer
851    type, represented as identifiers.  */
852
853 cp_declarator *
854 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
855 {
856   cp_declarator *declarator;
857
858   declarator = make_declarator (cdk_pointer);
859   declarator->declarator = target;
860   declarator->u.pointer.qualifiers = cv_qualifiers;
861   declarator->u.pointer.class_type = NULL_TREE;
862
863   return declarator;
864 }
865
866 /* Like make_pointer_declarator -- but for references.  */
867
868 cp_declarator *
869 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
870 {
871   cp_declarator *declarator;
872
873   declarator = make_declarator (cdk_reference);
874   declarator->declarator = target;
875   declarator->u.pointer.qualifiers = cv_qualifiers;
876   declarator->u.pointer.class_type = NULL_TREE;
877
878   return declarator;
879 }
880
881 /* Like make_pointer_declarator -- but for a pointer to a non-static
882    member of CLASS_TYPE.  */
883
884 cp_declarator *
885 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
886                         cp_declarator *pointee)
887 {
888   cp_declarator *declarator;
889
890   declarator = make_declarator (cdk_ptrmem);
891   declarator->declarator = pointee;
892   declarator->u.pointer.qualifiers = cv_qualifiers;
893   declarator->u.pointer.class_type = class_type;
894
895   return declarator;
896 }
897
898 /* Make a declarator for the function given by TARGET, with the
899    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
900    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
901    indicates what exceptions can be thrown.  */
902
903 cp_declarator *
904 make_call_declarator (cp_declarator *target,
905                       cp_parameter_declarator *parms,
906                       cp_cv_quals cv_qualifiers,
907                       tree exception_specification)
908 {
909   cp_declarator *declarator;
910
911   declarator = make_declarator (cdk_function);
912   declarator->declarator = target;
913   declarator->u.function.parameters = parms;
914   declarator->u.function.qualifiers = cv_qualifiers;
915   declarator->u.function.exception_specification = exception_specification;
916
917   return declarator;
918 }
919
920 /* Make a declarator for an array of BOUNDS elements, each of which is
921    defined by ELEMENT.  */
922
923 cp_declarator *
924 make_array_declarator (cp_declarator *element, tree bounds)
925 {
926   cp_declarator *declarator;
927
928   declarator = make_declarator (cdk_array);
929   declarator->declarator = element;
930   declarator->u.array.bounds = bounds;
931
932   return declarator;
933 }
934
935 cp_parameter_declarator *no_parameters;
936
937 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
938    DECLARATOR and DEFAULT_ARGUMENT.  */
939
940 cp_parameter_declarator *
941 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
942                            cp_declarator *declarator,
943                            tree default_argument)
944 {
945   cp_parameter_declarator *parameter;
946
947   parameter = ((cp_parameter_declarator *)
948                alloc_declarator (sizeof (cp_parameter_declarator)));
949   parameter->next = NULL;
950   if (decl_specifiers)
951     parameter->decl_specifiers = *decl_specifiers;
952   else
953     clear_decl_specs (&parameter->decl_specifiers);
954   parameter->declarator = declarator;
955   parameter->default_argument = default_argument;
956   parameter->ellipsis_p = false;
957
958   return parameter;
959 }
960
961 /* The parser.  */
962
963 /* Overview
964    --------
965
966    A cp_parser parses the token stream as specified by the C++
967    grammar.  Its job is purely parsing, not semantic analysis.  For
968    example, the parser breaks the token stream into declarators,
969    expressions, statements, and other similar syntactic constructs.
970    It does not check that the types of the expressions on either side
971    of an assignment-statement are compatible, or that a function is
972    not declared with a parameter of type `void'.
973
974    The parser invokes routines elsewhere in the compiler to perform
975    semantic analysis and to build up the abstract syntax tree for the
976    code processed.
977
978    The parser (and the template instantiation code, which is, in a
979    way, a close relative of parsing) are the only parts of the
980    compiler that should be calling push_scope and pop_scope, or
981    related functions.  The parser (and template instantiation code)
982    keeps track of what scope is presently active; everything else
983    should simply honor that.  (The code that generates static
984    initializers may also need to set the scope, in order to check
985    access control correctly when emitting the initializers.)
986
987    Methodology
988    -----------
989
990    The parser is of the standard recursive-descent variety.  Upcoming
991    tokens in the token stream are examined in order to determine which
992    production to use when parsing a non-terminal.  Some C++ constructs
993    require arbitrary look ahead to disambiguate.  For example, it is
994    impossible, in the general case, to tell whether a statement is an
995    expression or declaration without scanning the entire statement.
996    Therefore, the parser is capable of "parsing tentatively."  When the
997    parser is not sure what construct comes next, it enters this mode.
998    Then, while we attempt to parse the construct, the parser queues up
999    error messages, rather than issuing them immediately, and saves the
1000    tokens it consumes.  If the construct is parsed successfully, the
1001    parser "commits", i.e., it issues any queued error messages and
1002    the tokens that were being preserved are permanently discarded.
1003    If, however, the construct is not parsed successfully, the parser
1004    rolls back its state completely so that it can resume parsing using
1005    a different alternative.
1006
1007    Future Improvements
1008    -------------------
1009
1010    The performance of the parser could probably be improved substantially.
1011    We could often eliminate the need to parse tentatively by looking ahead
1012    a little bit.  In some places, this approach might not entirely eliminate
1013    the need to parse tentatively, but it might still speed up the average
1014    case.  */
1015
1016 /* Flags that are passed to some parsing functions.  These values can
1017    be bitwise-ored together.  */
1018
1019 typedef enum cp_parser_flags
1020 {
1021   /* No flags.  */
1022   CP_PARSER_FLAGS_NONE = 0x0,
1023   /* The construct is optional.  If it is not present, then no error
1024      should be issued.  */
1025   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1026   /* When parsing a type-specifier, do not allow user-defined types.  */
1027   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2
1028 } cp_parser_flags;
1029
1030 /* The different kinds of declarators we want to parse.  */
1031
1032 typedef enum cp_parser_declarator_kind
1033 {
1034   /* We want an abstract declarator.  */
1035   CP_PARSER_DECLARATOR_ABSTRACT,
1036   /* We want a named declarator.  */
1037   CP_PARSER_DECLARATOR_NAMED,
1038   /* We don't mind, but the name must be an unqualified-id.  */
1039   CP_PARSER_DECLARATOR_EITHER
1040 } cp_parser_declarator_kind;
1041
1042 /* The precedence values used to parse binary expressions.  The minimum value
1043    of PREC must be 1, because zero is reserved to quickly discriminate
1044    binary operators from other tokens.  */
1045
1046 enum cp_parser_prec
1047 {
1048   PREC_NOT_OPERATOR,
1049   PREC_LOGICAL_OR_EXPRESSION,
1050   PREC_LOGICAL_AND_EXPRESSION,
1051   PREC_INCLUSIVE_OR_EXPRESSION,
1052   PREC_EXCLUSIVE_OR_EXPRESSION,
1053   PREC_AND_EXPRESSION,
1054   PREC_EQUALITY_EXPRESSION,
1055   PREC_RELATIONAL_EXPRESSION,
1056   PREC_SHIFT_EXPRESSION,
1057   PREC_ADDITIVE_EXPRESSION,
1058   PREC_MULTIPLICATIVE_EXPRESSION,
1059   PREC_PM_EXPRESSION,
1060   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1061 };
1062
1063 /* A mapping from a token type to a corresponding tree node type, with a
1064    precedence value.  */
1065
1066 typedef struct cp_parser_binary_operations_map_node
1067 {
1068   /* The token type.  */
1069   enum cpp_ttype token_type;
1070   /* The corresponding tree code.  */
1071   enum tree_code tree_type;
1072   /* The precedence of this operator.  */
1073   enum cp_parser_prec prec;
1074 } cp_parser_binary_operations_map_node;
1075
1076 /* The status of a tentative parse.  */
1077
1078 typedef enum cp_parser_status_kind
1079 {
1080   /* No errors have occurred.  */
1081   CP_PARSER_STATUS_KIND_NO_ERROR,
1082   /* An error has occurred.  */
1083   CP_PARSER_STATUS_KIND_ERROR,
1084   /* We are committed to this tentative parse, whether or not an error
1085      has occurred.  */
1086   CP_PARSER_STATUS_KIND_COMMITTED
1087 } cp_parser_status_kind;
1088
1089 typedef struct cp_parser_expression_stack_entry
1090 {
1091   tree lhs;
1092   enum tree_code tree_type;
1093   int prec;
1094 } cp_parser_expression_stack_entry;
1095
1096 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1097    entries because precedence levels on the stack are monotonically
1098    increasing.  */
1099 typedef struct cp_parser_expression_stack_entry
1100   cp_parser_expression_stack[NUM_PREC_VALUES];
1101
1102 /* Context that is saved and restored when parsing tentatively.  */
1103 typedef struct cp_parser_context GTY (())
1104 {
1105   /* If this is a tentative parsing context, the status of the
1106      tentative parse.  */
1107   enum cp_parser_status_kind status;
1108   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1109      that are looked up in this context must be looked up both in the
1110      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1111      the context of the containing expression.  */
1112   tree object_type;
1113
1114   /* The next parsing context in the stack.  */
1115   struct cp_parser_context *next;
1116 } cp_parser_context;
1117
1118 /* Prototypes.  */
1119
1120 /* Constructors and destructors.  */
1121
1122 static cp_parser_context *cp_parser_context_new
1123   (cp_parser_context *);
1124
1125 /* Class variables.  */
1126
1127 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1128
1129 /* The operator-precedence table used by cp_parser_binary_expression.
1130    Transformed into an associative array (binops_by_token) by
1131    cp_parser_new.  */
1132
1133 static const cp_parser_binary_operations_map_node binops[] = {
1134   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1135   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1136
1137   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1138   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1139   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1140
1141   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1142   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1143
1144   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1145   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1146
1147   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1148   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1149   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1150   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1151   { CPP_MIN, MIN_EXPR, PREC_RELATIONAL_EXPRESSION },
1152   { CPP_MAX, MAX_EXPR, PREC_RELATIONAL_EXPRESSION },
1153
1154   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1155   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1156
1157   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1158
1159   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1160
1161   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1162
1163   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1164
1165   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1166 };
1167
1168 /* The same as binops, but initialized by cp_parser_new so that
1169    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1170    for speed.  */
1171 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1172
1173 /* Constructors and destructors.  */
1174
1175 /* Construct a new context.  The context below this one on the stack
1176    is given by NEXT.  */
1177
1178 static cp_parser_context *
1179 cp_parser_context_new (cp_parser_context* next)
1180 {
1181   cp_parser_context *context;
1182
1183   /* Allocate the storage.  */
1184   if (cp_parser_context_free_list != NULL)
1185     {
1186       /* Pull the first entry from the free list.  */
1187       context = cp_parser_context_free_list;
1188       cp_parser_context_free_list = context->next;
1189       memset (context, 0, sizeof (*context));
1190     }
1191   else
1192     context = GGC_CNEW (cp_parser_context);
1193
1194   /* No errors have occurred yet in this context.  */
1195   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1196   /* If this is not the bottomost context, copy information that we
1197      need from the previous context.  */
1198   if (next)
1199     {
1200       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1201          expression, then we are parsing one in this context, too.  */
1202       context->object_type = next->object_type;
1203       /* Thread the stack.  */
1204       context->next = next;
1205     }
1206
1207   return context;
1208 }
1209
1210 /* The cp_parser structure represents the C++ parser.  */
1211
1212 typedef struct cp_parser GTY(())
1213 {
1214   /* The lexer from which we are obtaining tokens.  */
1215   cp_lexer *lexer;
1216
1217   /* The scope in which names should be looked up.  If NULL_TREE, then
1218      we look up names in the scope that is currently open in the
1219      source program.  If non-NULL, this is either a TYPE or
1220      NAMESPACE_DECL for the scope in which we should look.
1221
1222      This value is not cleared automatically after a name is looked
1223      up, so we must be careful to clear it before starting a new look
1224      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1225      will look up `Z' in the scope of `X', rather than the current
1226      scope.)  Unfortunately, it is difficult to tell when name lookup
1227      is complete, because we sometimes peek at a token, look it up,
1228      and then decide not to consume it.  */
1229   tree scope;
1230
1231   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1232      last lookup took place.  OBJECT_SCOPE is used if an expression
1233      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1234      respectively.  QUALIFYING_SCOPE is used for an expression of the
1235      form "X::Y"; it refers to X.  */
1236   tree object_scope;
1237   tree qualifying_scope;
1238
1239   /* A stack of parsing contexts.  All but the bottom entry on the
1240      stack will be tentative contexts.
1241
1242      We parse tentatively in order to determine which construct is in
1243      use in some situations.  For example, in order to determine
1244      whether a statement is an expression-statement or a
1245      declaration-statement we parse it tentatively as a
1246      declaration-statement.  If that fails, we then reparse the same
1247      token stream as an expression-statement.  */
1248   cp_parser_context *context;
1249
1250   /* True if we are parsing GNU C++.  If this flag is not set, then
1251      GNU extensions are not recognized.  */
1252   bool allow_gnu_extensions_p;
1253
1254   /* TRUE if the `>' token should be interpreted as the greater-than
1255      operator.  FALSE if it is the end of a template-id or
1256      template-parameter-list.  */
1257   bool greater_than_is_operator_p;
1258
1259   /* TRUE if default arguments are allowed within a parameter list
1260      that starts at this point. FALSE if only a gnu extension makes
1261      them permissible.  */
1262   bool default_arg_ok_p;
1263
1264   /* TRUE if we are parsing an integral constant-expression.  See
1265      [expr.const] for a precise definition.  */
1266   bool integral_constant_expression_p;
1267
1268   /* TRUE if we are parsing an integral constant-expression -- but a
1269      non-constant expression should be permitted as well.  This flag
1270      is used when parsing an array bound so that GNU variable-length
1271      arrays are tolerated.  */
1272   bool allow_non_integral_constant_expression_p;
1273
1274   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1275      been seen that makes the expression non-constant.  */
1276   bool non_integral_constant_expression_p;
1277
1278   /* TRUE if local variable names and `this' are forbidden in the
1279      current context.  */
1280   bool local_variables_forbidden_p;
1281
1282   /* TRUE if the declaration we are parsing is part of a
1283      linkage-specification of the form `extern string-literal
1284      declaration'.  */
1285   bool in_unbraced_linkage_specification_p;
1286
1287   /* TRUE if we are presently parsing a declarator, after the
1288      direct-declarator.  */
1289   bool in_declarator_p;
1290
1291   /* TRUE if we are presently parsing a template-argument-list.  */
1292   bool in_template_argument_list_p;
1293
1294   /* TRUE if we are presently parsing the body of an
1295      iteration-statement.  */
1296   bool in_iteration_statement_p;
1297
1298   /* TRUE if we are presently parsing the body of a switch
1299      statement.  */
1300   bool in_switch_statement_p;
1301
1302   /* TRUE if we are parsing a type-id in an expression context.  In
1303      such a situation, both "type (expr)" and "type (type)" are valid
1304      alternatives.  */
1305   bool in_type_id_in_expr_p;
1306
1307   /* TRUE if we are currently in a header file where declarations are
1308      implicitly extern "C".  */
1309   bool implicit_extern_c;
1310
1311   /* TRUE if strings in expressions should be translated to the execution
1312      character set.  */
1313   bool translate_strings_p;
1314
1315   /* If non-NULL, then we are parsing a construct where new type
1316      definitions are not permitted.  The string stored here will be
1317      issued as an error message if a type is defined.  */
1318   const char *type_definition_forbidden_message;
1319
1320   /* A list of lists. The outer list is a stack, used for member
1321      functions of local classes. At each level there are two sub-list,
1322      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1323      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1324      TREE_VALUE's. The functions are chained in reverse declaration
1325      order.
1326
1327      The TREE_PURPOSE sublist contains those functions with default
1328      arguments that need post processing, and the TREE_VALUE sublist
1329      contains those functions with definitions that need post
1330      processing.
1331
1332      These lists can only be processed once the outermost class being
1333      defined is complete.  */
1334   tree unparsed_functions_queues;
1335
1336   /* The number of classes whose definitions are currently in
1337      progress.  */
1338   unsigned num_classes_being_defined;
1339
1340   /* The number of template parameter lists that apply directly to the
1341      current declaration.  */
1342   unsigned num_template_parameter_lists;
1343 } cp_parser;
1344
1345 /* The type of a function that parses some kind of expression.  */
1346 typedef tree (*cp_parser_expression_fn) (cp_parser *);
1347
1348 /* Prototypes.  */
1349
1350 /* Constructors and destructors.  */
1351
1352 static cp_parser *cp_parser_new
1353   (void);
1354
1355 /* Routines to parse various constructs.
1356
1357    Those that return `tree' will return the error_mark_node (rather
1358    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1359    Sometimes, they will return an ordinary node if error-recovery was
1360    attempted, even though a parse error occurred.  So, to check
1361    whether or not a parse error occurred, you should always use
1362    cp_parser_error_occurred.  If the construct is optional (indicated
1363    either by an `_opt' in the name of the function that does the
1364    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1365    the construct is not present.  */
1366
1367 /* Lexical conventions [gram.lex]  */
1368
1369 static tree cp_parser_identifier
1370   (cp_parser *);
1371 static tree cp_parser_string_literal
1372   (cp_parser *, bool, bool);
1373
1374 /* Basic concepts [gram.basic]  */
1375
1376 static bool cp_parser_translation_unit
1377   (cp_parser *);
1378
1379 /* Expressions [gram.expr]  */
1380
1381 static tree cp_parser_primary_expression
1382   (cp_parser *, bool, cp_id_kind *, tree *);
1383 static tree cp_parser_id_expression
1384   (cp_parser *, bool, bool, bool *, bool);
1385 static tree cp_parser_unqualified_id
1386   (cp_parser *, bool, bool, bool);
1387 static tree cp_parser_nested_name_specifier_opt
1388   (cp_parser *, bool, bool, bool, bool);
1389 static tree cp_parser_nested_name_specifier
1390   (cp_parser *, bool, bool, bool, bool);
1391 static tree cp_parser_class_or_namespace_name
1392   (cp_parser *, bool, bool, bool, bool, bool);
1393 static tree cp_parser_postfix_expression
1394   (cp_parser *, bool, bool);
1395 static tree cp_parser_postfix_open_square_expression
1396   (cp_parser *, tree, bool);
1397 static tree cp_parser_postfix_dot_deref_expression
1398   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *);
1399 static tree cp_parser_parenthesized_expression_list
1400   (cp_parser *, bool, bool, bool *);
1401 static void cp_parser_pseudo_destructor_name
1402   (cp_parser *, tree *, tree *);
1403 static tree cp_parser_unary_expression
1404   (cp_parser *, bool, bool);
1405 static enum tree_code cp_parser_unary_operator
1406   (cp_token *);
1407 static tree cp_parser_new_expression
1408   (cp_parser *);
1409 static tree cp_parser_new_placement
1410   (cp_parser *);
1411 static tree cp_parser_new_type_id
1412   (cp_parser *, tree *);
1413 static cp_declarator *cp_parser_new_declarator_opt
1414   (cp_parser *);
1415 static cp_declarator *cp_parser_direct_new_declarator
1416   (cp_parser *);
1417 static tree cp_parser_new_initializer
1418   (cp_parser *);
1419 static tree cp_parser_delete_expression
1420   (cp_parser *);
1421 static tree cp_parser_cast_expression
1422   (cp_parser *, bool, bool);
1423 static tree cp_parser_binary_expression
1424   (cp_parser *, bool);
1425 static tree cp_parser_question_colon_clause
1426   (cp_parser *, tree);
1427 static tree cp_parser_assignment_expression
1428   (cp_parser *, bool);
1429 static enum tree_code cp_parser_assignment_operator_opt
1430   (cp_parser *);
1431 static tree cp_parser_expression
1432   (cp_parser *, bool);
1433 static tree cp_parser_constant_expression
1434   (cp_parser *, bool, bool *);
1435 static tree cp_parser_builtin_offsetof
1436   (cp_parser *);
1437
1438 /* Statements [gram.stmt.stmt]  */
1439
1440 static void cp_parser_statement
1441   (cp_parser *, tree);
1442 static tree cp_parser_labeled_statement
1443   (cp_parser *, tree);
1444 static tree cp_parser_expression_statement
1445   (cp_parser *, tree);
1446 static tree cp_parser_compound_statement
1447   (cp_parser *, tree, bool);
1448 static void cp_parser_statement_seq_opt
1449   (cp_parser *, tree);
1450 static tree cp_parser_selection_statement
1451   (cp_parser *);
1452 static tree cp_parser_condition
1453   (cp_parser *);
1454 static tree cp_parser_iteration_statement
1455   (cp_parser *);
1456 static void cp_parser_for_init_statement
1457   (cp_parser *);
1458 static tree cp_parser_jump_statement
1459   (cp_parser *);
1460 static void cp_parser_declaration_statement
1461   (cp_parser *);
1462
1463 static tree cp_parser_implicitly_scoped_statement
1464   (cp_parser *);
1465 static void cp_parser_already_scoped_statement
1466   (cp_parser *);
1467
1468 /* Declarations [gram.dcl.dcl] */
1469
1470 static void cp_parser_declaration_seq_opt
1471   (cp_parser *);
1472 static void cp_parser_declaration
1473   (cp_parser *);
1474 static void cp_parser_block_declaration
1475   (cp_parser *, bool);
1476 static void cp_parser_simple_declaration
1477   (cp_parser *, bool);
1478 static void cp_parser_decl_specifier_seq
1479   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1480 static tree cp_parser_storage_class_specifier_opt
1481   (cp_parser *);
1482 static tree cp_parser_function_specifier_opt
1483   (cp_parser *, cp_decl_specifier_seq *);
1484 static tree cp_parser_type_specifier
1485   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1486    int *, bool *);
1487 static tree cp_parser_simple_type_specifier
1488   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1489 static tree cp_parser_type_name
1490   (cp_parser *);
1491 static tree cp_parser_elaborated_type_specifier
1492   (cp_parser *, bool, bool);
1493 static tree cp_parser_enum_specifier
1494   (cp_parser *);
1495 static void cp_parser_enumerator_list
1496   (cp_parser *, tree);
1497 static void cp_parser_enumerator_definition
1498   (cp_parser *, tree);
1499 static tree cp_parser_namespace_name
1500   (cp_parser *);
1501 static void cp_parser_namespace_definition
1502   (cp_parser *);
1503 static void cp_parser_namespace_body
1504   (cp_parser *);
1505 static tree cp_parser_qualified_namespace_specifier
1506   (cp_parser *);
1507 static void cp_parser_namespace_alias_definition
1508   (cp_parser *);
1509 static void cp_parser_using_declaration
1510   (cp_parser *);
1511 static void cp_parser_using_directive
1512   (cp_parser *);
1513 static void cp_parser_asm_definition
1514   (cp_parser *);
1515 static void cp_parser_linkage_specification
1516   (cp_parser *);
1517
1518 /* Declarators [gram.dcl.decl] */
1519
1520 static tree cp_parser_init_declarator
1521   (cp_parser *, cp_decl_specifier_seq *, bool, bool, int, bool *);
1522 static cp_declarator *cp_parser_declarator
1523   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1524 static cp_declarator *cp_parser_direct_declarator
1525   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1526 static enum tree_code cp_parser_ptr_operator
1527   (cp_parser *, tree *, cp_cv_quals *);
1528 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1529   (cp_parser *);
1530 static tree cp_parser_declarator_id
1531   (cp_parser *);
1532 static tree cp_parser_type_id
1533   (cp_parser *);
1534 static void cp_parser_type_specifier_seq
1535   (cp_parser *, bool, cp_decl_specifier_seq *);
1536 static cp_parameter_declarator *cp_parser_parameter_declaration_clause
1537   (cp_parser *);
1538 static cp_parameter_declarator *cp_parser_parameter_declaration_list
1539   (cp_parser *, bool *);
1540 static cp_parameter_declarator *cp_parser_parameter_declaration
1541   (cp_parser *, bool, bool *);
1542 static void cp_parser_function_body
1543   (cp_parser *);
1544 static tree cp_parser_initializer
1545   (cp_parser *, bool *, bool *);
1546 static tree cp_parser_initializer_clause
1547   (cp_parser *, bool *);
1548 static tree cp_parser_initializer_list
1549   (cp_parser *, bool *);
1550
1551 static bool cp_parser_ctor_initializer_opt_and_function_body
1552   (cp_parser *);
1553
1554 /* Classes [gram.class] */
1555
1556 static tree cp_parser_class_name
1557   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1558 static tree cp_parser_class_specifier
1559   (cp_parser *);
1560 static tree cp_parser_class_head
1561   (cp_parser *, bool *, tree *);
1562 static enum tag_types cp_parser_class_key
1563   (cp_parser *);
1564 static void cp_parser_member_specification_opt
1565   (cp_parser *);
1566 static void cp_parser_member_declaration
1567   (cp_parser *);
1568 static tree cp_parser_pure_specifier
1569   (cp_parser *);
1570 static tree cp_parser_constant_initializer
1571   (cp_parser *);
1572
1573 /* Derived classes [gram.class.derived] */
1574
1575 static tree cp_parser_base_clause
1576   (cp_parser *);
1577 static tree cp_parser_base_specifier
1578   (cp_parser *);
1579
1580 /* Special member functions [gram.special] */
1581
1582 static tree cp_parser_conversion_function_id
1583   (cp_parser *);
1584 static tree cp_parser_conversion_type_id
1585   (cp_parser *);
1586 static cp_declarator *cp_parser_conversion_declarator_opt
1587   (cp_parser *);
1588 static bool cp_parser_ctor_initializer_opt
1589   (cp_parser *);
1590 static void cp_parser_mem_initializer_list
1591   (cp_parser *);
1592 static tree cp_parser_mem_initializer
1593   (cp_parser *);
1594 static tree cp_parser_mem_initializer_id
1595   (cp_parser *);
1596
1597 /* Overloading [gram.over] */
1598
1599 static tree cp_parser_operator_function_id
1600   (cp_parser *);
1601 static tree cp_parser_operator
1602   (cp_parser *);
1603
1604 /* Templates [gram.temp] */
1605
1606 static void cp_parser_template_declaration
1607   (cp_parser *, bool);
1608 static tree cp_parser_template_parameter_list
1609   (cp_parser *);
1610 static tree cp_parser_template_parameter
1611   (cp_parser *, bool *);
1612 static tree cp_parser_type_parameter
1613   (cp_parser *);
1614 static tree cp_parser_template_id
1615   (cp_parser *, bool, bool, bool);
1616 static tree cp_parser_template_name
1617   (cp_parser *, bool, bool, bool, bool *);
1618 static tree cp_parser_template_argument_list
1619   (cp_parser *);
1620 static tree cp_parser_template_argument
1621   (cp_parser *);
1622 static void cp_parser_explicit_instantiation
1623   (cp_parser *);
1624 static void cp_parser_explicit_specialization
1625   (cp_parser *);
1626
1627 /* Exception handling [gram.exception] */
1628
1629 static tree cp_parser_try_block
1630   (cp_parser *);
1631 static bool cp_parser_function_try_block
1632   (cp_parser *);
1633 static void cp_parser_handler_seq
1634   (cp_parser *);
1635 static void cp_parser_handler
1636   (cp_parser *);
1637 static tree cp_parser_exception_declaration
1638   (cp_parser *);
1639 static tree cp_parser_throw_expression
1640   (cp_parser *);
1641 static tree cp_parser_exception_specification_opt
1642   (cp_parser *);
1643 static tree cp_parser_type_id_list
1644   (cp_parser *);
1645
1646 /* GNU Extensions */
1647
1648 static tree cp_parser_asm_specification_opt
1649   (cp_parser *);
1650 static tree cp_parser_asm_operand_list
1651   (cp_parser *);
1652 static tree cp_parser_asm_clobber_list
1653   (cp_parser *);
1654 static tree cp_parser_attributes_opt
1655   (cp_parser *);
1656 static tree cp_parser_attribute_list
1657   (cp_parser *);
1658 static bool cp_parser_extension_opt
1659   (cp_parser *, int *);
1660 static void cp_parser_label_declaration
1661   (cp_parser *);
1662
1663 /* Objective-C++ Productions */
1664
1665 static tree cp_parser_objc_message_receiver
1666   (cp_parser *);
1667 static tree cp_parser_objc_message_args
1668   (cp_parser *);
1669 static tree cp_parser_objc_message_expression
1670   (cp_parser *);
1671 static tree cp_parser_objc_encode_expression
1672   (cp_parser *);
1673 static tree cp_parser_objc_defs_expression 
1674   (cp_parser *);
1675 static tree cp_parser_objc_protocol_expression
1676   (cp_parser *);
1677 static tree cp_parser_objc_selector_expression
1678   (cp_parser *);
1679 static tree cp_parser_objc_expression
1680   (cp_parser *);
1681 static bool cp_parser_objc_selector_p
1682   (enum cpp_ttype);
1683 static tree cp_parser_objc_selector
1684   (cp_parser *);
1685 static tree cp_parser_objc_protocol_refs_opt
1686   (cp_parser *);
1687 static void cp_parser_objc_declaration
1688   (cp_parser *);
1689 static tree cp_parser_objc_statement
1690   (cp_parser *);
1691
1692 /* Utility Routines */
1693
1694 static tree cp_parser_lookup_name
1695   (cp_parser *, tree, enum tag_types, bool, bool, bool, bool *);
1696 static tree cp_parser_lookup_name_simple
1697   (cp_parser *, tree);
1698 static tree cp_parser_maybe_treat_template_as_class
1699   (tree, bool);
1700 static bool cp_parser_check_declarator_template_parameters
1701   (cp_parser *, cp_declarator *);
1702 static bool cp_parser_check_template_parameters
1703   (cp_parser *, unsigned);
1704 static tree cp_parser_simple_cast_expression
1705   (cp_parser *);
1706 static tree cp_parser_global_scope_opt
1707   (cp_parser *, bool);
1708 static bool cp_parser_constructor_declarator_p
1709   (cp_parser *, bool);
1710 static tree cp_parser_function_definition_from_specifiers_and_declarator
1711   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1712 static tree cp_parser_function_definition_after_declarator
1713   (cp_parser *, bool);
1714 static void cp_parser_template_declaration_after_export
1715   (cp_parser *, bool);
1716 static tree cp_parser_single_declaration
1717   (cp_parser *, bool, bool *);
1718 static tree cp_parser_functional_cast
1719   (cp_parser *, tree);
1720 static tree cp_parser_save_member_function_body
1721   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1722 static tree cp_parser_enclosed_template_argument_list
1723   (cp_parser *);
1724 static void cp_parser_save_default_args
1725   (cp_parser *, tree);
1726 static void cp_parser_late_parsing_for_member
1727   (cp_parser *, tree);
1728 static void cp_parser_late_parsing_default_args
1729   (cp_parser *, tree);
1730 static tree cp_parser_sizeof_operand
1731   (cp_parser *, enum rid);
1732 static bool cp_parser_declares_only_class_p
1733   (cp_parser *);
1734 static void cp_parser_set_storage_class
1735   (cp_decl_specifier_seq *, cp_storage_class);
1736 static void cp_parser_set_decl_spec_type
1737   (cp_decl_specifier_seq *, tree, bool);
1738 static bool cp_parser_friend_p
1739   (const cp_decl_specifier_seq *);
1740 static cp_token *cp_parser_require
1741   (cp_parser *, enum cpp_ttype, const char *);
1742 static cp_token *cp_parser_require_keyword
1743   (cp_parser *, enum rid, const char *);
1744 static bool cp_parser_token_starts_function_definition_p
1745   (cp_token *);
1746 static bool cp_parser_next_token_starts_class_definition_p
1747   (cp_parser *);
1748 static bool cp_parser_next_token_ends_template_argument_p
1749   (cp_parser *);
1750 static bool cp_parser_nth_token_starts_template_argument_list_p
1751   (cp_parser *, size_t);
1752 static enum tag_types cp_parser_token_is_class_key
1753   (cp_token *);
1754 static void cp_parser_check_class_key
1755   (enum tag_types, tree type);
1756 static void cp_parser_check_access_in_redeclaration
1757   (tree type);
1758 static bool cp_parser_optional_template_keyword
1759   (cp_parser *);
1760 static void cp_parser_pre_parsed_nested_name_specifier
1761   (cp_parser *);
1762 static void cp_parser_cache_group
1763   (cp_parser *, enum cpp_ttype, unsigned);
1764 static void cp_parser_parse_tentatively
1765   (cp_parser *);
1766 static void cp_parser_commit_to_tentative_parse
1767   (cp_parser *);
1768 static void cp_parser_abort_tentative_parse
1769   (cp_parser *);
1770 static bool cp_parser_parse_definitely
1771   (cp_parser *);
1772 static inline bool cp_parser_parsing_tentatively
1773   (cp_parser *);
1774 static bool cp_parser_uncommitted_to_tentative_parse_p
1775   (cp_parser *);
1776 static void cp_parser_error
1777   (cp_parser *, const char *);
1778 static void cp_parser_name_lookup_error
1779   (cp_parser *, tree, tree, const char *);
1780 static bool cp_parser_simulate_error
1781   (cp_parser *);
1782 static void cp_parser_check_type_definition
1783   (cp_parser *);
1784 static void cp_parser_check_for_definition_in_return_type
1785   (cp_declarator *, tree);
1786 static void cp_parser_check_for_invalid_template_id
1787   (cp_parser *, tree);
1788 static bool cp_parser_non_integral_constant_expression
1789   (cp_parser *, const char *);
1790 static void cp_parser_diagnose_invalid_type_name
1791   (cp_parser *, tree, tree);
1792 static bool cp_parser_parse_and_diagnose_invalid_type_name
1793   (cp_parser *);
1794 static int cp_parser_skip_to_closing_parenthesis
1795   (cp_parser *, bool, bool, bool);
1796 static void cp_parser_skip_to_end_of_statement
1797   (cp_parser *);
1798 static void cp_parser_consume_semicolon_at_end_of_statement
1799   (cp_parser *);
1800 static void cp_parser_skip_to_end_of_block_or_statement
1801   (cp_parser *);
1802 static void cp_parser_skip_to_closing_brace
1803   (cp_parser *);
1804 static void cp_parser_skip_until_found
1805   (cp_parser *, enum cpp_ttype, const char *);
1806 static bool cp_parser_error_occurred
1807   (cp_parser *);
1808 static bool cp_parser_allow_gnu_extensions_p
1809   (cp_parser *);
1810 static bool cp_parser_is_string_literal
1811   (cp_token *);
1812 static bool cp_parser_is_keyword
1813   (cp_token *, enum rid);
1814 static tree cp_parser_make_typename_type
1815   (cp_parser *, tree, tree);
1816
1817 /* Returns nonzero if we are parsing tentatively.  */
1818
1819 static inline bool
1820 cp_parser_parsing_tentatively (cp_parser* parser)
1821 {
1822   return parser->context->next != NULL;
1823 }
1824
1825 /* Returns nonzero if TOKEN is a string literal.  */
1826
1827 static bool
1828 cp_parser_is_string_literal (cp_token* token)
1829 {
1830   return (token->type == CPP_STRING || token->type == CPP_WSTRING);
1831 }
1832
1833 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
1834
1835 static bool
1836 cp_parser_is_keyword (cp_token* token, enum rid keyword)
1837 {
1838   return token->keyword == keyword;
1839 }
1840
1841 /* A minimum or maximum operator has been seen.  As these are
1842    deprecated, issue a warning.  */
1843
1844 static inline void
1845 cp_parser_warn_min_max (void)
1846 {
1847   if (warn_deprecated && !in_system_header)
1848     warning (0, "minimum/maximum operators are deprecated");
1849 }
1850
1851 /* If not parsing tentatively, issue a diagnostic of the form
1852       FILE:LINE: MESSAGE before TOKEN
1853    where TOKEN is the next token in the input stream.  MESSAGE
1854    (specified by the caller) is usually of the form "expected
1855    OTHER-TOKEN".  */
1856
1857 static void
1858 cp_parser_error (cp_parser* parser, const char* message)
1859 {
1860   if (!cp_parser_simulate_error (parser))
1861     {
1862       cp_token *token = cp_lexer_peek_token (parser->lexer);
1863       /* This diagnostic makes more sense if it is tagged to the line
1864          of the token we just peeked at.  */
1865       cp_lexer_set_source_position_from_token (token);
1866       if (token->type == CPP_PRAGMA)
1867         {
1868           error ("%<#pragma%> is not allowed here"); 
1869           cp_lexer_purge_token (parser->lexer);
1870           return;
1871         }
1872       c_parse_error (message,
1873                      /* Because c_parser_error does not understand
1874                         CPP_KEYWORD, keywords are treated like
1875                         identifiers.  */
1876                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
1877                      token->value);
1878     }
1879 }
1880
1881 /* Issue an error about name-lookup failing.  NAME is the
1882    IDENTIFIER_NODE DECL is the result of
1883    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
1884    the thing that we hoped to find.  */
1885
1886 static void
1887 cp_parser_name_lookup_error (cp_parser* parser,
1888                              tree name,
1889                              tree decl,
1890                              const char* desired)
1891 {
1892   /* If name lookup completely failed, tell the user that NAME was not
1893      declared.  */
1894   if (decl == error_mark_node)
1895     {
1896       if (parser->scope && parser->scope != global_namespace)
1897         error ("%<%D::%D%> has not been declared",
1898                parser->scope, name);
1899       else if (parser->scope == global_namespace)
1900         error ("%<::%D%> has not been declared", name);
1901       else if (parser->object_scope 
1902                && !CLASS_TYPE_P (parser->object_scope))
1903         error ("request for member %qD in non-class type %qT",
1904                name, parser->object_scope);
1905       else if (parser->object_scope)
1906         error ("%<%T::%D%> has not been declared", 
1907                parser->object_scope, name);
1908       else
1909         error ("%qD has not been declared", name);
1910     }
1911   else if (parser->scope && parser->scope != global_namespace)
1912     error ("%<%D::%D%> %s", parser->scope, name, desired);
1913   else if (parser->scope == global_namespace)
1914     error ("%<::%D%> %s", name, desired);
1915   else
1916     error ("%qD %s", name, desired);
1917 }
1918
1919 /* If we are parsing tentatively, remember that an error has occurred
1920    during this tentative parse.  Returns true if the error was
1921    simulated; false if a message should be issued by the caller.  */
1922
1923 static bool
1924 cp_parser_simulate_error (cp_parser* parser)
1925 {
1926   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1927     {
1928       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
1929       return true;
1930     }
1931   return false;
1932 }
1933
1934 /* This function is called when a type is defined.  If type
1935    definitions are forbidden at this point, an error message is
1936    issued.  */
1937
1938 static void
1939 cp_parser_check_type_definition (cp_parser* parser)
1940 {
1941   /* If types are forbidden here, issue a message.  */
1942   if (parser->type_definition_forbidden_message)
1943     /* Use `%s' to print the string in case there are any escape
1944        characters in the message.  */
1945     error ("%s", parser->type_definition_forbidden_message);
1946 }
1947
1948 /* This function is called when the DECLARATOR is processed.  The TYPE
1949    was a type defined in the decl-specifiers.  If it is invalid to
1950    define a type in the decl-specifiers for DECLARATOR, an error is
1951    issued.  */
1952
1953 static void
1954 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
1955                                                tree type)
1956 {
1957   /* [dcl.fct] forbids type definitions in return types.
1958      Unfortunately, it's not easy to know whether or not we are
1959      processing a return type until after the fact.  */
1960   while (declarator
1961          && (declarator->kind == cdk_pointer
1962              || declarator->kind == cdk_reference
1963              || declarator->kind == cdk_ptrmem))
1964     declarator = declarator->declarator;
1965   if (declarator
1966       && declarator->kind == cdk_function)
1967     {
1968       error ("new types may not be defined in a return type");
1969       inform ("(perhaps a semicolon is missing after the definition of %qT)",
1970               type);
1971     }
1972 }
1973
1974 /* A type-specifier (TYPE) has been parsed which cannot be followed by
1975    "<" in any valid C++ program.  If the next token is indeed "<",
1976    issue a message warning the user about what appears to be an
1977    invalid attempt to form a template-id.  */
1978
1979 static void
1980 cp_parser_check_for_invalid_template_id (cp_parser* parser,
1981                                          tree type)
1982 {
1983   cp_token_position start = 0;
1984
1985   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
1986     {
1987       if (TYPE_P (type))
1988         error ("%qT is not a template", type);
1989       else if (TREE_CODE (type) == IDENTIFIER_NODE)
1990         error ("%qE is not a template", type);
1991       else
1992         error ("invalid template-id");
1993       /* Remember the location of the invalid "<".  */
1994       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
1995         start = cp_lexer_token_position (parser->lexer, true);
1996       /* Consume the "<".  */
1997       cp_lexer_consume_token (parser->lexer);
1998       /* Parse the template arguments.  */
1999       cp_parser_enclosed_template_argument_list (parser);
2000       /* Permanently remove the invalid template arguments so that
2001          this error message is not issued again.  */
2002       if (start)
2003         cp_lexer_purge_tokens_after (parser->lexer, start);
2004     }
2005 }
2006
2007 /* If parsing an integral constant-expression, issue an error message
2008    about the fact that THING appeared and return true.  Otherwise,
2009    return false.  In either case, set
2010    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */ 
2011
2012 static bool
2013 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2014                                             const char *thing)
2015 {
2016   parser->non_integral_constant_expression_p = true;
2017   if (parser->integral_constant_expression_p)
2018     {
2019       if (!parser->allow_non_integral_constant_expression_p)
2020         {
2021           error ("%s cannot appear in a constant-expression", thing);
2022           return true;
2023         }
2024     }
2025   return false;
2026 }
2027
2028 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2029    qualifying scope (or NULL, if none) for ID.  This function commits
2030    to the current active tentative parse, if any.  (Otherwise, the
2031    problematic construct might be encountered again later, resulting
2032    in duplicate error messages.)  */
2033
2034 static void
2035 cp_parser_diagnose_invalid_type_name (cp_parser *parser, tree scope, tree id)
2036 {
2037   tree decl, old_scope;
2038   /* Try to lookup the identifier.  */
2039   old_scope = parser->scope;
2040   parser->scope = scope;
2041   decl = cp_parser_lookup_name_simple (parser, id);
2042   parser->scope = old_scope;
2043   /* If the lookup found a template-name, it means that the user forgot
2044   to specify an argument list. Emit an useful error message.  */
2045   if (TREE_CODE (decl) == TEMPLATE_DECL)
2046     error ("invalid use of template-name %qE without an argument list",
2047       decl);
2048   else if (!parser->scope)
2049     {
2050       /* Issue an error message.  */
2051       error ("%qE does not name a type", id);
2052       /* If we're in a template class, it's possible that the user was
2053          referring to a type from a base class.  For example:
2054
2055            template <typename T> struct A { typedef T X; };
2056            template <typename T> struct B : public A<T> { X x; };
2057
2058          The user should have said "typename A<T>::X".  */
2059       if (processing_template_decl && current_class_type
2060           && TYPE_BINFO (current_class_type))
2061         {
2062           tree b;
2063
2064           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2065                b;
2066                b = TREE_CHAIN (b))
2067             {
2068               tree base_type = BINFO_TYPE (b);
2069               if (CLASS_TYPE_P (base_type)
2070                   && dependent_type_p (base_type))
2071                 {
2072                   tree field;
2073                   /* Go from a particular instantiation of the
2074                      template (which will have an empty TYPE_FIELDs),
2075                      to the main version.  */
2076                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2077                   for (field = TYPE_FIELDS (base_type);
2078                        field;
2079                        field = TREE_CHAIN (field))
2080                     if (TREE_CODE (field) == TYPE_DECL
2081                         && DECL_NAME (field) == id)
2082                       {
2083                         inform ("(perhaps %<typename %T::%E%> was intended)",
2084                                 BINFO_TYPE (b), id);
2085                         break;
2086                       }
2087                   if (field)
2088                     break;
2089                 }
2090             }
2091         }
2092     }
2093   /* Here we diagnose qualified-ids where the scope is actually correct,
2094      but the identifier does not resolve to a valid type name.  */
2095   else
2096     {
2097       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2098         error ("%qE in namespace %qE does not name a type",
2099                id, parser->scope);
2100       else if (TYPE_P (parser->scope))
2101         error ("%qE in class %qT does not name a type", id, parser->scope);
2102       else
2103         gcc_unreachable ();
2104     }
2105   cp_parser_commit_to_tentative_parse (parser);
2106 }
2107
2108 /* Check for a common situation where a type-name should be present,
2109    but is not, and issue a sensible error message.  Returns true if an
2110    invalid type-name was detected.
2111
2112    The situation handled by this function are variable declarations of the
2113    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2114    Usually, `ID' should name a type, but if we got here it means that it
2115    does not. We try to emit the best possible error message depending on
2116    how exactly the id-expression looks like.
2117 */
2118
2119 static bool
2120 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2121 {
2122   tree id;
2123
2124   cp_parser_parse_tentatively (parser);
2125   id = cp_parser_id_expression (parser,
2126                                 /*template_keyword_p=*/false,
2127                                 /*check_dependency_p=*/true,
2128                                 /*template_p=*/NULL,
2129                                 /*declarator_p=*/true);
2130   /* After the id-expression, there should be a plain identifier,
2131      otherwise this is not a simple variable declaration. Also, if
2132      the scope is dependent, we cannot do much.  */
2133   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2134       || (parser->scope && TYPE_P (parser->scope)
2135           && dependent_type_p (parser->scope)))
2136     {
2137       cp_parser_abort_tentative_parse (parser);
2138       return false;
2139     }
2140   if (!cp_parser_parse_definitely (parser)
2141       || TREE_CODE (id) != IDENTIFIER_NODE)
2142     return false;
2143
2144   /* Emit a diagnostic for the invalid type.  */
2145   cp_parser_diagnose_invalid_type_name (parser, parser->scope, id);
2146   /* Skip to the end of the declaration; there's no point in
2147      trying to process it.  */
2148   cp_parser_skip_to_end_of_block_or_statement (parser);
2149   return true;
2150 }
2151
2152 /* Consume tokens up to, and including, the next non-nested closing `)'.
2153    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2154    are doing error recovery. Returns -1 if OR_COMMA is true and we
2155    found an unnested comma.  */
2156
2157 static int
2158 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2159                                        bool recovering,
2160                                        bool or_comma,
2161                                        bool consume_paren)
2162 {
2163   unsigned paren_depth = 0;
2164   unsigned brace_depth = 0;
2165   int result;
2166
2167   if (recovering && !or_comma
2168       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2169     return 0;
2170
2171   while (true)
2172     {
2173       cp_token *token;
2174
2175       /* If we've run out of tokens, then there is no closing `)'.  */
2176       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2177         {
2178           result = 0;
2179           break;
2180         }
2181
2182       token = cp_lexer_peek_token (parser->lexer);
2183
2184       /* This matches the processing in skip_to_end_of_statement.  */
2185       if (token->type == CPP_SEMICOLON && !brace_depth)
2186         {
2187           result = 0;
2188           break;
2189         }
2190       if (token->type == CPP_OPEN_BRACE)
2191         ++brace_depth;
2192       if (token->type == CPP_CLOSE_BRACE)
2193         {
2194           if (!brace_depth--)
2195             {
2196               result = 0;
2197               break;
2198             }
2199         }
2200       if (recovering && or_comma && token->type == CPP_COMMA
2201           && !brace_depth && !paren_depth)
2202         {
2203           result = -1;
2204           break;
2205         }
2206
2207       if (!brace_depth)
2208         {
2209           /* If it is an `(', we have entered another level of nesting.  */
2210           if (token->type == CPP_OPEN_PAREN)
2211             ++paren_depth;
2212           /* If it is a `)', then we might be done.  */
2213           else if (token->type == CPP_CLOSE_PAREN && !paren_depth--)
2214             {
2215               if (consume_paren)
2216                 cp_lexer_consume_token (parser->lexer);
2217               {
2218                 result = 1;
2219                 break;
2220               }
2221             }
2222         }
2223
2224       /* Consume the token.  */
2225       cp_lexer_consume_token (parser->lexer);
2226     }
2227
2228   return result;
2229 }
2230
2231 /* Consume tokens until we reach the end of the current statement.
2232    Normally, that will be just before consuming a `;'.  However, if a
2233    non-nested `}' comes first, then we stop before consuming that.  */
2234
2235 static void
2236 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2237 {
2238   unsigned nesting_depth = 0;
2239
2240   while (true)
2241     {
2242       cp_token *token;
2243
2244       /* Peek at the next token.  */
2245       token = cp_lexer_peek_token (parser->lexer);
2246       /* If we've run out of tokens, stop.  */
2247       if (token->type == CPP_EOF)
2248         break;
2249       /* If the next token is a `;', we have reached the end of the
2250          statement.  */
2251       if (token->type == CPP_SEMICOLON && !nesting_depth)
2252         break;
2253       /* If the next token is a non-nested `}', then we have reached
2254          the end of the current block.  */
2255       if (token->type == CPP_CLOSE_BRACE)
2256         {
2257           /* If this is a non-nested `}', stop before consuming it.
2258              That way, when confronted with something like:
2259
2260                { 3 + }
2261
2262              we stop before consuming the closing `}', even though we
2263              have not yet reached a `;'.  */
2264           if (nesting_depth == 0)
2265             break;
2266           /* If it is the closing `}' for a block that we have
2267              scanned, stop -- but only after consuming the token.
2268              That way given:
2269
2270                 void f g () { ... }
2271                 typedef int I;
2272
2273              we will stop after the body of the erroneously declared
2274              function, but before consuming the following `typedef'
2275              declaration.  */
2276           if (--nesting_depth == 0)
2277             {
2278               cp_lexer_consume_token (parser->lexer);
2279               break;
2280             }
2281         }
2282       /* If it the next token is a `{', then we are entering a new
2283          block.  Consume the entire block.  */
2284       else if (token->type == CPP_OPEN_BRACE)
2285         ++nesting_depth;
2286       /* Consume the token.  */
2287       cp_lexer_consume_token (parser->lexer);
2288     }
2289 }
2290
2291 /* This function is called at the end of a statement or declaration.
2292    If the next token is a semicolon, it is consumed; otherwise, error
2293    recovery is attempted.  */
2294
2295 static void
2296 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2297 {
2298   /* Look for the trailing `;'.  */
2299   if (!cp_parser_require (parser, CPP_SEMICOLON, "`;'"))
2300     {
2301       /* If there is additional (erroneous) input, skip to the end of
2302          the statement.  */
2303       cp_parser_skip_to_end_of_statement (parser);
2304       /* If the next token is now a `;', consume it.  */
2305       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2306         cp_lexer_consume_token (parser->lexer);
2307     }
2308 }
2309
2310 /* Skip tokens until we have consumed an entire block, or until we
2311    have consumed a non-nested `;'.  */
2312
2313 static void
2314 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2315 {
2316   unsigned nesting_depth = 0;
2317
2318   while (true)
2319     {
2320       cp_token *token;
2321
2322       /* Peek at the next token.  */
2323       token = cp_lexer_peek_token (parser->lexer);
2324       /* If we've run out of tokens, stop.  */
2325       if (token->type == CPP_EOF)
2326         break;
2327       /* If the next token is a `;', we have reached the end of the
2328          statement.  */
2329       if (token->type == CPP_SEMICOLON && !nesting_depth)
2330         {
2331           /* Consume the `;'.  */
2332           cp_lexer_consume_token (parser->lexer);
2333           break;
2334         }
2335       /* Consume the token.  */
2336       token = cp_lexer_consume_token (parser->lexer);
2337       /* If the next token is a non-nested `}', then we have reached
2338          the end of the current block.  */
2339       if (token->type == CPP_CLOSE_BRACE
2340           && (nesting_depth == 0 || --nesting_depth == 0))
2341         break;
2342       /* If it the next token is a `{', then we are entering a new
2343          block.  Consume the entire block.  */
2344       if (token->type == CPP_OPEN_BRACE)
2345         ++nesting_depth;
2346     }
2347 }
2348
2349 /* Skip tokens until a non-nested closing curly brace is the next
2350    token.  */
2351
2352 static void
2353 cp_parser_skip_to_closing_brace (cp_parser *parser)
2354 {
2355   unsigned nesting_depth = 0;
2356
2357   while (true)
2358     {
2359       cp_token *token;
2360
2361       /* Peek at the next token.  */
2362       token = cp_lexer_peek_token (parser->lexer);
2363       /* If we've run out of tokens, stop.  */
2364       if (token->type == CPP_EOF)
2365         break;
2366       /* If the next token is a non-nested `}', then we have reached
2367          the end of the current block.  */
2368       if (token->type == CPP_CLOSE_BRACE && nesting_depth-- == 0)
2369         break;
2370       /* If it the next token is a `{', then we are entering a new
2371          block.  Consume the entire block.  */
2372       else if (token->type == CPP_OPEN_BRACE)
2373         ++nesting_depth;
2374       /* Consume the token.  */
2375       cp_lexer_consume_token (parser->lexer);
2376     }
2377 }
2378
2379 /* This is a simple wrapper around make_typename_type. When the id is
2380    an unresolved identifier node, we can provide a superior diagnostic
2381    using cp_parser_diagnose_invalid_type_name.  */
2382
2383 static tree
2384 cp_parser_make_typename_type (cp_parser *parser, tree scope, tree id)
2385 {
2386   tree result;
2387   if (TREE_CODE (id) == IDENTIFIER_NODE)
2388     {
2389       result = make_typename_type (scope, id, typename_type,
2390                                    /*complain=*/0);
2391       if (result == error_mark_node)
2392         cp_parser_diagnose_invalid_type_name (parser, scope, id);
2393       return result;
2394     }
2395   return make_typename_type (scope, id, typename_type, tf_error);
2396 }
2397
2398
2399 /* Create a new C++ parser.  */
2400
2401 static cp_parser *
2402 cp_parser_new (void)
2403 {
2404   cp_parser *parser;
2405   cp_lexer *lexer;
2406   unsigned i;
2407
2408   /* cp_lexer_new_main is called before calling ggc_alloc because
2409      cp_lexer_new_main might load a PCH file.  */
2410   lexer = cp_lexer_new_main ();
2411
2412   /* Initialize the binops_by_token so that we can get the tree
2413      directly from the token.  */
2414   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2415     binops_by_token[binops[i].token_type] = binops[i];
2416
2417   parser = GGC_CNEW (cp_parser);
2418   parser->lexer = lexer;
2419   parser->context = cp_parser_context_new (NULL);
2420
2421   /* For now, we always accept GNU extensions.  */
2422   parser->allow_gnu_extensions_p = 1;
2423
2424   /* The `>' token is a greater-than operator, not the end of a
2425      template-id.  */
2426   parser->greater_than_is_operator_p = true;
2427
2428   parser->default_arg_ok_p = true;
2429
2430   /* We are not parsing a constant-expression.  */
2431   parser->integral_constant_expression_p = false;
2432   parser->allow_non_integral_constant_expression_p = false;
2433   parser->non_integral_constant_expression_p = false;
2434
2435   /* Local variable names are not forbidden.  */
2436   parser->local_variables_forbidden_p = false;
2437
2438   /* We are not processing an `extern "C"' declaration.  */
2439   parser->in_unbraced_linkage_specification_p = false;
2440
2441   /* We are not processing a declarator.  */
2442   parser->in_declarator_p = false;
2443
2444   /* We are not processing a template-argument-list.  */
2445   parser->in_template_argument_list_p = false;
2446
2447   /* We are not in an iteration statement.  */
2448   parser->in_iteration_statement_p = false;
2449
2450   /* We are not in a switch statement.  */
2451   parser->in_switch_statement_p = false;
2452
2453   /* We are not parsing a type-id inside an expression.  */
2454   parser->in_type_id_in_expr_p = false;
2455
2456   /* Declarations aren't implicitly extern "C".  */
2457   parser->implicit_extern_c = false;
2458
2459   /* String literals should be translated to the execution character set.  */
2460   parser->translate_strings_p = true;
2461
2462   /* The unparsed function queue is empty.  */
2463   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2464
2465   /* There are no classes being defined.  */
2466   parser->num_classes_being_defined = 0;
2467
2468   /* No template parameters apply.  */
2469   parser->num_template_parameter_lists = 0;
2470
2471   return parser;
2472 }
2473
2474 /* Create a cp_lexer structure which will emit the tokens in CACHE
2475    and push it onto the parser's lexer stack.  This is used for delayed
2476    parsing of in-class method bodies and default arguments, and should
2477    not be confused with tentative parsing.  */
2478 static void
2479 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2480 {
2481   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2482   lexer->next = parser->lexer;
2483   parser->lexer = lexer;
2484
2485   /* Move the current source position to that of the first token in the
2486      new lexer.  */
2487   cp_lexer_set_source_position_from_token (lexer->next_token);
2488 }
2489
2490 /* Pop the top lexer off the parser stack.  This is never used for the
2491    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2492 static void
2493 cp_parser_pop_lexer (cp_parser *parser)
2494 {
2495   cp_lexer *lexer = parser->lexer;
2496   parser->lexer = lexer->next;
2497   cp_lexer_destroy (lexer);
2498
2499   /* Put the current source position back where it was before this
2500      lexer was pushed.  */
2501   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2502 }
2503
2504 /* Lexical conventions [gram.lex]  */
2505
2506 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2507    identifier.  */
2508
2509 static tree
2510 cp_parser_identifier (cp_parser* parser)
2511 {
2512   cp_token *token;
2513
2514   /* Look for the identifier.  */
2515   token = cp_parser_require (parser, CPP_NAME, "identifier");
2516   /* Return the value.  */
2517   return token ? token->value : error_mark_node;
2518 }
2519
2520 /* Parse a sequence of adjacent string constants.  Returns a
2521    TREE_STRING representing the combined, nul-terminated string
2522    constant.  If TRANSLATE is true, translate the string to the
2523    execution character set.  If WIDE_OK is true, a wide string is
2524    invalid here.
2525
2526    C++98 [lex.string] says that if a narrow string literal token is
2527    adjacent to a wide string literal token, the behavior is undefined.
2528    However, C99 6.4.5p4 says that this results in a wide string literal.
2529    We follow C99 here, for consistency with the C front end.
2530
2531    This code is largely lifted from lex_string() in c-lex.c.
2532
2533    FUTURE: ObjC++ will need to handle @-strings here.  */
2534 static tree
2535 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2536 {
2537   tree value;
2538   bool wide = false;
2539   size_t count;
2540   struct obstack str_ob;
2541   cpp_string str, istr, *strs;
2542   cp_token *tok;
2543
2544   tok = cp_lexer_peek_token (parser->lexer);
2545   if (!cp_parser_is_string_literal (tok))
2546     {
2547       cp_parser_error (parser, "expected string-literal");
2548       return error_mark_node;
2549     }
2550
2551   /* Try to avoid the overhead of creating and destroying an obstack
2552      for the common case of just one string.  */
2553   if (!cp_parser_is_string_literal
2554       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2555     {
2556       cp_lexer_consume_token (parser->lexer);
2557
2558       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->value);
2559       str.len = TREE_STRING_LENGTH (tok->value);
2560       count = 1;
2561       if (tok->type == CPP_WSTRING)
2562         wide = true;
2563
2564       strs = &str;
2565     }
2566   else
2567     {
2568       gcc_obstack_init (&str_ob);
2569       count = 0;
2570
2571       do
2572         {
2573           cp_lexer_consume_token (parser->lexer);
2574           count++;
2575           str.text = (unsigned char *)TREE_STRING_POINTER (tok->value);
2576           str.len = TREE_STRING_LENGTH (tok->value);
2577           if (tok->type == CPP_WSTRING)
2578             wide = true;
2579
2580           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2581
2582           tok = cp_lexer_peek_token (parser->lexer);
2583         }
2584       while (cp_parser_is_string_literal (tok));
2585
2586       strs = (cpp_string *) obstack_finish (&str_ob);
2587     }
2588
2589   if (wide && !wide_ok)
2590     {
2591       cp_parser_error (parser, "a wide string is invalid in this context");
2592       wide = false;
2593     }
2594
2595   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
2596       (parse_in, strs, count, &istr, wide))
2597     {
2598       value = build_string (istr.len, (char *)istr.text);
2599       free ((void *)istr.text);
2600
2601       TREE_TYPE (value) = wide ? wchar_array_type_node : char_array_type_node;
2602       value = fix_string_type (value);
2603     }
2604   else
2605     /* cpp_interpret_string has issued an error.  */
2606     value = error_mark_node;
2607
2608   if (count > 1)
2609     obstack_free (&str_ob, 0);
2610
2611   return value;
2612 }
2613
2614
2615 /* Basic concepts [gram.basic]  */
2616
2617 /* Parse a translation-unit.
2618
2619    translation-unit:
2620      declaration-seq [opt]
2621
2622    Returns TRUE if all went well.  */
2623
2624 static bool
2625 cp_parser_translation_unit (cp_parser* parser)
2626 {
2627   /* The address of the first non-permanent object on the declarator
2628      obstack.  */
2629   static void *declarator_obstack_base;
2630
2631   bool success;
2632
2633   /* Create the declarator obstack, if necessary.  */
2634   if (!cp_error_declarator)
2635     {
2636       gcc_obstack_init (&declarator_obstack);
2637       /* Create the error declarator.  */
2638       cp_error_declarator = make_declarator (cdk_error);
2639       /* Create the empty parameter list.  */
2640       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
2641       /* Remember where the base of the declarator obstack lies.  */
2642       declarator_obstack_base = obstack_next_free (&declarator_obstack);
2643     }
2644
2645   while (true)
2646     {
2647       cp_parser_declaration_seq_opt (parser);
2648
2649       /* If there are no tokens left then all went well.  */
2650       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
2651         {
2652           /* Get rid of the token array; we don't need it any more.  */
2653           cp_lexer_destroy (parser->lexer);
2654           parser->lexer = NULL;
2655
2656           /* This file might have been a context that's implicitly extern
2657              "C".  If so, pop the lang context.  (Only relevant for PCH.) */
2658           if (parser->implicit_extern_c)
2659             {
2660               pop_lang_context ();
2661               parser->implicit_extern_c = false;
2662             }
2663
2664           /* Finish up.  */
2665           finish_translation_unit ();
2666
2667           success = true;
2668           break;
2669         }
2670       else
2671         {
2672           cp_parser_error (parser, "expected declaration");
2673           success = false;
2674           break;
2675         }
2676     }
2677
2678   /* Make sure the declarator obstack was fully cleaned up.  */
2679   gcc_assert (obstack_next_free (&declarator_obstack)
2680               == declarator_obstack_base);
2681
2682   /* All went well.  */
2683   return success;
2684 }
2685
2686 /* Expressions [gram.expr] */
2687
2688 /* Parse a primary-expression.
2689
2690    primary-expression:
2691      literal
2692      this
2693      ( expression )
2694      id-expression
2695
2696    GNU Extensions:
2697
2698    primary-expression:
2699      ( compound-statement )
2700      __builtin_va_arg ( assignment-expression , type-id )
2701
2702    Objective-C++ Extension:
2703
2704    primary-expression:
2705      objc-expression
2706
2707    literal:
2708      __null
2709
2710    CAST_P is true if this primary expression is the target of a cast.
2711
2712    Returns a representation of the expression.
2713
2714    *IDK indicates what kind of id-expression (if any) was present.
2715
2716    *QUALIFYING_CLASS is set to a non-NULL value if the id-expression can be
2717    used as the operand of a pointer-to-member.  In that case,
2718    *QUALIFYING_CLASS gives the class that is used as the qualifying
2719    class in the pointer-to-member.  */
2720
2721 static tree
2722 cp_parser_primary_expression (cp_parser *parser,
2723                               bool cast_p,
2724                               cp_id_kind *idk,
2725                               tree *qualifying_class)
2726 {
2727   cp_token *token;
2728
2729   /* Assume the primary expression is not an id-expression.  */
2730   *idk = CP_ID_KIND_NONE;
2731   /* And that it cannot be used as pointer-to-member.  */
2732   *qualifying_class = NULL_TREE;
2733
2734   /* Peek at the next token.  */
2735   token = cp_lexer_peek_token (parser->lexer);
2736   switch (token->type)
2737     {
2738       /* literal:
2739            integer-literal
2740            character-literal
2741            floating-literal
2742            string-literal
2743            boolean-literal  */
2744     case CPP_CHAR:
2745     case CPP_WCHAR:
2746     case CPP_NUMBER:
2747       token = cp_lexer_consume_token (parser->lexer);
2748       /* Floating-point literals are only allowed in an integral
2749          constant expression if they are cast to an integral or
2750          enumeration type.  */
2751       if (TREE_CODE (token->value) == REAL_CST
2752           && parser->integral_constant_expression_p
2753           && pedantic)
2754         {
2755           /* CAST_P will be set even in invalid code like "int(2.7 +
2756              ...)".   Therefore, we have to check that the next token
2757              is sure to end the cast.  */
2758           if (cast_p)
2759             {
2760               cp_token *next_token;
2761
2762               next_token = cp_lexer_peek_token (parser->lexer);
2763               if (/* The comma at the end of an
2764                      enumerator-definition.  */
2765                   next_token->type != CPP_COMMA
2766                   /* The curly brace at the end of an enum-specifier.  */
2767                   && next_token->type != CPP_CLOSE_BRACE
2768                   /* The end of a statement.  */
2769                   && next_token->type != CPP_SEMICOLON
2770                   /* The end of the cast-expression.  */
2771                   && next_token->type != CPP_CLOSE_PAREN
2772                   /* The end of an array bound.  */
2773                   && next_token->type != CPP_CLOSE_SQUARE)
2774                 cast_p = false;
2775             }
2776
2777           /* If we are within a cast, then the constraint that the
2778              cast is to an integral or enumeration type will be
2779              checked at that point.  If we are not within a cast, then
2780              this code is invalid.  */
2781           if (!cast_p)
2782             cp_parser_non_integral_constant_expression 
2783               (parser, "floating-point literal");
2784         }
2785       return token->value;
2786
2787     case CPP_STRING:
2788     case CPP_WSTRING:
2789       /* ??? Should wide strings be allowed when parser->translate_strings_p
2790          is false (i.e. in attributes)?  If not, we can kill the third
2791          argument to cp_parser_string_literal.  */
2792       return cp_parser_string_literal (parser,
2793                                        parser->translate_strings_p,
2794                                        true);
2795
2796     case CPP_OPEN_PAREN:
2797       {
2798         tree expr;
2799         bool saved_greater_than_is_operator_p;
2800
2801         /* Consume the `('.  */
2802         cp_lexer_consume_token (parser->lexer);
2803         /* Within a parenthesized expression, a `>' token is always
2804            the greater-than operator.  */
2805         saved_greater_than_is_operator_p
2806           = parser->greater_than_is_operator_p;
2807         parser->greater_than_is_operator_p = true;
2808         /* If we see `( { ' then we are looking at the beginning of
2809            a GNU statement-expression.  */
2810         if (cp_parser_allow_gnu_extensions_p (parser)
2811             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
2812           {
2813             /* Statement-expressions are not allowed by the standard.  */
2814             if (pedantic)
2815               pedwarn ("ISO C++ forbids braced-groups within expressions");
2816
2817             /* And they're not allowed outside of a function-body; you
2818                cannot, for example, write:
2819
2820                  int i = ({ int j = 3; j + 1; });
2821
2822                at class or namespace scope.  */
2823             if (!at_function_scope_p ())
2824               error ("statement-expressions are allowed only inside functions");
2825             /* Start the statement-expression.  */
2826             expr = begin_stmt_expr ();
2827             /* Parse the compound-statement.  */
2828             cp_parser_compound_statement (parser, expr, false);
2829             /* Finish up.  */
2830             expr = finish_stmt_expr (expr, false);
2831           }
2832         else
2833           {
2834             /* Parse the parenthesized expression.  */
2835             expr = cp_parser_expression (parser, cast_p);
2836             /* Let the front end know that this expression was
2837                enclosed in parentheses. This matters in case, for
2838                example, the expression is of the form `A::B', since
2839                `&A::B' might be a pointer-to-member, but `&(A::B)' is
2840                not.  */
2841             finish_parenthesized_expr (expr);
2842           }
2843         /* The `>' token might be the end of a template-id or
2844            template-parameter-list now.  */
2845         parser->greater_than_is_operator_p
2846           = saved_greater_than_is_operator_p;
2847         /* Consume the `)'.  */
2848         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'"))
2849           cp_parser_skip_to_end_of_statement (parser);
2850
2851         return expr;
2852       }
2853
2854     case CPP_KEYWORD:
2855       switch (token->keyword)
2856         {
2857           /* These two are the boolean literals.  */
2858         case RID_TRUE:
2859           cp_lexer_consume_token (parser->lexer);
2860           return boolean_true_node;
2861         case RID_FALSE:
2862           cp_lexer_consume_token (parser->lexer);
2863           return boolean_false_node;
2864
2865           /* The `__null' literal.  */
2866         case RID_NULL:
2867           cp_lexer_consume_token (parser->lexer);
2868           return null_node;
2869
2870           /* Recognize the `this' keyword.  */
2871         case RID_THIS:
2872           cp_lexer_consume_token (parser->lexer);
2873           if (parser->local_variables_forbidden_p)
2874             {
2875               error ("%<this%> may not be used in this context");
2876               return error_mark_node;
2877             }
2878           /* Pointers cannot appear in constant-expressions.  */
2879           if (cp_parser_non_integral_constant_expression (parser,
2880                                                           "`this'"))
2881             return error_mark_node;
2882           return finish_this_expr ();
2883
2884           /* The `operator' keyword can be the beginning of an
2885              id-expression.  */
2886         case RID_OPERATOR:
2887           goto id_expression;
2888
2889         case RID_FUNCTION_NAME:
2890         case RID_PRETTY_FUNCTION_NAME:
2891         case RID_C99_FUNCTION_NAME:
2892           /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
2893              __func__ are the names of variables -- but they are
2894              treated specially.  Therefore, they are handled here,
2895              rather than relying on the generic id-expression logic
2896              below.  Grammatically, these names are id-expressions.
2897
2898              Consume the token.  */
2899           token = cp_lexer_consume_token (parser->lexer);
2900           /* Look up the name.  */
2901           return finish_fname (token->value);
2902
2903         case RID_VA_ARG:
2904           {
2905             tree expression;
2906             tree type;
2907
2908             /* The `__builtin_va_arg' construct is used to handle
2909                `va_arg'.  Consume the `__builtin_va_arg' token.  */
2910             cp_lexer_consume_token (parser->lexer);
2911             /* Look for the opening `('.  */
2912             cp_parser_require (parser, CPP_OPEN_PAREN, "`('");
2913             /* Now, parse the assignment-expression.  */
2914             expression = cp_parser_assignment_expression (parser,
2915                                                           /*cast_p=*/false);
2916             /* Look for the `,'.  */
2917             cp_parser_require (parser, CPP_COMMA, "`,'");
2918             /* Parse the type-id.  */
2919             type = cp_parser_type_id (parser);
2920             /* Look for the closing `)'.  */
2921             cp_parser_require (parser, CPP_CLOSE_PAREN, "`)'");
2922             /* Using `va_arg' in a constant-expression is not
2923                allowed.  */
2924             if (cp_parser_non_integral_constant_expression (parser,
2925                                                             "`va_arg'"))
2926               return error_mark_node;
2927             return build_x_va_arg (expression, type);
2928           }
2929
2930         case RID_OFFSETOF:
2931           return cp_parser_builtin_offsetof (parser);
2932
2933           /* Objective-C++ expressions.  */
2934         case RID_AT_ENCODE:
2935         case RID_AT_PROTOCOL:
2936         case RID_AT_SELECTOR:
2937           return cp_parser_objc_expression (parser);
2938
2939         default:
2940           cp_parser_error (parser, "expected primary-expression");
2941           return error_mark_node;
2942         }
2943
2944       /* An id-expression can start with either an identifier, a
2945          `::' as the beginning of a qualified-id, or the "operator"
2946          keyword.  */
2947     case CPP_NAME:
2948     case CPP_SCOPE:
2949     case CPP_TEMPLATE_ID:
2950     case CPP_NESTED_NAME_SPECIFIER:
2951       {
2952         tree id_expression;
2953         tree decl;
2954         const char *error_msg;
2955
2956       id_expression:
2957         /* Parse the id-expression.  */
2958         id_expression
2959           = cp_parser_id_expression (parser,
2960                                      /*template_keyword_p=*/false,
2961                                      /*check_dependency_p=*/true,
2962                                      /*template_p=*/NULL,
2963                                      /*declarator_p=*/false);
2964         if (id_expression == error_mark_node)
2965           return error_mark_node;
2966         /* If we have a template-id, then no further lookup is
2967            required.  If the template-id was for a template-class, we
2968            will sometimes have a TYPE_DECL at this point.  */
2969         else if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
2970             || TREE_CODE (id_expression) == TYPE_DECL)
2971           decl = id_expression;
2972         /* Look up the name.  */
2973         else
2974           {
2975             bool ambiguous_p;
2976
2977             decl = cp_parser_lookup_name (parser, id_expression,
2978                                           none_type,
2979                                           /*is_template=*/false,
2980                                           /*is_namespace=*/false,
2981                                           /*check_dependency=*/true,
2982                                           &ambiguous_p);
2983             /* If the lookup was ambiguous, an error will already have
2984                been issued.  */
2985             if (ambiguous_p)
2986               return error_mark_node;
2987
2988             /* In Objective-C++, an instance variable (ivar) may be preferred
2989                to whatever cp_parser_lookup_name() found.  */
2990             decl = objc_lookup_ivar (decl, id_expression);
2991
2992             /* If name lookup gives us a SCOPE_REF, then the
2993                qualifying scope was dependent.  Just propagate the
2994                name.  */
2995             if (TREE_CODE (decl) == SCOPE_REF)
2996               {
2997                 if (TYPE_P (TREE_OPERAND (decl, 0)))
2998                   *qualifying_class = TREE_OPERAND (decl, 0);
2999                 return decl;
3000               }
3001             /* Check to see if DECL is a local variable in a context
3002                where that is forbidden.  */
3003             if (parser->local_variables_forbidden_p
3004                 && local_variable_p (decl))
3005               {
3006                 /* It might be that we only found DECL because we are
3007                    trying to be generous with pre-ISO scoping rules.
3008                    For example, consider:
3009
3010                      int i;
3011                      void g() {
3012                        for (int i = 0; i < 10; ++i) {}
3013                        extern void f(int j = i);
3014                      }
3015
3016                    Here, name look up will originally find the out
3017                    of scope `i'.  We need to issue a warning message,
3018                    but then use the global `i'.  */
3019                 decl = check_for_out_of_scope_variable (decl);
3020                 if (local_variable_p (decl))
3021                   {
3022                     error ("local variable %qD may not appear in this context",
3023                            decl);
3024                     return error_mark_node;
3025                   }
3026               }
3027           }
3028
3029         decl = finish_id_expression (id_expression, decl, parser->scope,
3030                                      idk, qualifying_class,
3031                                      parser->integral_constant_expression_p,
3032                                      parser->allow_non_integral_constant_expression_p,
3033                                      &parser->non_integral_constant_expression_p,
3034                                      &error_msg);
3035         if (error_msg)
3036           cp_parser_error (parser, error_msg);
3037         return decl;
3038       }
3039
3040       /* Anything else is an error.  */
3041     default:
3042       /* ...unless we have an Objective-C++ message or string literal, that is.  */
3043       if (c_dialect_objc () 
3044           && (token->type == CPP_OPEN_SQUARE || token->type == CPP_OBJC_STRING))
3045         return cp_parser_objc_expression (parser);
3046
3047       cp_parser_error (parser, "expected primary-expression");
3048       return error_mark_node;
3049     }
3050 }
3051
3052 /* Parse an id-expression.
3053
3054    id-expression:
3055      unqualified-id
3056      qualified-id
3057
3058    qualified-id:
3059      :: [opt] nested-name-specifier template [opt] unqualified-id
3060      :: identifier
3061      :: operator-function-id
3062      :: template-id
3063
3064    Return a representation of the unqualified portion of the
3065    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3066    a `::' or nested-name-specifier.
3067
3068    Often, if the id-expression was a qualified-id, the caller will
3069    want to make a SCOPE_REF to represent the qualified-id.  This
3070    function does not do this in order to avoid wastefully creating
3071    SCOPE_REFs when they are not required.
3072
3073    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3074    `template' keyword.
3075
3076    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3077    uninstantiated templates.
3078
3079    If *TEMPLATE_P is non-NULL, it is set to true iff the
3080    `template' keyword is used to explicitly indicate that the entity
3081    named is a template.
3082
3083    If DECLARATOR_P is true, the id-expression is appearing as part of
3084    a declarator, rather than as part of an expression.  */
3085
3086 static tree
3087 cp_parser_id_expression (cp_parser *parser,
3088                          bool template_keyword_p,
3089                          bool check_dependency_p,
3090                          bool *template_p,
3091                          bool declarator_p)
3092 {
3093   bool global_scope_p;
3094   bool nested_name_specifier_p;
3095
3096   /* Assume the `template' keyword was not used.  */
3097   if (template_p)
3098     *template_p = false;
3099
3100   /* Look for the optional `::' operator.  */
3101   global_scope_p
3102     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3103        != NULL_TREE);
3104   /* Look for the optional nested-name-specifier.  */
3105   nested_name_specifier_p
3106     = (cp_parser_nested_name_specifier_opt (parser,
3107                                             /*typename_keyword_p=*/false,
3108                                             check_dependency_p,
3109                                             /*type_p=*/false,
3110                                             declarator_p)
3111        != NULL_TREE);
3112   /* If there is a nested-name-specifier, then we are looking at
3113      the first qualified-id production.  */
3114   if (nested_name_specifier_p)
3115     {
3116       tree saved_scope;
3117       tree saved_object_scope;
3118       tree saved_qualifying_scope;
3119       tree unqualified_id;
3120       bool is_template;
3121
3122       /* See if the next token is the `template' keyword.  */
3123       if (!template_p)
3124         template_p = &is_template;
3125       *template_p = cp_parser_optional_template_keyword (parser);
3126       /* Name lookup we do during the processing of the
3127          unqualified-id might obliterate SCOPE.  */
3128       saved_scope = parser->scope;
3129       saved_object_scope = parser->object_scope;
3130       saved_qualifying_scope = parser->qualifying_scope;
3131       /* Process the final unqualified-id.  */
3132       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3133                                                  check_dependency_p,
3134                                                  declarator_p);
3135       /* Restore the SAVED_SCOPE for our caller.  */
3136       parser->scope = saved_scope;
3137       parser->object_scope = saved_object_scope;
3138       parser->qualifying_scope = saved_qualifying_scope;
3139
3140       return unqualified_id;
3141     }
3142   /* Otherwise, if we are in global scope, then we are looking at one
3143      of the other qualified-id productions.  */
3144   else if (global_scope_p)
3145     {
3146       cp_token *token;
3147       tree id;
3148
3149       /* Peek at the next token.  */
3150       token = cp_lexer_peek_token (parser->lexer);
3151
3152       /* If it's an identifier, and the next token is not a "<", then
3153          we can avoid the template-id case.  This is an optimization
3154          for this common case.  */
3155       if (token->type == CPP_NAME
3156           && !cp_parser_nth_token_starts_template_argument_list_p
3157                (parser, 2))
3158         return cp_parser_identifier (parser);
3159
3160       cp_parser_parse_tentatively (parser);
3161       /* Try a template-id.  */
3162       id = cp_parser_template_id (parser,
3163                                   /*template_keyword_p=*/false,
3164                                   /*check_dependency_p=*/true,
3165                                   declarator_p);
3166       /* If that worked, we're done.  */
3167       if (cp_parser_parse_definitely (parser))
3168         return id;
3169
3170       /* Peek at the next token.  (Changes in the token buffer may
3171          have invalidated the pointer obtained above.)  */
3172       token = cp_lexer_peek_token (parser->lexer);
3173
3174       switch (token->type)
3175         {
3176         case CPP_NAME:
3177           return cp_parser_identifier (parser);
3178
3179         case CPP_KEYWORD:
3180           if (token->keyword == RID_OPERATOR)
3181             return cp_parser_operator_function_id (parser);
3182           /* Fall through.  */
3183
3184         default:
3185           cp_parser_error (parser, "expected id-expression");
3186           return error_mark_node;
3187         }
3188     }
3189   else
3190     return cp_parser_unqualified_id (parser, template_keyword_p,
3191                                      /*check_dependency_p=*/true,
3192                                      declarator_p);
3193 }
3194
3195 /* Parse an unqualified-id.
3196
3197    unqualified-id:
3198      identifier
3199      operator-function-id
3200      conversion-function-id
3201      ~ class-name
3202      template-id
3203
3204    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3205    keyword, in a construct like `A::template ...'.
3206
3207    Returns a representation of unqualified-id.  For the `identifier'
3208    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3209    production a BIT_NOT_EXPR is returned; the operand of the
3210    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3211    other productions, see the documentation accompanying the
3212    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3213    names are looked up in uninstantiated templates.  If DECLARATOR_P
3214    is true, the unqualified-id is appearing as part of a declarator,
3215    rather than as part of an expression.  */
3216
3217 static tree
3218 cp_parser_unqualified_id (cp_parser* parser,
3219                           bool template_keyword_p,
3220                           bool check_dependency_p,
3221                           bool declarator_p)
3222 {
3223   cp_token *token;
3224
3225   /* Peek at the next token.  */
3226   token = cp_lexer_peek_token (parser->lexer);
3227
3228   switch (token->type)
3229     {
3230     case CPP_NAME:
3231       {
3232         tree id;
3233
3234         /* We don't know yet whether or not this will be a
3235            template-id.  */
3236         cp_parser_parse_tentatively (parser);
3237         /* Try a template-id.  */
3238         id = cp_parser_template_id (parser, template_keyword_p,
3239                                     check_dependency_p,
3240                                     declarator_p);
3241         /* If it worked, we're done.  */
3242         if (cp_parser_parse_definitely (parser))
3243           return id;
3244         /* Otherwise, it's an ordinary identifier.  */
3245         return cp_parser_identifier (parser);
3246       }
3247
3248     case CPP_TEMPLATE_ID:
3249       return cp_parser_template_id (parser, template_keyword_p,
3250                                     check_dependency_p,
3251                                     declarator_p);
3252
3253     case CPP_COMPL:
3254       {
3255         tree type_decl;
3256         tree qualifying_scope;
3257         tree object_scope;
3258         tree scope;
3259         bool done;
3260
3261         /* Consume the `~' token.  */
3262         cp_lexer_consume_token (parser->lexer);
3263         /* Parse the class-name.  The standard, as written, seems to
3264            say that:
3265
3266              template <typename T> struct S { ~S (); };
3267              template <typename T> S<T>::~S() {}
3268
3269            is invalid, since `~' must be followed by a class-name, but
3270            `S<T>' is dependent, and so not known to be a class.
3271            That's not right; we need to look in uninstantiated
3272            templates.  A further complication arises from:
3273
3274              template <typename T> void f(T t) {
3275                t.T::~T();
3276              }
3277
3278            Here, it is not possible to look up `T' in the scope of `T'
3279            itself.  We must look in both the current scope, and the
3280            scope of the containing complete expression.
3281
3282            Yet another issue is:
3283
3284              struct S {
3285                int S;
3286                ~S();
3287              };
3288
3289              S::~S() {}
3290
3291            The standard does not seem to say that the `S' in `~S'
3292            should refer to the type `S' and not the data member
3293            `S::S'.  */
3294
3295         /* DR 244 says that we look up the name after the "~" in the
3296            same scope as we looked up the qualifying name.  That idea
3297            isn't fully worked out; it's more complicated than that.  */
3298         scope = parser->scope;
3299         object_scope = parser->object_scope;
3300         qualifying_scope = parser->qualifying_scope;
3301
3302         /* If the name is of the form "X::~X" it's OK.  */
3303         if (scope && TYPE_P (scope)
3304             && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
3305             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3306                 == CPP_OPEN_PAREN)
3307             && (cp_lexer_peek_token (parser->lexer)->value
3308                 == TYPE_IDENTIFIER (scope)))
3309           {
3310             cp_lexer_consume_token (parser->lexer);
3311             return build_nt (BIT_NOT_EXPR, scope);
3312           }
3313
3314         /* If there was an explicit qualification (S::~T), first look
3315            in the scope given by the qualification (i.e., S).  */
3316         done = false;
3317         type_decl = NULL_TREE;
3318         if (scope)
3319           {
3320             cp_parser_parse_tentatively (parser);
3321             type_decl = cp_parser_class_name (parser,
3322                                               /*typename_keyword_p=*/false,
3323                                               /*template_keyword_p=*/false,
3324                                               none_type,
3325                                               /*check_dependency=*/false,
3326                                               /*class_head_p=*/false,
3327                                               declarator_p);
3328             if (cp_parser_parse_definitely (parser))
3329               done = true;
3330           }
3331         /* In "N::S::~S", look in "N" as well.  */
3332         if (!done && scope && qualifying_scope)
3333           {
3334             cp_parser_parse_tentatively (parser);
3335             parser->scope = qualifying_scope;
3336             parser->object_scope = NULL_TREE;
3337             parser->qualifying_scope = NULL_TREE;
3338             type_decl
3339               = cp_parser_class_name (parser,
3340                                       /*typename_keyword_p=*/false,
3341                                       /*template_keyword_p=*/false,
3342                                       none_type,
3343                                       /*check_dependency=*/false,
3344                                       /*class_head_p=*/false,
3345                                       declarator_p);
3346             if (cp_parser_parse_definitely (parser))
3347               done = true;
3348           }
3349         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3350         else if (!done && object_scope)
3351           {
3352             cp_parser_parse_tentatively (parser);
3353             parser->scope = object_scope;
3354             parser->object_scope = NULL_TREE;
3355             parser->qualifying_scope = NULL_TREE;
3356             type_decl
3357               = cp_parser_class_name (parser,
3358                                       /*typename_keyword_p=*/false,
3359                                       /*template_keyword_p=*/false,
3360                                       none_type,
3361                                       /*check_dependency=*/false,
3362                                       /*class_head_p=*/false,
3363                                       declarator_p);
3364             if (cp_parser_parse_definitely (parser))
3365               done = true;
3366           }
3367         /* Look in the surrounding context.  */
3368         if (!done)
3369           {
3370             parser->scope = NULL_TREE;
3371             parser->object_scope = NULL_TREE;
3372             parser->qualifying_scope = NULL_TREE;
3373             type_decl
3374               = cp_parser_class_name (parser,
3375                                       /*typename_keyword_p=*/false,
3376                                       /*template_keyword_p=*/false,
3377                                       none_type,
3378                                       /*check_dependency=*/false,
3379                                       /*class_head_p=*/false,
3380                                       declarator_p);
3381           }
3382         /* If an error occurred, assume that the name of the
3383            destructor is the same as the name of the qualifying
3384            class.  That allows us to keep parsing after running
3385            into ill-formed destructor names.  */
3386         if (type_decl == error_mark_node && scope && TYPE_P (scope))
3387           return build_nt (BIT_NOT_EXPR, scope);
3388         else if (type_decl == error_mark_node)
3389           return error_mark_node;
3390
3391         /* [class.dtor]
3392
3393            A typedef-name that names a class shall not be used as the
3394            identifier in the declarator for a destructor declaration.  */
3395         if (declarator_p
3396             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3397             && !DECL_SELF_REFERENCE_P (type_decl)
3398             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3399           error ("typedef-name %qD used as destructor declarator",
3400                  type_decl);
3401
3402         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3403       }
3404
3405     case CPP_KEYWORD:
3406       if (token->keyword == RID_OPERATOR)
3407         {
3408           tree id;
3409
3410           /* This could be a template-id, so we try that first.  */
3411           cp_parser_parse_tentatively (parser);
3412           /* Try a template-id.  */
3413           id = cp_parser_template_id (parser, template_keyword_p,
3414                                       /*check_dependency_p=*/true,
3415                                       declarator_p);
3416           /* If that worked, we're done.  */
3417           if (cp_parser_parse_definitely (parser))
3418             return id;
3419           /* We still don't know whether we're looking at an
3420              operator-function-id or a conversion-function-id.  */
3421           cp_parser_parse_tentatively (parser);
3422           /* Try an operator-function-id.  */
3423           id = cp_parser_operator_function_id (parser);
3424           /* If that didn't work, try a conversion-function-id.  */
3425           if (!cp_parser_parse_definitely (parser))
3426             id = cp_parser_conversion_function_id (parser);
3427
3428           return id;
3429         }
3430       /* Fall through.  */
3431
3432     default:
3433       cp_parser_error (parser, "expected unqualified-id");
3434       return error_mark_node;
3435     }
3436 }
3437
3438 /* Parse an (optional) nested-name-specifier.
3439
3440    nested-name-specifier:
3441      class-or-namespace-name :: nested-name-specifier [opt]
3442      class-or-namespace-name :: template nested-name-specifier [opt]
3443
3444    PARSER->SCOPE should be set appropriately before this function is
3445    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
3446    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
3447    in name lookups.
3448
3449    Sets PARSER->SCOPE to the class (TYPE) or namespace
3450    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
3451    it unchanged if there is no nested-name-specifier.  Returns the new
3452    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
3453
3454    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
3455    part of a declaration and/or decl-specifier.  */
3456
3457 static tree
3458 cp_parser_nested_name_specifier_opt (cp_parser *parser,
3459                                      bool typename_keyword_p,
3460                                      bool check_dependency_p,
3461                                      bool type_p,
3462                                      bool is_declaration)
3463 {
3464   bool success = false;
3465   tree access_check = NULL_TREE;
3466   cp_token_position start = 0;
3467   cp_token *token;
3468
3469   /* If the next token corresponds to a nested name specifier, there
3470      is no need to reparse it.  However, if CHECK_DEPENDENCY_P is
3471      false, it may have been true before, in which case something
3472      like `A<X>::B<Y>::C' may have resulted in a nested-name-specifier
3473      of `A<X>::', where it should now be `A<X>::B<Y>::'.  So, when
3474      CHECK_DEPENDENCY_P is false, we have to fall through into the
3475      main loop.  */
3476   if (check_dependency_p
3477       && cp_lexer_next_token_is (parser->lexer, CPP_NESTED_NAME_SPECIFIER))
3478     {
3479       cp_parser_pre_parsed_nested_name_specifier (parser);
3480       return parser->scope;
3481     }
3482
3483   /* Remember where the nested-name-specifier starts.  */
3484   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
3485     start = cp_lexer_token_position (parser->lexer, false);
3486
3487   push_deferring_access_checks (dk_deferred);
3488
3489   while (true)
3490     {
3491       tree new_scope;
3492       tree old_scope;
3493       tree saved_qualifying_scope;
3494       bool template_keyword_p;
3495
3496       /* Spot cases that cannot be the beginning of a
3497          nested-name-specifier.  */
3498       token = cp_lexer_peek_token (parser->lexer);
3499
3500       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
3501          the already parsed nested-name-specifier.  */
3502       if (token->type == CPP_NESTED_NAME_SPECIFIER)
3503         {
3504           /* Grab the nested-name-specifier and continue the loop.  */
3505           cp_parser_pre_parsed_nested_name_specifier (parser);
3506           success = true;
3507           continue;
3508         }
3509
3510       /* Spot cases that cannot be the beginning of a
3511          nested-name-specifier.  On the second and subsequent times
3512          through the loop, we look for the `template' keyword.  */
3513       if (success && token->keyword == RID_TEMPLATE)
3514         ;
3515       /* A template-id can start a nested-name-specifier.  */
3516       else if (token->type == CPP_TEMPLATE_ID)
3517         ;
3518       else
3519         {
3520           /* If the next token is not an identifier, then it is
3521              definitely not a class-or-namespace-name.  */
3522           if (token->type != CPP_NAME)
3523             break;
3524           /* If the following token is neither a `<' (to begin a
3525              template-id), nor a `::', then we are not looking at a
3526              nested-name-specifier.  */
3527           token = cp_lexer_peek_nth_token (parser->lexer, 2);
3528           if (token->type != CPP_SCOPE
3529               && !cp_parser_nth_token_starts_template_argument_list_p
3530                   (parser, 2))
3531             break;
3532         }
3533
3534       /* The nested-name-specifier is optional, so we parse
3535          tentatively.  */
3536       cp_parser_parse_tentatively (parser);
3537
3538       /* Look for the optional `template' keyword, if this isn't the
3539          first time through the loop.  */
3540       if (success)
3541         template_keyword_p = cp_parser_optional_template_keyword (parser);
3542       else
3543         template_keyword_p = false;
3544
3545       /* Save the old scope since the name lookup we are about to do
3546          might destroy it.  */
3547       old_scope = parser->scope;
3548       saved_qualifying_scope = parser->qualifying_scope;
3549       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
3550          look up names in "X<T>::I" in order to determine that "Y" is
3551          a template.  So, if we have a typename at this point, we make
3552          an effort to look through it.  */
3553       if (is_declaration 
3554           && !typename_keyword_p
3555           && parser->scope 
3556           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
3557         parser->scope = resolve_typename_type (parser->scope, 
3558                                                /*only_current_p=*/false);
3559       /* Parse the qualifying entity.  */
3560       new_scope
3561         = cp_parser_class_or_namespace_name (parser,
3562                                              typename_keyword_p,
3563                                              template_keyword_p,
3564                                              check_dependency_p,
3565                                              type_p,
3566                                              is_declaration);
3567       /* Look for the `::' token.  */
3568       cp_parser_require (parser, CPP_SCOPE, "`::'");
3569
3570       /* If we found what we wanted, we keep going; otherwise, we're
3571          done.  */
3572       if (!cp_parser_parse_definitely (parser))
3573         {
3574           bool error_p = false;
3575
3576           /* Restore the OLD_SCOPE since it was valid before the
3577              failed attempt at finding the last
3578              class-or-namespace-name.  */
3579      &n