OSDN Git Service

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