OSDN Git Service

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