OSDN Git Service

PR c++/43509
[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,
3613                  id_expr_token->location));
3614         if (error_msg)
3615           cp_parser_error (parser, error_msg);
3616         return decl;
3617       }
3618
3619       /* Anything else is an error.  */
3620     default:
3621       cp_parser_error (parser, "expected primary-expression");
3622       return error_mark_node;
3623     }
3624 }
3625
3626 /* Parse an id-expression.
3627
3628    id-expression:
3629      unqualified-id
3630      qualified-id
3631
3632    qualified-id:
3633      :: [opt] nested-name-specifier template [opt] unqualified-id
3634      :: identifier
3635      :: operator-function-id
3636      :: template-id
3637
3638    Return a representation of the unqualified portion of the
3639    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3640    a `::' or nested-name-specifier.
3641
3642    Often, if the id-expression was a qualified-id, the caller will
3643    want to make a SCOPE_REF to represent the qualified-id.  This
3644    function does not do this in order to avoid wastefully creating
3645    SCOPE_REFs when they are not required.
3646
3647    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3648    `template' keyword.
3649
3650    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3651    uninstantiated templates.
3652
3653    If *TEMPLATE_P is non-NULL, it is set to true iff the
3654    `template' keyword is used to explicitly indicate that the entity
3655    named is a template.
3656
3657    If DECLARATOR_P is true, the id-expression is appearing as part of
3658    a declarator, rather than as part of an expression.  */
3659
3660 static tree
3661 cp_parser_id_expression (cp_parser *parser,
3662                          bool template_keyword_p,
3663                          bool check_dependency_p,
3664                          bool *template_p,
3665                          bool declarator_p,
3666                          bool optional_p)
3667 {
3668   bool global_scope_p;
3669   bool nested_name_specifier_p;
3670
3671   /* Assume the `template' keyword was not used.  */
3672   if (template_p)
3673     *template_p = template_keyword_p;
3674
3675   /* Look for the optional `::' operator.  */
3676   global_scope_p
3677     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3678        != NULL_TREE);
3679   /* Look for the optional nested-name-specifier.  */
3680   nested_name_specifier_p
3681     = (cp_parser_nested_name_specifier_opt (parser,
3682                                             /*typename_keyword_p=*/false,
3683                                             check_dependency_p,
3684                                             /*type_p=*/false,
3685                                             declarator_p)
3686        != NULL_TREE);
3687   /* If there is a nested-name-specifier, then we are looking at
3688      the first qualified-id production.  */
3689   if (nested_name_specifier_p)
3690     {
3691       tree saved_scope;
3692       tree saved_object_scope;
3693       tree saved_qualifying_scope;
3694       tree unqualified_id;
3695       bool is_template;
3696
3697       /* See if the next token is the `template' keyword.  */
3698       if (!template_p)
3699         template_p = &is_template;
3700       *template_p = cp_parser_optional_template_keyword (parser);
3701       /* Name lookup we do during the processing of the
3702          unqualified-id might obliterate SCOPE.  */
3703       saved_scope = parser->scope;
3704       saved_object_scope = parser->object_scope;
3705       saved_qualifying_scope = parser->qualifying_scope;
3706       /* Process the final unqualified-id.  */
3707       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3708                                                  check_dependency_p,
3709                                                  declarator_p,
3710                                                  /*optional_p=*/false);
3711       /* Restore the SAVED_SCOPE for our caller.  */
3712       parser->scope = saved_scope;
3713       parser->object_scope = saved_object_scope;
3714       parser->qualifying_scope = saved_qualifying_scope;
3715
3716       return unqualified_id;
3717     }
3718   /* Otherwise, if we are in global scope, then we are looking at one
3719      of the other qualified-id productions.  */
3720   else if (global_scope_p)
3721     {
3722       cp_token *token;
3723       tree id;
3724
3725       /* Peek at the next token.  */
3726       token = cp_lexer_peek_token (parser->lexer);
3727
3728       /* If it's an identifier, and the next token is not a "<", then
3729          we can avoid the template-id case.  This is an optimization
3730          for this common case.  */
3731       if (token->type == CPP_NAME
3732           && !cp_parser_nth_token_starts_template_argument_list_p
3733                (parser, 2))
3734         return cp_parser_identifier (parser);
3735
3736       cp_parser_parse_tentatively (parser);
3737       /* Try a template-id.  */
3738       id = cp_parser_template_id (parser,
3739                                   /*template_keyword_p=*/false,
3740                                   /*check_dependency_p=*/true,
3741                                   declarator_p);
3742       /* If that worked, we're done.  */
3743       if (cp_parser_parse_definitely (parser))
3744         return id;
3745
3746       /* Peek at the next token.  (Changes in the token buffer may
3747          have invalidated the pointer obtained above.)  */
3748       token = cp_lexer_peek_token (parser->lexer);
3749
3750       switch (token->type)
3751         {
3752         case CPP_NAME:
3753           return cp_parser_identifier (parser);
3754
3755         case CPP_KEYWORD:
3756           if (token->keyword == RID_OPERATOR)
3757             return cp_parser_operator_function_id (parser);
3758           /* Fall through.  */
3759
3760         default:
3761           cp_parser_error (parser, "expected id-expression");
3762           return error_mark_node;
3763         }
3764     }
3765   else
3766     return cp_parser_unqualified_id (parser, template_keyword_p,
3767                                      /*check_dependency_p=*/true,
3768                                      declarator_p,
3769                                      optional_p);
3770 }
3771
3772 /* Parse an unqualified-id.
3773
3774    unqualified-id:
3775      identifier
3776      operator-function-id
3777      conversion-function-id
3778      ~ class-name
3779      template-id
3780
3781    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3782    keyword, in a construct like `A::template ...'.
3783
3784    Returns a representation of unqualified-id.  For the `identifier'
3785    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3786    production a BIT_NOT_EXPR is returned; the operand of the
3787    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3788    other productions, see the documentation accompanying the
3789    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3790    names are looked up in uninstantiated templates.  If DECLARATOR_P
3791    is true, the unqualified-id is appearing as part of a declarator,
3792    rather than as part of an expression.  */
3793
3794 static tree
3795 cp_parser_unqualified_id (cp_parser* parser,
3796                           bool template_keyword_p,
3797                           bool check_dependency_p,
3798                           bool declarator_p,
3799                           bool optional_p)
3800 {
3801   cp_token *token;
3802
3803   /* Peek at the next token.  */
3804   token = cp_lexer_peek_token (parser->lexer);
3805
3806   switch (token->type)
3807     {
3808     case CPP_NAME:
3809       {
3810         tree id;
3811
3812         /* We don't know yet whether or not this will be a
3813            template-id.  */
3814         cp_parser_parse_tentatively (parser);
3815         /* Try a template-id.  */
3816         id = cp_parser_template_id (parser, template_keyword_p,
3817                                     check_dependency_p,
3818                                     declarator_p);
3819         /* If it worked, we're done.  */
3820         if (cp_parser_parse_definitely (parser))
3821           return id;
3822         /* Otherwise, it's an ordinary identifier.  */
3823         return cp_parser_identifier (parser);
3824       }
3825
3826     case CPP_TEMPLATE_ID:
3827       return cp_parser_template_id (parser, template_keyword_p,
3828                                     check_dependency_p,
3829                                     declarator_p);
3830
3831     case CPP_COMPL:
3832       {
3833         tree type_decl;
3834         tree qualifying_scope;
3835         tree object_scope;
3836         tree scope;
3837         bool done;
3838
3839         /* Consume the `~' token.  */
3840         cp_lexer_consume_token (parser->lexer);
3841         /* Parse the class-name.  The standard, as written, seems to
3842            say that:
3843
3844              template <typename T> struct S { ~S (); };
3845              template <typename T> S<T>::~S() {}
3846
3847            is invalid, since `~' must be followed by a class-name, but
3848            `S<T>' is dependent, and so not known to be a class.
3849            That's not right; we need to look in uninstantiated
3850            templates.  A further complication arises from:
3851
3852              template <typename T> void f(T t) {
3853                t.T::~T();
3854              }
3855
3856            Here, it is not possible to look up `T' in the scope of `T'
3857            itself.  We must look in both the current scope, and the
3858            scope of the containing complete expression.
3859
3860            Yet another issue is:
3861
3862              struct S {
3863                int S;
3864                ~S();
3865              };
3866
3867              S::~S() {}
3868
3869            The standard does not seem to say that the `S' in `~S'
3870            should refer to the type `S' and not the data member
3871            `S::S'.  */
3872
3873         /* DR 244 says that we look up the name after the "~" in the
3874            same scope as we looked up the qualifying name.  That idea
3875            isn't fully worked out; it's more complicated than that.  */
3876         scope = parser->scope;
3877         object_scope = parser->object_scope;
3878         qualifying_scope = parser->qualifying_scope;
3879
3880         /* Check for invalid scopes.  */
3881         if (scope == error_mark_node)
3882           {
3883             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3884               cp_lexer_consume_token (parser->lexer);
3885             return error_mark_node;
3886           }
3887         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3888           {
3889             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3890               error_at (token->location,
3891                         "scope %qT before %<~%> is not a class-name",
3892                         scope);
3893             cp_parser_simulate_error (parser);
3894             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3895               cp_lexer_consume_token (parser->lexer);
3896             return error_mark_node;
3897           }
3898         gcc_assert (!scope || TYPE_P (scope));
3899
3900         /* If the name is of the form "X::~X" it's OK.  */
3901         token = cp_lexer_peek_token (parser->lexer);
3902         if (scope
3903             && token->type == CPP_NAME
3904             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3905                 != CPP_LESS)
3906             && constructor_name_p (token->u.value, scope))
3907           {
3908             cp_lexer_consume_token (parser->lexer);
3909             return build_nt (BIT_NOT_EXPR, scope);
3910           }
3911
3912         /* If there was an explicit qualification (S::~T), first look
3913            in the scope given by the qualification (i.e., S).
3914
3915            Note: in the calls to cp_parser_class_name below we pass
3916            typename_type so that lookup finds the injected-class-name
3917            rather than the constructor.  */
3918         done = false;
3919         type_decl = NULL_TREE;
3920         if (scope)
3921           {
3922             cp_parser_parse_tentatively (parser);
3923             type_decl = cp_parser_class_name (parser,
3924                                               /*typename_keyword_p=*/false,
3925                                               /*template_keyword_p=*/false,
3926                                               typename_type,
3927                                               /*check_dependency=*/false,
3928                                               /*class_head_p=*/false,
3929                                               declarator_p);
3930             if (cp_parser_parse_definitely (parser))
3931               done = true;
3932           }
3933         /* In "N::S::~S", look in "N" as well.  */
3934         if (!done && scope && qualifying_scope)
3935           {
3936             cp_parser_parse_tentatively (parser);
3937             parser->scope = qualifying_scope;
3938             parser->object_scope = NULL_TREE;
3939             parser->qualifying_scope = NULL_TREE;
3940             type_decl
3941               = cp_parser_class_name (parser,
3942                                       /*typename_keyword_p=*/false,
3943                                       /*template_keyword_p=*/false,
3944                                       typename_type,
3945                                       /*check_dependency=*/false,
3946                                       /*class_head_p=*/false,
3947                                       declarator_p);
3948             if (cp_parser_parse_definitely (parser))
3949               done = true;
3950           }
3951         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3952         else if (!done && object_scope)
3953           {
3954             cp_parser_parse_tentatively (parser);
3955             parser->scope = object_scope;
3956             parser->object_scope = NULL_TREE;
3957             parser->qualifying_scope = NULL_TREE;
3958             type_decl
3959               = cp_parser_class_name (parser,
3960                                       /*typename_keyword_p=*/false,
3961                                       /*template_keyword_p=*/false,
3962                                       typename_type,
3963                                       /*check_dependency=*/false,
3964                                       /*class_head_p=*/false,
3965                                       declarator_p);
3966             if (cp_parser_parse_definitely (parser))
3967               done = true;
3968           }
3969         /* Look in the surrounding context.  */
3970         if (!done)
3971           {
3972             parser->scope = NULL_TREE;
3973             parser->object_scope = NULL_TREE;
3974             parser->qualifying_scope = NULL_TREE;
3975             if (processing_template_decl)
3976               cp_parser_parse_tentatively (parser);
3977             type_decl
3978               = cp_parser_class_name (parser,
3979                                       /*typename_keyword_p=*/false,
3980                                       /*template_keyword_p=*/false,
3981                                       typename_type,
3982                                       /*check_dependency=*/false,
3983                                       /*class_head_p=*/false,
3984                                       declarator_p);
3985             if (processing_template_decl
3986                 && ! cp_parser_parse_definitely (parser))
3987               {
3988                 /* We couldn't find a type with this name, so just accept
3989                    it and check for a match at instantiation time.  */
3990                 type_decl = cp_parser_identifier (parser);
3991                 if (type_decl != error_mark_node)
3992                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3993                 return type_decl;
3994               }
3995           }
3996         /* If an error occurred, assume that the name of the
3997            destructor is the same as the name of the qualifying
3998            class.  That allows us to keep parsing after running
3999            into ill-formed destructor names.  */
4000         if (type_decl == error_mark_node && scope)
4001           return build_nt (BIT_NOT_EXPR, scope);
4002         else if (type_decl == error_mark_node)
4003           return error_mark_node;
4004
4005         /* Check that destructor name and scope match.  */
4006         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4007           {
4008             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4009               error_at (token->location,
4010                         "declaration of %<~%T%> as member of %qT",
4011                         type_decl, scope);
4012             cp_parser_simulate_error (parser);
4013             return error_mark_node;
4014           }
4015
4016         /* [class.dtor]
4017
4018            A typedef-name that names a class shall not be used as the
4019            identifier in the declarator for a destructor declaration.  */
4020         if (declarator_p
4021             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4022             && !DECL_SELF_REFERENCE_P (type_decl)
4023             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4024           error_at (token->location,
4025                     "typedef-name %qD used as destructor declarator",
4026                     type_decl);
4027
4028         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4029       }
4030
4031     case CPP_KEYWORD:
4032       if (token->keyword == RID_OPERATOR)
4033         {
4034           tree id;
4035
4036           /* This could be a template-id, so we try that first.  */
4037           cp_parser_parse_tentatively (parser);
4038           /* Try a template-id.  */
4039           id = cp_parser_template_id (parser, template_keyword_p,
4040                                       /*check_dependency_p=*/true,
4041                                       declarator_p);
4042           /* If that worked, we're done.  */
4043           if (cp_parser_parse_definitely (parser))
4044             return id;
4045           /* We still don't know whether we're looking at an
4046              operator-function-id or a conversion-function-id.  */
4047           cp_parser_parse_tentatively (parser);
4048           /* Try an operator-function-id.  */
4049           id = cp_parser_operator_function_id (parser);
4050           /* If that didn't work, try a conversion-function-id.  */
4051           if (!cp_parser_parse_definitely (parser))
4052             id = cp_parser_conversion_function_id (parser);
4053
4054           return id;
4055         }
4056       /* Fall through.  */
4057
4058     default:
4059       if (optional_p)
4060         return NULL_TREE;
4061       cp_parser_error (parser, "expected unqualified-id");
4062       return error_mark_node;
4063     }
4064 }
4065
4066 /* Parse an (optional) nested-name-specifier.
4067
4068    nested-name-specifier: [C++98]
4069      class-or-namespace-name :: nested-name-specifier [opt]
4070      class-or-namespace-name :: template nested-name-specifier [opt]
4071
4072    nested-name-specifier: [C++0x]
4073      type-name ::
4074      namespace-name ::
4075      nested-name-specifier identifier ::
4076      nested-name-specifier template [opt] simple-template-id ::
4077
4078    PARSER->SCOPE should be set appropriately before this function is
4079    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4080    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4081    in name lookups.
4082
4083    Sets PARSER->SCOPE to the class (TYPE) or namespace
4084    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4085    it unchanged if there is no nested-name-specifier.  Returns the new
4086    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4087
4088    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4089    part of a declaration and/or decl-specifier.  */
4090
4091 static tree
4092 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4093                                      bool typename_keyword_p,
4094                                      bool check_dependency_p,
4095                                      bool type_p,
4096                                      bool is_declaration)
4097 {
4098   bool success = false;
4099   cp_token_position start = 0;
4100   cp_token *token;
4101
4102   /* Remember where the nested-name-specifier starts.  */
4103   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4104     {
4105       start = cp_lexer_token_position (parser->lexer, false);
4106       push_deferring_access_checks (dk_deferred);
4107     }
4108
4109   while (true)
4110     {
4111       tree new_scope;
4112       tree old_scope;
4113       tree saved_qualifying_scope;
4114       bool template_keyword_p;
4115
4116       /* Spot cases that cannot be the beginning of a
4117          nested-name-specifier.  */
4118       token = cp_lexer_peek_token (parser->lexer);
4119
4120       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4121          the already parsed nested-name-specifier.  */
4122       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4123         {
4124           /* Grab the nested-name-specifier and continue the loop.  */
4125           cp_parser_pre_parsed_nested_name_specifier (parser);
4126           /* If we originally encountered this nested-name-specifier
4127              with IS_DECLARATION set to false, we will not have
4128              resolved TYPENAME_TYPEs, so we must do so here.  */
4129           if (is_declaration
4130               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4131             {
4132               new_scope = resolve_typename_type (parser->scope,
4133                                                  /*only_current_p=*/false);
4134               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4135                 parser->scope = new_scope;
4136             }
4137           success = true;
4138           continue;
4139         }
4140
4141       /* Spot cases that cannot be the beginning of a
4142          nested-name-specifier.  On the second and subsequent times
4143          through the loop, we look for the `template' keyword.  */
4144       if (success && token->keyword == RID_TEMPLATE)
4145         ;
4146       /* A template-id can start a nested-name-specifier.  */
4147       else if (token->type == CPP_TEMPLATE_ID)
4148         ;
4149       else
4150         {
4151           /* If the next token is not an identifier, then it is
4152              definitely not a type-name or namespace-name.  */
4153           if (token->type != CPP_NAME)
4154             break;
4155           /* If the following token is neither a `<' (to begin a
4156              template-id), nor a `::', then we are not looking at a
4157              nested-name-specifier.  */
4158           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4159           if (token->type != CPP_SCOPE
4160               && !cp_parser_nth_token_starts_template_argument_list_p
4161                   (parser, 2))
4162             break;
4163         }
4164
4165       /* The nested-name-specifier is optional, so we parse
4166          tentatively.  */
4167       cp_parser_parse_tentatively (parser);
4168
4169       /* Look for the optional `template' keyword, if this isn't the
4170          first time through the loop.  */
4171       if (success)
4172         template_keyword_p = cp_parser_optional_template_keyword (parser);
4173       else
4174         template_keyword_p = false;
4175
4176       /* Save the old scope since the name lookup we are about to do
4177          might destroy it.  */
4178       old_scope = parser->scope;
4179       saved_qualifying_scope = parser->qualifying_scope;
4180       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4181          look up names in "X<T>::I" in order to determine that "Y" is
4182          a template.  So, if we have a typename at this point, we make
4183          an effort to look through it.  */
4184       if (is_declaration
4185           && !typename_keyword_p
4186           && parser->scope
4187           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4188         parser->scope = resolve_typename_type (parser->scope,
4189                                                /*only_current_p=*/false);
4190       /* Parse the qualifying entity.  */
4191       new_scope
4192         = cp_parser_qualifying_entity (parser,
4193                                        typename_keyword_p,
4194                                        template_keyword_p,
4195                                        check_dependency_p,
4196                                        type_p,
4197                                        is_declaration);
4198       /* Look for the `::' token.  */
4199       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4200
4201       /* If we found what we wanted, we keep going; otherwise, we're
4202          done.  */
4203       if (!cp_parser_parse_definitely (parser))
4204         {
4205           bool error_p = false;
4206
4207           /* Restore the OLD_SCOPE since it was valid before the
4208              failed attempt at finding the last
4209              class-or-namespace-name.  */
4210           parser->scope = old_scope;
4211           parser->qualifying_scope = saved_qualifying_scope;
4212           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4213             break;
4214           /* If the next token is an identifier, and the one after
4215              that is a `::', then any valid interpretation would have
4216              found a class-or-namespace-name.  */
4217           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4218                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4219                      == CPP_SCOPE)
4220                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4221                      != CPP_COMPL))
4222             {
4223               token = cp_lexer_consume_token (parser->lexer);
4224               if (!error_p)
4225                 {
4226                   if (!token->ambiguous_p)
4227                     {
4228                       tree decl;
4229                       tree ambiguous_decls;
4230
4231                       decl = cp_parser_lookup_name (parser, token->u.value,
4232                                                     none_type,
4233                                                     /*is_template=*/false,
4234                                                     /*is_namespace=*/false,
4235                                                     /*check_dependency=*/true,
4236                                                     &ambiguous_decls,
4237                                                     token->location);
4238                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4239                         error_at (token->location,
4240                                   "%qD used without template parameters",
4241                                   decl);
4242                       else if (ambiguous_decls)
4243                         {
4244                           error_at (token->location,
4245                                     "reference to %qD is ambiguous",
4246                                     token->u.value);
4247                           print_candidates (ambiguous_decls);
4248                           decl = error_mark_node;
4249                         }
4250                       else
4251                         {
4252                           const char* msg = "is not a class or namespace";
4253                           if (cxx_dialect != cxx98)
4254                             msg = "is not a class, namespace, or enumeration";
4255                           cp_parser_name_lookup_error
4256                             (parser, token->u.value, decl, msg,
4257                              token->location);
4258                         }
4259                     }
4260                   parser->scope = error_mark_node;
4261                   error_p = true;
4262                   /* Treat this as a successful nested-name-specifier
4263                      due to:
4264
4265                      [basic.lookup.qual]
4266
4267                      If the name found is not a class-name (clause
4268                      _class_) or namespace-name (_namespace.def_), the
4269                      program is ill-formed.  */
4270                   success = true;
4271                 }
4272               cp_lexer_consume_token (parser->lexer);
4273             }
4274           break;
4275         }
4276       /* We've found one valid nested-name-specifier.  */
4277       success = true;
4278       /* Name lookup always gives us a DECL.  */
4279       if (TREE_CODE (new_scope) == TYPE_DECL)
4280         new_scope = TREE_TYPE (new_scope);
4281       /* Uses of "template" must be followed by actual templates.  */
4282       if (template_keyword_p
4283           && !(CLASS_TYPE_P (new_scope)
4284                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4285                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4286                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4287           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4288                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4289                    == TEMPLATE_ID_EXPR)))
4290         permerror (input_location, TYPE_P (new_scope)
4291                    ? "%qT is not a template"
4292                    : "%qD is not a template",
4293                    new_scope);
4294       /* If it is a class scope, try to complete it; we are about to
4295          be looking up names inside the class.  */
4296       if (TYPE_P (new_scope)
4297           /* Since checking types for dependency can be expensive,
4298              avoid doing it if the type is already complete.  */
4299           && !COMPLETE_TYPE_P (new_scope)
4300           /* Do not try to complete dependent types.  */
4301           && !dependent_type_p (new_scope))
4302         {
4303           new_scope = complete_type (new_scope);
4304           /* If it is a typedef to current class, use the current
4305              class instead, as the typedef won't have any names inside
4306              it yet.  */
4307           if (!COMPLETE_TYPE_P (new_scope)
4308               && currently_open_class (new_scope))
4309             new_scope = TYPE_MAIN_VARIANT (new_scope);
4310         }
4311       /* Make sure we look in the right scope the next time through
4312          the loop.  */
4313       parser->scope = new_scope;
4314     }
4315
4316   /* If parsing tentatively, replace the sequence of tokens that makes
4317      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4318      token.  That way, should we re-parse the token stream, we will
4319      not have to repeat the effort required to do the parse, nor will
4320      we issue duplicate error messages.  */
4321   if (success && start)
4322     {
4323       cp_token *token;
4324
4325       token = cp_lexer_token_at (parser->lexer, start);
4326       /* Reset the contents of the START token.  */
4327       token->type = CPP_NESTED_NAME_SPECIFIER;
4328       /* Retrieve any deferred checks.  Do not pop this access checks yet
4329          so the memory will not be reclaimed during token replacing below.  */
4330       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4331       token->u.tree_check_value->value = parser->scope;
4332       token->u.tree_check_value->checks = get_deferred_access_checks ();
4333       token->u.tree_check_value->qualifying_scope =
4334         parser->qualifying_scope;
4335       token->keyword = RID_MAX;
4336
4337       /* Purge all subsequent tokens.  */
4338       cp_lexer_purge_tokens_after (parser->lexer, start);
4339     }
4340
4341   if (start)
4342     pop_to_parent_deferring_access_checks ();
4343
4344   return success ? parser->scope : NULL_TREE;
4345 }
4346
4347 /* Parse a nested-name-specifier.  See
4348    cp_parser_nested_name_specifier_opt for details.  This function
4349    behaves identically, except that it will an issue an error if no
4350    nested-name-specifier is present.  */
4351
4352 static tree
4353 cp_parser_nested_name_specifier (cp_parser *parser,
4354                                  bool typename_keyword_p,
4355                                  bool check_dependency_p,
4356                                  bool type_p,
4357                                  bool is_declaration)
4358 {
4359   tree scope;
4360
4361   /* Look for the nested-name-specifier.  */
4362   scope = cp_parser_nested_name_specifier_opt (parser,
4363                                                typename_keyword_p,
4364                                                check_dependency_p,
4365                                                type_p,
4366                                                is_declaration);
4367   /* If it was not present, issue an error message.  */
4368   if (!scope)
4369     {
4370       cp_parser_error (parser, "expected nested-name-specifier");
4371       parser->scope = NULL_TREE;
4372     }
4373
4374   return scope;
4375 }
4376
4377 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4378    this is either a class-name or a namespace-name (which corresponds
4379    to the class-or-namespace-name production in the grammar). For
4380    C++0x, it can also be a type-name that refers to an enumeration
4381    type.
4382
4383    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4384    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4385    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4386    TYPE_P is TRUE iff the next name should be taken as a class-name,
4387    even the same name is declared to be another entity in the same
4388    scope.
4389
4390    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4391    specified by the class-or-namespace-name.  If neither is found the
4392    ERROR_MARK_NODE is returned.  */
4393
4394 static tree
4395 cp_parser_qualifying_entity (cp_parser *parser,
4396                              bool typename_keyword_p,
4397                              bool template_keyword_p,
4398                              bool check_dependency_p,
4399                              bool type_p,
4400                              bool is_declaration)
4401 {
4402   tree saved_scope;
4403   tree saved_qualifying_scope;
4404   tree saved_object_scope;
4405   tree scope;
4406   bool only_class_p;
4407   bool successful_parse_p;
4408
4409   /* Before we try to parse the class-name, we must save away the
4410      current PARSER->SCOPE since cp_parser_class_name will destroy
4411      it.  */
4412   saved_scope = parser->scope;
4413   saved_qualifying_scope = parser->qualifying_scope;
4414   saved_object_scope = parser->object_scope;
4415   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4416      there is no need to look for a namespace-name.  */
4417   only_class_p = template_keyword_p 
4418     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4419   if (!only_class_p)
4420     cp_parser_parse_tentatively (parser);
4421   scope = cp_parser_class_name (parser,
4422                                 typename_keyword_p,
4423                                 template_keyword_p,
4424                                 type_p ? class_type : none_type,
4425                                 check_dependency_p,
4426                                 /*class_head_p=*/false,
4427                                 is_declaration);
4428   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4429   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4430   if (!only_class_p 
4431       && cxx_dialect != cxx98
4432       && !successful_parse_p)
4433     {
4434       /* Restore the saved scope.  */
4435       parser->scope = saved_scope;
4436       parser->qualifying_scope = saved_qualifying_scope;
4437       parser->object_scope = saved_object_scope;
4438
4439       /* Parse tentatively.  */
4440       cp_parser_parse_tentatively (parser);
4441      
4442       /* Parse a typedef-name or enum-name.  */
4443       scope = cp_parser_nonclass_name (parser);
4444
4445       /* "If the name found does not designate a namespace or a class,
4446          enumeration, or dependent type, the program is ill-formed."
4447
4448          We cover classes and dependent types above and namespaces below,
4449          so this code is only looking for enums.  */
4450       if (!scope || TREE_CODE (scope) != TYPE_DECL
4451           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4452         cp_parser_simulate_error (parser);
4453
4454       successful_parse_p = cp_parser_parse_definitely (parser);
4455     }
4456   /* If that didn't work, try for a namespace-name.  */
4457   if (!only_class_p && !successful_parse_p)
4458     {
4459       /* Restore the saved scope.  */
4460       parser->scope = saved_scope;
4461       parser->qualifying_scope = saved_qualifying_scope;
4462       parser->object_scope = saved_object_scope;
4463       /* If we are not looking at an identifier followed by the scope
4464          resolution operator, then this is not part of a
4465          nested-name-specifier.  (Note that this function is only used
4466          to parse the components of a nested-name-specifier.)  */
4467       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4468           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4469         return error_mark_node;
4470       scope = cp_parser_namespace_name (parser);
4471     }
4472
4473   return scope;
4474 }
4475
4476 /* Parse a postfix-expression.
4477
4478    postfix-expression:
4479      primary-expression
4480      postfix-expression [ expression ]
4481      postfix-expression ( expression-list [opt] )
4482      simple-type-specifier ( expression-list [opt] )
4483      typename :: [opt] nested-name-specifier identifier
4484        ( expression-list [opt] )
4485      typename :: [opt] nested-name-specifier template [opt] template-id
4486        ( expression-list [opt] )
4487      postfix-expression . template [opt] id-expression
4488      postfix-expression -> template [opt] id-expression
4489      postfix-expression . pseudo-destructor-name
4490      postfix-expression -> pseudo-destructor-name
4491      postfix-expression ++
4492      postfix-expression --
4493      dynamic_cast < type-id > ( expression )
4494      static_cast < type-id > ( expression )
4495      reinterpret_cast < type-id > ( expression )
4496      const_cast < type-id > ( expression )
4497      typeid ( expression )
4498      typeid ( type-id )
4499
4500    GNU Extension:
4501
4502    postfix-expression:
4503      ( type-id ) { initializer-list , [opt] }
4504
4505    This extension is a GNU version of the C99 compound-literal
4506    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4507    but they are essentially the same concept.)
4508
4509    If ADDRESS_P is true, the postfix expression is the operand of the
4510    `&' operator.  CAST_P is true if this expression is the target of a
4511    cast.
4512
4513    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4514    class member access expressions [expr.ref].
4515
4516    Returns a representation of the expression.  */
4517
4518 static tree
4519 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4520                               bool member_access_only_p,
4521                               cp_id_kind * pidk_return)
4522 {
4523   cp_token *token;
4524   enum rid keyword;
4525   cp_id_kind idk = CP_ID_KIND_NONE;
4526   tree postfix_expression = NULL_TREE;
4527   bool is_member_access = false;
4528
4529   /* Peek at the next token.  */
4530   token = cp_lexer_peek_token (parser->lexer);
4531   /* Some of the productions are determined by keywords.  */
4532   keyword = token->keyword;
4533   switch (keyword)
4534     {
4535     case RID_DYNCAST:
4536     case RID_STATCAST:
4537     case RID_REINTCAST:
4538     case RID_CONSTCAST:
4539       {
4540         tree type;
4541         tree expression;
4542         const char *saved_message;
4543
4544         /* All of these can be handled in the same way from the point
4545            of view of parsing.  Begin by consuming the token
4546            identifying the cast.  */
4547         cp_lexer_consume_token (parser->lexer);
4548
4549         /* New types cannot be defined in the cast.  */
4550         saved_message = parser->type_definition_forbidden_message;
4551         parser->type_definition_forbidden_message
4552           = G_("types may not be defined in casts");
4553
4554         /* Look for the opening `<'.  */
4555         cp_parser_require (parser, CPP_LESS, "%<<%>");
4556         /* Parse the type to which we are casting.  */
4557         type = cp_parser_type_id (parser);
4558         /* Look for the closing `>'.  */
4559         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4560         /* Restore the old message.  */
4561         parser->type_definition_forbidden_message = saved_message;
4562
4563         /* And the expression which is being cast.  */
4564         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4565         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4566         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4567
4568         /* Only type conversions to integral or enumeration types
4569            can be used in constant-expressions.  */
4570         if (!cast_valid_in_integral_constant_expression_p (type)
4571             && (cp_parser_non_integral_constant_expression
4572                 (parser,
4573                  "a cast to a type other than an integral or "
4574                  "enumeration type")))
4575           return error_mark_node;
4576
4577         switch (keyword)
4578           {
4579           case RID_DYNCAST:
4580             postfix_expression
4581               = build_dynamic_cast (type, expression, tf_warning_or_error);
4582             break;
4583           case RID_STATCAST:
4584             postfix_expression
4585               = build_static_cast (type, expression, tf_warning_or_error);
4586             break;
4587           case RID_REINTCAST:
4588             postfix_expression
4589               = build_reinterpret_cast (type, expression, 
4590                                         tf_warning_or_error);
4591             break;
4592           case RID_CONSTCAST:
4593             postfix_expression
4594               = build_const_cast (type, expression, tf_warning_or_error);
4595             break;
4596           default:
4597             gcc_unreachable ();
4598           }
4599       }
4600       break;
4601
4602     case RID_TYPEID:
4603       {
4604         tree type;
4605         const char *saved_message;
4606         bool saved_in_type_id_in_expr_p;
4607
4608         /* Consume the `typeid' token.  */
4609         cp_lexer_consume_token (parser->lexer);
4610         /* Look for the `(' token.  */
4611         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4612         /* Types cannot be defined in a `typeid' expression.  */
4613         saved_message = parser->type_definition_forbidden_message;
4614         parser->type_definition_forbidden_message
4615           = G_("types may not be defined in a %<typeid%> expression");
4616         /* We can't be sure yet whether we're looking at a type-id or an
4617            expression.  */
4618         cp_parser_parse_tentatively (parser);
4619         /* Try a type-id first.  */
4620         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4621         parser->in_type_id_in_expr_p = true;
4622         type = cp_parser_type_id (parser);
4623         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4624         /* Look for the `)' token.  Otherwise, we can't be sure that
4625            we're not looking at an expression: consider `typeid (int
4626            (3))', for example.  */
4627         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4628         /* If all went well, simply lookup the type-id.  */
4629         if (cp_parser_parse_definitely (parser))
4630           postfix_expression = get_typeid (type);
4631         /* Otherwise, fall back to the expression variant.  */
4632         else
4633           {
4634             tree expression;
4635
4636             /* Look for an expression.  */
4637             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4638             /* Compute its typeid.  */
4639             postfix_expression = build_typeid (expression);
4640             /* Look for the `)' token.  */
4641             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4642           }
4643         /* Restore the saved message.  */
4644         parser->type_definition_forbidden_message = saved_message;
4645         /* `typeid' may not appear in an integral constant expression.  */
4646         if (cp_parser_non_integral_constant_expression(parser,
4647                                                        "%<typeid%> operator"))
4648           return error_mark_node;
4649       }
4650       break;
4651
4652     case RID_TYPENAME:
4653       {
4654         tree type;
4655         /* The syntax permitted here is the same permitted for an
4656            elaborated-type-specifier.  */
4657         type = cp_parser_elaborated_type_specifier (parser,
4658                                                     /*is_friend=*/false,
4659                                                     /*is_declaration=*/false);
4660         postfix_expression = cp_parser_functional_cast (parser, type);
4661       }
4662       break;
4663
4664     default:
4665       {
4666         tree type;
4667
4668         /* If the next thing is a simple-type-specifier, we may be
4669            looking at a functional cast.  We could also be looking at
4670            an id-expression.  So, we try the functional cast, and if
4671            that doesn't work we fall back to the primary-expression.  */
4672         cp_parser_parse_tentatively (parser);
4673         /* Look for the simple-type-specifier.  */
4674         type = cp_parser_simple_type_specifier (parser,
4675                                                 /*decl_specs=*/NULL,
4676                                                 CP_PARSER_FLAGS_NONE);
4677         /* Parse the cast itself.  */
4678         if (!cp_parser_error_occurred (parser))
4679           postfix_expression
4680             = cp_parser_functional_cast (parser, type);
4681         /* If that worked, we're done.  */
4682         if (cp_parser_parse_definitely (parser))
4683           break;
4684
4685         /* If the functional-cast didn't work out, try a
4686            compound-literal.  */
4687         if (cp_parser_allow_gnu_extensions_p (parser)
4688             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4689           {
4690             VEC(constructor_elt,gc) *initializer_list = NULL;
4691             bool saved_in_type_id_in_expr_p;
4692
4693             cp_parser_parse_tentatively (parser);
4694             /* Consume the `('.  */
4695             cp_lexer_consume_token (parser->lexer);
4696             /* Parse the type.  */
4697             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4698             parser->in_type_id_in_expr_p = true;
4699             type = cp_parser_type_id (parser);
4700             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4701             /* Look for the `)'.  */
4702             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4703             /* Look for the `{'.  */
4704             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4705             /* If things aren't going well, there's no need to
4706                keep going.  */
4707             if (!cp_parser_error_occurred (parser))
4708               {
4709                 bool non_constant_p;
4710                 /* Parse the initializer-list.  */
4711                 initializer_list
4712                   = cp_parser_initializer_list (parser, &non_constant_p);
4713                 /* Allow a trailing `,'.  */
4714                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4715                   cp_lexer_consume_token (parser->lexer);
4716                 /* Look for the final `}'.  */
4717                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4718               }
4719             /* If that worked, we're definitely looking at a
4720                compound-literal expression.  */
4721             if (cp_parser_parse_definitely (parser))
4722               {
4723                 /* Warn the user that a compound literal is not
4724                    allowed in standard C++.  */
4725                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4726                 /* For simplicity, we disallow compound literals in
4727                    constant-expressions.  We could
4728                    allow compound literals of integer type, whose
4729                    initializer was a constant, in constant
4730                    expressions.  Permitting that usage, as a further
4731                    extension, would not change the meaning of any
4732                    currently accepted programs.  (Of course, as
4733                    compound literals are not part of ISO C++, the
4734                    standard has nothing to say.)  */
4735                 if (cp_parser_non_integral_constant_expression 
4736                     (parser, "non-constant compound literals"))
4737                   {
4738                     postfix_expression = error_mark_node;
4739                     break;
4740                   }
4741                 /* Form the representation of the compound-literal.  */
4742                 postfix_expression
4743                   = (finish_compound_literal
4744                      (type, build_constructor (init_list_type_node,
4745                                                initializer_list)));
4746                 break;
4747               }
4748           }
4749
4750         /* It must be a primary-expression.  */
4751         postfix_expression
4752           = cp_parser_primary_expression (parser, address_p, cast_p,
4753                                           /*template_arg_p=*/false,
4754                                           &idk);
4755       }
4756       break;
4757     }
4758
4759   /* Keep looping until the postfix-expression is complete.  */
4760   while (true)
4761     {
4762       if (idk == CP_ID_KIND_UNQUALIFIED
4763           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4764           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4765         /* It is not a Koenig lookup function call.  */
4766         postfix_expression
4767           = unqualified_name_lookup_error (postfix_expression);
4768
4769       /* Peek at the next token.  */
4770       token = cp_lexer_peek_token (parser->lexer);
4771
4772       switch (token->type)
4773         {
4774         case CPP_OPEN_SQUARE:
4775           postfix_expression
4776             = cp_parser_postfix_open_square_expression (parser,
4777                                                         postfix_expression,
4778                                                         false);
4779           idk = CP_ID_KIND_NONE;
4780           is_member_access = false;
4781           break;
4782
4783         case CPP_OPEN_PAREN:
4784           /* postfix-expression ( expression-list [opt] ) */
4785           {
4786             bool koenig_p;
4787             bool is_builtin_constant_p;
4788             bool saved_integral_constant_expression_p = false;
4789             bool saved_non_integral_constant_expression_p = false;
4790             VEC(tree,gc) *args;
4791
4792             is_member_access = false;
4793
4794             is_builtin_constant_p
4795               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4796             if (is_builtin_constant_p)
4797               {
4798                 /* The whole point of __builtin_constant_p is to allow
4799                    non-constant expressions to appear as arguments.  */
4800                 saved_integral_constant_expression_p
4801                   = parser->integral_constant_expression_p;
4802                 saved_non_integral_constant_expression_p
4803                   = parser->non_integral_constant_expression_p;
4804                 parser->integral_constant_expression_p = false;
4805               }
4806             args = (cp_parser_parenthesized_expression_list
4807                     (parser, /*is_attribute_list=*/false,
4808                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4809                      /*non_constant_p=*/NULL));
4810             if (is_builtin_constant_p)
4811               {
4812                 parser->integral_constant_expression_p
4813                   = saved_integral_constant_expression_p;
4814                 parser->non_integral_constant_expression_p
4815                   = saved_non_integral_constant_expression_p;
4816               }
4817
4818             if (args == NULL)
4819               {
4820                 postfix_expression = error_mark_node;
4821                 break;
4822               }
4823
4824             /* Function calls are not permitted in
4825                constant-expressions.  */
4826             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4827                 && cp_parser_non_integral_constant_expression (parser,
4828                                                                "a function call"))
4829               {
4830                 postfix_expression = error_mark_node;
4831                 release_tree_vector (args);
4832                 break;
4833               }
4834
4835             koenig_p = false;
4836             if (idk == CP_ID_KIND_UNQUALIFIED
4837                 || idk == CP_ID_KIND_TEMPLATE_ID)
4838               {
4839                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4840                   {
4841                     if (!VEC_empty (tree, args))
4842                       {
4843                         koenig_p = true;
4844                         if (!any_type_dependent_arguments_p (args))
4845                           postfix_expression
4846                             = perform_koenig_lookup (postfix_expression, args);
4847                       }
4848                     else
4849                       postfix_expression
4850                         = unqualified_fn_lookup_error (postfix_expression);
4851                   }
4852                 /* We do not perform argument-dependent lookup if
4853                    normal lookup finds a non-function, in accordance
4854                    with the expected resolution of DR 218.  */
4855                 else if (!VEC_empty (tree, args)
4856                          && is_overloaded_fn (postfix_expression))
4857                   {
4858                     tree fn = get_first_fn (postfix_expression);
4859                     fn = STRIP_TEMPLATE (fn);
4860
4861                     /* Do not do argument dependent lookup if regular
4862                        lookup finds a member function or a block-scope
4863                        function declaration.  [basic.lookup.argdep]/3  */
4864                     if (!DECL_FUNCTION_MEMBER_P (fn)
4865                         && !DECL_LOCAL_FUNCTION_P (fn))
4866                       {
4867                         koenig_p = true;
4868                         if (!any_type_dependent_arguments_p (args))
4869                           postfix_expression
4870                             = perform_koenig_lookup (postfix_expression, args);
4871                       }
4872                   }
4873               }
4874
4875             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4876               {
4877                 tree instance = TREE_OPERAND (postfix_expression, 0);
4878                 tree fn = TREE_OPERAND (postfix_expression, 1);
4879
4880                 if (processing_template_decl
4881                     && (type_dependent_expression_p (instance)
4882                         || (!BASELINK_P (fn)
4883                             && TREE_CODE (fn) != FIELD_DECL)
4884                         || type_dependent_expression_p (fn)
4885                         || any_type_dependent_arguments_p (args)))
4886                   {
4887                     postfix_expression
4888                       = build_nt_call_vec (postfix_expression, args);
4889                     release_tree_vector (args);
4890                     break;
4891                   }
4892
4893                 if (BASELINK_P (fn))
4894                   {
4895                   postfix_expression
4896                     = (build_new_method_call
4897                        (instance, fn, &args, NULL_TREE,
4898                         (idk == CP_ID_KIND_QUALIFIED
4899                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4900                         /*fn_p=*/NULL,
4901                         tf_warning_or_error));
4902                   }
4903                 else
4904                   postfix_expression
4905                     = finish_call_expr (postfix_expression, &args,
4906                                         /*disallow_virtual=*/false,
4907                                         /*koenig_p=*/false,
4908                                         tf_warning_or_error);
4909               }
4910             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4911                      || TREE_CODE (postfix_expression) == MEMBER_REF
4912                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4913               postfix_expression = (build_offset_ref_call_from_tree
4914                                     (postfix_expression, &args));
4915             else if (idk == CP_ID_KIND_QUALIFIED)
4916               /* A call to a static class member, or a namespace-scope
4917                  function.  */
4918               postfix_expression
4919                 = finish_call_expr (postfix_expression, &args,
4920                                     /*disallow_virtual=*/true,
4921                                     koenig_p,
4922                                     tf_warning_or_error);
4923             else
4924               /* All other function calls.  */
4925               postfix_expression
4926                 = finish_call_expr (postfix_expression, &args,
4927                                     /*disallow_virtual=*/false,
4928                                     koenig_p,
4929                                     tf_warning_or_error);
4930
4931             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4932             idk = CP_ID_KIND_NONE;
4933
4934             release_tree_vector (args);
4935           }
4936           break;
4937
4938         case CPP_DOT:
4939         case CPP_DEREF:
4940           /* postfix-expression . template [opt] id-expression
4941              postfix-expression . pseudo-destructor-name
4942              postfix-expression -> template [opt] id-expression
4943              postfix-expression -> pseudo-destructor-name */
4944
4945           /* Consume the `.' or `->' operator.  */
4946           cp_lexer_consume_token (parser->lexer);
4947
4948           postfix_expression
4949             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4950                                                       postfix_expression,
4951                                                       false, &idk,
4952                                                       token->location);
4953
4954           is_member_access = true;
4955           break;
4956
4957         case CPP_PLUS_PLUS:
4958           /* postfix-expression ++  */
4959           /* Consume the `++' token.  */
4960           cp_lexer_consume_token (parser->lexer);
4961           /* Generate a representation for the complete expression.  */
4962           postfix_expression
4963             = finish_increment_expr (postfix_expression,
4964                                      POSTINCREMENT_EXPR);
4965           /* Increments may not appear in constant-expressions.  */
4966           if (cp_parser_non_integral_constant_expression (parser,
4967                                                           "an increment"))
4968             postfix_expression = error_mark_node;
4969           idk = CP_ID_KIND_NONE;
4970           is_member_access = false;
4971           break;
4972
4973         case CPP_MINUS_MINUS:
4974           /* postfix-expression -- */
4975           /* Consume the `--' token.  */
4976           cp_lexer_consume_token (parser->lexer);
4977           /* Generate a representation for the complete expression.  */
4978           postfix_expression
4979             = finish_increment_expr (postfix_expression,
4980                                      POSTDECREMENT_EXPR);
4981           /* Decrements may not appear in constant-expressions.  */
4982           if (cp_parser_non_integral_constant_expression (parser,
4983                                                           "a decrement"))
4984             postfix_expression = error_mark_node;
4985           idk = CP_ID_KIND_NONE;
4986           is_member_access = false;
4987           break;
4988
4989         default:
4990           if (pidk_return != NULL)
4991             * pidk_return = idk;
4992           if (member_access_only_p)
4993             return is_member_access? postfix_expression : error_mark_node;
4994           else
4995             return postfix_expression;
4996         }
4997     }
4998
4999   /* We should never get here.  */
5000   gcc_unreachable ();
5001   return error_mark_node;
5002 }
5003
5004 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5005    by cp_parser_builtin_offsetof.  We're looking for
5006
5007      postfix-expression [ expression ]
5008
5009    FOR_OFFSETOF is set if we're being called in that context, which
5010    changes how we deal with integer constant expressions.  */
5011
5012 static tree
5013 cp_parser_postfix_open_square_expression (cp_parser *parser,
5014                                           tree postfix_expression,
5015                                           bool for_offsetof)
5016 {
5017   tree index;
5018
5019   /* Consume the `[' token.  */
5020   cp_lexer_consume_token (parser->lexer);
5021
5022   /* Parse the index expression.  */
5023   /* ??? For offsetof, there is a question of what to allow here.  If
5024      offsetof is not being used in an integral constant expression context,
5025      then we *could* get the right answer by computing the value at runtime.
5026      If we are in an integral constant expression context, then we might
5027      could accept any constant expression; hard to say without analysis.
5028      Rather than open the barn door too wide right away, allow only integer
5029      constant expressions here.  */
5030   if (for_offsetof)
5031     index = cp_parser_constant_expression (parser, false, NULL);
5032   else
5033     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5034
5035   /* Look for the closing `]'.  */
5036   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5037
5038   /* Build the ARRAY_REF.  */
5039   postfix_expression = grok_array_decl (postfix_expression, index);
5040
5041   /* When not doing offsetof, array references are not permitted in
5042      constant-expressions.  */
5043   if (!for_offsetof
5044       && (cp_parser_non_integral_constant_expression
5045           (parser, "an array reference")))
5046     postfix_expression = error_mark_node;
5047
5048   return postfix_expression;
5049 }
5050
5051 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5052    by cp_parser_builtin_offsetof.  We're looking for
5053
5054      postfix-expression . template [opt] id-expression
5055      postfix-expression . pseudo-destructor-name
5056      postfix-expression -> template [opt] id-expression
5057      postfix-expression -> pseudo-destructor-name
5058
5059    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5060    limits what of the above we'll actually accept, but nevermind.
5061    TOKEN_TYPE is the "." or "->" token, which will already have been
5062    removed from the stream.  */
5063
5064 static tree
5065 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5066                                         enum cpp_ttype token_type,
5067                                         tree postfix_expression,
5068                                         bool for_offsetof, cp_id_kind *idk,
5069                                         location_t location)
5070 {
5071   tree name;
5072   bool dependent_p;
5073   bool pseudo_destructor_p;
5074   tree scope = NULL_TREE;
5075
5076   /* If this is a `->' operator, dereference the pointer.  */
5077   if (token_type == CPP_DEREF)
5078     postfix_expression = build_x_arrow (postfix_expression);
5079   /* Check to see whether or not the expression is type-dependent.  */
5080   dependent_p = type_dependent_expression_p (postfix_expression);
5081   /* The identifier following the `->' or `.' is not qualified.  */
5082   parser->scope = NULL_TREE;
5083   parser->qualifying_scope = NULL_TREE;
5084   parser->object_scope = NULL_TREE;
5085   *idk = CP_ID_KIND_NONE;
5086
5087   /* Enter the scope corresponding to the type of the object
5088      given by the POSTFIX_EXPRESSION.  */
5089   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5090     {
5091       scope = TREE_TYPE (postfix_expression);
5092       /* According to the standard, no expression should ever have
5093          reference type.  Unfortunately, we do not currently match
5094          the standard in this respect in that our internal representation
5095          of an expression may have reference type even when the standard
5096          says it does not.  Therefore, we have to manually obtain the
5097          underlying type here.  */
5098       scope = non_reference (scope);
5099       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5100       if (scope == unknown_type_node)
5101         {
5102           error_at (location, "%qE does not have class type",
5103                     postfix_expression);
5104           scope = NULL_TREE;
5105         }
5106       else
5107         scope = complete_type_or_else (scope, NULL_TREE);
5108       /* Let the name lookup machinery know that we are processing a
5109          class member access expression.  */
5110       parser->context->object_type = scope;
5111       /* If something went wrong, we want to be able to discern that case,
5112          as opposed to the case where there was no SCOPE due to the type
5113          of expression being dependent.  */
5114       if (!scope)
5115         scope = error_mark_node;
5116       /* If the SCOPE was erroneous, make the various semantic analysis
5117          functions exit quickly -- and without issuing additional error
5118          messages.  */
5119       if (scope == error_mark_node)
5120         postfix_expression = error_mark_node;
5121     }
5122
5123   /* Assume this expression is not a pseudo-destructor access.  */
5124   pseudo_destructor_p = false;
5125
5126   /* If the SCOPE is a scalar type, then, if this is a valid program,
5127      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5128      is type dependent, it can be pseudo-destructor-name or something else.
5129      Try to parse it as pseudo-destructor-name first.  */
5130   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5131     {
5132       tree s;
5133       tree type;
5134
5135       cp_parser_parse_tentatively (parser);
5136       /* Parse the pseudo-destructor-name.  */
5137       s = NULL_TREE;
5138       cp_parser_pseudo_destructor_name (parser, &s, &type);
5139       if (dependent_p
5140           && (cp_parser_error_occurred (parser)
5141               || TREE_CODE (type) != TYPE_DECL
5142               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5143         cp_parser_abort_tentative_parse (parser);
5144       else if (cp_parser_parse_definitely (parser))
5145         {
5146           pseudo_destructor_p = true;
5147           postfix_expression
5148             = finish_pseudo_destructor_expr (postfix_expression,
5149                                              s, TREE_TYPE (type));
5150         }
5151     }
5152
5153   if (!pseudo_destructor_p)
5154     {
5155       /* If the SCOPE is not a scalar type, we are looking at an
5156          ordinary class member access expression, rather than a
5157          pseudo-destructor-name.  */
5158       bool template_p;
5159       cp_token *token = cp_lexer_peek_token (parser->lexer);
5160       /* Parse the id-expression.  */
5161       name = (cp_parser_id_expression
5162               (parser,
5163                cp_parser_optional_template_keyword (parser),
5164                /*check_dependency_p=*/true,
5165                &template_p,
5166                /*declarator_p=*/false,
5167                /*optional_p=*/false));
5168       /* In general, build a SCOPE_REF if the member name is qualified.
5169          However, if the name was not dependent and has already been
5170          resolved; there is no need to build the SCOPE_REF.  For example;
5171
5172              struct X { void f(); };
5173              template <typename T> void f(T* t) { t->X::f(); }
5174
5175          Even though "t" is dependent, "X::f" is not and has been resolved
5176          to a BASELINK; there is no need to include scope information.  */
5177
5178       /* But we do need to remember that there was an explicit scope for
5179          virtual function calls.  */
5180       if (parser->scope)
5181         *idk = CP_ID_KIND_QUALIFIED;
5182
5183       /* If the name is a template-id that names a type, we will get a
5184          TYPE_DECL here.  That is invalid code.  */
5185       if (TREE_CODE (name) == TYPE_DECL)
5186         {
5187           error_at (token->location, "invalid use of %qD", name);
5188           postfix_expression = error_mark_node;
5189         }
5190       else
5191         {
5192           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5193             {
5194               name = build_qualified_name (/*type=*/NULL_TREE,
5195                                            parser->scope,
5196                                            name,
5197                                            template_p);
5198               parser->scope = NULL_TREE;
5199               parser->qualifying_scope = NULL_TREE;
5200               parser->object_scope = NULL_TREE;
5201             }
5202           if (scope && name && BASELINK_P (name))
5203             adjust_result_of_qualified_name_lookup
5204               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5205           postfix_expression
5206             = finish_class_member_access_expr (postfix_expression, name,
5207                                                template_p, 
5208                                                tf_warning_or_error);
5209         }
5210     }
5211
5212   /* We no longer need to look up names in the scope of the object on
5213      the left-hand side of the `.' or `->' operator.  */
5214   parser->context->object_type = NULL_TREE;
5215
5216   /* Outside of offsetof, these operators may not appear in
5217      constant-expressions.  */
5218   if (!for_offsetof
5219       && (cp_parser_non_integral_constant_expression
5220           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5221     postfix_expression = error_mark_node;
5222
5223   return postfix_expression;
5224 }
5225
5226 /* Parse a parenthesized expression-list.
5227
5228    expression-list:
5229      assignment-expression
5230      expression-list, assignment-expression
5231
5232    attribute-list:
5233      expression-list
5234      identifier
5235      identifier, expression-list
5236
5237    CAST_P is true if this expression is the target of a cast.
5238
5239    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5240    argument pack.
5241
5242    Returns a vector of trees.  Each element is a representation of an
5243    assignment-expression.  NULL is returned if the ( and or ) are
5244    missing.  An empty, but allocated, vector is returned on no
5245    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5246    if this is really an attribute list being parsed.  If
5247    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5248    not all of the expressions in the list were constant.  */
5249
5250 static VEC(tree,gc) *
5251 cp_parser_parenthesized_expression_list (cp_parser* parser,
5252                                          bool is_attribute_list,
5253                                          bool cast_p,
5254                                          bool allow_expansion_p,
5255                                          bool *non_constant_p)
5256 {
5257   VEC(tree,gc) *expression_list;
5258   bool fold_expr_p = is_attribute_list;
5259   tree identifier = NULL_TREE;
5260   bool saved_greater_than_is_operator_p;
5261
5262   /* Assume all the expressions will be constant.  */
5263   if (non_constant_p)
5264     *non_constant_p = false;
5265
5266   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5267     return NULL;
5268
5269   expression_list = make_tree_vector ();
5270
5271   /* Within a parenthesized expression, a `>' token is always
5272      the greater-than operator.  */
5273   saved_greater_than_is_operator_p
5274     = parser->greater_than_is_operator_p;
5275   parser->greater_than_is_operator_p = true;
5276
5277   /* Consume expressions until there are no more.  */
5278   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5279     while (true)
5280       {
5281         tree expr;
5282
5283         /* At the beginning of attribute lists, check to see if the
5284            next token is an identifier.  */
5285         if (is_attribute_list
5286             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5287           {
5288             cp_token *token;
5289
5290             /* Consume the identifier.  */
5291             token = cp_lexer_consume_token (parser->lexer);
5292             /* Save the identifier.  */
5293             identifier = token->u.value;
5294           }
5295         else
5296           {
5297             bool expr_non_constant_p;
5298
5299             /* Parse the next assignment-expression.  */
5300             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5301               {
5302                 /* A braced-init-list.  */
5303                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5304                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5305                 if (non_constant_p && expr_non_constant_p)
5306                   *non_constant_p = true;
5307               }
5308             else if (non_constant_p)
5309               {
5310                 expr = (cp_parser_constant_expression
5311                         (parser, /*allow_non_constant_p=*/true,
5312                          &expr_non_constant_p));
5313                 if (expr_non_constant_p)
5314                   *non_constant_p = true;
5315               }
5316             else
5317               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5318
5319             if (fold_expr_p)
5320               expr = fold_non_dependent_expr (expr);
5321
5322             /* If we have an ellipsis, then this is an expression
5323                expansion.  */
5324             if (allow_expansion_p
5325                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5326               {
5327                 /* Consume the `...'.  */
5328                 cp_lexer_consume_token (parser->lexer);
5329
5330                 /* Build the argument pack.  */
5331                 expr = make_pack_expansion (expr);
5332               }
5333
5334              /* Add it to the list.  We add error_mark_node
5335                 expressions to the list, so that we can still tell if
5336                 the correct form for a parenthesized expression-list
5337                 is found. That gives better errors.  */
5338             VEC_safe_push (tree, gc, expression_list, expr);
5339
5340             if (expr == error_mark_node)
5341               goto skip_comma;
5342           }
5343
5344         /* After the first item, attribute lists look the same as
5345            expression lists.  */
5346         is_attribute_list = false;
5347
5348       get_comma:;
5349         /* If the next token isn't a `,', then we are done.  */
5350         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5351           break;
5352
5353         /* Otherwise, consume the `,' and keep going.  */
5354         cp_lexer_consume_token (parser->lexer);
5355       }
5356
5357   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5358     {
5359       int ending;
5360
5361     skip_comma:;
5362       /* We try and resync to an unnested comma, as that will give the
5363          user better diagnostics.  */
5364       ending = cp_parser_skip_to_closing_parenthesis (parser,
5365                                                       /*recovering=*/true,
5366                                                       /*or_comma=*/true,
5367                                                       /*consume_paren=*/true);
5368       if (ending < 0)
5369         goto get_comma;
5370       if (!ending)
5371         {
5372           parser->greater_than_is_operator_p
5373             = saved_greater_than_is_operator_p;
5374           return NULL;
5375         }
5376     }
5377
5378   parser->greater_than_is_operator_p
5379     = saved_greater_than_is_operator_p;
5380
5381   if (identifier)
5382     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5383
5384   return expression_list;
5385 }
5386
5387 /* Parse a pseudo-destructor-name.
5388
5389    pseudo-destructor-name:
5390      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5391      :: [opt] nested-name-specifier template template-id :: ~ type-name
5392      :: [opt] nested-name-specifier [opt] ~ type-name
5393
5394    If either of the first two productions is used, sets *SCOPE to the
5395    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5396    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5397    or ERROR_MARK_NODE if the parse fails.  */
5398
5399 static void
5400 cp_parser_pseudo_destructor_name (cp_parser* parser,
5401                                   tree* scope,
5402                                   tree* type)
5403 {
5404   bool nested_name_specifier_p;
5405
5406   /* Assume that things will not work out.  */
5407   *type = error_mark_node;
5408
5409   /* Look for the optional `::' operator.  */
5410   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5411   /* Look for the optional nested-name-specifier.  */
5412   nested_name_specifier_p
5413     = (cp_parser_nested_name_specifier_opt (parser,
5414                                             /*typename_keyword_p=*/false,
5415                                             /*check_dependency_p=*/true,
5416                                             /*type_p=*/false,
5417                                             /*is_declaration=*/false)
5418        != NULL_TREE);
5419   /* Now, if we saw a nested-name-specifier, we might be doing the
5420      second production.  */
5421   if (nested_name_specifier_p
5422       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5423     {
5424       /* Consume the `template' keyword.  */
5425       cp_lexer_consume_token (parser->lexer);
5426       /* Parse the template-id.  */
5427       cp_parser_template_id (parser,
5428                              /*template_keyword_p=*/true,
5429                              /*check_dependency_p=*/false,
5430                              /*is_declaration=*/true);
5431       /* Look for the `::' token.  */
5432       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5433     }
5434   /* If the next token is not a `~', then there might be some
5435      additional qualification.  */
5436   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5437     {
5438       /* At this point, we're looking for "type-name :: ~".  The type-name
5439          must not be a class-name, since this is a pseudo-destructor.  So,
5440          it must be either an enum-name, or a typedef-name -- both of which
5441          are just identifiers.  So, we peek ahead to check that the "::"
5442          and "~" tokens are present; if they are not, then we can avoid
5443          calling type_name.  */
5444       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5445           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5446           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5447         {
5448           cp_parser_error (parser, "non-scalar type");
5449           return;
5450         }
5451
5452       /* Look for the type-name.  */
5453       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5454       if (*scope == error_mark_node)
5455         return;
5456
5457       /* Look for the `::' token.  */
5458       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5459     }
5460   else
5461     *scope = NULL_TREE;
5462
5463   /* Look for the `~'.  */
5464   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5465   /* Look for the type-name again.  We are not responsible for
5466      checking that it matches the first type-name.  */
5467   *type = cp_parser_nonclass_name (parser);
5468 }
5469
5470 /* Parse a unary-expression.
5471
5472    unary-expression:
5473      postfix-expression
5474      ++ cast-expression
5475      -- cast-expression
5476      unary-operator cast-expression
5477      sizeof unary-expression
5478      sizeof ( type-id )
5479      new-expression
5480      delete-expression
5481
5482    GNU Extensions:
5483
5484    unary-expression:
5485      __extension__ cast-expression
5486      __alignof__ unary-expression
5487      __alignof__ ( type-id )
5488      __real__ cast-expression
5489      __imag__ cast-expression
5490      && identifier
5491
5492    ADDRESS_P is true iff the unary-expression is appearing as the
5493    operand of the `&' operator.   CAST_P is true if this expression is
5494    the target of a cast.
5495
5496    Returns a representation of the expression.  */
5497
5498 static tree
5499 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5500                             cp_id_kind * pidk)
5501 {
5502   cp_token *token;
5503   enum tree_code unary_operator;
5504
5505   /* Peek at the next token.  */
5506   token = cp_lexer_peek_token (parser->lexer);
5507   /* Some keywords give away the kind of expression.  */
5508   if (token->type == CPP_KEYWORD)
5509     {
5510       enum rid keyword = token->keyword;
5511
5512       switch (keyword)
5513         {
5514         case RID_ALIGNOF:
5515         case RID_SIZEOF:
5516           {
5517             tree operand;
5518             enum tree_code op;
5519
5520             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5521             /* Consume the token.  */
5522             cp_lexer_consume_token (parser->lexer);
5523             /* Parse the operand.  */
5524             operand = cp_parser_sizeof_operand (parser, keyword);
5525
5526             if (TYPE_P (operand))
5527               return cxx_sizeof_or_alignof_type (operand, op, true);
5528             else
5529               return cxx_sizeof_or_alignof_expr (operand, op, true);
5530           }
5531
5532         case RID_NEW:
5533           return cp_parser_new_expression (parser);
5534
5535         case RID_DELETE:
5536           return cp_parser_delete_expression (parser);
5537
5538         case RID_EXTENSION:
5539           {
5540             /* The saved value of the PEDANTIC flag.  */
5541             int saved_pedantic;
5542             tree expr;
5543
5544             /* Save away the PEDANTIC flag.  */
5545             cp_parser_extension_opt (parser, &saved_pedantic);
5546             /* Parse the cast-expression.  */
5547             expr = cp_parser_simple_cast_expression (parser);
5548             /* Restore the PEDANTIC flag.  */
5549             pedantic = saved_pedantic;
5550
5551             return expr;
5552           }
5553
5554         case RID_REALPART:
5555         case RID_IMAGPART:
5556           {
5557             tree expression;
5558
5559             /* Consume the `__real__' or `__imag__' token.  */
5560             cp_lexer_consume_token (parser->lexer);
5561             /* Parse the cast-expression.  */
5562             expression = cp_parser_simple_cast_expression (parser);
5563             /* Create the complete representation.  */
5564             return build_x_unary_op ((keyword == RID_REALPART
5565                                       ? REALPART_EXPR : IMAGPART_EXPR),
5566                                      expression,
5567                                      tf_warning_or_error);
5568           }
5569           break;
5570
5571         default:
5572           break;
5573         }
5574     }
5575
5576   /* Look for the `:: new' and `:: delete', which also signal the
5577      beginning of a new-expression, or delete-expression,
5578      respectively.  If the next token is `::', then it might be one of
5579      these.  */
5580   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5581     {
5582       enum rid keyword;
5583
5584       /* See if the token after the `::' is one of the keywords in
5585          which we're interested.  */
5586       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5587       /* If it's `new', we have a new-expression.  */
5588       if (keyword == RID_NEW)
5589         return cp_parser_new_expression (parser);
5590       /* Similarly, for `delete'.  */
5591       else if (keyword == RID_DELETE)
5592         return cp_parser_delete_expression (parser);
5593     }
5594
5595   /* Look for a unary operator.  */
5596   unary_operator = cp_parser_unary_operator (token);
5597   /* The `++' and `--' operators can be handled similarly, even though
5598      they are not technically unary-operators in the grammar.  */
5599   if (unary_operator == ERROR_MARK)
5600     {
5601       if (token->type == CPP_PLUS_PLUS)
5602         unary_operator = PREINCREMENT_EXPR;
5603       else if (token->type == CPP_MINUS_MINUS)
5604         unary_operator = PREDECREMENT_EXPR;
5605       /* Handle the GNU address-of-label extension.  */
5606       else if (cp_parser_allow_gnu_extensions_p (parser)
5607                && token->type == CPP_AND_AND)
5608         {
5609           tree identifier;
5610           tree expression;
5611           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5612
5613           /* Consume the '&&' token.  */
5614           cp_lexer_consume_token (parser->lexer);
5615           /* Look for the identifier.  */
5616           identifier = cp_parser_identifier (parser);
5617           /* Create an expression representing the address.  */
5618           expression = finish_label_address_expr (identifier, loc);
5619           if (cp_parser_non_integral_constant_expression (parser,
5620                                                 "the address of a label"))
5621             expression = error_mark_node;
5622           return expression;
5623         }
5624     }
5625   if (unary_operator != ERROR_MARK)
5626     {
5627       tree cast_expression;
5628       tree expression = error_mark_node;
5629       const char *non_constant_p = NULL;
5630
5631       /* Consume the operator token.  */
5632       token = cp_lexer_consume_token (parser->lexer);
5633       /* Parse the cast-expression.  */
5634       cast_expression
5635         = cp_parser_cast_expression (parser,
5636                                      unary_operator == ADDR_EXPR,
5637                                      /*cast_p=*/false, pidk);
5638       /* Now, build an appropriate representation.  */
5639       switch (unary_operator)
5640         {
5641         case INDIRECT_REF:
5642           non_constant_p = "%<*%>";
5643           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5644                                              tf_warning_or_error);
5645           break;
5646
5647         case ADDR_EXPR:
5648           non_constant_p = "%<&%>";
5649           /* Fall through.  */
5650         case BIT_NOT_EXPR:
5651           expression = build_x_unary_op (unary_operator, cast_expression,
5652                                          tf_warning_or_error);
5653           break;
5654
5655         case PREINCREMENT_EXPR:
5656         case PREDECREMENT_EXPR:
5657           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5658                             ? "%<++%>" : "%<--%>");
5659           /* Fall through.  */
5660         case UNARY_PLUS_EXPR:
5661         case NEGATE_EXPR:
5662         case TRUTH_NOT_EXPR:
5663           expression = finish_unary_op_expr (unary_operator, cast_expression);
5664           break;
5665
5666         default:
5667           gcc_unreachable ();
5668         }
5669
5670       if (non_constant_p
5671           && cp_parser_non_integral_constant_expression (parser,
5672                                                          non_constant_p))
5673         expression = error_mark_node;
5674
5675       return expression;
5676     }
5677
5678   return cp_parser_postfix_expression (parser, address_p, cast_p,
5679                                        /*member_access_only_p=*/false,
5680                                        pidk);
5681 }
5682
5683 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5684    unary-operator, the corresponding tree code is returned.  */
5685
5686 static enum tree_code
5687 cp_parser_unary_operator (cp_token* token)
5688 {
5689   switch (token->type)
5690     {
5691     case CPP_MULT:
5692       return INDIRECT_REF;
5693
5694     case CPP_AND:
5695       return ADDR_EXPR;
5696
5697     case CPP_PLUS:
5698       return UNARY_PLUS_EXPR;
5699
5700     case CPP_MINUS:
5701       return NEGATE_EXPR;
5702
5703     case CPP_NOT:
5704       return TRUTH_NOT_EXPR;
5705
5706     case CPP_COMPL:
5707       return BIT_NOT_EXPR;
5708
5709     default:
5710       return ERROR_MARK;
5711     }
5712 }
5713
5714 /* Parse a new-expression.
5715
5716    new-expression:
5717      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5718      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5719
5720    Returns a representation of the expression.  */
5721
5722 static tree
5723 cp_parser_new_expression (cp_parser* parser)
5724 {
5725   bool global_scope_p;
5726   VEC(tree,gc) *placement;
5727   tree type;
5728   VEC(tree,gc) *initializer;
5729   tree nelts;
5730   tree ret;
5731
5732   /* Look for the optional `::' operator.  */
5733   global_scope_p
5734     = (cp_parser_global_scope_opt (parser,
5735                                    /*current_scope_valid_p=*/false)
5736        != NULL_TREE);
5737   /* Look for the `new' operator.  */
5738   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5739   /* There's no easy way to tell a new-placement from the
5740      `( type-id )' construct.  */
5741   cp_parser_parse_tentatively (parser);
5742   /* Look for a new-placement.  */
5743   placement = cp_parser_new_placement (parser);
5744   /* If that didn't work out, there's no new-placement.  */
5745   if (!cp_parser_parse_definitely (parser))
5746     {
5747       if (placement != NULL)
5748         release_tree_vector (placement);
5749       placement = NULL;
5750     }
5751
5752   /* If the next token is a `(', then we have a parenthesized
5753      type-id.  */
5754   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5755     {
5756       cp_token *token;
5757       /* Consume the `('.  */
5758       cp_lexer_consume_token (parser->lexer);
5759       /* Parse the type-id.  */
5760       type = cp_parser_type_id (parser);
5761       /* Look for the closing `)'.  */
5762       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5763       token = cp_lexer_peek_token (parser->lexer);
5764       /* There should not be a direct-new-declarator in this production,
5765          but GCC used to allowed this, so we check and emit a sensible error
5766          message for this case.  */
5767       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5768         {
5769           error_at (token->location,
5770                     "array bound forbidden after parenthesized type-id");
5771           inform (token->location, 
5772                   "try removing the parentheses around the type-id");
5773           cp_parser_direct_new_declarator (parser);
5774         }
5775       nelts = NULL_TREE;
5776     }
5777   /* Otherwise, there must be a new-type-id.  */
5778   else
5779     type = cp_parser_new_type_id (parser, &nelts);
5780
5781   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5782   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5783       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5784     initializer = cp_parser_new_initializer (parser);
5785   else
5786     initializer = NULL;
5787
5788   /* A new-expression may not appear in an integral constant
5789      expression.  */
5790   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5791     ret = error_mark_node;
5792   else
5793     {
5794       /* Create a representation of the new-expression.  */
5795       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5796                        tf_warning_or_error);
5797     }
5798
5799   if (placement != NULL)
5800     release_tree_vector (placement);
5801   if (initializer != NULL)
5802     release_tree_vector (initializer);
5803
5804   return ret;
5805 }
5806
5807 /* Parse a new-placement.
5808
5809    new-placement:
5810      ( expression-list )
5811
5812    Returns the same representation as for an expression-list.  */
5813
5814 static VEC(tree,gc) *
5815 cp_parser_new_placement (cp_parser* parser)
5816 {
5817   VEC(tree,gc) *expression_list;
5818
5819   /* Parse the expression-list.  */
5820   expression_list = (cp_parser_parenthesized_expression_list
5821                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5822                       /*non_constant_p=*/NULL));
5823
5824   return expression_list;
5825 }
5826
5827 /* Parse a new-type-id.
5828
5829    new-type-id:
5830      type-specifier-seq new-declarator [opt]
5831
5832    Returns the TYPE allocated.  If the new-type-id indicates an array
5833    type, *NELTS is set to the number of elements in the last array
5834    bound; the TYPE will not include the last array bound.  */
5835
5836 static tree
5837 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5838 {
5839   cp_decl_specifier_seq type_specifier_seq;
5840   cp_declarator *new_declarator;
5841   cp_declarator *declarator;
5842   cp_declarator *outer_declarator;
5843   const char *saved_message;
5844   tree type;
5845
5846   /* The type-specifier sequence must not contain type definitions.
5847      (It cannot contain declarations of new types either, but if they
5848      are not definitions we will catch that because they are not
5849      complete.)  */
5850   saved_message = parser->type_definition_forbidden_message;
5851   parser->type_definition_forbidden_message
5852     = G_("types may not be defined in a new-type-id");
5853   /* Parse the type-specifier-seq.  */
5854   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
5855                                 /*is_trailing_return=*/false,
5856                                 &type_specifier_seq);
5857   /* Restore the old message.  */
5858   parser->type_definition_forbidden_message = saved_message;
5859   /* Parse the new-declarator.  */
5860   new_declarator = cp_parser_new_declarator_opt (parser);
5861
5862   /* Determine the number of elements in the last array dimension, if
5863      any.  */
5864   *nelts = NULL_TREE;
5865   /* Skip down to the last array dimension.  */
5866   declarator = new_declarator;
5867   outer_declarator = NULL;
5868   while (declarator && (declarator->kind == cdk_pointer
5869                         || declarator->kind == cdk_ptrmem))
5870     {
5871       outer_declarator = declarator;
5872       declarator = declarator->declarator;
5873     }
5874   while (declarator
5875          && declarator->kind == cdk_array
5876          && declarator->declarator
5877          && declarator->declarator->kind == cdk_array)
5878     {
5879       outer_declarator = declarator;
5880       declarator = declarator->declarator;
5881     }
5882
5883   if (declarator && declarator->kind == cdk_array)
5884     {
5885       *nelts = declarator->u.array.bounds;
5886       if (*nelts == error_mark_node)
5887         *nelts = integer_one_node;
5888
5889       if (outer_declarator)
5890         outer_declarator->declarator = declarator->declarator;
5891       else
5892         new_declarator = NULL;
5893     }
5894
5895   type = groktypename (&type_specifier_seq, new_declarator, false);
5896   return type;
5897 }
5898
5899 /* Parse an (optional) new-declarator.
5900
5901    new-declarator:
5902      ptr-operator new-declarator [opt]
5903      direct-new-declarator
5904
5905    Returns the declarator.  */
5906
5907 static cp_declarator *
5908 cp_parser_new_declarator_opt (cp_parser* parser)
5909 {
5910   enum tree_code code;
5911   tree type;
5912   cp_cv_quals cv_quals;
5913
5914   /* We don't know if there's a ptr-operator next, or not.  */
5915   cp_parser_parse_tentatively (parser);
5916   /* Look for a ptr-operator.  */
5917   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5918   /* If that worked, look for more new-declarators.  */
5919   if (cp_parser_parse_definitely (parser))
5920     {
5921       cp_declarator *declarator;
5922
5923       /* Parse another optional declarator.  */
5924       declarator = cp_parser_new_declarator_opt (parser);
5925
5926       return cp_parser_make_indirect_declarator
5927         (code, type, cv_quals, declarator);
5928     }
5929
5930   /* If the next token is a `[', there is a direct-new-declarator.  */
5931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5932     return cp_parser_direct_new_declarator (parser);
5933
5934   return NULL;
5935 }
5936
5937 /* Parse a direct-new-declarator.
5938
5939    direct-new-declarator:
5940      [ expression ]
5941      direct-new-declarator [constant-expression]
5942
5943    */
5944
5945 static cp_declarator *
5946 cp_parser_direct_new_declarator (cp_parser* parser)
5947 {
5948   cp_declarator *declarator = NULL;
5949
5950   while (true)
5951     {
5952       tree expression;
5953
5954       /* Look for the opening `['.  */
5955       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5956       /* The first expression is not required to be constant.  */
5957       if (!declarator)
5958         {
5959           cp_token *token = cp_lexer_peek_token (parser->lexer);
5960           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5961           /* The standard requires that the expression have integral
5962              type.  DR 74 adds enumeration types.  We believe that the
5963              real intent is that these expressions be handled like the
5964              expression in a `switch' condition, which also allows
5965              classes with a single conversion to integral or
5966              enumeration type.  */
5967           if (!processing_template_decl)
5968             {
5969               expression
5970                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5971                                               expression,
5972                                               /*complain=*/true);
5973               if (!expression)
5974                 {
5975                   error_at (token->location,
5976                             "expression in new-declarator must have integral "
5977                             "or enumeration type");
5978                   expression = error_mark_node;
5979                 }
5980             }
5981         }
5982       /* But all the other expressions must be.  */
5983       else
5984         expression
5985           = cp_parser_constant_expression (parser,
5986                                            /*allow_non_constant=*/false,
5987                                            NULL);
5988       /* Look for the closing `]'.  */
5989       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5990
5991       /* Add this bound to the declarator.  */
5992       declarator = make_array_declarator (declarator, expression);
5993
5994       /* If the next token is not a `[', then there are no more
5995          bounds.  */
5996       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5997         break;
5998     }
5999
6000   return declarator;
6001 }
6002
6003 /* Parse a new-initializer.
6004
6005    new-initializer:
6006      ( expression-list [opt] )
6007      braced-init-list
6008
6009    Returns a representation of the expression-list.  */
6010
6011 static VEC(tree,gc) *
6012 cp_parser_new_initializer (cp_parser* parser)
6013 {
6014   VEC(tree,gc) *expression_list;
6015
6016   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6017     {
6018       tree t;
6019       bool expr_non_constant_p;
6020       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6021       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6022       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6023       expression_list = make_tree_vector_single (t);
6024     }
6025   else
6026     expression_list = (cp_parser_parenthesized_expression_list
6027                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
6028                         /*non_constant_p=*/NULL));
6029
6030   return expression_list;
6031 }
6032
6033 /* Parse a delete-expression.
6034
6035    delete-expression:
6036      :: [opt] delete cast-expression
6037      :: [opt] delete [ ] cast-expression
6038
6039    Returns a representation of the expression.  */
6040
6041 static tree
6042 cp_parser_delete_expression (cp_parser* parser)
6043 {
6044   bool global_scope_p;
6045   bool array_p;
6046   tree expression;
6047
6048   /* Look for the optional `::' operator.  */
6049   global_scope_p
6050     = (cp_parser_global_scope_opt (parser,
6051                                    /*current_scope_valid_p=*/false)
6052        != NULL_TREE);
6053   /* Look for the `delete' keyword.  */
6054   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6055   /* See if the array syntax is in use.  */
6056   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6057     {
6058       /* Consume the `[' token.  */
6059       cp_lexer_consume_token (parser->lexer);
6060       /* Look for the `]' token.  */
6061       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6062       /* Remember that this is the `[]' construct.  */
6063       array_p = true;
6064     }
6065   else
6066     array_p = false;
6067
6068   /* Parse the cast-expression.  */
6069   expression = cp_parser_simple_cast_expression (parser);
6070
6071   /* A delete-expression may not appear in an integral constant
6072      expression.  */
6073   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6074     return error_mark_node;
6075
6076   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6077 }
6078
6079 /* Returns true if TOKEN may start a cast-expression and false
6080    otherwise.  */
6081
6082 static bool
6083 cp_parser_token_starts_cast_expression (cp_token *token)
6084 {
6085   switch (token->type)
6086     {
6087     case CPP_COMMA:
6088     case CPP_SEMICOLON:
6089     case CPP_QUERY:
6090     case CPP_COLON:
6091     case CPP_CLOSE_SQUARE:
6092     case CPP_CLOSE_PAREN:
6093     case CPP_CLOSE_BRACE:
6094     case CPP_DOT:
6095     case CPP_DOT_STAR:
6096     case CPP_DEREF:
6097     case CPP_DEREF_STAR:
6098     case CPP_DIV:
6099     case CPP_MOD:
6100     case CPP_LSHIFT:
6101     case CPP_RSHIFT:
6102     case CPP_LESS:
6103     case CPP_GREATER:
6104     case CPP_LESS_EQ:
6105     case CPP_GREATER_EQ:
6106     case CPP_EQ_EQ:
6107     case CPP_NOT_EQ:
6108     case CPP_EQ:
6109     case CPP_MULT_EQ:
6110     case CPP_DIV_EQ:
6111     case CPP_MOD_EQ:
6112     case CPP_PLUS_EQ:
6113     case CPP_MINUS_EQ:
6114     case CPP_RSHIFT_EQ:
6115     case CPP_LSHIFT_EQ:
6116     case CPP_AND_EQ:
6117     case CPP_XOR_EQ:
6118     case CPP_OR_EQ:
6119     case CPP_XOR:
6120     case CPP_OR:
6121     case CPP_OR_OR:
6122     case CPP_EOF:
6123       return false;
6124
6125       /* '[' may start a primary-expression in obj-c++.  */
6126     case CPP_OPEN_SQUARE:
6127       return c_dialect_objc ();
6128
6129     default:
6130       return true;
6131     }
6132 }
6133
6134 /* Parse a cast-expression.
6135
6136    cast-expression:
6137      unary-expression
6138      ( type-id ) cast-expression
6139
6140    ADDRESS_P is true iff the unary-expression is appearing as the
6141    operand of the `&' operator.   CAST_P is true if this expression is
6142    the target of a cast.
6143
6144    Returns a representation of the expression.  */
6145
6146 static tree
6147 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6148                            cp_id_kind * pidk)
6149 {
6150   /* If it's a `(', then we might be looking at a cast.  */
6151   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6152     {
6153       tree type = NULL_TREE;
6154       tree expr = NULL_TREE;
6155       bool compound_literal_p;
6156       const char *saved_message;
6157
6158       /* There's no way to know yet whether or not this is a cast.
6159          For example, `(int (3))' is a unary-expression, while `(int)
6160          3' is a cast.  So, we resort to parsing tentatively.  */
6161       cp_parser_parse_tentatively (parser);
6162       /* Types may not be defined in a cast.  */
6163       saved_message = parser->type_definition_forbidden_message;
6164       parser->type_definition_forbidden_message
6165         = G_("types may not be defined in casts");
6166       /* Consume the `('.  */
6167       cp_lexer_consume_token (parser->lexer);
6168       /* A very tricky bit is that `(struct S) { 3 }' is a
6169          compound-literal (which we permit in C++ as an extension).
6170          But, that construct is not a cast-expression -- it is a
6171          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6172          is legal; if the compound-literal were a cast-expression,
6173          you'd need an extra set of parentheses.)  But, if we parse
6174          the type-id, and it happens to be a class-specifier, then we
6175          will commit to the parse at that point, because we cannot
6176          undo the action that is done when creating a new class.  So,
6177          then we cannot back up and do a postfix-expression.
6178
6179          Therefore, we scan ahead to the closing `)', and check to see
6180          if the token after the `)' is a `{'.  If so, we are not
6181          looking at a cast-expression.
6182
6183          Save tokens so that we can put them back.  */
6184       cp_lexer_save_tokens (parser->lexer);
6185       /* Skip tokens until the next token is a closing parenthesis.
6186          If we find the closing `)', and the next token is a `{', then
6187          we are looking at a compound-literal.  */
6188       compound_literal_p
6189         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6190                                                   /*consume_paren=*/true)
6191            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6192       /* Roll back the tokens we skipped.  */
6193       cp_lexer_rollback_tokens (parser->lexer);
6194       /* If we were looking at a compound-literal, simulate an error
6195          so that the call to cp_parser_parse_definitely below will
6196          fail.  */
6197       if (compound_literal_p)
6198         cp_parser_simulate_error (parser);
6199       else
6200         {
6201           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6202           parser->in_type_id_in_expr_p = true;
6203           /* Look for the type-id.  */
6204           type = cp_parser_type_id (parser);
6205           /* Look for the closing `)'.  */
6206           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6207           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6208         }
6209
6210       /* Restore the saved message.  */
6211       parser->type_definition_forbidden_message = saved_message;
6212
6213       /* At this point this can only be either a cast or a
6214          parenthesized ctor such as `(T ())' that looks like a cast to
6215          function returning T.  */
6216       if (!cp_parser_error_occurred (parser)
6217           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6218                                                      (parser->lexer)))
6219         {
6220           cp_parser_parse_definitely (parser);
6221           expr = cp_parser_cast_expression (parser,
6222                                             /*address_p=*/false,
6223                                             /*cast_p=*/true, pidk);
6224
6225           /* Warn about old-style casts, if so requested.  */
6226           if (warn_old_style_cast
6227               && !in_system_header
6228               && !VOID_TYPE_P (type)
6229               && current_lang_name != lang_name_c)
6230             warning (OPT_Wold_style_cast, "use of old-style cast");
6231
6232           /* Only type conversions to integral or enumeration types
6233              can be used in constant-expressions.  */
6234           if (!cast_valid_in_integral_constant_expression_p (type)
6235               && (cp_parser_non_integral_constant_expression
6236                   (parser,
6237                    "a cast to a type other than an integral or "
6238                    "enumeration type")))
6239             return error_mark_node;
6240
6241           /* Perform the cast.  */
6242           expr = build_c_cast (input_location, type, expr);
6243           return expr;
6244         }
6245       else 
6246         cp_parser_abort_tentative_parse (parser);
6247     }
6248
6249   /* If we get here, then it's not a cast, so it must be a
6250      unary-expression.  */
6251   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6252 }
6253
6254 /* Parse a binary expression of the general form:
6255
6256    pm-expression:
6257      cast-expression
6258      pm-expression .* cast-expression
6259      pm-expression ->* cast-expression
6260
6261    multiplicative-expression:
6262      pm-expression
6263      multiplicative-expression * pm-expression
6264      multiplicative-expression / pm-expression
6265      multiplicative-expression % pm-expression
6266
6267    additive-expression:
6268      multiplicative-expression
6269      additive-expression + multiplicative-expression
6270      additive-expression - multiplicative-expression
6271
6272    shift-expression:
6273      additive-expression
6274      shift-expression << additive-expression
6275      shift-expression >> additive-expression
6276
6277    relational-expression:
6278      shift-expression
6279      relational-expression < shift-expression
6280      relational-expression > shift-expression
6281      relational-expression <= shift-expression
6282      relational-expression >= shift-expression
6283
6284   GNU Extension:
6285
6286    relational-expression:
6287      relational-expression <? shift-expression
6288      relational-expression >? shift-expression
6289
6290    equality-expression:
6291      relational-expression
6292      equality-expression == relational-expression
6293      equality-expression != relational-expression
6294
6295    and-expression:
6296      equality-expression
6297      and-expression & equality-expression
6298
6299    exclusive-or-expression:
6300      and-expression
6301      exclusive-or-expression ^ and-expression
6302
6303    inclusive-or-expression:
6304      exclusive-or-expression
6305      inclusive-or-expression | exclusive-or-expression
6306
6307    logical-and-expression:
6308      inclusive-or-expression
6309      logical-and-expression && inclusive-or-expression
6310
6311    logical-or-expression:
6312      logical-and-expression
6313      logical-or-expression || logical-and-expression
6314
6315    All these are implemented with a single function like:
6316
6317    binary-expression:
6318      simple-cast-expression
6319      binary-expression <token> binary-expression
6320
6321    CAST_P is true if this expression is the target of a cast.
6322
6323    The binops_by_token map is used to get the tree codes for each <token> type.
6324    binary-expressions are associated according to a precedence table.  */
6325
6326 #define TOKEN_PRECEDENCE(token)                              \
6327 (((token->type == CPP_GREATER                                \
6328    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6329   && !parser->greater_than_is_operator_p)                    \
6330  ? PREC_NOT_OPERATOR                                         \
6331  : binops_by_token[token->type].prec)
6332
6333 static tree
6334 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6335                              bool no_toplevel_fold_p,
6336                              enum cp_parser_prec prec,
6337                              cp_id_kind * pidk)
6338 {
6339   cp_parser_expression_stack stack;
6340   cp_parser_expression_stack_entry *sp = &stack[0];
6341   tree lhs, rhs;
6342   cp_token *token;
6343   enum tree_code tree_type, lhs_type, rhs_type;
6344   enum cp_parser_prec new_prec, lookahead_prec;
6345   bool overloaded_p;
6346
6347   /* Parse the first expression.  */
6348   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6349   lhs_type = ERROR_MARK;
6350
6351   for (;;)
6352     {
6353       /* Get an operator token.  */
6354       token = cp_lexer_peek_token (parser->lexer);
6355
6356       if (warn_cxx0x_compat
6357           && token->type == CPP_RSHIFT
6358           && !parser->greater_than_is_operator_p)
6359         {
6360           if (warning_at (token->location, OPT_Wc__0x_compat, 
6361                           "%<>>%> operator will be treated as"
6362                           " two right angle brackets in C++0x"))
6363             inform (token->location,
6364                     "suggest parentheses around %<>>%> expression");
6365         }
6366
6367       new_prec = TOKEN_PRECEDENCE (token);
6368
6369       /* Popping an entry off the stack means we completed a subexpression:
6370          - either we found a token which is not an operator (`>' where it is not
6371            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6372            will happen repeatedly;
6373          - or, we found an operator which has lower priority.  This is the case
6374            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6375            parsing `3 * 4'.  */
6376       if (new_prec <= prec)
6377         {
6378           if (sp == stack)
6379             break;
6380           else
6381             goto pop;
6382         }
6383
6384      get_rhs:
6385       tree_type = binops_by_token[token->type].tree_type;
6386
6387       /* We used the operator token.  */
6388       cp_lexer_consume_token (parser->lexer);
6389
6390       /* For "false && x" or "true || x", x will never be executed;
6391          disable warnings while evaluating it.  */
6392       if (tree_type == TRUTH_ANDIF_EXPR)
6393         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6394       else if (tree_type == TRUTH_ORIF_EXPR)
6395         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6396
6397       /* Extract another operand.  It may be the RHS of this expression
6398          or the LHS of a new, higher priority expression.  */
6399       rhs = cp_parser_simple_cast_expression (parser);
6400       rhs_type = ERROR_MARK;
6401
6402       /* Get another operator token.  Look up its precedence to avoid
6403          building a useless (immediately popped) stack entry for common
6404          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6405       token = cp_lexer_peek_token (parser->lexer);
6406       lookahead_prec = TOKEN_PRECEDENCE (token);
6407       if (lookahead_prec > new_prec)
6408         {
6409           /* ... and prepare to parse the RHS of the new, higher priority
6410              expression.  Since precedence levels on the stack are
6411              monotonically increasing, we do not have to care about
6412              stack overflows.  */
6413           sp->prec = prec;
6414           sp->tree_type = tree_type;
6415           sp->lhs = lhs;
6416           sp->lhs_type = lhs_type;
6417           sp++;
6418           lhs = rhs;
6419           lhs_type = rhs_type;
6420           prec = new_prec;
6421           new_prec = lookahead_prec;
6422           goto get_rhs;
6423
6424          pop:
6425           lookahead_prec = new_prec;
6426           /* If the stack is not empty, we have parsed into LHS the right side
6427              (`4' in the example above) of an expression we had suspended.
6428              We can use the information on the stack to recover the LHS (`3')
6429              from the stack together with the tree code (`MULT_EXPR'), and
6430              the precedence of the higher level subexpression
6431              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6432              which will be used to actually build the additive expression.  */
6433           --sp;
6434           prec = sp->prec;
6435           tree_type = sp->tree_type;
6436           rhs = lhs;
6437           rhs_type = lhs_type;
6438           lhs = sp->lhs;
6439           lhs_type = sp->lhs_type;
6440         }
6441
6442       /* Undo the disabling of warnings done above.  */
6443       if (tree_type == TRUTH_ANDIF_EXPR)
6444         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6445       else if (tree_type == TRUTH_ORIF_EXPR)
6446         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6447
6448       overloaded_p = false;
6449       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6450          ERROR_MARK for everything that is not a binary expression.
6451          This makes warn_about_parentheses miss some warnings that
6452          involve unary operators.  For unary expressions we should
6453          pass the correct tree_code unless the unary expression was
6454          surrounded by parentheses.
6455       */
6456       if (no_toplevel_fold_p
6457           && lookahead_prec <= prec
6458           && sp == stack
6459           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6460         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6461       else
6462         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6463                                  &overloaded_p, tf_warning_or_error);
6464       lhs_type = tree_type;
6465
6466       /* If the binary operator required the use of an overloaded operator,
6467          then this expression cannot be an integral constant-expression.
6468          An overloaded operator can be used even if both operands are
6469          otherwise permissible in an integral constant-expression if at
6470          least one of the operands is of enumeration type.  */
6471
6472       if (overloaded_p
6473           && (cp_parser_non_integral_constant_expression
6474               (parser, "calls to overloaded operators")))
6475         return error_mark_node;
6476     }
6477
6478   return lhs;
6479 }
6480
6481
6482 /* Parse the `? expression : assignment-expression' part of a
6483    conditional-expression.  The LOGICAL_OR_EXPR is the
6484    logical-or-expression that started the conditional-expression.
6485    Returns a representation of the entire conditional-expression.
6486
6487    This routine is used by cp_parser_assignment_expression.
6488
6489      ? expression : assignment-expression
6490
6491    GNU Extensions:
6492
6493      ? : assignment-expression */
6494
6495 static tree
6496 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6497 {
6498   tree expr;
6499   tree assignment_expr;
6500
6501   /* Consume the `?' token.  */
6502   cp_lexer_consume_token (parser->lexer);
6503   if (cp_parser_allow_gnu_extensions_p (parser)
6504       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6505     {
6506       /* Implicit true clause.  */
6507       expr = NULL_TREE;
6508       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6509     }
6510   else
6511     {
6512       /* Parse the expression.  */
6513       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6514       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6515       c_inhibit_evaluation_warnings +=
6516         ((logical_or_expr == truthvalue_true_node)
6517          - (logical_or_expr == truthvalue_false_node));
6518     }
6519
6520   /* The next token should be a `:'.  */
6521   cp_parser_require (parser, CPP_COLON, "%<:%>");
6522   /* Parse the assignment-expression.  */
6523   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6524   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6525
6526   /* Build the conditional-expression.  */
6527   return build_x_conditional_expr (logical_or_expr,
6528                                    expr,
6529                                    assignment_expr,
6530                                    tf_warning_or_error);
6531 }
6532
6533 /* Parse an assignment-expression.
6534
6535    assignment-expression:
6536      conditional-expression
6537      logical-or-expression assignment-operator assignment_expression
6538      throw-expression
6539
6540    CAST_P is true if this expression is the target of a cast.
6541
6542    Returns a representation for the expression.  */
6543
6544 static tree
6545 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6546                                  cp_id_kind * pidk)
6547 {
6548   tree expr;
6549
6550   /* If the next token is the `throw' keyword, then we're looking at
6551      a throw-expression.  */
6552   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6553     expr = cp_parser_throw_expression (parser);
6554   /* Otherwise, it must be that we are looking at a
6555      logical-or-expression.  */
6556   else
6557     {
6558       /* Parse the binary expressions (logical-or-expression).  */
6559       expr = cp_parser_binary_expression (parser, cast_p, false,
6560                                           PREC_NOT_OPERATOR, pidk);
6561       /* If the next token is a `?' then we're actually looking at a
6562          conditional-expression.  */
6563       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6564         return cp_parser_question_colon_clause (parser, expr);
6565       else
6566         {
6567           enum tree_code assignment_operator;
6568
6569           /* If it's an assignment-operator, we're using the second
6570              production.  */
6571           assignment_operator
6572             = cp_parser_assignment_operator_opt (parser);
6573           if (assignment_operator != ERROR_MARK)
6574             {
6575               bool non_constant_p;
6576
6577               /* Parse the right-hand side of the assignment.  */
6578               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6579
6580               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6581                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6582
6583               /* An assignment may not appear in a
6584                  constant-expression.  */
6585               if (cp_parser_non_integral_constant_expression (parser,
6586                                                               "an assignment"))
6587                 return error_mark_node;
6588               /* Build the assignment expression.  */
6589               expr = build_x_modify_expr (expr,
6590                                           assignment_operator,
6591                                           rhs,
6592                                           tf_warning_or_error);
6593             }
6594         }
6595     }
6596
6597   return expr;
6598 }
6599
6600 /* Parse an (optional) assignment-operator.
6601
6602    assignment-operator: one of
6603      = *= /= %= += -= >>= <<= &= ^= |=
6604
6605    GNU Extension:
6606
6607    assignment-operator: one of
6608      <?= >?=
6609
6610    If the next token is an assignment operator, the corresponding tree
6611    code is returned, and the token is consumed.  For example, for
6612    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6613    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6614    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6615    operator, ERROR_MARK is returned.  */
6616
6617 static enum tree_code
6618 cp_parser_assignment_operator_opt (cp_parser* parser)
6619 {
6620   enum tree_code op;
6621   cp_token *token;
6622
6623   /* Peek at the next token.  */
6624   token = cp_lexer_peek_token (parser->lexer);
6625
6626   switch (token->type)
6627     {
6628     case CPP_EQ:
6629       op = NOP_EXPR;
6630       break;
6631
6632     case CPP_MULT_EQ:
6633       op = MULT_EXPR;
6634       break;
6635
6636     case CPP_DIV_EQ:
6637       op = TRUNC_DIV_EXPR;
6638       break;
6639
6640     case CPP_MOD_EQ:
6641       op = TRUNC_MOD_EXPR;
6642       break;
6643
6644     case CPP_PLUS_EQ:
6645       op = PLUS_EXPR;
6646       break;
6647
6648     case CPP_MINUS_EQ:
6649       op = MINUS_EXPR;
6650       break;
6651
6652     case CPP_RSHIFT_EQ:
6653       op = RSHIFT_EXPR;
6654       break;
6655
6656     case CPP_LSHIFT_EQ:
6657       op = LSHIFT_EXPR;
6658       break;
6659
6660     case CPP_AND_EQ:
6661       op = BIT_AND_EXPR;
6662       break;
6663
6664     case CPP_XOR_EQ:
6665       op = BIT_XOR_EXPR;
6666       break;
6667
6668     case CPP_OR_EQ:
6669       op = BIT_IOR_EXPR;
6670       break;
6671
6672     default:
6673       /* Nothing else is an assignment operator.  */
6674       op = ERROR_MARK;
6675     }
6676
6677   /* If it was an assignment operator, consume it.  */
6678   if (op != ERROR_MARK)
6679     cp_lexer_consume_token (parser->lexer);
6680
6681   return op;
6682 }
6683
6684 /* Parse an expression.
6685
6686    expression:
6687      assignment-expression
6688      expression , assignment-expression
6689
6690    CAST_P is true if this expression is the target of a cast.
6691
6692    Returns a representation of the expression.  */
6693
6694 static tree
6695 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6696 {
6697   tree expression = NULL_TREE;
6698
6699   while (true)
6700     {
6701       tree assignment_expression;
6702
6703       /* Parse the next assignment-expression.  */
6704       assignment_expression
6705         = cp_parser_assignment_expression (parser, cast_p, pidk);
6706       /* If this is the first assignment-expression, we can just
6707          save it away.  */
6708       if (!expression)
6709         expression = assignment_expression;
6710       else
6711         expression = build_x_compound_expr (expression,
6712                                             assignment_expression,
6713                                             tf_warning_or_error);
6714       /* If the next token is not a comma, then we are done with the
6715          expression.  */
6716       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6717         break;
6718       /* Consume the `,'.  */
6719       cp_lexer_consume_token (parser->lexer);
6720       /* A comma operator cannot appear in a constant-expression.  */
6721       if (cp_parser_non_integral_constant_expression (parser,
6722                                                       "a comma operator"))
6723         expression = error_mark_node;
6724     }
6725
6726   return expression;
6727 }
6728
6729 /* Parse a constant-expression.
6730
6731    constant-expression:
6732      conditional-expression
6733
6734   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6735   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6736   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6737   is false, NON_CONSTANT_P should be NULL.  */
6738
6739 static tree
6740 cp_parser_constant_expression (cp_parser* parser,
6741                                bool allow_non_constant_p,
6742                                bool *non_constant_p)
6743 {
6744   bool saved_integral_constant_expression_p;
6745   bool saved_allow_non_integral_constant_expression_p;
6746   bool saved_non_integral_constant_expression_p;
6747   tree expression;
6748
6749   /* It might seem that we could simply parse the
6750      conditional-expression, and then check to see if it were
6751      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6752      one that the compiler can figure out is constant, possibly after
6753      doing some simplifications or optimizations.  The standard has a
6754      precise definition of constant-expression, and we must honor
6755      that, even though it is somewhat more restrictive.
6756
6757      For example:
6758
6759        int i[(2, 3)];
6760
6761      is not a legal declaration, because `(2, 3)' is not a
6762      constant-expression.  The `,' operator is forbidden in a
6763      constant-expression.  However, GCC's constant-folding machinery
6764      will fold this operation to an INTEGER_CST for `3'.  */
6765
6766   /* Save the old settings.  */
6767   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6768   saved_allow_non_integral_constant_expression_p
6769     = parser->allow_non_integral_constant_expression_p;
6770   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6771   /* We are now parsing a constant-expression.  */
6772   parser->integral_constant_expression_p = true;
6773   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6774   parser->non_integral_constant_expression_p = false;
6775   /* Although the grammar says "conditional-expression", we parse an
6776      "assignment-expression", which also permits "throw-expression"
6777      and the use of assignment operators.  In the case that
6778      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6779      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6780      actually essential that we look for an assignment-expression.
6781      For example, cp_parser_initializer_clauses uses this function to
6782      determine whether a particular assignment-expression is in fact
6783      constant.  */
6784   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6785   /* Restore the old settings.  */
6786   parser->integral_constant_expression_p
6787     = saved_integral_constant_expression_p;
6788   parser->allow_non_integral_constant_expression_p
6789     = saved_allow_non_integral_constant_expression_p;
6790   if (allow_non_constant_p)
6791     *non_constant_p = parser->non_integral_constant_expression_p;
6792   else if (parser->non_integral_constant_expression_p)
6793     expression = error_mark_node;
6794   parser->non_integral_constant_expression_p
6795     = saved_non_integral_constant_expression_p;
6796
6797   return expression;
6798 }
6799
6800 /* Parse __builtin_offsetof.
6801
6802    offsetof-expression:
6803      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6804
6805    offsetof-member-designator:
6806      id-expression
6807      | offsetof-member-designator "." id-expression
6808      | offsetof-member-designator "[" expression "]"
6809      | offsetof-member-designator "->" id-expression  */
6810
6811 static tree
6812 cp_parser_builtin_offsetof (cp_parser *parser)
6813 {
6814   int save_ice_p, save_non_ice_p;
6815   tree type, expr;
6816   cp_id_kind dummy;
6817   cp_token *token;
6818
6819   /* We're about to accept non-integral-constant things, but will
6820      definitely yield an integral constant expression.  Save and
6821      restore these values around our local parsing.  */
6822   save_ice_p = parser->integral_constant_expression_p;
6823   save_non_ice_p = parser->non_integral_constant_expression_p;
6824
6825   /* Consume the "__builtin_offsetof" token.  */
6826   cp_lexer_consume_token (parser->lexer);
6827   /* Consume the opening `('.  */
6828   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6829   /* Parse the type-id.  */
6830   type = cp_parser_type_id (parser);
6831   /* Look for the `,'.  */
6832   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6833   token = cp_lexer_peek_token (parser->lexer);
6834
6835   /* Build the (type *)null that begins the traditional offsetof macro.  */
6836   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6837                             tf_warning_or_error);
6838
6839   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6840   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6841                                                  true, &dummy, token->location);
6842   while (true)
6843     {
6844       token = cp_lexer_peek_token (parser->lexer);
6845       switch (token->type)
6846         {
6847         case CPP_OPEN_SQUARE:
6848           /* offsetof-member-designator "[" expression "]" */
6849           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6850           break;
6851
6852         case CPP_DEREF:
6853           /* offsetof-member-designator "->" identifier */
6854           expr = grok_array_decl (expr, integer_zero_node);
6855           /* FALLTHRU */
6856
6857         case CPP_DOT:
6858           /* offsetof-member-designator "." identifier */
6859           cp_lexer_consume_token (parser->lexer);
6860           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6861                                                          expr, true, &dummy,
6862                                                          token->location);
6863           break;
6864
6865         case CPP_CLOSE_PAREN:
6866           /* Consume the ")" token.  */
6867           cp_lexer_consume_token (parser->lexer);
6868           goto success;
6869
6870         default:
6871           /* Error.  We know the following require will fail, but
6872              that gives the proper error message.  */
6873           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6874           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6875           expr = error_mark_node;
6876           goto failure;
6877         }
6878     }
6879
6880  success:
6881   /* If we're processing a template, we can't finish the semantics yet.
6882      Otherwise we can fold the entire expression now.  */
6883   if (processing_template_decl)
6884     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6885   else
6886     expr = finish_offsetof (expr);
6887
6888  failure:
6889   parser->integral_constant_expression_p = save_ice_p;
6890   parser->non_integral_constant_expression_p = save_non_ice_p;
6891
6892   return expr;
6893 }
6894
6895 /* Parse a trait expression.  */
6896
6897 static tree
6898 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6899 {
6900   cp_trait_kind kind;
6901   tree type1, type2 = NULL_TREE;
6902   bool binary = false;
6903   cp_decl_specifier_seq decl_specs;
6904
6905   switch (keyword)
6906     {
6907     case RID_HAS_NOTHROW_ASSIGN:
6908       kind = CPTK_HAS_NOTHROW_ASSIGN;
6909       break;
6910     case RID_HAS_NOTHROW_CONSTRUCTOR:
6911       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6912       break;
6913     case RID_HAS_NOTHROW_COPY:
6914       kind = CPTK_HAS_NOTHROW_COPY;
6915       break;
6916     case RID_HAS_TRIVIAL_ASSIGN:
6917       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6918       break;
6919     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6920       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6921       break;
6922     case RID_HAS_TRIVIAL_COPY:
6923       kind = CPTK_HAS_TRIVIAL_COPY;
6924       break;
6925     case RID_HAS_TRIVIAL_DESTRUCTOR:
6926       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6927       break;
6928     case RID_HAS_VIRTUAL_DESTRUCTOR:
6929       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6930       break;
6931     case RID_IS_ABSTRACT:
6932       kind = CPTK_IS_ABSTRACT;
6933       break;
6934     case RID_IS_BASE_OF:
6935       kind = CPTK_IS_BASE_OF;
6936       binary = true;
6937       break;
6938     case RID_IS_CLASS:
6939       kind = CPTK_IS_CLASS;
6940       break;
6941     case RID_IS_CONVERTIBLE_TO:
6942       kind = CPTK_IS_CONVERTIBLE_TO;
6943       binary = true;
6944       break;
6945     case RID_IS_EMPTY:
6946       kind = CPTK_IS_EMPTY;
6947       break;
6948     case RID_IS_ENUM:
6949       kind = CPTK_IS_ENUM;
6950       break;
6951     case RID_IS_POD:
6952       kind = CPTK_IS_POD;
6953       break;
6954     case RID_IS_POLYMORPHIC:
6955       kind = CPTK_IS_POLYMORPHIC;
6956       break;
6957     case RID_IS_STD_LAYOUT:
6958       kind = CPTK_IS_STD_LAYOUT;
6959       break;
6960     case RID_IS_TRIVIAL:
6961       kind = CPTK_IS_TRIVIAL;
6962       break;
6963     case RID_IS_UNION:
6964       kind = CPTK_IS_UNION;
6965       break;
6966     default:
6967       gcc_unreachable ();
6968     }
6969
6970   /* Consume the token.  */
6971   cp_lexer_consume_token (parser->lexer);
6972
6973   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6974
6975   type1 = cp_parser_type_id (parser);
6976
6977   if (type1 == error_mark_node)
6978     return error_mark_node;
6979
6980   /* Build a trivial decl-specifier-seq.  */
6981   clear_decl_specs (&decl_specs);
6982   decl_specs.type = type1;
6983
6984   /* Call grokdeclarator to figure out what type this is.  */
6985   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6986                           /*initialized=*/0, /*attrlist=*/NULL);
6987
6988   if (binary)
6989     {
6990       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6991  
6992       type2 = cp_parser_type_id (parser);
6993
6994       if (type2 == error_mark_node)
6995         return error_mark_node;
6996
6997       /* Build a trivial decl-specifier-seq.  */
6998       clear_decl_specs (&decl_specs);
6999       decl_specs.type = type2;
7000
7001       /* Call grokdeclarator to figure out what type this is.  */
7002       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7003                               /*initialized=*/0, /*attrlist=*/NULL);
7004     }
7005
7006   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7007
7008   /* Complete the trait expression, which may mean either processing
7009      the trait expr now or saving it for template instantiation.  */
7010   return finish_trait_expr (kind, type1, type2);
7011 }
7012
7013 /* Lambdas that appear in variable initializer or default argument scope
7014    get that in their mangling, so we need to record it.  We might as well
7015    use the count for function and namespace scopes as well.  */
7016 static GTY(()) tree lambda_scope;
7017 static GTY(()) int lambda_count;
7018 typedef struct GTY(()) tree_int
7019 {
7020   tree t;
7021   int i;
7022 } tree_int;
7023 DEF_VEC_O(tree_int);
7024 DEF_VEC_ALLOC_O(tree_int,gc);
7025 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7026
7027 static void
7028 start_lambda_scope (tree decl)
7029 {
7030   tree_int ti;
7031   gcc_assert (decl);
7032   /* Once we're inside a function, we ignore other scopes and just push
7033      the function again so that popping works properly.  */
7034   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7035     decl = current_function_decl;
7036   ti.t = lambda_scope;
7037   ti.i = lambda_count;
7038   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7039   if (lambda_scope != decl)
7040     {
7041       /* Don't reset the count if we're still in the same function.  */
7042       lambda_scope = decl;
7043       lambda_count = 0;
7044     }
7045 }
7046
7047 static void
7048 record_lambda_scope (tree lambda)
7049 {
7050   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7051   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7052 }
7053
7054 static void
7055 finish_lambda_scope (void)
7056 {
7057   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7058   if (lambda_scope != p->t)
7059     {
7060       lambda_scope = p->t;
7061       lambda_count = p->i;
7062     }
7063   VEC_pop (tree_int, lambda_scope_stack);
7064 }
7065
7066 /* Parse a lambda expression.
7067
7068    lambda-expression:
7069      lambda-introducer lambda-declarator [opt] compound-statement
7070
7071    Returns a representation of the expression.  */
7072
7073 static tree
7074 cp_parser_lambda_expression (cp_parser* parser)
7075 {
7076   tree lambda_expr = build_lambda_expr ();
7077   tree type;
7078
7079   LAMBDA_EXPR_LOCATION (lambda_expr)
7080     = cp_lexer_peek_token (parser->lexer)->location;
7081
7082   /* We may be in the middle of deferred access check.  Disable
7083      it now.  */
7084   push_deferring_access_checks (dk_no_deferred);
7085
7086   cp_parser_lambda_introducer (parser, lambda_expr);
7087
7088   type = begin_lambda_type (lambda_expr);
7089
7090   record_lambda_scope (lambda_expr);
7091
7092   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7093   determine_visibility (TYPE_NAME (type));
7094
7095   /* Now that we've started the type, add the capture fields for any
7096      explicit captures.  */
7097   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7098
7099   {
7100     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7101     unsigned int saved_num_template_parameter_lists
7102         = parser->num_template_parameter_lists;
7103
7104     parser->num_template_parameter_lists = 0;
7105
7106     /* By virtue of defining a local class, a lambda expression has access to
7107        the private variables of enclosing classes.  */
7108
7109     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7110
7111     cp_parser_lambda_body (parser, lambda_expr);
7112
7113     /* The capture list was built up in reverse order; fix that now.  */
7114     {
7115       tree newlist = NULL_TREE;
7116       tree elt, next;
7117
7118       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7119            elt; elt = next)
7120         {
7121           tree field = TREE_PURPOSE (elt);
7122           char *buf;
7123
7124           next = TREE_CHAIN (elt);
7125           TREE_CHAIN (elt) = newlist;
7126           newlist = elt;
7127
7128           /* Also add __ to the beginning of the field name so that code
7129              outside the lambda body can't see the captured name.  We could
7130              just remove the name entirely, but this is more useful for
7131              debugging.  */
7132           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7133             /* The 'this' capture already starts with __.  */
7134             continue;
7135
7136           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7137           buf[1] = buf[0] = '_';
7138           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7139                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7140           DECL_NAME (field) = get_identifier (buf);
7141         }
7142       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7143     }
7144
7145     maybe_add_lambda_conv_op (type);
7146
7147     type = finish_struct (type, /*attributes=*/NULL_TREE);
7148
7149     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7150   }
7151
7152   pop_deferring_access_checks ();
7153
7154   return build_lambda_object (lambda_expr);
7155 }
7156
7157 /* Parse the beginning of a lambda expression.
7158
7159    lambda-introducer:
7160      [ lambda-capture [opt] ]
7161
7162    LAMBDA_EXPR is the current representation of the lambda expression.  */
7163
7164 static void
7165 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7166 {
7167   /* Need commas after the first capture.  */
7168   bool first = true;
7169
7170   /* Eat the leading `['.  */
7171   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7172
7173   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7174   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7175       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7176     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7177   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7178     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7179
7180   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7181     {
7182       cp_lexer_consume_token (parser->lexer);
7183       first = false;
7184     }
7185
7186   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7187     {
7188       cp_token* capture_token;
7189       tree capture_id;
7190       tree capture_init_expr;
7191       cp_id_kind idk = CP_ID_KIND_NONE;
7192       bool explicit_init_p = false;
7193
7194       enum capture_kind_type
7195       {
7196         BY_COPY,
7197         BY_REFERENCE
7198       };
7199       enum capture_kind_type capture_kind = BY_COPY;
7200
7201       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7202         {
7203           error ("expected end of capture-list");
7204           return;
7205         }
7206
7207       if (first)
7208         first = false;
7209       else
7210         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7211
7212       /* Possibly capture `this'.  */
7213       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7214         {
7215           cp_lexer_consume_token (parser->lexer);
7216           add_capture (lambda_expr,
7217                        /*id=*/get_identifier ("__this"),
7218                        /*initializer=*/finish_this_expr(),
7219                        /*by_reference_p=*/false,
7220                        explicit_init_p);
7221           continue;
7222         }
7223
7224       /* Remember whether we want to capture as a reference or not.  */
7225       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7226         {
7227           capture_kind = BY_REFERENCE;
7228           cp_lexer_consume_token (parser->lexer);
7229         }
7230
7231       /* Get the identifier.  */
7232       capture_token = cp_lexer_peek_token (parser->lexer);
7233       capture_id = cp_parser_identifier (parser);
7234
7235       if (capture_id == error_mark_node)
7236         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7237            delimiters, but I modified this to stop on unnested ']' as well.  It
7238            was already changed to stop on unnested '}', so the
7239            "closing_parenthesis" name is no more misleading with my change.  */
7240         {
7241           cp_parser_skip_to_closing_parenthesis (parser,
7242                                                  /*recovering=*/true,
7243                                                  /*or_comma=*/true,
7244                                                  /*consume_paren=*/true);
7245           break;
7246         }
7247
7248       /* Find the initializer for this capture.  */
7249       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7250         {
7251           /* An explicit expression exists.  */
7252           cp_lexer_consume_token (parser->lexer);
7253           pedwarn (input_location, OPT_pedantic,
7254                    "ISO C++ does not allow initializers "
7255                    "in lambda expression capture lists");
7256           capture_init_expr = cp_parser_assignment_expression (parser,
7257                                                                /*cast_p=*/true,
7258                                                                &idk);
7259           explicit_init_p = true;
7260         }
7261       else
7262         {
7263           const char* error_msg;
7264
7265           /* Turn the identifier into an id-expression.  */
7266           capture_init_expr
7267             = cp_parser_lookup_name
7268                 (parser,
7269                  capture_id,
7270                  none_type,
7271                  /*is_template=*/false,
7272                  /*is_namespace=*/false,
7273                  /*check_dependency=*/true,
7274                  /*ambiguous_decls=*/NULL,
7275                  capture_token->location);
7276
7277           capture_init_expr
7278             = finish_id_expression
7279                 (capture_id,
7280                  capture_init_expr,
7281                  parser->scope,
7282                  &idk,
7283                  /*integral_constant_expression_p=*/false,
7284                  /*allow_non_integral_constant_expression_p=*/false,
7285                  /*non_integral_constant_expression_p=*/NULL,
7286                  /*template_p=*/false,
7287                  /*done=*/true,
7288                  /*address_p=*/false,
7289                  /*template_arg_p=*/false,
7290                  &error_msg,
7291                  capture_token->location);
7292         }
7293
7294       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7295         capture_init_expr
7296           = unqualified_name_lookup_error (capture_init_expr);
7297
7298       add_capture (lambda_expr,
7299                    capture_id,
7300                    capture_init_expr,
7301                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7302                    explicit_init_p);
7303     }
7304
7305   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7306 }
7307
7308 /* Parse the (optional) middle of a lambda expression.
7309
7310    lambda-declarator:
7311      ( parameter-declaration-clause [opt] )
7312        attribute-specifier [opt]
7313        mutable [opt]
7314        exception-specification [opt]
7315        lambda-return-type-clause [opt]
7316
7317    LAMBDA_EXPR is the current representation of the lambda expression.  */
7318
7319 static void
7320 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7321 {
7322   /* 5.1.1.4 of the standard says:
7323        If a lambda-expression does not include a lambda-declarator, it is as if
7324        the lambda-declarator were ().
7325      This means an empty parameter list, no attributes, and no exception
7326      specification.  */
7327   tree param_list = void_list_node;
7328   tree attributes = NULL_TREE;
7329   tree exception_spec = NULL_TREE;
7330   tree t;
7331
7332   /* The lambda-declarator is optional, but must begin with an opening
7333      parenthesis if present.  */
7334   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7335     {
7336       cp_lexer_consume_token (parser->lexer);
7337
7338       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7339
7340       /* Parse parameters.  */
7341       param_list = cp_parser_parameter_declaration_clause (parser);
7342
7343       /* Default arguments shall not be specified in the
7344          parameter-declaration-clause of a lambda-declarator.  */
7345       for (t = param_list; t; t = TREE_CHAIN (t))
7346         if (TREE_PURPOSE (t))
7347           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7348                    "default argument specified for lambda parameter");
7349
7350       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7351
7352       attributes = cp_parser_attributes_opt (parser);
7353
7354       /* Parse optional `mutable' keyword.  */
7355       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7356         {
7357           cp_lexer_consume_token (parser->lexer);
7358           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7359         }
7360
7361       /* Parse optional exception specification.  */
7362       exception_spec = cp_parser_exception_specification_opt (parser);
7363
7364       /* Parse optional trailing return type.  */
7365       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7366         {
7367           cp_lexer_consume_token (parser->lexer);
7368           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7369         }
7370
7371       /* The function parameters must be in scope all the way until after the
7372          trailing-return-type in case of decltype.  */
7373       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7374         pop_binding (DECL_NAME (t), t);
7375
7376       leave_scope ();
7377     }
7378
7379   /* Create the function call operator.
7380
7381      Messing with declarators like this is no uglier than building up the
7382      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7383      other code.  */
7384   {
7385     cp_decl_specifier_seq return_type_specs;
7386     cp_declarator* declarator;
7387     tree fco;
7388     int quals;
7389     void *p;
7390
7391     clear_decl_specs (&return_type_specs);
7392     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7393       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7394     else
7395       /* Maybe we will deduce the return type later, but we can use void
7396          as a placeholder return type anyways.  */
7397       return_type_specs.type = void_type_node;
7398
7399     p = obstack_alloc (&declarator_obstack, 0);
7400
7401     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7402                                      sfk_none);
7403
7404     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7405              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7406     declarator = make_call_declarator (declarator, param_list, quals,
7407                                        exception_spec,
7408                                        /*late_return_type=*/NULL_TREE);
7409     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7410
7411     fco = grokmethod (&return_type_specs,
7412                       declarator,
7413                       attributes);
7414     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7415     DECL_ARTIFICIAL (fco) = 1;
7416
7417     finish_member_declaration (fco);
7418
7419     obstack_free (&declarator_obstack, p);
7420   }
7421 }
7422
7423 /* Parse the body of a lambda expression, which is simply
7424
7425    compound-statement
7426
7427    but which requires special handling.
7428    LAMBDA_EXPR is the current representation of the lambda expression.  */
7429
7430 static void
7431 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7432 {
7433   bool nested = (current_function_decl != NULL_TREE);
7434   if (nested)
7435     push_function_context ();
7436
7437   /* Finish the function call operator
7438      - class_specifier
7439      + late_parsing_for_member
7440      + function_definition_after_declarator
7441      + ctor_initializer_opt_and_function_body  */
7442   {
7443     tree fco = lambda_function (lambda_expr);
7444     tree body;
7445     bool done = false;
7446
7447     /* Let the front end know that we are going to be defining this
7448        function.  */
7449     start_preparsed_function (fco,
7450                               NULL_TREE,
7451                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7452
7453     start_lambda_scope (fco);
7454     body = begin_function_body ();
7455
7456     /* 5.1.1.4 of the standard says:
7457          If a lambda-expression does not include a trailing-return-type, it
7458          is as if the trailing-return-type denotes the following type:
7459           * if the compound-statement is of the form
7460                { return attribute-specifier [opt] expression ; }
7461              the type of the returned expression after lvalue-to-rvalue
7462              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7463              (_conv.array_ 4.2), and function-to-pointer conversion
7464              (_conv.func_ 4.3);
7465           * otherwise, void.  */
7466
7467     /* In a lambda that has neither a lambda-return-type-clause
7468        nor a deducible form, errors should be reported for return statements
7469        in the body.  Since we used void as the placeholder return type, parsing
7470        the body as usual will give such desired behavior.  */
7471     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7472         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7473         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7474         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7475       {
7476         tree compound_stmt;
7477         tree expr = NULL_TREE;
7478         cp_id_kind idk = CP_ID_KIND_NONE;
7479
7480         /* Parse tentatively in case there's more after the initial return
7481            statement.  */
7482         cp_parser_parse_tentatively (parser);
7483
7484         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7485         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7486
7487         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7488
7489         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7490         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7491
7492         if (cp_parser_parse_definitely (parser))
7493           {
7494             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7495
7496             compound_stmt = begin_compound_stmt (0);
7497             /* Will get error here if type not deduced yet.  */
7498             finish_return_stmt (expr);
7499             finish_compound_stmt (compound_stmt);
7500
7501             done = true;
7502           }
7503       }
7504
7505     if (!done)
7506       {
7507         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7508           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7509         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7510            cp_parser_compound_stmt does not pass it.  */
7511         cp_parser_function_body (parser);
7512         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7513       }
7514
7515     finish_function_body (body);
7516     finish_lambda_scope ();
7517
7518     /* Finish the function and generate code for it if necessary.  */
7519     expand_or_defer_fn (finish_function (/*inline*/2));
7520   }
7521
7522   if (nested)
7523     pop_function_context();
7524 }
7525
7526 /* Statements [gram.stmt.stmt]  */
7527
7528 /* Parse a statement.
7529
7530    statement:
7531      labeled-statement
7532      expression-statement
7533      compound-statement
7534      selection-statement
7535      iteration-statement
7536      jump-statement
7537      declaration-statement
7538      try-block
7539
7540   IN_COMPOUND is true when the statement is nested inside a
7541   cp_parser_compound_statement; this matters for certain pragmas.
7542
7543   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7544   is a (possibly labeled) if statement which is not enclosed in braces
7545   and has an else clause.  This is used to implement -Wparentheses.  */
7546
7547 static void
7548 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7549                      bool in_compound, bool *if_p)
7550 {
7551   tree statement;
7552   cp_token *token;
7553   location_t statement_location;
7554
7555  restart:
7556   if (if_p != NULL)
7557     *if_p = false;
7558   /* There is no statement yet.  */
7559   statement = NULL_TREE;
7560   /* Peek at the next token.  */
7561   token = cp_lexer_peek_token (parser->lexer);
7562   /* Remember the location of the first token in the statement.  */
7563   statement_location = token->location;
7564   /* If this is a keyword, then that will often determine what kind of
7565      statement we have.  */
7566   if (token->type == CPP_KEYWORD)
7567     {
7568       enum rid keyword = token->keyword;
7569
7570       switch (keyword)
7571         {
7572         case RID_CASE:
7573         case RID_DEFAULT:
7574           /* Looks like a labeled-statement with a case label.
7575              Parse the label, and then use tail recursion to parse
7576              the statement.  */
7577           cp_parser_label_for_labeled_statement (parser);
7578           goto restart;
7579
7580         case RID_IF:
7581         case RID_SWITCH:
7582           statement = cp_parser_selection_statement (parser, if_p);
7583           break;
7584
7585         case RID_WHILE:
7586         case RID_DO:
7587         case RID_FOR:
7588           statement = cp_parser_iteration_statement (parser);
7589           break;
7590
7591         case RID_BREAK:
7592         case RID_CONTINUE:
7593         case RID_RETURN:
7594         case RID_GOTO:
7595           statement = cp_parser_jump_statement (parser);
7596           break;
7597
7598           /* Objective-C++ exception-handling constructs.  */
7599         case RID_AT_TRY:
7600         case RID_AT_CATCH:
7601         case RID_AT_FINALLY:
7602         case RID_AT_SYNCHRONIZED:
7603         case RID_AT_THROW:
7604           statement = cp_parser_objc_statement (parser);
7605           break;
7606
7607         case RID_TRY:
7608           statement = cp_parser_try_block (parser);
7609           break;
7610
7611         case RID_NAMESPACE:
7612           /* This must be a namespace alias definition.  */
7613           cp_parser_declaration_statement (parser);
7614           return;
7615           
7616         default:
7617           /* It might be a keyword like `int' that can start a
7618              declaration-statement.  */
7619           break;
7620         }
7621     }
7622   else if (token->type == CPP_NAME)
7623     {
7624       /* If the next token is a `:', then we are looking at a
7625          labeled-statement.  */
7626       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7627       if (token->type == CPP_COLON)
7628         {
7629           /* Looks like a labeled-statement with an ordinary label.
7630              Parse the label, and then use tail recursion to parse
7631              the statement.  */
7632           cp_parser_label_for_labeled_statement (parser);
7633           goto restart;
7634         }
7635     }
7636   /* Anything that starts with a `{' must be a compound-statement.  */
7637   else if (token->type == CPP_OPEN_BRACE)
7638     statement = cp_parser_compound_statement (parser, NULL, false);
7639   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7640      a statement all its own.  */
7641   else if (token->type == CPP_PRAGMA)
7642     {
7643       /* Only certain OpenMP pragmas are attached to statements, and thus
7644          are considered statements themselves.  All others are not.  In
7645          the context of a compound, accept the pragma as a "statement" and
7646          return so that we can check for a close brace.  Otherwise we
7647          require a real statement and must go back and read one.  */
7648       if (in_compound)
7649         cp_parser_pragma (parser, pragma_compound);
7650       else if (!cp_parser_pragma (parser, pragma_stmt))
7651         goto restart;
7652       return;
7653     }
7654   else if (token->type == CPP_EOF)
7655     {
7656       cp_parser_error (parser, "expected statement");
7657       return;
7658     }
7659
7660   /* Everything else must be a declaration-statement or an
7661      expression-statement.  Try for the declaration-statement
7662      first, unless we are looking at a `;', in which case we know that
7663      we have an expression-statement.  */
7664   if (!statement)
7665     {
7666       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7667         {
7668           cp_parser_parse_tentatively (parser);
7669           /* Try to parse the declaration-statement.  */
7670           cp_parser_declaration_statement (parser);
7671           /* If that worked, we're done.  */
7672           if (cp_parser_parse_definitely (parser))
7673             return;
7674         }
7675       /* Look for an expression-statement instead.  */
7676       statement = cp_parser_expression_statement (parser, in_statement_expr);
7677     }
7678
7679   /* Set the line number for the statement.  */
7680   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7681     SET_EXPR_LOCATION (statement, statement_location);
7682 }
7683
7684 /* Parse the label for a labeled-statement, i.e.
7685
7686    identifier :
7687    case constant-expression :
7688    default :
7689
7690    GNU Extension:
7691    case constant-expression ... constant-expression : statement
7692
7693    When a label is parsed without errors, the label is added to the
7694    parse tree by the finish_* functions, so this function doesn't
7695    have to return the label.  */
7696
7697 static void
7698 cp_parser_label_for_labeled_statement (cp_parser* parser)
7699 {
7700   cp_token *token;
7701   tree label = NULL_TREE;
7702
7703   /* The next token should be an identifier.  */
7704   token = cp_lexer_peek_token (parser->lexer);
7705   if (token->type != CPP_NAME
7706       && token->type != CPP_KEYWORD)
7707     {
7708       cp_parser_error (parser, "expected labeled-statement");
7709       return;
7710     }
7711
7712   switch (token->keyword)
7713     {
7714     case RID_CASE:
7715       {
7716         tree expr, expr_hi;
7717         cp_token *ellipsis;
7718
7719         /* Consume the `case' token.  */
7720         cp_lexer_consume_token (parser->lexer);
7721         /* Parse the constant-expression.  */
7722         expr = cp_parser_constant_expression (parser,
7723                                               /*allow_non_constant_p=*/false,
7724                                               NULL);
7725
7726         ellipsis = cp_lexer_peek_token (parser->lexer);
7727         if (ellipsis->type == CPP_ELLIPSIS)
7728           {
7729             /* Consume the `...' token.  */
7730             cp_lexer_consume_token (parser->lexer);
7731             expr_hi =
7732               cp_parser_constant_expression (parser,
7733                                              /*allow_non_constant_p=*/false,
7734                                              NULL);
7735             /* We don't need to emit warnings here, as the common code
7736                will do this for us.  */
7737           }
7738         else
7739           expr_hi = NULL_TREE;
7740
7741         if (parser->in_switch_statement_p)
7742           finish_case_label (token->location, expr, expr_hi);
7743         else
7744           error_at (token->location,
7745                     "case label %qE not within a switch statement",
7746                     expr);
7747       }
7748       break;
7749
7750     case RID_DEFAULT:
7751       /* Consume the `default' token.  */
7752       cp_lexer_consume_token (parser->lexer);
7753
7754       if (parser->in_switch_statement_p)
7755         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7756       else
7757         error_at (token->location, "case label not within a switch statement");
7758       break;
7759
7760     default:
7761       /* Anything else must be an ordinary label.  */
7762       label = finish_label_stmt (cp_parser_identifier (parser));
7763       break;
7764     }
7765
7766   /* Require the `:' token.  */
7767   cp_parser_require (parser, CPP_COLON, "%<:%>");
7768
7769   /* An ordinary label may optionally be followed by attributes.
7770      However, this is only permitted if the attributes are then
7771      followed by a semicolon.  This is because, for backward
7772      compatibility, when parsing
7773        lab: __attribute__ ((unused)) int i;
7774      we want the attribute to attach to "i", not "lab".  */
7775   if (label != NULL_TREE
7776       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7777     {
7778       tree attrs;
7779
7780       cp_parser_parse_tentatively (parser);
7781       attrs = cp_parser_attributes_opt (parser);
7782       if (attrs == NULL_TREE
7783           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7784         cp_parser_abort_tentative_parse (parser);
7785       else if (!cp_parser_parse_definitely (parser))
7786         ;
7787       else
7788         cplus_decl_attributes (&label, attrs, 0);
7789     }
7790 }
7791
7792 /* Parse an expression-statement.
7793
7794    expression-statement:
7795      expression [opt] ;
7796
7797    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7798    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7799    indicates whether this expression-statement is part of an
7800    expression statement.  */
7801
7802 static tree
7803 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7804 {
7805   tree statement = NULL_TREE;
7806   cp_token *token = cp_lexer_peek_token (parser->lexer);
7807
7808   /* If the next token is a ';', then there is no expression
7809      statement.  */
7810   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7811     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7812
7813   /* Give a helpful message for "A<T>::type t;" and the like.  */
7814   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
7815       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
7816     {
7817       if (TREE_CODE (statement) == SCOPE_REF)
7818         error_at (token->location, "need %<typename%> before %qE because "
7819                   "%qT is a dependent scope",
7820                   statement, TREE_OPERAND (statement, 0));
7821       else if (is_overloaded_fn (statement)
7822                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
7823         {
7824           /* A::A a; */
7825           tree fn = get_first_fn (statement);
7826           error_at (token->location,
7827                     "%<%T::%D%> names the constructor, not the type",
7828                     DECL_CONTEXT (fn), DECL_NAME (fn));
7829         }
7830     }
7831
7832   /* Consume the final `;'.  */
7833   cp_parser_consume_semicolon_at_end_of_statement (parser);
7834
7835   if (in_statement_expr
7836       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7837     /* This is the final expression statement of a statement
7838        expression.  */
7839     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7840   else if (statement)
7841     statement = finish_expr_stmt (statement);
7842   else
7843     finish_stmt ();
7844
7845   return statement;
7846 }
7847
7848 /* Parse a compound-statement.
7849
7850    compound-statement:
7851      { statement-seq [opt] }
7852
7853    GNU extension:
7854
7855    compound-statement:
7856      { label-declaration-seq [opt] statement-seq [opt] }
7857
7858    label-declaration-seq:
7859      label-declaration
7860      label-declaration-seq label-declaration
7861
7862    Returns a tree representing the statement.  */
7863
7864 static tree
7865 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7866                               bool in_try)
7867 {
7868   tree compound_stmt;
7869
7870   /* Consume the `{'.  */
7871   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7872     return error_mark_node;
7873   /* Begin the compound-statement.  */
7874   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7875   /* If the next keyword is `__label__' we have a label declaration.  */
7876   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7877     cp_parser_label_declaration (parser);
7878   /* Parse an (optional) statement-seq.  */
7879   cp_parser_statement_seq_opt (parser, in_statement_expr);
7880   /* Finish the compound-statement.  */
7881   finish_compound_stmt (compound_stmt);
7882   /* Consume the `}'.  */
7883   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7884
7885   return compound_stmt;
7886 }
7887
7888 /* Parse an (optional) statement-seq.
7889
7890    statement-seq:
7891      statement
7892      statement-seq [opt] statement  */
7893
7894 static void
7895 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7896 {
7897   /* Scan statements until there aren't any more.  */
7898   while (true)
7899     {
7900       cp_token *token = cp_lexer_peek_token (parser->lexer);
7901
7902       /* If we're looking at a `}', then we've run out of statements.  */
7903       if (token->type == CPP_CLOSE_BRACE
7904           || token->type == CPP_EOF
7905           || token->type == CPP_PRAGMA_EOL)
7906         break;
7907       
7908       /* If we are in a compound statement and find 'else' then
7909          something went wrong.  */
7910       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7911         {
7912           if (parser->in_statement & IN_IF_STMT) 
7913             break;
7914           else
7915             {
7916               token = cp_lexer_consume_token (parser->lexer);
7917               error_at (token->location, "%<else%> without a previous %<if%>");
7918             }
7919         }
7920
7921       /* Parse the statement.  */
7922       cp_parser_statement (parser, in_statement_expr, true, NULL);
7923     }
7924 }
7925
7926 /* Parse a selection-statement.
7927
7928    selection-statement:
7929      if ( condition ) statement
7930      if ( condition ) statement else statement
7931      switch ( condition ) statement
7932
7933    Returns the new IF_STMT or SWITCH_STMT.
7934
7935    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7936    is a (possibly labeled) if statement which is not enclosed in
7937    braces and has an else clause.  This is used to implement
7938    -Wparentheses.  */
7939
7940 static tree
7941 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7942 {
7943   cp_token *token;
7944   enum rid keyword;
7945
7946   if (if_p != NULL)
7947     *if_p = false;
7948
7949   /* Peek at the next token.  */
7950   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7951
7952   /* See what kind of keyword it is.  */
7953   keyword = token->keyword;
7954   switch (keyword)
7955     {
7956     case RID_IF:
7957     case RID_SWITCH:
7958       {
7959         tree statement;
7960         tree condition;
7961
7962         /* Look for the `('.  */
7963         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7964           {
7965             cp_parser_skip_to_end_of_statement (parser);
7966             return error_mark_node;
7967           }
7968
7969         /* Begin the selection-statement.  */
7970         if (keyword == RID_IF)
7971           statement = begin_if_stmt ();
7972         else
7973           statement = begin_switch_stmt ();
7974
7975         /* Parse the condition.  */
7976         condition = cp_parser_condition (parser);
7977         /* Look for the `)'.  */
7978         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7979           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7980                                                  /*consume_paren=*/true);
7981
7982         if (keyword == RID_IF)
7983           {
7984             bool nested_if;
7985             unsigned char in_statement;
7986
7987             /* Add the condition.  */
7988             finish_if_stmt_cond (condition, statement);
7989
7990             /* Parse the then-clause.  */
7991             in_statement = parser->in_statement;
7992             parser->in_statement |= IN_IF_STMT;
7993             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7994               {
7995                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7996                 add_stmt (build_empty_stmt (loc));
7997                 cp_lexer_consume_token (parser->lexer);
7998                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7999                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8000                               "empty body in an %<if%> statement");
8001                 nested_if = false;
8002               }
8003             else
8004               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8005             parser->in_statement = in_statement;
8006
8007             finish_then_clause (statement);
8008
8009             /* If the next token is `else', parse the else-clause.  */
8010             if (cp_lexer_next_token_is_keyword (parser->lexer,
8011                                                 RID_ELSE))
8012               {
8013                 /* Consume the `else' keyword.  */
8014                 cp_lexer_consume_token (parser->lexer);
8015                 begin_else_clause (statement);
8016                 /* Parse the else-clause.  */
8017                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8018                   {
8019                     location_t loc;
8020                     loc = cp_lexer_peek_token (parser->lexer)->location;
8021                     warning_at (loc,
8022                                 OPT_Wempty_body, "suggest braces around "
8023                                 "empty body in an %<else%> statement");
8024                     add_stmt (build_empty_stmt (loc));
8025                     cp_lexer_consume_token (parser->lexer);
8026                   }
8027                 else
8028                   cp_parser_implicitly_scoped_statement (parser, NULL);
8029
8030                 finish_else_clause (statement);
8031
8032                 /* If we are currently parsing a then-clause, then
8033                    IF_P will not be NULL.  We set it to true to
8034                    indicate that this if statement has an else clause.
8035                    This may trigger the Wparentheses warning below
8036                    when we get back up to the parent if statement.  */
8037                 if (if_p != NULL)
8038                   *if_p = true;
8039               }
8040             else
8041               {
8042                 /* This if statement does not have an else clause.  If
8043                    NESTED_IF is true, then the then-clause is an if
8044                    statement which does have an else clause.  We warn
8045                    about the potential ambiguity.  */
8046                 if (nested_if)
8047                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8048                               "suggest explicit braces to avoid ambiguous"
8049                               " %<else%>");
8050               }
8051
8052             /* Now we're all done with the if-statement.  */
8053             finish_if_stmt (statement);
8054           }
8055         else
8056           {
8057             bool in_switch_statement_p;
8058             unsigned char in_statement;
8059
8060             /* Add the condition.  */
8061             finish_switch_cond (condition, statement);
8062
8063             /* Parse the body of the switch-statement.  */
8064             in_switch_statement_p = parser->in_switch_statement_p;
8065             in_statement = parser->in_statement;
8066             parser->in_switch_statement_p = true;
8067             parser->in_statement |= IN_SWITCH_STMT;
8068             cp_parser_implicitly_scoped_statement (parser, NULL);
8069             parser->in_switch_statement_p = in_switch_statement_p;
8070             parser->in_statement = in_statement;
8071
8072             /* Now we're all done with the switch-statement.  */
8073             finish_switch_stmt (statement);
8074           }
8075
8076         return statement;
8077       }
8078       break;
8079
8080     default:
8081       cp_parser_error (parser, "expected selection-statement");
8082       return error_mark_node;
8083     }
8084 }
8085
8086 /* Parse a condition.
8087
8088    condition:
8089      expression
8090      type-specifier-seq declarator = initializer-clause
8091      type-specifier-seq declarator braced-init-list
8092
8093    GNU Extension:
8094
8095    condition:
8096      type-specifier-seq declarator asm-specification [opt]
8097        attributes [opt] = assignment-expression
8098
8099    Returns the expression that should be tested.  */
8100
8101 static tree
8102 cp_parser_condition (cp_parser* parser)
8103 {
8104   cp_decl_specifier_seq type_specifiers;
8105   const char *saved_message;
8106
8107   /* Try the declaration first.  */
8108   cp_parser_parse_tentatively (parser);
8109   /* New types are not allowed in the type-specifier-seq for a
8110      condition.  */
8111   saved_message = parser->type_definition_forbidden_message;
8112   parser->type_definition_forbidden_message
8113     = G_("types may not be defined in conditions");
8114   /* Parse the type-specifier-seq.  */
8115   cp_parser_type_specifier_seq (parser, /*is_declaration==*/true,
8116                                 /*is_trailing_return=*/false,
8117                                 &type_specifiers);
8118   /* Restore the saved message.  */
8119   parser->type_definition_forbidden_message = saved_message;
8120   /* If all is well, we might be looking at a declaration.  */
8121   if (!cp_parser_error_occurred (parser))
8122     {
8123       tree decl;
8124       tree asm_specification;
8125       tree attributes;
8126       cp_declarator *declarator;
8127       tree initializer = NULL_TREE;
8128
8129       /* Parse the declarator.  */
8130       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8131                                          /*ctor_dtor_or_conv_p=*/NULL,
8132                                          /*parenthesized_p=*/NULL,
8133                                          /*member_p=*/false);
8134       /* Parse the attributes.  */
8135       attributes = cp_parser_attributes_opt (parser);
8136       /* Parse the asm-specification.  */
8137       asm_specification = cp_parser_asm_specification_opt (parser);
8138       /* If the next token is not an `=' or '{', then we might still be
8139          looking at an expression.  For example:
8140
8141            if (A(a).x)
8142
8143          looks like a decl-specifier-seq and a declarator -- but then
8144          there is no `=', so this is an expression.  */
8145       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8146           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8147         cp_parser_simulate_error (parser);
8148         
8149       /* If we did see an `=' or '{', then we are looking at a declaration
8150          for sure.  */
8151       if (cp_parser_parse_definitely (parser))
8152         {
8153           tree pushed_scope;
8154           bool non_constant_p;
8155           bool flags = LOOKUP_ONLYCONVERTING;
8156
8157           /* Create the declaration.  */
8158           decl = start_decl (declarator, &type_specifiers,
8159                              /*initialized_p=*/true,
8160                              attributes, /*prefix_attributes=*/NULL_TREE,
8161                              &pushed_scope);
8162
8163           /* Parse the initializer.  */
8164           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8165             {
8166               initializer = cp_parser_braced_list (parser, &non_constant_p);
8167               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8168               flags = 0;
8169             }
8170           else
8171             {
8172               /* Consume the `='.  */
8173               cp_parser_require (parser, CPP_EQ, "%<=%>");
8174               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8175             }
8176           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8177             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8178
8179           if (!non_constant_p)
8180             initializer = fold_non_dependent_expr (initializer);
8181
8182           /* Process the initializer.  */
8183           cp_finish_decl (decl,
8184                           initializer, !non_constant_p,
8185                           asm_specification,
8186                           flags);
8187
8188           if (pushed_scope)
8189             pop_scope (pushed_scope);
8190
8191           return convert_from_reference (decl);
8192         }
8193     }
8194   /* If we didn't even get past the declarator successfully, we are
8195      definitely not looking at a declaration.  */
8196   else
8197     cp_parser_abort_tentative_parse (parser);
8198
8199   /* Otherwise, we are looking at an expression.  */
8200   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8201 }
8202
8203 /* Parse an iteration-statement.
8204
8205    iteration-statement:
8206      while ( condition ) statement
8207      do statement while ( expression ) ;
8208      for ( for-init-statement condition [opt] ; expression [opt] )
8209        statement
8210
8211    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8212
8213 static tree
8214 cp_parser_iteration_statement (cp_parser* parser)
8215 {
8216   cp_token *token;
8217   enum rid keyword;
8218   tree statement;
8219   unsigned char in_statement;
8220
8221   /* Peek at the next token.  */
8222   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8223   if (!token)
8224     return error_mark_node;
8225
8226   /* Remember whether or not we are already within an iteration
8227      statement.  */
8228   in_statement = parser->in_statement;
8229
8230   /* See what kind of keyword it is.  */
8231   keyword = token->keyword;
8232   switch (keyword)
8233     {
8234     case RID_WHILE:
8235       {
8236         tree condition;
8237
8238         /* Begin the while-statement.  */
8239         statement = begin_while_stmt ();
8240         /* Look for the `('.  */
8241         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8242         /* Parse the condition.  */
8243         condition = cp_parser_condition (parser);
8244         finish_while_stmt_cond (condition, statement);
8245         /* Look for the `)'.  */
8246         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8247         /* Parse the dependent statement.  */
8248         parser->in_statement = IN_ITERATION_STMT;
8249         cp_parser_already_scoped_statement (parser);
8250         parser->in_statement = in_statement;
8251         /* We're done with the while-statement.  */
8252         finish_while_stmt (statement);
8253       }
8254       break;
8255
8256     case RID_DO:
8257       {
8258         tree expression;
8259
8260         /* Begin the do-statement.  */
8261         statement = begin_do_stmt ();
8262         /* Parse the body of the do-statement.  */
8263         parser->in_statement = IN_ITERATION_STMT;
8264         cp_parser_implicitly_scoped_statement (parser, NULL);
8265         parser->in_statement = in_statement;
8266         finish_do_body (statement);
8267         /* Look for the `while' keyword.  */
8268         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8269         /* Look for the `('.  */
8270         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8271         /* Parse the expression.  */
8272         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8273         /* We're done with the do-statement.  */
8274         finish_do_stmt (expression, statement);
8275         /* Look for the `)'.  */
8276         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8277         /* Look for the `;'.  */
8278         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8279       }
8280       break;
8281
8282     case RID_FOR:
8283       {
8284         tree condition = NULL_TREE;
8285         tree expression = NULL_TREE;
8286
8287         /* Begin the for-statement.  */
8288         statement = begin_for_stmt ();
8289         /* Look for the `('.  */
8290         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8291         /* Parse the initialization.  */
8292         cp_parser_for_init_statement (parser);
8293         finish_for_init_stmt (statement);
8294
8295         /* If there's a condition, process it.  */
8296         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8297           condition = cp_parser_condition (parser);
8298         finish_for_cond (condition, statement);
8299         /* Look for the `;'.  */
8300         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8301
8302         /* If there's an expression, process it.  */
8303         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8304           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8305         finish_for_expr (expression, statement);
8306         /* Look for the `)'.  */
8307         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8308
8309         /* Parse the body of the for-statement.  */
8310         parser->in_statement = IN_ITERATION_STMT;
8311         cp_parser_already_scoped_statement (parser);
8312         parser->in_statement = in_statement;
8313
8314         /* We're done with the for-statement.  */
8315         finish_for_stmt (statement);
8316       }
8317       break;
8318
8319     default:
8320       cp_parser_error (parser, "expected iteration-statement");
8321       statement = error_mark_node;
8322       break;
8323     }
8324
8325   return statement;
8326 }
8327
8328 /* Parse a for-init-statement.
8329
8330    for-init-statement:
8331      expression-statement
8332      simple-declaration  */
8333
8334 static void
8335 cp_parser_for_init_statement (cp_parser* parser)
8336 {
8337   /* If the next token is a `;', then we have an empty
8338      expression-statement.  Grammatically, this is also a
8339      simple-declaration, but an invalid one, because it does not
8340      declare anything.  Therefore, if we did not handle this case
8341      specially, we would issue an error message about an invalid
8342      declaration.  */
8343   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8344     {
8345       /* We're going to speculatively look for a declaration, falling back
8346          to an expression, if necessary.  */
8347       cp_parser_parse_tentatively (parser);
8348       /* Parse the declaration.  */
8349       cp_parser_simple_declaration (parser,
8350                                     /*function_definition_allowed_p=*/false);
8351       /* If the tentative parse failed, then we shall need to look for an
8352          expression-statement.  */
8353       if (cp_parser_parse_definitely (parser))
8354         return;
8355     }
8356
8357   cp_parser_expression_statement (parser, NULL_TREE);
8358 }
8359
8360 /* Parse a jump-statement.
8361
8362    jump-statement:
8363      break ;
8364      continue ;
8365      return expression [opt] ;
8366      return braced-init-list ;
8367      goto identifier ;
8368
8369    GNU extension:
8370
8371    jump-statement:
8372      goto * expression ;
8373
8374    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8375
8376 static tree
8377 cp_parser_jump_statement (cp_parser* parser)
8378 {
8379   tree statement = error_mark_node;
8380   cp_token *token;
8381   enum rid keyword;
8382   unsigned char in_statement;
8383
8384   /* Peek at the next token.  */
8385   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8386   if (!token)
8387     return error_mark_node;
8388
8389   /* See what kind of keyword it is.  */
8390   keyword = token->keyword;
8391   switch (keyword)
8392     {
8393     case RID_BREAK:
8394       in_statement = parser->in_statement & ~IN_IF_STMT;      
8395       switch (in_statement)
8396         {
8397         case 0:
8398           error_at (token->location, "break statement not within loop or switch");
8399           break;
8400         default:
8401           gcc_assert ((in_statement & IN_SWITCH_STMT)
8402                       || in_statement == IN_ITERATION_STMT);
8403           statement = finish_break_stmt ();
8404           break;
8405         case IN_OMP_BLOCK:
8406           error_at (token->location, "invalid exit from OpenMP structured block");
8407           break;
8408         case IN_OMP_FOR:
8409           error_at (token->location, "break statement used with OpenMP for loop");
8410           break;
8411         }
8412       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8413       break;
8414
8415     case RID_CONTINUE:
8416       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8417         {
8418         case 0:
8419           error_at (token->location, "continue statement not within a loop");
8420           break;
8421         case IN_ITERATION_STMT:
8422         case IN_OMP_FOR:
8423           statement = finish_continue_stmt ();
8424           break;
8425         case IN_OMP_BLOCK:
8426           error_at (token->location, "invalid exit from OpenMP structured block");
8427           break;
8428         default:
8429           gcc_unreachable ();
8430         }
8431       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8432       break;
8433
8434     case RID_RETURN:
8435       {
8436         tree expr;
8437         bool expr_non_constant_p;
8438
8439         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8440           {
8441             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8442             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8443           }
8444         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8445           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8446         else
8447           /* If the next token is a `;', then there is no
8448              expression.  */
8449           expr = NULL_TREE;
8450         /* Build the return-statement.  */
8451         statement = finish_return_stmt (expr);
8452         /* Look for the final `;'.  */
8453         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8454       }
8455       break;
8456
8457     case RID_GOTO:
8458       /* Create the goto-statement.  */
8459       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8460         {
8461           /* Issue a warning about this use of a GNU extension.  */
8462           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8463           /* Consume the '*' token.  */
8464           cp_lexer_consume_token (parser->lexer);
8465           /* Parse the dependent expression.  */
8466           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8467         }
8468       else
8469         finish_goto_stmt (cp_parser_identifier (parser));
8470       /* Look for the final `;'.  */
8471       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8472       break;
8473
8474     default:
8475       cp_parser_error (parser, "expected jump-statement");
8476       break;
8477     }
8478
8479   return statement;
8480 }
8481
8482 /* Parse a declaration-statement.
8483
8484    declaration-statement:
8485      block-declaration  */
8486
8487 static void
8488 cp_parser_declaration_statement (cp_parser* parser)
8489 {
8490   void *p;
8491
8492   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8493   p = obstack_alloc (&declarator_obstack, 0);
8494
8495  /* Parse the block-declaration.  */
8496   cp_parser_block_declaration (parser, /*statement_p=*/true);
8497
8498   /* Free any declarators allocated.  */
8499   obstack_free (&declarator_obstack, p);
8500
8501   /* Finish off the statement.  */
8502   finish_stmt ();
8503 }
8504
8505 /* Some dependent statements (like `if (cond) statement'), are
8506    implicitly in their own scope.  In other words, if the statement is
8507    a single statement (as opposed to a compound-statement), it is
8508    none-the-less treated as if it were enclosed in braces.  Any
8509    declarations appearing in the dependent statement are out of scope
8510    after control passes that point.  This function parses a statement,
8511    but ensures that is in its own scope, even if it is not a
8512    compound-statement.
8513
8514    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8515    is a (possibly labeled) if statement which is not enclosed in
8516    braces and has an else clause.  This is used to implement
8517    -Wparentheses.
8518
8519    Returns the new statement.  */
8520
8521 static tree
8522 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8523 {
8524   tree statement;
8525
8526   if (if_p != NULL)
8527     *if_p = false;
8528
8529   /* Mark if () ; with a special NOP_EXPR.  */
8530   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8531     {
8532       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8533       cp_lexer_consume_token (parser->lexer);
8534       statement = add_stmt (build_empty_stmt (loc));
8535     }
8536   /* if a compound is opened, we simply parse the statement directly.  */
8537   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8538     statement = cp_parser_compound_statement (parser, NULL, false);
8539   /* If the token is not a `{', then we must take special action.  */
8540   else
8541     {
8542       /* Create a compound-statement.  */
8543       statement = begin_compound_stmt (0);
8544       /* Parse the dependent-statement.  */
8545       cp_parser_statement (parser, NULL_TREE, false, if_p);
8546       /* Finish the dummy compound-statement.  */
8547       finish_compound_stmt (statement);
8548     }
8549
8550   /* Return the statement.  */
8551   return statement;
8552 }
8553
8554 /* For some dependent statements (like `while (cond) statement'), we
8555    have already created a scope.  Therefore, even if the dependent
8556    statement is a compound-statement, we do not want to create another
8557    scope.  */
8558
8559 static void
8560 cp_parser_already_scoped_statement (cp_parser* parser)
8561 {
8562   /* If the token is a `{', then we must take special action.  */
8563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8564     cp_parser_statement (parser, NULL_TREE, false, NULL);
8565   else
8566     {
8567       /* Avoid calling cp_parser_compound_statement, so that we
8568          don't create a new scope.  Do everything else by hand.  */
8569       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8570       /* If the next keyword is `__label__' we have a label declaration.  */
8571       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8572         cp_parser_label_declaration (parser);
8573       /* Parse an (optional) statement-seq.  */
8574       cp_parser_statement_seq_opt (parser, NULL_TREE);
8575       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8576     }
8577 }
8578
8579 /* Declarations [gram.dcl.dcl] */
8580
8581 /* Parse an optional declaration-sequence.
8582
8583    declaration-seq:
8584      declaration
8585      declaration-seq declaration  */
8586
8587 static void
8588 cp_parser_declaration_seq_opt (cp_parser* parser)
8589 {
8590   while (true)
8591     {
8592       cp_token *token;
8593
8594       token = cp_lexer_peek_token (parser->lexer);
8595
8596       if (token->type == CPP_CLOSE_BRACE
8597           || token->type == CPP_EOF
8598           || token->type == CPP_PRAGMA_EOL)
8599         break;
8600
8601       if (token->type == CPP_SEMICOLON)
8602         {
8603           /* A declaration consisting of a single semicolon is
8604              invalid.  Allow it unless we're being pedantic.  */
8605           cp_lexer_consume_token (parser->lexer);
8606           if (!in_system_header)
8607             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8608           continue;
8609         }
8610
8611       /* If we're entering or exiting a region that's implicitly
8612          extern "C", modify the lang context appropriately.  */
8613       if (!parser->implicit_extern_c && token->implicit_extern_c)
8614         {
8615           push_lang_context (lang_name_c);
8616           parser->implicit_extern_c = true;
8617         }
8618       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8619         {
8620           pop_lang_context ();
8621           parser->implicit_extern_c = false;
8622         }
8623
8624       if (token->type == CPP_PRAGMA)
8625         {
8626           /* A top-level declaration can consist solely of a #pragma.
8627              A nested declaration cannot, so this is done here and not
8628              in cp_parser_declaration.  (A #pragma at block scope is
8629              handled in cp_parser_statement.)  */
8630           cp_parser_pragma (parser, pragma_external);
8631           continue;
8632         }
8633
8634       /* Parse the declaration itself.  */
8635       cp_parser_declaration (parser);
8636     }
8637 }
8638
8639 /* Parse a declaration.
8640
8641    declaration:
8642      block-declaration
8643      function-definition
8644      template-declaration
8645      explicit-instantiation
8646      explicit-specialization
8647      linkage-specification
8648      namespace-definition
8649
8650    GNU extension:
8651
8652    declaration:
8653       __extension__ declaration */
8654
8655 static void
8656 cp_parser_declaration (cp_parser* parser)
8657 {
8658   cp_token token1;
8659   cp_token token2;
8660   int saved_pedantic;
8661   void *p;
8662
8663   /* Check for the `__extension__' keyword.  */
8664   if (cp_parser_extension_opt (parser, &saved_pedantic))
8665     {
8666       /* Parse the qualified declaration.  */
8667       cp_parser_declaration (parser);
8668       /* Restore the PEDANTIC flag.  */
8669       pedantic = saved_pedantic;
8670
8671       return;
8672     }
8673
8674   /* Try to figure out what kind of declaration is present.  */
8675   token1 = *cp_lexer_peek_token (parser->lexer);
8676
8677   if (token1.type != CPP_EOF)
8678     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8679   else
8680     {
8681       token2.type = CPP_EOF;
8682       token2.keyword = RID_MAX;
8683     }
8684
8685   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8686   p = obstack_alloc (&declarator_obstack, 0);
8687
8688   /* If the next token is `extern' and the following token is a string
8689      literal, then we have a linkage specification.  */
8690   if (token1.keyword == RID_EXTERN
8691       && cp_parser_is_string_literal (&token2))
8692     cp_parser_linkage_specification (parser);
8693   /* If the next token is `template', then we have either a template
8694      declaration, an explicit instantiation, or an explicit
8695      specialization.  */
8696   else if (token1.keyword == RID_TEMPLATE)
8697     {
8698       /* `template <>' indicates a template specialization.  */
8699       if (token2.type == CPP_LESS
8700           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8701         cp_parser_explicit_specialization (parser);
8702       /* `template <' indicates a template declaration.  */
8703       else if (token2.type == CPP_LESS)
8704         cp_parser_template_declaration (parser, /*member_p=*/false);
8705       /* Anything else must be an explicit instantiation.  */
8706       else
8707         cp_parser_explicit_instantiation (parser);
8708     }
8709   /* If the next token is `export', then we have a template
8710      declaration.  */
8711   else if (token1.keyword == RID_EXPORT)
8712     cp_parser_template_declaration (parser, /*member_p=*/false);
8713   /* If the next token is `extern', 'static' or 'inline' and the one
8714      after that is `template', we have a GNU extended explicit
8715      instantiation directive.  */
8716   else if (cp_parser_allow_gnu_extensions_p (parser)
8717            && (token1.keyword == RID_EXTERN
8718                || token1.keyword == RID_STATIC
8719                || token1.keyword == RID_INLINE)
8720            && token2.keyword == RID_TEMPLATE)
8721     cp_parser_explicit_instantiation (parser);
8722   /* If the next token is `namespace', check for a named or unnamed
8723      namespace definition.  */
8724   else if (token1.keyword == RID_NAMESPACE
8725            && (/* A named namespace definition.  */
8726                (token2.type == CPP_NAME
8727                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8728                     != CPP_EQ))
8729                /* An unnamed namespace definition.  */
8730                || token2.type == CPP_OPEN_BRACE
8731                || token2.keyword == RID_ATTRIBUTE))
8732     cp_parser_namespace_definition (parser);
8733   /* An inline (associated) namespace definition.  */
8734   else if (token1.keyword == RID_INLINE
8735            && token2.keyword == RID_NAMESPACE)
8736     cp_parser_namespace_definition (parser);
8737   /* Objective-C++ declaration/definition.  */
8738   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8739     cp_parser_objc_declaration (parser);
8740   /* We must have either a block declaration or a function
8741      definition.  */
8742   else
8743     /* Try to parse a block-declaration, or a function-definition.  */
8744     cp_parser_block_declaration (parser, /*statement_p=*/false);
8745
8746   /* Free any declarators allocated.  */
8747   obstack_free (&declarator_obstack, p);
8748 }
8749
8750 /* Parse a block-declaration.
8751
8752    block-declaration:
8753      simple-declaration
8754      asm-definition
8755      namespace-alias-definition
8756      using-declaration
8757      using-directive
8758
8759    GNU Extension:
8760
8761    block-declaration:
8762      __extension__ block-declaration
8763
8764    C++0x Extension:
8765
8766    block-declaration:
8767      static_assert-declaration
8768
8769    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8770    part of a declaration-statement.  */
8771
8772 static void
8773 cp_parser_block_declaration (cp_parser *parser,
8774                              bool      statement_p)
8775 {
8776   cp_token *token1;
8777   int saved_pedantic;
8778
8779   /* Check for the `__extension__' keyword.  */
8780   if (cp_parser_extension_opt (parser, &saved_pedantic))
8781     {
8782       /* Parse the qualified declaration.  */
8783       cp_parser_block_declaration (parser, statement_p);
8784       /* Restore the PEDANTIC flag.  */
8785       pedantic = saved_pedantic;
8786
8787       return;
8788     }
8789
8790   /* Peek at the next token to figure out which kind of declaration is
8791      present.  */
8792   token1 = cp_lexer_peek_token (parser->lexer);
8793
8794   /* If the next keyword is `asm', we have an asm-definition.  */
8795   if (token1->keyword == RID_ASM)
8796     {
8797       if (statement_p)
8798         cp_parser_commit_to_tentative_parse (parser);
8799       cp_parser_asm_definition (parser);
8800     }
8801   /* If the next keyword is `namespace', we have a
8802      namespace-alias-definition.  */
8803   else if (token1->keyword == RID_NAMESPACE)
8804     cp_parser_namespace_alias_definition (parser);
8805   /* If the next keyword is `using', we have either a
8806      using-declaration or a using-directive.  */
8807   else if (token1->keyword == RID_USING)
8808     {
8809       cp_token *token2;
8810
8811       if (statement_p)
8812         cp_parser_commit_to_tentative_parse (parser);
8813       /* If the token after `using' is `namespace', then we have a
8814          using-directive.  */
8815       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8816       if (token2->keyword == RID_NAMESPACE)
8817         cp_parser_using_directive (parser);
8818       /* Otherwise, it's a using-declaration.  */
8819       else
8820         cp_parser_using_declaration (parser,
8821                                      /*access_declaration_p=*/false);
8822     }
8823   /* If the next keyword is `__label__' we have a misplaced label
8824      declaration.  */
8825   else if (token1->keyword == RID_LABEL)
8826     {
8827       cp_lexer_consume_token (parser->lexer);
8828       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8829       cp_parser_skip_to_end_of_statement (parser);
8830       /* If the next token is now a `;', consume it.  */
8831       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8832         cp_lexer_consume_token (parser->lexer);
8833     }
8834   /* If the next token is `static_assert' we have a static assertion.  */
8835   else if (token1->keyword == RID_STATIC_ASSERT)
8836     cp_parser_static_assert (parser, /*member_p=*/false);
8837   /* Anything else must be a simple-declaration.  */
8838   else
8839     cp_parser_simple_declaration (parser, !statement_p);
8840 }
8841
8842 /* Parse a simple-declaration.
8843
8844    simple-declaration:
8845      decl-specifier-seq [opt] init-declarator-list [opt] ;
8846
8847    init-declarator-list:
8848      init-declarator
8849      init-declarator-list , init-declarator
8850
8851    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8852    function-definition as a simple-declaration.  */
8853
8854 static void
8855 cp_parser_simple_declaration (cp_parser* parser,
8856                               bool function_definition_allowed_p)
8857 {
8858   cp_decl_specifier_seq decl_specifiers;
8859   int declares_class_or_enum;
8860   bool saw_declarator;
8861
8862   /* Defer access checks until we know what is being declared; the
8863      checks for names appearing in the decl-specifier-seq should be
8864      done as if we were in the scope of the thing being declared.  */
8865   push_deferring_access_checks (dk_deferred);
8866
8867   /* Parse the decl-specifier-seq.  We have to keep track of whether
8868      or not the decl-specifier-seq declares a named class or
8869      enumeration type, since that is the only case in which the
8870      init-declarator-list is allowed to be empty.
8871
8872      [dcl.dcl]
8873
8874      In a simple-declaration, the optional init-declarator-list can be
8875      omitted only when declaring a class or enumeration, that is when
8876      the decl-specifier-seq contains either a class-specifier, an
8877      elaborated-type-specifier, or an enum-specifier.  */
8878   cp_parser_decl_specifier_seq (parser,
8879                                 CP_PARSER_FLAGS_OPTIONAL,
8880                                 &decl_specifiers,
8881                                 &declares_class_or_enum);
8882   /* We no longer need to defer access checks.  */
8883   stop_deferring_access_checks ();
8884
8885   /* In a block scope, a valid declaration must always have a
8886      decl-specifier-seq.  By not trying to parse declarators, we can
8887      resolve the declaration/expression ambiguity more quickly.  */
8888   if (!function_definition_allowed_p
8889       && !decl_specifiers.any_specifiers_p)
8890     {
8891       cp_parser_error (parser, "expected declaration");
8892       goto done;
8893     }
8894
8895   /* If the next two tokens are both identifiers, the code is
8896      erroneous. The usual cause of this situation is code like:
8897
8898        T t;
8899
8900      where "T" should name a type -- but does not.  */
8901   if (!decl_specifiers.any_type_specifiers_p
8902       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8903     {
8904       /* If parsing tentatively, we should commit; we really are
8905          looking at a declaration.  */
8906       cp_parser_commit_to_tentative_parse (parser);
8907       /* Give up.  */
8908       goto done;
8909     }
8910
8911   /* If we have seen at least one decl-specifier, and the next token
8912      is not a parenthesis, then we must be looking at a declaration.
8913      (After "int (" we might be looking at a functional cast.)  */
8914   if (decl_specifiers.any_specifiers_p
8915       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8916       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8917       && !cp_parser_error_occurred (parser))
8918     cp_parser_commit_to_tentative_parse (parser);
8919
8920   /* Keep going until we hit the `;' at the end of the simple
8921      declaration.  */
8922   saw_declarator = false;
8923   while (cp_lexer_next_token_is_not (parser->lexer,
8924                                      CPP_SEMICOLON))
8925     {
8926       cp_token *token;
8927       bool function_definition_p;
8928       tree decl;
8929
8930       if (saw_declarator)
8931         {
8932           /* If we are processing next declarator, coma is expected */
8933           token = cp_lexer_peek_token (parser->lexer);
8934           gcc_assert (token->type == CPP_COMMA);
8935           cp_lexer_consume_token (parser->lexer);
8936         }
8937       else
8938         saw_declarator = true;
8939
8940       /* Parse the init-declarator.  */
8941       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8942                                         /*checks=*/NULL,
8943                                         function_definition_allowed_p,
8944                                         /*member_p=*/false,
8945                                         declares_class_or_enum,
8946                                         &function_definition_p);
8947       /* If an error occurred while parsing tentatively, exit quickly.
8948          (That usually happens when in the body of a function; each
8949          statement is treated as a declaration-statement until proven
8950          otherwise.)  */
8951       if (cp_parser_error_occurred (parser))
8952         goto done;
8953       /* Handle function definitions specially.  */
8954       if (function_definition_p)
8955         {
8956           /* If the next token is a `,', then we are probably
8957              processing something like:
8958
8959                void f() {}, *p;
8960
8961              which is erroneous.  */
8962           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8963             {
8964               cp_token *token = cp_lexer_peek_token (parser->lexer);
8965               error_at (token->location,
8966                         "mixing"
8967                         " declarations and function-definitions is forbidden");
8968             }
8969           /* Otherwise, we're done with the list of declarators.  */
8970           else
8971             {
8972               pop_deferring_access_checks ();
8973               return;
8974             }
8975         }
8976       /* The next token should be either a `,' or a `;'.  */
8977       token = cp_lexer_peek_token (parser->lexer);
8978       /* If it's a `,', there are more declarators to come.  */
8979       if (token->type == CPP_COMMA)
8980         /* will be consumed next time around */;
8981       /* If it's a `;', we are done.  */
8982       else if (token->type == CPP_SEMICOLON)
8983         break;
8984       /* Anything else is an error.  */
8985       else
8986         {
8987           /* If we have already issued an error message we don't need
8988              to issue another one.  */
8989           if (decl != error_mark_node
8990               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8991             cp_parser_error (parser, "expected %<,%> or %<;%>");
8992           /* Skip tokens until we reach the end of the statement.  */
8993           cp_parser_skip_to_end_of_statement (parser);
8994           /* If the next token is now a `;', consume it.  */
8995           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8996             cp_lexer_consume_token (parser->lexer);
8997           goto done;
8998         }
8999       /* After the first time around, a function-definition is not
9000          allowed -- even if it was OK at first.  For example:
9001
9002            int i, f() {}
9003
9004          is not valid.  */
9005       function_definition_allowed_p = false;
9006     }
9007
9008   /* Issue an error message if no declarators are present, and the
9009      decl-specifier-seq does not itself declare a class or
9010      enumeration.  */
9011   if (!saw_declarator)
9012     {
9013       if (cp_parser_declares_only_class_p (parser))
9014         shadow_tag (&decl_specifiers);
9015       /* Perform any deferred access checks.  */
9016       perform_deferred_access_checks ();
9017     }
9018
9019   /* Consume the `;'.  */
9020   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9021
9022  done:
9023   pop_deferring_access_checks ();
9024 }
9025
9026 /* Parse a decl-specifier-seq.
9027
9028    decl-specifier-seq:
9029      decl-specifier-seq [opt] decl-specifier
9030
9031    decl-specifier:
9032      storage-class-specifier
9033      type-specifier
9034      function-specifier
9035      friend
9036      typedef
9037
9038    GNU Extension:
9039
9040    decl-specifier:
9041      attributes
9042
9043    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9044
9045    The parser flags FLAGS is used to control type-specifier parsing.
9046
9047    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9048    flags:
9049
9050      1: one of the decl-specifiers is an elaborated-type-specifier
9051         (i.e., a type declaration)
9052      2: one of the decl-specifiers is an enum-specifier or a
9053         class-specifier (i.e., a type definition)
9054
9055    */
9056
9057 static void
9058 cp_parser_decl_specifier_seq (cp_parser* parser,
9059                               cp_parser_flags flags,
9060                               cp_decl_specifier_seq *decl_specs,
9061                               int* declares_class_or_enum)
9062 {
9063   bool constructor_possible_p = !parser->in_declarator_p;
9064   cp_token *start_token = NULL;
9065
9066   /* Clear DECL_SPECS.  */
9067   clear_decl_specs (decl_specs);
9068
9069   /* Assume no class or enumeration type is declared.  */
9070   *declares_class_or_enum = 0;
9071
9072   /* Keep reading specifiers until there are no more to read.  */
9073   while (true)
9074     {
9075       bool constructor_p;
9076       bool found_decl_spec;
9077       cp_token *token;
9078
9079       /* Peek at the next token.  */
9080       token = cp_lexer_peek_token (parser->lexer);
9081
9082       /* Save the first token of the decl spec list for error
9083          reporting.  */
9084       if (!start_token)
9085         start_token = token;
9086       /* Handle attributes.  */
9087       if (token->keyword == RID_ATTRIBUTE)
9088         {
9089           /* Parse the attributes.  */
9090           decl_specs->attributes
9091             = chainon (decl_specs->attributes,
9092                        cp_parser_attributes_opt (parser));
9093           continue;
9094         }
9095       /* Assume we will find a decl-specifier keyword.  */
9096       found_decl_spec = true;
9097       /* If the next token is an appropriate keyword, we can simply
9098          add it to the list.  */
9099       switch (token->keyword)
9100         {
9101           /* decl-specifier:
9102                friend
9103                constexpr */
9104         case RID_FRIEND:
9105           if (!at_class_scope_p ())
9106             {
9107               error_at (token->location, "%<friend%> used outside of class");
9108               cp_lexer_purge_token (parser->lexer);
9109             }
9110           else
9111             {
9112               ++decl_specs->specs[(int) ds_friend];
9113               /* Consume the token.  */
9114               cp_lexer_consume_token (parser->lexer);
9115             }
9116           break;
9117
9118         case RID_CONSTEXPR:
9119           ++decl_specs->specs[(int) ds_constexpr];
9120           cp_lexer_consume_token (parser->lexer);
9121           break;
9122
9123           /* function-specifier:
9124                inline
9125                virtual
9126                explicit  */
9127         case RID_INLINE:
9128         case RID_VIRTUAL:
9129         case RID_EXPLICIT:
9130           cp_parser_function_specifier_opt (parser, decl_specs);
9131           break;
9132
9133           /* decl-specifier:
9134                typedef  */
9135         case RID_TYPEDEF:
9136           ++decl_specs->specs[(int) ds_typedef];
9137           /* Consume the token.  */
9138           cp_lexer_consume_token (parser->lexer);
9139           /* A constructor declarator cannot appear in a typedef.  */
9140           constructor_possible_p = false;
9141           /* The "typedef" keyword can only occur in a declaration; we
9142              may as well commit at this point.  */
9143           cp_parser_commit_to_tentative_parse (parser);
9144
9145           if (decl_specs->storage_class != sc_none)
9146             decl_specs->conflicting_specifiers_p = true;
9147           break;
9148
9149           /* storage-class-specifier:
9150                auto
9151                register
9152                static
9153                extern
9154                mutable
9155
9156              GNU Extension:
9157                thread  */
9158         case RID_AUTO:
9159           if (cxx_dialect == cxx98) 
9160             {
9161               /* Consume the token.  */
9162               cp_lexer_consume_token (parser->lexer);
9163
9164               /* Complain about `auto' as a storage specifier, if
9165                  we're complaining about C++0x compatibility.  */
9166               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9167                           " will change meaning in C++0x; please remove it");
9168
9169               /* Set the storage class anyway.  */
9170               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9171                                            token->location);
9172             }
9173           else
9174             /* C++0x auto type-specifier.  */
9175             found_decl_spec = false;
9176           break;
9177
9178         case RID_REGISTER:
9179         case RID_STATIC:
9180         case RID_EXTERN:
9181         case RID_MUTABLE:
9182           /* Consume the token.  */
9183           cp_lexer_consume_token (parser->lexer);
9184           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9185                                        token->location);
9186           break;
9187         case RID_THREAD:
9188           /* Consume the token.  */
9189           cp_lexer_consume_token (parser->lexer);
9190           ++decl_specs->specs[(int) ds_thread];
9191           break;
9192
9193         default:
9194           /* We did not yet find a decl-specifier yet.  */
9195           found_decl_spec = false;
9196           break;
9197         }
9198
9199       /* Constructors are a special case.  The `S' in `S()' is not a
9200          decl-specifier; it is the beginning of the declarator.  */
9201       constructor_p
9202         = (!found_decl_spec
9203            && constructor_possible_p
9204            && (cp_parser_constructor_declarator_p
9205                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9206
9207       /* If we don't have a DECL_SPEC yet, then we must be looking at
9208          a type-specifier.  */
9209       if (!found_decl_spec && !constructor_p)
9210         {
9211           int decl_spec_declares_class_or_enum;
9212           bool is_cv_qualifier;
9213           tree type_spec;
9214
9215           type_spec
9216             = cp_parser_type_specifier (parser, flags,
9217                                         decl_specs,
9218                                         /*is_declaration=*/true,
9219                                         &decl_spec_declares_class_or_enum,
9220                                         &is_cv_qualifier);
9221           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9222
9223           /* If this type-specifier referenced a user-defined type
9224              (a typedef, class-name, etc.), then we can't allow any
9225              more such type-specifiers henceforth.
9226
9227              [dcl.spec]
9228
9229              The longest sequence of decl-specifiers that could
9230              possibly be a type name is taken as the
9231              decl-specifier-seq of a declaration.  The sequence shall
9232              be self-consistent as described below.
9233
9234              [dcl.type]
9235
9236              As a general rule, at most one type-specifier is allowed
9237              in the complete decl-specifier-seq of a declaration.  The
9238              only exceptions are the following:
9239
9240              -- const or volatile can be combined with any other
9241                 type-specifier.
9242
9243              -- signed or unsigned can be combined with char, long,
9244                 short, or int.
9245
9246              -- ..
9247
9248              Example:
9249
9250                typedef char* Pc;
9251                void g (const int Pc);
9252
9253              Here, Pc is *not* part of the decl-specifier seq; it's
9254              the declarator.  Therefore, once we see a type-specifier
9255              (other than a cv-qualifier), we forbid any additional
9256              user-defined types.  We *do* still allow things like `int
9257              int' to be considered a decl-specifier-seq, and issue the
9258              error message later.  */
9259           if (type_spec && !is_cv_qualifier)
9260             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9261           /* A constructor declarator cannot follow a type-specifier.  */
9262           if (type_spec)
9263             {
9264               constructor_possible_p = false;
9265               found_decl_spec = true;
9266               if (!is_cv_qualifier)
9267                 decl_specs->any_type_specifiers_p = true;
9268             }
9269         }
9270
9271       /* If we still do not have a DECL_SPEC, then there are no more
9272          decl-specifiers.  */
9273       if (!found_decl_spec)
9274         break;
9275
9276       decl_specs->any_specifiers_p = true;
9277       /* After we see one decl-specifier, further decl-specifiers are
9278          always optional.  */
9279       flags |= CP_PARSER_FLAGS_OPTIONAL;
9280     }
9281
9282   cp_parser_check_decl_spec (decl_specs, start_token->location);
9283
9284   /* Don't allow a friend specifier with a class definition.  */
9285   if (decl_specs->specs[(int) ds_friend] != 0
9286       && (*declares_class_or_enum & 2))
9287     error_at (start_token->location,
9288               "class definition may not be declared a friend");
9289 }
9290
9291 /* Parse an (optional) storage-class-specifier.
9292
9293    storage-class-specifier:
9294      auto
9295      register
9296      static
9297      extern
9298      mutable
9299
9300    GNU Extension:
9301
9302    storage-class-specifier:
9303      thread
9304
9305    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9306
9307 static tree
9308 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9309 {
9310   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9311     {
9312     case RID_AUTO:
9313       if (cxx_dialect != cxx98)
9314         return NULL_TREE;
9315       /* Fall through for C++98.  */
9316
9317     case RID_REGISTER:
9318     case RID_STATIC:
9319     case RID_EXTERN:
9320     case RID_MUTABLE:
9321     case RID_THREAD:
9322       /* Consume the token.  */
9323       return cp_lexer_consume_token (parser->lexer)->u.value;
9324
9325     default:
9326       return NULL_TREE;
9327     }
9328 }
9329
9330 /* Parse an (optional) function-specifier.
9331
9332    function-specifier:
9333      inline
9334      virtual
9335      explicit
9336
9337    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9338    Updates DECL_SPECS, if it is non-NULL.  */
9339
9340 static tree
9341 cp_parser_function_specifier_opt (cp_parser* parser,
9342                                   cp_decl_specifier_seq *decl_specs)
9343 {
9344   cp_token *token = cp_lexer_peek_token (parser->lexer);
9345   switch (token->keyword)
9346     {
9347     case RID_INLINE:
9348       if (decl_specs)
9349         ++decl_specs->specs[(int) ds_inline];
9350       break;
9351
9352     case RID_VIRTUAL:
9353       /* 14.5.2.3 [temp.mem]
9354
9355          A member function template shall not be virtual.  */
9356       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9357         error_at (token->location, "templates may not be %<virtual%>");
9358       else if (decl_specs)
9359         ++decl_specs->specs[(int) ds_virtual];
9360       break;
9361
9362     case RID_EXPLICIT:
9363       if (decl_specs)
9364         ++decl_specs->specs[(int) ds_explicit];
9365       break;
9366
9367     default:
9368       return NULL_TREE;
9369     }
9370
9371   /* Consume the token.  */
9372   return cp_lexer_consume_token (parser->lexer)->u.value;
9373 }
9374
9375 /* Parse a linkage-specification.
9376
9377    linkage-specification:
9378      extern string-literal { declaration-seq [opt] }
9379      extern string-literal declaration  */
9380
9381 static void
9382 cp_parser_linkage_specification (cp_parser* parser)
9383 {
9384   tree linkage;
9385
9386   /* Look for the `extern' keyword.  */
9387   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9388
9389   /* Look for the string-literal.  */
9390   linkage = cp_parser_string_literal (parser, false, false);
9391
9392   /* Transform the literal into an identifier.  If the literal is a
9393      wide-character string, or contains embedded NULs, then we can't
9394      handle it as the user wants.  */
9395   if (strlen (TREE_STRING_POINTER (linkage))
9396       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9397     {
9398       cp_parser_error (parser, "invalid linkage-specification");
9399       /* Assume C++ linkage.  */
9400       linkage = lang_name_cplusplus;
9401     }
9402   else
9403     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9404
9405   /* We're now using the new linkage.  */
9406   push_lang_context (linkage);
9407
9408   /* If the next token is a `{', then we're using the first
9409      production.  */
9410   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9411     {
9412       /* Consume the `{' token.  */
9413       cp_lexer_consume_token (parser->lexer);
9414       /* Parse the declarations.  */
9415       cp_parser_declaration_seq_opt (parser);
9416       /* Look for the closing `}'.  */
9417       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9418     }
9419   /* Otherwise, there's just one declaration.  */
9420   else
9421     {
9422       bool saved_in_unbraced_linkage_specification_p;
9423
9424       saved_in_unbraced_linkage_specification_p
9425         = parser->in_unbraced_linkage_specification_p;
9426       parser->in_unbraced_linkage_specification_p = true;
9427       cp_parser_declaration (parser);
9428       parser->in_unbraced_linkage_specification_p
9429         = saved_in_unbraced_linkage_specification_p;
9430     }
9431
9432   /* We're done with the linkage-specification.  */
9433   pop_lang_context ();
9434 }
9435
9436 /* Parse a static_assert-declaration.
9437
9438    static_assert-declaration:
9439      static_assert ( constant-expression , string-literal ) ; 
9440
9441    If MEMBER_P, this static_assert is a class member.  */
9442
9443 static void 
9444 cp_parser_static_assert(cp_parser *parser, bool member_p)
9445 {
9446   tree condition;
9447   tree message;
9448   cp_token *token;
9449   location_t saved_loc;
9450
9451   /* Peek at the `static_assert' token so we can keep track of exactly
9452      where the static assertion started.  */
9453   token = cp_lexer_peek_token (parser->lexer);
9454   saved_loc = token->location;
9455
9456   /* Look for the `static_assert' keyword.  */
9457   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9458                                   "%<static_assert%>"))
9459     return;
9460
9461   /*  We know we are in a static assertion; commit to any tentative
9462       parse.  */
9463   if (cp_parser_parsing_tentatively (parser))
9464     cp_parser_commit_to_tentative_parse (parser);
9465
9466   /* Parse the `(' starting the static assertion condition.  */
9467   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9468
9469   /* Parse the constant-expression.  */
9470   condition = 
9471     cp_parser_constant_expression (parser,
9472                                    /*allow_non_constant_p=*/false,
9473                                    /*non_constant_p=*/NULL);
9474
9475   /* Parse the separating `,'.  */
9476   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9477
9478   /* Parse the string-literal message.  */
9479   message = cp_parser_string_literal (parser, 
9480                                       /*translate=*/false,
9481                                       /*wide_ok=*/true);
9482
9483   /* A `)' completes the static assertion.  */
9484   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9485     cp_parser_skip_to_closing_parenthesis (parser, 
9486                                            /*recovering=*/true, 
9487                                            /*or_comma=*/false,
9488                                            /*consume_paren=*/true);
9489
9490   /* A semicolon terminates the declaration.  */
9491   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9492
9493   /* Complete the static assertion, which may mean either processing 
9494      the static assert now or saving it for template instantiation.  */
9495   finish_static_assert (condition, message, saved_loc, member_p);
9496 }
9497
9498 /* Parse a `decltype' type. Returns the type. 
9499
9500    simple-type-specifier:
9501      decltype ( expression )  */
9502
9503 static tree
9504 cp_parser_decltype (cp_parser *parser)
9505 {
9506   tree expr;
9507   bool id_expression_or_member_access_p = false;
9508   const char *saved_message;
9509   bool saved_integral_constant_expression_p;
9510   bool saved_non_integral_constant_expression_p;
9511   cp_token *id_expr_start_token;
9512
9513   /* Look for the `decltype' token.  */
9514   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9515     return error_mark_node;
9516
9517   /* Types cannot be defined in a `decltype' expression.  Save away the
9518      old message.  */
9519   saved_message = parser->type_definition_forbidden_message;
9520
9521   /* And create the new one.  */
9522   parser->type_definition_forbidden_message
9523     = G_("types may not be defined in %<decltype%> expressions");
9524
9525   /* The restrictions on constant-expressions do not apply inside
9526      decltype expressions.  */
9527   saved_integral_constant_expression_p
9528     = parser->integral_constant_expression_p;
9529   saved_non_integral_constant_expression_p
9530     = parser->non_integral_constant_expression_p;
9531   parser->integral_constant_expression_p = false;
9532
9533   /* Do not actually evaluate the expression.  */
9534   ++cp_unevaluated_operand;
9535
9536   /* Do not warn about problems with the expression.  */
9537   ++c_inhibit_evaluation_warnings;
9538
9539   /* Parse the opening `('.  */
9540   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9541     return error_mark_node;
9542   
9543   /* First, try parsing an id-expression.  */
9544   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9545   cp_parser_parse_tentatively (parser);
9546   expr = cp_parser_id_expression (parser,
9547                                   /*template_keyword_p=*/false,
9548                                   /*check_dependency_p=*/true,
9549                                   /*template_p=*/NULL,
9550                                   /*declarator_p=*/false,
9551                                   /*optional_p=*/false);
9552
9553   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9554     {
9555       bool non_integral_constant_expression_p = false;
9556       tree id_expression = expr;
9557       cp_id_kind idk;
9558       const char *error_msg;
9559
9560       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9561         /* Lookup the name we got back from the id-expression.  */
9562         expr = cp_parser_lookup_name (parser, expr,
9563                                       none_type,
9564                                       /*is_template=*/false,
9565                                       /*is_namespace=*/false,
9566                                       /*check_dependency=*/true,
9567                                       /*ambiguous_decls=*/NULL,
9568                                       id_expr_start_token->location);
9569
9570       if (expr
9571           && expr != error_mark_node
9572           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9573           && TREE_CODE (expr) != TYPE_DECL
9574           && (TREE_CODE (expr) != BIT_NOT_EXPR
9575               || !TYPE_P (TREE_OPERAND (expr, 0)))
9576           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9577         {
9578           /* Complete lookup of the id-expression.  */
9579           expr = (finish_id_expression
9580                   (id_expression, expr, parser->scope, &idk,
9581                    /*integral_constant_expression_p=*/false,
9582                    /*allow_non_integral_constant_expression_p=*/true,
9583                    &non_integral_constant_expression_p,
9584                    /*template_p=*/false,
9585                    /*done=*/true,
9586                    /*address_p=*/false,
9587                    /*template_arg_p=*/false,
9588                    &error_msg,
9589                    id_expr_start_token->location));
9590
9591           if (expr == error_mark_node)
9592             /* We found an id-expression, but it was something that we
9593                should not have found. This is an error, not something
9594                we can recover from, so note that we found an
9595                id-expression and we'll recover as gracefully as
9596                possible.  */
9597             id_expression_or_member_access_p = true;
9598         }
9599
9600       if (expr 
9601           && expr != error_mark_node
9602           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9603         /* We have an id-expression.  */
9604         id_expression_or_member_access_p = true;
9605     }
9606
9607   if (!id_expression_or_member_access_p)
9608     {
9609       /* Abort the id-expression parse.  */
9610       cp_parser_abort_tentative_parse (parser);
9611
9612       /* Parsing tentatively, again.  */
9613       cp_parser_parse_tentatively (parser);
9614
9615       /* Parse a class member access.  */
9616       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9617                                            /*cast_p=*/false,
9618                                            /*member_access_only_p=*/true, NULL);
9619
9620       if (expr 
9621           && expr != error_mark_node
9622           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9623         /* We have an id-expression.  */
9624         id_expression_or_member_access_p = true;
9625     }
9626
9627   if (id_expression_or_member_access_p)
9628     /* We have parsed the complete id-expression or member access.  */
9629     cp_parser_parse_definitely (parser);
9630   else
9631     {
9632       bool saved_greater_than_is_operator_p;
9633
9634       /* Abort our attempt to parse an id-expression or member access
9635          expression.  */
9636       cp_parser_abort_tentative_parse (parser);
9637
9638       /* Within a parenthesized expression, a `>' token is always
9639          the greater-than operator.  */
9640       saved_greater_than_is_operator_p
9641         = parser->greater_than_is_operator_p;
9642       parser->greater_than_is_operator_p = true;
9643
9644       /* Parse a full expression.  */
9645       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9646
9647       /* The `>' token might be the end of a template-id or
9648          template-parameter-list now.  */
9649       parser->greater_than_is_operator_p
9650         = saved_greater_than_is_operator_p;
9651     }
9652
9653   /* Go back to evaluating expressions.  */
9654   --cp_unevaluated_operand;
9655   --c_inhibit_evaluation_warnings;
9656
9657   /* Restore the old message and the integral constant expression
9658      flags.  */
9659   parser->type_definition_forbidden_message = saved_message;
9660   parser->integral_constant_expression_p
9661     = saved_integral_constant_expression_p;
9662   parser->non_integral_constant_expression_p
9663     = saved_non_integral_constant_expression_p;
9664
9665   if (expr == error_mark_node)
9666     {
9667       /* Skip everything up to the closing `)'.  */
9668       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9669                                              /*consume_paren=*/true);
9670       return error_mark_node;
9671     }
9672   
9673   /* Parse to the closing `)'.  */
9674   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9675     {
9676       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9677                                              /*consume_paren=*/true);
9678       return error_mark_node;
9679     }
9680
9681   return finish_decltype_type (expr, id_expression_or_member_access_p);
9682 }
9683
9684 /* Special member functions [gram.special] */
9685
9686 /* Parse a conversion-function-id.
9687
9688    conversion-function-id:
9689      operator conversion-type-id
9690
9691    Returns an IDENTIFIER_NODE representing the operator.  */
9692
9693 static tree
9694 cp_parser_conversion_function_id (cp_parser* parser)
9695 {
9696   tree type;
9697   tree saved_scope;
9698   tree saved_qualifying_scope;
9699   tree saved_object_scope;
9700   tree pushed_scope = NULL_TREE;
9701
9702   /* Look for the `operator' token.  */
9703   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9704     return error_mark_node;
9705   /* When we parse the conversion-type-id, the current scope will be
9706      reset.  However, we need that information in able to look up the
9707      conversion function later, so we save it here.  */
9708   saved_scope = parser->scope;
9709   saved_qualifying_scope = parser->qualifying_scope;
9710   saved_object_scope = parser->object_scope;
9711   /* We must enter the scope of the class so that the names of
9712      entities declared within the class are available in the
9713      conversion-type-id.  For example, consider:
9714
9715        struct S {
9716          typedef int I;
9717          operator I();
9718        };
9719
9720        S::operator I() { ... }
9721
9722      In order to see that `I' is a type-name in the definition, we
9723      must be in the scope of `S'.  */
9724   if (saved_scope)
9725     pushed_scope = push_scope (saved_scope);
9726   /* Parse the conversion-type-id.  */
9727   type = cp_parser_conversion_type_id (parser);
9728   /* Leave the scope of the class, if any.  */
9729   if (pushed_scope)
9730     pop_scope (pushed_scope);
9731   /* Restore the saved scope.  */
9732   parser->scope = saved_scope;
9733   parser->qualifying_scope = saved_qualifying_scope;
9734   parser->object_scope = saved_object_scope;
9735   /* If the TYPE is invalid, indicate failure.  */
9736   if (type == error_mark_node)
9737     return error_mark_node;
9738   return mangle_conv_op_name_for_type (type);
9739 }
9740
9741 /* Parse a conversion-type-id:
9742
9743    conversion-type-id:
9744      type-specifier-seq conversion-declarator [opt]
9745
9746    Returns the TYPE specified.  */
9747
9748 static tree
9749 cp_parser_conversion_type_id (cp_parser* parser)
9750 {
9751   tree attributes;
9752   cp_decl_specifier_seq type_specifiers;
9753   cp_declarator *declarator;
9754   tree type_specified;
9755
9756   /* Parse the attributes.  */
9757   attributes = cp_parser_attributes_opt (parser);
9758   /* Parse the type-specifiers.  */
9759   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
9760                                 /*is_trailing_return=*/false,
9761                                 &type_specifiers);
9762   /* If that didn't work, stop.  */
9763   if (type_specifiers.type == error_mark_node)
9764     return error_mark_node;
9765   /* Parse the conversion-declarator.  */
9766   declarator = cp_parser_conversion_declarator_opt (parser);
9767
9768   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9769                                     /*initialized=*/0, &attributes);
9770   if (attributes)
9771     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9772
9773   /* Don't give this error when parsing tentatively.  This happens to
9774      work because we always parse this definitively once.  */
9775   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9776       && type_uses_auto (type_specified))
9777     {
9778       error ("invalid use of %<auto%> in conversion operator");
9779       return error_mark_node;
9780     }
9781
9782   return type_specified;
9783 }
9784
9785 /* Parse an (optional) conversion-declarator.
9786
9787    conversion-declarator:
9788      ptr-operator conversion-declarator [opt]
9789
9790    */
9791
9792 static cp_declarator *
9793 cp_parser_conversion_declarator_opt (cp_parser* parser)
9794 {
9795   enum tree_code code;
9796   tree class_type;
9797   cp_cv_quals cv_quals;
9798
9799   /* We don't know if there's a ptr-operator next, or not.  */
9800   cp_parser_parse_tentatively (parser);
9801   /* Try the ptr-operator.  */
9802   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9803   /* If it worked, look for more conversion-declarators.  */
9804   if (cp_parser_parse_definitely (parser))
9805     {
9806       cp_declarator *declarator;
9807
9808       /* Parse another optional declarator.  */
9809       declarator = cp_parser_conversion_declarator_opt (parser);
9810
9811       return cp_parser_make_indirect_declarator
9812         (code, class_type, cv_quals, declarator);
9813    }
9814
9815   return NULL;
9816 }
9817
9818 /* Parse an (optional) ctor-initializer.
9819
9820    ctor-initializer:
9821      : mem-initializer-list
9822
9823    Returns TRUE iff the ctor-initializer was actually present.  */
9824
9825 static bool
9826 cp_parser_ctor_initializer_opt (cp_parser* parser)
9827 {
9828   /* If the next token is not a `:', then there is no
9829      ctor-initializer.  */
9830   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9831     {
9832       /* Do default initialization of any bases and members.  */
9833       if (DECL_CONSTRUCTOR_P (current_function_decl))
9834         finish_mem_initializers (NULL_TREE);
9835
9836       return false;
9837     }
9838
9839   /* Consume the `:' token.  */
9840   cp_lexer_consume_token (parser->lexer);
9841   /* And the mem-initializer-list.  */
9842   cp_parser_mem_initializer_list (parser);
9843
9844   return true;
9845 }
9846
9847 /* Parse a mem-initializer-list.
9848
9849    mem-initializer-list:
9850      mem-initializer ... [opt]
9851      mem-initializer ... [opt] , mem-initializer-list  */
9852
9853 static void
9854 cp_parser_mem_initializer_list (cp_parser* parser)
9855 {
9856   tree mem_initializer_list = NULL_TREE;
9857   cp_token *token = cp_lexer_peek_token (parser->lexer);
9858
9859   /* Let the semantic analysis code know that we are starting the
9860      mem-initializer-list.  */
9861   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9862     error_at (token->location,
9863               "only constructors take base initializers");
9864
9865   /* Loop through the list.  */
9866   while (true)
9867     {
9868       tree mem_initializer;
9869
9870       token = cp_lexer_peek_token (parser->lexer);
9871       /* Parse the mem-initializer.  */
9872       mem_initializer = cp_parser_mem_initializer (parser);
9873       /* If the next token is a `...', we're expanding member initializers. */
9874       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9875         {
9876           /* Consume the `...'. */
9877           cp_lexer_consume_token (parser->lexer);
9878
9879           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9880              can be expanded but members cannot. */
9881           if (mem_initializer != error_mark_node
9882               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9883             {
9884               error_at (token->location,
9885                         "cannot expand initializer for member %<%D%>",
9886                         TREE_PURPOSE (mem_initializer));
9887               mem_initializer = error_mark_node;
9888             }
9889
9890           /* Construct the pack expansion type. */
9891           if (mem_initializer != error_mark_node)
9892             mem_initializer = make_pack_expansion (mem_initializer);
9893         }
9894       /* Add it to the list, unless it was erroneous.  */
9895       if (mem_initializer != error_mark_node)
9896         {
9897           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9898           mem_initializer_list = mem_initializer;
9899         }
9900       /* If the next token is not a `,', we're done.  */
9901       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9902         break;
9903       /* Consume the `,' token.  */
9904       cp_lexer_consume_token (parser->lexer);
9905     }
9906
9907   /* Perform semantic analysis.  */
9908   if (DECL_CONSTRUCTOR_P (current_function_decl))
9909     finish_mem_initializers (mem_initializer_list);
9910 }
9911
9912 /* Parse a mem-initializer.
9913
9914    mem-initializer:
9915      mem-initializer-id ( expression-list [opt] )
9916      mem-initializer-id braced-init-list
9917
9918    GNU extension:
9919
9920    mem-initializer:
9921      ( expression-list [opt] )
9922
9923    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9924    class) or FIELD_DECL (for a non-static data member) to initialize;
9925    the TREE_VALUE is the expression-list.  An empty initialization
9926    list is represented by void_list_node.  */
9927
9928 static tree
9929 cp_parser_mem_initializer (cp_parser* parser)
9930 {
9931   tree mem_initializer_id;
9932   tree expression_list;
9933   tree member;
9934   cp_token *token = cp_lexer_peek_token (parser->lexer);
9935
9936   /* Find out what is being initialized.  */
9937   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9938     {
9939       permerror (token->location,
9940                  "anachronistic old-style base class initializer");
9941       mem_initializer_id = NULL_TREE;
9942     }
9943   else
9944     {
9945       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9946       if (mem_initializer_id == error_mark_node)
9947         return mem_initializer_id;
9948     }
9949   member = expand_member_init (mem_initializer_id);
9950   if (member && !DECL_P (member))
9951     in_base_initializer = 1;
9952
9953   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9954     {
9955       bool expr_non_constant_p;
9956       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9957       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9958       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9959       expression_list = build_tree_list (NULL_TREE, expression_list);
9960     }
9961   else
9962     {
9963       VEC(tree,gc)* vec;
9964       vec = cp_parser_parenthesized_expression_list (parser, false,
9965                                                      /*cast_p=*/false,
9966                                                      /*allow_expansion_p=*/true,
9967                                                      /*non_constant_p=*/NULL);
9968       if (vec == NULL)
9969         return error_mark_node;
9970       expression_list = build_tree_list_vec (vec);
9971       release_tree_vector (vec);
9972     }
9973
9974   if (expression_list == error_mark_node)
9975     return error_mark_node;
9976   if (!expression_list)
9977     expression_list = void_type_node;
9978
9979   in_base_initializer = 0;
9980
9981   return member ? build_tree_list (member, expression_list) : error_mark_node;
9982 }
9983
9984 /* Parse a mem-initializer-id.
9985
9986    mem-initializer-id:
9987      :: [opt] nested-name-specifier [opt] class-name
9988      identifier
9989
9990    Returns a TYPE indicating the class to be initializer for the first
9991    production.  Returns an IDENTIFIER_NODE indicating the data member
9992    to be initialized for the second production.  */
9993
9994 static tree
9995 cp_parser_mem_initializer_id (cp_parser* parser)
9996 {
9997   bool global_scope_p;
9998   bool nested_name_specifier_p;
9999   bool template_p = false;
10000   tree id;
10001
10002   cp_token *token = cp_lexer_peek_token (parser->lexer);
10003
10004   /* `typename' is not allowed in this context ([temp.res]).  */
10005   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10006     {
10007       error_at (token->location, 
10008                 "keyword %<typename%> not allowed in this context (a qualified "
10009                 "member initializer is implicitly a type)");
10010       cp_lexer_consume_token (parser->lexer);
10011     }
10012   /* Look for the optional `::' operator.  */
10013   global_scope_p
10014     = (cp_parser_global_scope_opt (parser,
10015                                    /*current_scope_valid_p=*/false)
10016        != NULL_TREE);
10017   /* Look for the optional nested-name-specifier.  The simplest way to
10018      implement:
10019
10020        [temp.res]
10021
10022        The keyword `typename' is not permitted in a base-specifier or
10023        mem-initializer; in these contexts a qualified name that
10024        depends on a template-parameter is implicitly assumed to be a
10025        type name.
10026
10027      is to assume that we have seen the `typename' keyword at this
10028      point.  */
10029   nested_name_specifier_p
10030     = (cp_parser_nested_name_specifier_opt (parser,
10031                                             /*typename_keyword_p=*/true,
10032                                             /*check_dependency_p=*/true,
10033                                             /*type_p=*/true,
10034                                             /*is_declaration=*/true)
10035        != NULL_TREE);
10036   if (nested_name_specifier_p)
10037     template_p = cp_parser_optional_template_keyword (parser);
10038   /* If there is a `::' operator or a nested-name-specifier, then we
10039      are definitely looking for a class-name.  */
10040   if (global_scope_p || nested_name_specifier_p)
10041     return cp_parser_class_name (parser,
10042                                  /*typename_keyword_p=*/true,
10043                                  /*template_keyword_p=*/template_p,
10044                                  typename_type,
10045                                  /*check_dependency_p=*/true,
10046                                  /*class_head_p=*/false,
10047                                  /*is_declaration=*/true);
10048   /* Otherwise, we could also be looking for an ordinary identifier.  */
10049   cp_parser_parse_tentatively (parser);
10050   /* Try a class-name.  */
10051   id = cp_parser_class_name (parser,
10052                              /*typename_keyword_p=*/true,
10053                              /*template_keyword_p=*/false,
10054                              none_type,
10055                              /*check_dependency_p=*/true,
10056                              /*class_head_p=*/false,
10057                              /*is_declaration=*/true);
10058   /* If we found one, we're done.  */
10059   if (cp_parser_parse_definitely (parser))
10060     return id;
10061   /* Otherwise, look for an ordinary identifier.  */
10062   return cp_parser_identifier (parser);
10063 }
10064
10065 /* Overloading [gram.over] */
10066
10067 /* Parse an operator-function-id.
10068
10069    operator-function-id:
10070      operator operator
10071
10072    Returns an IDENTIFIER_NODE for the operator which is a
10073    human-readable spelling of the identifier, e.g., `operator +'.  */
10074
10075 static tree
10076 cp_parser_operator_function_id (cp_parser* parser)
10077 {
10078   /* Look for the `operator' keyword.  */
10079   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10080     return error_mark_node;
10081   /* And then the name of the operator itself.  */
10082   return cp_parser_operator (parser);
10083 }
10084
10085 /* Parse an operator.
10086
10087    operator:
10088      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10089      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10090      || ++ -- , ->* -> () []
10091
10092    GNU Extensions:
10093
10094    operator:
10095      <? >? <?= >?=
10096
10097    Returns an IDENTIFIER_NODE for the operator which is a
10098    human-readable spelling of the identifier, e.g., `operator +'.  */
10099
10100 static tree
10101 cp_parser_operator (cp_parser* parser)
10102 {
10103   tree id = NULL_TREE;
10104   cp_token *token;
10105
10106   /* Peek at the next token.  */
10107   token = cp_lexer_peek_token (parser->lexer);
10108   /* Figure out which operator we have.  */
10109   switch (token->type)
10110     {
10111     case CPP_KEYWORD:
10112       {
10113         enum tree_code op;
10114
10115         /* The keyword should be either `new' or `delete'.  */
10116         if (token->keyword == RID_NEW)
10117           op = NEW_EXPR;
10118         else if (token->keyword == RID_DELETE)
10119           op = DELETE_EXPR;
10120         else
10121           break;
10122
10123         /* Consume the `new' or `delete' token.  */
10124         cp_lexer_consume_token (parser->lexer);
10125
10126         /* Peek at the next token.  */
10127         token = cp_lexer_peek_token (parser->lexer);
10128         /* If it's a `[' token then this is the array variant of the
10129            operator.  */
10130         if (token->type == CPP_OPEN_SQUARE)
10131           {
10132             /* Consume the `[' token.  */
10133             cp_lexer_consume_token (parser->lexer);
10134             /* Look for the `]' token.  */
10135             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10136             id = ansi_opname (op == NEW_EXPR
10137                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10138           }
10139         /* Otherwise, we have the non-array variant.  */
10140         else
10141           id = ansi_opname (op);
10142
10143         return id;
10144       }
10145
10146     case CPP_PLUS:
10147       id = ansi_opname (PLUS_EXPR);
10148       break;
10149
10150     case CPP_MINUS:
10151       id = ansi_opname (MINUS_EXPR);
10152       break;
10153
10154     case CPP_MULT:
10155       id = ansi_opname (MULT_EXPR);
10156       break;
10157
10158     case CPP_DIV:
10159       id = ansi_opname (TRUNC_DIV_EXPR);
10160       break;
10161
10162     case CPP_MOD:
10163       id = ansi_opname (TRUNC_MOD_EXPR);
10164       break;
10165
10166     case CPP_XOR:
10167       id = ansi_opname (BIT_XOR_EXPR);
10168       break;
10169
10170     case CPP_AND:
10171       id = ansi_opname (BIT_AND_EXPR);
10172       break;
10173
10174     case CPP_OR:
10175       id = ansi_opname (BIT_IOR_EXPR);
10176       break;
10177
10178     case CPP_COMPL:
10179       id = ansi_opname (BIT_NOT_EXPR);
10180       break;
10181
10182     case CPP_NOT:
10183       id = ansi_opname (TRUTH_NOT_EXPR);
10184       break;
10185
10186     case CPP_EQ:
10187       id = ansi_assopname (NOP_EXPR);
10188       break;
10189
10190     case CPP_LESS:
10191       id = ansi_opname (LT_EXPR);
10192       break;
10193
10194     case CPP_GREATER:
10195       id = ansi_opname (GT_EXPR);
10196       break;
10197
10198     case CPP_PLUS_EQ:
10199       id = ansi_assopname (PLUS_EXPR);
10200       break;
10201
10202     case CPP_MINUS_EQ:
10203       id = ansi_assopname (MINUS_EXPR);
10204       break;
10205
10206     case CPP_MULT_EQ:
10207       id = ansi_assopname (MULT_EXPR);
10208       break;
10209
10210     case CPP_DIV_EQ:
10211       id = ansi_assopname (TRUNC_DIV_EXPR);
10212       break;
10213
10214     case CPP_MOD_EQ:
10215       id = ansi_assopname (TRUNC_MOD_EXPR);
10216       break;
10217
10218     case CPP_XOR_EQ:
10219       id = ansi_assopname (BIT_XOR_EXPR);
10220       break;
10221
10222     case CPP_AND_EQ:
10223       id = ansi_assopname (BIT_AND_EXPR);
10224       break;
10225
10226     case CPP_OR_EQ:
10227       id = ansi_assopname (BIT_IOR_EXPR);
10228       break;
10229
10230     case CPP_LSHIFT:
10231       id = ansi_opname (LSHIFT_EXPR);
10232       break;
10233
10234     case CPP_RSHIFT:
10235       id = ansi_opname (RSHIFT_EXPR);
10236       break;
10237
10238     case CPP_LSHIFT_EQ:
10239       id = ansi_assopname (LSHIFT_EXPR);
10240       break;
10241
10242     case CPP_RSHIFT_EQ:
10243       id = ansi_assopname (RSHIFT_EXPR);
10244       break;
10245
10246     case CPP_EQ_EQ:
10247       id = ansi_opname (EQ_EXPR);
10248       break;
10249
10250     case CPP_NOT_EQ:
10251       id = ansi_opname (NE_EXPR);
10252       break;
10253
10254     case CPP_LESS_EQ:
10255       id = ansi_opname (LE_EXPR);
10256       break;
10257
10258     case CPP_GREATER_EQ:
10259       id = ansi_opname (GE_EXPR);
10260       break;
10261
10262     case CPP_AND_AND:
10263       id = ansi_opname (TRUTH_ANDIF_EXPR);
10264       break;
10265
10266     case CPP_OR_OR:
10267       id = ansi_opname (TRUTH_ORIF_EXPR);
10268       break;
10269
10270     case CPP_PLUS_PLUS:
10271       id = ansi_opname (POSTINCREMENT_EXPR);
10272       break;
10273
10274     case CPP_MINUS_MINUS:
10275       id = ansi_opname (PREDECREMENT_EXPR);
10276       break;
10277
10278     case CPP_COMMA:
10279       id = ansi_opname (COMPOUND_EXPR);
10280       break;
10281
10282     case CPP_DEREF_STAR:
10283       id = ansi_opname (MEMBER_REF);
10284       break;
10285
10286     case CPP_DEREF:
10287       id = ansi_opname (COMPONENT_REF);
10288       break;
10289
10290     case CPP_OPEN_PAREN:
10291       /* Consume the `('.  */
10292       cp_lexer_consume_token (parser->lexer);
10293       /* Look for the matching `)'.  */
10294       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10295       return ansi_opname (CALL_EXPR);
10296
10297     case CPP_OPEN_SQUARE:
10298       /* Consume the `['.  */
10299       cp_lexer_consume_token (parser->lexer);
10300       /* Look for the matching `]'.  */
10301       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10302       return ansi_opname (ARRAY_REF);
10303
10304     default:
10305       /* Anything else is an error.  */
10306       break;
10307     }
10308
10309   /* If we have selected an identifier, we need to consume the
10310      operator token.  */
10311   if (id)
10312     cp_lexer_consume_token (parser->lexer);
10313   /* Otherwise, no valid operator name was present.  */
10314   else
10315     {
10316       cp_parser_error (parser, "expected operator");
10317       id = error_mark_node;
10318     }
10319
10320   return id;
10321 }
10322
10323 /* Parse a template-declaration.
10324
10325    template-declaration:
10326      export [opt] template < template-parameter-list > declaration
10327
10328    If MEMBER_P is TRUE, this template-declaration occurs within a
10329    class-specifier.
10330
10331    The grammar rule given by the standard isn't correct.  What
10332    is really meant is:
10333
10334    template-declaration:
10335      export [opt] template-parameter-list-seq
10336        decl-specifier-seq [opt] init-declarator [opt] ;
10337      export [opt] template-parameter-list-seq
10338        function-definition
10339
10340    template-parameter-list-seq:
10341      template-parameter-list-seq [opt]
10342      template < template-parameter-list >  */
10343
10344 static void
10345 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10346 {
10347   /* Check for `export'.  */
10348   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10349     {
10350       /* Consume the `export' token.  */
10351       cp_lexer_consume_token (parser->lexer);
10352       /* Warn that we do not support `export'.  */
10353       warning (0, "keyword %<export%> not implemented, and will be ignored");
10354     }
10355
10356   cp_parser_template_declaration_after_export (parser, member_p);
10357 }
10358
10359 /* Parse a template-parameter-list.
10360
10361    template-parameter-list:
10362      template-parameter
10363      template-parameter-list , template-parameter
10364
10365    Returns a TREE_LIST.  Each node represents a template parameter.
10366    The nodes are connected via their TREE_CHAINs.  */
10367
10368 static tree
10369 cp_parser_template_parameter_list (cp_parser* parser)
10370 {
10371   tree parameter_list = NULL_TREE;
10372
10373   begin_template_parm_list ();
10374   while (true)
10375     {
10376       tree parameter;
10377       bool is_non_type;
10378       bool is_parameter_pack;
10379       location_t parm_loc;
10380
10381       /* Parse the template-parameter.  */
10382       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10383       parameter = cp_parser_template_parameter (parser, 
10384                                                 &is_non_type,
10385                                                 &is_parameter_pack);
10386       /* Add it to the list.  */
10387       if (parameter != error_mark_node)
10388         parameter_list = process_template_parm (parameter_list,
10389                                                 parm_loc,
10390                                                 parameter,
10391                                                 is_non_type,
10392                                                 is_parameter_pack);
10393       else
10394        {
10395          tree err_parm = build_tree_list (parameter, parameter);
10396          TREE_VALUE (err_parm) = error_mark_node;
10397          parameter_list = chainon (parameter_list, err_parm);
10398        }
10399
10400       /* If the next token is not a `,', we're done.  */
10401       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10402         break;
10403       /* Otherwise, consume the `,' token.  */
10404       cp_lexer_consume_token (parser->lexer);
10405     }
10406
10407   return end_template_parm_list (parameter_list);
10408 }
10409
10410 /* Parse a template-parameter.
10411
10412    template-parameter:
10413      type-parameter
10414      parameter-declaration
10415
10416    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10417    the parameter.  The TREE_PURPOSE is the default value, if any.
10418    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10419    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10420    set to true iff this parameter is a parameter pack. */
10421
10422 static tree
10423 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10424                               bool *is_parameter_pack)
10425 {
10426   cp_token *token;
10427   cp_parameter_declarator *parameter_declarator;
10428   cp_declarator *id_declarator;
10429   tree parm;
10430
10431   /* Assume it is a type parameter or a template parameter.  */
10432   *is_non_type = false;
10433   /* Assume it not a parameter pack. */
10434   *is_parameter_pack = false;
10435   /* Peek at the next token.  */
10436   token = cp_lexer_peek_token (parser->lexer);
10437   /* If it is `class' or `template', we have a type-parameter.  */
10438   if (token->keyword == RID_TEMPLATE)
10439     return cp_parser_type_parameter (parser, is_parameter_pack);
10440   /* If it is `class' or `typename' we do not know yet whether it is a
10441      type parameter or a non-type parameter.  Consider:
10442
10443        template <typename T, typename T::X X> ...
10444
10445      or:
10446
10447        template <class C, class D*> ...
10448
10449      Here, the first parameter is a type parameter, and the second is
10450      a non-type parameter.  We can tell by looking at the token after
10451      the identifier -- if it is a `,', `=', or `>' then we have a type
10452      parameter.  */
10453   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10454     {
10455       /* Peek at the token after `class' or `typename'.  */
10456       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10457       /* If it's an ellipsis, we have a template type parameter
10458          pack. */
10459       if (token->type == CPP_ELLIPSIS)
10460         return cp_parser_type_parameter (parser, is_parameter_pack);
10461       /* If it's an identifier, skip it.  */
10462       if (token->type == CPP_NAME)
10463         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10464       /* Now, see if the token looks like the end of a template
10465          parameter.  */
10466       if (token->type == CPP_COMMA
10467           || token->type == CPP_EQ
10468           || token->type == CPP_GREATER)
10469         return cp_parser_type_parameter (parser, is_parameter_pack);
10470     }
10471
10472   /* Otherwise, it is a non-type parameter.
10473
10474      [temp.param]
10475
10476      When parsing a default template-argument for a non-type
10477      template-parameter, the first non-nested `>' is taken as the end
10478      of the template parameter-list rather than a greater-than
10479      operator.  */
10480   *is_non_type = true;
10481   parameter_declarator
10482      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10483                                         /*parenthesized_p=*/NULL);
10484
10485   /* If the parameter declaration is marked as a parameter pack, set
10486      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10487      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10488      grokdeclarator. */
10489   if (parameter_declarator
10490       && parameter_declarator->declarator
10491       && parameter_declarator->declarator->parameter_pack_p)
10492     {
10493       *is_parameter_pack = true;
10494       parameter_declarator->declarator->parameter_pack_p = false;
10495     }
10496
10497   /* If the next token is an ellipsis, and we don't already have it
10498      marked as a parameter pack, then we have a parameter pack (that
10499      has no declarator).  */
10500   if (!*is_parameter_pack
10501       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10502       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10503     {
10504       /* Consume the `...'.  */
10505       cp_lexer_consume_token (parser->lexer);
10506       maybe_warn_variadic_templates ();
10507       
10508       *is_parameter_pack = true;
10509     }
10510   /* We might end up with a pack expansion as the type of the non-type
10511      template parameter, in which case this is a non-type template
10512      parameter pack.  */
10513   else if (parameter_declarator
10514            && parameter_declarator->decl_specifiers.type
10515            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10516     {
10517       *is_parameter_pack = true;
10518       parameter_declarator->decl_specifiers.type = 
10519         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10520     }
10521
10522   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10523     {
10524       /* Parameter packs cannot have default arguments.  However, a
10525          user may try to do so, so we'll parse them and give an
10526          appropriate diagnostic here.  */
10527
10528       /* Consume the `='.  */
10529       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10530       cp_lexer_consume_token (parser->lexer);
10531       
10532       /* Find the name of the parameter pack.  */     
10533       id_declarator = parameter_declarator->declarator;
10534       while (id_declarator && id_declarator->kind != cdk_id)
10535         id_declarator = id_declarator->declarator;
10536       
10537       if (id_declarator && id_declarator->kind == cdk_id)
10538         error_at (start_token->location,
10539                   "template parameter pack %qD cannot have a default argument",
10540                   id_declarator->u.id.unqualified_name);
10541       else
10542         error_at (start_token->location,
10543                   "template parameter pack cannot have a default argument");
10544       
10545       /* Parse the default argument, but throw away the result.  */
10546       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10547     }
10548
10549   parm = grokdeclarator (parameter_declarator->declarator,
10550                          &parameter_declarator->decl_specifiers,
10551                          TPARM, /*initialized=*/0,
10552                          /*attrlist=*/NULL);
10553   if (parm == error_mark_node)
10554     return error_mark_node;
10555
10556   return build_tree_list (parameter_declarator->default_argument, parm);
10557 }
10558
10559 /* Parse a type-parameter.
10560
10561    type-parameter:
10562      class identifier [opt]
10563      class identifier [opt] = type-id
10564      typename identifier [opt]
10565      typename identifier [opt] = type-id
10566      template < template-parameter-list > class identifier [opt]
10567      template < template-parameter-list > class identifier [opt]
10568        = id-expression
10569
10570    GNU Extension (variadic templates):
10571
10572    type-parameter:
10573      class ... identifier [opt]
10574      typename ... identifier [opt]
10575
10576    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10577    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10578    the declaration of the parameter.
10579
10580    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10581
10582 static tree
10583 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10584 {
10585   cp_token *token;
10586   tree parameter;
10587
10588   /* Look for a keyword to tell us what kind of parameter this is.  */
10589   token = cp_parser_require (parser, CPP_KEYWORD,
10590                              "%<class%>, %<typename%>, or %<template%>");
10591   if (!token)
10592     return error_mark_node;
10593
10594   switch (token->keyword)
10595     {
10596     case RID_CLASS:
10597     case RID_TYPENAME:
10598       {
10599         tree identifier;
10600         tree default_argument;
10601
10602         /* If the next token is an ellipsis, we have a template
10603            argument pack. */
10604         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10605           {
10606             /* Consume the `...' token. */
10607             cp_lexer_consume_token (parser->lexer);
10608             maybe_warn_variadic_templates ();
10609
10610             *is_parameter_pack = true;
10611           }
10612
10613         /* If the next token is an identifier, then it names the
10614            parameter.  */
10615         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10616           identifier = cp_parser_identifier (parser);
10617         else
10618           identifier = NULL_TREE;
10619
10620         /* Create the parameter.  */
10621         parameter = finish_template_type_parm (class_type_node, identifier);
10622
10623         /* If the next token is an `=', we have a default argument.  */
10624         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10625           {
10626             /* Consume the `=' token.  */
10627             cp_lexer_consume_token (parser->lexer);
10628             /* Parse the default-argument.  */
10629             push_deferring_access_checks (dk_no_deferred);
10630             default_argument = cp_parser_type_id (parser);
10631
10632             /* Template parameter packs cannot have default
10633                arguments. */
10634             if (*is_parameter_pack)
10635               {
10636                 if (identifier)
10637                   error_at (token->location,
10638                             "template parameter pack %qD cannot have a "
10639                             "default argument", identifier);
10640                 else
10641                   error_at (token->location,
10642                             "template parameter packs cannot have "
10643                             "default arguments");
10644                 default_argument = NULL_TREE;
10645               }
10646             pop_deferring_access_checks ();
10647           }
10648         else
10649           default_argument = NULL_TREE;
10650
10651         /* Create the combined representation of the parameter and the
10652            default argument.  */
10653         parameter = build_tree_list (default_argument, parameter);
10654       }
10655       break;
10656
10657     case RID_TEMPLATE:
10658       {
10659         tree identifier;
10660         tree default_argument;
10661
10662         /* Look for the `<'.  */
10663         cp_parser_require (parser, CPP_LESS, "%<<%>");
10664         /* Parse the template-parameter-list.  */
10665         cp_parser_template_parameter_list (parser);
10666         /* Look for the `>'.  */
10667         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10668         /* Look for the `class' keyword.  */
10669         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10670         /* If the next token is an ellipsis, we have a template
10671            argument pack. */
10672         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10673           {
10674             /* Consume the `...' token. */
10675             cp_lexer_consume_token (parser->lexer);
10676             maybe_warn_variadic_templates ();
10677
10678             *is_parameter_pack = true;
10679           }
10680         /* If the next token is an `=', then there is a
10681            default-argument.  If the next token is a `>', we are at
10682            the end of the parameter-list.  If the next token is a `,',
10683            then we are at the end of this parameter.  */
10684         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10685             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10686             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10687           {
10688             identifier = cp_parser_identifier (parser);
10689             /* Treat invalid names as if the parameter were nameless.  */
10690             if (identifier == error_mark_node)
10691               identifier = NULL_TREE;
10692           }
10693         else
10694           identifier = NULL_TREE;
10695
10696         /* Create the template parameter.  */
10697         parameter = finish_template_template_parm (class_type_node,
10698                                                    identifier);
10699
10700         /* If the next token is an `=', then there is a
10701            default-argument.  */
10702         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10703           {
10704             bool is_template;
10705
10706             /* Consume the `='.  */
10707             cp_lexer_consume_token (parser->lexer);
10708             /* Parse the id-expression.  */
10709             push_deferring_access_checks (dk_no_deferred);
10710             /* save token before parsing the id-expression, for error
10711                reporting */
10712             token = cp_lexer_peek_token (parser->lexer);
10713             default_argument
10714               = cp_parser_id_expression (parser,
10715                                          /*template_keyword_p=*/false,
10716                                          /*check_dependency_p=*/true,
10717                                          /*template_p=*/&is_template,
10718                                          /*declarator_p=*/false,
10719                                          /*optional_p=*/false);
10720             if (TREE_CODE (default_argument) == TYPE_DECL)
10721               /* If the id-expression was a template-id that refers to
10722                  a template-class, we already have the declaration here,
10723                  so no further lookup is needed.  */
10724                  ;
10725             else
10726               /* Look up the name.  */
10727               default_argument
10728                 = cp_parser_lookup_name (parser, default_argument,
10729                                          none_type,
10730                                          /*is_template=*/is_template,
10731                                          /*is_namespace=*/false,
10732                                          /*check_dependency=*/true,
10733                                          /*ambiguous_decls=*/NULL,
10734                                          token->location);
10735             /* See if the default argument is valid.  */
10736             default_argument
10737               = check_template_template_default_arg (default_argument);
10738
10739             /* Template parameter packs cannot have default
10740                arguments. */
10741             if (*is_parameter_pack)
10742               {
10743                 if (identifier)
10744                   error_at (token->location,
10745                             "template parameter pack %qD cannot "
10746                             "have a default argument",
10747                             identifier);
10748                 else
10749                   error_at (token->location, "template parameter packs cannot "
10750                             "have default arguments");
10751                 default_argument = NULL_TREE;
10752               }
10753             pop_deferring_access_checks ();
10754           }
10755         else
10756           default_argument = NULL_TREE;
10757
10758         /* Create the combined representation of the parameter and the
10759            default argument.  */
10760         parameter = build_tree_list (default_argument, parameter);
10761       }
10762       break;
10763
10764     default:
10765       gcc_unreachable ();
10766       break;
10767     }
10768
10769   return parameter;
10770 }
10771
10772 /* Parse a template-id.
10773
10774    template-id:
10775      template-name < template-argument-list [opt] >
10776
10777    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10778    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10779    returned.  Otherwise, if the template-name names a function, or set
10780    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10781    names a class, returns a TYPE_DECL for the specialization.
10782
10783    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10784    uninstantiated templates.  */
10785
10786 static tree
10787 cp_parser_template_id (cp_parser *parser,
10788                        bool template_keyword_p,
10789                        bool check_dependency_p,
10790                        bool is_declaration)
10791 {
10792   int i;
10793   tree templ;
10794   tree arguments;
10795   tree template_id;
10796   cp_token_position start_of_id = 0;
10797   deferred_access_check *chk;
10798   VEC (deferred_access_check,gc) *access_check;
10799   cp_token *next_token = NULL, *next_token_2 = NULL;
10800   bool is_identifier;
10801
10802   /* If the next token corresponds to a template-id, there is no need
10803      to reparse it.  */
10804   next_token = cp_lexer_peek_token (parser->lexer);
10805   if (next_token->type == CPP_TEMPLATE_ID)
10806     {
10807       struct tree_check *check_value;
10808
10809       /* Get the stored value.  */
10810       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10811       /* Perform any access checks that were deferred.  */
10812       access_check = check_value->checks;
10813       if (access_check)
10814         {
10815           for (i = 0 ;
10816                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10817                ++i)
10818             {
10819               perform_or_defer_access_check (chk->binfo,
10820                                              chk->decl,
10821                                              chk->diag_decl);
10822             }
10823         }
10824       /* Return the stored value.  */
10825       return check_value->value;
10826     }
10827
10828   /* Avoid performing name lookup if there is no possibility of
10829      finding a template-id.  */
10830   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10831       || (next_token->type == CPP_NAME
10832           && !cp_parser_nth_token_starts_template_argument_list_p
10833                (parser, 2)))
10834     {
10835       cp_parser_error (parser, "expected template-id");
10836       return error_mark_node;
10837     }
10838
10839   /* Remember where the template-id starts.  */
10840   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10841     start_of_id = cp_lexer_token_position (parser->lexer, false);
10842
10843   push_deferring_access_checks (dk_deferred);
10844
10845   /* Parse the template-name.  */
10846   is_identifier = false;
10847   templ = cp_parser_template_name (parser, template_keyword_p,
10848                                    check_dependency_p,
10849                                    is_declaration,
10850                                    &is_identifier);
10851   if (templ == error_mark_node || is_identifier)
10852     {
10853       pop_deferring_access_checks ();
10854       return templ;
10855     }
10856
10857   /* If we find the sequence `[:' after a template-name, it's probably
10858      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10859      parse correctly the argument list.  */
10860   next_token = cp_lexer_peek_token (parser->lexer);
10861   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10862   if (next_token->type == CPP_OPEN_SQUARE
10863       && next_token->flags & DIGRAPH
10864       && next_token_2->type == CPP_COLON
10865       && !(next_token_2->flags & PREV_WHITE))
10866     {
10867       cp_parser_parse_tentatively (parser);
10868       /* Change `:' into `::'.  */
10869       next_token_2->type = CPP_SCOPE;
10870       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10871          CPP_LESS.  */
10872       cp_lexer_consume_token (parser->lexer);
10873
10874       /* Parse the arguments.  */
10875       arguments = cp_parser_enclosed_template_argument_list (parser);
10876       if (!cp_parser_parse_definitely (parser))
10877         {
10878           /* If we couldn't parse an argument list, then we revert our changes
10879              and return simply an error. Maybe this is not a template-id
10880              after all.  */
10881           next_token_2->type = CPP_COLON;
10882           cp_parser_error (parser, "expected %<<%>");
10883           pop_deferring_access_checks ();
10884           return error_mark_node;
10885         }
10886       /* Otherwise, emit an error about the invalid digraph, but continue
10887          parsing because we got our argument list.  */
10888       if (permerror (next_token->location,
10889                      "%<<::%> cannot begin a template-argument list"))
10890         {
10891           static bool hint = false;
10892           inform (next_token->location,
10893                   "%<<:%> is an alternate spelling for %<[%>."
10894                   " Insert whitespace between %<<%> and %<::%>");
10895           if (!hint && !flag_permissive)
10896             {
10897               inform (next_token->location, "(if you use %<-fpermissive%>"
10898                       " G++ will accept your code)");
10899               hint = true;
10900             }
10901         }
10902     }
10903   else
10904     {
10905       /* Look for the `<' that starts the template-argument-list.  */
10906       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10907         {
10908           pop_deferring_access_checks ();
10909           return error_mark_node;
10910         }
10911       /* Parse the arguments.  */
10912       arguments = cp_parser_enclosed_template_argument_list (parser);
10913     }
10914
10915   /* Build a representation of the specialization.  */
10916   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10917     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10918   else if (DECL_CLASS_TEMPLATE_P (templ)
10919            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10920     {
10921       bool entering_scope;
10922       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10923          template (rather than some instantiation thereof) only if
10924          is not nested within some other construct.  For example, in
10925          "template <typename T> void f(T) { A<T>::", A<T> is just an
10926          instantiation of A.  */
10927       entering_scope = (template_parm_scope_p ()
10928                         && cp_lexer_next_token_is (parser->lexer,
10929                                                    CPP_SCOPE));
10930       template_id
10931         = finish_template_type (templ, arguments, entering_scope);
10932     }
10933   else
10934     {
10935       /* If it's not a class-template or a template-template, it should be
10936          a function-template.  */
10937       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10938                    || TREE_CODE (templ) == OVERLOAD
10939                    || BASELINK_P (templ)));
10940
10941       template_id = lookup_template_function (templ, arguments);
10942     }
10943
10944   /* If parsing tentatively, replace the sequence of tokens that makes
10945      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10946      should we re-parse the token stream, we will not have to repeat
10947      the effort required to do the parse, nor will we issue duplicate
10948      error messages about problems during instantiation of the
10949      template.  */
10950   if (start_of_id)
10951     {
10952       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10953
10954       /* Reset the contents of the START_OF_ID token.  */
10955       token->type = CPP_TEMPLATE_ID;
10956       /* Retrieve any deferred checks.  Do not pop this access checks yet
10957          so the memory will not be reclaimed during token replacing below.  */
10958       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10959       token->u.tree_check_value->value = template_id;
10960       token->u.tree_check_value->checks = get_deferred_access_checks ();
10961       token->keyword = RID_MAX;
10962
10963       /* Purge all subsequent tokens.  */
10964       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10965
10966       /* ??? Can we actually assume that, if template_id ==
10967          error_mark_node, we will have issued a diagnostic to the
10968          user, as opposed to simply marking the tentative parse as
10969          failed?  */
10970       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10971         error_at (token->location, "parse error in template argument list");
10972     }
10973
10974   pop_deferring_access_checks ();
10975   return template_id;
10976 }
10977
10978 /* Parse a template-name.
10979
10980    template-name:
10981      identifier
10982
10983    The standard should actually say:
10984
10985    template-name:
10986      identifier
10987      operator-function-id
10988
10989    A defect report has been filed about this issue.
10990
10991    A conversion-function-id cannot be a template name because they cannot
10992    be part of a template-id. In fact, looking at this code:
10993
10994    a.operator K<int>()
10995
10996    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10997    It is impossible to call a templated conversion-function-id with an
10998    explicit argument list, since the only allowed template parameter is
10999    the type to which it is converting.
11000
11001    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11002    `template' keyword, in a construction like:
11003
11004      T::template f<3>()
11005
11006    In that case `f' is taken to be a template-name, even though there
11007    is no way of knowing for sure.
11008
11009    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11010    name refers to a set of overloaded functions, at least one of which
11011    is a template, or an IDENTIFIER_NODE with the name of the template,
11012    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11013    names are looked up inside uninstantiated templates.  */
11014
11015 static tree
11016 cp_parser_template_name (cp_parser* parser,
11017                          bool template_keyword_p,
11018                          bool check_dependency_p,
11019                          bool is_declaration,
11020                          bool *is_identifier)
11021 {
11022   tree identifier;
11023   tree decl;
11024   tree fns;
11025   cp_token *token = cp_lexer_peek_token (parser->lexer);
11026
11027   /* If the next token is `operator', then we have either an
11028      operator-function-id or a conversion-function-id.  */
11029   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11030     {
11031       /* We don't know whether we're looking at an
11032          operator-function-id or a conversion-function-id.  */
11033       cp_parser_parse_tentatively (parser);
11034       /* Try an operator-function-id.  */
11035       identifier = cp_parser_operator_function_id (parser);
11036       /* If that didn't work, try a conversion-function-id.  */
11037       if (!cp_parser_parse_definitely (parser))
11038         {
11039           cp_parser_error (parser, "expected template-name");
11040           return error_mark_node;
11041         }
11042     }
11043   /* Look for the identifier.  */
11044   else
11045     identifier = cp_parser_identifier (parser);
11046
11047   /* If we didn't find an identifier, we don't have a template-id.  */
11048   if (identifier == error_mark_node)
11049     return error_mark_node;
11050
11051   /* If the name immediately followed the `template' keyword, then it
11052      is a template-name.  However, if the next token is not `<', then
11053      we do not treat it as a template-name, since it is not being used
11054      as part of a template-id.  This enables us to handle constructs
11055      like:
11056
11057        template <typename T> struct S { S(); };
11058        template <typename T> S<T>::S();
11059
11060      correctly.  We would treat `S' as a template -- if it were `S<T>'
11061      -- but we do not if there is no `<'.  */
11062
11063   if (processing_template_decl
11064       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11065     {
11066       /* In a declaration, in a dependent context, we pretend that the
11067          "template" keyword was present in order to improve error
11068          recovery.  For example, given:
11069
11070            template <typename T> void f(T::X<int>);
11071
11072          we want to treat "X<int>" as a template-id.  */
11073       if (is_declaration
11074           && !template_keyword_p
11075           && parser->scope && TYPE_P (parser->scope)
11076           && check_dependency_p
11077           && dependent_scope_p (parser->scope)
11078           /* Do not do this for dtors (or ctors), since they never
11079              need the template keyword before their name.  */
11080           && !constructor_name_p (identifier, parser->scope))
11081         {
11082           cp_token_position start = 0;
11083
11084           /* Explain what went wrong.  */
11085           error_at (token->location, "non-template %qD used as template",
11086                     identifier);
11087           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11088                   parser->scope, identifier);
11089           /* If parsing tentatively, find the location of the "<" token.  */
11090           if (cp_parser_simulate_error (parser))
11091             start = cp_lexer_token_position (parser->lexer, true);
11092           /* Parse the template arguments so that we can issue error
11093              messages about them.  */
11094           cp_lexer_consume_token (parser->lexer);
11095           cp_parser_enclosed_template_argument_list (parser);
11096           /* Skip tokens until we find a good place from which to
11097              continue parsing.  */
11098           cp_parser_skip_to_closing_parenthesis (parser,
11099                                                  /*recovering=*/true,
11100                                                  /*or_comma=*/true,
11101                                                  /*consume_paren=*/false);
11102           /* If parsing tentatively, permanently remove the
11103              template argument list.  That will prevent duplicate
11104              error messages from being issued about the missing
11105              "template" keyword.  */
11106           if (start)
11107             cp_lexer_purge_tokens_after (parser->lexer, start);
11108           if (is_identifier)
11109             *is_identifier = true;
11110           return identifier;
11111         }
11112
11113       /* If the "template" keyword is present, then there is generally
11114          no point in doing name-lookup, so we just return IDENTIFIER.
11115          But, if the qualifying scope is non-dependent then we can
11116          (and must) do name-lookup normally.  */
11117       if (template_keyword_p
11118           && (!parser->scope
11119               || (TYPE_P (parser->scope)
11120                   && dependent_type_p (parser->scope))))
11121         return identifier;
11122     }
11123
11124   /* Look up the name.  */
11125   decl = cp_parser_lookup_name (parser, identifier,
11126                                 none_type,
11127                                 /*is_template=*/true,
11128                                 /*is_namespace=*/false,
11129                                 check_dependency_p,
11130                                 /*ambiguous_decls=*/NULL,
11131                                 token->location);
11132
11133   /* If DECL is a template, then the name was a template-name.  */
11134   if (TREE_CODE (decl) == TEMPLATE_DECL)
11135     ;
11136   else
11137     {
11138       tree fn = NULL_TREE;
11139
11140       /* The standard does not explicitly indicate whether a name that
11141          names a set of overloaded declarations, some of which are
11142          templates, is a template-name.  However, such a name should
11143          be a template-name; otherwise, there is no way to form a
11144          template-id for the overloaded templates.  */
11145       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11146       if (TREE_CODE (fns) == OVERLOAD)
11147         for (fn = fns; fn; fn = OVL_NEXT (fn))
11148           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11149             break;
11150
11151       if (!fn)
11152         {
11153           /* The name does not name a template.  */
11154           cp_parser_error (parser, "expected template-name");
11155           return error_mark_node;
11156         }
11157     }
11158
11159   /* If DECL is dependent, and refers to a function, then just return
11160      its name; we will look it up again during template instantiation.  */
11161   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11162     {
11163       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11164       if (TYPE_P (scope) && dependent_type_p (scope))
11165         return identifier;
11166     }
11167
11168   return decl;
11169 }
11170
11171 /* Parse a template-argument-list.
11172
11173    template-argument-list:
11174      template-argument ... [opt]
11175      template-argument-list , template-argument ... [opt]
11176
11177    Returns a TREE_VEC containing the arguments.  */
11178
11179 static tree
11180 cp_parser_template_argument_list (cp_parser* parser)
11181 {
11182   tree fixed_args[10];
11183   unsigned n_args = 0;
11184   unsigned alloced = 10;
11185   tree *arg_ary = fixed_args;
11186   tree vec;
11187   bool saved_in_template_argument_list_p;
11188   bool saved_ice_p;
11189   bool saved_non_ice_p;
11190
11191   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11192   parser->in_template_argument_list_p = true;
11193   /* Even if the template-id appears in an integral
11194      constant-expression, the contents of the argument list do
11195      not.  */
11196   saved_ice_p = parser->integral_constant_expression_p;
11197   parser->integral_constant_expression_p = false;
11198   saved_non_ice_p = parser->non_integral_constant_expression_p;
11199   parser->non_integral_constant_expression_p = false;
11200   /* Parse the arguments.  */
11201   do
11202     {
11203       tree argument;
11204
11205       if (n_args)
11206         /* Consume the comma.  */
11207         cp_lexer_consume_token (parser->lexer);
11208
11209       /* Parse the template-argument.  */
11210       argument = cp_parser_template_argument (parser);
11211
11212       /* If the next token is an ellipsis, we're expanding a template
11213          argument pack. */
11214       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11215         {
11216           if (argument == error_mark_node)
11217             {
11218               cp_token *token = cp_lexer_peek_token (parser->lexer);
11219               error_at (token->location,
11220                         "expected parameter pack before %<...%>");
11221             }
11222           /* Consume the `...' token. */
11223           cp_lexer_consume_token (parser->lexer);
11224
11225           /* Make the argument into a TYPE_PACK_EXPANSION or
11226              EXPR_PACK_EXPANSION. */
11227           argument = make_pack_expansion (argument);
11228         }
11229
11230       if (n_args == alloced)
11231         {
11232           alloced *= 2;
11233
11234           if (arg_ary == fixed_args)
11235             {
11236               arg_ary = XNEWVEC (tree, alloced);
11237               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11238             }
11239           else
11240             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11241         }
11242       arg_ary[n_args++] = argument;
11243     }
11244   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11245
11246   vec = make_tree_vec (n_args);
11247
11248   while (n_args--)
11249     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11250
11251   if (arg_ary != fixed_args)
11252     free (arg_ary);
11253   parser->non_integral_constant_expression_p = saved_non_ice_p;
11254   parser->integral_constant_expression_p = saved_ice_p;
11255   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11256 #ifdef ENABLE_CHECKING
11257   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11258 #endif
11259   return vec;
11260 }
11261
11262 /* Parse a template-argument.
11263
11264    template-argument:
11265      assignment-expression
11266      type-id
11267      id-expression
11268
11269    The representation is that of an assignment-expression, type-id, or
11270    id-expression -- except that the qualified id-expression is
11271    evaluated, so that the value returned is either a DECL or an
11272    OVERLOAD.
11273
11274    Although the standard says "assignment-expression", it forbids
11275    throw-expressions or assignments in the template argument.
11276    Therefore, we use "conditional-expression" instead.  */
11277
11278 static tree
11279 cp_parser_template_argument (cp_parser* parser)
11280 {
11281   tree argument;
11282   bool template_p;
11283   bool address_p;
11284   bool maybe_type_id = false;
11285   cp_token *token = NULL, *argument_start_token = NULL;
11286   cp_id_kind idk;
11287
11288   /* There's really no way to know what we're looking at, so we just
11289      try each alternative in order.
11290
11291        [temp.arg]
11292
11293        In a template-argument, an ambiguity between a type-id and an
11294        expression is resolved to a type-id, regardless of the form of
11295        the corresponding template-parameter.
11296
11297      Therefore, we try a type-id first.  */
11298   cp_parser_parse_tentatively (parser);
11299   argument = cp_parser_template_type_arg (parser);
11300   /* If there was no error parsing the type-id but the next token is a
11301      '>>', our behavior depends on which dialect of C++ we're
11302      parsing. In C++98, we probably found a typo for '> >'. But there
11303      are type-id which are also valid expressions. For instance:
11304
11305      struct X { int operator >> (int); };
11306      template <int V> struct Foo {};
11307      Foo<X () >> 5> r;
11308
11309      Here 'X()' is a valid type-id of a function type, but the user just
11310      wanted to write the expression "X() >> 5". Thus, we remember that we
11311      found a valid type-id, but we still try to parse the argument as an
11312      expression to see what happens. 
11313
11314      In C++0x, the '>>' will be considered two separate '>'
11315      tokens.  */
11316   if (!cp_parser_error_occurred (parser)
11317       && cxx_dialect == cxx98
11318       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11319     {
11320       maybe_type_id = true;
11321       cp_parser_abort_tentative_parse (parser);
11322     }
11323   else
11324     {
11325       /* If the next token isn't a `,' or a `>', then this argument wasn't
11326       really finished. This means that the argument is not a valid
11327       type-id.  */
11328       if (!cp_parser_next_token_ends_template_argument_p (parser))
11329         cp_parser_error (parser, "expected template-argument");
11330       /* If that worked, we're done.  */
11331       if (cp_parser_parse_definitely (parser))
11332         return argument;
11333     }
11334   /* We're still not sure what the argument will be.  */
11335   cp_parser_parse_tentatively (parser);
11336   /* Try a template.  */
11337   argument_start_token = cp_lexer_peek_token (parser->lexer);
11338   argument = cp_parser_id_expression (parser,
11339                                       /*template_keyword_p=*/false,
11340                                       /*check_dependency_p=*/true,
11341                                       &template_p,
11342                                       /*declarator_p=*/false,
11343                                       /*optional_p=*/false);
11344   /* If the next token isn't a `,' or a `>', then this argument wasn't
11345      really finished.  */
11346   if (!cp_parser_next_token_ends_template_argument_p (parser))
11347     cp_parser_error (parser, "expected template-argument");
11348   if (!cp_parser_error_occurred (parser))
11349     {
11350       /* Figure out what is being referred to.  If the id-expression
11351          was for a class template specialization, then we will have a
11352          TYPE_DECL at this point.  There is no need to do name lookup
11353          at this point in that case.  */
11354       if (TREE_CODE (argument) != TYPE_DECL)
11355         argument = cp_parser_lookup_name (parser, argument,
11356                                           none_type,
11357                                           /*is_template=*/template_p,
11358                                           /*is_namespace=*/false,
11359                                           /*check_dependency=*/true,
11360                                           /*ambiguous_decls=*/NULL,
11361                                           argument_start_token->location);
11362       if (TREE_CODE (argument) != TEMPLATE_DECL
11363           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11364         cp_parser_error (parser, "expected template-name");
11365     }
11366   if (cp_parser_parse_definitely (parser))
11367     return argument;
11368   /* It must be a non-type argument.  There permitted cases are given
11369      in [temp.arg.nontype]:
11370
11371      -- an integral constant-expression of integral or enumeration
11372         type; or
11373
11374      -- the name of a non-type template-parameter; or
11375
11376      -- the name of an object or function with external linkage...
11377
11378      -- the address of an object or function with external linkage...
11379
11380      -- a pointer to member...  */
11381   /* Look for a non-type template parameter.  */
11382   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11383     {
11384       cp_parser_parse_tentatively (parser);
11385       argument = cp_parser_primary_expression (parser,
11386                                                /*address_p=*/false,
11387                                                /*cast_p=*/false,
11388                                                /*template_arg_p=*/true,
11389                                                &idk);
11390       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11391           || !cp_parser_next_token_ends_template_argument_p (parser))
11392         cp_parser_simulate_error (parser);
11393       if (cp_parser_parse_definitely (parser))
11394         return argument;
11395     }
11396
11397   /* If the next token is "&", the argument must be the address of an
11398      object or function with external linkage.  */
11399   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11400   if (address_p)
11401     cp_lexer_consume_token (parser->lexer);
11402   /* See if we might have an id-expression.  */
11403   token = cp_lexer_peek_token (parser->lexer);
11404   if (token->type == CPP_NAME
11405       || token->keyword == RID_OPERATOR
11406       || token->type == CPP_SCOPE
11407       || token->type == CPP_TEMPLATE_ID
11408       || token->type == CPP_NESTED_NAME_SPECIFIER)
11409     {
11410       cp_parser_parse_tentatively (parser);
11411       argument = cp_parser_primary_expression (parser,
11412                                                address_p,
11413                                                /*cast_p=*/false,
11414                                                /*template_arg_p=*/true,
11415                                                &idk);
11416       if (cp_parser_error_occurred (parser)
11417           || !cp_parser_next_token_ends_template_argument_p (parser))
11418         cp_parser_abort_tentative_parse (parser);
11419       else
11420         {
11421           tree probe;
11422
11423           if (TREE_CODE (argument) == INDIRECT_REF)
11424             {
11425               gcc_assert (REFERENCE_REF_P (argument));
11426               argument = TREE_OPERAND (argument, 0);
11427             }
11428
11429           /* If we're in a template, we represent a qualified-id referring
11430              to a static data member as a SCOPE_REF even if the scope isn't
11431              dependent so that we can check access control later.  */
11432           probe = argument;
11433           if (TREE_CODE (probe) == SCOPE_REF)
11434             probe = TREE_OPERAND (probe, 1);
11435           if (TREE_CODE (probe) == VAR_DECL)
11436             {
11437               /* A variable without external linkage might still be a
11438                  valid constant-expression, so no error is issued here
11439                  if the external-linkage check fails.  */
11440               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
11441                 cp_parser_simulate_error (parser);
11442             }
11443           else if (is_overloaded_fn (argument))
11444             /* All overloaded functions are allowed; if the external
11445                linkage test does not pass, an error will be issued
11446                later.  */
11447             ;
11448           else if (address_p
11449                    && (TREE_CODE (argument) == OFFSET_REF
11450                        || TREE_CODE (argument) == SCOPE_REF))
11451             /* A pointer-to-member.  */
11452             ;
11453           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11454             ;
11455           else
11456             cp_parser_simulate_error (parser);
11457
11458           if (cp_parser_parse_definitely (parser))
11459             {
11460               if (address_p)
11461                 argument = build_x_unary_op (ADDR_EXPR, argument,
11462                                              tf_warning_or_error);
11463               return argument;
11464             }
11465         }
11466     }
11467   /* If the argument started with "&", there are no other valid
11468      alternatives at this point.  */
11469   if (address_p)
11470     {
11471       cp_parser_error (parser, "invalid non-type template argument");
11472       return error_mark_node;
11473     }
11474
11475   /* If the argument wasn't successfully parsed as a type-id followed
11476      by '>>', the argument can only be a constant expression now.
11477      Otherwise, we try parsing the constant-expression tentatively,
11478      because the argument could really be a type-id.  */
11479   if (maybe_type_id)
11480     cp_parser_parse_tentatively (parser);
11481   argument = cp_parser_constant_expression (parser,
11482                                             /*allow_non_constant_p=*/false,
11483                                             /*non_constant_p=*/NULL);
11484   argument = fold_non_dependent_expr (argument);
11485   if (!maybe_type_id)
11486     return argument;
11487   if (!cp_parser_next_token_ends_template_argument_p (parser))
11488     cp_parser_error (parser, "expected template-argument");
11489   if (cp_parser_parse_definitely (parser))
11490     return argument;
11491   /* We did our best to parse the argument as a non type-id, but that
11492      was the only alternative that matched (albeit with a '>' after
11493      it). We can assume it's just a typo from the user, and a
11494      diagnostic will then be issued.  */
11495   return cp_parser_template_type_arg (parser);
11496 }
11497
11498 /* Parse an explicit-instantiation.
11499
11500    explicit-instantiation:
11501      template declaration
11502
11503    Although the standard says `declaration', what it really means is:
11504
11505    explicit-instantiation:
11506      template decl-specifier-seq [opt] declarator [opt] ;
11507
11508    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11509    supposed to be allowed.  A defect report has been filed about this
11510    issue.
11511
11512    GNU Extension:
11513
11514    explicit-instantiation:
11515      storage-class-specifier template
11516        decl-specifier-seq [opt] declarator [opt] ;
11517      function-specifier template
11518        decl-specifier-seq [opt] declarator [opt] ;  */
11519
11520 static void
11521 cp_parser_explicit_instantiation (cp_parser* parser)
11522 {
11523   int declares_class_or_enum;
11524   cp_decl_specifier_seq decl_specifiers;
11525   tree extension_specifier = NULL_TREE;
11526
11527   /* Look for an (optional) storage-class-specifier or
11528      function-specifier.  */
11529   if (cp_parser_allow_gnu_extensions_p (parser))
11530     {
11531       extension_specifier
11532         = cp_parser_storage_class_specifier_opt (parser);
11533       if (!extension_specifier)
11534         extension_specifier
11535           = cp_parser_function_specifier_opt (parser,
11536                                               /*decl_specs=*/NULL);
11537     }
11538
11539   /* Look for the `template' keyword.  */
11540   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11541   /* Let the front end know that we are processing an explicit
11542      instantiation.  */
11543   begin_explicit_instantiation ();
11544   /* [temp.explicit] says that we are supposed to ignore access
11545      control while processing explicit instantiation directives.  */
11546   push_deferring_access_checks (dk_no_check);
11547   /* Parse a decl-specifier-seq.  */
11548   cp_parser_decl_specifier_seq (parser,
11549                                 CP_PARSER_FLAGS_OPTIONAL,
11550                                 &decl_specifiers,
11551                                 &declares_class_or_enum);
11552   /* If there was exactly one decl-specifier, and it declared a class,
11553      and there's no declarator, then we have an explicit type
11554      instantiation.  */
11555   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11556     {
11557       tree type;
11558
11559       type = check_tag_decl (&decl_specifiers);
11560       /* Turn access control back on for names used during
11561          template instantiation.  */
11562       pop_deferring_access_checks ();
11563       if (type)
11564         do_type_instantiation (type, extension_specifier,
11565                                /*complain=*/tf_error);
11566     }
11567   else
11568     {
11569       cp_declarator *declarator;
11570       tree decl;
11571
11572       /* Parse the declarator.  */
11573       declarator
11574         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11575                                 /*ctor_dtor_or_conv_p=*/NULL,
11576                                 /*parenthesized_p=*/NULL,
11577                                 /*member_p=*/false);
11578       if (declares_class_or_enum & 2)
11579         cp_parser_check_for_definition_in_return_type (declarator,
11580                                                        decl_specifiers.type,
11581                                                        decl_specifiers.type_location);
11582       if (declarator != cp_error_declarator)
11583         {
11584           decl = grokdeclarator (declarator, &decl_specifiers,
11585                                  NORMAL, 0, &decl_specifiers.attributes);
11586           /* Turn access control back on for names used during
11587              template instantiation.  */
11588           pop_deferring_access_checks ();
11589           /* Do the explicit instantiation.  */
11590           do_decl_instantiation (decl, extension_specifier);
11591         }
11592       else
11593         {
11594           pop_deferring_access_checks ();
11595           /* Skip the body of the explicit instantiation.  */
11596           cp_parser_skip_to_end_of_statement (parser);
11597         }
11598     }
11599   /* We're done with the instantiation.  */
11600   end_explicit_instantiation ();
11601
11602   cp_parser_consume_semicolon_at_end_of_statement (parser);
11603 }
11604
11605 /* Parse an explicit-specialization.
11606
11607    explicit-specialization:
11608      template < > declaration
11609
11610    Although the standard says `declaration', what it really means is:
11611
11612    explicit-specialization:
11613      template <> decl-specifier [opt] init-declarator [opt] ;
11614      template <> function-definition
11615      template <> explicit-specialization
11616      template <> template-declaration  */
11617
11618 static void
11619 cp_parser_explicit_specialization (cp_parser* parser)
11620 {
11621   bool need_lang_pop;
11622   cp_token *token = cp_lexer_peek_token (parser->lexer);
11623
11624   /* Look for the `template' keyword.  */
11625   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11626   /* Look for the `<'.  */
11627   cp_parser_require (parser, CPP_LESS, "%<<%>");
11628   /* Look for the `>'.  */
11629   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11630   /* We have processed another parameter list.  */
11631   ++parser->num_template_parameter_lists;
11632   /* [temp]
11633
11634      A template ... explicit specialization ... shall not have C
11635      linkage.  */
11636   if (current_lang_name == lang_name_c)
11637     {
11638       error_at (token->location, "template specialization with C linkage");
11639       /* Give it C++ linkage to avoid confusing other parts of the
11640          front end.  */
11641       push_lang_context (lang_name_cplusplus);
11642       need_lang_pop = true;
11643     }
11644   else
11645     need_lang_pop = false;
11646   /* Let the front end know that we are beginning a specialization.  */
11647   if (!begin_specialization ())
11648     {
11649       end_specialization ();
11650       return;
11651     }
11652
11653   /* If the next keyword is `template', we need to figure out whether
11654      or not we're looking a template-declaration.  */
11655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11656     {
11657       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11658           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11659         cp_parser_template_declaration_after_export (parser,
11660                                                      /*member_p=*/false);
11661       else
11662         cp_parser_explicit_specialization (parser);
11663     }
11664   else
11665     /* Parse the dependent declaration.  */
11666     cp_parser_single_declaration (parser,
11667                                   /*checks=*/NULL,
11668                                   /*member_p=*/false,
11669                                   /*explicit_specialization_p=*/true,
11670                                   /*friend_p=*/NULL);
11671   /* We're done with the specialization.  */
11672   end_specialization ();
11673   /* For the erroneous case of a template with C linkage, we pushed an
11674      implicit C++ linkage scope; exit that scope now.  */
11675   if (need_lang_pop)
11676     pop_lang_context ();
11677   /* We're done with this parameter list.  */
11678   --parser->num_template_parameter_lists;
11679 }
11680
11681 /* Parse a type-specifier.
11682
11683    type-specifier:
11684      simple-type-specifier
11685      class-specifier
11686      enum-specifier
11687      elaborated-type-specifier
11688      cv-qualifier
11689
11690    GNU Extension:
11691
11692    type-specifier:
11693      __complex__
11694
11695    Returns a representation of the type-specifier.  For a
11696    class-specifier, enum-specifier, or elaborated-type-specifier, a
11697    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11698
11699    The parser flags FLAGS is used to control type-specifier parsing.
11700
11701    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11702    in a decl-specifier-seq.
11703
11704    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11705    class-specifier, enum-specifier, or elaborated-type-specifier, then
11706    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11707    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11708    zero.
11709
11710    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11711    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11712    is set to FALSE.  */
11713
11714 static tree
11715 cp_parser_type_specifier (cp_parser* parser,
11716                           cp_parser_flags flags,
11717                           cp_decl_specifier_seq *decl_specs,
11718                           bool is_declaration,
11719                           int* declares_class_or_enum,
11720                           bool* is_cv_qualifier)
11721 {
11722   tree type_spec = NULL_TREE;
11723   cp_token *token;
11724   enum rid keyword;
11725   cp_decl_spec ds = ds_last;
11726
11727   /* Assume this type-specifier does not declare a new type.  */
11728   if (declares_class_or_enum)
11729     *declares_class_or_enum = 0;
11730   /* And that it does not specify a cv-qualifier.  */
11731   if (is_cv_qualifier)
11732     *is_cv_qualifier = false;
11733   /* Peek at the next token.  */
11734   token = cp_lexer_peek_token (parser->lexer);
11735
11736   /* If we're looking at a keyword, we can use that to guide the
11737      production we choose.  */
11738   keyword = token->keyword;
11739   switch (keyword)
11740     {
11741     case RID_ENUM:
11742       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11743         goto elaborated_type_specifier;
11744
11745       /* Look for the enum-specifier.  */
11746       type_spec = cp_parser_enum_specifier (parser);
11747       /* If that worked, we're done.  */
11748       if (type_spec)
11749         {
11750           if (declares_class_or_enum)
11751             *declares_class_or_enum = 2;
11752           if (decl_specs)
11753             cp_parser_set_decl_spec_type (decl_specs,
11754                                           type_spec,
11755                                           token->location,
11756                                           /*user_defined_p=*/true);
11757           return type_spec;
11758         }
11759       else
11760         goto elaborated_type_specifier;
11761
11762       /* Any of these indicate either a class-specifier, or an
11763          elaborated-type-specifier.  */
11764     case RID_CLASS:
11765     case RID_STRUCT:
11766     case RID_UNION:
11767       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11768         goto elaborated_type_specifier;
11769
11770       /* Parse tentatively so that we can back up if we don't find a
11771          class-specifier.  */
11772       cp_parser_parse_tentatively (parser);
11773       /* Look for the class-specifier.  */
11774       type_spec = cp_parser_class_specifier (parser);
11775       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11776       /* If that worked, we're done.  */
11777       if (cp_parser_parse_definitely (parser))
11778         {
11779           if (declares_class_or_enum)
11780             *declares_class_or_enum = 2;
11781           if (decl_specs)
11782             cp_parser_set_decl_spec_type (decl_specs,
11783                                           type_spec,
11784                                           token->location,
11785                                           /*user_defined_p=*/true);
11786           return type_spec;
11787         }
11788
11789       /* Fall through.  */
11790     elaborated_type_specifier:
11791       /* We're declaring (not defining) a class or enum.  */
11792       if (declares_class_or_enum)
11793         *declares_class_or_enum = 1;
11794
11795       /* Fall through.  */
11796     case RID_TYPENAME:
11797       /* Look for an elaborated-type-specifier.  */
11798       type_spec
11799         = (cp_parser_elaborated_type_specifier
11800            (parser,
11801             decl_specs && decl_specs->specs[(int) ds_friend],
11802             is_declaration));
11803       if (decl_specs)
11804         cp_parser_set_decl_spec_type (decl_specs,
11805                                       type_spec,
11806                                       token->location,
11807                                       /*user_defined_p=*/true);
11808       return type_spec;
11809
11810     case RID_CONST:
11811       ds = ds_const;
11812       if (is_cv_qualifier)
11813         *is_cv_qualifier = true;
11814       break;
11815
11816     case RID_VOLATILE:
11817       ds = ds_volatile;
11818       if (is_cv_qualifier)
11819         *is_cv_qualifier = true;
11820       break;
11821
11822     case RID_RESTRICT:
11823       ds = ds_restrict;
11824       if (is_cv_qualifier)
11825         *is_cv_qualifier = true;
11826       break;
11827
11828     case RID_COMPLEX:
11829       /* The `__complex__' keyword is a GNU extension.  */
11830       ds = ds_complex;
11831       break;
11832
11833     default:
11834       break;
11835     }
11836
11837   /* Handle simple keywords.  */
11838   if (ds != ds_last)
11839     {
11840       if (decl_specs)
11841         {
11842           ++decl_specs->specs[(int)ds];
11843           decl_specs->any_specifiers_p = true;
11844         }
11845       return cp_lexer_consume_token (parser->lexer)->u.value;
11846     }
11847
11848   /* If we do not already have a type-specifier, assume we are looking
11849      at a simple-type-specifier.  */
11850   type_spec = cp_parser_simple_type_specifier (parser,
11851                                                decl_specs,
11852                                                flags);
11853
11854   /* If we didn't find a type-specifier, and a type-specifier was not
11855      optional in this context, issue an error message.  */
11856   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11857     {
11858       cp_parser_error (parser, "expected type specifier");
11859       return error_mark_node;
11860     }
11861
11862   return type_spec;
11863 }
11864
11865 /* Parse a simple-type-specifier.
11866
11867    simple-type-specifier:
11868      :: [opt] nested-name-specifier [opt] type-name
11869      :: [opt] nested-name-specifier template template-id
11870      char
11871      wchar_t
11872      bool
11873      short
11874      int
11875      long
11876      signed
11877      unsigned
11878      float
11879      double
11880      void
11881
11882    C++0x Extension:
11883
11884    simple-type-specifier:
11885      auto
11886      decltype ( expression )   
11887      char16_t
11888      char32_t
11889
11890    GNU Extension:
11891
11892    simple-type-specifier:
11893      __typeof__ unary-expression
11894      __typeof__ ( type-id )
11895
11896    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11897    appropriately updated.  */
11898
11899 static tree
11900 cp_parser_simple_type_specifier (cp_parser* parser,
11901                                  cp_decl_specifier_seq *decl_specs,
11902                                  cp_parser_flags flags)
11903 {
11904   tree type = NULL_TREE;
11905   cp_token *token;
11906
11907   /* Peek at the next token.  */
11908   token = cp_lexer_peek_token (parser->lexer);
11909
11910   /* If we're looking at a keyword, things are easy.  */
11911   switch (token->keyword)
11912     {
11913     case RID_CHAR:
11914       if (decl_specs)
11915         decl_specs->explicit_char_p = true;
11916       type = char_type_node;
11917       break;
11918     case RID_CHAR16:
11919       type = char16_type_node;
11920       break;
11921     case RID_CHAR32:
11922       type = char32_type_node;
11923       break;
11924     case RID_WCHAR:
11925       type = wchar_type_node;
11926       break;
11927     case RID_BOOL:
11928       type = boolean_type_node;
11929       break;
11930     case RID_SHORT:
11931       if (decl_specs)
11932         ++decl_specs->specs[(int) ds_short];
11933       type = short_integer_type_node;
11934       break;
11935     case RID_INT:
11936       if (decl_specs)
11937         decl_specs->explicit_int_p = true;
11938       type = integer_type_node;
11939       break;
11940     case RID_LONG:
11941       if (decl_specs)
11942         ++decl_specs->specs[(int) ds_long];
11943       type = long_integer_type_node;
11944       break;
11945     case RID_SIGNED:
11946       if (decl_specs)
11947         ++decl_specs->specs[(int) ds_signed];
11948       type = integer_type_node;
11949       break;
11950     case RID_UNSIGNED:
11951       if (decl_specs)
11952         ++decl_specs->specs[(int) ds_unsigned];
11953       type = unsigned_type_node;
11954       break;
11955     case RID_FLOAT:
11956       type = float_type_node;
11957       break;
11958     case RID_DOUBLE:
11959       type = double_type_node;
11960       break;
11961     case RID_VOID:
11962       type = void_type_node;
11963       break;
11964       
11965     case RID_AUTO:
11966       maybe_warn_cpp0x (CPP0X_AUTO);
11967       type = make_auto ();
11968       break;
11969
11970     case RID_DECLTYPE:
11971       /* Parse the `decltype' type.  */
11972       type = cp_parser_decltype (parser);
11973
11974       if (decl_specs)
11975         cp_parser_set_decl_spec_type (decl_specs, type,
11976                                       token->location,
11977                                       /*user_defined_p=*/true);
11978
11979       return type;
11980
11981     case RID_TYPEOF:
11982       /* Consume the `typeof' token.  */
11983       cp_lexer_consume_token (parser->lexer);
11984       /* Parse the operand to `typeof'.  */
11985       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11986       /* If it is not already a TYPE, take its type.  */
11987       if (!TYPE_P (type))
11988         type = finish_typeof (type);
11989
11990       if (decl_specs)
11991         cp_parser_set_decl_spec_type (decl_specs, type,
11992                                       token->location,
11993                                       /*user_defined_p=*/true);
11994
11995       return type;
11996
11997     default:
11998       break;
11999     }
12000
12001   /* If the type-specifier was for a built-in type, we're done.  */
12002   if (type)
12003     {
12004       /* Record the type.  */
12005       if (decl_specs
12006           && (token->keyword != RID_SIGNED
12007               && token->keyword != RID_UNSIGNED
12008               && token->keyword != RID_SHORT
12009               && token->keyword != RID_LONG))
12010         cp_parser_set_decl_spec_type (decl_specs,
12011                                       type,
12012                                       token->location,
12013                                       /*user_defined=*/false);
12014       if (decl_specs)
12015         decl_specs->any_specifiers_p = true;
12016
12017       /* Consume the token.  */
12018       cp_lexer_consume_token (parser->lexer);
12019
12020       /* There is no valid C++ program where a non-template type is
12021          followed by a "<".  That usually indicates that the user thought
12022          that the type was a template.  */
12023       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12024
12025       return TYPE_NAME (type);
12026     }
12027
12028   /* The type-specifier must be a user-defined type.  */
12029   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12030     {
12031       bool qualified_p;
12032       bool global_p;
12033
12034       /* Don't gobble tokens or issue error messages if this is an
12035          optional type-specifier.  */
12036       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12037         cp_parser_parse_tentatively (parser);
12038
12039       /* Look for the optional `::' operator.  */
12040       global_p
12041         = (cp_parser_global_scope_opt (parser,
12042                                        /*current_scope_valid_p=*/false)
12043            != NULL_TREE);
12044       /* Look for the nested-name specifier.  */
12045       qualified_p
12046         = (cp_parser_nested_name_specifier_opt (parser,
12047                                                 /*typename_keyword_p=*/false,
12048                                                 /*check_dependency_p=*/true,
12049                                                 /*type_p=*/false,
12050                                                 /*is_declaration=*/false)
12051            != NULL_TREE);
12052       token = cp_lexer_peek_token (parser->lexer);
12053       /* If we have seen a nested-name-specifier, and the next token
12054          is `template', then we are using the template-id production.  */
12055       if (parser->scope
12056           && cp_parser_optional_template_keyword (parser))
12057         {
12058           /* Look for the template-id.  */
12059           type = cp_parser_template_id (parser,
12060                                         /*template_keyword_p=*/true,
12061                                         /*check_dependency_p=*/true,
12062                                         /*is_declaration=*/false);
12063           /* If the template-id did not name a type, we are out of
12064              luck.  */
12065           if (TREE_CODE (type) != TYPE_DECL)
12066             {
12067               cp_parser_error (parser, "expected template-id for type");
12068               type = NULL_TREE;
12069             }
12070         }
12071       /* Otherwise, look for a type-name.  */
12072       else
12073         type = cp_parser_type_name (parser);
12074       /* Keep track of all name-lookups performed in class scopes.  */
12075       if (type
12076           && !global_p
12077           && !qualified_p
12078           && TREE_CODE (type) == TYPE_DECL
12079           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12080         maybe_note_name_used_in_class (DECL_NAME (type), type);
12081       /* If it didn't work out, we don't have a TYPE.  */
12082       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12083           && !cp_parser_parse_definitely (parser))
12084         type = NULL_TREE;
12085       if (type && decl_specs)
12086         cp_parser_set_decl_spec_type (decl_specs, type,
12087                                       token->location,
12088                                       /*user_defined=*/true);
12089     }
12090
12091   /* If we didn't get a type-name, issue an error message.  */
12092   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12093     {
12094       cp_parser_error (parser, "expected type-name");
12095       return error_mark_node;
12096     }
12097
12098   /* There is no valid C++ program where a non-template type is
12099      followed by a "<".  That usually indicates that the user thought
12100      that the type was a template.  */
12101   if (type && type != error_mark_node)
12102     {
12103       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12104          If it is, then the '<'...'>' enclose protocol names rather than
12105          template arguments, and so everything is fine.  */
12106       if (c_dialect_objc ()
12107           && (objc_is_id (type) || objc_is_class_name (type)))
12108         {
12109           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12110           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12111
12112           /* Clobber the "unqualified" type previously entered into
12113              DECL_SPECS with the new, improved protocol-qualified version.  */
12114           if (decl_specs)
12115             decl_specs->type = qual_type;
12116
12117           return qual_type;
12118         }
12119
12120       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12121                                                token->location);
12122     }
12123
12124   return type;
12125 }
12126
12127 /* Parse a type-name.
12128
12129    type-name:
12130      class-name
12131      enum-name
12132      typedef-name
12133
12134    enum-name:
12135      identifier
12136
12137    typedef-name:
12138      identifier
12139
12140    Returns a TYPE_DECL for the type.  */
12141
12142 static tree
12143 cp_parser_type_name (cp_parser* parser)
12144 {
12145   tree type_decl;
12146
12147   /* We can't know yet whether it is a class-name or not.  */
12148   cp_parser_parse_tentatively (parser);
12149   /* Try a class-name.  */
12150   type_decl = cp_parser_class_name (parser,
12151                                     /*typename_keyword_p=*/false,
12152                                     /*template_keyword_p=*/false,
12153                                     none_type,
12154                                     /*check_dependency_p=*/true,
12155                                     /*class_head_p=*/false,
12156                                     /*is_declaration=*/false);
12157   /* If it's not a class-name, keep looking.  */
12158   if (!cp_parser_parse_definitely (parser))
12159     {
12160       /* It must be a typedef-name or an enum-name.  */
12161       return cp_parser_nonclass_name (parser);
12162     }
12163
12164   return type_decl;
12165 }
12166
12167 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12168
12169    enum-name:
12170      identifier
12171
12172    typedef-name:
12173      identifier
12174
12175    Returns a TYPE_DECL for the type.  */
12176
12177 static tree
12178 cp_parser_nonclass_name (cp_parser* parser)
12179 {
12180   tree type_decl;
12181   tree identifier;
12182
12183   cp_token *token = cp_lexer_peek_token (parser->lexer);
12184   identifier = cp_parser_identifier (parser);
12185   if (identifier == error_mark_node)
12186     return error_mark_node;
12187
12188   /* Look up the type-name.  */
12189   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12190
12191   if (TREE_CODE (type_decl) != TYPE_DECL
12192       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12193     {
12194       /* See if this is an Objective-C type.  */
12195       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12196       tree type = objc_get_protocol_qualified_type (identifier, protos);
12197       if (type)
12198         type_decl = TYPE_NAME (type);
12199     }
12200   
12201   /* Issue an error if we did not find a type-name.  */
12202   if (TREE_CODE (type_decl) != TYPE_DECL)
12203     {
12204       if (!cp_parser_simulate_error (parser))
12205         cp_parser_name_lookup_error (parser, identifier, type_decl,
12206                                      "is not a type", token->location);
12207       return error_mark_node;
12208     }
12209   /* Remember that the name was used in the definition of the
12210      current class so that we can check later to see if the
12211      meaning would have been different after the class was
12212      entirely defined.  */
12213   else if (type_decl != error_mark_node
12214            && !parser->scope)
12215     maybe_note_name_used_in_class (identifier, type_decl);
12216   
12217   return type_decl;
12218 }
12219
12220 /* Parse an elaborated-type-specifier.  Note that the grammar given
12221    here incorporates the resolution to DR68.
12222
12223    elaborated-type-specifier:
12224      class-key :: [opt] nested-name-specifier [opt] identifier
12225      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12226      enum-key :: [opt] nested-name-specifier [opt] identifier
12227      typename :: [opt] nested-name-specifier identifier
12228      typename :: [opt] nested-name-specifier template [opt]
12229        template-id
12230
12231    GNU extension:
12232
12233    elaborated-type-specifier:
12234      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12235      class-key attributes :: [opt] nested-name-specifier [opt]
12236                template [opt] template-id
12237      enum attributes :: [opt] nested-name-specifier [opt] identifier
12238
12239    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12240    declared `friend'.  If IS_DECLARATION is TRUE, then this
12241    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12242    something is being declared.
12243
12244    Returns the TYPE specified.  */
12245
12246 static tree
12247 cp_parser_elaborated_type_specifier (cp_parser* parser,
12248                                      bool is_friend,
12249                                      bool is_declaration)
12250 {
12251   enum tag_types tag_type;
12252   tree identifier;
12253   tree type = NULL_TREE;
12254   tree attributes = NULL_TREE;
12255   tree globalscope;
12256   cp_token *token = NULL;
12257
12258   /* See if we're looking at the `enum' keyword.  */
12259   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12260     {
12261       /* Consume the `enum' token.  */
12262       cp_lexer_consume_token (parser->lexer);
12263       /* Remember that it's an enumeration type.  */
12264       tag_type = enum_type;
12265       /* Parse the optional `struct' or `class' key (for C++0x scoped
12266          enums).  */
12267       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12268           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12269         {
12270           if (cxx_dialect == cxx98)
12271             maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12272
12273           /* Consume the `struct' or `class'.  */
12274           cp_lexer_consume_token (parser->lexer);
12275         }
12276       /* Parse the attributes.  */
12277       attributes = cp_parser_attributes_opt (parser);
12278     }
12279   /* Or, it might be `typename'.  */
12280   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12281                                            RID_TYPENAME))
12282     {
12283       /* Consume the `typename' token.  */
12284       cp_lexer_consume_token (parser->lexer);
12285       /* Remember that it's a `typename' type.  */
12286       tag_type = typename_type;
12287     }
12288   /* Otherwise it must be a class-key.  */
12289   else
12290     {
12291       tag_type = cp_parser_class_key (parser);
12292       if (tag_type == none_type)
12293         return error_mark_node;
12294       /* Parse the attributes.  */
12295       attributes = cp_parser_attributes_opt (parser);
12296     }
12297
12298   /* Look for the `::' operator.  */
12299   globalscope =  cp_parser_global_scope_opt (parser,
12300                                              /*current_scope_valid_p=*/false);
12301   /* Look for the nested-name-specifier.  */
12302   if (tag_type == typename_type && !globalscope)
12303     {
12304       if (!cp_parser_nested_name_specifier (parser,
12305                                            /*typename_keyword_p=*/true,
12306                                            /*check_dependency_p=*/true,
12307                                            /*type_p=*/true,
12308                                             is_declaration))
12309         return error_mark_node;
12310     }
12311   else
12312     /* Even though `typename' is not present, the proposed resolution
12313        to Core Issue 180 says that in `class A<T>::B', `B' should be
12314        considered a type-name, even if `A<T>' is dependent.  */
12315     cp_parser_nested_name_specifier_opt (parser,
12316                                          /*typename_keyword_p=*/true,
12317                                          /*check_dependency_p=*/true,
12318                                          /*type_p=*/true,
12319                                          is_declaration);
12320  /* For everything but enumeration types, consider a template-id.
12321     For an enumeration type, consider only a plain identifier.  */
12322   if (tag_type != enum_type)
12323     {
12324       bool template_p = false;
12325       tree decl;
12326
12327       /* Allow the `template' keyword.  */
12328       template_p = cp_parser_optional_template_keyword (parser);
12329       /* If we didn't see `template', we don't know if there's a
12330          template-id or not.  */
12331       if (!template_p)
12332         cp_parser_parse_tentatively (parser);
12333       /* Parse the template-id.  */
12334       token = cp_lexer_peek_token (parser->lexer);
12335       decl = cp_parser_template_id (parser, template_p,
12336                                     /*check_dependency_p=*/true,
12337                                     is_declaration);
12338       /* If we didn't find a template-id, look for an ordinary
12339          identifier.  */
12340       if (!template_p && !cp_parser_parse_definitely (parser))
12341         ;
12342       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12343          in effect, then we must assume that, upon instantiation, the
12344          template will correspond to a class.  */
12345       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12346                && tag_type == typename_type)
12347         type = make_typename_type (parser->scope, decl,
12348                                    typename_type,
12349                                    /*complain=*/tf_error);
12350       /* If the `typename' keyword is in effect and DECL is not a type
12351          decl. Then type is non existant.   */
12352       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12353         type = NULL_TREE; 
12354       else 
12355         type = TREE_TYPE (decl);
12356     }
12357
12358   if (!type)
12359     {
12360       token = cp_lexer_peek_token (parser->lexer);
12361       identifier = cp_parser_identifier (parser);
12362
12363       if (identifier == error_mark_node)
12364         {
12365           parser->scope = NULL_TREE;
12366           return error_mark_node;
12367         }
12368
12369       /* For a `typename', we needn't call xref_tag.  */
12370       if (tag_type == typename_type
12371           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12372         return cp_parser_make_typename_type (parser, parser->scope,
12373                                              identifier,
12374                                              token->location);
12375       /* Look up a qualified name in the usual way.  */
12376       if (parser->scope)
12377         {
12378           tree decl;
12379           tree ambiguous_decls;
12380
12381           decl = cp_parser_lookup_name (parser, identifier,
12382                                         tag_type,
12383                                         /*is_template=*/false,
12384                                         /*is_namespace=*/false,
12385                                         /*check_dependency=*/true,
12386                                         &ambiguous_decls,
12387                                         token->location);
12388
12389           /* If the lookup was ambiguous, an error will already have been
12390              issued.  */
12391           if (ambiguous_decls)
12392             return error_mark_node;
12393
12394           /* If we are parsing friend declaration, DECL may be a
12395              TEMPLATE_DECL tree node here.  However, we need to check
12396              whether this TEMPLATE_DECL results in valid code.  Consider
12397              the following example:
12398
12399                namespace N {
12400                  template <class T> class C {};
12401                }
12402                class X {
12403                  template <class T> friend class N::C; // #1, valid code
12404                };
12405                template <class T> class Y {
12406                  friend class N::C;                    // #2, invalid code
12407                };
12408
12409              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12410              name lookup of `N::C'.  We see that friend declaration must
12411              be template for the code to be valid.  Note that
12412              processing_template_decl does not work here since it is
12413              always 1 for the above two cases.  */
12414
12415           decl = (cp_parser_maybe_treat_template_as_class
12416                   (decl, /*tag_name_p=*/is_friend
12417                          && parser->num_template_parameter_lists));
12418
12419           if (TREE_CODE (decl) != TYPE_DECL)
12420             {
12421               cp_parser_diagnose_invalid_type_name (parser,
12422                                                     parser->scope,
12423                                                     identifier,
12424                                                     token->location);
12425               return error_mark_node;
12426             }
12427
12428           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12429             {
12430               bool allow_template = (parser->num_template_parameter_lists
12431                                       || DECL_SELF_REFERENCE_P (decl));
12432               type = check_elaborated_type_specifier (tag_type, decl, 
12433                                                       allow_template);
12434
12435               if (type == error_mark_node)
12436                 return error_mark_node;
12437             }
12438
12439           /* Forward declarations of nested types, such as
12440
12441                class C1::C2;
12442                class C1::C2::C3;
12443
12444              are invalid unless all components preceding the final '::'
12445              are complete.  If all enclosing types are complete, these
12446              declarations become merely pointless.
12447
12448              Invalid forward declarations of nested types are errors
12449              caught elsewhere in parsing.  Those that are pointless arrive
12450              here.  */
12451
12452           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12453               && !is_friend && !processing_explicit_instantiation)
12454             warning (0, "declaration %qD does not declare anything", decl);
12455
12456           type = TREE_TYPE (decl);
12457         }
12458       else
12459         {
12460           /* An elaborated-type-specifier sometimes introduces a new type and
12461              sometimes names an existing type.  Normally, the rule is that it
12462              introduces a new type only if there is not an existing type of
12463              the same name already in scope.  For example, given:
12464
12465                struct S {};
12466                void f() { struct S s; }
12467
12468              the `struct S' in the body of `f' is the same `struct S' as in
12469              the global scope; the existing definition is used.  However, if
12470              there were no global declaration, this would introduce a new
12471              local class named `S'.
12472
12473              An exception to this rule applies to the following code:
12474
12475                namespace N { struct S; }
12476
12477              Here, the elaborated-type-specifier names a new type
12478              unconditionally; even if there is already an `S' in the
12479              containing scope this declaration names a new type.
12480              This exception only applies if the elaborated-type-specifier
12481              forms the complete declaration:
12482
12483                [class.name]
12484
12485                A declaration consisting solely of `class-key identifier ;' is
12486                either a redeclaration of the name in the current scope or a
12487                forward declaration of the identifier as a class name.  It
12488                introduces the name into the current scope.
12489
12490              We are in this situation precisely when the next token is a `;'.
12491
12492              An exception to the exception is that a `friend' declaration does
12493              *not* name a new type; i.e., given:
12494
12495                struct S { friend struct T; };
12496
12497              `T' is not a new type in the scope of `S'.
12498
12499              Also, `new struct S' or `sizeof (struct S)' never results in the
12500              definition of a new type; a new type can only be declared in a
12501              declaration context.  */
12502
12503           tag_scope ts;
12504           bool template_p;
12505
12506           if (is_friend)
12507             /* Friends have special name lookup rules.  */
12508             ts = ts_within_enclosing_non_class;
12509           else if (is_declaration
12510                    && cp_lexer_next_token_is (parser->lexer,
12511                                               CPP_SEMICOLON))
12512             /* This is a `class-key identifier ;' */
12513             ts = ts_current;
12514           else
12515             ts = ts_global;
12516
12517           template_p =
12518             (parser->num_template_parameter_lists
12519              && (cp_parser_next_token_starts_class_definition_p (parser)
12520                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12521           /* An unqualified name was used to reference this type, so
12522              there were no qualifying templates.  */
12523           if (!cp_parser_check_template_parameters (parser,
12524                                                     /*num_templates=*/0,
12525                                                     token->location,
12526                                                     /*declarator=*/NULL))
12527             return error_mark_node;
12528           type = xref_tag (tag_type, identifier, ts, template_p);
12529         }
12530     }
12531
12532   if (type == error_mark_node)
12533     return error_mark_node;
12534
12535   /* Allow attributes on forward declarations of classes.  */
12536   if (attributes)
12537     {
12538       if (TREE_CODE (type) == TYPENAME_TYPE)
12539         warning (OPT_Wattributes,
12540                  "attributes ignored on uninstantiated type");
12541       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12542                && ! processing_explicit_instantiation)
12543         warning (OPT_Wattributes,
12544                  "attributes ignored on template instantiation");
12545       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12546         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12547       else
12548         warning (OPT_Wattributes,
12549                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12550     }
12551
12552   if (tag_type != enum_type)
12553     cp_parser_check_class_key (tag_type, type);
12554
12555   /* A "<" cannot follow an elaborated type specifier.  If that
12556      happens, the user was probably trying to form a template-id.  */
12557   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12558
12559   return type;
12560 }
12561
12562 /* Parse an enum-specifier.
12563
12564    enum-specifier:
12565      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12566
12567    enum-key:
12568      enum
12569      enum class   [C++0x]
12570      enum struct  [C++0x]
12571
12572    enum-base:   [C++0x]
12573      : type-specifier-seq
12574
12575    GNU Extensions:
12576      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12577        { enumerator-list [opt] }attributes[opt]
12578
12579    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12580    if the token stream isn't an enum-specifier after all.  */
12581
12582 static tree
12583 cp_parser_enum_specifier (cp_parser* parser)
12584 {
12585   tree identifier;
12586   tree type;
12587   tree attributes;
12588   bool scoped_enum_p = false;
12589   bool has_underlying_type = false;
12590   tree underlying_type = NULL_TREE;
12591
12592   /* Parse tentatively so that we can back up if we don't find a
12593      enum-specifier.  */
12594   cp_parser_parse_tentatively (parser);
12595
12596   /* Caller guarantees that the current token is 'enum', an identifier
12597      possibly follows, and the token after that is an opening brace.
12598      If we don't have an identifier, fabricate an anonymous name for
12599      the enumeration being defined.  */
12600   cp_lexer_consume_token (parser->lexer);
12601
12602   /* Parse the "class" or "struct", which indicates a scoped
12603      enumeration type in C++0x.  */
12604   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12605       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12606     {
12607       if (cxx_dialect == cxx98)
12608         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12609
12610       /* Consume the `struct' or `class' token.  */
12611       cp_lexer_consume_token (parser->lexer);
12612
12613       scoped_enum_p = true;
12614     }
12615
12616   attributes = cp_parser_attributes_opt (parser);
12617
12618   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12619     identifier = cp_parser_identifier (parser);
12620   else
12621     identifier = make_anon_name ();
12622
12623   /* Check for the `:' that denotes a specified underlying type in C++0x.
12624      Note that a ':' could also indicate a bitfield width, however.  */
12625   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12626     {
12627       cp_decl_specifier_seq type_specifiers;
12628
12629       /* Consume the `:'.  */
12630       cp_lexer_consume_token (parser->lexer);
12631
12632       /* Parse the type-specifier-seq.  */
12633       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
12634                                     /*is_trailing_return=*/false,
12635                                     &type_specifiers);
12636
12637       /* At this point this is surely not elaborated type specifier.  */
12638       if (!cp_parser_parse_definitely (parser))
12639         return NULL_TREE;
12640
12641       if (cxx_dialect == cxx98)
12642         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
12643
12644       has_underlying_type = true;
12645
12646       /* If that didn't work, stop.  */
12647       if (type_specifiers.type != error_mark_node)
12648         {
12649           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12650                                             /*initialized=*/0, NULL);
12651           if (underlying_type == error_mark_node)
12652             underlying_type = NULL_TREE;
12653         }
12654     }
12655
12656   /* Look for the `{' but don't consume it yet.  */
12657   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12658     {
12659       cp_parser_error (parser, "expected %<{%>");
12660       if (has_underlying_type)
12661         return NULL_TREE;
12662     }
12663
12664   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12665     return NULL_TREE;
12666
12667   /* Issue an error message if type-definitions are forbidden here.  */
12668   if (!cp_parser_check_type_definition (parser))
12669     type = error_mark_node;
12670   else
12671     /* Create the new type.  We do this before consuming the opening
12672        brace so the enum will be recorded as being on the line of its
12673        tag (or the 'enum' keyword, if there is no tag).  */
12674     type = start_enum (identifier, underlying_type, scoped_enum_p);
12675   
12676   /* Consume the opening brace.  */
12677   cp_lexer_consume_token (parser->lexer);
12678
12679   if (type == error_mark_node)
12680     {
12681       cp_parser_skip_to_end_of_block_or_statement (parser);
12682       return error_mark_node;
12683     }
12684
12685   /* If the next token is not '}', then there are some enumerators.  */
12686   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12687     cp_parser_enumerator_list (parser, type);
12688
12689   /* Consume the final '}'.  */
12690   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12691
12692   /* Look for trailing attributes to apply to this enumeration, and
12693      apply them if appropriate.  */
12694   if (cp_parser_allow_gnu_extensions_p (parser))
12695     {
12696       tree trailing_attr = cp_parser_attributes_opt (parser);
12697       trailing_attr = chainon (trailing_attr, attributes);
12698       cplus_decl_attributes (&type,
12699                              trailing_attr,
12700                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12701     }
12702
12703   /* Finish up the enumeration.  */
12704   finish_enum (type);
12705
12706   return type;
12707 }
12708
12709 /* Parse an enumerator-list.  The enumerators all have the indicated
12710    TYPE.
12711
12712    enumerator-list:
12713      enumerator-definition
12714      enumerator-list , enumerator-definition  */
12715
12716 static void
12717 cp_parser_enumerator_list (cp_parser* parser, tree type)
12718 {
12719   while (true)
12720     {
12721       /* Parse an enumerator-definition.  */
12722       cp_parser_enumerator_definition (parser, type);
12723
12724       /* If the next token is not a ',', we've reached the end of
12725          the list.  */
12726       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12727         break;
12728       /* Otherwise, consume the `,' and keep going.  */
12729       cp_lexer_consume_token (parser->lexer);
12730       /* If the next token is a `}', there is a trailing comma.  */
12731       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12732         {
12733           if (!in_system_header)
12734             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12735           break;
12736         }
12737     }
12738 }
12739
12740 /* Parse an enumerator-definition.  The enumerator has the indicated
12741    TYPE.
12742
12743    enumerator-definition:
12744      enumerator
12745      enumerator = constant-expression
12746
12747    enumerator:
12748      identifier  */
12749
12750 static void
12751 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12752 {
12753   tree identifier;
12754   tree value;
12755
12756   /* Look for the identifier.  */
12757   identifier = cp_parser_identifier (parser);
12758   if (identifier == error_mark_node)
12759     return;
12760
12761   /* If the next token is an '=', then there is an explicit value.  */
12762   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12763     {
12764       /* Consume the `=' token.  */
12765       cp_lexer_consume_token (parser->lexer);
12766       /* Parse the value.  */
12767       value = cp_parser_constant_expression (parser,
12768                                              /*allow_non_constant_p=*/false,
12769                                              NULL);
12770     }
12771   else
12772     value = NULL_TREE;
12773
12774   /* If we are processing a template, make sure the initializer of the
12775      enumerator doesn't contain any bare template parameter pack.  */
12776   if (check_for_bare_parameter_packs (value))
12777     value = error_mark_node;
12778
12779   /* Create the enumerator.  */
12780   build_enumerator (identifier, value, type);
12781 }
12782
12783 /* Parse a namespace-name.
12784
12785    namespace-name:
12786      original-namespace-name
12787      namespace-alias
12788
12789    Returns the NAMESPACE_DECL for the namespace.  */
12790
12791 static tree
12792 cp_parser_namespace_name (cp_parser* parser)
12793 {
12794   tree identifier;
12795   tree namespace_decl;
12796
12797   cp_token *token = cp_lexer_peek_token (parser->lexer);
12798
12799   /* Get the name of the namespace.  */
12800   identifier = cp_parser_identifier (parser);
12801   if (identifier == error_mark_node)
12802     return error_mark_node;
12803
12804   /* Look up the identifier in the currently active scope.  Look only
12805      for namespaces, due to:
12806
12807        [basic.lookup.udir]
12808
12809        When looking up a namespace-name in a using-directive or alias
12810        definition, only namespace names are considered.
12811
12812      And:
12813
12814        [basic.lookup.qual]
12815
12816        During the lookup of a name preceding the :: scope resolution
12817        operator, object, function, and enumerator names are ignored.
12818
12819      (Note that cp_parser_qualifying_entity only calls this
12820      function if the token after the name is the scope resolution
12821      operator.)  */
12822   namespace_decl = cp_parser_lookup_name (parser, identifier,
12823                                           none_type,
12824                                           /*is_template=*/false,
12825                                           /*is_namespace=*/true,
12826                                           /*check_dependency=*/true,
12827                                           /*ambiguous_decls=*/NULL,
12828                                           token->location);
12829   /* If it's not a namespace, issue an error.  */
12830   if (namespace_decl == error_mark_node
12831       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12832     {
12833       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12834         error_at (token->location, "%qD is not a namespace-name", identifier);
12835       cp_parser_error (parser, "expected namespace-name");
12836       namespace_decl = error_mark_node;
12837     }
12838
12839   return namespace_decl;
12840 }
12841
12842 /* Parse a namespace-definition.
12843
12844    namespace-definition:
12845      named-namespace-definition
12846      unnamed-namespace-definition
12847
12848    named-namespace-definition:
12849      original-namespace-definition
12850      extension-namespace-definition
12851
12852    original-namespace-definition:
12853      namespace identifier { namespace-body }
12854
12855    extension-namespace-definition:
12856      namespace original-namespace-name { namespace-body }
12857
12858    unnamed-namespace-definition:
12859      namespace { namespace-body } */
12860
12861 static void
12862 cp_parser_namespace_definition (cp_parser* parser)
12863 {
12864   tree identifier, attribs;
12865   bool has_visibility;
12866   bool is_inline;
12867
12868   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12869     {
12870       is_inline = true;
12871       cp_lexer_consume_token (parser->lexer);
12872     }
12873   else
12874     is_inline = false;
12875
12876   /* Look for the `namespace' keyword.  */
12877   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12878
12879   /* Get the name of the namespace.  We do not attempt to distinguish
12880      between an original-namespace-definition and an
12881      extension-namespace-definition at this point.  The semantic
12882      analysis routines are responsible for that.  */
12883   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12884     identifier = cp_parser_identifier (parser);
12885   else
12886     identifier = NULL_TREE;
12887
12888   /* Parse any specified attributes.  */
12889   attribs = cp_parser_attributes_opt (parser);
12890
12891   /* Look for the `{' to start the namespace.  */
12892   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12893   /* Start the namespace.  */
12894   push_namespace (identifier);
12895
12896   /* "inline namespace" is equivalent to a stub namespace definition
12897      followed by a strong using directive.  */
12898   if (is_inline)
12899     {
12900       tree name_space = current_namespace;
12901       /* Set up namespace association.  */
12902       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12903         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12904                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12905       /* Import the contents of the inline namespace.  */
12906       pop_namespace ();
12907       do_using_directive (name_space);
12908       push_namespace (identifier);
12909     }
12910
12911   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12912
12913   /* Parse the body of the namespace.  */
12914   cp_parser_namespace_body (parser);
12915
12916 #ifdef HANDLE_PRAGMA_VISIBILITY
12917   if (has_visibility)
12918     pop_visibility (1);
12919 #endif
12920
12921   /* Finish the namespace.  */
12922   pop_namespace ();
12923   /* Look for the final `}'.  */
12924   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12925 }
12926
12927 /* Parse a namespace-body.
12928
12929    namespace-body:
12930      declaration-seq [opt]  */
12931
12932 static void
12933 cp_parser_namespace_body (cp_parser* parser)
12934 {
12935   cp_parser_declaration_seq_opt (parser);
12936 }
12937
12938 /* Parse a namespace-alias-definition.
12939
12940    namespace-alias-definition:
12941      namespace identifier = qualified-namespace-specifier ;  */
12942
12943 static void
12944 cp_parser_namespace_alias_definition (cp_parser* parser)
12945 {
12946   tree identifier;
12947   tree namespace_specifier;
12948
12949   cp_token *token = cp_lexer_peek_token (parser->lexer);
12950
12951   /* Look for the `namespace' keyword.  */
12952   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12953   /* Look for the identifier.  */
12954   identifier = cp_parser_identifier (parser);
12955   if (identifier == error_mark_node)
12956     return;
12957   /* Look for the `=' token.  */
12958   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12959       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12960     {
12961       error_at (token->location, "%<namespace%> definition is not allowed here");
12962       /* Skip the definition.  */
12963       cp_lexer_consume_token (parser->lexer);
12964       if (cp_parser_skip_to_closing_brace (parser))
12965         cp_lexer_consume_token (parser->lexer);
12966       return;
12967     }
12968   cp_parser_require (parser, CPP_EQ, "%<=%>");
12969   /* Look for the qualified-namespace-specifier.  */
12970   namespace_specifier
12971     = cp_parser_qualified_namespace_specifier (parser);
12972   /* Look for the `;' token.  */
12973   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12974
12975   /* Register the alias in the symbol table.  */
12976   do_namespace_alias (identifier, namespace_specifier);
12977 }
12978
12979 /* Parse a qualified-namespace-specifier.
12980
12981    qualified-namespace-specifier:
12982      :: [opt] nested-name-specifier [opt] namespace-name
12983
12984    Returns a NAMESPACE_DECL corresponding to the specified
12985    namespace.  */
12986
12987 static tree
12988 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12989 {
12990   /* Look for the optional `::'.  */
12991   cp_parser_global_scope_opt (parser,
12992                               /*current_scope_valid_p=*/false);
12993
12994   /* Look for the optional nested-name-specifier.  */
12995   cp_parser_nested_name_specifier_opt (parser,
12996                                        /*typename_keyword_p=*/false,
12997                                        /*check_dependency_p=*/true,
12998                                        /*type_p=*/false,
12999                                        /*is_declaration=*/true);
13000
13001   return cp_parser_namespace_name (parser);
13002 }
13003
13004 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13005    access declaration.
13006
13007    using-declaration:
13008      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13009      using :: unqualified-id ;  
13010
13011    access-declaration:
13012      qualified-id ;  
13013
13014    */
13015
13016 static bool
13017 cp_parser_using_declaration (cp_parser* parser, 
13018                              bool access_declaration_p)
13019 {
13020   cp_token *token;
13021   bool typename_p = false;
13022   bool global_scope_p;
13023   tree decl;
13024   tree identifier;
13025   tree qscope;
13026
13027   if (access_declaration_p)
13028     cp_parser_parse_tentatively (parser);
13029   else
13030     {
13031       /* Look for the `using' keyword.  */
13032       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13033       
13034       /* Peek at the next token.  */
13035       token = cp_lexer_peek_token (parser->lexer);
13036       /* See if it's `typename'.  */
13037       if (token->keyword == RID_TYPENAME)
13038         {
13039           /* Remember that we've seen it.  */
13040           typename_p = true;
13041           /* Consume the `typename' token.  */
13042           cp_lexer_consume_token (parser->lexer);
13043         }
13044     }
13045
13046   /* Look for the optional global scope qualification.  */
13047   global_scope_p
13048     = (cp_parser_global_scope_opt (parser,
13049                                    /*current_scope_valid_p=*/false)
13050        != NULL_TREE);
13051
13052   /* If we saw `typename', or didn't see `::', then there must be a
13053      nested-name-specifier present.  */
13054   if (typename_p || !global_scope_p)
13055     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13056                                               /*check_dependency_p=*/true,
13057                                               /*type_p=*/false,
13058                                               /*is_declaration=*/true);
13059   /* Otherwise, we could be in either of the two productions.  In that
13060      case, treat the nested-name-specifier as optional.  */
13061   else
13062     qscope = cp_parser_nested_name_specifier_opt (parser,
13063                                                   /*typename_keyword_p=*/false,
13064                                                   /*check_dependency_p=*/true,
13065                                                   /*type_p=*/false,
13066                                                   /*is_declaration=*/true);
13067   if (!qscope)
13068     qscope = global_namespace;
13069
13070   if (access_declaration_p && cp_parser_error_occurred (parser))
13071     /* Something has already gone wrong; there's no need to parse
13072        further.  Since an error has occurred, the return value of
13073        cp_parser_parse_definitely will be false, as required.  */
13074     return cp_parser_parse_definitely (parser);
13075
13076   token = cp_lexer_peek_token (parser->lexer);
13077   /* Parse the unqualified-id.  */
13078   identifier = cp_parser_unqualified_id (parser,
13079                                          /*template_keyword_p=*/false,
13080                                          /*check_dependency_p=*/true,
13081                                          /*declarator_p=*/true,
13082                                          /*optional_p=*/false);
13083
13084   if (access_declaration_p)
13085     {
13086       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13087         cp_parser_simulate_error (parser);
13088       if (!cp_parser_parse_definitely (parser))
13089         return false;
13090     }
13091
13092   /* The function we call to handle a using-declaration is different
13093      depending on what scope we are in.  */
13094   if (qscope == error_mark_node || identifier == error_mark_node)
13095     ;
13096   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13097            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13098     /* [namespace.udecl]
13099
13100        A using declaration shall not name a template-id.  */
13101     error_at (token->location,
13102               "a template-id may not appear in a using-declaration");
13103   else
13104     {
13105       if (at_class_scope_p ())
13106         {
13107           /* Create the USING_DECL.  */
13108           decl = do_class_using_decl (parser->scope, identifier);
13109
13110           if (check_for_bare_parameter_packs (decl))
13111             return false;
13112           else
13113             /* Add it to the list of members in this class.  */
13114             finish_member_declaration (decl);
13115         }
13116       else
13117         {
13118           decl = cp_parser_lookup_name_simple (parser,
13119                                                identifier,
13120                                                token->location);
13121           if (decl == error_mark_node)
13122             cp_parser_name_lookup_error (parser, identifier,
13123                                          decl, NULL,
13124                                          token->location);
13125           else if (check_for_bare_parameter_packs (decl))
13126             return false;
13127           else if (!at_namespace_scope_p ())
13128             do_local_using_decl (decl, qscope, identifier);
13129           else
13130             do_toplevel_using_decl (decl, qscope, identifier);
13131         }
13132     }
13133
13134   /* Look for the final `;'.  */
13135   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13136   
13137   return true;
13138 }
13139
13140 /* Parse a using-directive.
13141
13142    using-directive:
13143      using namespace :: [opt] nested-name-specifier [opt]
13144        namespace-name ;  */
13145
13146 static void
13147 cp_parser_using_directive (cp_parser* parser)
13148 {
13149   tree namespace_decl;
13150   tree attribs;
13151
13152   /* Look for the `using' keyword.  */
13153   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13154   /* And the `namespace' keyword.  */
13155   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13156   /* Look for the optional `::' operator.  */
13157   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13158   /* And the optional nested-name-specifier.  */
13159   cp_parser_nested_name_specifier_opt (parser,
13160                                        /*typename_keyword_p=*/false,
13161                                        /*check_dependency_p=*/true,
13162                                        /*type_p=*/false,
13163                                        /*is_declaration=*/true);
13164   /* Get the namespace being used.  */
13165   namespace_decl = cp_parser_namespace_name (parser);
13166   /* And any specified attributes.  */
13167   attribs = cp_parser_attributes_opt (parser);
13168   /* Update the symbol table.  */
13169   parse_using_directive (namespace_decl, attribs);
13170   /* Look for the final `;'.  */
13171   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13172 }
13173
13174 /* Parse an asm-definition.
13175
13176    asm-definition:
13177      asm ( string-literal ) ;
13178
13179    GNU Extension:
13180
13181    asm-definition:
13182      asm volatile [opt] ( string-literal ) ;
13183      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13184      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13185                           : asm-operand-list [opt] ) ;
13186      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13187                           : asm-operand-list [opt]
13188                           : asm-clobber-list [opt] ) ;
13189      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13190                                : asm-clobber-list [opt]
13191                                : asm-goto-list ) ;  */
13192
13193 static void
13194 cp_parser_asm_definition (cp_parser* parser)
13195 {
13196   tree string;
13197   tree outputs = NULL_TREE;
13198   tree inputs = NULL_TREE;
13199   tree clobbers = NULL_TREE;
13200   tree labels = NULL_TREE;
13201   tree asm_stmt;
13202   bool volatile_p = false;
13203   bool extended_p = false;
13204   bool invalid_inputs_p = false;
13205   bool invalid_outputs_p = false;
13206   bool goto_p = false;
13207   const char *missing = NULL;
13208
13209   /* Look for the `asm' keyword.  */
13210   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13211   /* See if the next token is `volatile'.  */
13212   if (cp_parser_allow_gnu_extensions_p (parser)
13213       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13214     {
13215       /* Remember that we saw the `volatile' keyword.  */
13216       volatile_p = true;
13217       /* Consume the token.  */
13218       cp_lexer_consume_token (parser->lexer);
13219     }
13220   if (cp_parser_allow_gnu_extensions_p (parser)
13221       && parser->in_function_body
13222       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13223     {
13224       /* Remember that we saw the `goto' keyword.  */
13225       goto_p = true;
13226       /* Consume the token.  */
13227       cp_lexer_consume_token (parser->lexer);
13228     }
13229   /* Look for the opening `('.  */
13230   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13231     return;
13232   /* Look for the string.  */
13233   string = cp_parser_string_literal (parser, false, false);
13234   if (string == error_mark_node)
13235     {
13236       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13237                                              /*consume_paren=*/true);
13238       return;
13239     }
13240
13241   /* If we're allowing GNU extensions, check for the extended assembly
13242      syntax.  Unfortunately, the `:' tokens need not be separated by
13243      a space in C, and so, for compatibility, we tolerate that here
13244      too.  Doing that means that we have to treat the `::' operator as
13245      two `:' tokens.  */
13246   if (cp_parser_allow_gnu_extensions_p (parser)
13247       && parser->in_function_body
13248       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13249           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13250     {
13251       bool inputs_p = false;
13252       bool clobbers_p = false;
13253       bool labels_p = false;
13254
13255       /* The extended syntax was used.  */
13256       extended_p = true;
13257
13258       /* Look for outputs.  */
13259       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13260         {
13261           /* Consume the `:'.  */
13262           cp_lexer_consume_token (parser->lexer);
13263           /* Parse the output-operands.  */
13264           if (cp_lexer_next_token_is_not (parser->lexer,
13265                                           CPP_COLON)
13266               && cp_lexer_next_token_is_not (parser->lexer,
13267                                              CPP_SCOPE)
13268               && cp_lexer_next_token_is_not (parser->lexer,
13269                                              CPP_CLOSE_PAREN)
13270               && !goto_p)
13271             outputs = cp_parser_asm_operand_list (parser);
13272
13273             if (outputs == error_mark_node)
13274               invalid_outputs_p = true;
13275         }
13276       /* If the next token is `::', there are no outputs, and the
13277          next token is the beginning of the inputs.  */
13278       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13279         /* The inputs are coming next.  */
13280         inputs_p = true;
13281
13282       /* Look for inputs.  */
13283       if (inputs_p
13284           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13285         {
13286           /* Consume the `:' or `::'.  */
13287           cp_lexer_consume_token (parser->lexer);
13288           /* Parse the output-operands.  */
13289           if (cp_lexer_next_token_is_not (parser->lexer,
13290                                           CPP_COLON)
13291               && cp_lexer_next_token_is_not (parser->lexer,
13292                                              CPP_SCOPE)
13293               && cp_lexer_next_token_is_not (parser->lexer,
13294                                              CPP_CLOSE_PAREN))
13295             inputs = cp_parser_asm_operand_list (parser);
13296
13297             if (inputs == error_mark_node)
13298               invalid_inputs_p = true;
13299         }
13300       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13301         /* The clobbers are coming next.  */
13302         clobbers_p = true;
13303
13304       /* Look for clobbers.  */
13305       if (clobbers_p
13306           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13307         {
13308           clobbers_p = true;
13309           /* Consume the `:' or `::'.  */
13310           cp_lexer_consume_token (parser->lexer);
13311           /* Parse the clobbers.  */
13312           if (cp_lexer_next_token_is_not (parser->lexer,
13313                                           CPP_COLON)
13314               && cp_lexer_next_token_is_not (parser->lexer,
13315                                              CPP_CLOSE_PAREN))
13316             clobbers = cp_parser_asm_clobber_list (parser);
13317         }
13318       else if (goto_p
13319                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13320         /* The labels are coming next.  */
13321         labels_p = true;
13322
13323       /* Look for labels.  */
13324       if (labels_p
13325           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13326         {
13327           labels_p = true;
13328           /* Consume the `:' or `::'.  */
13329           cp_lexer_consume_token (parser->lexer);
13330           /* Parse the labels.  */
13331           labels = cp_parser_asm_label_list (parser);
13332         }
13333
13334       if (goto_p && !labels_p)
13335         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13336     }
13337   else if (goto_p)
13338     missing = "%<:%> or %<::%>";
13339
13340   /* Look for the closing `)'.  */
13341   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13342                           missing ? missing : "%<)%>"))
13343     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13344                                            /*consume_paren=*/true);
13345   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13346
13347   if (!invalid_inputs_p && !invalid_outputs_p)
13348     {
13349       /* Create the ASM_EXPR.  */
13350       if (parser->in_function_body)
13351         {
13352           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13353                                       inputs, clobbers, labels);
13354           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13355           if (!extended_p)
13356             {
13357               tree temp = asm_stmt;
13358               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13359                 temp = TREE_OPERAND (temp, 0);
13360
13361               ASM_INPUT_P (temp) = 1;
13362             }
13363         }
13364       else
13365         cgraph_add_asm_node (string);
13366     }
13367 }
13368
13369 /* Declarators [gram.dcl.decl] */
13370
13371 /* Parse an init-declarator.
13372
13373    init-declarator:
13374      declarator initializer [opt]
13375
13376    GNU Extension:
13377
13378    init-declarator:
13379      declarator asm-specification [opt] attributes [opt] initializer [opt]
13380
13381    function-definition:
13382      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13383        function-body
13384      decl-specifier-seq [opt] declarator function-try-block
13385
13386    GNU Extension:
13387
13388    function-definition:
13389      __extension__ function-definition
13390
13391    The DECL_SPECIFIERS apply to this declarator.  Returns a
13392    representation of the entity declared.  If MEMBER_P is TRUE, then
13393    this declarator appears in a class scope.  The new DECL created by
13394    this declarator is returned.
13395
13396    The CHECKS are access checks that should be performed once we know
13397    what entity is being declared (and, therefore, what classes have
13398    befriended it).
13399
13400    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13401    for a function-definition here as well.  If the declarator is a
13402    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13403    be TRUE upon return.  By that point, the function-definition will
13404    have been completely parsed.
13405
13406    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13407    is FALSE.  */
13408
13409 static tree
13410 cp_parser_init_declarator (cp_parser* parser,
13411                            cp_decl_specifier_seq *decl_specifiers,
13412                            VEC (deferred_access_check,gc)* checks,
13413                            bool function_definition_allowed_p,
13414                            bool member_p,
13415                            int declares_class_or_enum,
13416                            bool* function_definition_p)
13417 {
13418   cp_token *token = NULL, *asm_spec_start_token = NULL,
13419            *attributes_start_token = NULL;
13420   cp_declarator *declarator;
13421   tree prefix_attributes;
13422   tree attributes;
13423   tree asm_specification;
13424   tree initializer;
13425   tree decl = NULL_TREE;
13426   tree scope;
13427   int is_initialized;
13428   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13429      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13430      "(...)".  */
13431   enum cpp_ttype initialization_kind;
13432   bool is_direct_init = false;
13433   bool is_non_constant_init;
13434   int ctor_dtor_or_conv_p;
13435   bool friend_p;
13436   tree pushed_scope = NULL;
13437
13438   /* Gather the attributes that were provided with the
13439      decl-specifiers.  */
13440   prefix_attributes = decl_specifiers->attributes;
13441
13442   /* Assume that this is not the declarator for a function
13443      definition.  */
13444   if (function_definition_p)
13445     *function_definition_p = false;
13446
13447   /* Defer access checks while parsing the declarator; we cannot know
13448      what names are accessible until we know what is being
13449      declared.  */
13450   resume_deferring_access_checks ();
13451
13452   /* Parse the declarator.  */
13453   token = cp_lexer_peek_token (parser->lexer);
13454   declarator
13455     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13456                             &ctor_dtor_or_conv_p,
13457                             /*parenthesized_p=*/NULL,
13458                             /*member_p=*/false);
13459   /* Gather up the deferred checks.  */
13460   stop_deferring_access_checks ();
13461
13462   /* If the DECLARATOR was erroneous, there's no need to go
13463      further.  */
13464   if (declarator == cp_error_declarator)
13465     return error_mark_node;
13466
13467   /* Check that the number of template-parameter-lists is OK.  */
13468   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13469                                                        token->location))
13470     return error_mark_node;
13471
13472   if (declares_class_or_enum & 2)
13473     cp_parser_check_for_definition_in_return_type (declarator,
13474                                                    decl_specifiers->type,
13475                                                    decl_specifiers->type_location);
13476
13477   /* Figure out what scope the entity declared by the DECLARATOR is
13478      located in.  `grokdeclarator' sometimes changes the scope, so
13479      we compute it now.  */
13480   scope = get_scope_of_declarator (declarator);
13481
13482   /* Perform any lookups in the declared type which were thought to be
13483      dependent, but are not in the scope of the declarator.  */
13484   decl_specifiers->type
13485     = maybe_update_decl_type (decl_specifiers->type, scope);
13486
13487   /* If we're allowing GNU extensions, look for an asm-specification
13488      and attributes.  */
13489   if (cp_parser_allow_gnu_extensions_p (parser))
13490     {
13491       /* Look for an asm-specification.  */
13492       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13493       asm_specification = cp_parser_asm_specification_opt (parser);
13494       /* And attributes.  */
13495       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13496       attributes = cp_parser_attributes_opt (parser);
13497     }
13498   else
13499     {
13500       asm_specification = NULL_TREE;
13501       attributes = NULL_TREE;
13502     }
13503
13504   /* Peek at the next token.  */
13505   token = cp_lexer_peek_token (parser->lexer);
13506   /* Check to see if the token indicates the start of a
13507      function-definition.  */
13508   if (function_declarator_p (declarator)
13509       && cp_parser_token_starts_function_definition_p (token))
13510     {
13511       if (!function_definition_allowed_p)
13512         {
13513           /* If a function-definition should not appear here, issue an
13514              error message.  */
13515           cp_parser_error (parser,
13516                            "a function-definition is not allowed here");
13517           return error_mark_node;
13518         }
13519       else
13520         {
13521           location_t func_brace_location
13522             = cp_lexer_peek_token (parser->lexer)->location;
13523
13524           /* Neither attributes nor an asm-specification are allowed
13525              on a function-definition.  */
13526           if (asm_specification)
13527             error_at (asm_spec_start_token->location,
13528                       "an asm-specification is not allowed "
13529                       "on a function-definition");
13530           if (attributes)
13531             error_at (attributes_start_token->location,
13532                       "attributes are not allowed on a function-definition");
13533           /* This is a function-definition.  */
13534           *function_definition_p = true;
13535
13536           /* Parse the function definition.  */
13537           if (member_p)
13538             decl = cp_parser_save_member_function_body (parser,
13539                                                         decl_specifiers,
13540                                                         declarator,
13541                                                         prefix_attributes);
13542           else
13543             decl
13544               = (cp_parser_function_definition_from_specifiers_and_declarator
13545                  (parser, decl_specifiers, prefix_attributes, declarator));
13546
13547           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13548             {
13549               /* This is where the prologue starts...  */
13550               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13551                 = func_brace_location;
13552             }
13553
13554           return decl;
13555         }
13556     }
13557
13558   /* [dcl.dcl]
13559
13560      Only in function declarations for constructors, destructors, and
13561      type conversions can the decl-specifier-seq be omitted.
13562
13563      We explicitly postpone this check past the point where we handle
13564      function-definitions because we tolerate function-definitions
13565      that are missing their return types in some modes.  */
13566   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13567     {
13568       cp_parser_error (parser,
13569                        "expected constructor, destructor, or type conversion");
13570       return error_mark_node;
13571     }
13572
13573   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13574   if (token->type == CPP_EQ
13575       || token->type == CPP_OPEN_PAREN
13576       || token->type == CPP_OPEN_BRACE)
13577     {
13578       is_initialized = SD_INITIALIZED;
13579       initialization_kind = token->type;
13580
13581       if (token->type == CPP_EQ
13582           && function_declarator_p (declarator))
13583         {
13584           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13585           if (t2->keyword == RID_DEFAULT)
13586             is_initialized = SD_DEFAULTED;
13587           else if (t2->keyword == RID_DELETE)
13588             is_initialized = SD_DELETED;
13589         }
13590     }
13591   else
13592     {
13593       /* If the init-declarator isn't initialized and isn't followed by a
13594          `,' or `;', it's not a valid init-declarator.  */
13595       if (token->type != CPP_COMMA
13596           && token->type != CPP_SEMICOLON)
13597         {
13598           cp_parser_error (parser, "expected initializer");
13599           return error_mark_node;
13600         }
13601       is_initialized = SD_UNINITIALIZED;
13602       initialization_kind = CPP_EOF;
13603     }
13604
13605   /* Because start_decl has side-effects, we should only call it if we
13606      know we're going ahead.  By this point, we know that we cannot
13607      possibly be looking at any other construct.  */
13608   cp_parser_commit_to_tentative_parse (parser);
13609
13610   /* If the decl specifiers were bad, issue an error now that we're
13611      sure this was intended to be a declarator.  Then continue
13612      declaring the variable(s), as int, to try to cut down on further
13613      errors.  */
13614   if (decl_specifiers->any_specifiers_p
13615       && decl_specifiers->type == error_mark_node)
13616     {
13617       cp_parser_error (parser, "invalid type in declaration");
13618       decl_specifiers->type = integer_type_node;
13619     }
13620
13621   /* Check to see whether or not this declaration is a friend.  */
13622   friend_p = cp_parser_friend_p (decl_specifiers);
13623
13624   /* Enter the newly declared entry in the symbol table.  If we're
13625      processing a declaration in a class-specifier, we wait until
13626      after processing the initializer.  */
13627   if (!member_p)
13628     {
13629       if (parser->in_unbraced_linkage_specification_p)
13630         decl_specifiers->storage_class = sc_extern;
13631       decl = start_decl (declarator, decl_specifiers,
13632                          is_initialized, attributes, prefix_attributes,
13633                          &pushed_scope);
13634     }
13635   else if (scope)
13636     /* Enter the SCOPE.  That way unqualified names appearing in the
13637        initializer will be looked up in SCOPE.  */
13638     pushed_scope = push_scope (scope);
13639
13640   /* Perform deferred access control checks, now that we know in which
13641      SCOPE the declared entity resides.  */
13642   if (!member_p && decl)
13643     {
13644       tree saved_current_function_decl = NULL_TREE;
13645
13646       /* If the entity being declared is a function, pretend that we
13647          are in its scope.  If it is a `friend', it may have access to
13648          things that would not otherwise be accessible.  */
13649       if (TREE_CODE (decl) == FUNCTION_DECL)
13650         {
13651           saved_current_function_decl = current_function_decl;
13652           current_function_decl = decl;
13653         }
13654
13655       /* Perform access checks for template parameters.  */
13656       cp_parser_perform_template_parameter_access_checks (checks);
13657
13658       /* Perform the access control checks for the declarator and the
13659          decl-specifiers.  */
13660       perform_deferred_access_checks ();
13661
13662       /* Restore the saved value.  */
13663       if (TREE_CODE (decl) == FUNCTION_DECL)
13664         current_function_decl = saved_current_function_decl;
13665     }
13666
13667   /* Parse the initializer.  */
13668   initializer = NULL_TREE;
13669   is_direct_init = false;
13670   is_non_constant_init = true;
13671   if (is_initialized)
13672     {
13673       if (function_declarator_p (declarator))
13674         {
13675           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13676            if (initialization_kind == CPP_EQ)
13677              initializer = cp_parser_pure_specifier (parser);
13678            else
13679              {
13680                /* If the declaration was erroneous, we don't really
13681                   know what the user intended, so just silently
13682                   consume the initializer.  */
13683                if (decl != error_mark_node)
13684                  error_at (initializer_start_token->location,
13685                            "initializer provided for function");
13686                cp_parser_skip_to_closing_parenthesis (parser,
13687                                                       /*recovering=*/true,
13688                                                       /*or_comma=*/false,
13689                                                       /*consume_paren=*/true);
13690              }
13691         }
13692       else
13693         {
13694           /* We want to record the extra mangling scope for in-class
13695              initializers of class members and initializers of static data
13696              member templates.  The former is a C++0x feature which isn't
13697              implemented yet, and I expect it will involve deferring
13698              parsing of the initializer until end of class as with default
13699              arguments.  So right here we only handle the latter.  */
13700           if (!member_p && processing_template_decl)
13701             start_lambda_scope (decl);
13702           initializer = cp_parser_initializer (parser,
13703                                                &is_direct_init,
13704                                                &is_non_constant_init);
13705           if (!member_p && processing_template_decl)
13706             finish_lambda_scope ();
13707         }
13708     }
13709
13710   /* The old parser allows attributes to appear after a parenthesized
13711      initializer.  Mark Mitchell proposed removing this functionality
13712      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13713      attributes -- but ignores them.  */
13714   if (cp_parser_allow_gnu_extensions_p (parser)
13715       && initialization_kind == CPP_OPEN_PAREN)
13716     if (cp_parser_attributes_opt (parser))
13717       warning (OPT_Wattributes,
13718                "attributes after parenthesized initializer ignored");
13719
13720   /* For an in-class declaration, use `grokfield' to create the
13721      declaration.  */
13722   if (member_p)
13723     {
13724       if (pushed_scope)
13725         {
13726           pop_scope (pushed_scope);
13727           pushed_scope = false;
13728         }
13729       decl = grokfield (declarator, decl_specifiers,
13730                         initializer, !is_non_constant_init,
13731                         /*asmspec=*/NULL_TREE,
13732                         prefix_attributes);
13733       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13734         cp_parser_save_default_args (parser, decl);
13735     }
13736
13737   /* Finish processing the declaration.  But, skip friend
13738      declarations.  */
13739   if (!friend_p && decl && decl != error_mark_node)
13740     {
13741       cp_finish_decl (decl,
13742                       initializer, !is_non_constant_init,
13743                       asm_specification,
13744                       /* If the initializer is in parentheses, then this is
13745                          a direct-initialization, which means that an
13746                          `explicit' constructor is OK.  Otherwise, an
13747                          `explicit' constructor cannot be used.  */
13748                       ((is_direct_init || !is_initialized)
13749                        ? 0 : LOOKUP_ONLYCONVERTING));
13750     }
13751   else if ((cxx_dialect != cxx98) && friend_p
13752            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13753     /* Core issue #226 (C++0x only): A default template-argument
13754        shall not be specified in a friend class template
13755        declaration. */
13756     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13757                              /*is_partial=*/0, /*is_friend_decl=*/1);
13758
13759   if (!friend_p && pushed_scope)
13760     pop_scope (pushed_scope);
13761
13762   return decl;
13763 }
13764
13765 /* Parse a declarator.
13766
13767    declarator:
13768      direct-declarator
13769      ptr-operator declarator
13770
13771    abstract-declarator:
13772      ptr-operator abstract-declarator [opt]
13773      direct-abstract-declarator
13774
13775    GNU Extensions:
13776
13777    declarator:
13778      attributes [opt] direct-declarator
13779      attributes [opt] ptr-operator declarator
13780
13781    abstract-declarator:
13782      attributes [opt] ptr-operator abstract-declarator [opt]
13783      attributes [opt] direct-abstract-declarator
13784
13785    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13786    detect constructor, destructor or conversion operators. It is set
13787    to -1 if the declarator is a name, and +1 if it is a
13788    function. Otherwise it is set to zero. Usually you just want to
13789    test for >0, but internally the negative value is used.
13790
13791    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13792    a decl-specifier-seq unless it declares a constructor, destructor,
13793    or conversion.  It might seem that we could check this condition in
13794    semantic analysis, rather than parsing, but that makes it difficult
13795    to handle something like `f()'.  We want to notice that there are
13796    no decl-specifiers, and therefore realize that this is an
13797    expression, not a declaration.)
13798
13799    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13800    the declarator is a direct-declarator of the form "(...)".
13801
13802    MEMBER_P is true iff this declarator is a member-declarator.  */
13803
13804 static cp_declarator *
13805 cp_parser_declarator (cp_parser* parser,
13806                       cp_parser_declarator_kind dcl_kind,
13807                       int* ctor_dtor_or_conv_p,
13808                       bool* parenthesized_p,
13809                       bool member_p)
13810 {
13811   cp_declarator *declarator;
13812   enum tree_code code;
13813   cp_cv_quals cv_quals;
13814   tree class_type;
13815   tree attributes = NULL_TREE;
13816
13817   /* Assume this is not a constructor, destructor, or type-conversion
13818      operator.  */
13819   if (ctor_dtor_or_conv_p)
13820     *ctor_dtor_or_conv_p = 0;
13821
13822   if (cp_parser_allow_gnu_extensions_p (parser))
13823     attributes = cp_parser_attributes_opt (parser);
13824
13825   /* Check for the ptr-operator production.  */
13826   cp_parser_parse_tentatively (parser);
13827   /* Parse the ptr-operator.  */
13828   code = cp_parser_ptr_operator (parser,
13829                                  &class_type,
13830                                  &cv_quals);
13831   /* If that worked, then we have a ptr-operator.  */
13832   if (cp_parser_parse_definitely (parser))
13833     {
13834       /* If a ptr-operator was found, then this declarator was not
13835          parenthesized.  */
13836       if (parenthesized_p)
13837         *parenthesized_p = true;
13838       /* The dependent declarator is optional if we are parsing an
13839          abstract-declarator.  */
13840       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13841         cp_parser_parse_tentatively (parser);
13842
13843       /* Parse the dependent declarator.  */
13844       declarator = cp_parser_declarator (parser, dcl_kind,
13845                                          /*ctor_dtor_or_conv_p=*/NULL,
13846                                          /*parenthesized_p=*/NULL,
13847                                          /*member_p=*/false);
13848
13849       /* If we are parsing an abstract-declarator, we must handle the
13850          case where the dependent declarator is absent.  */
13851       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13852           && !cp_parser_parse_definitely (parser))
13853         declarator = NULL;
13854
13855       declarator = cp_parser_make_indirect_declarator
13856         (code, class_type, cv_quals, declarator);
13857     }
13858   /* Everything else is a direct-declarator.  */
13859   else
13860     {
13861       if (parenthesized_p)
13862         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13863                                                    CPP_OPEN_PAREN);
13864       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13865                                                 ctor_dtor_or_conv_p,
13866                                                 member_p);
13867     }
13868
13869   if (attributes && declarator && declarator != cp_error_declarator)
13870     declarator->attributes = attributes;
13871
13872   return declarator;
13873 }
13874
13875 /* Parse a direct-declarator or direct-abstract-declarator.
13876
13877    direct-declarator:
13878      declarator-id
13879      direct-declarator ( parameter-declaration-clause )
13880        cv-qualifier-seq [opt]
13881        exception-specification [opt]
13882      direct-declarator [ constant-expression [opt] ]
13883      ( declarator )
13884
13885    direct-abstract-declarator:
13886      direct-abstract-declarator [opt]
13887        ( parameter-declaration-clause )
13888        cv-qualifier-seq [opt]
13889        exception-specification [opt]
13890      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13891      ( abstract-declarator )
13892
13893    Returns a representation of the declarator.  DCL_KIND is
13894    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13895    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13896    we are parsing a direct-declarator.  It is
13897    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13898    of ambiguity we prefer an abstract declarator, as per
13899    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13900    cp_parser_declarator.  */
13901
13902 static cp_declarator *
13903 cp_parser_direct_declarator (cp_parser* parser,
13904                              cp_parser_declarator_kind dcl_kind,
13905                              int* ctor_dtor_or_conv_p,
13906                              bool member_p)
13907 {
13908   cp_token *token;
13909   cp_declarator *declarator = NULL;
13910   tree scope = NULL_TREE;
13911   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13912   bool saved_in_declarator_p = parser->in_declarator_p;
13913   bool first = true;
13914   tree pushed_scope = NULL_TREE;
13915
13916   while (true)
13917     {
13918       /* Peek at the next token.  */
13919       token = cp_lexer_peek_token (parser->lexer);
13920       if (token->type == CPP_OPEN_PAREN)
13921         {
13922           /* This is either a parameter-declaration-clause, or a
13923              parenthesized declarator. When we know we are parsing a
13924              named declarator, it must be a parenthesized declarator
13925              if FIRST is true. For instance, `(int)' is a
13926              parameter-declaration-clause, with an omitted
13927              direct-abstract-declarator. But `((*))', is a
13928              parenthesized abstract declarator. Finally, when T is a
13929              template parameter `(T)' is a
13930              parameter-declaration-clause, and not a parenthesized
13931              named declarator.
13932
13933              We first try and parse a parameter-declaration-clause,
13934              and then try a nested declarator (if FIRST is true).
13935
13936              It is not an error for it not to be a
13937              parameter-declaration-clause, even when FIRST is
13938              false. Consider,
13939
13940                int i (int);
13941                int i (3);
13942
13943              The first is the declaration of a function while the
13944              second is the definition of a variable, including its
13945              initializer.
13946
13947              Having seen only the parenthesis, we cannot know which of
13948              these two alternatives should be selected.  Even more
13949              complex are examples like:
13950
13951                int i (int (a));
13952                int i (int (3));
13953
13954              The former is a function-declaration; the latter is a
13955              variable initialization.
13956
13957              Thus again, we try a parameter-declaration-clause, and if
13958              that fails, we back out and return.  */
13959
13960           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13961             {
13962               tree params;
13963               unsigned saved_num_template_parameter_lists;
13964               bool is_declarator = false;
13965               tree t;
13966
13967               /* In a member-declarator, the only valid interpretation
13968                  of a parenthesis is the start of a
13969                  parameter-declaration-clause.  (It is invalid to
13970                  initialize a static data member with a parenthesized
13971                  initializer; only the "=" form of initialization is
13972                  permitted.)  */
13973               if (!member_p)
13974                 cp_parser_parse_tentatively (parser);
13975
13976               /* Consume the `('.  */
13977               cp_lexer_consume_token (parser->lexer);
13978               if (first)
13979                 {
13980                   /* If this is going to be an abstract declarator, we're
13981                      in a declarator and we can't have default args.  */
13982                   parser->default_arg_ok_p = false;
13983                   parser->in_declarator_p = true;
13984                 }
13985
13986               /* Inside the function parameter list, surrounding
13987                  template-parameter-lists do not apply.  */
13988               saved_num_template_parameter_lists
13989                 = parser->num_template_parameter_lists;
13990               parser->num_template_parameter_lists = 0;
13991
13992               begin_scope (sk_function_parms, NULL_TREE);
13993
13994               /* Parse the parameter-declaration-clause.  */
13995               params = cp_parser_parameter_declaration_clause (parser);
13996
13997               parser->num_template_parameter_lists
13998                 = saved_num_template_parameter_lists;
13999
14000               /* If all went well, parse the cv-qualifier-seq and the
14001                  exception-specification.  */
14002               if (member_p || cp_parser_parse_definitely (parser))
14003                 {
14004                   cp_cv_quals cv_quals;
14005                   tree exception_specification;
14006                   tree late_return;
14007
14008                   is_declarator = true;
14009
14010                   if (ctor_dtor_or_conv_p)
14011                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14012                   first = false;
14013                   /* Consume the `)'.  */
14014                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
14015
14016                   /* Parse the cv-qualifier-seq.  */
14017                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14018                   /* And the exception-specification.  */
14019                   exception_specification
14020                     = cp_parser_exception_specification_opt (parser);
14021
14022                   late_return
14023                     = cp_parser_late_return_type_opt (parser);
14024
14025                   /* Create the function-declarator.  */
14026                   declarator = make_call_declarator (declarator,
14027                                                      params,
14028                                                      cv_quals,
14029                                                      exception_specification,
14030                                                      late_return);
14031                   /* Any subsequent parameter lists are to do with
14032                      return type, so are not those of the declared
14033                      function.  */
14034                   parser->default_arg_ok_p = false;
14035                 }
14036
14037               /* Remove the function parms from scope.  */
14038               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
14039                 pop_binding (DECL_NAME (t), t);
14040               leave_scope();
14041
14042               if (is_declarator)
14043                 /* Repeat the main loop.  */
14044                 continue;
14045             }
14046
14047           /* If this is the first, we can try a parenthesized
14048              declarator.  */
14049           if (first)
14050             {
14051               bool saved_in_type_id_in_expr_p;
14052
14053               parser->default_arg_ok_p = saved_default_arg_ok_p;
14054               parser->in_declarator_p = saved_in_declarator_p;
14055
14056               /* Consume the `('.  */
14057               cp_lexer_consume_token (parser->lexer);
14058               /* Parse the nested declarator.  */
14059               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14060               parser->in_type_id_in_expr_p = true;
14061               declarator
14062                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14063                                         /*parenthesized_p=*/NULL,
14064                                         member_p);
14065               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14066               first = false;
14067               /* Expect a `)'.  */
14068               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
14069                 declarator = cp_error_declarator;
14070               if (declarator == cp_error_declarator)
14071                 break;
14072
14073               goto handle_declarator;
14074             }
14075           /* Otherwise, we must be done.  */
14076           else
14077             break;
14078         }
14079       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14080                && token->type == CPP_OPEN_SQUARE)
14081         {
14082           /* Parse an array-declarator.  */
14083           tree bounds;
14084
14085           if (ctor_dtor_or_conv_p)
14086             *ctor_dtor_or_conv_p = 0;
14087
14088           first = false;
14089           parser->default_arg_ok_p = false;
14090           parser->in_declarator_p = true;
14091           /* Consume the `['.  */
14092           cp_lexer_consume_token (parser->lexer);
14093           /* Peek at the next token.  */
14094           token = cp_lexer_peek_token (parser->lexer);
14095           /* If the next token is `]', then there is no
14096              constant-expression.  */
14097           if (token->type != CPP_CLOSE_SQUARE)
14098             {
14099               bool non_constant_p;
14100
14101               bounds
14102                 = cp_parser_constant_expression (parser,
14103                                                  /*allow_non_constant=*/true,
14104                                                  &non_constant_p);
14105               if (!non_constant_p)
14106                 bounds = fold_non_dependent_expr (bounds);
14107               /* Normally, the array bound must be an integral constant
14108                  expression.  However, as an extension, we allow VLAs
14109                  in function scopes.  */
14110               else if (!parser->in_function_body)
14111                 {
14112                   error_at (token->location,
14113                             "array bound is not an integer constant");
14114                   bounds = error_mark_node;
14115                 }
14116               else if (processing_template_decl && !error_operand_p (bounds))
14117                 {
14118                   /* Remember this wasn't a constant-expression.  */
14119                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14120                   TREE_SIDE_EFFECTS (bounds) = 1;
14121                 }
14122             }
14123           else
14124             bounds = NULL_TREE;
14125           /* Look for the closing `]'.  */
14126           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14127             {
14128               declarator = cp_error_declarator;
14129               break;
14130             }
14131
14132           declarator = make_array_declarator (declarator, bounds);
14133         }
14134       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14135         {
14136           {
14137             tree qualifying_scope;
14138             tree unqualified_name;
14139             special_function_kind sfk;
14140             bool abstract_ok;
14141             bool pack_expansion_p = false;
14142             cp_token *declarator_id_start_token;
14143
14144             /* Parse a declarator-id */
14145             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14146             if (abstract_ok)
14147               {
14148                 cp_parser_parse_tentatively (parser);
14149
14150                 /* If we see an ellipsis, we should be looking at a
14151                    parameter pack. */
14152                 if (token->type == CPP_ELLIPSIS)
14153                   {
14154                     /* Consume the `...' */
14155                     cp_lexer_consume_token (parser->lexer);
14156
14157                     pack_expansion_p = true;
14158                   }
14159               }
14160
14161             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14162             unqualified_name
14163               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14164             qualifying_scope = parser->scope;
14165             if (abstract_ok)
14166               {
14167                 bool okay = false;
14168
14169                 if (!unqualified_name && pack_expansion_p)
14170                   {
14171                     /* Check whether an error occurred. */
14172                     okay = !cp_parser_error_occurred (parser);
14173
14174                     /* We already consumed the ellipsis to mark a
14175                        parameter pack, but we have no way to report it,
14176                        so abort the tentative parse. We will be exiting
14177                        immediately anyway. */
14178                     cp_parser_abort_tentative_parse (parser);
14179                   }
14180                 else
14181                   okay = cp_parser_parse_definitely (parser);
14182
14183                 if (!okay)
14184                   unqualified_name = error_mark_node;
14185                 else if (unqualified_name
14186                          && (qualifying_scope
14187                              || (TREE_CODE (unqualified_name)
14188                                  != IDENTIFIER_NODE)))
14189                   {
14190                     cp_parser_error (parser, "expected unqualified-id");
14191                     unqualified_name = error_mark_node;
14192                   }
14193               }
14194
14195             if (!unqualified_name)
14196               return NULL;
14197             if (unqualified_name == error_mark_node)
14198               {
14199                 declarator = cp_error_declarator;
14200                 pack_expansion_p = false;
14201                 declarator->parameter_pack_p = false;
14202                 break;
14203               }
14204
14205             if (qualifying_scope && at_namespace_scope_p ()
14206                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14207               {
14208                 /* In the declaration of a member of a template class
14209                    outside of the class itself, the SCOPE will sometimes
14210                    be a TYPENAME_TYPE.  For example, given:
14211
14212                    template <typename T>
14213                    int S<T>::R::i = 3;
14214
14215                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14216                    this context, we must resolve S<T>::R to an ordinary
14217                    type, rather than a typename type.
14218
14219                    The reason we normally avoid resolving TYPENAME_TYPEs
14220                    is that a specialization of `S' might render
14221                    `S<T>::R' not a type.  However, if `S' is
14222                    specialized, then this `i' will not be used, so there
14223                    is no harm in resolving the types here.  */
14224                 tree type;
14225
14226                 /* Resolve the TYPENAME_TYPE.  */
14227                 type = resolve_typename_type (qualifying_scope,
14228                                               /*only_current_p=*/false);
14229                 /* If that failed, the declarator is invalid.  */
14230                 if (TREE_CODE (type) == TYPENAME_TYPE)
14231                   {
14232                     if (typedef_variant_p (type))
14233                       error_at (declarator_id_start_token->location,
14234                                 "cannot define member of dependent typedef "
14235                                 "%qT", type);
14236                     else
14237                       error_at (declarator_id_start_token->location,
14238                                 "%<%T::%E%> is not a type",
14239                                 TYPE_CONTEXT (qualifying_scope),
14240                                 TYPE_IDENTIFIER (qualifying_scope));
14241                   }
14242                 qualifying_scope = type;
14243               }
14244
14245             sfk = sfk_none;
14246
14247             if (unqualified_name)
14248               {
14249                 tree class_type;
14250
14251                 if (qualifying_scope
14252                     && CLASS_TYPE_P (qualifying_scope))
14253                   class_type = qualifying_scope;
14254                 else
14255                   class_type = current_class_type;
14256
14257                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14258                   {
14259                     tree name_type = TREE_TYPE (unqualified_name);
14260                     if (class_type && same_type_p (name_type, class_type))
14261                       {
14262                         if (qualifying_scope
14263                             && CLASSTYPE_USE_TEMPLATE (name_type))
14264                           {
14265                             error_at (declarator_id_start_token->location,
14266                                       "invalid use of constructor as a template");
14267                             inform (declarator_id_start_token->location,
14268                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14269                                     "name the constructor in a qualified name",
14270                                     class_type,
14271                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14272                                     class_type, name_type);
14273                             declarator = cp_error_declarator;
14274                             break;
14275                           }
14276                         else
14277                           unqualified_name = constructor_name (class_type);
14278                       }
14279                     else
14280                       {
14281                         /* We do not attempt to print the declarator
14282                            here because we do not have enough
14283                            information about its original syntactic
14284                            form.  */
14285                         cp_parser_error (parser, "invalid declarator");
14286                         declarator = cp_error_declarator;
14287                         break;
14288                       }
14289                   }
14290
14291                 if (class_type)
14292                   {
14293                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14294                       sfk = sfk_destructor;
14295                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14296                       sfk = sfk_conversion;
14297                     else if (/* There's no way to declare a constructor
14298                                 for an anonymous type, even if the type
14299                                 got a name for linkage purposes.  */
14300                              !TYPE_WAS_ANONYMOUS (class_type)
14301                              && constructor_name_p (unqualified_name,
14302                                                     class_type))
14303                       {
14304                         unqualified_name = constructor_name (class_type);
14305                         sfk = sfk_constructor;
14306                       }
14307                     else if (is_overloaded_fn (unqualified_name)
14308                              && DECL_CONSTRUCTOR_P (get_first_fn
14309                                                     (unqualified_name)))
14310                       sfk = sfk_constructor;
14311
14312                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14313                       *ctor_dtor_or_conv_p = -1;
14314                   }
14315               }
14316             declarator = make_id_declarator (qualifying_scope,
14317                                              unqualified_name,
14318                                              sfk);
14319             declarator->id_loc = token->location;
14320             declarator->parameter_pack_p = pack_expansion_p;
14321
14322             if (pack_expansion_p)
14323               maybe_warn_variadic_templates ();
14324           }
14325
14326         handle_declarator:;
14327           scope = get_scope_of_declarator (declarator);
14328           if (scope)
14329             /* Any names that appear after the declarator-id for a
14330                member are looked up in the containing scope.  */
14331             pushed_scope = push_scope (scope);
14332           parser->in_declarator_p = true;
14333           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14334               || (declarator && declarator->kind == cdk_id))
14335             /* Default args are only allowed on function
14336                declarations.  */
14337             parser->default_arg_ok_p = saved_default_arg_ok_p;
14338           else
14339             parser->default_arg_ok_p = false;
14340
14341           first = false;
14342         }
14343       /* We're done.  */
14344       else
14345         break;
14346     }
14347
14348   /* For an abstract declarator, we might wind up with nothing at this
14349      point.  That's an error; the declarator is not optional.  */
14350   if (!declarator)
14351     cp_parser_error (parser, "expected declarator");
14352
14353   /* If we entered a scope, we must exit it now.  */
14354   if (pushed_scope)
14355     pop_scope (pushed_scope);
14356
14357   parser->default_arg_ok_p = saved_default_arg_ok_p;
14358   parser->in_declarator_p = saved_in_declarator_p;
14359
14360   return declarator;
14361 }
14362
14363 /* Parse a ptr-operator.
14364
14365    ptr-operator:
14366      * cv-qualifier-seq [opt]
14367      &
14368      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14369
14370    GNU Extension:
14371
14372    ptr-operator:
14373      & cv-qualifier-seq [opt]
14374
14375    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14376    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14377    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14378    filled in with the TYPE containing the member.  *CV_QUALS is
14379    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14380    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14381    Note that the tree codes returned by this function have nothing
14382    to do with the types of trees that will be eventually be created
14383    to represent the pointer or reference type being parsed. They are
14384    just constants with suggestive names. */
14385 static enum tree_code
14386 cp_parser_ptr_operator (cp_parser* parser,
14387                         tree* type,
14388                         cp_cv_quals *cv_quals)
14389 {
14390   enum tree_code code = ERROR_MARK;
14391   cp_token *token;
14392
14393   /* Assume that it's not a pointer-to-member.  */
14394   *type = NULL_TREE;
14395   /* And that there are no cv-qualifiers.  */
14396   *cv_quals = TYPE_UNQUALIFIED;
14397
14398   /* Peek at the next token.  */
14399   token = cp_lexer_peek_token (parser->lexer);
14400
14401   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14402   if (token->type == CPP_MULT)
14403     code = INDIRECT_REF;
14404   else if (token->type == CPP_AND)
14405     code = ADDR_EXPR;
14406   else if ((cxx_dialect != cxx98) &&
14407            token->type == CPP_AND_AND) /* C++0x only */
14408     code = NON_LVALUE_EXPR;
14409
14410   if (code != ERROR_MARK)
14411     {
14412       /* Consume the `*', `&' or `&&'.  */
14413       cp_lexer_consume_token (parser->lexer);
14414
14415       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14416          `&', if we are allowing GNU extensions.  (The only qualifier
14417          that can legally appear after `&' is `restrict', but that is
14418          enforced during semantic analysis.  */
14419       if (code == INDIRECT_REF
14420           || cp_parser_allow_gnu_extensions_p (parser))
14421         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14422     }
14423   else
14424     {
14425       /* Try the pointer-to-member case.  */
14426       cp_parser_parse_tentatively (parser);
14427       /* Look for the optional `::' operator.  */
14428       cp_parser_global_scope_opt (parser,
14429                                   /*current_scope_valid_p=*/false);
14430       /* Look for the nested-name specifier.  */
14431       token = cp_lexer_peek_token (parser->lexer);
14432       cp_parser_nested_name_specifier (parser,
14433                                        /*typename_keyword_p=*/false,
14434                                        /*check_dependency_p=*/true,
14435                                        /*type_p=*/false,
14436                                        /*is_declaration=*/false);
14437       /* If we found it, and the next token is a `*', then we are
14438          indeed looking at a pointer-to-member operator.  */
14439       if (!cp_parser_error_occurred (parser)
14440           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14441         {
14442           /* Indicate that the `*' operator was used.  */
14443           code = INDIRECT_REF;
14444
14445           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14446             error_at (token->location, "%qD is a namespace", parser->scope);
14447           else
14448             {
14449               /* The type of which the member is a member is given by the
14450                  current SCOPE.  */
14451               *type = parser->scope;
14452               /* The next name will not be qualified.  */
14453               parser->scope = NULL_TREE;
14454               parser->qualifying_scope = NULL_TREE;
14455               parser->object_scope = NULL_TREE;
14456               /* Look for the optional cv-qualifier-seq.  */
14457               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14458             }
14459         }
14460       /* If that didn't work we don't have a ptr-operator.  */
14461       if (!cp_parser_parse_definitely (parser))
14462         cp_parser_error (parser, "expected ptr-operator");
14463     }
14464
14465   return code;
14466 }
14467
14468 /* Parse an (optional) cv-qualifier-seq.
14469
14470    cv-qualifier-seq:
14471      cv-qualifier cv-qualifier-seq [opt]
14472
14473    cv-qualifier:
14474      const
14475      volatile
14476
14477    GNU Extension:
14478
14479    cv-qualifier:
14480      __restrict__
14481
14482    Returns a bitmask representing the cv-qualifiers.  */
14483
14484 static cp_cv_quals
14485 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14486 {
14487   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14488
14489   while (true)
14490     {
14491       cp_token *token;
14492       cp_cv_quals cv_qualifier;
14493
14494       /* Peek at the next token.  */
14495       token = cp_lexer_peek_token (parser->lexer);
14496       /* See if it's a cv-qualifier.  */
14497       switch (token->keyword)
14498         {
14499         case RID_CONST:
14500           cv_qualifier = TYPE_QUAL_CONST;
14501           break;
14502
14503         case RID_VOLATILE:
14504           cv_qualifier = TYPE_QUAL_VOLATILE;
14505           break;
14506
14507         case RID_RESTRICT:
14508           cv_qualifier = TYPE_QUAL_RESTRICT;
14509           break;
14510
14511         default:
14512           cv_qualifier = TYPE_UNQUALIFIED;
14513           break;
14514         }
14515
14516       if (!cv_qualifier)
14517         break;
14518
14519       if (cv_quals & cv_qualifier)
14520         {
14521           error_at (token->location, "duplicate cv-qualifier");
14522           cp_lexer_purge_token (parser->lexer);
14523         }
14524       else
14525         {
14526           cp_lexer_consume_token (parser->lexer);
14527           cv_quals |= cv_qualifier;
14528         }
14529     }
14530
14531   return cv_quals;
14532 }
14533
14534 /* Parse a late-specified return type, if any.  This is not a separate
14535    non-terminal, but part of a function declarator, which looks like
14536
14537    -> trailing-type-specifier-seq abstract-declarator(opt)
14538
14539    Returns the type indicated by the type-id.  */
14540
14541 static tree
14542 cp_parser_late_return_type_opt (cp_parser* parser)
14543 {
14544   cp_token *token;
14545
14546   /* Peek at the next token.  */
14547   token = cp_lexer_peek_token (parser->lexer);
14548   /* A late-specified return type is indicated by an initial '->'. */
14549   if (token->type != CPP_DEREF)
14550     return NULL_TREE;
14551
14552   /* Consume the ->.  */
14553   cp_lexer_consume_token (parser->lexer);
14554
14555   return cp_parser_trailing_type_id (parser);
14556 }
14557
14558 /* Parse a declarator-id.
14559
14560    declarator-id:
14561      id-expression
14562      :: [opt] nested-name-specifier [opt] type-name
14563
14564    In the `id-expression' case, the value returned is as for
14565    cp_parser_id_expression if the id-expression was an unqualified-id.
14566    If the id-expression was a qualified-id, then a SCOPE_REF is
14567    returned.  The first operand is the scope (either a NAMESPACE_DECL
14568    or TREE_TYPE), but the second is still just a representation of an
14569    unqualified-id.  */
14570
14571 static tree
14572 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14573 {
14574   tree id;
14575   /* The expression must be an id-expression.  Assume that qualified
14576      names are the names of types so that:
14577
14578        template <class T>
14579        int S<T>::R::i = 3;
14580
14581      will work; we must treat `S<T>::R' as the name of a type.
14582      Similarly, assume that qualified names are templates, where
14583      required, so that:
14584
14585        template <class T>
14586        int S<T>::R<T>::i = 3;
14587
14588      will work, too.  */
14589   id = cp_parser_id_expression (parser,
14590                                 /*template_keyword_p=*/false,
14591                                 /*check_dependency_p=*/false,
14592                                 /*template_p=*/NULL,
14593                                 /*declarator_p=*/true,
14594                                 optional_p);
14595   if (id && BASELINK_P (id))
14596     id = BASELINK_FUNCTIONS (id);
14597   return id;
14598 }
14599
14600 /* Parse a type-id.
14601
14602    type-id:
14603      type-specifier-seq abstract-declarator [opt]
14604
14605    Returns the TYPE specified.  */
14606
14607 static tree
14608 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14609                      bool is_trailing_return)
14610 {
14611   cp_decl_specifier_seq type_specifier_seq;
14612   cp_declarator *abstract_declarator;
14613
14614   /* Parse the type-specifier-seq.  */
14615   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14616                                 is_trailing_return,
14617                                 &type_specifier_seq);
14618   if (type_specifier_seq.type == error_mark_node)
14619     return error_mark_node;
14620
14621   /* There might or might not be an abstract declarator.  */
14622   cp_parser_parse_tentatively (parser);
14623   /* Look for the declarator.  */
14624   abstract_declarator
14625     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14626                             /*parenthesized_p=*/NULL,
14627                             /*member_p=*/false);
14628   /* Check to see if there really was a declarator.  */
14629   if (!cp_parser_parse_definitely (parser))
14630     abstract_declarator = NULL;
14631
14632   if (type_specifier_seq.type
14633       && type_uses_auto (type_specifier_seq.type))
14634     {
14635       /* A type-id with type 'auto' is only ok if the abstract declarator
14636          is a function declarator with a late-specified return type.  */
14637       if (abstract_declarator
14638           && abstract_declarator->kind == cdk_function
14639           && abstract_declarator->u.function.late_return_type)
14640         /* OK */;
14641       else
14642         {
14643           error ("invalid use of %<auto%>");
14644           return error_mark_node;
14645         }
14646     }
14647   
14648   return groktypename (&type_specifier_seq, abstract_declarator,
14649                        is_template_arg);
14650 }
14651
14652 static tree cp_parser_type_id (cp_parser *parser)
14653 {
14654   return cp_parser_type_id_1 (parser, false, false);
14655 }
14656
14657 static tree cp_parser_template_type_arg (cp_parser *parser)
14658 {
14659   return cp_parser_type_id_1 (parser, true, false);
14660 }
14661
14662 static tree cp_parser_trailing_type_id (cp_parser *parser)
14663 {
14664   return cp_parser_type_id_1 (parser, false, true);
14665 }
14666
14667 /* Parse a type-specifier-seq.
14668
14669    type-specifier-seq:
14670      type-specifier type-specifier-seq [opt]
14671
14672    GNU extension:
14673
14674    type-specifier-seq:
14675      attributes type-specifier-seq [opt]
14676
14677    If IS_DECLARATION is true, we are at the start of a "condition" or
14678    exception-declaration, so we might be followed by a declarator-id.
14679
14680    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14681    i.e. we've just seen "->".
14682
14683    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14684
14685 static void
14686 cp_parser_type_specifier_seq (cp_parser* parser,
14687                               bool is_declaration,
14688                               bool is_trailing_return,
14689                               cp_decl_specifier_seq *type_specifier_seq)
14690 {
14691   bool seen_type_specifier = false;
14692   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14693   cp_token *start_token = NULL;
14694
14695   /* Clear the TYPE_SPECIFIER_SEQ.  */
14696   clear_decl_specs (type_specifier_seq);
14697
14698   /* In the context of a trailing return type, enum E { } is an
14699      elaborated-type-specifier followed by a function-body, not an
14700      enum-specifier.  */
14701   if (is_trailing_return)
14702     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14703
14704   /* Parse the type-specifiers and attributes.  */
14705   while (true)
14706     {
14707       tree type_specifier;
14708       bool is_cv_qualifier;
14709
14710       /* Check for attributes first.  */
14711       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14712         {
14713           type_specifier_seq->attributes =
14714             chainon (type_specifier_seq->attributes,
14715                      cp_parser_attributes_opt (parser));
14716           continue;
14717         }
14718
14719       /* record the token of the beginning of the type specifier seq,
14720          for error reporting purposes*/
14721      if (!start_token)
14722        start_token = cp_lexer_peek_token (parser->lexer);
14723
14724       /* Look for the type-specifier.  */
14725       type_specifier = cp_parser_type_specifier (parser,
14726                                                  flags,
14727                                                  type_specifier_seq,
14728                                                  /*is_declaration=*/false,
14729                                                  NULL,
14730                                                  &is_cv_qualifier);
14731       if (!type_specifier)
14732         {
14733           /* If the first type-specifier could not be found, this is not a
14734              type-specifier-seq at all.  */
14735           if (!seen_type_specifier)
14736             {
14737               cp_parser_error (parser, "expected type-specifier");
14738               type_specifier_seq->type = error_mark_node;
14739               return;
14740             }
14741           /* If subsequent type-specifiers could not be found, the
14742              type-specifier-seq is complete.  */
14743           break;
14744         }
14745
14746       seen_type_specifier = true;
14747       /* The standard says that a condition can be:
14748
14749             type-specifier-seq declarator = assignment-expression
14750
14751          However, given:
14752
14753            struct S {};
14754            if (int S = ...)
14755
14756          we should treat the "S" as a declarator, not as a
14757          type-specifier.  The standard doesn't say that explicitly for
14758          type-specifier-seq, but it does say that for
14759          decl-specifier-seq in an ordinary declaration.  Perhaps it
14760          would be clearer just to allow a decl-specifier-seq here, and
14761          then add a semantic restriction that if any decl-specifiers
14762          that are not type-specifiers appear, the program is invalid.  */
14763       if (is_declaration && !is_cv_qualifier)
14764         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14765     }
14766
14767   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14768 }
14769
14770 /* Parse a parameter-declaration-clause.
14771
14772    parameter-declaration-clause:
14773      parameter-declaration-list [opt] ... [opt]
14774      parameter-declaration-list , ...
14775
14776    Returns a representation for the parameter declarations.  A return
14777    value of NULL indicates a parameter-declaration-clause consisting
14778    only of an ellipsis.  */
14779
14780 static tree
14781 cp_parser_parameter_declaration_clause (cp_parser* parser)
14782 {
14783   tree parameters;
14784   cp_token *token;
14785   bool ellipsis_p;
14786   bool is_error;
14787
14788   /* Peek at the next token.  */
14789   token = cp_lexer_peek_token (parser->lexer);
14790   /* Check for trivial parameter-declaration-clauses.  */
14791   if (token->type == CPP_ELLIPSIS)
14792     {
14793       /* Consume the `...' token.  */
14794       cp_lexer_consume_token (parser->lexer);
14795       return NULL_TREE;
14796     }
14797   else if (token->type == CPP_CLOSE_PAREN)
14798     /* There are no parameters.  */
14799     {
14800 #ifndef NO_IMPLICIT_EXTERN_C
14801       if (in_system_header && current_class_type == NULL
14802           && current_lang_name == lang_name_c)
14803         return NULL_TREE;
14804       else
14805 #endif
14806         return void_list_node;
14807     }
14808   /* Check for `(void)', too, which is a special case.  */
14809   else if (token->keyword == RID_VOID
14810            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14811                == CPP_CLOSE_PAREN))
14812     {
14813       /* Consume the `void' token.  */
14814       cp_lexer_consume_token (parser->lexer);
14815       /* There are no parameters.  */
14816       return void_list_node;
14817     }
14818
14819   /* Parse the parameter-declaration-list.  */
14820   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14821   /* If a parse error occurred while parsing the
14822      parameter-declaration-list, then the entire
14823      parameter-declaration-clause is erroneous.  */
14824   if (is_error)
14825     return NULL;
14826
14827   /* Peek at the next token.  */
14828   token = cp_lexer_peek_token (parser->lexer);
14829   /* If it's a `,', the clause should terminate with an ellipsis.  */
14830   if (token->type == CPP_COMMA)
14831     {
14832       /* Consume the `,'.  */
14833       cp_lexer_consume_token (parser->lexer);
14834       /* Expect an ellipsis.  */
14835       ellipsis_p
14836         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14837     }
14838   /* It might also be `...' if the optional trailing `,' was
14839      omitted.  */
14840   else if (token->type == CPP_ELLIPSIS)
14841     {
14842       /* Consume the `...' token.  */
14843       cp_lexer_consume_token (parser->lexer);
14844       /* And remember that we saw it.  */
14845       ellipsis_p = true;
14846     }
14847   else
14848     ellipsis_p = false;
14849
14850   /* Finish the parameter list.  */
14851   if (!ellipsis_p)
14852     parameters = chainon (parameters, void_list_node);
14853
14854   return parameters;
14855 }
14856
14857 /* Parse a parameter-declaration-list.
14858
14859    parameter-declaration-list:
14860      parameter-declaration
14861      parameter-declaration-list , parameter-declaration
14862
14863    Returns a representation of the parameter-declaration-list, as for
14864    cp_parser_parameter_declaration_clause.  However, the
14865    `void_list_node' is never appended to the list.  Upon return,
14866    *IS_ERROR will be true iff an error occurred.  */
14867
14868 static tree
14869 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14870 {
14871   tree parameters = NULL_TREE;
14872   tree *tail = &parameters; 
14873   bool saved_in_unbraced_linkage_specification_p;
14874   int index = 0;
14875
14876   /* Assume all will go well.  */
14877   *is_error = false;
14878   /* The special considerations that apply to a function within an
14879      unbraced linkage specifications do not apply to the parameters
14880      to the function.  */
14881   saved_in_unbraced_linkage_specification_p 
14882     = parser->in_unbraced_linkage_specification_p;
14883   parser->in_unbraced_linkage_specification_p = false;
14884
14885   /* Look for more parameters.  */
14886   while (true)
14887     {
14888       cp_parameter_declarator *parameter;
14889       tree decl = error_mark_node;
14890       bool parenthesized_p;
14891       /* Parse the parameter.  */
14892       parameter
14893         = cp_parser_parameter_declaration (parser,
14894                                            /*template_parm_p=*/false,
14895                                            &parenthesized_p);
14896
14897       /* We don't know yet if the enclosing context is deprecated, so wait
14898          and warn in grokparms if appropriate.  */
14899       deprecated_state = DEPRECATED_SUPPRESS;
14900
14901       if (parameter)
14902         decl = grokdeclarator (parameter->declarator,
14903                                &parameter->decl_specifiers,
14904                                PARM,
14905                                parameter->default_argument != NULL_TREE,
14906                                &parameter->decl_specifiers.attributes);
14907
14908       deprecated_state = DEPRECATED_NORMAL;
14909
14910       /* If a parse error occurred parsing the parameter declaration,
14911          then the entire parameter-declaration-list is erroneous.  */
14912       if (decl == error_mark_node)
14913         {
14914           *is_error = true;
14915           parameters = error_mark_node;
14916           break;
14917         }
14918
14919       if (parameter->decl_specifiers.attributes)
14920         cplus_decl_attributes (&decl,
14921                                parameter->decl_specifiers.attributes,
14922                                0);
14923       if (DECL_NAME (decl))
14924         decl = pushdecl (decl);
14925
14926       if (decl != error_mark_node)
14927         {
14928           retrofit_lang_decl (decl);
14929           DECL_PARM_INDEX (decl) = ++index;
14930         }
14931
14932       /* Add the new parameter to the list.  */
14933       *tail = build_tree_list (parameter->default_argument, decl);
14934       tail = &TREE_CHAIN (*tail);
14935
14936       /* Peek at the next token.  */
14937       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14938           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14939           /* These are for Objective-C++ */
14940           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14941           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14942         /* The parameter-declaration-list is complete.  */
14943         break;
14944       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14945         {
14946           cp_token *token;
14947
14948           /* Peek at the next token.  */
14949           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14950           /* If it's an ellipsis, then the list is complete.  */
14951           if (token->type == CPP_ELLIPSIS)
14952             break;
14953           /* Otherwise, there must be more parameters.  Consume the
14954              `,'.  */
14955           cp_lexer_consume_token (parser->lexer);
14956           /* When parsing something like:
14957
14958                 int i(float f, double d)
14959
14960              we can tell after seeing the declaration for "f" that we
14961              are not looking at an initialization of a variable "i",
14962              but rather at the declaration of a function "i".
14963
14964              Due to the fact that the parsing of template arguments
14965              (as specified to a template-id) requires backtracking we
14966              cannot use this technique when inside a template argument
14967              list.  */
14968           if (!parser->in_template_argument_list_p
14969               && !parser->in_type_id_in_expr_p
14970               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14971               /* However, a parameter-declaration of the form
14972                  "foat(f)" (which is a valid declaration of a
14973                  parameter "f") can also be interpreted as an
14974                  expression (the conversion of "f" to "float").  */
14975               && !parenthesized_p)
14976             cp_parser_commit_to_tentative_parse (parser);
14977         }
14978       else
14979         {
14980           cp_parser_error (parser, "expected %<,%> or %<...%>");
14981           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14982             cp_parser_skip_to_closing_parenthesis (parser,
14983                                                    /*recovering=*/true,
14984                                                    /*or_comma=*/false,
14985                                                    /*consume_paren=*/false);
14986           break;
14987         }
14988     }
14989
14990   parser->in_unbraced_linkage_specification_p
14991     = saved_in_unbraced_linkage_specification_p;
14992
14993   return parameters;
14994 }
14995
14996 /* Parse a parameter declaration.
14997
14998    parameter-declaration:
14999      decl-specifier-seq ... [opt] declarator
15000      decl-specifier-seq declarator = assignment-expression
15001      decl-specifier-seq ... [opt] abstract-declarator [opt]
15002      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15003
15004    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15005    declares a template parameter.  (In that case, a non-nested `>'
15006    token encountered during the parsing of the assignment-expression
15007    is not interpreted as a greater-than operator.)
15008
15009    Returns a representation of the parameter, or NULL if an error
15010    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15011    true iff the declarator is of the form "(p)".  */
15012
15013 static cp_parameter_declarator *
15014 cp_parser_parameter_declaration (cp_parser *parser,
15015                                  bool template_parm_p,
15016                                  bool *parenthesized_p)
15017 {
15018   int declares_class_or_enum;
15019   cp_decl_specifier_seq decl_specifiers;
15020   cp_declarator *declarator;
15021   tree default_argument;
15022   cp_token *token = NULL, *declarator_token_start = NULL;
15023   const char *saved_message;
15024
15025   /* In a template parameter, `>' is not an operator.
15026
15027      [temp.param]
15028
15029      When parsing a default template-argument for a non-type
15030      template-parameter, the first non-nested `>' is taken as the end
15031      of the template parameter-list rather than a greater-than
15032      operator.  */
15033
15034   /* Type definitions may not appear in parameter types.  */
15035   saved_message = parser->type_definition_forbidden_message;
15036   parser->type_definition_forbidden_message
15037     = G_("types may not be defined in parameter types");
15038
15039   /* Parse the declaration-specifiers.  */
15040   cp_parser_decl_specifier_seq (parser,
15041                                 CP_PARSER_FLAGS_NONE,
15042                                 &decl_specifiers,
15043                                 &declares_class_or_enum);
15044
15045   /* Complain about missing 'typename' or other invalid type names.  */
15046   if (!decl_specifiers.any_type_specifiers_p)
15047     cp_parser_parse_and_diagnose_invalid_type_name (parser);
15048
15049   /* If an error occurred, there's no reason to attempt to parse the
15050      rest of the declaration.  */
15051   if (cp_parser_error_occurred (parser))
15052     {
15053       parser->type_definition_forbidden_message = saved_message;
15054       return NULL;
15055     }
15056
15057   /* Peek at the next token.  */
15058   token = cp_lexer_peek_token (parser->lexer);
15059
15060   /* If the next token is a `)', `,', `=', `>', or `...', then there
15061      is no declarator. However, when variadic templates are enabled,
15062      there may be a declarator following `...'.  */
15063   if (token->type == CPP_CLOSE_PAREN
15064       || token->type == CPP_COMMA
15065       || token->type == CPP_EQ
15066       || token->type == CPP_GREATER)
15067     {
15068       declarator = NULL;
15069       if (parenthesized_p)
15070         *parenthesized_p = false;
15071     }
15072   /* Otherwise, there should be a declarator.  */
15073   else
15074     {
15075       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15076       parser->default_arg_ok_p = false;
15077
15078       /* After seeing a decl-specifier-seq, if the next token is not a
15079          "(", there is no possibility that the code is a valid
15080          expression.  Therefore, if parsing tentatively, we commit at
15081          this point.  */
15082       if (!parser->in_template_argument_list_p
15083           /* In an expression context, having seen:
15084
15085                (int((char ...
15086
15087              we cannot be sure whether we are looking at a
15088              function-type (taking a "char" as a parameter) or a cast
15089              of some object of type "char" to "int".  */
15090           && !parser->in_type_id_in_expr_p
15091           && cp_parser_uncommitted_to_tentative_parse_p (parser)
15092           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
15093         cp_parser_commit_to_tentative_parse (parser);
15094       /* Parse the declarator.  */
15095       declarator_token_start = token;
15096       declarator = cp_parser_declarator (parser,
15097                                          CP_PARSER_DECLARATOR_EITHER,
15098                                          /*ctor_dtor_or_conv_p=*/NULL,
15099                                          parenthesized_p,
15100                                          /*member_p=*/false);
15101       parser->default_arg_ok_p = saved_default_arg_ok_p;
15102       /* After the declarator, allow more attributes.  */
15103       decl_specifiers.attributes
15104         = chainon (decl_specifiers.attributes,
15105                    cp_parser_attributes_opt (parser));
15106     }
15107
15108   /* If the next token is an ellipsis, and we have not seen a
15109      declarator name, and the type of the declarator contains parameter
15110      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15111      a parameter pack expansion expression. Otherwise, leave the
15112      ellipsis for a C-style variadic function. */
15113   token = cp_lexer_peek_token (parser->lexer);
15114   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15115     {
15116       tree type = decl_specifiers.type;
15117
15118       if (type && DECL_P (type))
15119         type = TREE_TYPE (type);
15120
15121       if (type
15122           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15123           && declarator_can_be_parameter_pack (declarator)
15124           && (!declarator || !declarator->parameter_pack_p)
15125           && uses_parameter_packs (type))
15126         {
15127           /* Consume the `...'. */
15128           cp_lexer_consume_token (parser->lexer);
15129           maybe_warn_variadic_templates ();
15130           
15131           /* Build a pack expansion type */
15132           if (declarator)
15133             declarator->parameter_pack_p = true;
15134           else
15135             decl_specifiers.type = make_pack_expansion (type);
15136         }
15137     }
15138
15139   /* The restriction on defining new types applies only to the type
15140      of the parameter, not to the default argument.  */
15141   parser->type_definition_forbidden_message = saved_message;
15142
15143   /* If the next token is `=', then process a default argument.  */
15144   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15145     {
15146       /* Consume the `='.  */
15147       cp_lexer_consume_token (parser->lexer);
15148
15149       /* If we are defining a class, then the tokens that make up the
15150          default argument must be saved and processed later.  */
15151       if (!template_parm_p && at_class_scope_p ()
15152           && TYPE_BEING_DEFINED (current_class_type)
15153           && !LAMBDA_TYPE_P (current_class_type))
15154         {
15155           unsigned depth = 0;
15156           int maybe_template_id = 0;
15157           cp_token *first_token;
15158           cp_token *token;
15159
15160           /* Add tokens until we have processed the entire default
15161              argument.  We add the range [first_token, token).  */
15162           first_token = cp_lexer_peek_token (parser->lexer);
15163           while (true)
15164             {
15165               bool done = false;
15166
15167               /* Peek at the next token.  */
15168               token = cp_lexer_peek_token (parser->lexer);
15169               /* What we do depends on what token we have.  */
15170               switch (token->type)
15171                 {
15172                   /* In valid code, a default argument must be
15173                      immediately followed by a `,' `)', or `...'.  */
15174                 case CPP_COMMA:
15175                   if (depth == 0 && maybe_template_id)
15176                     {
15177                       /* If we've seen a '<', we might be in a
15178                          template-argument-list.  Until Core issue 325 is
15179                          resolved, we don't know how this situation ought
15180                          to be handled, so try to DTRT.  We check whether
15181                          what comes after the comma is a valid parameter
15182                          declaration list.  If it is, then the comma ends
15183                          the default argument; otherwise the default
15184                          argument continues.  */
15185                       bool error = false;
15186
15187                       /* Set ITALP so cp_parser_parameter_declaration_list
15188                          doesn't decide to commit to this parse.  */
15189                       bool saved_italp = parser->in_template_argument_list_p;
15190                       parser->in_template_argument_list_p = true;
15191
15192                       cp_parser_parse_tentatively (parser);
15193                       cp_lexer_consume_token (parser->lexer);
15194                       cp_parser_parameter_declaration_list (parser, &error);
15195                       if (!cp_parser_error_occurred (parser) && !error)
15196                         done = true;
15197                       cp_parser_abort_tentative_parse (parser);
15198
15199                       parser->in_template_argument_list_p = saved_italp;
15200                       break;
15201                     }
15202                 case CPP_CLOSE_PAREN:
15203                 case CPP_ELLIPSIS:
15204                   /* If we run into a non-nested `;', `}', or `]',
15205                      then the code is invalid -- but the default
15206                      argument is certainly over.  */
15207                 case CPP_SEMICOLON:
15208                 case CPP_CLOSE_BRACE:
15209                 case CPP_CLOSE_SQUARE:
15210                   if (depth == 0)
15211                     done = true;
15212                   /* Update DEPTH, if necessary.  */
15213                   else if (token->type == CPP_CLOSE_PAREN
15214                            || token->type == CPP_CLOSE_BRACE
15215                            || token->type == CPP_CLOSE_SQUARE)
15216                     --depth;
15217                   break;
15218
15219                 case CPP_OPEN_PAREN:
15220                 case CPP_OPEN_SQUARE:
15221                 case CPP_OPEN_BRACE:
15222                   ++depth;
15223                   break;
15224
15225                 case CPP_LESS:
15226                   if (depth == 0)
15227                     /* This might be the comparison operator, or it might
15228                        start a template argument list.  */
15229                     ++maybe_template_id;
15230                   break;
15231
15232                 case CPP_RSHIFT:
15233                   if (cxx_dialect == cxx98)
15234                     break;
15235                   /* Fall through for C++0x, which treats the `>>'
15236                      operator like two `>' tokens in certain
15237                      cases.  */
15238
15239                 case CPP_GREATER:
15240                   if (depth == 0)
15241                     {
15242                       /* This might be an operator, or it might close a
15243                          template argument list.  But if a previous '<'
15244                          started a template argument list, this will have
15245                          closed it, so we can't be in one anymore.  */
15246                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15247                       if (maybe_template_id < 0)
15248                         maybe_template_id = 0;
15249                     }
15250                   break;
15251
15252                   /* If we run out of tokens, issue an error message.  */
15253                 case CPP_EOF:
15254                 case CPP_PRAGMA_EOL:
15255                   error_at (token->location, "file ends in default argument");
15256                   done = true;
15257                   break;
15258
15259                 case CPP_NAME:
15260                 case CPP_SCOPE:
15261                   /* In these cases, we should look for template-ids.
15262                      For example, if the default argument is
15263                      `X<int, double>()', we need to do name lookup to
15264                      figure out whether or not `X' is a template; if
15265                      so, the `,' does not end the default argument.
15266
15267                      That is not yet done.  */
15268                   break;
15269
15270                 default:
15271                   break;
15272                 }
15273
15274               /* If we've reached the end, stop.  */
15275               if (done)
15276                 break;
15277
15278               /* Add the token to the token block.  */
15279               token = cp_lexer_consume_token (parser->lexer);
15280             }
15281
15282           /* Create a DEFAULT_ARG to represent the unparsed default
15283              argument.  */
15284           default_argument = make_node (DEFAULT_ARG);
15285           DEFARG_TOKENS (default_argument)
15286             = cp_token_cache_new (first_token, token);
15287           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15288         }
15289       /* Outside of a class definition, we can just parse the
15290          assignment-expression.  */
15291       else
15292         {
15293           token = cp_lexer_peek_token (parser->lexer);
15294           default_argument 
15295             = cp_parser_default_argument (parser, template_parm_p);
15296         }
15297
15298       if (!parser->default_arg_ok_p)
15299         {
15300           if (flag_permissive)
15301             warning (0, "deprecated use of default argument for parameter of non-function");
15302           else
15303             {
15304               error_at (token->location,
15305                         "default arguments are only "
15306                         "permitted for function parameters");
15307               default_argument = NULL_TREE;
15308             }
15309         }
15310       else if ((declarator && declarator->parameter_pack_p)
15311                || (decl_specifiers.type
15312                    && PACK_EXPANSION_P (decl_specifiers.type)))
15313         {
15314           /* Find the name of the parameter pack.  */     
15315           cp_declarator *id_declarator = declarator;
15316           while (id_declarator && id_declarator->kind != cdk_id)
15317             id_declarator = id_declarator->declarator;
15318           
15319           if (id_declarator && id_declarator->kind == cdk_id)
15320             error_at (declarator_token_start->location,
15321                       template_parm_p 
15322                       ? "template parameter pack %qD"
15323                       " cannot have a default argument"
15324                       : "parameter pack %qD cannot have a default argument",
15325                       id_declarator->u.id.unqualified_name);
15326           else
15327             error_at (declarator_token_start->location,
15328                       template_parm_p 
15329                       ? "template parameter pack cannot have a default argument"
15330                       : "parameter pack cannot have a default argument");
15331           
15332           default_argument = NULL_TREE;
15333         }
15334     }
15335   else
15336     default_argument = NULL_TREE;
15337
15338   return make_parameter_declarator (&decl_specifiers,
15339                                     declarator,
15340                                     default_argument);
15341 }
15342
15343 /* Parse a default argument and return it.
15344
15345    TEMPLATE_PARM_P is true if this is a default argument for a
15346    non-type template parameter.  */
15347 static tree
15348 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15349 {
15350   tree default_argument = NULL_TREE;
15351   bool saved_greater_than_is_operator_p;
15352   bool saved_local_variables_forbidden_p;
15353
15354   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15355      set correctly.  */
15356   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15357   parser->greater_than_is_operator_p = !template_parm_p;
15358   /* Local variable names (and the `this' keyword) may not
15359      appear in a default argument.  */
15360   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15361   parser->local_variables_forbidden_p = true;
15362   /* Parse the assignment-expression.  */
15363   if (template_parm_p)
15364     push_deferring_access_checks (dk_no_deferred);
15365   default_argument
15366     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15367   if (template_parm_p)
15368     pop_deferring_access_checks ();
15369   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15370   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15371
15372   return default_argument;
15373 }
15374
15375 /* Parse a function-body.
15376
15377    function-body:
15378      compound_statement  */
15379
15380 static void
15381 cp_parser_function_body (cp_parser *parser)
15382 {
15383   cp_parser_compound_statement (parser, NULL, false);
15384 }
15385
15386 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15387    true if a ctor-initializer was present.  */
15388
15389 static bool
15390 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15391 {
15392   tree body;
15393   bool ctor_initializer_p;
15394
15395   /* Begin the function body.  */
15396   body = begin_function_body ();
15397   /* Parse the optional ctor-initializer.  */
15398   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15399   /* Parse the function-body.  */
15400   cp_parser_function_body (parser);
15401   /* Finish the function body.  */
15402   finish_function_body (body);
15403
15404   return ctor_initializer_p;
15405 }
15406
15407 /* Parse an initializer.
15408
15409    initializer:
15410      = initializer-clause
15411      ( expression-list )
15412
15413    Returns an expression representing the initializer.  If no
15414    initializer is present, NULL_TREE is returned.
15415
15416    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15417    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15418    set to TRUE if there is no initializer present.  If there is an
15419    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15420    is set to true; otherwise it is set to false.  */
15421
15422 static tree
15423 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15424                        bool* non_constant_p)
15425 {
15426   cp_token *token;
15427   tree init;
15428
15429   /* Peek at the next token.  */
15430   token = cp_lexer_peek_token (parser->lexer);
15431
15432   /* Let our caller know whether or not this initializer was
15433      parenthesized.  */
15434   *is_direct_init = (token->type != CPP_EQ);
15435   /* Assume that the initializer is constant.  */
15436   *non_constant_p = false;
15437
15438   if (token->type == CPP_EQ)
15439     {
15440       /* Consume the `='.  */
15441       cp_lexer_consume_token (parser->lexer);
15442       /* Parse the initializer-clause.  */
15443       init = cp_parser_initializer_clause (parser, non_constant_p);
15444     }
15445   else if (token->type == CPP_OPEN_PAREN)
15446     {
15447       VEC(tree,gc) *vec;
15448       vec = cp_parser_parenthesized_expression_list (parser, false,
15449                                                      /*cast_p=*/false,
15450                                                      /*allow_expansion_p=*/true,
15451                                                      non_constant_p);
15452       if (vec == NULL)
15453         return error_mark_node;
15454       init = build_tree_list_vec (vec);
15455       release_tree_vector (vec);
15456     }
15457   else if (token->type == CPP_OPEN_BRACE)
15458     {
15459       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
15460       init = cp_parser_braced_list (parser, non_constant_p);
15461       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15462     }
15463   else
15464     {
15465       /* Anything else is an error.  */
15466       cp_parser_error (parser, "expected initializer");
15467       init = error_mark_node;
15468     }
15469
15470   return init;
15471 }
15472
15473 /* Parse an initializer-clause.
15474
15475    initializer-clause:
15476      assignment-expression
15477      braced-init-list
15478
15479    Returns an expression representing the initializer.
15480
15481    If the `assignment-expression' production is used the value
15482    returned is simply a representation for the expression.
15483
15484    Otherwise, calls cp_parser_braced_list.  */
15485
15486 static tree
15487 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15488 {
15489   tree initializer;
15490
15491   /* Assume the expression is constant.  */
15492   *non_constant_p = false;
15493
15494   /* If it is not a `{', then we are looking at an
15495      assignment-expression.  */
15496   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15497     {
15498       initializer
15499         = cp_parser_constant_expression (parser,
15500                                         /*allow_non_constant_p=*/true,
15501                                         non_constant_p);
15502       if (!*non_constant_p)
15503         initializer = fold_non_dependent_expr (initializer);
15504     }
15505   else
15506     initializer = cp_parser_braced_list (parser, non_constant_p);
15507
15508   return initializer;
15509 }
15510
15511 /* Parse a brace-enclosed initializer list.
15512
15513    braced-init-list:
15514      { initializer-list , [opt] }
15515      { }
15516
15517    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15518    the elements of the initializer-list (or NULL, if the last
15519    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15520    NULL_TREE.  There is no way to detect whether or not the optional
15521    trailing `,' was provided.  NON_CONSTANT_P is as for
15522    cp_parser_initializer.  */     
15523
15524 static tree
15525 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15526 {
15527   tree initializer;
15528
15529   /* Consume the `{' token.  */
15530   cp_lexer_consume_token (parser->lexer);
15531   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15532   initializer = make_node (CONSTRUCTOR);
15533   /* If it's not a `}', then there is a non-trivial initializer.  */
15534   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15535     {
15536       /* Parse the initializer list.  */
15537       CONSTRUCTOR_ELTS (initializer)
15538         = cp_parser_initializer_list (parser, non_constant_p);
15539       /* A trailing `,' token is allowed.  */
15540       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15541         cp_lexer_consume_token (parser->lexer);
15542     }
15543   /* Now, there should be a trailing `}'.  */
15544   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15545   TREE_TYPE (initializer) = init_list_type_node;
15546   return initializer;
15547 }
15548
15549 /* Parse an initializer-list.
15550
15551    initializer-list:
15552      initializer-clause ... [opt]
15553      initializer-list , initializer-clause ... [opt]
15554
15555    GNU Extension:
15556
15557    initializer-list:
15558      identifier : initializer-clause
15559      initializer-list, identifier : initializer-clause
15560
15561    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15562    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15563    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15564    as for cp_parser_initializer.  */
15565
15566 static VEC(constructor_elt,gc) *
15567 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15568 {
15569   VEC(constructor_elt,gc) *v = NULL;
15570
15571   /* Assume all of the expressions are constant.  */
15572   *non_constant_p = false;
15573
15574   /* Parse the rest of the list.  */
15575   while (true)
15576     {
15577       cp_token *token;
15578       tree identifier;
15579       tree initializer;
15580       bool clause_non_constant_p;
15581
15582       /* If the next token is an identifier and the following one is a
15583          colon, we are looking at the GNU designated-initializer
15584          syntax.  */
15585       if (cp_parser_allow_gnu_extensions_p (parser)
15586           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15587           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15588         {
15589           /* Warn the user that they are using an extension.  */
15590           pedwarn (input_location, OPT_pedantic, 
15591                    "ISO C++ does not allow designated initializers");
15592           /* Consume the identifier.  */
15593           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15594           /* Consume the `:'.  */
15595           cp_lexer_consume_token (parser->lexer);
15596         }
15597       else
15598         identifier = NULL_TREE;
15599
15600       /* Parse the initializer.  */
15601       initializer = cp_parser_initializer_clause (parser,
15602                                                   &clause_non_constant_p);
15603       /* If any clause is non-constant, so is the entire initializer.  */
15604       if (clause_non_constant_p)
15605         *non_constant_p = true;
15606
15607       /* If we have an ellipsis, this is an initializer pack
15608          expansion.  */
15609       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15610         {
15611           /* Consume the `...'.  */
15612           cp_lexer_consume_token (parser->lexer);
15613
15614           /* Turn the initializer into an initializer expansion.  */
15615           initializer = make_pack_expansion (initializer);
15616         }
15617
15618       /* Add it to the vector.  */
15619       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15620
15621       /* If the next token is not a comma, we have reached the end of
15622          the list.  */
15623       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15624         break;
15625
15626       /* Peek at the next token.  */
15627       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15628       /* If the next token is a `}', then we're still done.  An
15629          initializer-clause can have a trailing `,' after the
15630          initializer-list and before the closing `}'.  */
15631       if (token->type == CPP_CLOSE_BRACE)
15632         break;
15633
15634       /* Consume the `,' token.  */
15635       cp_lexer_consume_token (parser->lexer);
15636     }
15637
15638   return v;
15639 }
15640
15641 /* Classes [gram.class] */
15642
15643 /* Parse a class-name.
15644
15645    class-name:
15646      identifier
15647      template-id
15648
15649    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15650    to indicate that names looked up in dependent types should be
15651    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15652    keyword has been used to indicate that the name that appears next
15653    is a template.  TAG_TYPE indicates the explicit tag given before
15654    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15655    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15656    is the class being defined in a class-head.
15657
15658    Returns the TYPE_DECL representing the class.  */
15659
15660 static tree
15661 cp_parser_class_name (cp_parser *parser,
15662                       bool typename_keyword_p,
15663                       bool template_keyword_p,
15664                       enum tag_types tag_type,
15665                       bool check_dependency_p,
15666                       bool class_head_p,
15667                       bool is_declaration)
15668 {
15669   tree decl;
15670   tree scope;
15671   bool typename_p;
15672   cp_token *token;
15673   tree identifier = NULL_TREE;
15674
15675   /* All class-names start with an identifier.  */
15676   token = cp_lexer_peek_token (parser->lexer);
15677   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15678     {
15679       cp_parser_error (parser, "expected class-name");
15680       return error_mark_node;
15681     }
15682
15683   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15684      to a template-id, so we save it here.  */
15685   scope = parser->scope;
15686   if (scope == error_mark_node)
15687     return error_mark_node;
15688
15689   /* Any name names a type if we're following the `typename' keyword
15690      in a qualified name where the enclosing scope is type-dependent.  */
15691   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15692                 && dependent_type_p (scope));
15693   /* Handle the common case (an identifier, but not a template-id)
15694      efficiently.  */
15695   if (token->type == CPP_NAME
15696       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15697     {
15698       cp_token *identifier_token;
15699       bool ambiguous_p;
15700
15701       /* Look for the identifier.  */
15702       identifier_token = cp_lexer_peek_token (parser->lexer);
15703       ambiguous_p = identifier_token->ambiguous_p;
15704       identifier = cp_parser_identifier (parser);
15705       /* If the next token isn't an identifier, we are certainly not
15706          looking at a class-name.  */
15707       if (identifier == error_mark_node)
15708         decl = error_mark_node;
15709       /* If we know this is a type-name, there's no need to look it
15710          up.  */
15711       else if (typename_p)
15712         decl = identifier;
15713       else
15714         {
15715           tree ambiguous_decls;
15716           /* If we already know that this lookup is ambiguous, then
15717              we've already issued an error message; there's no reason
15718              to check again.  */
15719           if (ambiguous_p)
15720             {
15721               cp_parser_simulate_error (parser);
15722               return error_mark_node;
15723             }
15724           /* If the next token is a `::', then the name must be a type
15725              name.
15726
15727              [basic.lookup.qual]
15728
15729              During the lookup for a name preceding the :: scope
15730              resolution operator, object, function, and enumerator
15731              names are ignored.  */
15732           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15733             tag_type = typename_type;
15734           /* Look up the name.  */
15735           decl = cp_parser_lookup_name (parser, identifier,
15736                                         tag_type,
15737                                         /*is_template=*/false,
15738                                         /*is_namespace=*/false,
15739                                         check_dependency_p,
15740                                         &ambiguous_decls,
15741                                         identifier_token->location);
15742           if (ambiguous_decls)
15743             {
15744               if (cp_parser_parsing_tentatively (parser))
15745                 cp_parser_simulate_error (parser);
15746               return error_mark_node;
15747             }
15748         }
15749     }
15750   else
15751     {
15752       /* Try a template-id.  */
15753       decl = cp_parser_template_id (parser, template_keyword_p,
15754                                     check_dependency_p,
15755                                     is_declaration);
15756       if (decl == error_mark_node)
15757         return error_mark_node;
15758     }
15759
15760   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15761
15762   /* If this is a typename, create a TYPENAME_TYPE.  */
15763   if (typename_p && decl != error_mark_node)
15764     {
15765       decl = make_typename_type (scope, decl, typename_type,
15766                                  /*complain=*/tf_error);
15767       if (decl != error_mark_node)
15768         decl = TYPE_NAME (decl);
15769     }
15770
15771   /* Check to see that it is really the name of a class.  */
15772   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15773       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15774       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15775     /* Situations like this:
15776
15777          template <typename T> struct A {
15778            typename T::template X<int>::I i;
15779          };
15780
15781        are problematic.  Is `T::template X<int>' a class-name?  The
15782        standard does not seem to be definitive, but there is no other
15783        valid interpretation of the following `::'.  Therefore, those
15784        names are considered class-names.  */
15785     {
15786       decl = make_typename_type (scope, decl, tag_type, tf_error);
15787       if (decl != error_mark_node)
15788         decl = TYPE_NAME (decl);
15789     }
15790   else if (TREE_CODE (decl) != TYPE_DECL
15791            || TREE_TYPE (decl) == error_mark_node
15792            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15793     decl = error_mark_node;
15794
15795   if (decl == error_mark_node)
15796     cp_parser_error (parser, "expected class-name");
15797   else if (identifier && !parser->scope)
15798     maybe_note_name_used_in_class (identifier, decl);
15799
15800   return decl;
15801 }
15802
15803 /* Parse a class-specifier.
15804
15805    class-specifier:
15806      class-head { member-specification [opt] }
15807
15808    Returns the TREE_TYPE representing the class.  */
15809
15810 static tree
15811 cp_parser_class_specifier (cp_parser* parser)
15812 {
15813   tree type;
15814   tree attributes = NULL_TREE;
15815   bool nested_name_specifier_p;
15816   unsigned saved_num_template_parameter_lists;
15817   bool saved_in_function_body;
15818   bool saved_in_unbraced_linkage_specification_p;
15819   tree old_scope = NULL_TREE;
15820   tree scope = NULL_TREE;
15821   tree bases;
15822
15823   push_deferring_access_checks (dk_no_deferred);
15824
15825   /* Parse the class-head.  */
15826   type = cp_parser_class_head (parser,
15827                                &nested_name_specifier_p,
15828                                &attributes,
15829                                &bases);
15830   /* If the class-head was a semantic disaster, skip the entire body
15831      of the class.  */
15832   if (!type)
15833     {
15834       cp_parser_skip_to_end_of_block_or_statement (parser);
15835       pop_deferring_access_checks ();
15836       return error_mark_node;
15837     }
15838
15839   /* Look for the `{'.  */
15840   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15841     {
15842       pop_deferring_access_checks ();
15843       return error_mark_node;
15844     }
15845
15846   /* Process the base classes. If they're invalid, skip the 
15847      entire class body.  */
15848   if (!xref_basetypes (type, bases))
15849     {
15850       /* Consuming the closing brace yields better error messages
15851          later on.  */
15852       if (cp_parser_skip_to_closing_brace (parser))
15853         cp_lexer_consume_token (parser->lexer);
15854       pop_deferring_access_checks ();
15855       return error_mark_node;
15856     }
15857
15858   /* Issue an error message if type-definitions are forbidden here.  */
15859   cp_parser_check_type_definition (parser);
15860   /* Remember that we are defining one more class.  */
15861   ++parser->num_classes_being_defined;
15862   /* Inside the class, surrounding template-parameter-lists do not
15863      apply.  */
15864   saved_num_template_parameter_lists
15865     = parser->num_template_parameter_lists;
15866   parser->num_template_parameter_lists = 0;
15867   /* We are not in a function body.  */
15868   saved_in_function_body = parser->in_function_body;
15869   parser->in_function_body = false;
15870   /* We are not immediately inside an extern "lang" block.  */
15871   saved_in_unbraced_linkage_specification_p
15872     = parser->in_unbraced_linkage_specification_p;
15873   parser->in_unbraced_linkage_specification_p = false;
15874
15875   /* Start the class.  */
15876   if (nested_name_specifier_p)
15877     {
15878       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15879       old_scope = push_inner_scope (scope);
15880     }
15881   type = begin_class_definition (type, attributes);
15882
15883   if (type == error_mark_node)
15884     /* If the type is erroneous, skip the entire body of the class.  */
15885     cp_parser_skip_to_closing_brace (parser);
15886   else
15887     /* Parse the member-specification.  */
15888     cp_parser_member_specification_opt (parser);
15889
15890   /* Look for the trailing `}'.  */
15891   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15892   /* Look for trailing attributes to apply to this class.  */
15893   if (cp_parser_allow_gnu_extensions_p (parser))
15894     attributes = cp_parser_attributes_opt (parser);
15895   if (type != error_mark_node)
15896     type = finish_struct (type, attributes);
15897   if (nested_name_specifier_p)
15898     pop_inner_scope (old_scope, scope);
15899   /* If this class is not itself within the scope of another class,
15900      then we need to parse the bodies of all of the queued function
15901      definitions.  Note that the queued functions defined in a class
15902      are not always processed immediately following the
15903      class-specifier for that class.  Consider:
15904
15905        struct A {
15906          struct B { void f() { sizeof (A); } };
15907        };
15908
15909      If `f' were processed before the processing of `A' were
15910      completed, there would be no way to compute the size of `A'.
15911      Note that the nesting we are interested in here is lexical --
15912      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15913      for:
15914
15915        struct A { struct B; };
15916        struct A::B { void f() { } };
15917
15918      there is no need to delay the parsing of `A::B::f'.  */
15919   if (--parser->num_classes_being_defined == 0)
15920     {
15921       tree queue_entry;
15922       tree fn;
15923       tree class_type = NULL_TREE;
15924       tree pushed_scope = NULL_TREE;
15925
15926       /* In a first pass, parse default arguments to the functions.
15927          Then, in a second pass, parse the bodies of the functions.
15928          This two-phased approach handles cases like:
15929
15930             struct S {
15931               void f() { g(); }
15932               void g(int i = 3);
15933             };
15934
15935          */
15936       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15937              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15938            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15939            TREE_PURPOSE (parser->unparsed_functions_queues)
15940              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15941         {
15942           fn = TREE_VALUE (queue_entry);
15943           /* If there are default arguments that have not yet been processed,
15944              take care of them now.  */
15945           if (class_type != TREE_PURPOSE (queue_entry))
15946             {
15947               if (pushed_scope)
15948                 pop_scope (pushed_scope);
15949               class_type = TREE_PURPOSE (queue_entry);
15950               pushed_scope = push_scope (class_type);
15951             }
15952           /* Make sure that any template parameters are in scope.  */
15953           maybe_begin_member_template_processing (fn);
15954           /* Parse the default argument expressions.  */
15955           cp_parser_late_parsing_default_args (parser, fn);
15956           /* Remove any template parameters from the symbol table.  */
15957           maybe_end_member_template_processing ();
15958         }
15959       if (pushed_scope)
15960         pop_scope (pushed_scope);
15961       /* Now parse the body of the functions.  */
15962       for (TREE_VALUE (parser->unparsed_functions_queues)
15963              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15964            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15965            TREE_VALUE (parser->unparsed_functions_queues)
15966              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15967         {
15968           /* Figure out which function we need to process.  */
15969           fn = TREE_VALUE (queue_entry);
15970           /* Parse the function.  */
15971           cp_parser_late_parsing_for_member (parser, fn);
15972         }
15973     }
15974
15975   /* Put back any saved access checks.  */
15976   pop_deferring_access_checks ();
15977
15978   /* Restore saved state.  */
15979   parser->in_function_body = saved_in_function_body;
15980   parser->num_template_parameter_lists
15981     = saved_num_template_parameter_lists;
15982   parser->in_unbraced_linkage_specification_p
15983     = saved_in_unbraced_linkage_specification_p;
15984
15985   return type;
15986 }
15987
15988 /* Parse a class-head.
15989
15990    class-head:
15991      class-key identifier [opt] base-clause [opt]
15992      class-key nested-name-specifier identifier base-clause [opt]
15993      class-key nested-name-specifier [opt] template-id
15994        base-clause [opt]
15995
15996    GNU Extensions:
15997      class-key attributes identifier [opt] base-clause [opt]
15998      class-key attributes nested-name-specifier identifier base-clause [opt]
15999      class-key attributes nested-name-specifier [opt] template-id
16000        base-clause [opt]
16001
16002    Upon return BASES is initialized to the list of base classes (or
16003    NULL, if there are none) in the same form returned by
16004    cp_parser_base_clause.
16005
16006    Returns the TYPE of the indicated class.  Sets
16007    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
16008    involving a nested-name-specifier was used, and FALSE otherwise.
16009
16010    Returns error_mark_node if this is not a class-head.
16011
16012    Returns NULL_TREE if the class-head is syntactically valid, but
16013    semantically invalid in a way that means we should skip the entire
16014    body of the class.  */
16015
16016 static tree
16017 cp_parser_class_head (cp_parser* parser,
16018                       bool* nested_name_specifier_p,
16019                       tree *attributes_p,
16020                       tree *bases)
16021 {
16022   tree nested_name_specifier;
16023   enum tag_types class_key;
16024   tree id = NULL_TREE;
16025   tree type = NULL_TREE;
16026   tree attributes;
16027   bool template_id_p = false;
16028   bool qualified_p = false;
16029   bool invalid_nested_name_p = false;
16030   bool invalid_explicit_specialization_p = false;
16031   tree pushed_scope = NULL_TREE;
16032   unsigned num_templates;
16033   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
16034   /* Assume no nested-name-specifier will be present.  */
16035   *nested_name_specifier_p = false;
16036   /* Assume no template parameter lists will be used in defining the
16037      type.  */
16038   num_templates = 0;
16039
16040   *bases = NULL_TREE;
16041
16042   /* Look for the class-key.  */
16043   class_key = cp_parser_class_key (parser);
16044   if (class_key == none_type)
16045     return error_mark_node;
16046
16047   /* Parse the attributes.  */
16048   attributes = cp_parser_attributes_opt (parser);
16049
16050   /* If the next token is `::', that is invalid -- but sometimes
16051      people do try to write:
16052
16053        struct ::S {};
16054
16055      Handle this gracefully by accepting the extra qualifier, and then
16056      issuing an error about it later if this really is a
16057      class-head.  If it turns out just to be an elaborated type
16058      specifier, remain silent.  */
16059   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
16060     qualified_p = true;
16061
16062   push_deferring_access_checks (dk_no_check);
16063
16064   /* Determine the name of the class.  Begin by looking for an
16065      optional nested-name-specifier.  */
16066   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
16067   nested_name_specifier
16068     = cp_parser_nested_name_specifier_opt (parser,
16069                                            /*typename_keyword_p=*/false,
16070                                            /*check_dependency_p=*/false,
16071                                            /*type_p=*/false,
16072                                            /*is_declaration=*/false);
16073   /* If there was a nested-name-specifier, then there *must* be an
16074      identifier.  */
16075   if (nested_name_specifier)
16076     {
16077       type_start_token = cp_lexer_peek_token (parser->lexer);
16078       /* Although the grammar says `identifier', it really means
16079          `class-name' or `template-name'.  You are only allowed to
16080          define a class that has already been declared with this
16081          syntax.
16082
16083          The proposed resolution for Core Issue 180 says that wherever
16084          you see `class T::X' you should treat `X' as a type-name.
16085
16086          It is OK to define an inaccessible class; for example:
16087
16088            class A { class B; };
16089            class A::B {};
16090
16091          We do not know if we will see a class-name, or a
16092          template-name.  We look for a class-name first, in case the
16093          class-name is a template-id; if we looked for the
16094          template-name first we would stop after the template-name.  */
16095       cp_parser_parse_tentatively (parser);
16096       type = cp_parser_class_name (parser,
16097                                    /*typename_keyword_p=*/false,
16098                                    /*template_keyword_p=*/false,
16099                                    class_type,
16100                                    /*check_dependency_p=*/false,
16101                                    /*class_head_p=*/true,
16102                                    /*is_declaration=*/false);
16103       /* If that didn't work, ignore the nested-name-specifier.  */
16104       if (!cp_parser_parse_definitely (parser))
16105         {
16106           invalid_nested_name_p = true;
16107           type_start_token = cp_lexer_peek_token (parser->lexer);
16108           id = cp_parser_identifier (parser);
16109           if (id == error_mark_node)
16110             id = NULL_TREE;
16111         }
16112       /* If we could not find a corresponding TYPE, treat this
16113          declaration like an unqualified declaration.  */
16114       if (type == error_mark_node)
16115         nested_name_specifier = NULL_TREE;
16116       /* Otherwise, count the number of templates used in TYPE and its
16117          containing scopes.  */
16118       else
16119         {
16120           tree scope;
16121
16122           for (scope = TREE_TYPE (type);
16123                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16124                scope = (TYPE_P (scope)
16125                         ? TYPE_CONTEXT (scope)
16126                         : DECL_CONTEXT (scope)))
16127             if (TYPE_P (scope)
16128                 && CLASS_TYPE_P (scope)
16129                 && CLASSTYPE_TEMPLATE_INFO (scope)
16130                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16131                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16132               ++num_templates;
16133         }
16134     }
16135   /* Otherwise, the identifier is optional.  */
16136   else
16137     {
16138       /* We don't know whether what comes next is a template-id,
16139          an identifier, or nothing at all.  */
16140       cp_parser_parse_tentatively (parser);
16141       /* Check for a template-id.  */
16142       type_start_token = cp_lexer_peek_token (parser->lexer);
16143       id = cp_parser_template_id (parser,
16144                                   /*template_keyword_p=*/false,
16145                                   /*check_dependency_p=*/true,
16146                                   /*is_declaration=*/true);
16147       /* If that didn't work, it could still be an identifier.  */
16148       if (!cp_parser_parse_definitely (parser))
16149         {
16150           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16151             {
16152               type_start_token = cp_lexer_peek_token (parser->lexer);
16153               id = cp_parser_identifier (parser);
16154             }
16155           else
16156             id = NULL_TREE;
16157         }
16158       else
16159         {
16160           template_id_p = true;
16161           ++num_templates;
16162         }
16163     }
16164
16165   pop_deferring_access_checks ();
16166
16167   if (id)
16168     cp_parser_check_for_invalid_template_id (parser, id,
16169                                              type_start_token->location);
16170
16171   /* If it's not a `:' or a `{' then we can't really be looking at a
16172      class-head, since a class-head only appears as part of a
16173      class-specifier.  We have to detect this situation before calling
16174      xref_tag, since that has irreversible side-effects.  */
16175   if (!cp_parser_next_token_starts_class_definition_p (parser))
16176     {
16177       cp_parser_error (parser, "expected %<{%> or %<:%>");
16178       return error_mark_node;
16179     }
16180
16181   /* At this point, we're going ahead with the class-specifier, even
16182      if some other problem occurs.  */
16183   cp_parser_commit_to_tentative_parse (parser);
16184   /* Issue the error about the overly-qualified name now.  */
16185   if (qualified_p)
16186     {
16187       cp_parser_error (parser,
16188                        "global qualification of class name is invalid");
16189       return error_mark_node;
16190     }
16191   else if (invalid_nested_name_p)
16192     {
16193       cp_parser_error (parser,
16194                        "qualified name does not name a class");
16195       return error_mark_node;
16196     }
16197   else if (nested_name_specifier)
16198     {
16199       tree scope;
16200
16201       /* Reject typedef-names in class heads.  */
16202       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16203         {
16204           error_at (type_start_token->location,
16205                     "invalid class name in declaration of %qD",
16206                     type);
16207           type = NULL_TREE;
16208           goto done;
16209         }
16210
16211       /* Figure out in what scope the declaration is being placed.  */
16212       scope = current_scope ();
16213       /* If that scope does not contain the scope in which the
16214          class was originally declared, the program is invalid.  */
16215       if (scope && !is_ancestor (scope, nested_name_specifier))
16216         {
16217           if (at_namespace_scope_p ())
16218             error_at (type_start_token->location,
16219                       "declaration of %qD in namespace %qD which does not "
16220                       "enclose %qD",
16221                       type, scope, nested_name_specifier);
16222           else
16223             error_at (type_start_token->location,
16224                       "declaration of %qD in %qD which does not enclose %qD",
16225                       type, scope, nested_name_specifier);
16226           type = NULL_TREE;
16227           goto done;
16228         }
16229       /* [dcl.meaning]
16230
16231          A declarator-id shall not be qualified except for the
16232          definition of a ... nested class outside of its class
16233          ... [or] the definition or explicit instantiation of a
16234          class member of a namespace outside of its namespace.  */
16235       if (scope == nested_name_specifier)
16236         {
16237           permerror (nested_name_specifier_token_start->location,
16238                      "extra qualification not allowed");
16239           nested_name_specifier = NULL_TREE;
16240           num_templates = 0;
16241         }
16242     }
16243   /* An explicit-specialization must be preceded by "template <>".  If
16244      it is not, try to recover gracefully.  */
16245   if (at_namespace_scope_p ()
16246       && parser->num_template_parameter_lists == 0
16247       && template_id_p)
16248     {
16249       error_at (type_start_token->location,
16250                 "an explicit specialization must be preceded by %<template <>%>");
16251       invalid_explicit_specialization_p = true;
16252       /* Take the same action that would have been taken by
16253          cp_parser_explicit_specialization.  */
16254       ++parser->num_template_parameter_lists;
16255       begin_specialization ();
16256     }
16257   /* There must be no "return" statements between this point and the
16258      end of this function; set "type "to the correct return value and
16259      use "goto done;" to return.  */
16260   /* Make sure that the right number of template parameters were
16261      present.  */
16262   if (!cp_parser_check_template_parameters (parser, num_templates,
16263                                             type_start_token->location,
16264                                             /*declarator=*/NULL))
16265     {
16266       /* If something went wrong, there is no point in even trying to
16267          process the class-definition.  */
16268       type = NULL_TREE;
16269       goto done;
16270     }
16271
16272   /* Look up the type.  */
16273   if (template_id_p)
16274     {
16275       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16276           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16277               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16278         {
16279           error_at (type_start_token->location,
16280                     "function template %qD redeclared as a class template", id);
16281           type = error_mark_node;
16282         }
16283       else
16284         {
16285           type = TREE_TYPE (id);
16286           type = maybe_process_partial_specialization (type);
16287         }
16288       if (nested_name_specifier)
16289         pushed_scope = push_scope (nested_name_specifier);
16290     }
16291   else if (nested_name_specifier)
16292     {
16293       tree class_type;
16294
16295       /* Given:
16296
16297             template <typename T> struct S { struct T };
16298             template <typename T> struct S<T>::T { };
16299
16300          we will get a TYPENAME_TYPE when processing the definition of
16301          `S::T'.  We need to resolve it to the actual type before we
16302          try to define it.  */
16303       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16304         {
16305           class_type = resolve_typename_type (TREE_TYPE (type),
16306                                               /*only_current_p=*/false);
16307           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16308             type = TYPE_NAME (class_type);
16309           else
16310             {
16311               cp_parser_error (parser, "could not resolve typename type");
16312               type = error_mark_node;
16313             }
16314         }
16315
16316       if (maybe_process_partial_specialization (TREE_TYPE (type))
16317           == error_mark_node)
16318         {
16319           type = NULL_TREE;
16320           goto done;
16321         }
16322
16323       class_type = current_class_type;
16324       /* Enter the scope indicated by the nested-name-specifier.  */
16325       pushed_scope = push_scope (nested_name_specifier);
16326       /* Get the canonical version of this type.  */
16327       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16328       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16329           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16330         {
16331           type = push_template_decl (type);
16332           if (type == error_mark_node)
16333             {
16334               type = NULL_TREE;
16335               goto done;
16336             }
16337         }
16338
16339       type = TREE_TYPE (type);
16340       *nested_name_specifier_p = true;
16341     }
16342   else      /* The name is not a nested name.  */
16343     {
16344       /* If the class was unnamed, create a dummy name.  */
16345       if (!id)
16346         id = make_anon_name ();
16347       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16348                        parser->num_template_parameter_lists);
16349     }
16350
16351   /* Indicate whether this class was declared as a `class' or as a
16352      `struct'.  */
16353   if (TREE_CODE (type) == RECORD_TYPE)
16354     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16355   cp_parser_check_class_key (class_key, type);
16356
16357   /* If this type was already complete, and we see another definition,
16358      that's an error.  */
16359   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16360     {
16361       error_at (type_start_token->location, "redefinition of %q#T",
16362                 type);
16363       error_at (type_start_token->location, "previous definition of %q+#T",
16364                 type);
16365       type = NULL_TREE;
16366       goto done;
16367     }
16368   else if (type == error_mark_node)
16369     type = NULL_TREE;
16370
16371   /* We will have entered the scope containing the class; the names of
16372      base classes should be looked up in that context.  For example:
16373
16374        struct A { struct B {}; struct C; };
16375        struct A::C : B {};
16376
16377      is valid.  */
16378
16379   /* Get the list of base-classes, if there is one.  */
16380   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16381     *bases = cp_parser_base_clause (parser);
16382
16383  done:
16384   /* Leave the scope given by the nested-name-specifier.  We will
16385      enter the class scope itself while processing the members.  */
16386   if (pushed_scope)
16387     pop_scope (pushed_scope);
16388
16389   if (invalid_explicit_specialization_p)
16390     {
16391       end_specialization ();
16392       --parser->num_template_parameter_lists;
16393     }
16394   *attributes_p = attributes;
16395   return type;
16396 }
16397
16398 /* Parse a class-key.
16399
16400    class-key:
16401      class
16402      struct
16403      union
16404
16405    Returns the kind of class-key specified, or none_type to indicate
16406    error.  */
16407
16408 static enum tag_types
16409 cp_parser_class_key (cp_parser* parser)
16410 {
16411   cp_token *token;
16412   enum tag_types tag_type;
16413
16414   /* Look for the class-key.  */
16415   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16416   if (!token)
16417     return none_type;
16418
16419   /* Check to see if the TOKEN is a class-key.  */
16420   tag_type = cp_parser_token_is_class_key (token);
16421   if (!tag_type)
16422     cp_parser_error (parser, "expected class-key");
16423   return tag_type;
16424 }
16425
16426 /* Parse an (optional) member-specification.
16427
16428    member-specification:
16429      member-declaration member-specification [opt]
16430      access-specifier : member-specification [opt]  */
16431
16432 static void
16433 cp_parser_member_specification_opt (cp_parser* parser)
16434 {
16435   while (true)
16436     {
16437       cp_token *token;
16438       enum rid keyword;
16439
16440       /* Peek at the next token.  */
16441       token = cp_lexer_peek_token (parser->lexer);
16442       /* If it's a `}', or EOF then we've seen all the members.  */
16443       if (token->type == CPP_CLOSE_BRACE
16444           || token->type == CPP_EOF
16445           || token->type == CPP_PRAGMA_EOL)
16446         break;
16447
16448       /* See if this token is a keyword.  */
16449       keyword = token->keyword;
16450       switch (keyword)
16451         {
16452         case RID_PUBLIC:
16453         case RID_PROTECTED:
16454         case RID_PRIVATE:
16455           /* Consume the access-specifier.  */
16456           cp_lexer_consume_token (parser->lexer);
16457           /* Remember which access-specifier is active.  */
16458           current_access_specifier = token->u.value;
16459           /* Look for the `:'.  */
16460           cp_parser_require (parser, CPP_COLON, "%<:%>");
16461           break;
16462
16463         default:
16464           /* Accept #pragmas at class scope.  */
16465           if (token->type == CPP_PRAGMA)
16466             {
16467               cp_parser_pragma (parser, pragma_external);
16468               break;
16469             }
16470
16471           /* Otherwise, the next construction must be a
16472              member-declaration.  */
16473           cp_parser_member_declaration (parser);
16474         }
16475     }
16476 }
16477
16478 /* Parse a member-declaration.
16479
16480    member-declaration:
16481      decl-specifier-seq [opt] member-declarator-list [opt] ;
16482      function-definition ; [opt]
16483      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16484      using-declaration
16485      template-declaration
16486
16487    member-declarator-list:
16488      member-declarator
16489      member-declarator-list , member-declarator
16490
16491    member-declarator:
16492      declarator pure-specifier [opt]
16493      declarator constant-initializer [opt]
16494      identifier [opt] : constant-expression
16495
16496    GNU Extensions:
16497
16498    member-declaration:
16499      __extension__ member-declaration
16500
16501    member-declarator:
16502      declarator attributes [opt] pure-specifier [opt]
16503      declarator attributes [opt] constant-initializer [opt]
16504      identifier [opt] attributes [opt] : constant-expression  
16505
16506    C++0x Extensions:
16507
16508    member-declaration:
16509      static_assert-declaration  */
16510
16511 static void
16512 cp_parser_member_declaration (cp_parser* parser)
16513 {
16514   cp_decl_specifier_seq decl_specifiers;
16515   tree prefix_attributes;
16516   tree decl;
16517   int declares_class_or_enum;
16518   bool friend_p;
16519   cp_token *token = NULL;
16520   cp_token *decl_spec_token_start = NULL;
16521   cp_token *initializer_token_start = NULL;
16522   int saved_pedantic;
16523
16524   /* Check for the `__extension__' keyword.  */
16525   if (cp_parser_extension_opt (parser, &saved_pedantic))
16526     {
16527       /* Recurse.  */
16528       cp_parser_member_declaration (parser);
16529       /* Restore the old value of the PEDANTIC flag.  */
16530       pedantic = saved_pedantic;
16531
16532       return;
16533     }
16534
16535   /* Check for a template-declaration.  */
16536   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16537     {
16538       /* An explicit specialization here is an error condition, and we
16539          expect the specialization handler to detect and report this.  */
16540       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16541           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16542         cp_parser_explicit_specialization (parser);
16543       else
16544         cp_parser_template_declaration (parser, /*member_p=*/true);
16545
16546       return;
16547     }
16548
16549   /* Check for a using-declaration.  */
16550   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16551     {
16552       /* Parse the using-declaration.  */
16553       cp_parser_using_declaration (parser,
16554                                    /*access_declaration_p=*/false);
16555       return;
16556     }
16557
16558   /* Check for @defs.  */
16559   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16560     {
16561       tree ivar, member;
16562       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16563       ivar = ivar_chains;
16564       while (ivar)
16565         {
16566           member = ivar;
16567           ivar = TREE_CHAIN (member);
16568           TREE_CHAIN (member) = NULL_TREE;
16569           finish_member_declaration (member);
16570         }
16571       return;
16572     }
16573
16574   /* If the next token is `static_assert' we have a static assertion.  */
16575   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16576     {
16577       cp_parser_static_assert (parser, /*member_p=*/true);
16578       return;
16579     }
16580
16581   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16582     return;
16583
16584   /* Parse the decl-specifier-seq.  */
16585   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16586   cp_parser_decl_specifier_seq (parser,
16587                                 CP_PARSER_FLAGS_OPTIONAL,
16588                                 &decl_specifiers,
16589                                 &declares_class_or_enum);
16590   prefix_attributes = decl_specifiers.attributes;
16591   decl_specifiers.attributes = NULL_TREE;
16592   /* Check for an invalid type-name.  */
16593   if (!decl_specifiers.any_type_specifiers_p
16594       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16595     return;
16596   /* If there is no declarator, then the decl-specifier-seq should
16597      specify a type.  */
16598   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16599     {
16600       /* If there was no decl-specifier-seq, and the next token is a
16601          `;', then we have something like:
16602
16603            struct S { ; };
16604
16605          [class.mem]
16606
16607          Each member-declaration shall declare at least one member
16608          name of the class.  */
16609       if (!decl_specifiers.any_specifiers_p)
16610         {
16611           cp_token *token = cp_lexer_peek_token (parser->lexer);
16612           if (!in_system_header_at (token->location))
16613             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16614         }
16615       else
16616         {
16617           tree type;
16618
16619           /* See if this declaration is a friend.  */
16620           friend_p = cp_parser_friend_p (&decl_specifiers);
16621           /* If there were decl-specifiers, check to see if there was
16622              a class-declaration.  */
16623           type = check_tag_decl (&decl_specifiers);
16624           /* Nested classes have already been added to the class, but
16625              a `friend' needs to be explicitly registered.  */
16626           if (friend_p)
16627             {
16628               /* If the `friend' keyword was present, the friend must
16629                  be introduced with a class-key.  */
16630                if (!declares_class_or_enum)
16631                  error_at (decl_spec_token_start->location,
16632                            "a class-key must be used when declaring a friend");
16633                /* In this case:
16634
16635                     template <typename T> struct A {
16636                       friend struct A<T>::B;
16637                     };
16638
16639                   A<T>::B will be represented by a TYPENAME_TYPE, and
16640                   therefore not recognized by check_tag_decl.  */
16641                if (!type
16642                    && decl_specifiers.type
16643                    && TYPE_P (decl_specifiers.type))
16644                  type = decl_specifiers.type;
16645                if (!type || !TYPE_P (type))
16646                  error_at (decl_spec_token_start->location,
16647                            "friend declaration does not name a class or "
16648                            "function");
16649                else
16650                  make_friend_class (current_class_type, type,
16651                                     /*complain=*/true);
16652             }
16653           /* If there is no TYPE, an error message will already have
16654              been issued.  */
16655           else if (!type || type == error_mark_node)
16656             ;
16657           /* An anonymous aggregate has to be handled specially; such
16658              a declaration really declares a data member (with a
16659              particular type), as opposed to a nested class.  */
16660           else if (ANON_AGGR_TYPE_P (type))
16661             {
16662               /* Remove constructors and such from TYPE, now that we
16663                  know it is an anonymous aggregate.  */
16664               fixup_anonymous_aggr (type);
16665               /* And make the corresponding data member.  */
16666               decl = build_decl (decl_spec_token_start->location,
16667                                  FIELD_DECL, NULL_TREE, type);
16668               /* Add it to the class.  */
16669               finish_member_declaration (decl);
16670             }
16671           else
16672             cp_parser_check_access_in_redeclaration
16673                                               (TYPE_NAME (type),
16674                                                decl_spec_token_start->location);
16675         }
16676     }
16677   else
16678     {
16679       /* See if these declarations will be friends.  */
16680       friend_p = cp_parser_friend_p (&decl_specifiers);
16681
16682       /* Keep going until we hit the `;' at the end of the
16683          declaration.  */
16684       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16685         {
16686           tree attributes = NULL_TREE;
16687           tree first_attribute;
16688
16689           /* Peek at the next token.  */
16690           token = cp_lexer_peek_token (parser->lexer);
16691
16692           /* Check for a bitfield declaration.  */
16693           if (token->type == CPP_COLON
16694               || (token->type == CPP_NAME
16695                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16696                   == CPP_COLON))
16697             {
16698               tree identifier;
16699               tree width;
16700
16701               /* Get the name of the bitfield.  Note that we cannot just
16702                  check TOKEN here because it may have been invalidated by
16703                  the call to cp_lexer_peek_nth_token above.  */
16704               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16705                 identifier = cp_parser_identifier (parser);
16706               else
16707                 identifier = NULL_TREE;
16708
16709               /* Consume the `:' token.  */
16710               cp_lexer_consume_token (parser->lexer);
16711               /* Get the width of the bitfield.  */
16712               width
16713                 = cp_parser_constant_expression (parser,
16714                                                  /*allow_non_constant=*/false,
16715                                                  NULL);
16716
16717               /* Look for attributes that apply to the bitfield.  */
16718               attributes = cp_parser_attributes_opt (parser);
16719               /* Remember which attributes are prefix attributes and
16720                  which are not.  */
16721               first_attribute = attributes;
16722               /* Combine the attributes.  */
16723               attributes = chainon (prefix_attributes, attributes);
16724
16725               /* Create the bitfield declaration.  */
16726               decl = grokbitfield (identifier
16727                                    ? make_id_declarator (NULL_TREE,
16728                                                          identifier,
16729                                                          sfk_none)
16730                                    : NULL,
16731                                    &decl_specifiers,
16732                                    width,
16733                                    attributes);
16734             }
16735           else
16736             {
16737               cp_declarator *declarator;
16738               tree initializer;
16739               tree asm_specification;
16740               int ctor_dtor_or_conv_p;
16741
16742               /* Parse the declarator.  */
16743               declarator
16744                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16745                                         &ctor_dtor_or_conv_p,
16746                                         /*parenthesized_p=*/NULL,
16747                                         /*member_p=*/true);
16748
16749               /* If something went wrong parsing the declarator, make sure
16750                  that we at least consume some tokens.  */
16751               if (declarator == cp_error_declarator)
16752                 {
16753                   /* Skip to the end of the statement.  */
16754                   cp_parser_skip_to_end_of_statement (parser);
16755                   /* If the next token is not a semicolon, that is
16756                      probably because we just skipped over the body of
16757                      a function.  So, we consume a semicolon if
16758                      present, but do not issue an error message if it
16759                      is not present.  */
16760                   if (cp_lexer_next_token_is (parser->lexer,
16761                                               CPP_SEMICOLON))
16762                     cp_lexer_consume_token (parser->lexer);
16763                   return;
16764                 }
16765
16766               if (declares_class_or_enum & 2)
16767                 cp_parser_check_for_definition_in_return_type
16768                                             (declarator, decl_specifiers.type,
16769                                              decl_specifiers.type_location);
16770
16771               /* Look for an asm-specification.  */
16772               asm_specification = cp_parser_asm_specification_opt (parser);
16773               /* Look for attributes that apply to the declaration.  */
16774               attributes = cp_parser_attributes_opt (parser);
16775               /* Remember which attributes are prefix attributes and
16776                  which are not.  */
16777               first_attribute = attributes;
16778               /* Combine the attributes.  */
16779               attributes = chainon (prefix_attributes, attributes);
16780
16781               /* If it's an `=', then we have a constant-initializer or a
16782                  pure-specifier.  It is not correct to parse the
16783                  initializer before registering the member declaration
16784                  since the member declaration should be in scope while
16785                  its initializer is processed.  However, the rest of the
16786                  front end does not yet provide an interface that allows
16787                  us to handle this correctly.  */
16788               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16789                 {
16790                   /* In [class.mem]:
16791
16792                      A pure-specifier shall be used only in the declaration of
16793                      a virtual function.
16794
16795                      A member-declarator can contain a constant-initializer
16796                      only if it declares a static member of integral or
16797                      enumeration type.
16798
16799                      Therefore, if the DECLARATOR is for a function, we look
16800                      for a pure-specifier; otherwise, we look for a
16801                      constant-initializer.  When we call `grokfield', it will
16802                      perform more stringent semantics checks.  */
16803                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16804                   if (function_declarator_p (declarator))
16805                     initializer = cp_parser_pure_specifier (parser);
16806                   else
16807                     /* Parse the initializer.  */
16808                     initializer = cp_parser_constant_initializer (parser);
16809                 }
16810               /* Otherwise, there is no initializer.  */
16811               else
16812                 initializer = NULL_TREE;
16813
16814               /* See if we are probably looking at a function
16815                  definition.  We are certainly not looking at a
16816                  member-declarator.  Calling `grokfield' has
16817                  side-effects, so we must not do it unless we are sure
16818                  that we are looking at a member-declarator.  */
16819               if (cp_parser_token_starts_function_definition_p
16820                   (cp_lexer_peek_token (parser->lexer)))
16821                 {
16822                   /* The grammar does not allow a pure-specifier to be
16823                      used when a member function is defined.  (It is
16824                      possible that this fact is an oversight in the
16825                      standard, since a pure function may be defined
16826                      outside of the class-specifier.  */
16827                   if (initializer)
16828                     error_at (initializer_token_start->location,
16829                               "pure-specifier on function-definition");
16830                   decl = cp_parser_save_member_function_body (parser,
16831                                                               &decl_specifiers,
16832                                                               declarator,
16833                                                               attributes);
16834                   /* If the member was not a friend, declare it here.  */
16835                   if (!friend_p)
16836                     finish_member_declaration (decl);
16837                   /* Peek at the next token.  */
16838                   token = cp_lexer_peek_token (parser->lexer);
16839                   /* If the next token is a semicolon, consume it.  */
16840                   if (token->type == CPP_SEMICOLON)
16841                     cp_lexer_consume_token (parser->lexer);
16842                   return;
16843                 }
16844               else
16845                 if (declarator->kind == cdk_function)
16846                   declarator->id_loc = token->location;
16847                 /* Create the declaration.  */
16848                 decl = grokfield (declarator, &decl_specifiers,
16849                                   initializer, /*init_const_expr_p=*/true,
16850                                   asm_specification,
16851                                   attributes);
16852             }
16853
16854           /* Reset PREFIX_ATTRIBUTES.  */
16855           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16856             attributes = TREE_CHAIN (attributes);
16857           if (attributes)
16858             TREE_CHAIN (attributes) = NULL_TREE;
16859
16860           /* If there is any qualification still in effect, clear it
16861              now; we will be starting fresh with the next declarator.  */
16862           parser->scope = NULL_TREE;
16863           parser->qualifying_scope = NULL_TREE;
16864           parser->object_scope = NULL_TREE;
16865           /* If it's a `,', then there are more declarators.  */
16866           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16867             cp_lexer_consume_token (parser->lexer);
16868           /* If the next token isn't a `;', then we have a parse error.  */
16869           else if (cp_lexer_next_token_is_not (parser->lexer,
16870                                                CPP_SEMICOLON))
16871             {
16872               cp_parser_error (parser, "expected %<;%>");
16873               /* Skip tokens until we find a `;'.  */
16874               cp_parser_skip_to_end_of_statement (parser);
16875
16876               break;
16877             }
16878
16879           if (decl)
16880             {
16881               /* Add DECL to the list of members.  */
16882               if (!friend_p)
16883                 finish_member_declaration (decl);
16884
16885               if (TREE_CODE (decl) == FUNCTION_DECL)
16886                 cp_parser_save_default_args (parser, decl);
16887             }
16888         }
16889     }
16890
16891   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16892 }
16893
16894 /* Parse a pure-specifier.
16895
16896    pure-specifier:
16897      = 0
16898
16899    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16900    Otherwise, ERROR_MARK_NODE is returned.  */
16901
16902 static tree
16903 cp_parser_pure_specifier (cp_parser* parser)
16904 {
16905   cp_token *token;
16906
16907   /* Look for the `=' token.  */
16908   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16909     return error_mark_node;
16910   /* Look for the `0' token.  */
16911   token = cp_lexer_peek_token (parser->lexer);
16912
16913   if (token->type == CPP_EOF
16914       || token->type == CPP_PRAGMA_EOL)
16915     return error_mark_node;
16916
16917   cp_lexer_consume_token (parser->lexer);
16918
16919   /* Accept = default or = delete in c++0x mode.  */
16920   if (token->keyword == RID_DEFAULT
16921       || token->keyword == RID_DELETE)
16922     {
16923       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
16924       return token->u.value;
16925     }
16926
16927   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16928   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16929     {
16930       cp_parser_error (parser,
16931                        "invalid pure specifier (only %<= 0%> is allowed)");
16932       cp_parser_skip_to_end_of_statement (parser);
16933       return error_mark_node;
16934     }
16935   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16936     {
16937       error_at (token->location, "templates may not be %<virtual%>");
16938       return error_mark_node;
16939     }
16940
16941   return integer_zero_node;
16942 }
16943
16944 /* Parse a constant-initializer.
16945
16946    constant-initializer:
16947      = constant-expression
16948
16949    Returns a representation of the constant-expression.  */
16950
16951 static tree
16952 cp_parser_constant_initializer (cp_parser* parser)
16953 {
16954   /* Look for the `=' token.  */
16955   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16956     return error_mark_node;
16957
16958   /* It is invalid to write:
16959
16960        struct S { static const int i = { 7 }; };
16961
16962      */
16963   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16964     {
16965       cp_parser_error (parser,
16966                        "a brace-enclosed initializer is not allowed here");
16967       /* Consume the opening brace.  */
16968       cp_lexer_consume_token (parser->lexer);
16969       /* Skip the initializer.  */
16970       cp_parser_skip_to_closing_brace (parser);
16971       /* Look for the trailing `}'.  */
16972       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16973
16974       return error_mark_node;
16975     }
16976
16977   return cp_parser_constant_expression (parser,
16978                                         /*allow_non_constant=*/false,
16979                                         NULL);
16980 }
16981
16982 /* Derived classes [gram.class.derived] */
16983
16984 /* Parse a base-clause.
16985
16986    base-clause:
16987      : base-specifier-list
16988
16989    base-specifier-list:
16990      base-specifier ... [opt]
16991      base-specifier-list , base-specifier ... [opt]
16992
16993    Returns a TREE_LIST representing the base-classes, in the order in
16994    which they were declared.  The representation of each node is as
16995    described by cp_parser_base_specifier.
16996
16997    In the case that no bases are specified, this function will return
16998    NULL_TREE, not ERROR_MARK_NODE.  */
16999
17000 static tree
17001 cp_parser_base_clause (cp_parser* parser)
17002 {
17003   tree bases = NULL_TREE;
17004
17005   /* Look for the `:' that begins the list.  */
17006   cp_parser_require (parser, CPP_COLON, "%<:%>");
17007
17008   /* Scan the base-specifier-list.  */
17009   while (true)
17010     {
17011       cp_token *token;
17012       tree base;
17013       bool pack_expansion_p = false;
17014
17015       /* Look for the base-specifier.  */
17016       base = cp_parser_base_specifier (parser);
17017       /* Look for the (optional) ellipsis. */
17018       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17019         {
17020           /* Consume the `...'. */
17021           cp_lexer_consume_token (parser->lexer);
17022
17023           pack_expansion_p = true;
17024         }
17025
17026       /* Add BASE to the front of the list.  */
17027       if (base != error_mark_node)
17028         {
17029           if (pack_expansion_p)
17030             /* Make this a pack expansion type. */
17031             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
17032           
17033
17034           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
17035             {
17036               TREE_CHAIN (base) = bases;
17037               bases = base;
17038             }
17039         }
17040       /* Peek at the next token.  */
17041       token = cp_lexer_peek_token (parser->lexer);
17042       /* If it's not a comma, then the list is complete.  */
17043       if (token->type != CPP_COMMA)
17044         break;
17045       /* Consume the `,'.  */
17046       cp_lexer_consume_token (parser->lexer);
17047     }
17048
17049   /* PARSER->SCOPE may still be non-NULL at this point, if the last
17050      base class had a qualified name.  However, the next name that
17051      appears is certainly not qualified.  */
17052   parser->scope = NULL_TREE;
17053   parser->qualifying_scope = NULL_TREE;
17054   parser->object_scope = NULL_TREE;
17055
17056   return nreverse (bases);
17057 }
17058
17059 /* Parse a base-specifier.
17060
17061    base-specifier:
17062      :: [opt] nested-name-specifier [opt] class-name
17063      virtual access-specifier [opt] :: [opt] nested-name-specifier
17064        [opt] class-name
17065      access-specifier virtual [opt] :: [opt] nested-name-specifier
17066        [opt] class-name
17067
17068    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
17069    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
17070    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
17071    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
17072
17073 static tree
17074 cp_parser_base_specifier (cp_parser* parser)
17075 {
17076   cp_token *token;
17077   bool done = false;
17078   bool virtual_p = false;
17079   bool duplicate_virtual_error_issued_p = false;
17080   bool duplicate_access_error_issued_p = false;
17081   bool class_scope_p, template_p;
17082   tree access = access_default_node;
17083   tree type;
17084
17085   /* Process the optional `virtual' and `access-specifier'.  */
17086   while (!done)
17087     {
17088       /* Peek at the next token.  */
17089       token = cp_lexer_peek_token (parser->lexer);
17090       /* Process `virtual'.  */
17091       switch (token->keyword)
17092         {
17093         case RID_VIRTUAL:
17094           /* If `virtual' appears more than once, issue an error.  */
17095           if (virtual_p && !duplicate_virtual_error_issued_p)
17096             {
17097               cp_parser_error (parser,
17098                                "%<virtual%> specified more than once in base-specified");
17099               duplicate_virtual_error_issued_p = true;
17100             }
17101
17102           virtual_p = true;
17103
17104           /* Consume the `virtual' token.  */
17105           cp_lexer_consume_token (parser->lexer);
17106
17107           break;
17108
17109         case RID_PUBLIC:
17110         case RID_PROTECTED:
17111         case RID_PRIVATE:
17112           /* If more than one access specifier appears, issue an
17113              error.  */
17114           if (access != access_default_node
17115               && !duplicate_access_error_issued_p)
17116             {
17117               cp_parser_error (parser,
17118                                "more than one access specifier in base-specified");
17119               duplicate_access_error_issued_p = true;
17120             }
17121
17122           access = ridpointers[(int) token->keyword];
17123
17124           /* Consume the access-specifier.  */
17125           cp_lexer_consume_token (parser->lexer);
17126
17127           break;
17128
17129         default:
17130           done = true;
17131           break;
17132         }
17133     }
17134   /* It is not uncommon to see programs mechanically, erroneously, use
17135      the 'typename' keyword to denote (dependent) qualified types
17136      as base classes.  */
17137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17138     {
17139       token = cp_lexer_peek_token (parser->lexer);
17140       if (!processing_template_decl)
17141         error_at (token->location,
17142                   "keyword %<typename%> not allowed outside of templates");
17143       else
17144         error_at (token->location,
17145                   "keyword %<typename%> not allowed in this context "
17146                   "(the base class is implicitly a type)");
17147       cp_lexer_consume_token (parser->lexer);
17148     }
17149
17150   /* Look for the optional `::' operator.  */
17151   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17152   /* Look for the nested-name-specifier.  The simplest way to
17153      implement:
17154
17155        [temp.res]
17156
17157        The keyword `typename' is not permitted in a base-specifier or
17158        mem-initializer; in these contexts a qualified name that
17159        depends on a template-parameter is implicitly assumed to be a
17160        type name.
17161
17162      is to pretend that we have seen the `typename' keyword at this
17163      point.  */
17164   cp_parser_nested_name_specifier_opt (parser,
17165                                        /*typename_keyword_p=*/true,
17166                                        /*check_dependency_p=*/true,
17167                                        typename_type,
17168                                        /*is_declaration=*/true);
17169   /* If the base class is given by a qualified name, assume that names
17170      we see are type names or templates, as appropriate.  */
17171   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17172   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17173
17174   /* Finally, look for the class-name.  */
17175   type = cp_parser_class_name (parser,
17176                                class_scope_p,
17177                                template_p,
17178                                typename_type,
17179                                /*check_dependency_p=*/true,
17180                                /*class_head_p=*/false,
17181                                /*is_declaration=*/true);
17182
17183   if (type == error_mark_node)
17184     return error_mark_node;
17185
17186   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17187 }
17188
17189 /* Exception handling [gram.exception] */
17190
17191 /* Parse an (optional) exception-specification.
17192
17193    exception-specification:
17194      throw ( type-id-list [opt] )
17195
17196    Returns a TREE_LIST representing the exception-specification.  The
17197    TREE_VALUE of each node is a type.  */
17198
17199 static tree
17200 cp_parser_exception_specification_opt (cp_parser* parser)
17201 {
17202   cp_token *token;
17203   tree type_id_list;
17204
17205   /* Peek at the next token.  */
17206   token = cp_lexer_peek_token (parser->lexer);
17207   /* If it's not `throw', then there's no exception-specification.  */
17208   if (!cp_parser_is_keyword (token, RID_THROW))
17209     return NULL_TREE;
17210
17211   /* Consume the `throw'.  */
17212   cp_lexer_consume_token (parser->lexer);
17213
17214   /* Look for the `('.  */
17215   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17216
17217   /* Peek at the next token.  */
17218   token = cp_lexer_peek_token (parser->lexer);
17219   /* If it's not a `)', then there is a type-id-list.  */
17220   if (token->type != CPP_CLOSE_PAREN)
17221     {
17222       const char *saved_message;
17223
17224       /* Types may not be defined in an exception-specification.  */
17225       saved_message = parser->type_definition_forbidden_message;
17226       parser->type_definition_forbidden_message
17227         = G_("types may not be defined in an exception-specification");
17228       /* Parse the type-id-list.  */
17229       type_id_list = cp_parser_type_id_list (parser);
17230       /* Restore the saved message.  */
17231       parser->type_definition_forbidden_message = saved_message;
17232     }
17233   else
17234     type_id_list = empty_except_spec;
17235
17236   /* Look for the `)'.  */
17237   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17238
17239   return type_id_list;
17240 }
17241
17242 /* Parse an (optional) type-id-list.
17243
17244    type-id-list:
17245      type-id ... [opt]
17246      type-id-list , type-id ... [opt]
17247
17248    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17249    in the order that the types were presented.  */
17250
17251 static tree
17252 cp_parser_type_id_list (cp_parser* parser)
17253 {
17254   tree types = NULL_TREE;
17255
17256   while (true)
17257     {
17258       cp_token *token;
17259       tree type;
17260
17261       /* Get the next type-id.  */
17262       type = cp_parser_type_id (parser);
17263       /* Parse the optional ellipsis. */
17264       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17265         {
17266           /* Consume the `...'. */
17267           cp_lexer_consume_token (parser->lexer);
17268
17269           /* Turn the type into a pack expansion expression. */
17270           type = make_pack_expansion (type);
17271         }
17272       /* Add it to the list.  */
17273       types = add_exception_specifier (types, type, /*complain=*/1);
17274       /* Peek at the next token.  */
17275       token = cp_lexer_peek_token (parser->lexer);
17276       /* If it is not a `,', we are done.  */
17277       if (token->type != CPP_COMMA)
17278         break;
17279       /* Consume the `,'.  */
17280       cp_lexer_consume_token (parser->lexer);
17281     }
17282
17283   return nreverse (types);
17284 }
17285
17286 /* Parse a try-block.
17287
17288    try-block:
17289      try compound-statement handler-seq  */
17290
17291 static tree
17292 cp_parser_try_block (cp_parser* parser)
17293 {
17294   tree try_block;
17295
17296   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17297   try_block = begin_try_block ();
17298   cp_parser_compound_statement (parser, NULL, true);
17299   finish_try_block (try_block);
17300   cp_parser_handler_seq (parser);
17301   finish_handler_sequence (try_block);
17302
17303   return try_block;
17304 }
17305
17306 /* Parse a function-try-block.
17307
17308    function-try-block:
17309      try ctor-initializer [opt] function-body handler-seq  */
17310
17311 static bool
17312 cp_parser_function_try_block (cp_parser* parser)
17313 {
17314   tree compound_stmt;
17315   tree try_block;
17316   bool ctor_initializer_p;
17317
17318   /* Look for the `try' keyword.  */
17319   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17320     return false;
17321   /* Let the rest of the front end know where we are.  */
17322   try_block = begin_function_try_block (&compound_stmt);
17323   /* Parse the function-body.  */
17324   ctor_initializer_p
17325     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17326   /* We're done with the `try' part.  */
17327   finish_function_try_block (try_block);
17328   /* Parse the handlers.  */
17329   cp_parser_handler_seq (parser);
17330   /* We're done with the handlers.  */
17331   finish_function_handler_sequence (try_block, compound_stmt);
17332
17333   return ctor_initializer_p;
17334 }
17335
17336 /* Parse a handler-seq.
17337
17338    handler-seq:
17339      handler handler-seq [opt]  */
17340
17341 static void
17342 cp_parser_handler_seq (cp_parser* parser)
17343 {
17344   while (true)
17345     {
17346       cp_token *token;
17347
17348       /* Parse the handler.  */
17349       cp_parser_handler (parser);
17350       /* Peek at the next token.  */
17351       token = cp_lexer_peek_token (parser->lexer);
17352       /* If it's not `catch' then there are no more handlers.  */
17353       if (!cp_parser_is_keyword (token, RID_CATCH))
17354         break;
17355     }
17356 }
17357
17358 /* Parse a handler.
17359
17360    handler:
17361      catch ( exception-declaration ) compound-statement  */
17362
17363 static void
17364 cp_parser_handler (cp_parser* parser)
17365 {
17366   tree handler;
17367   tree declaration;
17368
17369   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17370   handler = begin_handler ();
17371   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17372   declaration = cp_parser_exception_declaration (parser);
17373   finish_handler_parms (declaration, handler);
17374   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17375   cp_parser_compound_statement (parser, NULL, false);
17376   finish_handler (handler);
17377 }
17378
17379 /* Parse an exception-declaration.
17380
17381    exception-declaration:
17382      type-specifier-seq declarator
17383      type-specifier-seq abstract-declarator
17384      type-specifier-seq
17385      ...
17386
17387    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17388    ellipsis variant is used.  */
17389
17390 static tree
17391 cp_parser_exception_declaration (cp_parser* parser)
17392 {
17393   cp_decl_specifier_seq type_specifiers;
17394   cp_declarator *declarator;
17395   const char *saved_message;
17396
17397   /* If it's an ellipsis, it's easy to handle.  */
17398   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17399     {
17400       /* Consume the `...' token.  */
17401       cp_lexer_consume_token (parser->lexer);
17402       return NULL_TREE;
17403     }
17404
17405   /* Types may not be defined in exception-declarations.  */
17406   saved_message = parser->type_definition_forbidden_message;
17407   parser->type_definition_forbidden_message
17408     = G_("types may not be defined in exception-declarations");
17409
17410   /* Parse the type-specifier-seq.  */
17411   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
17412                                 /*is_trailing_return=*/false,
17413                                 &type_specifiers);
17414   /* If it's a `)', then there is no declarator.  */
17415   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17416     declarator = NULL;
17417   else
17418     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17419                                        /*ctor_dtor_or_conv_p=*/NULL,
17420                                        /*parenthesized_p=*/NULL,
17421                                        /*member_p=*/false);
17422
17423   /* Restore the saved message.  */
17424   parser->type_definition_forbidden_message = saved_message;
17425
17426   if (!type_specifiers.any_specifiers_p)
17427     return error_mark_node;
17428
17429   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17430 }
17431
17432 /* Parse a throw-expression.
17433
17434    throw-expression:
17435      throw assignment-expression [opt]
17436
17437    Returns a THROW_EXPR representing the throw-expression.  */
17438
17439 static tree
17440 cp_parser_throw_expression (cp_parser* parser)
17441 {
17442   tree expression;
17443   cp_token* token;
17444
17445   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17446   token = cp_lexer_peek_token (parser->lexer);
17447   /* Figure out whether or not there is an assignment-expression
17448      following the "throw" keyword.  */
17449   if (token->type == CPP_COMMA
17450       || token->type == CPP_SEMICOLON
17451       || token->type == CPP_CLOSE_PAREN
17452       || token->type == CPP_CLOSE_SQUARE
17453       || token->type == CPP_CLOSE_BRACE
17454       || token->type == CPP_COLON)
17455     expression = NULL_TREE;
17456   else
17457     expression = cp_parser_assignment_expression (parser,
17458                                                   /*cast_p=*/false, NULL);
17459
17460   return build_throw (expression);
17461 }
17462
17463 /* GNU Extensions */
17464
17465 /* Parse an (optional) asm-specification.
17466
17467    asm-specification:
17468      asm ( string-literal )
17469
17470    If the asm-specification is present, returns a STRING_CST
17471    corresponding to the string-literal.  Otherwise, returns
17472    NULL_TREE.  */
17473
17474 static tree
17475 cp_parser_asm_specification_opt (cp_parser* parser)
17476 {
17477   cp_token *token;
17478   tree asm_specification;
17479
17480   /* Peek at the next token.  */
17481   token = cp_lexer_peek_token (parser->lexer);
17482   /* If the next token isn't the `asm' keyword, then there's no
17483      asm-specification.  */
17484   if (!cp_parser_is_keyword (token, RID_ASM))
17485     return NULL_TREE;
17486
17487   /* Consume the `asm' token.  */
17488   cp_lexer_consume_token (parser->lexer);
17489   /* Look for the `('.  */
17490   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17491
17492   /* Look for the string-literal.  */
17493   asm_specification = cp_parser_string_literal (parser, false, false);
17494
17495   /* Look for the `)'.  */
17496   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17497
17498   return asm_specification;
17499 }
17500
17501 /* Parse an asm-operand-list.
17502
17503    asm-operand-list:
17504      asm-operand
17505      asm-operand-list , asm-operand
17506
17507    asm-operand:
17508      string-literal ( expression )
17509      [ string-literal ] string-literal ( expression )
17510
17511    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17512    each node is the expression.  The TREE_PURPOSE is itself a
17513    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17514    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17515    is a STRING_CST for the string literal before the parenthesis. Returns
17516    ERROR_MARK_NODE if any of the operands are invalid.  */
17517
17518 static tree
17519 cp_parser_asm_operand_list (cp_parser* parser)
17520 {
17521   tree asm_operands = NULL_TREE;
17522   bool invalid_operands = false;
17523
17524   while (true)
17525     {
17526       tree string_literal;
17527       tree expression;
17528       tree name;
17529
17530       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17531         {
17532           /* Consume the `[' token.  */
17533           cp_lexer_consume_token (parser->lexer);
17534           /* Read the operand name.  */
17535           name = cp_parser_identifier (parser);
17536           if (name != error_mark_node)
17537             name = build_string (IDENTIFIER_LENGTH (name),
17538                                  IDENTIFIER_POINTER (name));
17539           /* Look for the closing `]'.  */
17540           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17541         }
17542       else
17543         name = NULL_TREE;
17544       /* Look for the string-literal.  */
17545       string_literal = cp_parser_string_literal (parser, false, false);
17546
17547       /* Look for the `('.  */
17548       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17549       /* Parse the expression.  */
17550       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17551       /* Look for the `)'.  */
17552       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17553
17554       if (name == error_mark_node 
17555           || string_literal == error_mark_node 
17556           || expression == error_mark_node)
17557         invalid_operands = true;
17558
17559       /* Add this operand to the list.  */
17560       asm_operands = tree_cons (build_tree_list (name, string_literal),
17561                                 expression,
17562                                 asm_operands);
17563       /* If the next token is not a `,', there are no more
17564          operands.  */
17565       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17566         break;
17567       /* Consume the `,'.  */
17568       cp_lexer_consume_token (parser->lexer);
17569     }
17570
17571   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17572 }
17573
17574 /* Parse an asm-clobber-list.
17575
17576    asm-clobber-list:
17577      string-literal
17578      asm-clobber-list , string-literal
17579
17580    Returns a TREE_LIST, indicating the clobbers in the order that they
17581    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17582
17583 static tree
17584 cp_parser_asm_clobber_list (cp_parser* parser)
17585 {
17586   tree clobbers = NULL_TREE;
17587
17588   while (true)
17589     {
17590       tree string_literal;
17591
17592       /* Look for the string literal.  */
17593       string_literal = cp_parser_string_literal (parser, false, false);
17594       /* Add it to the list.  */
17595       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17596       /* If the next token is not a `,', then the list is
17597          complete.  */
17598       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17599         break;
17600       /* Consume the `,' token.  */
17601       cp_lexer_consume_token (parser->lexer);
17602     }
17603
17604   return clobbers;
17605 }
17606
17607 /* Parse an asm-label-list.
17608
17609    asm-label-list:
17610      identifier
17611      asm-label-list , identifier
17612
17613    Returns a TREE_LIST, indicating the labels in the order that they
17614    appeared.  The TREE_VALUE of each node is a label.  */
17615
17616 static tree
17617 cp_parser_asm_label_list (cp_parser* parser)
17618 {
17619   tree labels = NULL_TREE;
17620
17621   while (true)
17622     {
17623       tree identifier, label, name;
17624
17625       /* Look for the identifier.  */
17626       identifier = cp_parser_identifier (parser);
17627       if (!error_operand_p (identifier))
17628         {
17629           label = lookup_label (identifier);
17630           if (TREE_CODE (label) == LABEL_DECL)
17631             {
17632               TREE_USED (label) = 1;
17633               check_goto (label);
17634               name = build_string (IDENTIFIER_LENGTH (identifier),
17635                                    IDENTIFIER_POINTER (identifier));
17636               labels = tree_cons (name, label, labels);
17637             }
17638         }
17639       /* If the next token is not a `,', then the list is
17640          complete.  */
17641       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17642         break;
17643       /* Consume the `,' token.  */
17644       cp_lexer_consume_token (parser->lexer);
17645     }
17646
17647   return nreverse (labels);
17648 }
17649
17650 /* Parse an (optional) series of attributes.
17651
17652    attributes:
17653      attributes attribute
17654
17655    attribute:
17656      __attribute__ (( attribute-list [opt] ))
17657
17658    The return value is as for cp_parser_attribute_list.  */
17659
17660 static tree
17661 cp_parser_attributes_opt (cp_parser* parser)
17662 {
17663   tree attributes = NULL_TREE;
17664
17665   while (true)
17666     {
17667       cp_token *token;
17668       tree attribute_list;
17669
17670       /* Peek at the next token.  */
17671       token = cp_lexer_peek_token (parser->lexer);
17672       /* If it's not `__attribute__', then we're done.  */
17673       if (token->keyword != RID_ATTRIBUTE)
17674         break;
17675
17676       /* Consume the `__attribute__' keyword.  */
17677       cp_lexer_consume_token (parser->lexer);
17678       /* Look for the two `(' tokens.  */
17679       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17680       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17681
17682       /* Peek at the next token.  */
17683       token = cp_lexer_peek_token (parser->lexer);
17684       if (token->type != CPP_CLOSE_PAREN)
17685         /* Parse the attribute-list.  */
17686         attribute_list = cp_parser_attribute_list (parser);
17687       else
17688         /* If the next token is a `)', then there is no attribute
17689            list.  */
17690         attribute_list = NULL;
17691
17692       /* Look for the two `)' tokens.  */
17693       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17694       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17695
17696       /* Add these new attributes to the list.  */
17697       attributes = chainon (attributes, attribute_list);
17698     }
17699
17700   return attributes;
17701 }
17702
17703 /* Parse an attribute-list.
17704
17705    attribute-list:
17706      attribute
17707      attribute-list , attribute
17708
17709    attribute:
17710      identifier
17711      identifier ( identifier )
17712      identifier ( identifier , expression-list )
17713      identifier ( expression-list )
17714
17715    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17716    to an attribute.  The TREE_PURPOSE of each node is the identifier
17717    indicating which attribute is in use.  The TREE_VALUE represents
17718    the arguments, if any.  */
17719
17720 static tree
17721 cp_parser_attribute_list (cp_parser* parser)
17722 {
17723   tree attribute_list = NULL_TREE;
17724   bool save_translate_strings_p = parser->translate_strings_p;
17725
17726   parser->translate_strings_p = false;
17727   while (true)
17728     {
17729       cp_token *token;
17730       tree identifier;
17731       tree attribute;
17732
17733       /* Look for the identifier.  We also allow keywords here; for
17734          example `__attribute__ ((const))' is legal.  */
17735       token = cp_lexer_peek_token (parser->lexer);
17736       if (token->type == CPP_NAME
17737           || token->type == CPP_KEYWORD)
17738         {
17739           tree arguments = NULL_TREE;
17740
17741           /* Consume the token.  */
17742           token = cp_lexer_consume_token (parser->lexer);
17743
17744           /* Save away the identifier that indicates which attribute
17745              this is.  */
17746           identifier = (token->type == CPP_KEYWORD) 
17747             /* For keywords, use the canonical spelling, not the
17748                parsed identifier.  */
17749             ? ridpointers[(int) token->keyword]
17750             : token->u.value;
17751           
17752           attribute = build_tree_list (identifier, NULL_TREE);
17753
17754           /* Peek at the next token.  */
17755           token = cp_lexer_peek_token (parser->lexer);
17756           /* If it's an `(', then parse the attribute arguments.  */
17757           if (token->type == CPP_OPEN_PAREN)
17758             {
17759               VEC(tree,gc) *vec;
17760               vec = cp_parser_parenthesized_expression_list
17761                     (parser, true, /*cast_p=*/false,
17762                      /*allow_expansion_p=*/false,
17763                      /*non_constant_p=*/NULL);
17764               if (vec == NULL)
17765                 arguments = error_mark_node;
17766               else
17767                 {
17768                   arguments = build_tree_list_vec (vec);
17769                   release_tree_vector (vec);
17770                 }
17771               /* Save the arguments away.  */
17772               TREE_VALUE (attribute) = arguments;
17773             }
17774
17775           if (arguments != error_mark_node)
17776             {
17777               /* Add this attribute to the list.  */
17778               TREE_CHAIN (attribute) = attribute_list;
17779               attribute_list = attribute;
17780             }
17781
17782           token = cp_lexer_peek_token (parser->lexer);
17783         }
17784       /* Now, look for more attributes.  If the next token isn't a
17785          `,', we're done.  */
17786       if (token->type != CPP_COMMA)
17787         break;
17788
17789       /* Consume the comma and keep going.  */
17790       cp_lexer_consume_token (parser->lexer);
17791     }
17792   parser->translate_strings_p = save_translate_strings_p;
17793
17794   /* We built up the list in reverse order.  */
17795   return nreverse (attribute_list);
17796 }
17797
17798 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17799    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17800    current value of the PEDANTIC flag, regardless of whether or not
17801    the `__extension__' keyword is present.  The caller is responsible
17802    for restoring the value of the PEDANTIC flag.  */
17803
17804 static bool
17805 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17806 {
17807   /* Save the old value of the PEDANTIC flag.  */
17808   *saved_pedantic = pedantic;
17809
17810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17811     {
17812       /* Consume the `__extension__' token.  */
17813       cp_lexer_consume_token (parser->lexer);
17814       /* We're not being pedantic while the `__extension__' keyword is
17815          in effect.  */
17816       pedantic = 0;
17817
17818       return true;
17819     }
17820
17821   return false;
17822 }
17823
17824 /* Parse a label declaration.
17825
17826    label-declaration:
17827      __label__ label-declarator-seq ;
17828
17829    label-declarator-seq:
17830      identifier , label-declarator-seq
17831      identifier  */
17832
17833 static void
17834 cp_parser_label_declaration (cp_parser* parser)
17835 {
17836   /* Look for the `__label__' keyword.  */
17837   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17838
17839   while (true)
17840     {
17841       tree identifier;
17842
17843       /* Look for an identifier.  */
17844       identifier = cp_parser_identifier (parser);
17845       /* If we failed, stop.  */
17846       if (identifier == error_mark_node)
17847         break;
17848       /* Declare it as a label.  */
17849       finish_label_decl (identifier);
17850       /* If the next token is a `;', stop.  */
17851       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17852         break;
17853       /* Look for the `,' separating the label declarations.  */
17854       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17855     }
17856
17857   /* Look for the final `;'.  */
17858   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17859 }
17860
17861 /* Support Functions */
17862
17863 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17864    NAME should have one of the representations used for an
17865    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17866    is returned.  If PARSER->SCOPE is a dependent type, then a
17867    SCOPE_REF is returned.
17868
17869    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17870    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17871    was formed.  Abstractly, such entities should not be passed to this
17872    function, because they do not need to be looked up, but it is
17873    simpler to check for this special case here, rather than at the
17874    call-sites.
17875
17876    In cases not explicitly covered above, this function returns a
17877    DECL, OVERLOAD, or baselink representing the result of the lookup.
17878    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17879    is returned.
17880
17881    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17882    (e.g., "struct") that was used.  In that case bindings that do not
17883    refer to types are ignored.
17884
17885    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17886    ignored.
17887
17888    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17889    are ignored.
17890
17891    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17892    types.
17893
17894    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17895    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17896    NULL_TREE otherwise.  */
17897
17898 static tree
17899 cp_parser_lookup_name (cp_parser *parser, tree name,
17900                        enum tag_types tag_type,
17901                        bool is_template,
17902                        bool is_namespace,
17903                        bool check_dependency,
17904                        tree *ambiguous_decls,
17905                        location_t name_location)
17906 {
17907   int flags = 0;
17908   tree decl;
17909   tree object_type = parser->context->object_type;
17910
17911   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17912     flags |= LOOKUP_COMPLAIN;
17913
17914   /* Assume that the lookup will be unambiguous.  */
17915   if (ambiguous_decls)
17916     *ambiguous_decls = NULL_TREE;
17917
17918   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17919      no longer valid.  Note that if we are parsing tentatively, and
17920      the parse fails, OBJECT_TYPE will be automatically restored.  */
17921   parser->context->object_type = NULL_TREE;
17922
17923   if (name == error_mark_node)
17924     return error_mark_node;
17925
17926   /* A template-id has already been resolved; there is no lookup to
17927      do.  */
17928   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17929     return name;
17930   if (BASELINK_P (name))
17931     {
17932       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17933                   == TEMPLATE_ID_EXPR);
17934       return name;
17935     }
17936
17937   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17938      it should already have been checked to make sure that the name
17939      used matches the type being destroyed.  */
17940   if (TREE_CODE (name) == BIT_NOT_EXPR)
17941     {
17942       tree type;
17943
17944       /* Figure out to which type this destructor applies.  */
17945       if (parser->scope)
17946         type = parser->scope;
17947       else if (object_type)
17948         type = object_type;
17949       else
17950         type = current_class_type;
17951       /* If that's not a class type, there is no destructor.  */
17952       if (!type || !CLASS_TYPE_P (type))
17953         return error_mark_node;
17954       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17955         lazily_declare_fn (sfk_destructor, type);
17956       if (!CLASSTYPE_DESTRUCTORS (type))
17957           return error_mark_node;
17958       /* If it was a class type, return the destructor.  */
17959       return CLASSTYPE_DESTRUCTORS (type);
17960     }
17961
17962   /* By this point, the NAME should be an ordinary identifier.  If
17963      the id-expression was a qualified name, the qualifying scope is
17964      stored in PARSER->SCOPE at this point.  */
17965   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17966
17967   /* Perform the lookup.  */
17968   if (parser->scope)
17969     {
17970       bool dependent_p;
17971
17972       if (parser->scope == error_mark_node)
17973         return error_mark_node;
17974
17975       /* If the SCOPE is dependent, the lookup must be deferred until
17976          the template is instantiated -- unless we are explicitly
17977          looking up names in uninstantiated templates.  Even then, we
17978          cannot look up the name if the scope is not a class type; it
17979          might, for example, be a template type parameter.  */
17980       dependent_p = (TYPE_P (parser->scope)
17981                      && dependent_scope_p (parser->scope));
17982       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17983           && dependent_p)
17984         /* Defer lookup.  */
17985         decl = error_mark_node;
17986       else
17987         {
17988           tree pushed_scope = NULL_TREE;
17989
17990           /* If PARSER->SCOPE is a dependent type, then it must be a
17991              class type, and we must not be checking dependencies;
17992              otherwise, we would have processed this lookup above.  So
17993              that PARSER->SCOPE is not considered a dependent base by
17994              lookup_member, we must enter the scope here.  */
17995           if (dependent_p)
17996             pushed_scope = push_scope (parser->scope);
17997
17998           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
17999              lookup result and the nested-name-specifier nominates a class C:
18000                * if the name specified after the nested-name-specifier, when
18001                looked up in C, is the injected-class-name of C (Clause 9), or
18002                * if the name specified after the nested-name-specifier is the
18003                same as the identifier or the simple-template-id's template-
18004                name in the last component of the nested-name-specifier,
18005              the name is instead considered to name the constructor of
18006              class C. [ Note: for example, the constructor is not an
18007              acceptable lookup result in an elaborated-type-specifier so
18008              the constructor would not be used in place of the
18009              injected-class-name. --end note ] Such a constructor name
18010              shall be used only in the declarator-id of a declaration that
18011              names a constructor or in a using-declaration.  */
18012           if (tag_type == none_type
18013               && CLASS_TYPE_P (parser->scope)
18014               && constructor_name_p (name, parser->scope))
18015             name = ctor_identifier;
18016
18017           /* If the PARSER->SCOPE is a template specialization, it
18018              may be instantiated during name lookup.  In that case,
18019              errors may be issued.  Even if we rollback the current
18020              tentative parse, those errors are valid.  */
18021           decl = lookup_qualified_name (parser->scope, name,
18022                                         tag_type != none_type,
18023                                         /*complain=*/true);
18024
18025           /* If we have a single function from a using decl, pull it out.  */
18026           if (TREE_CODE (decl) == OVERLOAD
18027               && !really_overloaded_fn (decl))
18028             decl = OVL_FUNCTION (decl);
18029
18030           if (pushed_scope)
18031             pop_scope (pushed_scope);
18032         }
18033
18034       /* If the scope is a dependent type and either we deferred lookup or
18035          we did lookup but didn't find the name, rememeber the name.  */
18036       if (decl == error_mark_node && TYPE_P (parser->scope)
18037           && dependent_type_p (parser->scope))
18038         {
18039           if (tag_type)
18040             {
18041               tree type;
18042
18043               /* The resolution to Core Issue 180 says that `struct
18044                  A::B' should be considered a type-name, even if `A'
18045                  is dependent.  */
18046               type = make_typename_type (parser->scope, name, tag_type,
18047                                          /*complain=*/tf_error);
18048               decl = TYPE_NAME (type);
18049             }
18050           else if (is_template
18051                    && (cp_parser_next_token_ends_template_argument_p (parser)
18052                        || cp_lexer_next_token_is (parser->lexer,
18053                                                   CPP_CLOSE_PAREN)))
18054             decl = make_unbound_class_template (parser->scope,
18055                                                 name, NULL_TREE,
18056                                                 /*complain=*/tf_error);
18057           else
18058             decl = build_qualified_name (/*type=*/NULL_TREE,
18059                                          parser->scope, name,
18060                                          is_template);
18061         }
18062       parser->qualifying_scope = parser->scope;
18063       parser->object_scope = NULL_TREE;
18064     }
18065   else if (object_type)
18066     {
18067       tree object_decl = NULL_TREE;
18068       /* Look up the name in the scope of the OBJECT_TYPE, unless the
18069          OBJECT_TYPE is not a class.  */
18070       if (CLASS_TYPE_P (object_type))
18071         /* If the OBJECT_TYPE is a template specialization, it may
18072            be instantiated during name lookup.  In that case, errors
18073            may be issued.  Even if we rollback the current tentative
18074            parse, those errors are valid.  */
18075         object_decl = lookup_member (object_type,
18076                                      name,
18077                                      /*protect=*/0,
18078                                      tag_type != none_type);
18079       /* Look it up in the enclosing context, too.  */
18080       decl = lookup_name_real (name, tag_type != none_type,
18081                                /*nonclass=*/0,
18082                                /*block_p=*/true, is_namespace, flags);
18083       parser->object_scope = object_type;
18084       parser->qualifying_scope = NULL_TREE;
18085       if (object_decl)
18086         decl = object_decl;
18087     }
18088   else
18089     {
18090       decl = lookup_name_real (name, tag_type != none_type,
18091                                /*nonclass=*/0,
18092                                /*block_p=*/true, is_namespace, flags);
18093       parser->qualifying_scope = NULL_TREE;
18094       parser->object_scope = NULL_TREE;
18095     }
18096
18097   /* If the lookup failed, let our caller know.  */
18098   if (!decl || decl == error_mark_node)
18099     return error_mark_node;
18100
18101   /* Pull out the template from an injected-class-name (or multiple).  */
18102   if (is_template)
18103     decl = maybe_get_template_decl_from_type_decl (decl);
18104
18105   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
18106   if (TREE_CODE (decl) == TREE_LIST)
18107     {
18108       if (ambiguous_decls)
18109         *ambiguous_decls = decl;
18110       /* The error message we have to print is too complicated for
18111          cp_parser_error, so we incorporate its actions directly.  */
18112       if (!cp_parser_simulate_error (parser))
18113         {
18114           error_at (name_location, "reference to %qD is ambiguous",
18115                     name);
18116           print_candidates (decl);
18117         }
18118       return error_mark_node;
18119     }
18120
18121   gcc_assert (DECL_P (decl)
18122               || TREE_CODE (decl) == OVERLOAD
18123               || TREE_CODE (decl) == SCOPE_REF
18124               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18125               || BASELINK_P (decl));
18126
18127   /* If we have resolved the name of a member declaration, check to
18128      see if the declaration is accessible.  When the name resolves to
18129      set of overloaded functions, accessibility is checked when
18130      overload resolution is done.
18131
18132      During an explicit instantiation, access is not checked at all,
18133      as per [temp.explicit].  */
18134   if (DECL_P (decl))
18135     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18136
18137   return decl;
18138 }
18139
18140 /* Like cp_parser_lookup_name, but for use in the typical case where
18141    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18142    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18143
18144 static tree
18145 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18146 {
18147   return cp_parser_lookup_name (parser, name,
18148                                 none_type,
18149                                 /*is_template=*/false,
18150                                 /*is_namespace=*/false,
18151                                 /*check_dependency=*/true,
18152                                 /*ambiguous_decls=*/NULL,
18153                                 location);
18154 }
18155
18156 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18157    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18158    true, the DECL indicates the class being defined in a class-head,
18159    or declared in an elaborated-type-specifier.
18160
18161    Otherwise, return DECL.  */
18162
18163 static tree
18164 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18165 {
18166   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18167      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18168
18169        struct A {
18170          template <typename T> struct B;
18171        };
18172
18173        template <typename T> struct A::B {};
18174
18175      Similarly, in an elaborated-type-specifier:
18176
18177        namespace N { struct X{}; }
18178
18179        struct A {
18180          template <typename T> friend struct N::X;
18181        };
18182
18183      However, if the DECL refers to a class type, and we are in
18184      the scope of the class, then the name lookup automatically
18185      finds the TYPE_DECL created by build_self_reference rather
18186      than a TEMPLATE_DECL.  For example, in:
18187
18188        template <class T> struct S {
18189          S s;
18190        };
18191
18192      there is no need to handle such case.  */
18193
18194   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18195     return DECL_TEMPLATE_RESULT (decl);
18196
18197   return decl;
18198 }
18199
18200 /* If too many, or too few, template-parameter lists apply to the
18201    declarator, issue an error message.  Returns TRUE if all went well,
18202    and FALSE otherwise.  */
18203
18204 static bool
18205 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18206                                                 cp_declarator *declarator,
18207                                                 location_t declarator_location)
18208 {
18209   unsigned num_templates;
18210
18211   /* We haven't seen any classes that involve template parameters yet.  */
18212   num_templates = 0;
18213
18214   switch (declarator->kind)
18215     {
18216     case cdk_id:
18217       if (declarator->u.id.qualifying_scope)
18218         {
18219           tree scope;
18220
18221           scope = declarator->u.id.qualifying_scope;
18222
18223           while (scope && CLASS_TYPE_P (scope))
18224             {
18225               /* You're supposed to have one `template <...>'
18226                  for every template class, but you don't need one
18227                  for a full specialization.  For example:
18228
18229                  template <class T> struct S{};
18230                  template <> struct S<int> { void f(); };
18231                  void S<int>::f () {}
18232
18233                  is correct; there shouldn't be a `template <>' for
18234                  the definition of `S<int>::f'.  */
18235               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18236                 /* If SCOPE does not have template information of any
18237                    kind, then it is not a template, nor is it nested
18238                    within a template.  */
18239                 break;
18240               if (explicit_class_specialization_p (scope))
18241                 break;
18242               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18243                 ++num_templates;
18244
18245               scope = TYPE_CONTEXT (scope);
18246             }
18247         }
18248       else if (TREE_CODE (declarator->u.id.unqualified_name)
18249                == TEMPLATE_ID_EXPR)
18250         /* If the DECLARATOR has the form `X<y>' then it uses one
18251            additional level of template parameters.  */
18252         ++num_templates;
18253
18254       return cp_parser_check_template_parameters 
18255         (parser, num_templates, declarator_location, declarator);
18256
18257
18258     case cdk_function:
18259     case cdk_array:
18260     case cdk_pointer:
18261     case cdk_reference:
18262     case cdk_ptrmem:
18263       return (cp_parser_check_declarator_template_parameters
18264               (parser, declarator->declarator, declarator_location));
18265
18266     case cdk_error:
18267       return true;
18268
18269     default:
18270       gcc_unreachable ();
18271     }
18272   return false;
18273 }
18274
18275 /* NUM_TEMPLATES were used in the current declaration.  If that is
18276    invalid, return FALSE and issue an error messages.  Otherwise,
18277    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18278    declarator and we can print more accurate diagnostics.  */
18279
18280 static bool
18281 cp_parser_check_template_parameters (cp_parser* parser,
18282                                      unsigned num_templates,
18283                                      location_t location,
18284                                      cp_declarator *declarator)
18285 {
18286   /* If there are the same number of template classes and parameter
18287      lists, that's OK.  */
18288   if (parser->num_template_parameter_lists == num_templates)
18289     return true;
18290   /* If there are more, but only one more, then we are referring to a
18291      member template.  That's OK too.  */
18292   if (parser->num_template_parameter_lists == num_templates + 1)
18293     return true;
18294   /* If there are more template classes than parameter lists, we have
18295      something like:
18296
18297        template <class T> void S<T>::R<T>::f ();  */
18298   if (parser->num_template_parameter_lists < num_templates)
18299     {
18300       if (declarator && !current_function_decl)
18301         error_at (location, "specializing member %<%T::%E%> "
18302                   "requires %<template<>%> syntax", 
18303                   declarator->u.id.qualifying_scope,
18304                   declarator->u.id.unqualified_name);
18305       else if (declarator)
18306         error_at (location, "invalid declaration of %<%T::%E%>",
18307                   declarator->u.id.qualifying_scope,
18308                   declarator->u.id.unqualified_name);
18309       else 
18310         error_at (location, "too few template-parameter-lists");
18311       return false;
18312     }
18313   /* Otherwise, there are too many template parameter lists.  We have
18314      something like:
18315
18316      template <class T> template <class U> void S::f();  */
18317   error_at (location, "too many template-parameter-lists");
18318   return false;
18319 }
18320
18321 /* Parse an optional `::' token indicating that the following name is
18322    from the global namespace.  If so, PARSER->SCOPE is set to the
18323    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18324    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18325    Returns the new value of PARSER->SCOPE, if the `::' token is
18326    present, and NULL_TREE otherwise.  */
18327
18328 static tree
18329 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18330 {
18331   cp_token *token;
18332
18333   /* Peek at the next token.  */
18334   token = cp_lexer_peek_token (parser->lexer);
18335   /* If we're looking at a `::' token then we're starting from the
18336      global namespace, not our current location.  */
18337   if (token->type == CPP_SCOPE)
18338     {
18339       /* Consume the `::' token.  */
18340       cp_lexer_consume_token (parser->lexer);
18341       /* Set the SCOPE so that we know where to start the lookup.  */
18342       parser->scope = global_namespace;
18343       parser->qualifying_scope = global_namespace;
18344       parser->object_scope = NULL_TREE;
18345
18346       return parser->scope;
18347     }
18348   else if (!current_scope_valid_p)
18349     {
18350       parser->scope = NULL_TREE;
18351       parser->qualifying_scope = NULL_TREE;
18352       parser->object_scope = NULL_TREE;
18353     }
18354
18355   return NULL_TREE;
18356 }
18357
18358 /* Returns TRUE if the upcoming token sequence is the start of a
18359    constructor declarator.  If FRIEND_P is true, the declarator is
18360    preceded by the `friend' specifier.  */
18361
18362 static bool
18363 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18364 {
18365   bool constructor_p;
18366   tree nested_name_specifier;
18367   cp_token *next_token;
18368
18369   /* The common case is that this is not a constructor declarator, so
18370      try to avoid doing lots of work if at all possible.  It's not
18371      valid declare a constructor at function scope.  */
18372   if (parser->in_function_body)
18373     return false;
18374   /* And only certain tokens can begin a constructor declarator.  */
18375   next_token = cp_lexer_peek_token (parser->lexer);
18376   if (next_token->type != CPP_NAME
18377       && next_token->type != CPP_SCOPE
18378       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18379       && next_token->type != CPP_TEMPLATE_ID)
18380     return false;
18381
18382   /* Parse tentatively; we are going to roll back all of the tokens
18383      consumed here.  */
18384   cp_parser_parse_tentatively (parser);
18385   /* Assume that we are looking at a constructor declarator.  */
18386   constructor_p = true;
18387
18388   /* Look for the optional `::' operator.  */
18389   cp_parser_global_scope_opt (parser,
18390                               /*current_scope_valid_p=*/false);
18391   /* Look for the nested-name-specifier.  */
18392   nested_name_specifier
18393     = (cp_parser_nested_name_specifier_opt (parser,
18394                                             /*typename_keyword_p=*/false,
18395                                             /*check_dependency_p=*/false,
18396                                             /*type_p=*/false,
18397                                             /*is_declaration=*/false));
18398   /* Outside of a class-specifier, there must be a
18399      nested-name-specifier.  */
18400   if (!nested_name_specifier &&
18401       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18402        || friend_p))
18403     constructor_p = false;
18404   else if (nested_name_specifier == error_mark_node)
18405     constructor_p = false;
18406
18407   /* If we have a class scope, this is easy; DR 147 says that S::S always
18408      names the constructor, and no other qualified name could.  */
18409   if (constructor_p && nested_name_specifier
18410       && TYPE_P (nested_name_specifier))
18411     {
18412       tree id = cp_parser_unqualified_id (parser,
18413                                           /*template_keyword_p=*/false,
18414                                           /*check_dependency_p=*/false,
18415                                           /*declarator_p=*/true,
18416                                           /*optional_p=*/false);
18417       if (is_overloaded_fn (id))
18418         id = DECL_NAME (get_first_fn (id));
18419       if (!constructor_name_p (id, nested_name_specifier))
18420         constructor_p = false;
18421     }
18422   /* If we still think that this might be a constructor-declarator,
18423      look for a class-name.  */
18424   else if (constructor_p)
18425     {
18426       /* If we have:
18427
18428            template <typename T> struct S {
18429              S();
18430            };
18431
18432          we must recognize that the nested `S' names a class.  */
18433       tree type_decl;
18434       type_decl = cp_parser_class_name (parser,
18435                                         /*typename_keyword_p=*/false,
18436                                         /*template_keyword_p=*/false,
18437                                         none_type,
18438                                         /*check_dependency_p=*/false,
18439                                         /*class_head_p=*/false,
18440                                         /*is_declaration=*/false);
18441       /* If there was no class-name, then this is not a constructor.  */
18442       constructor_p = !cp_parser_error_occurred (parser);
18443
18444       /* If we're still considering a constructor, we have to see a `(',
18445          to begin the parameter-declaration-clause, followed by either a
18446          `)', an `...', or a decl-specifier.  We need to check for a
18447          type-specifier to avoid being fooled into thinking that:
18448
18449            S (f) (int);
18450
18451          is a constructor.  (It is actually a function named `f' that
18452          takes one parameter (of type `int') and returns a value of type
18453          `S'.  */
18454       if (constructor_p
18455           && !cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18456         constructor_p = false;
18457
18458       if (constructor_p
18459           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18460           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18461           /* A parameter declaration begins with a decl-specifier,
18462              which is either the "attribute" keyword, a storage class
18463              specifier, or (usually) a type-specifier.  */
18464           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18465         {
18466           tree type;
18467           tree pushed_scope = NULL_TREE;
18468           unsigned saved_num_template_parameter_lists;
18469
18470           /* Names appearing in the type-specifier should be looked up
18471              in the scope of the class.  */
18472           if (current_class_type)
18473             type = NULL_TREE;
18474           else
18475             {
18476               type = TREE_TYPE (type_decl);
18477               if (TREE_CODE (type) == TYPENAME_TYPE)
18478                 {
18479                   type = resolve_typename_type (type,
18480                                                 /*only_current_p=*/false);
18481                   if (TREE_CODE (type) == TYPENAME_TYPE)
18482                     {
18483                       cp_parser_abort_tentative_parse (parser);
18484                       return false;
18485                     }
18486                 }
18487               pushed_scope = push_scope (type);
18488             }
18489
18490           /* Inside the constructor parameter list, surrounding
18491              template-parameter-lists do not apply.  */
18492           saved_num_template_parameter_lists
18493             = parser->num_template_parameter_lists;
18494           parser->num_template_parameter_lists = 0;
18495
18496           /* Look for the type-specifier.  */
18497           cp_parser_type_specifier (parser,
18498                                     CP_PARSER_FLAGS_NONE,
18499                                     /*decl_specs=*/NULL,
18500                                     /*is_declarator=*/true,
18501                                     /*declares_class_or_enum=*/NULL,
18502                                     /*is_cv_qualifier=*/NULL);
18503
18504           parser->num_template_parameter_lists
18505             = saved_num_template_parameter_lists;
18506
18507           /* Leave the scope of the class.  */
18508           if (pushed_scope)
18509             pop_scope (pushed_scope);
18510
18511           constructor_p = !cp_parser_error_occurred (parser);
18512         }
18513     }
18514
18515   /* We did not really want to consume any tokens.  */
18516   cp_parser_abort_tentative_parse (parser);
18517
18518   return constructor_p;
18519 }
18520
18521 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18522    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18523    they must be performed once we are in the scope of the function.
18524
18525    Returns the function defined.  */
18526
18527 static tree
18528 cp_parser_function_definition_from_specifiers_and_declarator
18529   (cp_parser* parser,
18530    cp_decl_specifier_seq *decl_specifiers,
18531    tree attributes,
18532    const cp_declarator *declarator)
18533 {
18534   tree fn;
18535   bool success_p;
18536
18537   /* Begin the function-definition.  */
18538   success_p = start_function (decl_specifiers, declarator, attributes);
18539
18540   /* The things we're about to see are not directly qualified by any
18541      template headers we've seen thus far.  */
18542   reset_specialization ();
18543
18544   /* If there were names looked up in the decl-specifier-seq that we
18545      did not check, check them now.  We must wait until we are in the
18546      scope of the function to perform the checks, since the function
18547      might be a friend.  */
18548   perform_deferred_access_checks ();
18549
18550   if (!success_p)
18551     {
18552       /* Skip the entire function.  */
18553       cp_parser_skip_to_end_of_block_or_statement (parser);
18554       fn = error_mark_node;
18555     }
18556   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18557     {
18558       /* Seen already, skip it.  An error message has already been output.  */
18559       cp_parser_skip_to_end_of_block_or_statement (parser);
18560       fn = current_function_decl;
18561       current_function_decl = NULL_TREE;
18562       /* If this is a function from a class, pop the nested class.  */
18563       if (current_class_name)
18564         pop_nested_class ();
18565     }
18566   else
18567     fn = cp_parser_function_definition_after_declarator (parser,
18568                                                          /*inline_p=*/false);
18569
18570   return fn;
18571 }
18572
18573 /* Parse the part of a function-definition that follows the
18574    declarator.  INLINE_P is TRUE iff this function is an inline
18575    function defined within a class-specifier.
18576
18577    Returns the function defined.  */
18578
18579 static tree
18580 cp_parser_function_definition_after_declarator (cp_parser* parser,
18581                                                 bool inline_p)
18582 {
18583   tree fn;
18584   bool ctor_initializer_p = false;
18585   bool saved_in_unbraced_linkage_specification_p;
18586   bool saved_in_function_body;
18587   unsigned saved_num_template_parameter_lists;
18588   cp_token *token;
18589
18590   saved_in_function_body = parser->in_function_body;
18591   parser->in_function_body = true;
18592   /* If the next token is `return', then the code may be trying to
18593      make use of the "named return value" extension that G++ used to
18594      support.  */
18595   token = cp_lexer_peek_token (parser->lexer);
18596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18597     {
18598       /* Consume the `return' keyword.  */
18599       cp_lexer_consume_token (parser->lexer);
18600       /* Look for the identifier that indicates what value is to be
18601          returned.  */
18602       cp_parser_identifier (parser);
18603       /* Issue an error message.  */
18604       error_at (token->location,
18605                 "named return values are no longer supported");
18606       /* Skip tokens until we reach the start of the function body.  */
18607       while (true)
18608         {
18609           cp_token *token = cp_lexer_peek_token (parser->lexer);
18610           if (token->type == CPP_OPEN_BRACE
18611               || token->type == CPP_EOF
18612               || token->type == CPP_PRAGMA_EOL)
18613             break;
18614           cp_lexer_consume_token (parser->lexer);
18615         }
18616     }
18617   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18618      anything declared inside `f'.  */
18619   saved_in_unbraced_linkage_specification_p
18620     = parser->in_unbraced_linkage_specification_p;
18621   parser->in_unbraced_linkage_specification_p = false;
18622   /* Inside the function, surrounding template-parameter-lists do not
18623      apply.  */
18624   saved_num_template_parameter_lists
18625     = parser->num_template_parameter_lists;
18626   parser->num_template_parameter_lists = 0;
18627
18628   start_lambda_scope (current_function_decl);
18629
18630   /* If the next token is `try', then we are looking at a
18631      function-try-block.  */
18632   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18633     ctor_initializer_p = cp_parser_function_try_block (parser);
18634   /* A function-try-block includes the function-body, so we only do
18635      this next part if we're not processing a function-try-block.  */
18636   else
18637     ctor_initializer_p
18638       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18639
18640   finish_lambda_scope ();
18641
18642   /* Finish the function.  */
18643   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18644                         (inline_p ? 2 : 0));
18645   /* Generate code for it, if necessary.  */
18646   expand_or_defer_fn (fn);
18647   /* Restore the saved values.  */
18648   parser->in_unbraced_linkage_specification_p
18649     = saved_in_unbraced_linkage_specification_p;
18650   parser->num_template_parameter_lists
18651     = saved_num_template_parameter_lists;
18652   parser->in_function_body = saved_in_function_body;
18653
18654   return fn;
18655 }
18656
18657 /* Parse a template-declaration, assuming that the `export' (and
18658    `extern') keywords, if present, has already been scanned.  MEMBER_P
18659    is as for cp_parser_template_declaration.  */
18660
18661 static void
18662 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18663 {
18664   tree decl = NULL_TREE;
18665   VEC (deferred_access_check,gc) *checks;
18666   tree parameter_list;
18667   bool friend_p = false;
18668   bool need_lang_pop;
18669   cp_token *token;
18670
18671   /* Look for the `template' keyword.  */
18672   token = cp_lexer_peek_token (parser->lexer);
18673   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18674     return;
18675
18676   /* And the `<'.  */
18677   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18678     return;
18679   if (at_class_scope_p () && current_function_decl)
18680     {
18681       /* 14.5.2.2 [temp.mem]
18682
18683          A local class shall not have member templates.  */
18684       error_at (token->location,
18685                 "invalid declaration of member template in local class");
18686       cp_parser_skip_to_end_of_block_or_statement (parser);
18687       return;
18688     }
18689   /* [temp]
18690
18691      A template ... shall not have C linkage.  */
18692   if (current_lang_name == lang_name_c)
18693     {
18694       error_at (token->location, "template with C linkage");
18695       /* Give it C++ linkage to avoid confusing other parts of the
18696          front end.  */
18697       push_lang_context (lang_name_cplusplus);
18698       need_lang_pop = true;
18699     }
18700   else
18701     need_lang_pop = false;
18702
18703   /* We cannot perform access checks on the template parameter
18704      declarations until we know what is being declared, just as we
18705      cannot check the decl-specifier list.  */
18706   push_deferring_access_checks (dk_deferred);
18707
18708   /* If the next token is `>', then we have an invalid
18709      specialization.  Rather than complain about an invalid template
18710      parameter, issue an error message here.  */
18711   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18712     {
18713       cp_parser_error (parser, "invalid explicit specialization");
18714       begin_specialization ();
18715       parameter_list = NULL_TREE;
18716     }
18717   else
18718     /* Parse the template parameters.  */
18719     parameter_list = cp_parser_template_parameter_list (parser);
18720
18721   /* Get the deferred access checks from the parameter list.  These
18722      will be checked once we know what is being declared, as for a
18723      member template the checks must be performed in the scope of the
18724      class containing the member.  */
18725   checks = get_deferred_access_checks ();
18726
18727   /* Look for the `>'.  */
18728   cp_parser_skip_to_end_of_template_parameter_list (parser);
18729   /* We just processed one more parameter list.  */
18730   ++parser->num_template_parameter_lists;
18731   /* If the next token is `template', there are more template
18732      parameters.  */
18733   if (cp_lexer_next_token_is_keyword (parser->lexer,
18734                                       RID_TEMPLATE))
18735     cp_parser_template_declaration_after_export (parser, member_p);
18736   else
18737     {
18738       /* There are no access checks when parsing a template, as we do not
18739          know if a specialization will be a friend.  */
18740       push_deferring_access_checks (dk_no_check);
18741       token = cp_lexer_peek_token (parser->lexer);
18742       decl = cp_parser_single_declaration (parser,
18743                                            checks,
18744                                            member_p,
18745                                            /*explicit_specialization_p=*/false,
18746                                            &friend_p);
18747       pop_deferring_access_checks ();
18748
18749       /* If this is a member template declaration, let the front
18750          end know.  */
18751       if (member_p && !friend_p && decl)
18752         {
18753           if (TREE_CODE (decl) == TYPE_DECL)
18754             cp_parser_check_access_in_redeclaration (decl, token->location);
18755
18756           decl = finish_member_template_decl (decl);
18757         }
18758       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18759         make_friend_class (current_class_type, TREE_TYPE (decl),
18760                            /*complain=*/true);
18761     }
18762   /* We are done with the current parameter list.  */
18763   --parser->num_template_parameter_lists;
18764
18765   pop_deferring_access_checks ();
18766
18767   /* Finish up.  */
18768   finish_template_decl (parameter_list);
18769
18770   /* Register member declarations.  */
18771   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18772     finish_member_declaration (decl);
18773   /* For the erroneous case of a template with C linkage, we pushed an
18774      implicit C++ linkage scope; exit that scope now.  */
18775   if (need_lang_pop)
18776     pop_lang_context ();
18777   /* If DECL is a function template, we must return to parse it later.
18778      (Even though there is no definition, there might be default
18779      arguments that need handling.)  */
18780   if (member_p && decl
18781       && (TREE_CODE (decl) == FUNCTION_DECL
18782           || DECL_FUNCTION_TEMPLATE_P (decl)))
18783     TREE_VALUE (parser->unparsed_functions_queues)
18784       = tree_cons (NULL_TREE, decl,
18785                    TREE_VALUE (parser->unparsed_functions_queues));
18786 }
18787
18788 /* Perform the deferred access checks from a template-parameter-list.
18789    CHECKS is a TREE_LIST of access checks, as returned by
18790    get_deferred_access_checks.  */
18791
18792 static void
18793 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18794 {
18795   ++processing_template_parmlist;
18796   perform_access_checks (checks);
18797   --processing_template_parmlist;
18798 }
18799
18800 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18801    `function-definition' sequence.  MEMBER_P is true, this declaration
18802    appears in a class scope.
18803
18804    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18805    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18806
18807 static tree
18808 cp_parser_single_declaration (cp_parser* parser,
18809                               VEC (deferred_access_check,gc)* checks,
18810                               bool member_p,
18811                               bool explicit_specialization_p,
18812                               bool* friend_p)
18813 {
18814   int declares_class_or_enum;
18815   tree decl = NULL_TREE;
18816   cp_decl_specifier_seq decl_specifiers;
18817   bool function_definition_p = false;
18818   cp_token *decl_spec_token_start;
18819
18820   /* This function is only used when processing a template
18821      declaration.  */
18822   gcc_assert (innermost_scope_kind () == sk_template_parms
18823               || innermost_scope_kind () == sk_template_spec);
18824
18825   /* Defer access checks until we know what is being declared.  */
18826   push_deferring_access_checks (dk_deferred);
18827
18828   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18829      alternative.  */
18830   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18831   cp_parser_decl_specifier_seq (parser,
18832                                 CP_PARSER_FLAGS_OPTIONAL,
18833                                 &decl_specifiers,
18834                                 &declares_class_or_enum);
18835   if (friend_p)
18836     *friend_p = cp_parser_friend_p (&decl_specifiers);
18837
18838   /* There are no template typedefs.  */
18839   if (decl_specifiers.specs[(int) ds_typedef])
18840     {
18841       error_at (decl_spec_token_start->location,
18842                 "template declaration of %<typedef%>");
18843       decl = error_mark_node;
18844     }
18845
18846   /* Gather up the access checks that occurred the
18847      decl-specifier-seq.  */
18848   stop_deferring_access_checks ();
18849
18850   /* Check for the declaration of a template class.  */
18851   if (declares_class_or_enum)
18852     {
18853       if (cp_parser_declares_only_class_p (parser))
18854         {
18855           decl = shadow_tag (&decl_specifiers);
18856
18857           /* In this case:
18858
18859                struct C {
18860                  friend template <typename T> struct A<T>::B;
18861                };
18862
18863              A<T>::B will be represented by a TYPENAME_TYPE, and
18864              therefore not recognized by shadow_tag.  */
18865           if (friend_p && *friend_p
18866               && !decl
18867               && decl_specifiers.type
18868               && TYPE_P (decl_specifiers.type))
18869             decl = decl_specifiers.type;
18870
18871           if (decl && decl != error_mark_node)
18872             decl = TYPE_NAME (decl);
18873           else
18874             decl = error_mark_node;
18875
18876           /* Perform access checks for template parameters.  */
18877           cp_parser_perform_template_parameter_access_checks (checks);
18878         }
18879     }
18880
18881   /* Complain about missing 'typename' or other invalid type names.  */
18882   if (!decl_specifiers.any_type_specifiers_p)
18883     cp_parser_parse_and_diagnose_invalid_type_name (parser);
18884
18885   /* If it's not a template class, try for a template function.  If
18886      the next token is a `;', then this declaration does not declare
18887      anything.  But, if there were errors in the decl-specifiers, then
18888      the error might well have come from an attempted class-specifier.
18889      In that case, there's no need to warn about a missing declarator.  */
18890   if (!decl
18891       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18892           || decl_specifiers.type != error_mark_node))
18893     {
18894       decl = cp_parser_init_declarator (parser,
18895                                         &decl_specifiers,
18896                                         checks,
18897                                         /*function_definition_allowed_p=*/true,
18898                                         member_p,
18899                                         declares_class_or_enum,
18900                                         &function_definition_p);
18901
18902     /* 7.1.1-1 [dcl.stc]
18903
18904        A storage-class-specifier shall not be specified in an explicit
18905        specialization...  */
18906     if (decl
18907         && explicit_specialization_p
18908         && decl_specifiers.storage_class != sc_none)
18909       {
18910         error_at (decl_spec_token_start->location,
18911                   "explicit template specialization cannot have a storage class");
18912         decl = error_mark_node;
18913       }
18914     }
18915
18916   pop_deferring_access_checks ();
18917
18918   /* Clear any current qualification; whatever comes next is the start
18919      of something new.  */
18920   parser->scope = NULL_TREE;
18921   parser->qualifying_scope = NULL_TREE;
18922   parser->object_scope = NULL_TREE;
18923   /* Look for a trailing `;' after the declaration.  */
18924   if (!function_definition_p
18925       && (decl == error_mark_node
18926           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18927     cp_parser_skip_to_end_of_block_or_statement (parser);
18928
18929   return decl;
18930 }
18931
18932 /* Parse a cast-expression that is not the operand of a unary "&".  */
18933
18934 static tree
18935 cp_parser_simple_cast_expression (cp_parser *parser)
18936 {
18937   return cp_parser_cast_expression (parser, /*address_p=*/false,
18938                                     /*cast_p=*/false, NULL);
18939 }
18940
18941 /* Parse a functional cast to TYPE.  Returns an expression
18942    representing the cast.  */
18943
18944 static tree
18945 cp_parser_functional_cast (cp_parser* parser, tree type)
18946 {
18947   VEC(tree,gc) *vec;
18948   tree expression_list;
18949   tree cast;
18950   bool nonconst_p;
18951
18952   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18953     {
18954       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
18955       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18956       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18957       if (TREE_CODE (type) == TYPE_DECL)
18958         type = TREE_TYPE (type);
18959       return finish_compound_literal (type, expression_list);
18960     }
18961
18962
18963   vec = cp_parser_parenthesized_expression_list (parser, false,
18964                                                  /*cast_p=*/true,
18965                                                  /*allow_expansion_p=*/true,
18966                                                  /*non_constant_p=*/NULL);
18967   if (vec == NULL)
18968     expression_list = error_mark_node;
18969   else
18970     {
18971       expression_list = build_tree_list_vec (vec);
18972       release_tree_vector (vec);
18973     }
18974
18975   cast = build_functional_cast (type, expression_list,
18976                                 tf_warning_or_error);
18977   /* [expr.const]/1: In an integral constant expression "only type
18978      conversions to integral or enumeration type can be used".  */
18979   if (TREE_CODE (type) == TYPE_DECL)
18980     type = TREE_TYPE (type);
18981   if (cast != error_mark_node
18982       && !cast_valid_in_integral_constant_expression_p (type)
18983       && (cp_parser_non_integral_constant_expression
18984           (parser, "a call to a constructor")))
18985     return error_mark_node;
18986   return cast;
18987 }
18988
18989 /* Save the tokens that make up the body of a member function defined
18990    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18991    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18992    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18993    for the member function.  */
18994
18995 static tree
18996 cp_parser_save_member_function_body (cp_parser* parser,
18997                                      cp_decl_specifier_seq *decl_specifiers,
18998                                      cp_declarator *declarator,
18999                                      tree attributes)
19000 {
19001   cp_token *first;
19002   cp_token *last;
19003   tree fn;
19004
19005   /* Create the FUNCTION_DECL.  */
19006   fn = grokmethod (decl_specifiers, declarator, attributes);
19007   /* If something went badly wrong, bail out now.  */
19008   if (fn == error_mark_node)
19009     {
19010       /* If there's a function-body, skip it.  */
19011       if (cp_parser_token_starts_function_definition_p
19012           (cp_lexer_peek_token (parser->lexer)))
19013         cp_parser_skip_to_end_of_block_or_statement (parser);
19014       return error_mark_node;
19015     }
19016
19017   /* Remember it, if there default args to post process.  */
19018   cp_parser_save_default_args (parser, fn);
19019
19020   /* Save away the tokens that make up the body of the
19021      function.  */
19022   first = parser->lexer->next_token;
19023   /* We can have braced-init-list mem-initializers before the fn body.  */
19024   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
19025     {
19026       cp_lexer_consume_token (parser->lexer);
19027       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
19028              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
19029         {
19030           /* cache_group will stop after an un-nested { } pair, too.  */
19031           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
19032             break;
19033
19034           /* variadic mem-inits have ... after the ')'.  */
19035           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19036             cp_lexer_consume_token (parser->lexer);
19037         }
19038     }
19039   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19040   /* Handle function try blocks.  */
19041   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
19042     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
19043   last = parser->lexer->next_token;
19044
19045   /* Save away the inline definition; we will process it when the
19046      class is complete.  */
19047   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
19048   DECL_PENDING_INLINE_P (fn) = 1;
19049
19050   /* We need to know that this was defined in the class, so that
19051      friend templates are handled correctly.  */
19052   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
19053
19054   /* Add FN to the queue of functions to be parsed later.  */
19055   TREE_VALUE (parser->unparsed_functions_queues)
19056     = tree_cons (NULL_TREE, fn,
19057                  TREE_VALUE (parser->unparsed_functions_queues));
19058
19059   return fn;
19060 }
19061
19062 /* Parse a template-argument-list, as well as the trailing ">" (but
19063    not the opening ">").  See cp_parser_template_argument_list for the
19064    return value.  */
19065
19066 static tree
19067 cp_parser_enclosed_template_argument_list (cp_parser* parser)
19068 {
19069   tree arguments;
19070   tree saved_scope;
19071   tree saved_qualifying_scope;
19072   tree saved_object_scope;
19073   bool saved_greater_than_is_operator_p;
19074   int saved_unevaluated_operand;
19075   int saved_inhibit_evaluation_warnings;
19076
19077   /* [temp.names]
19078
19079      When parsing a template-id, the first non-nested `>' is taken as
19080      the end of the template-argument-list rather than a greater-than
19081      operator.  */
19082   saved_greater_than_is_operator_p
19083     = parser->greater_than_is_operator_p;
19084   parser->greater_than_is_operator_p = false;
19085   /* Parsing the argument list may modify SCOPE, so we save it
19086      here.  */
19087   saved_scope = parser->scope;
19088   saved_qualifying_scope = parser->qualifying_scope;
19089   saved_object_scope = parser->object_scope;
19090   /* We need to evaluate the template arguments, even though this
19091      template-id may be nested within a "sizeof".  */
19092   saved_unevaluated_operand = cp_unevaluated_operand;
19093   cp_unevaluated_operand = 0;
19094   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
19095   c_inhibit_evaluation_warnings = 0;
19096   /* Parse the template-argument-list itself.  */
19097   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
19098       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19099     arguments = NULL_TREE;
19100   else
19101     arguments = cp_parser_template_argument_list (parser);
19102   /* Look for the `>' that ends the template-argument-list. If we find
19103      a '>>' instead, it's probably just a typo.  */
19104   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
19105     {
19106       if (cxx_dialect != cxx98)
19107         {
19108           /* In C++0x, a `>>' in a template argument list or cast
19109              expression is considered to be two separate `>'
19110              tokens. So, change the current token to a `>', but don't
19111              consume it: it will be consumed later when the outer
19112              template argument list (or cast expression) is parsed.
19113              Note that this replacement of `>' for `>>' is necessary
19114              even if we are parsing tentatively: in the tentative
19115              case, after calling
19116              cp_parser_enclosed_template_argument_list we will always
19117              throw away all of the template arguments and the first
19118              closing `>', either because the template argument list
19119              was erroneous or because we are replacing those tokens
19120              with a CPP_TEMPLATE_ID token.  The second `>' (which will
19121              not have been thrown away) is needed either to close an
19122              outer template argument list or to complete a new-style
19123              cast.  */
19124           cp_token *token = cp_lexer_peek_token (parser->lexer);
19125           token->type = CPP_GREATER;
19126         }
19127       else if (!saved_greater_than_is_operator_p)
19128         {
19129           /* If we're in a nested template argument list, the '>>' has
19130             to be a typo for '> >'. We emit the error message, but we
19131             continue parsing and we push a '>' as next token, so that
19132             the argument list will be parsed correctly.  Note that the
19133             global source location is still on the token before the
19134             '>>', so we need to say explicitly where we want it.  */
19135           cp_token *token = cp_lexer_peek_token (parser->lexer);
19136           error_at (token->location, "%<>>%> should be %<> >%> "
19137                     "within a nested template argument list");
19138
19139           token->type = CPP_GREATER;
19140         }
19141       else
19142         {
19143           /* If this is not a nested template argument list, the '>>'
19144             is a typo for '>'. Emit an error message and continue.
19145             Same deal about the token location, but here we can get it
19146             right by consuming the '>>' before issuing the diagnostic.  */
19147           cp_token *token = cp_lexer_consume_token (parser->lexer);
19148           error_at (token->location,
19149                     "spurious %<>>%>, use %<>%> to terminate "
19150                     "a template argument list");
19151         }
19152     }
19153   else
19154     cp_parser_skip_to_end_of_template_parameter_list (parser);
19155   /* The `>' token might be a greater-than operator again now.  */
19156   parser->greater_than_is_operator_p
19157     = saved_greater_than_is_operator_p;
19158   /* Restore the SAVED_SCOPE.  */
19159   parser->scope = saved_scope;
19160   parser->qualifying_scope = saved_qualifying_scope;
19161   parser->object_scope = saved_object_scope;
19162   cp_unevaluated_operand = saved_unevaluated_operand;
19163   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19164
19165   return arguments;
19166 }
19167
19168 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19169    arguments, or the body of the function have not yet been parsed,
19170    parse them now.  */
19171
19172 static void
19173 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19174 {
19175   /* If this member is a template, get the underlying
19176      FUNCTION_DECL.  */
19177   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19178     member_function = DECL_TEMPLATE_RESULT (member_function);
19179
19180   /* There should not be any class definitions in progress at this
19181      point; the bodies of members are only parsed outside of all class
19182      definitions.  */
19183   gcc_assert (parser->num_classes_being_defined == 0);
19184   /* While we're parsing the member functions we might encounter more
19185      classes.  We want to handle them right away, but we don't want
19186      them getting mixed up with functions that are currently in the
19187      queue.  */
19188   parser->unparsed_functions_queues
19189     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19190
19191   /* Make sure that any template parameters are in scope.  */
19192   maybe_begin_member_template_processing (member_function);
19193
19194   /* If the body of the function has not yet been parsed, parse it
19195      now.  */
19196   if (DECL_PENDING_INLINE_P (member_function))
19197     {
19198       tree function_scope;
19199       cp_token_cache *tokens;
19200
19201       /* The function is no longer pending; we are processing it.  */
19202       tokens = DECL_PENDING_INLINE_INFO (member_function);
19203       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19204       DECL_PENDING_INLINE_P (member_function) = 0;
19205
19206       /* If this is a local class, enter the scope of the containing
19207          function.  */
19208       function_scope = current_function_decl;
19209       if (function_scope)
19210         push_function_context ();
19211
19212       /* Push the body of the function onto the lexer stack.  */
19213       cp_parser_push_lexer_for_tokens (parser, tokens);
19214
19215       /* Let the front end know that we going to be defining this
19216          function.  */
19217       start_preparsed_function (member_function, NULL_TREE,
19218                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19219
19220       /* Don't do access checking if it is a templated function.  */
19221       if (processing_template_decl)
19222         push_deferring_access_checks (dk_no_check);
19223
19224       /* Now, parse the body of the function.  */
19225       cp_parser_function_definition_after_declarator (parser,
19226                                                       /*inline_p=*/true);
19227
19228       if (processing_template_decl)
19229         pop_deferring_access_checks ();
19230
19231       /* Leave the scope of the containing function.  */
19232       if (function_scope)
19233         pop_function_context ();
19234       cp_parser_pop_lexer (parser);
19235     }
19236
19237   /* Remove any template parameters from the symbol table.  */
19238   maybe_end_member_template_processing ();
19239
19240   /* Restore the queue.  */
19241   parser->unparsed_functions_queues
19242     = TREE_CHAIN (parser->unparsed_functions_queues);
19243 }
19244
19245 /* If DECL contains any default args, remember it on the unparsed
19246    functions queue.  */
19247
19248 static void
19249 cp_parser_save_default_args (cp_parser* parser, tree decl)
19250 {
19251   tree probe;
19252
19253   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19254        probe;
19255        probe = TREE_CHAIN (probe))
19256     if (TREE_PURPOSE (probe))
19257       {
19258         TREE_PURPOSE (parser->unparsed_functions_queues)
19259           = tree_cons (current_class_type, decl,
19260                        TREE_PURPOSE (parser->unparsed_functions_queues));
19261         break;
19262       }
19263 }
19264
19265 /* FN is a FUNCTION_DECL which may contains a parameter with an
19266    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19267    assumes that the current scope is the scope in which the default
19268    argument should be processed.  */
19269
19270 static void
19271 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19272 {
19273   bool saved_local_variables_forbidden_p;
19274   tree parm, parmdecl;
19275
19276   /* While we're parsing the default args, we might (due to the
19277      statement expression extension) encounter more classes.  We want
19278      to handle them right away, but we don't want them getting mixed
19279      up with default args that are currently in the queue.  */
19280   parser->unparsed_functions_queues
19281     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19282
19283   /* Local variable names (and the `this' keyword) may not appear
19284      in a default argument.  */
19285   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19286   parser->local_variables_forbidden_p = true;
19287
19288   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19289          parmdecl = DECL_ARGUMENTS (fn);
19290        parm && parm != void_list_node;
19291        parm = TREE_CHAIN (parm),
19292          parmdecl = TREE_CHAIN (parmdecl))
19293     {
19294       cp_token_cache *tokens;
19295       tree default_arg = TREE_PURPOSE (parm);
19296       tree parsed_arg;
19297       VEC(tree,gc) *insts;
19298       tree copy;
19299       unsigned ix;
19300
19301       if (!default_arg)
19302         continue;
19303
19304       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19305         /* This can happen for a friend declaration for a function
19306            already declared with default arguments.  */
19307         continue;
19308
19309        /* Push the saved tokens for the default argument onto the parser's
19310           lexer stack.  */
19311       tokens = DEFARG_TOKENS (default_arg);
19312       cp_parser_push_lexer_for_tokens (parser, tokens);
19313
19314       start_lambda_scope (parmdecl);
19315
19316       /* Parse the assignment-expression.  */
19317       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19318       if (parsed_arg == error_mark_node)
19319         {
19320           cp_parser_pop_lexer (parser);
19321           continue;
19322         }
19323
19324       if (!processing_template_decl)
19325         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19326
19327       TREE_PURPOSE (parm) = parsed_arg;
19328
19329       /* Update any instantiations we've already created.  */
19330       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19331            VEC_iterate (tree, insts, ix, copy); ix++)
19332         TREE_PURPOSE (copy) = parsed_arg;
19333
19334       finish_lambda_scope ();
19335
19336       /* If the token stream has not been completely used up, then
19337          there was extra junk after the end of the default
19338          argument.  */
19339       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19340         cp_parser_error (parser, "expected %<,%>");
19341
19342       /* Revert to the main lexer.  */
19343       cp_parser_pop_lexer (parser);
19344     }
19345
19346   /* Make sure no default arg is missing.  */
19347   check_default_args (fn);
19348
19349   /* Restore the state of local_variables_forbidden_p.  */
19350   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19351
19352   /* Restore the queue.  */
19353   parser->unparsed_functions_queues
19354     = TREE_CHAIN (parser->unparsed_functions_queues);
19355 }
19356
19357 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19358    either a TYPE or an expression, depending on the form of the
19359    input.  The KEYWORD indicates which kind of expression we have
19360    encountered.  */
19361
19362 static tree
19363 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19364 {
19365   tree expr = NULL_TREE;
19366   const char *saved_message;
19367   char *tmp;
19368   bool saved_integral_constant_expression_p;
19369   bool saved_non_integral_constant_expression_p;
19370   bool pack_expansion_p = false;
19371
19372   /* Types cannot be defined in a `sizeof' expression.  Save away the
19373      old message.  */
19374   saved_message = parser->type_definition_forbidden_message;
19375   /* And create the new one.  */
19376   tmp = concat ("types may not be defined in %<",
19377                 IDENTIFIER_POINTER (ridpointers[keyword]),
19378                 "%> expressions", NULL);
19379   parser->type_definition_forbidden_message = tmp;
19380
19381   /* The restrictions on constant-expressions do not apply inside
19382      sizeof expressions.  */
19383   saved_integral_constant_expression_p
19384     = parser->integral_constant_expression_p;
19385   saved_non_integral_constant_expression_p
19386     = parser->non_integral_constant_expression_p;
19387   parser->integral_constant_expression_p = false;
19388
19389   /* If it's a `...', then we are computing the length of a parameter
19390      pack.  */
19391   if (keyword == RID_SIZEOF
19392       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19393     {
19394       /* Consume the `...'.  */
19395       cp_lexer_consume_token (parser->lexer);
19396       maybe_warn_variadic_templates ();
19397
19398       /* Note that this is an expansion.  */
19399       pack_expansion_p = true;
19400     }
19401
19402   /* Do not actually evaluate the expression.  */
19403   ++cp_unevaluated_operand;
19404   ++c_inhibit_evaluation_warnings;
19405   /* If it's a `(', then we might be looking at the type-id
19406      construction.  */
19407   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19408     {
19409       tree type;
19410       bool saved_in_type_id_in_expr_p;
19411
19412       /* We can't be sure yet whether we're looking at a type-id or an
19413          expression.  */
19414       cp_parser_parse_tentatively (parser);
19415       /* Consume the `('.  */
19416       cp_lexer_consume_token (parser->lexer);
19417       /* Parse the type-id.  */
19418       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19419       parser->in_type_id_in_expr_p = true;
19420       type = cp_parser_type_id (parser);
19421       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19422       /* Now, look for the trailing `)'.  */
19423       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19424       /* If all went well, then we're done.  */
19425       if (cp_parser_parse_definitely (parser))
19426         {
19427           cp_decl_specifier_seq decl_specs;
19428
19429           /* Build a trivial decl-specifier-seq.  */
19430           clear_decl_specs (&decl_specs);
19431           decl_specs.type = type;
19432
19433           /* Call grokdeclarator to figure out what type this is.  */
19434           expr = grokdeclarator (NULL,
19435                                  &decl_specs,
19436                                  TYPENAME,
19437                                  /*initialized=*/0,
19438                                  /*attrlist=*/NULL);
19439         }
19440     }
19441
19442   /* If the type-id production did not work out, then we must be
19443      looking at the unary-expression production.  */
19444   if (!expr)
19445     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19446                                        /*cast_p=*/false, NULL);
19447
19448   if (pack_expansion_p)
19449     /* Build a pack expansion. */
19450     expr = make_pack_expansion (expr);
19451
19452   /* Go back to evaluating expressions.  */
19453   --cp_unevaluated_operand;
19454   --c_inhibit_evaluation_warnings;
19455
19456   /* Free the message we created.  */
19457   free (tmp);
19458   /* And restore the old one.  */
19459   parser->type_definition_forbidden_message = saved_message;
19460   parser->integral_constant_expression_p
19461     = saved_integral_constant_expression_p;
19462   parser->non_integral_constant_expression_p
19463     = saved_non_integral_constant_expression_p;
19464
19465   return expr;
19466 }
19467
19468 /* If the current declaration has no declarator, return true.  */
19469
19470 static bool
19471 cp_parser_declares_only_class_p (cp_parser *parser)
19472 {
19473   /* If the next token is a `;' or a `,' then there is no
19474      declarator.  */
19475   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19476           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19477 }
19478
19479 /* Update the DECL_SPECS to reflect the storage class indicated by
19480    KEYWORD.  */
19481
19482 static void
19483 cp_parser_set_storage_class (cp_parser *parser,
19484                              cp_decl_specifier_seq *decl_specs,
19485                              enum rid keyword,
19486                              location_t location)
19487 {
19488   cp_storage_class storage_class;
19489
19490   if (parser->in_unbraced_linkage_specification_p)
19491     {
19492       error_at (location, "invalid use of %qD in linkage specification",
19493                 ridpointers[keyword]);
19494       return;
19495     }
19496   else if (decl_specs->storage_class != sc_none)
19497     {
19498       decl_specs->conflicting_specifiers_p = true;
19499       return;
19500     }
19501
19502   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19503       && decl_specs->specs[(int) ds_thread])
19504     {
19505       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19506       decl_specs->specs[(int) ds_thread] = 0;
19507     }
19508
19509   switch (keyword)
19510     {
19511     case RID_AUTO:
19512       storage_class = sc_auto;
19513       break;
19514     case RID_REGISTER:
19515       storage_class = sc_register;
19516       break;
19517     case RID_STATIC:
19518       storage_class = sc_static;
19519       break;
19520     case RID_EXTERN:
19521       storage_class = sc_extern;
19522       break;
19523     case RID_MUTABLE:
19524       storage_class = sc_mutable;
19525       break;
19526     default:
19527       gcc_unreachable ();
19528     }
19529   decl_specs->storage_class = storage_class;
19530
19531   /* A storage class specifier cannot be applied alongside a typedef 
19532      specifier. If there is a typedef specifier present then set 
19533      conflicting_specifiers_p which will trigger an error later
19534      on in grokdeclarator. */
19535   if (decl_specs->specs[(int)ds_typedef])
19536     decl_specs->conflicting_specifiers_p = true;
19537 }
19538
19539 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19540    is true, the type is a user-defined type; otherwise it is a
19541    built-in type specified by a keyword.  */
19542
19543 static void
19544 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19545                               tree type_spec,
19546                               location_t location,
19547                               bool user_defined_p)
19548 {
19549   decl_specs->any_specifiers_p = true;
19550
19551   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19552      (with, for example, in "typedef int wchar_t;") we remember that
19553      this is what happened.  In system headers, we ignore these
19554      declarations so that G++ can work with system headers that are not
19555      C++-safe.  */
19556   if (decl_specs->specs[(int) ds_typedef]
19557       && !user_defined_p
19558       && (type_spec == boolean_type_node
19559           || type_spec == char16_type_node
19560           || type_spec == char32_type_node
19561           || type_spec == wchar_type_node)
19562       && (decl_specs->type
19563           || decl_specs->specs[(int) ds_long]
19564           || decl_specs->specs[(int) ds_short]
19565           || decl_specs->specs[(int) ds_unsigned]
19566           || decl_specs->specs[(int) ds_signed]))
19567     {
19568       decl_specs->redefined_builtin_type = type_spec;
19569       if (!decl_specs->type)
19570         {
19571           decl_specs->type = type_spec;
19572           decl_specs->user_defined_type_p = false;
19573           decl_specs->type_location = location;
19574         }
19575     }
19576   else if (decl_specs->type)
19577     decl_specs->multiple_types_p = true;
19578   else
19579     {
19580       decl_specs->type = type_spec;
19581       decl_specs->user_defined_type_p = user_defined_p;
19582       decl_specs->redefined_builtin_type = NULL_TREE;
19583       decl_specs->type_location = location;
19584     }
19585 }
19586
19587 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19588    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19589
19590 static bool
19591 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19592 {
19593   return decl_specifiers->specs[(int) ds_friend] != 0;
19594 }
19595
19596 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19597    issue an error message indicating that TOKEN_DESC was expected.
19598
19599    Returns the token consumed, if the token had the appropriate type.
19600    Otherwise, returns NULL.  */
19601
19602 static cp_token *
19603 cp_parser_require (cp_parser* parser,
19604                    enum cpp_ttype type,
19605                    const char* token_desc)
19606 {
19607   if (cp_lexer_next_token_is (parser->lexer, type))
19608     return cp_lexer_consume_token (parser->lexer);
19609   else
19610     {
19611       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19612       if (!cp_parser_simulate_error (parser))
19613         {
19614           char *message = concat ("expected ", token_desc, NULL);
19615           cp_parser_error (parser, message);
19616           free (message);
19617         }
19618       return NULL;
19619     }
19620 }
19621
19622 /* An error message is produced if the next token is not '>'.
19623    All further tokens are skipped until the desired token is
19624    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19625
19626 static void
19627 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19628 {
19629   /* Current level of '< ... >'.  */
19630   unsigned level = 0;
19631   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19632   unsigned nesting_depth = 0;
19633
19634   /* Are we ready, yet?  If not, issue error message.  */
19635   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19636     return;
19637
19638   /* Skip tokens until the desired token is found.  */
19639   while (true)
19640     {
19641       /* Peek at the next token.  */
19642       switch (cp_lexer_peek_token (parser->lexer)->type)
19643         {
19644         case CPP_LESS:
19645           if (!nesting_depth)
19646             ++level;
19647           break;
19648
19649         case CPP_RSHIFT:
19650           if (cxx_dialect == cxx98)
19651             /* C++0x views the `>>' operator as two `>' tokens, but
19652                C++98 does not. */
19653             break;
19654           else if (!nesting_depth && level-- == 0)
19655             {
19656               /* We've hit a `>>' where the first `>' closes the
19657                  template argument list, and the second `>' is
19658                  spurious.  Just consume the `>>' and stop; we've
19659                  already produced at least one error.  */
19660               cp_lexer_consume_token (parser->lexer);
19661               return;
19662             }
19663           /* Fall through for C++0x, so we handle the second `>' in
19664              the `>>'.  */
19665
19666         case CPP_GREATER:
19667           if (!nesting_depth && level-- == 0)
19668             {
19669               /* We've reached the token we want, consume it and stop.  */
19670               cp_lexer_consume_token (parser->lexer);
19671               return;
19672             }
19673           break;
19674
19675         case CPP_OPEN_PAREN:
19676         case CPP_OPEN_SQUARE:
19677           ++nesting_depth;
19678           break;
19679
19680         case CPP_CLOSE_PAREN:
19681         case CPP_CLOSE_SQUARE:
19682           if (nesting_depth-- == 0)
19683             return;
19684           break;
19685
19686         case CPP_EOF:
19687         case CPP_PRAGMA_EOL:
19688         case CPP_SEMICOLON:
19689         case CPP_OPEN_BRACE:
19690         case CPP_CLOSE_BRACE:
19691           /* The '>' was probably forgotten, don't look further.  */
19692           return;
19693
19694         default:
19695           break;
19696         }
19697
19698       /* Consume this token.  */
19699       cp_lexer_consume_token (parser->lexer);
19700     }
19701 }
19702
19703 /* If the next token is the indicated keyword, consume it.  Otherwise,
19704    issue an error message indicating that TOKEN_DESC was expected.
19705
19706    Returns the token consumed, if the token had the appropriate type.
19707    Otherwise, returns NULL.  */
19708
19709 static cp_token *
19710 cp_parser_require_keyword (cp_parser* parser,
19711                            enum rid keyword,
19712                            const char* token_desc)
19713 {
19714   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19715
19716   if (token && token->keyword != keyword)
19717     {
19718       dyn_string_t error_msg;
19719
19720       /* Format the error message.  */
19721       error_msg = dyn_string_new (0);
19722       dyn_string_append_cstr (error_msg, "expected ");
19723       dyn_string_append_cstr (error_msg, token_desc);
19724       cp_parser_error (parser, error_msg->s);
19725       dyn_string_delete (error_msg);
19726       return NULL;
19727     }
19728
19729   return token;
19730 }
19731
19732 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19733    function-definition.  */
19734
19735 static bool
19736 cp_parser_token_starts_function_definition_p (cp_token* token)
19737 {
19738   return (/* An ordinary function-body begins with an `{'.  */
19739           token->type == CPP_OPEN_BRACE
19740           /* A ctor-initializer begins with a `:'.  */
19741           || token->type == CPP_COLON
19742           /* A function-try-block begins with `try'.  */
19743           || token->keyword == RID_TRY
19744           /* The named return value extension begins with `return'.  */
19745           || token->keyword == RID_RETURN);
19746 }
19747
19748 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19749    definition.  */
19750
19751 static bool
19752 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19753 {
19754   cp_token *token;
19755
19756   token = cp_lexer_peek_token (parser->lexer);
19757   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19758 }
19759
19760 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19761    C++0x) ending a template-argument.  */
19762
19763 static bool
19764 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19765 {
19766   cp_token *token;
19767
19768   token = cp_lexer_peek_token (parser->lexer);
19769   return (token->type == CPP_COMMA 
19770           || token->type == CPP_GREATER
19771           || token->type == CPP_ELLIPSIS
19772           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19773 }
19774
19775 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19776    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19777
19778 static bool
19779 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19780                                                      size_t n)
19781 {
19782   cp_token *token;
19783
19784   token = cp_lexer_peek_nth_token (parser->lexer, n);
19785   if (token->type == CPP_LESS)
19786     return true;
19787   /* Check for the sequence `<::' in the original code. It would be lexed as
19788      `[:', where `[' is a digraph, and there is no whitespace before
19789      `:'.  */
19790   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19791     {
19792       cp_token *token2;
19793       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19794       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19795         return true;
19796     }
19797   return false;
19798 }
19799
19800 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19801    or none_type otherwise.  */
19802
19803 static enum tag_types
19804 cp_parser_token_is_class_key (cp_token* token)
19805 {
19806   switch (token->keyword)
19807     {
19808     case RID_CLASS:
19809       return class_type;
19810     case RID_STRUCT:
19811       return record_type;
19812     case RID_UNION:
19813       return union_type;
19814
19815     default:
19816       return none_type;
19817     }
19818 }
19819
19820 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19821
19822 static void
19823 cp_parser_check_class_key (enum tag_types class_key, tree type)
19824 {
19825   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19826     permerror (input_location, "%qs tag used in naming %q#T",
19827             class_key == union_type ? "union"
19828              : class_key == record_type ? "struct" : "class",
19829              type);
19830 }
19831
19832 /* Issue an error message if DECL is redeclared with different
19833    access than its original declaration [class.access.spec/3].
19834    This applies to nested classes and nested class templates.
19835    [class.mem/1].  */
19836
19837 static void
19838 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19839 {
19840   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19841     return;
19842
19843   if ((TREE_PRIVATE (decl)
19844        != (current_access_specifier == access_private_node))
19845       || (TREE_PROTECTED (decl)
19846           != (current_access_specifier == access_protected_node)))
19847     error_at (location, "%qD redeclared with different access", decl);
19848 }
19849
19850 /* Look for the `template' keyword, as a syntactic disambiguator.
19851    Return TRUE iff it is present, in which case it will be
19852    consumed.  */
19853
19854 static bool
19855 cp_parser_optional_template_keyword (cp_parser *parser)
19856 {
19857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19858     {
19859       /* The `template' keyword can only be used within templates;
19860          outside templates the parser can always figure out what is a
19861          template and what is not.  */
19862       if (!processing_template_decl)
19863         {
19864           cp_token *token = cp_lexer_peek_token (parser->lexer);
19865           error_at (token->location,
19866                     "%<template%> (as a disambiguator) is only allowed "
19867                     "within templates");
19868           /* If this part of the token stream is rescanned, the same
19869              error message would be generated.  So, we purge the token
19870              from the stream.  */
19871           cp_lexer_purge_token (parser->lexer);
19872           return false;
19873         }
19874       else
19875         {
19876           /* Consume the `template' keyword.  */
19877           cp_lexer_consume_token (parser->lexer);
19878           return true;
19879         }
19880     }
19881
19882   return false;
19883 }
19884
19885 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19886    set PARSER->SCOPE, and perform other related actions.  */
19887
19888 static void
19889 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19890 {
19891   int i;
19892   struct tree_check *check_value;
19893   deferred_access_check *chk;
19894   VEC (deferred_access_check,gc) *checks;
19895
19896   /* Get the stored value.  */
19897   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19898   /* Perform any access checks that were deferred.  */
19899   checks = check_value->checks;
19900   if (checks)
19901     {
19902       for (i = 0 ;
19903            VEC_iterate (deferred_access_check, checks, i, chk) ;
19904            ++i)
19905         {
19906           perform_or_defer_access_check (chk->binfo,
19907                                          chk->decl,
19908                                          chk->diag_decl);
19909         }
19910     }
19911   /* Set the scope from the stored value.  */
19912   parser->scope = check_value->value;
19913   parser->qualifying_scope = check_value->qualifying_scope;
19914   parser->object_scope = NULL_TREE;
19915 }
19916
19917 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19918    encounter the end of a block before what we were looking for.  */
19919
19920 static bool
19921 cp_parser_cache_group (cp_parser *parser,
19922                        enum cpp_ttype end,
19923                        unsigned depth)
19924 {
19925   while (true)
19926     {
19927       cp_token *token = cp_lexer_peek_token (parser->lexer);
19928
19929       /* Abort a parenthesized expression if we encounter a semicolon.  */
19930       if ((end == CPP_CLOSE_PAREN || depth == 0)
19931           && token->type == CPP_SEMICOLON)
19932         return true;
19933       /* If we've reached the end of the file, stop.  */
19934       if (token->type == CPP_EOF
19935           || (end != CPP_PRAGMA_EOL
19936               && token->type == CPP_PRAGMA_EOL))
19937         return true;
19938       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19939         /* We've hit the end of an enclosing block, so there's been some
19940            kind of syntax error.  */
19941         return true;
19942
19943       /* Consume the token.  */
19944       cp_lexer_consume_token (parser->lexer);
19945       /* See if it starts a new group.  */
19946       if (token->type == CPP_OPEN_BRACE)
19947         {
19948           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19949           /* In theory this should probably check end == '}', but
19950              cp_parser_save_member_function_body needs it to exit
19951              after either '}' or ')' when called with ')'.  */
19952           if (depth == 0)
19953             return false;
19954         }
19955       else if (token->type == CPP_OPEN_PAREN)
19956         {
19957           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19958           if (depth == 0 && end == CPP_CLOSE_PAREN)
19959             return false;
19960         }
19961       else if (token->type == CPP_PRAGMA)
19962         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19963       else if (token->type == end)
19964         return false;
19965     }
19966 }
19967
19968 /* Begin parsing tentatively.  We always save tokens while parsing
19969    tentatively so that if the tentative parsing fails we can restore the
19970    tokens.  */
19971
19972 static void
19973 cp_parser_parse_tentatively (cp_parser* parser)
19974 {
19975   /* Enter a new parsing context.  */
19976   parser->context = cp_parser_context_new (parser->context);
19977   /* Begin saving tokens.  */
19978   cp_lexer_save_tokens (parser->lexer);
19979   /* In order to avoid repetitive access control error messages,
19980      access checks are queued up until we are no longer parsing
19981      tentatively.  */
19982   push_deferring_access_checks (dk_deferred);
19983 }
19984
19985 /* Commit to the currently active tentative parse.  */
19986
19987 static void
19988 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19989 {
19990   cp_parser_context *context;
19991   cp_lexer *lexer;
19992
19993   /* Mark all of the levels as committed.  */
19994   lexer = parser->lexer;
19995   for (context = parser->context; context->next; context = context->next)
19996     {
19997       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19998         break;
19999       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
20000       while (!cp_lexer_saving_tokens (lexer))
20001         lexer = lexer->next;
20002       cp_lexer_commit_tokens (lexer);
20003     }
20004 }
20005
20006 /* Abort the currently active tentative parse.  All consumed tokens
20007    will be rolled back, and no diagnostics will be issued.  */
20008
20009 static void
20010 cp_parser_abort_tentative_parse (cp_parser* parser)
20011 {
20012   cp_parser_simulate_error (parser);
20013   /* Now, pretend that we want to see if the construct was
20014      successfully parsed.  */
20015   cp_parser_parse_definitely (parser);
20016 }
20017
20018 /* Stop parsing tentatively.  If a parse error has occurred, restore the
20019    token stream.  Otherwise, commit to the tokens we have consumed.
20020    Returns true if no error occurred; false otherwise.  */
20021
20022 static bool
20023 cp_parser_parse_definitely (cp_parser* parser)
20024 {
20025   bool error_occurred;
20026   cp_parser_context *context;
20027
20028   /* Remember whether or not an error occurred, since we are about to
20029      destroy that information.  */
20030   error_occurred = cp_parser_error_occurred (parser);
20031   /* Remove the topmost context from the stack.  */
20032   context = parser->context;
20033   parser->context = context->next;
20034   /* If no parse errors occurred, commit to the tentative parse.  */
20035   if (!error_occurred)
20036     {
20037       /* Commit to the tokens read tentatively, unless that was
20038          already done.  */
20039       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
20040         cp_lexer_commit_tokens (parser->lexer);
20041
20042       pop_to_parent_deferring_access_checks ();
20043     }
20044   /* Otherwise, if errors occurred, roll back our state so that things
20045      are just as they were before we began the tentative parse.  */
20046   else
20047     {
20048       cp_lexer_rollback_tokens (parser->lexer);
20049       pop_deferring_access_checks ();
20050     }
20051   /* Add the context to the front of the free list.  */
20052   context->next = cp_parser_context_free_list;
20053   cp_parser_context_free_list = context;
20054
20055   return !error_occurred;
20056 }
20057
20058 /* Returns true if we are parsing tentatively and are not committed to
20059    this tentative parse.  */
20060
20061 static bool
20062 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
20063 {
20064   return (cp_parser_parsing_tentatively (parser)
20065           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
20066 }
20067
20068 /* Returns nonzero iff an error has occurred during the most recent
20069    tentative parse.  */
20070
20071 static bool
20072 cp_parser_error_occurred (cp_parser* parser)
20073 {
20074   return (cp_parser_parsing_tentatively (parser)
20075           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
20076 }
20077
20078 /* Returns nonzero if GNU extensions are allowed.  */
20079
20080 static bool
20081 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
20082 {
20083   return parser->allow_gnu_extensions_p;
20084 }
20085 \f
20086 /* Objective-C++ Productions */
20087
20088
20089 /* Parse an Objective-C expression, which feeds into a primary-expression
20090    above.
20091
20092    objc-expression:
20093      objc-message-expression
20094      objc-string-literal
20095      objc-encode-expression
20096      objc-protocol-expression
20097      objc-selector-expression
20098
20099   Returns a tree representation of the expression.  */
20100
20101 static tree
20102 cp_parser_objc_expression (cp_parser* parser)
20103 {
20104   /* Try to figure out what kind of declaration is present.  */
20105   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20106
20107   switch (kwd->type)
20108     {
20109     case CPP_OPEN_SQUARE:
20110       return cp_parser_objc_message_expression (parser);
20111
20112     case CPP_OBJC_STRING:
20113       kwd = cp_lexer_consume_token (parser->lexer);
20114       return objc_build_string_object (kwd->u.value);
20115
20116     case CPP_KEYWORD:
20117       switch (kwd->keyword)
20118         {
20119         case RID_AT_ENCODE:
20120           return cp_parser_objc_encode_expression (parser);
20121
20122         case RID_AT_PROTOCOL:
20123           return cp_parser_objc_protocol_expression (parser);
20124
20125         case RID_AT_SELECTOR:
20126           return cp_parser_objc_selector_expression (parser);
20127
20128         default:
20129           break;
20130         }
20131     default:
20132       error_at (kwd->location,
20133                 "misplaced %<@%D%> Objective-C++ construct",
20134                 kwd->u.value);
20135       cp_parser_skip_to_end_of_block_or_statement (parser);
20136     }
20137
20138   return error_mark_node;
20139 }
20140
20141 /* Parse an Objective-C message expression.
20142
20143    objc-message-expression:
20144      [ objc-message-receiver objc-message-args ]
20145
20146    Returns a representation of an Objective-C message.  */
20147
20148 static tree
20149 cp_parser_objc_message_expression (cp_parser* parser)
20150 {
20151   tree receiver, messageargs;
20152
20153   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20154   receiver = cp_parser_objc_message_receiver (parser);
20155   messageargs = cp_parser_objc_message_args (parser);
20156   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20157
20158   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20159 }
20160
20161 /* Parse an objc-message-receiver.
20162
20163    objc-message-receiver:
20164      expression
20165      simple-type-specifier
20166
20167   Returns a representation of the type or expression.  */
20168
20169 static tree
20170 cp_parser_objc_message_receiver (cp_parser* parser)
20171 {
20172   tree rcv;
20173
20174   /* An Objective-C message receiver may be either (1) a type
20175      or (2) an expression.  */
20176   cp_parser_parse_tentatively (parser);
20177   rcv = cp_parser_expression (parser, false, NULL);
20178
20179   if (cp_parser_parse_definitely (parser))
20180     return rcv;
20181
20182   rcv = cp_parser_simple_type_specifier (parser,
20183                                          /*decl_specs=*/NULL,
20184                                          CP_PARSER_FLAGS_NONE);
20185
20186   return objc_get_class_reference (rcv);
20187 }
20188
20189 /* Parse the arguments and selectors comprising an Objective-C message.
20190
20191    objc-message-args:
20192      objc-selector
20193      objc-selector-args
20194      objc-selector-args , objc-comma-args
20195
20196    objc-selector-args:
20197      objc-selector [opt] : assignment-expression
20198      objc-selector-args objc-selector [opt] : assignment-expression
20199
20200    objc-comma-args:
20201      assignment-expression
20202      objc-comma-args , assignment-expression
20203
20204    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20205    selector arguments and TREE_VALUE containing a list of comma
20206    arguments.  */
20207
20208 static tree
20209 cp_parser_objc_message_args (cp_parser* parser)
20210 {
20211   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20212   bool maybe_unary_selector_p = true;
20213   cp_token *token = cp_lexer_peek_token (parser->lexer);
20214
20215   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20216     {
20217       tree selector = NULL_TREE, arg;
20218
20219       if (token->type != CPP_COLON)
20220         selector = cp_parser_objc_selector (parser);
20221
20222       /* Detect if we have a unary selector.  */
20223       if (maybe_unary_selector_p
20224           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20225         return build_tree_list (selector, NULL_TREE);
20226
20227       maybe_unary_selector_p = false;
20228       cp_parser_require (parser, CPP_COLON, "%<:%>");
20229       arg = cp_parser_assignment_expression (parser, false, NULL);
20230
20231       sel_args
20232         = chainon (sel_args,
20233                    build_tree_list (selector, arg));
20234
20235       token = cp_lexer_peek_token (parser->lexer);
20236     }
20237
20238   /* Handle non-selector arguments, if any. */
20239   while (token->type == CPP_COMMA)
20240     {
20241       tree arg;
20242
20243       cp_lexer_consume_token (parser->lexer);
20244       arg = cp_parser_assignment_expression (parser, false, NULL);
20245
20246       addl_args
20247         = chainon (addl_args,
20248                    build_tree_list (NULL_TREE, arg));
20249
20250       token = cp_lexer_peek_token (parser->lexer);
20251     }
20252
20253   return build_tree_list (sel_args, addl_args);
20254 }
20255
20256 /* Parse an Objective-C encode expression.
20257
20258    objc-encode-expression:
20259      @encode objc-typename
20260
20261    Returns an encoded representation of the type argument.  */
20262
20263 static tree
20264 cp_parser_objc_encode_expression (cp_parser* parser)
20265 {
20266   tree type;
20267   cp_token *token;
20268
20269   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20270   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20271   token = cp_lexer_peek_token (parser->lexer);
20272   type = complete_type (cp_parser_type_id (parser));
20273   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20274
20275   if (!type)
20276     {
20277       error_at (token->location, 
20278                 "%<@encode%> must specify a type as an argument");
20279       return error_mark_node;
20280     }
20281
20282   return objc_build_encode_expr (type);
20283 }
20284
20285 /* Parse an Objective-C @defs expression.  */
20286
20287 static tree
20288 cp_parser_objc_defs_expression (cp_parser *parser)
20289 {
20290   tree name;
20291
20292   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20293   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20294   name = cp_parser_identifier (parser);
20295   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20296
20297   return objc_get_class_ivars (name);
20298 }
20299
20300 /* Parse an Objective-C protocol expression.
20301
20302   objc-protocol-expression:
20303     @protocol ( identifier )
20304
20305   Returns a representation of the protocol expression.  */
20306
20307 static tree
20308 cp_parser_objc_protocol_expression (cp_parser* parser)
20309 {
20310   tree proto;
20311
20312   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20313   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20314   proto = cp_parser_identifier (parser);
20315   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20316
20317   return objc_build_protocol_expr (proto);
20318 }
20319
20320 /* Parse an Objective-C selector expression.
20321
20322    objc-selector-expression:
20323      @selector ( objc-method-signature )
20324
20325    objc-method-signature:
20326      objc-selector
20327      objc-selector-seq
20328
20329    objc-selector-seq:
20330      objc-selector :
20331      objc-selector-seq objc-selector :
20332
20333   Returns a representation of the method selector.  */
20334
20335 static tree
20336 cp_parser_objc_selector_expression (cp_parser* parser)
20337 {
20338   tree sel_seq = NULL_TREE;
20339   bool maybe_unary_selector_p = true;
20340   cp_token *token;
20341   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20342
20343   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20344   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20345   token = cp_lexer_peek_token (parser->lexer);
20346
20347   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20348          || token->type == CPP_SCOPE)
20349     {
20350       tree selector = NULL_TREE;
20351
20352       if (token->type != CPP_COLON
20353           || token->type == CPP_SCOPE)
20354         selector = cp_parser_objc_selector (parser);
20355
20356       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20357           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20358         {
20359           /* Detect if we have a unary selector.  */
20360           if (maybe_unary_selector_p)
20361             {
20362               sel_seq = selector;
20363               goto finish_selector;
20364             }
20365           else
20366             {
20367               cp_parser_error (parser, "expected %<:%>");
20368             }
20369         }
20370       maybe_unary_selector_p = false;
20371       token = cp_lexer_consume_token (parser->lexer);
20372
20373       if (token->type == CPP_SCOPE)
20374         {
20375           sel_seq
20376             = chainon (sel_seq,
20377                        build_tree_list (selector, NULL_TREE));
20378           sel_seq
20379             = chainon (sel_seq,
20380                        build_tree_list (NULL_TREE, NULL_TREE));
20381         }
20382       else
20383         sel_seq
20384           = chainon (sel_seq,
20385                      build_tree_list (selector, NULL_TREE));
20386
20387       token = cp_lexer_peek_token (parser->lexer);
20388     }
20389
20390  finish_selector:
20391   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20392
20393   return objc_build_selector_expr (loc, sel_seq);
20394 }
20395
20396 /* Parse a list of identifiers.
20397
20398    objc-identifier-list:
20399      identifier
20400      objc-identifier-list , identifier
20401
20402    Returns a TREE_LIST of identifier nodes.  */
20403
20404 static tree
20405 cp_parser_objc_identifier_list (cp_parser* parser)
20406 {
20407   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20408   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20409
20410   while (sep->type == CPP_COMMA)
20411     {
20412       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20413       list = chainon (list,
20414                       build_tree_list (NULL_TREE,
20415                                        cp_parser_identifier (parser)));
20416       sep = cp_lexer_peek_token (parser->lexer);
20417     }
20418
20419   return list;
20420 }
20421
20422 /* Parse an Objective-C alias declaration.
20423
20424    objc-alias-declaration:
20425      @compatibility_alias identifier identifier ;
20426
20427    This function registers the alias mapping with the Objective-C front end.
20428    It returns nothing.  */
20429
20430 static void
20431 cp_parser_objc_alias_declaration (cp_parser* parser)
20432 {
20433   tree alias, orig;
20434
20435   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20436   alias = cp_parser_identifier (parser);
20437   orig = cp_parser_identifier (parser);
20438   objc_declare_alias (alias, orig);
20439   cp_parser_consume_semicolon_at_end_of_statement (parser);
20440 }
20441
20442 /* Parse an Objective-C class forward-declaration.
20443
20444    objc-class-declaration:
20445      @class objc-identifier-list ;
20446
20447    The function registers the forward declarations with the Objective-C
20448    front end.  It returns nothing.  */
20449
20450 static void
20451 cp_parser_objc_class_declaration (cp_parser* parser)
20452 {
20453   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20454   objc_declare_class (cp_parser_objc_identifier_list (parser));
20455   cp_parser_consume_semicolon_at_end_of_statement (parser);
20456 }
20457
20458 /* Parse a list of Objective-C protocol references.
20459
20460    objc-protocol-refs-opt:
20461      objc-protocol-refs [opt]
20462
20463    objc-protocol-refs:
20464      < objc-identifier-list >
20465
20466    Returns a TREE_LIST of identifiers, if any.  */
20467
20468 static tree
20469 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20470 {
20471   tree protorefs = NULL_TREE;
20472
20473   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20474     {
20475       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20476       protorefs = cp_parser_objc_identifier_list (parser);
20477       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20478     }
20479
20480   return protorefs;
20481 }
20482
20483 /* Parse a Objective-C visibility specification.  */
20484
20485 static void
20486 cp_parser_objc_visibility_spec (cp_parser* parser)
20487 {
20488   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20489
20490   switch (vis->keyword)
20491     {
20492     case RID_AT_PRIVATE:
20493       objc_set_visibility (2);
20494       break;
20495     case RID_AT_PROTECTED:
20496       objc_set_visibility (0);
20497       break;
20498     case RID_AT_PUBLIC:
20499       objc_set_visibility (1);
20500       break;
20501     default:
20502       return;
20503     }
20504
20505   /* Eat '@private'/'@protected'/'@public'.  */
20506   cp_lexer_consume_token (parser->lexer);
20507 }
20508
20509 /* Parse an Objective-C method type.  */
20510
20511 static void
20512 cp_parser_objc_method_type (cp_parser* parser)
20513 {
20514   objc_set_method_type
20515    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20516     ? PLUS_EXPR
20517     : MINUS_EXPR);
20518 }
20519
20520 /* Parse an Objective-C protocol qualifier.  */
20521
20522 static tree
20523 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20524 {
20525   tree quals = NULL_TREE, node;
20526   cp_token *token = cp_lexer_peek_token (parser->lexer);
20527
20528   node = token->u.value;
20529
20530   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20531          && (node == ridpointers [(int) RID_IN]
20532              || node == ridpointers [(int) RID_OUT]
20533              || node == ridpointers [(int) RID_INOUT]
20534              || node == ridpointers [(int) RID_BYCOPY]
20535              || node == ridpointers [(int) RID_BYREF]
20536              || node == ridpointers [(int) RID_ONEWAY]))
20537     {
20538       quals = tree_cons (NULL_TREE, node, quals);
20539       cp_lexer_consume_token (parser->lexer);
20540       token = cp_lexer_peek_token (parser->lexer);
20541       node = token->u.value;
20542     }
20543
20544   return quals;
20545 }
20546
20547 /* Parse an Objective-C typename.  */
20548
20549 static tree
20550 cp_parser_objc_typename (cp_parser* parser)
20551 {
20552   tree type_name = NULL_TREE;
20553
20554   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20555     {
20556       tree proto_quals, cp_type = NULL_TREE;
20557
20558       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20559       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20560
20561       /* An ObjC type name may consist of just protocol qualifiers, in which
20562          case the type shall default to 'id'.  */
20563       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20564         cp_type = cp_parser_type_id (parser);
20565
20566       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20567       type_name = build_tree_list (proto_quals, cp_type);
20568     }
20569
20570   return type_name;
20571 }
20572
20573 /* Check to see if TYPE refers to an Objective-C selector name.  */
20574
20575 static bool
20576 cp_parser_objc_selector_p (enum cpp_ttype type)
20577 {
20578   return (type == CPP_NAME || type == CPP_KEYWORD
20579           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20580           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20581           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20582           || type == CPP_XOR || type == CPP_XOR_EQ);
20583 }
20584
20585 /* Parse an Objective-C selector.  */
20586
20587 static tree
20588 cp_parser_objc_selector (cp_parser* parser)
20589 {
20590   cp_token *token = cp_lexer_consume_token (parser->lexer);
20591
20592   if (!cp_parser_objc_selector_p (token->type))
20593     {
20594       error_at (token->location, "invalid Objective-C++ selector name");
20595       return error_mark_node;
20596     }
20597
20598   /* C++ operator names are allowed to appear in ObjC selectors.  */
20599   switch (token->type)
20600     {
20601     case CPP_AND_AND: return get_identifier ("and");
20602     case CPP_AND_EQ: return get_identifier ("and_eq");
20603     case CPP_AND: return get_identifier ("bitand");
20604     case CPP_OR: return get_identifier ("bitor");
20605     case CPP_COMPL: return get_identifier ("compl");
20606     case CPP_NOT: return get_identifier ("not");
20607     case CPP_NOT_EQ: return get_identifier ("not_eq");
20608     case CPP_OR_OR: return get_identifier ("or");
20609     case CPP_OR_EQ: return get_identifier ("or_eq");
20610     case CPP_XOR: return get_identifier ("xor");
20611     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20612     default: return token->u.value;
20613     }
20614 }
20615
20616 /* Parse an Objective-C params list.  */
20617
20618 static tree
20619 cp_parser_objc_method_keyword_params (cp_parser* parser)
20620 {
20621   tree params = NULL_TREE;
20622   bool maybe_unary_selector_p = true;
20623   cp_token *token = cp_lexer_peek_token (parser->lexer);
20624
20625   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20626     {
20627       tree selector = NULL_TREE, type_name, identifier;
20628
20629       if (token->type != CPP_COLON)
20630         selector = cp_parser_objc_selector (parser);
20631
20632       /* Detect if we have a unary selector.  */
20633       if (maybe_unary_selector_p
20634           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20635         return selector;
20636
20637       maybe_unary_selector_p = false;
20638       cp_parser_require (parser, CPP_COLON, "%<:%>");
20639       type_name = cp_parser_objc_typename (parser);
20640       identifier = cp_parser_identifier (parser);
20641
20642       params
20643         = chainon (params,
20644                    objc_build_keyword_decl (selector,
20645                                             type_name,
20646                                             identifier));
20647
20648       token = cp_lexer_peek_token (parser->lexer);
20649     }
20650
20651   return params;
20652 }
20653
20654 /* Parse the non-keyword Objective-C params.  */
20655
20656 static tree
20657 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20658 {
20659   tree params = make_node (TREE_LIST);
20660   cp_token *token = cp_lexer_peek_token (parser->lexer);
20661   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20662
20663   while (token->type == CPP_COMMA)
20664     {
20665       cp_parameter_declarator *parmdecl;
20666       tree parm;
20667
20668       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20669       token = cp_lexer_peek_token (parser->lexer);
20670
20671       if (token->type == CPP_ELLIPSIS)
20672         {
20673           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20674           *ellipsisp = true;
20675           break;
20676         }
20677
20678       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20679       parm = grokdeclarator (parmdecl->declarator,
20680                              &parmdecl->decl_specifiers,
20681                              PARM, /*initialized=*/0,
20682                              /*attrlist=*/NULL);
20683
20684       chainon (params, build_tree_list (NULL_TREE, parm));
20685       token = cp_lexer_peek_token (parser->lexer);
20686     }
20687
20688   return params;
20689 }
20690
20691 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20692
20693 static void
20694 cp_parser_objc_interstitial_code (cp_parser* parser)
20695 {
20696   cp_token *token = cp_lexer_peek_token (parser->lexer);
20697
20698   /* If the next token is `extern' and the following token is a string
20699      literal, then we have a linkage specification.  */
20700   if (token->keyword == RID_EXTERN
20701       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20702     cp_parser_linkage_specification (parser);
20703   /* Handle #pragma, if any.  */
20704   else if (token->type == CPP_PRAGMA)
20705     cp_parser_pragma (parser, pragma_external);
20706   /* Allow stray semicolons.  */
20707   else if (token->type == CPP_SEMICOLON)
20708     cp_lexer_consume_token (parser->lexer);
20709   /* Finally, try to parse a block-declaration, or a function-definition.  */
20710   else
20711     cp_parser_block_declaration (parser, /*statement_p=*/false);
20712 }
20713
20714 /* Parse a method signature.  */
20715
20716 static tree
20717 cp_parser_objc_method_signature (cp_parser* parser)
20718 {
20719   tree rettype, kwdparms, optparms;
20720   bool ellipsis = false;
20721
20722   cp_parser_objc_method_type (parser);
20723   rettype = cp_parser_objc_typename (parser);
20724   kwdparms = cp_parser_objc_method_keyword_params (parser);
20725   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20726
20727   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20728 }
20729
20730 /* Pars an Objective-C method prototype list.  */
20731
20732 static void
20733 cp_parser_objc_method_prototype_list (cp_parser* parser)
20734 {
20735   cp_token *token = cp_lexer_peek_token (parser->lexer);
20736
20737   while (token->keyword != RID_AT_END)
20738     {
20739       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20740         {
20741           objc_add_method_declaration
20742            (cp_parser_objc_method_signature (parser));
20743           cp_parser_consume_semicolon_at_end_of_statement (parser);
20744         }
20745       else
20746         /* Allow for interspersed non-ObjC++ code.  */
20747         cp_parser_objc_interstitial_code (parser);
20748
20749       token = cp_lexer_peek_token (parser->lexer);
20750     }
20751
20752   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20753   objc_finish_interface ();
20754 }
20755
20756 /* Parse an Objective-C method definition list.  */
20757
20758 static void
20759 cp_parser_objc_method_definition_list (cp_parser* parser)
20760 {
20761   cp_token *token = cp_lexer_peek_token (parser->lexer);
20762
20763   while (token->keyword != RID_AT_END)
20764     {
20765       tree meth;
20766
20767       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20768         {
20769           push_deferring_access_checks (dk_deferred);
20770           objc_start_method_definition
20771            (cp_parser_objc_method_signature (parser));
20772
20773           /* For historical reasons, we accept an optional semicolon.  */
20774           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20775             cp_lexer_consume_token (parser->lexer);
20776
20777           perform_deferred_access_checks ();
20778           stop_deferring_access_checks ();
20779           meth = cp_parser_function_definition_after_declarator (parser,
20780                                                                  false);
20781           pop_deferring_access_checks ();
20782           objc_finish_method_definition (meth);
20783         }
20784       else
20785         /* Allow for interspersed non-ObjC++ code.  */
20786         cp_parser_objc_interstitial_code (parser);
20787
20788       token = cp_lexer_peek_token (parser->lexer);
20789     }
20790
20791   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20792   objc_finish_implementation ();
20793 }
20794
20795 /* Parse Objective-C ivars.  */
20796
20797 static void
20798 cp_parser_objc_class_ivars (cp_parser* parser)
20799 {
20800   cp_token *token = cp_lexer_peek_token (parser->lexer);
20801
20802   if (token->type != CPP_OPEN_BRACE)
20803     return;     /* No ivars specified.  */
20804
20805   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20806   token = cp_lexer_peek_token (parser->lexer);
20807
20808   while (token->type != CPP_CLOSE_BRACE)
20809     {
20810       cp_decl_specifier_seq declspecs;
20811       int decl_class_or_enum_p;
20812       tree prefix_attributes;
20813
20814       cp_parser_objc_visibility_spec (parser);
20815
20816       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20817         break;
20818
20819       cp_parser_decl_specifier_seq (parser,
20820                                     CP_PARSER_FLAGS_OPTIONAL,
20821                                     &declspecs,
20822                                     &decl_class_or_enum_p);
20823       prefix_attributes = declspecs.attributes;
20824       declspecs.attributes = NULL_TREE;
20825
20826       /* Keep going until we hit the `;' at the end of the
20827          declaration.  */
20828       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20829         {
20830           tree width = NULL_TREE, attributes, first_attribute, decl;
20831           cp_declarator *declarator = NULL;
20832           int ctor_dtor_or_conv_p;
20833
20834           /* Check for a (possibly unnamed) bitfield declaration.  */
20835           token = cp_lexer_peek_token (parser->lexer);
20836           if (token->type == CPP_COLON)
20837             goto eat_colon;
20838
20839           if (token->type == CPP_NAME
20840               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20841                   == CPP_COLON))
20842             {
20843               /* Get the name of the bitfield.  */
20844               declarator = make_id_declarator (NULL_TREE,
20845                                                cp_parser_identifier (parser),
20846                                                sfk_none);
20847
20848              eat_colon:
20849               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20850               /* Get the width of the bitfield.  */
20851               width
20852                 = cp_parser_constant_expression (parser,
20853                                                  /*allow_non_constant=*/false,
20854                                                  NULL);
20855             }
20856           else
20857             {
20858               /* Parse the declarator.  */
20859               declarator
20860                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20861                                         &ctor_dtor_or_conv_p,
20862                                         /*parenthesized_p=*/NULL,
20863                                         /*member_p=*/false);
20864             }
20865
20866           /* Look for attributes that apply to the ivar.  */
20867           attributes = cp_parser_attributes_opt (parser);
20868           /* Remember which attributes are prefix attributes and
20869              which are not.  */
20870           first_attribute = attributes;
20871           /* Combine the attributes.  */
20872           attributes = chainon (prefix_attributes, attributes);
20873
20874           if (width)
20875               /* Create the bitfield declaration.  */
20876               decl = grokbitfield (declarator, &declspecs,
20877                                    width,
20878                                    attributes);
20879           else
20880             decl = grokfield (declarator, &declspecs,
20881                               NULL_TREE, /*init_const_expr_p=*/false,
20882                               NULL_TREE, attributes);
20883
20884           /* Add the instance variable.  */
20885           objc_add_instance_variable (decl);
20886
20887           /* Reset PREFIX_ATTRIBUTES.  */
20888           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20889             attributes = TREE_CHAIN (attributes);
20890           if (attributes)
20891             TREE_CHAIN (attributes) = NULL_TREE;
20892
20893           token = cp_lexer_peek_token (parser->lexer);
20894
20895           if (token->type == CPP_COMMA)
20896             {
20897               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20898               continue;
20899             }
20900           break;
20901         }
20902
20903       cp_parser_consume_semicolon_at_end_of_statement (parser);
20904       token = cp_lexer_peek_token (parser->lexer);
20905     }
20906
20907   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20908   /* For historical reasons, we accept an optional semicolon.  */
20909   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20910     cp_lexer_consume_token (parser->lexer);
20911 }
20912
20913 /* Parse an Objective-C protocol declaration.  */
20914
20915 static void
20916 cp_parser_objc_protocol_declaration (cp_parser* parser)
20917 {
20918   tree proto, protorefs;
20919   cp_token *tok;
20920
20921   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20922   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20923     {
20924       tok = cp_lexer_peek_token (parser->lexer);
20925       error_at (tok->location, "identifier expected after %<@protocol%>");
20926       goto finish;
20927     }
20928
20929   /* See if we have a forward declaration or a definition.  */
20930   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20931
20932   /* Try a forward declaration first.  */
20933   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20934     {
20935       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20936      finish:
20937       cp_parser_consume_semicolon_at_end_of_statement (parser);
20938     }
20939
20940   /* Ok, we got a full-fledged definition (or at least should).  */
20941   else
20942     {
20943       proto = cp_parser_identifier (parser);
20944       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20945       objc_start_protocol (proto, protorefs);
20946       cp_parser_objc_method_prototype_list (parser);
20947     }
20948 }
20949
20950 /* Parse an Objective-C superclass or category.  */
20951
20952 static void
20953 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20954                                                           tree *categ)
20955 {
20956   cp_token *next = cp_lexer_peek_token (parser->lexer);
20957
20958   *super = *categ = NULL_TREE;
20959   if (next->type == CPP_COLON)
20960     {
20961       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20962       *super = cp_parser_identifier (parser);
20963     }
20964   else if (next->type == CPP_OPEN_PAREN)
20965     {
20966       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20967       *categ = cp_parser_identifier (parser);
20968       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20969     }
20970 }
20971
20972 /* Parse an Objective-C class interface.  */
20973
20974 static void
20975 cp_parser_objc_class_interface (cp_parser* parser)
20976 {
20977   tree name, super, categ, protos;
20978
20979   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20980   name = cp_parser_identifier (parser);
20981   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20982   protos = cp_parser_objc_protocol_refs_opt (parser);
20983
20984   /* We have either a class or a category on our hands.  */
20985   if (categ)
20986     objc_start_category_interface (name, categ, protos);
20987   else
20988     {
20989       objc_start_class_interface (name, super, protos);
20990       /* Handle instance variable declarations, if any.  */
20991       cp_parser_objc_class_ivars (parser);
20992       objc_continue_interface ();
20993     }
20994
20995   cp_parser_objc_method_prototype_list (parser);
20996 }
20997
20998 /* Parse an Objective-C class implementation.  */
20999
21000 static void
21001 cp_parser_objc_class_implementation (cp_parser* parser)
21002 {
21003   tree name, super, categ;
21004
21005   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
21006   name = cp_parser_identifier (parser);
21007   cp_parser_objc_superclass_or_category (parser, &super, &categ);
21008
21009   /* We have either a class or a category on our hands.  */
21010   if (categ)
21011     objc_start_category_implementation (name, categ);
21012   else
21013     {
21014       objc_start_class_implementation (name, super);
21015       /* Handle instance variable declarations, if any.  */
21016       cp_parser_objc_class_ivars (parser);
21017       objc_continue_implementation ();
21018     }
21019
21020   cp_parser_objc_method_definition_list (parser);
21021 }
21022
21023 /* Consume the @end token and finish off the implementation.  */
21024
21025 static void
21026 cp_parser_objc_end_implementation (cp_parser* parser)
21027 {
21028   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
21029   objc_finish_implementation ();
21030 }
21031
21032 /* Parse an Objective-C declaration.  */
21033
21034 static void
21035 cp_parser_objc_declaration (cp_parser* parser)
21036 {
21037   /* Try to figure out what kind of declaration is present.  */
21038   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21039
21040   switch (kwd->keyword)
21041     {
21042     case RID_AT_ALIAS:
21043       cp_parser_objc_alias_declaration (parser);
21044       break;
21045     case RID_AT_CLASS:
21046       cp_parser_objc_class_declaration (parser);
21047       break;
21048     case RID_AT_PROTOCOL:
21049       cp_parser_objc_protocol_declaration (parser);
21050       break;
21051     case RID_AT_INTERFACE:
21052       cp_parser_objc_class_interface (parser);
21053       break;
21054     case RID_AT_IMPLEMENTATION:
21055       cp_parser_objc_class_implementation (parser);
21056       break;
21057     case RID_AT_END:
21058       cp_parser_objc_end_implementation (parser);
21059       break;
21060     default:
21061       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21062                 kwd->u.value);
21063       cp_parser_skip_to_end_of_block_or_statement (parser);
21064     }
21065 }
21066
21067 /* Parse an Objective-C try-catch-finally statement.
21068
21069    objc-try-catch-finally-stmt:
21070      @try compound-statement objc-catch-clause-seq [opt]
21071        objc-finally-clause [opt]
21072
21073    objc-catch-clause-seq:
21074      objc-catch-clause objc-catch-clause-seq [opt]
21075
21076    objc-catch-clause:
21077      @catch ( exception-declaration ) compound-statement
21078
21079    objc-finally-clause
21080      @finally compound-statement
21081
21082    Returns NULL_TREE.  */
21083
21084 static tree
21085 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
21086   location_t location;
21087   tree stmt;
21088
21089   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
21090   location = cp_lexer_peek_token (parser->lexer)->location;
21091   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
21092      node, lest it get absorbed into the surrounding block.  */
21093   stmt = push_stmt_list ();
21094   cp_parser_compound_statement (parser, NULL, false);
21095   objc_begin_try_stmt (location, pop_stmt_list (stmt));
21096
21097   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
21098     {
21099       cp_parameter_declarator *parmdecl;
21100       tree parm;
21101
21102       cp_lexer_consume_token (parser->lexer);
21103       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21104       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
21105       parm = grokdeclarator (parmdecl->declarator,
21106                              &parmdecl->decl_specifiers,
21107                              PARM, /*initialized=*/0,
21108                              /*attrlist=*/NULL);
21109       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21110       objc_begin_catch_clause (parm);
21111       cp_parser_compound_statement (parser, NULL, false);
21112       objc_finish_catch_clause ();
21113     }
21114
21115   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
21116     {
21117       cp_lexer_consume_token (parser->lexer);
21118       location = cp_lexer_peek_token (parser->lexer)->location;
21119       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
21120          node, lest it get absorbed into the surrounding block.  */
21121       stmt = push_stmt_list ();
21122       cp_parser_compound_statement (parser, NULL, false);
21123       objc_build_finally_clause (location, pop_stmt_list (stmt));
21124     }
21125
21126   return objc_finish_try_stmt ();
21127 }
21128
21129 /* Parse an Objective-C synchronized statement.
21130
21131    objc-synchronized-stmt:
21132      @synchronized ( expression ) compound-statement
21133
21134    Returns NULL_TREE.  */
21135
21136 static tree
21137 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21138   location_t location;
21139   tree lock, stmt;
21140
21141   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21142
21143   location = cp_lexer_peek_token (parser->lexer)->location;
21144   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21145   lock = cp_parser_expression (parser, false, NULL);
21146   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21147
21148   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21149      node, lest it get absorbed into the surrounding block.  */
21150   stmt = push_stmt_list ();
21151   cp_parser_compound_statement (parser, NULL, false);
21152
21153   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21154 }
21155
21156 /* Parse an Objective-C throw statement.
21157
21158    objc-throw-stmt:
21159      @throw assignment-expression [opt] ;
21160
21161    Returns a constructed '@throw' statement.  */
21162
21163 static tree
21164 cp_parser_objc_throw_statement (cp_parser *parser) {
21165   tree expr = NULL_TREE;
21166   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21167
21168   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21169
21170   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21171     expr = cp_parser_assignment_expression (parser, false, NULL);
21172
21173   cp_parser_consume_semicolon_at_end_of_statement (parser);
21174
21175   return objc_build_throw_stmt (loc, expr);
21176 }
21177
21178 /* Parse an Objective-C statement.  */
21179
21180 static tree
21181 cp_parser_objc_statement (cp_parser * parser) {
21182   /* Try to figure out what kind of declaration is present.  */
21183   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21184
21185   switch (kwd->keyword)
21186     {
21187     case RID_AT_TRY:
21188       return cp_parser_objc_try_catch_finally_statement (parser);
21189     case RID_AT_SYNCHRONIZED:
21190       return cp_parser_objc_synchronized_statement (parser);
21191     case RID_AT_THROW:
21192       return cp_parser_objc_throw_statement (parser);
21193     default:
21194       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21195                kwd->u.value);
21196       cp_parser_skip_to_end_of_block_or_statement (parser);
21197     }
21198
21199   return error_mark_node;
21200 }
21201 \f
21202 /* OpenMP 2.5 parsing routines.  */
21203
21204 /* Returns name of the next clause.
21205    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21206    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21207    returned and the token is consumed.  */
21208
21209 static pragma_omp_clause
21210 cp_parser_omp_clause_name (cp_parser *parser)
21211 {
21212   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21213
21214   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21215     result = PRAGMA_OMP_CLAUSE_IF;
21216   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21217     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21218   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21219     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21220   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21221     {
21222       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21223       const char *p = IDENTIFIER_POINTER (id);
21224
21225       switch (p[0])
21226         {
21227         case 'c':
21228           if (!strcmp ("collapse", p))
21229             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21230           else if (!strcmp ("copyin", p))
21231             result = PRAGMA_OMP_CLAUSE_COPYIN;
21232           else if (!strcmp ("copyprivate", p))
21233             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21234           break;
21235         case 'f':
21236           if (!strcmp ("firstprivate", p))
21237             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21238           break;
21239         case 'l':
21240           if (!strcmp ("lastprivate", p))
21241             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21242           break;
21243         case 'n':
21244           if (!strcmp ("nowait", p))
21245             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21246           else if (!strcmp ("num_threads", p))
21247             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21248           break;
21249         case 'o':
21250           if (!strcmp ("ordered", p))
21251             result = PRAGMA_OMP_CLAUSE_ORDERED;
21252           break;
21253         case 'r':
21254           if (!strcmp ("reduction", p))
21255             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21256           break;
21257         case 's':
21258           if (!strcmp ("schedule", p))
21259             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21260           else if (!strcmp ("shared", p))
21261             result = PRAGMA_OMP_CLAUSE_SHARED;
21262           break;
21263         case 'u':
21264           if (!strcmp ("untied", p))
21265             result = PRAGMA_OMP_CLAUSE_UNTIED;
21266           break;
21267         }
21268     }
21269
21270   if (result != PRAGMA_OMP_CLAUSE_NONE)
21271     cp_lexer_consume_token (parser->lexer);
21272
21273   return result;
21274 }
21275
21276 /* Validate that a clause of the given type does not already exist.  */
21277
21278 static void
21279 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21280                            const char *name, location_t location)
21281 {
21282   tree c;
21283
21284   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21285     if (OMP_CLAUSE_CODE (c) == code)
21286       {
21287         error_at (location, "too many %qs clauses", name);
21288         break;
21289       }
21290 }
21291
21292 /* OpenMP 2.5:
21293    variable-list:
21294      identifier
21295      variable-list , identifier
21296
21297    In addition, we match a closing parenthesis.  An opening parenthesis
21298    will have been consumed by the caller.
21299
21300    If KIND is nonzero, create the appropriate node and install the decl
21301    in OMP_CLAUSE_DECL and add the node to the head of the list.
21302
21303    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21304    return the list created.  */
21305
21306 static tree
21307 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21308                                 tree list)
21309 {
21310   cp_token *token;
21311   while (1)
21312     {
21313       tree name, decl;
21314
21315       token = cp_lexer_peek_token (parser->lexer);
21316       name = cp_parser_id_expression (parser, /*template_p=*/false,
21317                                       /*check_dependency_p=*/true,
21318                                       /*template_p=*/NULL,
21319                                       /*declarator_p=*/false,
21320                                       /*optional_p=*/false);
21321       if (name == error_mark_node)
21322         goto skip_comma;
21323
21324       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21325       if (decl == error_mark_node)
21326         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21327       else if (kind != 0)
21328         {
21329           tree u = build_omp_clause (token->location, kind);
21330           OMP_CLAUSE_DECL (u) = decl;
21331           OMP_CLAUSE_CHAIN (u) = list;
21332           list = u;
21333         }
21334       else
21335         list = tree_cons (decl, NULL_TREE, list);
21336
21337     get_comma:
21338       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21339         break;
21340       cp_lexer_consume_token (parser->lexer);
21341     }
21342
21343   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21344     {
21345       int ending;
21346
21347       /* Try to resync to an unnested comma.  Copied from
21348          cp_parser_parenthesized_expression_list.  */
21349     skip_comma:
21350       ending = cp_parser_skip_to_closing_parenthesis (parser,
21351                                                       /*recovering=*/true,
21352                                                       /*or_comma=*/true,
21353                                                       /*consume_paren=*/true);
21354       if (ending < 0)
21355         goto get_comma;
21356     }
21357
21358   return list;
21359 }
21360
21361 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21362    common case for omp clauses.  */
21363
21364 static tree
21365 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21366 {
21367   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21368     return cp_parser_omp_var_list_no_open (parser, kind, list);
21369   return list;
21370 }
21371
21372 /* OpenMP 3.0:
21373    collapse ( constant-expression ) */
21374
21375 static tree
21376 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21377 {
21378   tree c, num;
21379   location_t loc;
21380   HOST_WIDE_INT n;
21381
21382   loc = cp_lexer_peek_token (parser->lexer)->location;
21383   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21384     return list;
21385
21386   num = cp_parser_constant_expression (parser, false, NULL);
21387
21388   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21389     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21390                                            /*or_comma=*/false,
21391                                            /*consume_paren=*/true);
21392
21393   if (num == error_mark_node)
21394     return list;
21395   num = fold_non_dependent_expr (num);
21396   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21397       || !host_integerp (num, 0)
21398       || (n = tree_low_cst (num, 0)) <= 0
21399       || (int) n != n)
21400     {
21401       error_at (loc, "collapse argument needs positive constant integer expression");
21402       return list;
21403     }
21404
21405   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21406   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21407   OMP_CLAUSE_CHAIN (c) = list;
21408   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21409
21410   return c;
21411 }
21412
21413 /* OpenMP 2.5:
21414    default ( shared | none ) */
21415
21416 static tree
21417 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21418 {
21419   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21420   tree c;
21421
21422   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21423     return list;
21424   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21425     {
21426       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21427       const char *p = IDENTIFIER_POINTER (id);
21428
21429       switch (p[0])
21430         {
21431         case 'n':
21432           if (strcmp ("none", p) != 0)
21433             goto invalid_kind;
21434           kind = OMP_CLAUSE_DEFAULT_NONE;
21435           break;
21436
21437         case 's':
21438           if (strcmp ("shared", p) != 0)
21439             goto invalid_kind;
21440           kind = OMP_CLAUSE_DEFAULT_SHARED;
21441           break;
21442
21443         default:
21444           goto invalid_kind;
21445         }
21446
21447       cp_lexer_consume_token (parser->lexer);
21448     }
21449   else
21450     {
21451     invalid_kind:
21452       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21453     }
21454
21455   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21456     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21457                                            /*or_comma=*/false,
21458                                            /*consume_paren=*/true);
21459
21460   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21461     return list;
21462
21463   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21464   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21465   OMP_CLAUSE_CHAIN (c) = list;
21466   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21467
21468   return c;
21469 }
21470
21471 /* OpenMP 2.5:
21472    if ( expression ) */
21473
21474 static tree
21475 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21476 {
21477   tree t, c;
21478
21479   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21480     return list;
21481
21482   t = cp_parser_condition (parser);
21483
21484   if (t == error_mark_node
21485       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21486     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21487                                            /*or_comma=*/false,
21488                                            /*consume_paren=*/true);
21489
21490   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21491
21492   c = build_omp_clause (location, OMP_CLAUSE_IF);
21493   OMP_CLAUSE_IF_EXPR (c) = t;
21494   OMP_CLAUSE_CHAIN (c) = list;
21495
21496   return c;
21497 }
21498
21499 /* OpenMP 2.5:
21500    nowait */
21501
21502 static tree
21503 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21504                              tree list, location_t location)
21505 {
21506   tree c;
21507
21508   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21509
21510   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21511   OMP_CLAUSE_CHAIN (c) = list;
21512   return c;
21513 }
21514
21515 /* OpenMP 2.5:
21516    num_threads ( expression ) */
21517
21518 static tree
21519 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21520                                   location_t location)
21521 {
21522   tree t, c;
21523
21524   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21525     return list;
21526
21527   t = cp_parser_expression (parser, false, NULL);
21528
21529   if (t == error_mark_node
21530       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21531     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21532                                            /*or_comma=*/false,
21533                                            /*consume_paren=*/true);
21534
21535   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21536                              "num_threads", location);
21537
21538   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21539   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21540   OMP_CLAUSE_CHAIN (c) = list;
21541
21542   return c;
21543 }
21544
21545 /* OpenMP 2.5:
21546    ordered */
21547
21548 static tree
21549 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21550                               tree list, location_t location)
21551 {
21552   tree c;
21553
21554   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21555                              "ordered", location);
21556
21557   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21558   OMP_CLAUSE_CHAIN (c) = list;
21559   return c;
21560 }
21561
21562 /* OpenMP 2.5:
21563    reduction ( reduction-operator : variable-list )
21564
21565    reduction-operator:
21566      One of: + * - & ^ | && || */
21567
21568 static tree
21569 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21570 {
21571   enum tree_code code;
21572   tree nlist, c;
21573
21574   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21575     return list;
21576
21577   switch (cp_lexer_peek_token (parser->lexer)->type)
21578     {
21579     case CPP_PLUS:
21580       code = PLUS_EXPR;
21581       break;
21582     case CPP_MULT:
21583       code = MULT_EXPR;
21584       break;
21585     case CPP_MINUS:
21586       code = MINUS_EXPR;
21587       break;
21588     case CPP_AND:
21589       code = BIT_AND_EXPR;
21590       break;
21591     case CPP_XOR:
21592       code = BIT_XOR_EXPR;
21593       break;
21594     case CPP_OR:
21595       code = BIT_IOR_EXPR;
21596       break;
21597     case CPP_AND_AND:
21598       code = TRUTH_ANDIF_EXPR;
21599       break;
21600     case CPP_OR_OR:
21601       code = TRUTH_ORIF_EXPR;
21602       break;
21603     default:
21604       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21605                                "%<|%>, %<&&%>, or %<||%>");
21606     resync_fail:
21607       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21608                                              /*or_comma=*/false,
21609                                              /*consume_paren=*/true);
21610       return list;
21611     }
21612   cp_lexer_consume_token (parser->lexer);
21613
21614   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21615     goto resync_fail;
21616
21617   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21618   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21619     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21620
21621   return nlist;
21622 }
21623
21624 /* OpenMP 2.5:
21625    schedule ( schedule-kind )
21626    schedule ( schedule-kind , expression )
21627
21628    schedule-kind:
21629      static | dynamic | guided | runtime | auto  */
21630
21631 static tree
21632 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21633 {
21634   tree c, t;
21635
21636   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21637     return list;
21638
21639   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21640
21641   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21642     {
21643       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21644       const char *p = IDENTIFIER_POINTER (id);
21645
21646       switch (p[0])
21647         {
21648         case 'd':
21649           if (strcmp ("dynamic", p) != 0)
21650             goto invalid_kind;
21651           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21652           break;
21653
21654         case 'g':
21655           if (strcmp ("guided", p) != 0)
21656             goto invalid_kind;
21657           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21658           break;
21659
21660         case 'r':
21661           if (strcmp ("runtime", p) != 0)
21662             goto invalid_kind;
21663           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21664           break;
21665
21666         default:
21667           goto invalid_kind;
21668         }
21669     }
21670   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21671     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21672   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21673     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21674   else
21675     goto invalid_kind;
21676   cp_lexer_consume_token (parser->lexer);
21677
21678   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21679     {
21680       cp_token *token;
21681       cp_lexer_consume_token (parser->lexer);
21682
21683       token = cp_lexer_peek_token (parser->lexer);
21684       t = cp_parser_assignment_expression (parser, false, NULL);
21685
21686       if (t == error_mark_node)
21687         goto resync_fail;
21688       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21689         error_at (token->location, "schedule %<runtime%> does not take "
21690                   "a %<chunk_size%> parameter");
21691       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21692         error_at (token->location, "schedule %<auto%> does not take "
21693                   "a %<chunk_size%> parameter");
21694       else
21695         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21696
21697       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21698         goto resync_fail;
21699     }
21700   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21701     goto resync_fail;
21702
21703   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21704   OMP_CLAUSE_CHAIN (c) = list;
21705   return c;
21706
21707  invalid_kind:
21708   cp_parser_error (parser, "invalid schedule kind");
21709  resync_fail:
21710   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21711                                          /*or_comma=*/false,
21712                                          /*consume_paren=*/true);
21713   return list;
21714 }
21715
21716 /* OpenMP 3.0:
21717    untied */
21718
21719 static tree
21720 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21721                              tree list, location_t location)
21722 {
21723   tree c;
21724
21725   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21726
21727   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21728   OMP_CLAUSE_CHAIN (c) = list;
21729   return c;
21730 }
21731
21732 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21733    is a bitmask in MASK.  Return the list of clauses found; the result
21734    of clause default goes in *pdefault.  */
21735
21736 static tree
21737 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21738                            const char *where, cp_token *pragma_tok)
21739 {
21740   tree clauses = NULL;
21741   bool first = true;
21742   cp_token *token = NULL;
21743
21744   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21745     {
21746       pragma_omp_clause c_kind;
21747       const char *c_name;
21748       tree prev = clauses;
21749
21750       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21751         cp_lexer_consume_token (parser->lexer);
21752
21753       token = cp_lexer_peek_token (parser->lexer);
21754       c_kind = cp_parser_omp_clause_name (parser);
21755       first = false;
21756
21757       switch (c_kind)
21758         {
21759         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21760           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21761                                                    token->location);
21762           c_name = "collapse";
21763           break;
21764         case PRAGMA_OMP_CLAUSE_COPYIN:
21765           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21766           c_name = "copyin";
21767           break;
21768         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21769           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21770                                             clauses);
21771           c_name = "copyprivate";
21772           break;
21773         case PRAGMA_OMP_CLAUSE_DEFAULT:
21774           clauses = cp_parser_omp_clause_default (parser, clauses,
21775                                                   token->location);
21776           c_name = "default";
21777           break;
21778         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21779           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21780                                             clauses);
21781           c_name = "firstprivate";
21782           break;
21783         case PRAGMA_OMP_CLAUSE_IF:
21784           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21785           c_name = "if";
21786           break;
21787         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21788           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21789                                             clauses);
21790           c_name = "lastprivate";
21791           break;
21792         case PRAGMA_OMP_CLAUSE_NOWAIT:
21793           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21794           c_name = "nowait";
21795           break;
21796         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21797           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21798                                                       token->location);
21799           c_name = "num_threads";
21800           break;
21801         case PRAGMA_OMP_CLAUSE_ORDERED:
21802           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21803                                                   token->location);
21804           c_name = "ordered";
21805           break;
21806         case PRAGMA_OMP_CLAUSE_PRIVATE:
21807           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21808                                             clauses);
21809           c_name = "private";
21810           break;
21811         case PRAGMA_OMP_CLAUSE_REDUCTION:
21812           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21813           c_name = "reduction";
21814           break;
21815         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21816           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21817                                                    token->location);
21818           c_name = "schedule";
21819           break;
21820         case PRAGMA_OMP_CLAUSE_SHARED:
21821           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21822                                             clauses);
21823           c_name = "shared";
21824           break;
21825         case PRAGMA_OMP_CLAUSE_UNTIED:
21826           clauses = cp_parser_omp_clause_untied (parser, clauses,
21827                                                  token->location);
21828           c_name = "nowait";
21829           break;
21830         default:
21831           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21832           goto saw_error;
21833         }
21834
21835       if (((mask >> c_kind) & 1) == 0)
21836         {
21837           /* Remove the invalid clause(s) from the list to avoid
21838              confusing the rest of the compiler.  */
21839           clauses = prev;
21840           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21841         }
21842     }
21843  saw_error:
21844   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21845   return finish_omp_clauses (clauses);
21846 }
21847
21848 /* OpenMP 2.5:
21849    structured-block:
21850      statement
21851
21852    In practice, we're also interested in adding the statement to an
21853    outer node.  So it is convenient if we work around the fact that
21854    cp_parser_statement calls add_stmt.  */
21855
21856 static unsigned
21857 cp_parser_begin_omp_structured_block (cp_parser *parser)
21858 {
21859   unsigned save = parser->in_statement;
21860
21861   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21862      This preserves the "not within loop or switch" style error messages
21863      for nonsense cases like
21864         void foo() {
21865         #pragma omp single
21866           break;
21867         }
21868   */
21869   if (parser->in_statement)
21870     parser->in_statement = IN_OMP_BLOCK;
21871
21872   return save;
21873 }
21874
21875 static void
21876 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21877 {
21878   parser->in_statement = save;
21879 }
21880
21881 static tree
21882 cp_parser_omp_structured_block (cp_parser *parser)
21883 {
21884   tree stmt = begin_omp_structured_block ();
21885   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21886
21887   cp_parser_statement (parser, NULL_TREE, false, NULL);
21888
21889   cp_parser_end_omp_structured_block (parser, save);
21890   return finish_omp_structured_block (stmt);
21891 }
21892
21893 /* OpenMP 2.5:
21894    # pragma omp atomic new-line
21895      expression-stmt
21896
21897    expression-stmt:
21898      x binop= expr | x++ | ++x | x-- | --x
21899    binop:
21900      +, *, -, /, &, ^, |, <<, >>
21901
21902   where x is an lvalue expression with scalar type.  */
21903
21904 static void
21905 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21906 {
21907   tree lhs, rhs;
21908   enum tree_code code;
21909
21910   cp_parser_require_pragma_eol (parser, pragma_tok);
21911
21912   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21913                                     /*cast_p=*/false, NULL);
21914   switch (TREE_CODE (lhs))
21915     {
21916     case ERROR_MARK:
21917       goto saw_error;
21918
21919     case PREINCREMENT_EXPR:
21920     case POSTINCREMENT_EXPR:
21921       lhs = TREE_OPERAND (lhs, 0);
21922       code = PLUS_EXPR;
21923       rhs = integer_one_node;
21924       break;
21925
21926     case PREDECREMENT_EXPR:
21927     case POSTDECREMENT_EXPR:
21928       lhs = TREE_OPERAND (lhs, 0);
21929       code = MINUS_EXPR;
21930       rhs = integer_one_node;
21931       break;
21932
21933     default:
21934       switch (cp_lexer_peek_token (parser->lexer)->type)
21935         {
21936         case CPP_MULT_EQ:
21937           code = MULT_EXPR;
21938           break;
21939         case CPP_DIV_EQ:
21940           code = TRUNC_DIV_EXPR;
21941           break;
21942         case CPP_PLUS_EQ:
21943           code = PLUS_EXPR;
21944           break;
21945         case CPP_MINUS_EQ:
21946           code = MINUS_EXPR;
21947           break;
21948         case CPP_LSHIFT_EQ:
21949           code = LSHIFT_EXPR;
21950           break;
21951         case CPP_RSHIFT_EQ:
21952           code = RSHIFT_EXPR;
21953           break;
21954         case CPP_AND_EQ:
21955           code = BIT_AND_EXPR;
21956           break;
21957         case CPP_OR_EQ:
21958           code = BIT_IOR_EXPR;
21959           break;
21960         case CPP_XOR_EQ:
21961           code = BIT_XOR_EXPR;
21962           break;
21963         default:
21964           cp_parser_error (parser,
21965                            "invalid operator for %<#pragma omp atomic%>");
21966           goto saw_error;
21967         }
21968       cp_lexer_consume_token (parser->lexer);
21969
21970       rhs = cp_parser_expression (parser, false, NULL);
21971       if (rhs == error_mark_node)
21972         goto saw_error;
21973       break;
21974     }
21975   finish_omp_atomic (code, lhs, rhs);
21976   cp_parser_consume_semicolon_at_end_of_statement (parser);
21977   return;
21978
21979  saw_error:
21980   cp_parser_skip_to_end_of_block_or_statement (parser);
21981 }
21982
21983
21984 /* OpenMP 2.5:
21985    # pragma omp barrier new-line  */
21986
21987 static void
21988 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21989 {
21990   cp_parser_require_pragma_eol (parser, pragma_tok);
21991   finish_omp_barrier ();
21992 }
21993
21994 /* OpenMP 2.5:
21995    # pragma omp critical [(name)] new-line
21996      structured-block  */
21997
21998 static tree
21999 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
22000 {
22001   tree stmt, name = NULL;
22002
22003   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22004     {
22005       cp_lexer_consume_token (parser->lexer);
22006
22007       name = cp_parser_identifier (parser);
22008
22009       if (name == error_mark_node
22010           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22011         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22012                                                /*or_comma=*/false,
22013                                                /*consume_paren=*/true);
22014       if (name == error_mark_node)
22015         name = NULL;
22016     }
22017   cp_parser_require_pragma_eol (parser, pragma_tok);
22018
22019   stmt = cp_parser_omp_structured_block (parser);
22020   return c_finish_omp_critical (input_location, stmt, name);
22021 }
22022
22023 /* OpenMP 2.5:
22024    # pragma omp flush flush-vars[opt] new-line
22025
22026    flush-vars:
22027      ( variable-list ) */
22028
22029 static void
22030 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
22031 {
22032   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22033     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22034   cp_parser_require_pragma_eol (parser, pragma_tok);
22035
22036   finish_omp_flush ();
22037 }
22038
22039 /* Helper function, to parse omp for increment expression.  */
22040
22041 static tree
22042 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
22043 {
22044   tree cond = cp_parser_binary_expression (parser, false, true,
22045                                            PREC_NOT_OPERATOR, NULL);
22046   bool overloaded_p;
22047
22048   if (cond == error_mark_node
22049       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22050     {
22051       cp_parser_skip_to_end_of_statement (parser);
22052       return error_mark_node;
22053     }
22054
22055   switch (TREE_CODE (cond))
22056     {
22057     case GT_EXPR:
22058     case GE_EXPR:
22059     case LT_EXPR:
22060     case LE_EXPR:
22061       break;
22062     default:
22063       return error_mark_node;
22064     }
22065
22066   /* If decl is an iterator, preserve LHS and RHS of the relational
22067      expr until finish_omp_for.  */
22068   if (decl
22069       && (type_dependent_expression_p (decl)
22070           || CLASS_TYPE_P (TREE_TYPE (decl))))
22071     return cond;
22072
22073   return build_x_binary_op (TREE_CODE (cond),
22074                             TREE_OPERAND (cond, 0), ERROR_MARK,
22075                             TREE_OPERAND (cond, 1), ERROR_MARK,
22076                             &overloaded_p, tf_warning_or_error);
22077 }
22078
22079 /* Helper function, to parse omp for increment expression.  */
22080
22081 static tree
22082 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
22083 {
22084   cp_token *token = cp_lexer_peek_token (parser->lexer);
22085   enum tree_code op;
22086   tree lhs, rhs;
22087   cp_id_kind idk;
22088   bool decl_first;
22089
22090   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22091     {
22092       op = (token->type == CPP_PLUS_PLUS
22093             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
22094       cp_lexer_consume_token (parser->lexer);
22095       lhs = cp_parser_cast_expression (parser, false, false, NULL);
22096       if (lhs != decl)
22097         return error_mark_node;
22098       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22099     }
22100
22101   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
22102   if (lhs != decl)
22103     return error_mark_node;
22104
22105   token = cp_lexer_peek_token (parser->lexer);
22106   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
22107     {
22108       op = (token->type == CPP_PLUS_PLUS
22109             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
22110       cp_lexer_consume_token (parser->lexer);
22111       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
22112     }
22113
22114   op = cp_parser_assignment_operator_opt (parser);
22115   if (op == ERROR_MARK)
22116     return error_mark_node;
22117
22118   if (op != NOP_EXPR)
22119     {
22120       rhs = cp_parser_assignment_expression (parser, false, NULL);
22121       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
22122       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22123     }
22124
22125   lhs = cp_parser_binary_expression (parser, false, false,
22126                                      PREC_ADDITIVE_EXPRESSION, NULL);
22127   token = cp_lexer_peek_token (parser->lexer);
22128   decl_first = lhs == decl;
22129   if (decl_first)
22130     lhs = NULL_TREE;
22131   if (token->type != CPP_PLUS
22132       && token->type != CPP_MINUS)
22133     return error_mark_node;
22134
22135   do
22136     {
22137       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22138       cp_lexer_consume_token (parser->lexer);
22139       rhs = cp_parser_binary_expression (parser, false, false,
22140                                          PREC_ADDITIVE_EXPRESSION, NULL);
22141       token = cp_lexer_peek_token (parser->lexer);
22142       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22143         {
22144           if (lhs == NULL_TREE)
22145             {
22146               if (op == PLUS_EXPR)
22147                 lhs = rhs;
22148               else
22149                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22150             }
22151           else
22152             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22153                                      NULL, tf_warning_or_error);
22154         }
22155     }
22156   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22157
22158   if (!decl_first)
22159     {
22160       if (rhs != decl || op == MINUS_EXPR)
22161         return error_mark_node;
22162       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22163     }
22164   else
22165     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22166
22167   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22168 }
22169
22170 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22171
22172 static tree
22173 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22174 {
22175   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22176   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22177   tree this_pre_body, cl;
22178   location_t loc_first;
22179   bool collapse_err = false;
22180   int i, collapse = 1, nbraces = 0;
22181
22182   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22183     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22184       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22185
22186   gcc_assert (collapse >= 1);
22187
22188   declv = make_tree_vec (collapse);
22189   initv = make_tree_vec (collapse);
22190   condv = make_tree_vec (collapse);
22191   incrv = make_tree_vec (collapse);
22192
22193   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22194
22195   for (i = 0; i < collapse; i++)
22196     {
22197       int bracecount = 0;
22198       bool add_private_clause = false;
22199       location_t loc;
22200
22201       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22202         {
22203           cp_parser_error (parser, "for statement expected");
22204           return NULL;
22205         }
22206       loc = cp_lexer_consume_token (parser->lexer)->location;
22207
22208       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22209         return NULL;
22210
22211       init = decl = real_decl = NULL;
22212       this_pre_body = push_stmt_list ();
22213       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22214         {
22215           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22216
22217              init-expr:
22218                        var = lb
22219                        integer-type var = lb
22220                        random-access-iterator-type var = lb
22221                        pointer-type var = lb
22222           */
22223           cp_decl_specifier_seq type_specifiers;
22224
22225           /* First, try to parse as an initialized declaration.  See
22226              cp_parser_condition, from whence the bulk of this is copied.  */
22227
22228           cp_parser_parse_tentatively (parser);
22229           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
22230                                         /*is_trailing_return=*/false,
22231                                         &type_specifiers);
22232           if (cp_parser_parse_definitely (parser))
22233             {
22234               /* If parsing a type specifier seq succeeded, then this
22235                  MUST be a initialized declaration.  */
22236               tree asm_specification, attributes;
22237               cp_declarator *declarator;
22238
22239               declarator = cp_parser_declarator (parser,
22240                                                  CP_PARSER_DECLARATOR_NAMED,
22241                                                  /*ctor_dtor_or_conv_p=*/NULL,
22242                                                  /*parenthesized_p=*/NULL,
22243                                                  /*member_p=*/false);
22244               attributes = cp_parser_attributes_opt (parser);
22245               asm_specification = cp_parser_asm_specification_opt (parser);
22246
22247               if (declarator == cp_error_declarator) 
22248                 cp_parser_skip_to_end_of_statement (parser);
22249
22250               else 
22251                 {
22252                   tree pushed_scope, auto_node;
22253
22254                   decl = start_decl (declarator, &type_specifiers,
22255                                      SD_INITIALIZED, attributes,
22256                                      /*prefix_attributes=*/NULL_TREE,
22257                                      &pushed_scope);
22258
22259                   auto_node = type_uses_auto (TREE_TYPE (decl));
22260                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22261                     {
22262                       if (cp_lexer_next_token_is (parser->lexer, 
22263                                                   CPP_OPEN_PAREN))
22264                         error ("parenthesized initialization is not allowed in "
22265                                "OpenMP %<for%> loop");
22266                       else
22267                         /* Trigger an error.  */
22268                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22269
22270                       init = error_mark_node;
22271                       cp_parser_skip_to_end_of_statement (parser);
22272                     }
22273                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22274                            || type_dependent_expression_p (decl)
22275                            || auto_node)
22276                     {
22277                       bool is_direct_init, is_non_constant_init;
22278
22279                       init = cp_parser_initializer (parser,
22280                                                     &is_direct_init,
22281                                                     &is_non_constant_init);
22282
22283                       if (auto_node && describable_type (init))
22284                         {
22285                           TREE_TYPE (decl)
22286                             = do_auto_deduction (TREE_TYPE (decl), init,
22287                                                  auto_node);
22288
22289                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22290                               && !type_dependent_expression_p (decl))
22291                             goto non_class;
22292                         }
22293                       
22294                       cp_finish_decl (decl, init, !is_non_constant_init,
22295                                       asm_specification,
22296                                       LOOKUP_ONLYCONVERTING);
22297                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22298                         {
22299                           for_block
22300                             = tree_cons (NULL, this_pre_body, for_block);
22301                           init = NULL_TREE;
22302                         }
22303                       else
22304                         init = pop_stmt_list (this_pre_body);
22305                       this_pre_body = NULL_TREE;
22306                     }
22307                   else
22308                     {
22309                       /* Consume '='.  */
22310                       cp_lexer_consume_token (parser->lexer);
22311                       init = cp_parser_assignment_expression (parser, false, NULL);
22312
22313                     non_class:
22314                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22315                         init = error_mark_node;
22316                       else
22317                         cp_finish_decl (decl, NULL_TREE,
22318                                         /*init_const_expr_p=*/false,
22319                                         asm_specification,
22320                                         LOOKUP_ONLYCONVERTING);
22321                     }
22322
22323                   if (pushed_scope)
22324                     pop_scope (pushed_scope);
22325                 }
22326             }
22327           else 
22328             {
22329               cp_id_kind idk;
22330               /* If parsing a type specifier sequence failed, then
22331                  this MUST be a simple expression.  */
22332               cp_parser_parse_tentatively (parser);
22333               decl = cp_parser_primary_expression (parser, false, false,
22334                                                    false, &idk);
22335               if (!cp_parser_error_occurred (parser)
22336                   && decl
22337                   && DECL_P (decl)
22338                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22339                 {
22340                   tree rhs;
22341
22342                   cp_parser_parse_definitely (parser);
22343                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22344                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22345                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22346                                                          rhs,
22347                                                          tf_warning_or_error));
22348                   add_private_clause = true;
22349                 }
22350               else
22351                 {
22352                   decl = NULL;
22353                   cp_parser_abort_tentative_parse (parser);
22354                   init = cp_parser_expression (parser, false, NULL);
22355                   if (init)
22356                     {
22357                       if (TREE_CODE (init) == MODIFY_EXPR
22358                           || TREE_CODE (init) == MODOP_EXPR)
22359                         real_decl = TREE_OPERAND (init, 0);
22360                     }
22361                 }
22362             }
22363         }
22364       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22365       if (this_pre_body)
22366         {
22367           this_pre_body = pop_stmt_list (this_pre_body);
22368           if (pre_body)
22369             {
22370               tree t = pre_body;
22371               pre_body = push_stmt_list ();
22372               add_stmt (t);
22373               add_stmt (this_pre_body);
22374               pre_body = pop_stmt_list (pre_body);
22375             }
22376           else
22377             pre_body = this_pre_body;
22378         }
22379
22380       if (decl)
22381         real_decl = decl;
22382       if (par_clauses != NULL && real_decl != NULL_TREE)
22383         {
22384           tree *c;
22385           for (c = par_clauses; *c ; )
22386             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22387                 && OMP_CLAUSE_DECL (*c) == real_decl)
22388               {
22389                 error_at (loc, "iteration variable %qD"
22390                           " should not be firstprivate", real_decl);
22391                 *c = OMP_CLAUSE_CHAIN (*c);
22392               }
22393             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22394                      && OMP_CLAUSE_DECL (*c) == real_decl)
22395               {
22396                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22397                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22398                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22399                 OMP_CLAUSE_DECL (l) = real_decl;
22400                 OMP_CLAUSE_CHAIN (l) = clauses;
22401                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22402                 clauses = l;
22403                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22404                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22405                 add_private_clause = false;
22406               }
22407             else
22408               {
22409                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22410                     && OMP_CLAUSE_DECL (*c) == real_decl)
22411                   add_private_clause = false;
22412                 c = &OMP_CLAUSE_CHAIN (*c);
22413               }
22414         }
22415
22416       if (add_private_clause)
22417         {
22418           tree c;
22419           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22420             {
22421               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22422                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22423                   && OMP_CLAUSE_DECL (c) == decl)
22424                 break;
22425               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22426                        && OMP_CLAUSE_DECL (c) == decl)
22427                 error_at (loc, "iteration variable %qD "
22428                           "should not be firstprivate",
22429                           decl);
22430               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22431                        && OMP_CLAUSE_DECL (c) == decl)
22432                 error_at (loc, "iteration variable %qD should not be reduction",
22433                           decl);
22434             }
22435           if (c == NULL)
22436             {
22437               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22438               OMP_CLAUSE_DECL (c) = decl;
22439               c = finish_omp_clauses (c);
22440               if (c)
22441                 {
22442                   OMP_CLAUSE_CHAIN (c) = clauses;
22443                   clauses = c;
22444                 }
22445             }
22446         }
22447
22448       cond = NULL;
22449       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22450         cond = cp_parser_omp_for_cond (parser, decl);
22451       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22452
22453       incr = NULL;
22454       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22455         {
22456           /* If decl is an iterator, preserve the operator on decl
22457              until finish_omp_for.  */
22458           if (decl
22459               && (type_dependent_expression_p (decl)
22460                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22461             incr = cp_parser_omp_for_incr (parser, decl);
22462           else
22463             incr = cp_parser_expression (parser, false, NULL);
22464         }
22465
22466       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22467         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22468                                                /*or_comma=*/false,
22469                                                /*consume_paren=*/true);
22470
22471       TREE_VEC_ELT (declv, i) = decl;
22472       TREE_VEC_ELT (initv, i) = init;
22473       TREE_VEC_ELT (condv, i) = cond;
22474       TREE_VEC_ELT (incrv, i) = incr;
22475
22476       if (i == collapse - 1)
22477         break;
22478
22479       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22480          in between the collapsed for loops to be still considered perfectly
22481          nested.  Hopefully the final version clarifies this.
22482          For now handle (multiple) {'s and empty statements.  */
22483       cp_parser_parse_tentatively (parser);
22484       do
22485         {
22486           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22487             break;
22488           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22489             {
22490               cp_lexer_consume_token (parser->lexer);
22491               bracecount++;
22492             }
22493           else if (bracecount
22494                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22495             cp_lexer_consume_token (parser->lexer);
22496           else
22497             {
22498               loc = cp_lexer_peek_token (parser->lexer)->location;
22499               error_at (loc, "not enough collapsed for loops");
22500               collapse_err = true;
22501               cp_parser_abort_tentative_parse (parser);
22502               declv = NULL_TREE;
22503               break;
22504             }
22505         }
22506       while (1);
22507
22508       if (declv)
22509         {
22510           cp_parser_parse_definitely (parser);
22511           nbraces += bracecount;
22512         }
22513     }
22514
22515   /* Note that we saved the original contents of this flag when we entered
22516      the structured block, and so we don't need to re-save it here.  */
22517   parser->in_statement = IN_OMP_FOR;
22518
22519   /* Note that the grammar doesn't call for a structured block here,
22520      though the loop as a whole is a structured block.  */
22521   body = push_stmt_list ();
22522   cp_parser_statement (parser, NULL_TREE, false, NULL);
22523   body = pop_stmt_list (body);
22524
22525   if (declv == NULL_TREE)
22526     ret = NULL_TREE;
22527   else
22528     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22529                           pre_body, clauses);
22530
22531   while (nbraces)
22532     {
22533       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22534         {
22535           cp_lexer_consume_token (parser->lexer);
22536           nbraces--;
22537         }
22538       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22539         cp_lexer_consume_token (parser->lexer);
22540       else
22541         {
22542           if (!collapse_err)
22543             {
22544               error_at (cp_lexer_peek_token (parser->lexer)->location,
22545                         "collapsed loops not perfectly nested");
22546             }
22547           collapse_err = true;
22548           cp_parser_statement_seq_opt (parser, NULL);
22549           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
22550             break;
22551         }
22552     }
22553
22554   while (for_block)
22555     {
22556       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22557       for_block = TREE_CHAIN (for_block);
22558     }
22559
22560   return ret;
22561 }
22562
22563 /* OpenMP 2.5:
22564    #pragma omp for for-clause[optseq] new-line
22565      for-loop  */
22566
22567 #define OMP_FOR_CLAUSE_MASK                             \
22568         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22569         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22570         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22571         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22572         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22573         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22574         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22575         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22576
22577 static tree
22578 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22579 {
22580   tree clauses, sb, ret;
22581   unsigned int save;
22582
22583   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22584                                        "#pragma omp for", pragma_tok);
22585
22586   sb = begin_omp_structured_block ();
22587   save = cp_parser_begin_omp_structured_block (parser);
22588
22589   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22590
22591   cp_parser_end_omp_structured_block (parser, save);
22592   add_stmt (finish_omp_structured_block (sb));
22593
22594   return ret;
22595 }
22596
22597 /* OpenMP 2.5:
22598    # pragma omp master new-line
22599      structured-block  */
22600
22601 static tree
22602 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22603 {
22604   cp_parser_require_pragma_eol (parser, pragma_tok);
22605   return c_finish_omp_master (input_location,
22606                               cp_parser_omp_structured_block (parser));
22607 }
22608
22609 /* OpenMP 2.5:
22610    # pragma omp ordered new-line
22611      structured-block  */
22612
22613 static tree
22614 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22615 {
22616   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22617   cp_parser_require_pragma_eol (parser, pragma_tok);
22618   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22619 }
22620
22621 /* OpenMP 2.5:
22622
22623    section-scope:
22624      { section-sequence }
22625
22626    section-sequence:
22627      section-directive[opt] structured-block
22628      section-sequence section-directive structured-block  */
22629
22630 static tree
22631 cp_parser_omp_sections_scope (cp_parser *parser)
22632 {
22633   tree stmt, substmt;
22634   bool error_suppress = false;
22635   cp_token *tok;
22636
22637   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22638     return NULL_TREE;
22639
22640   stmt = push_stmt_list ();
22641
22642   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22643     {
22644       unsigned save;
22645
22646       substmt = begin_omp_structured_block ();
22647       save = cp_parser_begin_omp_structured_block (parser);
22648
22649       while (1)
22650         {
22651           cp_parser_statement (parser, NULL_TREE, false, NULL);
22652
22653           tok = cp_lexer_peek_token (parser->lexer);
22654           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22655             break;
22656           if (tok->type == CPP_CLOSE_BRACE)
22657             break;
22658           if (tok->type == CPP_EOF)
22659             break;
22660         }
22661
22662       cp_parser_end_omp_structured_block (parser, save);
22663       substmt = finish_omp_structured_block (substmt);
22664       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22665       add_stmt (substmt);
22666     }
22667
22668   while (1)
22669     {
22670       tok = cp_lexer_peek_token (parser->lexer);
22671       if (tok->type == CPP_CLOSE_BRACE)
22672         break;
22673       if (tok->type == CPP_EOF)
22674         break;
22675
22676       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22677         {
22678           cp_lexer_consume_token (parser->lexer);
22679           cp_parser_require_pragma_eol (parser, tok);
22680           error_suppress = false;
22681         }
22682       else if (!error_suppress)
22683         {
22684           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22685           error_suppress = true;
22686         }
22687
22688       substmt = cp_parser_omp_structured_block (parser);
22689       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22690       add_stmt (substmt);
22691     }
22692   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22693
22694   substmt = pop_stmt_list (stmt);
22695
22696   stmt = make_node (OMP_SECTIONS);
22697   TREE_TYPE (stmt) = void_type_node;
22698   OMP_SECTIONS_BODY (stmt) = substmt;
22699
22700   add_stmt (stmt);
22701   return stmt;
22702 }
22703
22704 /* OpenMP 2.5:
22705    # pragma omp sections sections-clause[optseq] newline
22706      sections-scope  */
22707
22708 #define OMP_SECTIONS_CLAUSE_MASK                        \
22709         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22710         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22711         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22712         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22713         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22714
22715 static tree
22716 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22717 {
22718   tree clauses, ret;
22719
22720   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22721                                        "#pragma omp sections", pragma_tok);
22722
22723   ret = cp_parser_omp_sections_scope (parser);
22724   if (ret)
22725     OMP_SECTIONS_CLAUSES (ret) = clauses;
22726
22727   return ret;
22728 }
22729
22730 /* OpenMP 2.5:
22731    # pragma parallel parallel-clause new-line
22732    # pragma parallel for parallel-for-clause new-line
22733    # pragma parallel sections parallel-sections-clause new-line  */
22734
22735 #define OMP_PARALLEL_CLAUSE_MASK                        \
22736         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22737         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22738         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22739         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22740         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22741         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22742         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22743         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22744
22745 static tree
22746 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22747 {
22748   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22749   const char *p_name = "#pragma omp parallel";
22750   tree stmt, clauses, par_clause, ws_clause, block;
22751   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22752   unsigned int save;
22753   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22754
22755   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22756     {
22757       cp_lexer_consume_token (parser->lexer);
22758       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22759       p_name = "#pragma omp parallel for";
22760       mask |= OMP_FOR_CLAUSE_MASK;
22761       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22762     }
22763   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22764     {
22765       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22766       const char *p = IDENTIFIER_POINTER (id);
22767       if (strcmp (p, "sections") == 0)
22768         {
22769           cp_lexer_consume_token (parser->lexer);
22770           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22771           p_name = "#pragma omp parallel sections";
22772           mask |= OMP_SECTIONS_CLAUSE_MASK;
22773           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22774         }
22775     }
22776
22777   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22778   block = begin_omp_parallel ();
22779   save = cp_parser_begin_omp_structured_block (parser);
22780
22781   switch (p_kind)
22782     {
22783     case PRAGMA_OMP_PARALLEL:
22784       cp_parser_statement (parser, NULL_TREE, false, NULL);
22785       par_clause = clauses;
22786       break;
22787
22788     case PRAGMA_OMP_PARALLEL_FOR:
22789       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22790       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22791       break;
22792
22793     case PRAGMA_OMP_PARALLEL_SECTIONS:
22794       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22795       stmt = cp_parser_omp_sections_scope (parser);
22796       if (stmt)
22797         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22798       break;
22799
22800     default:
22801       gcc_unreachable ();
22802     }
22803
22804   cp_parser_end_omp_structured_block (parser, save);
22805   stmt = finish_omp_parallel (par_clause, block);
22806   if (p_kind != PRAGMA_OMP_PARALLEL)
22807     OMP_PARALLEL_COMBINED (stmt) = 1;
22808   return stmt;
22809 }
22810
22811 /* OpenMP 2.5:
22812    # pragma omp single single-clause[optseq] new-line
22813      structured-block  */
22814
22815 #define OMP_SINGLE_CLAUSE_MASK                          \
22816         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22817         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22818         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22819         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22820
22821 static tree
22822 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22823 {
22824   tree stmt = make_node (OMP_SINGLE);
22825   TREE_TYPE (stmt) = void_type_node;
22826
22827   OMP_SINGLE_CLAUSES (stmt)
22828     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22829                                  "#pragma omp single", pragma_tok);
22830   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22831
22832   return add_stmt (stmt);
22833 }
22834
22835 /* OpenMP 3.0:
22836    # pragma omp task task-clause[optseq] new-line
22837      structured-block  */
22838
22839 #define OMP_TASK_CLAUSE_MASK                            \
22840         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22841         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22842         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22843         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22844         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22845         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22846
22847 static tree
22848 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22849 {
22850   tree clauses, block;
22851   unsigned int save;
22852
22853   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22854                                        "#pragma omp task", pragma_tok);
22855   block = begin_omp_task ();
22856   save = cp_parser_begin_omp_structured_block (parser);
22857   cp_parser_statement (parser, NULL_TREE, false, NULL);
22858   cp_parser_end_omp_structured_block (parser, save);
22859   return finish_omp_task (clauses, block);
22860 }
22861
22862 /* OpenMP 3.0:
22863    # pragma omp taskwait new-line  */
22864
22865 static void
22866 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22867 {
22868   cp_parser_require_pragma_eol (parser, pragma_tok);
22869   finish_omp_taskwait ();
22870 }
22871
22872 /* OpenMP 2.5:
22873    # pragma omp threadprivate (variable-list) */
22874
22875 static void
22876 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22877 {
22878   tree vars;
22879
22880   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22881   cp_parser_require_pragma_eol (parser, pragma_tok);
22882
22883   finish_omp_threadprivate (vars);
22884 }
22885
22886 /* Main entry point to OpenMP statement pragmas.  */
22887
22888 static void
22889 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22890 {
22891   tree stmt;
22892
22893   switch (pragma_tok->pragma_kind)
22894     {
22895     case PRAGMA_OMP_ATOMIC:
22896       cp_parser_omp_atomic (parser, pragma_tok);
22897       return;
22898     case PRAGMA_OMP_CRITICAL:
22899       stmt = cp_parser_omp_critical (parser, pragma_tok);
22900       break;
22901     case PRAGMA_OMP_FOR:
22902       stmt = cp_parser_omp_for (parser, pragma_tok);
22903       break;
22904     case PRAGMA_OMP_MASTER:
22905       stmt = cp_parser_omp_master (parser, pragma_tok);
22906       break;
22907     case PRAGMA_OMP_ORDERED:
22908       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22909       break;
22910     case PRAGMA_OMP_PARALLEL:
22911       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22912       break;
22913     case PRAGMA_OMP_SECTIONS:
22914       stmt = cp_parser_omp_sections (parser, pragma_tok);
22915       break;
22916     case PRAGMA_OMP_SINGLE:
22917       stmt = cp_parser_omp_single (parser, pragma_tok);
22918       break;
22919     case PRAGMA_OMP_TASK:
22920       stmt = cp_parser_omp_task (parser, pragma_tok);
22921       break;
22922     default:
22923       gcc_unreachable ();
22924     }
22925
22926   if (stmt)
22927     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22928 }
22929 \f
22930 /* The parser.  */
22931
22932 static GTY (()) cp_parser *the_parser;
22933
22934 \f
22935 /* Special handling for the first token or line in the file.  The first
22936    thing in the file might be #pragma GCC pch_preprocess, which loads a
22937    PCH file, which is a GC collection point.  So we need to handle this
22938    first pragma without benefit of an existing lexer structure.
22939
22940    Always returns one token to the caller in *FIRST_TOKEN.  This is
22941    either the true first token of the file, or the first token after
22942    the initial pragma.  */
22943
22944 static void
22945 cp_parser_initial_pragma (cp_token *first_token)
22946 {
22947   tree name = NULL;
22948
22949   cp_lexer_get_preprocessor_token (NULL, first_token);
22950   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22951     return;
22952
22953   cp_lexer_get_preprocessor_token (NULL, first_token);
22954   if (first_token->type == CPP_STRING)
22955     {
22956       name = first_token->u.value;
22957
22958       cp_lexer_get_preprocessor_token (NULL, first_token);
22959       if (first_token->type != CPP_PRAGMA_EOL)
22960         error_at (first_token->location,
22961                   "junk at end of %<#pragma GCC pch_preprocess%>");
22962     }
22963   else
22964     error_at (first_token->location, "expected string literal");
22965
22966   /* Skip to the end of the pragma.  */
22967   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22968     cp_lexer_get_preprocessor_token (NULL, first_token);
22969
22970   /* Now actually load the PCH file.  */
22971   if (name)
22972     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22973
22974   /* Read one more token to return to our caller.  We have to do this
22975      after reading the PCH file in, since its pointers have to be
22976      live.  */
22977   cp_lexer_get_preprocessor_token (NULL, first_token);
22978 }
22979
22980 /* Normal parsing of a pragma token.  Here we can (and must) use the
22981    regular lexer.  */
22982
22983 static bool
22984 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22985 {
22986   cp_token *pragma_tok;
22987   unsigned int id;
22988
22989   pragma_tok = cp_lexer_consume_token (parser->lexer);
22990   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22991   parser->lexer->in_pragma = true;
22992
22993   id = pragma_tok->pragma_kind;
22994   switch (id)
22995     {
22996     case PRAGMA_GCC_PCH_PREPROCESS:
22997       error_at (pragma_tok->location,
22998                 "%<#pragma GCC pch_preprocess%> must be first");
22999       break;
23000
23001     case PRAGMA_OMP_BARRIER:
23002       switch (context)
23003         {
23004         case pragma_compound:
23005           cp_parser_omp_barrier (parser, pragma_tok);
23006           return false;
23007         case pragma_stmt:
23008           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
23009                     "used in compound statements");
23010           break;
23011         default:
23012           goto bad_stmt;
23013         }
23014       break;
23015
23016     case PRAGMA_OMP_FLUSH:
23017       switch (context)
23018         {
23019         case pragma_compound:
23020           cp_parser_omp_flush (parser, pragma_tok);
23021           return false;
23022         case pragma_stmt:
23023           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
23024                     "used in compound statements");
23025           break;
23026         default:
23027           goto bad_stmt;
23028         }
23029       break;
23030
23031     case PRAGMA_OMP_TASKWAIT:
23032       switch (context)
23033         {
23034         case pragma_compound:
23035           cp_parser_omp_taskwait (parser, pragma_tok);
23036           return false;
23037         case pragma_stmt:
23038           error_at (pragma_tok->location,
23039                     "%<#pragma omp taskwait%> may only be "
23040                     "used in compound statements");
23041           break;
23042         default:
23043           goto bad_stmt;
23044         }
23045       break;
23046
23047     case PRAGMA_OMP_THREADPRIVATE:
23048       cp_parser_omp_threadprivate (parser, pragma_tok);
23049       return false;
23050
23051     case PRAGMA_OMP_ATOMIC:
23052     case PRAGMA_OMP_CRITICAL:
23053     case PRAGMA_OMP_FOR:
23054     case PRAGMA_OMP_MASTER:
23055     case PRAGMA_OMP_ORDERED:
23056     case PRAGMA_OMP_PARALLEL:
23057     case PRAGMA_OMP_SECTIONS:
23058     case PRAGMA_OMP_SINGLE:
23059     case PRAGMA_OMP_TASK:
23060       if (context == pragma_external)
23061         goto bad_stmt;
23062       cp_parser_omp_construct (parser, pragma_tok);
23063       return true;
23064
23065     case PRAGMA_OMP_SECTION:
23066       error_at (pragma_tok->location, 
23067                 "%<#pragma omp section%> may only be used in "
23068                 "%<#pragma omp sections%> construct");
23069       break;
23070
23071     default:
23072       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
23073       c_invoke_pragma_handler (id);
23074       break;
23075
23076     bad_stmt:
23077       cp_parser_error (parser, "expected declaration specifiers");
23078       break;
23079     }
23080
23081   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
23082   return false;
23083 }
23084
23085 /* The interface the pragma parsers have to the lexer.  */
23086
23087 enum cpp_ttype
23088 pragma_lex (tree *value)
23089 {
23090   cp_token *tok;
23091   enum cpp_ttype ret;
23092
23093   tok = cp_lexer_peek_token (the_parser->lexer);
23094
23095   ret = tok->type;
23096   *value = tok->u.value;
23097
23098   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
23099     ret = CPP_EOF;
23100   else if (ret == CPP_STRING)
23101     *value = cp_parser_string_literal (the_parser, false, false);
23102   else
23103     {
23104       cp_lexer_consume_token (the_parser->lexer);
23105       if (ret == CPP_KEYWORD)
23106         ret = CPP_NAME;
23107     }
23108
23109   return ret;
23110 }
23111
23112 \f
23113 /* External interface.  */
23114
23115 /* Parse one entire translation unit.  */
23116
23117 void
23118 c_parse_file (void)
23119 {
23120   static bool already_called = false;
23121
23122   if (already_called)
23123     {
23124       sorry ("inter-module optimizations not implemented for C++");
23125       return;
23126     }
23127   already_called = true;
23128
23129   the_parser = cp_parser_new ();
23130   push_deferring_access_checks (flag_access_control
23131                                 ? dk_no_deferred : dk_no_check);
23132   cp_parser_translation_unit (the_parser);
23133   the_parser = NULL;
23134 }
23135
23136 #include "gt-cp-parser.h"