OSDN Git Service

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