7fd995f9e83f705ea0e4ecc554526f96e05db4fb
[platform/upstream/gcc.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 "c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic.h"
35 #include "toplev.h"
36 #include "output.h"
37 #include "target.h"
38 #include "cgraph.h"
39 #include "c-common.h"
40 #include "plugin.h"
41
42 \f
43 /* The lexer.  */
44
45 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
46    and c-lex.c) and the C++ parser.  */
47
48 /* A token's value and its associated deferred access checks and
49    qualifying scope.  */
50
51 struct GTY(()) tree_check {
52   /* The value associated with the token.  */
53   tree value;
54   /* The checks that have been associated with value.  */
55   VEC (deferred_access_check, gc)* checks;
56   /* The token's qualifying scope (used when it is a
57      CPP_NESTED_NAME_SPECIFIER).  */
58   tree qualifying_scope;
59 };
60
61 /* A C++ token.  */
62
63 typedef struct GTY (()) cp_token {
64   /* The kind of token.  */
65   ENUM_BITFIELD (cpp_ttype) type : 8;
66   /* If this token is a keyword, this value indicates which keyword.
67      Otherwise, this value is RID_MAX.  */
68   ENUM_BITFIELD (rid) keyword : 8;
69   /* Token flags.  */
70   unsigned char flags;
71   /* Identifier for the pragma.  */
72   ENUM_BITFIELD (pragma_kind) pragma_kind : 6;
73   /* True if this token is from a context where it is implicitly extern "C" */
74   BOOL_BITFIELD implicit_extern_c : 1;
75   /* True for a CPP_NAME token that is not a keyword (i.e., for which
76      KEYWORD is RID_MAX) iff this name was looked up and found to be
77      ambiguous.  An error has already been reported.  */
78   BOOL_BITFIELD ambiguous_p : 1;
79   /* The location at which this token was found.  */
80   location_t location;
81   /* The value associated with this token, if any.  */
82   union cp_token_value {
83     /* Used for CPP_NESTED_NAME_SPECIFIER and CPP_TEMPLATE_ID.  */
84     struct tree_check* GTY((tag ("1"))) tree_check_value;
85     /* Use for all other tokens.  */
86     tree GTY((tag ("0"))) value;
87   } GTY((desc ("(%1.type == CPP_TEMPLATE_ID) || (%1.type == CPP_NESTED_NAME_SPECIFIER)"))) u;
88 } cp_token;
89
90 /* We use a stack of token pointer for saving token sets.  */
91 typedef struct cp_token *cp_token_position;
92 DEF_VEC_P (cp_token_position);
93 DEF_VEC_ALLOC_P (cp_token_position,heap);
94
95 static cp_token eof_token =
96 {
97   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, 0, 0, { NULL }
98 };
99
100 /* The cp_lexer structure represents the C++ lexer.  It is responsible
101    for managing the token stream from the preprocessor and supplying
102    it to the parser.  Tokens are never added to the cp_lexer after
103    it is created.  */
104
105 typedef struct GTY (()) cp_lexer {
106   /* The memory allocated for the buffer.  NULL if this lexer does not
107      own the token buffer.  */
108   cp_token * GTY ((length ("%h.buffer_length"))) buffer;
109   /* If the lexer owns the buffer, this is the number of tokens in the
110      buffer.  */
111   size_t buffer_length;
112
113   /* A pointer just past the last available token.  The tokens
114      in this lexer are [buffer, last_token).  */
115   cp_token_position GTY ((skip)) last_token;
116
117   /* The next available token.  If NEXT_TOKEN is &eof_token, then there are
118      no more available tokens.  */
119   cp_token_position GTY ((skip)) next_token;
120
121   /* A stack indicating positions at which cp_lexer_save_tokens was
122      called.  The top entry is the most recent position at which we
123      began saving tokens.  If the stack is non-empty, we are saving
124      tokens.  */
125   VEC(cp_token_position,heap) *GTY ((skip)) saved_tokens;
126
127   /* The next lexer in a linked list of lexers.  */
128   struct cp_lexer *next;
129
130   /* True if we should output debugging information.  */
131   bool debugging_p;
132
133   /* True if we're in the context of parsing a pragma, and should not
134      increment past the end-of-line marker.  */
135   bool in_pragma;
136 } cp_lexer;
137
138 /* cp_token_cache is a range of tokens.  There is no need to represent
139    allocate heap memory for it, since tokens are never removed from the
140    lexer's array.  There is also no need for the GC to walk through
141    a cp_token_cache, since everything in here is referenced through
142    a lexer.  */
143
144 typedef struct GTY(()) cp_token_cache {
145   /* The beginning of the token range.  */
146   cp_token * GTY((skip)) first;
147
148   /* Points immediately after the last token in the range.  */
149   cp_token * GTY ((skip)) last;
150 } cp_token_cache;
151
152 /* Prototypes.  */
153
154 static cp_lexer *cp_lexer_new_main
155   (void);
156 static cp_lexer *cp_lexer_new_from_tokens
157   (cp_token_cache *tokens);
158 static void cp_lexer_destroy
159   (cp_lexer *);
160 static int cp_lexer_saving_tokens
161   (const cp_lexer *);
162 static cp_token_position cp_lexer_token_position
163   (cp_lexer *, bool);
164 static cp_token *cp_lexer_token_at
165   (cp_lexer *, cp_token_position);
166 static void cp_lexer_get_preprocessor_token
167   (cp_lexer *, cp_token *);
168 static inline cp_token *cp_lexer_peek_token
169   (cp_lexer *);
170 static cp_token *cp_lexer_peek_nth_token
171   (cp_lexer *, size_t);
172 static inline bool cp_lexer_next_token_is
173   (cp_lexer *, enum cpp_ttype);
174 static bool cp_lexer_next_token_is_not
175   (cp_lexer *, enum cpp_ttype);
176 static bool cp_lexer_next_token_is_keyword
177   (cp_lexer *, enum rid);
178 static cp_token *cp_lexer_consume_token
179   (cp_lexer *);
180 static void cp_lexer_purge_token
181   (cp_lexer *);
182 static void cp_lexer_purge_tokens_after
183   (cp_lexer *, cp_token_position);
184 static void cp_lexer_save_tokens
185   (cp_lexer *);
186 static void cp_lexer_commit_tokens
187   (cp_lexer *);
188 static void cp_lexer_rollback_tokens
189   (cp_lexer *);
190 #ifdef ENABLE_CHECKING
191 static void cp_lexer_print_token
192   (FILE *, cp_token *);
193 static inline bool cp_lexer_debugging_p
194   (cp_lexer *);
195 static void cp_lexer_start_debugging
196   (cp_lexer *) ATTRIBUTE_UNUSED;
197 static void cp_lexer_stop_debugging
198   (cp_lexer *) ATTRIBUTE_UNUSED;
199 #else
200 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
201    about passing NULL to functions that require non-NULL arguments
202    (fputs, fprintf).  It will never be used, so all we need is a value
203    of the right type that's guaranteed not to be NULL.  */
204 #define cp_lexer_debug_stream stdout
205 #define cp_lexer_print_token(str, tok) (void) 0
206 #define cp_lexer_debugging_p(lexer) 0
207 #endif /* ENABLE_CHECKING */
208
209 static cp_token_cache *cp_token_cache_new
210   (cp_token *, cp_token *);
211
212 static void cp_parser_initial_pragma
213   (cp_token *);
214
215 /* Manifest constants.  */
216 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
217 #define CP_SAVED_TOKEN_STACK 5
218
219 /* A token type for keywords, as opposed to ordinary identifiers.  */
220 #define CPP_KEYWORD ((enum cpp_ttype) (N_TTYPES + 1))
221
222 /* A token type for template-ids.  If a template-id is processed while
223    parsing tentatively, it is replaced with a CPP_TEMPLATE_ID token;
224    the value of the CPP_TEMPLATE_ID is whatever was returned by
225    cp_parser_template_id.  */
226 #define CPP_TEMPLATE_ID ((enum cpp_ttype) (CPP_KEYWORD + 1))
227
228 /* A token type for nested-name-specifiers.  If a
229    nested-name-specifier is processed while parsing tentatively, it is
230    replaced with a CPP_NESTED_NAME_SPECIFIER token; the value of the
231    CPP_NESTED_NAME_SPECIFIER is whatever was returned by
232    cp_parser_nested_name_specifier_opt.  */
233 #define CPP_NESTED_NAME_SPECIFIER ((enum cpp_ttype) (CPP_TEMPLATE_ID + 1))
234
235 /* A token type for tokens that are not tokens at all; these are used
236    to represent slots in the array where there used to be a token
237    that has now been deleted.  */
238 #define CPP_PURGED ((enum cpp_ttype) (CPP_NESTED_NAME_SPECIFIER + 1))
239
240 /* The number of token types, including C++-specific ones.  */
241 #define N_CP_TTYPES ((int) (CPP_PURGED + 1))
242
243 /* Variables.  */
244
245 #ifdef ENABLE_CHECKING
246 /* The stream to which debugging output should be written.  */
247 static FILE *cp_lexer_debug_stream;
248 #endif /* ENABLE_CHECKING */
249
250 /* Nonzero if we are parsing an unevaluated operand: an operand to
251    sizeof, typeof, or alignof.  */
252 int cp_unevaluated_operand;
253
254 /* Create a new main C++ lexer, the lexer that gets tokens from the
255    preprocessor.  */
256
257 static cp_lexer *
258 cp_lexer_new_main (void)
259 {
260   cp_token first_token;
261   cp_lexer *lexer;
262   cp_token *pos;
263   size_t alloc;
264   size_t space;
265   cp_token *buffer;
266
267   /* It's possible that parsing the first pragma will load a PCH file,
268      which is a GC collection point.  So we have to do that before
269      allocating any memory.  */
270   cp_parser_initial_pragma (&first_token);
271
272   c_common_no_more_pch ();
273
274   /* Allocate the memory.  */
275   lexer = GGC_CNEW (cp_lexer);
276
277 #ifdef ENABLE_CHECKING
278   /* Initially we are not debugging.  */
279   lexer->debugging_p = false;
280 #endif /* ENABLE_CHECKING */
281   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
282                                    CP_SAVED_TOKEN_STACK);
283
284   /* Create the buffer.  */
285   alloc = CP_LEXER_BUFFER_SIZE;
286   buffer = GGC_NEWVEC (cp_token, alloc);
287
288   /* Put the first token in the buffer.  */
289   space = alloc;
290   pos = buffer;
291   *pos = first_token;
292
293   /* Get the remaining tokens from the preprocessor.  */
294   while (pos->type != CPP_EOF)
295     {
296       pos++;
297       if (!--space)
298         {
299           space = alloc;
300           alloc *= 2;
301           buffer = GGC_RESIZEVEC (cp_token, buffer, alloc);
302           pos = buffer + space;
303         }
304       cp_lexer_get_preprocessor_token (lexer, pos);
305     }
306   lexer->buffer = buffer;
307   lexer->buffer_length = alloc - space;
308   lexer->last_token = pos;
309   lexer->next_token = lexer->buffer_length ? buffer : &eof_token;
310
311   /* Subsequent preprocessor diagnostics should use compiler
312      diagnostic functions to get the compiler source location.  */
313   done_lexing = true;
314
315   gcc_assert (lexer->next_token->type != CPP_PURGED);
316   return lexer;
317 }
318
319 /* Create a new lexer whose token stream is primed with the tokens in
320    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
321
322 static cp_lexer *
323 cp_lexer_new_from_tokens (cp_token_cache *cache)
324 {
325   cp_token *first = cache->first;
326   cp_token *last = cache->last;
327   cp_lexer *lexer = GGC_CNEW (cp_lexer);
328
329   /* We do not own the buffer.  */
330   lexer->buffer = NULL;
331   lexer->buffer_length = 0;
332   lexer->next_token = first == last ? &eof_token : first;
333   lexer->last_token = last;
334
335   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
336                                    CP_SAVED_TOKEN_STACK);
337
338 #ifdef ENABLE_CHECKING
339   /* Initially we are not debugging.  */
340   lexer->debugging_p = false;
341 #endif
342
343   gcc_assert (lexer->next_token->type != CPP_PURGED);
344   return lexer;
345 }
346
347 /* Frees all resources associated with LEXER.  */
348
349 static void
350 cp_lexer_destroy (cp_lexer *lexer)
351 {
352   if (lexer->buffer)
353     ggc_free (lexer->buffer);
354   VEC_free (cp_token_position, heap, lexer->saved_tokens);
355   ggc_free (lexer);
356 }
357
358 /* Returns nonzero if debugging information should be output.  */
359
360 #ifdef ENABLE_CHECKING
361
362 static inline bool
363 cp_lexer_debugging_p (cp_lexer *lexer)
364 {
365   return lexer->debugging_p;
366 }
367
368 #endif /* ENABLE_CHECKING */
369
370 static inline cp_token_position
371 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
372 {
373   gcc_assert (!previous_p || lexer->next_token != &eof_token);
374
375   return lexer->next_token - previous_p;
376 }
377
378 static inline cp_token *
379 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
380 {
381   return pos;
382 }
383
384 /* nonzero if we are presently saving tokens.  */
385
386 static inline int
387 cp_lexer_saving_tokens (const cp_lexer* lexer)
388 {
389   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
390 }
391
392 /* Store the next token from the preprocessor in *TOKEN.  Return true
393    if we reach EOF.  If LEXER is NULL, assume we are handling an
394    initial #pragma pch_preprocess, and thus want the lexer to return
395    processed strings.  */
396
397 static void
398 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
399 {
400   static int is_extern_c = 0;
401
402    /* Get a new token from the preprocessor.  */
403   token->type
404     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
405                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
406   token->keyword = RID_MAX;
407   token->pragma_kind = PRAGMA_NONE;
408
409   /* On some systems, some header files are surrounded by an
410      implicit extern "C" block.  Set a flag in the token if it
411      comes from such a header.  */
412   is_extern_c += pending_lang_change;
413   pending_lang_change = 0;
414   token->implicit_extern_c = is_extern_c > 0;
415
416   /* Check to see if this token is a keyword.  */
417   if (token->type == CPP_NAME)
418     {
419       if (C_IS_RESERVED_WORD (token->u.value))
420         {
421           /* Mark this token as a keyword.  */
422           token->type = CPP_KEYWORD;
423           /* Record which keyword.  */
424           token->keyword = C_RID_CODE (token->u.value);
425         }
426       else
427         {
428           if (warn_cxx0x_compat
429               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
430               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
431             {
432               /* Warn about the C++0x keyword (but still treat it as
433                  an identifier).  */
434               warning (OPT_Wc__0x_compat, 
435                        "identifier %qE will become a keyword in C++0x",
436                        token->u.value);
437
438               /* Clear out the C_RID_CODE so we don't warn about this
439                  particular identifier-turned-keyword again.  */
440               C_SET_RID_CODE (token->u.value, RID_MAX);
441             }
442
443           token->ambiguous_p = false;
444           token->keyword = RID_MAX;
445         }
446     }
447   /* Handle Objective-C++ keywords.  */
448   else if (token->type == CPP_AT_NAME)
449     {
450       token->type = CPP_KEYWORD;
451       switch (C_RID_CODE (token->u.value))
452         {
453         /* Map 'class' to '@class', 'private' to '@private', etc.  */
454         case RID_CLASS: token->keyword = RID_AT_CLASS; break;
455         case RID_PRIVATE: token->keyword = RID_AT_PRIVATE; break;
456         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
457         case RID_PUBLIC: token->keyword = RID_AT_PUBLIC; break;
458         case RID_THROW: token->keyword = RID_AT_THROW; break;
459         case RID_TRY: token->keyword = RID_AT_TRY; break;
460         case RID_CATCH: token->keyword = RID_AT_CATCH; break;
461         default: token->keyword = C_RID_CODE (token->u.value);
462         }
463     }
464   else if (token->type == CPP_PRAGMA)
465     {
466       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
467       token->pragma_kind = ((enum pragma_kind)
468                             TREE_INT_CST_LOW (token->u.value));
469       token->u.value = NULL_TREE;
470     }
471 }
472
473 /* Update the globals input_location and the input file stack from TOKEN.  */
474 static inline void
475 cp_lexer_set_source_position_from_token (cp_token *token)
476 {
477   if (token->type != CPP_EOF)
478     {
479       input_location = token->location;
480     }
481 }
482
483 /* Return a pointer to the next token in the token stream, but do not
484    consume it.  */
485
486 static inline cp_token *
487 cp_lexer_peek_token (cp_lexer *lexer)
488 {
489   if (cp_lexer_debugging_p (lexer))
490     {
491       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
492       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
493       putc ('\n', cp_lexer_debug_stream);
494     }
495   return lexer->next_token;
496 }
497
498 /* Return true if the next token has the indicated TYPE.  */
499
500 static inline bool
501 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
502 {
503   return cp_lexer_peek_token (lexer)->type == type;
504 }
505
506 /* Return true if the next token does not have the indicated TYPE.  */
507
508 static inline bool
509 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
510 {
511   return !cp_lexer_next_token_is (lexer, type);
512 }
513
514 /* Return true if the next token is the indicated KEYWORD.  */
515
516 static inline bool
517 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
518 {
519   return cp_lexer_peek_token (lexer)->keyword == keyword;
520 }
521
522 /* Return true if the next token is not the indicated KEYWORD.  */
523
524 static inline bool
525 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
526 {
527   return cp_lexer_peek_token (lexer)->keyword != keyword;
528 }
529
530 /* Return true if the next token is a keyword for a decl-specifier.  */
531
532 static bool
533 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
534 {
535   cp_token *token;
536
537   token = cp_lexer_peek_token (lexer);
538   switch (token->keyword) 
539     {
540       /* auto specifier: storage-class-specifier in C++,
541          simple-type-specifier in C++0x.  */
542     case RID_AUTO:
543       /* Storage classes.  */
544     case RID_REGISTER:
545     case RID_STATIC:
546     case RID_EXTERN:
547     case RID_MUTABLE:
548     case RID_THREAD:
549       /* Elaborated type specifiers.  */
550     case RID_ENUM:
551     case RID_CLASS:
552     case RID_STRUCT:
553     case RID_UNION:
554     case RID_TYPENAME:
555       /* Simple type specifiers.  */
556     case RID_CHAR:
557     case RID_CHAR16:
558     case RID_CHAR32:
559     case RID_WCHAR:
560     case RID_BOOL:
561     case RID_SHORT:
562     case RID_INT:
563     case RID_LONG:
564     case RID_SIGNED:
565     case RID_UNSIGNED:
566     case RID_FLOAT:
567     case RID_DOUBLE:
568     case RID_VOID:
569       /* GNU extensions.  */ 
570     case RID_ATTRIBUTE:
571     case RID_TYPEOF:
572       /* C++0x extensions.  */
573     case RID_DECLTYPE:
574       return true;
575
576     default:
577       return false;
578     }
579 }
580
581 /* Return a pointer to the Nth token in the token stream.  If N is 1,
582    then this is precisely equivalent to cp_lexer_peek_token (except
583    that it is not inline).  One would like to disallow that case, but
584    there is one case (cp_parser_nth_token_starts_template_id) where
585    the caller passes a variable for N and it might be 1.  */
586
587 static cp_token *
588 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
589 {
590   cp_token *token;
591
592   /* N is 1-based, not zero-based.  */
593   gcc_assert (n > 0);
594
595   if (cp_lexer_debugging_p (lexer))
596     fprintf (cp_lexer_debug_stream,
597              "cp_lexer: peeking ahead %ld at token: ", (long)n);
598
599   --n;
600   token = lexer->next_token;
601   gcc_assert (!n || token != &eof_token);
602   while (n != 0)
603     {
604       ++token;
605       if (token == lexer->last_token)
606         {
607           token = &eof_token;
608           break;
609         }
610
611       if (token->type != CPP_PURGED)
612         --n;
613     }
614
615   if (cp_lexer_debugging_p (lexer))
616     {
617       cp_lexer_print_token (cp_lexer_debug_stream, token);
618       putc ('\n', cp_lexer_debug_stream);
619     }
620
621   return token;
622 }
623
624 /* Return the next token, and advance the lexer's next_token pointer
625    to point to the next non-purged token.  */
626
627 static cp_token *
628 cp_lexer_consume_token (cp_lexer* lexer)
629 {
630   cp_token *token = lexer->next_token;
631
632   gcc_assert (token != &eof_token);
633   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
634
635   do
636     {
637       lexer->next_token++;
638       if (lexer->next_token == lexer->last_token)
639         {
640           lexer->next_token = &eof_token;
641           break;
642         }
643
644     }
645   while (lexer->next_token->type == CPP_PURGED);
646
647   cp_lexer_set_source_position_from_token (token);
648
649   /* Provide debugging output.  */
650   if (cp_lexer_debugging_p (lexer))
651     {
652       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
653       cp_lexer_print_token (cp_lexer_debug_stream, token);
654       putc ('\n', cp_lexer_debug_stream);
655     }
656
657   return token;
658 }
659
660 /* Permanently remove the next token from the token stream, and
661    advance the next_token pointer to refer to the next non-purged
662    token.  */
663
664 static void
665 cp_lexer_purge_token (cp_lexer *lexer)
666 {
667   cp_token *tok = lexer->next_token;
668
669   gcc_assert (tok != &eof_token);
670   tok->type = CPP_PURGED;
671   tok->location = UNKNOWN_LOCATION;
672   tok->u.value = NULL_TREE;
673   tok->keyword = RID_MAX;
674
675   do
676     {
677       tok++;
678       if (tok == lexer->last_token)
679         {
680           tok = &eof_token;
681           break;
682         }
683     }
684   while (tok->type == CPP_PURGED);
685   lexer->next_token = tok;
686 }
687
688 /* Permanently remove all tokens after TOK, up to, but not
689    including, the token that will be returned next by
690    cp_lexer_peek_token.  */
691
692 static void
693 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
694 {
695   cp_token *peek = lexer->next_token;
696
697   if (peek == &eof_token)
698     peek = lexer->last_token;
699
700   gcc_assert (tok < peek);
701
702   for ( tok += 1; tok != peek; tok += 1)
703     {
704       tok->type = CPP_PURGED;
705       tok->location = UNKNOWN_LOCATION;
706       tok->u.value = NULL_TREE;
707       tok->keyword = RID_MAX;
708     }
709 }
710
711 /* Begin saving tokens.  All tokens consumed after this point will be
712    preserved.  */
713
714 static void
715 cp_lexer_save_tokens (cp_lexer* lexer)
716 {
717   /* Provide debugging output.  */
718   if (cp_lexer_debugging_p (lexer))
719     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
720
721   VEC_safe_push (cp_token_position, heap,
722                  lexer->saved_tokens, lexer->next_token);
723 }
724
725 /* Commit to the portion of the token stream most recently saved.  */
726
727 static void
728 cp_lexer_commit_tokens (cp_lexer* lexer)
729 {
730   /* Provide debugging output.  */
731   if (cp_lexer_debugging_p (lexer))
732     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
733
734   VEC_pop (cp_token_position, lexer->saved_tokens);
735 }
736
737 /* Return all tokens saved since the last call to cp_lexer_save_tokens
738    to the token stream.  Stop saving tokens.  */
739
740 static void
741 cp_lexer_rollback_tokens (cp_lexer* lexer)
742 {
743   /* Provide debugging output.  */
744   if (cp_lexer_debugging_p (lexer))
745     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
746
747   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
748 }
749
750 /* Print a representation of the TOKEN on the STREAM.  */
751
752 #ifdef ENABLE_CHECKING
753
754 static void
755 cp_lexer_print_token (FILE * stream, cp_token *token)
756 {
757   /* We don't use cpp_type2name here because the parser defines
758      a few tokens of its own.  */
759   static const char *const token_names[] = {
760     /* cpplib-defined token types */
761 #define OP(e, s) #e,
762 #define TK(e, s) #e,
763     TTYPE_TABLE
764 #undef OP
765 #undef TK
766     /* C++ parser token types - see "Manifest constants", above.  */
767     "KEYWORD",
768     "TEMPLATE_ID",
769     "NESTED_NAME_SPECIFIER",
770     "PURGED"
771   };
772
773   /* If we have a name for the token, print it out.  Otherwise, we
774      simply give the numeric code.  */
775   gcc_assert (token->type < ARRAY_SIZE(token_names));
776   fputs (token_names[token->type], stream);
777
778   /* For some tokens, print the associated data.  */
779   switch (token->type)
780     {
781     case CPP_KEYWORD:
782       /* Some keywords have a value that is not an IDENTIFIER_NODE.
783          For example, `struct' is mapped to an INTEGER_CST.  */
784       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
785         break;
786       /* else fall through */
787     case CPP_NAME:
788       fputs (IDENTIFIER_POINTER (token->u.value), stream);
789       break;
790
791     case CPP_STRING:
792     case CPP_STRING16:
793     case CPP_STRING32:
794     case CPP_WSTRING:
795     case CPP_UTF8STRING:
796       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
797       break;
798
799     default:
800       break;
801     }
802 }
803
804 /* Start emitting debugging information.  */
805
806 static void
807 cp_lexer_start_debugging (cp_lexer* lexer)
808 {
809   lexer->debugging_p = true;
810 }
811
812 /* Stop emitting debugging information.  */
813
814 static void
815 cp_lexer_stop_debugging (cp_lexer* lexer)
816 {
817   lexer->debugging_p = false;
818 }
819
820 #endif /* ENABLE_CHECKING */
821
822 /* Create a new cp_token_cache, representing a range of tokens.  */
823
824 static cp_token_cache *
825 cp_token_cache_new (cp_token *first, cp_token *last)
826 {
827   cp_token_cache *cache = GGC_NEW (cp_token_cache);
828   cache->first = first;
829   cache->last = last;
830   return cache;
831 }
832
833 \f
834 /* Decl-specifiers.  */
835
836 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
837
838 static void
839 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
840 {
841   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
842 }
843
844 /* Declarators.  */
845
846 /* Nothing other than the parser should be creating declarators;
847    declarators are a semi-syntactic representation of C++ entities.
848    Other parts of the front end that need to create entities (like
849    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
850
851 static cp_declarator *make_call_declarator
852   (cp_declarator *, tree, cp_cv_quals, tree, tree);
853 static cp_declarator *make_array_declarator
854   (cp_declarator *, tree);
855 static cp_declarator *make_pointer_declarator
856   (cp_cv_quals, cp_declarator *);
857 static cp_declarator *make_reference_declarator
858   (cp_cv_quals, cp_declarator *, bool);
859 static cp_parameter_declarator *make_parameter_declarator
860   (cp_decl_specifier_seq *, cp_declarator *, tree);
861 static cp_declarator *make_ptrmem_declarator
862   (cp_cv_quals, tree, cp_declarator *);
863
864 /* An erroneous declarator.  */
865 static cp_declarator *cp_error_declarator;
866
867 /* The obstack on which declarators and related data structures are
868    allocated.  */
869 static struct obstack declarator_obstack;
870
871 /* Alloc BYTES from the declarator memory pool.  */
872
873 static inline void *
874 alloc_declarator (size_t bytes)
875 {
876   return obstack_alloc (&declarator_obstack, bytes);
877 }
878
879 /* Allocate a declarator of the indicated KIND.  Clear fields that are
880    common to all declarators.  */
881
882 static cp_declarator *
883 make_declarator (cp_declarator_kind kind)
884 {
885   cp_declarator *declarator;
886
887   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
888   declarator->kind = kind;
889   declarator->attributes = NULL_TREE;
890   declarator->declarator = NULL;
891   declarator->parameter_pack_p = false;
892
893   return declarator;
894 }
895
896 /* Make a declarator for a generalized identifier.  If
897    QUALIFYING_SCOPE is non-NULL, the identifier is
898    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
899    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
900    is, if any.   */
901
902 static cp_declarator *
903 make_id_declarator (tree qualifying_scope, tree unqualified_name,
904                     special_function_kind sfk)
905 {
906   cp_declarator *declarator;
907
908   /* It is valid to write:
909
910        class C { void f(); };
911        typedef C D;
912        void D::f();
913
914      The standard is not clear about whether `typedef const C D' is
915      legal; as of 2002-09-15 the committee is considering that
916      question.  EDG 3.0 allows that syntax.  Therefore, we do as
917      well.  */
918   if (qualifying_scope && TYPE_P (qualifying_scope))
919     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
920
921   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
922               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
923               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
924
925   declarator = make_declarator (cdk_id);
926   declarator->u.id.qualifying_scope = qualifying_scope;
927   declarator->u.id.unqualified_name = unqualified_name;
928   declarator->u.id.sfk = sfk;
929   
930   return declarator;
931 }
932
933 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
934    of modifiers such as const or volatile to apply to the pointer
935    type, represented as identifiers.  */
936
937 cp_declarator *
938 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
939 {
940   cp_declarator *declarator;
941
942   declarator = make_declarator (cdk_pointer);
943   declarator->declarator = target;
944   declarator->u.pointer.qualifiers = cv_qualifiers;
945   declarator->u.pointer.class_type = NULL_TREE;
946   if (target)
947     {
948       declarator->parameter_pack_p = target->parameter_pack_p;
949       target->parameter_pack_p = false;
950     }
951   else
952     declarator->parameter_pack_p = false;
953
954   return declarator;
955 }
956
957 /* Like make_pointer_declarator -- but for references.  */
958
959 cp_declarator *
960 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
961                            bool rvalue_ref)
962 {
963   cp_declarator *declarator;
964
965   declarator = make_declarator (cdk_reference);
966   declarator->declarator = target;
967   declarator->u.reference.qualifiers = cv_qualifiers;
968   declarator->u.reference.rvalue_ref = rvalue_ref;
969   if (target)
970     {
971       declarator->parameter_pack_p = target->parameter_pack_p;
972       target->parameter_pack_p = false;
973     }
974   else
975     declarator->parameter_pack_p = false;
976
977   return declarator;
978 }
979
980 /* Like make_pointer_declarator -- but for a pointer to a non-static
981    member of CLASS_TYPE.  */
982
983 cp_declarator *
984 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
985                         cp_declarator *pointee)
986 {
987   cp_declarator *declarator;
988
989   declarator = make_declarator (cdk_ptrmem);
990   declarator->declarator = pointee;
991   declarator->u.pointer.qualifiers = cv_qualifiers;
992   declarator->u.pointer.class_type = class_type;
993
994   if (pointee)
995     {
996       declarator->parameter_pack_p = pointee->parameter_pack_p;
997       pointee->parameter_pack_p = false;
998     }
999   else
1000     declarator->parameter_pack_p = false;
1001
1002   return declarator;
1003 }
1004
1005 /* Make a declarator for the function given by TARGET, with the
1006    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1007    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1008    indicates what exceptions can be thrown.  */
1009
1010 cp_declarator *
1011 make_call_declarator (cp_declarator *target,
1012                       tree parms,
1013                       cp_cv_quals cv_qualifiers,
1014                       tree exception_specification,
1015                       tree late_return_type)
1016 {
1017   cp_declarator *declarator;
1018
1019   declarator = make_declarator (cdk_function);
1020   declarator->declarator = target;
1021   declarator->u.function.parameters = parms;
1022   declarator->u.function.qualifiers = cv_qualifiers;
1023   declarator->u.function.exception_specification = exception_specification;
1024   declarator->u.function.late_return_type = late_return_type;
1025   if (target)
1026     {
1027       declarator->parameter_pack_p = target->parameter_pack_p;
1028       target->parameter_pack_p = false;
1029     }
1030   else
1031     declarator->parameter_pack_p = false;
1032
1033   return declarator;
1034 }
1035
1036 /* Make a declarator for an array of BOUNDS elements, each of which is
1037    defined by ELEMENT.  */
1038
1039 cp_declarator *
1040 make_array_declarator (cp_declarator *element, tree bounds)
1041 {
1042   cp_declarator *declarator;
1043
1044   declarator = make_declarator (cdk_array);
1045   declarator->declarator = element;
1046   declarator->u.array.bounds = bounds;
1047   if (element)
1048     {
1049       declarator->parameter_pack_p = element->parameter_pack_p;
1050       element->parameter_pack_p = false;
1051     }
1052   else
1053     declarator->parameter_pack_p = false;
1054
1055   return declarator;
1056 }
1057
1058 /* Determine whether the declarator we've seen so far can be a
1059    parameter pack, when followed by an ellipsis.  */
1060 static bool 
1061 declarator_can_be_parameter_pack (cp_declarator *declarator)
1062 {
1063   /* Search for a declarator name, or any other declarator that goes
1064      after the point where the ellipsis could appear in a parameter
1065      pack. If we find any of these, then this declarator can not be
1066      made into a parameter pack.  */
1067   bool found = false;
1068   while (declarator && !found)
1069     {
1070       switch ((int)declarator->kind)
1071         {
1072         case cdk_id:
1073         case cdk_array:
1074           found = true;
1075           break;
1076
1077         case cdk_error:
1078           return true;
1079
1080         default:
1081           declarator = declarator->declarator;
1082           break;
1083         }
1084     }
1085
1086   return !found;
1087 }
1088
1089 cp_parameter_declarator *no_parameters;
1090
1091 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1092    DECLARATOR and DEFAULT_ARGUMENT.  */
1093
1094 cp_parameter_declarator *
1095 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1096                            cp_declarator *declarator,
1097                            tree default_argument)
1098 {
1099   cp_parameter_declarator *parameter;
1100
1101   parameter = ((cp_parameter_declarator *)
1102                alloc_declarator (sizeof (cp_parameter_declarator)));
1103   parameter->next = NULL;
1104   if (decl_specifiers)
1105     parameter->decl_specifiers = *decl_specifiers;
1106   else
1107     clear_decl_specs (&parameter->decl_specifiers);
1108   parameter->declarator = declarator;
1109   parameter->default_argument = default_argument;
1110   parameter->ellipsis_p = false;
1111
1112   return parameter;
1113 }
1114
1115 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1116
1117 static bool
1118 function_declarator_p (const cp_declarator *declarator)
1119 {
1120   while (declarator)
1121     {
1122       if (declarator->kind == cdk_function
1123           && declarator->declarator->kind == cdk_id)
1124         return true;
1125       if (declarator->kind == cdk_id
1126           || declarator->kind == cdk_error)
1127         return false;
1128       declarator = declarator->declarator;
1129     }
1130   return false;
1131 }
1132  
1133 /* The parser.  */
1134
1135 /* Overview
1136    --------
1137
1138    A cp_parser parses the token stream as specified by the C++
1139    grammar.  Its job is purely parsing, not semantic analysis.  For
1140    example, the parser breaks the token stream into declarators,
1141    expressions, statements, and other similar syntactic constructs.
1142    It does not check that the types of the expressions on either side
1143    of an assignment-statement are compatible, or that a function is
1144    not declared with a parameter of type `void'.
1145
1146    The parser invokes routines elsewhere in the compiler to perform
1147    semantic analysis and to build up the abstract syntax tree for the
1148    code processed.
1149
1150    The parser (and the template instantiation code, which is, in a
1151    way, a close relative of parsing) are the only parts of the
1152    compiler that should be calling push_scope and pop_scope, or
1153    related functions.  The parser (and template instantiation code)
1154    keeps track of what scope is presently active; everything else
1155    should simply honor that.  (The code that generates static
1156    initializers may also need to set the scope, in order to check
1157    access control correctly when emitting the initializers.)
1158
1159    Methodology
1160    -----------
1161
1162    The parser is of the standard recursive-descent variety.  Upcoming
1163    tokens in the token stream are examined in order to determine which
1164    production to use when parsing a non-terminal.  Some C++ constructs
1165    require arbitrary look ahead to disambiguate.  For example, it is
1166    impossible, in the general case, to tell whether a statement is an
1167    expression or declaration without scanning the entire statement.
1168    Therefore, the parser is capable of "parsing tentatively."  When the
1169    parser is not sure what construct comes next, it enters this mode.
1170    Then, while we attempt to parse the construct, the parser queues up
1171    error messages, rather than issuing them immediately, and saves the
1172    tokens it consumes.  If the construct is parsed successfully, the
1173    parser "commits", i.e., it issues any queued error messages and
1174    the tokens that were being preserved are permanently discarded.
1175    If, however, the construct is not parsed successfully, the parser
1176    rolls back its state completely so that it can resume parsing using
1177    a different alternative.
1178
1179    Future Improvements
1180    -------------------
1181
1182    The performance of the parser could probably be improved substantially.
1183    We could often eliminate the need to parse tentatively by looking ahead
1184    a little bit.  In some places, this approach might not entirely eliminate
1185    the need to parse tentatively, but it might still speed up the average
1186    case.  */
1187
1188 /* Flags that are passed to some parsing functions.  These values can
1189    be bitwise-ored together.  */
1190
1191 enum
1192 {
1193   /* No flags.  */
1194   CP_PARSER_FLAGS_NONE = 0x0,
1195   /* The construct is optional.  If it is not present, then no error
1196      should be issued.  */
1197   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1198   /* When parsing a type-specifier, treat user-defined type-names
1199      as non-type identifiers.  */
1200   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1201   /* When parsing a type-specifier, do not try to parse a class-specifier
1202      or enum-specifier.  */
1203   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4
1204 };
1205
1206 /* This type is used for parameters and variables which hold
1207    combinations of the above flags.  */
1208 typedef int cp_parser_flags;
1209
1210 /* The different kinds of declarators we want to parse.  */
1211
1212 typedef enum cp_parser_declarator_kind
1213 {
1214   /* We want an abstract declarator.  */
1215   CP_PARSER_DECLARATOR_ABSTRACT,
1216   /* We want a named declarator.  */
1217   CP_PARSER_DECLARATOR_NAMED,
1218   /* We don't mind, but the name must be an unqualified-id.  */
1219   CP_PARSER_DECLARATOR_EITHER
1220 } cp_parser_declarator_kind;
1221
1222 /* The precedence values used to parse binary expressions.  The minimum value
1223    of PREC must be 1, because zero is reserved to quickly discriminate
1224    binary operators from other tokens.  */
1225
1226 enum cp_parser_prec
1227 {
1228   PREC_NOT_OPERATOR,
1229   PREC_LOGICAL_OR_EXPRESSION,
1230   PREC_LOGICAL_AND_EXPRESSION,
1231   PREC_INCLUSIVE_OR_EXPRESSION,
1232   PREC_EXCLUSIVE_OR_EXPRESSION,
1233   PREC_AND_EXPRESSION,
1234   PREC_EQUALITY_EXPRESSION,
1235   PREC_RELATIONAL_EXPRESSION,
1236   PREC_SHIFT_EXPRESSION,
1237   PREC_ADDITIVE_EXPRESSION,
1238   PREC_MULTIPLICATIVE_EXPRESSION,
1239   PREC_PM_EXPRESSION,
1240   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1241 };
1242
1243 /* A mapping from a token type to a corresponding tree node type, with a
1244    precedence value.  */
1245
1246 typedef struct cp_parser_binary_operations_map_node
1247 {
1248   /* The token type.  */
1249   enum cpp_ttype token_type;
1250   /* The corresponding tree code.  */
1251   enum tree_code tree_type;
1252   /* The precedence of this operator.  */
1253   enum cp_parser_prec prec;
1254 } cp_parser_binary_operations_map_node;
1255
1256 /* The status of a tentative parse.  */
1257
1258 typedef enum cp_parser_status_kind
1259 {
1260   /* No errors have occurred.  */
1261   CP_PARSER_STATUS_KIND_NO_ERROR,
1262   /* An error has occurred.  */
1263   CP_PARSER_STATUS_KIND_ERROR,
1264   /* We are committed to this tentative parse, whether or not an error
1265      has occurred.  */
1266   CP_PARSER_STATUS_KIND_COMMITTED
1267 } cp_parser_status_kind;
1268
1269 typedef struct cp_parser_expression_stack_entry
1270 {
1271   /* Left hand side of the binary operation we are currently
1272      parsing.  */
1273   tree lhs;
1274   /* Original tree code for left hand side, if it was a binary
1275      expression itself (used for -Wparentheses).  */
1276   enum tree_code lhs_type;
1277   /* Tree code for the binary operation we are parsing.  */
1278   enum tree_code tree_type;
1279   /* Precedence of the binary operation we are parsing.  */
1280   enum cp_parser_prec prec;
1281 } cp_parser_expression_stack_entry;
1282
1283 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1284    entries because precedence levels on the stack are monotonically
1285    increasing.  */
1286 typedef struct cp_parser_expression_stack_entry
1287   cp_parser_expression_stack[NUM_PREC_VALUES];
1288
1289 /* Context that is saved and restored when parsing tentatively.  */
1290 typedef struct GTY (()) cp_parser_context {
1291   /* If this is a tentative parsing context, the status of the
1292      tentative parse.  */
1293   enum cp_parser_status_kind status;
1294   /* If non-NULL, we have just seen a `x->' or `x.' expression.  Names
1295      that are looked up in this context must be looked up both in the
1296      scope given by OBJECT_TYPE (the type of `x' or `*x') and also in
1297      the context of the containing expression.  */
1298   tree object_type;
1299
1300   /* The next parsing context in the stack.  */
1301   struct cp_parser_context *next;
1302 } cp_parser_context;
1303
1304 /* Prototypes.  */
1305
1306 /* Constructors and destructors.  */
1307
1308 static cp_parser_context *cp_parser_context_new
1309   (cp_parser_context *);
1310
1311 /* Class variables.  */
1312
1313 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1314
1315 /* The operator-precedence table used by cp_parser_binary_expression.
1316    Transformed into an associative array (binops_by_token) by
1317    cp_parser_new.  */
1318
1319 static const cp_parser_binary_operations_map_node binops[] = {
1320   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1321   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1322
1323   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1324   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1325   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1326
1327   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1328   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1329
1330   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1331   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1332
1333   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1334   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1335   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1336   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1337
1338   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1339   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1340
1341   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1342
1343   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1344
1345   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1346
1347   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1348
1349   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1350 };
1351
1352 /* The same as binops, but initialized by cp_parser_new so that
1353    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1354    for speed.  */
1355 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1356
1357 /* Constructors and destructors.  */
1358
1359 /* Construct a new context.  The context below this one on the stack
1360    is given by NEXT.  */
1361
1362 static cp_parser_context *
1363 cp_parser_context_new (cp_parser_context* next)
1364 {
1365   cp_parser_context *context;
1366
1367   /* Allocate the storage.  */
1368   if (cp_parser_context_free_list != NULL)
1369     {
1370       /* Pull the first entry from the free list.  */
1371       context = cp_parser_context_free_list;
1372       cp_parser_context_free_list = context->next;
1373       memset (context, 0, sizeof (*context));
1374     }
1375   else
1376     context = GGC_CNEW (cp_parser_context);
1377
1378   /* No errors have occurred yet in this context.  */
1379   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1380   /* If this is not the bottommost context, copy information that we
1381      need from the previous context.  */
1382   if (next)
1383     {
1384       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1385          expression, then we are parsing one in this context, too.  */
1386       context->object_type = next->object_type;
1387       /* Thread the stack.  */
1388       context->next = next;
1389     }
1390
1391   return context;
1392 }
1393
1394 /* The cp_parser structure represents the C++ parser.  */
1395
1396 typedef struct GTY(()) cp_parser {
1397   /* The lexer from which we are obtaining tokens.  */
1398   cp_lexer *lexer;
1399
1400   /* The scope in which names should be looked up.  If NULL_TREE, then
1401      we look up names in the scope that is currently open in the
1402      source program.  If non-NULL, this is either a TYPE or
1403      NAMESPACE_DECL for the scope in which we should look.  It can
1404      also be ERROR_MARK, when we've parsed a bogus scope.
1405
1406      This value is not cleared automatically after a name is looked
1407      up, so we must be careful to clear it before starting a new look
1408      up sequence.  (If it is not cleared, then `X::Y' followed by `Z'
1409      will look up `Z' in the scope of `X', rather than the current
1410      scope.)  Unfortunately, it is difficult to tell when name lookup
1411      is complete, because we sometimes peek at a token, look it up,
1412      and then decide not to consume it.   */
1413   tree scope;
1414
1415   /* OBJECT_SCOPE and QUALIFYING_SCOPE give the scopes in which the
1416      last lookup took place.  OBJECT_SCOPE is used if an expression
1417      like "x->y" or "x.y" was used; it gives the type of "*x" or "x",
1418      respectively.  QUALIFYING_SCOPE is used for an expression of the
1419      form "X::Y"; it refers to X.  */
1420   tree object_scope;
1421   tree qualifying_scope;
1422
1423   /* A stack of parsing contexts.  All but the bottom entry on the
1424      stack will be tentative contexts.
1425
1426      We parse tentatively in order to determine which construct is in
1427      use in some situations.  For example, in order to determine
1428      whether a statement is an expression-statement or a
1429      declaration-statement we parse it tentatively as a
1430      declaration-statement.  If that fails, we then reparse the same
1431      token stream as an expression-statement.  */
1432   cp_parser_context *context;
1433
1434   /* True if we are parsing GNU C++.  If this flag is not set, then
1435      GNU extensions are not recognized.  */
1436   bool allow_gnu_extensions_p;
1437
1438   /* TRUE if the `>' token should be interpreted as the greater-than
1439      operator.  FALSE if it is the end of a template-id or
1440      template-parameter-list. In C++0x mode, this flag also applies to
1441      `>>' tokens, which are viewed as two consecutive `>' tokens when
1442      this flag is FALSE.  */
1443   bool greater_than_is_operator_p;
1444
1445   /* TRUE if default arguments are allowed within a parameter list
1446      that starts at this point. FALSE if only a gnu extension makes
1447      them permissible.  */
1448   bool default_arg_ok_p;
1449
1450   /* TRUE if we are parsing an integral constant-expression.  See
1451      [expr.const] for a precise definition.  */
1452   bool integral_constant_expression_p;
1453
1454   /* TRUE if we are parsing an integral constant-expression -- but a
1455      non-constant expression should be permitted as well.  This flag
1456      is used when parsing an array bound so that GNU variable-length
1457      arrays are tolerated.  */
1458   bool allow_non_integral_constant_expression_p;
1459
1460   /* TRUE if ALLOW_NON_CONSTANT_EXPRESSION_P is TRUE and something has
1461      been seen that makes the expression non-constant.  */
1462   bool non_integral_constant_expression_p;
1463
1464   /* TRUE if local variable names and `this' are forbidden in the
1465      current context.  */
1466   bool local_variables_forbidden_p;
1467
1468   /* TRUE if the declaration we are parsing is part of a
1469      linkage-specification of the form `extern string-literal
1470      declaration'.  */
1471   bool in_unbraced_linkage_specification_p;
1472
1473   /* TRUE if we are presently parsing a declarator, after the
1474      direct-declarator.  */
1475   bool in_declarator_p;
1476
1477   /* TRUE if we are presently parsing a template-argument-list.  */
1478   bool in_template_argument_list_p;
1479
1480   /* Set to IN_ITERATION_STMT if parsing an iteration-statement,
1481      to IN_OMP_BLOCK if parsing OpenMP structured block and
1482      IN_OMP_FOR if parsing OpenMP loop.  If parsing a switch statement,
1483      this is bitwise ORed with IN_SWITCH_STMT, unless parsing an
1484      iteration-statement, OpenMP block or loop within that switch.  */
1485 #define IN_SWITCH_STMT          1
1486 #define IN_ITERATION_STMT       2
1487 #define IN_OMP_BLOCK            4
1488 #define IN_OMP_FOR              8
1489 #define IN_IF_STMT             16
1490   unsigned char in_statement;
1491
1492   /* TRUE if we are presently parsing the body of a switch statement.
1493      Note that this doesn't quite overlap with in_statement above.
1494      The difference relates to giving the right sets of error messages:
1495      "case not in switch" vs "break statement used with OpenMP...".  */
1496   bool in_switch_statement_p;
1497
1498   /* TRUE if we are parsing a type-id in an expression context.  In
1499      such a situation, both "type (expr)" and "type (type)" are valid
1500      alternatives.  */
1501   bool in_type_id_in_expr_p;
1502
1503   /* TRUE if we are currently in a header file where declarations are
1504      implicitly extern "C".  */
1505   bool implicit_extern_c;
1506
1507   /* TRUE if strings in expressions should be translated to the execution
1508      character set.  */
1509   bool translate_strings_p;
1510
1511   /* TRUE if we are presently parsing the body of a function, but not
1512      a local class.  */
1513   bool in_function_body;
1514
1515   /* If non-NULL, then we are parsing a construct where new type
1516      definitions are not permitted.  The string stored here will be
1517      issued as an error message if a type is defined.  */
1518   const char *type_definition_forbidden_message;
1519
1520   /* A list of lists. The outer list is a stack, used for member
1521      functions of local classes. At each level there are two sub-list,
1522      one on TREE_VALUE and one on TREE_PURPOSE. Each of those
1523      sub-lists has a FUNCTION_DECL or TEMPLATE_DECL on their
1524      TREE_VALUE's. The functions are chained in reverse declaration
1525      order.
1526
1527      The TREE_PURPOSE sublist contains those functions with default
1528      arguments that need post processing, and the TREE_VALUE sublist
1529      contains those functions with definitions that need post
1530      processing.
1531
1532      These lists can only be processed once the outermost class being
1533      defined is complete.  */
1534   tree unparsed_functions_queues;
1535
1536   /* The number of classes whose definitions are currently in
1537      progress.  */
1538   unsigned num_classes_being_defined;
1539
1540   /* The number of template parameter lists that apply directly to the
1541      current declaration.  */
1542   unsigned num_template_parameter_lists;
1543 } cp_parser;
1544
1545 /* Prototypes.  */
1546
1547 /* Constructors and destructors.  */
1548
1549 static cp_parser *cp_parser_new
1550   (void);
1551
1552 /* Routines to parse various constructs.
1553
1554    Those that return `tree' will return the error_mark_node (rather
1555    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1556    Sometimes, they will return an ordinary node if error-recovery was
1557    attempted, even though a parse error occurred.  So, to check
1558    whether or not a parse error occurred, you should always use
1559    cp_parser_error_occurred.  If the construct is optional (indicated
1560    either by an `_opt' in the name of the function that does the
1561    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1562    the construct is not present.  */
1563
1564 /* Lexical conventions [gram.lex]  */
1565
1566 static tree cp_parser_identifier
1567   (cp_parser *);
1568 static tree cp_parser_string_literal
1569   (cp_parser *, bool, bool);
1570
1571 /* Basic concepts [gram.basic]  */
1572
1573 static bool cp_parser_translation_unit
1574   (cp_parser *);
1575
1576 /* Expressions [gram.expr]  */
1577
1578 static tree cp_parser_primary_expression
1579   (cp_parser *, bool, bool, bool, cp_id_kind *);
1580 static tree cp_parser_id_expression
1581   (cp_parser *, bool, bool, bool *, bool, bool);
1582 static tree cp_parser_unqualified_id
1583   (cp_parser *, bool, bool, bool, bool);
1584 static tree cp_parser_nested_name_specifier_opt
1585   (cp_parser *, bool, bool, bool, bool);
1586 static tree cp_parser_nested_name_specifier
1587   (cp_parser *, bool, bool, bool, bool);
1588 static tree cp_parser_qualifying_entity
1589   (cp_parser *, bool, bool, bool, bool, bool);
1590 static tree cp_parser_postfix_expression
1591   (cp_parser *, bool, bool, bool, cp_id_kind *);
1592 static tree cp_parser_postfix_open_square_expression
1593   (cp_parser *, tree, bool);
1594 static tree cp_parser_postfix_dot_deref_expression
1595   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1596 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1597   (cp_parser *, bool, bool, bool, bool *);
1598 static void cp_parser_pseudo_destructor_name
1599   (cp_parser *, tree *, tree *);
1600 static tree cp_parser_unary_expression
1601   (cp_parser *, bool, bool, cp_id_kind *);
1602 static enum tree_code cp_parser_unary_operator
1603   (cp_token *);
1604 static tree cp_parser_new_expression
1605   (cp_parser *);
1606 static VEC(tree,gc) *cp_parser_new_placement
1607   (cp_parser *);
1608 static tree cp_parser_new_type_id
1609   (cp_parser *, tree *);
1610 static cp_declarator *cp_parser_new_declarator_opt
1611   (cp_parser *);
1612 static cp_declarator *cp_parser_direct_new_declarator
1613   (cp_parser *);
1614 static VEC(tree,gc) *cp_parser_new_initializer
1615   (cp_parser *);
1616 static tree cp_parser_delete_expression
1617   (cp_parser *);
1618 static tree cp_parser_cast_expression
1619   (cp_parser *, bool, bool, cp_id_kind *);
1620 static tree cp_parser_binary_expression
1621   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1622 static tree cp_parser_question_colon_clause
1623   (cp_parser *, tree);
1624 static tree cp_parser_assignment_expression
1625   (cp_parser *, bool, cp_id_kind *);
1626 static enum tree_code cp_parser_assignment_operator_opt
1627   (cp_parser *);
1628 static tree cp_parser_expression
1629   (cp_parser *, bool, cp_id_kind *);
1630 static tree cp_parser_constant_expression
1631   (cp_parser *, bool, bool *);
1632 static tree cp_parser_builtin_offsetof
1633   (cp_parser *);
1634 static tree cp_parser_lambda_expression
1635   (cp_parser *);
1636 static void cp_parser_lambda_introducer
1637   (cp_parser *, tree);
1638 static void cp_parser_lambda_declarator_opt
1639   (cp_parser *, tree);
1640 static void cp_parser_lambda_body
1641   (cp_parser *, tree);
1642
1643 /* Statements [gram.stmt.stmt]  */
1644
1645 static void cp_parser_statement
1646   (cp_parser *, tree, bool, bool *);
1647 static void cp_parser_label_for_labeled_statement
1648   (cp_parser *);
1649 static tree cp_parser_expression_statement
1650   (cp_parser *, tree);
1651 static tree cp_parser_compound_statement
1652   (cp_parser *, tree, bool);
1653 static void cp_parser_statement_seq_opt
1654   (cp_parser *, tree);
1655 static tree cp_parser_selection_statement
1656   (cp_parser *, bool *);
1657 static tree cp_parser_condition
1658   (cp_parser *);
1659 static tree cp_parser_iteration_statement
1660   (cp_parser *);
1661 static void cp_parser_for_init_statement
1662   (cp_parser *);
1663 static tree cp_parser_jump_statement
1664   (cp_parser *);
1665 static void cp_parser_declaration_statement
1666   (cp_parser *);
1667
1668 static tree cp_parser_implicitly_scoped_statement
1669   (cp_parser *, bool *);
1670 static void cp_parser_already_scoped_statement
1671   (cp_parser *);
1672
1673 /* Declarations [gram.dcl.dcl] */
1674
1675 static void cp_parser_declaration_seq_opt
1676   (cp_parser *);
1677 static void cp_parser_declaration
1678   (cp_parser *);
1679 static void cp_parser_block_declaration
1680   (cp_parser *, bool);
1681 static void cp_parser_simple_declaration
1682   (cp_parser *, bool);
1683 static void cp_parser_decl_specifier_seq
1684   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1685 static tree cp_parser_storage_class_specifier_opt
1686   (cp_parser *);
1687 static tree cp_parser_function_specifier_opt
1688   (cp_parser *, cp_decl_specifier_seq *);
1689 static tree cp_parser_type_specifier
1690   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1691    int *, bool *);
1692 static tree cp_parser_simple_type_specifier
1693   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1694 static tree cp_parser_type_name
1695   (cp_parser *);
1696 static tree cp_parser_nonclass_name 
1697   (cp_parser* parser);
1698 static tree cp_parser_elaborated_type_specifier
1699   (cp_parser *, bool, bool);
1700 static tree cp_parser_enum_specifier
1701   (cp_parser *);
1702 static void cp_parser_enumerator_list
1703   (cp_parser *, tree);
1704 static void cp_parser_enumerator_definition
1705   (cp_parser *, tree);
1706 static tree cp_parser_namespace_name
1707   (cp_parser *);
1708 static void cp_parser_namespace_definition
1709   (cp_parser *);
1710 static void cp_parser_namespace_body
1711   (cp_parser *);
1712 static tree cp_parser_qualified_namespace_specifier
1713   (cp_parser *);
1714 static void cp_parser_namespace_alias_definition
1715   (cp_parser *);
1716 static bool cp_parser_using_declaration
1717   (cp_parser *, bool);
1718 static void cp_parser_using_directive
1719   (cp_parser *);
1720 static void cp_parser_asm_definition
1721   (cp_parser *);
1722 static void cp_parser_linkage_specification
1723   (cp_parser *);
1724 static void cp_parser_static_assert
1725   (cp_parser *, bool);
1726 static tree cp_parser_decltype
1727   (cp_parser *);
1728
1729 /* Declarators [gram.dcl.decl] */
1730
1731 static tree cp_parser_init_declarator
1732   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *);
1733 static cp_declarator *cp_parser_declarator
1734   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1735 static cp_declarator *cp_parser_direct_declarator
1736   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1737 static enum tree_code cp_parser_ptr_operator
1738   (cp_parser *, tree *, cp_cv_quals *);
1739 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1740   (cp_parser *);
1741 static tree cp_parser_late_return_type_opt
1742   (cp_parser *);
1743 static tree cp_parser_declarator_id
1744   (cp_parser *, bool);
1745 static tree cp_parser_type_id
1746   (cp_parser *);
1747 static tree cp_parser_template_type_arg
1748   (cp_parser *);
1749 static tree cp_parser_trailing_type_id (cp_parser *);
1750 static tree cp_parser_type_id_1
1751   (cp_parser *, bool, bool);
1752 static void cp_parser_type_specifier_seq
1753   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1754 static tree cp_parser_parameter_declaration_clause
1755   (cp_parser *);
1756 static tree cp_parser_parameter_declaration_list
1757   (cp_parser *, bool *);
1758 static cp_parameter_declarator *cp_parser_parameter_declaration
1759   (cp_parser *, bool, bool *);
1760 static tree cp_parser_default_argument 
1761   (cp_parser *, bool);
1762 static void cp_parser_function_body
1763   (cp_parser *);
1764 static tree cp_parser_initializer
1765   (cp_parser *, bool *, bool *);
1766 static tree cp_parser_initializer_clause
1767   (cp_parser *, bool *);
1768 static tree cp_parser_braced_list
1769   (cp_parser*, bool*);
1770 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1771   (cp_parser *, bool *);
1772
1773 static bool cp_parser_ctor_initializer_opt_and_function_body
1774   (cp_parser *);
1775
1776 /* Classes [gram.class] */
1777
1778 static tree cp_parser_class_name
1779   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1780 static tree cp_parser_class_specifier
1781   (cp_parser *);
1782 static tree cp_parser_class_head
1783   (cp_parser *, bool *, tree *, tree *);
1784 static enum tag_types cp_parser_class_key
1785   (cp_parser *);
1786 static void cp_parser_member_specification_opt
1787   (cp_parser *);
1788 static void cp_parser_member_declaration
1789   (cp_parser *);
1790 static tree cp_parser_pure_specifier
1791   (cp_parser *);
1792 static tree cp_parser_constant_initializer
1793   (cp_parser *);
1794
1795 /* Derived classes [gram.class.derived] */
1796
1797 static tree cp_parser_base_clause
1798   (cp_parser *);
1799 static tree cp_parser_base_specifier
1800   (cp_parser *);
1801
1802 /* Special member functions [gram.special] */
1803
1804 static tree cp_parser_conversion_function_id
1805   (cp_parser *);
1806 static tree cp_parser_conversion_type_id
1807   (cp_parser *);
1808 static cp_declarator *cp_parser_conversion_declarator_opt
1809   (cp_parser *);
1810 static bool cp_parser_ctor_initializer_opt
1811   (cp_parser *);
1812 static void cp_parser_mem_initializer_list
1813   (cp_parser *);
1814 static tree cp_parser_mem_initializer
1815   (cp_parser *);
1816 static tree cp_parser_mem_initializer_id
1817   (cp_parser *);
1818
1819 /* Overloading [gram.over] */
1820
1821 static tree cp_parser_operator_function_id
1822   (cp_parser *);
1823 static tree cp_parser_operator
1824   (cp_parser *);
1825
1826 /* Templates [gram.temp] */
1827
1828 static void cp_parser_template_declaration
1829   (cp_parser *, bool);
1830 static tree cp_parser_template_parameter_list
1831   (cp_parser *);
1832 static tree cp_parser_template_parameter
1833   (cp_parser *, bool *, bool *);
1834 static tree cp_parser_type_parameter
1835   (cp_parser *, bool *);
1836 static tree cp_parser_template_id
1837   (cp_parser *, bool, bool, bool);
1838 static tree cp_parser_template_name
1839   (cp_parser *, bool, bool, bool, bool *);
1840 static tree cp_parser_template_argument_list
1841   (cp_parser *);
1842 static tree cp_parser_template_argument
1843   (cp_parser *);
1844 static void cp_parser_explicit_instantiation
1845   (cp_parser *);
1846 static void cp_parser_explicit_specialization
1847   (cp_parser *);
1848
1849 /* Exception handling [gram.exception] */
1850
1851 static tree cp_parser_try_block
1852   (cp_parser *);
1853 static bool cp_parser_function_try_block
1854   (cp_parser *);
1855 static void cp_parser_handler_seq
1856   (cp_parser *);
1857 static void cp_parser_handler
1858   (cp_parser *);
1859 static tree cp_parser_exception_declaration
1860   (cp_parser *);
1861 static tree cp_parser_throw_expression
1862   (cp_parser *);
1863 static tree cp_parser_exception_specification_opt
1864   (cp_parser *);
1865 static tree cp_parser_type_id_list
1866   (cp_parser *);
1867
1868 /* GNU Extensions */
1869
1870 static tree cp_parser_asm_specification_opt
1871   (cp_parser *);
1872 static tree cp_parser_asm_operand_list
1873   (cp_parser *);
1874 static tree cp_parser_asm_clobber_list
1875   (cp_parser *);
1876 static tree cp_parser_asm_label_list
1877   (cp_parser *);
1878 static tree cp_parser_attributes_opt
1879   (cp_parser *);
1880 static tree cp_parser_attribute_list
1881   (cp_parser *);
1882 static bool cp_parser_extension_opt
1883   (cp_parser *, int *);
1884 static void cp_parser_label_declaration
1885   (cp_parser *);
1886
1887 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1888 static bool cp_parser_pragma
1889   (cp_parser *, enum pragma_context);
1890
1891 /* Objective-C++ Productions */
1892
1893 static tree cp_parser_objc_message_receiver
1894   (cp_parser *);
1895 static tree cp_parser_objc_message_args
1896   (cp_parser *);
1897 static tree cp_parser_objc_message_expression
1898   (cp_parser *);
1899 static tree cp_parser_objc_encode_expression
1900   (cp_parser *);
1901 static tree cp_parser_objc_defs_expression
1902   (cp_parser *);
1903 static tree cp_parser_objc_protocol_expression
1904   (cp_parser *);
1905 static tree cp_parser_objc_selector_expression
1906   (cp_parser *);
1907 static tree cp_parser_objc_expression
1908   (cp_parser *);
1909 static bool cp_parser_objc_selector_p
1910   (enum cpp_ttype);
1911 static tree cp_parser_objc_selector
1912   (cp_parser *);
1913 static tree cp_parser_objc_protocol_refs_opt
1914   (cp_parser *);
1915 static void cp_parser_objc_declaration
1916   (cp_parser *);
1917 static tree cp_parser_objc_statement
1918   (cp_parser *);
1919
1920 /* Utility Routines */
1921
1922 static tree cp_parser_lookup_name
1923   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1924 static tree cp_parser_lookup_name_simple
1925   (cp_parser *, tree, location_t);
1926 static tree cp_parser_maybe_treat_template_as_class
1927   (tree, bool);
1928 static bool cp_parser_check_declarator_template_parameters
1929   (cp_parser *, cp_declarator *, location_t);
1930 static bool cp_parser_check_template_parameters
1931   (cp_parser *, unsigned, location_t, cp_declarator *);
1932 static tree cp_parser_simple_cast_expression
1933   (cp_parser *);
1934 static tree cp_parser_global_scope_opt
1935   (cp_parser *, bool);
1936 static bool cp_parser_constructor_declarator_p
1937   (cp_parser *, bool);
1938 static tree cp_parser_function_definition_from_specifiers_and_declarator
1939   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1940 static tree cp_parser_function_definition_after_declarator
1941   (cp_parser *, bool);
1942 static void cp_parser_template_declaration_after_export
1943   (cp_parser *, bool);
1944 static void cp_parser_perform_template_parameter_access_checks
1945   (VEC (deferred_access_check,gc)*);
1946 static tree cp_parser_single_declaration
1947   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1948 static tree cp_parser_functional_cast
1949   (cp_parser *, tree);
1950 static tree cp_parser_save_member_function_body
1951   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1952 static tree cp_parser_enclosed_template_argument_list
1953   (cp_parser *);
1954 static void cp_parser_save_default_args
1955   (cp_parser *, tree);
1956 static void cp_parser_late_parsing_for_member
1957   (cp_parser *, tree);
1958 static void cp_parser_late_parsing_default_args
1959   (cp_parser *, tree);
1960 static tree cp_parser_sizeof_operand
1961   (cp_parser *, enum rid);
1962 static tree cp_parser_trait_expr
1963   (cp_parser *, enum rid);
1964 static bool cp_parser_declares_only_class_p
1965   (cp_parser *);
1966 static void cp_parser_set_storage_class
1967   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1968 static void cp_parser_set_decl_spec_type
1969   (cp_decl_specifier_seq *, tree, location_t, bool);
1970 static bool cp_parser_friend_p
1971   (const cp_decl_specifier_seq *);
1972 static cp_token *cp_parser_require
1973   (cp_parser *, enum cpp_ttype, const char *);
1974 static cp_token *cp_parser_require_keyword
1975   (cp_parser *, enum rid, const char *);
1976 static bool cp_parser_token_starts_function_definition_p
1977   (cp_token *);
1978 static bool cp_parser_next_token_starts_class_definition_p
1979   (cp_parser *);
1980 static bool cp_parser_next_token_ends_template_argument_p
1981   (cp_parser *);
1982 static bool cp_parser_nth_token_starts_template_argument_list_p
1983   (cp_parser *, size_t);
1984 static enum tag_types cp_parser_token_is_class_key
1985   (cp_token *);
1986 static void cp_parser_check_class_key
1987   (enum tag_types, tree type);
1988 static void cp_parser_check_access_in_redeclaration
1989   (tree type, location_t location);
1990 static bool cp_parser_optional_template_keyword
1991   (cp_parser *);
1992 static void cp_parser_pre_parsed_nested_name_specifier
1993   (cp_parser *);
1994 static bool cp_parser_cache_group
1995   (cp_parser *, enum cpp_ttype, unsigned);
1996 static void cp_parser_parse_tentatively
1997   (cp_parser *);
1998 static void cp_parser_commit_to_tentative_parse
1999   (cp_parser *);
2000 static void cp_parser_abort_tentative_parse
2001   (cp_parser *);
2002 static bool cp_parser_parse_definitely
2003   (cp_parser *);
2004 static inline bool cp_parser_parsing_tentatively
2005   (cp_parser *);
2006 static bool cp_parser_uncommitted_to_tentative_parse_p
2007   (cp_parser *);
2008 static void cp_parser_error
2009   (cp_parser *, const char *);
2010 static void cp_parser_name_lookup_error
2011   (cp_parser *, tree, tree, const char *, location_t);
2012 static bool cp_parser_simulate_error
2013   (cp_parser *);
2014 static bool cp_parser_check_type_definition
2015   (cp_parser *);
2016 static void cp_parser_check_for_definition_in_return_type
2017   (cp_declarator *, tree, location_t type_location);
2018 static void cp_parser_check_for_invalid_template_id
2019   (cp_parser *, tree, location_t location);
2020 static bool cp_parser_non_integral_constant_expression
2021   (cp_parser *, const char *);
2022 static void cp_parser_diagnose_invalid_type_name
2023   (cp_parser *, tree, tree, location_t);
2024 static bool cp_parser_parse_and_diagnose_invalid_type_name
2025   (cp_parser *);
2026 static int cp_parser_skip_to_closing_parenthesis
2027   (cp_parser *, bool, bool, bool);
2028 static void cp_parser_skip_to_end_of_statement
2029   (cp_parser *);
2030 static void cp_parser_consume_semicolon_at_end_of_statement
2031   (cp_parser *);
2032 static void cp_parser_skip_to_end_of_block_or_statement
2033   (cp_parser *);
2034 static bool cp_parser_skip_to_closing_brace
2035   (cp_parser *);
2036 static void cp_parser_skip_to_end_of_template_parameter_list
2037   (cp_parser *);
2038 static void cp_parser_skip_to_pragma_eol
2039   (cp_parser*, cp_token *);
2040 static bool cp_parser_error_occurred
2041   (cp_parser *);
2042 static bool cp_parser_allow_gnu_extensions_p
2043   (cp_parser *);
2044 static bool cp_parser_is_string_literal
2045   (cp_token *);
2046 static bool cp_parser_is_keyword
2047   (cp_token *, enum rid);
2048 static tree cp_parser_make_typename_type
2049   (cp_parser *, tree, tree, location_t location);
2050 static cp_declarator * cp_parser_make_indirect_declarator
2051   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2052
2053 /* Returns nonzero if we are parsing tentatively.  */
2054
2055 static inline bool
2056 cp_parser_parsing_tentatively (cp_parser* parser)
2057 {
2058   return parser->context->next != NULL;
2059 }
2060
2061 /* Returns nonzero if TOKEN is a string literal.  */
2062
2063 static bool
2064 cp_parser_is_string_literal (cp_token* token)
2065 {
2066   return (token->type == CPP_STRING ||
2067           token->type == CPP_STRING16 ||
2068           token->type == CPP_STRING32 ||
2069           token->type == CPP_WSTRING ||
2070           token->type == CPP_UTF8STRING);
2071 }
2072
2073 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2074
2075 static bool
2076 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2077 {
2078   return token->keyword == keyword;
2079 }
2080
2081 /* If not parsing tentatively, issue a diagnostic of the form
2082       FILE:LINE: MESSAGE before TOKEN
2083    where TOKEN is the next token in the input stream.  MESSAGE
2084    (specified by the caller) is usually of the form "expected
2085    OTHER-TOKEN".  */
2086
2087 static void
2088 cp_parser_error (cp_parser* parser, const char* message)
2089 {
2090   if (!cp_parser_simulate_error (parser))
2091     {
2092       cp_token *token = cp_lexer_peek_token (parser->lexer);
2093       /* This diagnostic makes more sense if it is tagged to the line
2094          of the token we just peeked at.  */
2095       cp_lexer_set_source_position_from_token (token);
2096
2097       if (token->type == CPP_PRAGMA)
2098         {
2099           error_at (token->location,
2100                     "%<#pragma%> is not allowed here");
2101           cp_parser_skip_to_pragma_eol (parser, token);
2102           return;
2103         }
2104
2105       c_parse_error (message,
2106                      /* Because c_parser_error does not understand
2107                         CPP_KEYWORD, keywords are treated like
2108                         identifiers.  */
2109                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2110                      token->u.value, token->flags);
2111     }
2112 }
2113
2114 /* Issue an error about name-lookup failing.  NAME is the
2115    IDENTIFIER_NODE DECL is the result of
2116    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2117    the thing that we hoped to find.  */
2118
2119 static void
2120 cp_parser_name_lookup_error (cp_parser* parser,
2121                              tree name,
2122                              tree decl,
2123                              const char* desired,
2124                              location_t location)
2125 {
2126   /* If name lookup completely failed, tell the user that NAME was not
2127      declared.  */
2128   if (decl == error_mark_node)
2129     {
2130       if (parser->scope && parser->scope != global_namespace)
2131         error_at (location, "%<%E::%E%> has not been declared",
2132                   parser->scope, name);
2133       else if (parser->scope == global_namespace)
2134         error_at (location, "%<::%E%> has not been declared", name);
2135       else if (parser->object_scope
2136                && !CLASS_TYPE_P (parser->object_scope))
2137         error_at (location, "request for member %qE in non-class type %qT",
2138                   name, parser->object_scope);
2139       else if (parser->object_scope)
2140         error_at (location, "%<%T::%E%> has not been declared",
2141                   parser->object_scope, name);
2142       else
2143         error_at (location, "%qE has not been declared", name);
2144     }
2145   else if (parser->scope && parser->scope != global_namespace)
2146     error_at (location, "%<%E::%E%> %s", parser->scope, name, desired);
2147   else if (parser->scope == global_namespace)
2148     error_at (location, "%<::%E%> %s", name, desired);
2149   else
2150     error_at (location, "%qE %s", name, desired);
2151 }
2152
2153 /* If we are parsing tentatively, remember that an error has occurred
2154    during this tentative parse.  Returns true if the error was
2155    simulated; false if a message should be issued by the caller.  */
2156
2157 static bool
2158 cp_parser_simulate_error (cp_parser* parser)
2159 {
2160   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2161     {
2162       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2163       return true;
2164     }
2165   return false;
2166 }
2167
2168 /* Check for repeated decl-specifiers.  */
2169
2170 static void
2171 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2172                            location_t location)
2173 {
2174   int ds;
2175
2176   for (ds = ds_first; ds != ds_last; ++ds)
2177     {
2178       unsigned count = decl_specs->specs[ds];
2179       if (count < 2)
2180         continue;
2181       /* The "long" specifier is a special case because of "long long".  */
2182       if (ds == ds_long)
2183         {
2184           if (count > 2)
2185             error_at (location, "%<long long long%> is too long for GCC");
2186           else 
2187             pedwarn_cxx98 (location, OPT_Wlong_long, 
2188                            "ISO C++ 1998 does not support %<long long%>");
2189         }
2190       else if (count > 1)
2191         {
2192           static const char *const decl_spec_names[] = {
2193             "signed",
2194             "unsigned",
2195             "short",
2196             "long",
2197             "const",
2198             "volatile",
2199             "restrict",
2200             "inline",
2201             "virtual",
2202             "explicit",
2203             "friend",
2204             "typedef",
2205             "constexpr",
2206             "__complex",
2207             "__thread"
2208           };
2209           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2210         }
2211     }
2212 }
2213
2214 /* This function is called when a type is defined.  If type
2215    definitions are forbidden at this point, an error message is
2216    issued.  */
2217
2218 static bool
2219 cp_parser_check_type_definition (cp_parser* parser)
2220 {
2221   /* If types are forbidden here, issue a message.  */
2222   if (parser->type_definition_forbidden_message)
2223     {
2224       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2225          in the message need to be interpreted.  */
2226       error (parser->type_definition_forbidden_message);
2227       return false;
2228     }
2229   return true;
2230 }
2231
2232 /* This function is called when the DECLARATOR is processed.  The TYPE
2233    was a type defined in the decl-specifiers.  If it is invalid to
2234    define a type in the decl-specifiers for DECLARATOR, an error is
2235    issued. TYPE_LOCATION is the location of TYPE and is used
2236    for error reporting.  */
2237
2238 static void
2239 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2240                                                tree type, location_t type_location)
2241 {
2242   /* [dcl.fct] forbids type definitions in return types.
2243      Unfortunately, it's not easy to know whether or not we are
2244      processing a return type until after the fact.  */
2245   while (declarator
2246          && (declarator->kind == cdk_pointer
2247              || declarator->kind == cdk_reference
2248              || declarator->kind == cdk_ptrmem))
2249     declarator = declarator->declarator;
2250   if (declarator
2251       && declarator->kind == cdk_function)
2252     {
2253       error_at (type_location,
2254                 "new types may not be defined in a return type");
2255       inform (type_location, 
2256               "(perhaps a semicolon is missing after the definition of %qT)",
2257               type);
2258     }
2259 }
2260
2261 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2262    "<" in any valid C++ program.  If the next token is indeed "<",
2263    issue a message warning the user about what appears to be an
2264    invalid attempt to form a template-id. LOCATION is the location
2265    of the type-specifier (TYPE) */
2266
2267 static void
2268 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2269                                          tree type, location_t location)
2270 {
2271   cp_token_position start = 0;
2272
2273   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2274     {
2275       if (TYPE_P (type))
2276         error_at (location, "%qT is not a template", type);
2277       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2278         error_at (location, "%qE is not a template", type);
2279       else
2280         error_at (location, "invalid template-id");
2281       /* Remember the location of the invalid "<".  */
2282       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2283         start = cp_lexer_token_position (parser->lexer, true);
2284       /* Consume the "<".  */
2285       cp_lexer_consume_token (parser->lexer);
2286       /* Parse the template arguments.  */
2287       cp_parser_enclosed_template_argument_list (parser);
2288       /* Permanently remove the invalid template arguments so that
2289          this error message is not issued again.  */
2290       if (start)
2291         cp_lexer_purge_tokens_after (parser->lexer, start);
2292     }
2293 }
2294
2295 /* If parsing an integral constant-expression, issue an error message
2296    about the fact that THING appeared and return true.  Otherwise,
2297    return false.  In either case, set
2298    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2299
2300 static bool
2301 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2302                                             const char *thing)
2303 {
2304   parser->non_integral_constant_expression_p = true;
2305   if (parser->integral_constant_expression_p)
2306     {
2307       if (!parser->allow_non_integral_constant_expression_p)
2308         {
2309           /* Don't use `%s' to print THING, because quotations (`%<', `%>')
2310              in the message need to be interpreted.  */
2311           char *message = concat (thing,
2312                                   " cannot appear in a constant-expression",
2313                                   NULL);
2314           error (message);
2315           free (message);
2316           return true;
2317         }
2318     }
2319   return false;
2320 }
2321
2322 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2323    qualifying scope (or NULL, if none) for ID.  This function commits
2324    to the current active tentative parse, if any.  (Otherwise, the
2325    problematic construct might be encountered again later, resulting
2326    in duplicate error messages.) LOCATION is the location of ID.  */
2327
2328 static void
2329 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2330                                       tree scope, tree id,
2331                                       location_t location)
2332 {
2333   tree decl, old_scope;
2334   /* Try to lookup the identifier.  */
2335   old_scope = parser->scope;
2336   parser->scope = scope;
2337   decl = cp_parser_lookup_name_simple (parser, id, location);
2338   parser->scope = old_scope;
2339   /* If the lookup found a template-name, it means that the user forgot
2340   to specify an argument list. Emit a useful error message.  */
2341   if (TREE_CODE (decl) == TEMPLATE_DECL)
2342     error_at (location,
2343               "invalid use of template-name %qE without an argument list",
2344               decl);
2345   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2346     error_at (location, "invalid use of destructor %qD as a type", id);
2347   else if (TREE_CODE (decl) == TYPE_DECL)
2348     /* Something like 'unsigned A a;'  */
2349     error_at (location, "invalid combination of multiple type-specifiers");
2350   else if (!parser->scope)
2351     {
2352       /* Issue an error message.  */
2353       error_at (location, "%qE does not name a type", id);
2354       /* If we're in a template class, it's possible that the user was
2355          referring to a type from a base class.  For example:
2356
2357            template <typename T> struct A { typedef T X; };
2358            template <typename T> struct B : public A<T> { X x; };
2359
2360          The user should have said "typename A<T>::X".  */
2361       if (processing_template_decl && current_class_type
2362           && TYPE_BINFO (current_class_type))
2363         {
2364           tree b;
2365
2366           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2367                b;
2368                b = TREE_CHAIN (b))
2369             {
2370               tree base_type = BINFO_TYPE (b);
2371               if (CLASS_TYPE_P (base_type)
2372                   && dependent_type_p (base_type))
2373                 {
2374                   tree field;
2375                   /* Go from a particular instantiation of the
2376                      template (which will have an empty TYPE_FIELDs),
2377                      to the main version.  */
2378                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2379                   for (field = TYPE_FIELDS (base_type);
2380                        field;
2381                        field = TREE_CHAIN (field))
2382                     if (TREE_CODE (field) == TYPE_DECL
2383                         && DECL_NAME (field) == id)
2384                       {
2385                         inform (location, 
2386                                 "(perhaps %<typename %T::%E%> was intended)",
2387                                 BINFO_TYPE (b), id);
2388                         break;
2389                       }
2390                   if (field)
2391                     break;
2392                 }
2393             }
2394         }
2395     }
2396   /* Here we diagnose qualified-ids where the scope is actually correct,
2397      but the identifier does not resolve to a valid type name.  */
2398   else if (parser->scope != error_mark_node)
2399     {
2400       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2401         error_at (location, "%qE in namespace %qE does not name a type",
2402                   id, parser->scope);
2403       else if (TYPE_P (parser->scope))
2404         error_at (location, "%qE in class %qT does not name a type",
2405                   id, parser->scope);
2406       else
2407         gcc_unreachable ();
2408     }
2409   cp_parser_commit_to_tentative_parse (parser);
2410 }
2411
2412 /* Check for a common situation where a type-name should be present,
2413    but is not, and issue a sensible error message.  Returns true if an
2414    invalid type-name was detected.
2415
2416    The situation handled by this function are variable declarations of the
2417    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2418    Usually, `ID' should name a type, but if we got here it means that it
2419    does not. We try to emit the best possible error message depending on
2420    how exactly the id-expression looks like.  */
2421
2422 static bool
2423 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2424 {
2425   tree id;
2426   cp_token *token = cp_lexer_peek_token (parser->lexer);
2427
2428   cp_parser_parse_tentatively (parser);
2429   id = cp_parser_id_expression (parser,
2430                                 /*template_keyword_p=*/false,
2431                                 /*check_dependency_p=*/true,
2432                                 /*template_p=*/NULL,
2433                                 /*declarator_p=*/true,
2434                                 /*optional_p=*/false);
2435   /* After the id-expression, there should be a plain identifier,
2436      otherwise this is not a simple variable declaration. Also, if
2437      the scope is dependent, we cannot do much.  */
2438   if (!cp_lexer_next_token_is (parser->lexer, CPP_NAME)
2439       || (parser->scope && TYPE_P (parser->scope)
2440           && dependent_type_p (parser->scope))
2441       || TREE_CODE (id) == TYPE_DECL)
2442     {
2443       cp_parser_abort_tentative_parse (parser);
2444       return false;
2445     }
2446   if (!cp_parser_parse_definitely (parser))
2447     return false;
2448
2449   /* Emit a diagnostic for the invalid type.  */
2450   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2451                                         id, token->location);
2452   /* Skip to the end of the declaration; there's no point in
2453      trying to process it.  */
2454   cp_parser_skip_to_end_of_block_or_statement (parser);
2455   return true;
2456 }
2457
2458 /* Consume tokens up to, and including, the next non-nested closing `)'.
2459    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2460    are doing error recovery. Returns -1 if OR_COMMA is true and we
2461    found an unnested comma.  */
2462
2463 static int
2464 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2465                                        bool recovering,
2466                                        bool or_comma,
2467                                        bool consume_paren)
2468 {
2469   unsigned paren_depth = 0;
2470   unsigned brace_depth = 0;
2471   unsigned square_depth = 0;
2472
2473   if (recovering && !or_comma
2474       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2475     return 0;
2476
2477   while (true)
2478     {
2479       cp_token * token = cp_lexer_peek_token (parser->lexer);
2480
2481       switch (token->type)
2482         {
2483         case CPP_EOF:
2484         case CPP_PRAGMA_EOL:
2485           /* If we've run out of tokens, then there is no closing `)'.  */
2486           return 0;
2487
2488         /* This is good for lambda expression capture-lists.  */
2489         case CPP_OPEN_SQUARE:
2490           ++square_depth;
2491           break;
2492         case CPP_CLOSE_SQUARE:
2493           if (!square_depth--)
2494             return 0;
2495           break;
2496
2497         case CPP_SEMICOLON:
2498           /* This matches the processing in skip_to_end_of_statement.  */
2499           if (!brace_depth)
2500             return 0;
2501           break;
2502
2503         case CPP_OPEN_BRACE:
2504           ++brace_depth;
2505           break;
2506         case CPP_CLOSE_BRACE:
2507           if (!brace_depth--)
2508             return 0;
2509           break;
2510
2511         case CPP_COMMA:
2512           if (recovering && or_comma && !brace_depth && !paren_depth
2513               && !square_depth)
2514             return -1;
2515           break;
2516
2517         case CPP_OPEN_PAREN:
2518           if (!brace_depth)
2519             ++paren_depth;
2520           break;
2521
2522         case CPP_CLOSE_PAREN:
2523           if (!brace_depth && !paren_depth--)
2524             {
2525               if (consume_paren)
2526                 cp_lexer_consume_token (parser->lexer);
2527               return 1;
2528             }
2529           break;
2530
2531         default:
2532           break;
2533         }
2534
2535       /* Consume the token.  */
2536       cp_lexer_consume_token (parser->lexer);
2537     }
2538 }
2539
2540 /* Consume tokens until we reach the end of the current statement.
2541    Normally, that will be just before consuming a `;'.  However, if a
2542    non-nested `}' comes first, then we stop before consuming that.  */
2543
2544 static void
2545 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2546 {
2547   unsigned nesting_depth = 0;
2548
2549   while (true)
2550     {
2551       cp_token *token = cp_lexer_peek_token (parser->lexer);
2552
2553       switch (token->type)
2554         {
2555         case CPP_EOF:
2556         case CPP_PRAGMA_EOL:
2557           /* If we've run out of tokens, stop.  */
2558           return;
2559
2560         case CPP_SEMICOLON:
2561           /* If the next token is a `;', we have reached the end of the
2562              statement.  */
2563           if (!nesting_depth)
2564             return;
2565           break;
2566
2567         case CPP_CLOSE_BRACE:
2568           /* If this is a non-nested '}', stop before consuming it.
2569              That way, when confronted with something like:
2570
2571                { 3 + }
2572
2573              we stop before consuming the closing '}', even though we
2574              have not yet reached a `;'.  */
2575           if (nesting_depth == 0)
2576             return;
2577
2578           /* If it is the closing '}' for a block that we have
2579              scanned, stop -- but only after consuming the token.
2580              That way given:
2581
2582                 void f g () { ... }
2583                 typedef int I;
2584
2585              we will stop after the body of the erroneously declared
2586              function, but before consuming the following `typedef'
2587              declaration.  */
2588           if (--nesting_depth == 0)
2589             {
2590               cp_lexer_consume_token (parser->lexer);
2591               return;
2592             }
2593
2594         case CPP_OPEN_BRACE:
2595           ++nesting_depth;
2596           break;
2597
2598         default:
2599           break;
2600         }
2601
2602       /* Consume the token.  */
2603       cp_lexer_consume_token (parser->lexer);
2604     }
2605 }
2606
2607 /* This function is called at the end of a statement or declaration.
2608    If the next token is a semicolon, it is consumed; otherwise, error
2609    recovery is attempted.  */
2610
2611 static void
2612 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2613 {
2614   /* Look for the trailing `;'.  */
2615   if (!cp_parser_require (parser, CPP_SEMICOLON, "%<;%>"))
2616     {
2617       /* If there is additional (erroneous) input, skip to the end of
2618          the statement.  */
2619       cp_parser_skip_to_end_of_statement (parser);
2620       /* If the next token is now a `;', consume it.  */
2621       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2622         cp_lexer_consume_token (parser->lexer);
2623     }
2624 }
2625
2626 /* Skip tokens until we have consumed an entire block, or until we
2627    have consumed a non-nested `;'.  */
2628
2629 static void
2630 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2631 {
2632   int nesting_depth = 0;
2633
2634   while (nesting_depth >= 0)
2635     {
2636       cp_token *token = cp_lexer_peek_token (parser->lexer);
2637
2638       switch (token->type)
2639         {
2640         case CPP_EOF:
2641         case CPP_PRAGMA_EOL:
2642           /* If we've run out of tokens, stop.  */
2643           return;
2644
2645         case CPP_SEMICOLON:
2646           /* Stop if this is an unnested ';'. */
2647           if (!nesting_depth)
2648             nesting_depth = -1;
2649           break;
2650
2651         case CPP_CLOSE_BRACE:
2652           /* Stop if this is an unnested '}', or closes the outermost
2653              nesting level.  */
2654           nesting_depth--;
2655           if (nesting_depth < 0)
2656             return;
2657           if (!nesting_depth)
2658             nesting_depth = -1;
2659           break;
2660
2661         case CPP_OPEN_BRACE:
2662           /* Nest. */
2663           nesting_depth++;
2664           break;
2665
2666         default:
2667           break;
2668         }
2669
2670       /* Consume the token.  */
2671       cp_lexer_consume_token (parser->lexer);
2672     }
2673 }
2674
2675 /* Skip tokens until a non-nested closing curly brace is the next
2676    token, or there are no more tokens. Return true in the first case,
2677    false otherwise.  */
2678
2679 static bool
2680 cp_parser_skip_to_closing_brace (cp_parser *parser)
2681 {
2682   unsigned nesting_depth = 0;
2683
2684   while (true)
2685     {
2686       cp_token *token = cp_lexer_peek_token (parser->lexer);
2687
2688       switch (token->type)
2689         {
2690         case CPP_EOF:
2691         case CPP_PRAGMA_EOL:
2692           /* If we've run out of tokens, stop.  */
2693           return false;
2694
2695         case CPP_CLOSE_BRACE:
2696           /* If the next token is a non-nested `}', then we have reached
2697              the end of the current block.  */
2698           if (nesting_depth-- == 0)
2699             return true;
2700           break;
2701
2702         case CPP_OPEN_BRACE:
2703           /* If it the next token is a `{', then we are entering a new
2704              block.  Consume the entire block.  */
2705           ++nesting_depth;
2706           break;
2707
2708         default:
2709           break;
2710         }
2711
2712       /* Consume the token.  */
2713       cp_lexer_consume_token (parser->lexer);
2714     }
2715 }
2716
2717 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2718    parameter is the PRAGMA token, allowing us to purge the entire pragma
2719    sequence.  */
2720
2721 static void
2722 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2723 {
2724   cp_token *token;
2725
2726   parser->lexer->in_pragma = false;
2727
2728   do
2729     token = cp_lexer_consume_token (parser->lexer);
2730   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2731
2732   /* Ensure that the pragma is not parsed again.  */
2733   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2734 }
2735
2736 /* Require pragma end of line, resyncing with it as necessary.  The
2737    arguments are as for cp_parser_skip_to_pragma_eol.  */
2738
2739 static void
2740 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2741 {
2742   parser->lexer->in_pragma = false;
2743   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, "end of line"))
2744     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2745 }
2746
2747 /* This is a simple wrapper around make_typename_type. When the id is
2748    an unresolved identifier node, we can provide a superior diagnostic
2749    using cp_parser_diagnose_invalid_type_name.  */
2750
2751 static tree
2752 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2753                               tree id, location_t id_location)
2754 {
2755   tree result;
2756   if (TREE_CODE (id) == IDENTIFIER_NODE)
2757     {
2758       result = make_typename_type (scope, id, typename_type,
2759                                    /*complain=*/tf_none);
2760       if (result == error_mark_node)
2761         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2762       return result;
2763     }
2764   return make_typename_type (scope, id, typename_type, tf_error);
2765 }
2766
2767 /* This is a wrapper around the
2768    make_{pointer,ptrmem,reference}_declarator functions that decides
2769    which one to call based on the CODE and CLASS_TYPE arguments. The
2770    CODE argument should be one of the values returned by
2771    cp_parser_ptr_operator. */
2772 static cp_declarator *
2773 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2774                                     cp_cv_quals cv_qualifiers,
2775                                     cp_declarator *target)
2776 {
2777   if (code == ERROR_MARK)
2778     return cp_error_declarator;
2779
2780   if (code == INDIRECT_REF)
2781     if (class_type == NULL_TREE)
2782       return make_pointer_declarator (cv_qualifiers, target);
2783     else
2784       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2785   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2786     return make_reference_declarator (cv_qualifiers, target, false);
2787   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2788     return make_reference_declarator (cv_qualifiers, target, true);
2789   gcc_unreachable ();
2790 }
2791
2792 /* Create a new C++ parser.  */
2793
2794 static cp_parser *
2795 cp_parser_new (void)
2796 {
2797   cp_parser *parser;
2798   cp_lexer *lexer;
2799   unsigned i;
2800
2801   /* cp_lexer_new_main is called before calling ggc_alloc because
2802      cp_lexer_new_main might load a PCH file.  */
2803   lexer = cp_lexer_new_main ();
2804
2805   /* Initialize the binops_by_token so that we can get the tree
2806      directly from the token.  */
2807   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2808     binops_by_token[binops[i].token_type] = binops[i];
2809
2810   parser = GGC_CNEW (cp_parser);
2811   parser->lexer = lexer;
2812   parser->context = cp_parser_context_new (NULL);
2813
2814   /* For now, we always accept GNU extensions.  */
2815   parser->allow_gnu_extensions_p = 1;
2816
2817   /* The `>' token is a greater-than operator, not the end of a
2818      template-id.  */
2819   parser->greater_than_is_operator_p = true;
2820
2821   parser->default_arg_ok_p = true;
2822
2823   /* We are not parsing a constant-expression.  */
2824   parser->integral_constant_expression_p = false;
2825   parser->allow_non_integral_constant_expression_p = false;
2826   parser->non_integral_constant_expression_p = false;
2827
2828   /* Local variable names are not forbidden.  */
2829   parser->local_variables_forbidden_p = false;
2830
2831   /* We are not processing an `extern "C"' declaration.  */
2832   parser->in_unbraced_linkage_specification_p = false;
2833
2834   /* We are not processing a declarator.  */
2835   parser->in_declarator_p = false;
2836
2837   /* We are not processing a template-argument-list.  */
2838   parser->in_template_argument_list_p = false;
2839
2840   /* We are not in an iteration statement.  */
2841   parser->in_statement = 0;
2842
2843   /* We are not in a switch statement.  */
2844   parser->in_switch_statement_p = false;
2845
2846   /* We are not parsing a type-id inside an expression.  */
2847   parser->in_type_id_in_expr_p = false;
2848
2849   /* Declarations aren't implicitly extern "C".  */
2850   parser->implicit_extern_c = false;
2851
2852   /* String literals should be translated to the execution character set.  */
2853   parser->translate_strings_p = true;
2854
2855   /* We are not parsing a function body.  */
2856   parser->in_function_body = false;
2857
2858   /* The unparsed function queue is empty.  */
2859   parser->unparsed_functions_queues = build_tree_list (NULL_TREE, NULL_TREE);
2860
2861   /* There are no classes being defined.  */
2862   parser->num_classes_being_defined = 0;
2863
2864   /* No template parameters apply.  */
2865   parser->num_template_parameter_lists = 0;
2866
2867   return parser;
2868 }
2869
2870 /* Create a cp_lexer structure which will emit the tokens in CACHE
2871    and push it onto the parser's lexer stack.  This is used for delayed
2872    parsing of in-class method bodies and default arguments, and should
2873    not be confused with tentative parsing.  */
2874 static void
2875 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
2876 {
2877   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
2878   lexer->next = parser->lexer;
2879   parser->lexer = lexer;
2880
2881   /* Move the current source position to that of the first token in the
2882      new lexer.  */
2883   cp_lexer_set_source_position_from_token (lexer->next_token);
2884 }
2885
2886 /* Pop the top lexer off the parser stack.  This is never used for the
2887    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
2888 static void
2889 cp_parser_pop_lexer (cp_parser *parser)
2890 {
2891   cp_lexer *lexer = parser->lexer;
2892   parser->lexer = lexer->next;
2893   cp_lexer_destroy (lexer);
2894
2895   /* Put the current source position back where it was before this
2896      lexer was pushed.  */
2897   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
2898 }
2899
2900 /* Lexical conventions [gram.lex]  */
2901
2902 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
2903    identifier.  */
2904
2905 static tree
2906 cp_parser_identifier (cp_parser* parser)
2907 {
2908   cp_token *token;
2909
2910   /* Look for the identifier.  */
2911   token = cp_parser_require (parser, CPP_NAME, "identifier");
2912   /* Return the value.  */
2913   return token ? token->u.value : error_mark_node;
2914 }
2915
2916 /* Parse a sequence of adjacent string constants.  Returns a
2917    TREE_STRING representing the combined, nul-terminated string
2918    constant.  If TRANSLATE is true, translate the string to the
2919    execution character set.  If WIDE_OK is true, a wide string is
2920    invalid here.
2921
2922    C++98 [lex.string] says that if a narrow string literal token is
2923    adjacent to a wide string literal token, the behavior is undefined.
2924    However, C99 6.4.5p4 says that this results in a wide string literal.
2925    We follow C99 here, for consistency with the C front end.
2926
2927    This code is largely lifted from lex_string() in c-lex.c.
2928
2929    FUTURE: ObjC++ will need to handle @-strings here.  */
2930 static tree
2931 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
2932 {
2933   tree value;
2934   size_t count;
2935   struct obstack str_ob;
2936   cpp_string str, istr, *strs;
2937   cp_token *tok;
2938   enum cpp_ttype type;
2939
2940   tok = cp_lexer_peek_token (parser->lexer);
2941   if (!cp_parser_is_string_literal (tok))
2942     {
2943       cp_parser_error (parser, "expected string-literal");
2944       return error_mark_node;
2945     }
2946
2947   type = tok->type;
2948
2949   /* Try to avoid the overhead of creating and destroying an obstack
2950      for the common case of just one string.  */
2951   if (!cp_parser_is_string_literal
2952       (cp_lexer_peek_nth_token (parser->lexer, 2)))
2953     {
2954       cp_lexer_consume_token (parser->lexer);
2955
2956       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2957       str.len = TREE_STRING_LENGTH (tok->u.value);
2958       count = 1;
2959
2960       strs = &str;
2961     }
2962   else
2963     {
2964       gcc_obstack_init (&str_ob);
2965       count = 0;
2966
2967       do
2968         {
2969           cp_lexer_consume_token (parser->lexer);
2970           count++;
2971           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
2972           str.len = TREE_STRING_LENGTH (tok->u.value);
2973
2974           if (type != tok->type)
2975             {
2976               if (type == CPP_STRING)
2977                 type = tok->type;
2978               else if (tok->type != CPP_STRING)
2979                 error_at (tok->location,
2980                           "unsupported non-standard concatenation "
2981                           "of string literals");
2982             }
2983
2984           obstack_grow (&str_ob, &str, sizeof (cpp_string));
2985
2986           tok = cp_lexer_peek_token (parser->lexer);
2987         }
2988       while (cp_parser_is_string_literal (tok));
2989
2990       strs = (cpp_string *) obstack_finish (&str_ob);
2991     }
2992
2993   if (type != CPP_STRING && !wide_ok)
2994     {
2995       cp_parser_error (parser, "a wide string is invalid in this context");
2996       type = CPP_STRING;
2997     }
2998
2999   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3000       (parse_in, strs, count, &istr, type))
3001     {
3002       value = build_string (istr.len, (const char *)istr.text);
3003       free (CONST_CAST (unsigned char *, istr.text));
3004
3005       switch (type)
3006         {
3007         default:
3008         case CPP_STRING:
3009         case CPP_UTF8STRING:
3010           TREE_TYPE (value) = char_array_type_node;
3011           break;
3012         case CPP_STRING16:
3013           TREE_TYPE (value) = char16_array_type_node;
3014           break;
3015         case CPP_STRING32:
3016           TREE_TYPE (value) = char32_array_type_node;
3017           break;
3018         case CPP_WSTRING:
3019           TREE_TYPE (value) = wchar_array_type_node;
3020           break;
3021         }
3022
3023       value = fix_string_type (value);
3024     }
3025   else
3026     /* cpp_interpret_string has issued an error.  */
3027     value = error_mark_node;
3028
3029   if (count > 1)
3030     obstack_free (&str_ob, 0);
3031
3032   return value;
3033 }
3034
3035
3036 /* Basic concepts [gram.basic]  */
3037
3038 /* Parse a translation-unit.
3039
3040    translation-unit:
3041      declaration-seq [opt]
3042
3043    Returns TRUE if all went well.  */
3044
3045 static bool
3046 cp_parser_translation_unit (cp_parser* parser)
3047 {
3048   /* The address of the first non-permanent object on the declarator
3049      obstack.  */
3050   static void *declarator_obstack_base;
3051
3052   bool success;
3053
3054   /* Create the declarator obstack, if necessary.  */
3055   if (!cp_error_declarator)
3056     {
3057       gcc_obstack_init (&declarator_obstack);
3058       /* Create the error declarator.  */
3059       cp_error_declarator = make_declarator (cdk_error);
3060       /* Create the empty parameter list.  */
3061       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3062       /* Remember where the base of the declarator obstack lies.  */
3063       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3064     }
3065
3066   cp_parser_declaration_seq_opt (parser);
3067
3068   /* If there are no tokens left then all went well.  */
3069   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3070     {
3071       /* Get rid of the token array; we don't need it any more.  */
3072       cp_lexer_destroy (parser->lexer);
3073       parser->lexer = NULL;
3074
3075       /* This file might have been a context that's implicitly extern
3076          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3077       if (parser->implicit_extern_c)
3078         {
3079           pop_lang_context ();
3080           parser->implicit_extern_c = false;
3081         }
3082
3083       /* Finish up.  */
3084       finish_translation_unit ();
3085
3086       success = true;
3087     }
3088   else
3089     {
3090       cp_parser_error (parser, "expected declaration");
3091       success = false;
3092     }
3093
3094   /* Make sure the declarator obstack was fully cleaned up.  */
3095   gcc_assert (obstack_next_free (&declarator_obstack)
3096               == declarator_obstack_base);
3097
3098   /* All went well.  */
3099   return success;
3100 }
3101
3102 /* Expressions [gram.expr] */
3103
3104 /* Parse a primary-expression.
3105
3106    primary-expression:
3107      literal
3108      this
3109      ( expression )
3110      id-expression
3111
3112    GNU Extensions:
3113
3114    primary-expression:
3115      ( compound-statement )
3116      __builtin_va_arg ( assignment-expression , type-id )
3117      __builtin_offsetof ( type-id , offsetof-expression )
3118
3119    C++ Extensions:
3120      __has_nothrow_assign ( type-id )   
3121      __has_nothrow_constructor ( type-id )
3122      __has_nothrow_copy ( type-id )
3123      __has_trivial_assign ( type-id )   
3124      __has_trivial_constructor ( type-id )
3125      __has_trivial_copy ( type-id )
3126      __has_trivial_destructor ( type-id )
3127      __has_virtual_destructor ( type-id )     
3128      __is_abstract ( type-id )
3129      __is_base_of ( type-id , type-id )
3130      __is_class ( type-id )
3131      __is_convertible_to ( type-id , type-id )     
3132      __is_empty ( type-id )
3133      __is_enum ( type-id )
3134      __is_pod ( type-id )
3135      __is_polymorphic ( type-id )
3136      __is_union ( type-id )
3137
3138    Objective-C++ Extension:
3139
3140    primary-expression:
3141      objc-expression
3142
3143    literal:
3144      __null
3145
3146    ADDRESS_P is true iff this expression was immediately preceded by
3147    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3148    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3149    true iff this expression is a template argument.
3150
3151    Returns a representation of the expression.  Upon return, *IDK
3152    indicates what kind of id-expression (if any) was present.  */
3153
3154 static tree
3155 cp_parser_primary_expression (cp_parser *parser,
3156                               bool address_p,
3157                               bool cast_p,
3158                               bool template_arg_p,
3159                               cp_id_kind *idk)
3160 {
3161   cp_token *token = NULL;
3162
3163   /* Assume the primary expression is not an id-expression.  */
3164   *idk = CP_ID_KIND_NONE;
3165
3166   /* Peek at the next token.  */
3167   token = cp_lexer_peek_token (parser->lexer);
3168   switch (token->type)
3169     {
3170       /* literal:
3171            integer-literal
3172            character-literal
3173            floating-literal
3174            string-literal
3175            boolean-literal  */
3176     case CPP_CHAR:
3177     case CPP_CHAR16:
3178     case CPP_CHAR32:
3179     case CPP_WCHAR:
3180     case CPP_NUMBER:
3181       token = cp_lexer_consume_token (parser->lexer);
3182       if (TREE_CODE (token->u.value) == FIXED_CST)
3183         {
3184           error_at (token->location,
3185                     "fixed-point types not supported in C++");
3186           return error_mark_node;
3187         }
3188       /* Floating-point literals are only allowed in an integral
3189          constant expression if they are cast to an integral or
3190          enumeration type.  */
3191       if (TREE_CODE (token->u.value) == REAL_CST
3192           && parser->integral_constant_expression_p
3193           && pedantic)
3194         {
3195           /* CAST_P will be set even in invalid code like "int(2.7 +
3196              ...)".   Therefore, we have to check that the next token
3197              is sure to end the cast.  */
3198           if (cast_p)
3199             {
3200               cp_token *next_token;
3201
3202               next_token = cp_lexer_peek_token (parser->lexer);
3203               if (/* The comma at the end of an
3204                      enumerator-definition.  */
3205                   next_token->type != CPP_COMMA
3206                   /* The curly brace at the end of an enum-specifier.  */
3207                   && next_token->type != CPP_CLOSE_BRACE
3208                   /* The end of a statement.  */
3209                   && next_token->type != CPP_SEMICOLON
3210                   /* The end of the cast-expression.  */
3211                   && next_token->type != CPP_CLOSE_PAREN
3212                   /* The end of an array bound.  */
3213                   && next_token->type != CPP_CLOSE_SQUARE
3214                   /* The closing ">" in a template-argument-list.  */
3215                   && (next_token->type != CPP_GREATER
3216                       || parser->greater_than_is_operator_p)
3217                   /* C++0x only: A ">>" treated like two ">" tokens,
3218                      in a template-argument-list.  */
3219                   && (next_token->type != CPP_RSHIFT
3220                       || (cxx_dialect == cxx98)
3221                       || parser->greater_than_is_operator_p))
3222                 cast_p = false;
3223             }
3224
3225           /* If we are within a cast, then the constraint that the
3226              cast is to an integral or enumeration type will be
3227              checked at that point.  If we are not within a cast, then
3228              this code is invalid.  */
3229           if (!cast_p)
3230             cp_parser_non_integral_constant_expression
3231               (parser, "floating-point literal");
3232         }
3233       return token->u.value;
3234
3235     case CPP_STRING:
3236     case CPP_STRING16:
3237     case CPP_STRING32:
3238     case CPP_WSTRING:
3239     case CPP_UTF8STRING:
3240       /* ??? Should wide strings be allowed when parser->translate_strings_p
3241          is false (i.e. in attributes)?  If not, we can kill the third
3242          argument to cp_parser_string_literal.  */
3243       return cp_parser_string_literal (parser,
3244                                        parser->translate_strings_p,
3245                                        true);
3246
3247     case CPP_OPEN_PAREN:
3248       {
3249         tree expr;
3250         bool saved_greater_than_is_operator_p;
3251
3252         /* Consume the `('.  */
3253         cp_lexer_consume_token (parser->lexer);
3254         /* Within a parenthesized expression, a `>' token is always
3255            the greater-than operator.  */
3256         saved_greater_than_is_operator_p
3257           = parser->greater_than_is_operator_p;
3258         parser->greater_than_is_operator_p = true;
3259         /* If we see `( { ' then we are looking at the beginning of
3260            a GNU statement-expression.  */
3261         if (cp_parser_allow_gnu_extensions_p (parser)
3262             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3263           {
3264             /* Statement-expressions are not allowed by the standard.  */
3265             pedwarn (token->location, OPT_pedantic, 
3266                      "ISO C++ forbids braced-groups within expressions");
3267
3268             /* And they're not allowed outside of a function-body; you
3269                cannot, for example, write:
3270
3271                  int i = ({ int j = 3; j + 1; });
3272
3273                at class or namespace scope.  */
3274             if (!parser->in_function_body
3275                 || parser->in_template_argument_list_p)
3276               {
3277                 error_at (token->location,
3278                           "statement-expressions are not allowed outside "
3279                           "functions nor in template-argument lists");
3280                 cp_parser_skip_to_end_of_block_or_statement (parser);
3281                 expr = error_mark_node;
3282               }
3283             else
3284               {
3285                 /* Start the statement-expression.  */
3286                 expr = begin_stmt_expr ();
3287                 /* Parse the compound-statement.  */
3288                 cp_parser_compound_statement (parser, expr, false);
3289                 /* Finish up.  */
3290                 expr = finish_stmt_expr (expr, false);
3291               }
3292           }
3293         else
3294           {
3295             /* Parse the parenthesized expression.  */
3296             expr = cp_parser_expression (parser, cast_p, idk);
3297             /* Let the front end know that this expression was
3298                enclosed in parentheses. This matters in case, for
3299                example, the expression is of the form `A::B', since
3300                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3301                not.  */
3302             finish_parenthesized_expr (expr);
3303           }
3304         /* The `>' token might be the end of a template-id or
3305            template-parameter-list now.  */
3306         parser->greater_than_is_operator_p
3307           = saved_greater_than_is_operator_p;
3308         /* Consume the `)'.  */
3309         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
3310           cp_parser_skip_to_end_of_statement (parser);
3311
3312         return expr;
3313       }
3314
3315     case CPP_OPEN_SQUARE:
3316       if (c_dialect_objc ())
3317         /* We have an Objective-C++ message. */
3318         return cp_parser_objc_expression (parser);
3319       maybe_warn_cpp0x ("lambda expressions");
3320       return cp_parser_lambda_expression (parser);
3321
3322     case CPP_OBJC_STRING:
3323       if (c_dialect_objc ())
3324         /* We have an Objective-C++ string literal. */
3325         return cp_parser_objc_expression (parser);
3326       cp_parser_error (parser, "expected primary-expression");
3327       return error_mark_node;
3328
3329     case CPP_KEYWORD:
3330       switch (token->keyword)
3331         {
3332           /* These two are the boolean literals.  */
3333         case RID_TRUE:
3334           cp_lexer_consume_token (parser->lexer);
3335           return boolean_true_node;
3336         case RID_FALSE:
3337           cp_lexer_consume_token (parser->lexer);
3338           return boolean_false_node;
3339
3340           /* The `__null' literal.  */
3341         case RID_NULL:
3342           cp_lexer_consume_token (parser->lexer);
3343           return null_node;
3344
3345           /* Recognize the `this' keyword.  */
3346         case RID_THIS:
3347           cp_lexer_consume_token (parser->lexer);
3348           if (parser->local_variables_forbidden_p)
3349             {
3350               error_at (token->location,
3351                         "%<this%> may not be used in this context");
3352               return error_mark_node;
3353             }
3354           /* Pointers cannot appear in constant-expressions.  */
3355           if (cp_parser_non_integral_constant_expression (parser, "%<this%>"))
3356             return error_mark_node;
3357           return finish_this_expr ();
3358
3359           /* The `operator' keyword can be the beginning of an
3360              id-expression.  */
3361         case RID_OPERATOR:
3362           goto id_expression;
3363
3364         case RID_FUNCTION_NAME:
3365         case RID_PRETTY_FUNCTION_NAME:
3366         case RID_C99_FUNCTION_NAME:
3367           {
3368             const char *name;
3369
3370             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3371                __func__ are the names of variables -- but they are
3372                treated specially.  Therefore, they are handled here,
3373                rather than relying on the generic id-expression logic
3374                below.  Grammatically, these names are id-expressions.
3375
3376                Consume the token.  */
3377             token = cp_lexer_consume_token (parser->lexer);
3378
3379             switch (token->keyword)
3380               {
3381               case RID_FUNCTION_NAME:
3382                 name = "%<__FUNCTION__%>";
3383                 break;
3384               case RID_PRETTY_FUNCTION_NAME:
3385                 name = "%<__PRETTY_FUNCTION__%>";
3386                 break;
3387               case RID_C99_FUNCTION_NAME:
3388                 name = "%<__func__%>";
3389                 break;
3390               default:
3391                 gcc_unreachable ();
3392               }
3393
3394             if (cp_parser_non_integral_constant_expression (parser, name))
3395               return error_mark_node;
3396
3397             /* Look up the name.  */
3398             return finish_fname (token->u.value);
3399           }
3400
3401         case RID_VA_ARG:
3402           {
3403             tree expression;
3404             tree type;
3405
3406             /* The `__builtin_va_arg' construct is used to handle
3407                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3408             cp_lexer_consume_token (parser->lexer);
3409             /* Look for the opening `('.  */
3410             cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
3411             /* Now, parse the assignment-expression.  */
3412             expression = cp_parser_assignment_expression (parser,
3413                                                           /*cast_p=*/false, NULL);
3414             /* Look for the `,'.  */
3415             cp_parser_require (parser, CPP_COMMA, "%<,%>");
3416             /* Parse the type-id.  */
3417             type = cp_parser_type_id (parser);
3418             /* Look for the closing `)'.  */
3419             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
3420             /* Using `va_arg' in a constant-expression is not
3421                allowed.  */
3422             if (cp_parser_non_integral_constant_expression (parser,
3423                                                             "%<va_arg%>"))
3424               return error_mark_node;
3425             return build_x_va_arg (expression, type);
3426           }
3427
3428         case RID_OFFSETOF:
3429           return cp_parser_builtin_offsetof (parser);
3430
3431         case RID_HAS_NOTHROW_ASSIGN:
3432         case RID_HAS_NOTHROW_CONSTRUCTOR:
3433         case RID_HAS_NOTHROW_COPY:        
3434         case RID_HAS_TRIVIAL_ASSIGN:
3435         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3436         case RID_HAS_TRIVIAL_COPY:        
3437         case RID_HAS_TRIVIAL_DESTRUCTOR:
3438         case RID_HAS_VIRTUAL_DESTRUCTOR:
3439         case RID_IS_ABSTRACT:
3440         case RID_IS_BASE_OF:
3441         case RID_IS_CLASS:
3442         case RID_IS_CONVERTIBLE_TO:
3443         case RID_IS_EMPTY:
3444         case RID_IS_ENUM:
3445         case RID_IS_POD:
3446         case RID_IS_POLYMORPHIC:
3447         case RID_IS_STD_LAYOUT:
3448         case RID_IS_TRIVIAL:
3449         case RID_IS_UNION:
3450           return cp_parser_trait_expr (parser, token->keyword);
3451
3452         /* Objective-C++ expressions.  */
3453         case RID_AT_ENCODE:
3454         case RID_AT_PROTOCOL:
3455         case RID_AT_SELECTOR:
3456           return cp_parser_objc_expression (parser);
3457
3458         default:
3459           cp_parser_error (parser, "expected primary-expression");
3460           return error_mark_node;
3461         }
3462
3463       /* An id-expression can start with either an identifier, a
3464          `::' as the beginning of a qualified-id, or the "operator"
3465          keyword.  */
3466     case CPP_NAME:
3467     case CPP_SCOPE:
3468     case CPP_TEMPLATE_ID:
3469     case CPP_NESTED_NAME_SPECIFIER:
3470       {
3471         tree id_expression;
3472         tree decl;
3473         const char *error_msg;
3474         bool template_p;
3475         bool done;
3476         cp_token *id_expr_token;
3477
3478       id_expression:
3479         /* Parse the id-expression.  */
3480         id_expression
3481           = cp_parser_id_expression (parser,
3482                                      /*template_keyword_p=*/false,
3483                                      /*check_dependency_p=*/true,
3484                                      &template_p,
3485                                      /*declarator_p=*/false,
3486                                      /*optional_p=*/false);
3487         if (id_expression == error_mark_node)
3488           return error_mark_node;
3489         id_expr_token = token;
3490         token = cp_lexer_peek_token (parser->lexer);
3491         done = (token->type != CPP_OPEN_SQUARE
3492                 && token->type != CPP_OPEN_PAREN
3493                 && token->type != CPP_DOT
3494                 && token->type != CPP_DEREF
3495                 && token->type != CPP_PLUS_PLUS
3496                 && token->type != CPP_MINUS_MINUS);
3497         /* If we have a template-id, then no further lookup is
3498            required.  If the template-id was for a template-class, we
3499            will sometimes have a TYPE_DECL at this point.  */
3500         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3501                  || TREE_CODE (id_expression) == TYPE_DECL)
3502           decl = id_expression;
3503         /* Look up the name.  */
3504         else
3505           {
3506             tree ambiguous_decls;
3507
3508             decl = cp_parser_lookup_name (parser, id_expression,
3509                                           none_type,
3510                                           template_p,
3511                                           /*is_namespace=*/false,
3512                                           /*check_dependency=*/true,
3513                                           &ambiguous_decls,
3514                                           id_expr_token->location);
3515             /* If the lookup was ambiguous, an error will already have
3516                been issued.  */
3517             if (ambiguous_decls)
3518               return error_mark_node;
3519
3520             /* In Objective-C++, an instance variable (ivar) may be preferred
3521                to whatever cp_parser_lookup_name() found.  */
3522             decl = objc_lookup_ivar (decl, id_expression);
3523
3524             /* If name lookup gives us a SCOPE_REF, then the
3525                qualifying scope was dependent.  */
3526             if (TREE_CODE (decl) == SCOPE_REF)
3527               {
3528                 /* At this point, we do not know if DECL is a valid
3529                    integral constant expression.  We assume that it is
3530                    in fact such an expression, so that code like:
3531
3532                       template <int N> struct A {
3533                         int a[B<N>::i];
3534                       };
3535                      
3536                    is accepted.  At template-instantiation time, we
3537                    will check that B<N>::i is actually a constant.  */
3538                 return decl;
3539               }
3540             /* Check to see if DECL is a local variable in a context
3541                where that is forbidden.  */
3542             if (parser->local_variables_forbidden_p
3543                 && local_variable_p (decl))
3544               {
3545                 /* It might be that we only found DECL because we are
3546                    trying to be generous with pre-ISO scoping rules.
3547                    For example, consider:
3548
3549                      int i;
3550                      void g() {
3551                        for (int i = 0; i < 10; ++i) {}
3552                        extern void f(int j = i);
3553                      }
3554
3555                    Here, name look up will originally find the out
3556                    of scope `i'.  We need to issue a warning message,
3557                    but then use the global `i'.  */
3558                 decl = check_for_out_of_scope_variable (decl);
3559                 if (local_variable_p (decl))
3560                   {
3561                     error_at (id_expr_token->location,
3562                               "local variable %qD may not appear in this context",
3563                               decl);
3564                     return error_mark_node;
3565                   }
3566               }
3567           }
3568
3569         decl = (finish_id_expression
3570                 (id_expression, decl, parser->scope,
3571                  idk,
3572                  parser->integral_constant_expression_p,
3573                  parser->allow_non_integral_constant_expression_p,
3574                  &parser->non_integral_constant_expression_p,
3575                  template_p, done, address_p,
3576                  template_arg_p,
3577                  &error_msg,
3578                  id_expr_token->location));
3579         if (error_msg)
3580           cp_parser_error (parser, error_msg);
3581         return decl;
3582       }
3583
3584       /* Anything else is an error.  */
3585     default:
3586       cp_parser_error (parser, "expected primary-expression");
3587       return error_mark_node;
3588     }
3589 }
3590
3591 /* Parse an id-expression.
3592
3593    id-expression:
3594      unqualified-id
3595      qualified-id
3596
3597    qualified-id:
3598      :: [opt] nested-name-specifier template [opt] unqualified-id
3599      :: identifier
3600      :: operator-function-id
3601      :: template-id
3602
3603    Return a representation of the unqualified portion of the
3604    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3605    a `::' or nested-name-specifier.
3606
3607    Often, if the id-expression was a qualified-id, the caller will
3608    want to make a SCOPE_REF to represent the qualified-id.  This
3609    function does not do this in order to avoid wastefully creating
3610    SCOPE_REFs when they are not required.
3611
3612    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3613    `template' keyword.
3614
3615    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3616    uninstantiated templates.
3617
3618    If *TEMPLATE_P is non-NULL, it is set to true iff the
3619    `template' keyword is used to explicitly indicate that the entity
3620    named is a template.
3621
3622    If DECLARATOR_P is true, the id-expression is appearing as part of
3623    a declarator, rather than as part of an expression.  */
3624
3625 static tree
3626 cp_parser_id_expression (cp_parser *parser,
3627                          bool template_keyword_p,
3628                          bool check_dependency_p,
3629                          bool *template_p,
3630                          bool declarator_p,
3631                          bool optional_p)
3632 {
3633   bool global_scope_p;
3634   bool nested_name_specifier_p;
3635
3636   /* Assume the `template' keyword was not used.  */
3637   if (template_p)
3638     *template_p = template_keyword_p;
3639
3640   /* Look for the optional `::' operator.  */
3641   global_scope_p
3642     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3643        != NULL_TREE);
3644   /* Look for the optional nested-name-specifier.  */
3645   nested_name_specifier_p
3646     = (cp_parser_nested_name_specifier_opt (parser,
3647                                             /*typename_keyword_p=*/false,
3648                                             check_dependency_p,
3649                                             /*type_p=*/false,
3650                                             declarator_p)
3651        != NULL_TREE);
3652   /* If there is a nested-name-specifier, then we are looking at
3653      the first qualified-id production.  */
3654   if (nested_name_specifier_p)
3655     {
3656       tree saved_scope;
3657       tree saved_object_scope;
3658       tree saved_qualifying_scope;
3659       tree unqualified_id;
3660       bool is_template;
3661
3662       /* See if the next token is the `template' keyword.  */
3663       if (!template_p)
3664         template_p = &is_template;
3665       *template_p = cp_parser_optional_template_keyword (parser);
3666       /* Name lookup we do during the processing of the
3667          unqualified-id might obliterate SCOPE.  */
3668       saved_scope = parser->scope;
3669       saved_object_scope = parser->object_scope;
3670       saved_qualifying_scope = parser->qualifying_scope;
3671       /* Process the final unqualified-id.  */
3672       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3673                                                  check_dependency_p,
3674                                                  declarator_p,
3675                                                  /*optional_p=*/false);
3676       /* Restore the SAVED_SCOPE for our caller.  */
3677       parser->scope = saved_scope;
3678       parser->object_scope = saved_object_scope;
3679       parser->qualifying_scope = saved_qualifying_scope;
3680
3681       return unqualified_id;
3682     }
3683   /* Otherwise, if we are in global scope, then we are looking at one
3684      of the other qualified-id productions.  */
3685   else if (global_scope_p)
3686     {
3687       cp_token *token;
3688       tree id;
3689
3690       /* Peek at the next token.  */
3691       token = cp_lexer_peek_token (parser->lexer);
3692
3693       /* If it's an identifier, and the next token is not a "<", then
3694          we can avoid the template-id case.  This is an optimization
3695          for this common case.  */
3696       if (token->type == CPP_NAME
3697           && !cp_parser_nth_token_starts_template_argument_list_p
3698                (parser, 2))
3699         return cp_parser_identifier (parser);
3700
3701       cp_parser_parse_tentatively (parser);
3702       /* Try a template-id.  */
3703       id = cp_parser_template_id (parser,
3704                                   /*template_keyword_p=*/false,
3705                                   /*check_dependency_p=*/true,
3706                                   declarator_p);
3707       /* If that worked, we're done.  */
3708       if (cp_parser_parse_definitely (parser))
3709         return id;
3710
3711       /* Peek at the next token.  (Changes in the token buffer may
3712          have invalidated the pointer obtained above.)  */
3713       token = cp_lexer_peek_token (parser->lexer);
3714
3715       switch (token->type)
3716         {
3717         case CPP_NAME:
3718           return cp_parser_identifier (parser);
3719
3720         case CPP_KEYWORD:
3721           if (token->keyword == RID_OPERATOR)
3722             return cp_parser_operator_function_id (parser);
3723           /* Fall through.  */
3724
3725         default:
3726           cp_parser_error (parser, "expected id-expression");
3727           return error_mark_node;
3728         }
3729     }
3730   else
3731     return cp_parser_unqualified_id (parser, template_keyword_p,
3732                                      /*check_dependency_p=*/true,
3733                                      declarator_p,
3734                                      optional_p);
3735 }
3736
3737 /* Parse an unqualified-id.
3738
3739    unqualified-id:
3740      identifier
3741      operator-function-id
3742      conversion-function-id
3743      ~ class-name
3744      template-id
3745
3746    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3747    keyword, in a construct like `A::template ...'.
3748
3749    Returns a representation of unqualified-id.  For the `identifier'
3750    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3751    production a BIT_NOT_EXPR is returned; the operand of the
3752    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3753    other productions, see the documentation accompanying the
3754    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3755    names are looked up in uninstantiated templates.  If DECLARATOR_P
3756    is true, the unqualified-id is appearing as part of a declarator,
3757    rather than as part of an expression.  */
3758
3759 static tree
3760 cp_parser_unqualified_id (cp_parser* parser,
3761                           bool template_keyword_p,
3762                           bool check_dependency_p,
3763                           bool declarator_p,
3764                           bool optional_p)
3765 {
3766   cp_token *token;
3767
3768   /* Peek at the next token.  */
3769   token = cp_lexer_peek_token (parser->lexer);
3770
3771   switch (token->type)
3772     {
3773     case CPP_NAME:
3774       {
3775         tree id;
3776
3777         /* We don't know yet whether or not this will be a
3778            template-id.  */
3779         cp_parser_parse_tentatively (parser);
3780         /* Try a template-id.  */
3781         id = cp_parser_template_id (parser, template_keyword_p,
3782                                     check_dependency_p,
3783                                     declarator_p);
3784         /* If it worked, we're done.  */
3785         if (cp_parser_parse_definitely (parser))
3786           return id;
3787         /* Otherwise, it's an ordinary identifier.  */
3788         return cp_parser_identifier (parser);
3789       }
3790
3791     case CPP_TEMPLATE_ID:
3792       return cp_parser_template_id (parser, template_keyword_p,
3793                                     check_dependency_p,
3794                                     declarator_p);
3795
3796     case CPP_COMPL:
3797       {
3798         tree type_decl;
3799         tree qualifying_scope;
3800         tree object_scope;
3801         tree scope;
3802         bool done;
3803
3804         /* Consume the `~' token.  */
3805         cp_lexer_consume_token (parser->lexer);
3806         /* Parse the class-name.  The standard, as written, seems to
3807            say that:
3808
3809              template <typename T> struct S { ~S (); };
3810              template <typename T> S<T>::~S() {}
3811
3812            is invalid, since `~' must be followed by a class-name, but
3813            `S<T>' is dependent, and so not known to be a class.
3814            That's not right; we need to look in uninstantiated
3815            templates.  A further complication arises from:
3816
3817              template <typename T> void f(T t) {
3818                t.T::~T();
3819              }
3820
3821            Here, it is not possible to look up `T' in the scope of `T'
3822            itself.  We must look in both the current scope, and the
3823            scope of the containing complete expression.
3824
3825            Yet another issue is:
3826
3827              struct S {
3828                int S;
3829                ~S();
3830              };
3831
3832              S::~S() {}
3833
3834            The standard does not seem to say that the `S' in `~S'
3835            should refer to the type `S' and not the data member
3836            `S::S'.  */
3837
3838         /* DR 244 says that we look up the name after the "~" in the
3839            same scope as we looked up the qualifying name.  That idea
3840            isn't fully worked out; it's more complicated than that.  */
3841         scope = parser->scope;
3842         object_scope = parser->object_scope;
3843         qualifying_scope = parser->qualifying_scope;
3844
3845         /* Check for invalid scopes.  */
3846         if (scope == error_mark_node)
3847           {
3848             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3849               cp_lexer_consume_token (parser->lexer);
3850             return error_mark_node;
3851           }
3852         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
3853           {
3854             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3855               error_at (token->location,
3856                         "scope %qT before %<~%> is not a class-name",
3857                         scope);
3858             cp_parser_simulate_error (parser);
3859             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
3860               cp_lexer_consume_token (parser->lexer);
3861             return error_mark_node;
3862           }
3863         gcc_assert (!scope || TYPE_P (scope));
3864
3865         /* If the name is of the form "X::~X" it's OK.  */
3866         token = cp_lexer_peek_token (parser->lexer);
3867         if (scope
3868             && token->type == CPP_NAME
3869             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3870                 == CPP_OPEN_PAREN)
3871             && constructor_name_p (token->u.value, scope))
3872           {
3873             cp_lexer_consume_token (parser->lexer);
3874             return build_nt (BIT_NOT_EXPR, scope);
3875           }
3876
3877         /* If there was an explicit qualification (S::~T), first look
3878            in the scope given by the qualification (i.e., S).  */
3879         done = false;
3880         type_decl = NULL_TREE;
3881         if (scope)
3882           {
3883             cp_parser_parse_tentatively (parser);
3884             type_decl = cp_parser_class_name (parser,
3885                                               /*typename_keyword_p=*/false,
3886                                               /*template_keyword_p=*/false,
3887                                               none_type,
3888                                               /*check_dependency=*/false,
3889                                               /*class_head_p=*/false,
3890                                               declarator_p);
3891             if (cp_parser_parse_definitely (parser))
3892               done = true;
3893           }
3894         /* In "N::S::~S", look in "N" as well.  */
3895         if (!done && scope && qualifying_scope)
3896           {
3897             cp_parser_parse_tentatively (parser);
3898             parser->scope = qualifying_scope;
3899             parser->object_scope = NULL_TREE;
3900             parser->qualifying_scope = NULL_TREE;
3901             type_decl
3902               = cp_parser_class_name (parser,
3903                                       /*typename_keyword_p=*/false,
3904                                       /*template_keyword_p=*/false,
3905                                       none_type,
3906                                       /*check_dependency=*/false,
3907                                       /*class_head_p=*/false,
3908                                       declarator_p);
3909             if (cp_parser_parse_definitely (parser))
3910               done = true;
3911           }
3912         /* In "p->S::~T", look in the scope given by "*p" as well.  */
3913         else if (!done && object_scope)
3914           {
3915             cp_parser_parse_tentatively (parser);
3916             parser->scope = object_scope;
3917             parser->object_scope = NULL_TREE;
3918             parser->qualifying_scope = NULL_TREE;
3919             type_decl
3920               = cp_parser_class_name (parser,
3921                                       /*typename_keyword_p=*/false,
3922                                       /*template_keyword_p=*/false,
3923                                       none_type,
3924                                       /*check_dependency=*/false,
3925                                       /*class_head_p=*/false,
3926                                       declarator_p);
3927             if (cp_parser_parse_definitely (parser))
3928               done = true;
3929           }
3930         /* Look in the surrounding context.  */
3931         if (!done)
3932           {
3933             parser->scope = NULL_TREE;
3934             parser->object_scope = NULL_TREE;
3935             parser->qualifying_scope = NULL_TREE;
3936             if (processing_template_decl)
3937               cp_parser_parse_tentatively (parser);
3938             type_decl
3939               = cp_parser_class_name (parser,
3940                                       /*typename_keyword_p=*/false,
3941                                       /*template_keyword_p=*/false,
3942                                       none_type,
3943                                       /*check_dependency=*/false,
3944                                       /*class_head_p=*/false,
3945                                       declarator_p);
3946             if (processing_template_decl
3947                 && ! cp_parser_parse_definitely (parser))
3948               {
3949                 /* We couldn't find a type with this name, so just accept
3950                    it and check for a match at instantiation time.  */
3951                 type_decl = cp_parser_identifier (parser);
3952                 if (type_decl != error_mark_node)
3953                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
3954                 return type_decl;
3955               }
3956           }
3957         /* If an error occurred, assume that the name of the
3958            destructor is the same as the name of the qualifying
3959            class.  That allows us to keep parsing after running
3960            into ill-formed destructor names.  */
3961         if (type_decl == error_mark_node && scope)
3962           return build_nt (BIT_NOT_EXPR, scope);
3963         else if (type_decl == error_mark_node)
3964           return error_mark_node;
3965
3966         /* Check that destructor name and scope match.  */
3967         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
3968           {
3969             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
3970               error_at (token->location,
3971                         "declaration of %<~%T%> as member of %qT",
3972                         type_decl, scope);
3973             cp_parser_simulate_error (parser);
3974             return error_mark_node;
3975           }
3976
3977         /* [class.dtor]
3978
3979            A typedef-name that names a class shall not be used as the
3980            identifier in the declarator for a destructor declaration.  */
3981         if (declarator_p
3982             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
3983             && !DECL_SELF_REFERENCE_P (type_decl)
3984             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
3985           error_at (token->location,
3986                     "typedef-name %qD used as destructor declarator",
3987                     type_decl);
3988
3989         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
3990       }
3991
3992     case CPP_KEYWORD:
3993       if (token->keyword == RID_OPERATOR)
3994         {
3995           tree id;
3996
3997           /* This could be a template-id, so we try that first.  */
3998           cp_parser_parse_tentatively (parser);
3999           /* Try a template-id.  */
4000           id = cp_parser_template_id (parser, template_keyword_p,
4001                                       /*check_dependency_p=*/true,
4002                                       declarator_p);
4003           /* If that worked, we're done.  */
4004           if (cp_parser_parse_definitely (parser))
4005             return id;
4006           /* We still don't know whether we're looking at an
4007              operator-function-id or a conversion-function-id.  */
4008           cp_parser_parse_tentatively (parser);
4009           /* Try an operator-function-id.  */
4010           id = cp_parser_operator_function_id (parser);
4011           /* If that didn't work, try a conversion-function-id.  */
4012           if (!cp_parser_parse_definitely (parser))
4013             id = cp_parser_conversion_function_id (parser);
4014
4015           return id;
4016         }
4017       /* Fall through.  */
4018
4019     default:
4020       if (optional_p)
4021         return NULL_TREE;
4022       cp_parser_error (parser, "expected unqualified-id");
4023       return error_mark_node;
4024     }
4025 }
4026
4027 /* Parse an (optional) nested-name-specifier.
4028
4029    nested-name-specifier: [C++98]
4030      class-or-namespace-name :: nested-name-specifier [opt]
4031      class-or-namespace-name :: template nested-name-specifier [opt]
4032
4033    nested-name-specifier: [C++0x]
4034      type-name ::
4035      namespace-name ::
4036      nested-name-specifier identifier ::
4037      nested-name-specifier template [opt] simple-template-id ::
4038
4039    PARSER->SCOPE should be set appropriately before this function is
4040    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4041    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4042    in name lookups.
4043
4044    Sets PARSER->SCOPE to the class (TYPE) or namespace
4045    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4046    it unchanged if there is no nested-name-specifier.  Returns the new
4047    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4048
4049    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4050    part of a declaration and/or decl-specifier.  */
4051
4052 static tree
4053 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4054                                      bool typename_keyword_p,
4055                                      bool check_dependency_p,
4056                                      bool type_p,
4057                                      bool is_declaration)
4058 {
4059   bool success = false;
4060   cp_token_position start = 0;
4061   cp_token *token;
4062
4063   /* Remember where the nested-name-specifier starts.  */
4064   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4065     {
4066       start = cp_lexer_token_position (parser->lexer, false);
4067       push_deferring_access_checks (dk_deferred);
4068     }
4069
4070   while (true)
4071     {
4072       tree new_scope;
4073       tree old_scope;
4074       tree saved_qualifying_scope;
4075       bool template_keyword_p;
4076
4077       /* Spot cases that cannot be the beginning of a
4078          nested-name-specifier.  */
4079       token = cp_lexer_peek_token (parser->lexer);
4080
4081       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4082          the already parsed nested-name-specifier.  */
4083       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4084         {
4085           /* Grab the nested-name-specifier and continue the loop.  */
4086           cp_parser_pre_parsed_nested_name_specifier (parser);
4087           /* If we originally encountered this nested-name-specifier
4088              with IS_DECLARATION set to false, we will not have
4089              resolved TYPENAME_TYPEs, so we must do so here.  */
4090           if (is_declaration
4091               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4092             {
4093               new_scope = resolve_typename_type (parser->scope,
4094                                                  /*only_current_p=*/false);
4095               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4096                 parser->scope = new_scope;
4097             }
4098           success = true;
4099           continue;
4100         }
4101
4102       /* Spot cases that cannot be the beginning of a
4103          nested-name-specifier.  On the second and subsequent times
4104          through the loop, we look for the `template' keyword.  */
4105       if (success && token->keyword == RID_TEMPLATE)
4106         ;
4107       /* A template-id can start a nested-name-specifier.  */
4108       else if (token->type == CPP_TEMPLATE_ID)
4109         ;
4110       else
4111         {
4112           /* If the next token is not an identifier, then it is
4113              definitely not a type-name or namespace-name.  */
4114           if (token->type != CPP_NAME)
4115             break;
4116           /* If the following token is neither a `<' (to begin a
4117              template-id), nor a `::', then we are not looking at a
4118              nested-name-specifier.  */
4119           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4120           if (token->type != CPP_SCOPE
4121               && !cp_parser_nth_token_starts_template_argument_list_p
4122                   (parser, 2))
4123             break;
4124         }
4125
4126       /* The nested-name-specifier is optional, so we parse
4127          tentatively.  */
4128       cp_parser_parse_tentatively (parser);
4129
4130       /* Look for the optional `template' keyword, if this isn't the
4131          first time through the loop.  */
4132       if (success)
4133         template_keyword_p = cp_parser_optional_template_keyword (parser);
4134       else
4135         template_keyword_p = false;
4136
4137       /* Save the old scope since the name lookup we are about to do
4138          might destroy it.  */
4139       old_scope = parser->scope;
4140       saved_qualifying_scope = parser->qualifying_scope;
4141       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4142          look up names in "X<T>::I" in order to determine that "Y" is
4143          a template.  So, if we have a typename at this point, we make
4144          an effort to look through it.  */
4145       if (is_declaration
4146           && !typename_keyword_p
4147           && parser->scope
4148           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4149         parser->scope = resolve_typename_type (parser->scope,
4150                                                /*only_current_p=*/false);
4151       /* Parse the qualifying entity.  */
4152       new_scope
4153         = cp_parser_qualifying_entity (parser,
4154                                        typename_keyword_p,
4155                                        template_keyword_p,
4156                                        check_dependency_p,
4157                                        type_p,
4158                                        is_declaration);
4159       /* Look for the `::' token.  */
4160       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
4161
4162       /* If we found what we wanted, we keep going; otherwise, we're
4163          done.  */
4164       if (!cp_parser_parse_definitely (parser))
4165         {
4166           bool error_p = false;
4167
4168           /* Restore the OLD_SCOPE since it was valid before the
4169              failed attempt at finding the last
4170              class-or-namespace-name.  */
4171           parser->scope = old_scope;
4172           parser->qualifying_scope = saved_qualifying_scope;
4173           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4174             break;
4175           /* If the next token is an identifier, and the one after
4176              that is a `::', then any valid interpretation would have
4177              found a class-or-namespace-name.  */
4178           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4179                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4180                      == CPP_SCOPE)
4181                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4182                      != CPP_COMPL))
4183             {
4184               token = cp_lexer_consume_token (parser->lexer);
4185               if (!error_p)
4186                 {
4187                   if (!token->ambiguous_p)
4188                     {
4189                       tree decl;
4190                       tree ambiguous_decls;
4191
4192                       decl = cp_parser_lookup_name (parser, token->u.value,
4193                                                     none_type,
4194                                                     /*is_template=*/false,
4195                                                     /*is_namespace=*/false,
4196                                                     /*check_dependency=*/true,
4197                                                     &ambiguous_decls,
4198                                                     token->location);
4199                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4200                         error_at (token->location,
4201                                   "%qD used without template parameters",
4202                                   decl);
4203                       else if (ambiguous_decls)
4204                         {
4205                           error_at (token->location,
4206                                     "reference to %qD is ambiguous",
4207                                     token->u.value);
4208                           print_candidates (ambiguous_decls);
4209                           decl = error_mark_node;
4210                         }
4211                       else
4212                         {
4213                           const char* msg = "is not a class or namespace";
4214                           if (cxx_dialect != cxx98)
4215                             msg = "is not a class, namespace, or enumeration";
4216                           cp_parser_name_lookup_error
4217                             (parser, token->u.value, decl, msg,
4218                              token->location);
4219                         }
4220                     }
4221                   parser->scope = error_mark_node;
4222                   error_p = true;
4223                   /* Treat this as a successful nested-name-specifier
4224                      due to:
4225
4226                      [basic.lookup.qual]
4227
4228                      If the name found is not a class-name (clause
4229                      _class_) or namespace-name (_namespace.def_), the
4230                      program is ill-formed.  */
4231                   success = true;
4232                 }
4233               cp_lexer_consume_token (parser->lexer);
4234             }
4235           break;
4236         }
4237       /* We've found one valid nested-name-specifier.  */
4238       success = true;
4239       /* Name lookup always gives us a DECL.  */
4240       if (TREE_CODE (new_scope) == TYPE_DECL)
4241         new_scope = TREE_TYPE (new_scope);
4242       /* Uses of "template" must be followed by actual templates.  */
4243       if (template_keyword_p
4244           && !(CLASS_TYPE_P (new_scope)
4245                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4246                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4247                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4248           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4249                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4250                    == TEMPLATE_ID_EXPR)))
4251         permerror (input_location, TYPE_P (new_scope)
4252                    ? "%qT is not a template"
4253                    : "%qD is not a template",
4254                    new_scope);
4255       /* If it is a class scope, try to complete it; we are about to
4256          be looking up names inside the class.  */
4257       if (TYPE_P (new_scope)
4258           /* Since checking types for dependency can be expensive,
4259              avoid doing it if the type is already complete.  */
4260           && !COMPLETE_TYPE_P (new_scope)
4261           /* Do not try to complete dependent types.  */
4262           && !dependent_type_p (new_scope))
4263         {
4264           new_scope = complete_type (new_scope);
4265           /* If it is a typedef to current class, use the current
4266              class instead, as the typedef won't have any names inside
4267              it yet.  */
4268           if (!COMPLETE_TYPE_P (new_scope)
4269               && currently_open_class (new_scope))
4270             new_scope = TYPE_MAIN_VARIANT (new_scope);
4271         }
4272       /* Make sure we look in the right scope the next time through
4273          the loop.  */
4274       parser->scope = new_scope;
4275     }
4276
4277   /* If parsing tentatively, replace the sequence of tokens that makes
4278      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4279      token.  That way, should we re-parse the token stream, we will
4280      not have to repeat the effort required to do the parse, nor will
4281      we issue duplicate error messages.  */
4282   if (success && start)
4283     {
4284       cp_token *token;
4285
4286       token = cp_lexer_token_at (parser->lexer, start);
4287       /* Reset the contents of the START token.  */
4288       token->type = CPP_NESTED_NAME_SPECIFIER;
4289       /* Retrieve any deferred checks.  Do not pop this access checks yet
4290          so the memory will not be reclaimed during token replacing below.  */
4291       token->u.tree_check_value = GGC_CNEW (struct tree_check);
4292       token->u.tree_check_value->value = parser->scope;
4293       token->u.tree_check_value->checks = get_deferred_access_checks ();
4294       token->u.tree_check_value->qualifying_scope =
4295         parser->qualifying_scope;
4296       token->keyword = RID_MAX;
4297
4298       /* Purge all subsequent tokens.  */
4299       cp_lexer_purge_tokens_after (parser->lexer, start);
4300     }
4301
4302   if (start)
4303     pop_to_parent_deferring_access_checks ();
4304
4305   return success ? parser->scope : NULL_TREE;
4306 }
4307
4308 /* Parse a nested-name-specifier.  See
4309    cp_parser_nested_name_specifier_opt for details.  This function
4310    behaves identically, except that it will an issue an error if no
4311    nested-name-specifier is present.  */
4312
4313 static tree
4314 cp_parser_nested_name_specifier (cp_parser *parser,
4315                                  bool typename_keyword_p,
4316                                  bool check_dependency_p,
4317                                  bool type_p,
4318                                  bool is_declaration)
4319 {
4320   tree scope;
4321
4322   /* Look for the nested-name-specifier.  */
4323   scope = cp_parser_nested_name_specifier_opt (parser,
4324                                                typename_keyword_p,
4325                                                check_dependency_p,
4326                                                type_p,
4327                                                is_declaration);
4328   /* If it was not present, issue an error message.  */
4329   if (!scope)
4330     {
4331       cp_parser_error (parser, "expected nested-name-specifier");
4332       parser->scope = NULL_TREE;
4333     }
4334
4335   return scope;
4336 }
4337
4338 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4339    this is either a class-name or a namespace-name (which corresponds
4340    to the class-or-namespace-name production in the grammar). For
4341    C++0x, it can also be a type-name that refers to an enumeration
4342    type.
4343
4344    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4345    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4346    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4347    TYPE_P is TRUE iff the next name should be taken as a class-name,
4348    even the same name is declared to be another entity in the same
4349    scope.
4350
4351    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4352    specified by the class-or-namespace-name.  If neither is found the
4353    ERROR_MARK_NODE is returned.  */
4354
4355 static tree
4356 cp_parser_qualifying_entity (cp_parser *parser,
4357                              bool typename_keyword_p,
4358                              bool template_keyword_p,
4359                              bool check_dependency_p,
4360                              bool type_p,
4361                              bool is_declaration)
4362 {
4363   tree saved_scope;
4364   tree saved_qualifying_scope;
4365   tree saved_object_scope;
4366   tree scope;
4367   bool only_class_p;
4368   bool successful_parse_p;
4369
4370   /* Before we try to parse the class-name, we must save away the
4371      current PARSER->SCOPE since cp_parser_class_name will destroy
4372      it.  */
4373   saved_scope = parser->scope;
4374   saved_qualifying_scope = parser->qualifying_scope;
4375   saved_object_scope = parser->object_scope;
4376   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4377      there is no need to look for a namespace-name.  */
4378   only_class_p = template_keyword_p 
4379     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4380   if (!only_class_p)
4381     cp_parser_parse_tentatively (parser);
4382   scope = cp_parser_class_name (parser,
4383                                 typename_keyword_p,
4384                                 template_keyword_p,
4385                                 type_p ? class_type : none_type,
4386                                 check_dependency_p,
4387                                 /*class_head_p=*/false,
4388                                 is_declaration);
4389   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4390   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4391   if (!only_class_p 
4392       && cxx_dialect != cxx98
4393       && !successful_parse_p)
4394     {
4395       /* Restore the saved scope.  */
4396       parser->scope = saved_scope;
4397       parser->qualifying_scope = saved_qualifying_scope;
4398       parser->object_scope = saved_object_scope;
4399
4400       /* Parse tentatively.  */
4401       cp_parser_parse_tentatively (parser);
4402      
4403       /* Parse a typedef-name or enum-name.  */
4404       scope = cp_parser_nonclass_name (parser);
4405       successful_parse_p = cp_parser_parse_definitely (parser);
4406     }
4407   /* If that didn't work, try for a namespace-name.  */
4408   if (!only_class_p && !successful_parse_p)
4409     {
4410       /* Restore the saved scope.  */
4411       parser->scope = saved_scope;
4412       parser->qualifying_scope = saved_qualifying_scope;
4413       parser->object_scope = saved_object_scope;
4414       /* If we are not looking at an identifier followed by the scope
4415          resolution operator, then this is not part of a
4416          nested-name-specifier.  (Note that this function is only used
4417          to parse the components of a nested-name-specifier.)  */
4418       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4419           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4420         return error_mark_node;
4421       scope = cp_parser_namespace_name (parser);
4422     }
4423
4424   return scope;
4425 }
4426
4427 /* Parse a postfix-expression.
4428
4429    postfix-expression:
4430      primary-expression
4431      postfix-expression [ expression ]
4432      postfix-expression ( expression-list [opt] )
4433      simple-type-specifier ( expression-list [opt] )
4434      typename :: [opt] nested-name-specifier identifier
4435        ( expression-list [opt] )
4436      typename :: [opt] nested-name-specifier template [opt] template-id
4437        ( expression-list [opt] )
4438      postfix-expression . template [opt] id-expression
4439      postfix-expression -> template [opt] id-expression
4440      postfix-expression . pseudo-destructor-name
4441      postfix-expression -> pseudo-destructor-name
4442      postfix-expression ++
4443      postfix-expression --
4444      dynamic_cast < type-id > ( expression )
4445      static_cast < type-id > ( expression )
4446      reinterpret_cast < type-id > ( expression )
4447      const_cast < type-id > ( expression )
4448      typeid ( expression )
4449      typeid ( type-id )
4450
4451    GNU Extension:
4452
4453    postfix-expression:
4454      ( type-id ) { initializer-list , [opt] }
4455
4456    This extension is a GNU version of the C99 compound-literal
4457    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4458    but they are essentially the same concept.)
4459
4460    If ADDRESS_P is true, the postfix expression is the operand of the
4461    `&' operator.  CAST_P is true if this expression is the target of a
4462    cast.
4463
4464    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4465    class member access expressions [expr.ref].
4466
4467    Returns a representation of the expression.  */
4468
4469 static tree
4470 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4471                               bool member_access_only_p,
4472                               cp_id_kind * pidk_return)
4473 {
4474   cp_token *token;
4475   enum rid keyword;
4476   cp_id_kind idk = CP_ID_KIND_NONE;
4477   tree postfix_expression = NULL_TREE;
4478   bool is_member_access = false;
4479
4480   /* Peek at the next token.  */
4481   token = cp_lexer_peek_token (parser->lexer);
4482   /* Some of the productions are determined by keywords.  */
4483   keyword = token->keyword;
4484   switch (keyword)
4485     {
4486     case RID_DYNCAST:
4487     case RID_STATCAST:
4488     case RID_REINTCAST:
4489     case RID_CONSTCAST:
4490       {
4491         tree type;
4492         tree expression;
4493         const char *saved_message;
4494
4495         /* All of these can be handled in the same way from the point
4496            of view of parsing.  Begin by consuming the token
4497            identifying the cast.  */
4498         cp_lexer_consume_token (parser->lexer);
4499
4500         /* New types cannot be defined in the cast.  */
4501         saved_message = parser->type_definition_forbidden_message;
4502         parser->type_definition_forbidden_message
4503           = "types may not be defined in casts";
4504
4505         /* Look for the opening `<'.  */
4506         cp_parser_require (parser, CPP_LESS, "%<<%>");
4507         /* Parse the type to which we are casting.  */
4508         type = cp_parser_type_id (parser);
4509         /* Look for the closing `>'.  */
4510         cp_parser_require (parser, CPP_GREATER, "%<>%>");
4511         /* Restore the old message.  */
4512         parser->type_definition_forbidden_message = saved_message;
4513
4514         /* And the expression which is being cast.  */
4515         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4516         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4517         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4518
4519         /* Only type conversions to integral or enumeration types
4520            can be used in constant-expressions.  */
4521         if (!cast_valid_in_integral_constant_expression_p (type)
4522             && (cp_parser_non_integral_constant_expression
4523                 (parser,
4524                  "a cast to a type other than an integral or "
4525                  "enumeration type")))
4526           return error_mark_node;
4527
4528         switch (keyword)
4529           {
4530           case RID_DYNCAST:
4531             postfix_expression
4532               = build_dynamic_cast (type, expression, tf_warning_or_error);
4533             break;
4534           case RID_STATCAST:
4535             postfix_expression
4536               = build_static_cast (type, expression, tf_warning_or_error);
4537             break;
4538           case RID_REINTCAST:
4539             postfix_expression
4540               = build_reinterpret_cast (type, expression, 
4541                                         tf_warning_or_error);
4542             break;
4543           case RID_CONSTCAST:
4544             postfix_expression
4545               = build_const_cast (type, expression, tf_warning_or_error);
4546             break;
4547           default:
4548             gcc_unreachable ();
4549           }
4550       }
4551       break;
4552
4553     case RID_TYPEID:
4554       {
4555         tree type;
4556         const char *saved_message;
4557         bool saved_in_type_id_in_expr_p;
4558
4559         /* Consume the `typeid' token.  */
4560         cp_lexer_consume_token (parser->lexer);
4561         /* Look for the `(' token.  */
4562         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
4563         /* Types cannot be defined in a `typeid' expression.  */
4564         saved_message = parser->type_definition_forbidden_message;
4565         parser->type_definition_forbidden_message
4566           = "types may not be defined in a %<typeid%> expression";
4567         /* We can't be sure yet whether we're looking at a type-id or an
4568            expression.  */
4569         cp_parser_parse_tentatively (parser);
4570         /* Try a type-id first.  */
4571         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4572         parser->in_type_id_in_expr_p = true;
4573         type = cp_parser_type_id (parser);
4574         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4575         /* Look for the `)' token.  Otherwise, we can't be sure that
4576            we're not looking at an expression: consider `typeid (int
4577            (3))', for example.  */
4578         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4579         /* If all went well, simply lookup the type-id.  */
4580         if (cp_parser_parse_definitely (parser))
4581           postfix_expression = get_typeid (type);
4582         /* Otherwise, fall back to the expression variant.  */
4583         else
4584           {
4585             tree expression;
4586
4587             /* Look for an expression.  */
4588             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4589             /* Compute its typeid.  */
4590             postfix_expression = build_typeid (expression);
4591             /* Look for the `)' token.  */
4592             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4593           }
4594         /* Restore the saved message.  */
4595         parser->type_definition_forbidden_message = saved_message;
4596         /* `typeid' may not appear in an integral constant expression.  */
4597         if (cp_parser_non_integral_constant_expression(parser,
4598                                                        "%<typeid%> operator"))
4599           return error_mark_node;
4600       }
4601       break;
4602
4603     case RID_TYPENAME:
4604       {
4605         tree type;
4606         /* The syntax permitted here is the same permitted for an
4607            elaborated-type-specifier.  */
4608         type = cp_parser_elaborated_type_specifier (parser,
4609                                                     /*is_friend=*/false,
4610                                                     /*is_declaration=*/false);
4611         postfix_expression = cp_parser_functional_cast (parser, type);
4612       }
4613       break;
4614
4615     default:
4616       {
4617         tree type;
4618
4619         /* If the next thing is a simple-type-specifier, we may be
4620            looking at a functional cast.  We could also be looking at
4621            an id-expression.  So, we try the functional cast, and if
4622            that doesn't work we fall back to the primary-expression.  */
4623         cp_parser_parse_tentatively (parser);
4624         /* Look for the simple-type-specifier.  */
4625         type = cp_parser_simple_type_specifier (parser,
4626                                                 /*decl_specs=*/NULL,
4627                                                 CP_PARSER_FLAGS_NONE);
4628         /* Parse the cast itself.  */
4629         if (!cp_parser_error_occurred (parser))
4630           postfix_expression
4631             = cp_parser_functional_cast (parser, type);
4632         /* If that worked, we're done.  */
4633         if (cp_parser_parse_definitely (parser))
4634           break;
4635
4636         /* If the functional-cast didn't work out, try a
4637            compound-literal.  */
4638         if (cp_parser_allow_gnu_extensions_p (parser)
4639             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4640           {
4641             VEC(constructor_elt,gc) *initializer_list = NULL;
4642             bool saved_in_type_id_in_expr_p;
4643
4644             cp_parser_parse_tentatively (parser);
4645             /* Consume the `('.  */
4646             cp_lexer_consume_token (parser->lexer);
4647             /* Parse the type.  */
4648             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4649             parser->in_type_id_in_expr_p = true;
4650             type = cp_parser_type_id (parser);
4651             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4652             /* Look for the `)'.  */
4653             cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
4654             /* Look for the `{'.  */
4655             cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
4656             /* If things aren't going well, there's no need to
4657                keep going.  */
4658             if (!cp_parser_error_occurred (parser))
4659               {
4660                 bool non_constant_p;
4661                 /* Parse the initializer-list.  */
4662                 initializer_list
4663                   = cp_parser_initializer_list (parser, &non_constant_p);
4664                 /* Allow a trailing `,'.  */
4665                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4666                   cp_lexer_consume_token (parser->lexer);
4667                 /* Look for the final `}'.  */
4668                 cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
4669               }
4670             /* If that worked, we're definitely looking at a
4671                compound-literal expression.  */
4672             if (cp_parser_parse_definitely (parser))
4673               {
4674                 /* Warn the user that a compound literal is not
4675                    allowed in standard C++.  */
4676                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4677                 /* For simplicity, we disallow compound literals in
4678                    constant-expressions.  We could
4679                    allow compound literals of integer type, whose
4680                    initializer was a constant, in constant
4681                    expressions.  Permitting that usage, as a further
4682                    extension, would not change the meaning of any
4683                    currently accepted programs.  (Of course, as
4684                    compound literals are not part of ISO C++, the
4685                    standard has nothing to say.)  */
4686                 if (cp_parser_non_integral_constant_expression 
4687                     (parser, "non-constant compound literals"))
4688                   {
4689                     postfix_expression = error_mark_node;
4690                     break;
4691                   }
4692                 /* Form the representation of the compound-literal.  */
4693                 postfix_expression
4694                   = (finish_compound_literal
4695                      (type, build_constructor (init_list_type_node,
4696                                                initializer_list)));
4697                 break;
4698               }
4699           }
4700
4701         /* It must be a primary-expression.  */
4702         postfix_expression
4703           = cp_parser_primary_expression (parser, address_p, cast_p,
4704                                           /*template_arg_p=*/false,
4705                                           &idk);
4706       }
4707       break;
4708     }
4709
4710   /* Keep looping until the postfix-expression is complete.  */
4711   while (true)
4712     {
4713       if (idk == CP_ID_KIND_UNQUALIFIED
4714           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4715           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4716         /* It is not a Koenig lookup function call.  */
4717         postfix_expression
4718           = unqualified_name_lookup_error (postfix_expression);
4719
4720       /* Peek at the next token.  */
4721       token = cp_lexer_peek_token (parser->lexer);
4722
4723       switch (token->type)
4724         {
4725         case CPP_OPEN_SQUARE:
4726           postfix_expression
4727             = cp_parser_postfix_open_square_expression (parser,
4728                                                         postfix_expression,
4729                                                         false);
4730           idk = CP_ID_KIND_NONE;
4731           is_member_access = false;
4732           break;
4733
4734         case CPP_OPEN_PAREN:
4735           /* postfix-expression ( expression-list [opt] ) */
4736           {
4737             bool koenig_p;
4738             bool is_builtin_constant_p;
4739             bool saved_integral_constant_expression_p = false;
4740             bool saved_non_integral_constant_expression_p = false;
4741             VEC(tree,gc) *args;
4742
4743             is_member_access = false;
4744
4745             is_builtin_constant_p
4746               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4747             if (is_builtin_constant_p)
4748               {
4749                 /* The whole point of __builtin_constant_p is to allow
4750                    non-constant expressions to appear as arguments.  */
4751                 saved_integral_constant_expression_p
4752                   = parser->integral_constant_expression_p;
4753                 saved_non_integral_constant_expression_p
4754                   = parser->non_integral_constant_expression_p;
4755                 parser->integral_constant_expression_p = false;
4756               }
4757             args = (cp_parser_parenthesized_expression_list
4758                     (parser, /*is_attribute_list=*/false,
4759                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4760                      /*non_constant_p=*/NULL));
4761             if (is_builtin_constant_p)
4762               {
4763                 parser->integral_constant_expression_p
4764                   = saved_integral_constant_expression_p;
4765                 parser->non_integral_constant_expression_p
4766                   = saved_non_integral_constant_expression_p;
4767               }
4768
4769             if (args == NULL)
4770               {
4771                 postfix_expression = error_mark_node;
4772                 break;
4773               }
4774
4775             /* Function calls are not permitted in
4776                constant-expressions.  */
4777             if (! builtin_valid_in_constant_expr_p (postfix_expression)
4778                 && cp_parser_non_integral_constant_expression (parser,
4779                                                                "a function call"))
4780               {
4781                 postfix_expression = error_mark_node;
4782                 release_tree_vector (args);
4783                 break;
4784               }
4785
4786             koenig_p = false;
4787             if (idk == CP_ID_KIND_UNQUALIFIED
4788                 || idk == CP_ID_KIND_TEMPLATE_ID)
4789               {
4790                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
4791                   {
4792                     if (!VEC_empty (tree, args))
4793                       {
4794                         koenig_p = true;
4795                         if (!any_type_dependent_arguments_p (args))
4796                           postfix_expression
4797                             = perform_koenig_lookup (postfix_expression, args);
4798                       }
4799                     else
4800                       postfix_expression
4801                         = unqualified_fn_lookup_error (postfix_expression);
4802                   }
4803                 /* We do not perform argument-dependent lookup if
4804                    normal lookup finds a non-function, in accordance
4805                    with the expected resolution of DR 218.  */
4806                 else if (!VEC_empty (tree, args)
4807                          && is_overloaded_fn (postfix_expression))
4808                   {
4809                     tree fn = get_first_fn (postfix_expression);
4810
4811                     if (TREE_CODE (fn) == TEMPLATE_ID_EXPR)
4812                       fn = OVL_CURRENT (TREE_OPERAND (fn, 0));
4813
4814                     /* Only do argument dependent lookup if regular
4815                        lookup does not find a set of member functions.
4816                        [basic.lookup.koenig]/2a  */
4817                     if (!DECL_FUNCTION_MEMBER_P (fn))
4818                       {
4819                         koenig_p = true;
4820                         if (!any_type_dependent_arguments_p (args))
4821                           postfix_expression
4822                             = perform_koenig_lookup (postfix_expression, args);
4823                       }
4824                   }
4825               }
4826
4827             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
4828               {
4829                 tree instance = TREE_OPERAND (postfix_expression, 0);
4830                 tree fn = TREE_OPERAND (postfix_expression, 1);
4831
4832                 if (processing_template_decl
4833                     && (type_dependent_expression_p (instance)
4834                         || (!BASELINK_P (fn)
4835                             && TREE_CODE (fn) != FIELD_DECL)
4836                         || type_dependent_expression_p (fn)
4837                         || any_type_dependent_arguments_p (args)))
4838                   {
4839                     postfix_expression
4840                       = build_nt_call_vec (postfix_expression, args);
4841                     release_tree_vector (args);
4842                     break;
4843                   }
4844
4845                 if (BASELINK_P (fn))
4846                   {
4847                   postfix_expression
4848                     = (build_new_method_call
4849                        (instance, fn, &args, NULL_TREE,
4850                         (idk == CP_ID_KIND_QUALIFIED
4851                          ? LOOKUP_NONVIRTUAL : LOOKUP_NORMAL),
4852                         /*fn_p=*/NULL,
4853                         tf_warning_or_error));
4854                   }
4855                 else
4856                   postfix_expression
4857                     = finish_call_expr (postfix_expression, &args,
4858                                         /*disallow_virtual=*/false,
4859                                         /*koenig_p=*/false,
4860                                         tf_warning_or_error);
4861               }
4862             else if (TREE_CODE (postfix_expression) == OFFSET_REF
4863                      || TREE_CODE (postfix_expression) == MEMBER_REF
4864                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
4865               postfix_expression = (build_offset_ref_call_from_tree
4866                                     (postfix_expression, &args));
4867             else if (idk == CP_ID_KIND_QUALIFIED)
4868               /* A call to a static class member, or a namespace-scope
4869                  function.  */
4870               postfix_expression
4871                 = finish_call_expr (postfix_expression, &args,
4872                                     /*disallow_virtual=*/true,
4873                                     koenig_p,
4874                                     tf_warning_or_error);
4875             else
4876               /* All other function calls.  */
4877               postfix_expression
4878                 = finish_call_expr (postfix_expression, &args,
4879                                     /*disallow_virtual=*/false,
4880                                     koenig_p,
4881                                     tf_warning_or_error);
4882
4883             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
4884             idk = CP_ID_KIND_NONE;
4885
4886             release_tree_vector (args);
4887           }
4888           break;
4889
4890         case CPP_DOT:
4891         case CPP_DEREF:
4892           /* postfix-expression . template [opt] id-expression
4893              postfix-expression . pseudo-destructor-name
4894              postfix-expression -> template [opt] id-expression
4895              postfix-expression -> pseudo-destructor-name */
4896
4897           /* Consume the `.' or `->' operator.  */
4898           cp_lexer_consume_token (parser->lexer);
4899
4900           postfix_expression
4901             = cp_parser_postfix_dot_deref_expression (parser, token->type,
4902                                                       postfix_expression,
4903                                                       false, &idk,
4904                                                       token->location);
4905
4906           is_member_access = true;
4907           break;
4908
4909         case CPP_PLUS_PLUS:
4910           /* postfix-expression ++  */
4911           /* Consume the `++' token.  */
4912           cp_lexer_consume_token (parser->lexer);
4913           /* Generate a representation for the complete expression.  */
4914           postfix_expression
4915             = finish_increment_expr (postfix_expression,
4916                                      POSTINCREMENT_EXPR);
4917           /* Increments may not appear in constant-expressions.  */
4918           if (cp_parser_non_integral_constant_expression (parser,
4919                                                           "an increment"))
4920             postfix_expression = error_mark_node;
4921           idk = CP_ID_KIND_NONE;
4922           is_member_access = false;
4923           break;
4924
4925         case CPP_MINUS_MINUS:
4926           /* postfix-expression -- */
4927           /* Consume the `--' token.  */
4928           cp_lexer_consume_token (parser->lexer);
4929           /* Generate a representation for the complete expression.  */
4930           postfix_expression
4931             = finish_increment_expr (postfix_expression,
4932                                      POSTDECREMENT_EXPR);
4933           /* Decrements may not appear in constant-expressions.  */
4934           if (cp_parser_non_integral_constant_expression (parser,
4935                                                           "a decrement"))
4936             postfix_expression = error_mark_node;
4937           idk = CP_ID_KIND_NONE;
4938           is_member_access = false;
4939           break;
4940
4941         default:
4942           if (pidk_return != NULL)
4943             * pidk_return = idk;
4944           if (member_access_only_p)
4945             return is_member_access? postfix_expression : error_mark_node;
4946           else
4947             return postfix_expression;
4948         }
4949     }
4950
4951   /* We should never get here.  */
4952   gcc_unreachable ();
4953   return error_mark_node;
4954 }
4955
4956 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
4957    by cp_parser_builtin_offsetof.  We're looking for
4958
4959      postfix-expression [ expression ]
4960
4961    FOR_OFFSETOF is set if we're being called in that context, which
4962    changes how we deal with integer constant expressions.  */
4963
4964 static tree
4965 cp_parser_postfix_open_square_expression (cp_parser *parser,
4966                                           tree postfix_expression,
4967                                           bool for_offsetof)
4968 {
4969   tree index;
4970
4971   /* Consume the `[' token.  */
4972   cp_lexer_consume_token (parser->lexer);
4973
4974   /* Parse the index expression.  */
4975   /* ??? For offsetof, there is a question of what to allow here.  If
4976      offsetof is not being used in an integral constant expression context,
4977      then we *could* get the right answer by computing the value at runtime.
4978      If we are in an integral constant expression context, then we might
4979      could accept any constant expression; hard to say without analysis.
4980      Rather than open the barn door too wide right away, allow only integer
4981      constant expressions here.  */
4982   if (for_offsetof)
4983     index = cp_parser_constant_expression (parser, false, NULL);
4984   else
4985     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
4986
4987   /* Look for the closing `]'.  */
4988   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
4989
4990   /* Build the ARRAY_REF.  */
4991   postfix_expression = grok_array_decl (postfix_expression, index);
4992
4993   /* When not doing offsetof, array references are not permitted in
4994      constant-expressions.  */
4995   if (!for_offsetof
4996       && (cp_parser_non_integral_constant_expression
4997           (parser, "an array reference")))
4998     postfix_expression = error_mark_node;
4999
5000   return postfix_expression;
5001 }
5002
5003 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5004    by cp_parser_builtin_offsetof.  We're looking for
5005
5006      postfix-expression . template [opt] id-expression
5007      postfix-expression . pseudo-destructor-name
5008      postfix-expression -> template [opt] id-expression
5009      postfix-expression -> pseudo-destructor-name
5010
5011    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5012    limits what of the above we'll actually accept, but nevermind.
5013    TOKEN_TYPE is the "." or "->" token, which will already have been
5014    removed from the stream.  */
5015
5016 static tree
5017 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5018                                         enum cpp_ttype token_type,
5019                                         tree postfix_expression,
5020                                         bool for_offsetof, cp_id_kind *idk,
5021                                         location_t location)
5022 {
5023   tree name;
5024   bool dependent_p;
5025   bool pseudo_destructor_p;
5026   tree scope = NULL_TREE;
5027
5028   /* If this is a `->' operator, dereference the pointer.  */
5029   if (token_type == CPP_DEREF)
5030     postfix_expression = build_x_arrow (postfix_expression);
5031   /* Check to see whether or not the expression is type-dependent.  */
5032   dependent_p = type_dependent_expression_p (postfix_expression);
5033   /* The identifier following the `->' or `.' is not qualified.  */
5034   parser->scope = NULL_TREE;
5035   parser->qualifying_scope = NULL_TREE;
5036   parser->object_scope = NULL_TREE;
5037   *idk = CP_ID_KIND_NONE;
5038
5039   /* Enter the scope corresponding to the type of the object
5040      given by the POSTFIX_EXPRESSION.  */
5041   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5042     {
5043       scope = TREE_TYPE (postfix_expression);
5044       /* According to the standard, no expression should ever have
5045          reference type.  Unfortunately, we do not currently match
5046          the standard in this respect in that our internal representation
5047          of an expression may have reference type even when the standard
5048          says it does not.  Therefore, we have to manually obtain the
5049          underlying type here.  */
5050       scope = non_reference (scope);
5051       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5052       if (scope == unknown_type_node)
5053         {
5054           error_at (location, "%qE does not have class type",
5055                     postfix_expression);
5056           scope = NULL_TREE;
5057         }
5058       else
5059         scope = complete_type_or_else (scope, NULL_TREE);
5060       /* Let the name lookup machinery know that we are processing a
5061          class member access expression.  */
5062       parser->context->object_type = scope;
5063       /* If something went wrong, we want to be able to discern that case,
5064          as opposed to the case where there was no SCOPE due to the type
5065          of expression being dependent.  */
5066       if (!scope)
5067         scope = error_mark_node;
5068       /* If the SCOPE was erroneous, make the various semantic analysis
5069          functions exit quickly -- and without issuing additional error
5070          messages.  */
5071       if (scope == error_mark_node)
5072         postfix_expression = error_mark_node;
5073     }
5074
5075   /* Assume this expression is not a pseudo-destructor access.  */
5076   pseudo_destructor_p = false;
5077
5078   /* If the SCOPE is a scalar type, then, if this is a valid program,
5079      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5080      is type dependent, it can be pseudo-destructor-name or something else.
5081      Try to parse it as pseudo-destructor-name first.  */
5082   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5083     {
5084       tree s;
5085       tree type;
5086
5087       cp_parser_parse_tentatively (parser);
5088       /* Parse the pseudo-destructor-name.  */
5089       s = NULL_TREE;
5090       cp_parser_pseudo_destructor_name (parser, &s, &type);
5091       if (dependent_p
5092           && (cp_parser_error_occurred (parser)
5093               || TREE_CODE (type) != TYPE_DECL
5094               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5095         cp_parser_abort_tentative_parse (parser);
5096       else if (cp_parser_parse_definitely (parser))
5097         {
5098           pseudo_destructor_p = true;
5099           postfix_expression
5100             = finish_pseudo_destructor_expr (postfix_expression,
5101                                              s, TREE_TYPE (type));
5102         }
5103     }
5104
5105   if (!pseudo_destructor_p)
5106     {
5107       /* If the SCOPE is not a scalar type, we are looking at an
5108          ordinary class member access expression, rather than a
5109          pseudo-destructor-name.  */
5110       bool template_p;
5111       cp_token *token = cp_lexer_peek_token (parser->lexer);
5112       /* Parse the id-expression.  */
5113       name = (cp_parser_id_expression
5114               (parser,
5115                cp_parser_optional_template_keyword (parser),
5116                /*check_dependency_p=*/true,
5117                &template_p,
5118                /*declarator_p=*/false,
5119                /*optional_p=*/false));
5120       /* In general, build a SCOPE_REF if the member name is qualified.
5121          However, if the name was not dependent and has already been
5122          resolved; there is no need to build the SCOPE_REF.  For example;
5123
5124              struct X { void f(); };
5125              template <typename T> void f(T* t) { t->X::f(); }
5126
5127          Even though "t" is dependent, "X::f" is not and has been resolved
5128          to a BASELINK; there is no need to include scope information.  */
5129
5130       /* But we do need to remember that there was an explicit scope for
5131          virtual function calls.  */
5132       if (parser->scope)
5133         *idk = CP_ID_KIND_QUALIFIED;
5134
5135       /* If the name is a template-id that names a type, we will get a
5136          TYPE_DECL here.  That is invalid code.  */
5137       if (TREE_CODE (name) == TYPE_DECL)
5138         {
5139           error_at (token->location, "invalid use of %qD", name);
5140           postfix_expression = error_mark_node;
5141         }
5142       else
5143         {
5144           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5145             {
5146               name = build_qualified_name (/*type=*/NULL_TREE,
5147                                            parser->scope,
5148                                            name,
5149                                            template_p);
5150               parser->scope = NULL_TREE;
5151               parser->qualifying_scope = NULL_TREE;
5152               parser->object_scope = NULL_TREE;
5153             }
5154           if (scope && name && BASELINK_P (name))
5155             adjust_result_of_qualified_name_lookup
5156               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5157           postfix_expression
5158             = finish_class_member_access_expr (postfix_expression, name,
5159                                                template_p, 
5160                                                tf_warning_or_error);
5161         }
5162     }
5163
5164   /* We no longer need to look up names in the scope of the object on
5165      the left-hand side of the `.' or `->' operator.  */
5166   parser->context->object_type = NULL_TREE;
5167
5168   /* Outside of offsetof, these operators may not appear in
5169      constant-expressions.  */
5170   if (!for_offsetof
5171       && (cp_parser_non_integral_constant_expression
5172           (parser, token_type == CPP_DEREF ? "%<->%>" : "%<.%>")))
5173     postfix_expression = error_mark_node;
5174
5175   return postfix_expression;
5176 }
5177
5178 /* Parse a parenthesized expression-list.
5179
5180    expression-list:
5181      assignment-expression
5182      expression-list, assignment-expression
5183
5184    attribute-list:
5185      expression-list
5186      identifier
5187      identifier, expression-list
5188
5189    CAST_P is true if this expression is the target of a cast.
5190
5191    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5192    argument pack.
5193
5194    Returns a vector of trees.  Each element is a representation of an
5195    assignment-expression.  NULL is returned if the ( and or ) are
5196    missing.  An empty, but allocated, vector is returned on no
5197    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is true
5198    if this is really an attribute list being parsed.  If
5199    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5200    not all of the expressions in the list were constant.  */
5201
5202 static VEC(tree,gc) *
5203 cp_parser_parenthesized_expression_list (cp_parser* parser,
5204                                          bool is_attribute_list,
5205                                          bool cast_p,
5206                                          bool allow_expansion_p,
5207                                          bool *non_constant_p)
5208 {
5209   VEC(tree,gc) *expression_list;
5210   bool fold_expr_p = is_attribute_list;
5211   tree identifier = NULL_TREE;
5212   bool saved_greater_than_is_operator_p;
5213
5214   /* Assume all the expressions will be constant.  */
5215   if (non_constant_p)
5216     *non_constant_p = false;
5217
5218   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
5219     return NULL;
5220
5221   expression_list = make_tree_vector ();
5222
5223   /* Within a parenthesized expression, a `>' token is always
5224      the greater-than operator.  */
5225   saved_greater_than_is_operator_p
5226     = parser->greater_than_is_operator_p;
5227   parser->greater_than_is_operator_p = true;
5228
5229   /* Consume expressions until there are no more.  */
5230   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5231     while (true)
5232       {
5233         tree expr;
5234
5235         /* At the beginning of attribute lists, check to see if the
5236            next token is an identifier.  */
5237         if (is_attribute_list
5238             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5239           {
5240             cp_token *token;
5241
5242             /* Consume the identifier.  */
5243             token = cp_lexer_consume_token (parser->lexer);
5244             /* Save the identifier.  */
5245             identifier = token->u.value;
5246           }
5247         else
5248           {
5249             bool expr_non_constant_p;
5250
5251             /* Parse the next assignment-expression.  */
5252             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5253               {
5254                 /* A braced-init-list.  */
5255                 maybe_warn_cpp0x ("extended initializer lists");
5256                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5257                 if (non_constant_p && expr_non_constant_p)
5258                   *non_constant_p = true;
5259               }
5260             else if (non_constant_p)
5261               {
5262                 expr = (cp_parser_constant_expression
5263                         (parser, /*allow_non_constant_p=*/true,
5264                          &expr_non_constant_p));
5265                 if (expr_non_constant_p)
5266                   *non_constant_p = true;
5267               }
5268             else
5269               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5270
5271             if (fold_expr_p)
5272               expr = fold_non_dependent_expr (expr);
5273
5274             /* If we have an ellipsis, then this is an expression
5275                expansion.  */
5276             if (allow_expansion_p
5277                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5278               {
5279                 /* Consume the `...'.  */
5280                 cp_lexer_consume_token (parser->lexer);
5281
5282                 /* Build the argument pack.  */
5283                 expr = make_pack_expansion (expr);
5284               }
5285
5286              /* Add it to the list.  We add error_mark_node
5287                 expressions to the list, so that we can still tell if
5288                 the correct form for a parenthesized expression-list
5289                 is found. That gives better errors.  */
5290             VEC_safe_push (tree, gc, expression_list, expr);
5291
5292             if (expr == error_mark_node)
5293               goto skip_comma;
5294           }
5295
5296         /* After the first item, attribute lists look the same as
5297            expression lists.  */
5298         is_attribute_list = false;
5299
5300       get_comma:;
5301         /* If the next token isn't a `,', then we are done.  */
5302         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5303           break;
5304
5305         /* Otherwise, consume the `,' and keep going.  */
5306         cp_lexer_consume_token (parser->lexer);
5307       }
5308
5309   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
5310     {
5311       int ending;
5312
5313     skip_comma:;
5314       /* We try and resync to an unnested comma, as that will give the
5315          user better diagnostics.  */
5316       ending = cp_parser_skip_to_closing_parenthesis (parser,
5317                                                       /*recovering=*/true,
5318                                                       /*or_comma=*/true,
5319                                                       /*consume_paren=*/true);
5320       if (ending < 0)
5321         goto get_comma;
5322       if (!ending)
5323         {
5324           parser->greater_than_is_operator_p
5325             = saved_greater_than_is_operator_p;
5326           return NULL;
5327         }
5328     }
5329
5330   parser->greater_than_is_operator_p
5331     = saved_greater_than_is_operator_p;
5332
5333   if (identifier)
5334     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5335
5336   return expression_list;
5337 }
5338
5339 /* Parse a pseudo-destructor-name.
5340
5341    pseudo-destructor-name:
5342      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5343      :: [opt] nested-name-specifier template template-id :: ~ type-name
5344      :: [opt] nested-name-specifier [opt] ~ type-name
5345
5346    If either of the first two productions is used, sets *SCOPE to the
5347    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5348    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5349    or ERROR_MARK_NODE if the parse fails.  */
5350
5351 static void
5352 cp_parser_pseudo_destructor_name (cp_parser* parser,
5353                                   tree* scope,
5354                                   tree* type)
5355 {
5356   bool nested_name_specifier_p;
5357
5358   /* Assume that things will not work out.  */
5359   *type = error_mark_node;
5360
5361   /* Look for the optional `::' operator.  */
5362   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5363   /* Look for the optional nested-name-specifier.  */
5364   nested_name_specifier_p
5365     = (cp_parser_nested_name_specifier_opt (parser,
5366                                             /*typename_keyword_p=*/false,
5367                                             /*check_dependency_p=*/true,
5368                                             /*type_p=*/false,
5369                                             /*is_declaration=*/false)
5370        != NULL_TREE);
5371   /* Now, if we saw a nested-name-specifier, we might be doing the
5372      second production.  */
5373   if (nested_name_specifier_p
5374       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5375     {
5376       /* Consume the `template' keyword.  */
5377       cp_lexer_consume_token (parser->lexer);
5378       /* Parse the template-id.  */
5379       cp_parser_template_id (parser,
5380                              /*template_keyword_p=*/true,
5381                              /*check_dependency_p=*/false,
5382                              /*is_declaration=*/true);
5383       /* Look for the `::' token.  */
5384       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5385     }
5386   /* If the next token is not a `~', then there might be some
5387      additional qualification.  */
5388   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5389     {
5390       /* At this point, we're looking for "type-name :: ~".  The type-name
5391          must not be a class-name, since this is a pseudo-destructor.  So,
5392          it must be either an enum-name, or a typedef-name -- both of which
5393          are just identifiers.  So, we peek ahead to check that the "::"
5394          and "~" tokens are present; if they are not, then we can avoid
5395          calling type_name.  */
5396       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5397           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5398           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5399         {
5400           cp_parser_error (parser, "non-scalar type");
5401           return;
5402         }
5403
5404       /* Look for the type-name.  */
5405       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5406       if (*scope == error_mark_node)
5407         return;
5408
5409       /* Look for the `::' token.  */
5410       cp_parser_require (parser, CPP_SCOPE, "%<::%>");
5411     }
5412   else
5413     *scope = NULL_TREE;
5414
5415   /* Look for the `~'.  */
5416   cp_parser_require (parser, CPP_COMPL, "%<~%>");
5417   /* Look for the type-name again.  We are not responsible for
5418      checking that it matches the first type-name.  */
5419   *type = cp_parser_nonclass_name (parser);
5420 }
5421
5422 /* Parse a unary-expression.
5423
5424    unary-expression:
5425      postfix-expression
5426      ++ cast-expression
5427      -- cast-expression
5428      unary-operator cast-expression
5429      sizeof unary-expression
5430      sizeof ( type-id )
5431      new-expression
5432      delete-expression
5433
5434    GNU Extensions:
5435
5436    unary-expression:
5437      __extension__ cast-expression
5438      __alignof__ unary-expression
5439      __alignof__ ( type-id )
5440      __real__ cast-expression
5441      __imag__ cast-expression
5442      && identifier
5443
5444    ADDRESS_P is true iff the unary-expression is appearing as the
5445    operand of the `&' operator.   CAST_P is true if this expression is
5446    the target of a cast.
5447
5448    Returns a representation of the expression.  */
5449
5450 static tree
5451 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5452                             cp_id_kind * pidk)
5453 {
5454   cp_token *token;
5455   enum tree_code unary_operator;
5456
5457   /* Peek at the next token.  */
5458   token = cp_lexer_peek_token (parser->lexer);
5459   /* Some keywords give away the kind of expression.  */
5460   if (token->type == CPP_KEYWORD)
5461     {
5462       enum rid keyword = token->keyword;
5463
5464       switch (keyword)
5465         {
5466         case RID_ALIGNOF:
5467         case RID_SIZEOF:
5468           {
5469             tree operand;
5470             enum tree_code op;
5471
5472             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5473             /* Consume the token.  */
5474             cp_lexer_consume_token (parser->lexer);
5475             /* Parse the operand.  */
5476             operand = cp_parser_sizeof_operand (parser, keyword);
5477
5478             if (TYPE_P (operand))
5479               return cxx_sizeof_or_alignof_type (operand, op, true);
5480             else
5481               return cxx_sizeof_or_alignof_expr (operand, op, true);
5482           }
5483
5484         case RID_NEW:
5485           return cp_parser_new_expression (parser);
5486
5487         case RID_DELETE:
5488           return cp_parser_delete_expression (parser);
5489
5490         case RID_EXTENSION:
5491           {
5492             /* The saved value of the PEDANTIC flag.  */
5493             int saved_pedantic;
5494             tree expr;
5495
5496             /* Save away the PEDANTIC flag.  */
5497             cp_parser_extension_opt (parser, &saved_pedantic);
5498             /* Parse the cast-expression.  */
5499             expr = cp_parser_simple_cast_expression (parser);
5500             /* Restore the PEDANTIC flag.  */
5501             pedantic = saved_pedantic;
5502
5503             return expr;
5504           }
5505
5506         case RID_REALPART:
5507         case RID_IMAGPART:
5508           {
5509             tree expression;
5510
5511             /* Consume the `__real__' or `__imag__' token.  */
5512             cp_lexer_consume_token (parser->lexer);
5513             /* Parse the cast-expression.  */
5514             expression = cp_parser_simple_cast_expression (parser);
5515             /* Create the complete representation.  */
5516             return build_x_unary_op ((keyword == RID_REALPART
5517                                       ? REALPART_EXPR : IMAGPART_EXPR),
5518                                      expression,
5519                                      tf_warning_or_error);
5520           }
5521           break;
5522
5523         default:
5524           break;
5525         }
5526     }
5527
5528   /* Look for the `:: new' and `:: delete', which also signal the
5529      beginning of a new-expression, or delete-expression,
5530      respectively.  If the next token is `::', then it might be one of
5531      these.  */
5532   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5533     {
5534       enum rid keyword;
5535
5536       /* See if the token after the `::' is one of the keywords in
5537          which we're interested.  */
5538       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5539       /* If it's `new', we have a new-expression.  */
5540       if (keyword == RID_NEW)
5541         return cp_parser_new_expression (parser);
5542       /* Similarly, for `delete'.  */
5543       else if (keyword == RID_DELETE)
5544         return cp_parser_delete_expression (parser);
5545     }
5546
5547   /* Look for a unary operator.  */
5548   unary_operator = cp_parser_unary_operator (token);
5549   /* The `++' and `--' operators can be handled similarly, even though
5550      they are not technically unary-operators in the grammar.  */
5551   if (unary_operator == ERROR_MARK)
5552     {
5553       if (token->type == CPP_PLUS_PLUS)
5554         unary_operator = PREINCREMENT_EXPR;
5555       else if (token->type == CPP_MINUS_MINUS)
5556         unary_operator = PREDECREMENT_EXPR;
5557       /* Handle the GNU address-of-label extension.  */
5558       else if (cp_parser_allow_gnu_extensions_p (parser)
5559                && token->type == CPP_AND_AND)
5560         {
5561           tree identifier;
5562           tree expression;
5563           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5564
5565           /* Consume the '&&' token.  */
5566           cp_lexer_consume_token (parser->lexer);
5567           /* Look for the identifier.  */
5568           identifier = cp_parser_identifier (parser);
5569           /* Create an expression representing the address.  */
5570           expression = finish_label_address_expr (identifier, loc);
5571           if (cp_parser_non_integral_constant_expression (parser,
5572                                                 "the address of a label"))
5573             expression = error_mark_node;
5574           return expression;
5575         }
5576     }
5577   if (unary_operator != ERROR_MARK)
5578     {
5579       tree cast_expression;
5580       tree expression = error_mark_node;
5581       const char *non_constant_p = NULL;
5582
5583       /* Consume the operator token.  */
5584       token = cp_lexer_consume_token (parser->lexer);
5585       /* Parse the cast-expression.  */
5586       cast_expression
5587         = cp_parser_cast_expression (parser,
5588                                      unary_operator == ADDR_EXPR,
5589                                      /*cast_p=*/false, pidk);
5590       /* Now, build an appropriate representation.  */
5591       switch (unary_operator)
5592         {
5593         case INDIRECT_REF:
5594           non_constant_p = "%<*%>";
5595           expression = build_x_indirect_ref (cast_expression, "unary *",
5596                                              tf_warning_or_error);
5597           break;
5598
5599         case ADDR_EXPR:
5600           non_constant_p = "%<&%>";
5601           /* Fall through.  */
5602         case BIT_NOT_EXPR:
5603           expression = build_x_unary_op (unary_operator, cast_expression,
5604                                          tf_warning_or_error);
5605           break;
5606
5607         case PREINCREMENT_EXPR:
5608         case PREDECREMENT_EXPR:
5609           non_constant_p = (unary_operator == PREINCREMENT_EXPR
5610                             ? "%<++%>" : "%<--%>");
5611           /* Fall through.  */
5612         case UNARY_PLUS_EXPR:
5613         case NEGATE_EXPR:
5614         case TRUTH_NOT_EXPR:
5615           expression = finish_unary_op_expr (unary_operator, cast_expression);
5616           break;
5617
5618         default:
5619           gcc_unreachable ();
5620         }
5621
5622       if (non_constant_p
5623           && cp_parser_non_integral_constant_expression (parser,
5624                                                          non_constant_p))
5625         expression = error_mark_node;
5626
5627       return expression;
5628     }
5629
5630   return cp_parser_postfix_expression (parser, address_p, cast_p,
5631                                        /*member_access_only_p=*/false,
5632                                        pidk);
5633 }
5634
5635 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5636    unary-operator, the corresponding tree code is returned.  */
5637
5638 static enum tree_code
5639 cp_parser_unary_operator (cp_token* token)
5640 {
5641   switch (token->type)
5642     {
5643     case CPP_MULT:
5644       return INDIRECT_REF;
5645
5646     case CPP_AND:
5647       return ADDR_EXPR;
5648
5649     case CPP_PLUS:
5650       return UNARY_PLUS_EXPR;
5651
5652     case CPP_MINUS:
5653       return NEGATE_EXPR;
5654
5655     case CPP_NOT:
5656       return TRUTH_NOT_EXPR;
5657
5658     case CPP_COMPL:
5659       return BIT_NOT_EXPR;
5660
5661     default:
5662       return ERROR_MARK;
5663     }
5664 }
5665
5666 /* Parse a new-expression.
5667
5668    new-expression:
5669      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5670      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5671
5672    Returns a representation of the expression.  */
5673
5674 static tree
5675 cp_parser_new_expression (cp_parser* parser)
5676 {
5677   bool global_scope_p;
5678   VEC(tree,gc) *placement;
5679   tree type;
5680   VEC(tree,gc) *initializer;
5681   tree nelts;
5682   tree ret;
5683
5684   /* Look for the optional `::' operator.  */
5685   global_scope_p
5686     = (cp_parser_global_scope_opt (parser,
5687                                    /*current_scope_valid_p=*/false)
5688        != NULL_TREE);
5689   /* Look for the `new' operator.  */
5690   cp_parser_require_keyword (parser, RID_NEW, "%<new%>");
5691   /* There's no easy way to tell a new-placement from the
5692      `( type-id )' construct.  */
5693   cp_parser_parse_tentatively (parser);
5694   /* Look for a new-placement.  */
5695   placement = cp_parser_new_placement (parser);
5696   /* If that didn't work out, there's no new-placement.  */
5697   if (!cp_parser_parse_definitely (parser))
5698     {
5699       if (placement != NULL)
5700         release_tree_vector (placement);
5701       placement = NULL;
5702     }
5703
5704   /* If the next token is a `(', then we have a parenthesized
5705      type-id.  */
5706   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5707     {
5708       cp_token *token;
5709       /* Consume the `('.  */
5710       cp_lexer_consume_token (parser->lexer);
5711       /* Parse the type-id.  */
5712       type = cp_parser_type_id (parser);
5713       /* Look for the closing `)'.  */
5714       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
5715       token = cp_lexer_peek_token (parser->lexer);
5716       /* There should not be a direct-new-declarator in this production,
5717          but GCC used to allowed this, so we check and emit a sensible error
5718          message for this case.  */
5719       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5720         {
5721           error_at (token->location,
5722                     "array bound forbidden after parenthesized type-id");
5723           inform (token->location, 
5724                   "try removing the parentheses around the type-id");
5725           cp_parser_direct_new_declarator (parser);
5726         }
5727       nelts = NULL_TREE;
5728     }
5729   /* Otherwise, there must be a new-type-id.  */
5730   else
5731     type = cp_parser_new_type_id (parser, &nelts);
5732
5733   /* If the next token is a `(' or '{', then we have a new-initializer.  */
5734   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
5735       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5736     initializer = cp_parser_new_initializer (parser);
5737   else
5738     initializer = NULL;
5739
5740   /* A new-expression may not appear in an integral constant
5741      expression.  */
5742   if (cp_parser_non_integral_constant_expression (parser, "%<new%>"))
5743     ret = error_mark_node;
5744   else
5745     {
5746       /* Create a representation of the new-expression.  */
5747       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
5748                        tf_warning_or_error);
5749     }
5750
5751   if (placement != NULL)
5752     release_tree_vector (placement);
5753   if (initializer != NULL)
5754     release_tree_vector (initializer);
5755
5756   return ret;
5757 }
5758
5759 /* Parse a new-placement.
5760
5761    new-placement:
5762      ( expression-list )
5763
5764    Returns the same representation as for an expression-list.  */
5765
5766 static VEC(tree,gc) *
5767 cp_parser_new_placement (cp_parser* parser)
5768 {
5769   VEC(tree,gc) *expression_list;
5770
5771   /* Parse the expression-list.  */
5772   expression_list = (cp_parser_parenthesized_expression_list
5773                      (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5774                       /*non_constant_p=*/NULL));
5775
5776   return expression_list;
5777 }
5778
5779 /* Parse a new-type-id.
5780
5781    new-type-id:
5782      type-specifier-seq new-declarator [opt]
5783
5784    Returns the TYPE allocated.  If the new-type-id indicates an array
5785    type, *NELTS is set to the number of elements in the last array
5786    bound; the TYPE will not include the last array bound.  */
5787
5788 static tree
5789 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
5790 {
5791   cp_decl_specifier_seq type_specifier_seq;
5792   cp_declarator *new_declarator;
5793   cp_declarator *declarator;
5794   cp_declarator *outer_declarator;
5795   const char *saved_message;
5796   tree type;
5797
5798   /* The type-specifier sequence must not contain type definitions.
5799      (It cannot contain declarations of new types either, but if they
5800      are not definitions we will catch that because they are not
5801      complete.)  */
5802   saved_message = parser->type_definition_forbidden_message;
5803   parser->type_definition_forbidden_message
5804     = "types may not be defined in a new-type-id";
5805   /* Parse the type-specifier-seq.  */
5806   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
5807                                 /*is_trailing_return=*/false,
5808                                 &type_specifier_seq);
5809   /* Restore the old message.  */
5810   parser->type_definition_forbidden_message = saved_message;
5811   /* Parse the new-declarator.  */
5812   new_declarator = cp_parser_new_declarator_opt (parser);
5813
5814   /* Determine the number of elements in the last array dimension, if
5815      any.  */
5816   *nelts = NULL_TREE;
5817   /* Skip down to the last array dimension.  */
5818   declarator = new_declarator;
5819   outer_declarator = NULL;
5820   while (declarator && (declarator->kind == cdk_pointer
5821                         || declarator->kind == cdk_ptrmem))
5822     {
5823       outer_declarator = declarator;
5824       declarator = declarator->declarator;
5825     }
5826   while (declarator
5827          && declarator->kind == cdk_array
5828          && declarator->declarator
5829          && declarator->declarator->kind == cdk_array)
5830     {
5831       outer_declarator = declarator;
5832       declarator = declarator->declarator;
5833     }
5834
5835   if (declarator && declarator->kind == cdk_array)
5836     {
5837       *nelts = declarator->u.array.bounds;
5838       if (*nelts == error_mark_node)
5839         *nelts = integer_one_node;
5840
5841       if (outer_declarator)
5842         outer_declarator->declarator = declarator->declarator;
5843       else
5844         new_declarator = NULL;
5845     }
5846
5847   type = groktypename (&type_specifier_seq, new_declarator, false);
5848   return type;
5849 }
5850
5851 /* Parse an (optional) new-declarator.
5852
5853    new-declarator:
5854      ptr-operator new-declarator [opt]
5855      direct-new-declarator
5856
5857    Returns the declarator.  */
5858
5859 static cp_declarator *
5860 cp_parser_new_declarator_opt (cp_parser* parser)
5861 {
5862   enum tree_code code;
5863   tree type;
5864   cp_cv_quals cv_quals;
5865
5866   /* We don't know if there's a ptr-operator next, or not.  */
5867   cp_parser_parse_tentatively (parser);
5868   /* Look for a ptr-operator.  */
5869   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
5870   /* If that worked, look for more new-declarators.  */
5871   if (cp_parser_parse_definitely (parser))
5872     {
5873       cp_declarator *declarator;
5874
5875       /* Parse another optional declarator.  */
5876       declarator = cp_parser_new_declarator_opt (parser);
5877
5878       return cp_parser_make_indirect_declarator
5879         (code, type, cv_quals, declarator);
5880     }
5881
5882   /* If the next token is a `[', there is a direct-new-declarator.  */
5883   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
5884     return cp_parser_direct_new_declarator (parser);
5885
5886   return NULL;
5887 }
5888
5889 /* Parse a direct-new-declarator.
5890
5891    direct-new-declarator:
5892      [ expression ]
5893      direct-new-declarator [constant-expression]
5894
5895    */
5896
5897 static cp_declarator *
5898 cp_parser_direct_new_declarator (cp_parser* parser)
5899 {
5900   cp_declarator *declarator = NULL;
5901
5902   while (true)
5903     {
5904       tree expression;
5905
5906       /* Look for the opening `['.  */
5907       cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
5908       /* The first expression is not required to be constant.  */
5909       if (!declarator)
5910         {
5911           cp_token *token = cp_lexer_peek_token (parser->lexer);
5912           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5913           /* The standard requires that the expression have integral
5914              type.  DR 74 adds enumeration types.  We believe that the
5915              real intent is that these expressions be handled like the
5916              expression in a `switch' condition, which also allows
5917              classes with a single conversion to integral or
5918              enumeration type.  */
5919           if (!processing_template_decl)
5920             {
5921               expression
5922                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
5923                                               expression,
5924                                               /*complain=*/true);
5925               if (!expression)
5926                 {
5927                   error_at (token->location,
5928                             "expression in new-declarator must have integral "
5929                             "or enumeration type");
5930                   expression = error_mark_node;
5931                 }
5932             }
5933         }
5934       /* But all the other expressions must be.  */
5935       else
5936         expression
5937           = cp_parser_constant_expression (parser,
5938                                            /*allow_non_constant=*/false,
5939                                            NULL);
5940       /* Look for the closing `]'.  */
5941       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
5942
5943       /* Add this bound to the declarator.  */
5944       declarator = make_array_declarator (declarator, expression);
5945
5946       /* If the next token is not a `[', then there are no more
5947          bounds.  */
5948       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
5949         break;
5950     }
5951
5952   return declarator;
5953 }
5954
5955 /* Parse a new-initializer.
5956
5957    new-initializer:
5958      ( expression-list [opt] )
5959      braced-init-list
5960
5961    Returns a representation of the expression-list.  */
5962
5963 static VEC(tree,gc) *
5964 cp_parser_new_initializer (cp_parser* parser)
5965 {
5966   VEC(tree,gc) *expression_list;
5967
5968   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5969     {
5970       tree t;
5971       bool expr_non_constant_p;
5972       maybe_warn_cpp0x ("extended initializer lists");
5973       t = cp_parser_braced_list (parser, &expr_non_constant_p);
5974       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
5975       expression_list = make_tree_vector_single (t);
5976     }
5977   else
5978     expression_list = (cp_parser_parenthesized_expression_list
5979                        (parser, false, /*cast_p=*/false, /*allow_expansion_p=*/true,
5980                         /*non_constant_p=*/NULL));
5981
5982   return expression_list;
5983 }
5984
5985 /* Parse a delete-expression.
5986
5987    delete-expression:
5988      :: [opt] delete cast-expression
5989      :: [opt] delete [ ] cast-expression
5990
5991    Returns a representation of the expression.  */
5992
5993 static tree
5994 cp_parser_delete_expression (cp_parser* parser)
5995 {
5996   bool global_scope_p;
5997   bool array_p;
5998   tree expression;
5999
6000   /* Look for the optional `::' operator.  */
6001   global_scope_p
6002     = (cp_parser_global_scope_opt (parser,
6003                                    /*current_scope_valid_p=*/false)
6004        != NULL_TREE);
6005   /* Look for the `delete' keyword.  */
6006   cp_parser_require_keyword (parser, RID_DELETE, "%<delete%>");
6007   /* See if the array syntax is in use.  */
6008   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6009     {
6010       /* Consume the `[' token.  */
6011       cp_lexer_consume_token (parser->lexer);
6012       /* Look for the `]' token.  */
6013       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
6014       /* Remember that this is the `[]' construct.  */
6015       array_p = true;
6016     }
6017   else
6018     array_p = false;
6019
6020   /* Parse the cast-expression.  */
6021   expression = cp_parser_simple_cast_expression (parser);
6022
6023   /* A delete-expression may not appear in an integral constant
6024      expression.  */
6025   if (cp_parser_non_integral_constant_expression (parser, "%<delete%>"))
6026     return error_mark_node;
6027
6028   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p);
6029 }
6030
6031 /* Returns true if TOKEN may start a cast-expression and false
6032    otherwise.  */
6033
6034 static bool
6035 cp_parser_token_starts_cast_expression (cp_token *token)
6036 {
6037   switch (token->type)
6038     {
6039     case CPP_COMMA:
6040     case CPP_SEMICOLON:
6041     case CPP_QUERY:
6042     case CPP_COLON:
6043     case CPP_CLOSE_SQUARE:
6044     case CPP_CLOSE_PAREN:
6045     case CPP_CLOSE_BRACE:
6046     case CPP_DOT:
6047     case CPP_DOT_STAR:
6048     case CPP_DEREF:
6049     case CPP_DEREF_STAR:
6050     case CPP_DIV:
6051     case CPP_MOD:
6052     case CPP_LSHIFT:
6053     case CPP_RSHIFT:
6054     case CPP_LESS:
6055     case CPP_GREATER:
6056     case CPP_LESS_EQ:
6057     case CPP_GREATER_EQ:
6058     case CPP_EQ_EQ:
6059     case CPP_NOT_EQ:
6060     case CPP_EQ:
6061     case CPP_MULT_EQ:
6062     case CPP_DIV_EQ:
6063     case CPP_MOD_EQ:
6064     case CPP_PLUS_EQ:
6065     case CPP_MINUS_EQ:
6066     case CPP_RSHIFT_EQ:
6067     case CPP_LSHIFT_EQ:
6068     case CPP_AND_EQ:
6069     case CPP_XOR_EQ:
6070     case CPP_OR_EQ:
6071     case CPP_XOR:
6072     case CPP_OR:
6073     case CPP_OR_OR:
6074     case CPP_EOF:
6075       return false;
6076
6077       /* '[' may start a primary-expression in obj-c++.  */
6078     case CPP_OPEN_SQUARE:
6079       return c_dialect_objc ();
6080
6081     default:
6082       return true;
6083     }
6084 }
6085
6086 /* Parse a cast-expression.
6087
6088    cast-expression:
6089      unary-expression
6090      ( type-id ) cast-expression
6091
6092    ADDRESS_P is true iff the unary-expression is appearing as the
6093    operand of the `&' operator.   CAST_P is true if this expression is
6094    the target of a cast.
6095
6096    Returns a representation of the expression.  */
6097
6098 static tree
6099 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6100                            cp_id_kind * pidk)
6101 {
6102   /* If it's a `(', then we might be looking at a cast.  */
6103   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6104     {
6105       tree type = NULL_TREE;
6106       tree expr = NULL_TREE;
6107       bool compound_literal_p;
6108       const char *saved_message;
6109
6110       /* There's no way to know yet whether or not this is a cast.
6111          For example, `(int (3))' is a unary-expression, while `(int)
6112          3' is a cast.  So, we resort to parsing tentatively.  */
6113       cp_parser_parse_tentatively (parser);
6114       /* Types may not be defined in a cast.  */
6115       saved_message = parser->type_definition_forbidden_message;
6116       parser->type_definition_forbidden_message
6117         = "types may not be defined in casts";
6118       /* Consume the `('.  */
6119       cp_lexer_consume_token (parser->lexer);
6120       /* A very tricky bit is that `(struct S) { 3 }' is a
6121          compound-literal (which we permit in C++ as an extension).
6122          But, that construct is not a cast-expression -- it is a
6123          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6124          is legal; if the compound-literal were a cast-expression,
6125          you'd need an extra set of parentheses.)  But, if we parse
6126          the type-id, and it happens to be a class-specifier, then we
6127          will commit to the parse at that point, because we cannot
6128          undo the action that is done when creating a new class.  So,
6129          then we cannot back up and do a postfix-expression.
6130
6131          Therefore, we scan ahead to the closing `)', and check to see
6132          if the token after the `)' is a `{'.  If so, we are not
6133          looking at a cast-expression.
6134
6135          Save tokens so that we can put them back.  */
6136       cp_lexer_save_tokens (parser->lexer);
6137       /* Skip tokens until the next token is a closing parenthesis.
6138          If we find the closing `)', and the next token is a `{', then
6139          we are looking at a compound-literal.  */
6140       compound_literal_p
6141         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6142                                                   /*consume_paren=*/true)
6143            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6144       /* Roll back the tokens we skipped.  */
6145       cp_lexer_rollback_tokens (parser->lexer);
6146       /* If we were looking at a compound-literal, simulate an error
6147          so that the call to cp_parser_parse_definitely below will
6148          fail.  */
6149       if (compound_literal_p)
6150         cp_parser_simulate_error (parser);
6151       else
6152         {
6153           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6154           parser->in_type_id_in_expr_p = true;
6155           /* Look for the type-id.  */
6156           type = cp_parser_type_id (parser);
6157           /* Look for the closing `)'.  */
6158           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6159           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6160         }
6161
6162       /* Restore the saved message.  */
6163       parser->type_definition_forbidden_message = saved_message;
6164
6165       /* At this point this can only be either a cast or a
6166          parenthesized ctor such as `(T ())' that looks like a cast to
6167          function returning T.  */
6168       if (!cp_parser_error_occurred (parser)
6169           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6170                                                      (parser->lexer)))
6171         {
6172           cp_parser_parse_definitely (parser);
6173           expr = cp_parser_cast_expression (parser,
6174                                             /*address_p=*/false,
6175                                             /*cast_p=*/true, pidk);
6176
6177           /* Warn about old-style casts, if so requested.  */
6178           if (warn_old_style_cast
6179               && !in_system_header
6180               && !VOID_TYPE_P (type)
6181               && current_lang_name != lang_name_c)
6182             warning (OPT_Wold_style_cast, "use of old-style cast");
6183
6184           /* Only type conversions to integral or enumeration types
6185              can be used in constant-expressions.  */
6186           if (!cast_valid_in_integral_constant_expression_p (type)
6187               && (cp_parser_non_integral_constant_expression
6188                   (parser,
6189                    "a cast to a type other than an integral or "
6190                    "enumeration type")))
6191             return error_mark_node;
6192
6193           /* Perform the cast.  */
6194           expr = build_c_cast (input_location, type, expr);
6195           return expr;
6196         }
6197       else 
6198         cp_parser_abort_tentative_parse (parser);
6199     }
6200
6201   /* If we get here, then it's not a cast, so it must be a
6202      unary-expression.  */
6203   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6204 }
6205
6206 /* Parse a binary expression of the general form:
6207
6208    pm-expression:
6209      cast-expression
6210      pm-expression .* cast-expression
6211      pm-expression ->* cast-expression
6212
6213    multiplicative-expression:
6214      pm-expression
6215      multiplicative-expression * pm-expression
6216      multiplicative-expression / pm-expression
6217      multiplicative-expression % pm-expression
6218
6219    additive-expression:
6220      multiplicative-expression
6221      additive-expression + multiplicative-expression
6222      additive-expression - multiplicative-expression
6223
6224    shift-expression:
6225      additive-expression
6226      shift-expression << additive-expression
6227      shift-expression >> additive-expression
6228
6229    relational-expression:
6230      shift-expression
6231      relational-expression < shift-expression
6232      relational-expression > shift-expression
6233      relational-expression <= shift-expression
6234      relational-expression >= shift-expression
6235
6236   GNU Extension:
6237
6238    relational-expression:
6239      relational-expression <? shift-expression
6240      relational-expression >? shift-expression
6241
6242    equality-expression:
6243      relational-expression
6244      equality-expression == relational-expression
6245      equality-expression != relational-expression
6246
6247    and-expression:
6248      equality-expression
6249      and-expression & equality-expression
6250
6251    exclusive-or-expression:
6252      and-expression
6253      exclusive-or-expression ^ and-expression
6254
6255    inclusive-or-expression:
6256      exclusive-or-expression
6257      inclusive-or-expression | exclusive-or-expression
6258
6259    logical-and-expression:
6260      inclusive-or-expression
6261      logical-and-expression && inclusive-or-expression
6262
6263    logical-or-expression:
6264      logical-and-expression
6265      logical-or-expression || logical-and-expression
6266
6267    All these are implemented with a single function like:
6268
6269    binary-expression:
6270      simple-cast-expression
6271      binary-expression <token> binary-expression
6272
6273    CAST_P is true if this expression is the target of a cast.
6274
6275    The binops_by_token map is used to get the tree codes for each <token> type.
6276    binary-expressions are associated according to a precedence table.  */
6277
6278 #define TOKEN_PRECEDENCE(token)                              \
6279 (((token->type == CPP_GREATER                                \
6280    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6281   && !parser->greater_than_is_operator_p)                    \
6282  ? PREC_NOT_OPERATOR                                         \
6283  : binops_by_token[token->type].prec)
6284
6285 static tree
6286 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6287                              bool no_toplevel_fold_p,
6288                              enum cp_parser_prec prec,
6289                              cp_id_kind * pidk)
6290 {
6291   cp_parser_expression_stack stack;
6292   cp_parser_expression_stack_entry *sp = &stack[0];
6293   tree lhs, rhs;
6294   cp_token *token;
6295   enum tree_code tree_type, lhs_type, rhs_type;
6296   enum cp_parser_prec new_prec, lookahead_prec;
6297   bool overloaded_p;
6298
6299   /* Parse the first expression.  */
6300   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6301   lhs_type = ERROR_MARK;
6302
6303   for (;;)
6304     {
6305       /* Get an operator token.  */
6306       token = cp_lexer_peek_token (parser->lexer);
6307
6308       if (warn_cxx0x_compat
6309           && token->type == CPP_RSHIFT
6310           && !parser->greater_than_is_operator_p)
6311         {
6312           if (warning_at (token->location, OPT_Wc__0x_compat, 
6313                           "%<>>%> operator will be treated as"
6314                           " two right angle brackets in C++0x"))
6315             inform (token->location,
6316                     "suggest parentheses around %<>>%> expression");
6317         }
6318
6319       new_prec = TOKEN_PRECEDENCE (token);
6320
6321       /* Popping an entry off the stack means we completed a subexpression:
6322          - either we found a token which is not an operator (`>' where it is not
6323            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6324            will happen repeatedly;
6325          - or, we found an operator which has lower priority.  This is the case
6326            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6327            parsing `3 * 4'.  */
6328       if (new_prec <= prec)
6329         {
6330           if (sp == stack)
6331             break;
6332           else
6333             goto pop;
6334         }
6335
6336      get_rhs:
6337       tree_type = binops_by_token[token->type].tree_type;
6338
6339       /* We used the operator token.  */
6340       cp_lexer_consume_token (parser->lexer);
6341
6342       /* For "false && x" or "true || x", x will never be executed;
6343          disable warnings while evaluating it.  */
6344       if (tree_type == TRUTH_ANDIF_EXPR)
6345         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6346       else if (tree_type == TRUTH_ORIF_EXPR)
6347         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6348
6349       /* Extract another operand.  It may be the RHS of this expression
6350          or the LHS of a new, higher priority expression.  */
6351       rhs = cp_parser_simple_cast_expression (parser);
6352       rhs_type = ERROR_MARK;
6353
6354       /* Get another operator token.  Look up its precedence to avoid
6355          building a useless (immediately popped) stack entry for common
6356          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6357       token = cp_lexer_peek_token (parser->lexer);
6358       lookahead_prec = TOKEN_PRECEDENCE (token);
6359       if (lookahead_prec > new_prec)
6360         {
6361           /* ... and prepare to parse the RHS of the new, higher priority
6362              expression.  Since precedence levels on the stack are
6363              monotonically increasing, we do not have to care about
6364              stack overflows.  */
6365           sp->prec = prec;
6366           sp->tree_type = tree_type;
6367           sp->lhs = lhs;
6368           sp->lhs_type = lhs_type;
6369           sp++;
6370           lhs = rhs;
6371           lhs_type = rhs_type;
6372           prec = new_prec;
6373           new_prec = lookahead_prec;
6374           goto get_rhs;
6375
6376          pop:
6377           lookahead_prec = new_prec;
6378           /* If the stack is not empty, we have parsed into LHS the right side
6379              (`4' in the example above) of an expression we had suspended.
6380              We can use the information on the stack to recover the LHS (`3')
6381              from the stack together with the tree code (`MULT_EXPR'), and
6382              the precedence of the higher level subexpression
6383              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6384              which will be used to actually build the additive expression.  */
6385           --sp;
6386           prec = sp->prec;
6387           tree_type = sp->tree_type;
6388           rhs = lhs;
6389           rhs_type = lhs_type;
6390           lhs = sp->lhs;
6391           lhs_type = sp->lhs_type;
6392         }
6393
6394       /* Undo the disabling of warnings done above.  */
6395       if (tree_type == TRUTH_ANDIF_EXPR)
6396         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6397       else if (tree_type == TRUTH_ORIF_EXPR)
6398         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6399
6400       overloaded_p = false;
6401       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6402          ERROR_MARK for everything that is not a binary expression.
6403          This makes warn_about_parentheses miss some warnings that
6404          involve unary operators.  For unary expressions we should
6405          pass the correct tree_code unless the unary expression was
6406          surrounded by parentheses.
6407       */
6408       if (no_toplevel_fold_p
6409           && lookahead_prec <= prec
6410           && sp == stack
6411           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6412         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6413       else
6414         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6415                                  &overloaded_p, tf_warning_or_error);
6416       lhs_type = tree_type;
6417
6418       /* If the binary operator required the use of an overloaded operator,
6419          then this expression cannot be an integral constant-expression.
6420          An overloaded operator can be used even if both operands are
6421          otherwise permissible in an integral constant-expression if at
6422          least one of the operands is of enumeration type.  */
6423
6424       if (overloaded_p
6425           && (cp_parser_non_integral_constant_expression
6426               (parser, "calls to overloaded operators")))
6427         return error_mark_node;
6428     }
6429
6430   return lhs;
6431 }
6432
6433
6434 /* Parse the `? expression : assignment-expression' part of a
6435    conditional-expression.  The LOGICAL_OR_EXPR is the
6436    logical-or-expression that started the conditional-expression.
6437    Returns a representation of the entire conditional-expression.
6438
6439    This routine is used by cp_parser_assignment_expression.
6440
6441      ? expression : assignment-expression
6442
6443    GNU Extensions:
6444
6445      ? : assignment-expression */
6446
6447 static tree
6448 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6449 {
6450   tree expr;
6451   tree assignment_expr;
6452
6453   /* Consume the `?' token.  */
6454   cp_lexer_consume_token (parser->lexer);
6455   if (cp_parser_allow_gnu_extensions_p (parser)
6456       && cp_lexer_next_token_is (parser->lexer, CPP_COLON))
6457     {
6458       /* Implicit true clause.  */
6459       expr = NULL_TREE;
6460       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6461     }
6462   else
6463     {
6464       /* Parse the expression.  */
6465       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6466       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6467       c_inhibit_evaluation_warnings +=
6468         ((logical_or_expr == truthvalue_true_node)
6469          - (logical_or_expr == truthvalue_false_node));
6470     }
6471
6472   /* The next token should be a `:'.  */
6473   cp_parser_require (parser, CPP_COLON, "%<:%>");
6474   /* Parse the assignment-expression.  */
6475   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6476   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6477
6478   /* Build the conditional-expression.  */
6479   return build_x_conditional_expr (logical_or_expr,
6480                                    expr,
6481                                    assignment_expr,
6482                                    tf_warning_or_error);
6483 }
6484
6485 /* Parse an assignment-expression.
6486
6487    assignment-expression:
6488      conditional-expression
6489      logical-or-expression assignment-operator assignment_expression
6490      throw-expression
6491
6492    CAST_P is true if this expression is the target of a cast.
6493
6494    Returns a representation for the expression.  */
6495
6496 static tree
6497 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6498                                  cp_id_kind * pidk)
6499 {
6500   tree expr;
6501
6502   /* If the next token is the `throw' keyword, then we're looking at
6503      a throw-expression.  */
6504   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6505     expr = cp_parser_throw_expression (parser);
6506   /* Otherwise, it must be that we are looking at a
6507      logical-or-expression.  */
6508   else
6509     {
6510       /* Parse the binary expressions (logical-or-expression).  */
6511       expr = cp_parser_binary_expression (parser, cast_p, false,
6512                                           PREC_NOT_OPERATOR, pidk);
6513       /* If the next token is a `?' then we're actually looking at a
6514          conditional-expression.  */
6515       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6516         return cp_parser_question_colon_clause (parser, expr);
6517       else
6518         {
6519           enum tree_code assignment_operator;
6520
6521           /* If it's an assignment-operator, we're using the second
6522              production.  */
6523           assignment_operator
6524             = cp_parser_assignment_operator_opt (parser);
6525           if (assignment_operator != ERROR_MARK)
6526             {
6527               bool non_constant_p;
6528
6529               /* Parse the right-hand side of the assignment.  */
6530               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6531
6532               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6533                 maybe_warn_cpp0x ("extended initializer lists");
6534
6535               /* An assignment may not appear in a
6536                  constant-expression.  */
6537               if (cp_parser_non_integral_constant_expression (parser,
6538                                                               "an assignment"))
6539                 return error_mark_node;
6540               /* Build the assignment expression.  */
6541               expr = build_x_modify_expr (expr,
6542                                           assignment_operator,
6543                                           rhs,
6544                                           tf_warning_or_error);
6545             }
6546         }
6547     }
6548
6549   return expr;
6550 }
6551
6552 /* Parse an (optional) assignment-operator.
6553
6554    assignment-operator: one of
6555      = *= /= %= += -= >>= <<= &= ^= |=
6556
6557    GNU Extension:
6558
6559    assignment-operator: one of
6560      <?= >?=
6561
6562    If the next token is an assignment operator, the corresponding tree
6563    code is returned, and the token is consumed.  For example, for
6564    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6565    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6566    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6567    operator, ERROR_MARK is returned.  */
6568
6569 static enum tree_code
6570 cp_parser_assignment_operator_opt (cp_parser* parser)
6571 {
6572   enum tree_code op;
6573   cp_token *token;
6574
6575   /* Peek at the next token.  */
6576   token = cp_lexer_peek_token (parser->lexer);
6577
6578   switch (token->type)
6579     {
6580     case CPP_EQ:
6581       op = NOP_EXPR;
6582       break;
6583
6584     case CPP_MULT_EQ:
6585       op = MULT_EXPR;
6586       break;
6587
6588     case CPP_DIV_EQ:
6589       op = TRUNC_DIV_EXPR;
6590       break;
6591
6592     case CPP_MOD_EQ:
6593       op = TRUNC_MOD_EXPR;
6594       break;
6595
6596     case CPP_PLUS_EQ:
6597       op = PLUS_EXPR;
6598       break;
6599
6600     case CPP_MINUS_EQ:
6601       op = MINUS_EXPR;
6602       break;
6603
6604     case CPP_RSHIFT_EQ:
6605       op = RSHIFT_EXPR;
6606       break;
6607
6608     case CPP_LSHIFT_EQ:
6609       op = LSHIFT_EXPR;
6610       break;
6611
6612     case CPP_AND_EQ:
6613       op = BIT_AND_EXPR;
6614       break;
6615
6616     case CPP_XOR_EQ:
6617       op = BIT_XOR_EXPR;
6618       break;
6619
6620     case CPP_OR_EQ:
6621       op = BIT_IOR_EXPR;
6622       break;
6623
6624     default:
6625       /* Nothing else is an assignment operator.  */
6626       op = ERROR_MARK;
6627     }
6628
6629   /* If it was an assignment operator, consume it.  */
6630   if (op != ERROR_MARK)
6631     cp_lexer_consume_token (parser->lexer);
6632
6633   return op;
6634 }
6635
6636 /* Parse an expression.
6637
6638    expression:
6639      assignment-expression
6640      expression , assignment-expression
6641
6642    CAST_P is true if this expression is the target of a cast.
6643
6644    Returns a representation of the expression.  */
6645
6646 static tree
6647 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6648 {
6649   tree expression = NULL_TREE;
6650
6651   while (true)
6652     {
6653       tree assignment_expression;
6654
6655       /* Parse the next assignment-expression.  */
6656       assignment_expression
6657         = cp_parser_assignment_expression (parser, cast_p, pidk);
6658       /* If this is the first assignment-expression, we can just
6659          save it away.  */
6660       if (!expression)
6661         expression = assignment_expression;
6662       else
6663         expression = build_x_compound_expr (expression,
6664                                             assignment_expression,
6665                                             tf_warning_or_error);
6666       /* If the next token is not a comma, then we are done with the
6667          expression.  */
6668       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6669         break;
6670       /* Consume the `,'.  */
6671       cp_lexer_consume_token (parser->lexer);
6672       /* A comma operator cannot appear in a constant-expression.  */
6673       if (cp_parser_non_integral_constant_expression (parser,
6674                                                       "a comma operator"))
6675         expression = error_mark_node;
6676     }
6677
6678   return expression;
6679 }
6680
6681 /* Parse a constant-expression.
6682
6683    constant-expression:
6684      conditional-expression
6685
6686   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6687   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6688   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6689   is false, NON_CONSTANT_P should be NULL.  */
6690
6691 static tree
6692 cp_parser_constant_expression (cp_parser* parser,
6693                                bool allow_non_constant_p,
6694                                bool *non_constant_p)
6695 {
6696   bool saved_integral_constant_expression_p;
6697   bool saved_allow_non_integral_constant_expression_p;
6698   bool saved_non_integral_constant_expression_p;
6699   tree expression;
6700
6701   /* It might seem that we could simply parse the
6702      conditional-expression, and then check to see if it were
6703      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6704      one that the compiler can figure out is constant, possibly after
6705      doing some simplifications or optimizations.  The standard has a
6706      precise definition of constant-expression, and we must honor
6707      that, even though it is somewhat more restrictive.
6708
6709      For example:
6710
6711        int i[(2, 3)];
6712
6713      is not a legal declaration, because `(2, 3)' is not a
6714      constant-expression.  The `,' operator is forbidden in a
6715      constant-expression.  However, GCC's constant-folding machinery
6716      will fold this operation to an INTEGER_CST for `3'.  */
6717
6718   /* Save the old settings.  */
6719   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
6720   saved_allow_non_integral_constant_expression_p
6721     = parser->allow_non_integral_constant_expression_p;
6722   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
6723   /* We are now parsing a constant-expression.  */
6724   parser->integral_constant_expression_p = true;
6725   parser->allow_non_integral_constant_expression_p = allow_non_constant_p;
6726   parser->non_integral_constant_expression_p = false;
6727   /* Although the grammar says "conditional-expression", we parse an
6728      "assignment-expression", which also permits "throw-expression"
6729      and the use of assignment operators.  In the case that
6730      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
6731      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
6732      actually essential that we look for an assignment-expression.
6733      For example, cp_parser_initializer_clauses uses this function to
6734      determine whether a particular assignment-expression is in fact
6735      constant.  */
6736   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6737   /* Restore the old settings.  */
6738   parser->integral_constant_expression_p
6739     = saved_integral_constant_expression_p;
6740   parser->allow_non_integral_constant_expression_p
6741     = saved_allow_non_integral_constant_expression_p;
6742   if (allow_non_constant_p)
6743     *non_constant_p = parser->non_integral_constant_expression_p;
6744   else if (parser->non_integral_constant_expression_p)
6745     expression = error_mark_node;
6746   parser->non_integral_constant_expression_p
6747     = saved_non_integral_constant_expression_p;
6748
6749   return expression;
6750 }
6751
6752 /* Parse __builtin_offsetof.
6753
6754    offsetof-expression:
6755      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
6756
6757    offsetof-member-designator:
6758      id-expression
6759      | offsetof-member-designator "." id-expression
6760      | offsetof-member-designator "[" expression "]"
6761      | offsetof-member-designator "->" id-expression  */
6762
6763 static tree
6764 cp_parser_builtin_offsetof (cp_parser *parser)
6765 {
6766   int save_ice_p, save_non_ice_p;
6767   tree type, expr;
6768   cp_id_kind dummy;
6769   cp_token *token;
6770
6771   /* We're about to accept non-integral-constant things, but will
6772      definitely yield an integral constant expression.  Save and
6773      restore these values around our local parsing.  */
6774   save_ice_p = parser->integral_constant_expression_p;
6775   save_non_ice_p = parser->non_integral_constant_expression_p;
6776
6777   /* Consume the "__builtin_offsetof" token.  */
6778   cp_lexer_consume_token (parser->lexer);
6779   /* Consume the opening `('.  */
6780   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6781   /* Parse the type-id.  */
6782   type = cp_parser_type_id (parser);
6783   /* Look for the `,'.  */
6784   cp_parser_require (parser, CPP_COMMA, "%<,%>");
6785   token = cp_lexer_peek_token (parser->lexer);
6786
6787   /* Build the (type *)null that begins the traditional offsetof macro.  */
6788   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
6789                             tf_warning_or_error);
6790
6791   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
6792   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
6793                                                  true, &dummy, token->location);
6794   while (true)
6795     {
6796       token = cp_lexer_peek_token (parser->lexer);
6797       switch (token->type)
6798         {
6799         case CPP_OPEN_SQUARE:
6800           /* offsetof-member-designator "[" expression "]" */
6801           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
6802           break;
6803
6804         case CPP_DEREF:
6805           /* offsetof-member-designator "->" identifier */
6806           expr = grok_array_decl (expr, integer_zero_node);
6807           /* FALLTHRU */
6808
6809         case CPP_DOT:
6810           /* offsetof-member-designator "." identifier */
6811           cp_lexer_consume_token (parser->lexer);
6812           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
6813                                                          expr, true, &dummy,
6814                                                          token->location);
6815           break;
6816
6817         case CPP_CLOSE_PAREN:
6818           /* Consume the ")" token.  */
6819           cp_lexer_consume_token (parser->lexer);
6820           goto success;
6821
6822         default:
6823           /* Error.  We know the following require will fail, but
6824              that gives the proper error message.  */
6825           cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6826           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
6827           expr = error_mark_node;
6828           goto failure;
6829         }
6830     }
6831
6832  success:
6833   /* If we're processing a template, we can't finish the semantics yet.
6834      Otherwise we can fold the entire expression now.  */
6835   if (processing_template_decl)
6836     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
6837   else
6838     expr = finish_offsetof (expr);
6839
6840  failure:
6841   parser->integral_constant_expression_p = save_ice_p;
6842   parser->non_integral_constant_expression_p = save_non_ice_p;
6843
6844   return expr;
6845 }
6846
6847 /* Parse a trait expression.  */
6848
6849 static tree
6850 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
6851 {
6852   cp_trait_kind kind;
6853   tree type1, type2 = NULL_TREE;
6854   bool binary = false;
6855   cp_decl_specifier_seq decl_specs;
6856
6857   switch (keyword)
6858     {
6859     case RID_HAS_NOTHROW_ASSIGN:
6860       kind = CPTK_HAS_NOTHROW_ASSIGN;
6861       break;
6862     case RID_HAS_NOTHROW_CONSTRUCTOR:
6863       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
6864       break;
6865     case RID_HAS_NOTHROW_COPY:
6866       kind = CPTK_HAS_NOTHROW_COPY;
6867       break;
6868     case RID_HAS_TRIVIAL_ASSIGN:
6869       kind = CPTK_HAS_TRIVIAL_ASSIGN;
6870       break;
6871     case RID_HAS_TRIVIAL_CONSTRUCTOR:
6872       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
6873       break;
6874     case RID_HAS_TRIVIAL_COPY:
6875       kind = CPTK_HAS_TRIVIAL_COPY;
6876       break;
6877     case RID_HAS_TRIVIAL_DESTRUCTOR:
6878       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
6879       break;
6880     case RID_HAS_VIRTUAL_DESTRUCTOR:
6881       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
6882       break;
6883     case RID_IS_ABSTRACT:
6884       kind = CPTK_IS_ABSTRACT;
6885       break;
6886     case RID_IS_BASE_OF:
6887       kind = CPTK_IS_BASE_OF;
6888       binary = true;
6889       break;
6890     case RID_IS_CLASS:
6891       kind = CPTK_IS_CLASS;
6892       break;
6893     case RID_IS_CONVERTIBLE_TO:
6894       kind = CPTK_IS_CONVERTIBLE_TO;
6895       binary = true;
6896       break;
6897     case RID_IS_EMPTY:
6898       kind = CPTK_IS_EMPTY;
6899       break;
6900     case RID_IS_ENUM:
6901       kind = CPTK_IS_ENUM;
6902       break;
6903     case RID_IS_POD:
6904       kind = CPTK_IS_POD;
6905       break;
6906     case RID_IS_POLYMORPHIC:
6907       kind = CPTK_IS_POLYMORPHIC;
6908       break;
6909     case RID_IS_STD_LAYOUT:
6910       kind = CPTK_IS_STD_LAYOUT;
6911       break;
6912     case RID_IS_TRIVIAL:
6913       kind = CPTK_IS_TRIVIAL;
6914       break;
6915     case RID_IS_UNION:
6916       kind = CPTK_IS_UNION;
6917       break;
6918     default:
6919       gcc_unreachable ();
6920     }
6921
6922   /* Consume the token.  */
6923   cp_lexer_consume_token (parser->lexer);
6924
6925   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
6926
6927   type1 = cp_parser_type_id (parser);
6928
6929   if (type1 == error_mark_node)
6930     return error_mark_node;
6931
6932   /* Build a trivial decl-specifier-seq.  */
6933   clear_decl_specs (&decl_specs);
6934   decl_specs.type = type1;
6935
6936   /* Call grokdeclarator to figure out what type this is.  */
6937   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6938                           /*initialized=*/0, /*attrlist=*/NULL);
6939
6940   if (binary)
6941     {
6942       cp_parser_require (parser, CPP_COMMA, "%<,%>");
6943  
6944       type2 = cp_parser_type_id (parser);
6945
6946       if (type2 == error_mark_node)
6947         return error_mark_node;
6948
6949       /* Build a trivial decl-specifier-seq.  */
6950       clear_decl_specs (&decl_specs);
6951       decl_specs.type = type2;
6952
6953       /* Call grokdeclarator to figure out what type this is.  */
6954       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
6955                               /*initialized=*/0, /*attrlist=*/NULL);
6956     }
6957
6958   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
6959
6960   /* Complete the trait expression, which may mean either processing
6961      the trait expr now or saving it for template instantiation.  */
6962   return finish_trait_expr (kind, type1, type2);
6963 }
6964
6965 /* Lambdas that appear in variable initializer or default argument scope
6966    get that in their mangling, so we need to record it.  We might as well
6967    use the count for function and namespace scopes as well.  */
6968 static GTY(()) tree lambda_scope;
6969 static GTY(()) int lambda_count;
6970 typedef struct GTY(()) tree_int
6971 {
6972   tree t;
6973   int i;
6974 } tree_int;
6975 DEF_VEC_O(tree_int);
6976 DEF_VEC_ALLOC_O(tree_int,gc);
6977 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
6978
6979 static void
6980 start_lambda_scope (tree decl)
6981 {
6982   tree_int ti;
6983   gcc_assert (decl);
6984   /* Once we're inside a function, we ignore other scopes and just push
6985      the function again so that popping works properly.  */
6986   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
6987     decl = current_function_decl;
6988   ti.t = lambda_scope;
6989   ti.i = lambda_count;
6990   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
6991   if (lambda_scope != decl)
6992     {
6993       /* Don't reset the count if we're still in the same function.  */
6994       lambda_scope = decl;
6995       lambda_count = 0;
6996     }
6997 }
6998
6999 static void
7000 record_lambda_scope (tree lambda)
7001 {
7002   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7003   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7004 }
7005
7006 static void
7007 finish_lambda_scope (void)
7008 {
7009   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7010   if (lambda_scope != p->t)
7011     {
7012       lambda_scope = p->t;
7013       lambda_count = p->i;
7014     }
7015   VEC_pop (tree_int, lambda_scope_stack);
7016 }
7017
7018 /* Parse a lambda expression.
7019
7020    lambda-expression:
7021      lambda-introducer lambda-declarator [opt] compound-statement
7022
7023    Returns a representation of the expression.  */
7024
7025 static tree
7026 cp_parser_lambda_expression (cp_parser* parser)
7027 {
7028   tree lambda_expr = build_lambda_expr ();
7029   tree type;
7030
7031   LAMBDA_EXPR_LOCATION (lambda_expr)
7032     = cp_lexer_peek_token (parser->lexer)->location;
7033
7034   /* We may be in the middle of deferred access check.  Disable
7035      it now.  */
7036   push_deferring_access_checks (dk_no_deferred);
7037
7038   type = begin_lambda_type (lambda_expr);
7039
7040   record_lambda_scope (lambda_expr);
7041
7042   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7043   determine_visibility (TYPE_NAME (type));
7044
7045   {
7046     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7047     unsigned int saved_num_template_parameter_lists
7048         = parser->num_template_parameter_lists;
7049
7050     parser->num_template_parameter_lists = 0;
7051
7052     cp_parser_lambda_introducer (parser, lambda_expr);
7053
7054     /* By virtue of defining a local class, a lambda expression has access to
7055        the private variables of enclosing classes.  */
7056
7057     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7058
7059     cp_parser_lambda_body (parser, lambda_expr);
7060
7061     /* The capture list was built up in reverse order; fix that now.  */
7062     {
7063       tree newlist = NULL_TREE;
7064       tree elt, next;
7065
7066       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7067            elt; elt = next)
7068         {
7069           tree field = TREE_PURPOSE (elt);
7070           char *buf;
7071
7072           next = TREE_CHAIN (elt);
7073           TREE_CHAIN (elt) = newlist;
7074           newlist = elt;
7075
7076           /* Also add __ to the beginning of the field name so that code
7077              outside the lambda body can't see the captured name.  We could
7078              just remove the name entirely, but this is more useful for
7079              debugging.  */
7080           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7081             /* The 'this' capture already starts with __.  */
7082             continue;
7083
7084           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7085           buf[1] = buf[0] = '_';
7086           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7087                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7088           DECL_NAME (field) = get_identifier (buf);
7089         }
7090       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7091     }
7092
7093     type = finish_struct (type, /*attributes=*/NULL_TREE);
7094
7095     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7096   }
7097
7098   pop_deferring_access_checks ();
7099
7100   return build_lambda_object (lambda_expr);
7101 }
7102
7103 /* Parse the beginning of a lambda expression.
7104
7105    lambda-introducer:
7106      [ lambda-capture [opt] ]
7107
7108    LAMBDA_EXPR is the current representation of the lambda expression.  */
7109
7110 static void
7111 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7112 {
7113   /* Need commas after the first capture.  */
7114   bool first = true;
7115
7116   /* Eat the leading `['.  */
7117   cp_parser_require (parser, CPP_OPEN_SQUARE, "%<[%>");
7118
7119   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7120   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7121       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7122     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7123   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7124     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7125
7126   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7127     {
7128       cp_lexer_consume_token (parser->lexer);
7129       first = false;
7130     }
7131
7132   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7133     {
7134       cp_token* capture_token;
7135       tree capture_id;
7136       tree capture_init_expr;
7137       cp_id_kind idk = CP_ID_KIND_NONE;
7138       bool explicit_init_p = false;
7139
7140       enum capture_kind_type
7141       {
7142         BY_COPY,
7143         BY_REFERENCE
7144       };
7145       enum capture_kind_type capture_kind = BY_COPY;
7146
7147       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7148         {
7149           error ("expected end of capture-list");
7150           return;
7151         }
7152
7153       if (first)
7154         first = false;
7155       else
7156         cp_parser_require (parser, CPP_COMMA, "%<,%>");
7157
7158       /* Possibly capture `this'.  */
7159       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7160         {
7161           cp_lexer_consume_token (parser->lexer);
7162           add_capture (lambda_expr,
7163                        /*id=*/get_identifier ("__this"),
7164                        /*initializer=*/finish_this_expr(),
7165                        /*by_reference_p=*/false,
7166                        explicit_init_p);
7167           continue;
7168         }
7169
7170       /* Remember whether we want to capture as a reference or not.  */
7171       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7172         {
7173           capture_kind = BY_REFERENCE;
7174           cp_lexer_consume_token (parser->lexer);
7175         }
7176
7177       /* Get the identifier.  */
7178       capture_token = cp_lexer_peek_token (parser->lexer);
7179       capture_id = cp_parser_identifier (parser);
7180
7181       if (capture_id == error_mark_node)
7182         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7183            delimiters, but I modified this to stop on unnested ']' as well.  It
7184            was already changed to stop on unnested '}', so the
7185            "closing_parenthesis" name is no more misleading with my change.  */
7186         {
7187           cp_parser_skip_to_closing_parenthesis (parser,
7188                                                  /*recovering=*/true,
7189                                                  /*or_comma=*/true,
7190                                                  /*consume_paren=*/true);
7191           break;
7192         }
7193
7194       /* Find the initializer for this capture.  */
7195       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7196         {
7197           /* An explicit expression exists.  */
7198           cp_lexer_consume_token (parser->lexer);
7199           pedwarn (input_location, OPT_pedantic,
7200                    "ISO C++ does not allow initializers "
7201                    "in lambda expression capture lists");
7202           capture_init_expr = cp_parser_assignment_expression (parser,
7203                                                                /*cast_p=*/true,
7204                                                                &idk);
7205           explicit_init_p = true;
7206         }
7207       else
7208         {
7209           const char* error_msg;
7210
7211           /* Turn the identifier into an id-expression.  */
7212           capture_init_expr
7213             = cp_parser_lookup_name
7214                 (parser,
7215                  capture_id,
7216                  none_type,
7217                  /*is_template=*/false,
7218                  /*is_namespace=*/false,
7219                  /*check_dependency=*/true,
7220                  /*ambiguous_decls=*/NULL,
7221                  capture_token->location);
7222
7223           capture_init_expr
7224             = finish_id_expression
7225                 (capture_id,
7226                  capture_init_expr,
7227                  parser->scope,
7228                  &idk,
7229                  /*integral_constant_expression_p=*/false,
7230                  /*allow_non_integral_constant_expression_p=*/false,
7231                  /*non_integral_constant_expression_p=*/NULL,
7232                  /*template_p=*/false,
7233                  /*done=*/true,
7234                  /*address_p=*/false,
7235                  /*template_arg_p=*/false,
7236                  &error_msg,
7237                  capture_token->location);
7238         }
7239
7240       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7241         capture_init_expr
7242           = unqualified_name_lookup_error (capture_init_expr);
7243
7244       add_capture (lambda_expr,
7245                    capture_id,
7246                    capture_init_expr,
7247                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7248                    explicit_init_p);
7249     }
7250
7251   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
7252 }
7253
7254 /* Parse the (optional) middle of a lambda expression.
7255
7256    lambda-declarator:
7257      ( parameter-declaration-clause [opt] )
7258        attribute-specifier [opt]
7259        mutable [opt]
7260        exception-specification [opt]
7261        lambda-return-type-clause [opt]
7262
7263    LAMBDA_EXPR is the current representation of the lambda expression.  */
7264
7265 static void
7266 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7267 {
7268   /* 5.1.1.4 of the standard says:
7269        If a lambda-expression does not include a lambda-declarator, it is as if
7270        the lambda-declarator were ().
7271      This means an empty parameter list, no attributes, and no exception
7272      specification.  */
7273   tree param_list = void_list_node;
7274   tree attributes = NULL_TREE;
7275   tree exception_spec = NULL_TREE;
7276   tree t;
7277
7278   /* The lambda-declarator is optional, but must begin with an opening
7279      parenthesis if present.  */
7280   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7281     {
7282       cp_lexer_consume_token (parser->lexer);
7283
7284       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7285
7286       /* Parse parameters.  */
7287       param_list = cp_parser_parameter_declaration_clause (parser);
7288
7289       /* Default arguments shall not be specified in the
7290          parameter-declaration-clause of a lambda-declarator.  */
7291       for (t = param_list; t; t = TREE_CHAIN (t))
7292         if (TREE_PURPOSE (t))
7293           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7294                    "default argument specified for lambda parameter");
7295
7296       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
7297
7298       attributes = cp_parser_attributes_opt (parser);
7299
7300       /* Parse optional `mutable' keyword.  */
7301       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7302         {
7303           cp_lexer_consume_token (parser->lexer);
7304           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7305         }
7306
7307       /* Parse optional exception specification.  */
7308       exception_spec = cp_parser_exception_specification_opt (parser);
7309
7310       /* Parse optional trailing return type.  */
7311       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7312         {
7313           cp_lexer_consume_token (parser->lexer);
7314           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7315         }
7316
7317       /* The function parameters must be in scope all the way until after the
7318          trailing-return-type in case of decltype.  */
7319       for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
7320         pop_binding (DECL_NAME (t), t);
7321
7322       leave_scope ();
7323     }
7324
7325   /* Create the function call operator.
7326
7327      Messing with declarators like this is no uglier than building up the
7328      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7329      other code.  */
7330   {
7331     cp_decl_specifier_seq return_type_specs;
7332     cp_declarator* declarator;
7333     tree fco;
7334     int quals;
7335     void *p;
7336
7337     clear_decl_specs (&return_type_specs);
7338     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7339       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7340     else
7341       /* Maybe we will deduce the return type later, but we can use void
7342          as a placeholder return type anyways.  */
7343       return_type_specs.type = void_type_node;
7344
7345     p = obstack_alloc (&declarator_obstack, 0);
7346
7347     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7348                                      sfk_none);
7349
7350     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7351              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7352     declarator = make_call_declarator (declarator, param_list, quals,
7353                                        exception_spec,
7354                                        /*late_return_type=*/NULL_TREE);
7355
7356     fco = grokmethod (&return_type_specs,
7357                         declarator,
7358                         attributes);
7359     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7360     DECL_ARTIFICIAL (fco) = 1;
7361
7362     finish_member_declaration (fco);
7363
7364     obstack_free (&declarator_obstack, p);
7365   }
7366 }
7367
7368 /* Parse the body of a lambda expression, which is simply
7369
7370    compound-statement
7371
7372    but which requires special handling.
7373    LAMBDA_EXPR is the current representation of the lambda expression.  */
7374
7375 static void
7376 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7377 {
7378   bool nested = (current_function_decl != NULL_TREE);
7379   if (nested)
7380     push_function_context ();
7381
7382   /* Finish the function call operator
7383      - class_specifier
7384      + late_parsing_for_member
7385      + function_definition_after_declarator
7386      + ctor_initializer_opt_and_function_body  */
7387   {
7388     tree fco = lambda_function (lambda_expr);
7389     tree body;
7390     bool done = false;
7391
7392     /* Let the front end know that we are going to be defining this
7393        function.  */
7394     start_preparsed_function (fco,
7395                               NULL_TREE,
7396                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7397
7398     start_lambda_scope (fco);
7399     body = begin_function_body ();
7400
7401     /* 5.1.1.4 of the standard says:
7402          If a lambda-expression does not include a trailing-return-type, it
7403          is as if the trailing-return-type denotes the following type:
7404           * if the compound-statement is of the form
7405                { return attribute-specifier [opt] expression ; }
7406              the type of the returned expression after lvalue-to-rvalue
7407              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7408              (_conv.array_ 4.2), and function-to-pointer conversion
7409              (_conv.func_ 4.3);
7410           * otherwise, void.  */
7411
7412     /* In a lambda that has neither a lambda-return-type-clause
7413        nor a deducible form, errors should be reported for return statements
7414        in the body.  Since we used void as the placeholder return type, parsing
7415        the body as usual will give such desired behavior.  */
7416     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7417         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7418         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7419         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7420       {
7421         tree compound_stmt;
7422         tree expr = NULL_TREE;
7423         cp_id_kind idk = CP_ID_KIND_NONE;
7424
7425         /* Parse tentatively in case there's more after the initial return
7426            statement.  */
7427         cp_parser_parse_tentatively (parser);
7428
7429         cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
7430         cp_parser_require_keyword (parser, RID_RETURN, "%<return%>");
7431
7432         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7433
7434         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
7435         cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7436
7437         if (cp_parser_parse_definitely (parser))
7438           {
7439             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7440
7441             compound_stmt = begin_compound_stmt (0);
7442             /* Will get error here if type not deduced yet.  */
7443             finish_return_stmt (expr);
7444             finish_compound_stmt (compound_stmt);
7445
7446             done = true;
7447           }
7448       }
7449
7450     if (!done)
7451       {
7452         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7453           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7454         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7455            cp_parser_compound_stmt does not pass it.  */
7456         cp_parser_function_body (parser);
7457         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7458       }
7459
7460     finish_function_body (body);
7461     finish_lambda_scope ();
7462
7463     /* Finish the function and generate code for it if necessary.  */
7464     expand_or_defer_fn (finish_function (/*inline*/2));
7465   }
7466
7467   if (nested)
7468     pop_function_context();
7469 }
7470
7471 /* Statements [gram.stmt.stmt]  */
7472
7473 /* Parse a statement.
7474
7475    statement:
7476      labeled-statement
7477      expression-statement
7478      compound-statement
7479      selection-statement
7480      iteration-statement
7481      jump-statement
7482      declaration-statement
7483      try-block
7484
7485   IN_COMPOUND is true when the statement is nested inside a
7486   cp_parser_compound_statement; this matters for certain pragmas.
7487
7488   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7489   is a (possibly labeled) if statement which is not enclosed in braces
7490   and has an else clause.  This is used to implement -Wparentheses.  */
7491
7492 static void
7493 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7494                      bool in_compound, bool *if_p)
7495 {
7496   tree statement;
7497   cp_token *token;
7498   location_t statement_location;
7499
7500  restart:
7501   if (if_p != NULL)
7502     *if_p = false;
7503   /* There is no statement yet.  */
7504   statement = NULL_TREE;
7505   /* Peek at the next token.  */
7506   token = cp_lexer_peek_token (parser->lexer);
7507   /* Remember the location of the first token in the statement.  */
7508   statement_location = token->location;
7509   /* If this is a keyword, then that will often determine what kind of
7510      statement we have.  */
7511   if (token->type == CPP_KEYWORD)
7512     {
7513       enum rid keyword = token->keyword;
7514
7515       switch (keyword)
7516         {
7517         case RID_CASE:
7518         case RID_DEFAULT:
7519           /* Looks like a labeled-statement with a case label.
7520              Parse the label, and then use tail recursion to parse
7521              the statement.  */
7522           cp_parser_label_for_labeled_statement (parser);
7523           goto restart;
7524
7525         case RID_IF:
7526         case RID_SWITCH:
7527           statement = cp_parser_selection_statement (parser, if_p);
7528           break;
7529
7530         case RID_WHILE:
7531         case RID_DO:
7532         case RID_FOR:
7533           statement = cp_parser_iteration_statement (parser);
7534           break;
7535
7536         case RID_BREAK:
7537         case RID_CONTINUE:
7538         case RID_RETURN:
7539         case RID_GOTO:
7540           statement = cp_parser_jump_statement (parser);
7541           break;
7542
7543           /* Objective-C++ exception-handling constructs.  */
7544         case RID_AT_TRY:
7545         case RID_AT_CATCH:
7546         case RID_AT_FINALLY:
7547         case RID_AT_SYNCHRONIZED:
7548         case RID_AT_THROW:
7549           statement = cp_parser_objc_statement (parser);
7550           break;
7551
7552         case RID_TRY:
7553           statement = cp_parser_try_block (parser);
7554           break;
7555
7556         case RID_NAMESPACE:
7557           /* This must be a namespace alias definition.  */
7558           cp_parser_declaration_statement (parser);
7559           return;
7560           
7561         default:
7562           /* It might be a keyword like `int' that can start a
7563              declaration-statement.  */
7564           break;
7565         }
7566     }
7567   else if (token->type == CPP_NAME)
7568     {
7569       /* If the next token is a `:', then we are looking at a
7570          labeled-statement.  */
7571       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7572       if (token->type == CPP_COLON)
7573         {
7574           /* Looks like a labeled-statement with an ordinary 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     }
7581   /* Anything that starts with a `{' must be a compound-statement.  */
7582   else if (token->type == CPP_OPEN_BRACE)
7583     statement = cp_parser_compound_statement (parser, NULL, false);
7584   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7585      a statement all its own.  */
7586   else if (token->type == CPP_PRAGMA)
7587     {
7588       /* Only certain OpenMP pragmas are attached to statements, and thus
7589          are considered statements themselves.  All others are not.  In
7590          the context of a compound, accept the pragma as a "statement" and
7591          return so that we can check for a close brace.  Otherwise we
7592          require a real statement and must go back and read one.  */
7593       if (in_compound)
7594         cp_parser_pragma (parser, pragma_compound);
7595       else if (!cp_parser_pragma (parser, pragma_stmt))
7596         goto restart;
7597       return;
7598     }
7599   else if (token->type == CPP_EOF)
7600     {
7601       cp_parser_error (parser, "expected statement");
7602       return;
7603     }
7604
7605   /* Everything else must be a declaration-statement or an
7606      expression-statement.  Try for the declaration-statement
7607      first, unless we are looking at a `;', in which case we know that
7608      we have an expression-statement.  */
7609   if (!statement)
7610     {
7611       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7612         {
7613           cp_parser_parse_tentatively (parser);
7614           /* Try to parse the declaration-statement.  */
7615           cp_parser_declaration_statement (parser);
7616           /* If that worked, we're done.  */
7617           if (cp_parser_parse_definitely (parser))
7618             return;
7619         }
7620       /* Look for an expression-statement instead.  */
7621       statement = cp_parser_expression_statement (parser, in_statement_expr);
7622     }
7623
7624   /* Set the line number for the statement.  */
7625   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7626     SET_EXPR_LOCATION (statement, statement_location);
7627 }
7628
7629 /* Parse the label for a labeled-statement, i.e.
7630
7631    identifier :
7632    case constant-expression :
7633    default :
7634
7635    GNU Extension:
7636    case constant-expression ... constant-expression : statement
7637
7638    When a label is parsed without errors, the label is added to the
7639    parse tree by the finish_* functions, so this function doesn't
7640    have to return the label.  */
7641
7642 static void
7643 cp_parser_label_for_labeled_statement (cp_parser* parser)
7644 {
7645   cp_token *token;
7646   tree label = NULL_TREE;
7647
7648   /* The next token should be an identifier.  */
7649   token = cp_lexer_peek_token (parser->lexer);
7650   if (token->type != CPP_NAME
7651       && token->type != CPP_KEYWORD)
7652     {
7653       cp_parser_error (parser, "expected labeled-statement");
7654       return;
7655     }
7656
7657   switch (token->keyword)
7658     {
7659     case RID_CASE:
7660       {
7661         tree expr, expr_hi;
7662         cp_token *ellipsis;
7663
7664         /* Consume the `case' token.  */
7665         cp_lexer_consume_token (parser->lexer);
7666         /* Parse the constant-expression.  */
7667         expr = cp_parser_constant_expression (parser,
7668                                               /*allow_non_constant_p=*/false,
7669                                               NULL);
7670
7671         ellipsis = cp_lexer_peek_token (parser->lexer);
7672         if (ellipsis->type == CPP_ELLIPSIS)
7673           {
7674             /* Consume the `...' token.  */
7675             cp_lexer_consume_token (parser->lexer);
7676             expr_hi =
7677               cp_parser_constant_expression (parser,
7678                                              /*allow_non_constant_p=*/false,
7679                                              NULL);
7680             /* We don't need to emit warnings here, as the common code
7681                will do this for us.  */
7682           }
7683         else
7684           expr_hi = NULL_TREE;
7685
7686         if (parser->in_switch_statement_p)
7687           finish_case_label (token->location, expr, expr_hi);
7688         else
7689           error_at (token->location,
7690                     "case label %qE not within a switch statement",
7691                     expr);
7692       }
7693       break;
7694
7695     case RID_DEFAULT:
7696       /* Consume the `default' token.  */
7697       cp_lexer_consume_token (parser->lexer);
7698
7699       if (parser->in_switch_statement_p)
7700         finish_case_label (token->location, NULL_TREE, NULL_TREE);
7701       else
7702         error_at (token->location, "case label not within a switch statement");
7703       break;
7704
7705     default:
7706       /* Anything else must be an ordinary label.  */
7707       label = finish_label_stmt (cp_parser_identifier (parser));
7708       break;
7709     }
7710
7711   /* Require the `:' token.  */
7712   cp_parser_require (parser, CPP_COLON, "%<:%>");
7713
7714   /* An ordinary label may optionally be followed by attributes.
7715      However, this is only permitted if the attributes are then
7716      followed by a semicolon.  This is because, for backward
7717      compatibility, when parsing
7718        lab: __attribute__ ((unused)) int i;
7719      we want the attribute to attach to "i", not "lab".  */
7720   if (label != NULL_TREE
7721       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
7722     {
7723       tree attrs;
7724
7725       cp_parser_parse_tentatively (parser);
7726       attrs = cp_parser_attributes_opt (parser);
7727       if (attrs == NULL_TREE
7728           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7729         cp_parser_abort_tentative_parse (parser);
7730       else if (!cp_parser_parse_definitely (parser))
7731         ;
7732       else
7733         cplus_decl_attributes (&label, attrs, 0);
7734     }
7735 }
7736
7737 /* Parse an expression-statement.
7738
7739    expression-statement:
7740      expression [opt] ;
7741
7742    Returns the new EXPR_STMT -- or NULL_TREE if the expression
7743    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
7744    indicates whether this expression-statement is part of an
7745    expression statement.  */
7746
7747 static tree
7748 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
7749 {
7750   tree statement = NULL_TREE;
7751
7752   /* If the next token is a ';', then there is no expression
7753      statement.  */
7754   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7755     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7756
7757   /* Consume the final `;'.  */
7758   cp_parser_consume_semicolon_at_end_of_statement (parser);
7759
7760   if (in_statement_expr
7761       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
7762     /* This is the final expression statement of a statement
7763        expression.  */
7764     statement = finish_stmt_expr_expr (statement, in_statement_expr);
7765   else if (statement)
7766     statement = finish_expr_stmt (statement);
7767   else
7768     finish_stmt ();
7769
7770   return statement;
7771 }
7772
7773 /* Parse a compound-statement.
7774
7775    compound-statement:
7776      { statement-seq [opt] }
7777
7778    GNU extension:
7779
7780    compound-statement:
7781      { label-declaration-seq [opt] statement-seq [opt] }
7782
7783    label-declaration-seq:
7784      label-declaration
7785      label-declaration-seq label-declaration
7786
7787    Returns a tree representing the statement.  */
7788
7789 static tree
7790 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
7791                               bool in_try)
7792 {
7793   tree compound_stmt;
7794
7795   /* Consume the `{'.  */
7796   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
7797     return error_mark_node;
7798   /* Begin the compound-statement.  */
7799   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
7800   /* If the next keyword is `__label__' we have a label declaration.  */
7801   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7802     cp_parser_label_declaration (parser);
7803   /* Parse an (optional) statement-seq.  */
7804   cp_parser_statement_seq_opt (parser, in_statement_expr);
7805   /* Finish the compound-statement.  */
7806   finish_compound_stmt (compound_stmt);
7807   /* Consume the `}'.  */
7808   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
7809
7810   return compound_stmt;
7811 }
7812
7813 /* Parse an (optional) statement-seq.
7814
7815    statement-seq:
7816      statement
7817      statement-seq [opt] statement  */
7818
7819 static void
7820 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
7821 {
7822   /* Scan statements until there aren't any more.  */
7823   while (true)
7824     {
7825       cp_token *token = cp_lexer_peek_token (parser->lexer);
7826
7827       /* If we're looking at a `}', then we've run out of statements.  */
7828       if (token->type == CPP_CLOSE_BRACE
7829           || token->type == CPP_EOF
7830           || token->type == CPP_PRAGMA_EOL)
7831         break;
7832       
7833       /* If we are in a compound statement and find 'else' then
7834          something went wrong.  */
7835       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
7836         {
7837           if (parser->in_statement & IN_IF_STMT) 
7838             break;
7839           else
7840             {
7841               token = cp_lexer_consume_token (parser->lexer);
7842               error_at (token->location, "%<else%> without a previous %<if%>");
7843             }
7844         }
7845
7846       /* Parse the statement.  */
7847       cp_parser_statement (parser, in_statement_expr, true, NULL);
7848     }
7849 }
7850
7851 /* Parse a selection-statement.
7852
7853    selection-statement:
7854      if ( condition ) statement
7855      if ( condition ) statement else statement
7856      switch ( condition ) statement
7857
7858    Returns the new IF_STMT or SWITCH_STMT.
7859
7860    If IF_P is not NULL, *IF_P is set to indicate whether the statement
7861    is a (possibly labeled) if statement which is not enclosed in
7862    braces and has an else clause.  This is used to implement
7863    -Wparentheses.  */
7864
7865 static tree
7866 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
7867 {
7868   cp_token *token;
7869   enum rid keyword;
7870
7871   if (if_p != NULL)
7872     *if_p = false;
7873
7874   /* Peek at the next token.  */
7875   token = cp_parser_require (parser, CPP_KEYWORD, "selection-statement");
7876
7877   /* See what kind of keyword it is.  */
7878   keyword = token->keyword;
7879   switch (keyword)
7880     {
7881     case RID_IF:
7882     case RID_SWITCH:
7883       {
7884         tree statement;
7885         tree condition;
7886
7887         /* Look for the `('.  */
7888         if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
7889           {
7890             cp_parser_skip_to_end_of_statement (parser);
7891             return error_mark_node;
7892           }
7893
7894         /* Begin the selection-statement.  */
7895         if (keyword == RID_IF)
7896           statement = begin_if_stmt ();
7897         else
7898           statement = begin_switch_stmt ();
7899
7900         /* Parse the condition.  */
7901         condition = cp_parser_condition (parser);
7902         /* Look for the `)'.  */
7903         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
7904           cp_parser_skip_to_closing_parenthesis (parser, true, false,
7905                                                  /*consume_paren=*/true);
7906
7907         if (keyword == RID_IF)
7908           {
7909             bool nested_if;
7910             unsigned char in_statement;
7911
7912             /* Add the condition.  */
7913             finish_if_stmt_cond (condition, statement);
7914
7915             /* Parse the then-clause.  */
7916             in_statement = parser->in_statement;
7917             parser->in_statement |= IN_IF_STMT;
7918             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7919               {
7920                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7921                 add_stmt (build_empty_stmt (loc));
7922                 cp_lexer_consume_token (parser->lexer);
7923                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
7924                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
7925                               "empty body in an %<if%> statement");
7926                 nested_if = false;
7927               }
7928             else
7929               cp_parser_implicitly_scoped_statement (parser, &nested_if);
7930             parser->in_statement = in_statement;
7931
7932             finish_then_clause (statement);
7933
7934             /* If the next token is `else', parse the else-clause.  */
7935             if (cp_lexer_next_token_is_keyword (parser->lexer,
7936                                                 RID_ELSE))
7937               {
7938                 /* Consume the `else' keyword.  */
7939                 cp_lexer_consume_token (parser->lexer);
7940                 begin_else_clause (statement);
7941                 /* Parse the else-clause.  */
7942                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
7943                   {
7944                     location_t loc;
7945                     loc = cp_lexer_peek_token (parser->lexer)->location;
7946                     warning_at (loc,
7947                                 OPT_Wempty_body, "suggest braces around "
7948                                 "empty body in an %<else%> statement");
7949                     add_stmt (build_empty_stmt (loc));
7950                     cp_lexer_consume_token (parser->lexer);
7951                   }
7952                 else
7953                   cp_parser_implicitly_scoped_statement (parser, NULL);
7954
7955                 finish_else_clause (statement);
7956
7957                 /* If we are currently parsing a then-clause, then
7958                    IF_P will not be NULL.  We set it to true to
7959                    indicate that this if statement has an else clause.
7960                    This may trigger the Wparentheses warning below
7961                    when we get back up to the parent if statement.  */
7962                 if (if_p != NULL)
7963                   *if_p = true;
7964               }
7965             else
7966               {
7967                 /* This if statement does not have an else clause.  If
7968                    NESTED_IF is true, then the then-clause is an if
7969                    statement which does have an else clause.  We warn
7970                    about the potential ambiguity.  */
7971                 if (nested_if)
7972                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
7973                               "suggest explicit braces to avoid ambiguous"
7974                               " %<else%>");
7975               }
7976
7977             /* Now we're all done with the if-statement.  */
7978             finish_if_stmt (statement);
7979           }
7980         else
7981           {
7982             bool in_switch_statement_p;
7983             unsigned char in_statement;
7984
7985             /* Add the condition.  */
7986             finish_switch_cond (condition, statement);
7987
7988             /* Parse the body of the switch-statement.  */
7989             in_switch_statement_p = parser->in_switch_statement_p;
7990             in_statement = parser->in_statement;
7991             parser->in_switch_statement_p = true;
7992             parser->in_statement |= IN_SWITCH_STMT;
7993             cp_parser_implicitly_scoped_statement (parser, NULL);
7994             parser->in_switch_statement_p = in_switch_statement_p;
7995             parser->in_statement = in_statement;
7996
7997             /* Now we're all done with the switch-statement.  */
7998             finish_switch_stmt (statement);
7999           }
8000
8001         return statement;
8002       }
8003       break;
8004
8005     default:
8006       cp_parser_error (parser, "expected selection-statement");
8007       return error_mark_node;
8008     }
8009 }
8010
8011 /* Parse a condition.
8012
8013    condition:
8014      expression
8015      type-specifier-seq declarator = initializer-clause
8016      type-specifier-seq declarator braced-init-list
8017
8018    GNU Extension:
8019
8020    condition:
8021      type-specifier-seq declarator asm-specification [opt]
8022        attributes [opt] = assignment-expression
8023
8024    Returns the expression that should be tested.  */
8025
8026 static tree
8027 cp_parser_condition (cp_parser* parser)
8028 {
8029   cp_decl_specifier_seq type_specifiers;
8030   const char *saved_message;
8031
8032   /* Try the declaration first.  */
8033   cp_parser_parse_tentatively (parser);
8034   /* New types are not allowed in the type-specifier-seq for a
8035      condition.  */
8036   saved_message = parser->type_definition_forbidden_message;
8037   parser->type_definition_forbidden_message
8038     = "types may not be defined in conditions";
8039   /* Parse the type-specifier-seq.  */
8040   cp_parser_type_specifier_seq (parser, /*is_condition==*/true,
8041                                 /*is_trailing_return=*/false,
8042                                 &type_specifiers);
8043   /* Restore the saved message.  */
8044   parser->type_definition_forbidden_message = saved_message;
8045   /* If all is well, we might be looking at a declaration.  */
8046   if (!cp_parser_error_occurred (parser))
8047     {
8048       tree decl;
8049       tree asm_specification;
8050       tree attributes;
8051       cp_declarator *declarator;
8052       tree initializer = NULL_TREE;
8053
8054       /* Parse the declarator.  */
8055       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8056                                          /*ctor_dtor_or_conv_p=*/NULL,
8057                                          /*parenthesized_p=*/NULL,
8058                                          /*member_p=*/false);
8059       /* Parse the attributes.  */
8060       attributes = cp_parser_attributes_opt (parser);
8061       /* Parse the asm-specification.  */
8062       asm_specification = cp_parser_asm_specification_opt (parser);
8063       /* If the next token is not an `=' or '{', then we might still be
8064          looking at an expression.  For example:
8065
8066            if (A(a).x)
8067
8068          looks like a decl-specifier-seq and a declarator -- but then
8069          there is no `=', so this is an expression.  */
8070       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8071           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8072         cp_parser_simulate_error (parser);
8073         
8074       /* If we did see an `=' or '{', then we are looking at a declaration
8075          for sure.  */
8076       if (cp_parser_parse_definitely (parser))
8077         {
8078           tree pushed_scope;
8079           bool non_constant_p;
8080           bool flags = LOOKUP_ONLYCONVERTING;
8081
8082           /* Create the declaration.  */
8083           decl = start_decl (declarator, &type_specifiers,
8084                              /*initialized_p=*/true,
8085                              attributes, /*prefix_attributes=*/NULL_TREE,
8086                              &pushed_scope);
8087
8088           /* Parse the initializer.  */
8089           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8090             {
8091               initializer = cp_parser_braced_list (parser, &non_constant_p);
8092               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8093               flags = 0;
8094             }
8095           else
8096             {
8097               /* Consume the `='.  */
8098               cp_parser_require (parser, CPP_EQ, "%<=%>");
8099               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8100             }
8101           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8102             maybe_warn_cpp0x ("extended initializer lists");
8103
8104           if (!non_constant_p)
8105             initializer = fold_non_dependent_expr (initializer);
8106
8107           /* Process the initializer.  */
8108           cp_finish_decl (decl,
8109                           initializer, !non_constant_p,
8110                           asm_specification,
8111                           flags);
8112
8113           if (pushed_scope)
8114             pop_scope (pushed_scope);
8115
8116           return convert_from_reference (decl);
8117         }
8118     }
8119   /* If we didn't even get past the declarator successfully, we are
8120      definitely not looking at a declaration.  */
8121   else
8122     cp_parser_abort_tentative_parse (parser);
8123
8124   /* Otherwise, we are looking at an expression.  */
8125   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8126 }
8127
8128 /* Parse an iteration-statement.
8129
8130    iteration-statement:
8131      while ( condition ) statement
8132      do statement while ( expression ) ;
8133      for ( for-init-statement condition [opt] ; expression [opt] )
8134        statement
8135
8136    Returns the new WHILE_STMT, DO_STMT, or FOR_STMT.  */
8137
8138 static tree
8139 cp_parser_iteration_statement (cp_parser* parser)
8140 {
8141   cp_token *token;
8142   enum rid keyword;
8143   tree statement;
8144   unsigned char in_statement;
8145
8146   /* Peek at the next token.  */
8147   token = cp_parser_require (parser, CPP_KEYWORD, "iteration-statement");
8148   if (!token)
8149     return error_mark_node;
8150
8151   /* Remember whether or not we are already within an iteration
8152      statement.  */
8153   in_statement = parser->in_statement;
8154
8155   /* See what kind of keyword it is.  */
8156   keyword = token->keyword;
8157   switch (keyword)
8158     {
8159     case RID_WHILE:
8160       {
8161         tree condition;
8162
8163         /* Begin the while-statement.  */
8164         statement = begin_while_stmt ();
8165         /* Look for the `('.  */
8166         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8167         /* Parse the condition.  */
8168         condition = cp_parser_condition (parser);
8169         finish_while_stmt_cond (condition, statement);
8170         /* Look for the `)'.  */
8171         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8172         /* Parse the dependent statement.  */
8173         parser->in_statement = IN_ITERATION_STMT;
8174         cp_parser_already_scoped_statement (parser);
8175         parser->in_statement = in_statement;
8176         /* We're done with the while-statement.  */
8177         finish_while_stmt (statement);
8178       }
8179       break;
8180
8181     case RID_DO:
8182       {
8183         tree expression;
8184
8185         /* Begin the do-statement.  */
8186         statement = begin_do_stmt ();
8187         /* Parse the body of the do-statement.  */
8188         parser->in_statement = IN_ITERATION_STMT;
8189         cp_parser_implicitly_scoped_statement (parser, NULL);
8190         parser->in_statement = in_statement;
8191         finish_do_body (statement);
8192         /* Look for the `while' keyword.  */
8193         cp_parser_require_keyword (parser, RID_WHILE, "%<while%>");
8194         /* Look for the `('.  */
8195         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8196         /* Parse the expression.  */
8197         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8198         /* We're done with the do-statement.  */
8199         finish_do_stmt (expression, statement);
8200         /* Look for the `)'.  */
8201         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8202         /* Look for the `;'.  */
8203         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8204       }
8205       break;
8206
8207     case RID_FOR:
8208       {
8209         tree condition = NULL_TREE;
8210         tree expression = NULL_TREE;
8211
8212         /* Begin the for-statement.  */
8213         statement = begin_for_stmt ();
8214         /* Look for the `('.  */
8215         cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
8216         /* Parse the initialization.  */
8217         cp_parser_for_init_statement (parser);
8218         finish_for_init_stmt (statement);
8219
8220         /* If there's a condition, process it.  */
8221         if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8222           condition = cp_parser_condition (parser);
8223         finish_for_cond (condition, statement);
8224         /* Look for the `;'.  */
8225         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8226
8227         /* If there's an expression, process it.  */
8228         if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8229           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8230         finish_for_expr (expression, statement);
8231         /* Look for the `)'.  */
8232         cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
8233
8234         /* Parse the body of the for-statement.  */
8235         parser->in_statement = IN_ITERATION_STMT;
8236         cp_parser_already_scoped_statement (parser);
8237         parser->in_statement = in_statement;
8238
8239         /* We're done with the for-statement.  */
8240         finish_for_stmt (statement);
8241       }
8242       break;
8243
8244     default:
8245       cp_parser_error (parser, "expected iteration-statement");
8246       statement = error_mark_node;
8247       break;
8248     }
8249
8250   return statement;
8251 }
8252
8253 /* Parse a for-init-statement.
8254
8255    for-init-statement:
8256      expression-statement
8257      simple-declaration  */
8258
8259 static void
8260 cp_parser_for_init_statement (cp_parser* parser)
8261 {
8262   /* If the next token is a `;', then we have an empty
8263      expression-statement.  Grammatically, this is also a
8264      simple-declaration, but an invalid one, because it does not
8265      declare anything.  Therefore, if we did not handle this case
8266      specially, we would issue an error message about an invalid
8267      declaration.  */
8268   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8269     {
8270       /* We're going to speculatively look for a declaration, falling back
8271          to an expression, if necessary.  */
8272       cp_parser_parse_tentatively (parser);
8273       /* Parse the declaration.  */
8274       cp_parser_simple_declaration (parser,
8275                                     /*function_definition_allowed_p=*/false);
8276       /* If the tentative parse failed, then we shall need to look for an
8277          expression-statement.  */
8278       if (cp_parser_parse_definitely (parser))
8279         return;
8280     }
8281
8282   cp_parser_expression_statement (parser, false);
8283 }
8284
8285 /* Parse a jump-statement.
8286
8287    jump-statement:
8288      break ;
8289      continue ;
8290      return expression [opt] ;
8291      return braced-init-list ;
8292      goto identifier ;
8293
8294    GNU extension:
8295
8296    jump-statement:
8297      goto * expression ;
8298
8299    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8300
8301 static tree
8302 cp_parser_jump_statement (cp_parser* parser)
8303 {
8304   tree statement = error_mark_node;
8305   cp_token *token;
8306   enum rid keyword;
8307   unsigned char in_statement;
8308
8309   /* Peek at the next token.  */
8310   token = cp_parser_require (parser, CPP_KEYWORD, "jump-statement");
8311   if (!token)
8312     return error_mark_node;
8313
8314   /* See what kind of keyword it is.  */
8315   keyword = token->keyword;
8316   switch (keyword)
8317     {
8318     case RID_BREAK:
8319       in_statement = parser->in_statement & ~IN_IF_STMT;      
8320       switch (in_statement)
8321         {
8322         case 0:
8323           error_at (token->location, "break statement not within loop or switch");
8324           break;
8325         default:
8326           gcc_assert ((in_statement & IN_SWITCH_STMT)
8327                       || in_statement == IN_ITERATION_STMT);
8328           statement = finish_break_stmt ();
8329           break;
8330         case IN_OMP_BLOCK:
8331           error_at (token->location, "invalid exit from OpenMP structured block");
8332           break;
8333         case IN_OMP_FOR:
8334           error_at (token->location, "break statement used with OpenMP for loop");
8335           break;
8336         }
8337       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8338       break;
8339
8340     case RID_CONTINUE:
8341       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
8342         {
8343         case 0:
8344           error_at (token->location, "continue statement not within a loop");
8345           break;
8346         case IN_ITERATION_STMT:
8347         case IN_OMP_FOR:
8348           statement = finish_continue_stmt ();
8349           break;
8350         case IN_OMP_BLOCK:
8351           error_at (token->location, "invalid exit from OpenMP structured block");
8352           break;
8353         default:
8354           gcc_unreachable ();
8355         }
8356       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8357       break;
8358
8359     case RID_RETURN:
8360       {
8361         tree expr;
8362         bool expr_non_constant_p;
8363
8364         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8365           {
8366             maybe_warn_cpp0x ("extended initializer lists");
8367             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8368           }
8369         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8370           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8371         else
8372           /* If the next token is a `;', then there is no
8373              expression.  */
8374           expr = NULL_TREE;
8375         /* Build the return-statement.  */
8376         statement = finish_return_stmt (expr);
8377         /* Look for the final `;'.  */
8378         cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8379       }
8380       break;
8381
8382     case RID_GOTO:
8383       /* Create the goto-statement.  */
8384       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
8385         {
8386           /* Issue a warning about this use of a GNU extension.  */
8387           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
8388           /* Consume the '*' token.  */
8389           cp_lexer_consume_token (parser->lexer);
8390           /* Parse the dependent expression.  */
8391           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
8392         }
8393       else
8394         finish_goto_stmt (cp_parser_identifier (parser));
8395       /* Look for the final `;'.  */
8396       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8397       break;
8398
8399     default:
8400       cp_parser_error (parser, "expected jump-statement");
8401       break;
8402     }
8403
8404   return statement;
8405 }
8406
8407 /* Parse a declaration-statement.
8408
8409    declaration-statement:
8410      block-declaration  */
8411
8412 static void
8413 cp_parser_declaration_statement (cp_parser* parser)
8414 {
8415   void *p;
8416
8417   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8418   p = obstack_alloc (&declarator_obstack, 0);
8419
8420  /* Parse the block-declaration.  */
8421   cp_parser_block_declaration (parser, /*statement_p=*/true);
8422
8423   /* Free any declarators allocated.  */
8424   obstack_free (&declarator_obstack, p);
8425
8426   /* Finish off the statement.  */
8427   finish_stmt ();
8428 }
8429
8430 /* Some dependent statements (like `if (cond) statement'), are
8431    implicitly in their own scope.  In other words, if the statement is
8432    a single statement (as opposed to a compound-statement), it is
8433    none-the-less treated as if it were enclosed in braces.  Any
8434    declarations appearing in the dependent statement are out of scope
8435    after control passes that point.  This function parses a statement,
8436    but ensures that is in its own scope, even if it is not a
8437    compound-statement.
8438
8439    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8440    is a (possibly labeled) if statement which is not enclosed in
8441    braces and has an else clause.  This is used to implement
8442    -Wparentheses.
8443
8444    Returns the new statement.  */
8445
8446 static tree
8447 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
8448 {
8449   tree statement;
8450
8451   if (if_p != NULL)
8452     *if_p = false;
8453
8454   /* Mark if () ; with a special NOP_EXPR.  */
8455   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8456     {
8457       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8458       cp_lexer_consume_token (parser->lexer);
8459       statement = add_stmt (build_empty_stmt (loc));
8460     }
8461   /* if a compound is opened, we simply parse the statement directly.  */
8462   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8463     statement = cp_parser_compound_statement (parser, NULL, false);
8464   /* If the token is not a `{', then we must take special action.  */
8465   else
8466     {
8467       /* Create a compound-statement.  */
8468       statement = begin_compound_stmt (0);
8469       /* Parse the dependent-statement.  */
8470       cp_parser_statement (parser, NULL_TREE, false, if_p);
8471       /* Finish the dummy compound-statement.  */
8472       finish_compound_stmt (statement);
8473     }
8474
8475   /* Return the statement.  */
8476   return statement;
8477 }
8478
8479 /* For some dependent statements (like `while (cond) statement'), we
8480    have already created a scope.  Therefore, even if the dependent
8481    statement is a compound-statement, we do not want to create another
8482    scope.  */
8483
8484 static void
8485 cp_parser_already_scoped_statement (cp_parser* parser)
8486 {
8487   /* If the token is a `{', then we must take special action.  */
8488   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8489     cp_parser_statement (parser, NULL_TREE, false, NULL);
8490   else
8491     {
8492       /* Avoid calling cp_parser_compound_statement, so that we
8493          don't create a new scope.  Do everything else by hand.  */
8494       cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
8495       /* If the next keyword is `__label__' we have a label declaration.  */
8496       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8497         cp_parser_label_declaration (parser);
8498       /* Parse an (optional) statement-seq.  */
8499       cp_parser_statement_seq_opt (parser, NULL_TREE);
8500       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
8501     }
8502 }
8503
8504 /* Declarations [gram.dcl.dcl] */
8505
8506 /* Parse an optional declaration-sequence.
8507
8508    declaration-seq:
8509      declaration
8510      declaration-seq declaration  */
8511
8512 static void
8513 cp_parser_declaration_seq_opt (cp_parser* parser)
8514 {
8515   while (true)
8516     {
8517       cp_token *token;
8518
8519       token = cp_lexer_peek_token (parser->lexer);
8520
8521       if (token->type == CPP_CLOSE_BRACE
8522           || token->type == CPP_EOF
8523           || token->type == CPP_PRAGMA_EOL)
8524         break;
8525
8526       if (token->type == CPP_SEMICOLON)
8527         {
8528           /* A declaration consisting of a single semicolon is
8529              invalid.  Allow it unless we're being pedantic.  */
8530           cp_lexer_consume_token (parser->lexer);
8531           if (!in_system_header)
8532             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
8533           continue;
8534         }
8535
8536       /* If we're entering or exiting a region that's implicitly
8537          extern "C", modify the lang context appropriately.  */
8538       if (!parser->implicit_extern_c && token->implicit_extern_c)
8539         {
8540           push_lang_context (lang_name_c);
8541           parser->implicit_extern_c = true;
8542         }
8543       else if (parser->implicit_extern_c && !token->implicit_extern_c)
8544         {
8545           pop_lang_context ();
8546           parser->implicit_extern_c = false;
8547         }
8548
8549       if (token->type == CPP_PRAGMA)
8550         {
8551           /* A top-level declaration can consist solely of a #pragma.
8552              A nested declaration cannot, so this is done here and not
8553              in cp_parser_declaration.  (A #pragma at block scope is
8554              handled in cp_parser_statement.)  */
8555           cp_parser_pragma (parser, pragma_external);
8556           continue;
8557         }
8558
8559       /* Parse the declaration itself.  */
8560       cp_parser_declaration (parser);
8561     }
8562 }
8563
8564 /* Parse a declaration.
8565
8566    declaration:
8567      block-declaration
8568      function-definition
8569      template-declaration
8570      explicit-instantiation
8571      explicit-specialization
8572      linkage-specification
8573      namespace-definition
8574
8575    GNU extension:
8576
8577    declaration:
8578       __extension__ declaration */
8579
8580 static void
8581 cp_parser_declaration (cp_parser* parser)
8582 {
8583   cp_token token1;
8584   cp_token token2;
8585   int saved_pedantic;
8586   void *p;
8587
8588   /* Check for the `__extension__' keyword.  */
8589   if (cp_parser_extension_opt (parser, &saved_pedantic))
8590     {
8591       /* Parse the qualified declaration.  */
8592       cp_parser_declaration (parser);
8593       /* Restore the PEDANTIC flag.  */
8594       pedantic = saved_pedantic;
8595
8596       return;
8597     }
8598
8599   /* Try to figure out what kind of declaration is present.  */
8600   token1 = *cp_lexer_peek_token (parser->lexer);
8601
8602   if (token1.type != CPP_EOF)
8603     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
8604   else
8605     {
8606       token2.type = CPP_EOF;
8607       token2.keyword = RID_MAX;
8608     }
8609
8610   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
8611   p = obstack_alloc (&declarator_obstack, 0);
8612
8613   /* If the next token is `extern' and the following token is a string
8614      literal, then we have a linkage specification.  */
8615   if (token1.keyword == RID_EXTERN
8616       && cp_parser_is_string_literal (&token2))
8617     cp_parser_linkage_specification (parser);
8618   /* If the next token is `template', then we have either a template
8619      declaration, an explicit instantiation, or an explicit
8620      specialization.  */
8621   else if (token1.keyword == RID_TEMPLATE)
8622     {
8623       /* `template <>' indicates a template specialization.  */
8624       if (token2.type == CPP_LESS
8625           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
8626         cp_parser_explicit_specialization (parser);
8627       /* `template <' indicates a template declaration.  */
8628       else if (token2.type == CPP_LESS)
8629         cp_parser_template_declaration (parser, /*member_p=*/false);
8630       /* Anything else must be an explicit instantiation.  */
8631       else
8632         cp_parser_explicit_instantiation (parser);
8633     }
8634   /* If the next token is `export', then we have a template
8635      declaration.  */
8636   else if (token1.keyword == RID_EXPORT)
8637     cp_parser_template_declaration (parser, /*member_p=*/false);
8638   /* If the next token is `extern', 'static' or 'inline' and the one
8639      after that is `template', we have a GNU extended explicit
8640      instantiation directive.  */
8641   else if (cp_parser_allow_gnu_extensions_p (parser)
8642            && (token1.keyword == RID_EXTERN
8643                || token1.keyword == RID_STATIC
8644                || token1.keyword == RID_INLINE)
8645            && token2.keyword == RID_TEMPLATE)
8646     cp_parser_explicit_instantiation (parser);
8647   /* If the next token is `namespace', check for a named or unnamed
8648      namespace definition.  */
8649   else if (token1.keyword == RID_NAMESPACE
8650            && (/* A named namespace definition.  */
8651                (token2.type == CPP_NAME
8652                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
8653                     != CPP_EQ))
8654                /* An unnamed namespace definition.  */
8655                || token2.type == CPP_OPEN_BRACE
8656                || token2.keyword == RID_ATTRIBUTE))
8657     cp_parser_namespace_definition (parser);
8658   /* An inline (associated) namespace definition.  */
8659   else if (token1.keyword == RID_INLINE
8660            && token2.keyword == RID_NAMESPACE)
8661     cp_parser_namespace_definition (parser);
8662   /* Objective-C++ declaration/definition.  */
8663   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
8664     cp_parser_objc_declaration (parser);
8665   /* We must have either a block declaration or a function
8666      definition.  */
8667   else
8668     /* Try to parse a block-declaration, or a function-definition.  */
8669     cp_parser_block_declaration (parser, /*statement_p=*/false);
8670
8671   /* Free any declarators allocated.  */
8672   obstack_free (&declarator_obstack, p);
8673 }
8674
8675 /* Parse a block-declaration.
8676
8677    block-declaration:
8678      simple-declaration
8679      asm-definition
8680      namespace-alias-definition
8681      using-declaration
8682      using-directive
8683
8684    GNU Extension:
8685
8686    block-declaration:
8687      __extension__ block-declaration
8688
8689    C++0x Extension:
8690
8691    block-declaration:
8692      static_assert-declaration
8693
8694    If STATEMENT_P is TRUE, then this block-declaration is occurring as
8695    part of a declaration-statement.  */
8696
8697 static void
8698 cp_parser_block_declaration (cp_parser *parser,
8699                              bool      statement_p)
8700 {
8701   cp_token *token1;
8702   int saved_pedantic;
8703
8704   /* Check for the `__extension__' keyword.  */
8705   if (cp_parser_extension_opt (parser, &saved_pedantic))
8706     {
8707       /* Parse the qualified declaration.  */
8708       cp_parser_block_declaration (parser, statement_p);
8709       /* Restore the PEDANTIC flag.  */
8710       pedantic = saved_pedantic;
8711
8712       return;
8713     }
8714
8715   /* Peek at the next token to figure out which kind of declaration is
8716      present.  */
8717   token1 = cp_lexer_peek_token (parser->lexer);
8718
8719   /* If the next keyword is `asm', we have an asm-definition.  */
8720   if (token1->keyword == RID_ASM)
8721     {
8722       if (statement_p)
8723         cp_parser_commit_to_tentative_parse (parser);
8724       cp_parser_asm_definition (parser);
8725     }
8726   /* If the next keyword is `namespace', we have a
8727      namespace-alias-definition.  */
8728   else if (token1->keyword == RID_NAMESPACE)
8729     cp_parser_namespace_alias_definition (parser);
8730   /* If the next keyword is `using', we have either a
8731      using-declaration or a using-directive.  */
8732   else if (token1->keyword == RID_USING)
8733     {
8734       cp_token *token2;
8735
8736       if (statement_p)
8737         cp_parser_commit_to_tentative_parse (parser);
8738       /* If the token after `using' is `namespace', then we have a
8739          using-directive.  */
8740       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
8741       if (token2->keyword == RID_NAMESPACE)
8742         cp_parser_using_directive (parser);
8743       /* Otherwise, it's a using-declaration.  */
8744       else
8745         cp_parser_using_declaration (parser,
8746                                      /*access_declaration_p=*/false);
8747     }
8748   /* If the next keyword is `__label__' we have a misplaced label
8749      declaration.  */
8750   else if (token1->keyword == RID_LABEL)
8751     {
8752       cp_lexer_consume_token (parser->lexer);
8753       error_at (token1->location, "%<__label__%> not at the beginning of a block");
8754       cp_parser_skip_to_end_of_statement (parser);
8755       /* If the next token is now a `;', consume it.  */
8756       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8757         cp_lexer_consume_token (parser->lexer);
8758     }
8759   /* If the next token is `static_assert' we have a static assertion.  */
8760   else if (token1->keyword == RID_STATIC_ASSERT)
8761     cp_parser_static_assert (parser, /*member_p=*/false);
8762   /* Anything else must be a simple-declaration.  */
8763   else
8764     cp_parser_simple_declaration (parser, !statement_p);
8765 }
8766
8767 /* Parse a simple-declaration.
8768
8769    simple-declaration:
8770      decl-specifier-seq [opt] init-declarator-list [opt] ;
8771
8772    init-declarator-list:
8773      init-declarator
8774      init-declarator-list , init-declarator
8775
8776    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
8777    function-definition as a simple-declaration.  */
8778
8779 static void
8780 cp_parser_simple_declaration (cp_parser* parser,
8781                               bool function_definition_allowed_p)
8782 {
8783   cp_decl_specifier_seq decl_specifiers;
8784   int declares_class_or_enum;
8785   bool saw_declarator;
8786
8787   /* Defer access checks until we know what is being declared; the
8788      checks for names appearing in the decl-specifier-seq should be
8789      done as if we were in the scope of the thing being declared.  */
8790   push_deferring_access_checks (dk_deferred);
8791
8792   /* Parse the decl-specifier-seq.  We have to keep track of whether
8793      or not the decl-specifier-seq declares a named class or
8794      enumeration type, since that is the only case in which the
8795      init-declarator-list is allowed to be empty.
8796
8797      [dcl.dcl]
8798
8799      In a simple-declaration, the optional init-declarator-list can be
8800      omitted only when declaring a class or enumeration, that is when
8801      the decl-specifier-seq contains either a class-specifier, an
8802      elaborated-type-specifier, or an enum-specifier.  */
8803   cp_parser_decl_specifier_seq (parser,
8804                                 CP_PARSER_FLAGS_OPTIONAL,
8805                                 &decl_specifiers,
8806                                 &declares_class_or_enum);
8807   /* We no longer need to defer access checks.  */
8808   stop_deferring_access_checks ();
8809
8810   /* In a block scope, a valid declaration must always have a
8811      decl-specifier-seq.  By not trying to parse declarators, we can
8812      resolve the declaration/expression ambiguity more quickly.  */
8813   if (!function_definition_allowed_p
8814       && !decl_specifiers.any_specifiers_p)
8815     {
8816       cp_parser_error (parser, "expected declaration");
8817       goto done;
8818     }
8819
8820   /* If the next two tokens are both identifiers, the code is
8821      erroneous. The usual cause of this situation is code like:
8822
8823        T t;
8824
8825      where "T" should name a type -- but does not.  */
8826   if (!decl_specifiers.type
8827       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
8828     {
8829       /* If parsing tentatively, we should commit; we really are
8830          looking at a declaration.  */
8831       cp_parser_commit_to_tentative_parse (parser);
8832       /* Give up.  */
8833       goto done;
8834     }
8835
8836   /* If we have seen at least one decl-specifier, and the next token
8837      is not a parenthesis, then we must be looking at a declaration.
8838      (After "int (" we might be looking at a functional cast.)  */
8839   if (decl_specifiers.any_specifiers_p
8840       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
8841       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
8842       && !cp_parser_error_occurred (parser))
8843     cp_parser_commit_to_tentative_parse (parser);
8844
8845   /* Keep going until we hit the `;' at the end of the simple
8846      declaration.  */
8847   saw_declarator = false;
8848   while (cp_lexer_next_token_is_not (parser->lexer,
8849                                      CPP_SEMICOLON))
8850     {
8851       cp_token *token;
8852       bool function_definition_p;
8853       tree decl;
8854
8855       if (saw_declarator)
8856         {
8857           /* If we are processing next declarator, coma is expected */
8858           token = cp_lexer_peek_token (parser->lexer);
8859           gcc_assert (token->type == CPP_COMMA);
8860           cp_lexer_consume_token (parser->lexer);
8861         }
8862       else
8863         saw_declarator = true;
8864
8865       /* Parse the init-declarator.  */
8866       decl = cp_parser_init_declarator (parser, &decl_specifiers,
8867                                         /*checks=*/NULL,
8868                                         function_definition_allowed_p,
8869                                         /*member_p=*/false,
8870                                         declares_class_or_enum,
8871                                         &function_definition_p);
8872       /* If an error occurred while parsing tentatively, exit quickly.
8873          (That usually happens when in the body of a function; each
8874          statement is treated as a declaration-statement until proven
8875          otherwise.)  */
8876       if (cp_parser_error_occurred (parser))
8877         goto done;
8878       /* Handle function definitions specially.  */
8879       if (function_definition_p)
8880         {
8881           /* If the next token is a `,', then we are probably
8882              processing something like:
8883
8884                void f() {}, *p;
8885
8886              which is erroneous.  */
8887           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
8888             {
8889               cp_token *token = cp_lexer_peek_token (parser->lexer);
8890               error_at (token->location,
8891                         "mixing"
8892                         " declarations and function-definitions is forbidden");
8893             }
8894           /* Otherwise, we're done with the list of declarators.  */
8895           else
8896             {
8897               pop_deferring_access_checks ();
8898               return;
8899             }
8900         }
8901       /* The next token should be either a `,' or a `;'.  */
8902       token = cp_lexer_peek_token (parser->lexer);
8903       /* If it's a `,', there are more declarators to come.  */
8904       if (token->type == CPP_COMMA)
8905         /* will be consumed next time around */;
8906       /* If it's a `;', we are done.  */
8907       else if (token->type == CPP_SEMICOLON)
8908         break;
8909       /* Anything else is an error.  */
8910       else
8911         {
8912           /* If we have already issued an error message we don't need
8913              to issue another one.  */
8914           if (decl != error_mark_node
8915               || cp_parser_uncommitted_to_tentative_parse_p (parser))
8916             cp_parser_error (parser, "expected %<,%> or %<;%>");
8917           /* Skip tokens until we reach the end of the statement.  */
8918           cp_parser_skip_to_end_of_statement (parser);
8919           /* If the next token is now a `;', consume it.  */
8920           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8921             cp_lexer_consume_token (parser->lexer);
8922           goto done;
8923         }
8924       /* After the first time around, a function-definition is not
8925          allowed -- even if it was OK at first.  For example:
8926
8927            int i, f() {}
8928
8929          is not valid.  */
8930       function_definition_allowed_p = false;
8931     }
8932
8933   /* Issue an error message if no declarators are present, and the
8934      decl-specifier-seq does not itself declare a class or
8935      enumeration.  */
8936   if (!saw_declarator)
8937     {
8938       if (cp_parser_declares_only_class_p (parser))
8939         shadow_tag (&decl_specifiers);
8940       /* Perform any deferred access checks.  */
8941       perform_deferred_access_checks ();
8942     }
8943
8944   /* Consume the `;'.  */
8945   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
8946
8947  done:
8948   pop_deferring_access_checks ();
8949 }
8950
8951 /* Parse a decl-specifier-seq.
8952
8953    decl-specifier-seq:
8954      decl-specifier-seq [opt] decl-specifier
8955
8956    decl-specifier:
8957      storage-class-specifier
8958      type-specifier
8959      function-specifier
8960      friend
8961      typedef
8962
8963    GNU Extension:
8964
8965    decl-specifier:
8966      attributes
8967
8968    Set *DECL_SPECS to a representation of the decl-specifier-seq.
8969
8970    The parser flags FLAGS is used to control type-specifier parsing.
8971
8972    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
8973    flags:
8974
8975      1: one of the decl-specifiers is an elaborated-type-specifier
8976         (i.e., a type declaration)
8977      2: one of the decl-specifiers is an enum-specifier or a
8978         class-specifier (i.e., a type definition)
8979
8980    */
8981
8982 static void
8983 cp_parser_decl_specifier_seq (cp_parser* parser,
8984                               cp_parser_flags flags,
8985                               cp_decl_specifier_seq *decl_specs,
8986                               int* declares_class_or_enum)
8987 {
8988   bool constructor_possible_p = !parser->in_declarator_p;
8989   cp_token *start_token = NULL;
8990
8991   /* Clear DECL_SPECS.  */
8992   clear_decl_specs (decl_specs);
8993
8994   /* Assume no class or enumeration type is declared.  */
8995   *declares_class_or_enum = 0;
8996
8997   /* Keep reading specifiers until there are no more to read.  */
8998   while (true)
8999     {
9000       bool constructor_p;
9001       bool found_decl_spec;
9002       cp_token *token;
9003
9004       /* Peek at the next token.  */
9005       token = cp_lexer_peek_token (parser->lexer);
9006
9007       /* Save the first token of the decl spec list for error
9008          reporting.  */
9009       if (!start_token)
9010         start_token = token;
9011       /* Handle attributes.  */
9012       if (token->keyword == RID_ATTRIBUTE)
9013         {
9014           /* Parse the attributes.  */
9015           decl_specs->attributes
9016             = chainon (decl_specs->attributes,
9017                        cp_parser_attributes_opt (parser));
9018           continue;
9019         }
9020       /* Assume we will find a decl-specifier keyword.  */
9021       found_decl_spec = true;
9022       /* If the next token is an appropriate keyword, we can simply
9023          add it to the list.  */
9024       switch (token->keyword)
9025         {
9026           /* decl-specifier:
9027                friend
9028                constexpr */
9029         case RID_FRIEND:
9030           if (!at_class_scope_p ())
9031             {
9032               error_at (token->location, "%<friend%> used outside of class");
9033               cp_lexer_purge_token (parser->lexer);
9034             }
9035           else
9036             {
9037               ++decl_specs->specs[(int) ds_friend];
9038               /* Consume the token.  */
9039               cp_lexer_consume_token (parser->lexer);
9040             }
9041           break;
9042
9043         case RID_CONSTEXPR:
9044           ++decl_specs->specs[(int) ds_constexpr];
9045           cp_lexer_consume_token (parser->lexer);
9046           break;
9047
9048           /* function-specifier:
9049                inline
9050                virtual
9051                explicit  */
9052         case RID_INLINE:
9053         case RID_VIRTUAL:
9054         case RID_EXPLICIT:
9055           cp_parser_function_specifier_opt (parser, decl_specs);
9056           break;
9057
9058           /* decl-specifier:
9059                typedef  */
9060         case RID_TYPEDEF:
9061           ++decl_specs->specs[(int) ds_typedef];
9062           /* Consume the token.  */
9063           cp_lexer_consume_token (parser->lexer);
9064           /* A constructor declarator cannot appear in a typedef.  */
9065           constructor_possible_p = false;
9066           /* The "typedef" keyword can only occur in a declaration; we
9067              may as well commit at this point.  */
9068           cp_parser_commit_to_tentative_parse (parser);
9069
9070           if (decl_specs->storage_class != sc_none)
9071             decl_specs->conflicting_specifiers_p = true;
9072           break;
9073
9074           /* storage-class-specifier:
9075                auto
9076                register
9077                static
9078                extern
9079                mutable
9080
9081              GNU Extension:
9082                thread  */
9083         case RID_AUTO:
9084           if (cxx_dialect == cxx98) 
9085             {
9086               /* Consume the token.  */
9087               cp_lexer_consume_token (parser->lexer);
9088
9089               /* Complain about `auto' as a storage specifier, if
9090                  we're complaining about C++0x compatibility.  */
9091               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9092                           " will change meaning in C++0x; please remove it");
9093
9094               /* Set the storage class anyway.  */
9095               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9096                                            token->location);
9097             }
9098           else
9099             /* C++0x auto type-specifier.  */
9100             found_decl_spec = false;
9101           break;
9102
9103         case RID_REGISTER:
9104         case RID_STATIC:
9105         case RID_EXTERN:
9106         case RID_MUTABLE:
9107           /* Consume the token.  */
9108           cp_lexer_consume_token (parser->lexer);
9109           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9110                                        token->location);
9111           break;
9112         case RID_THREAD:
9113           /* Consume the token.  */
9114           cp_lexer_consume_token (parser->lexer);
9115           ++decl_specs->specs[(int) ds_thread];
9116           break;
9117
9118         default:
9119           /* We did not yet find a decl-specifier yet.  */
9120           found_decl_spec = false;
9121           break;
9122         }
9123
9124       /* Constructors are a special case.  The `S' in `S()' is not a
9125          decl-specifier; it is the beginning of the declarator.  */
9126       constructor_p
9127         = (!found_decl_spec
9128            && constructor_possible_p
9129            && (cp_parser_constructor_declarator_p
9130                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9131
9132       /* If we don't have a DECL_SPEC yet, then we must be looking at
9133          a type-specifier.  */
9134       if (!found_decl_spec && !constructor_p)
9135         {
9136           int decl_spec_declares_class_or_enum;
9137           bool is_cv_qualifier;
9138           tree type_spec;
9139
9140           type_spec
9141             = cp_parser_type_specifier (parser, flags,
9142                                         decl_specs,
9143                                         /*is_declaration=*/true,
9144                                         &decl_spec_declares_class_or_enum,
9145                                         &is_cv_qualifier);
9146           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9147
9148           /* If this type-specifier referenced a user-defined type
9149              (a typedef, class-name, etc.), then we can't allow any
9150              more such type-specifiers henceforth.
9151
9152              [dcl.spec]
9153
9154              The longest sequence of decl-specifiers that could
9155              possibly be a type name is taken as the
9156              decl-specifier-seq of a declaration.  The sequence shall
9157              be self-consistent as described below.
9158
9159              [dcl.type]
9160
9161              As a general rule, at most one type-specifier is allowed
9162              in the complete decl-specifier-seq of a declaration.  The
9163              only exceptions are the following:
9164
9165              -- const or volatile can be combined with any other
9166                 type-specifier.
9167
9168              -- signed or unsigned can be combined with char, long,
9169                 short, or int.
9170
9171              -- ..
9172
9173              Example:
9174
9175                typedef char* Pc;
9176                void g (const int Pc);
9177
9178              Here, Pc is *not* part of the decl-specifier seq; it's
9179              the declarator.  Therefore, once we see a type-specifier
9180              (other than a cv-qualifier), we forbid any additional
9181              user-defined types.  We *do* still allow things like `int
9182              int' to be considered a decl-specifier-seq, and issue the
9183              error message later.  */
9184           if (type_spec && !is_cv_qualifier)
9185             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9186           /* A constructor declarator cannot follow a type-specifier.  */
9187           if (type_spec)
9188             {
9189               constructor_possible_p = false;
9190               found_decl_spec = true;
9191             }
9192         }
9193
9194       /* If we still do not have a DECL_SPEC, then there are no more
9195          decl-specifiers.  */
9196       if (!found_decl_spec)
9197         break;
9198
9199       decl_specs->any_specifiers_p = true;
9200       /* After we see one decl-specifier, further decl-specifiers are
9201          always optional.  */
9202       flags |= CP_PARSER_FLAGS_OPTIONAL;
9203     }
9204
9205   cp_parser_check_decl_spec (decl_specs, start_token->location);
9206
9207   /* Don't allow a friend specifier with a class definition.  */
9208   if (decl_specs->specs[(int) ds_friend] != 0
9209       && (*declares_class_or_enum & 2))
9210     error_at (start_token->location,
9211               "class definition may not be declared a friend");
9212 }
9213
9214 /* Parse an (optional) storage-class-specifier.
9215
9216    storage-class-specifier:
9217      auto
9218      register
9219      static
9220      extern
9221      mutable
9222
9223    GNU Extension:
9224
9225    storage-class-specifier:
9226      thread
9227
9228    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9229
9230 static tree
9231 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9232 {
9233   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9234     {
9235     case RID_AUTO:
9236       if (cxx_dialect != cxx98)
9237         return NULL_TREE;
9238       /* Fall through for C++98.  */
9239
9240     case RID_REGISTER:
9241     case RID_STATIC:
9242     case RID_EXTERN:
9243     case RID_MUTABLE:
9244     case RID_THREAD:
9245       /* Consume the token.  */
9246       return cp_lexer_consume_token (parser->lexer)->u.value;
9247
9248     default:
9249       return NULL_TREE;
9250     }
9251 }
9252
9253 /* Parse an (optional) function-specifier.
9254
9255    function-specifier:
9256      inline
9257      virtual
9258      explicit
9259
9260    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9261    Updates DECL_SPECS, if it is non-NULL.  */
9262
9263 static tree
9264 cp_parser_function_specifier_opt (cp_parser* parser,
9265                                   cp_decl_specifier_seq *decl_specs)
9266 {
9267   cp_token *token = cp_lexer_peek_token (parser->lexer);
9268   switch (token->keyword)
9269     {
9270     case RID_INLINE:
9271       if (decl_specs)
9272         ++decl_specs->specs[(int) ds_inline];
9273       break;
9274
9275     case RID_VIRTUAL:
9276       /* 14.5.2.3 [temp.mem]
9277
9278          A member function template shall not be virtual.  */
9279       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9280         error_at (token->location, "templates may not be %<virtual%>");
9281       else if (decl_specs)
9282         ++decl_specs->specs[(int) ds_virtual];
9283       break;
9284
9285     case RID_EXPLICIT:
9286       if (decl_specs)
9287         ++decl_specs->specs[(int) ds_explicit];
9288       break;
9289
9290     default:
9291       return NULL_TREE;
9292     }
9293
9294   /* Consume the token.  */
9295   return cp_lexer_consume_token (parser->lexer)->u.value;
9296 }
9297
9298 /* Parse a linkage-specification.
9299
9300    linkage-specification:
9301      extern string-literal { declaration-seq [opt] }
9302      extern string-literal declaration  */
9303
9304 static void
9305 cp_parser_linkage_specification (cp_parser* parser)
9306 {
9307   tree linkage;
9308
9309   /* Look for the `extern' keyword.  */
9310   cp_parser_require_keyword (parser, RID_EXTERN, "%<extern%>");
9311
9312   /* Look for the string-literal.  */
9313   linkage = cp_parser_string_literal (parser, false, false);
9314
9315   /* Transform the literal into an identifier.  If the literal is a
9316      wide-character string, or contains embedded NULs, then we can't
9317      handle it as the user wants.  */
9318   if (strlen (TREE_STRING_POINTER (linkage))
9319       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
9320     {
9321       cp_parser_error (parser, "invalid linkage-specification");
9322       /* Assume C++ linkage.  */
9323       linkage = lang_name_cplusplus;
9324     }
9325   else
9326     linkage = get_identifier (TREE_STRING_POINTER (linkage));
9327
9328   /* We're now using the new linkage.  */
9329   push_lang_context (linkage);
9330
9331   /* If the next token is a `{', then we're using the first
9332      production.  */
9333   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9334     {
9335       /* Consume the `{' token.  */
9336       cp_lexer_consume_token (parser->lexer);
9337       /* Parse the declarations.  */
9338       cp_parser_declaration_seq_opt (parser);
9339       /* Look for the closing `}'.  */
9340       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
9341     }
9342   /* Otherwise, there's just one declaration.  */
9343   else
9344     {
9345       bool saved_in_unbraced_linkage_specification_p;
9346
9347       saved_in_unbraced_linkage_specification_p
9348         = parser->in_unbraced_linkage_specification_p;
9349       parser->in_unbraced_linkage_specification_p = true;
9350       cp_parser_declaration (parser);
9351       parser->in_unbraced_linkage_specification_p
9352         = saved_in_unbraced_linkage_specification_p;
9353     }
9354
9355   /* We're done with the linkage-specification.  */
9356   pop_lang_context ();
9357 }
9358
9359 /* Parse a static_assert-declaration.
9360
9361    static_assert-declaration:
9362      static_assert ( constant-expression , string-literal ) ; 
9363
9364    If MEMBER_P, this static_assert is a class member.  */
9365
9366 static void 
9367 cp_parser_static_assert(cp_parser *parser, bool member_p)
9368 {
9369   tree condition;
9370   tree message;
9371   cp_token *token;
9372   location_t saved_loc;
9373
9374   /* Peek at the `static_assert' token so we can keep track of exactly
9375      where the static assertion started.  */
9376   token = cp_lexer_peek_token (parser->lexer);
9377   saved_loc = token->location;
9378
9379   /* Look for the `static_assert' keyword.  */
9380   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
9381                                   "%<static_assert%>"))
9382     return;
9383
9384   /*  We know we are in a static assertion; commit to any tentative
9385       parse.  */
9386   if (cp_parser_parsing_tentatively (parser))
9387     cp_parser_commit_to_tentative_parse (parser);
9388
9389   /* Parse the `(' starting the static assertion condition.  */
9390   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
9391
9392   /* Parse the constant-expression.  */
9393   condition = 
9394     cp_parser_constant_expression (parser,
9395                                    /*allow_non_constant_p=*/false,
9396                                    /*non_constant_p=*/NULL);
9397
9398   /* Parse the separating `,'.  */
9399   cp_parser_require (parser, CPP_COMMA, "%<,%>");
9400
9401   /* Parse the string-literal message.  */
9402   message = cp_parser_string_literal (parser, 
9403                                       /*translate=*/false,
9404                                       /*wide_ok=*/true);
9405
9406   /* A `)' completes the static assertion.  */
9407   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9408     cp_parser_skip_to_closing_parenthesis (parser, 
9409                                            /*recovering=*/true, 
9410                                            /*or_comma=*/false,
9411                                            /*consume_paren=*/true);
9412
9413   /* A semicolon terminates the declaration.  */
9414   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
9415
9416   /* Complete the static assertion, which may mean either processing 
9417      the static assert now or saving it for template instantiation.  */
9418   finish_static_assert (condition, message, saved_loc, member_p);
9419 }
9420
9421 /* Parse a `decltype' type. Returns the type. 
9422
9423    simple-type-specifier:
9424      decltype ( expression )  */
9425
9426 static tree
9427 cp_parser_decltype (cp_parser *parser)
9428 {
9429   tree expr;
9430   bool id_expression_or_member_access_p = false;
9431   const char *saved_message;
9432   bool saved_integral_constant_expression_p;
9433   bool saved_non_integral_constant_expression_p;
9434   cp_token *id_expr_start_token;
9435
9436   /* Look for the `decltype' token.  */
9437   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, "%<decltype%>"))
9438     return error_mark_node;
9439
9440   /* Types cannot be defined in a `decltype' expression.  Save away the
9441      old message.  */
9442   saved_message = parser->type_definition_forbidden_message;
9443
9444   /* And create the new one.  */
9445   parser->type_definition_forbidden_message
9446     = "types may not be defined in %<decltype%> expressions";
9447
9448   /* The restrictions on constant-expressions do not apply inside
9449      decltype expressions.  */
9450   saved_integral_constant_expression_p
9451     = parser->integral_constant_expression_p;
9452   saved_non_integral_constant_expression_p
9453     = parser->non_integral_constant_expression_p;
9454   parser->integral_constant_expression_p = false;
9455
9456   /* Do not actually evaluate the expression.  */
9457   ++cp_unevaluated_operand;
9458
9459   /* Do not warn about problems with the expression.  */
9460   ++c_inhibit_evaluation_warnings;
9461
9462   /* Parse the opening `('.  */
9463   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
9464     return error_mark_node;
9465   
9466   /* First, try parsing an id-expression.  */
9467   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
9468   cp_parser_parse_tentatively (parser);
9469   expr = cp_parser_id_expression (parser,
9470                                   /*template_keyword_p=*/false,
9471                                   /*check_dependency_p=*/true,
9472                                   /*template_p=*/NULL,
9473                                   /*declarator_p=*/false,
9474                                   /*optional_p=*/false);
9475
9476   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
9477     {
9478       bool non_integral_constant_expression_p = false;
9479       tree id_expression = expr;
9480       cp_id_kind idk;
9481       const char *error_msg;
9482
9483       if (TREE_CODE (expr) == IDENTIFIER_NODE)
9484         /* Lookup the name we got back from the id-expression.  */
9485         expr = cp_parser_lookup_name (parser, expr,
9486                                       none_type,
9487                                       /*is_template=*/false,
9488                                       /*is_namespace=*/false,
9489                                       /*check_dependency=*/true,
9490                                       /*ambiguous_decls=*/NULL,
9491                                       id_expr_start_token->location);
9492
9493       if (expr
9494           && expr != error_mark_node
9495           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
9496           && TREE_CODE (expr) != TYPE_DECL
9497           && (TREE_CODE (expr) != BIT_NOT_EXPR
9498               || !TYPE_P (TREE_OPERAND (expr, 0)))
9499           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9500         {
9501           /* Complete lookup of the id-expression.  */
9502           expr = (finish_id_expression
9503                   (id_expression, expr, parser->scope, &idk,
9504                    /*integral_constant_expression_p=*/false,
9505                    /*allow_non_integral_constant_expression_p=*/true,
9506                    &non_integral_constant_expression_p,
9507                    /*template_p=*/false,
9508                    /*done=*/true,
9509                    /*address_p=*/false,
9510                    /*template_arg_p=*/false,
9511                    &error_msg,
9512                    id_expr_start_token->location));
9513
9514           if (expr == error_mark_node)
9515             /* We found an id-expression, but it was something that we
9516                should not have found. This is an error, not something
9517                we can recover from, so note that we found an
9518                id-expression and we'll recover as gracefully as
9519                possible.  */
9520             id_expression_or_member_access_p = true;
9521         }
9522
9523       if (expr 
9524           && expr != error_mark_node
9525           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9526         /* We have an id-expression.  */
9527         id_expression_or_member_access_p = true;
9528     }
9529
9530   if (!id_expression_or_member_access_p)
9531     {
9532       /* Abort the id-expression parse.  */
9533       cp_parser_abort_tentative_parse (parser);
9534
9535       /* Parsing tentatively, again.  */
9536       cp_parser_parse_tentatively (parser);
9537
9538       /* Parse a class member access.  */
9539       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
9540                                            /*cast_p=*/false,
9541                                            /*member_access_only_p=*/true, NULL);
9542
9543       if (expr 
9544           && expr != error_mark_node
9545           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
9546         /* We have an id-expression.  */
9547         id_expression_or_member_access_p = true;
9548     }
9549
9550   if (id_expression_or_member_access_p)
9551     /* We have parsed the complete id-expression or member access.  */
9552     cp_parser_parse_definitely (parser);
9553   else
9554     {
9555       bool saved_greater_than_is_operator_p;
9556
9557       /* Abort our attempt to parse an id-expression or member access
9558          expression.  */
9559       cp_parser_abort_tentative_parse (parser);
9560
9561       /* Within a parenthesized expression, a `>' token is always
9562          the greater-than operator.  */
9563       saved_greater_than_is_operator_p
9564         = parser->greater_than_is_operator_p;
9565       parser->greater_than_is_operator_p = true;
9566
9567       /* Parse a full expression.  */
9568       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9569
9570       /* The `>' token might be the end of a template-id or
9571          template-parameter-list now.  */
9572       parser->greater_than_is_operator_p
9573         = saved_greater_than_is_operator_p;
9574     }
9575
9576   /* Go back to evaluating expressions.  */
9577   --cp_unevaluated_operand;
9578   --c_inhibit_evaluation_warnings;
9579
9580   /* Restore the old message and the integral constant expression
9581      flags.  */
9582   parser->type_definition_forbidden_message = saved_message;
9583   parser->integral_constant_expression_p
9584     = saved_integral_constant_expression_p;
9585   parser->non_integral_constant_expression_p
9586     = saved_non_integral_constant_expression_p;
9587
9588   if (expr == error_mark_node)
9589     {
9590       /* Skip everything up to the closing `)'.  */
9591       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9592                                              /*consume_paren=*/true);
9593       return error_mark_node;
9594     }
9595   
9596   /* Parse to the closing `)'.  */
9597   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
9598     {
9599       cp_parser_skip_to_closing_parenthesis (parser, true, false,
9600                                              /*consume_paren=*/true);
9601       return error_mark_node;
9602     }
9603
9604   return finish_decltype_type (expr, id_expression_or_member_access_p);
9605 }
9606
9607 /* Special member functions [gram.special] */
9608
9609 /* Parse a conversion-function-id.
9610
9611    conversion-function-id:
9612      operator conversion-type-id
9613
9614    Returns an IDENTIFIER_NODE representing the operator.  */
9615
9616 static tree
9617 cp_parser_conversion_function_id (cp_parser* parser)
9618 {
9619   tree type;
9620   tree saved_scope;
9621   tree saved_qualifying_scope;
9622   tree saved_object_scope;
9623   tree pushed_scope = NULL_TREE;
9624
9625   /* Look for the `operator' token.  */
9626   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
9627     return error_mark_node;
9628   /* When we parse the conversion-type-id, the current scope will be
9629      reset.  However, we need that information in able to look up the
9630      conversion function later, so we save it here.  */
9631   saved_scope = parser->scope;
9632   saved_qualifying_scope = parser->qualifying_scope;
9633   saved_object_scope = parser->object_scope;
9634   /* We must enter the scope of the class so that the names of
9635      entities declared within the class are available in the
9636      conversion-type-id.  For example, consider:
9637
9638        struct S {
9639          typedef int I;
9640          operator I();
9641        };
9642
9643        S::operator I() { ... }
9644
9645      In order to see that `I' is a type-name in the definition, we
9646      must be in the scope of `S'.  */
9647   if (saved_scope)
9648     pushed_scope = push_scope (saved_scope);
9649   /* Parse the conversion-type-id.  */
9650   type = cp_parser_conversion_type_id (parser);
9651   /* Leave the scope of the class, if any.  */
9652   if (pushed_scope)
9653     pop_scope (pushed_scope);
9654   /* Restore the saved scope.  */
9655   parser->scope = saved_scope;
9656   parser->qualifying_scope = saved_qualifying_scope;
9657   parser->object_scope = saved_object_scope;
9658   /* If the TYPE is invalid, indicate failure.  */
9659   if (type == error_mark_node)
9660     return error_mark_node;
9661   return mangle_conv_op_name_for_type (type);
9662 }
9663
9664 /* Parse a conversion-type-id:
9665
9666    conversion-type-id:
9667      type-specifier-seq conversion-declarator [opt]
9668
9669    Returns the TYPE specified.  */
9670
9671 static tree
9672 cp_parser_conversion_type_id (cp_parser* parser)
9673 {
9674   tree attributes;
9675   cp_decl_specifier_seq type_specifiers;
9676   cp_declarator *declarator;
9677   tree type_specified;
9678
9679   /* Parse the attributes.  */
9680   attributes = cp_parser_attributes_opt (parser);
9681   /* Parse the type-specifiers.  */
9682   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
9683                                 /*is_trailing_return=*/false,
9684                                 &type_specifiers);
9685   /* If that didn't work, stop.  */
9686   if (type_specifiers.type == error_mark_node)
9687     return error_mark_node;
9688   /* Parse the conversion-declarator.  */
9689   declarator = cp_parser_conversion_declarator_opt (parser);
9690
9691   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
9692                                     /*initialized=*/0, &attributes);
9693   if (attributes)
9694     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
9695
9696   /* Don't give this error when parsing tentatively.  This happens to
9697      work because we always parse this definitively once.  */
9698   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
9699       && type_uses_auto (type_specified))
9700     {
9701       error ("invalid use of %<auto%> in conversion operator");
9702       return error_mark_node;
9703     }
9704
9705   return type_specified;
9706 }
9707
9708 /* Parse an (optional) conversion-declarator.
9709
9710    conversion-declarator:
9711      ptr-operator conversion-declarator [opt]
9712
9713    */
9714
9715 static cp_declarator *
9716 cp_parser_conversion_declarator_opt (cp_parser* parser)
9717 {
9718   enum tree_code code;
9719   tree class_type;
9720   cp_cv_quals cv_quals;
9721
9722   /* We don't know if there's a ptr-operator next, or not.  */
9723   cp_parser_parse_tentatively (parser);
9724   /* Try the ptr-operator.  */
9725   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
9726   /* If it worked, look for more conversion-declarators.  */
9727   if (cp_parser_parse_definitely (parser))
9728     {
9729       cp_declarator *declarator;
9730
9731       /* Parse another optional declarator.  */
9732       declarator = cp_parser_conversion_declarator_opt (parser);
9733
9734       return cp_parser_make_indirect_declarator
9735         (code, class_type, cv_quals, declarator);
9736    }
9737
9738   return NULL;
9739 }
9740
9741 /* Parse an (optional) ctor-initializer.
9742
9743    ctor-initializer:
9744      : mem-initializer-list
9745
9746    Returns TRUE iff the ctor-initializer was actually present.  */
9747
9748 static bool
9749 cp_parser_ctor_initializer_opt (cp_parser* parser)
9750 {
9751   /* If the next token is not a `:', then there is no
9752      ctor-initializer.  */
9753   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
9754     {
9755       /* Do default initialization of any bases and members.  */
9756       if (DECL_CONSTRUCTOR_P (current_function_decl))
9757         finish_mem_initializers (NULL_TREE);
9758
9759       return false;
9760     }
9761
9762   /* Consume the `:' token.  */
9763   cp_lexer_consume_token (parser->lexer);
9764   /* And the mem-initializer-list.  */
9765   cp_parser_mem_initializer_list (parser);
9766
9767   return true;
9768 }
9769
9770 /* Parse a mem-initializer-list.
9771
9772    mem-initializer-list:
9773      mem-initializer ... [opt]
9774      mem-initializer ... [opt] , mem-initializer-list  */
9775
9776 static void
9777 cp_parser_mem_initializer_list (cp_parser* parser)
9778 {
9779   tree mem_initializer_list = NULL_TREE;
9780   cp_token *token = cp_lexer_peek_token (parser->lexer);
9781
9782   /* Let the semantic analysis code know that we are starting the
9783      mem-initializer-list.  */
9784   if (!DECL_CONSTRUCTOR_P (current_function_decl))
9785     error_at (token->location,
9786               "only constructors take base initializers");
9787
9788   /* Loop through the list.  */
9789   while (true)
9790     {
9791       tree mem_initializer;
9792
9793       token = cp_lexer_peek_token (parser->lexer);
9794       /* Parse the mem-initializer.  */
9795       mem_initializer = cp_parser_mem_initializer (parser);
9796       /* If the next token is a `...', we're expanding member initializers. */
9797       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
9798         {
9799           /* Consume the `...'. */
9800           cp_lexer_consume_token (parser->lexer);
9801
9802           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
9803              can be expanded but members cannot. */
9804           if (mem_initializer != error_mark_node
9805               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
9806             {
9807               error_at (token->location,
9808                         "cannot expand initializer for member %<%D%>",
9809                         TREE_PURPOSE (mem_initializer));
9810               mem_initializer = error_mark_node;
9811             }
9812
9813           /* Construct the pack expansion type. */
9814           if (mem_initializer != error_mark_node)
9815             mem_initializer = make_pack_expansion (mem_initializer);
9816         }
9817       /* Add it to the list, unless it was erroneous.  */
9818       if (mem_initializer != error_mark_node)
9819         {
9820           TREE_CHAIN (mem_initializer) = mem_initializer_list;
9821           mem_initializer_list = mem_initializer;
9822         }
9823       /* If the next token is not a `,', we're done.  */
9824       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
9825         break;
9826       /* Consume the `,' token.  */
9827       cp_lexer_consume_token (parser->lexer);
9828     }
9829
9830   /* Perform semantic analysis.  */
9831   if (DECL_CONSTRUCTOR_P (current_function_decl))
9832     finish_mem_initializers (mem_initializer_list);
9833 }
9834
9835 /* Parse a mem-initializer.
9836
9837    mem-initializer:
9838      mem-initializer-id ( expression-list [opt] )
9839      mem-initializer-id braced-init-list
9840
9841    GNU extension:
9842
9843    mem-initializer:
9844      ( expression-list [opt] )
9845
9846    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
9847    class) or FIELD_DECL (for a non-static data member) to initialize;
9848    the TREE_VALUE is the expression-list.  An empty initialization
9849    list is represented by void_list_node.  */
9850
9851 static tree
9852 cp_parser_mem_initializer (cp_parser* parser)
9853 {
9854   tree mem_initializer_id;
9855   tree expression_list;
9856   tree member;
9857   cp_token *token = cp_lexer_peek_token (parser->lexer);
9858
9859   /* Find out what is being initialized.  */
9860   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
9861     {
9862       permerror (token->location,
9863                  "anachronistic old-style base class initializer");
9864       mem_initializer_id = NULL_TREE;
9865     }
9866   else
9867     {
9868       mem_initializer_id = cp_parser_mem_initializer_id (parser);
9869       if (mem_initializer_id == error_mark_node)
9870         return mem_initializer_id;
9871     }
9872   member = expand_member_init (mem_initializer_id);
9873   if (member && !DECL_P (member))
9874     in_base_initializer = 1;
9875
9876   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9877     {
9878       bool expr_non_constant_p;
9879       maybe_warn_cpp0x ("extended initializer lists");
9880       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
9881       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
9882       expression_list = build_tree_list (NULL_TREE, expression_list);
9883     }
9884   else
9885     {
9886       VEC(tree,gc)* vec;
9887       vec = cp_parser_parenthesized_expression_list (parser, false,
9888                                                      /*cast_p=*/false,
9889                                                      /*allow_expansion_p=*/true,
9890                                                      /*non_constant_p=*/NULL);
9891       if (vec == NULL)
9892         return error_mark_node;
9893       expression_list = build_tree_list_vec (vec);
9894       release_tree_vector (vec);
9895     }
9896
9897   if (expression_list == error_mark_node)
9898     return error_mark_node;
9899   if (!expression_list)
9900     expression_list = void_type_node;
9901
9902   in_base_initializer = 0;
9903
9904   return member ? build_tree_list (member, expression_list) : error_mark_node;
9905 }
9906
9907 /* Parse a mem-initializer-id.
9908
9909    mem-initializer-id:
9910      :: [opt] nested-name-specifier [opt] class-name
9911      identifier
9912
9913    Returns a TYPE indicating the class to be initializer for the first
9914    production.  Returns an IDENTIFIER_NODE indicating the data member
9915    to be initialized for the second production.  */
9916
9917 static tree
9918 cp_parser_mem_initializer_id (cp_parser* parser)
9919 {
9920   bool global_scope_p;
9921   bool nested_name_specifier_p;
9922   bool template_p = false;
9923   tree id;
9924
9925   cp_token *token = cp_lexer_peek_token (parser->lexer);
9926
9927   /* `typename' is not allowed in this context ([temp.res]).  */
9928   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
9929     {
9930       error_at (token->location, 
9931                 "keyword %<typename%> not allowed in this context (a qualified "
9932                 "member initializer is implicitly a type)");
9933       cp_lexer_consume_token (parser->lexer);
9934     }
9935   /* Look for the optional `::' operator.  */
9936   global_scope_p
9937     = (cp_parser_global_scope_opt (parser,
9938                                    /*current_scope_valid_p=*/false)
9939        != NULL_TREE);
9940   /* Look for the optional nested-name-specifier.  The simplest way to
9941      implement:
9942
9943        [temp.res]
9944
9945        The keyword `typename' is not permitted in a base-specifier or
9946        mem-initializer; in these contexts a qualified name that
9947        depends on a template-parameter is implicitly assumed to be a
9948        type name.
9949
9950      is to assume that we have seen the `typename' keyword at this
9951      point.  */
9952   nested_name_specifier_p
9953     = (cp_parser_nested_name_specifier_opt (parser,
9954                                             /*typename_keyword_p=*/true,
9955                                             /*check_dependency_p=*/true,
9956                                             /*type_p=*/true,
9957                                             /*is_declaration=*/true)
9958        != NULL_TREE);
9959   if (nested_name_specifier_p)
9960     template_p = cp_parser_optional_template_keyword (parser);
9961   /* If there is a `::' operator or a nested-name-specifier, then we
9962      are definitely looking for a class-name.  */
9963   if (global_scope_p || nested_name_specifier_p)
9964     return cp_parser_class_name (parser,
9965                                  /*typename_keyword_p=*/true,
9966                                  /*template_keyword_p=*/template_p,
9967                                  none_type,
9968                                  /*check_dependency_p=*/true,
9969                                  /*class_head_p=*/false,
9970                                  /*is_declaration=*/true);
9971   /* Otherwise, we could also be looking for an ordinary identifier.  */
9972   cp_parser_parse_tentatively (parser);
9973   /* Try a class-name.  */
9974   id = cp_parser_class_name (parser,
9975                              /*typename_keyword_p=*/true,
9976                              /*template_keyword_p=*/false,
9977                              none_type,
9978                              /*check_dependency_p=*/true,
9979                              /*class_head_p=*/false,
9980                              /*is_declaration=*/true);
9981   /* If we found one, we're done.  */
9982   if (cp_parser_parse_definitely (parser))
9983     return id;
9984   /* Otherwise, look for an ordinary identifier.  */
9985   return cp_parser_identifier (parser);
9986 }
9987
9988 /* Overloading [gram.over] */
9989
9990 /* Parse an operator-function-id.
9991
9992    operator-function-id:
9993      operator operator
9994
9995    Returns an IDENTIFIER_NODE for the operator which is a
9996    human-readable spelling of the identifier, e.g., `operator +'.  */
9997
9998 static tree
9999 cp_parser_operator_function_id (cp_parser* parser)
10000 {
10001   /* Look for the `operator' keyword.  */
10002   if (!cp_parser_require_keyword (parser, RID_OPERATOR, "%<operator%>"))
10003     return error_mark_node;
10004   /* And then the name of the operator itself.  */
10005   return cp_parser_operator (parser);
10006 }
10007
10008 /* Parse an operator.
10009
10010    operator:
10011      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10012      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10013      || ++ -- , ->* -> () []
10014
10015    GNU Extensions:
10016
10017    operator:
10018      <? >? <?= >?=
10019
10020    Returns an IDENTIFIER_NODE for the operator which is a
10021    human-readable spelling of the identifier, e.g., `operator +'.  */
10022
10023 static tree
10024 cp_parser_operator (cp_parser* parser)
10025 {
10026   tree id = NULL_TREE;
10027   cp_token *token;
10028
10029   /* Peek at the next token.  */
10030   token = cp_lexer_peek_token (parser->lexer);
10031   /* Figure out which operator we have.  */
10032   switch (token->type)
10033     {
10034     case CPP_KEYWORD:
10035       {
10036         enum tree_code op;
10037
10038         /* The keyword should be either `new' or `delete'.  */
10039         if (token->keyword == RID_NEW)
10040           op = NEW_EXPR;
10041         else if (token->keyword == RID_DELETE)
10042           op = DELETE_EXPR;
10043         else
10044           break;
10045
10046         /* Consume the `new' or `delete' token.  */
10047         cp_lexer_consume_token (parser->lexer);
10048
10049         /* Peek at the next token.  */
10050         token = cp_lexer_peek_token (parser->lexer);
10051         /* If it's a `[' token then this is the array variant of the
10052            operator.  */
10053         if (token->type == CPP_OPEN_SQUARE)
10054           {
10055             /* Consume the `[' token.  */
10056             cp_lexer_consume_token (parser->lexer);
10057             /* Look for the `]' token.  */
10058             cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10059             id = ansi_opname (op == NEW_EXPR
10060                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10061           }
10062         /* Otherwise, we have the non-array variant.  */
10063         else
10064           id = ansi_opname (op);
10065
10066         return id;
10067       }
10068
10069     case CPP_PLUS:
10070       id = ansi_opname (PLUS_EXPR);
10071       break;
10072
10073     case CPP_MINUS:
10074       id = ansi_opname (MINUS_EXPR);
10075       break;
10076
10077     case CPP_MULT:
10078       id = ansi_opname (MULT_EXPR);
10079       break;
10080
10081     case CPP_DIV:
10082       id = ansi_opname (TRUNC_DIV_EXPR);
10083       break;
10084
10085     case CPP_MOD:
10086       id = ansi_opname (TRUNC_MOD_EXPR);
10087       break;
10088
10089     case CPP_XOR:
10090       id = ansi_opname (BIT_XOR_EXPR);
10091       break;
10092
10093     case CPP_AND:
10094       id = ansi_opname (BIT_AND_EXPR);
10095       break;
10096
10097     case CPP_OR:
10098       id = ansi_opname (BIT_IOR_EXPR);
10099       break;
10100
10101     case CPP_COMPL:
10102       id = ansi_opname (BIT_NOT_EXPR);
10103       break;
10104
10105     case CPP_NOT:
10106       id = ansi_opname (TRUTH_NOT_EXPR);
10107       break;
10108
10109     case CPP_EQ:
10110       id = ansi_assopname (NOP_EXPR);
10111       break;
10112
10113     case CPP_LESS:
10114       id = ansi_opname (LT_EXPR);
10115       break;
10116
10117     case CPP_GREATER:
10118       id = ansi_opname (GT_EXPR);
10119       break;
10120
10121     case CPP_PLUS_EQ:
10122       id = ansi_assopname (PLUS_EXPR);
10123       break;
10124
10125     case CPP_MINUS_EQ:
10126       id = ansi_assopname (MINUS_EXPR);
10127       break;
10128
10129     case CPP_MULT_EQ:
10130       id = ansi_assopname (MULT_EXPR);
10131       break;
10132
10133     case CPP_DIV_EQ:
10134       id = ansi_assopname (TRUNC_DIV_EXPR);
10135       break;
10136
10137     case CPP_MOD_EQ:
10138       id = ansi_assopname (TRUNC_MOD_EXPR);
10139       break;
10140
10141     case CPP_XOR_EQ:
10142       id = ansi_assopname (BIT_XOR_EXPR);
10143       break;
10144
10145     case CPP_AND_EQ:
10146       id = ansi_assopname (BIT_AND_EXPR);
10147       break;
10148
10149     case CPP_OR_EQ:
10150       id = ansi_assopname (BIT_IOR_EXPR);
10151       break;
10152
10153     case CPP_LSHIFT:
10154       id = ansi_opname (LSHIFT_EXPR);
10155       break;
10156
10157     case CPP_RSHIFT:
10158       id = ansi_opname (RSHIFT_EXPR);
10159       break;
10160
10161     case CPP_LSHIFT_EQ:
10162       id = ansi_assopname (LSHIFT_EXPR);
10163       break;
10164
10165     case CPP_RSHIFT_EQ:
10166       id = ansi_assopname (RSHIFT_EXPR);
10167       break;
10168
10169     case CPP_EQ_EQ:
10170       id = ansi_opname (EQ_EXPR);
10171       break;
10172
10173     case CPP_NOT_EQ:
10174       id = ansi_opname (NE_EXPR);
10175       break;
10176
10177     case CPP_LESS_EQ:
10178       id = ansi_opname (LE_EXPR);
10179       break;
10180
10181     case CPP_GREATER_EQ:
10182       id = ansi_opname (GE_EXPR);
10183       break;
10184
10185     case CPP_AND_AND:
10186       id = ansi_opname (TRUTH_ANDIF_EXPR);
10187       break;
10188
10189     case CPP_OR_OR:
10190       id = ansi_opname (TRUTH_ORIF_EXPR);
10191       break;
10192
10193     case CPP_PLUS_PLUS:
10194       id = ansi_opname (POSTINCREMENT_EXPR);
10195       break;
10196
10197     case CPP_MINUS_MINUS:
10198       id = ansi_opname (PREDECREMENT_EXPR);
10199       break;
10200
10201     case CPP_COMMA:
10202       id = ansi_opname (COMPOUND_EXPR);
10203       break;
10204
10205     case CPP_DEREF_STAR:
10206       id = ansi_opname (MEMBER_REF);
10207       break;
10208
10209     case CPP_DEREF:
10210       id = ansi_opname (COMPONENT_REF);
10211       break;
10212
10213     case CPP_OPEN_PAREN:
10214       /* Consume the `('.  */
10215       cp_lexer_consume_token (parser->lexer);
10216       /* Look for the matching `)'.  */
10217       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
10218       return ansi_opname (CALL_EXPR);
10219
10220     case CPP_OPEN_SQUARE:
10221       /* Consume the `['.  */
10222       cp_lexer_consume_token (parser->lexer);
10223       /* Look for the matching `]'.  */
10224       cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
10225       return ansi_opname (ARRAY_REF);
10226
10227     default:
10228       /* Anything else is an error.  */
10229       break;
10230     }
10231
10232   /* If we have selected an identifier, we need to consume the
10233      operator token.  */
10234   if (id)
10235     cp_lexer_consume_token (parser->lexer);
10236   /* Otherwise, no valid operator name was present.  */
10237   else
10238     {
10239       cp_parser_error (parser, "expected operator");
10240       id = error_mark_node;
10241     }
10242
10243   return id;
10244 }
10245
10246 /* Parse a template-declaration.
10247
10248    template-declaration:
10249      export [opt] template < template-parameter-list > declaration
10250
10251    If MEMBER_P is TRUE, this template-declaration occurs within a
10252    class-specifier.
10253
10254    The grammar rule given by the standard isn't correct.  What
10255    is really meant is:
10256
10257    template-declaration:
10258      export [opt] template-parameter-list-seq
10259        decl-specifier-seq [opt] init-declarator [opt] ;
10260      export [opt] template-parameter-list-seq
10261        function-definition
10262
10263    template-parameter-list-seq:
10264      template-parameter-list-seq [opt]
10265      template < template-parameter-list >  */
10266
10267 static void
10268 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10269 {
10270   /* Check for `export'.  */
10271   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10272     {
10273       /* Consume the `export' token.  */
10274       cp_lexer_consume_token (parser->lexer);
10275       /* Warn that we do not support `export'.  */
10276       warning (0, "keyword %<export%> not implemented, and will be ignored");
10277     }
10278
10279   cp_parser_template_declaration_after_export (parser, member_p);
10280 }
10281
10282 /* Parse a template-parameter-list.
10283
10284    template-parameter-list:
10285      template-parameter
10286      template-parameter-list , template-parameter
10287
10288    Returns a TREE_LIST.  Each node represents a template parameter.
10289    The nodes are connected via their TREE_CHAINs.  */
10290
10291 static tree
10292 cp_parser_template_parameter_list (cp_parser* parser)
10293 {
10294   tree parameter_list = NULL_TREE;
10295
10296   begin_template_parm_list ();
10297   while (true)
10298     {
10299       tree parameter;
10300       bool is_non_type;
10301       bool is_parameter_pack;
10302       location_t parm_loc;
10303
10304       /* Parse the template-parameter.  */
10305       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
10306       parameter = cp_parser_template_parameter (parser, 
10307                                                 &is_non_type,
10308                                                 &is_parameter_pack);
10309       /* Add it to the list.  */
10310       if (parameter != error_mark_node)
10311         parameter_list = process_template_parm (parameter_list,
10312                                                 parm_loc,
10313                                                 parameter,
10314                                                 is_non_type,
10315                                                 is_parameter_pack);
10316       else
10317        {
10318          tree err_parm = build_tree_list (parameter, parameter);
10319          TREE_VALUE (err_parm) = error_mark_node;
10320          parameter_list = chainon (parameter_list, err_parm);
10321        }
10322
10323       /* If the next token is not a `,', we're done.  */
10324       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10325         break;
10326       /* Otherwise, consume the `,' token.  */
10327       cp_lexer_consume_token (parser->lexer);
10328     }
10329
10330   return end_template_parm_list (parameter_list);
10331 }
10332
10333 /* Parse a template-parameter.
10334
10335    template-parameter:
10336      type-parameter
10337      parameter-declaration
10338
10339    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
10340    the parameter.  The TREE_PURPOSE is the default value, if any.
10341    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
10342    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
10343    set to true iff this parameter is a parameter pack. */
10344
10345 static tree
10346 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
10347                               bool *is_parameter_pack)
10348 {
10349   cp_token *token;
10350   cp_parameter_declarator *parameter_declarator;
10351   cp_declarator *id_declarator;
10352   tree parm;
10353
10354   /* Assume it is a type parameter or a template parameter.  */
10355   *is_non_type = false;
10356   /* Assume it not a parameter pack. */
10357   *is_parameter_pack = false;
10358   /* Peek at the next token.  */
10359   token = cp_lexer_peek_token (parser->lexer);
10360   /* If it is `class' or `template', we have a type-parameter.  */
10361   if (token->keyword == RID_TEMPLATE)
10362     return cp_parser_type_parameter (parser, is_parameter_pack);
10363   /* If it is `class' or `typename' we do not know yet whether it is a
10364      type parameter or a non-type parameter.  Consider:
10365
10366        template <typename T, typename T::X X> ...
10367
10368      or:
10369
10370        template <class C, class D*> ...
10371
10372      Here, the first parameter is a type parameter, and the second is
10373      a non-type parameter.  We can tell by looking at the token after
10374      the identifier -- if it is a `,', `=', or `>' then we have a type
10375      parameter.  */
10376   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
10377     {
10378       /* Peek at the token after `class' or `typename'.  */
10379       token = cp_lexer_peek_nth_token (parser->lexer, 2);
10380       /* If it's an ellipsis, we have a template type parameter
10381          pack. */
10382       if (token->type == CPP_ELLIPSIS)
10383         return cp_parser_type_parameter (parser, is_parameter_pack);
10384       /* If it's an identifier, skip it.  */
10385       if (token->type == CPP_NAME)
10386         token = cp_lexer_peek_nth_token (parser->lexer, 3);
10387       /* Now, see if the token looks like the end of a template
10388          parameter.  */
10389       if (token->type == CPP_COMMA
10390           || token->type == CPP_EQ
10391           || token->type == CPP_GREATER)
10392         return cp_parser_type_parameter (parser, is_parameter_pack);
10393     }
10394
10395   /* Otherwise, it is a non-type parameter.
10396
10397      [temp.param]
10398
10399      When parsing a default template-argument for a non-type
10400      template-parameter, the first non-nested `>' is taken as the end
10401      of the template parameter-list rather than a greater-than
10402      operator.  */
10403   *is_non_type = true;
10404   parameter_declarator
10405      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
10406                                         /*parenthesized_p=*/NULL);
10407
10408   /* If the parameter declaration is marked as a parameter pack, set
10409      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
10410      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
10411      grokdeclarator. */
10412   if (parameter_declarator
10413       && parameter_declarator->declarator
10414       && parameter_declarator->declarator->parameter_pack_p)
10415     {
10416       *is_parameter_pack = true;
10417       parameter_declarator->declarator->parameter_pack_p = false;
10418     }
10419
10420   /* If the next token is an ellipsis, and we don't already have it
10421      marked as a parameter pack, then we have a parameter pack (that
10422      has no declarator).  */
10423   if (!*is_parameter_pack
10424       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
10425       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
10426     {
10427       /* Consume the `...'.  */
10428       cp_lexer_consume_token (parser->lexer);
10429       maybe_warn_variadic_templates ();
10430       
10431       *is_parameter_pack = true;
10432     }
10433   /* We might end up with a pack expansion as the type of the non-type
10434      template parameter, in which case this is a non-type template
10435      parameter pack.  */
10436   else if (parameter_declarator
10437            && parameter_declarator->decl_specifiers.type
10438            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
10439     {
10440       *is_parameter_pack = true;
10441       parameter_declarator->decl_specifiers.type = 
10442         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
10443     }
10444
10445   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10446     {
10447       /* Parameter packs cannot have default arguments.  However, a
10448          user may try to do so, so we'll parse them and give an
10449          appropriate diagnostic here.  */
10450
10451       /* Consume the `='.  */
10452       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10453       cp_lexer_consume_token (parser->lexer);
10454       
10455       /* Find the name of the parameter pack.  */     
10456       id_declarator = parameter_declarator->declarator;
10457       while (id_declarator && id_declarator->kind != cdk_id)
10458         id_declarator = id_declarator->declarator;
10459       
10460       if (id_declarator && id_declarator->kind == cdk_id)
10461         error_at (start_token->location,
10462                   "template parameter pack %qD cannot have a default argument",
10463                   id_declarator->u.id.unqualified_name);
10464       else
10465         error_at (start_token->location,
10466                   "template parameter pack cannot have a default argument");
10467       
10468       /* Parse the default argument, but throw away the result.  */
10469       cp_parser_default_argument (parser, /*template_parm_p=*/true);
10470     }
10471
10472   parm = grokdeclarator (parameter_declarator->declarator,
10473                          &parameter_declarator->decl_specifiers,
10474                          PARM, /*initialized=*/0,
10475                          /*attrlist=*/NULL);
10476   if (parm == error_mark_node)
10477     return error_mark_node;
10478
10479   return build_tree_list (parameter_declarator->default_argument, parm);
10480 }
10481
10482 /* Parse a type-parameter.
10483
10484    type-parameter:
10485      class identifier [opt]
10486      class identifier [opt] = type-id
10487      typename identifier [opt]
10488      typename identifier [opt] = type-id
10489      template < template-parameter-list > class identifier [opt]
10490      template < template-parameter-list > class identifier [opt]
10491        = id-expression
10492
10493    GNU Extension (variadic templates):
10494
10495    type-parameter:
10496      class ... identifier [opt]
10497      typename ... identifier [opt]
10498
10499    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
10500    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
10501    the declaration of the parameter.
10502
10503    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
10504
10505 static tree
10506 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
10507 {
10508   cp_token *token;
10509   tree parameter;
10510
10511   /* Look for a keyword to tell us what kind of parameter this is.  */
10512   token = cp_parser_require (parser, CPP_KEYWORD,
10513                              "%<class%>, %<typename%>, or %<template%>");
10514   if (!token)
10515     return error_mark_node;
10516
10517   switch (token->keyword)
10518     {
10519     case RID_CLASS:
10520     case RID_TYPENAME:
10521       {
10522         tree identifier;
10523         tree default_argument;
10524
10525         /* If the next token is an ellipsis, we have a template
10526            argument pack. */
10527         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10528           {
10529             /* Consume the `...' token. */
10530             cp_lexer_consume_token (parser->lexer);
10531             maybe_warn_variadic_templates ();
10532
10533             *is_parameter_pack = true;
10534           }
10535
10536         /* If the next token is an identifier, then it names the
10537            parameter.  */
10538         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
10539           identifier = cp_parser_identifier (parser);
10540         else
10541           identifier = NULL_TREE;
10542
10543         /* Create the parameter.  */
10544         parameter = finish_template_type_parm (class_type_node, identifier);
10545
10546         /* If the next token is an `=', we have a default argument.  */
10547         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10548           {
10549             /* Consume the `=' token.  */
10550             cp_lexer_consume_token (parser->lexer);
10551             /* Parse the default-argument.  */
10552             push_deferring_access_checks (dk_no_deferred);
10553             default_argument = cp_parser_type_id (parser);
10554
10555             /* Template parameter packs cannot have default
10556                arguments. */
10557             if (*is_parameter_pack)
10558               {
10559                 if (identifier)
10560                   error_at (token->location,
10561                             "template parameter pack %qD cannot have a "
10562                             "default argument", identifier);
10563                 else
10564                   error_at (token->location,
10565                             "template parameter packs cannot have "
10566                             "default arguments");
10567                 default_argument = NULL_TREE;
10568               }
10569             pop_deferring_access_checks ();
10570           }
10571         else
10572           default_argument = NULL_TREE;
10573
10574         /* Create the combined representation of the parameter and the
10575            default argument.  */
10576         parameter = build_tree_list (default_argument, parameter);
10577       }
10578       break;
10579
10580     case RID_TEMPLATE:
10581       {
10582         tree parameter_list;
10583         tree identifier;
10584         tree default_argument;
10585
10586         /* Look for the `<'.  */
10587         cp_parser_require (parser, CPP_LESS, "%<<%>");
10588         /* Parse the template-parameter-list.  */
10589         parameter_list = cp_parser_template_parameter_list (parser);
10590         /* Look for the `>'.  */
10591         cp_parser_require (parser, CPP_GREATER, "%<>%>");
10592         /* Look for the `class' keyword.  */
10593         cp_parser_require_keyword (parser, RID_CLASS, "%<class%>");
10594         /* If the next token is an ellipsis, we have a template
10595            argument pack. */
10596         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10597           {
10598             /* Consume the `...' token. */
10599             cp_lexer_consume_token (parser->lexer);
10600             maybe_warn_variadic_templates ();
10601
10602             *is_parameter_pack = true;
10603           }
10604         /* If the next token is an `=', then there is a
10605            default-argument.  If the next token is a `>', we are at
10606            the end of the parameter-list.  If the next token is a `,',
10607            then we are at the end of this parameter.  */
10608         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
10609             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
10610             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10611           {
10612             identifier = cp_parser_identifier (parser);
10613             /* Treat invalid names as if the parameter were nameless.  */
10614             if (identifier == error_mark_node)
10615               identifier = NULL_TREE;
10616           }
10617         else
10618           identifier = NULL_TREE;
10619
10620         /* Create the template parameter.  */
10621         parameter = finish_template_template_parm (class_type_node,
10622                                                    identifier);
10623
10624         /* If the next token is an `=', then there is a
10625            default-argument.  */
10626         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
10627           {
10628             bool is_template;
10629
10630             /* Consume the `='.  */
10631             cp_lexer_consume_token (parser->lexer);
10632             /* Parse the id-expression.  */
10633             push_deferring_access_checks (dk_no_deferred);
10634             /* save token before parsing the id-expression, for error
10635                reporting */
10636             token = cp_lexer_peek_token (parser->lexer);
10637             default_argument
10638               = cp_parser_id_expression (parser,
10639                                          /*template_keyword_p=*/false,
10640                                          /*check_dependency_p=*/true,
10641                                          /*template_p=*/&is_template,
10642                                          /*declarator_p=*/false,
10643                                          /*optional_p=*/false);
10644             if (TREE_CODE (default_argument) == TYPE_DECL)
10645               /* If the id-expression was a template-id that refers to
10646                  a template-class, we already have the declaration here,
10647                  so no further lookup is needed.  */
10648                  ;
10649             else
10650               /* Look up the name.  */
10651               default_argument
10652                 = cp_parser_lookup_name (parser, default_argument,
10653                                          none_type,
10654                                          /*is_template=*/is_template,
10655                                          /*is_namespace=*/false,
10656                                          /*check_dependency=*/true,
10657                                          /*ambiguous_decls=*/NULL,
10658                                          token->location);
10659             /* See if the default argument is valid.  */
10660             default_argument
10661               = check_template_template_default_arg (default_argument);
10662
10663             /* Template parameter packs cannot have default
10664                arguments. */
10665             if (*is_parameter_pack)
10666               {
10667                 if (identifier)
10668                   error_at (token->location,
10669                             "template parameter pack %qD cannot "
10670                             "have a default argument",
10671                             identifier);
10672                 else
10673                   error_at (token->location, "template parameter packs cannot "
10674                             "have default arguments");
10675                 default_argument = NULL_TREE;
10676               }
10677             pop_deferring_access_checks ();
10678           }
10679         else
10680           default_argument = NULL_TREE;
10681
10682         /* Create the combined representation of the parameter and the
10683            default argument.  */
10684         parameter = build_tree_list (default_argument, parameter);
10685       }
10686       break;
10687
10688     default:
10689       gcc_unreachable ();
10690       break;
10691     }
10692
10693   return parameter;
10694 }
10695
10696 /* Parse a template-id.
10697
10698    template-id:
10699      template-name < template-argument-list [opt] >
10700
10701    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
10702    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
10703    returned.  Otherwise, if the template-name names a function, or set
10704    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
10705    names a class, returns a TYPE_DECL for the specialization.
10706
10707    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
10708    uninstantiated templates.  */
10709
10710 static tree
10711 cp_parser_template_id (cp_parser *parser,
10712                        bool template_keyword_p,
10713                        bool check_dependency_p,
10714                        bool is_declaration)
10715 {
10716   int i;
10717   tree templ;
10718   tree arguments;
10719   tree template_id;
10720   cp_token_position start_of_id = 0;
10721   deferred_access_check *chk;
10722   VEC (deferred_access_check,gc) *access_check;
10723   cp_token *next_token = NULL, *next_token_2 = NULL, *token = NULL;
10724   bool is_identifier;
10725
10726   /* If the next token corresponds to a template-id, there is no need
10727      to reparse it.  */
10728   next_token = cp_lexer_peek_token (parser->lexer);
10729   if (next_token->type == CPP_TEMPLATE_ID)
10730     {
10731       struct tree_check *check_value;
10732
10733       /* Get the stored value.  */
10734       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
10735       /* Perform any access checks that were deferred.  */
10736       access_check = check_value->checks;
10737       if (access_check)
10738         {
10739           for (i = 0 ;
10740                VEC_iterate (deferred_access_check, access_check, i, chk) ;
10741                ++i)
10742             {
10743               perform_or_defer_access_check (chk->binfo,
10744                                              chk->decl,
10745                                              chk->diag_decl);
10746             }
10747         }
10748       /* Return the stored value.  */
10749       return check_value->value;
10750     }
10751
10752   /* Avoid performing name lookup if there is no possibility of
10753      finding a template-id.  */
10754   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
10755       || (next_token->type == CPP_NAME
10756           && !cp_parser_nth_token_starts_template_argument_list_p
10757                (parser, 2)))
10758     {
10759       cp_parser_error (parser, "expected template-id");
10760       return error_mark_node;
10761     }
10762
10763   /* Remember where the template-id starts.  */
10764   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
10765     start_of_id = cp_lexer_token_position (parser->lexer, false);
10766
10767   push_deferring_access_checks (dk_deferred);
10768
10769   /* Parse the template-name.  */
10770   is_identifier = false;
10771   token = cp_lexer_peek_token (parser->lexer);
10772   templ = cp_parser_template_name (parser, template_keyword_p,
10773                                    check_dependency_p,
10774                                    is_declaration,
10775                                    &is_identifier);
10776   if (templ == error_mark_node || is_identifier)
10777     {
10778       pop_deferring_access_checks ();
10779       return templ;
10780     }
10781
10782   /* If we find the sequence `[:' after a template-name, it's probably
10783      a digraph-typo for `< ::'. Substitute the tokens and check if we can
10784      parse correctly the argument list.  */
10785   next_token = cp_lexer_peek_token (parser->lexer);
10786   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10787   if (next_token->type == CPP_OPEN_SQUARE
10788       && next_token->flags & DIGRAPH
10789       && next_token_2->type == CPP_COLON
10790       && !(next_token_2->flags & PREV_WHITE))
10791     {
10792       cp_parser_parse_tentatively (parser);
10793       /* Change `:' into `::'.  */
10794       next_token_2->type = CPP_SCOPE;
10795       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
10796          CPP_LESS.  */
10797       cp_lexer_consume_token (parser->lexer);
10798
10799       /* Parse the arguments.  */
10800       arguments = cp_parser_enclosed_template_argument_list (parser);
10801       if (!cp_parser_parse_definitely (parser))
10802         {
10803           /* If we couldn't parse an argument list, then we revert our changes
10804              and return simply an error. Maybe this is not a template-id
10805              after all.  */
10806           next_token_2->type = CPP_COLON;
10807           cp_parser_error (parser, "expected %<<%>");
10808           pop_deferring_access_checks ();
10809           return error_mark_node;
10810         }
10811       /* Otherwise, emit an error about the invalid digraph, but continue
10812          parsing because we got our argument list.  */
10813       if (permerror (next_token->location,
10814                      "%<<::%> cannot begin a template-argument list"))
10815         {
10816           static bool hint = false;
10817           inform (next_token->location,
10818                   "%<<:%> is an alternate spelling for %<[%>."
10819                   " Insert whitespace between %<<%> and %<::%>");
10820           if (!hint && !flag_permissive)
10821             {
10822               inform (next_token->location, "(if you use %<-fpermissive%>"
10823                       " G++ will accept your code)");
10824               hint = true;
10825             }
10826         }
10827     }
10828   else
10829     {
10830       /* Look for the `<' that starts the template-argument-list.  */
10831       if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
10832         {
10833           pop_deferring_access_checks ();
10834           return error_mark_node;
10835         }
10836       /* Parse the arguments.  */
10837       arguments = cp_parser_enclosed_template_argument_list (parser);
10838     }
10839
10840   /* Build a representation of the specialization.  */
10841   if (TREE_CODE (templ) == IDENTIFIER_NODE)
10842     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
10843   else if (DECL_CLASS_TEMPLATE_P (templ)
10844            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
10845     {
10846       bool entering_scope;
10847       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
10848          template (rather than some instantiation thereof) only if
10849          is not nested within some other construct.  For example, in
10850          "template <typename T> void f(T) { A<T>::", A<T> is just an
10851          instantiation of A.  */
10852       entering_scope = (template_parm_scope_p ()
10853                         && cp_lexer_next_token_is (parser->lexer,
10854                                                    CPP_SCOPE));
10855       template_id
10856         = finish_template_type (templ, arguments, entering_scope);
10857     }
10858   else
10859     {
10860       /* If it's not a class-template or a template-template, it should be
10861          a function-template.  */
10862       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
10863                    || TREE_CODE (templ) == OVERLOAD
10864                    || BASELINK_P (templ)));
10865
10866       template_id = lookup_template_function (templ, arguments);
10867     }
10868
10869   /* If parsing tentatively, replace the sequence of tokens that makes
10870      up the template-id with a CPP_TEMPLATE_ID token.  That way,
10871      should we re-parse the token stream, we will not have to repeat
10872      the effort required to do the parse, nor will we issue duplicate
10873      error messages about problems during instantiation of the
10874      template.  */
10875   if (start_of_id)
10876     {
10877       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
10878
10879       /* Reset the contents of the START_OF_ID token.  */
10880       token->type = CPP_TEMPLATE_ID;
10881       /* Retrieve any deferred checks.  Do not pop this access checks yet
10882          so the memory will not be reclaimed during token replacing below.  */
10883       token->u.tree_check_value = GGC_CNEW (struct tree_check);
10884       token->u.tree_check_value->value = template_id;
10885       token->u.tree_check_value->checks = get_deferred_access_checks ();
10886       token->keyword = RID_MAX;
10887
10888       /* Purge all subsequent tokens.  */
10889       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
10890
10891       /* ??? Can we actually assume that, if template_id ==
10892          error_mark_node, we will have issued a diagnostic to the
10893          user, as opposed to simply marking the tentative parse as
10894          failed?  */
10895       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
10896         error_at (token->location, "parse error in template argument list");
10897     }
10898
10899   pop_deferring_access_checks ();
10900   return template_id;
10901 }
10902
10903 /* Parse a template-name.
10904
10905    template-name:
10906      identifier
10907
10908    The standard should actually say:
10909
10910    template-name:
10911      identifier
10912      operator-function-id
10913
10914    A defect report has been filed about this issue.
10915
10916    A conversion-function-id cannot be a template name because they cannot
10917    be part of a template-id. In fact, looking at this code:
10918
10919    a.operator K<int>()
10920
10921    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
10922    It is impossible to call a templated conversion-function-id with an
10923    explicit argument list, since the only allowed template parameter is
10924    the type to which it is converting.
10925
10926    If TEMPLATE_KEYWORD_P is true, then we have just seen the
10927    `template' keyword, in a construction like:
10928
10929      T::template f<3>()
10930
10931    In that case `f' is taken to be a template-name, even though there
10932    is no way of knowing for sure.
10933
10934    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
10935    name refers to a set of overloaded functions, at least one of which
10936    is a template, or an IDENTIFIER_NODE with the name of the template,
10937    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
10938    names are looked up inside uninstantiated templates.  */
10939
10940 static tree
10941 cp_parser_template_name (cp_parser* parser,
10942                          bool template_keyword_p,
10943                          bool check_dependency_p,
10944                          bool is_declaration,
10945                          bool *is_identifier)
10946 {
10947   tree identifier;
10948   tree decl;
10949   tree fns;
10950   cp_token *token = cp_lexer_peek_token (parser->lexer);
10951
10952   /* If the next token is `operator', then we have either an
10953      operator-function-id or a conversion-function-id.  */
10954   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
10955     {
10956       /* We don't know whether we're looking at an
10957          operator-function-id or a conversion-function-id.  */
10958       cp_parser_parse_tentatively (parser);
10959       /* Try an operator-function-id.  */
10960       identifier = cp_parser_operator_function_id (parser);
10961       /* If that didn't work, try a conversion-function-id.  */
10962       if (!cp_parser_parse_definitely (parser))
10963         {
10964           cp_parser_error (parser, "expected template-name");
10965           return error_mark_node;
10966         }
10967     }
10968   /* Look for the identifier.  */
10969   else
10970     identifier = cp_parser_identifier (parser);
10971
10972   /* If we didn't find an identifier, we don't have a template-id.  */
10973   if (identifier == error_mark_node)
10974     return error_mark_node;
10975
10976   /* If the name immediately followed the `template' keyword, then it
10977      is a template-name.  However, if the next token is not `<', then
10978      we do not treat it as a template-name, since it is not being used
10979      as part of a template-id.  This enables us to handle constructs
10980      like:
10981
10982        template <typename T> struct S { S(); };
10983        template <typename T> S<T>::S();
10984
10985      correctly.  We would treat `S' as a template -- if it were `S<T>'
10986      -- but we do not if there is no `<'.  */
10987
10988   if (processing_template_decl
10989       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
10990     {
10991       /* In a declaration, in a dependent context, we pretend that the
10992          "template" keyword was present in order to improve error
10993          recovery.  For example, given:
10994
10995            template <typename T> void f(T::X<int>);
10996
10997          we want to treat "X<int>" as a template-id.  */
10998       if (is_declaration
10999           && !template_keyword_p
11000           && parser->scope && TYPE_P (parser->scope)
11001           && check_dependency_p
11002           && dependent_scope_p (parser->scope)
11003           /* Do not do this for dtors (or ctors), since they never
11004              need the template keyword before their name.  */
11005           && !constructor_name_p (identifier, parser->scope))
11006         {
11007           cp_token_position start = 0;
11008
11009           /* Explain what went wrong.  */
11010           error_at (token->location, "non-template %qD used as template",
11011                     identifier);
11012           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11013                   parser->scope, identifier);
11014           /* If parsing tentatively, find the location of the "<" token.  */
11015           if (cp_parser_simulate_error (parser))
11016             start = cp_lexer_token_position (parser->lexer, true);
11017           /* Parse the template arguments so that we can issue error
11018              messages about them.  */
11019           cp_lexer_consume_token (parser->lexer);
11020           cp_parser_enclosed_template_argument_list (parser);
11021           /* Skip tokens until we find a good place from which to
11022              continue parsing.  */
11023           cp_parser_skip_to_closing_parenthesis (parser,
11024                                                  /*recovering=*/true,
11025                                                  /*or_comma=*/true,
11026                                                  /*consume_paren=*/false);
11027           /* If parsing tentatively, permanently remove the
11028              template argument list.  That will prevent duplicate
11029              error messages from being issued about the missing
11030              "template" keyword.  */
11031           if (start)
11032             cp_lexer_purge_tokens_after (parser->lexer, start);
11033           if (is_identifier)
11034             *is_identifier = true;
11035           return identifier;
11036         }
11037
11038       /* If the "template" keyword is present, then there is generally
11039          no point in doing name-lookup, so we just return IDENTIFIER.
11040          But, if the qualifying scope is non-dependent then we can
11041          (and must) do name-lookup normally.  */
11042       if (template_keyword_p
11043           && (!parser->scope
11044               || (TYPE_P (parser->scope)
11045                   && dependent_type_p (parser->scope))))
11046         return identifier;
11047     }
11048
11049   /* Look up the name.  */
11050   decl = cp_parser_lookup_name (parser, identifier,
11051                                 none_type,
11052                                 /*is_template=*/false,
11053                                 /*is_namespace=*/false,
11054                                 check_dependency_p,
11055                                 /*ambiguous_decls=*/NULL,
11056                                 token->location);
11057   decl = maybe_get_template_decl_from_type_decl (decl);
11058
11059   /* If DECL is a template, then the name was a template-name.  */
11060   if (TREE_CODE (decl) == TEMPLATE_DECL)
11061     ;
11062   else
11063     {
11064       tree fn = NULL_TREE;
11065
11066       /* The standard does not explicitly indicate whether a name that
11067          names a set of overloaded declarations, some of which are
11068          templates, is a template-name.  However, such a name should
11069          be a template-name; otherwise, there is no way to form a
11070          template-id for the overloaded templates.  */
11071       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11072       if (TREE_CODE (fns) == OVERLOAD)
11073         for (fn = fns; fn; fn = OVL_NEXT (fn))
11074           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11075             break;
11076
11077       if (!fn)
11078         {
11079           /* The name does not name a template.  */
11080           cp_parser_error (parser, "expected template-name");
11081           return error_mark_node;
11082         }
11083     }
11084
11085   /* If DECL is dependent, and refers to a function, then just return
11086      its name; we will look it up again during template instantiation.  */
11087   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11088     {
11089       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11090       if (TYPE_P (scope) && dependent_type_p (scope))
11091         return identifier;
11092     }
11093
11094   return decl;
11095 }
11096
11097 /* Parse a template-argument-list.
11098
11099    template-argument-list:
11100      template-argument ... [opt]
11101      template-argument-list , template-argument ... [opt]
11102
11103    Returns a TREE_VEC containing the arguments.  */
11104
11105 static tree
11106 cp_parser_template_argument_list (cp_parser* parser)
11107 {
11108   tree fixed_args[10];
11109   unsigned n_args = 0;
11110   unsigned alloced = 10;
11111   tree *arg_ary = fixed_args;
11112   tree vec;
11113   bool saved_in_template_argument_list_p;
11114   bool saved_ice_p;
11115   bool saved_non_ice_p;
11116
11117   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11118   parser->in_template_argument_list_p = true;
11119   /* Even if the template-id appears in an integral
11120      constant-expression, the contents of the argument list do
11121      not.  */
11122   saved_ice_p = parser->integral_constant_expression_p;
11123   parser->integral_constant_expression_p = false;
11124   saved_non_ice_p = parser->non_integral_constant_expression_p;
11125   parser->non_integral_constant_expression_p = false;
11126   /* Parse the arguments.  */
11127   do
11128     {
11129       tree argument;
11130
11131       if (n_args)
11132         /* Consume the comma.  */
11133         cp_lexer_consume_token (parser->lexer);
11134
11135       /* Parse the template-argument.  */
11136       argument = cp_parser_template_argument (parser);
11137
11138       /* If the next token is an ellipsis, we're expanding a template
11139          argument pack. */
11140       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11141         {
11142           if (argument == error_mark_node)
11143             {
11144               cp_token *token = cp_lexer_peek_token (parser->lexer);
11145               error_at (token->location,
11146                         "expected parameter pack before %<...%>");
11147             }
11148           /* Consume the `...' token. */
11149           cp_lexer_consume_token (parser->lexer);
11150
11151           /* Make the argument into a TYPE_PACK_EXPANSION or
11152              EXPR_PACK_EXPANSION. */
11153           argument = make_pack_expansion (argument);
11154         }
11155
11156       if (n_args == alloced)
11157         {
11158           alloced *= 2;
11159
11160           if (arg_ary == fixed_args)
11161             {
11162               arg_ary = XNEWVEC (tree, alloced);
11163               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11164             }
11165           else
11166             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11167         }
11168       arg_ary[n_args++] = argument;
11169     }
11170   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11171
11172   vec = make_tree_vec (n_args);
11173
11174   while (n_args--)
11175     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11176
11177   if (arg_ary != fixed_args)
11178     free (arg_ary);
11179   parser->non_integral_constant_expression_p = saved_non_ice_p;
11180   parser->integral_constant_expression_p = saved_ice_p;
11181   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11182   return vec;
11183 }
11184
11185 /* Parse a template-argument.
11186
11187    template-argument:
11188      assignment-expression
11189      type-id
11190      id-expression
11191
11192    The representation is that of an assignment-expression, type-id, or
11193    id-expression -- except that the qualified id-expression is
11194    evaluated, so that the value returned is either a DECL or an
11195    OVERLOAD.
11196
11197    Although the standard says "assignment-expression", it forbids
11198    throw-expressions or assignments in the template argument.
11199    Therefore, we use "conditional-expression" instead.  */
11200
11201 static tree
11202 cp_parser_template_argument (cp_parser* parser)
11203 {
11204   tree argument;
11205   bool template_p;
11206   bool address_p;
11207   bool maybe_type_id = false;
11208   cp_token *token = NULL, *argument_start_token = NULL;
11209   cp_id_kind idk;
11210
11211   /* There's really no way to know what we're looking at, so we just
11212      try each alternative in order.
11213
11214        [temp.arg]
11215
11216        In a template-argument, an ambiguity between a type-id and an
11217        expression is resolved to a type-id, regardless of the form of
11218        the corresponding template-parameter.
11219
11220      Therefore, we try a type-id first.  */
11221   cp_parser_parse_tentatively (parser);
11222   argument = cp_parser_template_type_arg (parser);
11223   /* If there was no error parsing the type-id but the next token is a
11224      '>>', our behavior depends on which dialect of C++ we're
11225      parsing. In C++98, we probably found a typo for '> >'. But there
11226      are type-id which are also valid expressions. For instance:
11227
11228      struct X { int operator >> (int); };
11229      template <int V> struct Foo {};
11230      Foo<X () >> 5> r;
11231
11232      Here 'X()' is a valid type-id of a function type, but the user just
11233      wanted to write the expression "X() >> 5". Thus, we remember that we
11234      found a valid type-id, but we still try to parse the argument as an
11235      expression to see what happens. 
11236
11237      In C++0x, the '>>' will be considered two separate '>'
11238      tokens.  */
11239   if (!cp_parser_error_occurred (parser)
11240       && cxx_dialect == cxx98
11241       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11242     {
11243       maybe_type_id = true;
11244       cp_parser_abort_tentative_parse (parser);
11245     }
11246   else
11247     {
11248       /* If the next token isn't a `,' or a `>', then this argument wasn't
11249       really finished. This means that the argument is not a valid
11250       type-id.  */
11251       if (!cp_parser_next_token_ends_template_argument_p (parser))
11252         cp_parser_error (parser, "expected template-argument");
11253       /* If that worked, we're done.  */
11254       if (cp_parser_parse_definitely (parser))
11255         return argument;
11256     }
11257   /* We're still not sure what the argument will be.  */
11258   cp_parser_parse_tentatively (parser);
11259   /* Try a template.  */
11260   argument_start_token = cp_lexer_peek_token (parser->lexer);
11261   argument = cp_parser_id_expression (parser,
11262                                       /*template_keyword_p=*/false,
11263                                       /*check_dependency_p=*/true,
11264                                       &template_p,
11265                                       /*declarator_p=*/false,
11266                                       /*optional_p=*/false);
11267   /* If the next token isn't a `,' or a `>', then this argument wasn't
11268      really finished.  */
11269   if (!cp_parser_next_token_ends_template_argument_p (parser))
11270     cp_parser_error (parser, "expected template-argument");
11271   if (!cp_parser_error_occurred (parser))
11272     {
11273       /* Figure out what is being referred to.  If the id-expression
11274          was for a class template specialization, then we will have a
11275          TYPE_DECL at this point.  There is no need to do name lookup
11276          at this point in that case.  */
11277       if (TREE_CODE (argument) != TYPE_DECL)
11278         argument = cp_parser_lookup_name (parser, argument,
11279                                           none_type,
11280                                           /*is_template=*/template_p,
11281                                           /*is_namespace=*/false,
11282                                           /*check_dependency=*/true,
11283                                           /*ambiguous_decls=*/NULL,
11284                                           argument_start_token->location);
11285       if (TREE_CODE (argument) != TEMPLATE_DECL
11286           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
11287         cp_parser_error (parser, "expected template-name");
11288     }
11289   if (cp_parser_parse_definitely (parser))
11290     return argument;
11291   /* It must be a non-type argument.  There permitted cases are given
11292      in [temp.arg.nontype]:
11293
11294      -- an integral constant-expression of integral or enumeration
11295         type; or
11296
11297      -- the name of a non-type template-parameter; or
11298
11299      -- the name of an object or function with external linkage...
11300
11301      -- the address of an object or function with external linkage...
11302
11303      -- a pointer to member...  */
11304   /* Look for a non-type template parameter.  */
11305   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11306     {
11307       cp_parser_parse_tentatively (parser);
11308       argument = cp_parser_primary_expression (parser,
11309                                                /*address_p=*/false,
11310                                                /*cast_p=*/false,
11311                                                /*template_arg_p=*/true,
11312                                                &idk);
11313       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
11314           || !cp_parser_next_token_ends_template_argument_p (parser))
11315         cp_parser_simulate_error (parser);
11316       if (cp_parser_parse_definitely (parser))
11317         return argument;
11318     }
11319
11320   /* If the next token is "&", the argument must be the address of an
11321      object or function with external linkage.  */
11322   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
11323   if (address_p)
11324     cp_lexer_consume_token (parser->lexer);
11325   /* See if we might have an id-expression.  */
11326   token = cp_lexer_peek_token (parser->lexer);
11327   if (token->type == CPP_NAME
11328       || token->keyword == RID_OPERATOR
11329       || token->type == CPP_SCOPE
11330       || token->type == CPP_TEMPLATE_ID
11331       || token->type == CPP_NESTED_NAME_SPECIFIER)
11332     {
11333       cp_parser_parse_tentatively (parser);
11334       argument = cp_parser_primary_expression (parser,
11335                                                address_p,
11336                                                /*cast_p=*/false,
11337                                                /*template_arg_p=*/true,
11338                                                &idk);
11339       if (cp_parser_error_occurred (parser)
11340           || !cp_parser_next_token_ends_template_argument_p (parser))
11341         cp_parser_abort_tentative_parse (parser);
11342       else
11343         {
11344           if (TREE_CODE (argument) == INDIRECT_REF)
11345             {
11346               gcc_assert (REFERENCE_REF_P (argument));
11347               argument = TREE_OPERAND (argument, 0);
11348             }
11349
11350           if (TREE_CODE (argument) == VAR_DECL)
11351             {
11352               /* A variable without external linkage might still be a
11353                  valid constant-expression, so no error is issued here
11354                  if the external-linkage check fails.  */
11355               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (argument))
11356                 cp_parser_simulate_error (parser);
11357             }
11358           else if (is_overloaded_fn (argument))
11359             /* All overloaded functions are allowed; if the external
11360                linkage test does not pass, an error will be issued
11361                later.  */
11362             ;
11363           else if (address_p
11364                    && (TREE_CODE (argument) == OFFSET_REF
11365                        || TREE_CODE (argument) == SCOPE_REF))
11366             /* A pointer-to-member.  */
11367             ;
11368           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
11369             ;
11370           else
11371             cp_parser_simulate_error (parser);
11372
11373           if (cp_parser_parse_definitely (parser))
11374             {
11375               if (address_p)
11376                 argument = build_x_unary_op (ADDR_EXPR, argument,
11377                                              tf_warning_or_error);
11378               return argument;
11379             }
11380         }
11381     }
11382   /* If the argument started with "&", there are no other valid
11383      alternatives at this point.  */
11384   if (address_p)
11385     {
11386       cp_parser_error (parser, "invalid non-type template argument");
11387       return error_mark_node;
11388     }
11389
11390   /* If the argument wasn't successfully parsed as a type-id followed
11391      by '>>', the argument can only be a constant expression now.
11392      Otherwise, we try parsing the constant-expression tentatively,
11393      because the argument could really be a type-id.  */
11394   if (maybe_type_id)
11395     cp_parser_parse_tentatively (parser);
11396   argument = cp_parser_constant_expression (parser,
11397                                             /*allow_non_constant_p=*/false,
11398                                             /*non_constant_p=*/NULL);
11399   argument = fold_non_dependent_expr (argument);
11400   if (!maybe_type_id)
11401     return argument;
11402   if (!cp_parser_next_token_ends_template_argument_p (parser))
11403     cp_parser_error (parser, "expected template-argument");
11404   if (cp_parser_parse_definitely (parser))
11405     return argument;
11406   /* We did our best to parse the argument as a non type-id, but that
11407      was the only alternative that matched (albeit with a '>' after
11408      it). We can assume it's just a typo from the user, and a
11409      diagnostic will then be issued.  */
11410   return cp_parser_template_type_arg (parser);
11411 }
11412
11413 /* Parse an explicit-instantiation.
11414
11415    explicit-instantiation:
11416      template declaration
11417
11418    Although the standard says `declaration', what it really means is:
11419
11420    explicit-instantiation:
11421      template decl-specifier-seq [opt] declarator [opt] ;
11422
11423    Things like `template int S<int>::i = 5, int S<double>::j;' are not
11424    supposed to be allowed.  A defect report has been filed about this
11425    issue.
11426
11427    GNU Extension:
11428
11429    explicit-instantiation:
11430      storage-class-specifier template
11431        decl-specifier-seq [opt] declarator [opt] ;
11432      function-specifier template
11433        decl-specifier-seq [opt] declarator [opt] ;  */
11434
11435 static void
11436 cp_parser_explicit_instantiation (cp_parser* parser)
11437 {
11438   int declares_class_or_enum;
11439   cp_decl_specifier_seq decl_specifiers;
11440   tree extension_specifier = NULL_TREE;
11441   cp_token *token;
11442
11443   /* Look for an (optional) storage-class-specifier or
11444      function-specifier.  */
11445   if (cp_parser_allow_gnu_extensions_p (parser))
11446     {
11447       extension_specifier
11448         = cp_parser_storage_class_specifier_opt (parser);
11449       if (!extension_specifier)
11450         extension_specifier
11451           = cp_parser_function_specifier_opt (parser,
11452                                               /*decl_specs=*/NULL);
11453     }
11454
11455   /* Look for the `template' keyword.  */
11456   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11457   /* Let the front end know that we are processing an explicit
11458      instantiation.  */
11459   begin_explicit_instantiation ();
11460   /* [temp.explicit] says that we are supposed to ignore access
11461      control while processing explicit instantiation directives.  */
11462   push_deferring_access_checks (dk_no_check);
11463   /* Parse a decl-specifier-seq.  */
11464   token = cp_lexer_peek_token (parser->lexer);
11465   cp_parser_decl_specifier_seq (parser,
11466                                 CP_PARSER_FLAGS_OPTIONAL,
11467                                 &decl_specifiers,
11468                                 &declares_class_or_enum);
11469   /* If there was exactly one decl-specifier, and it declared a class,
11470      and there's no declarator, then we have an explicit type
11471      instantiation.  */
11472   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
11473     {
11474       tree type;
11475
11476       type = check_tag_decl (&decl_specifiers);
11477       /* Turn access control back on for names used during
11478          template instantiation.  */
11479       pop_deferring_access_checks ();
11480       if (type)
11481         do_type_instantiation (type, extension_specifier,
11482                                /*complain=*/tf_error);
11483     }
11484   else
11485     {
11486       cp_declarator *declarator;
11487       tree decl;
11488
11489       /* Parse the declarator.  */
11490       declarator
11491         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
11492                                 /*ctor_dtor_or_conv_p=*/NULL,
11493                                 /*parenthesized_p=*/NULL,
11494                                 /*member_p=*/false);
11495       if (declares_class_or_enum & 2)
11496         cp_parser_check_for_definition_in_return_type (declarator,
11497                                                        decl_specifiers.type,
11498                                                        decl_specifiers.type_location);
11499       if (declarator != cp_error_declarator)
11500         {
11501           decl = grokdeclarator (declarator, &decl_specifiers,
11502                                  NORMAL, 0, &decl_specifiers.attributes);
11503           /* Turn access control back on for names used during
11504              template instantiation.  */
11505           pop_deferring_access_checks ();
11506           /* Do the explicit instantiation.  */
11507           do_decl_instantiation (decl, extension_specifier);
11508         }
11509       else
11510         {
11511           pop_deferring_access_checks ();
11512           /* Skip the body of the explicit instantiation.  */
11513           cp_parser_skip_to_end_of_statement (parser);
11514         }
11515     }
11516   /* We're done with the instantiation.  */
11517   end_explicit_instantiation ();
11518
11519   cp_parser_consume_semicolon_at_end_of_statement (parser);
11520 }
11521
11522 /* Parse an explicit-specialization.
11523
11524    explicit-specialization:
11525      template < > declaration
11526
11527    Although the standard says `declaration', what it really means is:
11528
11529    explicit-specialization:
11530      template <> decl-specifier [opt] init-declarator [opt] ;
11531      template <> function-definition
11532      template <> explicit-specialization
11533      template <> template-declaration  */
11534
11535 static void
11536 cp_parser_explicit_specialization (cp_parser* parser)
11537 {
11538   bool need_lang_pop;
11539   cp_token *token = cp_lexer_peek_token (parser->lexer);
11540
11541   /* Look for the `template' keyword.  */
11542   cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>");
11543   /* Look for the `<'.  */
11544   cp_parser_require (parser, CPP_LESS, "%<<%>");
11545   /* Look for the `>'.  */
11546   cp_parser_require (parser, CPP_GREATER, "%<>%>");
11547   /* We have processed another parameter list.  */
11548   ++parser->num_template_parameter_lists;
11549   /* [temp]
11550
11551      A template ... explicit specialization ... shall not have C
11552      linkage.  */
11553   if (current_lang_name == lang_name_c)
11554     {
11555       error_at (token->location, "template specialization with C linkage");
11556       /* Give it C++ linkage to avoid confusing other parts of the
11557          front end.  */
11558       push_lang_context (lang_name_cplusplus);
11559       need_lang_pop = true;
11560     }
11561   else
11562     need_lang_pop = false;
11563   /* Let the front end know that we are beginning a specialization.  */
11564   if (!begin_specialization ())
11565     {
11566       end_specialization ();
11567       return;
11568     }
11569
11570   /* If the next keyword is `template', we need to figure out whether
11571      or not we're looking a template-declaration.  */
11572   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
11573     {
11574       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
11575           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
11576         cp_parser_template_declaration_after_export (parser,
11577                                                      /*member_p=*/false);
11578       else
11579         cp_parser_explicit_specialization (parser);
11580     }
11581   else
11582     /* Parse the dependent declaration.  */
11583     cp_parser_single_declaration (parser,
11584                                   /*checks=*/NULL,
11585                                   /*member_p=*/false,
11586                                   /*explicit_specialization_p=*/true,
11587                                   /*friend_p=*/NULL);
11588   /* We're done with the specialization.  */
11589   end_specialization ();
11590   /* For the erroneous case of a template with C linkage, we pushed an
11591      implicit C++ linkage scope; exit that scope now.  */
11592   if (need_lang_pop)
11593     pop_lang_context ();
11594   /* We're done with this parameter list.  */
11595   --parser->num_template_parameter_lists;
11596 }
11597
11598 /* Parse a type-specifier.
11599
11600    type-specifier:
11601      simple-type-specifier
11602      class-specifier
11603      enum-specifier
11604      elaborated-type-specifier
11605      cv-qualifier
11606
11607    GNU Extension:
11608
11609    type-specifier:
11610      __complex__
11611
11612    Returns a representation of the type-specifier.  For a
11613    class-specifier, enum-specifier, or elaborated-type-specifier, a
11614    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
11615
11616    The parser flags FLAGS is used to control type-specifier parsing.
11617
11618    If IS_DECLARATION is TRUE, then this type-specifier is appearing
11619    in a decl-specifier-seq.
11620
11621    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
11622    class-specifier, enum-specifier, or elaborated-type-specifier, then
11623    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
11624    if a type is declared; 2 if it is defined.  Otherwise, it is set to
11625    zero.
11626
11627    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
11628    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
11629    is set to FALSE.  */
11630
11631 static tree
11632 cp_parser_type_specifier (cp_parser* parser,
11633                           cp_parser_flags flags,
11634                           cp_decl_specifier_seq *decl_specs,
11635                           bool is_declaration,
11636                           int* declares_class_or_enum,
11637                           bool* is_cv_qualifier)
11638 {
11639   tree type_spec = NULL_TREE;
11640   cp_token *token;
11641   enum rid keyword;
11642   cp_decl_spec ds = ds_last;
11643
11644   /* Assume this type-specifier does not declare a new type.  */
11645   if (declares_class_or_enum)
11646     *declares_class_or_enum = 0;
11647   /* And that it does not specify a cv-qualifier.  */
11648   if (is_cv_qualifier)
11649     *is_cv_qualifier = false;
11650   /* Peek at the next token.  */
11651   token = cp_lexer_peek_token (parser->lexer);
11652
11653   /* If we're looking at a keyword, we can use that to guide the
11654      production we choose.  */
11655   keyword = token->keyword;
11656   switch (keyword)
11657     {
11658     case RID_ENUM:
11659       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11660         goto elaborated_type_specifier;
11661
11662       /* Look for the enum-specifier.  */
11663       type_spec = cp_parser_enum_specifier (parser);
11664       /* If that worked, we're done.  */
11665       if (type_spec)
11666         {
11667           if (declares_class_or_enum)
11668             *declares_class_or_enum = 2;
11669           if (decl_specs)
11670             cp_parser_set_decl_spec_type (decl_specs,
11671                                           type_spec,
11672                                           token->location,
11673                                           /*user_defined_p=*/true);
11674           return type_spec;
11675         }
11676       else
11677         goto elaborated_type_specifier;
11678
11679       /* Any of these indicate either a class-specifier, or an
11680          elaborated-type-specifier.  */
11681     case RID_CLASS:
11682     case RID_STRUCT:
11683     case RID_UNION:
11684       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
11685         goto elaborated_type_specifier;
11686
11687       /* Parse tentatively so that we can back up if we don't find a
11688          class-specifier.  */
11689       cp_parser_parse_tentatively (parser);
11690       /* Look for the class-specifier.  */
11691       type_spec = cp_parser_class_specifier (parser);
11692       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
11693       /* If that worked, we're done.  */
11694       if (cp_parser_parse_definitely (parser))
11695         {
11696           if (declares_class_or_enum)
11697             *declares_class_or_enum = 2;
11698           if (decl_specs)
11699             cp_parser_set_decl_spec_type (decl_specs,
11700                                           type_spec,
11701                                           token->location,
11702                                           /*user_defined_p=*/true);
11703           return type_spec;
11704         }
11705
11706       /* Fall through.  */
11707     elaborated_type_specifier:
11708       /* We're declaring (not defining) a class or enum.  */
11709       if (declares_class_or_enum)
11710         *declares_class_or_enum = 1;
11711
11712       /* Fall through.  */
11713     case RID_TYPENAME:
11714       /* Look for an elaborated-type-specifier.  */
11715       type_spec
11716         = (cp_parser_elaborated_type_specifier
11717            (parser,
11718             decl_specs && decl_specs->specs[(int) ds_friend],
11719             is_declaration));
11720       if (decl_specs)
11721         cp_parser_set_decl_spec_type (decl_specs,
11722                                       type_spec,
11723                                       token->location,
11724                                       /*user_defined_p=*/true);
11725       return type_spec;
11726
11727     case RID_CONST:
11728       ds = ds_const;
11729       if (is_cv_qualifier)
11730         *is_cv_qualifier = true;
11731       break;
11732
11733     case RID_VOLATILE:
11734       ds = ds_volatile;
11735       if (is_cv_qualifier)
11736         *is_cv_qualifier = true;
11737       break;
11738
11739     case RID_RESTRICT:
11740       ds = ds_restrict;
11741       if (is_cv_qualifier)
11742         *is_cv_qualifier = true;
11743       break;
11744
11745     case RID_COMPLEX:
11746       /* The `__complex__' keyword is a GNU extension.  */
11747       ds = ds_complex;
11748       break;
11749
11750     default:
11751       break;
11752     }
11753
11754   /* Handle simple keywords.  */
11755   if (ds != ds_last)
11756     {
11757       if (decl_specs)
11758         {
11759           ++decl_specs->specs[(int)ds];
11760           decl_specs->any_specifiers_p = true;
11761         }
11762       return cp_lexer_consume_token (parser->lexer)->u.value;
11763     }
11764
11765   /* If we do not already have a type-specifier, assume we are looking
11766      at a simple-type-specifier.  */
11767   type_spec = cp_parser_simple_type_specifier (parser,
11768                                                decl_specs,
11769                                                flags);
11770
11771   /* If we didn't find a type-specifier, and a type-specifier was not
11772      optional in this context, issue an error message.  */
11773   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
11774     {
11775       cp_parser_error (parser, "expected type specifier");
11776       return error_mark_node;
11777     }
11778
11779   return type_spec;
11780 }
11781
11782 /* Parse a simple-type-specifier.
11783
11784    simple-type-specifier:
11785      :: [opt] nested-name-specifier [opt] type-name
11786      :: [opt] nested-name-specifier template template-id
11787      char
11788      wchar_t
11789      bool
11790      short
11791      int
11792      long
11793      signed
11794      unsigned
11795      float
11796      double
11797      void
11798
11799    C++0x Extension:
11800
11801    simple-type-specifier:
11802      auto
11803      decltype ( expression )   
11804      char16_t
11805      char32_t
11806
11807    GNU Extension:
11808
11809    simple-type-specifier:
11810      __typeof__ unary-expression
11811      __typeof__ ( type-id )
11812
11813    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
11814    appropriately updated.  */
11815
11816 static tree
11817 cp_parser_simple_type_specifier (cp_parser* parser,
11818                                  cp_decl_specifier_seq *decl_specs,
11819                                  cp_parser_flags flags)
11820 {
11821   tree type = NULL_TREE;
11822   cp_token *token;
11823
11824   /* Peek at the next token.  */
11825   token = cp_lexer_peek_token (parser->lexer);
11826
11827   /* If we're looking at a keyword, things are easy.  */
11828   switch (token->keyword)
11829     {
11830     case RID_CHAR:
11831       if (decl_specs)
11832         decl_specs->explicit_char_p = true;
11833       type = char_type_node;
11834       break;
11835     case RID_CHAR16:
11836       type = char16_type_node;
11837       break;
11838     case RID_CHAR32:
11839       type = char32_type_node;
11840       break;
11841     case RID_WCHAR:
11842       type = wchar_type_node;
11843       break;
11844     case RID_BOOL:
11845       type = boolean_type_node;
11846       break;
11847     case RID_SHORT:
11848       if (decl_specs)
11849         ++decl_specs->specs[(int) ds_short];
11850       type = short_integer_type_node;
11851       break;
11852     case RID_INT:
11853       if (decl_specs)
11854         decl_specs->explicit_int_p = true;
11855       type = integer_type_node;
11856       break;
11857     case RID_LONG:
11858       if (decl_specs)
11859         ++decl_specs->specs[(int) ds_long];
11860       type = long_integer_type_node;
11861       break;
11862     case RID_SIGNED:
11863       if (decl_specs)
11864         ++decl_specs->specs[(int) ds_signed];
11865       type = integer_type_node;
11866       break;
11867     case RID_UNSIGNED:
11868       if (decl_specs)
11869         ++decl_specs->specs[(int) ds_unsigned];
11870       type = unsigned_type_node;
11871       break;
11872     case RID_FLOAT:
11873       type = float_type_node;
11874       break;
11875     case RID_DOUBLE:
11876       type = double_type_node;
11877       break;
11878     case RID_VOID:
11879       type = void_type_node;
11880       break;
11881       
11882     case RID_AUTO:
11883       maybe_warn_cpp0x ("C++0x auto");
11884       type = make_auto ();
11885       break;
11886
11887     case RID_DECLTYPE:
11888       /* Parse the `decltype' type.  */
11889       type = cp_parser_decltype (parser);
11890
11891       if (decl_specs)
11892         cp_parser_set_decl_spec_type (decl_specs, type,
11893                                       token->location,
11894                                       /*user_defined_p=*/true);
11895
11896       return type;
11897
11898     case RID_TYPEOF:
11899       /* Consume the `typeof' token.  */
11900       cp_lexer_consume_token (parser->lexer);
11901       /* Parse the operand to `typeof'.  */
11902       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
11903       /* If it is not already a TYPE, take its type.  */
11904       if (!TYPE_P (type))
11905         type = finish_typeof (type);
11906
11907       if (decl_specs)
11908         cp_parser_set_decl_spec_type (decl_specs, type,
11909                                       token->location,
11910                                       /*user_defined_p=*/true);
11911
11912       return type;
11913
11914     default:
11915       break;
11916     }
11917
11918   /* If the type-specifier was for a built-in type, we're done.  */
11919   if (type)
11920     {
11921       tree id;
11922
11923       /* Record the type.  */
11924       if (decl_specs
11925           && (token->keyword != RID_SIGNED
11926               && token->keyword != RID_UNSIGNED
11927               && token->keyword != RID_SHORT
11928               && token->keyword != RID_LONG))
11929         cp_parser_set_decl_spec_type (decl_specs,
11930                                       type,
11931                                       token->location,
11932                                       /*user_defined=*/false);
11933       if (decl_specs)
11934         decl_specs->any_specifiers_p = true;
11935
11936       /* Consume the token.  */
11937       id = cp_lexer_consume_token (parser->lexer)->u.value;
11938
11939       /* There is no valid C++ program where a non-template type is
11940          followed by a "<".  That usually indicates that the user thought
11941          that the type was a template.  */
11942       cp_parser_check_for_invalid_template_id (parser, type, token->location);
11943
11944       return TYPE_NAME (type);
11945     }
11946
11947   /* The type-specifier must be a user-defined type.  */
11948   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
11949     {
11950       bool qualified_p;
11951       bool global_p;
11952
11953       /* Don't gobble tokens or issue error messages if this is an
11954          optional type-specifier.  */
11955       if (flags & CP_PARSER_FLAGS_OPTIONAL)
11956         cp_parser_parse_tentatively (parser);
11957
11958       /* Look for the optional `::' operator.  */
11959       global_p
11960         = (cp_parser_global_scope_opt (parser,
11961                                        /*current_scope_valid_p=*/false)
11962            != NULL_TREE);
11963       /* Look for the nested-name specifier.  */
11964       qualified_p
11965         = (cp_parser_nested_name_specifier_opt (parser,
11966                                                 /*typename_keyword_p=*/false,
11967                                                 /*check_dependency_p=*/true,
11968                                                 /*type_p=*/false,
11969                                                 /*is_declaration=*/false)
11970            != NULL_TREE);
11971       token = cp_lexer_peek_token (parser->lexer);
11972       /* If we have seen a nested-name-specifier, and the next token
11973          is `template', then we are using the template-id production.  */
11974       if (parser->scope
11975           && cp_parser_optional_template_keyword (parser))
11976         {
11977           /* Look for the template-id.  */
11978           type = cp_parser_template_id (parser,
11979                                         /*template_keyword_p=*/true,
11980                                         /*check_dependency_p=*/true,
11981                                         /*is_declaration=*/false);
11982           /* If the template-id did not name a type, we are out of
11983              luck.  */
11984           if (TREE_CODE (type) != TYPE_DECL)
11985             {
11986               cp_parser_error (parser, "expected template-id for type");
11987               type = NULL_TREE;
11988             }
11989         }
11990       /* Otherwise, look for a type-name.  */
11991       else
11992         type = cp_parser_type_name (parser);
11993       /* Keep track of all name-lookups performed in class scopes.  */
11994       if (type
11995           && !global_p
11996           && !qualified_p
11997           && TREE_CODE (type) == TYPE_DECL
11998           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
11999         maybe_note_name_used_in_class (DECL_NAME (type), type);
12000       /* If it didn't work out, we don't have a TYPE.  */
12001       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12002           && !cp_parser_parse_definitely (parser))
12003         type = NULL_TREE;
12004       if (type && decl_specs)
12005         cp_parser_set_decl_spec_type (decl_specs, type,
12006                                       token->location,
12007                                       /*user_defined=*/true);
12008     }
12009
12010   /* If we didn't get a type-name, issue an error message.  */
12011   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12012     {
12013       cp_parser_error (parser, "expected type-name");
12014       return error_mark_node;
12015     }
12016
12017   /* There is no valid C++ program where a non-template type is
12018      followed by a "<".  That usually indicates that the user thought
12019      that the type was a template.  */
12020   if (type && type != error_mark_node)
12021     {
12022       /* As a last-ditch effort, see if TYPE is an Objective-C type.
12023          If it is, then the '<'...'>' enclose protocol names rather than
12024          template arguments, and so everything is fine.  */
12025       if (c_dialect_objc ()
12026           && (objc_is_id (type) || objc_is_class_name (type)))
12027         {
12028           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12029           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12030
12031           /* Clobber the "unqualified" type previously entered into
12032              DECL_SPECS with the new, improved protocol-qualified version.  */
12033           if (decl_specs)
12034             decl_specs->type = qual_type;
12035
12036           return qual_type;
12037         }
12038
12039       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12040                                                token->location);
12041     }
12042
12043   return type;
12044 }
12045
12046 /* Parse a type-name.
12047
12048    type-name:
12049      class-name
12050      enum-name
12051      typedef-name
12052
12053    enum-name:
12054      identifier
12055
12056    typedef-name:
12057      identifier
12058
12059    Returns a TYPE_DECL for the type.  */
12060
12061 static tree
12062 cp_parser_type_name (cp_parser* parser)
12063 {
12064   tree type_decl;
12065
12066   /* We can't know yet whether it is a class-name or not.  */
12067   cp_parser_parse_tentatively (parser);
12068   /* Try a class-name.  */
12069   type_decl = cp_parser_class_name (parser,
12070                                     /*typename_keyword_p=*/false,
12071                                     /*template_keyword_p=*/false,
12072                                     none_type,
12073                                     /*check_dependency_p=*/true,
12074                                     /*class_head_p=*/false,
12075                                     /*is_declaration=*/false);
12076   /* If it's not a class-name, keep looking.  */
12077   if (!cp_parser_parse_definitely (parser))
12078     {
12079       /* It must be a typedef-name or an enum-name.  */
12080       return cp_parser_nonclass_name (parser);
12081     }
12082
12083   return type_decl;
12084 }
12085
12086 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12087
12088    enum-name:
12089      identifier
12090
12091    typedef-name:
12092      identifier
12093
12094    Returns a TYPE_DECL for the type.  */
12095
12096 static tree
12097 cp_parser_nonclass_name (cp_parser* parser)
12098 {
12099   tree type_decl;
12100   tree identifier;
12101
12102   cp_token *token = cp_lexer_peek_token (parser->lexer);
12103   identifier = cp_parser_identifier (parser);
12104   if (identifier == error_mark_node)
12105     return error_mark_node;
12106
12107   /* Look up the type-name.  */
12108   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12109
12110   if (TREE_CODE (type_decl) != TYPE_DECL
12111       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12112     {
12113       /* See if this is an Objective-C type.  */
12114       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12115       tree type = objc_get_protocol_qualified_type (identifier, protos);
12116       if (type)
12117         type_decl = TYPE_NAME (type);
12118     }
12119   
12120   /* Issue an error if we did not find a type-name.  */
12121   if (TREE_CODE (type_decl) != TYPE_DECL)
12122     {
12123       if (!cp_parser_simulate_error (parser))
12124         cp_parser_name_lookup_error (parser, identifier, type_decl,
12125                                      "is not a type", token->location);
12126       return error_mark_node;
12127     }
12128   /* Remember that the name was used in the definition of the
12129      current class so that we can check later to see if the
12130      meaning would have been different after the class was
12131      entirely defined.  */
12132   else if (type_decl != error_mark_node
12133            && !parser->scope)
12134     maybe_note_name_used_in_class (identifier, type_decl);
12135   
12136   return type_decl;
12137 }
12138
12139 /* Parse an elaborated-type-specifier.  Note that the grammar given
12140    here incorporates the resolution to DR68.
12141
12142    elaborated-type-specifier:
12143      class-key :: [opt] nested-name-specifier [opt] identifier
12144      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12145      enum-key :: [opt] nested-name-specifier [opt] identifier
12146      typename :: [opt] nested-name-specifier identifier
12147      typename :: [opt] nested-name-specifier template [opt]
12148        template-id
12149
12150    GNU extension:
12151
12152    elaborated-type-specifier:
12153      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12154      class-key attributes :: [opt] nested-name-specifier [opt]
12155                template [opt] template-id
12156      enum attributes :: [opt] nested-name-specifier [opt] identifier
12157
12158    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12159    declared `friend'.  If IS_DECLARATION is TRUE, then this
12160    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12161    something is being declared.
12162
12163    Returns the TYPE specified.  */
12164
12165 static tree
12166 cp_parser_elaborated_type_specifier (cp_parser* parser,
12167                                      bool is_friend,
12168                                      bool is_declaration)
12169 {
12170   enum tag_types tag_type;
12171   tree identifier;
12172   tree type = NULL_TREE;
12173   tree attributes = NULL_TREE;
12174   tree globalscope;
12175   cp_token *token = NULL;
12176
12177   /* See if we're looking at the `enum' keyword.  */
12178   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12179     {
12180       /* Consume the `enum' token.  */
12181       cp_lexer_consume_token (parser->lexer);
12182       /* Remember that it's an enumeration type.  */
12183       tag_type = enum_type;
12184       /* Parse the optional `struct' or `class' key (for C++0x scoped
12185          enums).  */
12186       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12187           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12188         {
12189           if (cxx_dialect == cxx98)
12190             maybe_warn_cpp0x ("scoped enums");
12191
12192           /* Consume the `struct' or `class'.  */
12193           cp_lexer_consume_token (parser->lexer);
12194         }
12195       /* Parse the attributes.  */
12196       attributes = cp_parser_attributes_opt (parser);
12197     }
12198   /* Or, it might be `typename'.  */
12199   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12200                                            RID_TYPENAME))
12201     {
12202       /* Consume the `typename' token.  */
12203       cp_lexer_consume_token (parser->lexer);
12204       /* Remember that it's a `typename' type.  */
12205       tag_type = typename_type;
12206     }
12207   /* Otherwise it must be a class-key.  */
12208   else
12209     {
12210       tag_type = cp_parser_class_key (parser);
12211       if (tag_type == none_type)
12212         return error_mark_node;
12213       /* Parse the attributes.  */
12214       attributes = cp_parser_attributes_opt (parser);
12215     }
12216
12217   /* Look for the `::' operator.  */
12218   globalscope =  cp_parser_global_scope_opt (parser,
12219                                              /*current_scope_valid_p=*/false);
12220   /* Look for the nested-name-specifier.  */
12221   if (tag_type == typename_type && !globalscope)
12222     {
12223       if (!cp_parser_nested_name_specifier (parser,
12224                                            /*typename_keyword_p=*/true,
12225                                            /*check_dependency_p=*/true,
12226                                            /*type_p=*/true,
12227                                             is_declaration))
12228         return error_mark_node;
12229     }
12230   else
12231     /* Even though `typename' is not present, the proposed resolution
12232        to Core Issue 180 says that in `class A<T>::B', `B' should be
12233        considered a type-name, even if `A<T>' is dependent.  */
12234     cp_parser_nested_name_specifier_opt (parser,
12235                                          /*typename_keyword_p=*/true,
12236                                          /*check_dependency_p=*/true,
12237                                          /*type_p=*/true,
12238                                          is_declaration);
12239  /* For everything but enumeration types, consider a template-id.
12240     For an enumeration type, consider only a plain identifier.  */
12241   if (tag_type != enum_type)
12242     {
12243       bool template_p = false;
12244       tree decl;
12245
12246       /* Allow the `template' keyword.  */
12247       template_p = cp_parser_optional_template_keyword (parser);
12248       /* If we didn't see `template', we don't know if there's a
12249          template-id or not.  */
12250       if (!template_p)
12251         cp_parser_parse_tentatively (parser);
12252       /* Parse the template-id.  */
12253       token = cp_lexer_peek_token (parser->lexer);
12254       decl = cp_parser_template_id (parser, template_p,
12255                                     /*check_dependency_p=*/true,
12256                                     is_declaration);
12257       /* If we didn't find a template-id, look for an ordinary
12258          identifier.  */
12259       if (!template_p && !cp_parser_parse_definitely (parser))
12260         ;
12261       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
12262          in effect, then we must assume that, upon instantiation, the
12263          template will correspond to a class.  */
12264       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
12265                && tag_type == typename_type)
12266         type = make_typename_type (parser->scope, decl,
12267                                    typename_type,
12268                                    /*complain=*/tf_error);
12269       /* If the `typename' keyword is in effect and DECL is not a type
12270          decl. Then type is non existant.   */
12271       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
12272         type = NULL_TREE; 
12273       else 
12274         type = TREE_TYPE (decl);
12275     }
12276
12277   if (!type)
12278     {
12279       token = cp_lexer_peek_token (parser->lexer);
12280       identifier = cp_parser_identifier (parser);
12281
12282       if (identifier == error_mark_node)
12283         {
12284           parser->scope = NULL_TREE;
12285           return error_mark_node;
12286         }
12287
12288       /* For a `typename', we needn't call xref_tag.  */
12289       if (tag_type == typename_type
12290           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
12291         return cp_parser_make_typename_type (parser, parser->scope,
12292                                              identifier,
12293                                              token->location);
12294       /* Look up a qualified name in the usual way.  */
12295       if (parser->scope)
12296         {
12297           tree decl;
12298           tree ambiguous_decls;
12299
12300           decl = cp_parser_lookup_name (parser, identifier,
12301                                         tag_type,
12302                                         /*is_template=*/false,
12303                                         /*is_namespace=*/false,
12304                                         /*check_dependency=*/true,
12305                                         &ambiguous_decls,
12306                                         token->location);
12307
12308           /* If the lookup was ambiguous, an error will already have been
12309              issued.  */
12310           if (ambiguous_decls)
12311             return error_mark_node;
12312
12313           /* If we are parsing friend declaration, DECL may be a
12314              TEMPLATE_DECL tree node here.  However, we need to check
12315              whether this TEMPLATE_DECL results in valid code.  Consider
12316              the following example:
12317
12318                namespace N {
12319                  template <class T> class C {};
12320                }
12321                class X {
12322                  template <class T> friend class N::C; // #1, valid code
12323                };
12324                template <class T> class Y {
12325                  friend class N::C;                    // #2, invalid code
12326                };
12327
12328              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
12329              name lookup of `N::C'.  We see that friend declaration must
12330              be template for the code to be valid.  Note that
12331              processing_template_decl does not work here since it is
12332              always 1 for the above two cases.  */
12333
12334           decl = (cp_parser_maybe_treat_template_as_class
12335                   (decl, /*tag_name_p=*/is_friend
12336                          && parser->num_template_parameter_lists));
12337
12338           if (TREE_CODE (decl) != TYPE_DECL)
12339             {
12340               cp_parser_diagnose_invalid_type_name (parser,
12341                                                     parser->scope,
12342                                                     identifier,
12343                                                     token->location);
12344               return error_mark_node;
12345             }
12346
12347           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
12348             {
12349               bool allow_template = (parser->num_template_parameter_lists
12350                                       || DECL_SELF_REFERENCE_P (decl));
12351               type = check_elaborated_type_specifier (tag_type, decl, 
12352                                                       allow_template);
12353
12354               if (type == error_mark_node)
12355                 return error_mark_node;
12356             }
12357
12358           /* Forward declarations of nested types, such as
12359
12360                class C1::C2;
12361                class C1::C2::C3;
12362
12363              are invalid unless all components preceding the final '::'
12364              are complete.  If all enclosing types are complete, these
12365              declarations become merely pointless.
12366
12367              Invalid forward declarations of nested types are errors
12368              caught elsewhere in parsing.  Those that are pointless arrive
12369              here.  */
12370
12371           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
12372               && !is_friend && !processing_explicit_instantiation)
12373             warning (0, "declaration %qD does not declare anything", decl);
12374
12375           type = TREE_TYPE (decl);
12376         }
12377       else
12378         {
12379           /* An elaborated-type-specifier sometimes introduces a new type and
12380              sometimes names an existing type.  Normally, the rule is that it
12381              introduces a new type only if there is not an existing type of
12382              the same name already in scope.  For example, given:
12383
12384                struct S {};
12385                void f() { struct S s; }
12386
12387              the `struct S' in the body of `f' is the same `struct S' as in
12388              the global scope; the existing definition is used.  However, if
12389              there were no global declaration, this would introduce a new
12390              local class named `S'.
12391
12392              An exception to this rule applies to the following code:
12393
12394                namespace N { struct S; }
12395
12396              Here, the elaborated-type-specifier names a new type
12397              unconditionally; even if there is already an `S' in the
12398              containing scope this declaration names a new type.
12399              This exception only applies if the elaborated-type-specifier
12400              forms the complete declaration:
12401
12402                [class.name]
12403
12404                A declaration consisting solely of `class-key identifier ;' is
12405                either a redeclaration of the name in the current scope or a
12406                forward declaration of the identifier as a class name.  It
12407                introduces the name into the current scope.
12408
12409              We are in this situation precisely when the next token is a `;'.
12410
12411              An exception to the exception is that a `friend' declaration does
12412              *not* name a new type; i.e., given:
12413
12414                struct S { friend struct T; };
12415
12416              `T' is not a new type in the scope of `S'.
12417
12418              Also, `new struct S' or `sizeof (struct S)' never results in the
12419              definition of a new type; a new type can only be declared in a
12420              declaration context.  */
12421
12422           tag_scope ts;
12423           bool template_p;
12424
12425           if (is_friend)
12426             /* Friends have special name lookup rules.  */
12427             ts = ts_within_enclosing_non_class;
12428           else if (is_declaration
12429                    && cp_lexer_next_token_is (parser->lexer,
12430                                               CPP_SEMICOLON))
12431             /* This is a `class-key identifier ;' */
12432             ts = ts_current;
12433           else
12434             ts = ts_global;
12435
12436           template_p =
12437             (parser->num_template_parameter_lists
12438              && (cp_parser_next_token_starts_class_definition_p (parser)
12439                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
12440           /* An unqualified name was used to reference this type, so
12441              there were no qualifying templates.  */
12442           if (!cp_parser_check_template_parameters (parser,
12443                                                     /*num_templates=*/0,
12444                                                     token->location,
12445                                                     /*declarator=*/NULL))
12446             return error_mark_node;
12447           type = xref_tag (tag_type, identifier, ts, template_p);
12448         }
12449     }
12450
12451   if (type == error_mark_node)
12452     return error_mark_node;
12453
12454   /* Allow attributes on forward declarations of classes.  */
12455   if (attributes)
12456     {
12457       if (TREE_CODE (type) == TYPENAME_TYPE)
12458         warning (OPT_Wattributes,
12459                  "attributes ignored on uninstantiated type");
12460       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
12461                && ! processing_explicit_instantiation)
12462         warning (OPT_Wattributes,
12463                  "attributes ignored on template instantiation");
12464       else if (is_declaration && cp_parser_declares_only_class_p (parser))
12465         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
12466       else
12467         warning (OPT_Wattributes,
12468                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
12469     }
12470
12471   if (tag_type != enum_type)
12472     cp_parser_check_class_key (tag_type, type);
12473
12474   /* A "<" cannot follow an elaborated type specifier.  If that
12475      happens, the user was probably trying to form a template-id.  */
12476   cp_parser_check_for_invalid_template_id (parser, type, token->location);
12477
12478   return type;
12479 }
12480
12481 /* Parse an enum-specifier.
12482
12483    enum-specifier:
12484      enum-key identifier [opt] enum-base [opt] { enumerator-list [opt] }
12485
12486    enum-key:
12487      enum
12488      enum class   [C++0x]
12489      enum struct  [C++0x]
12490
12491    enum-base:   [C++0x]
12492      : type-specifier-seq
12493
12494    GNU Extensions:
12495      enum-key attributes[opt] identifier [opt] enum-base [opt] 
12496        { enumerator-list [opt] }attributes[opt]
12497
12498    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
12499    if the token stream isn't an enum-specifier after all.  */
12500
12501 static tree
12502 cp_parser_enum_specifier (cp_parser* parser)
12503 {
12504   tree identifier;
12505   tree type;
12506   tree attributes;
12507   bool scoped_enum_p = false;
12508   bool has_underlying_type = false;
12509   tree underlying_type = NULL_TREE;
12510
12511   /* Parse tentatively so that we can back up if we don't find a
12512      enum-specifier.  */
12513   cp_parser_parse_tentatively (parser);
12514
12515   /* Caller guarantees that the current token is 'enum', an identifier
12516      possibly follows, and the token after that is an opening brace.
12517      If we don't have an identifier, fabricate an anonymous name for
12518      the enumeration being defined.  */
12519   cp_lexer_consume_token (parser->lexer);
12520
12521   /* Parse the "class" or "struct", which indicates a scoped
12522      enumeration type in C++0x.  */
12523   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12524       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12525     {
12526       if (cxx_dialect == cxx98)
12527         maybe_warn_cpp0x ("scoped enums");
12528
12529       /* Consume the `struct' or `class' token.  */
12530       cp_lexer_consume_token (parser->lexer);
12531
12532       scoped_enum_p = true;
12533     }
12534
12535   attributes = cp_parser_attributes_opt (parser);
12536
12537   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12538     identifier = cp_parser_identifier (parser);
12539   else
12540     identifier = make_anon_name ();
12541
12542   /* Check for the `:' that denotes a specified underlying type in C++0x.
12543      Note that a ':' could also indicate a bitfield width, however.  */
12544   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
12545     {
12546       cp_decl_specifier_seq type_specifiers;
12547
12548       /* Consume the `:'.  */
12549       cp_lexer_consume_token (parser->lexer);
12550
12551       /* Parse the type-specifier-seq.  */
12552       cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
12553                                     /*is_trailing_return=*/false,
12554                                     &type_specifiers);
12555
12556       /* At this point this is surely not elaborated type specifier.  */
12557       if (!cp_parser_parse_definitely (parser))
12558         return NULL_TREE;
12559
12560       if (cxx_dialect == cxx98)
12561         maybe_warn_cpp0x ("scoped enums");
12562
12563       has_underlying_type = true;
12564
12565       /* If that didn't work, stop.  */
12566       if (type_specifiers.type != error_mark_node)
12567         {
12568           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
12569                                             /*initialized=*/0, NULL);
12570           if (underlying_type == error_mark_node)
12571             underlying_type = NULL_TREE;
12572         }
12573     }
12574
12575   /* Look for the `{' but don't consume it yet.  */
12576   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
12577     {
12578       cp_parser_error (parser, "expected %<{%>");
12579       if (has_underlying_type)
12580         return NULL_TREE;
12581     }
12582
12583   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
12584     return NULL_TREE;
12585
12586   /* Issue an error message if type-definitions are forbidden here.  */
12587   if (!cp_parser_check_type_definition (parser))
12588     type = error_mark_node;
12589   else
12590     /* Create the new type.  We do this before consuming the opening
12591        brace so the enum will be recorded as being on the line of its
12592        tag (or the 'enum' keyword, if there is no tag).  */
12593     type = start_enum (identifier, underlying_type, scoped_enum_p);
12594   
12595   /* Consume the opening brace.  */
12596   cp_lexer_consume_token (parser->lexer);
12597
12598   if (type == error_mark_node)
12599     {
12600       cp_parser_skip_to_end_of_block_or_statement (parser);
12601       return error_mark_node;
12602     }
12603
12604   /* If the next token is not '}', then there are some enumerators.  */
12605   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
12606     cp_parser_enumerator_list (parser, type);
12607
12608   /* Consume the final '}'.  */
12609   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12610
12611   /* Look for trailing attributes to apply to this enumeration, and
12612      apply them if appropriate.  */
12613   if (cp_parser_allow_gnu_extensions_p (parser))
12614     {
12615       tree trailing_attr = cp_parser_attributes_opt (parser);
12616       trailing_attr = chainon (trailing_attr, attributes);
12617       cplus_decl_attributes (&type,
12618                              trailing_attr,
12619                              (int) ATTR_FLAG_TYPE_IN_PLACE);
12620     }
12621
12622   /* Finish up the enumeration.  */
12623   finish_enum (type);
12624
12625   return type;
12626 }
12627
12628 /* Parse an enumerator-list.  The enumerators all have the indicated
12629    TYPE.
12630
12631    enumerator-list:
12632      enumerator-definition
12633      enumerator-list , enumerator-definition  */
12634
12635 static void
12636 cp_parser_enumerator_list (cp_parser* parser, tree type)
12637 {
12638   while (true)
12639     {
12640       /* Parse an enumerator-definition.  */
12641       cp_parser_enumerator_definition (parser, type);
12642
12643       /* If the next token is not a ',', we've reached the end of
12644          the list.  */
12645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12646         break;
12647       /* Otherwise, consume the `,' and keep going.  */
12648       cp_lexer_consume_token (parser->lexer);
12649       /* If the next token is a `}', there is a trailing comma.  */
12650       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
12651         {
12652           if (!in_system_header)
12653             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
12654           break;
12655         }
12656     }
12657 }
12658
12659 /* Parse an enumerator-definition.  The enumerator has the indicated
12660    TYPE.
12661
12662    enumerator-definition:
12663      enumerator
12664      enumerator = constant-expression
12665
12666    enumerator:
12667      identifier  */
12668
12669 static void
12670 cp_parser_enumerator_definition (cp_parser* parser, tree type)
12671 {
12672   tree identifier;
12673   tree value;
12674
12675   /* Look for the identifier.  */
12676   identifier = cp_parser_identifier (parser);
12677   if (identifier == error_mark_node)
12678     return;
12679
12680   /* If the next token is an '=', then there is an explicit value.  */
12681   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12682     {
12683       /* Consume the `=' token.  */
12684       cp_lexer_consume_token (parser->lexer);
12685       /* Parse the value.  */
12686       value = cp_parser_constant_expression (parser,
12687                                              /*allow_non_constant_p=*/false,
12688                                              NULL);
12689     }
12690   else
12691     value = NULL_TREE;
12692
12693   /* If we are processing a template, make sure the initializer of the
12694      enumerator doesn't contain any bare template parameter pack.  */
12695   if (check_for_bare_parameter_packs (value))
12696     value = error_mark_node;
12697
12698   /* Create the enumerator.  */
12699   build_enumerator (identifier, value, type);
12700 }
12701
12702 /* Parse a namespace-name.
12703
12704    namespace-name:
12705      original-namespace-name
12706      namespace-alias
12707
12708    Returns the NAMESPACE_DECL for the namespace.  */
12709
12710 static tree
12711 cp_parser_namespace_name (cp_parser* parser)
12712 {
12713   tree identifier;
12714   tree namespace_decl;
12715
12716   cp_token *token = cp_lexer_peek_token (parser->lexer);
12717
12718   /* Get the name of the namespace.  */
12719   identifier = cp_parser_identifier (parser);
12720   if (identifier == error_mark_node)
12721     return error_mark_node;
12722
12723   /* Look up the identifier in the currently active scope.  Look only
12724      for namespaces, due to:
12725
12726        [basic.lookup.udir]
12727
12728        When looking up a namespace-name in a using-directive or alias
12729        definition, only namespace names are considered.
12730
12731      And:
12732
12733        [basic.lookup.qual]
12734
12735        During the lookup of a name preceding the :: scope resolution
12736        operator, object, function, and enumerator names are ignored.
12737
12738      (Note that cp_parser_qualifying_entity only calls this
12739      function if the token after the name is the scope resolution
12740      operator.)  */
12741   namespace_decl = cp_parser_lookup_name (parser, identifier,
12742                                           none_type,
12743                                           /*is_template=*/false,
12744                                           /*is_namespace=*/true,
12745                                           /*check_dependency=*/true,
12746                                           /*ambiguous_decls=*/NULL,
12747                                           token->location);
12748   /* If it's not a namespace, issue an error.  */
12749   if (namespace_decl == error_mark_node
12750       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
12751     {
12752       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
12753         error_at (token->location, "%qD is not a namespace-name", identifier);
12754       cp_parser_error (parser, "expected namespace-name");
12755       namespace_decl = error_mark_node;
12756     }
12757
12758   return namespace_decl;
12759 }
12760
12761 /* Parse a namespace-definition.
12762
12763    namespace-definition:
12764      named-namespace-definition
12765      unnamed-namespace-definition
12766
12767    named-namespace-definition:
12768      original-namespace-definition
12769      extension-namespace-definition
12770
12771    original-namespace-definition:
12772      namespace identifier { namespace-body }
12773
12774    extension-namespace-definition:
12775      namespace original-namespace-name { namespace-body }
12776
12777    unnamed-namespace-definition:
12778      namespace { namespace-body } */
12779
12780 static void
12781 cp_parser_namespace_definition (cp_parser* parser)
12782 {
12783   tree identifier, attribs;
12784   bool has_visibility;
12785   bool is_inline;
12786
12787   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
12788     {
12789       is_inline = true;
12790       cp_lexer_consume_token (parser->lexer);
12791     }
12792   else
12793     is_inline = false;
12794
12795   /* Look for the `namespace' keyword.  */
12796   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12797
12798   /* Get the name of the namespace.  We do not attempt to distinguish
12799      between an original-namespace-definition and an
12800      extension-namespace-definition at this point.  The semantic
12801      analysis routines are responsible for that.  */
12802   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12803     identifier = cp_parser_identifier (parser);
12804   else
12805     identifier = NULL_TREE;
12806
12807   /* Parse any specified attributes.  */
12808   attribs = cp_parser_attributes_opt (parser);
12809
12810   /* Look for the `{' to start the namespace.  */
12811   cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>");
12812   /* Start the namespace.  */
12813   push_namespace (identifier);
12814
12815   /* "inline namespace" is equivalent to a stub namespace definition
12816      followed by a strong using directive.  */
12817   if (is_inline)
12818     {
12819       tree name_space = current_namespace;
12820       /* Set up namespace association.  */
12821       DECL_NAMESPACE_ASSOCIATIONS (name_space)
12822         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
12823                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
12824       /* Import the contents of the inline namespace.  */
12825       pop_namespace ();
12826       do_using_directive (name_space);
12827       push_namespace (identifier);
12828     }
12829
12830   has_visibility = handle_namespace_attrs (current_namespace, attribs);
12831
12832   /* Parse the body of the namespace.  */
12833   cp_parser_namespace_body (parser);
12834
12835 #ifdef HANDLE_PRAGMA_VISIBILITY
12836   if (has_visibility)
12837     pop_visibility ();
12838 #endif
12839
12840   /* Finish the namespace.  */
12841   pop_namespace ();
12842   /* Look for the final `}'.  */
12843   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
12844 }
12845
12846 /* Parse a namespace-body.
12847
12848    namespace-body:
12849      declaration-seq [opt]  */
12850
12851 static void
12852 cp_parser_namespace_body (cp_parser* parser)
12853 {
12854   cp_parser_declaration_seq_opt (parser);
12855 }
12856
12857 /* Parse a namespace-alias-definition.
12858
12859    namespace-alias-definition:
12860      namespace identifier = qualified-namespace-specifier ;  */
12861
12862 static void
12863 cp_parser_namespace_alias_definition (cp_parser* parser)
12864 {
12865   tree identifier;
12866   tree namespace_specifier;
12867
12868   cp_token *token = cp_lexer_peek_token (parser->lexer);
12869
12870   /* Look for the `namespace' keyword.  */
12871   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
12872   /* Look for the identifier.  */
12873   identifier = cp_parser_identifier (parser);
12874   if (identifier == error_mark_node)
12875     return;
12876   /* Look for the `=' token.  */
12877   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
12878       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
12879     {
12880       error_at (token->location, "%<namespace%> definition is not allowed here");
12881       /* Skip the definition.  */
12882       cp_lexer_consume_token (parser->lexer);
12883       if (cp_parser_skip_to_closing_brace (parser))
12884         cp_lexer_consume_token (parser->lexer);
12885       return;
12886     }
12887   cp_parser_require (parser, CPP_EQ, "%<=%>");
12888   /* Look for the qualified-namespace-specifier.  */
12889   namespace_specifier
12890     = cp_parser_qualified_namespace_specifier (parser);
12891   /* Look for the `;' token.  */
12892   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
12893
12894   /* Register the alias in the symbol table.  */
12895   do_namespace_alias (identifier, namespace_specifier);
12896 }
12897
12898 /* Parse a qualified-namespace-specifier.
12899
12900    qualified-namespace-specifier:
12901      :: [opt] nested-name-specifier [opt] namespace-name
12902
12903    Returns a NAMESPACE_DECL corresponding to the specified
12904    namespace.  */
12905
12906 static tree
12907 cp_parser_qualified_namespace_specifier (cp_parser* parser)
12908 {
12909   /* Look for the optional `::'.  */
12910   cp_parser_global_scope_opt (parser,
12911                               /*current_scope_valid_p=*/false);
12912
12913   /* Look for the optional nested-name-specifier.  */
12914   cp_parser_nested_name_specifier_opt (parser,
12915                                        /*typename_keyword_p=*/false,
12916                                        /*check_dependency_p=*/true,
12917                                        /*type_p=*/false,
12918                                        /*is_declaration=*/true);
12919
12920   return cp_parser_namespace_name (parser);
12921 }
12922
12923 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
12924    access declaration.
12925
12926    using-declaration:
12927      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
12928      using :: unqualified-id ;  
12929
12930    access-declaration:
12931      qualified-id ;  
12932
12933    */
12934
12935 static bool
12936 cp_parser_using_declaration (cp_parser* parser, 
12937                              bool access_declaration_p)
12938 {
12939   cp_token *token;
12940   bool typename_p = false;
12941   bool global_scope_p;
12942   tree decl;
12943   tree identifier;
12944   tree qscope;
12945
12946   if (access_declaration_p)
12947     cp_parser_parse_tentatively (parser);
12948   else
12949     {
12950       /* Look for the `using' keyword.  */
12951       cp_parser_require_keyword (parser, RID_USING, "%<using%>");
12952       
12953       /* Peek at the next token.  */
12954       token = cp_lexer_peek_token (parser->lexer);
12955       /* See if it's `typename'.  */
12956       if (token->keyword == RID_TYPENAME)
12957         {
12958           /* Remember that we've seen it.  */
12959           typename_p = true;
12960           /* Consume the `typename' token.  */
12961           cp_lexer_consume_token (parser->lexer);
12962         }
12963     }
12964
12965   /* Look for the optional global scope qualification.  */
12966   global_scope_p
12967     = (cp_parser_global_scope_opt (parser,
12968                                    /*current_scope_valid_p=*/false)
12969        != NULL_TREE);
12970
12971   /* If we saw `typename', or didn't see `::', then there must be a
12972      nested-name-specifier present.  */
12973   if (typename_p || !global_scope_p)
12974     qscope = cp_parser_nested_name_specifier (parser, typename_p,
12975                                               /*check_dependency_p=*/true,
12976                                               /*type_p=*/false,
12977                                               /*is_declaration=*/true);
12978   /* Otherwise, we could be in either of the two productions.  In that
12979      case, treat the nested-name-specifier as optional.  */
12980   else
12981     qscope = cp_parser_nested_name_specifier_opt (parser,
12982                                                   /*typename_keyword_p=*/false,
12983                                                   /*check_dependency_p=*/true,
12984                                                   /*type_p=*/false,
12985                                                   /*is_declaration=*/true);
12986   if (!qscope)
12987     qscope = global_namespace;
12988
12989   if (access_declaration_p && cp_parser_error_occurred (parser))
12990     /* Something has already gone wrong; there's no need to parse
12991        further.  Since an error has occurred, the return value of
12992        cp_parser_parse_definitely will be false, as required.  */
12993     return cp_parser_parse_definitely (parser);
12994
12995   token = cp_lexer_peek_token (parser->lexer);
12996   /* Parse the unqualified-id.  */
12997   identifier = cp_parser_unqualified_id (parser,
12998                                          /*template_keyword_p=*/false,
12999                                          /*check_dependency_p=*/true,
13000                                          /*declarator_p=*/true,
13001                                          /*optional_p=*/false);
13002
13003   if (access_declaration_p)
13004     {
13005       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13006         cp_parser_simulate_error (parser);
13007       if (!cp_parser_parse_definitely (parser))
13008         return false;
13009     }
13010
13011   /* The function we call to handle a using-declaration is different
13012      depending on what scope we are in.  */
13013   if (qscope == error_mark_node || identifier == error_mark_node)
13014     ;
13015   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13016            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13017     /* [namespace.udecl]
13018
13019        A using declaration shall not name a template-id.  */
13020     error_at (token->location,
13021               "a template-id may not appear in a using-declaration");
13022   else
13023     {
13024       if (at_class_scope_p ())
13025         {
13026           /* Create the USING_DECL.  */
13027           decl = do_class_using_decl (parser->scope, identifier);
13028
13029           if (check_for_bare_parameter_packs (decl))
13030             return false;
13031           else
13032             /* Add it to the list of members in this class.  */
13033             finish_member_declaration (decl);
13034         }
13035       else
13036         {
13037           decl = cp_parser_lookup_name_simple (parser,
13038                                                identifier,
13039                                                token->location);
13040           if (decl == error_mark_node)
13041             cp_parser_name_lookup_error (parser, identifier,
13042                                          decl, NULL,
13043                                          token->location);
13044           else if (check_for_bare_parameter_packs (decl))
13045             return false;
13046           else if (!at_namespace_scope_p ())
13047             do_local_using_decl (decl, qscope, identifier);
13048           else
13049             do_toplevel_using_decl (decl, qscope, identifier);
13050         }
13051     }
13052
13053   /* Look for the final `;'.  */
13054   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13055   
13056   return true;
13057 }
13058
13059 /* Parse a using-directive.
13060
13061    using-directive:
13062      using namespace :: [opt] nested-name-specifier [opt]
13063        namespace-name ;  */
13064
13065 static void
13066 cp_parser_using_directive (cp_parser* parser)
13067 {
13068   tree namespace_decl;
13069   tree attribs;
13070
13071   /* Look for the `using' keyword.  */
13072   cp_parser_require_keyword (parser, RID_USING, "%<using%>");
13073   /* And the `namespace' keyword.  */
13074   cp_parser_require_keyword (parser, RID_NAMESPACE, "%<namespace%>");
13075   /* Look for the optional `::' operator.  */
13076   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
13077   /* And the optional nested-name-specifier.  */
13078   cp_parser_nested_name_specifier_opt (parser,
13079                                        /*typename_keyword_p=*/false,
13080                                        /*check_dependency_p=*/true,
13081                                        /*type_p=*/false,
13082                                        /*is_declaration=*/true);
13083   /* Get the namespace being used.  */
13084   namespace_decl = cp_parser_namespace_name (parser);
13085   /* And any specified attributes.  */
13086   attribs = cp_parser_attributes_opt (parser);
13087   /* Update the symbol table.  */
13088   parse_using_directive (namespace_decl, attribs);
13089   /* Look for the final `;'.  */
13090   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13091 }
13092
13093 /* Parse an asm-definition.
13094
13095    asm-definition:
13096      asm ( string-literal ) ;
13097
13098    GNU Extension:
13099
13100    asm-definition:
13101      asm volatile [opt] ( string-literal ) ;
13102      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
13103      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13104                           : asm-operand-list [opt] ) ;
13105      asm volatile [opt] ( string-literal : asm-operand-list [opt]
13106                           : asm-operand-list [opt]
13107                           : asm-clobber-list [opt] ) ;
13108      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
13109                                : asm-clobber-list [opt]
13110                                : asm-goto-list ) ;  */
13111
13112 static void
13113 cp_parser_asm_definition (cp_parser* parser)
13114 {
13115   tree string;
13116   tree outputs = NULL_TREE;
13117   tree inputs = NULL_TREE;
13118   tree clobbers = NULL_TREE;
13119   tree labels = NULL_TREE;
13120   tree asm_stmt;
13121   bool volatile_p = false;
13122   bool extended_p = false;
13123   bool invalid_inputs_p = false;
13124   bool invalid_outputs_p = false;
13125   bool goto_p = false;
13126   const char *missing = NULL;
13127
13128   /* Look for the `asm' keyword.  */
13129   cp_parser_require_keyword (parser, RID_ASM, "%<asm%>");
13130   /* See if the next token is `volatile'.  */
13131   if (cp_parser_allow_gnu_extensions_p (parser)
13132       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
13133     {
13134       /* Remember that we saw the `volatile' keyword.  */
13135       volatile_p = true;
13136       /* Consume the token.  */
13137       cp_lexer_consume_token (parser->lexer);
13138     }
13139   if (cp_parser_allow_gnu_extensions_p (parser)
13140       && parser->in_function_body
13141       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
13142     {
13143       /* Remember that we saw the `goto' keyword.  */
13144       goto_p = true;
13145       /* Consume the token.  */
13146       cp_lexer_consume_token (parser->lexer);
13147     }
13148   /* Look for the opening `('.  */
13149   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
13150     return;
13151   /* Look for the string.  */
13152   string = cp_parser_string_literal (parser, false, false);
13153   if (string == error_mark_node)
13154     {
13155       cp_parser_skip_to_closing_parenthesis (parser, true, false,
13156                                              /*consume_paren=*/true);
13157       return;
13158     }
13159
13160   /* If we're allowing GNU extensions, check for the extended assembly
13161      syntax.  Unfortunately, the `:' tokens need not be separated by
13162      a space in C, and so, for compatibility, we tolerate that here
13163      too.  Doing that means that we have to treat the `::' operator as
13164      two `:' tokens.  */
13165   if (cp_parser_allow_gnu_extensions_p (parser)
13166       && parser->in_function_body
13167       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
13168           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
13169     {
13170       bool inputs_p = false;
13171       bool clobbers_p = false;
13172       bool labels_p = false;
13173
13174       /* The extended syntax was used.  */
13175       extended_p = true;
13176
13177       /* Look for outputs.  */
13178       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13179         {
13180           /* Consume the `:'.  */
13181           cp_lexer_consume_token (parser->lexer);
13182           /* Parse the output-operands.  */
13183           if (cp_lexer_next_token_is_not (parser->lexer,
13184                                           CPP_COLON)
13185               && cp_lexer_next_token_is_not (parser->lexer,
13186                                              CPP_SCOPE)
13187               && cp_lexer_next_token_is_not (parser->lexer,
13188                                              CPP_CLOSE_PAREN)
13189               && !goto_p)
13190             outputs = cp_parser_asm_operand_list (parser);
13191
13192             if (outputs == error_mark_node)
13193               invalid_outputs_p = true;
13194         }
13195       /* If the next token is `::', there are no outputs, and the
13196          next token is the beginning of the inputs.  */
13197       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13198         /* The inputs are coming next.  */
13199         inputs_p = true;
13200
13201       /* Look for inputs.  */
13202       if (inputs_p
13203           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13204         {
13205           /* Consume the `:' or `::'.  */
13206           cp_lexer_consume_token (parser->lexer);
13207           /* Parse the output-operands.  */
13208           if (cp_lexer_next_token_is_not (parser->lexer,
13209                                           CPP_COLON)
13210               && cp_lexer_next_token_is_not (parser->lexer,
13211                                              CPP_SCOPE)
13212               && cp_lexer_next_token_is_not (parser->lexer,
13213                                              CPP_CLOSE_PAREN))
13214             inputs = cp_parser_asm_operand_list (parser);
13215
13216             if (inputs == error_mark_node)
13217               invalid_inputs_p = true;
13218         }
13219       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13220         /* The clobbers are coming next.  */
13221         clobbers_p = true;
13222
13223       /* Look for clobbers.  */
13224       if (clobbers_p
13225           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13226         {
13227           clobbers_p = true;
13228           /* Consume the `:' or `::'.  */
13229           cp_lexer_consume_token (parser->lexer);
13230           /* Parse the clobbers.  */
13231           if (cp_lexer_next_token_is_not (parser->lexer,
13232                                           CPP_COLON)
13233               && cp_lexer_next_token_is_not (parser->lexer,
13234                                              CPP_CLOSE_PAREN))
13235             clobbers = cp_parser_asm_clobber_list (parser);
13236         }
13237       else if (goto_p
13238                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
13239         /* The labels are coming next.  */
13240         labels_p = true;
13241
13242       /* Look for labels.  */
13243       if (labels_p
13244           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
13245         {
13246           labels_p = true;
13247           /* Consume the `:' or `::'.  */
13248           cp_lexer_consume_token (parser->lexer);
13249           /* Parse the labels.  */
13250           labels = cp_parser_asm_label_list (parser);
13251         }
13252
13253       if (goto_p && !labels_p)
13254         missing = clobbers_p ? "%<:%>" : "%<:%> or %<::%>";
13255     }
13256   else if (goto_p)
13257     missing = "%<:%> or %<::%>";
13258
13259   /* Look for the closing `)'.  */
13260   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
13261                           missing ? missing : "%<)%>"))
13262     cp_parser_skip_to_closing_parenthesis (parser, true, false,
13263                                            /*consume_paren=*/true);
13264   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
13265
13266   if (!invalid_inputs_p && !invalid_outputs_p)
13267     {
13268       /* Create the ASM_EXPR.  */
13269       if (parser->in_function_body)
13270         {
13271           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
13272                                       inputs, clobbers, labels);
13273           /* If the extended syntax was not used, mark the ASM_EXPR.  */
13274           if (!extended_p)
13275             {
13276               tree temp = asm_stmt;
13277               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
13278                 temp = TREE_OPERAND (temp, 0);
13279
13280               ASM_INPUT_P (temp) = 1;
13281             }
13282         }
13283       else
13284         cgraph_add_asm_node (string);
13285     }
13286 }
13287
13288 /* Declarators [gram.dcl.decl] */
13289
13290 /* Parse an init-declarator.
13291
13292    init-declarator:
13293      declarator initializer [opt]
13294
13295    GNU Extension:
13296
13297    init-declarator:
13298      declarator asm-specification [opt] attributes [opt] initializer [opt]
13299
13300    function-definition:
13301      decl-specifier-seq [opt] declarator ctor-initializer [opt]
13302        function-body
13303      decl-specifier-seq [opt] declarator function-try-block
13304
13305    GNU Extension:
13306
13307    function-definition:
13308      __extension__ function-definition
13309
13310    The DECL_SPECIFIERS apply to this declarator.  Returns a
13311    representation of the entity declared.  If MEMBER_P is TRUE, then
13312    this declarator appears in a class scope.  The new DECL created by
13313    this declarator is returned.
13314
13315    The CHECKS are access checks that should be performed once we know
13316    what entity is being declared (and, therefore, what classes have
13317    befriended it).
13318
13319    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
13320    for a function-definition here as well.  If the declarator is a
13321    declarator for a function-definition, *FUNCTION_DEFINITION_P will
13322    be TRUE upon return.  By that point, the function-definition will
13323    have been completely parsed.
13324
13325    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
13326    is FALSE.  */
13327
13328 static tree
13329 cp_parser_init_declarator (cp_parser* parser,
13330                            cp_decl_specifier_seq *decl_specifiers,
13331                            VEC (deferred_access_check,gc)* checks,
13332                            bool function_definition_allowed_p,
13333                            bool member_p,
13334                            int declares_class_or_enum,
13335                            bool* function_definition_p)
13336 {
13337   cp_token *token = NULL, *asm_spec_start_token = NULL,
13338            *attributes_start_token = NULL;
13339   cp_declarator *declarator;
13340   tree prefix_attributes;
13341   tree attributes;
13342   tree asm_specification;
13343   tree initializer;
13344   tree decl = NULL_TREE;
13345   tree scope;
13346   int is_initialized;
13347   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
13348      initialized with "= ..", CPP_OPEN_PAREN if initialized with
13349      "(...)".  */
13350   enum cpp_ttype initialization_kind;
13351   bool is_direct_init = false;
13352   bool is_non_constant_init;
13353   int ctor_dtor_or_conv_p;
13354   bool friend_p;
13355   tree pushed_scope = NULL;
13356
13357   /* Gather the attributes that were provided with the
13358      decl-specifiers.  */
13359   prefix_attributes = decl_specifiers->attributes;
13360
13361   /* Assume that this is not the declarator for a function
13362      definition.  */
13363   if (function_definition_p)
13364     *function_definition_p = false;
13365
13366   /* Defer access checks while parsing the declarator; we cannot know
13367      what names are accessible until we know what is being
13368      declared.  */
13369   resume_deferring_access_checks ();
13370
13371   /* Parse the declarator.  */
13372   token = cp_lexer_peek_token (parser->lexer);
13373   declarator
13374     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13375                             &ctor_dtor_or_conv_p,
13376                             /*parenthesized_p=*/NULL,
13377                             /*member_p=*/false);
13378   /* Gather up the deferred checks.  */
13379   stop_deferring_access_checks ();
13380
13381   /* If the DECLARATOR was erroneous, there's no need to go
13382      further.  */
13383   if (declarator == cp_error_declarator)
13384     return error_mark_node;
13385
13386   /* Check that the number of template-parameter-lists is OK.  */
13387   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
13388                                                        token->location))
13389     return error_mark_node;
13390
13391   if (declares_class_or_enum & 2)
13392     cp_parser_check_for_definition_in_return_type (declarator,
13393                                                    decl_specifiers->type,
13394                                                    decl_specifiers->type_location);
13395
13396   /* Figure out what scope the entity declared by the DECLARATOR is
13397      located in.  `grokdeclarator' sometimes changes the scope, so
13398      we compute it now.  */
13399   scope = get_scope_of_declarator (declarator);
13400
13401   /* If we're allowing GNU extensions, look for an asm-specification
13402      and attributes.  */
13403   if (cp_parser_allow_gnu_extensions_p (parser))
13404     {
13405       /* Look for an asm-specification.  */
13406       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
13407       asm_specification = cp_parser_asm_specification_opt (parser);
13408       /* And attributes.  */
13409       attributes_start_token = cp_lexer_peek_token (parser->lexer);
13410       attributes = cp_parser_attributes_opt (parser);
13411     }
13412   else
13413     {
13414       asm_specification = NULL_TREE;
13415       attributes = NULL_TREE;
13416     }
13417
13418   /* Peek at the next token.  */
13419   token = cp_lexer_peek_token (parser->lexer);
13420   /* Check to see if the token indicates the start of a
13421      function-definition.  */
13422   if (function_declarator_p (declarator)
13423       && cp_parser_token_starts_function_definition_p (token))
13424     {
13425       if (!function_definition_allowed_p)
13426         {
13427           /* If a function-definition should not appear here, issue an
13428              error message.  */
13429           cp_parser_error (parser,
13430                            "a function-definition is not allowed here");
13431           return error_mark_node;
13432         }
13433       else
13434         {
13435           location_t func_brace_location
13436             = cp_lexer_peek_token (parser->lexer)->location;
13437
13438           /* Neither attributes nor an asm-specification are allowed
13439              on a function-definition.  */
13440           if (asm_specification)
13441             error_at (asm_spec_start_token->location,
13442                       "an asm-specification is not allowed "
13443                       "on a function-definition");
13444           if (attributes)
13445             error_at (attributes_start_token->location,
13446                       "attributes are not allowed on a function-definition");
13447           /* This is a function-definition.  */
13448           *function_definition_p = true;
13449
13450           /* Parse the function definition.  */
13451           if (member_p)
13452             decl = cp_parser_save_member_function_body (parser,
13453                                                         decl_specifiers,
13454                                                         declarator,
13455                                                         prefix_attributes);
13456           else
13457             decl
13458               = (cp_parser_function_definition_from_specifiers_and_declarator
13459                  (parser, decl_specifiers, prefix_attributes, declarator));
13460
13461           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
13462             {
13463               /* This is where the prologue starts...  */
13464               DECL_STRUCT_FUNCTION (decl)->function_start_locus
13465                 = func_brace_location;
13466             }
13467
13468           return decl;
13469         }
13470     }
13471
13472   /* [dcl.dcl]
13473
13474      Only in function declarations for constructors, destructors, and
13475      type conversions can the decl-specifier-seq be omitted.
13476
13477      We explicitly postpone this check past the point where we handle
13478      function-definitions because we tolerate function-definitions
13479      that are missing their return types in some modes.  */
13480   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
13481     {
13482       cp_parser_error (parser,
13483                        "expected constructor, destructor, or type conversion");
13484       return error_mark_node;
13485     }
13486
13487   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
13488   if (token->type == CPP_EQ
13489       || token->type == CPP_OPEN_PAREN
13490       || token->type == CPP_OPEN_BRACE)
13491     {
13492       is_initialized = SD_INITIALIZED;
13493       initialization_kind = token->type;
13494
13495       if (token->type == CPP_EQ
13496           && function_declarator_p (declarator))
13497         {
13498           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
13499           if (t2->keyword == RID_DEFAULT)
13500             is_initialized = SD_DEFAULTED;
13501           else if (t2->keyword == RID_DELETE)
13502             is_initialized = SD_DELETED;
13503         }
13504     }
13505   else
13506     {
13507       /* If the init-declarator isn't initialized and isn't followed by a
13508          `,' or `;', it's not a valid init-declarator.  */
13509       if (token->type != CPP_COMMA
13510           && token->type != CPP_SEMICOLON)
13511         {
13512           cp_parser_error (parser, "expected initializer");
13513           return error_mark_node;
13514         }
13515       is_initialized = SD_UNINITIALIZED;
13516       initialization_kind = CPP_EOF;
13517     }
13518
13519   /* Because start_decl has side-effects, we should only call it if we
13520      know we're going ahead.  By this point, we know that we cannot
13521      possibly be looking at any other construct.  */
13522   cp_parser_commit_to_tentative_parse (parser);
13523
13524   /* If the decl specifiers were bad, issue an error now that we're
13525      sure this was intended to be a declarator.  Then continue
13526      declaring the variable(s), as int, to try to cut down on further
13527      errors.  */
13528   if (decl_specifiers->any_specifiers_p
13529       && decl_specifiers->type == error_mark_node)
13530     {
13531       cp_parser_error (parser, "invalid type in declaration");
13532       decl_specifiers->type = integer_type_node;
13533     }
13534
13535   /* Check to see whether or not this declaration is a friend.  */
13536   friend_p = cp_parser_friend_p (decl_specifiers);
13537
13538   /* Enter the newly declared entry in the symbol table.  If we're
13539      processing a declaration in a class-specifier, we wait until
13540      after processing the initializer.  */
13541   if (!member_p)
13542     {
13543       if (parser->in_unbraced_linkage_specification_p)
13544         decl_specifiers->storage_class = sc_extern;
13545       decl = start_decl (declarator, decl_specifiers,
13546                          is_initialized, attributes, prefix_attributes,
13547                          &pushed_scope);
13548     }
13549   else if (scope)
13550     /* Enter the SCOPE.  That way unqualified names appearing in the
13551        initializer will be looked up in SCOPE.  */
13552     pushed_scope = push_scope (scope);
13553
13554   /* Perform deferred access control checks, now that we know in which
13555      SCOPE the declared entity resides.  */
13556   if (!member_p && decl)
13557     {
13558       tree saved_current_function_decl = NULL_TREE;
13559
13560       /* If the entity being declared is a function, pretend that we
13561          are in its scope.  If it is a `friend', it may have access to
13562          things that would not otherwise be accessible.  */
13563       if (TREE_CODE (decl) == FUNCTION_DECL)
13564         {
13565           saved_current_function_decl = current_function_decl;
13566           current_function_decl = decl;
13567         }
13568
13569       /* Perform access checks for template parameters.  */
13570       cp_parser_perform_template_parameter_access_checks (checks);
13571
13572       /* Perform the access control checks for the declarator and the
13573          decl-specifiers.  */
13574       perform_deferred_access_checks ();
13575
13576       /* Restore the saved value.  */
13577       if (TREE_CODE (decl) == FUNCTION_DECL)
13578         current_function_decl = saved_current_function_decl;
13579     }
13580
13581   /* Parse the initializer.  */
13582   initializer = NULL_TREE;
13583   is_direct_init = false;
13584   is_non_constant_init = true;
13585   if (is_initialized)
13586     {
13587       if (function_declarator_p (declarator))
13588         {
13589           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
13590            if (initialization_kind == CPP_EQ)
13591              initializer = cp_parser_pure_specifier (parser);
13592            else
13593              {
13594                /* If the declaration was erroneous, we don't really
13595                   know what the user intended, so just silently
13596                   consume the initializer.  */
13597                if (decl != error_mark_node)
13598                  error_at (initializer_start_token->location,
13599                            "initializer provided for function");
13600                cp_parser_skip_to_closing_parenthesis (parser,
13601                                                       /*recovering=*/true,
13602                                                       /*or_comma=*/false,
13603                                                       /*consume_paren=*/true);
13604              }
13605         }
13606       else
13607         {
13608           /* We want to record the extra mangling scope for in-class
13609              initializers of class members and initializers of static data
13610              member templates.  The former is a C++0x feature which isn't
13611              implemented yet, and I expect it will involve deferring
13612              parsing of the initializer until end of class as with default
13613              arguments.  So right here we only handle the latter.  */
13614           if (!member_p && processing_template_decl)
13615             start_lambda_scope (decl);
13616           initializer = cp_parser_initializer (parser,
13617                                                &is_direct_init,
13618                                                &is_non_constant_init);
13619           if (!member_p && processing_template_decl)
13620             finish_lambda_scope ();
13621         }
13622     }
13623
13624   /* The old parser allows attributes to appear after a parenthesized
13625      initializer.  Mark Mitchell proposed removing this functionality
13626      on the GCC mailing lists on 2002-08-13.  This parser accepts the
13627      attributes -- but ignores them.  */
13628   if (cp_parser_allow_gnu_extensions_p (parser)
13629       && initialization_kind == CPP_OPEN_PAREN)
13630     if (cp_parser_attributes_opt (parser))
13631       warning (OPT_Wattributes,
13632                "attributes after parenthesized initializer ignored");
13633
13634   /* For an in-class declaration, use `grokfield' to create the
13635      declaration.  */
13636   if (member_p)
13637     {
13638       if (pushed_scope)
13639         {
13640           pop_scope (pushed_scope);
13641           pushed_scope = false;
13642         }
13643       decl = grokfield (declarator, decl_specifiers,
13644                         initializer, !is_non_constant_init,
13645                         /*asmspec=*/NULL_TREE,
13646                         prefix_attributes);
13647       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
13648         cp_parser_save_default_args (parser, decl);
13649     }
13650
13651   /* Finish processing the declaration.  But, skip friend
13652      declarations.  */
13653   if (!friend_p && decl && decl != error_mark_node)
13654     {
13655       cp_finish_decl (decl,
13656                       initializer, !is_non_constant_init,
13657                       asm_specification,
13658                       /* If the initializer is in parentheses, then this is
13659                          a direct-initialization, which means that an
13660                          `explicit' constructor is OK.  Otherwise, an
13661                          `explicit' constructor cannot be used.  */
13662                       ((is_direct_init || !is_initialized)
13663                        ? 0 : LOOKUP_ONLYCONVERTING));
13664     }
13665   else if ((cxx_dialect != cxx98) && friend_p
13666            && decl && TREE_CODE (decl) == FUNCTION_DECL)
13667     /* Core issue #226 (C++0x only): A default template-argument
13668        shall not be specified in a friend class template
13669        declaration. */
13670     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
13671                              /*is_partial=*/0, /*is_friend_decl=*/1);
13672
13673   if (!friend_p && pushed_scope)
13674     pop_scope (pushed_scope);
13675
13676   return decl;
13677 }
13678
13679 /* Parse a declarator.
13680
13681    declarator:
13682      direct-declarator
13683      ptr-operator declarator
13684
13685    abstract-declarator:
13686      ptr-operator abstract-declarator [opt]
13687      direct-abstract-declarator
13688
13689    GNU Extensions:
13690
13691    declarator:
13692      attributes [opt] direct-declarator
13693      attributes [opt] ptr-operator declarator
13694
13695    abstract-declarator:
13696      attributes [opt] ptr-operator abstract-declarator [opt]
13697      attributes [opt] direct-abstract-declarator
13698
13699    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
13700    detect constructor, destructor or conversion operators. It is set
13701    to -1 if the declarator is a name, and +1 if it is a
13702    function. Otherwise it is set to zero. Usually you just want to
13703    test for >0, but internally the negative value is used.
13704
13705    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
13706    a decl-specifier-seq unless it declares a constructor, destructor,
13707    or conversion.  It might seem that we could check this condition in
13708    semantic analysis, rather than parsing, but that makes it difficult
13709    to handle something like `f()'.  We want to notice that there are
13710    no decl-specifiers, and therefore realize that this is an
13711    expression, not a declaration.)
13712
13713    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
13714    the declarator is a direct-declarator of the form "(...)".
13715
13716    MEMBER_P is true iff this declarator is a member-declarator.  */
13717
13718 static cp_declarator *
13719 cp_parser_declarator (cp_parser* parser,
13720                       cp_parser_declarator_kind dcl_kind,
13721                       int* ctor_dtor_or_conv_p,
13722                       bool* parenthesized_p,
13723                       bool member_p)
13724 {
13725   cp_token *token;
13726   cp_declarator *declarator;
13727   enum tree_code code;
13728   cp_cv_quals cv_quals;
13729   tree class_type;
13730   tree attributes = NULL_TREE;
13731
13732   /* Assume this is not a constructor, destructor, or type-conversion
13733      operator.  */
13734   if (ctor_dtor_or_conv_p)
13735     *ctor_dtor_or_conv_p = 0;
13736
13737   if (cp_parser_allow_gnu_extensions_p (parser))
13738     attributes = cp_parser_attributes_opt (parser);
13739
13740   /* Peek at the next token.  */
13741   token = cp_lexer_peek_token (parser->lexer);
13742
13743   /* Check for the ptr-operator production.  */
13744   cp_parser_parse_tentatively (parser);
13745   /* Parse the ptr-operator.  */
13746   code = cp_parser_ptr_operator (parser,
13747                                  &class_type,
13748                                  &cv_quals);
13749   /* If that worked, then we have a ptr-operator.  */
13750   if (cp_parser_parse_definitely (parser))
13751     {
13752       /* If a ptr-operator was found, then this declarator was not
13753          parenthesized.  */
13754       if (parenthesized_p)
13755         *parenthesized_p = true;
13756       /* The dependent declarator is optional if we are parsing an
13757          abstract-declarator.  */
13758       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13759         cp_parser_parse_tentatively (parser);
13760
13761       /* Parse the dependent declarator.  */
13762       declarator = cp_parser_declarator (parser, dcl_kind,
13763                                          /*ctor_dtor_or_conv_p=*/NULL,
13764                                          /*parenthesized_p=*/NULL,
13765                                          /*member_p=*/false);
13766
13767       /* If we are parsing an abstract-declarator, we must handle the
13768          case where the dependent declarator is absent.  */
13769       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
13770           && !cp_parser_parse_definitely (parser))
13771         declarator = NULL;
13772
13773       declarator = cp_parser_make_indirect_declarator
13774         (code, class_type, cv_quals, declarator);
13775     }
13776   /* Everything else is a direct-declarator.  */
13777   else
13778     {
13779       if (parenthesized_p)
13780         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
13781                                                    CPP_OPEN_PAREN);
13782       declarator = cp_parser_direct_declarator (parser, dcl_kind,
13783                                                 ctor_dtor_or_conv_p,
13784                                                 member_p);
13785     }
13786
13787   if (attributes && declarator && declarator != cp_error_declarator)
13788     declarator->attributes = attributes;
13789
13790   return declarator;
13791 }
13792
13793 /* Parse a direct-declarator or direct-abstract-declarator.
13794
13795    direct-declarator:
13796      declarator-id
13797      direct-declarator ( parameter-declaration-clause )
13798        cv-qualifier-seq [opt]
13799        exception-specification [opt]
13800      direct-declarator [ constant-expression [opt] ]
13801      ( declarator )
13802
13803    direct-abstract-declarator:
13804      direct-abstract-declarator [opt]
13805        ( parameter-declaration-clause )
13806        cv-qualifier-seq [opt]
13807        exception-specification [opt]
13808      direct-abstract-declarator [opt] [ constant-expression [opt] ]
13809      ( abstract-declarator )
13810
13811    Returns a representation of the declarator.  DCL_KIND is
13812    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
13813    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
13814    we are parsing a direct-declarator.  It is
13815    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
13816    of ambiguity we prefer an abstract declarator, as per
13817    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
13818    cp_parser_declarator.  */
13819
13820 static cp_declarator *
13821 cp_parser_direct_declarator (cp_parser* parser,
13822                              cp_parser_declarator_kind dcl_kind,
13823                              int* ctor_dtor_or_conv_p,
13824                              bool member_p)
13825 {
13826   cp_token *token;
13827   cp_declarator *declarator = NULL;
13828   tree scope = NULL_TREE;
13829   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
13830   bool saved_in_declarator_p = parser->in_declarator_p;
13831   bool first = true;
13832   tree pushed_scope = NULL_TREE;
13833
13834   while (true)
13835     {
13836       /* Peek at the next token.  */
13837       token = cp_lexer_peek_token (parser->lexer);
13838       if (token->type == CPP_OPEN_PAREN)
13839         {
13840           /* This is either a parameter-declaration-clause, or a
13841              parenthesized declarator. When we know we are parsing a
13842              named declarator, it must be a parenthesized declarator
13843              if FIRST is true. For instance, `(int)' is a
13844              parameter-declaration-clause, with an omitted
13845              direct-abstract-declarator. But `((*))', is a
13846              parenthesized abstract declarator. Finally, when T is a
13847              template parameter `(T)' is a
13848              parameter-declaration-clause, and not a parenthesized
13849              named declarator.
13850
13851              We first try and parse a parameter-declaration-clause,
13852              and then try a nested declarator (if FIRST is true).
13853
13854              It is not an error for it not to be a
13855              parameter-declaration-clause, even when FIRST is
13856              false. Consider,
13857
13858                int i (int);
13859                int i (3);
13860
13861              The first is the declaration of a function while the
13862              second is the definition of a variable, including its
13863              initializer.
13864
13865              Having seen only the parenthesis, we cannot know which of
13866              these two alternatives should be selected.  Even more
13867              complex are examples like:
13868
13869                int i (int (a));
13870                int i (int (3));
13871
13872              The former is a function-declaration; the latter is a
13873              variable initialization.
13874
13875              Thus again, we try a parameter-declaration-clause, and if
13876              that fails, we back out and return.  */
13877
13878           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13879             {
13880               tree params;
13881               unsigned saved_num_template_parameter_lists;
13882               bool is_declarator = false;
13883               tree t;
13884
13885               /* In a member-declarator, the only valid interpretation
13886                  of a parenthesis is the start of a
13887                  parameter-declaration-clause.  (It is invalid to
13888                  initialize a static data member with a parenthesized
13889                  initializer; only the "=" form of initialization is
13890                  permitted.)  */
13891               if (!member_p)
13892                 cp_parser_parse_tentatively (parser);
13893
13894               /* Consume the `('.  */
13895               cp_lexer_consume_token (parser->lexer);
13896               if (first)
13897                 {
13898                   /* If this is going to be an abstract declarator, we're
13899                      in a declarator and we can't have default args.  */
13900                   parser->default_arg_ok_p = false;
13901                   parser->in_declarator_p = true;
13902                 }
13903
13904               /* Inside the function parameter list, surrounding
13905                  template-parameter-lists do not apply.  */
13906               saved_num_template_parameter_lists
13907                 = parser->num_template_parameter_lists;
13908               parser->num_template_parameter_lists = 0;
13909
13910               begin_scope (sk_function_parms, NULL_TREE);
13911
13912               /* Parse the parameter-declaration-clause.  */
13913               params = cp_parser_parameter_declaration_clause (parser);
13914
13915               parser->num_template_parameter_lists
13916                 = saved_num_template_parameter_lists;
13917
13918               /* If all went well, parse the cv-qualifier-seq and the
13919                  exception-specification.  */
13920               if (member_p || cp_parser_parse_definitely (parser))
13921                 {
13922                   cp_cv_quals cv_quals;
13923                   tree exception_specification;
13924                   tree late_return;
13925
13926                   is_declarator = true;
13927
13928                   if (ctor_dtor_or_conv_p)
13929                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
13930                   first = false;
13931                   /* Consume the `)'.  */
13932                   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
13933
13934                   /* Parse the cv-qualifier-seq.  */
13935                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
13936                   /* And the exception-specification.  */
13937                   exception_specification
13938                     = cp_parser_exception_specification_opt (parser);
13939
13940                   late_return
13941                     = cp_parser_late_return_type_opt (parser);
13942
13943                   /* Create the function-declarator.  */
13944                   declarator = make_call_declarator (declarator,
13945                                                      params,
13946                                                      cv_quals,
13947                                                      exception_specification,
13948                                                      late_return);
13949                   /* Any subsequent parameter lists are to do with
13950                      return type, so are not those of the declared
13951                      function.  */
13952                   parser->default_arg_ok_p = false;
13953                 }
13954
13955               /* Remove the function parms from scope.  */
13956               for (t = current_binding_level->names; t; t = TREE_CHAIN (t))
13957                 pop_binding (DECL_NAME (t), t);
13958               leave_scope();
13959
13960               if (is_declarator)
13961                 /* Repeat the main loop.  */
13962                 continue;
13963             }
13964
13965           /* If this is the first, we can try a parenthesized
13966              declarator.  */
13967           if (first)
13968             {
13969               bool saved_in_type_id_in_expr_p;
13970
13971               parser->default_arg_ok_p = saved_default_arg_ok_p;
13972               parser->in_declarator_p = saved_in_declarator_p;
13973
13974               /* Consume the `('.  */
13975               cp_lexer_consume_token (parser->lexer);
13976               /* Parse the nested declarator.  */
13977               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
13978               parser->in_type_id_in_expr_p = true;
13979               declarator
13980                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
13981                                         /*parenthesized_p=*/NULL,
13982                                         member_p);
13983               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
13984               first = false;
13985               /* Expect a `)'.  */
13986               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
13987                 declarator = cp_error_declarator;
13988               if (declarator == cp_error_declarator)
13989                 break;
13990
13991               goto handle_declarator;
13992             }
13993           /* Otherwise, we must be done.  */
13994           else
13995             break;
13996         }
13997       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
13998                && token->type == CPP_OPEN_SQUARE)
13999         {
14000           /* Parse an array-declarator.  */
14001           tree bounds;
14002
14003           if (ctor_dtor_or_conv_p)
14004             *ctor_dtor_or_conv_p = 0;
14005
14006           first = false;
14007           parser->default_arg_ok_p = false;
14008           parser->in_declarator_p = true;
14009           /* Consume the `['.  */
14010           cp_lexer_consume_token (parser->lexer);
14011           /* Peek at the next token.  */
14012           token = cp_lexer_peek_token (parser->lexer);
14013           /* If the next token is `]', then there is no
14014              constant-expression.  */
14015           if (token->type != CPP_CLOSE_SQUARE)
14016             {
14017               bool non_constant_p;
14018
14019               bounds
14020                 = cp_parser_constant_expression (parser,
14021                                                  /*allow_non_constant=*/true,
14022                                                  &non_constant_p);
14023               if (!non_constant_p)
14024                 bounds = fold_non_dependent_expr (bounds);
14025               /* Normally, the array bound must be an integral constant
14026                  expression.  However, as an extension, we allow VLAs
14027                  in function scopes.  */
14028               else if (!parser->in_function_body)
14029                 {
14030                   error_at (token->location,
14031                             "array bound is not an integer constant");
14032                   bounds = error_mark_node;
14033                 }
14034               else if (processing_template_decl && !error_operand_p (bounds))
14035                 {
14036                   /* Remember this wasn't a constant-expression.  */
14037                   bounds = build_nop (TREE_TYPE (bounds), bounds);
14038                   TREE_SIDE_EFFECTS (bounds) = 1;
14039                 }
14040             }
14041           else
14042             bounds = NULL_TREE;
14043           /* Look for the closing `]'.  */
14044           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>"))
14045             {
14046               declarator = cp_error_declarator;
14047               break;
14048             }
14049
14050           declarator = make_array_declarator (declarator, bounds);
14051         }
14052       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
14053         {
14054           {
14055             tree qualifying_scope;
14056             tree unqualified_name;
14057             special_function_kind sfk;
14058             bool abstract_ok;
14059             bool pack_expansion_p = false;
14060             cp_token *declarator_id_start_token;
14061
14062             /* Parse a declarator-id */
14063             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
14064             if (abstract_ok)
14065               {
14066                 cp_parser_parse_tentatively (parser);
14067
14068                 /* If we see an ellipsis, we should be looking at a
14069                    parameter pack. */
14070                 if (token->type == CPP_ELLIPSIS)
14071                   {
14072                     /* Consume the `...' */
14073                     cp_lexer_consume_token (parser->lexer);
14074
14075                     pack_expansion_p = true;
14076                   }
14077               }
14078
14079             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
14080             unqualified_name
14081               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
14082             qualifying_scope = parser->scope;
14083             if (abstract_ok)
14084               {
14085                 bool okay = false;
14086
14087                 if (!unqualified_name && pack_expansion_p)
14088                   {
14089                     /* Check whether an error occurred. */
14090                     okay = !cp_parser_error_occurred (parser);
14091
14092                     /* We already consumed the ellipsis to mark a
14093                        parameter pack, but we have no way to report it,
14094                        so abort the tentative parse. We will be exiting
14095                        immediately anyway. */
14096                     cp_parser_abort_tentative_parse (parser);
14097                   }
14098                 else
14099                   okay = cp_parser_parse_definitely (parser);
14100
14101                 if (!okay)
14102                   unqualified_name = error_mark_node;
14103                 else if (unqualified_name
14104                          && (qualifying_scope
14105                              || (TREE_CODE (unqualified_name)
14106                                  != IDENTIFIER_NODE)))
14107                   {
14108                     cp_parser_error (parser, "expected unqualified-id");
14109                     unqualified_name = error_mark_node;
14110                   }
14111               }
14112
14113             if (!unqualified_name)
14114               return NULL;
14115             if (unqualified_name == error_mark_node)
14116               {
14117                 declarator = cp_error_declarator;
14118                 pack_expansion_p = false;
14119                 declarator->parameter_pack_p = false;
14120                 break;
14121               }
14122
14123             if (qualifying_scope && at_namespace_scope_p ()
14124                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
14125               {
14126                 /* In the declaration of a member of a template class
14127                    outside of the class itself, the SCOPE will sometimes
14128                    be a TYPENAME_TYPE.  For example, given:
14129
14130                    template <typename T>
14131                    int S<T>::R::i = 3;
14132
14133                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
14134                    this context, we must resolve S<T>::R to an ordinary
14135                    type, rather than a typename type.
14136
14137                    The reason we normally avoid resolving TYPENAME_TYPEs
14138                    is that a specialization of `S' might render
14139                    `S<T>::R' not a type.  However, if `S' is
14140                    specialized, then this `i' will not be used, so there
14141                    is no harm in resolving the types here.  */
14142                 tree type;
14143
14144                 /* Resolve the TYPENAME_TYPE.  */
14145                 type = resolve_typename_type (qualifying_scope,
14146                                               /*only_current_p=*/false);
14147                 /* If that failed, the declarator is invalid.  */
14148                 if (TREE_CODE (type) == TYPENAME_TYPE)
14149                   error_at (declarator_id_start_token->location,
14150                             "%<%T::%E%> is not a type",
14151                             TYPE_CONTEXT (qualifying_scope),
14152                             TYPE_IDENTIFIER (qualifying_scope));
14153                 qualifying_scope = type;
14154               }
14155
14156             sfk = sfk_none;
14157
14158             if (unqualified_name)
14159               {
14160                 tree class_type;
14161
14162                 if (qualifying_scope
14163                     && CLASS_TYPE_P (qualifying_scope))
14164                   class_type = qualifying_scope;
14165                 else
14166                   class_type = current_class_type;
14167
14168                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
14169                   {
14170                     tree name_type = TREE_TYPE (unqualified_name);
14171                     if (class_type && same_type_p (name_type, class_type))
14172                       {
14173                         if (qualifying_scope
14174                             && CLASSTYPE_USE_TEMPLATE (name_type))
14175                           {
14176                             error_at (declarator_id_start_token->location,
14177                                       "invalid use of constructor as a template");
14178                             inform (declarator_id_start_token->location,
14179                                     "use %<%T::%D%> instead of %<%T::%D%> to "
14180                                     "name the constructor in a qualified name",
14181                                     class_type,
14182                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
14183                                     class_type, name_type);
14184                             declarator = cp_error_declarator;
14185                             break;
14186                           }
14187                         else
14188                           unqualified_name = constructor_name (class_type);
14189                       }
14190                     else
14191                       {
14192                         /* We do not attempt to print the declarator
14193                            here because we do not have enough
14194                            information about its original syntactic
14195                            form.  */
14196                         cp_parser_error (parser, "invalid declarator");
14197                         declarator = cp_error_declarator;
14198                         break;
14199                       }
14200                   }
14201
14202                 if (class_type)
14203                   {
14204                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
14205                       sfk = sfk_destructor;
14206                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
14207                       sfk = sfk_conversion;
14208                     else if (/* There's no way to declare a constructor
14209                                 for an anonymous type, even if the type
14210                                 got a name for linkage purposes.  */
14211                              !TYPE_WAS_ANONYMOUS (class_type)
14212                              && constructor_name_p (unqualified_name,
14213                                                     class_type))
14214                       {
14215                         unqualified_name = constructor_name (class_type);
14216                         sfk = sfk_constructor;
14217                       }
14218
14219                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
14220                       *ctor_dtor_or_conv_p = -1;
14221                   }
14222               }
14223             declarator = make_id_declarator (qualifying_scope,
14224                                              unqualified_name,
14225                                              sfk);
14226             declarator->id_loc = token->location;
14227             declarator->parameter_pack_p = pack_expansion_p;
14228
14229             if (pack_expansion_p)
14230               maybe_warn_variadic_templates ();
14231           }
14232
14233         handle_declarator:;
14234           scope = get_scope_of_declarator (declarator);
14235           if (scope)
14236             /* Any names that appear after the declarator-id for a
14237                member are looked up in the containing scope.  */
14238             pushed_scope = push_scope (scope);
14239           parser->in_declarator_p = true;
14240           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
14241               || (declarator && declarator->kind == cdk_id))
14242             /* Default args are only allowed on function
14243                declarations.  */
14244             parser->default_arg_ok_p = saved_default_arg_ok_p;
14245           else
14246             parser->default_arg_ok_p = false;
14247
14248           first = false;
14249         }
14250       /* We're done.  */
14251       else
14252         break;
14253     }
14254
14255   /* For an abstract declarator, we might wind up with nothing at this
14256      point.  That's an error; the declarator is not optional.  */
14257   if (!declarator)
14258     cp_parser_error (parser, "expected declarator");
14259
14260   /* If we entered a scope, we must exit it now.  */
14261   if (pushed_scope)
14262     pop_scope (pushed_scope);
14263
14264   parser->default_arg_ok_p = saved_default_arg_ok_p;
14265   parser->in_declarator_p = saved_in_declarator_p;
14266
14267   return declarator;
14268 }
14269
14270 /* Parse a ptr-operator.
14271
14272    ptr-operator:
14273      * cv-qualifier-seq [opt]
14274      &
14275      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
14276
14277    GNU Extension:
14278
14279    ptr-operator:
14280      & cv-qualifier-seq [opt]
14281
14282    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
14283    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
14284    an rvalue reference. In the case of a pointer-to-member, *TYPE is
14285    filled in with the TYPE containing the member.  *CV_QUALS is
14286    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
14287    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
14288    Note that the tree codes returned by this function have nothing
14289    to do with the types of trees that will be eventually be created
14290    to represent the pointer or reference type being parsed. They are
14291    just constants with suggestive names. */
14292 static enum tree_code
14293 cp_parser_ptr_operator (cp_parser* parser,
14294                         tree* type,
14295                         cp_cv_quals *cv_quals)
14296 {
14297   enum tree_code code = ERROR_MARK;
14298   cp_token *token;
14299
14300   /* Assume that it's not a pointer-to-member.  */
14301   *type = NULL_TREE;
14302   /* And that there are no cv-qualifiers.  */
14303   *cv_quals = TYPE_UNQUALIFIED;
14304
14305   /* Peek at the next token.  */
14306   token = cp_lexer_peek_token (parser->lexer);
14307
14308   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
14309   if (token->type == CPP_MULT)
14310     code = INDIRECT_REF;
14311   else if (token->type == CPP_AND)
14312     code = ADDR_EXPR;
14313   else if ((cxx_dialect != cxx98) &&
14314            token->type == CPP_AND_AND) /* C++0x only */
14315     code = NON_LVALUE_EXPR;
14316
14317   if (code != ERROR_MARK)
14318     {
14319       /* Consume the `*', `&' or `&&'.  */
14320       cp_lexer_consume_token (parser->lexer);
14321
14322       /* A `*' can be followed by a cv-qualifier-seq, and so can a
14323          `&', if we are allowing GNU extensions.  (The only qualifier
14324          that can legally appear after `&' is `restrict', but that is
14325          enforced during semantic analysis.  */
14326       if (code == INDIRECT_REF
14327           || cp_parser_allow_gnu_extensions_p (parser))
14328         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14329     }
14330   else
14331     {
14332       /* Try the pointer-to-member case.  */
14333       cp_parser_parse_tentatively (parser);
14334       /* Look for the optional `::' operator.  */
14335       cp_parser_global_scope_opt (parser,
14336                                   /*current_scope_valid_p=*/false);
14337       /* Look for the nested-name specifier.  */
14338       token = cp_lexer_peek_token (parser->lexer);
14339       cp_parser_nested_name_specifier (parser,
14340                                        /*typename_keyword_p=*/false,
14341                                        /*check_dependency_p=*/true,
14342                                        /*type_p=*/false,
14343                                        /*is_declaration=*/false);
14344       /* If we found it, and the next token is a `*', then we are
14345          indeed looking at a pointer-to-member operator.  */
14346       if (!cp_parser_error_occurred (parser)
14347           && cp_parser_require (parser, CPP_MULT, "%<*%>"))
14348         {
14349           /* Indicate that the `*' operator was used.  */
14350           code = INDIRECT_REF;
14351
14352           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
14353             error_at (token->location, "%qD is a namespace", parser->scope);
14354           else
14355             {
14356               /* The type of which the member is a member is given by the
14357                  current SCOPE.  */
14358               *type = parser->scope;
14359               /* The next name will not be qualified.  */
14360               parser->scope = NULL_TREE;
14361               parser->qualifying_scope = NULL_TREE;
14362               parser->object_scope = NULL_TREE;
14363               /* Look for the optional cv-qualifier-seq.  */
14364               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14365             }
14366         }
14367       /* If that didn't work we don't have a ptr-operator.  */
14368       if (!cp_parser_parse_definitely (parser))
14369         cp_parser_error (parser, "expected ptr-operator");
14370     }
14371
14372   return code;
14373 }
14374
14375 /* Parse an (optional) cv-qualifier-seq.
14376
14377    cv-qualifier-seq:
14378      cv-qualifier cv-qualifier-seq [opt]
14379
14380    cv-qualifier:
14381      const
14382      volatile
14383
14384    GNU Extension:
14385
14386    cv-qualifier:
14387      __restrict__
14388
14389    Returns a bitmask representing the cv-qualifiers.  */
14390
14391 static cp_cv_quals
14392 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
14393 {
14394   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
14395
14396   while (true)
14397     {
14398       cp_token *token;
14399       cp_cv_quals cv_qualifier;
14400
14401       /* Peek at the next token.  */
14402       token = cp_lexer_peek_token (parser->lexer);
14403       /* See if it's a cv-qualifier.  */
14404       switch (token->keyword)
14405         {
14406         case RID_CONST:
14407           cv_qualifier = TYPE_QUAL_CONST;
14408           break;
14409
14410         case RID_VOLATILE:
14411           cv_qualifier = TYPE_QUAL_VOLATILE;
14412           break;
14413
14414         case RID_RESTRICT:
14415           cv_qualifier = TYPE_QUAL_RESTRICT;
14416           break;
14417
14418         default:
14419           cv_qualifier = TYPE_UNQUALIFIED;
14420           break;
14421         }
14422
14423       if (!cv_qualifier)
14424         break;
14425
14426       if (cv_quals & cv_qualifier)
14427         {
14428           error_at (token->location, "duplicate cv-qualifier");
14429           cp_lexer_purge_token (parser->lexer);
14430         }
14431       else
14432         {
14433           cp_lexer_consume_token (parser->lexer);
14434           cv_quals |= cv_qualifier;
14435         }
14436     }
14437
14438   return cv_quals;
14439 }
14440
14441 /* Parse a late-specified return type, if any.  This is not a separate
14442    non-terminal, but part of a function declarator, which looks like
14443
14444    -> trailing-type-specifier-seq abstract-declarator(opt)
14445
14446    Returns the type indicated by the type-id.  */
14447
14448 static tree
14449 cp_parser_late_return_type_opt (cp_parser* parser)
14450 {
14451   cp_token *token;
14452
14453   /* Peek at the next token.  */
14454   token = cp_lexer_peek_token (parser->lexer);
14455   /* A late-specified return type is indicated by an initial '->'. */
14456   if (token->type != CPP_DEREF)
14457     return NULL_TREE;
14458
14459   /* Consume the ->.  */
14460   cp_lexer_consume_token (parser->lexer);
14461
14462   return cp_parser_trailing_type_id (parser);
14463 }
14464
14465 /* Parse a declarator-id.
14466
14467    declarator-id:
14468      id-expression
14469      :: [opt] nested-name-specifier [opt] type-name
14470
14471    In the `id-expression' case, the value returned is as for
14472    cp_parser_id_expression if the id-expression was an unqualified-id.
14473    If the id-expression was a qualified-id, then a SCOPE_REF is
14474    returned.  The first operand is the scope (either a NAMESPACE_DECL
14475    or TREE_TYPE), but the second is still just a representation of an
14476    unqualified-id.  */
14477
14478 static tree
14479 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
14480 {
14481   tree id;
14482   /* The expression must be an id-expression.  Assume that qualified
14483      names are the names of types so that:
14484
14485        template <class T>
14486        int S<T>::R::i = 3;
14487
14488      will work; we must treat `S<T>::R' as the name of a type.
14489      Similarly, assume that qualified names are templates, where
14490      required, so that:
14491
14492        template <class T>
14493        int S<T>::R<T>::i = 3;
14494
14495      will work, too.  */
14496   id = cp_parser_id_expression (parser,
14497                                 /*template_keyword_p=*/false,
14498                                 /*check_dependency_p=*/false,
14499                                 /*template_p=*/NULL,
14500                                 /*declarator_p=*/true,
14501                                 optional_p);
14502   if (id && BASELINK_P (id))
14503     id = BASELINK_FUNCTIONS (id);
14504   return id;
14505 }
14506
14507 /* Parse a type-id.
14508
14509    type-id:
14510      type-specifier-seq abstract-declarator [opt]
14511
14512    Returns the TYPE specified.  */
14513
14514 static tree
14515 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
14516                      bool is_trailing_return)
14517 {
14518   cp_decl_specifier_seq type_specifier_seq;
14519   cp_declarator *abstract_declarator;
14520
14521   /* Parse the type-specifier-seq.  */
14522   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
14523                                 is_trailing_return,
14524                                 &type_specifier_seq);
14525   if (type_specifier_seq.type == error_mark_node)
14526     return error_mark_node;
14527
14528   /* There might or might not be an abstract declarator.  */
14529   cp_parser_parse_tentatively (parser);
14530   /* Look for the declarator.  */
14531   abstract_declarator
14532     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
14533                             /*parenthesized_p=*/NULL,
14534                             /*member_p=*/false);
14535   /* Check to see if there really was a declarator.  */
14536   if (!cp_parser_parse_definitely (parser))
14537     abstract_declarator = NULL;
14538
14539   if (type_specifier_seq.type
14540       && type_uses_auto (type_specifier_seq.type))
14541     {
14542       /* A type-id with type 'auto' is only ok if the abstract declarator
14543          is a function declarator with a late-specified return type.  */
14544       if (abstract_declarator
14545           && abstract_declarator->kind == cdk_function
14546           && abstract_declarator->u.function.late_return_type)
14547         /* OK */;
14548       else
14549         {
14550           error ("invalid use of %<auto%>");
14551           return error_mark_node;
14552         }
14553     }
14554   
14555   return groktypename (&type_specifier_seq, abstract_declarator,
14556                        is_template_arg);
14557 }
14558
14559 static tree cp_parser_type_id (cp_parser *parser)
14560 {
14561   return cp_parser_type_id_1 (parser, false, false);
14562 }
14563
14564 static tree cp_parser_template_type_arg (cp_parser *parser)
14565 {
14566   return cp_parser_type_id_1 (parser, true, false);
14567 }
14568
14569 static tree cp_parser_trailing_type_id (cp_parser *parser)
14570 {
14571   return cp_parser_type_id_1 (parser, false, true);
14572 }
14573
14574 /* Parse a type-specifier-seq.
14575
14576    type-specifier-seq:
14577      type-specifier type-specifier-seq [opt]
14578
14579    GNU extension:
14580
14581    type-specifier-seq:
14582      attributes type-specifier-seq [opt]
14583
14584    If IS_CONDITION is true, we are at the start of a "condition",
14585    e.g., we've just seen "if (".
14586
14587    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
14588    i.e. we've just seen "->".
14589
14590    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
14591
14592 static void
14593 cp_parser_type_specifier_seq (cp_parser* parser,
14594                               bool is_condition,
14595                               bool is_trailing_return,
14596                               cp_decl_specifier_seq *type_specifier_seq)
14597 {
14598   bool seen_type_specifier = false;
14599   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
14600   cp_token *start_token = NULL;
14601
14602   /* Clear the TYPE_SPECIFIER_SEQ.  */
14603   clear_decl_specs (type_specifier_seq);
14604
14605   /* In the context of a trailing return type, enum E { } is an
14606      elaborated-type-specifier followed by a function-body, not an
14607      enum-specifier.  */
14608   if (is_trailing_return)
14609     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
14610
14611   /* Parse the type-specifiers and attributes.  */
14612   while (true)
14613     {
14614       tree type_specifier;
14615       bool is_cv_qualifier;
14616
14617       /* Check for attributes first.  */
14618       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
14619         {
14620           type_specifier_seq->attributes =
14621             chainon (type_specifier_seq->attributes,
14622                      cp_parser_attributes_opt (parser));
14623           continue;
14624         }
14625
14626       /* record the token of the beginning of the type specifier seq,
14627          for error reporting purposes*/
14628      if (!start_token)
14629        start_token = cp_lexer_peek_token (parser->lexer);
14630
14631       /* Look for the type-specifier.  */
14632       type_specifier = cp_parser_type_specifier (parser,
14633                                                  flags,
14634                                                  type_specifier_seq,
14635                                                  /*is_declaration=*/false,
14636                                                  NULL,
14637                                                  &is_cv_qualifier);
14638       if (!type_specifier)
14639         {
14640           /* If the first type-specifier could not be found, this is not a
14641              type-specifier-seq at all.  */
14642           if (!seen_type_specifier)
14643             {
14644               cp_parser_error (parser, "expected type-specifier");
14645               type_specifier_seq->type = error_mark_node;
14646               return;
14647             }
14648           /* If subsequent type-specifiers could not be found, the
14649              type-specifier-seq is complete.  */
14650           break;
14651         }
14652
14653       seen_type_specifier = true;
14654       /* The standard says that a condition can be:
14655
14656             type-specifier-seq declarator = assignment-expression
14657
14658          However, given:
14659
14660            struct S {};
14661            if (int S = ...)
14662
14663          we should treat the "S" as a declarator, not as a
14664          type-specifier.  The standard doesn't say that explicitly for
14665          type-specifier-seq, but it does say that for
14666          decl-specifier-seq in an ordinary declaration.  Perhaps it
14667          would be clearer just to allow a decl-specifier-seq here, and
14668          then add a semantic restriction that if any decl-specifiers
14669          that are not type-specifiers appear, the program is invalid.  */
14670       if (is_condition && !is_cv_qualifier)
14671         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
14672     }
14673
14674   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
14675 }
14676
14677 /* Parse a parameter-declaration-clause.
14678
14679    parameter-declaration-clause:
14680      parameter-declaration-list [opt] ... [opt]
14681      parameter-declaration-list , ...
14682
14683    Returns a representation for the parameter declarations.  A return
14684    value of NULL indicates a parameter-declaration-clause consisting
14685    only of an ellipsis.  */
14686
14687 static tree
14688 cp_parser_parameter_declaration_clause (cp_parser* parser)
14689 {
14690   tree parameters;
14691   cp_token *token;
14692   bool ellipsis_p;
14693   bool is_error;
14694
14695   /* Peek at the next token.  */
14696   token = cp_lexer_peek_token (parser->lexer);
14697   /* Check for trivial parameter-declaration-clauses.  */
14698   if (token->type == CPP_ELLIPSIS)
14699     {
14700       /* Consume the `...' token.  */
14701       cp_lexer_consume_token (parser->lexer);
14702       return NULL_TREE;
14703     }
14704   else if (token->type == CPP_CLOSE_PAREN)
14705     /* There are no parameters.  */
14706     {
14707 #ifndef NO_IMPLICIT_EXTERN_C
14708       if (in_system_header && current_class_type == NULL
14709           && current_lang_name == lang_name_c)
14710         return NULL_TREE;
14711       else
14712 #endif
14713         return void_list_node;
14714     }
14715   /* Check for `(void)', too, which is a special case.  */
14716   else if (token->keyword == RID_VOID
14717            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
14718                == CPP_CLOSE_PAREN))
14719     {
14720       /* Consume the `void' token.  */
14721       cp_lexer_consume_token (parser->lexer);
14722       /* There are no parameters.  */
14723       return void_list_node;
14724     }
14725
14726   /* Parse the parameter-declaration-list.  */
14727   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
14728   /* If a parse error occurred while parsing the
14729      parameter-declaration-list, then the entire
14730      parameter-declaration-clause is erroneous.  */
14731   if (is_error)
14732     return NULL;
14733
14734   /* Peek at the next token.  */
14735   token = cp_lexer_peek_token (parser->lexer);
14736   /* If it's a `,', the clause should terminate with an ellipsis.  */
14737   if (token->type == CPP_COMMA)
14738     {
14739       /* Consume the `,'.  */
14740       cp_lexer_consume_token (parser->lexer);
14741       /* Expect an ellipsis.  */
14742       ellipsis_p
14743         = (cp_parser_require (parser, CPP_ELLIPSIS, "%<...%>") != NULL);
14744     }
14745   /* It might also be `...' if the optional trailing `,' was
14746      omitted.  */
14747   else if (token->type == CPP_ELLIPSIS)
14748     {
14749       /* Consume the `...' token.  */
14750       cp_lexer_consume_token (parser->lexer);
14751       /* And remember that we saw it.  */
14752       ellipsis_p = true;
14753     }
14754   else
14755     ellipsis_p = false;
14756
14757   /* Finish the parameter list.  */
14758   if (!ellipsis_p)
14759     parameters = chainon (parameters, void_list_node);
14760
14761   return parameters;
14762 }
14763
14764 /* Parse a parameter-declaration-list.
14765
14766    parameter-declaration-list:
14767      parameter-declaration
14768      parameter-declaration-list , parameter-declaration
14769
14770    Returns a representation of the parameter-declaration-list, as for
14771    cp_parser_parameter_declaration_clause.  However, the
14772    `void_list_node' is never appended to the list.  Upon return,
14773    *IS_ERROR will be true iff an error occurred.  */
14774
14775 static tree
14776 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
14777 {
14778   tree parameters = NULL_TREE;
14779   tree *tail = &parameters; 
14780   bool saved_in_unbraced_linkage_specification_p;
14781   int index = 0;
14782
14783   /* Assume all will go well.  */
14784   *is_error = false;
14785   /* The special considerations that apply to a function within an
14786      unbraced linkage specifications do not apply to the parameters
14787      to the function.  */
14788   saved_in_unbraced_linkage_specification_p 
14789     = parser->in_unbraced_linkage_specification_p;
14790   parser->in_unbraced_linkage_specification_p = false;
14791
14792   /* Look for more parameters.  */
14793   while (true)
14794     {
14795       cp_parameter_declarator *parameter;
14796       tree decl = error_mark_node;
14797       bool parenthesized_p;
14798       /* Parse the parameter.  */
14799       parameter
14800         = cp_parser_parameter_declaration (parser,
14801                                            /*template_parm_p=*/false,
14802                                            &parenthesized_p);
14803
14804       /* We don't know yet if the enclosing context is deprecated, so wait
14805          and warn in grokparms if appropriate.  */
14806       deprecated_state = DEPRECATED_SUPPRESS;
14807
14808       if (parameter)
14809         decl = grokdeclarator (parameter->declarator,
14810                                &parameter->decl_specifiers,
14811                                PARM,
14812                                parameter->default_argument != NULL_TREE,
14813                                &parameter->decl_specifiers.attributes);
14814
14815       deprecated_state = DEPRECATED_NORMAL;
14816
14817       /* If a parse error occurred parsing the parameter declaration,
14818          then the entire parameter-declaration-list is erroneous.  */
14819       if (decl == error_mark_node)
14820         {
14821           *is_error = true;
14822           parameters = error_mark_node;
14823           break;
14824         }
14825
14826       if (parameter->decl_specifiers.attributes)
14827         cplus_decl_attributes (&decl,
14828                                parameter->decl_specifiers.attributes,
14829                                0);
14830       if (DECL_NAME (decl))
14831         decl = pushdecl (decl);
14832
14833       if (decl != error_mark_node)
14834         {
14835           retrofit_lang_decl (decl);
14836           DECL_PARM_INDEX (decl) = ++index;
14837         }
14838
14839       /* Add the new parameter to the list.  */
14840       *tail = build_tree_list (parameter->default_argument, decl);
14841       tail = &TREE_CHAIN (*tail);
14842
14843       /* Peek at the next token.  */
14844       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
14845           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
14846           /* These are for Objective-C++ */
14847           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14848           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14849         /* The parameter-declaration-list is complete.  */
14850         break;
14851       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
14852         {
14853           cp_token *token;
14854
14855           /* Peek at the next token.  */
14856           token = cp_lexer_peek_nth_token (parser->lexer, 2);
14857           /* If it's an ellipsis, then the list is complete.  */
14858           if (token->type == CPP_ELLIPSIS)
14859             break;
14860           /* Otherwise, there must be more parameters.  Consume the
14861              `,'.  */
14862           cp_lexer_consume_token (parser->lexer);
14863           /* When parsing something like:
14864
14865                 int i(float f, double d)
14866
14867              we can tell after seeing the declaration for "f" that we
14868              are not looking at an initialization of a variable "i",
14869              but rather at the declaration of a function "i".
14870
14871              Due to the fact that the parsing of template arguments
14872              (as specified to a template-id) requires backtracking we
14873              cannot use this technique when inside a template argument
14874              list.  */
14875           if (!parser->in_template_argument_list_p
14876               && !parser->in_type_id_in_expr_p
14877               && cp_parser_uncommitted_to_tentative_parse_p (parser)
14878               /* However, a parameter-declaration of the form
14879                  "foat(f)" (which is a valid declaration of a
14880                  parameter "f") can also be interpreted as an
14881                  expression (the conversion of "f" to "float").  */
14882               && !parenthesized_p)
14883             cp_parser_commit_to_tentative_parse (parser);
14884         }
14885       else
14886         {
14887           cp_parser_error (parser, "expected %<,%> or %<...%>");
14888           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14889             cp_parser_skip_to_closing_parenthesis (parser,
14890                                                    /*recovering=*/true,
14891                                                    /*or_comma=*/false,
14892                                                    /*consume_paren=*/false);
14893           break;
14894         }
14895     }
14896
14897   parser->in_unbraced_linkage_specification_p
14898     = saved_in_unbraced_linkage_specification_p;
14899
14900   return parameters;
14901 }
14902
14903 /* Parse a parameter declaration.
14904
14905    parameter-declaration:
14906      decl-specifier-seq ... [opt] declarator
14907      decl-specifier-seq declarator = assignment-expression
14908      decl-specifier-seq ... [opt] abstract-declarator [opt]
14909      decl-specifier-seq abstract-declarator [opt] = assignment-expression
14910
14911    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
14912    declares a template parameter.  (In that case, a non-nested `>'
14913    token encountered during the parsing of the assignment-expression
14914    is not interpreted as a greater-than operator.)
14915
14916    Returns a representation of the parameter, or NULL if an error
14917    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
14918    true iff the declarator is of the form "(p)".  */
14919
14920 static cp_parameter_declarator *
14921 cp_parser_parameter_declaration (cp_parser *parser,
14922                                  bool template_parm_p,
14923                                  bool *parenthesized_p)
14924 {
14925   int declares_class_or_enum;
14926   bool greater_than_is_operator_p;
14927   cp_decl_specifier_seq decl_specifiers;
14928   cp_declarator *declarator;
14929   tree default_argument;
14930   cp_token *token = NULL, *declarator_token_start = NULL;
14931   const char *saved_message;
14932
14933   /* In a template parameter, `>' is not an operator.
14934
14935      [temp.param]
14936
14937      When parsing a default template-argument for a non-type
14938      template-parameter, the first non-nested `>' is taken as the end
14939      of the template parameter-list rather than a greater-than
14940      operator.  */
14941   greater_than_is_operator_p = !template_parm_p;
14942
14943   /* Type definitions may not appear in parameter types.  */
14944   saved_message = parser->type_definition_forbidden_message;
14945   parser->type_definition_forbidden_message
14946     = "types may not be defined in parameter types";
14947
14948   /* Parse the declaration-specifiers.  */
14949   cp_parser_decl_specifier_seq (parser,
14950                                 CP_PARSER_FLAGS_NONE,
14951                                 &decl_specifiers,
14952                                 &declares_class_or_enum);
14953   /* If an error occurred, there's no reason to attempt to parse the
14954      rest of the declaration.  */
14955   if (cp_parser_error_occurred (parser))
14956     {
14957       parser->type_definition_forbidden_message = saved_message;
14958       return NULL;
14959     }
14960
14961   /* Peek at the next token.  */
14962   token = cp_lexer_peek_token (parser->lexer);
14963
14964   /* If the next token is a `)', `,', `=', `>', or `...', then there
14965      is no declarator. However, when variadic templates are enabled,
14966      there may be a declarator following `...'.  */
14967   if (token->type == CPP_CLOSE_PAREN
14968       || token->type == CPP_COMMA
14969       || token->type == CPP_EQ
14970       || token->type == CPP_GREATER)
14971     {
14972       declarator = NULL;
14973       if (parenthesized_p)
14974         *parenthesized_p = false;
14975     }
14976   /* Otherwise, there should be a declarator.  */
14977   else
14978     {
14979       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14980       parser->default_arg_ok_p = false;
14981
14982       /* After seeing a decl-specifier-seq, if the next token is not a
14983          "(", there is no possibility that the code is a valid
14984          expression.  Therefore, if parsing tentatively, we commit at
14985          this point.  */
14986       if (!parser->in_template_argument_list_p
14987           /* In an expression context, having seen:
14988
14989                (int((char ...
14990
14991              we cannot be sure whether we are looking at a
14992              function-type (taking a "char" as a parameter) or a cast
14993              of some object of type "char" to "int".  */
14994           && !parser->in_type_id_in_expr_p
14995           && cp_parser_uncommitted_to_tentative_parse_p (parser)
14996           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
14997         cp_parser_commit_to_tentative_parse (parser);
14998       /* Parse the declarator.  */
14999       declarator_token_start = token;
15000       declarator = cp_parser_declarator (parser,
15001                                          CP_PARSER_DECLARATOR_EITHER,
15002                                          /*ctor_dtor_or_conv_p=*/NULL,
15003                                          parenthesized_p,
15004                                          /*member_p=*/false);
15005       parser->default_arg_ok_p = saved_default_arg_ok_p;
15006       /* After the declarator, allow more attributes.  */
15007       decl_specifiers.attributes
15008         = chainon (decl_specifiers.attributes,
15009                    cp_parser_attributes_opt (parser));
15010     }
15011
15012   /* If the next token is an ellipsis, and we have not seen a
15013      declarator name, and the type of the declarator contains parameter
15014      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
15015      a parameter pack expansion expression. Otherwise, leave the
15016      ellipsis for a C-style variadic function. */
15017   token = cp_lexer_peek_token (parser->lexer);
15018   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15019     {
15020       tree type = decl_specifiers.type;
15021
15022       if (type && DECL_P (type))
15023         type = TREE_TYPE (type);
15024
15025       if (type
15026           && TREE_CODE (type) != TYPE_PACK_EXPANSION
15027           && declarator_can_be_parameter_pack (declarator)
15028           && (!declarator || !declarator->parameter_pack_p)
15029           && uses_parameter_packs (type))
15030         {
15031           /* Consume the `...'. */
15032           cp_lexer_consume_token (parser->lexer);
15033           maybe_warn_variadic_templates ();
15034           
15035           /* Build a pack expansion type */
15036           if (declarator)
15037             declarator->parameter_pack_p = true;
15038           else
15039             decl_specifiers.type = make_pack_expansion (type);
15040         }
15041     }
15042
15043   /* The restriction on defining new types applies only to the type
15044      of the parameter, not to the default argument.  */
15045   parser->type_definition_forbidden_message = saved_message;
15046
15047   /* If the next token is `=', then process a default argument.  */
15048   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
15049     {
15050       /* Consume the `='.  */
15051       cp_lexer_consume_token (parser->lexer);
15052
15053       /* If we are defining a class, then the tokens that make up the
15054          default argument must be saved and processed later.  */
15055       if (!template_parm_p && at_class_scope_p ()
15056           && TYPE_BEING_DEFINED (current_class_type)
15057           && !LAMBDA_TYPE_P (current_class_type))
15058         {
15059           unsigned depth = 0;
15060           int maybe_template_id = 0;
15061           cp_token *first_token;
15062           cp_token *token;
15063
15064           /* Add tokens until we have processed the entire default
15065              argument.  We add the range [first_token, token).  */
15066           first_token = cp_lexer_peek_token (parser->lexer);
15067           while (true)
15068             {
15069               bool done = false;
15070
15071               /* Peek at the next token.  */
15072               token = cp_lexer_peek_token (parser->lexer);
15073               /* What we do depends on what token we have.  */
15074               switch (token->type)
15075                 {
15076                   /* In valid code, a default argument must be
15077                      immediately followed by a `,' `)', or `...'.  */
15078                 case CPP_COMMA:
15079                   if (depth == 0 && maybe_template_id)
15080                     {
15081                       /* If we've seen a '<', we might be in a
15082                          template-argument-list.  Until Core issue 325 is
15083                          resolved, we don't know how this situation ought
15084                          to be handled, so try to DTRT.  We check whether
15085                          what comes after the comma is a valid parameter
15086                          declaration list.  If it is, then the comma ends
15087                          the default argument; otherwise the default
15088                          argument continues.  */
15089                       bool error = false;
15090
15091                       /* Set ITALP so cp_parser_parameter_declaration_list
15092                          doesn't decide to commit to this parse.  */
15093                       bool saved_italp = parser->in_template_argument_list_p;
15094                       parser->in_template_argument_list_p = true;
15095
15096                       cp_parser_parse_tentatively (parser);
15097                       cp_lexer_consume_token (parser->lexer);
15098                       cp_parser_parameter_declaration_list (parser, &error);
15099                       if (!cp_parser_error_occurred (parser) && !error)
15100                         done = true;
15101                       cp_parser_abort_tentative_parse (parser);
15102
15103                       parser->in_template_argument_list_p = saved_italp;
15104                       break;
15105                     }
15106                 case CPP_CLOSE_PAREN:
15107                 case CPP_ELLIPSIS:
15108                   /* If we run into a non-nested `;', `}', or `]',
15109                      then the code is invalid -- but the default
15110                      argument is certainly over.  */
15111                 case CPP_SEMICOLON:
15112                 case CPP_CLOSE_BRACE:
15113                 case CPP_CLOSE_SQUARE:
15114                   if (depth == 0)
15115                     done = true;
15116                   /* Update DEPTH, if necessary.  */
15117                   else if (token->type == CPP_CLOSE_PAREN
15118                            || token->type == CPP_CLOSE_BRACE
15119                            || token->type == CPP_CLOSE_SQUARE)
15120                     --depth;
15121                   break;
15122
15123                 case CPP_OPEN_PAREN:
15124                 case CPP_OPEN_SQUARE:
15125                 case CPP_OPEN_BRACE:
15126                   ++depth;
15127                   break;
15128
15129                 case CPP_LESS:
15130                   if (depth == 0)
15131                     /* This might be the comparison operator, or it might
15132                        start a template argument list.  */
15133                     ++maybe_template_id;
15134                   break;
15135
15136                 case CPP_RSHIFT:
15137                   if (cxx_dialect == cxx98)
15138                     break;
15139                   /* Fall through for C++0x, which treats the `>>'
15140                      operator like two `>' tokens in certain
15141                      cases.  */
15142
15143                 case CPP_GREATER:
15144                   if (depth == 0)
15145                     {
15146                       /* This might be an operator, or it might close a
15147                          template argument list.  But if a previous '<'
15148                          started a template argument list, this will have
15149                          closed it, so we can't be in one anymore.  */
15150                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
15151                       if (maybe_template_id < 0)
15152                         maybe_template_id = 0;
15153                     }
15154                   break;
15155
15156                   /* If we run out of tokens, issue an error message.  */
15157                 case CPP_EOF:
15158                 case CPP_PRAGMA_EOL:
15159                   error_at (token->location, "file ends in default argument");
15160                   done = true;
15161                   break;
15162
15163                 case CPP_NAME:
15164                 case CPP_SCOPE:
15165                   /* In these cases, we should look for template-ids.
15166                      For example, if the default argument is
15167                      `X<int, double>()', we need to do name lookup to
15168                      figure out whether or not `X' is a template; if
15169                      so, the `,' does not end the default argument.
15170
15171                      That is not yet done.  */
15172                   break;
15173
15174                 default:
15175                   break;
15176                 }
15177
15178               /* If we've reached the end, stop.  */
15179               if (done)
15180                 break;
15181
15182               /* Add the token to the token block.  */
15183               token = cp_lexer_consume_token (parser->lexer);
15184             }
15185
15186           /* Create a DEFAULT_ARG to represent the unparsed default
15187              argument.  */
15188           default_argument = make_node (DEFAULT_ARG);
15189           DEFARG_TOKENS (default_argument)
15190             = cp_token_cache_new (first_token, token);
15191           DEFARG_INSTANTIATIONS (default_argument) = NULL;
15192         }
15193       /* Outside of a class definition, we can just parse the
15194          assignment-expression.  */
15195       else
15196         {
15197           token = cp_lexer_peek_token (parser->lexer);
15198           default_argument 
15199             = cp_parser_default_argument (parser, template_parm_p);
15200         }
15201
15202       if (!parser->default_arg_ok_p)
15203         {
15204           if (flag_permissive)
15205             warning (0, "deprecated use of default argument for parameter of non-function");
15206           else
15207             {
15208               error_at (token->location,
15209                         "default arguments are only "
15210                         "permitted for function parameters");
15211               default_argument = NULL_TREE;
15212             }
15213         }
15214       else if ((declarator && declarator->parameter_pack_p)
15215                || (decl_specifiers.type
15216                    && PACK_EXPANSION_P (decl_specifiers.type)))
15217         {
15218           /* Find the name of the parameter pack.  */     
15219           cp_declarator *id_declarator = declarator;
15220           while (id_declarator && id_declarator->kind != cdk_id)
15221             id_declarator = id_declarator->declarator;
15222           
15223           if (id_declarator && id_declarator->kind == cdk_id)
15224             error_at (declarator_token_start->location,
15225                       template_parm_p 
15226                       ? "template parameter pack %qD"
15227                       " cannot have a default argument"
15228                       : "parameter pack %qD cannot have a default argument",
15229                       id_declarator->u.id.unqualified_name);
15230           else
15231             error_at (declarator_token_start->location,
15232                       template_parm_p 
15233                       ? "template parameter pack cannot have a default argument"
15234                       : "parameter pack cannot have a default argument");
15235           
15236           default_argument = NULL_TREE;
15237         }
15238     }
15239   else
15240     default_argument = NULL_TREE;
15241
15242   return make_parameter_declarator (&decl_specifiers,
15243                                     declarator,
15244                                     default_argument);
15245 }
15246
15247 /* Parse a default argument and return it.
15248
15249    TEMPLATE_PARM_P is true if this is a default argument for a
15250    non-type template parameter.  */
15251 static tree
15252 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
15253 {
15254   tree default_argument = NULL_TREE;
15255   bool saved_greater_than_is_operator_p;
15256   bool saved_local_variables_forbidden_p;
15257
15258   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
15259      set correctly.  */
15260   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
15261   parser->greater_than_is_operator_p = !template_parm_p;
15262   /* Local variable names (and the `this' keyword) may not
15263      appear in a default argument.  */
15264   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
15265   parser->local_variables_forbidden_p = true;
15266   /* Parse the assignment-expression.  */
15267   if (template_parm_p)
15268     push_deferring_access_checks (dk_no_deferred);
15269   default_argument
15270     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
15271   if (template_parm_p)
15272     pop_deferring_access_checks ();
15273   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
15274   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
15275
15276   return default_argument;
15277 }
15278
15279 /* Parse a function-body.
15280
15281    function-body:
15282      compound_statement  */
15283
15284 static void
15285 cp_parser_function_body (cp_parser *parser)
15286 {
15287   cp_parser_compound_statement (parser, NULL, false);
15288 }
15289
15290 /* Parse a ctor-initializer-opt followed by a function-body.  Return
15291    true if a ctor-initializer was present.  */
15292
15293 static bool
15294 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
15295 {
15296   tree body;
15297   bool ctor_initializer_p;
15298
15299   /* Begin the function body.  */
15300   body = begin_function_body ();
15301   /* Parse the optional ctor-initializer.  */
15302   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
15303   /* Parse the function-body.  */
15304   cp_parser_function_body (parser);
15305   /* Finish the function body.  */
15306   finish_function_body (body);
15307
15308   return ctor_initializer_p;
15309 }
15310
15311 /* Parse an initializer.
15312
15313    initializer:
15314      = initializer-clause
15315      ( expression-list )
15316
15317    Returns an expression representing the initializer.  If no
15318    initializer is present, NULL_TREE is returned.
15319
15320    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
15321    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
15322    set to TRUE if there is no initializer present.  If there is an
15323    initializer, and it is not a constant-expression, *NON_CONSTANT_P
15324    is set to true; otherwise it is set to false.  */
15325
15326 static tree
15327 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
15328                        bool* non_constant_p)
15329 {
15330   cp_token *token;
15331   tree init;
15332
15333   /* Peek at the next token.  */
15334   token = cp_lexer_peek_token (parser->lexer);
15335
15336   /* Let our caller know whether or not this initializer was
15337      parenthesized.  */
15338   *is_direct_init = (token->type != CPP_EQ);
15339   /* Assume that the initializer is constant.  */
15340   *non_constant_p = false;
15341
15342   if (token->type == CPP_EQ)
15343     {
15344       /* Consume the `='.  */
15345       cp_lexer_consume_token (parser->lexer);
15346       /* Parse the initializer-clause.  */
15347       init = cp_parser_initializer_clause (parser, non_constant_p);
15348     }
15349   else if (token->type == CPP_OPEN_PAREN)
15350     {
15351       VEC(tree,gc) *vec;
15352       vec = cp_parser_parenthesized_expression_list (parser, false,
15353                                                      /*cast_p=*/false,
15354                                                      /*allow_expansion_p=*/true,
15355                                                      non_constant_p);
15356       if (vec == NULL)
15357         return error_mark_node;
15358       init = build_tree_list_vec (vec);
15359       release_tree_vector (vec);
15360     }
15361   else if (token->type == CPP_OPEN_BRACE)
15362     {
15363       maybe_warn_cpp0x ("extended initializer lists");
15364       init = cp_parser_braced_list (parser, non_constant_p);
15365       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
15366     }
15367   else
15368     {
15369       /* Anything else is an error.  */
15370       cp_parser_error (parser, "expected initializer");
15371       init = error_mark_node;
15372     }
15373
15374   return init;
15375 }
15376
15377 /* Parse an initializer-clause.
15378
15379    initializer-clause:
15380      assignment-expression
15381      braced-init-list
15382
15383    Returns an expression representing the initializer.
15384
15385    If the `assignment-expression' production is used the value
15386    returned is simply a representation for the expression.
15387
15388    Otherwise, calls cp_parser_braced_list.  */
15389
15390 static tree
15391 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
15392 {
15393   tree initializer;
15394
15395   /* Assume the expression is constant.  */
15396   *non_constant_p = false;
15397
15398   /* If it is not a `{', then we are looking at an
15399      assignment-expression.  */
15400   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
15401     {
15402       initializer
15403         = cp_parser_constant_expression (parser,
15404                                         /*allow_non_constant_p=*/true,
15405                                         non_constant_p);
15406       if (!*non_constant_p)
15407         initializer = fold_non_dependent_expr (initializer);
15408     }
15409   else
15410     initializer = cp_parser_braced_list (parser, non_constant_p);
15411
15412   return initializer;
15413 }
15414
15415 /* Parse a brace-enclosed initializer list.
15416
15417    braced-init-list:
15418      { initializer-list , [opt] }
15419      { }
15420
15421    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
15422    the elements of the initializer-list (or NULL, if the last
15423    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
15424    NULL_TREE.  There is no way to detect whether or not the optional
15425    trailing `,' was provided.  NON_CONSTANT_P is as for
15426    cp_parser_initializer.  */     
15427
15428 static tree
15429 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
15430 {
15431   tree initializer;
15432
15433   /* Consume the `{' token.  */
15434   cp_lexer_consume_token (parser->lexer);
15435   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
15436   initializer = make_node (CONSTRUCTOR);
15437   /* If it's not a `}', then there is a non-trivial initializer.  */
15438   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
15439     {
15440       /* Parse the initializer list.  */
15441       CONSTRUCTOR_ELTS (initializer)
15442         = cp_parser_initializer_list (parser, non_constant_p);
15443       /* A trailing `,' token is allowed.  */
15444       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15445         cp_lexer_consume_token (parser->lexer);
15446     }
15447   /* Now, there should be a trailing `}'.  */
15448   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15449   TREE_TYPE (initializer) = init_list_type_node;
15450   return initializer;
15451 }
15452
15453 /* Parse an initializer-list.
15454
15455    initializer-list:
15456      initializer-clause ... [opt]
15457      initializer-list , initializer-clause ... [opt]
15458
15459    GNU Extension:
15460
15461    initializer-list:
15462      identifier : initializer-clause
15463      initializer-list, identifier : initializer-clause
15464
15465    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
15466    for the initializer.  If the INDEX of the elt is non-NULL, it is the
15467    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
15468    as for cp_parser_initializer.  */
15469
15470 static VEC(constructor_elt,gc) *
15471 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
15472 {
15473   VEC(constructor_elt,gc) *v = NULL;
15474
15475   /* Assume all of the expressions are constant.  */
15476   *non_constant_p = false;
15477
15478   /* Parse the rest of the list.  */
15479   while (true)
15480     {
15481       cp_token *token;
15482       tree identifier;
15483       tree initializer;
15484       bool clause_non_constant_p;
15485
15486       /* If the next token is an identifier and the following one is a
15487          colon, we are looking at the GNU designated-initializer
15488          syntax.  */
15489       if (cp_parser_allow_gnu_extensions_p (parser)
15490           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
15491           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
15492         {
15493           /* Warn the user that they are using an extension.  */
15494           pedwarn (input_location, OPT_pedantic, 
15495                    "ISO C++ does not allow designated initializers");
15496           /* Consume the identifier.  */
15497           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
15498           /* Consume the `:'.  */
15499           cp_lexer_consume_token (parser->lexer);
15500         }
15501       else
15502         identifier = NULL_TREE;
15503
15504       /* Parse the initializer.  */
15505       initializer = cp_parser_initializer_clause (parser,
15506                                                   &clause_non_constant_p);
15507       /* If any clause is non-constant, so is the entire initializer.  */
15508       if (clause_non_constant_p)
15509         *non_constant_p = true;
15510
15511       /* If we have an ellipsis, this is an initializer pack
15512          expansion.  */
15513       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
15514         {
15515           /* Consume the `...'.  */
15516           cp_lexer_consume_token (parser->lexer);
15517
15518           /* Turn the initializer into an initializer expansion.  */
15519           initializer = make_pack_expansion (initializer);
15520         }
15521
15522       /* Add it to the vector.  */
15523       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
15524
15525       /* If the next token is not a comma, we have reached the end of
15526          the list.  */
15527       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
15528         break;
15529
15530       /* Peek at the next token.  */
15531       token = cp_lexer_peek_nth_token (parser->lexer, 2);
15532       /* If the next token is a `}', then we're still done.  An
15533          initializer-clause can have a trailing `,' after the
15534          initializer-list and before the closing `}'.  */
15535       if (token->type == CPP_CLOSE_BRACE)
15536         break;
15537
15538       /* Consume the `,' token.  */
15539       cp_lexer_consume_token (parser->lexer);
15540     }
15541
15542   return v;
15543 }
15544
15545 /* Classes [gram.class] */
15546
15547 /* Parse a class-name.
15548
15549    class-name:
15550      identifier
15551      template-id
15552
15553    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
15554    to indicate that names looked up in dependent types should be
15555    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
15556    keyword has been used to indicate that the name that appears next
15557    is a template.  TAG_TYPE indicates the explicit tag given before
15558    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
15559    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
15560    is the class being defined in a class-head.
15561
15562    Returns the TYPE_DECL representing the class.  */
15563
15564 static tree
15565 cp_parser_class_name (cp_parser *parser,
15566                       bool typename_keyword_p,
15567                       bool template_keyword_p,
15568                       enum tag_types tag_type,
15569                       bool check_dependency_p,
15570                       bool class_head_p,
15571                       bool is_declaration)
15572 {
15573   tree decl;
15574   tree scope;
15575   bool typename_p;
15576   cp_token *token;
15577   tree identifier = NULL_TREE;
15578
15579   /* All class-names start with an identifier.  */
15580   token = cp_lexer_peek_token (parser->lexer);
15581   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
15582     {
15583       cp_parser_error (parser, "expected class-name");
15584       return error_mark_node;
15585     }
15586
15587   /* PARSER->SCOPE can be cleared when parsing the template-arguments
15588      to a template-id, so we save it here.  */
15589   scope = parser->scope;
15590   if (scope == error_mark_node)
15591     return error_mark_node;
15592
15593   /* Any name names a type if we're following the `typename' keyword
15594      in a qualified name where the enclosing scope is type-dependent.  */
15595   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
15596                 && dependent_type_p (scope));
15597   /* Handle the common case (an identifier, but not a template-id)
15598      efficiently.  */
15599   if (token->type == CPP_NAME
15600       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
15601     {
15602       cp_token *identifier_token;
15603       bool ambiguous_p;
15604
15605       /* Look for the identifier.  */
15606       identifier_token = cp_lexer_peek_token (parser->lexer);
15607       ambiguous_p = identifier_token->ambiguous_p;
15608       identifier = cp_parser_identifier (parser);
15609       /* If the next token isn't an identifier, we are certainly not
15610          looking at a class-name.  */
15611       if (identifier == error_mark_node)
15612         decl = error_mark_node;
15613       /* If we know this is a type-name, there's no need to look it
15614          up.  */
15615       else if (typename_p)
15616         decl = identifier;
15617       else
15618         {
15619           tree ambiguous_decls;
15620           /* If we already know that this lookup is ambiguous, then
15621              we've already issued an error message; there's no reason
15622              to check again.  */
15623           if (ambiguous_p)
15624             {
15625               cp_parser_simulate_error (parser);
15626               return error_mark_node;
15627             }
15628           /* If the next token is a `::', then the name must be a type
15629              name.
15630
15631              [basic.lookup.qual]
15632
15633              During the lookup for a name preceding the :: scope
15634              resolution operator, object, function, and enumerator
15635              names are ignored.  */
15636           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15637             tag_type = typename_type;
15638           /* Look up the name.  */
15639           decl = cp_parser_lookup_name (parser, identifier,
15640                                         tag_type,
15641                                         /*is_template=*/false,
15642                                         /*is_namespace=*/false,
15643                                         check_dependency_p,
15644                                         &ambiguous_decls,
15645                                         identifier_token->location);
15646           if (ambiguous_decls)
15647             {
15648               error_at (identifier_token->location,
15649                         "reference to %qD is ambiguous", identifier);
15650               print_candidates (ambiguous_decls);
15651               if (cp_parser_parsing_tentatively (parser))
15652                 {
15653                   identifier_token->ambiguous_p = true;
15654                   cp_parser_simulate_error (parser);
15655                 }
15656               return error_mark_node;
15657             }
15658         }
15659     }
15660   else
15661     {
15662       /* Try a template-id.  */
15663       decl = cp_parser_template_id (parser, template_keyword_p,
15664                                     check_dependency_p,
15665                                     is_declaration);
15666       if (decl == error_mark_node)
15667         return error_mark_node;
15668     }
15669
15670   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
15671
15672   /* If this is a typename, create a TYPENAME_TYPE.  */
15673   if (typename_p && decl != error_mark_node)
15674     {
15675       decl = make_typename_type (scope, decl, typename_type,
15676                                  /*complain=*/tf_error);
15677       if (decl != error_mark_node)
15678         decl = TYPE_NAME (decl);
15679     }
15680
15681   /* Check to see that it is really the name of a class.  */
15682   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
15683       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
15684       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15685     /* Situations like this:
15686
15687          template <typename T> struct A {
15688            typename T::template X<int>::I i;
15689          };
15690
15691        are problematic.  Is `T::template X<int>' a class-name?  The
15692        standard does not seem to be definitive, but there is no other
15693        valid interpretation of the following `::'.  Therefore, those
15694        names are considered class-names.  */
15695     {
15696       decl = make_typename_type (scope, decl, tag_type, tf_error);
15697       if (decl != error_mark_node)
15698         decl = TYPE_NAME (decl);
15699     }
15700   else if (TREE_CODE (decl) != TYPE_DECL
15701            || TREE_TYPE (decl) == error_mark_node
15702            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl)))
15703     decl = error_mark_node;
15704
15705   if (decl == error_mark_node)
15706     cp_parser_error (parser, "expected class-name");
15707   else if (identifier && !parser->scope)
15708     maybe_note_name_used_in_class (identifier, decl);
15709
15710   return decl;
15711 }
15712
15713 /* Parse a class-specifier.
15714
15715    class-specifier:
15716      class-head { member-specification [opt] }
15717
15718    Returns the TREE_TYPE representing the class.  */
15719
15720 static tree
15721 cp_parser_class_specifier (cp_parser* parser)
15722 {
15723   tree type;
15724   tree attributes = NULL_TREE;
15725   bool nested_name_specifier_p;
15726   unsigned saved_num_template_parameter_lists;
15727   bool saved_in_function_body;
15728   bool saved_in_unbraced_linkage_specification_p;
15729   tree old_scope = NULL_TREE;
15730   tree scope = NULL_TREE;
15731   tree bases;
15732
15733   push_deferring_access_checks (dk_no_deferred);
15734
15735   /* Parse the class-head.  */
15736   type = cp_parser_class_head (parser,
15737                                &nested_name_specifier_p,
15738                                &attributes,
15739                                &bases);
15740   /* If the class-head was a semantic disaster, skip the entire body
15741      of the class.  */
15742   if (!type)
15743     {
15744       cp_parser_skip_to_end_of_block_or_statement (parser);
15745       pop_deferring_access_checks ();
15746       return error_mark_node;
15747     }
15748
15749   /* Look for the `{'.  */
15750   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
15751     {
15752       pop_deferring_access_checks ();
15753       return error_mark_node;
15754     }
15755
15756   /* Process the base classes. If they're invalid, skip the 
15757      entire class body.  */
15758   if (!xref_basetypes (type, bases))
15759     {
15760       /* Consuming the closing brace yields better error messages
15761          later on.  */
15762       if (cp_parser_skip_to_closing_brace (parser))
15763         cp_lexer_consume_token (parser->lexer);
15764       pop_deferring_access_checks ();
15765       return error_mark_node;
15766     }
15767
15768   /* Issue an error message if type-definitions are forbidden here.  */
15769   cp_parser_check_type_definition (parser);
15770   /* Remember that we are defining one more class.  */
15771   ++parser->num_classes_being_defined;
15772   /* Inside the class, surrounding template-parameter-lists do not
15773      apply.  */
15774   saved_num_template_parameter_lists
15775     = parser->num_template_parameter_lists;
15776   parser->num_template_parameter_lists = 0;
15777   /* We are not in a function body.  */
15778   saved_in_function_body = parser->in_function_body;
15779   parser->in_function_body = false;
15780   /* We are not immediately inside an extern "lang" block.  */
15781   saved_in_unbraced_linkage_specification_p
15782     = parser->in_unbraced_linkage_specification_p;
15783   parser->in_unbraced_linkage_specification_p = false;
15784
15785   /* Start the class.  */
15786   if (nested_name_specifier_p)
15787     {
15788       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
15789       old_scope = push_inner_scope (scope);
15790     }
15791   type = begin_class_definition (type, attributes);
15792
15793   if (type == error_mark_node)
15794     /* If the type is erroneous, skip the entire body of the class.  */
15795     cp_parser_skip_to_closing_brace (parser);
15796   else
15797     /* Parse the member-specification.  */
15798     cp_parser_member_specification_opt (parser);
15799
15800   /* Look for the trailing `}'.  */
15801   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
15802   /* Look for trailing attributes to apply to this class.  */
15803   if (cp_parser_allow_gnu_extensions_p (parser))
15804     attributes = cp_parser_attributes_opt (parser);
15805   if (type != error_mark_node)
15806     type = finish_struct (type, attributes);
15807   if (nested_name_specifier_p)
15808     pop_inner_scope (old_scope, scope);
15809   /* If this class is not itself within the scope of another class,
15810      then we need to parse the bodies of all of the queued function
15811      definitions.  Note that the queued functions defined in a class
15812      are not always processed immediately following the
15813      class-specifier for that class.  Consider:
15814
15815        struct A {
15816          struct B { void f() { sizeof (A); } };
15817        };
15818
15819      If `f' were processed before the processing of `A' were
15820      completed, there would be no way to compute the size of `A'.
15821      Note that the nesting we are interested in here is lexical --
15822      not the semantic nesting given by TYPE_CONTEXT.  In particular,
15823      for:
15824
15825        struct A { struct B; };
15826        struct A::B { void f() { } };
15827
15828      there is no need to delay the parsing of `A::B::f'.  */
15829   if (--parser->num_classes_being_defined == 0)
15830     {
15831       tree queue_entry;
15832       tree fn;
15833       tree class_type = NULL_TREE;
15834       tree pushed_scope = NULL_TREE;
15835
15836       /* In a first pass, parse default arguments to the functions.
15837          Then, in a second pass, parse the bodies of the functions.
15838          This two-phased approach handles cases like:
15839
15840             struct S {
15841               void f() { g(); }
15842               void g(int i = 3);
15843             };
15844
15845          */
15846       for (TREE_PURPOSE (parser->unparsed_functions_queues)
15847              = nreverse (TREE_PURPOSE (parser->unparsed_functions_queues));
15848            (queue_entry = TREE_PURPOSE (parser->unparsed_functions_queues));
15849            TREE_PURPOSE (parser->unparsed_functions_queues)
15850              = TREE_CHAIN (TREE_PURPOSE (parser->unparsed_functions_queues)))
15851         {
15852           fn = TREE_VALUE (queue_entry);
15853           /* If there are default arguments that have not yet been processed,
15854              take care of them now.  */
15855           if (class_type != TREE_PURPOSE (queue_entry))
15856             {
15857               if (pushed_scope)
15858                 pop_scope (pushed_scope);
15859               class_type = TREE_PURPOSE (queue_entry);
15860               pushed_scope = push_scope (class_type);
15861             }
15862           /* Make sure that any template parameters are in scope.  */
15863           maybe_begin_member_template_processing (fn);
15864           /* Parse the default argument expressions.  */
15865           cp_parser_late_parsing_default_args (parser, fn);
15866           /* Remove any template parameters from the symbol table.  */
15867           maybe_end_member_template_processing ();
15868         }
15869       if (pushed_scope)
15870         pop_scope (pushed_scope);
15871       /* Now parse the body of the functions.  */
15872       for (TREE_VALUE (parser->unparsed_functions_queues)
15873              = nreverse (TREE_VALUE (parser->unparsed_functions_queues));
15874            (queue_entry = TREE_VALUE (parser->unparsed_functions_queues));
15875            TREE_VALUE (parser->unparsed_functions_queues)
15876              = TREE_CHAIN (TREE_VALUE (parser->unparsed_functions_queues)))
15877         {
15878           /* Figure out which function we need to process.  */
15879           fn = TREE_VALUE (queue_entry);
15880           /* Parse the function.  */
15881           cp_parser_late_parsing_for_member (parser, fn);
15882         }
15883     }
15884
15885   /* Put back any saved access checks.  */
15886   pop_deferring_access_checks ();
15887
15888   /* Restore saved state.  */
15889   parser->in_function_body = saved_in_function_body;
15890   parser->num_template_parameter_lists
15891     = saved_num_template_parameter_lists;
15892   parser->in_unbraced_linkage_specification_p
15893     = saved_in_unbraced_linkage_specification_p;
15894
15895   return type;
15896 }
15897
15898 /* Parse a class-head.
15899
15900    class-head:
15901      class-key identifier [opt] base-clause [opt]
15902      class-key nested-name-specifier identifier base-clause [opt]
15903      class-key nested-name-specifier [opt] template-id
15904        base-clause [opt]
15905
15906    GNU Extensions:
15907      class-key attributes identifier [opt] base-clause [opt]
15908      class-key attributes nested-name-specifier identifier base-clause [opt]
15909      class-key attributes nested-name-specifier [opt] template-id
15910        base-clause [opt]
15911
15912    Upon return BASES is initialized to the list of base classes (or
15913    NULL, if there are none) in the same form returned by
15914    cp_parser_base_clause.
15915
15916    Returns the TYPE of the indicated class.  Sets
15917    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
15918    involving a nested-name-specifier was used, and FALSE otherwise.
15919
15920    Returns error_mark_node if this is not a class-head.
15921
15922    Returns NULL_TREE if the class-head is syntactically valid, but
15923    semantically invalid in a way that means we should skip the entire
15924    body of the class.  */
15925
15926 static tree
15927 cp_parser_class_head (cp_parser* parser,
15928                       bool* nested_name_specifier_p,
15929                       tree *attributes_p,
15930                       tree *bases)
15931 {
15932   tree nested_name_specifier;
15933   enum tag_types class_key;
15934   tree id = NULL_TREE;
15935   tree type = NULL_TREE;
15936   tree attributes;
15937   bool template_id_p = false;
15938   bool qualified_p = false;
15939   bool invalid_nested_name_p = false;
15940   bool invalid_explicit_specialization_p = false;
15941   tree pushed_scope = NULL_TREE;
15942   unsigned num_templates;
15943   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
15944   /* Assume no nested-name-specifier will be present.  */
15945   *nested_name_specifier_p = false;
15946   /* Assume no template parameter lists will be used in defining the
15947      type.  */
15948   num_templates = 0;
15949
15950   *bases = NULL_TREE;
15951
15952   /* Look for the class-key.  */
15953   class_key = cp_parser_class_key (parser);
15954   if (class_key == none_type)
15955     return error_mark_node;
15956
15957   /* Parse the attributes.  */
15958   attributes = cp_parser_attributes_opt (parser);
15959
15960   /* If the next token is `::', that is invalid -- but sometimes
15961      people do try to write:
15962
15963        struct ::S {};
15964
15965      Handle this gracefully by accepting the extra qualifier, and then
15966      issuing an error about it later if this really is a
15967      class-head.  If it turns out just to be an elaborated type
15968      specifier, remain silent.  */
15969   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
15970     qualified_p = true;
15971
15972   push_deferring_access_checks (dk_no_check);
15973
15974   /* Determine the name of the class.  Begin by looking for an
15975      optional nested-name-specifier.  */
15976   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
15977   nested_name_specifier
15978     = cp_parser_nested_name_specifier_opt (parser,
15979                                            /*typename_keyword_p=*/false,
15980                                            /*check_dependency_p=*/false,
15981                                            /*type_p=*/false,
15982                                            /*is_declaration=*/false);
15983   /* If there was a nested-name-specifier, then there *must* be an
15984      identifier.  */
15985   if (nested_name_specifier)
15986     {
15987       type_start_token = cp_lexer_peek_token (parser->lexer);
15988       /* Although the grammar says `identifier', it really means
15989          `class-name' or `template-name'.  You are only allowed to
15990          define a class that has already been declared with this
15991          syntax.
15992
15993          The proposed resolution for Core Issue 180 says that wherever
15994          you see `class T::X' you should treat `X' as a type-name.
15995
15996          It is OK to define an inaccessible class; for example:
15997
15998            class A { class B; };
15999            class A::B {};
16000
16001          We do not know if we will see a class-name, or a
16002          template-name.  We look for a class-name first, in case the
16003          class-name is a template-id; if we looked for the
16004          template-name first we would stop after the template-name.  */
16005       cp_parser_parse_tentatively (parser);
16006       type = cp_parser_class_name (parser,
16007                                    /*typename_keyword_p=*/false,
16008                                    /*template_keyword_p=*/false,
16009                                    class_type,
16010                                    /*check_dependency_p=*/false,
16011                                    /*class_head_p=*/true,
16012                                    /*is_declaration=*/false);
16013       /* If that didn't work, ignore the nested-name-specifier.  */
16014       if (!cp_parser_parse_definitely (parser))
16015         {
16016           invalid_nested_name_p = true;
16017           type_start_token = cp_lexer_peek_token (parser->lexer);
16018           id = cp_parser_identifier (parser);
16019           if (id == error_mark_node)
16020             id = NULL_TREE;
16021         }
16022       /* If we could not find a corresponding TYPE, treat this
16023          declaration like an unqualified declaration.  */
16024       if (type == error_mark_node)
16025         nested_name_specifier = NULL_TREE;
16026       /* Otherwise, count the number of templates used in TYPE and its
16027          containing scopes.  */
16028       else
16029         {
16030           tree scope;
16031
16032           for (scope = TREE_TYPE (type);
16033                scope && TREE_CODE (scope) != NAMESPACE_DECL;
16034                scope = (TYPE_P (scope)
16035                         ? TYPE_CONTEXT (scope)
16036                         : DECL_CONTEXT (scope)))
16037             if (TYPE_P (scope)
16038                 && CLASS_TYPE_P (scope)
16039                 && CLASSTYPE_TEMPLATE_INFO (scope)
16040                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
16041                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
16042               ++num_templates;
16043         }
16044     }
16045   /* Otherwise, the identifier is optional.  */
16046   else
16047     {
16048       /* We don't know whether what comes next is a template-id,
16049          an identifier, or nothing at all.  */
16050       cp_parser_parse_tentatively (parser);
16051       /* Check for a template-id.  */
16052       type_start_token = cp_lexer_peek_token (parser->lexer);
16053       id = cp_parser_template_id (parser,
16054                                   /*template_keyword_p=*/false,
16055                                   /*check_dependency_p=*/true,
16056                                   /*is_declaration=*/true);
16057       /* If that didn't work, it could still be an identifier.  */
16058       if (!cp_parser_parse_definitely (parser))
16059         {
16060           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
16061             {
16062               type_start_token = cp_lexer_peek_token (parser->lexer);
16063               id = cp_parser_identifier (parser);
16064             }
16065           else
16066             id = NULL_TREE;
16067         }
16068       else
16069         {
16070           template_id_p = true;
16071           ++num_templates;
16072         }
16073     }
16074
16075   pop_deferring_access_checks ();
16076
16077   if (id)
16078     cp_parser_check_for_invalid_template_id (parser, id,
16079                                              type_start_token->location);
16080
16081   /* If it's not a `:' or a `{' then we can't really be looking at a
16082      class-head, since a class-head only appears as part of a
16083      class-specifier.  We have to detect this situation before calling
16084      xref_tag, since that has irreversible side-effects.  */
16085   if (!cp_parser_next_token_starts_class_definition_p (parser))
16086     {
16087       cp_parser_error (parser, "expected %<{%> or %<:%>");
16088       return error_mark_node;
16089     }
16090
16091   /* At this point, we're going ahead with the class-specifier, even
16092      if some other problem occurs.  */
16093   cp_parser_commit_to_tentative_parse (parser);
16094   /* Issue the error about the overly-qualified name now.  */
16095   if (qualified_p)
16096     {
16097       cp_parser_error (parser,
16098                        "global qualification of class name is invalid");
16099       return error_mark_node;
16100     }
16101   else if (invalid_nested_name_p)
16102     {
16103       cp_parser_error (parser,
16104                        "qualified name does not name a class");
16105       return error_mark_node;
16106     }
16107   else if (nested_name_specifier)
16108     {
16109       tree scope;
16110
16111       /* Reject typedef-names in class heads.  */
16112       if (!DECL_IMPLICIT_TYPEDEF_P (type))
16113         {
16114           error_at (type_start_token->location,
16115                     "invalid class name in declaration of %qD",
16116                     type);
16117           type = NULL_TREE;
16118           goto done;
16119         }
16120
16121       /* Figure out in what scope the declaration is being placed.  */
16122       scope = current_scope ();
16123       /* If that scope does not contain the scope in which the
16124          class was originally declared, the program is invalid.  */
16125       if (scope && !is_ancestor (scope, nested_name_specifier))
16126         {
16127           if (at_namespace_scope_p ())
16128             error_at (type_start_token->location,
16129                       "declaration of %qD in namespace %qD which does not "
16130                       "enclose %qD",
16131                       type, scope, nested_name_specifier);
16132           else
16133             error_at (type_start_token->location,
16134                       "declaration of %qD in %qD which does not enclose %qD",
16135                       type, scope, nested_name_specifier);
16136           type = NULL_TREE;
16137           goto done;
16138         }
16139       /* [dcl.meaning]
16140
16141          A declarator-id shall not be qualified except for the
16142          definition of a ... nested class outside of its class
16143          ... [or] the definition or explicit instantiation of a
16144          class member of a namespace outside of its namespace.  */
16145       if (scope == nested_name_specifier)
16146         {
16147           permerror (nested_name_specifier_token_start->location,
16148                      "extra qualification not allowed");
16149           nested_name_specifier = NULL_TREE;
16150           num_templates = 0;
16151         }
16152     }
16153   /* An explicit-specialization must be preceded by "template <>".  If
16154      it is not, try to recover gracefully.  */
16155   if (at_namespace_scope_p ()
16156       && parser->num_template_parameter_lists == 0
16157       && template_id_p)
16158     {
16159       error_at (type_start_token->location,
16160                 "an explicit specialization must be preceded by %<template <>%>");
16161       invalid_explicit_specialization_p = true;
16162       /* Take the same action that would have been taken by
16163          cp_parser_explicit_specialization.  */
16164       ++parser->num_template_parameter_lists;
16165       begin_specialization ();
16166     }
16167   /* There must be no "return" statements between this point and the
16168      end of this function; set "type "to the correct return value and
16169      use "goto done;" to return.  */
16170   /* Make sure that the right number of template parameters were
16171      present.  */
16172   if (!cp_parser_check_template_parameters (parser, num_templates,
16173                                             type_start_token->location,
16174                                             /*declarator=*/NULL))
16175     {
16176       /* If something went wrong, there is no point in even trying to
16177          process the class-definition.  */
16178       type = NULL_TREE;
16179       goto done;
16180     }
16181
16182   /* Look up the type.  */
16183   if (template_id_p)
16184     {
16185       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
16186           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
16187               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
16188         {
16189           error_at (type_start_token->location,
16190                     "function template %qD redeclared as a class template", id);
16191           type = error_mark_node;
16192         }
16193       else
16194         {
16195           type = TREE_TYPE (id);
16196           type = maybe_process_partial_specialization (type);
16197         }
16198       if (nested_name_specifier)
16199         pushed_scope = push_scope (nested_name_specifier);
16200     }
16201   else if (nested_name_specifier)
16202     {
16203       tree class_type;
16204
16205       /* Given:
16206
16207             template <typename T> struct S { struct T };
16208             template <typename T> struct S<T>::T { };
16209
16210          we will get a TYPENAME_TYPE when processing the definition of
16211          `S::T'.  We need to resolve it to the actual type before we
16212          try to define it.  */
16213       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
16214         {
16215           class_type = resolve_typename_type (TREE_TYPE (type),
16216                                               /*only_current_p=*/false);
16217           if (TREE_CODE (class_type) != TYPENAME_TYPE)
16218             type = TYPE_NAME (class_type);
16219           else
16220             {
16221               cp_parser_error (parser, "could not resolve typename type");
16222               type = error_mark_node;
16223             }
16224         }
16225
16226       if (maybe_process_partial_specialization (TREE_TYPE (type))
16227           == error_mark_node)
16228         {
16229           type = NULL_TREE;
16230           goto done;
16231         }
16232
16233       class_type = current_class_type;
16234       /* Enter the scope indicated by the nested-name-specifier.  */
16235       pushed_scope = push_scope (nested_name_specifier);
16236       /* Get the canonical version of this type.  */
16237       type = TYPE_MAIN_DECL (TREE_TYPE (type));
16238       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
16239           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
16240         {
16241           type = push_template_decl (type);
16242           if (type == error_mark_node)
16243             {
16244               type = NULL_TREE;
16245               goto done;
16246             }
16247         }
16248
16249       type = TREE_TYPE (type);
16250       *nested_name_specifier_p = true;
16251     }
16252   else      /* The name is not a nested name.  */
16253     {
16254       /* If the class was unnamed, create a dummy name.  */
16255       if (!id)
16256         id = make_anon_name ();
16257       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
16258                        parser->num_template_parameter_lists);
16259     }
16260
16261   /* Indicate whether this class was declared as a `class' or as a
16262      `struct'.  */
16263   if (TREE_CODE (type) == RECORD_TYPE)
16264     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
16265   cp_parser_check_class_key (class_key, type);
16266
16267   /* If this type was already complete, and we see another definition,
16268      that's an error.  */
16269   if (type != error_mark_node && COMPLETE_TYPE_P (type))
16270     {
16271       error_at (type_start_token->location, "redefinition of %q#T",
16272                 type);
16273       error_at (type_start_token->location, "previous definition of %q+#T",
16274                 type);
16275       type = NULL_TREE;
16276       goto done;
16277     }
16278   else if (type == error_mark_node)
16279     type = NULL_TREE;
16280
16281   /* We will have entered the scope containing the class; the names of
16282      base classes should be looked up in that context.  For example:
16283
16284        struct A { struct B {}; struct C; };
16285        struct A::C : B {};
16286
16287      is valid.  */
16288
16289   /* Get the list of base-classes, if there is one.  */
16290   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
16291     *bases = cp_parser_base_clause (parser);
16292
16293  done:
16294   /* Leave the scope given by the nested-name-specifier.  We will
16295      enter the class scope itself while processing the members.  */
16296   if (pushed_scope)
16297     pop_scope (pushed_scope);
16298
16299   if (invalid_explicit_specialization_p)
16300     {
16301       end_specialization ();
16302       --parser->num_template_parameter_lists;
16303     }
16304   *attributes_p = attributes;
16305   return type;
16306 }
16307
16308 /* Parse a class-key.
16309
16310    class-key:
16311      class
16312      struct
16313      union
16314
16315    Returns the kind of class-key specified, or none_type to indicate
16316    error.  */
16317
16318 static enum tag_types
16319 cp_parser_class_key (cp_parser* parser)
16320 {
16321   cp_token *token;
16322   enum tag_types tag_type;
16323
16324   /* Look for the class-key.  */
16325   token = cp_parser_require (parser, CPP_KEYWORD, "class-key");
16326   if (!token)
16327     return none_type;
16328
16329   /* Check to see if the TOKEN is a class-key.  */
16330   tag_type = cp_parser_token_is_class_key (token);
16331   if (!tag_type)
16332     cp_parser_error (parser, "expected class-key");
16333   return tag_type;
16334 }
16335
16336 /* Parse an (optional) member-specification.
16337
16338    member-specification:
16339      member-declaration member-specification [opt]
16340      access-specifier : member-specification [opt]  */
16341
16342 static void
16343 cp_parser_member_specification_opt (cp_parser* parser)
16344 {
16345   while (true)
16346     {
16347       cp_token *token;
16348       enum rid keyword;
16349
16350       /* Peek at the next token.  */
16351       token = cp_lexer_peek_token (parser->lexer);
16352       /* If it's a `}', or EOF then we've seen all the members.  */
16353       if (token->type == CPP_CLOSE_BRACE
16354           || token->type == CPP_EOF
16355           || token->type == CPP_PRAGMA_EOL)
16356         break;
16357
16358       /* See if this token is a keyword.  */
16359       keyword = token->keyword;
16360       switch (keyword)
16361         {
16362         case RID_PUBLIC:
16363         case RID_PROTECTED:
16364         case RID_PRIVATE:
16365           /* Consume the access-specifier.  */
16366           cp_lexer_consume_token (parser->lexer);
16367           /* Remember which access-specifier is active.  */
16368           current_access_specifier = token->u.value;
16369           /* Look for the `:'.  */
16370           cp_parser_require (parser, CPP_COLON, "%<:%>");
16371           break;
16372
16373         default:
16374           /* Accept #pragmas at class scope.  */
16375           if (token->type == CPP_PRAGMA)
16376             {
16377               cp_parser_pragma (parser, pragma_external);
16378               break;
16379             }
16380
16381           /* Otherwise, the next construction must be a
16382              member-declaration.  */
16383           cp_parser_member_declaration (parser);
16384         }
16385     }
16386 }
16387
16388 /* Parse a member-declaration.
16389
16390    member-declaration:
16391      decl-specifier-seq [opt] member-declarator-list [opt] ;
16392      function-definition ; [opt]
16393      :: [opt] nested-name-specifier template [opt] unqualified-id ;
16394      using-declaration
16395      template-declaration
16396
16397    member-declarator-list:
16398      member-declarator
16399      member-declarator-list , member-declarator
16400
16401    member-declarator:
16402      declarator pure-specifier [opt]
16403      declarator constant-initializer [opt]
16404      identifier [opt] : constant-expression
16405
16406    GNU Extensions:
16407
16408    member-declaration:
16409      __extension__ member-declaration
16410
16411    member-declarator:
16412      declarator attributes [opt] pure-specifier [opt]
16413      declarator attributes [opt] constant-initializer [opt]
16414      identifier [opt] attributes [opt] : constant-expression  
16415
16416    C++0x Extensions:
16417
16418    member-declaration:
16419      static_assert-declaration  */
16420
16421 static void
16422 cp_parser_member_declaration (cp_parser* parser)
16423 {
16424   cp_decl_specifier_seq decl_specifiers;
16425   tree prefix_attributes;
16426   tree decl;
16427   int declares_class_or_enum;
16428   bool friend_p;
16429   cp_token *token = NULL;
16430   cp_token *decl_spec_token_start = NULL;
16431   cp_token *initializer_token_start = NULL;
16432   int saved_pedantic;
16433
16434   /* Check for the `__extension__' keyword.  */
16435   if (cp_parser_extension_opt (parser, &saved_pedantic))
16436     {
16437       /* Recurse.  */
16438       cp_parser_member_declaration (parser);
16439       /* Restore the old value of the PEDANTIC flag.  */
16440       pedantic = saved_pedantic;
16441
16442       return;
16443     }
16444
16445   /* Check for a template-declaration.  */
16446   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
16447     {
16448       /* An explicit specialization here is an error condition, and we
16449          expect the specialization handler to detect and report this.  */
16450       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
16451           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
16452         cp_parser_explicit_specialization (parser);
16453       else
16454         cp_parser_template_declaration (parser, /*member_p=*/true);
16455
16456       return;
16457     }
16458
16459   /* Check for a using-declaration.  */
16460   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
16461     {
16462       /* Parse the using-declaration.  */
16463       cp_parser_using_declaration (parser,
16464                                    /*access_declaration_p=*/false);
16465       return;
16466     }
16467
16468   /* Check for @defs.  */
16469   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
16470     {
16471       tree ivar, member;
16472       tree ivar_chains = cp_parser_objc_defs_expression (parser);
16473       ivar = ivar_chains;
16474       while (ivar)
16475         {
16476           member = ivar;
16477           ivar = TREE_CHAIN (member);
16478           TREE_CHAIN (member) = NULL_TREE;
16479           finish_member_declaration (member);
16480         }
16481       return;
16482     }
16483
16484   /* If the next token is `static_assert' we have a static assertion.  */
16485   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
16486     {
16487       cp_parser_static_assert (parser, /*member_p=*/true);
16488       return;
16489     }
16490
16491   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
16492     return;
16493
16494   /* Parse the decl-specifier-seq.  */
16495   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
16496   cp_parser_decl_specifier_seq (parser,
16497                                 CP_PARSER_FLAGS_OPTIONAL,
16498                                 &decl_specifiers,
16499                                 &declares_class_or_enum);
16500   prefix_attributes = decl_specifiers.attributes;
16501   decl_specifiers.attributes = NULL_TREE;
16502   /* Check for an invalid type-name.  */
16503   if (!decl_specifiers.type
16504       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
16505     return;
16506   /* If there is no declarator, then the decl-specifier-seq should
16507      specify a type.  */
16508   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
16509     {
16510       /* If there was no decl-specifier-seq, and the next token is a
16511          `;', then we have something like:
16512
16513            struct S { ; };
16514
16515          [class.mem]
16516
16517          Each member-declaration shall declare at least one member
16518          name of the class.  */
16519       if (!decl_specifiers.any_specifiers_p)
16520         {
16521           cp_token *token = cp_lexer_peek_token (parser->lexer);
16522           if (!in_system_header_at (token->location))
16523             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
16524         }
16525       else
16526         {
16527           tree type;
16528
16529           /* See if this declaration is a friend.  */
16530           friend_p = cp_parser_friend_p (&decl_specifiers);
16531           /* If there were decl-specifiers, check to see if there was
16532              a class-declaration.  */
16533           type = check_tag_decl (&decl_specifiers);
16534           /* Nested classes have already been added to the class, but
16535              a `friend' needs to be explicitly registered.  */
16536           if (friend_p)
16537             {
16538               /* If the `friend' keyword was present, the friend must
16539                  be introduced with a class-key.  */
16540                if (!declares_class_or_enum)
16541                  error_at (decl_spec_token_start->location,
16542                            "a class-key must be used when declaring a friend");
16543                /* In this case:
16544
16545                     template <typename T> struct A {
16546                       friend struct A<T>::B;
16547                     };
16548
16549                   A<T>::B will be represented by a TYPENAME_TYPE, and
16550                   therefore not recognized by check_tag_decl.  */
16551                if (!type
16552                    && decl_specifiers.type
16553                    && TYPE_P (decl_specifiers.type))
16554                  type = decl_specifiers.type;
16555                if (!type || !TYPE_P (type))
16556                  error_at (decl_spec_token_start->location,
16557                            "friend declaration does not name a class or "
16558                            "function");
16559                else
16560                  make_friend_class (current_class_type, type,
16561                                     /*complain=*/true);
16562             }
16563           /* If there is no TYPE, an error message will already have
16564              been issued.  */
16565           else if (!type || type == error_mark_node)
16566             ;
16567           /* An anonymous aggregate has to be handled specially; such
16568              a declaration really declares a data member (with a
16569              particular type), as opposed to a nested class.  */
16570           else if (ANON_AGGR_TYPE_P (type))
16571             {
16572               /* Remove constructors and such from TYPE, now that we
16573                  know it is an anonymous aggregate.  */
16574               fixup_anonymous_aggr (type);
16575               /* And make the corresponding data member.  */
16576               decl = build_decl (decl_spec_token_start->location,
16577                                  FIELD_DECL, NULL_TREE, type);
16578               /* Add it to the class.  */
16579               finish_member_declaration (decl);
16580             }
16581           else
16582             cp_parser_check_access_in_redeclaration
16583                                               (TYPE_NAME (type),
16584                                                decl_spec_token_start->location);
16585         }
16586     }
16587   else
16588     {
16589       /* See if these declarations will be friends.  */
16590       friend_p = cp_parser_friend_p (&decl_specifiers);
16591
16592       /* Keep going until we hit the `;' at the end of the
16593          declaration.  */
16594       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
16595         {
16596           tree attributes = NULL_TREE;
16597           tree first_attribute;
16598
16599           /* Peek at the next token.  */
16600           token = cp_lexer_peek_token (parser->lexer);
16601
16602           /* Check for a bitfield declaration.  */
16603           if (token->type == CPP_COLON
16604               || (token->type == CPP_NAME
16605                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
16606                   == CPP_COLON))
16607             {
16608               tree identifier;
16609               tree width;
16610
16611               /* Get the name of the bitfield.  Note that we cannot just
16612                  check TOKEN here because it may have been invalidated by
16613                  the call to cp_lexer_peek_nth_token above.  */
16614               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
16615                 identifier = cp_parser_identifier (parser);
16616               else
16617                 identifier = NULL_TREE;
16618
16619               /* Consume the `:' token.  */
16620               cp_lexer_consume_token (parser->lexer);
16621               /* Get the width of the bitfield.  */
16622               width
16623                 = cp_parser_constant_expression (parser,
16624                                                  /*allow_non_constant=*/false,
16625                                                  NULL);
16626
16627               /* Look for attributes that apply to the bitfield.  */
16628               attributes = cp_parser_attributes_opt (parser);
16629               /* Remember which attributes are prefix attributes and
16630                  which are not.  */
16631               first_attribute = attributes;
16632               /* Combine the attributes.  */
16633               attributes = chainon (prefix_attributes, attributes);
16634
16635               /* Create the bitfield declaration.  */
16636               decl = grokbitfield (identifier
16637                                    ? make_id_declarator (NULL_TREE,
16638                                                          identifier,
16639                                                          sfk_none)
16640                                    : NULL,
16641                                    &decl_specifiers,
16642                                    width,
16643                                    attributes);
16644             }
16645           else
16646             {
16647               cp_declarator *declarator;
16648               tree initializer;
16649               tree asm_specification;
16650               int ctor_dtor_or_conv_p;
16651
16652               /* Parse the declarator.  */
16653               declarator
16654                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
16655                                         &ctor_dtor_or_conv_p,
16656                                         /*parenthesized_p=*/NULL,
16657                                         /*member_p=*/true);
16658
16659               /* If something went wrong parsing the declarator, make sure
16660                  that we at least consume some tokens.  */
16661               if (declarator == cp_error_declarator)
16662                 {
16663                   /* Skip to the end of the statement.  */
16664                   cp_parser_skip_to_end_of_statement (parser);
16665                   /* If the next token is not a semicolon, that is
16666                      probably because we just skipped over the body of
16667                      a function.  So, we consume a semicolon if
16668                      present, but do not issue an error message if it
16669                      is not present.  */
16670                   if (cp_lexer_next_token_is (parser->lexer,
16671                                               CPP_SEMICOLON))
16672                     cp_lexer_consume_token (parser->lexer);
16673                   return;
16674                 }
16675
16676               if (declares_class_or_enum & 2)
16677                 cp_parser_check_for_definition_in_return_type
16678                                             (declarator, decl_specifiers.type,
16679                                              decl_specifiers.type_location);
16680
16681               /* Look for an asm-specification.  */
16682               asm_specification = cp_parser_asm_specification_opt (parser);
16683               /* Look for attributes that apply to the declaration.  */
16684               attributes = cp_parser_attributes_opt (parser);
16685               /* Remember which attributes are prefix attributes and
16686                  which are not.  */
16687               first_attribute = attributes;
16688               /* Combine the attributes.  */
16689               attributes = chainon (prefix_attributes, attributes);
16690
16691               /* If it's an `=', then we have a constant-initializer or a
16692                  pure-specifier.  It is not correct to parse the
16693                  initializer before registering the member declaration
16694                  since the member declaration should be in scope while
16695                  its initializer is processed.  However, the rest of the
16696                  front end does not yet provide an interface that allows
16697                  us to handle this correctly.  */
16698               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16699                 {
16700                   /* In [class.mem]:
16701
16702                      A pure-specifier shall be used only in the declaration of
16703                      a virtual function.
16704
16705                      A member-declarator can contain a constant-initializer
16706                      only if it declares a static member of integral or
16707                      enumeration type.
16708
16709                      Therefore, if the DECLARATOR is for a function, we look
16710                      for a pure-specifier; otherwise, we look for a
16711                      constant-initializer.  When we call `grokfield', it will
16712                      perform more stringent semantics checks.  */
16713                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
16714                   if (function_declarator_p (declarator))
16715                     initializer = cp_parser_pure_specifier (parser);
16716                   else
16717                     /* Parse the initializer.  */
16718                     initializer = cp_parser_constant_initializer (parser);
16719                 }
16720               /* Otherwise, there is no initializer.  */
16721               else
16722                 initializer = NULL_TREE;
16723
16724               /* See if we are probably looking at a function
16725                  definition.  We are certainly not looking at a
16726                  member-declarator.  Calling `grokfield' has
16727                  side-effects, so we must not do it unless we are sure
16728                  that we are looking at a member-declarator.  */
16729               if (cp_parser_token_starts_function_definition_p
16730                   (cp_lexer_peek_token (parser->lexer)))
16731                 {
16732                   /* The grammar does not allow a pure-specifier to be
16733                      used when a member function is defined.  (It is
16734                      possible that this fact is an oversight in the
16735                      standard, since a pure function may be defined
16736                      outside of the class-specifier.  */
16737                   if (initializer)
16738                     error_at (initializer_token_start->location,
16739                               "pure-specifier on function-definition");
16740                   decl = cp_parser_save_member_function_body (parser,
16741                                                               &decl_specifiers,
16742                                                               declarator,
16743                                                               attributes);
16744                   /* If the member was not a friend, declare it here.  */
16745                   if (!friend_p)
16746                     finish_member_declaration (decl);
16747                   /* Peek at the next token.  */
16748                   token = cp_lexer_peek_token (parser->lexer);
16749                   /* If the next token is a semicolon, consume it.  */
16750                   if (token->type == CPP_SEMICOLON)
16751                     cp_lexer_consume_token (parser->lexer);
16752                   return;
16753                 }
16754               else
16755                 if (declarator->kind == cdk_function)
16756                   declarator->id_loc = token->location;
16757                 /* Create the declaration.  */
16758                 decl = grokfield (declarator, &decl_specifiers,
16759                                   initializer, /*init_const_expr_p=*/true,
16760                                   asm_specification,
16761                                   attributes);
16762             }
16763
16764           /* Reset PREFIX_ATTRIBUTES.  */
16765           while (attributes && TREE_CHAIN (attributes) != first_attribute)
16766             attributes = TREE_CHAIN (attributes);
16767           if (attributes)
16768             TREE_CHAIN (attributes) = NULL_TREE;
16769
16770           /* If there is any qualification still in effect, clear it
16771              now; we will be starting fresh with the next declarator.  */
16772           parser->scope = NULL_TREE;
16773           parser->qualifying_scope = NULL_TREE;
16774           parser->object_scope = NULL_TREE;
16775           /* If it's a `,', then there are more declarators.  */
16776           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16777             cp_lexer_consume_token (parser->lexer);
16778           /* If the next token isn't a `;', then we have a parse error.  */
16779           else if (cp_lexer_next_token_is_not (parser->lexer,
16780                                                CPP_SEMICOLON))
16781             {
16782               cp_parser_error (parser, "expected %<;%>");
16783               /* Skip tokens until we find a `;'.  */
16784               cp_parser_skip_to_end_of_statement (parser);
16785
16786               break;
16787             }
16788
16789           if (decl)
16790             {
16791               /* Add DECL to the list of members.  */
16792               if (!friend_p)
16793                 finish_member_declaration (decl);
16794
16795               if (TREE_CODE (decl) == FUNCTION_DECL)
16796                 cp_parser_save_default_args (parser, decl);
16797             }
16798         }
16799     }
16800
16801   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
16802 }
16803
16804 /* Parse a pure-specifier.
16805
16806    pure-specifier:
16807      = 0
16808
16809    Returns INTEGER_ZERO_NODE if a pure specifier is found.
16810    Otherwise, ERROR_MARK_NODE is returned.  */
16811
16812 static tree
16813 cp_parser_pure_specifier (cp_parser* parser)
16814 {
16815   cp_token *token;
16816
16817   /* Look for the `=' token.  */
16818   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16819     return error_mark_node;
16820   /* Look for the `0' token.  */
16821   token = cp_lexer_peek_token (parser->lexer);
16822
16823   if (token->type == CPP_EOF
16824       || token->type == CPP_PRAGMA_EOL)
16825     return error_mark_node;
16826
16827   cp_lexer_consume_token (parser->lexer);
16828
16829   /* Accept = default or = delete in c++0x mode.  */
16830   if (token->keyword == RID_DEFAULT
16831       || token->keyword == RID_DELETE)
16832     {
16833       maybe_warn_cpp0x ("defaulted and deleted functions");
16834       return token->u.value;
16835     }
16836
16837   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
16838   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
16839     {
16840       cp_parser_error (parser,
16841                        "invalid pure specifier (only %<= 0%> is allowed)");
16842       cp_parser_skip_to_end_of_statement (parser);
16843       return error_mark_node;
16844     }
16845   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
16846     {
16847       error_at (token->location, "templates may not be %<virtual%>");
16848       return error_mark_node;
16849     }
16850
16851   return integer_zero_node;
16852 }
16853
16854 /* Parse a constant-initializer.
16855
16856    constant-initializer:
16857      = constant-expression
16858
16859    Returns a representation of the constant-expression.  */
16860
16861 static tree
16862 cp_parser_constant_initializer (cp_parser* parser)
16863 {
16864   /* Look for the `=' token.  */
16865   if (!cp_parser_require (parser, CPP_EQ, "%<=%>"))
16866     return error_mark_node;
16867
16868   /* It is invalid to write:
16869
16870        struct S { static const int i = { 7 }; };
16871
16872      */
16873   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16874     {
16875       cp_parser_error (parser,
16876                        "a brace-enclosed initializer is not allowed here");
16877       /* Consume the opening brace.  */
16878       cp_lexer_consume_token (parser->lexer);
16879       /* Skip the initializer.  */
16880       cp_parser_skip_to_closing_brace (parser);
16881       /* Look for the trailing `}'.  */
16882       cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
16883
16884       return error_mark_node;
16885     }
16886
16887   return cp_parser_constant_expression (parser,
16888                                         /*allow_non_constant=*/false,
16889                                         NULL);
16890 }
16891
16892 /* Derived classes [gram.class.derived] */
16893
16894 /* Parse a base-clause.
16895
16896    base-clause:
16897      : base-specifier-list
16898
16899    base-specifier-list:
16900      base-specifier ... [opt]
16901      base-specifier-list , base-specifier ... [opt]
16902
16903    Returns a TREE_LIST representing the base-classes, in the order in
16904    which they were declared.  The representation of each node is as
16905    described by cp_parser_base_specifier.
16906
16907    In the case that no bases are specified, this function will return
16908    NULL_TREE, not ERROR_MARK_NODE.  */
16909
16910 static tree
16911 cp_parser_base_clause (cp_parser* parser)
16912 {
16913   tree bases = NULL_TREE;
16914
16915   /* Look for the `:' that begins the list.  */
16916   cp_parser_require (parser, CPP_COLON, "%<:%>");
16917
16918   /* Scan the base-specifier-list.  */
16919   while (true)
16920     {
16921       cp_token *token;
16922       tree base;
16923       bool pack_expansion_p = false;
16924
16925       /* Look for the base-specifier.  */
16926       base = cp_parser_base_specifier (parser);
16927       /* Look for the (optional) ellipsis. */
16928       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16929         {
16930           /* Consume the `...'. */
16931           cp_lexer_consume_token (parser->lexer);
16932
16933           pack_expansion_p = true;
16934         }
16935
16936       /* Add BASE to the front of the list.  */
16937       if (base != error_mark_node)
16938         {
16939           if (pack_expansion_p)
16940             /* Make this a pack expansion type. */
16941             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
16942           
16943
16944           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
16945             {
16946               TREE_CHAIN (base) = bases;
16947               bases = base;
16948             }
16949         }
16950       /* Peek at the next token.  */
16951       token = cp_lexer_peek_token (parser->lexer);
16952       /* If it's not a comma, then the list is complete.  */
16953       if (token->type != CPP_COMMA)
16954         break;
16955       /* Consume the `,'.  */
16956       cp_lexer_consume_token (parser->lexer);
16957     }
16958
16959   /* PARSER->SCOPE may still be non-NULL at this point, if the last
16960      base class had a qualified name.  However, the next name that
16961      appears is certainly not qualified.  */
16962   parser->scope = NULL_TREE;
16963   parser->qualifying_scope = NULL_TREE;
16964   parser->object_scope = NULL_TREE;
16965
16966   return nreverse (bases);
16967 }
16968
16969 /* Parse a base-specifier.
16970
16971    base-specifier:
16972      :: [opt] nested-name-specifier [opt] class-name
16973      virtual access-specifier [opt] :: [opt] nested-name-specifier
16974        [opt] class-name
16975      access-specifier virtual [opt] :: [opt] nested-name-specifier
16976        [opt] class-name
16977
16978    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
16979    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
16980    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
16981    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
16982
16983 static tree
16984 cp_parser_base_specifier (cp_parser* parser)
16985 {
16986   cp_token *token;
16987   bool done = false;
16988   bool virtual_p = false;
16989   bool duplicate_virtual_error_issued_p = false;
16990   bool duplicate_access_error_issued_p = false;
16991   bool class_scope_p, template_p;
16992   tree access = access_default_node;
16993   tree type;
16994
16995   /* Process the optional `virtual' and `access-specifier'.  */
16996   while (!done)
16997     {
16998       /* Peek at the next token.  */
16999       token = cp_lexer_peek_token (parser->lexer);
17000       /* Process `virtual'.  */
17001       switch (token->keyword)
17002         {
17003         case RID_VIRTUAL:
17004           /* If `virtual' appears more than once, issue an error.  */
17005           if (virtual_p && !duplicate_virtual_error_issued_p)
17006             {
17007               cp_parser_error (parser,
17008                                "%<virtual%> specified more than once in base-specified");
17009               duplicate_virtual_error_issued_p = true;
17010             }
17011
17012           virtual_p = true;
17013
17014           /* Consume the `virtual' token.  */
17015           cp_lexer_consume_token (parser->lexer);
17016
17017           break;
17018
17019         case RID_PUBLIC:
17020         case RID_PROTECTED:
17021         case RID_PRIVATE:
17022           /* If more than one access specifier appears, issue an
17023              error.  */
17024           if (access != access_default_node
17025               && !duplicate_access_error_issued_p)
17026             {
17027               cp_parser_error (parser,
17028                                "more than one access specifier in base-specified");
17029               duplicate_access_error_issued_p = true;
17030             }
17031
17032           access = ridpointers[(int) token->keyword];
17033
17034           /* Consume the access-specifier.  */
17035           cp_lexer_consume_token (parser->lexer);
17036
17037           break;
17038
17039         default:
17040           done = true;
17041           break;
17042         }
17043     }
17044   /* It is not uncommon to see programs mechanically, erroneously, use
17045      the 'typename' keyword to denote (dependent) qualified types
17046      as base classes.  */
17047   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
17048     {
17049       token = cp_lexer_peek_token (parser->lexer);
17050       if (!processing_template_decl)
17051         error_at (token->location,
17052                   "keyword %<typename%> not allowed outside of templates");
17053       else
17054         error_at (token->location,
17055                   "keyword %<typename%> not allowed in this context "
17056                   "(the base class is implicitly a type)");
17057       cp_lexer_consume_token (parser->lexer);
17058     }
17059
17060   /* Look for the optional `::' operator.  */
17061   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
17062   /* Look for the nested-name-specifier.  The simplest way to
17063      implement:
17064
17065        [temp.res]
17066
17067        The keyword `typename' is not permitted in a base-specifier or
17068        mem-initializer; in these contexts a qualified name that
17069        depends on a template-parameter is implicitly assumed to be a
17070        type name.
17071
17072      is to pretend that we have seen the `typename' keyword at this
17073      point.  */
17074   cp_parser_nested_name_specifier_opt (parser,
17075                                        /*typename_keyword_p=*/true,
17076                                        /*check_dependency_p=*/true,
17077                                        typename_type,
17078                                        /*is_declaration=*/true);
17079   /* If the base class is given by a qualified name, assume that names
17080      we see are type names or templates, as appropriate.  */
17081   class_scope_p = (parser->scope && TYPE_P (parser->scope));
17082   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
17083
17084   /* Finally, look for the class-name.  */
17085   type = cp_parser_class_name (parser,
17086                                class_scope_p,
17087                                template_p,
17088                                typename_type,
17089                                /*check_dependency_p=*/true,
17090                                /*class_head_p=*/false,
17091                                /*is_declaration=*/true);
17092
17093   if (type == error_mark_node)
17094     return error_mark_node;
17095
17096   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
17097 }
17098
17099 /* Exception handling [gram.exception] */
17100
17101 /* Parse an (optional) exception-specification.
17102
17103    exception-specification:
17104      throw ( type-id-list [opt] )
17105
17106    Returns a TREE_LIST representing the exception-specification.  The
17107    TREE_VALUE of each node is a type.  */
17108
17109 static tree
17110 cp_parser_exception_specification_opt (cp_parser* parser)
17111 {
17112   cp_token *token;
17113   tree type_id_list;
17114
17115   /* Peek at the next token.  */
17116   token = cp_lexer_peek_token (parser->lexer);
17117   /* If it's not `throw', then there's no exception-specification.  */
17118   if (!cp_parser_is_keyword (token, RID_THROW))
17119     return NULL_TREE;
17120
17121   /* Consume the `throw'.  */
17122   cp_lexer_consume_token (parser->lexer);
17123
17124   /* Look for the `('.  */
17125   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17126
17127   /* Peek at the next token.  */
17128   token = cp_lexer_peek_token (parser->lexer);
17129   /* If it's not a `)', then there is a type-id-list.  */
17130   if (token->type != CPP_CLOSE_PAREN)
17131     {
17132       const char *saved_message;
17133
17134       /* Types may not be defined in an exception-specification.  */
17135       saved_message = parser->type_definition_forbidden_message;
17136       parser->type_definition_forbidden_message
17137         = "types may not be defined in an exception-specification";
17138       /* Parse the type-id-list.  */
17139       type_id_list = cp_parser_type_id_list (parser);
17140       /* Restore the saved message.  */
17141       parser->type_definition_forbidden_message = saved_message;
17142     }
17143   else
17144     type_id_list = empty_except_spec;
17145
17146   /* Look for the `)'.  */
17147   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17148
17149   return type_id_list;
17150 }
17151
17152 /* Parse an (optional) type-id-list.
17153
17154    type-id-list:
17155      type-id ... [opt]
17156      type-id-list , type-id ... [opt]
17157
17158    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
17159    in the order that the types were presented.  */
17160
17161 static tree
17162 cp_parser_type_id_list (cp_parser* parser)
17163 {
17164   tree types = NULL_TREE;
17165
17166   while (true)
17167     {
17168       cp_token *token;
17169       tree type;
17170
17171       /* Get the next type-id.  */
17172       type = cp_parser_type_id (parser);
17173       /* Parse the optional ellipsis. */
17174       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17175         {
17176           /* Consume the `...'. */
17177           cp_lexer_consume_token (parser->lexer);
17178
17179           /* Turn the type into a pack expansion expression. */
17180           type = make_pack_expansion (type);
17181         }
17182       /* Add it to the list.  */
17183       types = add_exception_specifier (types, type, /*complain=*/1);
17184       /* Peek at the next token.  */
17185       token = cp_lexer_peek_token (parser->lexer);
17186       /* If it is not a `,', we are done.  */
17187       if (token->type != CPP_COMMA)
17188         break;
17189       /* Consume the `,'.  */
17190       cp_lexer_consume_token (parser->lexer);
17191     }
17192
17193   return nreverse (types);
17194 }
17195
17196 /* Parse a try-block.
17197
17198    try-block:
17199      try compound-statement handler-seq  */
17200
17201 static tree
17202 cp_parser_try_block (cp_parser* parser)
17203 {
17204   tree try_block;
17205
17206   cp_parser_require_keyword (parser, RID_TRY, "%<try%>");
17207   try_block = begin_try_block ();
17208   cp_parser_compound_statement (parser, NULL, true);
17209   finish_try_block (try_block);
17210   cp_parser_handler_seq (parser);
17211   finish_handler_sequence (try_block);
17212
17213   return try_block;
17214 }
17215
17216 /* Parse a function-try-block.
17217
17218    function-try-block:
17219      try ctor-initializer [opt] function-body handler-seq  */
17220
17221 static bool
17222 cp_parser_function_try_block (cp_parser* parser)
17223 {
17224   tree compound_stmt;
17225   tree try_block;
17226   bool ctor_initializer_p;
17227
17228   /* Look for the `try' keyword.  */
17229   if (!cp_parser_require_keyword (parser, RID_TRY, "%<try%>"))
17230     return false;
17231   /* Let the rest of the front end know where we are.  */
17232   try_block = begin_function_try_block (&compound_stmt);
17233   /* Parse the function-body.  */
17234   ctor_initializer_p
17235     = cp_parser_ctor_initializer_opt_and_function_body (parser);
17236   /* We're done with the `try' part.  */
17237   finish_function_try_block (try_block);
17238   /* Parse the handlers.  */
17239   cp_parser_handler_seq (parser);
17240   /* We're done with the handlers.  */
17241   finish_function_handler_sequence (try_block, compound_stmt);
17242
17243   return ctor_initializer_p;
17244 }
17245
17246 /* Parse a handler-seq.
17247
17248    handler-seq:
17249      handler handler-seq [opt]  */
17250
17251 static void
17252 cp_parser_handler_seq (cp_parser* parser)
17253 {
17254   while (true)
17255     {
17256       cp_token *token;
17257
17258       /* Parse the handler.  */
17259       cp_parser_handler (parser);
17260       /* Peek at the next token.  */
17261       token = cp_lexer_peek_token (parser->lexer);
17262       /* If it's not `catch' then there are no more handlers.  */
17263       if (!cp_parser_is_keyword (token, RID_CATCH))
17264         break;
17265     }
17266 }
17267
17268 /* Parse a handler.
17269
17270    handler:
17271      catch ( exception-declaration ) compound-statement  */
17272
17273 static void
17274 cp_parser_handler (cp_parser* parser)
17275 {
17276   tree handler;
17277   tree declaration;
17278
17279   cp_parser_require_keyword (parser, RID_CATCH, "%<catch%>");
17280   handler = begin_handler ();
17281   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17282   declaration = cp_parser_exception_declaration (parser);
17283   finish_handler_parms (declaration, handler);
17284   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17285   cp_parser_compound_statement (parser, NULL, false);
17286   finish_handler (handler);
17287 }
17288
17289 /* Parse an exception-declaration.
17290
17291    exception-declaration:
17292      type-specifier-seq declarator
17293      type-specifier-seq abstract-declarator
17294      type-specifier-seq
17295      ...
17296
17297    Returns a VAR_DECL for the declaration, or NULL_TREE if the
17298    ellipsis variant is used.  */
17299
17300 static tree
17301 cp_parser_exception_declaration (cp_parser* parser)
17302 {
17303   cp_decl_specifier_seq type_specifiers;
17304   cp_declarator *declarator;
17305   const char *saved_message;
17306
17307   /* If it's an ellipsis, it's easy to handle.  */
17308   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17309     {
17310       /* Consume the `...' token.  */
17311       cp_lexer_consume_token (parser->lexer);
17312       return NULL_TREE;
17313     }
17314
17315   /* Types may not be defined in exception-declarations.  */
17316   saved_message = parser->type_definition_forbidden_message;
17317   parser->type_definition_forbidden_message
17318     = "types may not be defined in exception-declarations";
17319
17320   /* Parse the type-specifier-seq.  */
17321   cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
17322                                 /*is_trailing_return=*/false,
17323                                 &type_specifiers);
17324   /* If it's a `)', then there is no declarator.  */
17325   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
17326     declarator = NULL;
17327   else
17328     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
17329                                        /*ctor_dtor_or_conv_p=*/NULL,
17330                                        /*parenthesized_p=*/NULL,
17331                                        /*member_p=*/false);
17332
17333   /* Restore the saved message.  */
17334   parser->type_definition_forbidden_message = saved_message;
17335
17336   if (!type_specifiers.any_specifiers_p)
17337     return error_mark_node;
17338
17339   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
17340 }
17341
17342 /* Parse a throw-expression.
17343
17344    throw-expression:
17345      throw assignment-expression [opt]
17346
17347    Returns a THROW_EXPR representing the throw-expression.  */
17348
17349 static tree
17350 cp_parser_throw_expression (cp_parser* parser)
17351 {
17352   tree expression;
17353   cp_token* token;
17354
17355   cp_parser_require_keyword (parser, RID_THROW, "%<throw%>");
17356   token = cp_lexer_peek_token (parser->lexer);
17357   /* Figure out whether or not there is an assignment-expression
17358      following the "throw" keyword.  */
17359   if (token->type == CPP_COMMA
17360       || token->type == CPP_SEMICOLON
17361       || token->type == CPP_CLOSE_PAREN
17362       || token->type == CPP_CLOSE_SQUARE
17363       || token->type == CPP_CLOSE_BRACE
17364       || token->type == CPP_COLON)
17365     expression = NULL_TREE;
17366   else
17367     expression = cp_parser_assignment_expression (parser,
17368                                                   /*cast_p=*/false, NULL);
17369
17370   return build_throw (expression);
17371 }
17372
17373 /* GNU Extensions */
17374
17375 /* Parse an (optional) asm-specification.
17376
17377    asm-specification:
17378      asm ( string-literal )
17379
17380    If the asm-specification is present, returns a STRING_CST
17381    corresponding to the string-literal.  Otherwise, returns
17382    NULL_TREE.  */
17383
17384 static tree
17385 cp_parser_asm_specification_opt (cp_parser* parser)
17386 {
17387   cp_token *token;
17388   tree asm_specification;
17389
17390   /* Peek at the next token.  */
17391   token = cp_lexer_peek_token (parser->lexer);
17392   /* If the next token isn't the `asm' keyword, then there's no
17393      asm-specification.  */
17394   if (!cp_parser_is_keyword (token, RID_ASM))
17395     return NULL_TREE;
17396
17397   /* Consume the `asm' token.  */
17398   cp_lexer_consume_token (parser->lexer);
17399   /* Look for the `('.  */
17400   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17401
17402   /* Look for the string-literal.  */
17403   asm_specification = cp_parser_string_literal (parser, false, false);
17404
17405   /* Look for the `)'.  */
17406   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17407
17408   return asm_specification;
17409 }
17410
17411 /* Parse an asm-operand-list.
17412
17413    asm-operand-list:
17414      asm-operand
17415      asm-operand-list , asm-operand
17416
17417    asm-operand:
17418      string-literal ( expression )
17419      [ string-literal ] string-literal ( expression )
17420
17421    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
17422    each node is the expression.  The TREE_PURPOSE is itself a
17423    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
17424    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
17425    is a STRING_CST for the string literal before the parenthesis. Returns
17426    ERROR_MARK_NODE if any of the operands are invalid.  */
17427
17428 static tree
17429 cp_parser_asm_operand_list (cp_parser* parser)
17430 {
17431   tree asm_operands = NULL_TREE;
17432   bool invalid_operands = false;
17433
17434   while (true)
17435     {
17436       tree string_literal;
17437       tree expression;
17438       tree name;
17439
17440       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17441         {
17442           /* Consume the `[' token.  */
17443           cp_lexer_consume_token (parser->lexer);
17444           /* Read the operand name.  */
17445           name = cp_parser_identifier (parser);
17446           if (name != error_mark_node)
17447             name = build_string (IDENTIFIER_LENGTH (name),
17448                                  IDENTIFIER_POINTER (name));
17449           /* Look for the closing `]'.  */
17450           cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
17451         }
17452       else
17453         name = NULL_TREE;
17454       /* Look for the string-literal.  */
17455       string_literal = cp_parser_string_literal (parser, false, false);
17456
17457       /* Look for the `('.  */
17458       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17459       /* Parse the expression.  */
17460       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
17461       /* Look for the `)'.  */
17462       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17463
17464       if (name == error_mark_node 
17465           || string_literal == error_mark_node 
17466           || expression == error_mark_node)
17467         invalid_operands = true;
17468
17469       /* Add this operand to the list.  */
17470       asm_operands = tree_cons (build_tree_list (name, string_literal),
17471                                 expression,
17472                                 asm_operands);
17473       /* If the next token is not a `,', there are no more
17474          operands.  */
17475       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17476         break;
17477       /* Consume the `,'.  */
17478       cp_lexer_consume_token (parser->lexer);
17479     }
17480
17481   return invalid_operands ? error_mark_node : nreverse (asm_operands);
17482 }
17483
17484 /* Parse an asm-clobber-list.
17485
17486    asm-clobber-list:
17487      string-literal
17488      asm-clobber-list , string-literal
17489
17490    Returns a TREE_LIST, indicating the clobbers in the order that they
17491    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
17492
17493 static tree
17494 cp_parser_asm_clobber_list (cp_parser* parser)
17495 {
17496   tree clobbers = NULL_TREE;
17497
17498   while (true)
17499     {
17500       tree string_literal;
17501
17502       /* Look for the string literal.  */
17503       string_literal = cp_parser_string_literal (parser, false, false);
17504       /* Add it to the list.  */
17505       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
17506       /* If the next token is not a `,', then the list is
17507          complete.  */
17508       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17509         break;
17510       /* Consume the `,' token.  */
17511       cp_lexer_consume_token (parser->lexer);
17512     }
17513
17514   return clobbers;
17515 }
17516
17517 /* Parse an asm-label-list.
17518
17519    asm-label-list:
17520      identifier
17521      asm-label-list , identifier
17522
17523    Returns a TREE_LIST, indicating the labels in the order that they
17524    appeared.  The TREE_VALUE of each node is a label.  */
17525
17526 static tree
17527 cp_parser_asm_label_list (cp_parser* parser)
17528 {
17529   tree labels = NULL_TREE;
17530
17531   while (true)
17532     {
17533       tree identifier, label, name;
17534
17535       /* Look for the identifier.  */
17536       identifier = cp_parser_identifier (parser);
17537       if (!error_operand_p (identifier))
17538         {
17539           label = lookup_label (identifier);
17540           if (TREE_CODE (label) == LABEL_DECL)
17541             {
17542               TREE_USED (label) = 1;
17543               check_goto (label);
17544               name = build_string (IDENTIFIER_LENGTH (identifier),
17545                                    IDENTIFIER_POINTER (identifier));
17546               labels = tree_cons (name, label, labels);
17547             }
17548         }
17549       /* If the next token is not a `,', then the list is
17550          complete.  */
17551       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17552         break;
17553       /* Consume the `,' token.  */
17554       cp_lexer_consume_token (parser->lexer);
17555     }
17556
17557   return nreverse (labels);
17558 }
17559
17560 /* Parse an (optional) series of attributes.
17561
17562    attributes:
17563      attributes attribute
17564
17565    attribute:
17566      __attribute__ (( attribute-list [opt] ))
17567
17568    The return value is as for cp_parser_attribute_list.  */
17569
17570 static tree
17571 cp_parser_attributes_opt (cp_parser* parser)
17572 {
17573   tree attributes = NULL_TREE;
17574
17575   while (true)
17576     {
17577       cp_token *token;
17578       tree attribute_list;
17579
17580       /* Peek at the next token.  */
17581       token = cp_lexer_peek_token (parser->lexer);
17582       /* If it's not `__attribute__', then we're done.  */
17583       if (token->keyword != RID_ATTRIBUTE)
17584         break;
17585
17586       /* Consume the `__attribute__' keyword.  */
17587       cp_lexer_consume_token (parser->lexer);
17588       /* Look for the two `(' tokens.  */
17589       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17590       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
17591
17592       /* Peek at the next token.  */
17593       token = cp_lexer_peek_token (parser->lexer);
17594       if (token->type != CPP_CLOSE_PAREN)
17595         /* Parse the attribute-list.  */
17596         attribute_list = cp_parser_attribute_list (parser);
17597       else
17598         /* If the next token is a `)', then there is no attribute
17599            list.  */
17600         attribute_list = NULL;
17601
17602       /* Look for the two `)' tokens.  */
17603       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17604       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
17605
17606       /* Add these new attributes to the list.  */
17607       attributes = chainon (attributes, attribute_list);
17608     }
17609
17610   return attributes;
17611 }
17612
17613 /* Parse an attribute-list.
17614
17615    attribute-list:
17616      attribute
17617      attribute-list , attribute
17618
17619    attribute:
17620      identifier
17621      identifier ( identifier )
17622      identifier ( identifier , expression-list )
17623      identifier ( expression-list )
17624
17625    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
17626    to an attribute.  The TREE_PURPOSE of each node is the identifier
17627    indicating which attribute is in use.  The TREE_VALUE represents
17628    the arguments, if any.  */
17629
17630 static tree
17631 cp_parser_attribute_list (cp_parser* parser)
17632 {
17633   tree attribute_list = NULL_TREE;
17634   bool save_translate_strings_p = parser->translate_strings_p;
17635
17636   parser->translate_strings_p = false;
17637   while (true)
17638     {
17639       cp_token *token;
17640       tree identifier;
17641       tree attribute;
17642
17643       /* Look for the identifier.  We also allow keywords here; for
17644          example `__attribute__ ((const))' is legal.  */
17645       token = cp_lexer_peek_token (parser->lexer);
17646       if (token->type == CPP_NAME
17647           || token->type == CPP_KEYWORD)
17648         {
17649           tree arguments = NULL_TREE;
17650
17651           /* Consume the token.  */
17652           token = cp_lexer_consume_token (parser->lexer);
17653
17654           /* Save away the identifier that indicates which attribute
17655              this is.  */
17656           identifier = (token->type == CPP_KEYWORD) 
17657             /* For keywords, use the canonical spelling, not the
17658                parsed identifier.  */
17659             ? ridpointers[(int) token->keyword]
17660             : token->u.value;
17661           
17662           attribute = build_tree_list (identifier, NULL_TREE);
17663
17664           /* Peek at the next token.  */
17665           token = cp_lexer_peek_token (parser->lexer);
17666           /* If it's an `(', then parse the attribute arguments.  */
17667           if (token->type == CPP_OPEN_PAREN)
17668             {
17669               VEC(tree,gc) *vec;
17670               vec = cp_parser_parenthesized_expression_list
17671                     (parser, true, /*cast_p=*/false,
17672                      /*allow_expansion_p=*/false,
17673                      /*non_constant_p=*/NULL);
17674               if (vec == NULL)
17675                 arguments = error_mark_node;
17676               else
17677                 {
17678                   arguments = build_tree_list_vec (vec);
17679                   release_tree_vector (vec);
17680                 }
17681               /* Save the arguments away.  */
17682               TREE_VALUE (attribute) = arguments;
17683             }
17684
17685           if (arguments != error_mark_node)
17686             {
17687               /* Add this attribute to the list.  */
17688               TREE_CHAIN (attribute) = attribute_list;
17689               attribute_list = attribute;
17690             }
17691
17692           token = cp_lexer_peek_token (parser->lexer);
17693         }
17694       /* Now, look for more attributes.  If the next token isn't a
17695          `,', we're done.  */
17696       if (token->type != CPP_COMMA)
17697         break;
17698
17699       /* Consume the comma and keep going.  */
17700       cp_lexer_consume_token (parser->lexer);
17701     }
17702   parser->translate_strings_p = save_translate_strings_p;
17703
17704   /* We built up the list in reverse order.  */
17705   return nreverse (attribute_list);
17706 }
17707
17708 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
17709    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
17710    current value of the PEDANTIC flag, regardless of whether or not
17711    the `__extension__' keyword is present.  The caller is responsible
17712    for restoring the value of the PEDANTIC flag.  */
17713
17714 static bool
17715 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
17716 {
17717   /* Save the old value of the PEDANTIC flag.  */
17718   *saved_pedantic = pedantic;
17719
17720   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
17721     {
17722       /* Consume the `__extension__' token.  */
17723       cp_lexer_consume_token (parser->lexer);
17724       /* We're not being pedantic while the `__extension__' keyword is
17725          in effect.  */
17726       pedantic = 0;
17727
17728       return true;
17729     }
17730
17731   return false;
17732 }
17733
17734 /* Parse a label declaration.
17735
17736    label-declaration:
17737      __label__ label-declarator-seq ;
17738
17739    label-declarator-seq:
17740      identifier , label-declarator-seq
17741      identifier  */
17742
17743 static void
17744 cp_parser_label_declaration (cp_parser* parser)
17745 {
17746   /* Look for the `__label__' keyword.  */
17747   cp_parser_require_keyword (parser, RID_LABEL, "%<__label__%>");
17748
17749   while (true)
17750     {
17751       tree identifier;
17752
17753       /* Look for an identifier.  */
17754       identifier = cp_parser_identifier (parser);
17755       /* If we failed, stop.  */
17756       if (identifier == error_mark_node)
17757         break;
17758       /* Declare it as a label.  */
17759       finish_label_decl (identifier);
17760       /* If the next token is a `;', stop.  */
17761       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17762         break;
17763       /* Look for the `,' separating the label declarations.  */
17764       cp_parser_require (parser, CPP_COMMA, "%<,%>");
17765     }
17766
17767   /* Look for the final `;'.  */
17768   cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
17769 }
17770
17771 /* Support Functions */
17772
17773 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
17774    NAME should have one of the representations used for an
17775    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
17776    is returned.  If PARSER->SCOPE is a dependent type, then a
17777    SCOPE_REF is returned.
17778
17779    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
17780    returned; the name was already resolved when the TEMPLATE_ID_EXPR
17781    was formed.  Abstractly, such entities should not be passed to this
17782    function, because they do not need to be looked up, but it is
17783    simpler to check for this special case here, rather than at the
17784    call-sites.
17785
17786    In cases not explicitly covered above, this function returns a
17787    DECL, OVERLOAD, or baselink representing the result of the lookup.
17788    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
17789    is returned.
17790
17791    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
17792    (e.g., "struct") that was used.  In that case bindings that do not
17793    refer to types are ignored.
17794
17795    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
17796    ignored.
17797
17798    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
17799    are ignored.
17800
17801    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
17802    types.
17803
17804    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
17805    TREE_LIST of candidates if name-lookup results in an ambiguity, and
17806    NULL_TREE otherwise.  */
17807
17808 static tree
17809 cp_parser_lookup_name (cp_parser *parser, tree name,
17810                        enum tag_types tag_type,
17811                        bool is_template,
17812                        bool is_namespace,
17813                        bool check_dependency,
17814                        tree *ambiguous_decls,
17815                        location_t name_location)
17816 {
17817   int flags = 0;
17818   tree decl;
17819   tree object_type = parser->context->object_type;
17820
17821   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17822     flags |= LOOKUP_COMPLAIN;
17823
17824   /* Assume that the lookup will be unambiguous.  */
17825   if (ambiguous_decls)
17826     *ambiguous_decls = NULL_TREE;
17827
17828   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
17829      no longer valid.  Note that if we are parsing tentatively, and
17830      the parse fails, OBJECT_TYPE will be automatically restored.  */
17831   parser->context->object_type = NULL_TREE;
17832
17833   if (name == error_mark_node)
17834     return error_mark_node;
17835
17836   /* A template-id has already been resolved; there is no lookup to
17837      do.  */
17838   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
17839     return name;
17840   if (BASELINK_P (name))
17841     {
17842       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
17843                   == TEMPLATE_ID_EXPR);
17844       return name;
17845     }
17846
17847   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
17848      it should already have been checked to make sure that the name
17849      used matches the type being destroyed.  */
17850   if (TREE_CODE (name) == BIT_NOT_EXPR)
17851     {
17852       tree type;
17853
17854       /* Figure out to which type this destructor applies.  */
17855       if (parser->scope)
17856         type = parser->scope;
17857       else if (object_type)
17858         type = object_type;
17859       else
17860         type = current_class_type;
17861       /* If that's not a class type, there is no destructor.  */
17862       if (!type || !CLASS_TYPE_P (type))
17863         return error_mark_node;
17864       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
17865         lazily_declare_fn (sfk_destructor, type);
17866       if (!CLASSTYPE_DESTRUCTORS (type))
17867           return error_mark_node;
17868       /* If it was a class type, return the destructor.  */
17869       return CLASSTYPE_DESTRUCTORS (type);
17870     }
17871
17872   /* By this point, the NAME should be an ordinary identifier.  If
17873      the id-expression was a qualified name, the qualifying scope is
17874      stored in PARSER->SCOPE at this point.  */
17875   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
17876
17877   /* Perform the lookup.  */
17878   if (parser->scope)
17879     {
17880       bool dependent_p;
17881
17882       if (parser->scope == error_mark_node)
17883         return error_mark_node;
17884
17885       /* If the SCOPE is dependent, the lookup must be deferred until
17886          the template is instantiated -- unless we are explicitly
17887          looking up names in uninstantiated templates.  Even then, we
17888          cannot look up the name if the scope is not a class type; it
17889          might, for example, be a template type parameter.  */
17890       dependent_p = (TYPE_P (parser->scope)
17891                      && dependent_scope_p (parser->scope));
17892       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
17893           && dependent_p)
17894         /* Defer lookup.  */
17895         decl = error_mark_node;
17896       else
17897         {
17898           tree pushed_scope = NULL_TREE;
17899
17900           /* If PARSER->SCOPE is a dependent type, then it must be a
17901              class type, and we must not be checking dependencies;
17902              otherwise, we would have processed this lookup above.  So
17903              that PARSER->SCOPE is not considered a dependent base by
17904              lookup_member, we must enter the scope here.  */
17905           if (dependent_p)
17906             pushed_scope = push_scope (parser->scope);
17907           /* If the PARSER->SCOPE is a template specialization, it
17908              may be instantiated during name lookup.  In that case,
17909              errors may be issued.  Even if we rollback the current
17910              tentative parse, those errors are valid.  */
17911           decl = lookup_qualified_name (parser->scope, name,
17912                                         tag_type != none_type,
17913                                         /*complain=*/true);
17914
17915           /* If we have a single function from a using decl, pull it out.  */
17916           if (TREE_CODE (decl) == OVERLOAD
17917               && !really_overloaded_fn (decl))
17918             decl = OVL_FUNCTION (decl);
17919
17920           if (pushed_scope)
17921             pop_scope (pushed_scope);
17922         }
17923
17924       /* If the scope is a dependent type and either we deferred lookup or
17925          we did lookup but didn't find the name, rememeber the name.  */
17926       if (decl == error_mark_node && TYPE_P (parser->scope)
17927           && dependent_type_p (parser->scope))
17928         {
17929           if (tag_type)
17930             {
17931               tree type;
17932
17933               /* The resolution to Core Issue 180 says that `struct
17934                  A::B' should be considered a type-name, even if `A'
17935                  is dependent.  */
17936               type = make_typename_type (parser->scope, name, tag_type,
17937                                          /*complain=*/tf_error);
17938               decl = TYPE_NAME (type);
17939             }
17940           else if (is_template
17941                    && (cp_parser_next_token_ends_template_argument_p (parser)
17942                        || cp_lexer_next_token_is (parser->lexer,
17943                                                   CPP_CLOSE_PAREN)))
17944             decl = make_unbound_class_template (parser->scope,
17945                                                 name, NULL_TREE,
17946                                                 /*complain=*/tf_error);
17947           else
17948             decl = build_qualified_name (/*type=*/NULL_TREE,
17949                                          parser->scope, name,
17950                                          is_template);
17951         }
17952       parser->qualifying_scope = parser->scope;
17953       parser->object_scope = NULL_TREE;
17954     }
17955   else if (object_type)
17956     {
17957       tree object_decl = NULL_TREE;
17958       /* Look up the name in the scope of the OBJECT_TYPE, unless the
17959          OBJECT_TYPE is not a class.  */
17960       if (CLASS_TYPE_P (object_type))
17961         /* If the OBJECT_TYPE is a template specialization, it may
17962            be instantiated during name lookup.  In that case, errors
17963            may be issued.  Even if we rollback the current tentative
17964            parse, those errors are valid.  */
17965         object_decl = lookup_member (object_type,
17966                                      name,
17967                                      /*protect=*/0,
17968                                      tag_type != none_type);
17969       /* Look it up in the enclosing context, too.  */
17970       decl = lookup_name_real (name, tag_type != none_type,
17971                                /*nonclass=*/0,
17972                                /*block_p=*/true, is_namespace, flags);
17973       parser->object_scope = object_type;
17974       parser->qualifying_scope = NULL_TREE;
17975       if (object_decl)
17976         decl = object_decl;
17977     }
17978   else
17979     {
17980       decl = lookup_name_real (name, tag_type != none_type,
17981                                /*nonclass=*/0,
17982                                /*block_p=*/true, is_namespace, flags);
17983       parser->qualifying_scope = NULL_TREE;
17984       parser->object_scope = NULL_TREE;
17985     }
17986
17987   /* If the lookup failed, let our caller know.  */
17988   if (!decl || decl == error_mark_node)
17989     return error_mark_node;
17990
17991   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
17992   if (TREE_CODE (decl) == TREE_LIST)
17993     {
17994       if (ambiguous_decls)
17995         *ambiguous_decls = decl;
17996       /* The error message we have to print is too complicated for
17997          cp_parser_error, so we incorporate its actions directly.  */
17998       if (!cp_parser_simulate_error (parser))
17999         {
18000           error_at (name_location, "reference to %qD is ambiguous",
18001                     name);
18002           print_candidates (decl);
18003         }
18004       return error_mark_node;
18005     }
18006
18007   gcc_assert (DECL_P (decl)
18008               || TREE_CODE (decl) == OVERLOAD
18009               || TREE_CODE (decl) == SCOPE_REF
18010               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
18011               || BASELINK_P (decl));
18012
18013   /* If we have resolved the name of a member declaration, check to
18014      see if the declaration is accessible.  When the name resolves to
18015      set of overloaded functions, accessibility is checked when
18016      overload resolution is done.
18017
18018      During an explicit instantiation, access is not checked at all,
18019      as per [temp.explicit].  */
18020   if (DECL_P (decl))
18021     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
18022
18023   return decl;
18024 }
18025
18026 /* Like cp_parser_lookup_name, but for use in the typical case where
18027    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
18028    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
18029
18030 static tree
18031 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
18032 {
18033   return cp_parser_lookup_name (parser, name,
18034                                 none_type,
18035                                 /*is_template=*/false,
18036                                 /*is_namespace=*/false,
18037                                 /*check_dependency=*/true,
18038                                 /*ambiguous_decls=*/NULL,
18039                                 location);
18040 }
18041
18042 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
18043    the current context, return the TYPE_DECL.  If TAG_NAME_P is
18044    true, the DECL indicates the class being defined in a class-head,
18045    or declared in an elaborated-type-specifier.
18046
18047    Otherwise, return DECL.  */
18048
18049 static tree
18050 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
18051 {
18052   /* If the TEMPLATE_DECL is being declared as part of a class-head,
18053      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
18054
18055        struct A {
18056          template <typename T> struct B;
18057        };
18058
18059        template <typename T> struct A::B {};
18060
18061      Similarly, in an elaborated-type-specifier:
18062
18063        namespace N { struct X{}; }
18064
18065        struct A {
18066          template <typename T> friend struct N::X;
18067        };
18068
18069      However, if the DECL refers to a class type, and we are in
18070      the scope of the class, then the name lookup automatically
18071      finds the TYPE_DECL created by build_self_reference rather
18072      than a TEMPLATE_DECL.  For example, in:
18073
18074        template <class T> struct S {
18075          S s;
18076        };
18077
18078      there is no need to handle such case.  */
18079
18080   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
18081     return DECL_TEMPLATE_RESULT (decl);
18082
18083   return decl;
18084 }
18085
18086 /* If too many, or too few, template-parameter lists apply to the
18087    declarator, issue an error message.  Returns TRUE if all went well,
18088    and FALSE otherwise.  */
18089
18090 static bool
18091 cp_parser_check_declarator_template_parameters (cp_parser* parser,
18092                                                 cp_declarator *declarator,
18093                                                 location_t declarator_location)
18094 {
18095   unsigned num_templates;
18096
18097   /* We haven't seen any classes that involve template parameters yet.  */
18098   num_templates = 0;
18099
18100   switch (declarator->kind)
18101     {
18102     case cdk_id:
18103       if (declarator->u.id.qualifying_scope)
18104         {
18105           tree scope;
18106           tree member;
18107
18108           scope = declarator->u.id.qualifying_scope;
18109           member = declarator->u.id.unqualified_name;
18110
18111           while (scope && CLASS_TYPE_P (scope))
18112             {
18113               /* You're supposed to have one `template <...>'
18114                  for every template class, but you don't need one
18115                  for a full specialization.  For example:
18116
18117                  template <class T> struct S{};
18118                  template <> struct S<int> { void f(); };
18119                  void S<int>::f () {}
18120
18121                  is correct; there shouldn't be a `template <>' for
18122                  the definition of `S<int>::f'.  */
18123               if (!CLASSTYPE_TEMPLATE_INFO (scope))
18124                 /* If SCOPE does not have template information of any
18125                    kind, then it is not a template, nor is it nested
18126                    within a template.  */
18127                 break;
18128               if (explicit_class_specialization_p (scope))
18129                 break;
18130               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
18131                 ++num_templates;
18132
18133               scope = TYPE_CONTEXT (scope);
18134             }
18135         }
18136       else if (TREE_CODE (declarator->u.id.unqualified_name)
18137                == TEMPLATE_ID_EXPR)
18138         /* If the DECLARATOR has the form `X<y>' then it uses one
18139            additional level of template parameters.  */
18140         ++num_templates;
18141
18142       return cp_parser_check_template_parameters 
18143         (parser, num_templates, declarator_location, declarator);
18144
18145
18146     case cdk_function:
18147     case cdk_array:
18148     case cdk_pointer:
18149     case cdk_reference:
18150     case cdk_ptrmem:
18151       return (cp_parser_check_declarator_template_parameters
18152               (parser, declarator->declarator, declarator_location));
18153
18154     case cdk_error:
18155       return true;
18156
18157     default:
18158       gcc_unreachable ();
18159     }
18160   return false;
18161 }
18162
18163 /* NUM_TEMPLATES were used in the current declaration.  If that is
18164    invalid, return FALSE and issue an error messages.  Otherwise,
18165    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
18166    declarator and we can print more accurate diagnostics.  */
18167
18168 static bool
18169 cp_parser_check_template_parameters (cp_parser* parser,
18170                                      unsigned num_templates,
18171                                      location_t location,
18172                                      cp_declarator *declarator)
18173 {
18174   /* If there are the same number of template classes and parameter
18175      lists, that's OK.  */
18176   if (parser->num_template_parameter_lists == num_templates)
18177     return true;
18178   /* If there are more, but only one more, then we are referring to a
18179      member template.  That's OK too.  */
18180   if (parser->num_template_parameter_lists == num_templates + 1)
18181     return true;
18182   /* If there are more template classes than parameter lists, we have
18183      something like:
18184
18185        template <class T> void S<T>::R<T>::f ();  */
18186   if (parser->num_template_parameter_lists < num_templates)
18187     {
18188       if (declarator)
18189         error_at (location, "specializing member %<%T::%E%> "
18190                   "requires %<template<>%> syntax", 
18191                   declarator->u.id.qualifying_scope,
18192                   declarator->u.id.unqualified_name);
18193       else 
18194         error_at (location, "too few template-parameter-lists");
18195       return false;
18196     }
18197   /* Otherwise, there are too many template parameter lists.  We have
18198      something like:
18199
18200      template <class T> template <class U> void S::f();  */
18201   error_at (location, "too many template-parameter-lists");
18202   return false;
18203 }
18204
18205 /* Parse an optional `::' token indicating that the following name is
18206    from the global namespace.  If so, PARSER->SCOPE is set to the
18207    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
18208    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
18209    Returns the new value of PARSER->SCOPE, if the `::' token is
18210    present, and NULL_TREE otherwise.  */
18211
18212 static tree
18213 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
18214 {
18215   cp_token *token;
18216
18217   /* Peek at the next token.  */
18218   token = cp_lexer_peek_token (parser->lexer);
18219   /* If we're looking at a `::' token then we're starting from the
18220      global namespace, not our current location.  */
18221   if (token->type == CPP_SCOPE)
18222     {
18223       /* Consume the `::' token.  */
18224       cp_lexer_consume_token (parser->lexer);
18225       /* Set the SCOPE so that we know where to start the lookup.  */
18226       parser->scope = global_namespace;
18227       parser->qualifying_scope = global_namespace;
18228       parser->object_scope = NULL_TREE;
18229
18230       return parser->scope;
18231     }
18232   else if (!current_scope_valid_p)
18233     {
18234       parser->scope = NULL_TREE;
18235       parser->qualifying_scope = NULL_TREE;
18236       parser->object_scope = NULL_TREE;
18237     }
18238
18239   return NULL_TREE;
18240 }
18241
18242 /* Returns TRUE if the upcoming token sequence is the start of a
18243    constructor declarator.  If FRIEND_P is true, the declarator is
18244    preceded by the `friend' specifier.  */
18245
18246 static bool
18247 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
18248 {
18249   bool constructor_p;
18250   tree type_decl = NULL_TREE;
18251   bool nested_name_p;
18252   cp_token *next_token;
18253
18254   /* The common case is that this is not a constructor declarator, so
18255      try to avoid doing lots of work if at all possible.  It's not
18256      valid declare a constructor at function scope.  */
18257   if (parser->in_function_body)
18258     return false;
18259   /* And only certain tokens can begin a constructor declarator.  */
18260   next_token = cp_lexer_peek_token (parser->lexer);
18261   if (next_token->type != CPP_NAME
18262       && next_token->type != CPP_SCOPE
18263       && next_token->type != CPP_NESTED_NAME_SPECIFIER
18264       && next_token->type != CPP_TEMPLATE_ID)
18265     return false;
18266
18267   /* Parse tentatively; we are going to roll back all of the tokens
18268      consumed here.  */
18269   cp_parser_parse_tentatively (parser);
18270   /* Assume that we are looking at a constructor declarator.  */
18271   constructor_p = true;
18272
18273   /* Look for the optional `::' operator.  */
18274   cp_parser_global_scope_opt (parser,
18275                               /*current_scope_valid_p=*/false);
18276   /* Look for the nested-name-specifier.  */
18277   nested_name_p
18278     = (cp_parser_nested_name_specifier_opt (parser,
18279                                             /*typename_keyword_p=*/false,
18280                                             /*check_dependency_p=*/false,
18281                                             /*type_p=*/false,
18282                                             /*is_declaration=*/false)
18283        != NULL_TREE);
18284   /* Outside of a class-specifier, there must be a
18285      nested-name-specifier.  */
18286   if (!nested_name_p &&
18287       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
18288        || friend_p))
18289     constructor_p = false;
18290   /* If we still think that this might be a constructor-declarator,
18291      look for a class-name.  */
18292   if (constructor_p)
18293     {
18294       /* If we have:
18295
18296            template <typename T> struct S { S(); };
18297            template <typename T> S<T>::S ();
18298
18299          we must recognize that the nested `S' names a class.
18300          Similarly, for:
18301
18302            template <typename T> S<T>::S<T> ();
18303
18304          we must recognize that the nested `S' names a template.  */
18305       type_decl = cp_parser_class_name (parser,
18306                                         /*typename_keyword_p=*/false,
18307                                         /*template_keyword_p=*/false,
18308                                         none_type,
18309                                         /*check_dependency_p=*/false,
18310                                         /*class_head_p=*/false,
18311                                         /*is_declaration=*/false);
18312       /* If there was no class-name, then this is not a constructor.  */
18313       constructor_p = !cp_parser_error_occurred (parser);
18314     }
18315
18316   /* If we're still considering a constructor, we have to see a `(',
18317      to begin the parameter-declaration-clause, followed by either a
18318      `)', an `...', or a decl-specifier.  We need to check for a
18319      type-specifier to avoid being fooled into thinking that:
18320
18321        S::S (f) (int);
18322
18323      is a constructor.  (It is actually a function named `f' that
18324      takes one parameter (of type `int') and returns a value of type
18325      `S::S'.  */
18326   if (constructor_p
18327       && cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
18328     {
18329       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
18330           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
18331           /* A parameter declaration begins with a decl-specifier,
18332              which is either the "attribute" keyword, a storage class
18333              specifier, or (usually) a type-specifier.  */
18334           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
18335         {
18336           tree type;
18337           tree pushed_scope = NULL_TREE;
18338           unsigned saved_num_template_parameter_lists;
18339
18340           /* Names appearing in the type-specifier should be looked up
18341              in the scope of the class.  */
18342           if (current_class_type)
18343             type = NULL_TREE;
18344           else
18345             {
18346               type = TREE_TYPE (type_decl);
18347               if (TREE_CODE (type) == TYPENAME_TYPE)
18348                 {
18349                   type = resolve_typename_type (type,
18350                                                 /*only_current_p=*/false);
18351                   if (TREE_CODE (type) == TYPENAME_TYPE)
18352                     {
18353                       cp_parser_abort_tentative_parse (parser);
18354                       return false;
18355                     }
18356                 }
18357               pushed_scope = push_scope (type);
18358             }
18359
18360           /* Inside the constructor parameter list, surrounding
18361              template-parameter-lists do not apply.  */
18362           saved_num_template_parameter_lists
18363             = parser->num_template_parameter_lists;
18364           parser->num_template_parameter_lists = 0;
18365
18366           /* Look for the type-specifier.  */
18367           cp_parser_type_specifier (parser,
18368                                     CP_PARSER_FLAGS_NONE,
18369                                     /*decl_specs=*/NULL,
18370                                     /*is_declarator=*/true,
18371                                     /*declares_class_or_enum=*/NULL,
18372                                     /*is_cv_qualifier=*/NULL);
18373
18374           parser->num_template_parameter_lists
18375             = saved_num_template_parameter_lists;
18376
18377           /* Leave the scope of the class.  */
18378           if (pushed_scope)
18379             pop_scope (pushed_scope);
18380
18381           constructor_p = !cp_parser_error_occurred (parser);
18382         }
18383     }
18384   else
18385     constructor_p = false;
18386   /* We did not really want to consume any tokens.  */
18387   cp_parser_abort_tentative_parse (parser);
18388
18389   return constructor_p;
18390 }
18391
18392 /* Parse the definition of the function given by the DECL_SPECIFIERS,
18393    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
18394    they must be performed once we are in the scope of the function.
18395
18396    Returns the function defined.  */
18397
18398 static tree
18399 cp_parser_function_definition_from_specifiers_and_declarator
18400   (cp_parser* parser,
18401    cp_decl_specifier_seq *decl_specifiers,
18402    tree attributes,
18403    const cp_declarator *declarator)
18404 {
18405   tree fn;
18406   bool success_p;
18407
18408   /* Begin the function-definition.  */
18409   success_p = start_function (decl_specifiers, declarator, attributes);
18410
18411   /* The things we're about to see are not directly qualified by any
18412      template headers we've seen thus far.  */
18413   reset_specialization ();
18414
18415   /* If there were names looked up in the decl-specifier-seq that we
18416      did not check, check them now.  We must wait until we are in the
18417      scope of the function to perform the checks, since the function
18418      might be a friend.  */
18419   perform_deferred_access_checks ();
18420
18421   if (!success_p)
18422     {
18423       /* Skip the entire function.  */
18424       cp_parser_skip_to_end_of_block_or_statement (parser);
18425       fn = error_mark_node;
18426     }
18427   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
18428     {
18429       /* Seen already, skip it.  An error message has already been output.  */
18430       cp_parser_skip_to_end_of_block_or_statement (parser);
18431       fn = current_function_decl;
18432       current_function_decl = NULL_TREE;
18433       /* If this is a function from a class, pop the nested class.  */
18434       if (current_class_name)
18435         pop_nested_class ();
18436     }
18437   else
18438     fn = cp_parser_function_definition_after_declarator (parser,
18439                                                          /*inline_p=*/false);
18440
18441   return fn;
18442 }
18443
18444 /* Parse the part of a function-definition that follows the
18445    declarator.  INLINE_P is TRUE iff this function is an inline
18446    function defined within a class-specifier.
18447
18448    Returns the function defined.  */
18449
18450 static tree
18451 cp_parser_function_definition_after_declarator (cp_parser* parser,
18452                                                 bool inline_p)
18453 {
18454   tree fn;
18455   bool ctor_initializer_p = false;
18456   bool saved_in_unbraced_linkage_specification_p;
18457   bool saved_in_function_body;
18458   unsigned saved_num_template_parameter_lists;
18459   cp_token *token;
18460
18461   saved_in_function_body = parser->in_function_body;
18462   parser->in_function_body = true;
18463   /* If the next token is `return', then the code may be trying to
18464      make use of the "named return value" extension that G++ used to
18465      support.  */
18466   token = cp_lexer_peek_token (parser->lexer);
18467   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
18468     {
18469       /* Consume the `return' keyword.  */
18470       cp_lexer_consume_token (parser->lexer);
18471       /* Look for the identifier that indicates what value is to be
18472          returned.  */
18473       cp_parser_identifier (parser);
18474       /* Issue an error message.  */
18475       error_at (token->location,
18476                 "named return values are no longer supported");
18477       /* Skip tokens until we reach the start of the function body.  */
18478       while (true)
18479         {
18480           cp_token *token = cp_lexer_peek_token (parser->lexer);
18481           if (token->type == CPP_OPEN_BRACE
18482               || token->type == CPP_EOF
18483               || token->type == CPP_PRAGMA_EOL)
18484             break;
18485           cp_lexer_consume_token (parser->lexer);
18486         }
18487     }
18488   /* The `extern' in `extern "C" void f () { ... }' does not apply to
18489      anything declared inside `f'.  */
18490   saved_in_unbraced_linkage_specification_p
18491     = parser->in_unbraced_linkage_specification_p;
18492   parser->in_unbraced_linkage_specification_p = false;
18493   /* Inside the function, surrounding template-parameter-lists do not
18494      apply.  */
18495   saved_num_template_parameter_lists
18496     = parser->num_template_parameter_lists;
18497   parser->num_template_parameter_lists = 0;
18498
18499   start_lambda_scope (current_function_decl);
18500
18501   /* If the next token is `try', then we are looking at a
18502      function-try-block.  */
18503   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
18504     ctor_initializer_p = cp_parser_function_try_block (parser);
18505   /* A function-try-block includes the function-body, so we only do
18506      this next part if we're not processing a function-try-block.  */
18507   else
18508     ctor_initializer_p
18509       = cp_parser_ctor_initializer_opt_and_function_body (parser);
18510
18511   finish_lambda_scope ();
18512
18513   /* Finish the function.  */
18514   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
18515                         (inline_p ? 2 : 0));
18516   /* Generate code for it, if necessary.  */
18517   expand_or_defer_fn (fn);
18518   /* Restore the saved values.  */
18519   parser->in_unbraced_linkage_specification_p
18520     = saved_in_unbraced_linkage_specification_p;
18521   parser->num_template_parameter_lists
18522     = saved_num_template_parameter_lists;
18523   parser->in_function_body = saved_in_function_body;
18524
18525   return fn;
18526 }
18527
18528 /* Parse a template-declaration, assuming that the `export' (and
18529    `extern') keywords, if present, has already been scanned.  MEMBER_P
18530    is as for cp_parser_template_declaration.  */
18531
18532 static void
18533 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
18534 {
18535   tree decl = NULL_TREE;
18536   VEC (deferred_access_check,gc) *checks;
18537   tree parameter_list;
18538   bool friend_p = false;
18539   bool need_lang_pop;
18540   cp_token *token;
18541
18542   /* Look for the `template' keyword.  */
18543   token = cp_lexer_peek_token (parser->lexer);
18544   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, "%<template%>"))
18545     return;
18546
18547   /* And the `<'.  */
18548   if (!cp_parser_require (parser, CPP_LESS, "%<<%>"))
18549     return;
18550   if (at_class_scope_p () && current_function_decl)
18551     {
18552       /* 14.5.2.2 [temp.mem]
18553
18554          A local class shall not have member templates.  */
18555       error_at (token->location,
18556                 "invalid declaration of member template in local class");
18557       cp_parser_skip_to_end_of_block_or_statement (parser);
18558       return;
18559     }
18560   /* [temp]
18561
18562      A template ... shall not have C linkage.  */
18563   if (current_lang_name == lang_name_c)
18564     {
18565       error_at (token->location, "template with C linkage");
18566       /* Give it C++ linkage to avoid confusing other parts of the
18567          front end.  */
18568       push_lang_context (lang_name_cplusplus);
18569       need_lang_pop = true;
18570     }
18571   else
18572     need_lang_pop = false;
18573
18574   /* We cannot perform access checks on the template parameter
18575      declarations until we know what is being declared, just as we
18576      cannot check the decl-specifier list.  */
18577   push_deferring_access_checks (dk_deferred);
18578
18579   /* If the next token is `>', then we have an invalid
18580      specialization.  Rather than complain about an invalid template
18581      parameter, issue an error message here.  */
18582   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
18583     {
18584       cp_parser_error (parser, "invalid explicit specialization");
18585       begin_specialization ();
18586       parameter_list = NULL_TREE;
18587     }
18588   else
18589     /* Parse the template parameters.  */
18590     parameter_list = cp_parser_template_parameter_list (parser);
18591
18592   /* Get the deferred access checks from the parameter list.  These
18593      will be checked once we know what is being declared, as for a
18594      member template the checks must be performed in the scope of the
18595      class containing the member.  */
18596   checks = get_deferred_access_checks ();
18597
18598   /* Look for the `>'.  */
18599   cp_parser_skip_to_end_of_template_parameter_list (parser);
18600   /* We just processed one more parameter list.  */
18601   ++parser->num_template_parameter_lists;
18602   /* If the next token is `template', there are more template
18603      parameters.  */
18604   if (cp_lexer_next_token_is_keyword (parser->lexer,
18605                                       RID_TEMPLATE))
18606     cp_parser_template_declaration_after_export (parser, member_p);
18607   else
18608     {
18609       /* There are no access checks when parsing a template, as we do not
18610          know if a specialization will be a friend.  */
18611       push_deferring_access_checks (dk_no_check);
18612       token = cp_lexer_peek_token (parser->lexer);
18613       decl = cp_parser_single_declaration (parser,
18614                                            checks,
18615                                            member_p,
18616                                            /*explicit_specialization_p=*/false,
18617                                            &friend_p);
18618       pop_deferring_access_checks ();
18619
18620       /* If this is a member template declaration, let the front
18621          end know.  */
18622       if (member_p && !friend_p && decl)
18623         {
18624           if (TREE_CODE (decl) == TYPE_DECL)
18625             cp_parser_check_access_in_redeclaration (decl, token->location);
18626
18627           decl = finish_member_template_decl (decl);
18628         }
18629       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
18630         make_friend_class (current_class_type, TREE_TYPE (decl),
18631                            /*complain=*/true);
18632     }
18633   /* We are done with the current parameter list.  */
18634   --parser->num_template_parameter_lists;
18635
18636   pop_deferring_access_checks ();
18637
18638   /* Finish up.  */
18639   finish_template_decl (parameter_list);
18640
18641   /* Register member declarations.  */
18642   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
18643     finish_member_declaration (decl);
18644   /* For the erroneous case of a template with C linkage, we pushed an
18645      implicit C++ linkage scope; exit that scope now.  */
18646   if (need_lang_pop)
18647     pop_lang_context ();
18648   /* If DECL is a function template, we must return to parse it later.
18649      (Even though there is no definition, there might be default
18650      arguments that need handling.)  */
18651   if (member_p && decl
18652       && (TREE_CODE (decl) == FUNCTION_DECL
18653           || DECL_FUNCTION_TEMPLATE_P (decl)))
18654     TREE_VALUE (parser->unparsed_functions_queues)
18655       = tree_cons (NULL_TREE, decl,
18656                    TREE_VALUE (parser->unparsed_functions_queues));
18657 }
18658
18659 /* Perform the deferred access checks from a template-parameter-list.
18660    CHECKS is a TREE_LIST of access checks, as returned by
18661    get_deferred_access_checks.  */
18662
18663 static void
18664 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
18665 {
18666   ++processing_template_parmlist;
18667   perform_access_checks (checks);
18668   --processing_template_parmlist;
18669 }
18670
18671 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
18672    `function-definition' sequence.  MEMBER_P is true, this declaration
18673    appears in a class scope.
18674
18675    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
18676    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
18677
18678 static tree
18679 cp_parser_single_declaration (cp_parser* parser,
18680                               VEC (deferred_access_check,gc)* checks,
18681                               bool member_p,
18682                               bool explicit_specialization_p,
18683                               bool* friend_p)
18684 {
18685   int declares_class_or_enum;
18686   tree decl = NULL_TREE;
18687   cp_decl_specifier_seq decl_specifiers;
18688   bool function_definition_p = false;
18689   cp_token *decl_spec_token_start;
18690
18691   /* This function is only used when processing a template
18692      declaration.  */
18693   gcc_assert (innermost_scope_kind () == sk_template_parms
18694               || innermost_scope_kind () == sk_template_spec);
18695
18696   /* Defer access checks until we know what is being declared.  */
18697   push_deferring_access_checks (dk_deferred);
18698
18699   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
18700      alternative.  */
18701   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18702   cp_parser_decl_specifier_seq (parser,
18703                                 CP_PARSER_FLAGS_OPTIONAL,
18704                                 &decl_specifiers,
18705                                 &declares_class_or_enum);
18706   if (friend_p)
18707     *friend_p = cp_parser_friend_p (&decl_specifiers);
18708
18709   /* There are no template typedefs.  */
18710   if (decl_specifiers.specs[(int) ds_typedef])
18711     {
18712       error_at (decl_spec_token_start->location,
18713                 "template declaration of %<typedef%>");
18714       decl = error_mark_node;
18715     }
18716
18717   /* Gather up the access checks that occurred the
18718      decl-specifier-seq.  */
18719   stop_deferring_access_checks ();
18720
18721   /* Check for the declaration of a template class.  */
18722   if (declares_class_or_enum)
18723     {
18724       if (cp_parser_declares_only_class_p (parser))
18725         {
18726           decl = shadow_tag (&decl_specifiers);
18727
18728           /* In this case:
18729
18730                struct C {
18731                  friend template <typename T> struct A<T>::B;
18732                };
18733
18734              A<T>::B will be represented by a TYPENAME_TYPE, and
18735              therefore not recognized by shadow_tag.  */
18736           if (friend_p && *friend_p
18737               && !decl
18738               && decl_specifiers.type
18739               && TYPE_P (decl_specifiers.type))
18740             decl = decl_specifiers.type;
18741
18742           if (decl && decl != error_mark_node)
18743             decl = TYPE_NAME (decl);
18744           else
18745             decl = error_mark_node;
18746
18747           /* Perform access checks for template parameters.  */
18748           cp_parser_perform_template_parameter_access_checks (checks);
18749         }
18750     }
18751   /* If it's not a template class, try for a template function.  If
18752      the next token is a `;', then this declaration does not declare
18753      anything.  But, if there were errors in the decl-specifiers, then
18754      the error might well have come from an attempted class-specifier.
18755      In that case, there's no need to warn about a missing declarator.  */
18756   if (!decl
18757       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
18758           || decl_specifiers.type != error_mark_node))
18759     {
18760       decl = cp_parser_init_declarator (parser,
18761                                         &decl_specifiers,
18762                                         checks,
18763                                         /*function_definition_allowed_p=*/true,
18764                                         member_p,
18765                                         declares_class_or_enum,
18766                                         &function_definition_p);
18767
18768     /* 7.1.1-1 [dcl.stc]
18769
18770        A storage-class-specifier shall not be specified in an explicit
18771        specialization...  */
18772     if (decl
18773         && explicit_specialization_p
18774         && decl_specifiers.storage_class != sc_none)
18775       {
18776         error_at (decl_spec_token_start->location,
18777                   "explicit template specialization cannot have a storage class");
18778         decl = error_mark_node;
18779       }
18780     }
18781
18782   pop_deferring_access_checks ();
18783
18784   /* Clear any current qualification; whatever comes next is the start
18785      of something new.  */
18786   parser->scope = NULL_TREE;
18787   parser->qualifying_scope = NULL_TREE;
18788   parser->object_scope = NULL_TREE;
18789   /* Look for a trailing `;' after the declaration.  */
18790   if (!function_definition_p
18791       && (decl == error_mark_node
18792           || !cp_parser_require (parser, CPP_SEMICOLON, "%<;%>")))
18793     cp_parser_skip_to_end_of_block_or_statement (parser);
18794
18795   return decl;
18796 }
18797
18798 /* Parse a cast-expression that is not the operand of a unary "&".  */
18799
18800 static tree
18801 cp_parser_simple_cast_expression (cp_parser *parser)
18802 {
18803   return cp_parser_cast_expression (parser, /*address_p=*/false,
18804                                     /*cast_p=*/false, NULL);
18805 }
18806
18807 /* Parse a functional cast to TYPE.  Returns an expression
18808    representing the cast.  */
18809
18810 static tree
18811 cp_parser_functional_cast (cp_parser* parser, tree type)
18812 {
18813   VEC(tree,gc) *vec;
18814   tree expression_list;
18815   tree cast;
18816   bool nonconst_p;
18817
18818   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18819     {
18820       maybe_warn_cpp0x ("extended initializer lists");
18821       expression_list = cp_parser_braced_list (parser, &nonconst_p);
18822       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
18823       if (TREE_CODE (type) == TYPE_DECL)
18824         type = TREE_TYPE (type);
18825       return finish_compound_literal (type, expression_list);
18826     }
18827
18828
18829   vec = cp_parser_parenthesized_expression_list (parser, false,
18830                                                  /*cast_p=*/true,
18831                                                  /*allow_expansion_p=*/true,
18832                                                  /*non_constant_p=*/NULL);
18833   if (vec == NULL)
18834     expression_list = error_mark_node;
18835   else
18836     {
18837       expression_list = build_tree_list_vec (vec);
18838       release_tree_vector (vec);
18839     }
18840
18841   cast = build_functional_cast (type, expression_list,
18842                                 tf_warning_or_error);
18843   /* [expr.const]/1: In an integral constant expression "only type
18844      conversions to integral or enumeration type can be used".  */
18845   if (TREE_CODE (type) == TYPE_DECL)
18846     type = TREE_TYPE (type);
18847   if (cast != error_mark_node
18848       && !cast_valid_in_integral_constant_expression_p (type)
18849       && (cp_parser_non_integral_constant_expression
18850           (parser, "a call to a constructor")))
18851     return error_mark_node;
18852   return cast;
18853 }
18854
18855 /* Save the tokens that make up the body of a member function defined
18856    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
18857    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
18858    specifiers applied to the declaration.  Returns the FUNCTION_DECL
18859    for the member function.  */
18860
18861 static tree
18862 cp_parser_save_member_function_body (cp_parser* parser,
18863                                      cp_decl_specifier_seq *decl_specifiers,
18864                                      cp_declarator *declarator,
18865                                      tree attributes)
18866 {
18867   cp_token *first;
18868   cp_token *last;
18869   tree fn;
18870
18871   /* Create the FUNCTION_DECL.  */
18872   fn = grokmethod (decl_specifiers, declarator, attributes);
18873   /* If something went badly wrong, bail out now.  */
18874   if (fn == error_mark_node)
18875     {
18876       /* If there's a function-body, skip it.  */
18877       if (cp_parser_token_starts_function_definition_p
18878           (cp_lexer_peek_token (parser->lexer)))
18879         cp_parser_skip_to_end_of_block_or_statement (parser);
18880       return error_mark_node;
18881     }
18882
18883   /* Remember it, if there default args to post process.  */
18884   cp_parser_save_default_args (parser, fn);
18885
18886   /* Save away the tokens that make up the body of the
18887      function.  */
18888   first = parser->lexer->next_token;
18889   /* We can have braced-init-list mem-initializers before the fn body.  */
18890   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18891     {
18892       cp_lexer_consume_token (parser->lexer);
18893       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
18894              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
18895         {
18896           /* cache_group will stop after an un-nested { } pair, too.  */
18897           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
18898             break;
18899
18900           /* variadic mem-inits have ... after the ')'.  */
18901           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18902             cp_lexer_consume_token (parser->lexer);
18903         }
18904     }
18905   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18906   /* Handle function try blocks.  */
18907   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
18908     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
18909   last = parser->lexer->next_token;
18910
18911   /* Save away the inline definition; we will process it when the
18912      class is complete.  */
18913   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
18914   DECL_PENDING_INLINE_P (fn) = 1;
18915
18916   /* We need to know that this was defined in the class, so that
18917      friend templates are handled correctly.  */
18918   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
18919
18920   /* Add FN to the queue of functions to be parsed later.  */
18921   TREE_VALUE (parser->unparsed_functions_queues)
18922     = tree_cons (NULL_TREE, fn,
18923                  TREE_VALUE (parser->unparsed_functions_queues));
18924
18925   return fn;
18926 }
18927
18928 /* Parse a template-argument-list, as well as the trailing ">" (but
18929    not the opening ">").  See cp_parser_template_argument_list for the
18930    return value.  */
18931
18932 static tree
18933 cp_parser_enclosed_template_argument_list (cp_parser* parser)
18934 {
18935   tree arguments;
18936   tree saved_scope;
18937   tree saved_qualifying_scope;
18938   tree saved_object_scope;
18939   bool saved_greater_than_is_operator_p;
18940   int saved_unevaluated_operand;
18941   int saved_inhibit_evaluation_warnings;
18942
18943   /* [temp.names]
18944
18945      When parsing a template-id, the first non-nested `>' is taken as
18946      the end of the template-argument-list rather than a greater-than
18947      operator.  */
18948   saved_greater_than_is_operator_p
18949     = parser->greater_than_is_operator_p;
18950   parser->greater_than_is_operator_p = false;
18951   /* Parsing the argument list may modify SCOPE, so we save it
18952      here.  */
18953   saved_scope = parser->scope;
18954   saved_qualifying_scope = parser->qualifying_scope;
18955   saved_object_scope = parser->object_scope;
18956   /* We need to evaluate the template arguments, even though this
18957      template-id may be nested within a "sizeof".  */
18958   saved_unevaluated_operand = cp_unevaluated_operand;
18959   cp_unevaluated_operand = 0;
18960   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
18961   c_inhibit_evaluation_warnings = 0;
18962   /* Parse the template-argument-list itself.  */
18963   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
18964       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18965     arguments = NULL_TREE;
18966   else
18967     arguments = cp_parser_template_argument_list (parser);
18968   /* Look for the `>' that ends the template-argument-list. If we find
18969      a '>>' instead, it's probably just a typo.  */
18970   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
18971     {
18972       if (cxx_dialect != cxx98)
18973         {
18974           /* In C++0x, a `>>' in a template argument list or cast
18975              expression is considered to be two separate `>'
18976              tokens. So, change the current token to a `>', but don't
18977              consume it: it will be consumed later when the outer
18978              template argument list (or cast expression) is parsed.
18979              Note that this replacement of `>' for `>>' is necessary
18980              even if we are parsing tentatively: in the tentative
18981              case, after calling
18982              cp_parser_enclosed_template_argument_list we will always
18983              throw away all of the template arguments and the first
18984              closing `>', either because the template argument list
18985              was erroneous or because we are replacing those tokens
18986              with a CPP_TEMPLATE_ID token.  The second `>' (which will
18987              not have been thrown away) is needed either to close an
18988              outer template argument list or to complete a new-style
18989              cast.  */
18990           cp_token *token = cp_lexer_peek_token (parser->lexer);
18991           token->type = CPP_GREATER;
18992         }
18993       else if (!saved_greater_than_is_operator_p)
18994         {
18995           /* If we're in a nested template argument list, the '>>' has
18996             to be a typo for '> >'. We emit the error message, but we
18997             continue parsing and we push a '>' as next token, so that
18998             the argument list will be parsed correctly.  Note that the
18999             global source location is still on the token before the
19000             '>>', so we need to say explicitly where we want it.  */
19001           cp_token *token = cp_lexer_peek_token (parser->lexer);
19002           error_at (token->location, "%<>>%> should be %<> >%> "
19003                     "within a nested template argument list");
19004
19005           token->type = CPP_GREATER;
19006         }
19007       else
19008         {
19009           /* If this is not a nested template argument list, the '>>'
19010             is a typo for '>'. Emit an error message and continue.
19011             Same deal about the token location, but here we can get it
19012             right by consuming the '>>' before issuing the diagnostic.  */
19013           cp_token *token = cp_lexer_consume_token (parser->lexer);
19014           error_at (token->location,
19015                     "spurious %<>>%>, use %<>%> to terminate "
19016                     "a template argument list");
19017         }
19018     }
19019   else
19020     cp_parser_skip_to_end_of_template_parameter_list (parser);
19021   /* The `>' token might be a greater-than operator again now.  */
19022   parser->greater_than_is_operator_p
19023     = saved_greater_than_is_operator_p;
19024   /* Restore the SAVED_SCOPE.  */
19025   parser->scope = saved_scope;
19026   parser->qualifying_scope = saved_qualifying_scope;
19027   parser->object_scope = saved_object_scope;
19028   cp_unevaluated_operand = saved_unevaluated_operand;
19029   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
19030
19031   return arguments;
19032 }
19033
19034 /* MEMBER_FUNCTION is a member function, or a friend.  If default
19035    arguments, or the body of the function have not yet been parsed,
19036    parse them now.  */
19037
19038 static void
19039 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
19040 {
19041   /* If this member is a template, get the underlying
19042      FUNCTION_DECL.  */
19043   if (DECL_FUNCTION_TEMPLATE_P (member_function))
19044     member_function = DECL_TEMPLATE_RESULT (member_function);
19045
19046   /* There should not be any class definitions in progress at this
19047      point; the bodies of members are only parsed outside of all class
19048      definitions.  */
19049   gcc_assert (parser->num_classes_being_defined == 0);
19050   /* While we're parsing the member functions we might encounter more
19051      classes.  We want to handle them right away, but we don't want
19052      them getting mixed up with functions that are currently in the
19053      queue.  */
19054   parser->unparsed_functions_queues
19055     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19056
19057   /* Make sure that any template parameters are in scope.  */
19058   maybe_begin_member_template_processing (member_function);
19059
19060   /* If the body of the function has not yet been parsed, parse it
19061      now.  */
19062   if (DECL_PENDING_INLINE_P (member_function))
19063     {
19064       tree function_scope;
19065       cp_token_cache *tokens;
19066
19067       /* The function is no longer pending; we are processing it.  */
19068       tokens = DECL_PENDING_INLINE_INFO (member_function);
19069       DECL_PENDING_INLINE_INFO (member_function) = NULL;
19070       DECL_PENDING_INLINE_P (member_function) = 0;
19071
19072       /* If this is a local class, enter the scope of the containing
19073          function.  */
19074       function_scope = current_function_decl;
19075       if (function_scope)
19076         push_function_context ();
19077
19078       /* Push the body of the function onto the lexer stack.  */
19079       cp_parser_push_lexer_for_tokens (parser, tokens);
19080
19081       /* Let the front end know that we going to be defining this
19082          function.  */
19083       start_preparsed_function (member_function, NULL_TREE,
19084                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
19085
19086       /* Don't do access checking if it is a templated function.  */
19087       if (processing_template_decl)
19088         push_deferring_access_checks (dk_no_check);
19089
19090       /* Now, parse the body of the function.  */
19091       cp_parser_function_definition_after_declarator (parser,
19092                                                       /*inline_p=*/true);
19093
19094       if (processing_template_decl)
19095         pop_deferring_access_checks ();
19096
19097       /* Leave the scope of the containing function.  */
19098       if (function_scope)
19099         pop_function_context ();
19100       cp_parser_pop_lexer (parser);
19101     }
19102
19103   /* Remove any template parameters from the symbol table.  */
19104   maybe_end_member_template_processing ();
19105
19106   /* Restore the queue.  */
19107   parser->unparsed_functions_queues
19108     = TREE_CHAIN (parser->unparsed_functions_queues);
19109 }
19110
19111 /* If DECL contains any default args, remember it on the unparsed
19112    functions queue.  */
19113
19114 static void
19115 cp_parser_save_default_args (cp_parser* parser, tree decl)
19116 {
19117   tree probe;
19118
19119   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
19120        probe;
19121        probe = TREE_CHAIN (probe))
19122     if (TREE_PURPOSE (probe))
19123       {
19124         TREE_PURPOSE (parser->unparsed_functions_queues)
19125           = tree_cons (current_class_type, decl,
19126                        TREE_PURPOSE (parser->unparsed_functions_queues));
19127         break;
19128       }
19129 }
19130
19131 /* FN is a FUNCTION_DECL which may contains a parameter with an
19132    unparsed DEFAULT_ARG.  Parse the default args now.  This function
19133    assumes that the current scope is the scope in which the default
19134    argument should be processed.  */
19135
19136 static void
19137 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
19138 {
19139   bool saved_local_variables_forbidden_p;
19140   tree parm, parmdecl;
19141
19142   /* While we're parsing the default args, we might (due to the
19143      statement expression extension) encounter more classes.  We want
19144      to handle them right away, but we don't want them getting mixed
19145      up with default args that are currently in the queue.  */
19146   parser->unparsed_functions_queues
19147     = tree_cons (NULL_TREE, NULL_TREE, parser->unparsed_functions_queues);
19148
19149   /* Local variable names (and the `this' keyword) may not appear
19150      in a default argument.  */
19151   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
19152   parser->local_variables_forbidden_p = true;
19153
19154   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
19155          parmdecl = DECL_ARGUMENTS (fn);
19156        parm && parm != void_list_node;
19157        parm = TREE_CHAIN (parm),
19158          parmdecl = TREE_CHAIN (parmdecl))
19159     {
19160       cp_token_cache *tokens;
19161       tree default_arg = TREE_PURPOSE (parm);
19162       tree parsed_arg;
19163       VEC(tree,gc) *insts;
19164       tree copy;
19165       unsigned ix;
19166
19167       if (!default_arg)
19168         continue;
19169
19170       if (TREE_CODE (default_arg) != DEFAULT_ARG)
19171         /* This can happen for a friend declaration for a function
19172            already declared with default arguments.  */
19173         continue;
19174
19175        /* Push the saved tokens for the default argument onto the parser's
19176           lexer stack.  */
19177       tokens = DEFARG_TOKENS (default_arg);
19178       cp_parser_push_lexer_for_tokens (parser, tokens);
19179
19180       start_lambda_scope (parmdecl);
19181
19182       /* Parse the assignment-expression.  */
19183       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
19184       if (parsed_arg == error_mark_node)
19185         {
19186           cp_parser_pop_lexer (parser);
19187           continue;
19188         }
19189
19190       if (!processing_template_decl)
19191         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
19192
19193       TREE_PURPOSE (parm) = parsed_arg;
19194
19195       /* Update any instantiations we've already created.  */
19196       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
19197            VEC_iterate (tree, insts, ix, copy); ix++)
19198         TREE_PURPOSE (copy) = parsed_arg;
19199
19200       finish_lambda_scope ();
19201
19202       /* If the token stream has not been completely used up, then
19203          there was extra junk after the end of the default
19204          argument.  */
19205       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
19206         cp_parser_error (parser, "expected %<,%>");
19207
19208       /* Revert to the main lexer.  */
19209       cp_parser_pop_lexer (parser);
19210     }
19211
19212   /* Make sure no default arg is missing.  */
19213   check_default_args (fn);
19214
19215   /* Restore the state of local_variables_forbidden_p.  */
19216   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
19217
19218   /* Restore the queue.  */
19219   parser->unparsed_functions_queues
19220     = TREE_CHAIN (parser->unparsed_functions_queues);
19221 }
19222
19223 /* Parse the operand of `sizeof' (or a similar operator).  Returns
19224    either a TYPE or an expression, depending on the form of the
19225    input.  The KEYWORD indicates which kind of expression we have
19226    encountered.  */
19227
19228 static tree
19229 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
19230 {
19231   tree expr = NULL_TREE;
19232   const char *saved_message;
19233   char *tmp;
19234   bool saved_integral_constant_expression_p;
19235   bool saved_non_integral_constant_expression_p;
19236   bool pack_expansion_p = false;
19237
19238   /* Types cannot be defined in a `sizeof' expression.  Save away the
19239      old message.  */
19240   saved_message = parser->type_definition_forbidden_message;
19241   /* And create the new one.  */
19242   tmp = concat ("types may not be defined in %<",
19243                 IDENTIFIER_POINTER (ridpointers[keyword]),
19244                 "%> expressions", NULL);
19245   parser->type_definition_forbidden_message = tmp;
19246
19247   /* The restrictions on constant-expressions do not apply inside
19248      sizeof expressions.  */
19249   saved_integral_constant_expression_p
19250     = parser->integral_constant_expression_p;
19251   saved_non_integral_constant_expression_p
19252     = parser->non_integral_constant_expression_p;
19253   parser->integral_constant_expression_p = false;
19254
19255   /* If it's a `...', then we are computing the length of a parameter
19256      pack.  */
19257   if (keyword == RID_SIZEOF
19258       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19259     {
19260       /* Consume the `...'.  */
19261       cp_lexer_consume_token (parser->lexer);
19262       maybe_warn_variadic_templates ();
19263
19264       /* Note that this is an expansion.  */
19265       pack_expansion_p = true;
19266     }
19267
19268   /* Do not actually evaluate the expression.  */
19269   ++cp_unevaluated_operand;
19270   ++c_inhibit_evaluation_warnings;
19271   /* If it's a `(', then we might be looking at the type-id
19272      construction.  */
19273   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
19274     {
19275       tree type;
19276       bool saved_in_type_id_in_expr_p;
19277
19278       /* We can't be sure yet whether we're looking at a type-id or an
19279          expression.  */
19280       cp_parser_parse_tentatively (parser);
19281       /* Consume the `('.  */
19282       cp_lexer_consume_token (parser->lexer);
19283       /* Parse the type-id.  */
19284       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
19285       parser->in_type_id_in_expr_p = true;
19286       type = cp_parser_type_id (parser);
19287       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
19288       /* Now, look for the trailing `)'.  */
19289       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
19290       /* If all went well, then we're done.  */
19291       if (cp_parser_parse_definitely (parser))
19292         {
19293           cp_decl_specifier_seq decl_specs;
19294
19295           /* Build a trivial decl-specifier-seq.  */
19296           clear_decl_specs (&decl_specs);
19297           decl_specs.type = type;
19298
19299           /* Call grokdeclarator to figure out what type this is.  */
19300           expr = grokdeclarator (NULL,
19301                                  &decl_specs,
19302                                  TYPENAME,
19303                                  /*initialized=*/0,
19304                                  /*attrlist=*/NULL);
19305         }
19306     }
19307
19308   /* If the type-id production did not work out, then we must be
19309      looking at the unary-expression production.  */
19310   if (!expr)
19311     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
19312                                        /*cast_p=*/false, NULL);
19313
19314   if (pack_expansion_p)
19315     /* Build a pack expansion. */
19316     expr = make_pack_expansion (expr);
19317
19318   /* Go back to evaluating expressions.  */
19319   --cp_unevaluated_operand;
19320   --c_inhibit_evaluation_warnings;
19321
19322   /* Free the message we created.  */
19323   free (tmp);
19324   /* And restore the old one.  */
19325   parser->type_definition_forbidden_message = saved_message;
19326   parser->integral_constant_expression_p
19327     = saved_integral_constant_expression_p;
19328   parser->non_integral_constant_expression_p
19329     = saved_non_integral_constant_expression_p;
19330
19331   return expr;
19332 }
19333
19334 /* If the current declaration has no declarator, return true.  */
19335
19336 static bool
19337 cp_parser_declares_only_class_p (cp_parser *parser)
19338 {
19339   /* If the next token is a `;' or a `,' then there is no
19340      declarator.  */
19341   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
19342           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
19343 }
19344
19345 /* Update the DECL_SPECS to reflect the storage class indicated by
19346    KEYWORD.  */
19347
19348 static void
19349 cp_parser_set_storage_class (cp_parser *parser,
19350                              cp_decl_specifier_seq *decl_specs,
19351                              enum rid keyword,
19352                              location_t location)
19353 {
19354   cp_storage_class storage_class;
19355
19356   if (parser->in_unbraced_linkage_specification_p)
19357     {
19358       error_at (location, "invalid use of %qD in linkage specification",
19359                 ridpointers[keyword]);
19360       return;
19361     }
19362   else if (decl_specs->storage_class != sc_none)
19363     {
19364       decl_specs->conflicting_specifiers_p = true;
19365       return;
19366     }
19367
19368   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
19369       && decl_specs->specs[(int) ds_thread])
19370     {
19371       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
19372       decl_specs->specs[(int) ds_thread] = 0;
19373     }
19374
19375   switch (keyword)
19376     {
19377     case RID_AUTO:
19378       storage_class = sc_auto;
19379       break;
19380     case RID_REGISTER:
19381       storage_class = sc_register;
19382       break;
19383     case RID_STATIC:
19384       storage_class = sc_static;
19385       break;
19386     case RID_EXTERN:
19387       storage_class = sc_extern;
19388       break;
19389     case RID_MUTABLE:
19390       storage_class = sc_mutable;
19391       break;
19392     default:
19393       gcc_unreachable ();
19394     }
19395   decl_specs->storage_class = storage_class;
19396
19397   /* A storage class specifier cannot be applied alongside a typedef 
19398      specifier. If there is a typedef specifier present then set 
19399      conflicting_specifiers_p which will trigger an error later
19400      on in grokdeclarator. */
19401   if (decl_specs->specs[(int)ds_typedef])
19402     decl_specs->conflicting_specifiers_p = true;
19403 }
19404
19405 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
19406    is true, the type is a user-defined type; otherwise it is a
19407    built-in type specified by a keyword.  */
19408
19409 static void
19410 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
19411                               tree type_spec,
19412                               location_t location,
19413                               bool user_defined_p)
19414 {
19415   decl_specs->any_specifiers_p = true;
19416
19417   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
19418      (with, for example, in "typedef int wchar_t;") we remember that
19419      this is what happened.  In system headers, we ignore these
19420      declarations so that G++ can work with system headers that are not
19421      C++-safe.  */
19422   if (decl_specs->specs[(int) ds_typedef]
19423       && !user_defined_p
19424       && (type_spec == boolean_type_node
19425           || type_spec == char16_type_node
19426           || type_spec == char32_type_node
19427           || type_spec == wchar_type_node)
19428       && (decl_specs->type
19429           || decl_specs->specs[(int) ds_long]
19430           || decl_specs->specs[(int) ds_short]
19431           || decl_specs->specs[(int) ds_unsigned]
19432           || decl_specs->specs[(int) ds_signed]))
19433     {
19434       decl_specs->redefined_builtin_type = type_spec;
19435       if (!decl_specs->type)
19436         {
19437           decl_specs->type = type_spec;
19438           decl_specs->user_defined_type_p = false;
19439           decl_specs->type_location = location;
19440         }
19441     }
19442   else if (decl_specs->type)
19443     decl_specs->multiple_types_p = true;
19444   else
19445     {
19446       decl_specs->type = type_spec;
19447       decl_specs->user_defined_type_p = user_defined_p;
19448       decl_specs->redefined_builtin_type = NULL_TREE;
19449       decl_specs->type_location = location;
19450     }
19451 }
19452
19453 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
19454    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
19455
19456 static bool
19457 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
19458 {
19459   return decl_specifiers->specs[(int) ds_friend] != 0;
19460 }
19461
19462 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
19463    issue an error message indicating that TOKEN_DESC was expected.
19464
19465    Returns the token consumed, if the token had the appropriate type.
19466    Otherwise, returns NULL.  */
19467
19468 static cp_token *
19469 cp_parser_require (cp_parser* parser,
19470                    enum cpp_ttype type,
19471                    const char* token_desc)
19472 {
19473   if (cp_lexer_next_token_is (parser->lexer, type))
19474     return cp_lexer_consume_token (parser->lexer);
19475   else
19476     {
19477       /* Output the MESSAGE -- unless we're parsing tentatively.  */
19478       if (!cp_parser_simulate_error (parser))
19479         {
19480           char *message = concat ("expected ", token_desc, NULL);
19481           cp_parser_error (parser, message);
19482           free (message);
19483         }
19484       return NULL;
19485     }
19486 }
19487
19488 /* An error message is produced if the next token is not '>'.
19489    All further tokens are skipped until the desired token is
19490    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
19491
19492 static void
19493 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
19494 {
19495   /* Current level of '< ... >'.  */
19496   unsigned level = 0;
19497   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
19498   unsigned nesting_depth = 0;
19499
19500   /* Are we ready, yet?  If not, issue error message.  */
19501   if (cp_parser_require (parser, CPP_GREATER, "%<>%>"))
19502     return;
19503
19504   /* Skip tokens until the desired token is found.  */
19505   while (true)
19506     {
19507       /* Peek at the next token.  */
19508       switch (cp_lexer_peek_token (parser->lexer)->type)
19509         {
19510         case CPP_LESS:
19511           if (!nesting_depth)
19512             ++level;
19513           break;
19514
19515         case CPP_RSHIFT:
19516           if (cxx_dialect == cxx98)
19517             /* C++0x views the `>>' operator as two `>' tokens, but
19518                C++98 does not. */
19519             break;
19520           else if (!nesting_depth && level-- == 0)
19521             {
19522               /* We've hit a `>>' where the first `>' closes the
19523                  template argument list, and the second `>' is
19524                  spurious.  Just consume the `>>' and stop; we've
19525                  already produced at least one error.  */
19526               cp_lexer_consume_token (parser->lexer);
19527               return;
19528             }
19529           /* Fall through for C++0x, so we handle the second `>' in
19530              the `>>'.  */
19531
19532         case CPP_GREATER:
19533           if (!nesting_depth && level-- == 0)
19534             {
19535               /* We've reached the token we want, consume it and stop.  */
19536               cp_lexer_consume_token (parser->lexer);
19537               return;
19538             }
19539           break;
19540
19541         case CPP_OPEN_PAREN:
19542         case CPP_OPEN_SQUARE:
19543           ++nesting_depth;
19544           break;
19545
19546         case CPP_CLOSE_PAREN:
19547         case CPP_CLOSE_SQUARE:
19548           if (nesting_depth-- == 0)
19549             return;
19550           break;
19551
19552         case CPP_EOF:
19553         case CPP_PRAGMA_EOL:
19554         case CPP_SEMICOLON:
19555         case CPP_OPEN_BRACE:
19556         case CPP_CLOSE_BRACE:
19557           /* The '>' was probably forgotten, don't look further.  */
19558           return;
19559
19560         default:
19561           break;
19562         }
19563
19564       /* Consume this token.  */
19565       cp_lexer_consume_token (parser->lexer);
19566     }
19567 }
19568
19569 /* If the next token is the indicated keyword, consume it.  Otherwise,
19570    issue an error message indicating that TOKEN_DESC was expected.
19571
19572    Returns the token consumed, if the token had the appropriate type.
19573    Otherwise, returns NULL.  */
19574
19575 static cp_token *
19576 cp_parser_require_keyword (cp_parser* parser,
19577                            enum rid keyword,
19578                            const char* token_desc)
19579 {
19580   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
19581
19582   if (token && token->keyword != keyword)
19583     {
19584       dyn_string_t error_msg;
19585
19586       /* Format the error message.  */
19587       error_msg = dyn_string_new (0);
19588       dyn_string_append_cstr (error_msg, "expected ");
19589       dyn_string_append_cstr (error_msg, token_desc);
19590       cp_parser_error (parser, error_msg->s);
19591       dyn_string_delete (error_msg);
19592       return NULL;
19593     }
19594
19595   return token;
19596 }
19597
19598 /* Returns TRUE iff TOKEN is a token that can begin the body of a
19599    function-definition.  */
19600
19601 static bool
19602 cp_parser_token_starts_function_definition_p (cp_token* token)
19603 {
19604   return (/* An ordinary function-body begins with an `{'.  */
19605           token->type == CPP_OPEN_BRACE
19606           /* A ctor-initializer begins with a `:'.  */
19607           || token->type == CPP_COLON
19608           /* A function-try-block begins with `try'.  */
19609           || token->keyword == RID_TRY
19610           /* The named return value extension begins with `return'.  */
19611           || token->keyword == RID_RETURN);
19612 }
19613
19614 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
19615    definition.  */
19616
19617 static bool
19618 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
19619 {
19620   cp_token *token;
19621
19622   token = cp_lexer_peek_token (parser->lexer);
19623   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
19624 }
19625
19626 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
19627    C++0x) ending a template-argument.  */
19628
19629 static bool
19630 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
19631 {
19632   cp_token *token;
19633
19634   token = cp_lexer_peek_token (parser->lexer);
19635   return (token->type == CPP_COMMA 
19636           || token->type == CPP_GREATER
19637           || token->type == CPP_ELLIPSIS
19638           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
19639 }
19640
19641 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
19642    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
19643
19644 static bool
19645 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
19646                                                      size_t n)
19647 {
19648   cp_token *token;
19649
19650   token = cp_lexer_peek_nth_token (parser->lexer, n);
19651   if (token->type == CPP_LESS)
19652     return true;
19653   /* Check for the sequence `<::' in the original code. It would be lexed as
19654      `[:', where `[' is a digraph, and there is no whitespace before
19655      `:'.  */
19656   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
19657     {
19658       cp_token *token2;
19659       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
19660       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
19661         return true;
19662     }
19663   return false;
19664 }
19665
19666 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
19667    or none_type otherwise.  */
19668
19669 static enum tag_types
19670 cp_parser_token_is_class_key (cp_token* token)
19671 {
19672   switch (token->keyword)
19673     {
19674     case RID_CLASS:
19675       return class_type;
19676     case RID_STRUCT:
19677       return record_type;
19678     case RID_UNION:
19679       return union_type;
19680
19681     default:
19682       return none_type;
19683     }
19684 }
19685
19686 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
19687
19688 static void
19689 cp_parser_check_class_key (enum tag_types class_key, tree type)
19690 {
19691   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
19692     permerror (input_location, "%qs tag used in naming %q#T",
19693             class_key == union_type ? "union"
19694              : class_key == record_type ? "struct" : "class",
19695              type);
19696 }
19697
19698 /* Issue an error message if DECL is redeclared with different
19699    access than its original declaration [class.access.spec/3].
19700    This applies to nested classes and nested class templates.
19701    [class.mem/1].  */
19702
19703 static void
19704 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
19705 {
19706   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
19707     return;
19708
19709   if ((TREE_PRIVATE (decl)
19710        != (current_access_specifier == access_private_node))
19711       || (TREE_PROTECTED (decl)
19712           != (current_access_specifier == access_protected_node)))
19713     error_at (location, "%qD redeclared with different access", decl);
19714 }
19715
19716 /* Look for the `template' keyword, as a syntactic disambiguator.
19717    Return TRUE iff it is present, in which case it will be
19718    consumed.  */
19719
19720 static bool
19721 cp_parser_optional_template_keyword (cp_parser *parser)
19722 {
19723   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
19724     {
19725       /* The `template' keyword can only be used within templates;
19726          outside templates the parser can always figure out what is a
19727          template and what is not.  */
19728       if (!processing_template_decl)
19729         {
19730           cp_token *token = cp_lexer_peek_token (parser->lexer);
19731           error_at (token->location,
19732                     "%<template%> (as a disambiguator) is only allowed "
19733                     "within templates");
19734           /* If this part of the token stream is rescanned, the same
19735              error message would be generated.  So, we purge the token
19736              from the stream.  */
19737           cp_lexer_purge_token (parser->lexer);
19738           return false;
19739         }
19740       else
19741         {
19742           /* Consume the `template' keyword.  */
19743           cp_lexer_consume_token (parser->lexer);
19744           return true;
19745         }
19746     }
19747
19748   return false;
19749 }
19750
19751 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
19752    set PARSER->SCOPE, and perform other related actions.  */
19753
19754 static void
19755 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
19756 {
19757   int i;
19758   struct tree_check *check_value;
19759   deferred_access_check *chk;
19760   VEC (deferred_access_check,gc) *checks;
19761
19762   /* Get the stored value.  */
19763   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
19764   /* Perform any access checks that were deferred.  */
19765   checks = check_value->checks;
19766   if (checks)
19767     {
19768       for (i = 0 ;
19769            VEC_iterate (deferred_access_check, checks, i, chk) ;
19770            ++i)
19771         {
19772           perform_or_defer_access_check (chk->binfo,
19773                                          chk->decl,
19774                                          chk->diag_decl);
19775         }
19776     }
19777   /* Set the scope from the stored value.  */
19778   parser->scope = check_value->value;
19779   parser->qualifying_scope = check_value->qualifying_scope;
19780   parser->object_scope = NULL_TREE;
19781 }
19782
19783 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
19784    encounter the end of a block before what we were looking for.  */
19785
19786 static bool
19787 cp_parser_cache_group (cp_parser *parser,
19788                        enum cpp_ttype end,
19789                        unsigned depth)
19790 {
19791   while (true)
19792     {
19793       cp_token *token = cp_lexer_peek_token (parser->lexer);
19794
19795       /* Abort a parenthesized expression if we encounter a semicolon.  */
19796       if ((end == CPP_CLOSE_PAREN || depth == 0)
19797           && token->type == CPP_SEMICOLON)
19798         return true;
19799       /* If we've reached the end of the file, stop.  */
19800       if (token->type == CPP_EOF
19801           || (end != CPP_PRAGMA_EOL
19802               && token->type == CPP_PRAGMA_EOL))
19803         return true;
19804       if (token->type == CPP_CLOSE_BRACE && depth == 0)
19805         /* We've hit the end of an enclosing block, so there's been some
19806            kind of syntax error.  */
19807         return true;
19808
19809       /* Consume the token.  */
19810       cp_lexer_consume_token (parser->lexer);
19811       /* See if it starts a new group.  */
19812       if (token->type == CPP_OPEN_BRACE)
19813         {
19814           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
19815           /* In theory this should probably check end == '}', but
19816              cp_parser_save_member_function_body needs it to exit
19817              after either '}' or ')' when called with ')'.  */
19818           if (depth == 0)
19819             return false;
19820         }
19821       else if (token->type == CPP_OPEN_PAREN)
19822         {
19823           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
19824           if (depth == 0 && end == CPP_CLOSE_PAREN)
19825             return false;
19826         }
19827       else if (token->type == CPP_PRAGMA)
19828         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
19829       else if (token->type == end)
19830         return false;
19831     }
19832 }
19833
19834 /* Begin parsing tentatively.  We always save tokens while parsing
19835    tentatively so that if the tentative parsing fails we can restore the
19836    tokens.  */
19837
19838 static void
19839 cp_parser_parse_tentatively (cp_parser* parser)
19840 {
19841   /* Enter a new parsing context.  */
19842   parser->context = cp_parser_context_new (parser->context);
19843   /* Begin saving tokens.  */
19844   cp_lexer_save_tokens (parser->lexer);
19845   /* In order to avoid repetitive access control error messages,
19846      access checks are queued up until we are no longer parsing
19847      tentatively.  */
19848   push_deferring_access_checks (dk_deferred);
19849 }
19850
19851 /* Commit to the currently active tentative parse.  */
19852
19853 static void
19854 cp_parser_commit_to_tentative_parse (cp_parser* parser)
19855 {
19856   cp_parser_context *context;
19857   cp_lexer *lexer;
19858
19859   /* Mark all of the levels as committed.  */
19860   lexer = parser->lexer;
19861   for (context = parser->context; context->next; context = context->next)
19862     {
19863       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
19864         break;
19865       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
19866       while (!cp_lexer_saving_tokens (lexer))
19867         lexer = lexer->next;
19868       cp_lexer_commit_tokens (lexer);
19869     }
19870 }
19871
19872 /* Abort the currently active tentative parse.  All consumed tokens
19873    will be rolled back, and no diagnostics will be issued.  */
19874
19875 static void
19876 cp_parser_abort_tentative_parse (cp_parser* parser)
19877 {
19878   cp_parser_simulate_error (parser);
19879   /* Now, pretend that we want to see if the construct was
19880      successfully parsed.  */
19881   cp_parser_parse_definitely (parser);
19882 }
19883
19884 /* Stop parsing tentatively.  If a parse error has occurred, restore the
19885    token stream.  Otherwise, commit to the tokens we have consumed.
19886    Returns true if no error occurred; false otherwise.  */
19887
19888 static bool
19889 cp_parser_parse_definitely (cp_parser* parser)
19890 {
19891   bool error_occurred;
19892   cp_parser_context *context;
19893
19894   /* Remember whether or not an error occurred, since we are about to
19895      destroy that information.  */
19896   error_occurred = cp_parser_error_occurred (parser);
19897   /* Remove the topmost context from the stack.  */
19898   context = parser->context;
19899   parser->context = context->next;
19900   /* If no parse errors occurred, commit to the tentative parse.  */
19901   if (!error_occurred)
19902     {
19903       /* Commit to the tokens read tentatively, unless that was
19904          already done.  */
19905       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
19906         cp_lexer_commit_tokens (parser->lexer);
19907
19908       pop_to_parent_deferring_access_checks ();
19909     }
19910   /* Otherwise, if errors occurred, roll back our state so that things
19911      are just as they were before we began the tentative parse.  */
19912   else
19913     {
19914       cp_lexer_rollback_tokens (parser->lexer);
19915       pop_deferring_access_checks ();
19916     }
19917   /* Add the context to the front of the free list.  */
19918   context->next = cp_parser_context_free_list;
19919   cp_parser_context_free_list = context;
19920
19921   return !error_occurred;
19922 }
19923
19924 /* Returns true if we are parsing tentatively and are not committed to
19925    this tentative parse.  */
19926
19927 static bool
19928 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
19929 {
19930   return (cp_parser_parsing_tentatively (parser)
19931           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
19932 }
19933
19934 /* Returns nonzero iff an error has occurred during the most recent
19935    tentative parse.  */
19936
19937 static bool
19938 cp_parser_error_occurred (cp_parser* parser)
19939 {
19940   return (cp_parser_parsing_tentatively (parser)
19941           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
19942 }
19943
19944 /* Returns nonzero if GNU extensions are allowed.  */
19945
19946 static bool
19947 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
19948 {
19949   return parser->allow_gnu_extensions_p;
19950 }
19951 \f
19952 /* Objective-C++ Productions */
19953
19954
19955 /* Parse an Objective-C expression, which feeds into a primary-expression
19956    above.
19957
19958    objc-expression:
19959      objc-message-expression
19960      objc-string-literal
19961      objc-encode-expression
19962      objc-protocol-expression
19963      objc-selector-expression
19964
19965   Returns a tree representation of the expression.  */
19966
19967 static tree
19968 cp_parser_objc_expression (cp_parser* parser)
19969 {
19970   /* Try to figure out what kind of declaration is present.  */
19971   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
19972
19973   switch (kwd->type)
19974     {
19975     case CPP_OPEN_SQUARE:
19976       return cp_parser_objc_message_expression (parser);
19977
19978     case CPP_OBJC_STRING:
19979       kwd = cp_lexer_consume_token (parser->lexer);
19980       return objc_build_string_object (kwd->u.value);
19981
19982     case CPP_KEYWORD:
19983       switch (kwd->keyword)
19984         {
19985         case RID_AT_ENCODE:
19986           return cp_parser_objc_encode_expression (parser);
19987
19988         case RID_AT_PROTOCOL:
19989           return cp_parser_objc_protocol_expression (parser);
19990
19991         case RID_AT_SELECTOR:
19992           return cp_parser_objc_selector_expression (parser);
19993
19994         default:
19995           break;
19996         }
19997     default:
19998       error_at (kwd->location,
19999                 "misplaced %<@%D%> Objective-C++ construct",
20000                 kwd->u.value);
20001       cp_parser_skip_to_end_of_block_or_statement (parser);
20002     }
20003
20004   return error_mark_node;
20005 }
20006
20007 /* Parse an Objective-C message expression.
20008
20009    objc-message-expression:
20010      [ objc-message-receiver objc-message-args ]
20011
20012    Returns a representation of an Objective-C message.  */
20013
20014 static tree
20015 cp_parser_objc_message_expression (cp_parser* parser)
20016 {
20017   tree receiver, messageargs;
20018
20019   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
20020   receiver = cp_parser_objc_message_receiver (parser);
20021   messageargs = cp_parser_objc_message_args (parser);
20022   cp_parser_require (parser, CPP_CLOSE_SQUARE, "%<]%>");
20023
20024   return objc_build_message_expr (build_tree_list (receiver, messageargs));
20025 }
20026
20027 /* Parse an objc-message-receiver.
20028
20029    objc-message-receiver:
20030      expression
20031      simple-type-specifier
20032
20033   Returns a representation of the type or expression.  */
20034
20035 static tree
20036 cp_parser_objc_message_receiver (cp_parser* parser)
20037 {
20038   tree rcv;
20039
20040   /* An Objective-C message receiver may be either (1) a type
20041      or (2) an expression.  */
20042   cp_parser_parse_tentatively (parser);
20043   rcv = cp_parser_expression (parser, false, NULL);
20044
20045   if (cp_parser_parse_definitely (parser))
20046     return rcv;
20047
20048   rcv = cp_parser_simple_type_specifier (parser,
20049                                          /*decl_specs=*/NULL,
20050                                          CP_PARSER_FLAGS_NONE);
20051
20052   return objc_get_class_reference (rcv);
20053 }
20054
20055 /* Parse the arguments and selectors comprising an Objective-C message.
20056
20057    objc-message-args:
20058      objc-selector
20059      objc-selector-args
20060      objc-selector-args , objc-comma-args
20061
20062    objc-selector-args:
20063      objc-selector [opt] : assignment-expression
20064      objc-selector-args objc-selector [opt] : assignment-expression
20065
20066    objc-comma-args:
20067      assignment-expression
20068      objc-comma-args , assignment-expression
20069
20070    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
20071    selector arguments and TREE_VALUE containing a list of comma
20072    arguments.  */
20073
20074 static tree
20075 cp_parser_objc_message_args (cp_parser* parser)
20076 {
20077   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
20078   bool maybe_unary_selector_p = true;
20079   cp_token *token = cp_lexer_peek_token (parser->lexer);
20080
20081   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20082     {
20083       tree selector = NULL_TREE, arg;
20084
20085       if (token->type != CPP_COLON)
20086         selector = cp_parser_objc_selector (parser);
20087
20088       /* Detect if we have a unary selector.  */
20089       if (maybe_unary_selector_p
20090           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20091         return build_tree_list (selector, NULL_TREE);
20092
20093       maybe_unary_selector_p = false;
20094       cp_parser_require (parser, CPP_COLON, "%<:%>");
20095       arg = cp_parser_assignment_expression (parser, false, NULL);
20096
20097       sel_args
20098         = chainon (sel_args,
20099                    build_tree_list (selector, arg));
20100
20101       token = cp_lexer_peek_token (parser->lexer);
20102     }
20103
20104   /* Handle non-selector arguments, if any. */
20105   while (token->type == CPP_COMMA)
20106     {
20107       tree arg;
20108
20109       cp_lexer_consume_token (parser->lexer);
20110       arg = cp_parser_assignment_expression (parser, false, NULL);
20111
20112       addl_args
20113         = chainon (addl_args,
20114                    build_tree_list (NULL_TREE, arg));
20115
20116       token = cp_lexer_peek_token (parser->lexer);
20117     }
20118
20119   return build_tree_list (sel_args, addl_args);
20120 }
20121
20122 /* Parse an Objective-C encode expression.
20123
20124    objc-encode-expression:
20125      @encode objc-typename
20126
20127    Returns an encoded representation of the type argument.  */
20128
20129 static tree
20130 cp_parser_objc_encode_expression (cp_parser* parser)
20131 {
20132   tree type;
20133   cp_token *token;
20134
20135   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
20136   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20137   token = cp_lexer_peek_token (parser->lexer);
20138   type = complete_type (cp_parser_type_id (parser));
20139   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20140
20141   if (!type)
20142     {
20143       error_at (token->location, 
20144                 "%<@encode%> must specify a type as an argument");
20145       return error_mark_node;
20146     }
20147
20148   return objc_build_encode_expr (type);
20149 }
20150
20151 /* Parse an Objective-C @defs expression.  */
20152
20153 static tree
20154 cp_parser_objc_defs_expression (cp_parser *parser)
20155 {
20156   tree name;
20157
20158   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
20159   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20160   name = cp_parser_identifier (parser);
20161   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20162
20163   return objc_get_class_ivars (name);
20164 }
20165
20166 /* Parse an Objective-C protocol expression.
20167
20168   objc-protocol-expression:
20169     @protocol ( identifier )
20170
20171   Returns a representation of the protocol expression.  */
20172
20173 static tree
20174 cp_parser_objc_protocol_expression (cp_parser* parser)
20175 {
20176   tree proto;
20177
20178   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20179   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20180   proto = cp_parser_identifier (parser);
20181   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20182
20183   return objc_build_protocol_expr (proto);
20184 }
20185
20186 /* Parse an Objective-C selector expression.
20187
20188    objc-selector-expression:
20189      @selector ( objc-method-signature )
20190
20191    objc-method-signature:
20192      objc-selector
20193      objc-selector-seq
20194
20195    objc-selector-seq:
20196      objc-selector :
20197      objc-selector-seq objc-selector :
20198
20199   Returns a representation of the method selector.  */
20200
20201 static tree
20202 cp_parser_objc_selector_expression (cp_parser* parser)
20203 {
20204   tree sel_seq = NULL_TREE;
20205   bool maybe_unary_selector_p = true;
20206   cp_token *token;
20207   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
20208
20209   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
20210   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20211   token = cp_lexer_peek_token (parser->lexer);
20212
20213   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
20214          || token->type == CPP_SCOPE)
20215     {
20216       tree selector = NULL_TREE;
20217
20218       if (token->type != CPP_COLON
20219           || token->type == CPP_SCOPE)
20220         selector = cp_parser_objc_selector (parser);
20221
20222       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
20223           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
20224         {
20225           /* Detect if we have a unary selector.  */
20226           if (maybe_unary_selector_p)
20227             {
20228               sel_seq = selector;
20229               goto finish_selector;
20230             }
20231           else
20232             {
20233               cp_parser_error (parser, "expected %<:%>");
20234             }
20235         }
20236       maybe_unary_selector_p = false;
20237       token = cp_lexer_consume_token (parser->lexer);
20238
20239       if (token->type == CPP_SCOPE)
20240         {
20241           sel_seq
20242             = chainon (sel_seq,
20243                        build_tree_list (selector, NULL_TREE));
20244           sel_seq
20245             = chainon (sel_seq,
20246                        build_tree_list (NULL_TREE, NULL_TREE));
20247         }
20248       else
20249         sel_seq
20250           = chainon (sel_seq,
20251                      build_tree_list (selector, NULL_TREE));
20252
20253       token = cp_lexer_peek_token (parser->lexer);
20254     }
20255
20256  finish_selector:
20257   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20258
20259   return objc_build_selector_expr (loc, sel_seq);
20260 }
20261
20262 /* Parse a list of identifiers.
20263
20264    objc-identifier-list:
20265      identifier
20266      objc-identifier-list , identifier
20267
20268    Returns a TREE_LIST of identifier nodes.  */
20269
20270 static tree
20271 cp_parser_objc_identifier_list (cp_parser* parser)
20272 {
20273   tree list = build_tree_list (NULL_TREE, cp_parser_identifier (parser));
20274   cp_token *sep = cp_lexer_peek_token (parser->lexer);
20275
20276   while (sep->type == CPP_COMMA)
20277     {
20278       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20279       list = chainon (list,
20280                       build_tree_list (NULL_TREE,
20281                                        cp_parser_identifier (parser)));
20282       sep = cp_lexer_peek_token (parser->lexer);
20283     }
20284
20285   return list;
20286 }
20287
20288 /* Parse an Objective-C alias declaration.
20289
20290    objc-alias-declaration:
20291      @compatibility_alias identifier identifier ;
20292
20293    This function registers the alias mapping with the Objective-C front end.
20294    It returns nothing.  */
20295
20296 static void
20297 cp_parser_objc_alias_declaration (cp_parser* parser)
20298 {
20299   tree alias, orig;
20300
20301   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
20302   alias = cp_parser_identifier (parser);
20303   orig = cp_parser_identifier (parser);
20304   objc_declare_alias (alias, orig);
20305   cp_parser_consume_semicolon_at_end_of_statement (parser);
20306 }
20307
20308 /* Parse an Objective-C class forward-declaration.
20309
20310    objc-class-declaration:
20311      @class objc-identifier-list ;
20312
20313    The function registers the forward declarations with the Objective-C
20314    front end.  It returns nothing.  */
20315
20316 static void
20317 cp_parser_objc_class_declaration (cp_parser* parser)
20318 {
20319   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
20320   objc_declare_class (cp_parser_objc_identifier_list (parser));
20321   cp_parser_consume_semicolon_at_end_of_statement (parser);
20322 }
20323
20324 /* Parse a list of Objective-C protocol references.
20325
20326    objc-protocol-refs-opt:
20327      objc-protocol-refs [opt]
20328
20329    objc-protocol-refs:
20330      < objc-identifier-list >
20331
20332    Returns a TREE_LIST of identifiers, if any.  */
20333
20334 static tree
20335 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
20336 {
20337   tree protorefs = NULL_TREE;
20338
20339   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
20340     {
20341       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
20342       protorefs = cp_parser_objc_identifier_list (parser);
20343       cp_parser_require (parser, CPP_GREATER, "%<>%>");
20344     }
20345
20346   return protorefs;
20347 }
20348
20349 /* Parse a Objective-C visibility specification.  */
20350
20351 static void
20352 cp_parser_objc_visibility_spec (cp_parser* parser)
20353 {
20354   cp_token *vis = cp_lexer_peek_token (parser->lexer);
20355
20356   switch (vis->keyword)
20357     {
20358     case RID_AT_PRIVATE:
20359       objc_set_visibility (2);
20360       break;
20361     case RID_AT_PROTECTED:
20362       objc_set_visibility (0);
20363       break;
20364     case RID_AT_PUBLIC:
20365       objc_set_visibility (1);
20366       break;
20367     default:
20368       return;
20369     }
20370
20371   /* Eat '@private'/'@protected'/'@public'.  */
20372   cp_lexer_consume_token (parser->lexer);
20373 }
20374
20375 /* Parse an Objective-C method type.  */
20376
20377 static void
20378 cp_parser_objc_method_type (cp_parser* parser)
20379 {
20380   objc_set_method_type
20381    (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS
20382     ? PLUS_EXPR
20383     : MINUS_EXPR);
20384 }
20385
20386 /* Parse an Objective-C protocol qualifier.  */
20387
20388 static tree
20389 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
20390 {
20391   tree quals = NULL_TREE, node;
20392   cp_token *token = cp_lexer_peek_token (parser->lexer);
20393
20394   node = token->u.value;
20395
20396   while (node && TREE_CODE (node) == IDENTIFIER_NODE
20397          && (node == ridpointers [(int) RID_IN]
20398              || node == ridpointers [(int) RID_OUT]
20399              || node == ridpointers [(int) RID_INOUT]
20400              || node == ridpointers [(int) RID_BYCOPY]
20401              || node == ridpointers [(int) RID_BYREF]
20402              || node == ridpointers [(int) RID_ONEWAY]))
20403     {
20404       quals = tree_cons (NULL_TREE, node, quals);
20405       cp_lexer_consume_token (parser->lexer);
20406       token = cp_lexer_peek_token (parser->lexer);
20407       node = token->u.value;
20408     }
20409
20410   return quals;
20411 }
20412
20413 /* Parse an Objective-C typename.  */
20414
20415 static tree
20416 cp_parser_objc_typename (cp_parser* parser)
20417 {
20418   tree type_name = NULL_TREE;
20419
20420   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20421     {
20422       tree proto_quals, cp_type = NULL_TREE;
20423
20424       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20425       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
20426
20427       /* An ObjC type name may consist of just protocol qualifiers, in which
20428          case the type shall default to 'id'.  */
20429       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
20430         cp_type = cp_parser_type_id (parser);
20431
20432       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20433       type_name = build_tree_list (proto_quals, cp_type);
20434     }
20435
20436   return type_name;
20437 }
20438
20439 /* Check to see if TYPE refers to an Objective-C selector name.  */
20440
20441 static bool
20442 cp_parser_objc_selector_p (enum cpp_ttype type)
20443 {
20444   return (type == CPP_NAME || type == CPP_KEYWORD
20445           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
20446           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
20447           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
20448           || type == CPP_XOR || type == CPP_XOR_EQ);
20449 }
20450
20451 /* Parse an Objective-C selector.  */
20452
20453 static tree
20454 cp_parser_objc_selector (cp_parser* parser)
20455 {
20456   cp_token *token = cp_lexer_consume_token (parser->lexer);
20457
20458   if (!cp_parser_objc_selector_p (token->type))
20459     {
20460       error_at (token->location, "invalid Objective-C++ selector name");
20461       return error_mark_node;
20462     }
20463
20464   /* C++ operator names are allowed to appear in ObjC selectors.  */
20465   switch (token->type)
20466     {
20467     case CPP_AND_AND: return get_identifier ("and");
20468     case CPP_AND_EQ: return get_identifier ("and_eq");
20469     case CPP_AND: return get_identifier ("bitand");
20470     case CPP_OR: return get_identifier ("bitor");
20471     case CPP_COMPL: return get_identifier ("compl");
20472     case CPP_NOT: return get_identifier ("not");
20473     case CPP_NOT_EQ: return get_identifier ("not_eq");
20474     case CPP_OR_OR: return get_identifier ("or");
20475     case CPP_OR_EQ: return get_identifier ("or_eq");
20476     case CPP_XOR: return get_identifier ("xor");
20477     case CPP_XOR_EQ: return get_identifier ("xor_eq");
20478     default: return token->u.value;
20479     }
20480 }
20481
20482 /* Parse an Objective-C params list.  */
20483
20484 static tree
20485 cp_parser_objc_method_keyword_params (cp_parser* parser)
20486 {
20487   tree params = NULL_TREE;
20488   bool maybe_unary_selector_p = true;
20489   cp_token *token = cp_lexer_peek_token (parser->lexer);
20490
20491   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
20492     {
20493       tree selector = NULL_TREE, type_name, identifier;
20494
20495       if (token->type != CPP_COLON)
20496         selector = cp_parser_objc_selector (parser);
20497
20498       /* Detect if we have a unary selector.  */
20499       if (maybe_unary_selector_p
20500           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
20501         return selector;
20502
20503       maybe_unary_selector_p = false;
20504       cp_parser_require (parser, CPP_COLON, "%<:%>");
20505       type_name = cp_parser_objc_typename (parser);
20506       identifier = cp_parser_identifier (parser);
20507
20508       params
20509         = chainon (params,
20510                    objc_build_keyword_decl (selector,
20511                                             type_name,
20512                                             identifier));
20513
20514       token = cp_lexer_peek_token (parser->lexer);
20515     }
20516
20517   return params;
20518 }
20519
20520 /* Parse the non-keyword Objective-C params.  */
20521
20522 static tree
20523 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp)
20524 {
20525   tree params = make_node (TREE_LIST);
20526   cp_token *token = cp_lexer_peek_token (parser->lexer);
20527   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
20528
20529   while (token->type == CPP_COMMA)
20530     {
20531       cp_parameter_declarator *parmdecl;
20532       tree parm;
20533
20534       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20535       token = cp_lexer_peek_token (parser->lexer);
20536
20537       if (token->type == CPP_ELLIPSIS)
20538         {
20539           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
20540           *ellipsisp = true;
20541           break;
20542         }
20543
20544       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20545       parm = grokdeclarator (parmdecl->declarator,
20546                              &parmdecl->decl_specifiers,
20547                              PARM, /*initialized=*/0,
20548                              /*attrlist=*/NULL);
20549
20550       chainon (params, build_tree_list (NULL_TREE, parm));
20551       token = cp_lexer_peek_token (parser->lexer);
20552     }
20553
20554   return params;
20555 }
20556
20557 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
20558
20559 static void
20560 cp_parser_objc_interstitial_code (cp_parser* parser)
20561 {
20562   cp_token *token = cp_lexer_peek_token (parser->lexer);
20563
20564   /* If the next token is `extern' and the following token is a string
20565      literal, then we have a linkage specification.  */
20566   if (token->keyword == RID_EXTERN
20567       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
20568     cp_parser_linkage_specification (parser);
20569   /* Handle #pragma, if any.  */
20570   else if (token->type == CPP_PRAGMA)
20571     cp_parser_pragma (parser, pragma_external);
20572   /* Allow stray semicolons.  */
20573   else if (token->type == CPP_SEMICOLON)
20574     cp_lexer_consume_token (parser->lexer);
20575   /* Finally, try to parse a block-declaration, or a function-definition.  */
20576   else
20577     cp_parser_block_declaration (parser, /*statement_p=*/false);
20578 }
20579
20580 /* Parse a method signature.  */
20581
20582 static tree
20583 cp_parser_objc_method_signature (cp_parser* parser)
20584 {
20585   tree rettype, kwdparms, optparms;
20586   bool ellipsis = false;
20587
20588   cp_parser_objc_method_type (parser);
20589   rettype = cp_parser_objc_typename (parser);
20590   kwdparms = cp_parser_objc_method_keyword_params (parser);
20591   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis);
20592
20593   return objc_build_method_signature (rettype, kwdparms, optparms, ellipsis);
20594 }
20595
20596 /* Pars an Objective-C method prototype list.  */
20597
20598 static void
20599 cp_parser_objc_method_prototype_list (cp_parser* parser)
20600 {
20601   cp_token *token = cp_lexer_peek_token (parser->lexer);
20602
20603   while (token->keyword != RID_AT_END)
20604     {
20605       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20606         {
20607           objc_add_method_declaration
20608            (cp_parser_objc_method_signature (parser));
20609           cp_parser_consume_semicolon_at_end_of_statement (parser);
20610         }
20611       else
20612         /* Allow for interspersed non-ObjC++ code.  */
20613         cp_parser_objc_interstitial_code (parser);
20614
20615       token = cp_lexer_peek_token (parser->lexer);
20616     }
20617
20618   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20619   objc_finish_interface ();
20620 }
20621
20622 /* Parse an Objective-C method definition list.  */
20623
20624 static void
20625 cp_parser_objc_method_definition_list (cp_parser* parser)
20626 {
20627   cp_token *token = cp_lexer_peek_token (parser->lexer);
20628
20629   while (token->keyword != RID_AT_END)
20630     {
20631       tree meth;
20632
20633       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
20634         {
20635           push_deferring_access_checks (dk_deferred);
20636           objc_start_method_definition
20637            (cp_parser_objc_method_signature (parser));
20638
20639           /* For historical reasons, we accept an optional semicolon.  */
20640           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20641             cp_lexer_consume_token (parser->lexer);
20642
20643           perform_deferred_access_checks ();
20644           stop_deferring_access_checks ();
20645           meth = cp_parser_function_definition_after_declarator (parser,
20646                                                                  false);
20647           pop_deferring_access_checks ();
20648           objc_finish_method_definition (meth);
20649         }
20650       else
20651         /* Allow for interspersed non-ObjC++ code.  */
20652         cp_parser_objc_interstitial_code (parser);
20653
20654       token = cp_lexer_peek_token (parser->lexer);
20655     }
20656
20657   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20658   objc_finish_implementation ();
20659 }
20660
20661 /* Parse Objective-C ivars.  */
20662
20663 static void
20664 cp_parser_objc_class_ivars (cp_parser* parser)
20665 {
20666   cp_token *token = cp_lexer_peek_token (parser->lexer);
20667
20668   if (token->type != CPP_OPEN_BRACE)
20669     return;     /* No ivars specified.  */
20670
20671   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
20672   token = cp_lexer_peek_token (parser->lexer);
20673
20674   while (token->type != CPP_CLOSE_BRACE)
20675     {
20676       cp_decl_specifier_seq declspecs;
20677       int decl_class_or_enum_p;
20678       tree prefix_attributes;
20679
20680       cp_parser_objc_visibility_spec (parser);
20681
20682       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
20683         break;
20684
20685       cp_parser_decl_specifier_seq (parser,
20686                                     CP_PARSER_FLAGS_OPTIONAL,
20687                                     &declspecs,
20688                                     &decl_class_or_enum_p);
20689       prefix_attributes = declspecs.attributes;
20690       declspecs.attributes = NULL_TREE;
20691
20692       /* Keep going until we hit the `;' at the end of the
20693          declaration.  */
20694       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
20695         {
20696           tree width = NULL_TREE, attributes, first_attribute, decl;
20697           cp_declarator *declarator = NULL;
20698           int ctor_dtor_or_conv_p;
20699
20700           /* Check for a (possibly unnamed) bitfield declaration.  */
20701           token = cp_lexer_peek_token (parser->lexer);
20702           if (token->type == CPP_COLON)
20703             goto eat_colon;
20704
20705           if (token->type == CPP_NAME
20706               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
20707                   == CPP_COLON))
20708             {
20709               /* Get the name of the bitfield.  */
20710               declarator = make_id_declarator (NULL_TREE,
20711                                                cp_parser_identifier (parser),
20712                                                sfk_none);
20713
20714              eat_colon:
20715               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20716               /* Get the width of the bitfield.  */
20717               width
20718                 = cp_parser_constant_expression (parser,
20719                                                  /*allow_non_constant=*/false,
20720                                                  NULL);
20721             }
20722           else
20723             {
20724               /* Parse the declarator.  */
20725               declarator
20726                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
20727                                         &ctor_dtor_or_conv_p,
20728                                         /*parenthesized_p=*/NULL,
20729                                         /*member_p=*/false);
20730             }
20731
20732           /* Look for attributes that apply to the ivar.  */
20733           attributes = cp_parser_attributes_opt (parser);
20734           /* Remember which attributes are prefix attributes and
20735              which are not.  */
20736           first_attribute = attributes;
20737           /* Combine the attributes.  */
20738           attributes = chainon (prefix_attributes, attributes);
20739
20740           if (width)
20741               /* Create the bitfield declaration.  */
20742               decl = grokbitfield (declarator, &declspecs,
20743                                    width,
20744                                    attributes);
20745           else
20746             decl = grokfield (declarator, &declspecs,
20747                               NULL_TREE, /*init_const_expr_p=*/false,
20748                               NULL_TREE, attributes);
20749
20750           /* Add the instance variable.  */
20751           objc_add_instance_variable (decl);
20752
20753           /* Reset PREFIX_ATTRIBUTES.  */
20754           while (attributes && TREE_CHAIN (attributes) != first_attribute)
20755             attributes = TREE_CHAIN (attributes);
20756           if (attributes)
20757             TREE_CHAIN (attributes) = NULL_TREE;
20758
20759           token = cp_lexer_peek_token (parser->lexer);
20760
20761           if (token->type == CPP_COMMA)
20762             {
20763               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
20764               continue;
20765             }
20766           break;
20767         }
20768
20769       cp_parser_consume_semicolon_at_end_of_statement (parser);
20770       token = cp_lexer_peek_token (parser->lexer);
20771     }
20772
20773   cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
20774   /* For historical reasons, we accept an optional semicolon.  */
20775   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20776     cp_lexer_consume_token (parser->lexer);
20777 }
20778
20779 /* Parse an Objective-C protocol declaration.  */
20780
20781 static void
20782 cp_parser_objc_protocol_declaration (cp_parser* parser)
20783 {
20784   tree proto, protorefs;
20785   cp_token *tok;
20786
20787   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
20788   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
20789     {
20790       tok = cp_lexer_peek_token (parser->lexer);
20791       error_at (tok->location, "identifier expected after %<@protocol%>");
20792       goto finish;
20793     }
20794
20795   /* See if we have a forward declaration or a definition.  */
20796   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
20797
20798   /* Try a forward declaration first.  */
20799   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
20800     {
20801       objc_declare_protocols (cp_parser_objc_identifier_list (parser));
20802      finish:
20803       cp_parser_consume_semicolon_at_end_of_statement (parser);
20804     }
20805
20806   /* Ok, we got a full-fledged definition (or at least should).  */
20807   else
20808     {
20809       proto = cp_parser_identifier (parser);
20810       protorefs = cp_parser_objc_protocol_refs_opt (parser);
20811       objc_start_protocol (proto, protorefs);
20812       cp_parser_objc_method_prototype_list (parser);
20813     }
20814 }
20815
20816 /* Parse an Objective-C superclass or category.  */
20817
20818 static void
20819 cp_parser_objc_superclass_or_category (cp_parser *parser, tree *super,
20820                                                           tree *categ)
20821 {
20822   cp_token *next = cp_lexer_peek_token (parser->lexer);
20823
20824   *super = *categ = NULL_TREE;
20825   if (next->type == CPP_COLON)
20826     {
20827       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
20828       *super = cp_parser_identifier (parser);
20829     }
20830   else if (next->type == CPP_OPEN_PAREN)
20831     {
20832       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
20833       *categ = cp_parser_identifier (parser);
20834       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20835     }
20836 }
20837
20838 /* Parse an Objective-C class interface.  */
20839
20840 static void
20841 cp_parser_objc_class_interface (cp_parser* parser)
20842 {
20843   tree name, super, categ, protos;
20844
20845   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
20846   name = cp_parser_identifier (parser);
20847   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20848   protos = cp_parser_objc_protocol_refs_opt (parser);
20849
20850   /* We have either a class or a category on our hands.  */
20851   if (categ)
20852     objc_start_category_interface (name, categ, protos);
20853   else
20854     {
20855       objc_start_class_interface (name, super, protos);
20856       /* Handle instance variable declarations, if any.  */
20857       cp_parser_objc_class_ivars (parser);
20858       objc_continue_interface ();
20859     }
20860
20861   cp_parser_objc_method_prototype_list (parser);
20862 }
20863
20864 /* Parse an Objective-C class implementation.  */
20865
20866 static void
20867 cp_parser_objc_class_implementation (cp_parser* parser)
20868 {
20869   tree name, super, categ;
20870
20871   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
20872   name = cp_parser_identifier (parser);
20873   cp_parser_objc_superclass_or_category (parser, &super, &categ);
20874
20875   /* We have either a class or a category on our hands.  */
20876   if (categ)
20877     objc_start_category_implementation (name, categ);
20878   else
20879     {
20880       objc_start_class_implementation (name, super);
20881       /* Handle instance variable declarations, if any.  */
20882       cp_parser_objc_class_ivars (parser);
20883       objc_continue_implementation ();
20884     }
20885
20886   cp_parser_objc_method_definition_list (parser);
20887 }
20888
20889 /* Consume the @end token and finish off the implementation.  */
20890
20891 static void
20892 cp_parser_objc_end_implementation (cp_parser* parser)
20893 {
20894   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
20895   objc_finish_implementation ();
20896 }
20897
20898 /* Parse an Objective-C declaration.  */
20899
20900 static void
20901 cp_parser_objc_declaration (cp_parser* parser)
20902 {
20903   /* Try to figure out what kind of declaration is present.  */
20904   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
20905
20906   switch (kwd->keyword)
20907     {
20908     case RID_AT_ALIAS:
20909       cp_parser_objc_alias_declaration (parser);
20910       break;
20911     case RID_AT_CLASS:
20912       cp_parser_objc_class_declaration (parser);
20913       break;
20914     case RID_AT_PROTOCOL:
20915       cp_parser_objc_protocol_declaration (parser);
20916       break;
20917     case RID_AT_INTERFACE:
20918       cp_parser_objc_class_interface (parser);
20919       break;
20920     case RID_AT_IMPLEMENTATION:
20921       cp_parser_objc_class_implementation (parser);
20922       break;
20923     case RID_AT_END:
20924       cp_parser_objc_end_implementation (parser);
20925       break;
20926     default:
20927       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
20928                 kwd->u.value);
20929       cp_parser_skip_to_end_of_block_or_statement (parser);
20930     }
20931 }
20932
20933 /* Parse an Objective-C try-catch-finally statement.
20934
20935    objc-try-catch-finally-stmt:
20936      @try compound-statement objc-catch-clause-seq [opt]
20937        objc-finally-clause [opt]
20938
20939    objc-catch-clause-seq:
20940      objc-catch-clause objc-catch-clause-seq [opt]
20941
20942    objc-catch-clause:
20943      @catch ( exception-declaration ) compound-statement
20944
20945    objc-finally-clause
20946      @finally compound-statement
20947
20948    Returns NULL_TREE.  */
20949
20950 static tree
20951 cp_parser_objc_try_catch_finally_statement (cp_parser *parser) {
20952   location_t location;
20953   tree stmt;
20954
20955   cp_parser_require_keyword (parser, RID_AT_TRY, "%<@try%>");
20956   location = cp_lexer_peek_token (parser->lexer)->location;
20957   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
20958      node, lest it get absorbed into the surrounding block.  */
20959   stmt = push_stmt_list ();
20960   cp_parser_compound_statement (parser, NULL, false);
20961   objc_begin_try_stmt (location, pop_stmt_list (stmt));
20962
20963   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
20964     {
20965       cp_parameter_declarator *parmdecl;
20966       tree parm;
20967
20968       cp_lexer_consume_token (parser->lexer);
20969       cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
20970       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
20971       parm = grokdeclarator (parmdecl->declarator,
20972                              &parmdecl->decl_specifiers,
20973                              PARM, /*initialized=*/0,
20974                              /*attrlist=*/NULL);
20975       cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
20976       objc_begin_catch_clause (parm);
20977       cp_parser_compound_statement (parser, NULL, false);
20978       objc_finish_catch_clause ();
20979     }
20980
20981   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
20982     {
20983       cp_lexer_consume_token (parser->lexer);
20984       location = cp_lexer_peek_token (parser->lexer)->location;
20985       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
20986          node, lest it get absorbed into the surrounding block.  */
20987       stmt = push_stmt_list ();
20988       cp_parser_compound_statement (parser, NULL, false);
20989       objc_build_finally_clause (location, pop_stmt_list (stmt));
20990     }
20991
20992   return objc_finish_try_stmt ();
20993 }
20994
20995 /* Parse an Objective-C synchronized statement.
20996
20997    objc-synchronized-stmt:
20998      @synchronized ( expression ) compound-statement
20999
21000    Returns NULL_TREE.  */
21001
21002 static tree
21003 cp_parser_objc_synchronized_statement (cp_parser *parser) {
21004   location_t location;
21005   tree lock, stmt;
21006
21007   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, "%<@synchronized%>");
21008
21009   location = cp_lexer_peek_token (parser->lexer)->location;
21010   cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>");
21011   lock = cp_parser_expression (parser, false, NULL);
21012   cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>");
21013
21014   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
21015      node, lest it get absorbed into the surrounding block.  */
21016   stmt = push_stmt_list ();
21017   cp_parser_compound_statement (parser, NULL, false);
21018
21019   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
21020 }
21021
21022 /* Parse an Objective-C throw statement.
21023
21024    objc-throw-stmt:
21025      @throw assignment-expression [opt] ;
21026
21027    Returns a constructed '@throw' statement.  */
21028
21029 static tree
21030 cp_parser_objc_throw_statement (cp_parser *parser) {
21031   tree expr = NULL_TREE;
21032   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21033
21034   cp_parser_require_keyword (parser, RID_AT_THROW, "%<@throw%>");
21035
21036   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21037     expr = cp_parser_assignment_expression (parser, false, NULL);
21038
21039   cp_parser_consume_semicolon_at_end_of_statement (parser);
21040
21041   return objc_build_throw_stmt (loc, expr);
21042 }
21043
21044 /* Parse an Objective-C statement.  */
21045
21046 static tree
21047 cp_parser_objc_statement (cp_parser * parser) {
21048   /* Try to figure out what kind of declaration is present.  */
21049   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21050
21051   switch (kwd->keyword)
21052     {
21053     case RID_AT_TRY:
21054       return cp_parser_objc_try_catch_finally_statement (parser);
21055     case RID_AT_SYNCHRONIZED:
21056       return cp_parser_objc_synchronized_statement (parser);
21057     case RID_AT_THROW:
21058       return cp_parser_objc_throw_statement (parser);
21059     default:
21060       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
21061                kwd->u.value);
21062       cp_parser_skip_to_end_of_block_or_statement (parser);
21063     }
21064
21065   return error_mark_node;
21066 }
21067 \f
21068 /* OpenMP 2.5 parsing routines.  */
21069
21070 /* Returns name of the next clause.
21071    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
21072    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
21073    returned and the token is consumed.  */
21074
21075 static pragma_omp_clause
21076 cp_parser_omp_clause_name (cp_parser *parser)
21077 {
21078   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
21079
21080   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
21081     result = PRAGMA_OMP_CLAUSE_IF;
21082   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
21083     result = PRAGMA_OMP_CLAUSE_DEFAULT;
21084   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
21085     result = PRAGMA_OMP_CLAUSE_PRIVATE;
21086   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21087     {
21088       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21089       const char *p = IDENTIFIER_POINTER (id);
21090
21091       switch (p[0])
21092         {
21093         case 'c':
21094           if (!strcmp ("collapse", p))
21095             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
21096           else if (!strcmp ("copyin", p))
21097             result = PRAGMA_OMP_CLAUSE_COPYIN;
21098           else if (!strcmp ("copyprivate", p))
21099             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
21100           break;
21101         case 'f':
21102           if (!strcmp ("firstprivate", p))
21103             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
21104           break;
21105         case 'l':
21106           if (!strcmp ("lastprivate", p))
21107             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
21108           break;
21109         case 'n':
21110           if (!strcmp ("nowait", p))
21111             result = PRAGMA_OMP_CLAUSE_NOWAIT;
21112           else if (!strcmp ("num_threads", p))
21113             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
21114           break;
21115         case 'o':
21116           if (!strcmp ("ordered", p))
21117             result = PRAGMA_OMP_CLAUSE_ORDERED;
21118           break;
21119         case 'r':
21120           if (!strcmp ("reduction", p))
21121             result = PRAGMA_OMP_CLAUSE_REDUCTION;
21122           break;
21123         case 's':
21124           if (!strcmp ("schedule", p))
21125             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
21126           else if (!strcmp ("shared", p))
21127             result = PRAGMA_OMP_CLAUSE_SHARED;
21128           break;
21129         case 'u':
21130           if (!strcmp ("untied", p))
21131             result = PRAGMA_OMP_CLAUSE_UNTIED;
21132           break;
21133         }
21134     }
21135
21136   if (result != PRAGMA_OMP_CLAUSE_NONE)
21137     cp_lexer_consume_token (parser->lexer);
21138
21139   return result;
21140 }
21141
21142 /* Validate that a clause of the given type does not already exist.  */
21143
21144 static void
21145 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
21146                            const char *name, location_t location)
21147 {
21148   tree c;
21149
21150   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
21151     if (OMP_CLAUSE_CODE (c) == code)
21152       {
21153         error_at (location, "too many %qs clauses", name);
21154         break;
21155       }
21156 }
21157
21158 /* OpenMP 2.5:
21159    variable-list:
21160      identifier
21161      variable-list , identifier
21162
21163    In addition, we match a closing parenthesis.  An opening parenthesis
21164    will have been consumed by the caller.
21165
21166    If KIND is nonzero, create the appropriate node and install the decl
21167    in OMP_CLAUSE_DECL and add the node to the head of the list.
21168
21169    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
21170    return the list created.  */
21171
21172 static tree
21173 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
21174                                 tree list)
21175 {
21176   cp_token *token;
21177   while (1)
21178     {
21179       tree name, decl;
21180
21181       token = cp_lexer_peek_token (parser->lexer);
21182       name = cp_parser_id_expression (parser, /*template_p=*/false,
21183                                       /*check_dependency_p=*/true,
21184                                       /*template_p=*/NULL,
21185                                       /*declarator_p=*/false,
21186                                       /*optional_p=*/false);
21187       if (name == error_mark_node)
21188         goto skip_comma;
21189
21190       decl = cp_parser_lookup_name_simple (parser, name, token->location);
21191       if (decl == error_mark_node)
21192         cp_parser_name_lookup_error (parser, name, decl, NULL, token->location);
21193       else if (kind != 0)
21194         {
21195           tree u = build_omp_clause (token->location, kind);
21196           OMP_CLAUSE_DECL (u) = decl;
21197           OMP_CLAUSE_CHAIN (u) = list;
21198           list = u;
21199         }
21200       else
21201         list = tree_cons (decl, NULL_TREE, list);
21202
21203     get_comma:
21204       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
21205         break;
21206       cp_lexer_consume_token (parser->lexer);
21207     }
21208
21209   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21210     {
21211       int ending;
21212
21213       /* Try to resync to an unnested comma.  Copied from
21214          cp_parser_parenthesized_expression_list.  */
21215     skip_comma:
21216       ending = cp_parser_skip_to_closing_parenthesis (parser,
21217                                                       /*recovering=*/true,
21218                                                       /*or_comma=*/true,
21219                                                       /*consume_paren=*/true);
21220       if (ending < 0)
21221         goto get_comma;
21222     }
21223
21224   return list;
21225 }
21226
21227 /* Similarly, but expect leading and trailing parenthesis.  This is a very
21228    common case for omp clauses.  */
21229
21230 static tree
21231 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
21232 {
21233   if (cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21234     return cp_parser_omp_var_list_no_open (parser, kind, list);
21235   return list;
21236 }
21237
21238 /* OpenMP 3.0:
21239    collapse ( constant-expression ) */
21240
21241 static tree
21242 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
21243 {
21244   tree c, num;
21245   location_t loc;
21246   HOST_WIDE_INT n;
21247
21248   loc = cp_lexer_peek_token (parser->lexer)->location;
21249   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21250     return list;
21251
21252   num = cp_parser_constant_expression (parser, false, NULL);
21253
21254   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21255     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21256                                            /*or_comma=*/false,
21257                                            /*consume_paren=*/true);
21258
21259   if (num == error_mark_node)
21260     return list;
21261   num = fold_non_dependent_expr (num);
21262   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
21263       || !host_integerp (num, 0)
21264       || (n = tree_low_cst (num, 0)) <= 0
21265       || (int) n != n)
21266     {
21267       error_at (loc, "collapse argument needs positive constant integer expression");
21268       return list;
21269     }
21270
21271   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
21272   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
21273   OMP_CLAUSE_CHAIN (c) = list;
21274   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
21275
21276   return c;
21277 }
21278
21279 /* OpenMP 2.5:
21280    default ( shared | none ) */
21281
21282 static tree
21283 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
21284 {
21285   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
21286   tree c;
21287
21288   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21289     return list;
21290   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21291     {
21292       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21293       const char *p = IDENTIFIER_POINTER (id);
21294
21295       switch (p[0])
21296         {
21297         case 'n':
21298           if (strcmp ("none", p) != 0)
21299             goto invalid_kind;
21300           kind = OMP_CLAUSE_DEFAULT_NONE;
21301           break;
21302
21303         case 's':
21304           if (strcmp ("shared", p) != 0)
21305             goto invalid_kind;
21306           kind = OMP_CLAUSE_DEFAULT_SHARED;
21307           break;
21308
21309         default:
21310           goto invalid_kind;
21311         }
21312
21313       cp_lexer_consume_token (parser->lexer);
21314     }
21315   else
21316     {
21317     invalid_kind:
21318       cp_parser_error (parser, "expected %<none%> or %<shared%>");
21319     }
21320
21321   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21322     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21323                                            /*or_comma=*/false,
21324                                            /*consume_paren=*/true);
21325
21326   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
21327     return list;
21328
21329   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
21330   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
21331   OMP_CLAUSE_CHAIN (c) = list;
21332   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
21333
21334   return c;
21335 }
21336
21337 /* OpenMP 2.5:
21338    if ( expression ) */
21339
21340 static tree
21341 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
21342 {
21343   tree t, c;
21344
21345   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21346     return list;
21347
21348   t = cp_parser_condition (parser);
21349
21350   if (t == error_mark_node
21351       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21352     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21353                                            /*or_comma=*/false,
21354                                            /*consume_paren=*/true);
21355
21356   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
21357
21358   c = build_omp_clause (location, OMP_CLAUSE_IF);
21359   OMP_CLAUSE_IF_EXPR (c) = t;
21360   OMP_CLAUSE_CHAIN (c) = list;
21361
21362   return c;
21363 }
21364
21365 /* OpenMP 2.5:
21366    nowait */
21367
21368 static tree
21369 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
21370                              tree list, location_t location)
21371 {
21372   tree c;
21373
21374   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
21375
21376   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
21377   OMP_CLAUSE_CHAIN (c) = list;
21378   return c;
21379 }
21380
21381 /* OpenMP 2.5:
21382    num_threads ( expression ) */
21383
21384 static tree
21385 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
21386                                   location_t location)
21387 {
21388   tree t, c;
21389
21390   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21391     return list;
21392
21393   t = cp_parser_expression (parser, false, NULL);
21394
21395   if (t == error_mark_node
21396       || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21397     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21398                                            /*or_comma=*/false,
21399                                            /*consume_paren=*/true);
21400
21401   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
21402                              "num_threads", location);
21403
21404   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
21405   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
21406   OMP_CLAUSE_CHAIN (c) = list;
21407
21408   return c;
21409 }
21410
21411 /* OpenMP 2.5:
21412    ordered */
21413
21414 static tree
21415 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
21416                               tree list, location_t location)
21417 {
21418   tree c;
21419
21420   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
21421                              "ordered", location);
21422
21423   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
21424   OMP_CLAUSE_CHAIN (c) = list;
21425   return c;
21426 }
21427
21428 /* OpenMP 2.5:
21429    reduction ( reduction-operator : variable-list )
21430
21431    reduction-operator:
21432      One of: + * - & ^ | && || */
21433
21434 static tree
21435 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
21436 {
21437   enum tree_code code;
21438   tree nlist, c;
21439
21440   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21441     return list;
21442
21443   switch (cp_lexer_peek_token (parser->lexer)->type)
21444     {
21445     case CPP_PLUS:
21446       code = PLUS_EXPR;
21447       break;
21448     case CPP_MULT:
21449       code = MULT_EXPR;
21450       break;
21451     case CPP_MINUS:
21452       code = MINUS_EXPR;
21453       break;
21454     case CPP_AND:
21455       code = BIT_AND_EXPR;
21456       break;
21457     case CPP_XOR:
21458       code = BIT_XOR_EXPR;
21459       break;
21460     case CPP_OR:
21461       code = BIT_IOR_EXPR;
21462       break;
21463     case CPP_AND_AND:
21464       code = TRUTH_ANDIF_EXPR;
21465       break;
21466     case CPP_OR_OR:
21467       code = TRUTH_ORIF_EXPR;
21468       break;
21469     default:
21470       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
21471                                "%<|%>, %<&&%>, or %<||%>");
21472     resync_fail:
21473       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21474                                              /*or_comma=*/false,
21475                                              /*consume_paren=*/true);
21476       return list;
21477     }
21478   cp_lexer_consume_token (parser->lexer);
21479
21480   if (!cp_parser_require (parser, CPP_COLON, "%<:%>"))
21481     goto resync_fail;
21482
21483   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
21484   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
21485     OMP_CLAUSE_REDUCTION_CODE (c) = code;
21486
21487   return nlist;
21488 }
21489
21490 /* OpenMP 2.5:
21491    schedule ( schedule-kind )
21492    schedule ( schedule-kind , expression )
21493
21494    schedule-kind:
21495      static | dynamic | guided | runtime | auto  */
21496
21497 static tree
21498 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
21499 {
21500   tree c, t;
21501
21502   if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
21503     return list;
21504
21505   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
21506
21507   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
21508     {
21509       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
21510       const char *p = IDENTIFIER_POINTER (id);
21511
21512       switch (p[0])
21513         {
21514         case 'd':
21515           if (strcmp ("dynamic", p) != 0)
21516             goto invalid_kind;
21517           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
21518           break;
21519
21520         case 'g':
21521           if (strcmp ("guided", p) != 0)
21522             goto invalid_kind;
21523           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
21524           break;
21525
21526         case 'r':
21527           if (strcmp ("runtime", p) != 0)
21528             goto invalid_kind;
21529           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
21530           break;
21531
21532         default:
21533           goto invalid_kind;
21534         }
21535     }
21536   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
21537     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
21538   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
21539     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
21540   else
21541     goto invalid_kind;
21542   cp_lexer_consume_token (parser->lexer);
21543
21544   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21545     {
21546       cp_token *token;
21547       cp_lexer_consume_token (parser->lexer);
21548
21549       token = cp_lexer_peek_token (parser->lexer);
21550       t = cp_parser_assignment_expression (parser, false, NULL);
21551
21552       if (t == error_mark_node)
21553         goto resync_fail;
21554       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
21555         error_at (token->location, "schedule %<runtime%> does not take "
21556                   "a %<chunk_size%> parameter");
21557       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
21558         error_at (token->location, "schedule %<auto%> does not take "
21559                   "a %<chunk_size%> parameter");
21560       else
21561         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
21562
21563       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21564         goto resync_fail;
21565     }
21566   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<,%> or %<)%>"))
21567     goto resync_fail;
21568
21569   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
21570   OMP_CLAUSE_CHAIN (c) = list;
21571   return c;
21572
21573  invalid_kind:
21574   cp_parser_error (parser, "invalid schedule kind");
21575  resync_fail:
21576   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21577                                          /*or_comma=*/false,
21578                                          /*consume_paren=*/true);
21579   return list;
21580 }
21581
21582 /* OpenMP 3.0:
21583    untied */
21584
21585 static tree
21586 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
21587                              tree list, location_t location)
21588 {
21589   tree c;
21590
21591   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
21592
21593   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
21594   OMP_CLAUSE_CHAIN (c) = list;
21595   return c;
21596 }
21597
21598 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
21599    is a bitmask in MASK.  Return the list of clauses found; the result
21600    of clause default goes in *pdefault.  */
21601
21602 static tree
21603 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
21604                            const char *where, cp_token *pragma_tok)
21605 {
21606   tree clauses = NULL;
21607   bool first = true;
21608   cp_token *token = NULL;
21609
21610   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
21611     {
21612       pragma_omp_clause c_kind;
21613       const char *c_name;
21614       tree prev = clauses;
21615
21616       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21617         cp_lexer_consume_token (parser->lexer);
21618
21619       token = cp_lexer_peek_token (parser->lexer);
21620       c_kind = cp_parser_omp_clause_name (parser);
21621       first = false;
21622
21623       switch (c_kind)
21624         {
21625         case PRAGMA_OMP_CLAUSE_COLLAPSE:
21626           clauses = cp_parser_omp_clause_collapse (parser, clauses,
21627                                                    token->location);
21628           c_name = "collapse";
21629           break;
21630         case PRAGMA_OMP_CLAUSE_COPYIN:
21631           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
21632           c_name = "copyin";
21633           break;
21634         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
21635           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
21636                                             clauses);
21637           c_name = "copyprivate";
21638           break;
21639         case PRAGMA_OMP_CLAUSE_DEFAULT:
21640           clauses = cp_parser_omp_clause_default (parser, clauses,
21641                                                   token->location);
21642           c_name = "default";
21643           break;
21644         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
21645           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
21646                                             clauses);
21647           c_name = "firstprivate";
21648           break;
21649         case PRAGMA_OMP_CLAUSE_IF:
21650           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
21651           c_name = "if";
21652           break;
21653         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
21654           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
21655                                             clauses);
21656           c_name = "lastprivate";
21657           break;
21658         case PRAGMA_OMP_CLAUSE_NOWAIT:
21659           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
21660           c_name = "nowait";
21661           break;
21662         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
21663           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
21664                                                       token->location);
21665           c_name = "num_threads";
21666           break;
21667         case PRAGMA_OMP_CLAUSE_ORDERED:
21668           clauses = cp_parser_omp_clause_ordered (parser, clauses,
21669                                                   token->location);
21670           c_name = "ordered";
21671           break;
21672         case PRAGMA_OMP_CLAUSE_PRIVATE:
21673           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
21674                                             clauses);
21675           c_name = "private";
21676           break;
21677         case PRAGMA_OMP_CLAUSE_REDUCTION:
21678           clauses = cp_parser_omp_clause_reduction (parser, clauses);
21679           c_name = "reduction";
21680           break;
21681         case PRAGMA_OMP_CLAUSE_SCHEDULE:
21682           clauses = cp_parser_omp_clause_schedule (parser, clauses,
21683                                                    token->location);
21684           c_name = "schedule";
21685           break;
21686         case PRAGMA_OMP_CLAUSE_SHARED:
21687           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
21688                                             clauses);
21689           c_name = "shared";
21690           break;
21691         case PRAGMA_OMP_CLAUSE_UNTIED:
21692           clauses = cp_parser_omp_clause_untied (parser, clauses,
21693                                                  token->location);
21694           c_name = "nowait";
21695           break;
21696         default:
21697           cp_parser_error (parser, "expected %<#pragma omp%> clause");
21698           goto saw_error;
21699         }
21700
21701       if (((mask >> c_kind) & 1) == 0)
21702         {
21703           /* Remove the invalid clause(s) from the list to avoid
21704              confusing the rest of the compiler.  */
21705           clauses = prev;
21706           error_at (token->location, "%qs is not valid for %qs", c_name, where);
21707         }
21708     }
21709  saw_error:
21710   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
21711   return finish_omp_clauses (clauses);
21712 }
21713
21714 /* OpenMP 2.5:
21715    structured-block:
21716      statement
21717
21718    In practice, we're also interested in adding the statement to an
21719    outer node.  So it is convenient if we work around the fact that
21720    cp_parser_statement calls add_stmt.  */
21721
21722 static unsigned
21723 cp_parser_begin_omp_structured_block (cp_parser *parser)
21724 {
21725   unsigned save = parser->in_statement;
21726
21727   /* Only move the values to IN_OMP_BLOCK if they weren't false.
21728      This preserves the "not within loop or switch" style error messages
21729      for nonsense cases like
21730         void foo() {
21731         #pragma omp single
21732           break;
21733         }
21734   */
21735   if (parser->in_statement)
21736     parser->in_statement = IN_OMP_BLOCK;
21737
21738   return save;
21739 }
21740
21741 static void
21742 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
21743 {
21744   parser->in_statement = save;
21745 }
21746
21747 static tree
21748 cp_parser_omp_structured_block (cp_parser *parser)
21749 {
21750   tree stmt = begin_omp_structured_block ();
21751   unsigned int save = cp_parser_begin_omp_structured_block (parser);
21752
21753   cp_parser_statement (parser, NULL_TREE, false, NULL);
21754
21755   cp_parser_end_omp_structured_block (parser, save);
21756   return finish_omp_structured_block (stmt);
21757 }
21758
21759 /* OpenMP 2.5:
21760    # pragma omp atomic new-line
21761      expression-stmt
21762
21763    expression-stmt:
21764      x binop= expr | x++ | ++x | x-- | --x
21765    binop:
21766      +, *, -, /, &, ^, |, <<, >>
21767
21768   where x is an lvalue expression with scalar type.  */
21769
21770 static void
21771 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
21772 {
21773   tree lhs, rhs;
21774   enum tree_code code;
21775
21776   cp_parser_require_pragma_eol (parser, pragma_tok);
21777
21778   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
21779                                     /*cast_p=*/false, NULL);
21780   switch (TREE_CODE (lhs))
21781     {
21782     case ERROR_MARK:
21783       goto saw_error;
21784
21785     case PREINCREMENT_EXPR:
21786     case POSTINCREMENT_EXPR:
21787       lhs = TREE_OPERAND (lhs, 0);
21788       code = PLUS_EXPR;
21789       rhs = integer_one_node;
21790       break;
21791
21792     case PREDECREMENT_EXPR:
21793     case POSTDECREMENT_EXPR:
21794       lhs = TREE_OPERAND (lhs, 0);
21795       code = MINUS_EXPR;
21796       rhs = integer_one_node;
21797       break;
21798
21799     default:
21800       switch (cp_lexer_peek_token (parser->lexer)->type)
21801         {
21802         case CPP_MULT_EQ:
21803           code = MULT_EXPR;
21804           break;
21805         case CPP_DIV_EQ:
21806           code = TRUNC_DIV_EXPR;
21807           break;
21808         case CPP_PLUS_EQ:
21809           code = PLUS_EXPR;
21810           break;
21811         case CPP_MINUS_EQ:
21812           code = MINUS_EXPR;
21813           break;
21814         case CPP_LSHIFT_EQ:
21815           code = LSHIFT_EXPR;
21816           break;
21817         case CPP_RSHIFT_EQ:
21818           code = RSHIFT_EXPR;
21819           break;
21820         case CPP_AND_EQ:
21821           code = BIT_AND_EXPR;
21822           break;
21823         case CPP_OR_EQ:
21824           code = BIT_IOR_EXPR;
21825           break;
21826         case CPP_XOR_EQ:
21827           code = BIT_XOR_EXPR;
21828           break;
21829         default:
21830           cp_parser_error (parser,
21831                            "invalid operator for %<#pragma omp atomic%>");
21832           goto saw_error;
21833         }
21834       cp_lexer_consume_token (parser->lexer);
21835
21836       rhs = cp_parser_expression (parser, false, NULL);
21837       if (rhs == error_mark_node)
21838         goto saw_error;
21839       break;
21840     }
21841   finish_omp_atomic (code, lhs, rhs);
21842   cp_parser_consume_semicolon_at_end_of_statement (parser);
21843   return;
21844
21845  saw_error:
21846   cp_parser_skip_to_end_of_block_or_statement (parser);
21847 }
21848
21849
21850 /* OpenMP 2.5:
21851    # pragma omp barrier new-line  */
21852
21853 static void
21854 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
21855 {
21856   cp_parser_require_pragma_eol (parser, pragma_tok);
21857   finish_omp_barrier ();
21858 }
21859
21860 /* OpenMP 2.5:
21861    # pragma omp critical [(name)] new-line
21862      structured-block  */
21863
21864 static tree
21865 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
21866 {
21867   tree stmt, name = NULL;
21868
21869   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21870     {
21871       cp_lexer_consume_token (parser->lexer);
21872
21873       name = cp_parser_identifier (parser);
21874
21875       if (name == error_mark_node
21876           || !cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
21877         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
21878                                                /*or_comma=*/false,
21879                                                /*consume_paren=*/true);
21880       if (name == error_mark_node)
21881         name = NULL;
21882     }
21883   cp_parser_require_pragma_eol (parser, pragma_tok);
21884
21885   stmt = cp_parser_omp_structured_block (parser);
21886   return c_finish_omp_critical (input_location, stmt, name);
21887 }
21888
21889 /* OpenMP 2.5:
21890    # pragma omp flush flush-vars[opt] new-line
21891
21892    flush-vars:
21893      ( variable-list ) */
21894
21895 static void
21896 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
21897 {
21898   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21899     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
21900   cp_parser_require_pragma_eol (parser, pragma_tok);
21901
21902   finish_omp_flush ();
21903 }
21904
21905 /* Helper function, to parse omp for increment expression.  */
21906
21907 static tree
21908 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
21909 {
21910   tree cond = cp_parser_binary_expression (parser, false, true,
21911                                            PREC_NOT_OPERATOR, NULL);
21912   bool overloaded_p;
21913
21914   if (cond == error_mark_node
21915       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
21916     {
21917       cp_parser_skip_to_end_of_statement (parser);
21918       return error_mark_node;
21919     }
21920
21921   switch (TREE_CODE (cond))
21922     {
21923     case GT_EXPR:
21924     case GE_EXPR:
21925     case LT_EXPR:
21926     case LE_EXPR:
21927       break;
21928     default:
21929       return error_mark_node;
21930     }
21931
21932   /* If decl is an iterator, preserve LHS and RHS of the relational
21933      expr until finish_omp_for.  */
21934   if (decl
21935       && (type_dependent_expression_p (decl)
21936           || CLASS_TYPE_P (TREE_TYPE (decl))))
21937     return cond;
21938
21939   return build_x_binary_op (TREE_CODE (cond),
21940                             TREE_OPERAND (cond, 0), ERROR_MARK,
21941                             TREE_OPERAND (cond, 1), ERROR_MARK,
21942                             &overloaded_p, tf_warning_or_error);
21943 }
21944
21945 /* Helper function, to parse omp for increment expression.  */
21946
21947 static tree
21948 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
21949 {
21950   cp_token *token = cp_lexer_peek_token (parser->lexer);
21951   enum tree_code op;
21952   tree lhs, rhs;
21953   cp_id_kind idk;
21954   bool decl_first;
21955
21956   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21957     {
21958       op = (token->type == CPP_PLUS_PLUS
21959             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
21960       cp_lexer_consume_token (parser->lexer);
21961       lhs = cp_parser_cast_expression (parser, false, false, NULL);
21962       if (lhs != decl)
21963         return error_mark_node;
21964       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21965     }
21966
21967   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
21968   if (lhs != decl)
21969     return error_mark_node;
21970
21971   token = cp_lexer_peek_token (parser->lexer);
21972   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
21973     {
21974       op = (token->type == CPP_PLUS_PLUS
21975             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
21976       cp_lexer_consume_token (parser->lexer);
21977       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
21978     }
21979
21980   op = cp_parser_assignment_operator_opt (parser);
21981   if (op == ERROR_MARK)
21982     return error_mark_node;
21983
21984   if (op != NOP_EXPR)
21985     {
21986       rhs = cp_parser_assignment_expression (parser, false, NULL);
21987       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
21988       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
21989     }
21990
21991   lhs = cp_parser_binary_expression (parser, false, false,
21992                                      PREC_ADDITIVE_EXPRESSION, NULL);
21993   token = cp_lexer_peek_token (parser->lexer);
21994   decl_first = lhs == decl;
21995   if (decl_first)
21996     lhs = NULL_TREE;
21997   if (token->type != CPP_PLUS
21998       && token->type != CPP_MINUS)
21999     return error_mark_node;
22000
22001   do
22002     {
22003       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
22004       cp_lexer_consume_token (parser->lexer);
22005       rhs = cp_parser_binary_expression (parser, false, false,
22006                                          PREC_ADDITIVE_EXPRESSION, NULL);
22007       token = cp_lexer_peek_token (parser->lexer);
22008       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
22009         {
22010           if (lhs == NULL_TREE)
22011             {
22012               if (op == PLUS_EXPR)
22013                 lhs = rhs;
22014               else
22015                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
22016             }
22017           else
22018             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
22019                                      NULL, tf_warning_or_error);
22020         }
22021     }
22022   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
22023
22024   if (!decl_first)
22025     {
22026       if (rhs != decl || op == MINUS_EXPR)
22027         return error_mark_node;
22028       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
22029     }
22030   else
22031     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
22032
22033   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
22034 }
22035
22036 /* Parse the restricted form of the for statement allowed by OpenMP.  */
22037
22038 static tree
22039 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
22040 {
22041   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
22042   tree for_block = NULL_TREE, real_decl, initv, condv, incrv, declv;
22043   tree this_pre_body, cl;
22044   location_t loc_first;
22045   bool collapse_err = false;
22046   int i, collapse = 1, nbraces = 0;
22047
22048   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
22049     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
22050       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
22051
22052   gcc_assert (collapse >= 1);
22053
22054   declv = make_tree_vec (collapse);
22055   initv = make_tree_vec (collapse);
22056   condv = make_tree_vec (collapse);
22057   incrv = make_tree_vec (collapse);
22058
22059   loc_first = cp_lexer_peek_token (parser->lexer)->location;
22060
22061   for (i = 0; i < collapse; i++)
22062     {
22063       int bracecount = 0;
22064       bool add_private_clause = false;
22065       location_t loc;
22066
22067       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22068         {
22069           cp_parser_error (parser, "for statement expected");
22070           return NULL;
22071         }
22072       loc = cp_lexer_consume_token (parser->lexer)->location;
22073
22074       if (!cp_parser_require (parser, CPP_OPEN_PAREN, "%<(%>"))
22075         return NULL;
22076
22077       init = decl = real_decl = NULL;
22078       this_pre_body = push_stmt_list ();
22079       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22080         {
22081           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
22082
22083              init-expr:
22084                        var = lb
22085                        integer-type var = lb
22086                        random-access-iterator-type var = lb
22087                        pointer-type var = lb
22088           */
22089           cp_decl_specifier_seq type_specifiers;
22090
22091           /* First, try to parse as an initialized declaration.  See
22092              cp_parser_condition, from whence the bulk of this is copied.  */
22093
22094           cp_parser_parse_tentatively (parser);
22095           cp_parser_type_specifier_seq (parser, /*is_condition=*/false,
22096                                         /*is_trailing_return=*/false,
22097                                         &type_specifiers);
22098           if (cp_parser_parse_definitely (parser))
22099             {
22100               /* If parsing a type specifier seq succeeded, then this
22101                  MUST be a initialized declaration.  */
22102               tree asm_specification, attributes;
22103               cp_declarator *declarator;
22104
22105               declarator = cp_parser_declarator (parser,
22106                                                  CP_PARSER_DECLARATOR_NAMED,
22107                                                  /*ctor_dtor_or_conv_p=*/NULL,
22108                                                  /*parenthesized_p=*/NULL,
22109                                                  /*member_p=*/false);
22110               attributes = cp_parser_attributes_opt (parser);
22111               asm_specification = cp_parser_asm_specification_opt (parser);
22112
22113               if (declarator == cp_error_declarator) 
22114                 cp_parser_skip_to_end_of_statement (parser);
22115
22116               else 
22117                 {
22118                   tree pushed_scope, auto_node;
22119
22120                   decl = start_decl (declarator, &type_specifiers,
22121                                      SD_INITIALIZED, attributes,
22122                                      /*prefix_attributes=*/NULL_TREE,
22123                                      &pushed_scope);
22124
22125                   auto_node = type_uses_auto (TREE_TYPE (decl));
22126                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
22127                     {
22128                       if (cp_lexer_next_token_is (parser->lexer, 
22129                                                   CPP_OPEN_PAREN))
22130                         error ("parenthesized initialization is not allowed in "
22131                                "OpenMP %<for%> loop");
22132                       else
22133                         /* Trigger an error.  */
22134                         cp_parser_require (parser, CPP_EQ, "%<=%>");
22135
22136                       init = error_mark_node;
22137                       cp_parser_skip_to_end_of_statement (parser);
22138                     }
22139                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
22140                            || type_dependent_expression_p (decl)
22141                            || auto_node)
22142                     {
22143                       bool is_direct_init, is_non_constant_init;
22144
22145                       init = cp_parser_initializer (parser,
22146                                                     &is_direct_init,
22147                                                     &is_non_constant_init);
22148
22149                       if (auto_node && describable_type (init))
22150                         {
22151                           TREE_TYPE (decl)
22152                             = do_auto_deduction (TREE_TYPE (decl), init,
22153                                                  auto_node);
22154
22155                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
22156                               && !type_dependent_expression_p (decl))
22157                             goto non_class;
22158                         }
22159                       
22160                       cp_finish_decl (decl, init, !is_non_constant_init,
22161                                       asm_specification,
22162                                       LOOKUP_ONLYCONVERTING);
22163                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
22164                         {
22165                           for_block
22166                             = tree_cons (NULL, this_pre_body, for_block);
22167                           init = NULL_TREE;
22168                         }
22169                       else
22170                         init = pop_stmt_list (this_pre_body);
22171                       this_pre_body = NULL_TREE;
22172                     }
22173                   else
22174                     {
22175                       /* Consume '='.  */
22176                       cp_lexer_consume_token (parser->lexer);
22177                       init = cp_parser_assignment_expression (parser, false, NULL);
22178
22179                     non_class:
22180                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
22181                         init = error_mark_node;
22182                       else
22183                         cp_finish_decl (decl, NULL_TREE,
22184                                         /*init_const_expr_p=*/false,
22185                                         asm_specification,
22186                                         LOOKUP_ONLYCONVERTING);
22187                     }
22188
22189                   if (pushed_scope)
22190                     pop_scope (pushed_scope);
22191                 }
22192             }
22193           else 
22194             {
22195               cp_id_kind idk;
22196               /* If parsing a type specifier sequence failed, then
22197                  this MUST be a simple expression.  */
22198               cp_parser_parse_tentatively (parser);
22199               decl = cp_parser_primary_expression (parser, false, false,
22200                                                    false, &idk);
22201               if (!cp_parser_error_occurred (parser)
22202                   && decl
22203                   && DECL_P (decl)
22204                   && CLASS_TYPE_P (TREE_TYPE (decl)))
22205                 {
22206                   tree rhs;
22207
22208                   cp_parser_parse_definitely (parser);
22209                   cp_parser_require (parser, CPP_EQ, "%<=%>");
22210                   rhs = cp_parser_assignment_expression (parser, false, NULL);
22211                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
22212                                                          rhs,
22213                                                          tf_warning_or_error));
22214                   add_private_clause = true;
22215                 }
22216               else
22217                 {
22218                   decl = NULL;
22219                   cp_parser_abort_tentative_parse (parser);
22220                   init = cp_parser_expression (parser, false, NULL);
22221                   if (init)
22222                     {
22223                       if (TREE_CODE (init) == MODIFY_EXPR
22224                           || TREE_CODE (init) == MODOP_EXPR)
22225                         real_decl = TREE_OPERAND (init, 0);
22226                     }
22227                 }
22228             }
22229         }
22230       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22231       if (this_pre_body)
22232         {
22233           this_pre_body = pop_stmt_list (this_pre_body);
22234           if (pre_body)
22235             {
22236               tree t = pre_body;
22237               pre_body = push_stmt_list ();
22238               add_stmt (t);
22239               add_stmt (this_pre_body);
22240               pre_body = pop_stmt_list (pre_body);
22241             }
22242           else
22243             pre_body = this_pre_body;
22244         }
22245
22246       if (decl)
22247         real_decl = decl;
22248       if (par_clauses != NULL && real_decl != NULL_TREE)
22249         {
22250           tree *c;
22251           for (c = par_clauses; *c ; )
22252             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
22253                 && OMP_CLAUSE_DECL (*c) == real_decl)
22254               {
22255                 error_at (loc, "iteration variable %qD"
22256                           " should not be firstprivate", real_decl);
22257                 *c = OMP_CLAUSE_CHAIN (*c);
22258               }
22259             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
22260                      && OMP_CLAUSE_DECL (*c) == real_decl)
22261               {
22262                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
22263                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
22264                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
22265                 OMP_CLAUSE_DECL (l) = real_decl;
22266                 OMP_CLAUSE_CHAIN (l) = clauses;
22267                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
22268                 clauses = l;
22269                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
22270                 CP_OMP_CLAUSE_INFO (*c) = NULL;
22271                 add_private_clause = false;
22272               }
22273             else
22274               {
22275                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
22276                     && OMP_CLAUSE_DECL (*c) == real_decl)
22277                   add_private_clause = false;
22278                 c = &OMP_CLAUSE_CHAIN (*c);
22279               }
22280         }
22281
22282       if (add_private_clause)
22283         {
22284           tree c;
22285           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
22286             {
22287               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
22288                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
22289                   && OMP_CLAUSE_DECL (c) == decl)
22290                 break;
22291               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
22292                        && OMP_CLAUSE_DECL (c) == decl)
22293                 error_at (loc, "iteration variable %qD "
22294                           "should not be firstprivate",
22295                           decl);
22296               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
22297                        && OMP_CLAUSE_DECL (c) == decl)
22298                 error_at (loc, "iteration variable %qD should not be reduction",
22299                           decl);
22300             }
22301           if (c == NULL)
22302             {
22303               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
22304               OMP_CLAUSE_DECL (c) = decl;
22305               c = finish_omp_clauses (c);
22306               if (c)
22307                 {
22308                   OMP_CLAUSE_CHAIN (c) = clauses;
22309                   clauses = c;
22310                 }
22311             }
22312         }
22313
22314       cond = NULL;
22315       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22316         cond = cp_parser_omp_for_cond (parser, decl);
22317       cp_parser_require (parser, CPP_SEMICOLON, "%<;%>");
22318
22319       incr = NULL;
22320       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22321         {
22322           /* If decl is an iterator, preserve the operator on decl
22323              until finish_omp_for.  */
22324           if (decl
22325               && (type_dependent_expression_p (decl)
22326                   || CLASS_TYPE_P (TREE_TYPE (decl))))
22327             incr = cp_parser_omp_for_incr (parser, decl);
22328           else
22329             incr = cp_parser_expression (parser, false, NULL);
22330         }
22331
22332       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, "%<)%>"))
22333         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
22334                                                /*or_comma=*/false,
22335                                                /*consume_paren=*/true);
22336
22337       TREE_VEC_ELT (declv, i) = decl;
22338       TREE_VEC_ELT (initv, i) = init;
22339       TREE_VEC_ELT (condv, i) = cond;
22340       TREE_VEC_ELT (incrv, i) = incr;
22341
22342       if (i == collapse - 1)
22343         break;
22344
22345       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
22346          in between the collapsed for loops to be still considered perfectly
22347          nested.  Hopefully the final version clarifies this.
22348          For now handle (multiple) {'s and empty statements.  */
22349       cp_parser_parse_tentatively (parser);
22350       do
22351         {
22352           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22353             break;
22354           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22355             {
22356               cp_lexer_consume_token (parser->lexer);
22357               bracecount++;
22358             }
22359           else if (bracecount
22360                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22361             cp_lexer_consume_token (parser->lexer);
22362           else
22363             {
22364               loc = cp_lexer_peek_token (parser->lexer)->location;
22365               error_at (loc, "not enough collapsed for loops");
22366               collapse_err = true;
22367               cp_parser_abort_tentative_parse (parser);
22368               declv = NULL_TREE;
22369               break;
22370             }
22371         }
22372       while (1);
22373
22374       if (declv)
22375         {
22376           cp_parser_parse_definitely (parser);
22377           nbraces += bracecount;
22378         }
22379     }
22380
22381   /* Note that we saved the original contents of this flag when we entered
22382      the structured block, and so we don't need to re-save it here.  */
22383   parser->in_statement = IN_OMP_FOR;
22384
22385   /* Note that the grammar doesn't call for a structured block here,
22386      though the loop as a whole is a structured block.  */
22387   body = push_stmt_list ();
22388   cp_parser_statement (parser, NULL_TREE, false, NULL);
22389   body = pop_stmt_list (body);
22390
22391   if (declv == NULL_TREE)
22392     ret = NULL_TREE;
22393   else
22394     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
22395                           pre_body, clauses);
22396
22397   while (nbraces)
22398     {
22399       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22400         {
22401           cp_lexer_consume_token (parser->lexer);
22402           nbraces--;
22403         }
22404       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22405         cp_lexer_consume_token (parser->lexer);
22406       else
22407         {
22408           if (!collapse_err)
22409             {
22410               error_at (cp_lexer_peek_token (parser->lexer)->location,
22411                         "collapsed loops not perfectly nested");
22412             }
22413           collapse_err = true;
22414           cp_parser_statement_seq_opt (parser, NULL);
22415           cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22416         }
22417     }
22418
22419   while (for_block)
22420     {
22421       add_stmt (pop_stmt_list (TREE_VALUE (for_block)));
22422       for_block = TREE_CHAIN (for_block);
22423     }
22424
22425   return ret;
22426 }
22427
22428 /* OpenMP 2.5:
22429    #pragma omp for for-clause[optseq] new-line
22430      for-loop  */
22431
22432 #define OMP_FOR_CLAUSE_MASK                             \
22433         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22434         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22435         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22436         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22437         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
22438         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
22439         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
22440         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
22441
22442 static tree
22443 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
22444 {
22445   tree clauses, sb, ret;
22446   unsigned int save;
22447
22448   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
22449                                        "#pragma omp for", pragma_tok);
22450
22451   sb = begin_omp_structured_block ();
22452   save = cp_parser_begin_omp_structured_block (parser);
22453
22454   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
22455
22456   cp_parser_end_omp_structured_block (parser, save);
22457   add_stmt (finish_omp_structured_block (sb));
22458
22459   return ret;
22460 }
22461
22462 /* OpenMP 2.5:
22463    # pragma omp master new-line
22464      structured-block  */
22465
22466 static tree
22467 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
22468 {
22469   cp_parser_require_pragma_eol (parser, pragma_tok);
22470   return c_finish_omp_master (input_location,
22471                               cp_parser_omp_structured_block (parser));
22472 }
22473
22474 /* OpenMP 2.5:
22475    # pragma omp ordered new-line
22476      structured-block  */
22477
22478 static tree
22479 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
22480 {
22481   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22482   cp_parser_require_pragma_eol (parser, pragma_tok);
22483   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
22484 }
22485
22486 /* OpenMP 2.5:
22487
22488    section-scope:
22489      { section-sequence }
22490
22491    section-sequence:
22492      section-directive[opt] structured-block
22493      section-sequence section-directive structured-block  */
22494
22495 static tree
22496 cp_parser_omp_sections_scope (cp_parser *parser)
22497 {
22498   tree stmt, substmt;
22499   bool error_suppress = false;
22500   cp_token *tok;
22501
22502   if (!cp_parser_require (parser, CPP_OPEN_BRACE, "%<{%>"))
22503     return NULL_TREE;
22504
22505   stmt = push_stmt_list ();
22506
22507   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
22508     {
22509       unsigned save;
22510
22511       substmt = begin_omp_structured_block ();
22512       save = cp_parser_begin_omp_structured_block (parser);
22513
22514       while (1)
22515         {
22516           cp_parser_statement (parser, NULL_TREE, false, NULL);
22517
22518           tok = cp_lexer_peek_token (parser->lexer);
22519           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22520             break;
22521           if (tok->type == CPP_CLOSE_BRACE)
22522             break;
22523           if (tok->type == CPP_EOF)
22524             break;
22525         }
22526
22527       cp_parser_end_omp_structured_block (parser, save);
22528       substmt = finish_omp_structured_block (substmt);
22529       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22530       add_stmt (substmt);
22531     }
22532
22533   while (1)
22534     {
22535       tok = cp_lexer_peek_token (parser->lexer);
22536       if (tok->type == CPP_CLOSE_BRACE)
22537         break;
22538       if (tok->type == CPP_EOF)
22539         break;
22540
22541       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
22542         {
22543           cp_lexer_consume_token (parser->lexer);
22544           cp_parser_require_pragma_eol (parser, tok);
22545           error_suppress = false;
22546         }
22547       else if (!error_suppress)
22548         {
22549           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
22550           error_suppress = true;
22551         }
22552
22553       substmt = cp_parser_omp_structured_block (parser);
22554       substmt = build1 (OMP_SECTION, void_type_node, substmt);
22555       add_stmt (substmt);
22556     }
22557   cp_parser_require (parser, CPP_CLOSE_BRACE, "%<}%>");
22558
22559   substmt = pop_stmt_list (stmt);
22560
22561   stmt = make_node (OMP_SECTIONS);
22562   TREE_TYPE (stmt) = void_type_node;
22563   OMP_SECTIONS_BODY (stmt) = substmt;
22564
22565   add_stmt (stmt);
22566   return stmt;
22567 }
22568
22569 /* OpenMP 2.5:
22570    # pragma omp sections sections-clause[optseq] newline
22571      sections-scope  */
22572
22573 #define OMP_SECTIONS_CLAUSE_MASK                        \
22574         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22575         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22576         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
22577         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22578         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22579
22580 static tree
22581 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
22582 {
22583   tree clauses, ret;
22584
22585   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
22586                                        "#pragma omp sections", pragma_tok);
22587
22588   ret = cp_parser_omp_sections_scope (parser);
22589   if (ret)
22590     OMP_SECTIONS_CLAUSES (ret) = clauses;
22591
22592   return ret;
22593 }
22594
22595 /* OpenMP 2.5:
22596    # pragma parallel parallel-clause new-line
22597    # pragma parallel for parallel-for-clause new-line
22598    # pragma parallel sections parallel-sections-clause new-line  */
22599
22600 #define OMP_PARALLEL_CLAUSE_MASK                        \
22601         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22602         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22603         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22604         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22605         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
22606         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
22607         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
22608         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
22609
22610 static tree
22611 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
22612 {
22613   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
22614   const char *p_name = "#pragma omp parallel";
22615   tree stmt, clauses, par_clause, ws_clause, block;
22616   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
22617   unsigned int save;
22618   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22619
22620   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
22621     {
22622       cp_lexer_consume_token (parser->lexer);
22623       p_kind = PRAGMA_OMP_PARALLEL_FOR;
22624       p_name = "#pragma omp parallel for";
22625       mask |= OMP_FOR_CLAUSE_MASK;
22626       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22627     }
22628   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
22629     {
22630       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
22631       const char *p = IDENTIFIER_POINTER (id);
22632       if (strcmp (p, "sections") == 0)
22633         {
22634           cp_lexer_consume_token (parser->lexer);
22635           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
22636           p_name = "#pragma omp parallel sections";
22637           mask |= OMP_SECTIONS_CLAUSE_MASK;
22638           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
22639         }
22640     }
22641
22642   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
22643   block = begin_omp_parallel ();
22644   save = cp_parser_begin_omp_structured_block (parser);
22645
22646   switch (p_kind)
22647     {
22648     case PRAGMA_OMP_PARALLEL:
22649       cp_parser_statement (parser, NULL_TREE, false, NULL);
22650       par_clause = clauses;
22651       break;
22652
22653     case PRAGMA_OMP_PARALLEL_FOR:
22654       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22655       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
22656       break;
22657
22658     case PRAGMA_OMP_PARALLEL_SECTIONS:
22659       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
22660       stmt = cp_parser_omp_sections_scope (parser);
22661       if (stmt)
22662         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
22663       break;
22664
22665     default:
22666       gcc_unreachable ();
22667     }
22668
22669   cp_parser_end_omp_structured_block (parser, save);
22670   stmt = finish_omp_parallel (par_clause, block);
22671   if (p_kind != PRAGMA_OMP_PARALLEL)
22672     OMP_PARALLEL_COMBINED (stmt) = 1;
22673   return stmt;
22674 }
22675
22676 /* OpenMP 2.5:
22677    # pragma omp single single-clause[optseq] new-line
22678      structured-block  */
22679
22680 #define OMP_SINGLE_CLAUSE_MASK                          \
22681         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22682         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22683         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
22684         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
22685
22686 static tree
22687 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
22688 {
22689   tree stmt = make_node (OMP_SINGLE);
22690   TREE_TYPE (stmt) = void_type_node;
22691
22692   OMP_SINGLE_CLAUSES (stmt)
22693     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
22694                                  "#pragma omp single", pragma_tok);
22695   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
22696
22697   return add_stmt (stmt);
22698 }
22699
22700 /* OpenMP 3.0:
22701    # pragma omp task task-clause[optseq] new-line
22702      structured-block  */
22703
22704 #define OMP_TASK_CLAUSE_MASK                            \
22705         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
22706         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
22707         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
22708         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
22709         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
22710         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
22711
22712 static tree
22713 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
22714 {
22715   tree clauses, block;
22716   unsigned int save;
22717
22718   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
22719                                        "#pragma omp task", pragma_tok);
22720   block = begin_omp_task ();
22721   save = cp_parser_begin_omp_structured_block (parser);
22722   cp_parser_statement (parser, NULL_TREE, false, NULL);
22723   cp_parser_end_omp_structured_block (parser, save);
22724   return finish_omp_task (clauses, block);
22725 }
22726
22727 /* OpenMP 3.0:
22728    # pragma omp taskwait new-line  */
22729
22730 static void
22731 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
22732 {
22733   cp_parser_require_pragma_eol (parser, pragma_tok);
22734   finish_omp_taskwait ();
22735 }
22736
22737 /* OpenMP 2.5:
22738    # pragma omp threadprivate (variable-list) */
22739
22740 static void
22741 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
22742 {
22743   tree vars;
22744
22745   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
22746   cp_parser_require_pragma_eol (parser, pragma_tok);
22747
22748   finish_omp_threadprivate (vars);
22749 }
22750
22751 /* Main entry point to OpenMP statement pragmas.  */
22752
22753 static void
22754 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
22755 {
22756   tree stmt;
22757
22758   switch (pragma_tok->pragma_kind)
22759     {
22760     case PRAGMA_OMP_ATOMIC:
22761       cp_parser_omp_atomic (parser, pragma_tok);
22762       return;
22763     case PRAGMA_OMP_CRITICAL:
22764       stmt = cp_parser_omp_critical (parser, pragma_tok);
22765       break;
22766     case PRAGMA_OMP_FOR:
22767       stmt = cp_parser_omp_for (parser, pragma_tok);
22768       break;
22769     case PRAGMA_OMP_MASTER:
22770       stmt = cp_parser_omp_master (parser, pragma_tok);
22771       break;
22772     case PRAGMA_OMP_ORDERED:
22773       stmt = cp_parser_omp_ordered (parser, pragma_tok);
22774       break;
22775     case PRAGMA_OMP_PARALLEL:
22776       stmt = cp_parser_omp_parallel (parser, pragma_tok);
22777       break;
22778     case PRAGMA_OMP_SECTIONS:
22779       stmt = cp_parser_omp_sections (parser, pragma_tok);
22780       break;
22781     case PRAGMA_OMP_SINGLE:
22782       stmt = cp_parser_omp_single (parser, pragma_tok);
22783       break;
22784     case PRAGMA_OMP_TASK:
22785       stmt = cp_parser_omp_task (parser, pragma_tok);
22786       break;
22787     default:
22788       gcc_unreachable ();
22789     }
22790
22791   if (stmt)
22792     SET_EXPR_LOCATION (stmt, pragma_tok->location);
22793 }
22794 \f
22795 /* The parser.  */
22796
22797 static GTY (()) cp_parser *the_parser;
22798
22799 \f
22800 /* Special handling for the first token or line in the file.  The first
22801    thing in the file might be #pragma GCC pch_preprocess, which loads a
22802    PCH file, which is a GC collection point.  So we need to handle this
22803    first pragma without benefit of an existing lexer structure.
22804
22805    Always returns one token to the caller in *FIRST_TOKEN.  This is
22806    either the true first token of the file, or the first token after
22807    the initial pragma.  */
22808
22809 static void
22810 cp_parser_initial_pragma (cp_token *first_token)
22811 {
22812   tree name = NULL;
22813
22814   cp_lexer_get_preprocessor_token (NULL, first_token);
22815   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
22816     return;
22817
22818   cp_lexer_get_preprocessor_token (NULL, first_token);
22819   if (first_token->type == CPP_STRING)
22820     {
22821       name = first_token->u.value;
22822
22823       cp_lexer_get_preprocessor_token (NULL, first_token);
22824       if (first_token->type != CPP_PRAGMA_EOL)
22825         error_at (first_token->location,
22826                   "junk at end of %<#pragma GCC pch_preprocess%>");
22827     }
22828   else
22829     error_at (first_token->location, "expected string literal");
22830
22831   /* Skip to the end of the pragma.  */
22832   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
22833     cp_lexer_get_preprocessor_token (NULL, first_token);
22834
22835   /* Now actually load the PCH file.  */
22836   if (name)
22837     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
22838
22839   /* Read one more token to return to our caller.  We have to do this
22840      after reading the PCH file in, since its pointers have to be
22841      live.  */
22842   cp_lexer_get_preprocessor_token (NULL, first_token);
22843 }
22844
22845 /* Normal parsing of a pragma token.  Here we can (and must) use the
22846    regular lexer.  */
22847
22848 static bool
22849 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
22850 {
22851   cp_token *pragma_tok;
22852   unsigned int id;
22853
22854   pragma_tok = cp_lexer_consume_token (parser->lexer);
22855   gcc_assert (pragma_tok->type == CPP_PRAGMA);
22856   parser->lexer->in_pragma = true;
22857
22858   id = pragma_tok->pragma_kind;
22859   switch (id)
22860     {
22861     case PRAGMA_GCC_PCH_PREPROCESS:
22862       error_at (pragma_tok->location,
22863                 "%<#pragma GCC pch_preprocess%> must be first");
22864       break;
22865
22866     case PRAGMA_OMP_BARRIER:
22867       switch (context)
22868         {
22869         case pragma_compound:
22870           cp_parser_omp_barrier (parser, pragma_tok);
22871           return false;
22872         case pragma_stmt:
22873           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
22874                     "used in compound statements");
22875           break;
22876         default:
22877           goto bad_stmt;
22878         }
22879       break;
22880
22881     case PRAGMA_OMP_FLUSH:
22882       switch (context)
22883         {
22884         case pragma_compound:
22885           cp_parser_omp_flush (parser, pragma_tok);
22886           return false;
22887         case pragma_stmt:
22888           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
22889                     "used in compound statements");
22890           break;
22891         default:
22892           goto bad_stmt;
22893         }
22894       break;
22895
22896     case PRAGMA_OMP_TASKWAIT:
22897       switch (context)
22898         {
22899         case pragma_compound:
22900           cp_parser_omp_taskwait (parser, pragma_tok);
22901           return false;
22902         case pragma_stmt:
22903           error_at (pragma_tok->location,
22904                     "%<#pragma omp taskwait%> may only be "
22905                     "used in compound statements");
22906           break;
22907         default:
22908           goto bad_stmt;
22909         }
22910       break;
22911
22912     case PRAGMA_OMP_THREADPRIVATE:
22913       cp_parser_omp_threadprivate (parser, pragma_tok);
22914       return false;
22915
22916     case PRAGMA_OMP_ATOMIC:
22917     case PRAGMA_OMP_CRITICAL:
22918     case PRAGMA_OMP_FOR:
22919     case PRAGMA_OMP_MASTER:
22920     case PRAGMA_OMP_ORDERED:
22921     case PRAGMA_OMP_PARALLEL:
22922     case PRAGMA_OMP_SECTIONS:
22923     case PRAGMA_OMP_SINGLE:
22924     case PRAGMA_OMP_TASK:
22925       if (context == pragma_external)
22926         goto bad_stmt;
22927       cp_parser_omp_construct (parser, pragma_tok);
22928       return true;
22929
22930     case PRAGMA_OMP_SECTION:
22931       error_at (pragma_tok->location, 
22932                 "%<#pragma omp section%> may only be used in "
22933                 "%<#pragma omp sections%> construct");
22934       break;
22935
22936     default:
22937       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
22938       c_invoke_pragma_handler (id);
22939       break;
22940
22941     bad_stmt:
22942       cp_parser_error (parser, "expected declaration specifiers");
22943       break;
22944     }
22945
22946   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
22947   return false;
22948 }
22949
22950 /* The interface the pragma parsers have to the lexer.  */
22951
22952 enum cpp_ttype
22953 pragma_lex (tree *value)
22954 {
22955   cp_token *tok;
22956   enum cpp_ttype ret;
22957
22958   tok = cp_lexer_peek_token (the_parser->lexer);
22959
22960   ret = tok->type;
22961   *value = tok->u.value;
22962
22963   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
22964     ret = CPP_EOF;
22965   else if (ret == CPP_STRING)
22966     *value = cp_parser_string_literal (the_parser, false, false);
22967   else
22968     {
22969       cp_lexer_consume_token (the_parser->lexer);
22970       if (ret == CPP_KEYWORD)
22971         ret = CPP_NAME;
22972     }
22973
22974   return ret;
22975 }
22976
22977 \f
22978 /* External interface.  */
22979
22980 /* Parse one entire translation unit.  */
22981
22982 void
22983 c_parse_file (void)
22984 {
22985   bool error_occurred;
22986   static bool already_called = false;
22987
22988   if (already_called)
22989     {
22990       sorry ("inter-module optimizations not implemented for C++");
22991       return;
22992     }
22993   already_called = true;
22994
22995   the_parser = cp_parser_new ();
22996   push_deferring_access_checks (flag_access_control
22997                                 ? dk_no_deferred : dk_no_check);
22998   error_occurred = cp_parser_translation_unit (the_parser);
22999   the_parser = NULL;
23000 }
23001
23002 #include "gt-cp-parser.h"