OSDN Git Service

9b3e56d07f43d2abd1f61c50cf6b88878f706473
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  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 "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR
110 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 #ifdef ENABLE_CHECKING
214 static void cp_lexer_print_token
215   (FILE *, cp_token *);
216 static inline bool cp_lexer_debugging_p
217   (cp_lexer *);
218 static void cp_lexer_start_debugging
219   (cp_lexer *) ATTRIBUTE_UNUSED;
220 static void cp_lexer_stop_debugging
221   (cp_lexer *) ATTRIBUTE_UNUSED;
222 #else
223 /* If we define cp_lexer_debug_stream to NULL it will provoke warnings
224    about passing NULL to functions that require non-NULL arguments
225    (fputs, fprintf).  It will never be used, so all we need is a value
226    of the right type that's guaranteed not to be NULL.  */
227 #define cp_lexer_debug_stream stdout
228 #define cp_lexer_print_token(str, tok) (void) 0
229 #define cp_lexer_debugging_p(lexer) 0
230 #endif /* ENABLE_CHECKING */
231
232 static cp_token_cache *cp_token_cache_new
233   (cp_token *, cp_token *);
234
235 static void cp_parser_initial_pragma
236   (cp_token *);
237
238 /* Manifest constants.  */
239 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
240 #define CP_SAVED_TOKEN_STACK 5
241
242 /* Variables.  */
243
244 #ifdef ENABLE_CHECKING
245 /* The stream to which debugging output should be written.  */
246 static FILE *cp_lexer_debug_stream;
247 #endif /* ENABLE_CHECKING */
248
249 /* Nonzero if we are parsing an unevaluated operand: an operand to
250    sizeof, typeof, or alignof.  */
251 int cp_unevaluated_operand;
252
253 #ifdef ENABLE_CHECKING
254 /* Dump up to NUM tokens in BUFFER to FILE.  If NUM is 0, dump all the
255    tokens.  */
256
257 void
258 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer, unsigned num)
259 {
260   unsigned i;
261   cp_token *token;
262
263   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
264
265   if (num == 0)
266     num = VEC_length (cp_token, buffer);
267
268   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && i < num; i++)
269     {
270       cp_lexer_print_token (file, token);
271       switch (token->type)
272         {
273           case CPP_SEMICOLON:
274           case CPP_OPEN_BRACE:
275           case CPP_CLOSE_BRACE:
276           case CPP_EOF:
277             fputc ('\n', file);
278             break;
279
280           default:
281             fputc (' ', file);
282         }
283     }
284
285   if (i == num && i < VEC_length (cp_token, buffer))
286     {
287       fprintf (file, " ... ");
288       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
289                             VEC_length (cp_token, buffer) - 1));
290     }
291
292   fprintf (file, "\n");
293 }
294
295
296 /* Dump all tokens in BUFFER to stderr.  */
297
298 void
299 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
300 {
301   cp_lexer_dump_tokens (stderr, buffer, 0);
302 }
303 #endif
304
305
306 /* Allocate memory for a new lexer object and return it.  */
307
308 static cp_lexer *
309 cp_lexer_alloc (void)
310 {
311   cp_lexer *lexer;
312
313   c_common_no_more_pch ();
314
315   /* Allocate the memory.  */
316   lexer = ggc_alloc_cleared_cp_lexer ();
317
318 #ifdef ENABLE_CHECKING
319   /* Initially we are not debugging.  */
320   lexer->debugging_p = false;
321 #endif /* ENABLE_CHECKING */
322   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
323                                    CP_SAVED_TOKEN_STACK);
324
325   /* Create the buffer.  */
326   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
327
328   return lexer;
329 }
330
331
332 /* Create a new main C++ lexer, the lexer that gets tokens from the
333    preprocessor.  */
334
335 static cp_lexer *
336 cp_lexer_new_main (void)
337 {
338   cp_lexer *lexer;
339   cp_token token;
340
341   /* It's possible that parsing the first pragma will load a PCH file,
342      which is a GC collection point.  So we have to do that before
343      allocating any memory.  */
344   cp_parser_initial_pragma (&token);
345
346   lexer = cp_lexer_alloc ();
347
348   /* Put the first token in the buffer.  */
349   VEC_quick_push (cp_token, lexer->buffer, &token);
350
351   /* Get the remaining tokens from the preprocessor.  */
352   while (token.type != CPP_EOF)
353     {
354       cp_lexer_get_preprocessor_token (lexer, &token);
355       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
356     }
357
358   lexer->last_token = VEC_address (cp_token, lexer->buffer)
359                       + VEC_length (cp_token, lexer->buffer)
360                       - 1;
361   lexer->next_token = VEC_length (cp_token, lexer->buffer)
362                       ? VEC_address (cp_token, lexer->buffer)
363                       : &eof_token;
364
365   /* Subsequent preprocessor diagnostics should use compiler
366      diagnostic functions to get the compiler source location.  */
367   done_lexing = true;
368
369   gcc_assert (!lexer->next_token->purged_p);
370   return lexer;
371 }
372
373 /* Create a new lexer whose token stream is primed with the tokens in
374    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
375
376 static cp_lexer *
377 cp_lexer_new_from_tokens (cp_token_cache *cache)
378 {
379   cp_token *first = cache->first;
380   cp_token *last = cache->last;
381   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
382
383   /* We do not own the buffer.  */
384   lexer->buffer = NULL;
385   lexer->next_token = first == last ? &eof_token : first;
386   lexer->last_token = last;
387
388   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
389                                    CP_SAVED_TOKEN_STACK);
390
391 #ifdef ENABLE_CHECKING
392   /* Initially we are not debugging.  */
393   lexer->debugging_p = false;
394 #endif
395
396   gcc_assert (!lexer->next_token->purged_p);
397   return lexer;
398 }
399
400 /* Frees all resources associated with LEXER.  */
401
402 static void
403 cp_lexer_destroy (cp_lexer *lexer)
404 {
405   VEC_free (cp_token, gc, lexer->buffer);
406   VEC_free (cp_token_position, heap, lexer->saved_tokens);
407   ggc_free (lexer);
408 }
409
410 /* Returns nonzero if debugging information should be output.  */
411
412 #ifdef ENABLE_CHECKING
413
414 static inline bool
415 cp_lexer_debugging_p (cp_lexer *lexer)
416 {
417   return lexer->debugging_p;
418 }
419
420 #endif /* ENABLE_CHECKING */
421
422 static inline cp_token_position
423 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
424 {
425   gcc_assert (!previous_p || lexer->next_token != &eof_token);
426
427   return lexer->next_token - previous_p;
428 }
429
430 static inline cp_token *
431 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
432 {
433   return pos;
434 }
435
436 static inline void
437 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
438 {
439   lexer->next_token = cp_lexer_token_at (lexer, pos);
440 }
441
442 static inline cp_token_position
443 cp_lexer_previous_token_position (cp_lexer *lexer)
444 {
445   if (lexer->next_token == &eof_token)
446     return lexer->last_token - 1;
447   else
448     return cp_lexer_token_position (lexer, true);
449 }
450
451 static inline cp_token *
452 cp_lexer_previous_token (cp_lexer *lexer)
453 {
454   cp_token_position tp = cp_lexer_previous_token_position (lexer);
455
456   return cp_lexer_token_at (lexer, tp);
457 }
458
459 /* nonzero if we are presently saving tokens.  */
460
461 static inline int
462 cp_lexer_saving_tokens (const cp_lexer* lexer)
463 {
464   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
465 }
466
467 /* Store the next token from the preprocessor in *TOKEN.  Return true
468    if we reach EOF.  If LEXER is NULL, assume we are handling an
469    initial #pragma pch_preprocess, and thus want the lexer to return
470    processed strings.  */
471
472 static void
473 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
474 {
475   static int is_extern_c = 0;
476
477    /* Get a new token from the preprocessor.  */
478   token->type
479     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
480                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
481   token->keyword = RID_MAX;
482   token->pragma_kind = PRAGMA_NONE;
483   token->purged_p = false;
484
485   /* On some systems, some header files are surrounded by an
486      implicit extern "C" block.  Set a flag in the token if it
487      comes from such a header.  */
488   is_extern_c += pending_lang_change;
489   pending_lang_change = 0;
490   token->implicit_extern_c = is_extern_c > 0;
491
492   /* Check to see if this token is a keyword.  */
493   if (token->type == CPP_NAME)
494     {
495       if (C_IS_RESERVED_WORD (token->u.value))
496         {
497           /* Mark this token as a keyword.  */
498           token->type = CPP_KEYWORD;
499           /* Record which keyword.  */
500           token->keyword = C_RID_CODE (token->u.value);
501         }
502       else
503         {
504           if (warn_cxx0x_compat
505               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
506               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
507             {
508               /* Warn about the C++0x keyword (but still treat it as
509                  an identifier).  */
510               warning (OPT_Wc__0x_compat, 
511                        "identifier %qE will become a keyword in C++0x",
512                        token->u.value);
513
514               /* Clear out the C_RID_CODE so we don't warn about this
515                  particular identifier-turned-keyword again.  */
516               C_SET_RID_CODE (token->u.value, RID_MAX);
517             }
518
519           token->ambiguous_p = false;
520           token->keyword = RID_MAX;
521         }
522     }
523   else if (token->type == CPP_AT_NAME)
524     {
525       /* This only happens in Objective-C++; it must be a keyword.  */
526       token->type = CPP_KEYWORD;
527       switch (C_RID_CODE (token->u.value))
528         {
529           /* Replace 'class' with '@class', 'private' with '@private',
530              etc.  This prevents confusion with the C++ keyword
531              'class', and makes the tokens consistent with other
532              Objective-C 'AT' keywords.  For example '@class' is
533              reported as RID_AT_CLASS which is consistent with
534              '@synchronized', which is reported as
535              RID_AT_SYNCHRONIZED.
536           */
537         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
538         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
539         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
540         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
541         case RID_THROW:     token->keyword = RID_AT_THROW; break;
542         case RID_TRY:       token->keyword = RID_AT_TRY; break;
543         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
544         default:            token->keyword = C_RID_CODE (token->u.value);
545         }
546     }
547   else if (token->type == CPP_PRAGMA)
548     {
549       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
550       token->pragma_kind = ((enum pragma_kind)
551                             TREE_INT_CST_LOW (token->u.value));
552       token->u.value = NULL_TREE;
553     }
554 }
555
556 /* Update the globals input_location and the input file stack from TOKEN.  */
557 static inline void
558 cp_lexer_set_source_position_from_token (cp_token *token)
559 {
560   if (token->type != CPP_EOF)
561     {
562       input_location = token->location;
563     }
564 }
565
566 /* Return a pointer to the next token in the token stream, but do not
567    consume it.  */
568
569 static inline cp_token *
570 cp_lexer_peek_token (cp_lexer *lexer)
571 {
572   if (cp_lexer_debugging_p (lexer))
573     {
574       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
575       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
576       putc ('\n', cp_lexer_debug_stream);
577     }
578   return lexer->next_token;
579 }
580
581 /* Return true if the next token has the indicated TYPE.  */
582
583 static inline bool
584 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
585 {
586   return cp_lexer_peek_token (lexer)->type == type;
587 }
588
589 /* Return true if the next token does not have the indicated TYPE.  */
590
591 static inline bool
592 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
593 {
594   return !cp_lexer_next_token_is (lexer, type);
595 }
596
597 /* Return true if the next token is the indicated KEYWORD.  */
598
599 static inline bool
600 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
601 {
602   return cp_lexer_peek_token (lexer)->keyword == keyword;
603 }
604
605 /* Return true if the next token is not the indicated KEYWORD.  */
606
607 static inline bool
608 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
609 {
610   return cp_lexer_peek_token (lexer)->keyword != keyword;
611 }
612
613 /* Return true if the next token is a keyword for a decl-specifier.  */
614
615 static bool
616 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
617 {
618   cp_token *token;
619
620   token = cp_lexer_peek_token (lexer);
621   switch (token->keyword) 
622     {
623       /* auto specifier: storage-class-specifier in C++,
624          simple-type-specifier in C++0x.  */
625     case RID_AUTO:
626       /* Storage classes.  */
627     case RID_REGISTER:
628     case RID_STATIC:
629     case RID_EXTERN:
630     case RID_MUTABLE:
631     case RID_THREAD:
632       /* Elaborated type specifiers.  */
633     case RID_ENUM:
634     case RID_CLASS:
635     case RID_STRUCT:
636     case RID_UNION:
637     case RID_TYPENAME:
638       /* Simple type specifiers.  */
639     case RID_CHAR:
640     case RID_CHAR16:
641     case RID_CHAR32:
642     case RID_WCHAR:
643     case RID_BOOL:
644     case RID_SHORT:
645     case RID_INT:
646     case RID_LONG:
647     case RID_INT128:
648     case RID_SIGNED:
649     case RID_UNSIGNED:
650     case RID_FLOAT:
651     case RID_DOUBLE:
652     case RID_VOID:
653       /* GNU extensions.  */ 
654     case RID_ATTRIBUTE:
655     case RID_TYPEOF:
656       /* C++0x extensions.  */
657     case RID_DECLTYPE:
658     case RID_UNDERLYING_TYPE:
659       return true;
660
661     default:
662       return false;
663     }
664 }
665
666 /* Returns TRUE iff the token T begins a decltype type.  */
667
668 static bool
669 token_is_decltype (cp_token *t)
670 {
671   return (t->keyword == RID_DECLTYPE
672           || t->type == CPP_DECLTYPE);
673 }
674
675 /* Returns TRUE iff the next token begins a decltype type.  */
676
677 static bool
678 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
679 {
680   cp_token *t = cp_lexer_peek_token (lexer);
681   return token_is_decltype (t);
682 }
683
684 /* Return a pointer to the Nth token in the token stream.  If N is 1,
685    then this is precisely equivalent to cp_lexer_peek_token (except
686    that it is not inline).  One would like to disallow that case, but
687    there is one case (cp_parser_nth_token_starts_template_id) where
688    the caller passes a variable for N and it might be 1.  */
689
690 static cp_token *
691 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
692 {
693   cp_token *token;
694
695   /* N is 1-based, not zero-based.  */
696   gcc_assert (n > 0);
697
698   if (cp_lexer_debugging_p (lexer))
699     fprintf (cp_lexer_debug_stream,
700              "cp_lexer: peeking ahead %ld at token: ", (long)n);
701
702   --n;
703   token = lexer->next_token;
704   gcc_assert (!n || token != &eof_token);
705   while (n != 0)
706     {
707       ++token;
708       if (token == lexer->last_token)
709         {
710           token = &eof_token;
711           break;
712         }
713
714       if (!token->purged_p)
715         --n;
716     }
717
718   if (cp_lexer_debugging_p (lexer))
719     {
720       cp_lexer_print_token (cp_lexer_debug_stream, token);
721       putc ('\n', cp_lexer_debug_stream);
722     }
723
724   return token;
725 }
726
727 /* Return the next token, and advance the lexer's next_token pointer
728    to point to the next non-purged token.  */
729
730 static cp_token *
731 cp_lexer_consume_token (cp_lexer* lexer)
732 {
733   cp_token *token = lexer->next_token;
734
735   gcc_assert (token != &eof_token);
736   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
737
738   do
739     {
740       lexer->next_token++;
741       if (lexer->next_token == lexer->last_token)
742         {
743           lexer->next_token = &eof_token;
744           break;
745         }
746
747     }
748   while (lexer->next_token->purged_p);
749
750   cp_lexer_set_source_position_from_token (token);
751
752   /* Provide debugging output.  */
753   if (cp_lexer_debugging_p (lexer))
754     {
755       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
756       cp_lexer_print_token (cp_lexer_debug_stream, token);
757       putc ('\n', cp_lexer_debug_stream);
758     }
759
760   return token;
761 }
762
763 /* Permanently remove the next token from the token stream, and
764    advance the next_token pointer to refer to the next non-purged
765    token.  */
766
767 static void
768 cp_lexer_purge_token (cp_lexer *lexer)
769 {
770   cp_token *tok = lexer->next_token;
771
772   gcc_assert (tok != &eof_token);
773   tok->purged_p = true;
774   tok->location = UNKNOWN_LOCATION;
775   tok->u.value = NULL_TREE;
776   tok->keyword = RID_MAX;
777
778   do
779     {
780       tok++;
781       if (tok == lexer->last_token)
782         {
783           tok = &eof_token;
784           break;
785         }
786     }
787   while (tok->purged_p);
788   lexer->next_token = tok;
789 }
790
791 /* Permanently remove all tokens after TOK, up to, but not
792    including, the token that will be returned next by
793    cp_lexer_peek_token.  */
794
795 static void
796 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
797 {
798   cp_token *peek = lexer->next_token;
799
800   if (peek == &eof_token)
801     peek = lexer->last_token;
802
803   gcc_assert (tok < peek);
804
805   for ( tok += 1; tok != peek; tok += 1)
806     {
807       tok->purged_p = true;
808       tok->location = UNKNOWN_LOCATION;
809       tok->u.value = NULL_TREE;
810       tok->keyword = RID_MAX;
811     }
812 }
813
814 /* Begin saving tokens.  All tokens consumed after this point will be
815    preserved.  */
816
817 static void
818 cp_lexer_save_tokens (cp_lexer* lexer)
819 {
820   /* Provide debugging output.  */
821   if (cp_lexer_debugging_p (lexer))
822     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
823
824   VEC_safe_push (cp_token_position, heap,
825                  lexer->saved_tokens, lexer->next_token);
826 }
827
828 /* Commit to the portion of the token stream most recently saved.  */
829
830 static void
831 cp_lexer_commit_tokens (cp_lexer* lexer)
832 {
833   /* Provide debugging output.  */
834   if (cp_lexer_debugging_p (lexer))
835     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
836
837   VEC_pop (cp_token_position, lexer->saved_tokens);
838 }
839
840 /* Return all tokens saved since the last call to cp_lexer_save_tokens
841    to the token stream.  Stop saving tokens.  */
842
843 static void
844 cp_lexer_rollback_tokens (cp_lexer* lexer)
845 {
846   /* Provide debugging output.  */
847   if (cp_lexer_debugging_p (lexer))
848     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
849
850   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
851 }
852
853 /* Print a representation of the TOKEN on the STREAM.  */
854
855 #ifdef ENABLE_CHECKING
856
857 static void
858 cp_lexer_print_token (FILE * stream, cp_token *token)
859 {
860   /* We don't use cpp_type2name here because the parser defines
861      a few tokens of its own.  */
862   static const char *const token_names[] = {
863     /* cpplib-defined token types */
864 #define OP(e, s) #e,
865 #define TK(e, s) #e,
866     TTYPE_TABLE
867 #undef OP
868 #undef TK
869     /* C++ parser token types - see "Manifest constants", above.  */
870     "KEYWORD",
871     "TEMPLATE_ID",
872     "NESTED_NAME_SPECIFIER",
873   };
874
875   /* For some tokens, print the associated data.  */
876   switch (token->type)
877     {
878     case CPP_KEYWORD:
879       /* Some keywords have a value that is not an IDENTIFIER_NODE.
880          For example, `struct' is mapped to an INTEGER_CST.  */
881       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
882         break;
883       /* else fall through */
884     case CPP_NAME:
885       fputs (IDENTIFIER_POINTER (token->u.value), stream);
886       break;
887
888     case CPP_STRING:
889     case CPP_STRING16:
890     case CPP_STRING32:
891     case CPP_WSTRING:
892     case CPP_UTF8STRING:
893       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
894       break;
895
896     case CPP_NUMBER:
897       print_generic_expr (stream, token->u.value, 0);
898       break;
899
900     default:
901       /* If we have a name for the token, print it out.  Otherwise, we
902          simply give the numeric code.  */
903       if (token->type < ARRAY_SIZE(token_names))
904         fputs (token_names[token->type], stream);
905       else
906         fprintf (stream, "[%d]", token->type);
907       break;
908     }
909 }
910
911 /* Start emitting debugging information.  */
912
913 static void
914 cp_lexer_start_debugging (cp_lexer* lexer)
915 {
916   lexer->debugging_p = true;
917 }
918
919 /* Stop emitting debugging information.  */
920
921 static void
922 cp_lexer_stop_debugging (cp_lexer* lexer)
923 {
924   lexer->debugging_p = false;
925 }
926
927 #endif /* ENABLE_CHECKING */
928
929 /* Create a new cp_token_cache, representing a range of tokens.  */
930
931 static cp_token_cache *
932 cp_token_cache_new (cp_token *first, cp_token *last)
933 {
934   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
935   cache->first = first;
936   cache->last = last;
937   return cache;
938 }
939
940 \f
941 /* Decl-specifiers.  */
942
943 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
944
945 static void
946 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
947 {
948   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
949 }
950
951 /* Declarators.  */
952
953 /* Nothing other than the parser should be creating declarators;
954    declarators are a semi-syntactic representation of C++ entities.
955    Other parts of the front end that need to create entities (like
956    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
957
958 static cp_declarator *make_call_declarator
959   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
960 static cp_declarator *make_array_declarator
961   (cp_declarator *, tree);
962 static cp_declarator *make_pointer_declarator
963   (cp_cv_quals, cp_declarator *);
964 static cp_declarator *make_reference_declarator
965   (cp_cv_quals, cp_declarator *, bool);
966 static cp_parameter_declarator *make_parameter_declarator
967   (cp_decl_specifier_seq *, cp_declarator *, tree);
968 static cp_declarator *make_ptrmem_declarator
969   (cp_cv_quals, tree, cp_declarator *);
970
971 /* An erroneous declarator.  */
972 static cp_declarator *cp_error_declarator;
973
974 /* The obstack on which declarators and related data structures are
975    allocated.  */
976 static struct obstack declarator_obstack;
977
978 /* Alloc BYTES from the declarator memory pool.  */
979
980 static inline void *
981 alloc_declarator (size_t bytes)
982 {
983   return obstack_alloc (&declarator_obstack, bytes);
984 }
985
986 /* Allocate a declarator of the indicated KIND.  Clear fields that are
987    common to all declarators.  */
988
989 static cp_declarator *
990 make_declarator (cp_declarator_kind kind)
991 {
992   cp_declarator *declarator;
993
994   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
995   declarator->kind = kind;
996   declarator->attributes = NULL_TREE;
997   declarator->declarator = NULL;
998   declarator->parameter_pack_p = false;
999   declarator->id_loc = UNKNOWN_LOCATION;
1000
1001   return declarator;
1002 }
1003
1004 /* Make a declarator for a generalized identifier.  If
1005    QUALIFYING_SCOPE is non-NULL, the identifier is
1006    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1007    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1008    is, if any.   */
1009
1010 static cp_declarator *
1011 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1012                     special_function_kind sfk)
1013 {
1014   cp_declarator *declarator;
1015
1016   /* It is valid to write:
1017
1018        class C { void f(); };
1019        typedef C D;
1020        void D::f();
1021
1022      The standard is not clear about whether `typedef const C D' is
1023      legal; as of 2002-09-15 the committee is considering that
1024      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1025      well.  */
1026   if (qualifying_scope && TYPE_P (qualifying_scope))
1027     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1028
1029   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1030               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1031               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1032
1033   declarator = make_declarator (cdk_id);
1034   declarator->u.id.qualifying_scope = qualifying_scope;
1035   declarator->u.id.unqualified_name = unqualified_name;
1036   declarator->u.id.sfk = sfk;
1037   
1038   return declarator;
1039 }
1040
1041 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1042    of modifiers such as const or volatile to apply to the pointer
1043    type, represented as identifiers.  */
1044
1045 cp_declarator *
1046 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1047 {
1048   cp_declarator *declarator;
1049
1050   declarator = make_declarator (cdk_pointer);
1051   declarator->declarator = target;
1052   declarator->u.pointer.qualifiers = cv_qualifiers;
1053   declarator->u.pointer.class_type = NULL_TREE;
1054   if (target)
1055     {
1056       declarator->id_loc = target->id_loc;
1057       declarator->parameter_pack_p = target->parameter_pack_p;
1058       target->parameter_pack_p = false;
1059     }
1060   else
1061     declarator->parameter_pack_p = false;
1062
1063   return declarator;
1064 }
1065
1066 /* Like make_pointer_declarator -- but for references.  */
1067
1068 cp_declarator *
1069 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1070                            bool rvalue_ref)
1071 {
1072   cp_declarator *declarator;
1073
1074   declarator = make_declarator (cdk_reference);
1075   declarator->declarator = target;
1076   declarator->u.reference.qualifiers = cv_qualifiers;
1077   declarator->u.reference.rvalue_ref = rvalue_ref;
1078   if (target)
1079     {
1080       declarator->id_loc = target->id_loc;
1081       declarator->parameter_pack_p = target->parameter_pack_p;
1082       target->parameter_pack_p = false;
1083     }
1084   else
1085     declarator->parameter_pack_p = false;
1086
1087   return declarator;
1088 }
1089
1090 /* Like make_pointer_declarator -- but for a pointer to a non-static
1091    member of CLASS_TYPE.  */
1092
1093 cp_declarator *
1094 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1095                         cp_declarator *pointee)
1096 {
1097   cp_declarator *declarator;
1098
1099   declarator = make_declarator (cdk_ptrmem);
1100   declarator->declarator = pointee;
1101   declarator->u.pointer.qualifiers = cv_qualifiers;
1102   declarator->u.pointer.class_type = class_type;
1103
1104   if (pointee)
1105     {
1106       declarator->parameter_pack_p = pointee->parameter_pack_p;
1107       pointee->parameter_pack_p = false;
1108     }
1109   else
1110     declarator->parameter_pack_p = false;
1111
1112   return declarator;
1113 }
1114
1115 /* Make a declarator for the function given by TARGET, with the
1116    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1117    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1118    indicates what exceptions can be thrown.  */
1119
1120 cp_declarator *
1121 make_call_declarator (cp_declarator *target,
1122                       tree parms,
1123                       cp_cv_quals cv_qualifiers,
1124                       cp_virt_specifiers virt_specifiers,
1125                       tree exception_specification,
1126                       tree late_return_type)
1127 {
1128   cp_declarator *declarator;
1129
1130   declarator = make_declarator (cdk_function);
1131   declarator->declarator = target;
1132   declarator->u.function.parameters = parms;
1133   declarator->u.function.qualifiers = cv_qualifiers;
1134   declarator->u.function.virt_specifiers = virt_specifiers;
1135   declarator->u.function.exception_specification = exception_specification;
1136   declarator->u.function.late_return_type = late_return_type;
1137   if (target)
1138     {
1139       declarator->id_loc = target->id_loc;
1140       declarator->parameter_pack_p = target->parameter_pack_p;
1141       target->parameter_pack_p = false;
1142     }
1143   else
1144     declarator->parameter_pack_p = false;
1145
1146   return declarator;
1147 }
1148
1149 /* Make a declarator for an array of BOUNDS elements, each of which is
1150    defined by ELEMENT.  */
1151
1152 cp_declarator *
1153 make_array_declarator (cp_declarator *element, tree bounds)
1154 {
1155   cp_declarator *declarator;
1156
1157   declarator = make_declarator (cdk_array);
1158   declarator->declarator = element;
1159   declarator->u.array.bounds = bounds;
1160   if (element)
1161     {
1162       declarator->id_loc = element->id_loc;
1163       declarator->parameter_pack_p = element->parameter_pack_p;
1164       element->parameter_pack_p = false;
1165     }
1166   else
1167     declarator->parameter_pack_p = false;
1168
1169   return declarator;
1170 }
1171
1172 /* Determine whether the declarator we've seen so far can be a
1173    parameter pack, when followed by an ellipsis.  */
1174 static bool 
1175 declarator_can_be_parameter_pack (cp_declarator *declarator)
1176 {
1177   /* Search for a declarator name, or any other declarator that goes
1178      after the point where the ellipsis could appear in a parameter
1179      pack. If we find any of these, then this declarator can not be
1180      made into a parameter pack.  */
1181   bool found = false;
1182   while (declarator && !found)
1183     {
1184       switch ((int)declarator->kind)
1185         {
1186         case cdk_id:
1187         case cdk_array:
1188           found = true;
1189           break;
1190
1191         case cdk_error:
1192           return true;
1193
1194         default:
1195           declarator = declarator->declarator;
1196           break;
1197         }
1198     }
1199
1200   return !found;
1201 }
1202
1203 cp_parameter_declarator *no_parameters;
1204
1205 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1206    DECLARATOR and DEFAULT_ARGUMENT.  */
1207
1208 cp_parameter_declarator *
1209 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1210                            cp_declarator *declarator,
1211                            tree default_argument)
1212 {
1213   cp_parameter_declarator *parameter;
1214
1215   parameter = ((cp_parameter_declarator *)
1216                alloc_declarator (sizeof (cp_parameter_declarator)));
1217   parameter->next = NULL;
1218   if (decl_specifiers)
1219     parameter->decl_specifiers = *decl_specifiers;
1220   else
1221     clear_decl_specs (&parameter->decl_specifiers);
1222   parameter->declarator = declarator;
1223   parameter->default_argument = default_argument;
1224   parameter->ellipsis_p = false;
1225
1226   return parameter;
1227 }
1228
1229 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1230
1231 static bool
1232 function_declarator_p (const cp_declarator *declarator)
1233 {
1234   while (declarator)
1235     {
1236       if (declarator->kind == cdk_function
1237           && declarator->declarator->kind == cdk_id)
1238         return true;
1239       if (declarator->kind == cdk_id
1240           || declarator->kind == cdk_error)
1241         return false;
1242       declarator = declarator->declarator;
1243     }
1244   return false;
1245 }
1246  
1247 /* The parser.  */
1248
1249 /* Overview
1250    --------
1251
1252    A cp_parser parses the token stream as specified by the C++
1253    grammar.  Its job is purely parsing, not semantic analysis.  For
1254    example, the parser breaks the token stream into declarators,
1255    expressions, statements, and other similar syntactic constructs.
1256    It does not check that the types of the expressions on either side
1257    of an assignment-statement are compatible, or that a function is
1258    not declared with a parameter of type `void'.
1259
1260    The parser invokes routines elsewhere in the compiler to perform
1261    semantic analysis and to build up the abstract syntax tree for the
1262    code processed.
1263
1264    The parser (and the template instantiation code, which is, in a
1265    way, a close relative of parsing) are the only parts of the
1266    compiler that should be calling push_scope and pop_scope, or
1267    related functions.  The parser (and template instantiation code)
1268    keeps track of what scope is presently active; everything else
1269    should simply honor that.  (The code that generates static
1270    initializers may also need to set the scope, in order to check
1271    access control correctly when emitting the initializers.)
1272
1273    Methodology
1274    -----------
1275
1276    The parser is of the standard recursive-descent variety.  Upcoming
1277    tokens in the token stream are examined in order to determine which
1278    production to use when parsing a non-terminal.  Some C++ constructs
1279    require arbitrary look ahead to disambiguate.  For example, it is
1280    impossible, in the general case, to tell whether a statement is an
1281    expression or declaration without scanning the entire statement.
1282    Therefore, the parser is capable of "parsing tentatively."  When the
1283    parser is not sure what construct comes next, it enters this mode.
1284    Then, while we attempt to parse the construct, the parser queues up
1285    error messages, rather than issuing them immediately, and saves the
1286    tokens it consumes.  If the construct is parsed successfully, the
1287    parser "commits", i.e., it issues any queued error messages and
1288    the tokens that were being preserved are permanently discarded.
1289    If, however, the construct is not parsed successfully, the parser
1290    rolls back its state completely so that it can resume parsing using
1291    a different alternative.
1292
1293    Future Improvements
1294    -------------------
1295
1296    The performance of the parser could probably be improved substantially.
1297    We could often eliminate the need to parse tentatively by looking ahead
1298    a little bit.  In some places, this approach might not entirely eliminate
1299    the need to parse tentatively, but it might still speed up the average
1300    case.  */
1301
1302 /* Flags that are passed to some parsing functions.  These values can
1303    be bitwise-ored together.  */
1304
1305 enum
1306 {
1307   /* No flags.  */
1308   CP_PARSER_FLAGS_NONE = 0x0,
1309   /* The construct is optional.  If it is not present, then no error
1310      should be issued.  */
1311   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1312   /* When parsing a type-specifier, treat user-defined type-names
1313      as non-type identifiers.  */
1314   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1315   /* When parsing a type-specifier, do not try to parse a class-specifier
1316      or enum-specifier.  */
1317   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1318   /* When parsing a decl-specifier-seq, only allow type-specifier or
1319      constexpr.  */
1320   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1321 };
1322
1323 /* This type is used for parameters and variables which hold
1324    combinations of the above flags.  */
1325 typedef int cp_parser_flags;
1326
1327 /* The different kinds of declarators we want to parse.  */
1328
1329 typedef enum cp_parser_declarator_kind
1330 {
1331   /* We want an abstract declarator.  */
1332   CP_PARSER_DECLARATOR_ABSTRACT,
1333   /* We want a named declarator.  */
1334   CP_PARSER_DECLARATOR_NAMED,
1335   /* We don't mind, but the name must be an unqualified-id.  */
1336   CP_PARSER_DECLARATOR_EITHER
1337 } cp_parser_declarator_kind;
1338
1339 /* The precedence values used to parse binary expressions.  The minimum value
1340    of PREC must be 1, because zero is reserved to quickly discriminate
1341    binary operators from other tokens.  */
1342
1343 enum cp_parser_prec
1344 {
1345   PREC_NOT_OPERATOR,
1346   PREC_LOGICAL_OR_EXPRESSION,
1347   PREC_LOGICAL_AND_EXPRESSION,
1348   PREC_INCLUSIVE_OR_EXPRESSION,
1349   PREC_EXCLUSIVE_OR_EXPRESSION,
1350   PREC_AND_EXPRESSION,
1351   PREC_EQUALITY_EXPRESSION,
1352   PREC_RELATIONAL_EXPRESSION,
1353   PREC_SHIFT_EXPRESSION,
1354   PREC_ADDITIVE_EXPRESSION,
1355   PREC_MULTIPLICATIVE_EXPRESSION,
1356   PREC_PM_EXPRESSION,
1357   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1358 };
1359
1360 /* A mapping from a token type to a corresponding tree node type, with a
1361    precedence value.  */
1362
1363 typedef struct cp_parser_binary_operations_map_node
1364 {
1365   /* The token type.  */
1366   enum cpp_ttype token_type;
1367   /* The corresponding tree code.  */
1368   enum tree_code tree_type;
1369   /* The precedence of this operator.  */
1370   enum cp_parser_prec prec;
1371 } cp_parser_binary_operations_map_node;
1372
1373 typedef struct cp_parser_expression_stack_entry
1374 {
1375   /* Left hand side of the binary operation we are currently
1376      parsing.  */
1377   tree lhs;
1378   /* Original tree code for left hand side, if it was a binary
1379      expression itself (used for -Wparentheses).  */
1380   enum tree_code lhs_type;
1381   /* Tree code for the binary operation we are parsing.  */
1382   enum tree_code tree_type;
1383   /* Precedence of the binary operation we are parsing.  */
1384   enum cp_parser_prec prec;
1385 } cp_parser_expression_stack_entry;
1386
1387 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1388    entries because precedence levels on the stack are monotonically
1389    increasing.  */
1390 typedef struct cp_parser_expression_stack_entry
1391   cp_parser_expression_stack[NUM_PREC_VALUES];
1392
1393 /* Prototypes.  */
1394
1395 /* Constructors and destructors.  */
1396
1397 static cp_parser_context *cp_parser_context_new
1398   (cp_parser_context *);
1399
1400 /* Class variables.  */
1401
1402 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1403
1404 /* The operator-precedence table used by cp_parser_binary_expression.
1405    Transformed into an associative array (binops_by_token) by
1406    cp_parser_new.  */
1407
1408 static const cp_parser_binary_operations_map_node binops[] = {
1409   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1410   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1411
1412   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1413   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1414   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1415
1416   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1417   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1418
1419   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1420   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1421
1422   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1423   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1424   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1425   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1426
1427   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1428   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1429
1430   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1431
1432   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1433
1434   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1435
1436   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1437
1438   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1439 };
1440
1441 /* The same as binops, but initialized by cp_parser_new so that
1442    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1443    for speed.  */
1444 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1445
1446 /* Constructors and destructors.  */
1447
1448 /* Construct a new context.  The context below this one on the stack
1449    is given by NEXT.  */
1450
1451 static cp_parser_context *
1452 cp_parser_context_new (cp_parser_context* next)
1453 {
1454   cp_parser_context *context;
1455
1456   /* Allocate the storage.  */
1457   if (cp_parser_context_free_list != NULL)
1458     {
1459       /* Pull the first entry from the free list.  */
1460       context = cp_parser_context_free_list;
1461       cp_parser_context_free_list = context->next;
1462       memset (context, 0, sizeof (*context));
1463     }
1464   else
1465     context = ggc_alloc_cleared_cp_parser_context ();
1466
1467   /* No errors have occurred yet in this context.  */
1468   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1469   /* If this is not the bottommost context, copy information that we
1470      need from the previous context.  */
1471   if (next)
1472     {
1473       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1474          expression, then we are parsing one in this context, too.  */
1475       context->object_type = next->object_type;
1476       /* Thread the stack.  */
1477       context->next = next;
1478     }
1479
1480   return context;
1481 }
1482
1483 /* Managing the unparsed function queues.  */
1484
1485 #define unparsed_funs_with_default_args \
1486   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1487 #define unparsed_funs_with_definitions \
1488   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1489
1490 static void
1491 push_unparsed_function_queues (cp_parser *parser)
1492 {
1493   VEC_safe_push (cp_unparsed_functions_entry, gc,
1494                  parser->unparsed_queues, NULL);
1495   unparsed_funs_with_default_args = NULL;
1496   unparsed_funs_with_definitions = make_tree_vector ();
1497 }
1498
1499 static void
1500 pop_unparsed_function_queues (cp_parser *parser)
1501 {
1502   release_tree_vector (unparsed_funs_with_definitions);
1503   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1504 }
1505
1506 /* Prototypes.  */
1507
1508 /* Constructors and destructors.  */
1509
1510 static cp_parser *cp_parser_new
1511   (void);
1512
1513 /* Routines to parse various constructs.
1514
1515    Those that return `tree' will return the error_mark_node (rather
1516    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1517    Sometimes, they will return an ordinary node if error-recovery was
1518    attempted, even though a parse error occurred.  So, to check
1519    whether or not a parse error occurred, you should always use
1520    cp_parser_error_occurred.  If the construct is optional (indicated
1521    either by an `_opt' in the name of the function that does the
1522    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1523    the construct is not present.  */
1524
1525 /* Lexical conventions [gram.lex]  */
1526
1527 static tree cp_parser_identifier
1528   (cp_parser *);
1529 static tree cp_parser_string_literal
1530   (cp_parser *, bool, bool);
1531
1532 /* Basic concepts [gram.basic]  */
1533
1534 static bool cp_parser_translation_unit
1535   (cp_parser *);
1536
1537 /* Expressions [gram.expr]  */
1538
1539 static tree cp_parser_primary_expression
1540   (cp_parser *, bool, bool, bool, cp_id_kind *);
1541 static tree cp_parser_id_expression
1542   (cp_parser *, bool, bool, bool *, bool, bool);
1543 static tree cp_parser_unqualified_id
1544   (cp_parser *, bool, bool, bool, bool);
1545 static tree cp_parser_nested_name_specifier_opt
1546   (cp_parser *, bool, bool, bool, bool);
1547 static tree cp_parser_nested_name_specifier
1548   (cp_parser *, bool, bool, bool, bool);
1549 static tree cp_parser_qualifying_entity
1550   (cp_parser *, bool, bool, bool, bool, bool);
1551 static tree cp_parser_postfix_expression
1552   (cp_parser *, bool, bool, bool, cp_id_kind *);
1553 static tree cp_parser_postfix_open_square_expression
1554   (cp_parser *, tree, bool);
1555 static tree cp_parser_postfix_dot_deref_expression
1556   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1557 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1558   (cp_parser *, int, bool, bool, bool *);
1559 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1560 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1561 static void cp_parser_pseudo_destructor_name
1562   (cp_parser *, tree *, tree *);
1563 static tree cp_parser_unary_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static enum tree_code cp_parser_unary_operator
1566   (cp_token *);
1567 static tree cp_parser_new_expression
1568   (cp_parser *);
1569 static VEC(tree,gc) *cp_parser_new_placement
1570   (cp_parser *);
1571 static tree cp_parser_new_type_id
1572   (cp_parser *, tree *);
1573 static cp_declarator *cp_parser_new_declarator_opt
1574   (cp_parser *);
1575 static cp_declarator *cp_parser_direct_new_declarator
1576   (cp_parser *);
1577 static VEC(tree,gc) *cp_parser_new_initializer
1578   (cp_parser *);
1579 static tree cp_parser_delete_expression
1580   (cp_parser *);
1581 static tree cp_parser_cast_expression
1582   (cp_parser *, bool, bool, cp_id_kind *);
1583 static tree cp_parser_binary_expression
1584   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1585 static tree cp_parser_question_colon_clause
1586   (cp_parser *, tree);
1587 static tree cp_parser_assignment_expression
1588   (cp_parser *, bool, cp_id_kind *);
1589 static enum tree_code cp_parser_assignment_operator_opt
1590   (cp_parser *);
1591 static tree cp_parser_expression
1592   (cp_parser *, bool, cp_id_kind *);
1593 static tree cp_parser_constant_expression
1594   (cp_parser *, bool, bool *);
1595 static tree cp_parser_builtin_offsetof
1596   (cp_parser *);
1597 static tree cp_parser_lambda_expression
1598   (cp_parser *);
1599 static void cp_parser_lambda_introducer
1600   (cp_parser *, tree);
1601 static bool cp_parser_lambda_declarator_opt
1602   (cp_parser *, tree);
1603 static void cp_parser_lambda_body
1604   (cp_parser *, tree);
1605
1606 /* Statements [gram.stmt.stmt]  */
1607
1608 static void cp_parser_statement
1609   (cp_parser *, tree, bool, bool *);
1610 static void cp_parser_label_for_labeled_statement
1611   (cp_parser *);
1612 static tree cp_parser_expression_statement
1613   (cp_parser *, tree);
1614 static tree cp_parser_compound_statement
1615   (cp_parser *, tree, bool, bool);
1616 static void cp_parser_statement_seq_opt
1617   (cp_parser *, tree);
1618 static tree cp_parser_selection_statement
1619   (cp_parser *, bool *);
1620 static tree cp_parser_condition
1621   (cp_parser *);
1622 static tree cp_parser_iteration_statement
1623   (cp_parser *);
1624 static bool cp_parser_for_init_statement
1625   (cp_parser *, tree *decl);
1626 static tree cp_parser_for
1627   (cp_parser *);
1628 static tree cp_parser_c_for
1629   (cp_parser *, tree, tree);
1630 static tree cp_parser_range_for
1631   (cp_parser *, tree, tree, tree);
1632 static void do_range_for_auto_deduction
1633   (tree, tree);
1634 static tree cp_parser_perform_range_for_lookup
1635   (tree, tree *, tree *);
1636 static tree cp_parser_range_for_member_function
1637   (tree, tree);
1638 static tree cp_parser_jump_statement
1639   (cp_parser *);
1640 static void cp_parser_declaration_statement
1641   (cp_parser *);
1642
1643 static tree cp_parser_implicitly_scoped_statement
1644   (cp_parser *, bool *);
1645 static void cp_parser_already_scoped_statement
1646   (cp_parser *);
1647
1648 /* Declarations [gram.dcl.dcl] */
1649
1650 static void cp_parser_declaration_seq_opt
1651   (cp_parser *);
1652 static void cp_parser_declaration
1653   (cp_parser *);
1654 static void cp_parser_block_declaration
1655   (cp_parser *, bool);
1656 static void cp_parser_simple_declaration
1657   (cp_parser *, bool, tree *);
1658 static void cp_parser_decl_specifier_seq
1659   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1660 static tree cp_parser_storage_class_specifier_opt
1661   (cp_parser *);
1662 static tree cp_parser_function_specifier_opt
1663   (cp_parser *, cp_decl_specifier_seq *);
1664 static tree cp_parser_type_specifier
1665   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1666    int *, bool *);
1667 static tree cp_parser_simple_type_specifier
1668   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1669 static tree cp_parser_type_name
1670   (cp_parser *);
1671 static tree cp_parser_nonclass_name 
1672   (cp_parser* parser);
1673 static tree cp_parser_elaborated_type_specifier
1674   (cp_parser *, bool, bool);
1675 static tree cp_parser_enum_specifier
1676   (cp_parser *);
1677 static void cp_parser_enumerator_list
1678   (cp_parser *, tree);
1679 static void cp_parser_enumerator_definition
1680   (cp_parser *, tree);
1681 static tree cp_parser_namespace_name
1682   (cp_parser *);
1683 static void cp_parser_namespace_definition
1684   (cp_parser *);
1685 static void cp_parser_namespace_body
1686   (cp_parser *);
1687 static tree cp_parser_qualified_namespace_specifier
1688   (cp_parser *);
1689 static void cp_parser_namespace_alias_definition
1690   (cp_parser *);
1691 static bool cp_parser_using_declaration
1692   (cp_parser *, bool);
1693 static void cp_parser_using_directive
1694   (cp_parser *);
1695 static void cp_parser_asm_definition
1696   (cp_parser *);
1697 static void cp_parser_linkage_specification
1698   (cp_parser *);
1699 static void cp_parser_static_assert
1700   (cp_parser *, bool);
1701 static tree cp_parser_decltype
1702   (cp_parser *);
1703
1704 /* Declarators [gram.dcl.decl] */
1705
1706 static tree cp_parser_init_declarator
1707   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1708 static cp_declarator *cp_parser_declarator
1709   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1710 static cp_declarator *cp_parser_direct_declarator
1711   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1712 static enum tree_code cp_parser_ptr_operator
1713   (cp_parser *, tree *, cp_cv_quals *);
1714 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1715   (cp_parser *);
1716 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1717   (cp_parser *);
1718 static tree cp_parser_late_return_type_opt
1719   (cp_parser *, cp_cv_quals);
1720 static tree cp_parser_declarator_id
1721   (cp_parser *, bool);
1722 static tree cp_parser_type_id
1723   (cp_parser *);
1724 static tree cp_parser_template_type_arg
1725   (cp_parser *);
1726 static tree cp_parser_trailing_type_id (cp_parser *);
1727 static tree cp_parser_type_id_1
1728   (cp_parser *, bool, bool);
1729 static void cp_parser_type_specifier_seq
1730   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1731 static tree cp_parser_parameter_declaration_clause
1732   (cp_parser *);
1733 static tree cp_parser_parameter_declaration_list
1734   (cp_parser *, bool *);
1735 static cp_parameter_declarator *cp_parser_parameter_declaration
1736   (cp_parser *, bool, bool *);
1737 static tree cp_parser_default_argument 
1738   (cp_parser *, bool);
1739 static void cp_parser_function_body
1740   (cp_parser *);
1741 static tree cp_parser_initializer
1742   (cp_parser *, bool *, bool *);
1743 static tree cp_parser_initializer_clause
1744   (cp_parser *, bool *);
1745 static tree cp_parser_braced_list
1746   (cp_parser*, bool*);
1747 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1748   (cp_parser *, bool *);
1749
1750 static bool cp_parser_ctor_initializer_opt_and_function_body
1751   (cp_parser *);
1752
1753 /* Classes [gram.class] */
1754
1755 static tree cp_parser_class_name
1756   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1757 static tree cp_parser_class_specifier
1758   (cp_parser *);
1759 static tree cp_parser_class_head
1760   (cp_parser *, bool *, tree *, tree *);
1761 static enum tag_types cp_parser_class_key
1762   (cp_parser *);
1763 static void cp_parser_member_specification_opt
1764   (cp_parser *);
1765 static void cp_parser_member_declaration
1766   (cp_parser *);
1767 static tree cp_parser_pure_specifier
1768   (cp_parser *);
1769 static tree cp_parser_constant_initializer
1770   (cp_parser *);
1771
1772 /* Derived classes [gram.class.derived] */
1773
1774 static tree cp_parser_base_clause
1775   (cp_parser *);
1776 static tree cp_parser_base_specifier
1777   (cp_parser *);
1778
1779 /* Special member functions [gram.special] */
1780
1781 static tree cp_parser_conversion_function_id
1782   (cp_parser *);
1783 static tree cp_parser_conversion_type_id
1784   (cp_parser *);
1785 static cp_declarator *cp_parser_conversion_declarator_opt
1786   (cp_parser *);
1787 static bool cp_parser_ctor_initializer_opt
1788   (cp_parser *);
1789 static void cp_parser_mem_initializer_list
1790   (cp_parser *);
1791 static tree cp_parser_mem_initializer
1792   (cp_parser *);
1793 static tree cp_parser_mem_initializer_id
1794   (cp_parser *);
1795
1796 /* Overloading [gram.over] */
1797
1798 static tree cp_parser_operator_function_id
1799   (cp_parser *);
1800 static tree cp_parser_operator
1801   (cp_parser *);
1802
1803 /* Templates [gram.temp] */
1804
1805 static void cp_parser_template_declaration
1806   (cp_parser *, bool);
1807 static tree cp_parser_template_parameter_list
1808   (cp_parser *);
1809 static tree cp_parser_template_parameter
1810   (cp_parser *, bool *, bool *);
1811 static tree cp_parser_type_parameter
1812   (cp_parser *, bool *);
1813 static tree cp_parser_template_id
1814   (cp_parser *, bool, bool, bool);
1815 static tree cp_parser_template_name
1816   (cp_parser *, bool, bool, bool, bool *);
1817 static tree cp_parser_template_argument_list
1818   (cp_parser *);
1819 static tree cp_parser_template_argument
1820   (cp_parser *);
1821 static void cp_parser_explicit_instantiation
1822   (cp_parser *);
1823 static void cp_parser_explicit_specialization
1824   (cp_parser *);
1825
1826 /* Exception handling [gram.exception] */
1827
1828 static tree cp_parser_try_block
1829   (cp_parser *);
1830 static bool cp_parser_function_try_block
1831   (cp_parser *);
1832 static void cp_parser_handler_seq
1833   (cp_parser *);
1834 static void cp_parser_handler
1835   (cp_parser *);
1836 static tree cp_parser_exception_declaration
1837   (cp_parser *);
1838 static tree cp_parser_throw_expression
1839   (cp_parser *);
1840 static tree cp_parser_exception_specification_opt
1841   (cp_parser *);
1842 static tree cp_parser_type_id_list
1843   (cp_parser *);
1844
1845 /* GNU Extensions */
1846
1847 static tree cp_parser_asm_specification_opt
1848   (cp_parser *);
1849 static tree cp_parser_asm_operand_list
1850   (cp_parser *);
1851 static tree cp_parser_asm_clobber_list
1852   (cp_parser *);
1853 static tree cp_parser_asm_label_list
1854   (cp_parser *);
1855 static tree cp_parser_attributes_opt
1856   (cp_parser *);
1857 static tree cp_parser_attribute_list
1858   (cp_parser *);
1859 static bool cp_parser_extension_opt
1860   (cp_parser *, int *);
1861 static void cp_parser_label_declaration
1862   (cp_parser *);
1863
1864 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1865 static bool cp_parser_pragma
1866   (cp_parser *, enum pragma_context);
1867
1868 /* Objective-C++ Productions */
1869
1870 static tree cp_parser_objc_message_receiver
1871   (cp_parser *);
1872 static tree cp_parser_objc_message_args
1873   (cp_parser *);
1874 static tree cp_parser_objc_message_expression
1875   (cp_parser *);
1876 static tree cp_parser_objc_encode_expression
1877   (cp_parser *);
1878 static tree cp_parser_objc_defs_expression
1879   (cp_parser *);
1880 static tree cp_parser_objc_protocol_expression
1881   (cp_parser *);
1882 static tree cp_parser_objc_selector_expression
1883   (cp_parser *);
1884 static tree cp_parser_objc_expression
1885   (cp_parser *);
1886 static bool cp_parser_objc_selector_p
1887   (enum cpp_ttype);
1888 static tree cp_parser_objc_selector
1889   (cp_parser *);
1890 static tree cp_parser_objc_protocol_refs_opt
1891   (cp_parser *);
1892 static void cp_parser_objc_declaration
1893   (cp_parser *, tree);
1894 static tree cp_parser_objc_statement
1895   (cp_parser *);
1896 static bool cp_parser_objc_valid_prefix_attributes
1897   (cp_parser *, tree *);
1898 static void cp_parser_objc_at_property_declaration 
1899   (cp_parser *) ;
1900 static void cp_parser_objc_at_synthesize_declaration 
1901   (cp_parser *) ;
1902 static void cp_parser_objc_at_dynamic_declaration
1903   (cp_parser *) ;
1904 static tree cp_parser_objc_struct_declaration
1905   (cp_parser *) ;
1906
1907 /* Utility Routines */
1908
1909 static tree cp_parser_lookup_name
1910   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1911 static tree cp_parser_lookup_name_simple
1912   (cp_parser *, tree, location_t);
1913 static tree cp_parser_maybe_treat_template_as_class
1914   (tree, bool);
1915 static bool cp_parser_check_declarator_template_parameters
1916   (cp_parser *, cp_declarator *, location_t);
1917 static bool cp_parser_check_template_parameters
1918   (cp_parser *, unsigned, location_t, cp_declarator *);
1919 static tree cp_parser_simple_cast_expression
1920   (cp_parser *);
1921 static tree cp_parser_global_scope_opt
1922   (cp_parser *, bool);
1923 static bool cp_parser_constructor_declarator_p
1924   (cp_parser *, bool);
1925 static tree cp_parser_function_definition_from_specifiers_and_declarator
1926   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1927 static tree cp_parser_function_definition_after_declarator
1928   (cp_parser *, bool);
1929 static void cp_parser_template_declaration_after_export
1930   (cp_parser *, bool);
1931 static void cp_parser_perform_template_parameter_access_checks
1932   (VEC (deferred_access_check,gc)*);
1933 static tree cp_parser_single_declaration
1934   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1935 static tree cp_parser_functional_cast
1936   (cp_parser *, tree);
1937 static tree cp_parser_save_member_function_body
1938   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1939 static tree cp_parser_enclosed_template_argument_list
1940   (cp_parser *);
1941 static void cp_parser_save_default_args
1942   (cp_parser *, tree);
1943 static void cp_parser_late_parsing_for_member
1944   (cp_parser *, tree);
1945 static void cp_parser_late_parsing_default_args
1946   (cp_parser *, tree);
1947 static tree cp_parser_sizeof_operand
1948   (cp_parser *, enum rid);
1949 static tree cp_parser_trait_expr
1950   (cp_parser *, enum rid);
1951 static bool cp_parser_declares_only_class_p
1952   (cp_parser *);
1953 static void cp_parser_set_storage_class
1954   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1955 static void cp_parser_set_decl_spec_type
1956   (cp_decl_specifier_seq *, tree, location_t, bool);
1957 static bool cp_parser_friend_p
1958   (const cp_decl_specifier_seq *);
1959 static void cp_parser_required_error
1960   (cp_parser *, required_token, bool);
1961 static cp_token *cp_parser_require
1962   (cp_parser *, enum cpp_ttype, required_token);
1963 static cp_token *cp_parser_require_keyword
1964   (cp_parser *, enum rid, required_token);
1965 static bool cp_parser_token_starts_function_definition_p
1966   (cp_token *);
1967 static bool cp_parser_next_token_starts_class_definition_p
1968   (cp_parser *);
1969 static bool cp_parser_next_token_ends_template_argument_p
1970   (cp_parser *);
1971 static bool cp_parser_nth_token_starts_template_argument_list_p
1972   (cp_parser *, size_t);
1973 static enum tag_types cp_parser_token_is_class_key
1974   (cp_token *);
1975 static void cp_parser_check_class_key
1976   (enum tag_types, tree type);
1977 static void cp_parser_check_access_in_redeclaration
1978   (tree type, location_t location);
1979 static bool cp_parser_optional_template_keyword
1980   (cp_parser *);
1981 static void cp_parser_pre_parsed_nested_name_specifier
1982   (cp_parser *);
1983 static bool cp_parser_cache_group
1984   (cp_parser *, enum cpp_ttype, unsigned);
1985 static void cp_parser_parse_tentatively
1986   (cp_parser *);
1987 static void cp_parser_commit_to_tentative_parse
1988   (cp_parser *);
1989 static void cp_parser_abort_tentative_parse
1990   (cp_parser *);
1991 static bool cp_parser_parse_definitely
1992   (cp_parser *);
1993 static inline bool cp_parser_parsing_tentatively
1994   (cp_parser *);
1995 static bool cp_parser_uncommitted_to_tentative_parse_p
1996   (cp_parser *);
1997 static void cp_parser_error
1998   (cp_parser *, const char *);
1999 static void cp_parser_name_lookup_error
2000   (cp_parser *, tree, tree, name_lookup_error, location_t);
2001 static bool cp_parser_simulate_error
2002   (cp_parser *);
2003 static bool cp_parser_check_type_definition
2004   (cp_parser *);
2005 static void cp_parser_check_for_definition_in_return_type
2006   (cp_declarator *, tree, location_t type_location);
2007 static void cp_parser_check_for_invalid_template_id
2008   (cp_parser *, tree, location_t location);
2009 static bool cp_parser_non_integral_constant_expression
2010   (cp_parser *, non_integral_constant);
2011 static void cp_parser_diagnose_invalid_type_name
2012   (cp_parser *, tree, tree, location_t);
2013 static bool cp_parser_parse_and_diagnose_invalid_type_name
2014   (cp_parser *);
2015 static int cp_parser_skip_to_closing_parenthesis
2016   (cp_parser *, bool, bool, bool);
2017 static void cp_parser_skip_to_end_of_statement
2018   (cp_parser *);
2019 static void cp_parser_consume_semicolon_at_end_of_statement
2020   (cp_parser *);
2021 static void cp_parser_skip_to_end_of_block_or_statement
2022   (cp_parser *);
2023 static bool cp_parser_skip_to_closing_brace
2024   (cp_parser *);
2025 static void cp_parser_skip_to_end_of_template_parameter_list
2026   (cp_parser *);
2027 static void cp_parser_skip_to_pragma_eol
2028   (cp_parser*, cp_token *);
2029 static bool cp_parser_error_occurred
2030   (cp_parser *);
2031 static bool cp_parser_allow_gnu_extensions_p
2032   (cp_parser *);
2033 static bool cp_parser_is_string_literal
2034   (cp_token *);
2035 static bool cp_parser_is_keyword
2036   (cp_token *, enum rid);
2037 static tree cp_parser_make_typename_type
2038   (cp_parser *, tree, tree, location_t location);
2039 static cp_declarator * cp_parser_make_indirect_declarator
2040   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2041
2042 /* Returns nonzero if we are parsing tentatively.  */
2043
2044 static inline bool
2045 cp_parser_parsing_tentatively (cp_parser* parser)
2046 {
2047   return parser->context->next != NULL;
2048 }
2049
2050 /* Returns nonzero if TOKEN is a string literal.  */
2051
2052 static bool
2053 cp_parser_is_string_literal (cp_token* token)
2054 {
2055   return (token->type == CPP_STRING ||
2056           token->type == CPP_STRING16 ||
2057           token->type == CPP_STRING32 ||
2058           token->type == CPP_WSTRING ||
2059           token->type == CPP_UTF8STRING);
2060 }
2061
2062 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2063
2064 static bool
2065 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2066 {
2067   return token->keyword == keyword;
2068 }
2069
2070 /* If not parsing tentatively, issue a diagnostic of the form
2071       FILE:LINE: MESSAGE before TOKEN
2072    where TOKEN is the next token in the input stream.  MESSAGE
2073    (specified by the caller) is usually of the form "expected
2074    OTHER-TOKEN".  */
2075
2076 static void
2077 cp_parser_error (cp_parser* parser, const char* gmsgid)
2078 {
2079   if (!cp_parser_simulate_error (parser))
2080     {
2081       cp_token *token = cp_lexer_peek_token (parser->lexer);
2082       /* This diagnostic makes more sense if it is tagged to the line
2083          of the token we just peeked at.  */
2084       cp_lexer_set_source_position_from_token (token);
2085
2086       if (token->type == CPP_PRAGMA)
2087         {
2088           error_at (token->location,
2089                     "%<#pragma%> is not allowed here");
2090           cp_parser_skip_to_pragma_eol (parser, token);
2091           return;
2092         }
2093
2094       c_parse_error (gmsgid,
2095                      /* Because c_parser_error does not understand
2096                         CPP_KEYWORD, keywords are treated like
2097                         identifiers.  */
2098                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2099                      token->u.value, token->flags);
2100     }
2101 }
2102
2103 /* Issue an error about name-lookup failing.  NAME is the
2104    IDENTIFIER_NODE DECL is the result of
2105    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2106    the thing that we hoped to find.  */
2107
2108 static void
2109 cp_parser_name_lookup_error (cp_parser* parser,
2110                              tree name,
2111                              tree decl,
2112                              name_lookup_error desired,
2113                              location_t location)
2114 {
2115   /* If name lookup completely failed, tell the user that NAME was not
2116      declared.  */
2117   if (decl == error_mark_node)
2118     {
2119       if (parser->scope && parser->scope != global_namespace)
2120         error_at (location, "%<%E::%E%> has not been declared",
2121                   parser->scope, name);
2122       else if (parser->scope == global_namespace)
2123         error_at (location, "%<::%E%> has not been declared", name);
2124       else if (parser->object_scope
2125                && !CLASS_TYPE_P (parser->object_scope))
2126         error_at (location, "request for member %qE in non-class type %qT",
2127                   name, parser->object_scope);
2128       else if (parser->object_scope)
2129         error_at (location, "%<%T::%E%> has not been declared",
2130                   parser->object_scope, name);
2131       else
2132         error_at (location, "%qE has not been declared", name);
2133     }
2134   else if (parser->scope && parser->scope != global_namespace)
2135     {
2136       switch (desired)
2137         {
2138           case NLE_TYPE:
2139             error_at (location, "%<%E::%E%> is not a type",
2140                                 parser->scope, name);
2141             break;
2142           case NLE_CXX98:
2143             error_at (location, "%<%E::%E%> is not a class or namespace",
2144                                 parser->scope, name);
2145             break;
2146           case NLE_NOT_CXX98:
2147             error_at (location,
2148                       "%<%E::%E%> is not a class, namespace, or enumeration",
2149                       parser->scope, name);
2150             break;
2151           default:
2152             gcc_unreachable ();
2153             
2154         }
2155     }
2156   else if (parser->scope == global_namespace)
2157     {
2158       switch (desired)
2159         {
2160           case NLE_TYPE:
2161             error_at (location, "%<::%E%> is not a type", name);
2162             break;
2163           case NLE_CXX98:
2164             error_at (location, "%<::%E%> is not a class or namespace", name);
2165             break;
2166           case NLE_NOT_CXX98:
2167             error_at (location,
2168                       "%<::%E%> is not a class, namespace, or enumeration",
2169                       name);
2170             break;
2171           default:
2172             gcc_unreachable ();
2173         }
2174     }
2175   else
2176     {
2177       switch (desired)
2178         {
2179           case NLE_TYPE:
2180             error_at (location, "%qE is not a type", name);
2181             break;
2182           case NLE_CXX98:
2183             error_at (location, "%qE is not a class or namespace", name);
2184             break;
2185           case NLE_NOT_CXX98:
2186             error_at (location,
2187                       "%qE is not a class, namespace, or enumeration", name);
2188             break;
2189           default:
2190             gcc_unreachable ();
2191         }
2192     }
2193 }
2194
2195 /* If we are parsing tentatively, remember that an error has occurred
2196    during this tentative parse.  Returns true if the error was
2197    simulated; false if a message should be issued by the caller.  */
2198
2199 static bool
2200 cp_parser_simulate_error (cp_parser* parser)
2201 {
2202   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2203     {
2204       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2205       return true;
2206     }
2207   return false;
2208 }
2209
2210 /* Check for repeated decl-specifiers.  */
2211
2212 static void
2213 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2214                            location_t location)
2215 {
2216   int ds;
2217
2218   for (ds = ds_first; ds != ds_last; ++ds)
2219     {
2220       unsigned count = decl_specs->specs[ds];
2221       if (count < 2)
2222         continue;
2223       /* The "long" specifier is a special case because of "long long".  */
2224       if (ds == ds_long)
2225         {
2226           if (count > 2)
2227             error_at (location, "%<long long long%> is too long for GCC");
2228           else 
2229             pedwarn_cxx98 (location, OPT_Wlong_long, 
2230                            "ISO C++ 1998 does not support %<long long%>");
2231         }
2232       else if (count > 1)
2233         {
2234           static const char *const decl_spec_names[] = {
2235             "signed",
2236             "unsigned",
2237             "short",
2238             "long",
2239             "const",
2240             "volatile",
2241             "restrict",
2242             "inline",
2243             "virtual",
2244             "explicit",
2245             "friend",
2246             "typedef",
2247             "constexpr",
2248             "__complex",
2249             "__thread"
2250           };
2251           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2252         }
2253     }
2254 }
2255
2256 /* This function is called when a type is defined.  If type
2257    definitions are forbidden at this point, an error message is
2258    issued.  */
2259
2260 static bool
2261 cp_parser_check_type_definition (cp_parser* parser)
2262 {
2263   /* If types are forbidden here, issue a message.  */
2264   if (parser->type_definition_forbidden_message)
2265     {
2266       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2267          in the message need to be interpreted.  */
2268       error (parser->type_definition_forbidden_message);
2269       return false;
2270     }
2271   return true;
2272 }
2273
2274 /* This function is called when the DECLARATOR is processed.  The TYPE
2275    was a type defined in the decl-specifiers.  If it is invalid to
2276    define a type in the decl-specifiers for DECLARATOR, an error is
2277    issued. TYPE_LOCATION is the location of TYPE and is used
2278    for error reporting.  */
2279
2280 static void
2281 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2282                                                tree type, location_t type_location)
2283 {
2284   /* [dcl.fct] forbids type definitions in return types.
2285      Unfortunately, it's not easy to know whether or not we are
2286      processing a return type until after the fact.  */
2287   while (declarator
2288          && (declarator->kind == cdk_pointer
2289              || declarator->kind == cdk_reference
2290              || declarator->kind == cdk_ptrmem))
2291     declarator = declarator->declarator;
2292   if (declarator
2293       && declarator->kind == cdk_function)
2294     {
2295       error_at (type_location,
2296                 "new types may not be defined in a return type");
2297       inform (type_location, 
2298               "(perhaps a semicolon is missing after the definition of %qT)",
2299               type);
2300     }
2301 }
2302
2303 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2304    "<" in any valid C++ program.  If the next token is indeed "<",
2305    issue a message warning the user about what appears to be an
2306    invalid attempt to form a template-id. LOCATION is the location
2307    of the type-specifier (TYPE) */
2308
2309 static void
2310 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2311                                          tree type, location_t location)
2312 {
2313   cp_token_position start = 0;
2314
2315   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2316     {
2317       if (TYPE_P (type))
2318         error_at (location, "%qT is not a template", type);
2319       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2320         error_at (location, "%qE is not a template", type);
2321       else
2322         error_at (location, "invalid template-id");
2323       /* Remember the location of the invalid "<".  */
2324       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2325         start = cp_lexer_token_position (parser->lexer, true);
2326       /* Consume the "<".  */
2327       cp_lexer_consume_token (parser->lexer);
2328       /* Parse the template arguments.  */
2329       cp_parser_enclosed_template_argument_list (parser);
2330       /* Permanently remove the invalid template arguments so that
2331          this error message is not issued again.  */
2332       if (start)
2333         cp_lexer_purge_tokens_after (parser->lexer, start);
2334     }
2335 }
2336
2337 /* If parsing an integral constant-expression, issue an error message
2338    about the fact that THING appeared and return true.  Otherwise,
2339    return false.  In either case, set
2340    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2341
2342 static bool
2343 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2344                                             non_integral_constant thing)
2345 {
2346   parser->non_integral_constant_expression_p = true;
2347   if (parser->integral_constant_expression_p)
2348     {
2349       if (!parser->allow_non_integral_constant_expression_p)
2350         {
2351           const char *msg = NULL;
2352           switch (thing)
2353             {
2354               case NIC_FLOAT:
2355                 error ("floating-point literal "
2356                        "cannot appear in a constant-expression");
2357                 return true;
2358               case NIC_CAST:
2359                 error ("a cast to a type other than an integral or "
2360                        "enumeration type cannot appear in a "
2361                        "constant-expression");
2362                 return true;
2363               case NIC_TYPEID:
2364                 error ("%<typeid%> operator "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_NCC:
2368                 error ("non-constant compound literals "
2369                        "cannot appear in a constant-expression");
2370                 return true;
2371               case NIC_FUNC_CALL:
2372                 error ("a function call "
2373                        "cannot appear in a constant-expression");
2374                 return true;
2375               case NIC_INC:
2376                 error ("an increment "
2377                        "cannot appear in a constant-expression");
2378                 return true;
2379               case NIC_DEC:
2380                 error ("an decrement "
2381                        "cannot appear in a constant-expression");
2382                 return true;
2383               case NIC_ARRAY_REF:
2384                 error ("an array reference "
2385                        "cannot appear in a constant-expression");
2386                 return true;
2387               case NIC_ADDR_LABEL:
2388                 error ("the address of a label "
2389                        "cannot appear in a constant-expression");
2390                 return true;
2391               case NIC_OVERLOADED:
2392                 error ("calls to overloaded operators "
2393                        "cannot appear in a constant-expression");
2394                 return true;
2395               case NIC_ASSIGNMENT:
2396                 error ("an assignment cannot appear in a constant-expression");
2397                 return true;
2398               case NIC_COMMA:
2399                 error ("a comma operator "
2400                        "cannot appear in a constant-expression");
2401                 return true;
2402               case NIC_CONSTRUCTOR:
2403                 error ("a call to a constructor "
2404                        "cannot appear in a constant-expression");
2405                 return true;
2406               case NIC_THIS:
2407                 msg = "this";
2408                 break;
2409               case NIC_FUNC_NAME:
2410                 msg = "__FUNCTION__";
2411                 break;
2412               case NIC_PRETTY_FUNC:
2413                 msg = "__PRETTY_FUNCTION__";
2414                 break;
2415               case NIC_C99_FUNC:
2416                 msg = "__func__";
2417                 break;
2418               case NIC_VA_ARG:
2419                 msg = "va_arg";
2420                 break;
2421               case NIC_ARROW:
2422                 msg = "->";
2423                 break;
2424               case NIC_POINT:
2425                 msg = ".";
2426                 break;
2427               case NIC_STAR:
2428                 msg = "*";
2429                 break;
2430               case NIC_ADDR:
2431                 msg = "&";
2432                 break;
2433               case NIC_PREINCREMENT:
2434                 msg = "++";
2435                 break;
2436               case NIC_PREDECREMENT:
2437                 msg = "--";
2438                 break;
2439               case NIC_NEW:
2440                 msg = "new";
2441                 break;
2442               case NIC_DEL:
2443                 msg = "delete";
2444                 break;
2445               default:
2446                 gcc_unreachable ();
2447             }
2448           if (msg)
2449             error ("%qs cannot appear in a constant-expression", msg);
2450           return true;
2451         }
2452     }
2453   return false;
2454 }
2455
2456 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2457    qualifying scope (or NULL, if none) for ID.  This function commits
2458    to the current active tentative parse, if any.  (Otherwise, the
2459    problematic construct might be encountered again later, resulting
2460    in duplicate error messages.) LOCATION is the location of ID.  */
2461
2462 static void
2463 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2464                                       tree scope, tree id,
2465                                       location_t location)
2466 {
2467   tree decl, old_scope;
2468   cp_parser_commit_to_tentative_parse (parser);
2469   /* Try to lookup the identifier.  */
2470   old_scope = parser->scope;
2471   parser->scope = scope;
2472   decl = cp_parser_lookup_name_simple (parser, id, location);
2473   parser->scope = old_scope;
2474   /* If the lookup found a template-name, it means that the user forgot
2475   to specify an argument list. Emit a useful error message.  */
2476   if (TREE_CODE (decl) == TEMPLATE_DECL)
2477     error_at (location,
2478               "invalid use of template-name %qE without an argument list",
2479               decl);
2480   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2481     error_at (location, "invalid use of destructor %qD as a type", id);
2482   else if (TREE_CODE (decl) == TYPE_DECL)
2483     /* Something like 'unsigned A a;'  */
2484     error_at (location, "invalid combination of multiple type-specifiers");
2485   else if (!parser->scope)
2486     {
2487       /* Issue an error message.  */
2488       error_at (location, "%qE does not name a type", id);
2489       /* If we're in a template class, it's possible that the user was
2490          referring to a type from a base class.  For example:
2491
2492            template <typename T> struct A { typedef T X; };
2493            template <typename T> struct B : public A<T> { X x; };
2494
2495          The user should have said "typename A<T>::X".  */
2496       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2497         inform (location, "C++0x %<constexpr%> only available with "
2498                 "-std=c++0x or -std=gnu++0x");
2499       else if (processing_template_decl && current_class_type
2500                && TYPE_BINFO (current_class_type))
2501         {
2502           tree b;
2503
2504           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2505                b;
2506                b = TREE_CHAIN (b))
2507             {
2508               tree base_type = BINFO_TYPE (b);
2509               if (CLASS_TYPE_P (base_type)
2510                   && dependent_type_p (base_type))
2511                 {
2512                   tree field;
2513                   /* Go from a particular instantiation of the
2514                      template (which will have an empty TYPE_FIELDs),
2515                      to the main version.  */
2516                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2517                   for (field = TYPE_FIELDS (base_type);
2518                        field;
2519                        field = DECL_CHAIN (field))
2520                     if (TREE_CODE (field) == TYPE_DECL
2521                         && DECL_NAME (field) == id)
2522                       {
2523                         inform (location, 
2524                                 "(perhaps %<typename %T::%E%> was intended)",
2525                                 BINFO_TYPE (b), id);
2526                         break;
2527                       }
2528                   if (field)
2529                     break;
2530                 }
2531             }
2532         }
2533     }
2534   /* Here we diagnose qualified-ids where the scope is actually correct,
2535      but the identifier does not resolve to a valid type name.  */
2536   else if (parser->scope != error_mark_node)
2537     {
2538       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2539         error_at (location, "%qE in namespace %qE does not name a type",
2540                   id, parser->scope);
2541       else if (CLASS_TYPE_P (parser->scope)
2542                && constructor_name_p (id, parser->scope))
2543         {
2544           /* A<T>::A<T>() */
2545           error_at (location, "%<%T::%E%> names the constructor, not"
2546                     " the type", parser->scope, id);
2547           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2548             error_at (location, "and %qT has no template constructors",
2549                       parser->scope);
2550         }
2551       else if (TYPE_P (parser->scope)
2552                && dependent_scope_p (parser->scope))
2553         error_at (location, "need %<typename%> before %<%T::%E%> because "
2554                   "%qT is a dependent scope",
2555                   parser->scope, id, parser->scope);
2556       else if (TYPE_P (parser->scope))
2557         error_at (location, "%qE in %q#T does not name a type",
2558                   id, parser->scope);
2559       else
2560         gcc_unreachable ();
2561     }
2562 }
2563
2564 /* Check for a common situation where a type-name should be present,
2565    but is not, and issue a sensible error message.  Returns true if an
2566    invalid type-name was detected.
2567
2568    The situation handled by this function are variable declarations of the
2569    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2570    Usually, `ID' should name a type, but if we got here it means that it
2571    does not. We try to emit the best possible error message depending on
2572    how exactly the id-expression looks like.  */
2573
2574 static bool
2575 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2576 {
2577   tree id;
2578   cp_token *token = cp_lexer_peek_token (parser->lexer);
2579
2580   /* Avoid duplicate error about ambiguous lookup.  */
2581   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2582     {
2583       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2584       if (next->type == CPP_NAME && next->ambiguous_p)
2585         goto out;
2586     }
2587
2588   cp_parser_parse_tentatively (parser);
2589   id = cp_parser_id_expression (parser,
2590                                 /*template_keyword_p=*/false,
2591                                 /*check_dependency_p=*/true,
2592                                 /*template_p=*/NULL,
2593                                 /*declarator_p=*/true,
2594                                 /*optional_p=*/false);
2595   /* If the next token is a (, this is a function with no explicit return
2596      type, i.e. constructor, destructor or conversion op.  */
2597   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2598       || TREE_CODE (id) == TYPE_DECL)
2599     {
2600       cp_parser_abort_tentative_parse (parser);
2601       return false;
2602     }
2603   if (!cp_parser_parse_definitely (parser))
2604     return false;
2605
2606   /* Emit a diagnostic for the invalid type.  */
2607   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2608                                         id, token->location);
2609  out:
2610   /* If we aren't in the middle of a declarator (i.e. in a
2611      parameter-declaration-clause), skip to the end of the declaration;
2612      there's no point in trying to process it.  */
2613   if (!parser->in_declarator_p)
2614     cp_parser_skip_to_end_of_block_or_statement (parser);
2615   return true;
2616 }
2617
2618 /* Consume tokens up to, and including, the next non-nested closing `)'.
2619    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2620    are doing error recovery. Returns -1 if OR_COMMA is true and we
2621    found an unnested comma.  */
2622
2623 static int
2624 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2625                                        bool recovering,
2626                                        bool or_comma,
2627                                        bool consume_paren)
2628 {
2629   unsigned paren_depth = 0;
2630   unsigned brace_depth = 0;
2631   unsigned square_depth = 0;
2632
2633   if (recovering && !or_comma
2634       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2635     return 0;
2636
2637   while (true)
2638     {
2639       cp_token * token = cp_lexer_peek_token (parser->lexer);
2640
2641       switch (token->type)
2642         {
2643         case CPP_EOF:
2644         case CPP_PRAGMA_EOL:
2645           /* If we've run out of tokens, then there is no closing `)'.  */
2646           return 0;
2647
2648         /* This is good for lambda expression capture-lists.  */
2649         case CPP_OPEN_SQUARE:
2650           ++square_depth;
2651           break;
2652         case CPP_CLOSE_SQUARE:
2653           if (!square_depth--)
2654             return 0;
2655           break;
2656
2657         case CPP_SEMICOLON:
2658           /* This matches the processing in skip_to_end_of_statement.  */
2659           if (!brace_depth)
2660             return 0;
2661           break;
2662
2663         case CPP_OPEN_BRACE:
2664           ++brace_depth;
2665           break;
2666         case CPP_CLOSE_BRACE:
2667           if (!brace_depth--)
2668             return 0;
2669           break;
2670
2671         case CPP_COMMA:
2672           if (recovering && or_comma && !brace_depth && !paren_depth
2673               && !square_depth)
2674             return -1;
2675           break;
2676
2677         case CPP_OPEN_PAREN:
2678           if (!brace_depth)
2679             ++paren_depth;
2680           break;
2681
2682         case CPP_CLOSE_PAREN:
2683           if (!brace_depth && !paren_depth--)
2684             {
2685               if (consume_paren)
2686                 cp_lexer_consume_token (parser->lexer);
2687               return 1;
2688             }
2689           break;
2690
2691         default:
2692           break;
2693         }
2694
2695       /* Consume the token.  */
2696       cp_lexer_consume_token (parser->lexer);
2697     }
2698 }
2699
2700 /* Consume tokens until we reach the end of the current statement.
2701    Normally, that will be just before consuming a `;'.  However, if a
2702    non-nested `}' comes first, then we stop before consuming that.  */
2703
2704 static void
2705 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2706 {
2707   unsigned nesting_depth = 0;
2708
2709   while (true)
2710     {
2711       cp_token *token = cp_lexer_peek_token (parser->lexer);
2712
2713       switch (token->type)
2714         {
2715         case CPP_EOF:
2716         case CPP_PRAGMA_EOL:
2717           /* If we've run out of tokens, stop.  */
2718           return;
2719
2720         case CPP_SEMICOLON:
2721           /* If the next token is a `;', we have reached the end of the
2722              statement.  */
2723           if (!nesting_depth)
2724             return;
2725           break;
2726
2727         case CPP_CLOSE_BRACE:
2728           /* If this is a non-nested '}', stop before consuming it.
2729              That way, when confronted with something like:
2730
2731                { 3 + }
2732
2733              we stop before consuming the closing '}', even though we
2734              have not yet reached a `;'.  */
2735           if (nesting_depth == 0)
2736             return;
2737
2738           /* If it is the closing '}' for a block that we have
2739              scanned, stop -- but only after consuming the token.
2740              That way given:
2741
2742                 void f g () { ... }
2743                 typedef int I;
2744
2745              we will stop after the body of the erroneously declared
2746              function, but before consuming the following `typedef'
2747              declaration.  */
2748           if (--nesting_depth == 0)
2749             {
2750               cp_lexer_consume_token (parser->lexer);
2751               return;
2752             }
2753
2754         case CPP_OPEN_BRACE:
2755           ++nesting_depth;
2756           break;
2757
2758         default:
2759           break;
2760         }
2761
2762       /* Consume the token.  */
2763       cp_lexer_consume_token (parser->lexer);
2764     }
2765 }
2766
2767 /* This function is called at the end of a statement or declaration.
2768    If the next token is a semicolon, it is consumed; otherwise, error
2769    recovery is attempted.  */
2770
2771 static void
2772 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2773 {
2774   /* Look for the trailing `;'.  */
2775   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2776     {
2777       /* If there is additional (erroneous) input, skip to the end of
2778          the statement.  */
2779       cp_parser_skip_to_end_of_statement (parser);
2780       /* If the next token is now a `;', consume it.  */
2781       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2782         cp_lexer_consume_token (parser->lexer);
2783     }
2784 }
2785
2786 /* Skip tokens until we have consumed an entire block, or until we
2787    have consumed a non-nested `;'.  */
2788
2789 static void
2790 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2791 {
2792   int nesting_depth = 0;
2793
2794   while (nesting_depth >= 0)
2795     {
2796       cp_token *token = cp_lexer_peek_token (parser->lexer);
2797
2798       switch (token->type)
2799         {
2800         case CPP_EOF:
2801         case CPP_PRAGMA_EOL:
2802           /* If we've run out of tokens, stop.  */
2803           return;
2804
2805         case CPP_SEMICOLON:
2806           /* Stop if this is an unnested ';'. */
2807           if (!nesting_depth)
2808             nesting_depth = -1;
2809           break;
2810
2811         case CPP_CLOSE_BRACE:
2812           /* Stop if this is an unnested '}', or closes the outermost
2813              nesting level.  */
2814           nesting_depth--;
2815           if (nesting_depth < 0)
2816             return;
2817           if (!nesting_depth)
2818             nesting_depth = -1;
2819           break;
2820
2821         case CPP_OPEN_BRACE:
2822           /* Nest. */
2823           nesting_depth++;
2824           break;
2825
2826         default:
2827           break;
2828         }
2829
2830       /* Consume the token.  */
2831       cp_lexer_consume_token (parser->lexer);
2832     }
2833 }
2834
2835 /* Skip tokens until a non-nested closing curly brace is the next
2836    token, or there are no more tokens. Return true in the first case,
2837    false otherwise.  */
2838
2839 static bool
2840 cp_parser_skip_to_closing_brace (cp_parser *parser)
2841 {
2842   unsigned nesting_depth = 0;
2843
2844   while (true)
2845     {
2846       cp_token *token = cp_lexer_peek_token (parser->lexer);
2847
2848       switch (token->type)
2849         {
2850         case CPP_EOF:
2851         case CPP_PRAGMA_EOL:
2852           /* If we've run out of tokens, stop.  */
2853           return false;
2854
2855         case CPP_CLOSE_BRACE:
2856           /* If the next token is a non-nested `}', then we have reached
2857              the end of the current block.  */
2858           if (nesting_depth-- == 0)
2859             return true;
2860           break;
2861
2862         case CPP_OPEN_BRACE:
2863           /* If it the next token is a `{', then we are entering a new
2864              block.  Consume the entire block.  */
2865           ++nesting_depth;
2866           break;
2867
2868         default:
2869           break;
2870         }
2871
2872       /* Consume the token.  */
2873       cp_lexer_consume_token (parser->lexer);
2874     }
2875 }
2876
2877 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2878    parameter is the PRAGMA token, allowing us to purge the entire pragma
2879    sequence.  */
2880
2881 static void
2882 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2883 {
2884   cp_token *token;
2885
2886   parser->lexer->in_pragma = false;
2887
2888   do
2889     token = cp_lexer_consume_token (parser->lexer);
2890   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2891
2892   /* Ensure that the pragma is not parsed again.  */
2893   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2894 }
2895
2896 /* Require pragma end of line, resyncing with it as necessary.  The
2897    arguments are as for cp_parser_skip_to_pragma_eol.  */
2898
2899 static void
2900 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2901 {
2902   parser->lexer->in_pragma = false;
2903   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2904     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2905 }
2906
2907 /* This is a simple wrapper around make_typename_type. When the id is
2908    an unresolved identifier node, we can provide a superior diagnostic
2909    using cp_parser_diagnose_invalid_type_name.  */
2910
2911 static tree
2912 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2913                               tree id, location_t id_location)
2914 {
2915   tree result;
2916   if (TREE_CODE (id) == IDENTIFIER_NODE)
2917     {
2918       result = make_typename_type (scope, id, typename_type,
2919                                    /*complain=*/tf_none);
2920       if (result == error_mark_node)
2921         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2922       return result;
2923     }
2924   return make_typename_type (scope, id, typename_type, tf_error);
2925 }
2926
2927 /* This is a wrapper around the
2928    make_{pointer,ptrmem,reference}_declarator functions that decides
2929    which one to call based on the CODE and CLASS_TYPE arguments. The
2930    CODE argument should be one of the values returned by
2931    cp_parser_ptr_operator. */
2932 static cp_declarator *
2933 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2934                                     cp_cv_quals cv_qualifiers,
2935                                     cp_declarator *target)
2936 {
2937   if (code == ERROR_MARK)
2938     return cp_error_declarator;
2939
2940   if (code == INDIRECT_REF)
2941     if (class_type == NULL_TREE)
2942       return make_pointer_declarator (cv_qualifiers, target);
2943     else
2944       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2945   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2946     return make_reference_declarator (cv_qualifiers, target, false);
2947   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2948     return make_reference_declarator (cv_qualifiers, target, true);
2949   gcc_unreachable ();
2950 }
2951
2952 /* Create a new C++ parser.  */
2953
2954 static cp_parser *
2955 cp_parser_new (void)
2956 {
2957   cp_parser *parser;
2958   cp_lexer *lexer;
2959   unsigned i;
2960
2961   /* cp_lexer_new_main is called before doing GC allocation because
2962      cp_lexer_new_main might load a PCH file.  */
2963   lexer = cp_lexer_new_main ();
2964
2965   /* Initialize the binops_by_token so that we can get the tree
2966      directly from the token.  */
2967   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2968     binops_by_token[binops[i].token_type] = binops[i];
2969
2970   parser = ggc_alloc_cleared_cp_parser ();
2971   parser->lexer = lexer;
2972   parser->context = cp_parser_context_new (NULL);
2973
2974   /* For now, we always accept GNU extensions.  */
2975   parser->allow_gnu_extensions_p = 1;
2976
2977   /* The `>' token is a greater-than operator, not the end of a
2978      template-id.  */
2979   parser->greater_than_is_operator_p = true;
2980
2981   parser->default_arg_ok_p = true;
2982
2983   /* We are not parsing a constant-expression.  */
2984   parser->integral_constant_expression_p = false;
2985   parser->allow_non_integral_constant_expression_p = false;
2986   parser->non_integral_constant_expression_p = false;
2987
2988   /* Local variable names are not forbidden.  */
2989   parser->local_variables_forbidden_p = false;
2990
2991   /* We are not processing an `extern "C"' declaration.  */
2992   parser->in_unbraced_linkage_specification_p = false;
2993
2994   /* We are not processing a declarator.  */
2995   parser->in_declarator_p = false;
2996
2997   /* We are not processing a template-argument-list.  */
2998   parser->in_template_argument_list_p = false;
2999
3000   /* We are not in an iteration statement.  */
3001   parser->in_statement = 0;
3002
3003   /* We are not in a switch statement.  */
3004   parser->in_switch_statement_p = false;
3005
3006   /* We are not parsing a type-id inside an expression.  */
3007   parser->in_type_id_in_expr_p = false;
3008
3009   /* Declarations aren't implicitly extern "C".  */
3010   parser->implicit_extern_c = false;
3011
3012   /* String literals should be translated to the execution character set.  */
3013   parser->translate_strings_p = true;
3014
3015   /* We are not parsing a function body.  */
3016   parser->in_function_body = false;
3017
3018   /* We can correct until told otherwise.  */
3019   parser->colon_corrects_to_scope_p = true;
3020
3021   /* The unparsed function queue is empty.  */
3022   push_unparsed_function_queues (parser);
3023
3024   /* There are no classes being defined.  */
3025   parser->num_classes_being_defined = 0;
3026
3027   /* No template parameters apply.  */
3028   parser->num_template_parameter_lists = 0;
3029
3030   return parser;
3031 }
3032
3033 /* Create a cp_lexer structure which will emit the tokens in CACHE
3034    and push it onto the parser's lexer stack.  This is used for delayed
3035    parsing of in-class method bodies and default arguments, and should
3036    not be confused with tentative parsing.  */
3037 static void
3038 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3039 {
3040   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3041   lexer->next = parser->lexer;
3042   parser->lexer = lexer;
3043
3044   /* Move the current source position to that of the first token in the
3045      new lexer.  */
3046   cp_lexer_set_source_position_from_token (lexer->next_token);
3047 }
3048
3049 /* Pop the top lexer off the parser stack.  This is never used for the
3050    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3051 static void
3052 cp_parser_pop_lexer (cp_parser *parser)
3053 {
3054   cp_lexer *lexer = parser->lexer;
3055   parser->lexer = lexer->next;
3056   cp_lexer_destroy (lexer);
3057
3058   /* Put the current source position back where it was before this
3059      lexer was pushed.  */
3060   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3061 }
3062
3063 /* Lexical conventions [gram.lex]  */
3064
3065 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3066    identifier.  */
3067
3068 static tree
3069 cp_parser_identifier (cp_parser* parser)
3070 {
3071   cp_token *token;
3072
3073   /* Look for the identifier.  */
3074   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3075   /* Return the value.  */
3076   return token ? token->u.value : error_mark_node;
3077 }
3078
3079 /* Parse a sequence of adjacent string constants.  Returns a
3080    TREE_STRING representing the combined, nul-terminated string
3081    constant.  If TRANSLATE is true, translate the string to the
3082    execution character set.  If WIDE_OK is true, a wide string is
3083    invalid here.
3084
3085    C++98 [lex.string] says that if a narrow string literal token is
3086    adjacent to a wide string literal token, the behavior is undefined.
3087    However, C99 6.4.5p4 says that this results in a wide string literal.
3088    We follow C99 here, for consistency with the C front end.
3089
3090    This code is largely lifted from lex_string() in c-lex.c.
3091
3092    FUTURE: ObjC++ will need to handle @-strings here.  */
3093 static tree
3094 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3095 {
3096   tree value;
3097   size_t count;
3098   struct obstack str_ob;
3099   cpp_string str, istr, *strs;
3100   cp_token *tok;
3101   enum cpp_ttype type;
3102
3103   tok = cp_lexer_peek_token (parser->lexer);
3104   if (!cp_parser_is_string_literal (tok))
3105     {
3106       cp_parser_error (parser, "expected string-literal");
3107       return error_mark_node;
3108     }
3109
3110   type = tok->type;
3111
3112   /* Try to avoid the overhead of creating and destroying an obstack
3113      for the common case of just one string.  */
3114   if (!cp_parser_is_string_literal
3115       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3116     {
3117       cp_lexer_consume_token (parser->lexer);
3118
3119       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3120       str.len = TREE_STRING_LENGTH (tok->u.value);
3121       count = 1;
3122
3123       strs = &str;
3124     }
3125   else
3126     {
3127       gcc_obstack_init (&str_ob);
3128       count = 0;
3129
3130       do
3131         {
3132           cp_lexer_consume_token (parser->lexer);
3133           count++;
3134           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3135           str.len = TREE_STRING_LENGTH (tok->u.value);
3136
3137           if (type != tok->type)
3138             {
3139               if (type == CPP_STRING)
3140                 type = tok->type;
3141               else if (tok->type != CPP_STRING)
3142                 error_at (tok->location,
3143                           "unsupported non-standard concatenation "
3144                           "of string literals");
3145             }
3146
3147           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3148
3149           tok = cp_lexer_peek_token (parser->lexer);
3150         }
3151       while (cp_parser_is_string_literal (tok));
3152
3153       strs = (cpp_string *) obstack_finish (&str_ob);
3154     }
3155
3156   if (type != CPP_STRING && !wide_ok)
3157     {
3158       cp_parser_error (parser, "a wide string is invalid in this context");
3159       type = CPP_STRING;
3160     }
3161
3162   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3163       (parse_in, strs, count, &istr, type))
3164     {
3165       value = build_string (istr.len, (const char *)istr.text);
3166       free (CONST_CAST (unsigned char *, istr.text));
3167
3168       switch (type)
3169         {
3170         default:
3171         case CPP_STRING:
3172         case CPP_UTF8STRING:
3173           TREE_TYPE (value) = char_array_type_node;
3174           break;
3175         case CPP_STRING16:
3176           TREE_TYPE (value) = char16_array_type_node;
3177           break;
3178         case CPP_STRING32:
3179           TREE_TYPE (value) = char32_array_type_node;
3180           break;
3181         case CPP_WSTRING:
3182           TREE_TYPE (value) = wchar_array_type_node;
3183           break;
3184         }
3185
3186       value = fix_string_type (value);
3187     }
3188   else
3189     /* cpp_interpret_string has issued an error.  */
3190     value = error_mark_node;
3191
3192   if (count > 1)
3193     obstack_free (&str_ob, 0);
3194
3195   return value;
3196 }
3197
3198
3199 /* Basic concepts [gram.basic]  */
3200
3201 /* Parse a translation-unit.
3202
3203    translation-unit:
3204      declaration-seq [opt]
3205
3206    Returns TRUE if all went well.  */
3207
3208 static bool
3209 cp_parser_translation_unit (cp_parser* parser)
3210 {
3211   /* The address of the first non-permanent object on the declarator
3212      obstack.  */
3213   static void *declarator_obstack_base;
3214
3215   bool success;
3216
3217   /* Create the declarator obstack, if necessary.  */
3218   if (!cp_error_declarator)
3219     {
3220       gcc_obstack_init (&declarator_obstack);
3221       /* Create the error declarator.  */
3222       cp_error_declarator = make_declarator (cdk_error);
3223       /* Create the empty parameter list.  */
3224       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3225       /* Remember where the base of the declarator obstack lies.  */
3226       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3227     }
3228
3229   cp_parser_declaration_seq_opt (parser);
3230
3231   /* If there are no tokens left then all went well.  */
3232   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3233     {
3234       /* Get rid of the token array; we don't need it any more.  */
3235       cp_lexer_destroy (parser->lexer);
3236       parser->lexer = NULL;
3237
3238       /* This file might have been a context that's implicitly extern
3239          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3240       if (parser->implicit_extern_c)
3241         {
3242           pop_lang_context ();
3243           parser->implicit_extern_c = false;
3244         }
3245
3246       /* Finish up.  */
3247       finish_translation_unit ();
3248
3249       success = true;
3250     }
3251   else
3252     {
3253       cp_parser_error (parser, "expected declaration");
3254       success = false;
3255     }
3256
3257   /* Make sure the declarator obstack was fully cleaned up.  */
3258   gcc_assert (obstack_next_free (&declarator_obstack)
3259               == declarator_obstack_base);
3260
3261   /* All went well.  */
3262   return success;
3263 }
3264
3265 /* Expressions [gram.expr] */
3266
3267 /* Parse a primary-expression.
3268
3269    primary-expression:
3270      literal
3271      this
3272      ( expression )
3273      id-expression
3274
3275    GNU Extensions:
3276
3277    primary-expression:
3278      ( compound-statement )
3279      __builtin_va_arg ( assignment-expression , type-id )
3280      __builtin_offsetof ( type-id , offsetof-expression )
3281
3282    C++ Extensions:
3283      __has_nothrow_assign ( type-id )   
3284      __has_nothrow_constructor ( type-id )
3285      __has_nothrow_copy ( type-id )
3286      __has_trivial_assign ( type-id )   
3287      __has_trivial_constructor ( type-id )
3288      __has_trivial_copy ( type-id )
3289      __has_trivial_destructor ( type-id )
3290      __has_virtual_destructor ( type-id )     
3291      __is_abstract ( type-id )
3292      __is_base_of ( type-id , type-id )
3293      __is_class ( type-id )
3294      __is_convertible_to ( type-id , type-id )     
3295      __is_empty ( type-id )
3296      __is_enum ( type-id )
3297      __is_literal_type ( type-id )
3298      __is_pod ( type-id )
3299      __is_polymorphic ( type-id )
3300      __is_std_layout ( type-id )
3301      __is_trivial ( type-id )
3302      __is_union ( type-id )
3303
3304    Objective-C++ Extension:
3305
3306    primary-expression:
3307      objc-expression
3308
3309    literal:
3310      __null
3311
3312    ADDRESS_P is true iff this expression was immediately preceded by
3313    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3314    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3315    true iff this expression is a template argument.
3316
3317    Returns a representation of the expression.  Upon return, *IDK
3318    indicates what kind of id-expression (if any) was present.  */
3319
3320 static tree
3321 cp_parser_primary_expression (cp_parser *parser,
3322                               bool address_p,
3323                               bool cast_p,
3324                               bool template_arg_p,
3325                               cp_id_kind *idk)
3326 {
3327   cp_token *token = NULL;
3328
3329   /* Assume the primary expression is not an id-expression.  */
3330   *idk = CP_ID_KIND_NONE;
3331
3332   /* Peek at the next token.  */
3333   token = cp_lexer_peek_token (parser->lexer);
3334   switch (token->type)
3335     {
3336       /* literal:
3337            integer-literal
3338            character-literal
3339            floating-literal
3340            string-literal
3341            boolean-literal  */
3342     case CPP_CHAR:
3343     case CPP_CHAR16:
3344     case CPP_CHAR32:
3345     case CPP_WCHAR:
3346     case CPP_NUMBER:
3347       token = cp_lexer_consume_token (parser->lexer);
3348       if (TREE_CODE (token->u.value) == FIXED_CST)
3349         {
3350           error_at (token->location,
3351                     "fixed-point types not supported in C++");
3352           return error_mark_node;
3353         }
3354       /* Floating-point literals are only allowed in an integral
3355          constant expression if they are cast to an integral or
3356          enumeration type.  */
3357       if (TREE_CODE (token->u.value) == REAL_CST
3358           && parser->integral_constant_expression_p
3359           && pedantic)
3360         {
3361           /* CAST_P will be set even in invalid code like "int(2.7 +
3362              ...)".   Therefore, we have to check that the next token
3363              is sure to end the cast.  */
3364           if (cast_p)
3365             {
3366               cp_token *next_token;
3367
3368               next_token = cp_lexer_peek_token (parser->lexer);
3369               if (/* The comma at the end of an
3370                      enumerator-definition.  */
3371                   next_token->type != CPP_COMMA
3372                   /* The curly brace at the end of an enum-specifier.  */
3373                   && next_token->type != CPP_CLOSE_BRACE
3374                   /* The end of a statement.  */
3375                   && next_token->type != CPP_SEMICOLON
3376                   /* The end of the cast-expression.  */
3377                   && next_token->type != CPP_CLOSE_PAREN
3378                   /* The end of an array bound.  */
3379                   && next_token->type != CPP_CLOSE_SQUARE
3380                   /* The closing ">" in a template-argument-list.  */
3381                   && (next_token->type != CPP_GREATER
3382                       || parser->greater_than_is_operator_p)
3383                   /* C++0x only: A ">>" treated like two ">" tokens,
3384                      in a template-argument-list.  */
3385                   && (next_token->type != CPP_RSHIFT
3386                       || (cxx_dialect == cxx98)
3387                       || parser->greater_than_is_operator_p))
3388                 cast_p = false;
3389             }
3390
3391           /* If we are within a cast, then the constraint that the
3392              cast is to an integral or enumeration type will be
3393              checked at that point.  If we are not within a cast, then
3394              this code is invalid.  */
3395           if (!cast_p)
3396             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3397         }
3398       return token->u.value;
3399
3400     case CPP_STRING:
3401     case CPP_STRING16:
3402     case CPP_STRING32:
3403     case CPP_WSTRING:
3404     case CPP_UTF8STRING:
3405       /* ??? Should wide strings be allowed when parser->translate_strings_p
3406          is false (i.e. in attributes)?  If not, we can kill the third
3407          argument to cp_parser_string_literal.  */
3408       return cp_parser_string_literal (parser,
3409                                        parser->translate_strings_p,
3410                                        true);
3411
3412     case CPP_OPEN_PAREN:
3413       {
3414         tree expr;
3415         bool saved_greater_than_is_operator_p;
3416
3417         /* Consume the `('.  */
3418         cp_lexer_consume_token (parser->lexer);
3419         /* Within a parenthesized expression, a `>' token is always
3420            the greater-than operator.  */
3421         saved_greater_than_is_operator_p
3422           = parser->greater_than_is_operator_p;
3423         parser->greater_than_is_operator_p = true;
3424         /* If we see `( { ' then we are looking at the beginning of
3425            a GNU statement-expression.  */
3426         if (cp_parser_allow_gnu_extensions_p (parser)
3427             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3428           {
3429             /* Statement-expressions are not allowed by the standard.  */
3430             pedwarn (token->location, OPT_pedantic, 
3431                      "ISO C++ forbids braced-groups within expressions");
3432
3433             /* And they're not allowed outside of a function-body; you
3434                cannot, for example, write:
3435
3436                  int i = ({ int j = 3; j + 1; });
3437
3438                at class or namespace scope.  */
3439             if (!parser->in_function_body
3440                 || parser->in_template_argument_list_p)
3441               {
3442                 error_at (token->location,
3443                           "statement-expressions are not allowed outside "
3444                           "functions nor in template-argument lists");
3445                 cp_parser_skip_to_end_of_block_or_statement (parser);
3446                 expr = error_mark_node;
3447               }
3448             else
3449               {
3450                 /* Start the statement-expression.  */
3451                 expr = begin_stmt_expr ();
3452                 /* Parse the compound-statement.  */
3453                 cp_parser_compound_statement (parser, expr, false, false);
3454                 /* Finish up.  */
3455                 expr = finish_stmt_expr (expr, false);
3456               }
3457           }
3458         else
3459           {
3460             /* Parse the parenthesized expression.  */
3461             expr = cp_parser_expression (parser, cast_p, idk);
3462             /* Let the front end know that this expression was
3463                enclosed in parentheses. This matters in case, for
3464                example, the expression is of the form `A::B', since
3465                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3466                not.  */
3467             finish_parenthesized_expr (expr);
3468             /* DR 705: Wrapping an unqualified name in parentheses
3469                suppresses arg-dependent lookup.  We want to pass back
3470                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3471                (c++/37862), but none of the others.  */
3472             if (*idk != CP_ID_KIND_QUALIFIED)
3473               *idk = CP_ID_KIND_NONE;
3474           }
3475         /* The `>' token might be the end of a template-id or
3476            template-parameter-list now.  */
3477         parser->greater_than_is_operator_p
3478           = saved_greater_than_is_operator_p;
3479         /* Consume the `)'.  */
3480         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3481           cp_parser_skip_to_end_of_statement (parser);
3482
3483         return expr;
3484       }
3485
3486     case CPP_OPEN_SQUARE:
3487       if (c_dialect_objc ())
3488         /* We have an Objective-C++ message. */
3489         return cp_parser_objc_expression (parser);
3490       {
3491         tree lam = cp_parser_lambda_expression (parser);
3492         /* Don't warn about a failed tentative parse.  */
3493         if (cp_parser_error_occurred (parser))
3494           return error_mark_node;
3495         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3496         return lam;
3497       }
3498
3499     case CPP_OBJC_STRING:
3500       if (c_dialect_objc ())
3501         /* We have an Objective-C++ string literal. */
3502         return cp_parser_objc_expression (parser);
3503       cp_parser_error (parser, "expected primary-expression");
3504       return error_mark_node;
3505
3506     case CPP_KEYWORD:
3507       switch (token->keyword)
3508         {
3509           /* These two are the boolean literals.  */
3510         case RID_TRUE:
3511           cp_lexer_consume_token (parser->lexer);
3512           return boolean_true_node;
3513         case RID_FALSE:
3514           cp_lexer_consume_token (parser->lexer);
3515           return boolean_false_node;
3516
3517           /* The `__null' literal.  */
3518         case RID_NULL:
3519           cp_lexer_consume_token (parser->lexer);
3520           return null_node;
3521
3522           /* The `nullptr' literal.  */
3523         case RID_NULLPTR:
3524           cp_lexer_consume_token (parser->lexer);
3525           return nullptr_node;
3526
3527           /* Recognize the `this' keyword.  */
3528         case RID_THIS:
3529           cp_lexer_consume_token (parser->lexer);
3530           if (parser->local_variables_forbidden_p)
3531             {
3532               error_at (token->location,
3533                         "%<this%> may not be used in this context");
3534               return error_mark_node;
3535             }
3536           /* Pointers cannot appear in constant-expressions.  */
3537           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3538             return error_mark_node;
3539           return finish_this_expr ();
3540
3541           /* The `operator' keyword can be the beginning of an
3542              id-expression.  */
3543         case RID_OPERATOR:
3544           goto id_expression;
3545
3546         case RID_FUNCTION_NAME:
3547         case RID_PRETTY_FUNCTION_NAME:
3548         case RID_C99_FUNCTION_NAME:
3549           {
3550             non_integral_constant name;
3551
3552             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3553                __func__ are the names of variables -- but they are
3554                treated specially.  Therefore, they are handled here,
3555                rather than relying on the generic id-expression logic
3556                below.  Grammatically, these names are id-expressions.
3557
3558                Consume the token.  */
3559             token = cp_lexer_consume_token (parser->lexer);
3560
3561             switch (token->keyword)
3562               {
3563               case RID_FUNCTION_NAME:
3564                 name = NIC_FUNC_NAME;
3565                 break;
3566               case RID_PRETTY_FUNCTION_NAME:
3567                 name = NIC_PRETTY_FUNC;
3568                 break;
3569               case RID_C99_FUNCTION_NAME:
3570                 name = NIC_C99_FUNC;
3571                 break;
3572               default:
3573                 gcc_unreachable ();
3574               }
3575
3576             if (cp_parser_non_integral_constant_expression (parser, name))
3577               return error_mark_node;
3578
3579             /* Look up the name.  */
3580             return finish_fname (token->u.value);
3581           }
3582
3583         case RID_VA_ARG:
3584           {
3585             tree expression;
3586             tree type;
3587
3588             /* The `__builtin_va_arg' construct is used to handle
3589                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3590             cp_lexer_consume_token (parser->lexer);
3591             /* Look for the opening `('.  */
3592             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3593             /* Now, parse the assignment-expression.  */
3594             expression = cp_parser_assignment_expression (parser,
3595                                                           /*cast_p=*/false, NULL);
3596             /* Look for the `,'.  */
3597             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3598             /* Parse the type-id.  */
3599             type = cp_parser_type_id (parser);
3600             /* Look for the closing `)'.  */
3601             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3602             /* Using `va_arg' in a constant-expression is not
3603                allowed.  */
3604             if (cp_parser_non_integral_constant_expression (parser,
3605                                                             NIC_VA_ARG))
3606               return error_mark_node;
3607             return build_x_va_arg (expression, type);
3608           }
3609
3610         case RID_OFFSETOF:
3611           return cp_parser_builtin_offsetof (parser);
3612
3613         case RID_HAS_NOTHROW_ASSIGN:
3614         case RID_HAS_NOTHROW_CONSTRUCTOR:
3615         case RID_HAS_NOTHROW_COPY:        
3616         case RID_HAS_TRIVIAL_ASSIGN:
3617         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3618         case RID_HAS_TRIVIAL_COPY:        
3619         case RID_HAS_TRIVIAL_DESTRUCTOR:
3620         case RID_HAS_VIRTUAL_DESTRUCTOR:
3621         case RID_IS_ABSTRACT:
3622         case RID_IS_BASE_OF:
3623         case RID_IS_CLASS:
3624         case RID_IS_CONVERTIBLE_TO:
3625         case RID_IS_EMPTY:
3626         case RID_IS_ENUM:
3627         case RID_IS_LITERAL_TYPE:
3628         case RID_IS_POD:
3629         case RID_IS_POLYMORPHIC:
3630         case RID_IS_STD_LAYOUT:
3631         case RID_IS_TRIVIAL:
3632         case RID_IS_UNION:
3633           return cp_parser_trait_expr (parser, token->keyword);
3634
3635         /* Objective-C++ expressions.  */
3636         case RID_AT_ENCODE:
3637         case RID_AT_PROTOCOL:
3638         case RID_AT_SELECTOR:
3639           return cp_parser_objc_expression (parser);
3640
3641         case RID_TEMPLATE:
3642           if (parser->in_function_body
3643               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3644                   == CPP_LESS))
3645             {
3646               error_at (token->location,
3647                         "a template declaration cannot appear at block scope");
3648               cp_parser_skip_to_end_of_block_or_statement (parser);
3649               return error_mark_node;
3650             }
3651         default:
3652           cp_parser_error (parser, "expected primary-expression");
3653           return error_mark_node;
3654         }
3655
3656       /* An id-expression can start with either an identifier, a
3657          `::' as the beginning of a qualified-id, or the "operator"
3658          keyword.  */
3659     case CPP_NAME:
3660     case CPP_SCOPE:
3661     case CPP_TEMPLATE_ID:
3662     case CPP_NESTED_NAME_SPECIFIER:
3663       {
3664         tree id_expression;
3665         tree decl;
3666         const char *error_msg;
3667         bool template_p;
3668         bool done;
3669         cp_token *id_expr_token;
3670
3671       id_expression:
3672         /* Parse the id-expression.  */
3673         id_expression
3674           = cp_parser_id_expression (parser,
3675                                      /*template_keyword_p=*/false,
3676                                      /*check_dependency_p=*/true,
3677                                      &template_p,
3678                                      /*declarator_p=*/false,
3679                                      /*optional_p=*/false);
3680         if (id_expression == error_mark_node)
3681           return error_mark_node;
3682         id_expr_token = token;
3683         token = cp_lexer_peek_token (parser->lexer);
3684         done = (token->type != CPP_OPEN_SQUARE
3685                 && token->type != CPP_OPEN_PAREN
3686                 && token->type != CPP_DOT
3687                 && token->type != CPP_DEREF
3688                 && token->type != CPP_PLUS_PLUS
3689                 && token->type != CPP_MINUS_MINUS);
3690         /* If we have a template-id, then no further lookup is
3691            required.  If the template-id was for a template-class, we
3692            will sometimes have a TYPE_DECL at this point.  */
3693         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3694                  || TREE_CODE (id_expression) == TYPE_DECL)
3695           decl = id_expression;
3696         /* Look up the name.  */
3697         else
3698           {
3699             tree ambiguous_decls;
3700
3701             /* If we already know that this lookup is ambiguous, then
3702                we've already issued an error message; there's no reason
3703                to check again.  */
3704             if (id_expr_token->type == CPP_NAME
3705                 && id_expr_token->ambiguous_p)
3706               {
3707                 cp_parser_simulate_error (parser);
3708                 return error_mark_node;
3709               }
3710
3711             decl = cp_parser_lookup_name (parser, id_expression,
3712                                           none_type,
3713                                           template_p,
3714                                           /*is_namespace=*/false,
3715                                           /*check_dependency=*/true,
3716                                           &ambiguous_decls,
3717                                           id_expr_token->location);
3718             /* If the lookup was ambiguous, an error will already have
3719                been issued.  */
3720             if (ambiguous_decls)
3721               return error_mark_node;
3722
3723             /* In Objective-C++, we may have an Objective-C 2.0
3724                dot-syntax for classes here.  */
3725             if (c_dialect_objc ()
3726                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3727                 && TREE_CODE (decl) == TYPE_DECL
3728                 && objc_is_class_name (decl))
3729               {
3730                 tree component;
3731                 cp_lexer_consume_token (parser->lexer);
3732                 component = cp_parser_identifier (parser);
3733                 if (component == error_mark_node)
3734                   return error_mark_node;
3735
3736                 return objc_build_class_component_ref (id_expression, component);
3737               }
3738
3739             /* In Objective-C++, an instance variable (ivar) may be preferred
3740                to whatever cp_parser_lookup_name() found.  */
3741             decl = objc_lookup_ivar (decl, id_expression);
3742
3743             /* If name lookup gives us a SCOPE_REF, then the
3744                qualifying scope was dependent.  */
3745             if (TREE_CODE (decl) == SCOPE_REF)
3746               {
3747                 /* At this point, we do not know if DECL is a valid
3748                    integral constant expression.  We assume that it is
3749                    in fact such an expression, so that code like:
3750
3751                       template <int N> struct A {
3752                         int a[B<N>::i];
3753                       };
3754                      
3755                    is accepted.  At template-instantiation time, we
3756                    will check that B<N>::i is actually a constant.  */
3757                 return decl;
3758               }
3759             /* Check to see if DECL is a local variable in a context
3760                where that is forbidden.  */
3761             if (parser->local_variables_forbidden_p
3762                 && local_variable_p (decl))
3763               {
3764                 /* It might be that we only found DECL because we are
3765                    trying to be generous with pre-ISO scoping rules.
3766                    For example, consider:
3767
3768                      int i;
3769                      void g() {
3770                        for (int i = 0; i < 10; ++i) {}
3771                        extern void f(int j = i);
3772                      }
3773
3774                    Here, name look up will originally find the out
3775                    of scope `i'.  We need to issue a warning message,
3776                    but then use the global `i'.  */
3777                 decl = check_for_out_of_scope_variable (decl);
3778                 if (local_variable_p (decl))
3779                   {
3780                     error_at (id_expr_token->location,
3781                               "local variable %qD may not appear in this context",
3782                               decl);
3783                     return error_mark_node;
3784                   }
3785               }
3786           }
3787
3788         decl = (finish_id_expression
3789                 (id_expression, decl, parser->scope,
3790                  idk,
3791                  parser->integral_constant_expression_p,
3792                  parser->allow_non_integral_constant_expression_p,
3793                  &parser->non_integral_constant_expression_p,
3794                  template_p, done, address_p,
3795                  template_arg_p,
3796                  &error_msg,
3797                  id_expr_token->location));
3798         if (error_msg)
3799           cp_parser_error (parser, error_msg);
3800         return decl;
3801       }
3802
3803       /* Anything else is an error.  */
3804     default:
3805       cp_parser_error (parser, "expected primary-expression");
3806       return error_mark_node;
3807     }
3808 }
3809
3810 /* Parse an id-expression.
3811
3812    id-expression:
3813      unqualified-id
3814      qualified-id
3815
3816    qualified-id:
3817      :: [opt] nested-name-specifier template [opt] unqualified-id
3818      :: identifier
3819      :: operator-function-id
3820      :: template-id
3821
3822    Return a representation of the unqualified portion of the
3823    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3824    a `::' or nested-name-specifier.
3825
3826    Often, if the id-expression was a qualified-id, the caller will
3827    want to make a SCOPE_REF to represent the qualified-id.  This
3828    function does not do this in order to avoid wastefully creating
3829    SCOPE_REFs when they are not required.
3830
3831    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3832    `template' keyword.
3833
3834    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3835    uninstantiated templates.
3836
3837    If *TEMPLATE_P is non-NULL, it is set to true iff the
3838    `template' keyword is used to explicitly indicate that the entity
3839    named is a template.
3840
3841    If DECLARATOR_P is true, the id-expression is appearing as part of
3842    a declarator, rather than as part of an expression.  */
3843
3844 static tree
3845 cp_parser_id_expression (cp_parser *parser,
3846                          bool template_keyword_p,
3847                          bool check_dependency_p,
3848                          bool *template_p,
3849                          bool declarator_p,
3850                          bool optional_p)
3851 {
3852   bool global_scope_p;
3853   bool nested_name_specifier_p;
3854
3855   /* Assume the `template' keyword was not used.  */
3856   if (template_p)
3857     *template_p = template_keyword_p;
3858
3859   /* Look for the optional `::' operator.  */
3860   global_scope_p
3861     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3862        != NULL_TREE);
3863   /* Look for the optional nested-name-specifier.  */
3864   nested_name_specifier_p
3865     = (cp_parser_nested_name_specifier_opt (parser,
3866                                             /*typename_keyword_p=*/false,
3867                                             check_dependency_p,
3868                                             /*type_p=*/false,
3869                                             declarator_p)
3870        != NULL_TREE);
3871   /* If there is a nested-name-specifier, then we are looking at
3872      the first qualified-id production.  */
3873   if (nested_name_specifier_p)
3874     {
3875       tree saved_scope;
3876       tree saved_object_scope;
3877       tree saved_qualifying_scope;
3878       tree unqualified_id;
3879       bool is_template;
3880
3881       /* See if the next token is the `template' keyword.  */
3882       if (!template_p)
3883         template_p = &is_template;
3884       *template_p = cp_parser_optional_template_keyword (parser);
3885       /* Name lookup we do during the processing of the
3886          unqualified-id might obliterate SCOPE.  */
3887       saved_scope = parser->scope;
3888       saved_object_scope = parser->object_scope;
3889       saved_qualifying_scope = parser->qualifying_scope;
3890       /* Process the final unqualified-id.  */
3891       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3892                                                  check_dependency_p,
3893                                                  declarator_p,
3894                                                  /*optional_p=*/false);
3895       /* Restore the SAVED_SCOPE for our caller.  */
3896       parser->scope = saved_scope;
3897       parser->object_scope = saved_object_scope;
3898       parser->qualifying_scope = saved_qualifying_scope;
3899
3900       return unqualified_id;
3901     }
3902   /* Otherwise, if we are in global scope, then we are looking at one
3903      of the other qualified-id productions.  */
3904   else if (global_scope_p)
3905     {
3906       cp_token *token;
3907       tree id;
3908
3909       /* Peek at the next token.  */
3910       token = cp_lexer_peek_token (parser->lexer);
3911
3912       /* If it's an identifier, and the next token is not a "<", then
3913          we can avoid the template-id case.  This is an optimization
3914          for this common case.  */
3915       if (token->type == CPP_NAME
3916           && !cp_parser_nth_token_starts_template_argument_list_p
3917                (parser, 2))
3918         return cp_parser_identifier (parser);
3919
3920       cp_parser_parse_tentatively (parser);
3921       /* Try a template-id.  */
3922       id = cp_parser_template_id (parser,
3923                                   /*template_keyword_p=*/false,
3924                                   /*check_dependency_p=*/true,
3925                                   declarator_p);
3926       /* If that worked, we're done.  */
3927       if (cp_parser_parse_definitely (parser))
3928         return id;
3929
3930       /* Peek at the next token.  (Changes in the token buffer may
3931          have invalidated the pointer obtained above.)  */
3932       token = cp_lexer_peek_token (parser->lexer);
3933
3934       switch (token->type)
3935         {
3936         case CPP_NAME:
3937           return cp_parser_identifier (parser);
3938
3939         case CPP_KEYWORD:
3940           if (token->keyword == RID_OPERATOR)
3941             return cp_parser_operator_function_id (parser);
3942           /* Fall through.  */
3943
3944         default:
3945           cp_parser_error (parser, "expected id-expression");
3946           return error_mark_node;
3947         }
3948     }
3949   else
3950     return cp_parser_unqualified_id (parser, template_keyword_p,
3951                                      /*check_dependency_p=*/true,
3952                                      declarator_p,
3953                                      optional_p);
3954 }
3955
3956 /* Parse an unqualified-id.
3957
3958    unqualified-id:
3959      identifier
3960      operator-function-id
3961      conversion-function-id
3962      ~ class-name
3963      template-id
3964
3965    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3966    keyword, in a construct like `A::template ...'.
3967
3968    Returns a representation of unqualified-id.  For the `identifier'
3969    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3970    production a BIT_NOT_EXPR is returned; the operand of the
3971    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3972    other productions, see the documentation accompanying the
3973    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3974    names are looked up in uninstantiated templates.  If DECLARATOR_P
3975    is true, the unqualified-id is appearing as part of a declarator,
3976    rather than as part of an expression.  */
3977
3978 static tree
3979 cp_parser_unqualified_id (cp_parser* parser,
3980                           bool template_keyword_p,
3981                           bool check_dependency_p,
3982                           bool declarator_p,
3983                           bool optional_p)
3984 {
3985   cp_token *token;
3986
3987   /* Peek at the next token.  */
3988   token = cp_lexer_peek_token (parser->lexer);
3989
3990   switch (token->type)
3991     {
3992     case CPP_NAME:
3993       {
3994         tree id;
3995
3996         /* We don't know yet whether or not this will be a
3997            template-id.  */
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,
4002                                     declarator_p);
4003         /* If it worked, we're done.  */
4004         if (cp_parser_parse_definitely (parser))
4005           return id;
4006         /* Otherwise, it's an ordinary identifier.  */
4007         return cp_parser_identifier (parser);
4008       }
4009
4010     case CPP_TEMPLATE_ID:
4011       return cp_parser_template_id (parser, template_keyword_p,
4012                                     check_dependency_p,
4013                                     declarator_p);
4014
4015     case CPP_COMPL:
4016       {
4017         tree type_decl;
4018         tree qualifying_scope;
4019         tree object_scope;
4020         tree scope;
4021         bool done;
4022
4023         /* Consume the `~' token.  */
4024         cp_lexer_consume_token (parser->lexer);
4025         /* Parse the class-name.  The standard, as written, seems to
4026            say that:
4027
4028              template <typename T> struct S { ~S (); };
4029              template <typename T> S<T>::~S() {}
4030
4031            is invalid, since `~' must be followed by a class-name, but
4032            `S<T>' is dependent, and so not known to be a class.
4033            That's not right; we need to look in uninstantiated
4034            templates.  A further complication arises from:
4035
4036              template <typename T> void f(T t) {
4037                t.T::~T();
4038              }
4039
4040            Here, it is not possible to look up `T' in the scope of `T'
4041            itself.  We must look in both the current scope, and the
4042            scope of the containing complete expression.
4043
4044            Yet another issue is:
4045
4046              struct S {
4047                int S;
4048                ~S();
4049              };
4050
4051              S::~S() {}
4052
4053            The standard does not seem to say that the `S' in `~S'
4054            should refer to the type `S' and not the data member
4055            `S::S'.  */
4056
4057         /* DR 244 says that we look up the name after the "~" in the
4058            same scope as we looked up the qualifying name.  That idea
4059            isn't fully worked out; it's more complicated than that.  */
4060         scope = parser->scope;
4061         object_scope = parser->object_scope;
4062         qualifying_scope = parser->qualifying_scope;
4063
4064         /* Check for invalid scopes.  */
4065         if (scope == error_mark_node)
4066           {
4067             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4068               cp_lexer_consume_token (parser->lexer);
4069             return error_mark_node;
4070           }
4071         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4072           {
4073             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4074               error_at (token->location,
4075                         "scope %qT before %<~%> is not a class-name",
4076                         scope);
4077             cp_parser_simulate_error (parser);
4078             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4079               cp_lexer_consume_token (parser->lexer);
4080             return error_mark_node;
4081           }
4082         gcc_assert (!scope || TYPE_P (scope));
4083
4084         /* If the name is of the form "X::~X" it's OK even if X is a
4085            typedef.  */
4086         token = cp_lexer_peek_token (parser->lexer);
4087         if (scope
4088             && token->type == CPP_NAME
4089             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4090                 != CPP_LESS)
4091             && (token->u.value == TYPE_IDENTIFIER (scope)
4092                 || (CLASS_TYPE_P (scope)
4093                     && constructor_name_p (token->u.value, scope))))
4094           {
4095             cp_lexer_consume_token (parser->lexer);
4096             return build_nt (BIT_NOT_EXPR, scope);
4097           }
4098
4099         /* If there was an explicit qualification (S::~T), first look
4100            in the scope given by the qualification (i.e., S).
4101
4102            Note: in the calls to cp_parser_class_name below we pass
4103            typename_type so that lookup finds the injected-class-name
4104            rather than the constructor.  */
4105         done = false;
4106         type_decl = NULL_TREE;
4107         if (scope)
4108           {
4109             cp_parser_parse_tentatively (parser);
4110             type_decl = cp_parser_class_name (parser,
4111                                               /*typename_keyword_p=*/false,
4112                                               /*template_keyword_p=*/false,
4113                                               typename_type,
4114                                               /*check_dependency=*/false,
4115                                               /*class_head_p=*/false,
4116                                               declarator_p);
4117             if (cp_parser_parse_definitely (parser))
4118               done = true;
4119           }
4120         /* In "N::S::~S", look in "N" as well.  */
4121         if (!done && scope && qualifying_scope)
4122           {
4123             cp_parser_parse_tentatively (parser);
4124             parser->scope = qualifying_scope;
4125             parser->object_scope = NULL_TREE;
4126             parser->qualifying_scope = NULL_TREE;
4127             type_decl
4128               = cp_parser_class_name (parser,
4129                                       /*typename_keyword_p=*/false,
4130                                       /*template_keyword_p=*/false,
4131                                       typename_type,
4132                                       /*check_dependency=*/false,
4133                                       /*class_head_p=*/false,
4134                                       declarator_p);
4135             if (cp_parser_parse_definitely (parser))
4136               done = true;
4137           }
4138         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4139         else if (!done && object_scope)
4140           {
4141             cp_parser_parse_tentatively (parser);
4142             parser->scope = object_scope;
4143             parser->object_scope = NULL_TREE;
4144             parser->qualifying_scope = NULL_TREE;
4145             type_decl
4146               = cp_parser_class_name (parser,
4147                                       /*typename_keyword_p=*/false,
4148                                       /*template_keyword_p=*/false,
4149                                       typename_type,
4150                                       /*check_dependency=*/false,
4151                                       /*class_head_p=*/false,
4152                                       declarator_p);
4153             if (cp_parser_parse_definitely (parser))
4154               done = true;
4155           }
4156         /* Look in the surrounding context.  */
4157         if (!done)
4158           {
4159             parser->scope = NULL_TREE;
4160             parser->object_scope = NULL_TREE;
4161             parser->qualifying_scope = NULL_TREE;
4162             if (processing_template_decl)
4163               cp_parser_parse_tentatively (parser);
4164             type_decl
4165               = cp_parser_class_name (parser,
4166                                       /*typename_keyword_p=*/false,
4167                                       /*template_keyword_p=*/false,
4168                                       typename_type,
4169                                       /*check_dependency=*/false,
4170                                       /*class_head_p=*/false,
4171                                       declarator_p);
4172             if (processing_template_decl
4173                 && ! cp_parser_parse_definitely (parser))
4174               {
4175                 /* We couldn't find a type with this name, so just accept
4176                    it and check for a match at instantiation time.  */
4177                 type_decl = cp_parser_identifier (parser);
4178                 if (type_decl != error_mark_node)
4179                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4180                 return type_decl;
4181               }
4182           }
4183         /* If an error occurred, assume that the name of the
4184            destructor is the same as the name of the qualifying
4185            class.  That allows us to keep parsing after running
4186            into ill-formed destructor names.  */
4187         if (type_decl == error_mark_node && scope)
4188           return build_nt (BIT_NOT_EXPR, scope);
4189         else if (type_decl == error_mark_node)
4190           return error_mark_node;
4191
4192         /* Check that destructor name and scope match.  */
4193         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4194           {
4195             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4196               error_at (token->location,
4197                         "declaration of %<~%T%> as member of %qT",
4198                         type_decl, scope);
4199             cp_parser_simulate_error (parser);
4200             return error_mark_node;
4201           }
4202
4203         /* [class.dtor]
4204
4205            A typedef-name that names a class shall not be used as the
4206            identifier in the declarator for a destructor declaration.  */
4207         if (declarator_p
4208             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4209             && !DECL_SELF_REFERENCE_P (type_decl)
4210             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4211           error_at (token->location,
4212                     "typedef-name %qD used as destructor declarator",
4213                     type_decl);
4214
4215         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4216       }
4217
4218     case CPP_KEYWORD:
4219       if (token->keyword == RID_OPERATOR)
4220         {
4221           tree id;
4222
4223           /* This could be a template-id, so we try that first.  */
4224           cp_parser_parse_tentatively (parser);
4225           /* Try a template-id.  */
4226           id = cp_parser_template_id (parser, template_keyword_p,
4227                                       /*check_dependency_p=*/true,
4228                                       declarator_p);
4229           /* If that worked, we're done.  */
4230           if (cp_parser_parse_definitely (parser))
4231             return id;
4232           /* We still don't know whether we're looking at an
4233              operator-function-id or a conversion-function-id.  */
4234           cp_parser_parse_tentatively (parser);
4235           /* Try an operator-function-id.  */
4236           id = cp_parser_operator_function_id (parser);
4237           /* If that didn't work, try a conversion-function-id.  */
4238           if (!cp_parser_parse_definitely (parser))
4239             id = cp_parser_conversion_function_id (parser);
4240
4241           return id;
4242         }
4243       /* Fall through.  */
4244
4245     default:
4246       if (optional_p)
4247         return NULL_TREE;
4248       cp_parser_error (parser, "expected unqualified-id");
4249       return error_mark_node;
4250     }
4251 }
4252
4253 /* Parse an (optional) nested-name-specifier.
4254
4255    nested-name-specifier: [C++98]
4256      class-or-namespace-name :: nested-name-specifier [opt]
4257      class-or-namespace-name :: template nested-name-specifier [opt]
4258
4259    nested-name-specifier: [C++0x]
4260      type-name ::
4261      namespace-name ::
4262      nested-name-specifier identifier ::
4263      nested-name-specifier template [opt] simple-template-id ::
4264
4265    PARSER->SCOPE should be set appropriately before this function is
4266    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4267    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4268    in name lookups.
4269
4270    Sets PARSER->SCOPE to the class (TYPE) or namespace
4271    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4272    it unchanged if there is no nested-name-specifier.  Returns the new
4273    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4274
4275    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4276    part of a declaration and/or decl-specifier.  */
4277
4278 static tree
4279 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4280                                      bool typename_keyword_p,
4281                                      bool check_dependency_p,
4282                                      bool type_p,
4283                                      bool is_declaration)
4284 {
4285   bool success = false;
4286   cp_token_position start = 0;
4287   cp_token *token;
4288
4289   /* Remember where the nested-name-specifier starts.  */
4290   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4291     {
4292       start = cp_lexer_token_position (parser->lexer, false);
4293       push_deferring_access_checks (dk_deferred);
4294     }
4295
4296   while (true)
4297     {
4298       tree new_scope;
4299       tree old_scope;
4300       tree saved_qualifying_scope;
4301       bool template_keyword_p;
4302
4303       /* Spot cases that cannot be the beginning of a
4304          nested-name-specifier.  */
4305       token = cp_lexer_peek_token (parser->lexer);
4306
4307       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4308          the already parsed nested-name-specifier.  */
4309       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4310         {
4311           /* Grab the nested-name-specifier and continue the loop.  */
4312           cp_parser_pre_parsed_nested_name_specifier (parser);
4313           /* If we originally encountered this nested-name-specifier
4314              with IS_DECLARATION set to false, we will not have
4315              resolved TYPENAME_TYPEs, so we must do so here.  */
4316           if (is_declaration
4317               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4318             {
4319               new_scope = resolve_typename_type (parser->scope,
4320                                                  /*only_current_p=*/false);
4321               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4322                 parser->scope = new_scope;
4323             }
4324           success = true;
4325           continue;
4326         }
4327
4328       /* Spot cases that cannot be the beginning of a
4329          nested-name-specifier.  On the second and subsequent times
4330          through the loop, we look for the `template' keyword.  */
4331       if (success && token->keyword == RID_TEMPLATE)
4332         ;
4333       /* A template-id can start a nested-name-specifier.  */
4334       else if (token->type == CPP_TEMPLATE_ID)
4335         ;
4336       /* DR 743: decltype can be used in a nested-name-specifier.  */
4337       else if (token_is_decltype (token))
4338         ;
4339       else
4340         {
4341           /* If the next token is not an identifier, then it is
4342              definitely not a type-name or namespace-name.  */
4343           if (token->type != CPP_NAME)
4344             break;
4345           /* If the following token is neither a `<' (to begin a
4346              template-id), nor a `::', then we are not looking at a
4347              nested-name-specifier.  */
4348           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4349
4350           if (token->type == CPP_COLON
4351               && parser->colon_corrects_to_scope_p
4352               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4353             {
4354               error_at (token->location,
4355                         "found %<:%> in nested-name-specifier, expected %<::%>");
4356               token->type = CPP_SCOPE;
4357             }
4358
4359           if (token->type != CPP_SCOPE
4360               && !cp_parser_nth_token_starts_template_argument_list_p
4361                   (parser, 2))
4362             break;
4363         }
4364
4365       /* The nested-name-specifier is optional, so we parse
4366          tentatively.  */
4367       cp_parser_parse_tentatively (parser);
4368
4369       /* Look for the optional `template' keyword, if this isn't the
4370          first time through the loop.  */
4371       if (success)
4372         template_keyword_p = cp_parser_optional_template_keyword (parser);
4373       else
4374         template_keyword_p = false;
4375
4376       /* Save the old scope since the name lookup we are about to do
4377          might destroy it.  */
4378       old_scope = parser->scope;
4379       saved_qualifying_scope = parser->qualifying_scope;
4380       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4381          look up names in "X<T>::I" in order to determine that "Y" is
4382          a template.  So, if we have a typename at this point, we make
4383          an effort to look through it.  */
4384       if (is_declaration
4385           && !typename_keyword_p
4386           && parser->scope
4387           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4388         parser->scope = resolve_typename_type (parser->scope,
4389                                                /*only_current_p=*/false);
4390       /* Parse the qualifying entity.  */
4391       new_scope
4392         = cp_parser_qualifying_entity (parser,
4393                                        typename_keyword_p,
4394                                        template_keyword_p,
4395                                        check_dependency_p,
4396                                        type_p,
4397                                        is_declaration);
4398       /* Look for the `::' token.  */
4399       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4400
4401       /* If we found what we wanted, we keep going; otherwise, we're
4402          done.  */
4403       if (!cp_parser_parse_definitely (parser))
4404         {
4405           bool error_p = false;
4406
4407           /* Restore the OLD_SCOPE since it was valid before the
4408              failed attempt at finding the last
4409              class-or-namespace-name.  */
4410           parser->scope = old_scope;
4411           parser->qualifying_scope = saved_qualifying_scope;
4412
4413           /* If the next token is a decltype, and the one after that is a
4414              `::', then the decltype has failed to resolve to a class or
4415              enumeration type.  Give this error even when parsing
4416              tentatively since it can't possibly be valid--and we're going
4417              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4418              won't get another chance.*/
4419           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4420               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4421                   == CPP_SCOPE))
4422             {
4423               token = cp_lexer_consume_token (parser->lexer);
4424               error_at (token->location, "decltype evaluates to %qT, "
4425                         "which is not a class or enumeration type",
4426                         token->u.value);
4427               parser->scope = error_mark_node;
4428               error_p = true;
4429               /* As below.  */
4430               success = true;
4431               cp_lexer_consume_token (parser->lexer);
4432             }
4433
4434           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4435             break;
4436           /* If the next token is an identifier, and the one after
4437              that is a `::', then any valid interpretation would have
4438              found a class-or-namespace-name.  */
4439           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4440                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4441                      == CPP_SCOPE)
4442                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4443                      != CPP_COMPL))
4444             {
4445               token = cp_lexer_consume_token (parser->lexer);
4446               if (!error_p)
4447                 {
4448                   if (!token->ambiguous_p)
4449                     {
4450                       tree decl;
4451                       tree ambiguous_decls;
4452
4453                       decl = cp_parser_lookup_name (parser, token->u.value,
4454                                                     none_type,
4455                                                     /*is_template=*/false,
4456                                                     /*is_namespace=*/false,
4457                                                     /*check_dependency=*/true,
4458                                                     &ambiguous_decls,
4459                                                     token->location);
4460                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4461                         error_at (token->location,
4462                                   "%qD used without template parameters",
4463                                   decl);
4464                       else if (ambiguous_decls)
4465                         {
4466                           error_at (token->location,
4467                                     "reference to %qD is ambiguous",
4468                                     token->u.value);
4469                           print_candidates (ambiguous_decls);
4470                           decl = error_mark_node;
4471                         }
4472                       else
4473                         {
4474                           if (cxx_dialect != cxx98)
4475                             cp_parser_name_lookup_error
4476                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4477                              token->location);
4478                           else
4479                             cp_parser_name_lookup_error
4480                             (parser, token->u.value, decl, NLE_CXX98,
4481                              token->location);
4482                         }
4483                     }
4484                   parser->scope = error_mark_node;
4485                   error_p = true;
4486                   /* Treat this as a successful nested-name-specifier
4487                      due to:
4488
4489                      [basic.lookup.qual]
4490
4491                      If the name found is not a class-name (clause
4492                      _class_) or namespace-name (_namespace.def_), the
4493                      program is ill-formed.  */
4494                   success = true;
4495                 }
4496               cp_lexer_consume_token (parser->lexer);
4497             }
4498           break;
4499         }
4500       /* We've found one valid nested-name-specifier.  */
4501       success = true;
4502       /* Name lookup always gives us a DECL.  */
4503       if (TREE_CODE (new_scope) == TYPE_DECL)
4504         new_scope = TREE_TYPE (new_scope);
4505       /* Uses of "template" must be followed by actual templates.  */
4506       if (template_keyword_p
4507           && !(CLASS_TYPE_P (new_scope)
4508                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4509                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4510                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4511           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4512                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4513                    == TEMPLATE_ID_EXPR)))
4514         permerror (input_location, TYPE_P (new_scope)
4515                    ? "%qT is not a template"
4516                    : "%qD is not a template",
4517                    new_scope);
4518       /* If it is a class scope, try to complete it; we are about to
4519          be looking up names inside the class.  */
4520       if (TYPE_P (new_scope)
4521           /* Since checking types for dependency can be expensive,
4522              avoid doing it if the type is already complete.  */
4523           && !COMPLETE_TYPE_P (new_scope)
4524           /* Do not try to complete dependent types.  */
4525           && !dependent_type_p (new_scope))
4526         {
4527           new_scope = complete_type (new_scope);
4528           /* If it is a typedef to current class, use the current
4529              class instead, as the typedef won't have any names inside
4530              it yet.  */
4531           if (!COMPLETE_TYPE_P (new_scope)
4532               && currently_open_class (new_scope))
4533             new_scope = TYPE_MAIN_VARIANT (new_scope);
4534         }
4535       /* Make sure we look in the right scope the next time through
4536          the loop.  */
4537       parser->scope = new_scope;
4538     }
4539
4540   /* If parsing tentatively, replace the sequence of tokens that makes
4541      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4542      token.  That way, should we re-parse the token stream, we will
4543      not have to repeat the effort required to do the parse, nor will
4544      we issue duplicate error messages.  */
4545   if (success && start)
4546     {
4547       cp_token *token;
4548
4549       token = cp_lexer_token_at (parser->lexer, start);
4550       /* Reset the contents of the START token.  */
4551       token->type = CPP_NESTED_NAME_SPECIFIER;
4552       /* Retrieve any deferred checks.  Do not pop this access checks yet
4553          so the memory will not be reclaimed during token replacing below.  */
4554       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4555       token->u.tree_check_value->value = parser->scope;
4556       token->u.tree_check_value->checks = get_deferred_access_checks ();
4557       token->u.tree_check_value->qualifying_scope =
4558         parser->qualifying_scope;
4559       token->keyword = RID_MAX;
4560
4561       /* Purge all subsequent tokens.  */
4562       cp_lexer_purge_tokens_after (parser->lexer, start);
4563     }
4564
4565   if (start)
4566     pop_to_parent_deferring_access_checks ();
4567
4568   return success ? parser->scope : NULL_TREE;
4569 }
4570
4571 /* Parse a nested-name-specifier.  See
4572    cp_parser_nested_name_specifier_opt for details.  This function
4573    behaves identically, except that it will an issue an error if no
4574    nested-name-specifier is present.  */
4575
4576 static tree
4577 cp_parser_nested_name_specifier (cp_parser *parser,
4578                                  bool typename_keyword_p,
4579                                  bool check_dependency_p,
4580                                  bool type_p,
4581                                  bool is_declaration)
4582 {
4583   tree scope;
4584
4585   /* Look for the nested-name-specifier.  */
4586   scope = cp_parser_nested_name_specifier_opt (parser,
4587                                                typename_keyword_p,
4588                                                check_dependency_p,
4589                                                type_p,
4590                                                is_declaration);
4591   /* If it was not present, issue an error message.  */
4592   if (!scope)
4593     {
4594       cp_parser_error (parser, "expected nested-name-specifier");
4595       parser->scope = NULL_TREE;
4596     }
4597
4598   return scope;
4599 }
4600
4601 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4602    this is either a class-name or a namespace-name (which corresponds
4603    to the class-or-namespace-name production in the grammar). For
4604    C++0x, it can also be a type-name that refers to an enumeration
4605    type.
4606
4607    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4608    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4609    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4610    TYPE_P is TRUE iff the next name should be taken as a class-name,
4611    even the same name is declared to be another entity in the same
4612    scope.
4613
4614    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4615    specified by the class-or-namespace-name.  If neither is found the
4616    ERROR_MARK_NODE is returned.  */
4617
4618 static tree
4619 cp_parser_qualifying_entity (cp_parser *parser,
4620                              bool typename_keyword_p,
4621                              bool template_keyword_p,
4622                              bool check_dependency_p,
4623                              bool type_p,
4624                              bool is_declaration)
4625 {
4626   tree saved_scope;
4627   tree saved_qualifying_scope;
4628   tree saved_object_scope;
4629   tree scope;
4630   bool only_class_p;
4631   bool successful_parse_p;
4632
4633   /* DR 743: decltype can appear in a nested-name-specifier.  */
4634   if (cp_lexer_next_token_is_decltype (parser->lexer))
4635     {
4636       scope = cp_parser_decltype (parser);
4637       if (TREE_CODE (scope) != ENUMERAL_TYPE
4638           && !MAYBE_CLASS_TYPE_P (scope))
4639         {
4640           cp_parser_simulate_error (parser);
4641           return error_mark_node;
4642         }
4643       if (TYPE_NAME (scope))
4644         scope = TYPE_NAME (scope);
4645       return scope;
4646     }
4647
4648   /* Before we try to parse the class-name, we must save away the
4649      current PARSER->SCOPE since cp_parser_class_name will destroy
4650      it.  */
4651   saved_scope = parser->scope;
4652   saved_qualifying_scope = parser->qualifying_scope;
4653   saved_object_scope = parser->object_scope;
4654   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4655      there is no need to look for a namespace-name.  */
4656   only_class_p = template_keyword_p 
4657     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4658   if (!only_class_p)
4659     cp_parser_parse_tentatively (parser);
4660   scope = cp_parser_class_name (parser,
4661                                 typename_keyword_p,
4662                                 template_keyword_p,
4663                                 type_p ? class_type : none_type,
4664                                 check_dependency_p,
4665                                 /*class_head_p=*/false,
4666                                 is_declaration);
4667   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4668   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4669   if (!only_class_p 
4670       && cxx_dialect != cxx98
4671       && !successful_parse_p)
4672     {
4673       /* Restore the saved scope.  */
4674       parser->scope = saved_scope;
4675       parser->qualifying_scope = saved_qualifying_scope;
4676       parser->object_scope = saved_object_scope;
4677
4678       /* Parse tentatively.  */
4679       cp_parser_parse_tentatively (parser);
4680      
4681       /* Parse a typedef-name or enum-name.  */
4682       scope = cp_parser_nonclass_name (parser);
4683
4684       /* "If the name found does not designate a namespace or a class,
4685          enumeration, or dependent type, the program is ill-formed."
4686
4687          We cover classes and dependent types above and namespaces below,
4688          so this code is only looking for enums.  */
4689       if (!scope || TREE_CODE (scope) != TYPE_DECL
4690           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4691         cp_parser_simulate_error (parser);
4692
4693       successful_parse_p = cp_parser_parse_definitely (parser);
4694     }
4695   /* If that didn't work, try for a namespace-name.  */
4696   if (!only_class_p && !successful_parse_p)
4697     {
4698       /* Restore the saved scope.  */
4699       parser->scope = saved_scope;
4700       parser->qualifying_scope = saved_qualifying_scope;
4701       parser->object_scope = saved_object_scope;
4702       /* If we are not looking at an identifier followed by the scope
4703          resolution operator, then this is not part of a
4704          nested-name-specifier.  (Note that this function is only used
4705          to parse the components of a nested-name-specifier.)  */
4706       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4707           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4708         return error_mark_node;
4709       scope = cp_parser_namespace_name (parser);
4710     }
4711
4712   return scope;
4713 }
4714
4715 /* Parse a postfix-expression.
4716
4717    postfix-expression:
4718      primary-expression
4719      postfix-expression [ expression ]
4720      postfix-expression ( expression-list [opt] )
4721      simple-type-specifier ( expression-list [opt] )
4722      typename :: [opt] nested-name-specifier identifier
4723        ( expression-list [opt] )
4724      typename :: [opt] nested-name-specifier template [opt] template-id
4725        ( expression-list [opt] )
4726      postfix-expression . template [opt] id-expression
4727      postfix-expression -> template [opt] id-expression
4728      postfix-expression . pseudo-destructor-name
4729      postfix-expression -> pseudo-destructor-name
4730      postfix-expression ++
4731      postfix-expression --
4732      dynamic_cast < type-id > ( expression )
4733      static_cast < type-id > ( expression )
4734      reinterpret_cast < type-id > ( expression )
4735      const_cast < type-id > ( expression )
4736      typeid ( expression )
4737      typeid ( type-id )
4738
4739    GNU Extension:
4740
4741    postfix-expression:
4742      ( type-id ) { initializer-list , [opt] }
4743
4744    This extension is a GNU version of the C99 compound-literal
4745    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4746    but they are essentially the same concept.)
4747
4748    If ADDRESS_P is true, the postfix expression is the operand of the
4749    `&' operator.  CAST_P is true if this expression is the target of a
4750    cast.
4751
4752    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4753    class member access expressions [expr.ref].
4754
4755    Returns a representation of the expression.  */
4756
4757 static tree
4758 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4759                               bool member_access_only_p,
4760                               cp_id_kind * pidk_return)
4761 {
4762   cp_token *token;
4763   enum rid keyword;
4764   cp_id_kind idk = CP_ID_KIND_NONE;
4765   tree postfix_expression = NULL_TREE;
4766   bool is_member_access = false;
4767
4768   /* Peek at the next token.  */
4769   token = cp_lexer_peek_token (parser->lexer);
4770   /* Some of the productions are determined by keywords.  */
4771   keyword = token->keyword;
4772   switch (keyword)
4773     {
4774     case RID_DYNCAST:
4775     case RID_STATCAST:
4776     case RID_REINTCAST:
4777     case RID_CONSTCAST:
4778       {
4779         tree type;
4780         tree expression;
4781         const char *saved_message;
4782
4783         /* All of these can be handled in the same way from the point
4784            of view of parsing.  Begin by consuming the token
4785            identifying the cast.  */
4786         cp_lexer_consume_token (parser->lexer);
4787
4788         /* New types cannot be defined in the cast.  */
4789         saved_message = parser->type_definition_forbidden_message;
4790         parser->type_definition_forbidden_message
4791           = G_("types may not be defined in casts");
4792
4793         /* Look for the opening `<'.  */
4794         cp_parser_require (parser, CPP_LESS, RT_LESS);
4795         /* Parse the type to which we are casting.  */
4796         type = cp_parser_type_id (parser);
4797         /* Look for the closing `>'.  */
4798         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4799         /* Restore the old message.  */
4800         parser->type_definition_forbidden_message = saved_message;
4801
4802         /* And the expression which is being cast.  */
4803         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4804         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4805         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4806
4807         /* Only type conversions to integral or enumeration types
4808            can be used in constant-expressions.  */
4809         if (!cast_valid_in_integral_constant_expression_p (type)
4810             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4811           return error_mark_node;
4812
4813         switch (keyword)
4814           {
4815           case RID_DYNCAST:
4816             postfix_expression
4817               = build_dynamic_cast (type, expression, tf_warning_or_error);
4818             break;
4819           case RID_STATCAST:
4820             postfix_expression
4821               = build_static_cast (type, expression, tf_warning_or_error);
4822             break;
4823           case RID_REINTCAST:
4824             postfix_expression
4825               = build_reinterpret_cast (type, expression, 
4826                                         tf_warning_or_error);
4827             break;
4828           case RID_CONSTCAST:
4829             postfix_expression
4830               = build_const_cast (type, expression, tf_warning_or_error);
4831             break;
4832           default:
4833             gcc_unreachable ();
4834           }
4835       }
4836       break;
4837
4838     case RID_TYPEID:
4839       {
4840         tree type;
4841         const char *saved_message;
4842         bool saved_in_type_id_in_expr_p;
4843
4844         /* Consume the `typeid' token.  */
4845         cp_lexer_consume_token (parser->lexer);
4846         /* Look for the `(' token.  */
4847         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4848         /* Types cannot be defined in a `typeid' expression.  */
4849         saved_message = parser->type_definition_forbidden_message;
4850         parser->type_definition_forbidden_message
4851           = G_("types may not be defined in a %<typeid%> expression");
4852         /* We can't be sure yet whether we're looking at a type-id or an
4853            expression.  */
4854         cp_parser_parse_tentatively (parser);
4855         /* Try a type-id first.  */
4856         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4857         parser->in_type_id_in_expr_p = true;
4858         type = cp_parser_type_id (parser);
4859         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4860         /* Look for the `)' token.  Otherwise, we can't be sure that
4861            we're not looking at an expression: consider `typeid (int
4862            (3))', for example.  */
4863         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4864         /* If all went well, simply lookup the type-id.  */
4865         if (cp_parser_parse_definitely (parser))
4866           postfix_expression = get_typeid (type);
4867         /* Otherwise, fall back to the expression variant.  */
4868         else
4869           {
4870             tree expression;
4871
4872             /* Look for an expression.  */
4873             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4874             /* Compute its typeid.  */
4875             postfix_expression = build_typeid (expression);
4876             /* Look for the `)' token.  */
4877             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4878           }
4879         /* Restore the saved message.  */
4880         parser->type_definition_forbidden_message = saved_message;
4881         /* `typeid' may not appear in an integral constant expression.  */
4882         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4883           return error_mark_node;
4884       }
4885       break;
4886
4887     case RID_TYPENAME:
4888       {
4889         tree type;
4890         /* The syntax permitted here is the same permitted for an
4891            elaborated-type-specifier.  */
4892         type = cp_parser_elaborated_type_specifier (parser,
4893                                                     /*is_friend=*/false,
4894                                                     /*is_declaration=*/false);
4895         postfix_expression = cp_parser_functional_cast (parser, type);
4896       }
4897       break;
4898
4899     default:
4900       {
4901         tree type;
4902
4903         /* If the next thing is a simple-type-specifier, we may be
4904            looking at a functional cast.  We could also be looking at
4905            an id-expression.  So, we try the functional cast, and if
4906            that doesn't work we fall back to the primary-expression.  */
4907         cp_parser_parse_tentatively (parser);
4908         /* Look for the simple-type-specifier.  */
4909         type = cp_parser_simple_type_specifier (parser,
4910                                                 /*decl_specs=*/NULL,
4911                                                 CP_PARSER_FLAGS_NONE);
4912         /* Parse the cast itself.  */
4913         if (!cp_parser_error_occurred (parser))
4914           postfix_expression
4915             = cp_parser_functional_cast (parser, type);
4916         /* If that worked, we're done.  */
4917         if (cp_parser_parse_definitely (parser))
4918           break;
4919
4920         /* If the functional-cast didn't work out, try a
4921            compound-literal.  */
4922         if (cp_parser_allow_gnu_extensions_p (parser)
4923             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4924           {
4925             VEC(constructor_elt,gc) *initializer_list = NULL;
4926             bool saved_in_type_id_in_expr_p;
4927
4928             cp_parser_parse_tentatively (parser);
4929             /* Consume the `('.  */
4930             cp_lexer_consume_token (parser->lexer);
4931             /* Parse the type.  */
4932             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4933             parser->in_type_id_in_expr_p = true;
4934             type = cp_parser_type_id (parser);
4935             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4936             /* Look for the `)'.  */
4937             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4938             /* Look for the `{'.  */
4939             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4940             /* If things aren't going well, there's no need to
4941                keep going.  */
4942             if (!cp_parser_error_occurred (parser))
4943               {
4944                 bool non_constant_p;
4945                 /* Parse the initializer-list.  */
4946                 initializer_list
4947                   = cp_parser_initializer_list (parser, &non_constant_p);
4948                 /* Allow a trailing `,'.  */
4949                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4950                   cp_lexer_consume_token (parser->lexer);
4951                 /* Look for the final `}'.  */
4952                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4953               }
4954             /* If that worked, we're definitely looking at a
4955                compound-literal expression.  */
4956             if (cp_parser_parse_definitely (parser))
4957               {
4958                 /* Warn the user that a compound literal is not
4959                    allowed in standard C++.  */
4960                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4961                 /* For simplicity, we disallow compound literals in
4962                    constant-expressions.  We could
4963                    allow compound literals of integer type, whose
4964                    initializer was a constant, in constant
4965                    expressions.  Permitting that usage, as a further
4966                    extension, would not change the meaning of any
4967                    currently accepted programs.  (Of course, as
4968                    compound literals are not part of ISO C++, the
4969                    standard has nothing to say.)  */
4970                 if (cp_parser_non_integral_constant_expression (parser,
4971                                                                 NIC_NCC))
4972                   {
4973                     postfix_expression = error_mark_node;
4974                     break;
4975                   }
4976                 /* Form the representation of the compound-literal.  */
4977                 postfix_expression
4978                   = (finish_compound_literal
4979                      (type, build_constructor (init_list_type_node,
4980                                                initializer_list),
4981                       tf_warning_or_error));
4982                 break;
4983               }
4984           }
4985
4986         /* It must be a primary-expression.  */
4987         postfix_expression
4988           = cp_parser_primary_expression (parser, address_p, cast_p,
4989                                           /*template_arg_p=*/false,
4990                                           &idk);
4991       }
4992       break;
4993     }
4994
4995   /* Keep looping until the postfix-expression is complete.  */
4996   while (true)
4997     {
4998       if (idk == CP_ID_KIND_UNQUALIFIED
4999           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5000           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5001         /* It is not a Koenig lookup function call.  */
5002         postfix_expression
5003           = unqualified_name_lookup_error (postfix_expression);
5004
5005       /* Peek at the next token.  */
5006       token = cp_lexer_peek_token (parser->lexer);
5007
5008       switch (token->type)
5009         {
5010         case CPP_OPEN_SQUARE:
5011           postfix_expression
5012             = cp_parser_postfix_open_square_expression (parser,
5013                                                         postfix_expression,
5014                                                         false);
5015           idk = CP_ID_KIND_NONE;
5016           is_member_access = false;
5017           break;
5018
5019         case CPP_OPEN_PAREN:
5020           /* postfix-expression ( expression-list [opt] ) */
5021           {
5022             bool koenig_p;
5023             bool is_builtin_constant_p;
5024             bool saved_integral_constant_expression_p = false;
5025             bool saved_non_integral_constant_expression_p = false;
5026             VEC(tree,gc) *args;
5027
5028             is_member_access = false;
5029
5030             is_builtin_constant_p
5031               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5032             if (is_builtin_constant_p)
5033               {
5034                 /* The whole point of __builtin_constant_p is to allow
5035                    non-constant expressions to appear as arguments.  */
5036                 saved_integral_constant_expression_p
5037                   = parser->integral_constant_expression_p;
5038                 saved_non_integral_constant_expression_p
5039                   = parser->non_integral_constant_expression_p;
5040                 parser->integral_constant_expression_p = false;
5041               }
5042             args = (cp_parser_parenthesized_expression_list
5043                     (parser, non_attr,
5044                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5045                      /*non_constant_p=*/NULL));
5046             if (is_builtin_constant_p)
5047               {
5048                 parser->integral_constant_expression_p
5049                   = saved_integral_constant_expression_p;
5050                 parser->non_integral_constant_expression_p
5051                   = saved_non_integral_constant_expression_p;
5052               }
5053
5054             if (args == NULL)
5055               {
5056                 postfix_expression = error_mark_node;
5057                 break;
5058               }
5059
5060             /* Function calls are not permitted in
5061                constant-expressions.  */
5062             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5063                 && cp_parser_non_integral_constant_expression (parser,
5064                                                                NIC_FUNC_CALL))
5065               {
5066                 postfix_expression = error_mark_node;
5067                 release_tree_vector (args);
5068                 break;
5069               }
5070
5071             koenig_p = false;
5072             if (idk == CP_ID_KIND_UNQUALIFIED
5073                 || idk == CP_ID_KIND_TEMPLATE_ID)
5074               {
5075                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5076                   {
5077                     if (!VEC_empty (tree, args))
5078                       {
5079                         koenig_p = true;
5080                         if (!any_type_dependent_arguments_p (args))
5081                           postfix_expression
5082                             = perform_koenig_lookup (postfix_expression, args,
5083                                                      /*include_std=*/false,
5084                                                      tf_warning_or_error);
5085                       }
5086                     else
5087                       postfix_expression
5088                         = unqualified_fn_lookup_error (postfix_expression);
5089                   }
5090                 /* We do not perform argument-dependent lookup if
5091                    normal lookup finds a non-function, in accordance
5092                    with the expected resolution of DR 218.  */
5093                 else if (!VEC_empty (tree, args)
5094                          && is_overloaded_fn (postfix_expression))
5095                   {
5096                     tree fn = get_first_fn (postfix_expression);
5097                     fn = STRIP_TEMPLATE (fn);
5098
5099                     /* Do not do argument dependent lookup if regular
5100                        lookup finds a member function or a block-scope
5101                        function declaration.  [basic.lookup.argdep]/3  */
5102                     if (!DECL_FUNCTION_MEMBER_P (fn)
5103                         && !DECL_LOCAL_FUNCTION_P (fn))
5104                       {
5105                         koenig_p = true;
5106                         if (!any_type_dependent_arguments_p (args))
5107                           postfix_expression
5108                             = perform_koenig_lookup (postfix_expression, args,
5109                                                      /*include_std=*/false,
5110                                                      tf_warning_or_error);
5111                       }
5112                   }
5113               }
5114
5115             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5116               {
5117                 tree instance = TREE_OPERAND (postfix_expression, 0);
5118                 tree fn = TREE_OPERAND (postfix_expression, 1);
5119
5120                 if (processing_template_decl
5121                     && (type_dependent_expression_p (instance)
5122                         || (!BASELINK_P (fn)
5123                             && TREE_CODE (fn) != FIELD_DECL)
5124                         || type_dependent_expression_p (fn)
5125                         || any_type_dependent_arguments_p (args)))
5126                   {
5127                     postfix_expression
5128                       = build_nt_call_vec (postfix_expression, args);
5129                     release_tree_vector (args);
5130                     break;
5131                   }
5132
5133                 if (BASELINK_P (fn))
5134                   {
5135                   postfix_expression
5136                     = (build_new_method_call
5137                        (instance, fn, &args, NULL_TREE,
5138                         (idk == CP_ID_KIND_QUALIFIED
5139                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5140                          : LOOKUP_NORMAL),
5141                         /*fn_p=*/NULL,
5142                         tf_warning_or_error));
5143                   }
5144                 else
5145                   postfix_expression
5146                     = finish_call_expr (postfix_expression, &args,
5147                                         /*disallow_virtual=*/false,
5148                                         /*koenig_p=*/false,
5149                                         tf_warning_or_error);
5150               }
5151             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5152                      || TREE_CODE (postfix_expression) == MEMBER_REF
5153                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5154               postfix_expression = (build_offset_ref_call_from_tree
5155                                     (postfix_expression, &args));
5156             else if (idk == CP_ID_KIND_QUALIFIED)
5157               /* A call to a static class member, or a namespace-scope
5158                  function.  */
5159               postfix_expression
5160                 = finish_call_expr (postfix_expression, &args,
5161                                     /*disallow_virtual=*/true,
5162                                     koenig_p,
5163                                     tf_warning_or_error);
5164             else
5165               /* All other function calls.  */
5166               postfix_expression
5167                 = finish_call_expr (postfix_expression, &args,
5168                                     /*disallow_virtual=*/false,
5169                                     koenig_p,
5170                                     tf_warning_or_error);
5171
5172             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5173             idk = CP_ID_KIND_NONE;
5174
5175             release_tree_vector (args);
5176           }
5177           break;
5178
5179         case CPP_DOT:
5180         case CPP_DEREF:
5181           /* postfix-expression . template [opt] id-expression
5182              postfix-expression . pseudo-destructor-name
5183              postfix-expression -> template [opt] id-expression
5184              postfix-expression -> pseudo-destructor-name */
5185
5186           /* Consume the `.' or `->' operator.  */
5187           cp_lexer_consume_token (parser->lexer);
5188
5189           postfix_expression
5190             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5191                                                       postfix_expression,
5192                                                       false, &idk,
5193                                                       token->location);
5194
5195           is_member_access = true;
5196           break;
5197
5198         case CPP_PLUS_PLUS:
5199           /* postfix-expression ++  */
5200           /* Consume the `++' token.  */
5201           cp_lexer_consume_token (parser->lexer);
5202           /* Generate a representation for the complete expression.  */
5203           postfix_expression
5204             = finish_increment_expr (postfix_expression,
5205                                      POSTINCREMENT_EXPR);
5206           /* Increments may not appear in constant-expressions.  */
5207           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5208             postfix_expression = error_mark_node;
5209           idk = CP_ID_KIND_NONE;
5210           is_member_access = false;
5211           break;
5212
5213         case CPP_MINUS_MINUS:
5214           /* postfix-expression -- */
5215           /* Consume the `--' token.  */
5216           cp_lexer_consume_token (parser->lexer);
5217           /* Generate a representation for the complete expression.  */
5218           postfix_expression
5219             = finish_increment_expr (postfix_expression,
5220                                      POSTDECREMENT_EXPR);
5221           /* Decrements may not appear in constant-expressions.  */
5222           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5223             postfix_expression = error_mark_node;
5224           idk = CP_ID_KIND_NONE;
5225           is_member_access = false;
5226           break;
5227
5228         default:
5229           if (pidk_return != NULL)
5230             * pidk_return = idk;
5231           if (member_access_only_p)
5232             return is_member_access? postfix_expression : error_mark_node;
5233           else
5234             return postfix_expression;
5235         }
5236     }
5237
5238   /* We should never get here.  */
5239   gcc_unreachable ();
5240   return error_mark_node;
5241 }
5242
5243 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5244    by cp_parser_builtin_offsetof.  We're looking for
5245
5246      postfix-expression [ expression ]
5247
5248    FOR_OFFSETOF is set if we're being called in that context, which
5249    changes how we deal with integer constant expressions.  */
5250
5251 static tree
5252 cp_parser_postfix_open_square_expression (cp_parser *parser,
5253                                           tree postfix_expression,
5254                                           bool for_offsetof)
5255 {
5256   tree index;
5257
5258   /* Consume the `[' token.  */
5259   cp_lexer_consume_token (parser->lexer);
5260
5261   /* Parse the index expression.  */
5262   /* ??? For offsetof, there is a question of what to allow here.  If
5263      offsetof is not being used in an integral constant expression context,
5264      then we *could* get the right answer by computing the value at runtime.
5265      If we are in an integral constant expression context, then we might
5266      could accept any constant expression; hard to say without analysis.
5267      Rather than open the barn door too wide right away, allow only integer
5268      constant expressions here.  */
5269   if (for_offsetof)
5270     index = cp_parser_constant_expression (parser, false, NULL);
5271   else
5272     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5273
5274   /* Look for the closing `]'.  */
5275   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5276
5277   /* Build the ARRAY_REF.  */
5278   postfix_expression = grok_array_decl (postfix_expression, index);
5279
5280   /* When not doing offsetof, array references are not permitted in
5281      constant-expressions.  */
5282   if (!for_offsetof
5283       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5284     postfix_expression = error_mark_node;
5285
5286   return postfix_expression;
5287 }
5288
5289 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5290    by cp_parser_builtin_offsetof.  We're looking for
5291
5292      postfix-expression . template [opt] id-expression
5293      postfix-expression . pseudo-destructor-name
5294      postfix-expression -> template [opt] id-expression
5295      postfix-expression -> pseudo-destructor-name
5296
5297    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5298    limits what of the above we'll actually accept, but nevermind.
5299    TOKEN_TYPE is the "." or "->" token, which will already have been
5300    removed from the stream.  */
5301
5302 static tree
5303 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5304                                         enum cpp_ttype token_type,
5305                                         tree postfix_expression,
5306                                         bool for_offsetof, cp_id_kind *idk,
5307                                         location_t location)
5308 {
5309   tree name;
5310   bool dependent_p;
5311   bool pseudo_destructor_p;
5312   tree scope = NULL_TREE;
5313
5314   /* If this is a `->' operator, dereference the pointer.  */
5315   if (token_type == CPP_DEREF)
5316     postfix_expression = build_x_arrow (postfix_expression);
5317   /* Check to see whether or not the expression is type-dependent.  */
5318   dependent_p = type_dependent_expression_p (postfix_expression);
5319   /* The identifier following the `->' or `.' is not qualified.  */
5320   parser->scope = NULL_TREE;
5321   parser->qualifying_scope = NULL_TREE;
5322   parser->object_scope = NULL_TREE;
5323   *idk = CP_ID_KIND_NONE;
5324
5325   /* Enter the scope corresponding to the type of the object
5326      given by the POSTFIX_EXPRESSION.  */
5327   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5328     {
5329       scope = TREE_TYPE (postfix_expression);
5330       /* According to the standard, no expression should ever have
5331          reference type.  Unfortunately, we do not currently match
5332          the standard in this respect in that our internal representation
5333          of an expression may have reference type even when the standard
5334          says it does not.  Therefore, we have to manually obtain the
5335          underlying type here.  */
5336       scope = non_reference (scope);
5337       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5338       if (scope == unknown_type_node)
5339         {
5340           error_at (location, "%qE does not have class type",
5341                     postfix_expression);
5342           scope = NULL_TREE;
5343         }
5344       /* Unlike the object expression in other contexts, *this is not
5345          required to be of complete type for purposes of class member
5346          access (5.2.5) outside the member function body.  */
5347       else if (scope != current_class_ref
5348                && !(processing_template_decl && scope == current_class_type))
5349         scope = complete_type_or_else (scope, NULL_TREE);
5350       /* Let the name lookup machinery know that we are processing a
5351          class member access expression.  */
5352       parser->context->object_type = scope;
5353       /* If something went wrong, we want to be able to discern that case,
5354          as opposed to the case where there was no SCOPE due to the type
5355          of expression being dependent.  */
5356       if (!scope)
5357         scope = error_mark_node;
5358       /* If the SCOPE was erroneous, make the various semantic analysis
5359          functions exit quickly -- and without issuing additional error
5360          messages.  */
5361       if (scope == error_mark_node)
5362         postfix_expression = error_mark_node;
5363     }
5364
5365   /* Assume this expression is not a pseudo-destructor access.  */
5366   pseudo_destructor_p = false;
5367
5368   /* If the SCOPE is a scalar type, then, if this is a valid program,
5369      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5370      is type dependent, it can be pseudo-destructor-name or something else.
5371      Try to parse it as pseudo-destructor-name first.  */
5372   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5373     {
5374       tree s;
5375       tree type;
5376
5377       cp_parser_parse_tentatively (parser);
5378       /* Parse the pseudo-destructor-name.  */
5379       s = NULL_TREE;
5380       cp_parser_pseudo_destructor_name (parser, &s, &type);
5381       if (dependent_p
5382           && (cp_parser_error_occurred (parser)
5383               || TREE_CODE (type) != TYPE_DECL
5384               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5385         cp_parser_abort_tentative_parse (parser);
5386       else if (cp_parser_parse_definitely (parser))
5387         {
5388           pseudo_destructor_p = true;
5389           postfix_expression
5390             = finish_pseudo_destructor_expr (postfix_expression,
5391                                              s, TREE_TYPE (type));
5392         }
5393     }
5394
5395   if (!pseudo_destructor_p)
5396     {
5397       /* If the SCOPE is not a scalar type, we are looking at an
5398          ordinary class member access expression, rather than a
5399          pseudo-destructor-name.  */
5400       bool template_p;
5401       cp_token *token = cp_lexer_peek_token (parser->lexer);
5402       /* Parse the id-expression.  */
5403       name = (cp_parser_id_expression
5404               (parser,
5405                cp_parser_optional_template_keyword (parser),
5406                /*check_dependency_p=*/true,
5407                &template_p,
5408                /*declarator_p=*/false,
5409                /*optional_p=*/false));
5410       /* In general, build a SCOPE_REF if the member name is qualified.
5411          However, if the name was not dependent and has already been
5412          resolved; there is no need to build the SCOPE_REF.  For example;
5413
5414              struct X { void f(); };
5415              template <typename T> void f(T* t) { t->X::f(); }
5416
5417          Even though "t" is dependent, "X::f" is not and has been resolved
5418          to a BASELINK; there is no need to include scope information.  */
5419
5420       /* But we do need to remember that there was an explicit scope for
5421          virtual function calls.  */
5422       if (parser->scope)
5423         *idk = CP_ID_KIND_QUALIFIED;
5424
5425       /* If the name is a template-id that names a type, we will get a
5426          TYPE_DECL here.  That is invalid code.  */
5427       if (TREE_CODE (name) == TYPE_DECL)
5428         {
5429           error_at (token->location, "invalid use of %qD", name);
5430           postfix_expression = error_mark_node;
5431         }
5432       else
5433         {
5434           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5435             {
5436               name = build_qualified_name (/*type=*/NULL_TREE,
5437                                            parser->scope,
5438                                            name,
5439                                            template_p);
5440               parser->scope = NULL_TREE;
5441               parser->qualifying_scope = NULL_TREE;
5442               parser->object_scope = NULL_TREE;
5443             }
5444           if (scope && name && BASELINK_P (name))
5445             adjust_result_of_qualified_name_lookup
5446               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5447           postfix_expression
5448             = finish_class_member_access_expr (postfix_expression, name,
5449                                                template_p, 
5450                                                tf_warning_or_error);
5451         }
5452     }
5453
5454   /* We no longer need to look up names in the scope of the object on
5455      the left-hand side of the `.' or `->' operator.  */
5456   parser->context->object_type = NULL_TREE;
5457
5458   /* Outside of offsetof, these operators may not appear in
5459      constant-expressions.  */
5460   if (!for_offsetof
5461       && (cp_parser_non_integral_constant_expression
5462           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5463     postfix_expression = error_mark_node;
5464
5465   return postfix_expression;
5466 }
5467
5468 /* Parse a parenthesized expression-list.
5469
5470    expression-list:
5471      assignment-expression
5472      expression-list, assignment-expression
5473
5474    attribute-list:
5475      expression-list
5476      identifier
5477      identifier, expression-list
5478
5479    CAST_P is true if this expression is the target of a cast.
5480
5481    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5482    argument pack.
5483
5484    Returns a vector of trees.  Each element is a representation of an
5485    assignment-expression.  NULL is returned if the ( and or ) are
5486    missing.  An empty, but allocated, vector is returned on no
5487    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5488    if we are parsing an attribute list for an attribute that wants a
5489    plain identifier argument, normal_attr for an attribute that wants
5490    an expression, or non_attr if we aren't parsing an attribute list.  If
5491    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5492    not all of the expressions in the list were constant.  */
5493
5494 static VEC(tree,gc) *
5495 cp_parser_parenthesized_expression_list (cp_parser* parser,
5496                                          int is_attribute_list,
5497                                          bool cast_p,
5498                                          bool allow_expansion_p,
5499                                          bool *non_constant_p)
5500 {
5501   VEC(tree,gc) *expression_list;
5502   bool fold_expr_p = is_attribute_list != non_attr;
5503   tree identifier = NULL_TREE;
5504   bool saved_greater_than_is_operator_p;
5505
5506   /* Assume all the expressions will be constant.  */
5507   if (non_constant_p)
5508     *non_constant_p = false;
5509
5510   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5511     return NULL;
5512
5513   expression_list = make_tree_vector ();
5514
5515   /* Within a parenthesized expression, a `>' token is always
5516      the greater-than operator.  */
5517   saved_greater_than_is_operator_p
5518     = parser->greater_than_is_operator_p;
5519   parser->greater_than_is_operator_p = true;
5520
5521   /* Consume expressions until there are no more.  */
5522   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5523     while (true)
5524       {
5525         tree expr;
5526
5527         /* At the beginning of attribute lists, check to see if the
5528            next token is an identifier.  */
5529         if (is_attribute_list == id_attr
5530             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5531           {
5532             cp_token *token;
5533
5534             /* Consume the identifier.  */
5535             token = cp_lexer_consume_token (parser->lexer);
5536             /* Save the identifier.  */
5537             identifier = token->u.value;
5538           }
5539         else
5540           {
5541             bool expr_non_constant_p;
5542
5543             /* Parse the next assignment-expression.  */
5544             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5545               {
5546                 /* A braced-init-list.  */
5547                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5548                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5549                 if (non_constant_p && expr_non_constant_p)
5550                   *non_constant_p = true;
5551               }
5552             else if (non_constant_p)
5553               {
5554                 expr = (cp_parser_constant_expression
5555                         (parser, /*allow_non_constant_p=*/true,
5556                          &expr_non_constant_p));
5557                 if (expr_non_constant_p)
5558                   *non_constant_p = true;
5559               }
5560             else
5561               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5562
5563             if (fold_expr_p)
5564               expr = fold_non_dependent_expr (expr);
5565
5566             /* If we have an ellipsis, then this is an expression
5567                expansion.  */
5568             if (allow_expansion_p
5569                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5570               {
5571                 /* Consume the `...'.  */
5572                 cp_lexer_consume_token (parser->lexer);
5573
5574                 /* Build the argument pack.  */
5575                 expr = make_pack_expansion (expr);
5576               }
5577
5578              /* Add it to the list.  We add error_mark_node
5579                 expressions to the list, so that we can still tell if
5580                 the correct form for a parenthesized expression-list
5581                 is found. That gives better errors.  */
5582             VEC_safe_push (tree, gc, expression_list, expr);
5583
5584             if (expr == error_mark_node)
5585               goto skip_comma;
5586           }
5587
5588         /* After the first item, attribute lists look the same as
5589            expression lists.  */
5590         is_attribute_list = non_attr;
5591
5592       get_comma:;
5593         /* If the next token isn't a `,', then we are done.  */
5594         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5595           break;
5596
5597         /* Otherwise, consume the `,' and keep going.  */
5598         cp_lexer_consume_token (parser->lexer);
5599       }
5600
5601   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5602     {
5603       int ending;
5604
5605     skip_comma:;
5606       /* We try and resync to an unnested comma, as that will give the
5607          user better diagnostics.  */
5608       ending = cp_parser_skip_to_closing_parenthesis (parser,
5609                                                       /*recovering=*/true,
5610                                                       /*or_comma=*/true,
5611                                                       /*consume_paren=*/true);
5612       if (ending < 0)
5613         goto get_comma;
5614       if (!ending)
5615         {
5616           parser->greater_than_is_operator_p
5617             = saved_greater_than_is_operator_p;
5618           return NULL;
5619         }
5620     }
5621
5622   parser->greater_than_is_operator_p
5623     = saved_greater_than_is_operator_p;
5624
5625   if (identifier)
5626     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5627
5628   return expression_list;
5629 }
5630
5631 /* Parse a pseudo-destructor-name.
5632
5633    pseudo-destructor-name:
5634      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5635      :: [opt] nested-name-specifier template template-id :: ~ type-name
5636      :: [opt] nested-name-specifier [opt] ~ type-name
5637
5638    If either of the first two productions is used, sets *SCOPE to the
5639    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5640    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5641    or ERROR_MARK_NODE if the parse fails.  */
5642
5643 static void
5644 cp_parser_pseudo_destructor_name (cp_parser* parser,
5645                                   tree* scope,
5646                                   tree* type)
5647 {
5648   bool nested_name_specifier_p;
5649
5650   /* Assume that things will not work out.  */
5651   *type = error_mark_node;
5652
5653   /* Look for the optional `::' operator.  */
5654   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5655   /* Look for the optional nested-name-specifier.  */
5656   nested_name_specifier_p
5657     = (cp_parser_nested_name_specifier_opt (parser,
5658                                             /*typename_keyword_p=*/false,
5659                                             /*check_dependency_p=*/true,
5660                                             /*type_p=*/false,
5661                                             /*is_declaration=*/false)
5662        != NULL_TREE);
5663   /* Now, if we saw a nested-name-specifier, we might be doing the
5664      second production.  */
5665   if (nested_name_specifier_p
5666       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5667     {
5668       /* Consume the `template' keyword.  */
5669       cp_lexer_consume_token (parser->lexer);
5670       /* Parse the template-id.  */
5671       cp_parser_template_id (parser,
5672                              /*template_keyword_p=*/true,
5673                              /*check_dependency_p=*/false,
5674                              /*is_declaration=*/true);
5675       /* Look for the `::' token.  */
5676       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5677     }
5678   /* If the next token is not a `~', then there might be some
5679      additional qualification.  */
5680   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5681     {
5682       /* At this point, we're looking for "type-name :: ~".  The type-name
5683          must not be a class-name, since this is a pseudo-destructor.  So,
5684          it must be either an enum-name, or a typedef-name -- both of which
5685          are just identifiers.  So, we peek ahead to check that the "::"
5686          and "~" tokens are present; if they are not, then we can avoid
5687          calling type_name.  */
5688       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5689           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5690           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5691         {
5692           cp_parser_error (parser, "non-scalar type");
5693           return;
5694         }
5695
5696       /* Look for the type-name.  */
5697       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5698       if (*scope == error_mark_node)
5699         return;
5700
5701       /* Look for the `::' token.  */
5702       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5703     }
5704   else
5705     *scope = NULL_TREE;
5706
5707   /* Look for the `~'.  */
5708   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5709
5710   /* Once we see the ~, this has to be a pseudo-destructor.  */
5711   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5712     cp_parser_commit_to_tentative_parse (parser);
5713
5714   /* Look for the type-name again.  We are not responsible for
5715      checking that it matches the first type-name.  */
5716   *type = cp_parser_nonclass_name (parser);
5717 }
5718
5719 /* Parse a unary-expression.
5720
5721    unary-expression:
5722      postfix-expression
5723      ++ cast-expression
5724      -- cast-expression
5725      unary-operator cast-expression
5726      sizeof unary-expression
5727      sizeof ( type-id )
5728      alignof ( type-id )  [C++0x]
5729      new-expression
5730      delete-expression
5731
5732    GNU Extensions:
5733
5734    unary-expression:
5735      __extension__ cast-expression
5736      __alignof__ unary-expression
5737      __alignof__ ( type-id )
5738      alignof unary-expression  [C++0x]
5739      __real__ cast-expression
5740      __imag__ cast-expression
5741      && identifier
5742
5743    ADDRESS_P is true iff the unary-expression is appearing as the
5744    operand of the `&' operator.   CAST_P is true if this expression is
5745    the target of a cast.
5746
5747    Returns a representation of the expression.  */
5748
5749 static tree
5750 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5751                             cp_id_kind * pidk)
5752 {
5753   cp_token *token;
5754   enum tree_code unary_operator;
5755
5756   /* Peek at the next token.  */
5757   token = cp_lexer_peek_token (parser->lexer);
5758   /* Some keywords give away the kind of expression.  */
5759   if (token->type == CPP_KEYWORD)
5760     {
5761       enum rid keyword = token->keyword;
5762
5763       switch (keyword)
5764         {
5765         case RID_ALIGNOF:
5766         case RID_SIZEOF:
5767           {
5768             tree operand;
5769             enum tree_code op;
5770
5771             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5772             /* Consume the token.  */
5773             cp_lexer_consume_token (parser->lexer);
5774             /* Parse the operand.  */
5775             operand = cp_parser_sizeof_operand (parser, keyword);
5776
5777             if (TYPE_P (operand))
5778               return cxx_sizeof_or_alignof_type (operand, op, true);
5779             else
5780               {
5781                 /* ISO C++ defines alignof only with types, not with
5782                    expressions. So pedwarn if alignof is used with a non-
5783                    type expression. However, __alignof__ is ok.  */
5784                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5785                   pedwarn (token->location, OPT_pedantic,
5786                            "ISO C++ does not allow %<alignof%> "
5787                            "with a non-type");
5788
5789                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5790               }
5791           }
5792
5793         case RID_NEW:
5794           return cp_parser_new_expression (parser);
5795
5796         case RID_DELETE:
5797           return cp_parser_delete_expression (parser);
5798
5799         case RID_EXTENSION:
5800           {
5801             /* The saved value of the PEDANTIC flag.  */
5802             int saved_pedantic;
5803             tree expr;
5804
5805             /* Save away the PEDANTIC flag.  */
5806             cp_parser_extension_opt (parser, &saved_pedantic);
5807             /* Parse the cast-expression.  */
5808             expr = cp_parser_simple_cast_expression (parser);
5809             /* Restore the PEDANTIC flag.  */
5810             pedantic = saved_pedantic;
5811
5812             return expr;
5813           }
5814
5815         case RID_REALPART:
5816         case RID_IMAGPART:
5817           {
5818             tree expression;
5819
5820             /* Consume the `__real__' or `__imag__' token.  */
5821             cp_lexer_consume_token (parser->lexer);
5822             /* Parse the cast-expression.  */
5823             expression = cp_parser_simple_cast_expression (parser);
5824             /* Create the complete representation.  */
5825             return build_x_unary_op ((keyword == RID_REALPART
5826                                       ? REALPART_EXPR : IMAGPART_EXPR),
5827                                      expression,
5828                                      tf_warning_or_error);
5829           }
5830           break;
5831
5832         case RID_NOEXCEPT:
5833           {
5834             tree expr;
5835             const char *saved_message;
5836             bool saved_integral_constant_expression_p;
5837             bool saved_non_integral_constant_expression_p;
5838             bool saved_greater_than_is_operator_p;
5839
5840             cp_lexer_consume_token (parser->lexer);
5841             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5842
5843             saved_message = parser->type_definition_forbidden_message;
5844             parser->type_definition_forbidden_message
5845               = G_("types may not be defined in %<noexcept%> expressions");
5846
5847             saved_integral_constant_expression_p
5848               = parser->integral_constant_expression_p;
5849             saved_non_integral_constant_expression_p
5850               = parser->non_integral_constant_expression_p;
5851             parser->integral_constant_expression_p = false;
5852
5853             saved_greater_than_is_operator_p
5854               = parser->greater_than_is_operator_p;
5855             parser->greater_than_is_operator_p = true;
5856
5857             ++cp_unevaluated_operand;
5858             ++c_inhibit_evaluation_warnings;
5859             expr = cp_parser_expression (parser, false, NULL);
5860             --c_inhibit_evaluation_warnings;
5861             --cp_unevaluated_operand;
5862
5863             parser->greater_than_is_operator_p
5864               = saved_greater_than_is_operator_p;
5865
5866             parser->integral_constant_expression_p
5867               = saved_integral_constant_expression_p;
5868             parser->non_integral_constant_expression_p
5869               = saved_non_integral_constant_expression_p;
5870
5871             parser->type_definition_forbidden_message = saved_message;
5872
5873             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5874             return finish_noexcept_expr (expr, tf_warning_or_error);
5875           }
5876
5877         default:
5878           break;
5879         }
5880     }
5881
5882   /* Look for the `:: new' and `:: delete', which also signal the
5883      beginning of a new-expression, or delete-expression,
5884      respectively.  If the next token is `::', then it might be one of
5885      these.  */
5886   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5887     {
5888       enum rid keyword;
5889
5890       /* See if the token after the `::' is one of the keywords in
5891          which we're interested.  */
5892       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5893       /* If it's `new', we have a new-expression.  */
5894       if (keyword == RID_NEW)
5895         return cp_parser_new_expression (parser);
5896       /* Similarly, for `delete'.  */
5897       else if (keyword == RID_DELETE)
5898         return cp_parser_delete_expression (parser);
5899     }
5900
5901   /* Look for a unary operator.  */
5902   unary_operator = cp_parser_unary_operator (token);
5903   /* The `++' and `--' operators can be handled similarly, even though
5904      they are not technically unary-operators in the grammar.  */
5905   if (unary_operator == ERROR_MARK)
5906     {
5907       if (token->type == CPP_PLUS_PLUS)
5908         unary_operator = PREINCREMENT_EXPR;
5909       else if (token->type == CPP_MINUS_MINUS)
5910         unary_operator = PREDECREMENT_EXPR;
5911       /* Handle the GNU address-of-label extension.  */
5912       else if (cp_parser_allow_gnu_extensions_p (parser)
5913                && token->type == CPP_AND_AND)
5914         {
5915           tree identifier;
5916           tree expression;
5917           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5918
5919           /* Consume the '&&' token.  */
5920           cp_lexer_consume_token (parser->lexer);
5921           /* Look for the identifier.  */
5922           identifier = cp_parser_identifier (parser);
5923           /* Create an expression representing the address.  */
5924           expression = finish_label_address_expr (identifier, loc);
5925           if (cp_parser_non_integral_constant_expression (parser,
5926                                                           NIC_ADDR_LABEL))
5927             expression = error_mark_node;
5928           return expression;
5929         }
5930     }
5931   if (unary_operator != ERROR_MARK)
5932     {
5933       tree cast_expression;
5934       tree expression = error_mark_node;
5935       non_integral_constant non_constant_p = NIC_NONE;
5936
5937       /* Consume the operator token.  */
5938       token = cp_lexer_consume_token (parser->lexer);
5939       /* Parse the cast-expression.  */
5940       cast_expression
5941         = cp_parser_cast_expression (parser,
5942                                      unary_operator == ADDR_EXPR,
5943                                      /*cast_p=*/false, pidk);
5944       /* Now, build an appropriate representation.  */
5945       switch (unary_operator)
5946         {
5947         case INDIRECT_REF:
5948           non_constant_p = NIC_STAR;
5949           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5950                                              tf_warning_or_error);
5951           break;
5952
5953         case ADDR_EXPR:
5954            non_constant_p = NIC_ADDR;
5955           /* Fall through.  */
5956         case BIT_NOT_EXPR:
5957           expression = build_x_unary_op (unary_operator, cast_expression,
5958                                          tf_warning_or_error);
5959           break;
5960
5961         case PREINCREMENT_EXPR:
5962         case PREDECREMENT_EXPR:
5963           non_constant_p = unary_operator == PREINCREMENT_EXPR
5964                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5965           /* Fall through.  */
5966         case UNARY_PLUS_EXPR:
5967         case NEGATE_EXPR:
5968         case TRUTH_NOT_EXPR:
5969           expression = finish_unary_op_expr (unary_operator, cast_expression);
5970           break;
5971
5972         default:
5973           gcc_unreachable ();
5974         }
5975
5976       if (non_constant_p != NIC_NONE
5977           && cp_parser_non_integral_constant_expression (parser,
5978                                                          non_constant_p))
5979         expression = error_mark_node;
5980
5981       return expression;
5982     }
5983
5984   return cp_parser_postfix_expression (parser, address_p, cast_p,
5985                                        /*member_access_only_p=*/false,
5986                                        pidk);
5987 }
5988
5989 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5990    unary-operator, the corresponding tree code is returned.  */
5991
5992 static enum tree_code
5993 cp_parser_unary_operator (cp_token* token)
5994 {
5995   switch (token->type)
5996     {
5997     case CPP_MULT:
5998       return INDIRECT_REF;
5999
6000     case CPP_AND:
6001       return ADDR_EXPR;
6002
6003     case CPP_PLUS:
6004       return UNARY_PLUS_EXPR;
6005
6006     case CPP_MINUS:
6007       return NEGATE_EXPR;
6008
6009     case CPP_NOT:
6010       return TRUTH_NOT_EXPR;
6011
6012     case CPP_COMPL:
6013       return BIT_NOT_EXPR;
6014
6015     default:
6016       return ERROR_MARK;
6017     }
6018 }
6019
6020 /* Parse a new-expression.
6021
6022    new-expression:
6023      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6024      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6025
6026    Returns a representation of the expression.  */
6027
6028 static tree
6029 cp_parser_new_expression (cp_parser* parser)
6030 {
6031   bool global_scope_p;
6032   VEC(tree,gc) *placement;
6033   tree type;
6034   VEC(tree,gc) *initializer;
6035   tree nelts;
6036   tree ret;
6037
6038   /* Look for the optional `::' operator.  */
6039   global_scope_p
6040     = (cp_parser_global_scope_opt (parser,
6041                                    /*current_scope_valid_p=*/false)
6042        != NULL_TREE);
6043   /* Look for the `new' operator.  */
6044   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6045   /* There's no easy way to tell a new-placement from the
6046      `( type-id )' construct.  */
6047   cp_parser_parse_tentatively (parser);
6048   /* Look for a new-placement.  */
6049   placement = cp_parser_new_placement (parser);
6050   /* If that didn't work out, there's no new-placement.  */
6051   if (!cp_parser_parse_definitely (parser))
6052     {
6053       if (placement != NULL)
6054         release_tree_vector (placement);
6055       placement = NULL;
6056     }
6057
6058   /* If the next token is a `(', then we have a parenthesized
6059      type-id.  */
6060   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6061     {
6062       cp_token *token;
6063       /* Consume the `('.  */
6064       cp_lexer_consume_token (parser->lexer);
6065       /* Parse the type-id.  */
6066       type = cp_parser_type_id (parser);
6067       /* Look for the closing `)'.  */
6068       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6069       token = cp_lexer_peek_token (parser->lexer);
6070       /* There should not be a direct-new-declarator in this production,
6071          but GCC used to allowed this, so we check and emit a sensible error
6072          message for this case.  */
6073       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6074         {
6075           error_at (token->location,
6076                     "array bound forbidden after parenthesized type-id");
6077           inform (token->location, 
6078                   "try removing the parentheses around the type-id");
6079           cp_parser_direct_new_declarator (parser);
6080         }
6081       nelts = NULL_TREE;
6082     }
6083   /* Otherwise, there must be a new-type-id.  */
6084   else
6085     type = cp_parser_new_type_id (parser, &nelts);
6086
6087   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6088   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6089       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6090     initializer = cp_parser_new_initializer (parser);
6091   else
6092     initializer = NULL;
6093
6094   /* A new-expression may not appear in an integral constant
6095      expression.  */
6096   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6097     ret = error_mark_node;
6098   else
6099     {
6100       /* Create a representation of the new-expression.  */
6101       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6102                        tf_warning_or_error);
6103     }
6104
6105   if (placement != NULL)
6106     release_tree_vector (placement);
6107   if (initializer != NULL)
6108     release_tree_vector (initializer);
6109
6110   return ret;
6111 }
6112
6113 /* Parse a new-placement.
6114
6115    new-placement:
6116      ( expression-list )
6117
6118    Returns the same representation as for an expression-list.  */
6119
6120 static VEC(tree,gc) *
6121 cp_parser_new_placement (cp_parser* parser)
6122 {
6123   VEC(tree,gc) *expression_list;
6124
6125   /* Parse the expression-list.  */
6126   expression_list = (cp_parser_parenthesized_expression_list
6127                      (parser, non_attr, /*cast_p=*/false,
6128                       /*allow_expansion_p=*/true,
6129                       /*non_constant_p=*/NULL));
6130
6131   return expression_list;
6132 }
6133
6134 /* Parse a new-type-id.
6135
6136    new-type-id:
6137      type-specifier-seq new-declarator [opt]
6138
6139    Returns the TYPE allocated.  If the new-type-id indicates an array
6140    type, *NELTS is set to the number of elements in the last array
6141    bound; the TYPE will not include the last array bound.  */
6142
6143 static tree
6144 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6145 {
6146   cp_decl_specifier_seq type_specifier_seq;
6147   cp_declarator *new_declarator;
6148   cp_declarator *declarator;
6149   cp_declarator *outer_declarator;
6150   const char *saved_message;
6151   tree type;
6152
6153   /* The type-specifier sequence must not contain type definitions.
6154      (It cannot contain declarations of new types either, but if they
6155      are not definitions we will catch that because they are not
6156      complete.)  */
6157   saved_message = parser->type_definition_forbidden_message;
6158   parser->type_definition_forbidden_message
6159     = G_("types may not be defined in a new-type-id");
6160   /* Parse the type-specifier-seq.  */
6161   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6162                                 /*is_trailing_return=*/false,
6163                                 &type_specifier_seq);
6164   /* Restore the old message.  */
6165   parser->type_definition_forbidden_message = saved_message;
6166   /* Parse the new-declarator.  */
6167   new_declarator = cp_parser_new_declarator_opt (parser);
6168
6169   /* Determine the number of elements in the last array dimension, if
6170      any.  */
6171   *nelts = NULL_TREE;
6172   /* Skip down to the last array dimension.  */
6173   declarator = new_declarator;
6174   outer_declarator = NULL;
6175   while (declarator && (declarator->kind == cdk_pointer
6176                         || declarator->kind == cdk_ptrmem))
6177     {
6178       outer_declarator = declarator;
6179       declarator = declarator->declarator;
6180     }
6181   while (declarator
6182          && declarator->kind == cdk_array
6183          && declarator->declarator
6184          && declarator->declarator->kind == cdk_array)
6185     {
6186       outer_declarator = declarator;
6187       declarator = declarator->declarator;
6188     }
6189
6190   if (declarator && declarator->kind == cdk_array)
6191     {
6192       *nelts = declarator->u.array.bounds;
6193       if (*nelts == error_mark_node)
6194         *nelts = integer_one_node;
6195
6196       if (outer_declarator)
6197         outer_declarator->declarator = declarator->declarator;
6198       else
6199         new_declarator = NULL;
6200     }
6201
6202   type = groktypename (&type_specifier_seq, new_declarator, false);
6203   return type;
6204 }
6205
6206 /* Parse an (optional) new-declarator.
6207
6208    new-declarator:
6209      ptr-operator new-declarator [opt]
6210      direct-new-declarator
6211
6212    Returns the declarator.  */
6213
6214 static cp_declarator *
6215 cp_parser_new_declarator_opt (cp_parser* parser)
6216 {
6217   enum tree_code code;
6218   tree type;
6219   cp_cv_quals cv_quals;
6220
6221   /* We don't know if there's a ptr-operator next, or not.  */
6222   cp_parser_parse_tentatively (parser);
6223   /* Look for a ptr-operator.  */
6224   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6225   /* If that worked, look for more new-declarators.  */
6226   if (cp_parser_parse_definitely (parser))
6227     {
6228       cp_declarator *declarator;
6229
6230       /* Parse another optional declarator.  */
6231       declarator = cp_parser_new_declarator_opt (parser);
6232
6233       return cp_parser_make_indirect_declarator
6234         (code, type, cv_quals, declarator);
6235     }
6236
6237   /* If the next token is a `[', there is a direct-new-declarator.  */
6238   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6239     return cp_parser_direct_new_declarator (parser);
6240
6241   return NULL;
6242 }
6243
6244 /* Parse a direct-new-declarator.
6245
6246    direct-new-declarator:
6247      [ expression ]
6248      direct-new-declarator [constant-expression]
6249
6250    */
6251
6252 static cp_declarator *
6253 cp_parser_direct_new_declarator (cp_parser* parser)
6254 {
6255   cp_declarator *declarator = NULL;
6256
6257   while (true)
6258     {
6259       tree expression;
6260
6261       /* Look for the opening `['.  */
6262       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6263       /* The first expression is not required to be constant.  */
6264       if (!declarator)
6265         {
6266           cp_token *token = cp_lexer_peek_token (parser->lexer);
6267           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6268           /* The standard requires that the expression have integral
6269              type.  DR 74 adds enumeration types.  We believe that the
6270              real intent is that these expressions be handled like the
6271              expression in a `switch' condition, which also allows
6272              classes with a single conversion to integral or
6273              enumeration type.  */
6274           if (!processing_template_decl)
6275             {
6276               expression
6277                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6278                                               expression,
6279                                               /*complain=*/true);
6280               if (!expression)
6281                 {
6282                   error_at (token->location,
6283                             "expression in new-declarator must have integral "
6284                             "or enumeration type");
6285                   expression = error_mark_node;
6286                 }
6287             }
6288         }
6289       /* But all the other expressions must be.  */
6290       else
6291         expression
6292           = cp_parser_constant_expression (parser,
6293                                            /*allow_non_constant=*/false,
6294                                            NULL);
6295       /* Look for the closing `]'.  */
6296       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6297
6298       /* Add this bound to the declarator.  */
6299       declarator = make_array_declarator (declarator, expression);
6300
6301       /* If the next token is not a `[', then there are no more
6302          bounds.  */
6303       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6304         break;
6305     }
6306
6307   return declarator;
6308 }
6309
6310 /* Parse a new-initializer.
6311
6312    new-initializer:
6313      ( expression-list [opt] )
6314      braced-init-list
6315
6316    Returns a representation of the expression-list.  */
6317
6318 static VEC(tree,gc) *
6319 cp_parser_new_initializer (cp_parser* parser)
6320 {
6321   VEC(tree,gc) *expression_list;
6322
6323   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6324     {
6325       tree t;
6326       bool expr_non_constant_p;
6327       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6328       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6329       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6330       expression_list = make_tree_vector_single (t);
6331     }
6332   else
6333     expression_list = (cp_parser_parenthesized_expression_list
6334                        (parser, non_attr, /*cast_p=*/false,
6335                         /*allow_expansion_p=*/true,
6336                         /*non_constant_p=*/NULL));
6337
6338   return expression_list;
6339 }
6340
6341 /* Parse a delete-expression.
6342
6343    delete-expression:
6344      :: [opt] delete cast-expression
6345      :: [opt] delete [ ] cast-expression
6346
6347    Returns a representation of the expression.  */
6348
6349 static tree
6350 cp_parser_delete_expression (cp_parser* parser)
6351 {
6352   bool global_scope_p;
6353   bool array_p;
6354   tree expression;
6355
6356   /* Look for the optional `::' operator.  */
6357   global_scope_p
6358     = (cp_parser_global_scope_opt (parser,
6359                                    /*current_scope_valid_p=*/false)
6360        != NULL_TREE);
6361   /* Look for the `delete' keyword.  */
6362   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6363   /* See if the array syntax is in use.  */
6364   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6365     {
6366       /* Consume the `[' token.  */
6367       cp_lexer_consume_token (parser->lexer);
6368       /* Look for the `]' token.  */
6369       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6370       /* Remember that this is the `[]' construct.  */
6371       array_p = true;
6372     }
6373   else
6374     array_p = false;
6375
6376   /* Parse the cast-expression.  */
6377   expression = cp_parser_simple_cast_expression (parser);
6378
6379   /* A delete-expression may not appear in an integral constant
6380      expression.  */
6381   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6382     return error_mark_node;
6383
6384   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6385                         tf_warning_or_error);
6386 }
6387
6388 /* Returns true if TOKEN may start a cast-expression and false
6389    otherwise.  */
6390
6391 static bool
6392 cp_parser_token_starts_cast_expression (cp_token *token)
6393 {
6394   switch (token->type)
6395     {
6396     case CPP_COMMA:
6397     case CPP_SEMICOLON:
6398     case CPP_QUERY:
6399     case CPP_COLON:
6400     case CPP_CLOSE_SQUARE:
6401     case CPP_CLOSE_PAREN:
6402     case CPP_CLOSE_BRACE:
6403     case CPP_DOT:
6404     case CPP_DOT_STAR:
6405     case CPP_DEREF:
6406     case CPP_DEREF_STAR:
6407     case CPP_DIV:
6408     case CPP_MOD:
6409     case CPP_LSHIFT:
6410     case CPP_RSHIFT:
6411     case CPP_LESS:
6412     case CPP_GREATER:
6413     case CPP_LESS_EQ:
6414     case CPP_GREATER_EQ:
6415     case CPP_EQ_EQ:
6416     case CPP_NOT_EQ:
6417     case CPP_EQ:
6418     case CPP_MULT_EQ:
6419     case CPP_DIV_EQ:
6420     case CPP_MOD_EQ:
6421     case CPP_PLUS_EQ:
6422     case CPP_MINUS_EQ:
6423     case CPP_RSHIFT_EQ:
6424     case CPP_LSHIFT_EQ:
6425     case CPP_AND_EQ:
6426     case CPP_XOR_EQ:
6427     case CPP_OR_EQ:
6428     case CPP_XOR:
6429     case CPP_OR:
6430     case CPP_OR_OR:
6431     case CPP_EOF:
6432       return false;
6433
6434       /* '[' may start a primary-expression in obj-c++.  */
6435     case CPP_OPEN_SQUARE:
6436       return c_dialect_objc ();
6437
6438     default:
6439       return true;
6440     }
6441 }
6442
6443 /* Parse a cast-expression.
6444
6445    cast-expression:
6446      unary-expression
6447      ( type-id ) cast-expression
6448
6449    ADDRESS_P is true iff the unary-expression is appearing as the
6450    operand of the `&' operator.   CAST_P is true if this expression is
6451    the target of a cast.
6452
6453    Returns a representation of the expression.  */
6454
6455 static tree
6456 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6457                            cp_id_kind * pidk)
6458 {
6459   /* If it's a `(', then we might be looking at a cast.  */
6460   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6461     {
6462       tree type = NULL_TREE;
6463       tree expr = NULL_TREE;
6464       bool compound_literal_p;
6465       const char *saved_message;
6466
6467       /* There's no way to know yet whether or not this is a cast.
6468          For example, `(int (3))' is a unary-expression, while `(int)
6469          3' is a cast.  So, we resort to parsing tentatively.  */
6470       cp_parser_parse_tentatively (parser);
6471       /* Types may not be defined in a cast.  */
6472       saved_message = parser->type_definition_forbidden_message;
6473       parser->type_definition_forbidden_message
6474         = G_("types may not be defined in casts");
6475       /* Consume the `('.  */
6476       cp_lexer_consume_token (parser->lexer);
6477       /* A very tricky bit is that `(struct S) { 3 }' is a
6478          compound-literal (which we permit in C++ as an extension).
6479          But, that construct is not a cast-expression -- it is a
6480          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6481          is legal; if the compound-literal were a cast-expression,
6482          you'd need an extra set of parentheses.)  But, if we parse
6483          the type-id, and it happens to be a class-specifier, then we
6484          will commit to the parse at that point, because we cannot
6485          undo the action that is done when creating a new class.  So,
6486          then we cannot back up and do a postfix-expression.
6487
6488          Therefore, we scan ahead to the closing `)', and check to see
6489          if the token after the `)' is a `{'.  If so, we are not
6490          looking at a cast-expression.
6491
6492          Save tokens so that we can put them back.  */
6493       cp_lexer_save_tokens (parser->lexer);
6494       /* Skip tokens until the next token is a closing parenthesis.
6495          If we find the closing `)', and the next token is a `{', then
6496          we are looking at a compound-literal.  */
6497       compound_literal_p
6498         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6499                                                   /*consume_paren=*/true)
6500            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6501       /* Roll back the tokens we skipped.  */
6502       cp_lexer_rollback_tokens (parser->lexer);
6503       /* If we were looking at a compound-literal, simulate an error
6504          so that the call to cp_parser_parse_definitely below will
6505          fail.  */
6506       if (compound_literal_p)
6507         cp_parser_simulate_error (parser);
6508       else
6509         {
6510           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6511           parser->in_type_id_in_expr_p = true;
6512           /* Look for the type-id.  */
6513           type = cp_parser_type_id (parser);
6514           /* Look for the closing `)'.  */
6515           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6516           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6517         }
6518
6519       /* Restore the saved message.  */
6520       parser->type_definition_forbidden_message = saved_message;
6521
6522       /* At this point this can only be either a cast or a
6523          parenthesized ctor such as `(T ())' that looks like a cast to
6524          function returning T.  */
6525       if (!cp_parser_error_occurred (parser)
6526           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6527                                                      (parser->lexer)))
6528         {
6529           cp_parser_parse_definitely (parser);
6530           expr = cp_parser_cast_expression (parser,
6531                                             /*address_p=*/false,
6532                                             /*cast_p=*/true, pidk);
6533
6534           /* Warn about old-style casts, if so requested.  */
6535           if (warn_old_style_cast
6536               && !in_system_header
6537               && !VOID_TYPE_P (type)
6538               && current_lang_name != lang_name_c)
6539             warning (OPT_Wold_style_cast, "use of old-style cast");
6540
6541           /* Only type conversions to integral or enumeration types
6542              can be used in constant-expressions.  */
6543           if (!cast_valid_in_integral_constant_expression_p (type)
6544               && cp_parser_non_integral_constant_expression (parser,
6545                                                              NIC_CAST))
6546             return error_mark_node;
6547
6548           /* Perform the cast.  */
6549           expr = build_c_cast (input_location, type, expr);
6550           return expr;
6551         }
6552       else 
6553         cp_parser_abort_tentative_parse (parser);
6554     }
6555
6556   /* If we get here, then it's not a cast, so it must be a
6557      unary-expression.  */
6558   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6559 }
6560
6561 /* Parse a binary expression of the general form:
6562
6563    pm-expression:
6564      cast-expression
6565      pm-expression .* cast-expression
6566      pm-expression ->* cast-expression
6567
6568    multiplicative-expression:
6569      pm-expression
6570      multiplicative-expression * pm-expression
6571      multiplicative-expression / pm-expression
6572      multiplicative-expression % pm-expression
6573
6574    additive-expression:
6575      multiplicative-expression
6576      additive-expression + multiplicative-expression
6577      additive-expression - multiplicative-expression
6578
6579    shift-expression:
6580      additive-expression
6581      shift-expression << additive-expression
6582      shift-expression >> additive-expression
6583
6584    relational-expression:
6585      shift-expression
6586      relational-expression < shift-expression
6587      relational-expression > shift-expression
6588      relational-expression <= shift-expression
6589      relational-expression >= shift-expression
6590
6591   GNU Extension:
6592
6593    relational-expression:
6594      relational-expression <? shift-expression
6595      relational-expression >? shift-expression
6596
6597    equality-expression:
6598      relational-expression
6599      equality-expression == relational-expression
6600      equality-expression != relational-expression
6601
6602    and-expression:
6603      equality-expression
6604      and-expression & equality-expression
6605
6606    exclusive-or-expression:
6607      and-expression
6608      exclusive-or-expression ^ and-expression
6609
6610    inclusive-or-expression:
6611      exclusive-or-expression
6612      inclusive-or-expression | exclusive-or-expression
6613
6614    logical-and-expression:
6615      inclusive-or-expression
6616      logical-and-expression && inclusive-or-expression
6617
6618    logical-or-expression:
6619      logical-and-expression
6620      logical-or-expression || logical-and-expression
6621
6622    All these are implemented with a single function like:
6623
6624    binary-expression:
6625      simple-cast-expression
6626      binary-expression <token> binary-expression
6627
6628    CAST_P is true if this expression is the target of a cast.
6629
6630    The binops_by_token map is used to get the tree codes for each <token> type.
6631    binary-expressions are associated according to a precedence table.  */
6632
6633 #define TOKEN_PRECEDENCE(token)                              \
6634 (((token->type == CPP_GREATER                                \
6635    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6636   && !parser->greater_than_is_operator_p)                    \
6637  ? PREC_NOT_OPERATOR                                         \
6638  : binops_by_token[token->type].prec)
6639
6640 static tree
6641 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6642                              bool no_toplevel_fold_p,
6643                              enum cp_parser_prec prec,
6644                              cp_id_kind * pidk)
6645 {
6646   cp_parser_expression_stack stack;
6647   cp_parser_expression_stack_entry *sp = &stack[0];
6648   tree lhs, rhs;
6649   cp_token *token;
6650   enum tree_code tree_type, lhs_type, rhs_type;
6651   enum cp_parser_prec new_prec, lookahead_prec;
6652   tree overload;
6653
6654   /* Parse the first expression.  */
6655   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6656   lhs_type = ERROR_MARK;
6657
6658   for (;;)
6659     {
6660       /* Get an operator token.  */
6661       token = cp_lexer_peek_token (parser->lexer);
6662
6663       if (warn_cxx0x_compat
6664           && token->type == CPP_RSHIFT
6665           && !parser->greater_than_is_operator_p)
6666         {
6667           if (warning_at (token->location, OPT_Wc__0x_compat, 
6668                           "%<>>%> operator will be treated as"
6669                           " two right angle brackets in C++0x"))
6670             inform (token->location,
6671                     "suggest parentheses around %<>>%> expression");
6672         }
6673
6674       new_prec = TOKEN_PRECEDENCE (token);
6675
6676       /* Popping an entry off the stack means we completed a subexpression:
6677          - either we found a token which is not an operator (`>' where it is not
6678            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6679            will happen repeatedly;
6680          - or, we found an operator which has lower priority.  This is the case
6681            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6682            parsing `3 * 4'.  */
6683       if (new_prec <= prec)
6684         {
6685           if (sp == stack)
6686             break;
6687           else
6688             goto pop;
6689         }
6690
6691      get_rhs:
6692       tree_type = binops_by_token[token->type].tree_type;
6693
6694       /* We used the operator token.  */
6695       cp_lexer_consume_token (parser->lexer);
6696
6697       /* For "false && x" or "true || x", x will never be executed;
6698          disable warnings while evaluating it.  */
6699       if (tree_type == TRUTH_ANDIF_EXPR)
6700         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6701       else if (tree_type == TRUTH_ORIF_EXPR)
6702         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6703
6704       /* Extract another operand.  It may be the RHS of this expression
6705          or the LHS of a new, higher priority expression.  */
6706       rhs = cp_parser_simple_cast_expression (parser);
6707       rhs_type = ERROR_MARK;
6708
6709       /* Get another operator token.  Look up its precedence to avoid
6710          building a useless (immediately popped) stack entry for common
6711          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6712       token = cp_lexer_peek_token (parser->lexer);
6713       lookahead_prec = TOKEN_PRECEDENCE (token);
6714       if (lookahead_prec > new_prec)
6715         {
6716           /* ... and prepare to parse the RHS of the new, higher priority
6717              expression.  Since precedence levels on the stack are
6718              monotonically increasing, we do not have to care about
6719              stack overflows.  */
6720           sp->prec = prec;
6721           sp->tree_type = tree_type;
6722           sp->lhs = lhs;
6723           sp->lhs_type = lhs_type;
6724           sp++;
6725           lhs = rhs;
6726           lhs_type = rhs_type;
6727           prec = new_prec;
6728           new_prec = lookahead_prec;
6729           goto get_rhs;
6730
6731          pop:
6732           lookahead_prec = new_prec;
6733           /* If the stack is not empty, we have parsed into LHS the right side
6734              (`4' in the example above) of an expression we had suspended.
6735              We can use the information on the stack to recover the LHS (`3')
6736              from the stack together with the tree code (`MULT_EXPR'), and
6737              the precedence of the higher level subexpression
6738              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6739              which will be used to actually build the additive expression.  */
6740           --sp;
6741           prec = sp->prec;
6742           tree_type = sp->tree_type;
6743           rhs = lhs;
6744           rhs_type = lhs_type;
6745           lhs = sp->lhs;
6746           lhs_type = sp->lhs_type;
6747         }
6748
6749       /* Undo the disabling of warnings done above.  */
6750       if (tree_type == TRUTH_ANDIF_EXPR)
6751         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6752       else if (tree_type == TRUTH_ORIF_EXPR)
6753         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6754
6755       overload = NULL;
6756       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6757          ERROR_MARK for everything that is not a binary expression.
6758          This makes warn_about_parentheses miss some warnings that
6759          involve unary operators.  For unary expressions we should
6760          pass the correct tree_code unless the unary expression was
6761          surrounded by parentheses.
6762       */
6763       if (no_toplevel_fold_p
6764           && lookahead_prec <= prec
6765           && sp == stack
6766           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6767         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6768       else
6769         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6770                                  &overload, tf_warning_or_error);
6771       lhs_type = tree_type;
6772
6773       /* If the binary operator required the use of an overloaded operator,
6774          then this expression cannot be an integral constant-expression.
6775          An overloaded operator can be used even if both operands are
6776          otherwise permissible in an integral constant-expression if at
6777          least one of the operands is of enumeration type.  */
6778
6779       if (overload
6780           && cp_parser_non_integral_constant_expression (parser,
6781                                                          NIC_OVERLOADED))
6782         return error_mark_node;
6783     }
6784
6785   return lhs;
6786 }
6787
6788
6789 /* Parse the `? expression : assignment-expression' part of a
6790    conditional-expression.  The LOGICAL_OR_EXPR is the
6791    logical-or-expression that started the conditional-expression.
6792    Returns a representation of the entire conditional-expression.
6793
6794    This routine is used by cp_parser_assignment_expression.
6795
6796      ? expression : assignment-expression
6797
6798    GNU Extensions:
6799
6800      ? : assignment-expression */
6801
6802 static tree
6803 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6804 {
6805   tree expr;
6806   tree assignment_expr;
6807   struct cp_token *token;
6808
6809   /* Consume the `?' token.  */
6810   cp_lexer_consume_token (parser->lexer);
6811   token = cp_lexer_peek_token (parser->lexer);
6812   if (cp_parser_allow_gnu_extensions_p (parser)
6813       && token->type == CPP_COLON)
6814     {
6815       pedwarn (token->location, OPT_pedantic, 
6816                "ISO C++ does not allow ?: with omitted middle operand");
6817       /* Implicit true clause.  */
6818       expr = NULL_TREE;
6819       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6820       warn_for_omitted_condop (token->location, logical_or_expr);
6821     }
6822   else
6823     {
6824       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6825       parser->colon_corrects_to_scope_p = false;
6826       /* Parse the expression.  */
6827       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6828       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6829       c_inhibit_evaluation_warnings +=
6830         ((logical_or_expr == truthvalue_true_node)
6831          - (logical_or_expr == truthvalue_false_node));
6832       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6833     }
6834
6835   /* The next token should be a `:'.  */
6836   cp_parser_require (parser, CPP_COLON, RT_COLON);
6837   /* Parse the assignment-expression.  */
6838   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6839   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6840
6841   /* Build the conditional-expression.  */
6842   return build_x_conditional_expr (logical_or_expr,
6843                                    expr,
6844                                    assignment_expr,
6845                                    tf_warning_or_error);
6846 }
6847
6848 /* Parse an assignment-expression.
6849
6850    assignment-expression:
6851      conditional-expression
6852      logical-or-expression assignment-operator assignment_expression
6853      throw-expression
6854
6855    CAST_P is true if this expression is the target of a cast.
6856
6857    Returns a representation for the expression.  */
6858
6859 static tree
6860 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6861                                  cp_id_kind * pidk)
6862 {
6863   tree expr;
6864
6865   /* If the next token is the `throw' keyword, then we're looking at
6866      a throw-expression.  */
6867   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6868     expr = cp_parser_throw_expression (parser);
6869   /* Otherwise, it must be that we are looking at a
6870      logical-or-expression.  */
6871   else
6872     {
6873       /* Parse the binary expressions (logical-or-expression).  */
6874       expr = cp_parser_binary_expression (parser, cast_p, false,
6875                                           PREC_NOT_OPERATOR, pidk);
6876       /* If the next token is a `?' then we're actually looking at a
6877          conditional-expression.  */
6878       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6879         return cp_parser_question_colon_clause (parser, expr);
6880       else
6881         {
6882           enum tree_code assignment_operator;
6883
6884           /* If it's an assignment-operator, we're using the second
6885              production.  */
6886           assignment_operator
6887             = cp_parser_assignment_operator_opt (parser);
6888           if (assignment_operator != ERROR_MARK)
6889             {
6890               bool non_constant_p;
6891
6892               /* Parse the right-hand side of the assignment.  */
6893               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6894
6895               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6896                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6897
6898               /* An assignment may not appear in a
6899                  constant-expression.  */
6900               if (cp_parser_non_integral_constant_expression (parser,
6901                                                               NIC_ASSIGNMENT))
6902                 return error_mark_node;
6903               /* Build the assignment expression.  */
6904               expr = build_x_modify_expr (expr,
6905                                           assignment_operator,
6906                                           rhs,
6907                                           tf_warning_or_error);
6908             }
6909         }
6910     }
6911
6912   return expr;
6913 }
6914
6915 /* Parse an (optional) assignment-operator.
6916
6917    assignment-operator: one of
6918      = *= /= %= += -= >>= <<= &= ^= |=
6919
6920    GNU Extension:
6921
6922    assignment-operator: one of
6923      <?= >?=
6924
6925    If the next token is an assignment operator, the corresponding tree
6926    code is returned, and the token is consumed.  For example, for
6927    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6928    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6929    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6930    operator, ERROR_MARK is returned.  */
6931
6932 static enum tree_code
6933 cp_parser_assignment_operator_opt (cp_parser* parser)
6934 {
6935   enum tree_code op;
6936   cp_token *token;
6937
6938   /* Peek at the next token.  */
6939   token = cp_lexer_peek_token (parser->lexer);
6940
6941   switch (token->type)
6942     {
6943     case CPP_EQ:
6944       op = NOP_EXPR;
6945       break;
6946
6947     case CPP_MULT_EQ:
6948       op = MULT_EXPR;
6949       break;
6950
6951     case CPP_DIV_EQ:
6952       op = TRUNC_DIV_EXPR;
6953       break;
6954
6955     case CPP_MOD_EQ:
6956       op = TRUNC_MOD_EXPR;
6957       break;
6958
6959     case CPP_PLUS_EQ:
6960       op = PLUS_EXPR;
6961       break;
6962
6963     case CPP_MINUS_EQ:
6964       op = MINUS_EXPR;
6965       break;
6966
6967     case CPP_RSHIFT_EQ:
6968       op = RSHIFT_EXPR;
6969       break;
6970
6971     case CPP_LSHIFT_EQ:
6972       op = LSHIFT_EXPR;
6973       break;
6974
6975     case CPP_AND_EQ:
6976       op = BIT_AND_EXPR;
6977       break;
6978
6979     case CPP_XOR_EQ:
6980       op = BIT_XOR_EXPR;
6981       break;
6982
6983     case CPP_OR_EQ:
6984       op = BIT_IOR_EXPR;
6985       break;
6986
6987     default:
6988       /* Nothing else is an assignment operator.  */
6989       op = ERROR_MARK;
6990     }
6991
6992   /* If it was an assignment operator, consume it.  */
6993   if (op != ERROR_MARK)
6994     cp_lexer_consume_token (parser->lexer);
6995
6996   return op;
6997 }
6998
6999 /* Parse an expression.
7000
7001    expression:
7002      assignment-expression
7003      expression , assignment-expression
7004
7005    CAST_P is true if this expression is the target of a cast.
7006
7007    Returns a representation of the expression.  */
7008
7009 static tree
7010 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7011 {
7012   tree expression = NULL_TREE;
7013
7014   while (true)
7015     {
7016       tree assignment_expression;
7017
7018       /* Parse the next assignment-expression.  */
7019       assignment_expression
7020         = cp_parser_assignment_expression (parser, cast_p, pidk);
7021       /* If this is the first assignment-expression, we can just
7022          save it away.  */
7023       if (!expression)
7024         expression = assignment_expression;
7025       else
7026         expression = build_x_compound_expr (expression,
7027                                             assignment_expression,
7028                                             tf_warning_or_error);
7029       /* If the next token is not a comma, then we are done with the
7030          expression.  */
7031       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7032         break;
7033       /* Consume the `,'.  */
7034       cp_lexer_consume_token (parser->lexer);
7035       /* A comma operator cannot appear in a constant-expression.  */
7036       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7037         expression = error_mark_node;
7038     }
7039
7040   return expression;
7041 }
7042
7043 /* Parse a constant-expression.
7044
7045    constant-expression:
7046      conditional-expression
7047
7048   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7049   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7050   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7051   is false, NON_CONSTANT_P should be NULL.  */
7052
7053 static tree
7054 cp_parser_constant_expression (cp_parser* parser,
7055                                bool allow_non_constant_p,
7056                                bool *non_constant_p)
7057 {
7058   bool saved_integral_constant_expression_p;
7059   bool saved_allow_non_integral_constant_expression_p;
7060   bool saved_non_integral_constant_expression_p;
7061   tree expression;
7062
7063   /* It might seem that we could simply parse the
7064      conditional-expression, and then check to see if it were
7065      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7066      one that the compiler can figure out is constant, possibly after
7067      doing some simplifications or optimizations.  The standard has a
7068      precise definition of constant-expression, and we must honor
7069      that, even though it is somewhat more restrictive.
7070
7071      For example:
7072
7073        int i[(2, 3)];
7074
7075      is not a legal declaration, because `(2, 3)' is not a
7076      constant-expression.  The `,' operator is forbidden in a
7077      constant-expression.  However, GCC's constant-folding machinery
7078      will fold this operation to an INTEGER_CST for `3'.  */
7079
7080   /* Save the old settings.  */
7081   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7082   saved_allow_non_integral_constant_expression_p
7083     = parser->allow_non_integral_constant_expression_p;
7084   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7085   /* We are now parsing a constant-expression.  */
7086   parser->integral_constant_expression_p = true;
7087   parser->allow_non_integral_constant_expression_p
7088     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7089   parser->non_integral_constant_expression_p = false;
7090   /* Although the grammar says "conditional-expression", we parse an
7091      "assignment-expression", which also permits "throw-expression"
7092      and the use of assignment operators.  In the case that
7093      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7094      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7095      actually essential that we look for an assignment-expression.
7096      For example, cp_parser_initializer_clauses uses this function to
7097      determine whether a particular assignment-expression is in fact
7098      constant.  */
7099   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7100   /* Restore the old settings.  */
7101   parser->integral_constant_expression_p
7102     = saved_integral_constant_expression_p;
7103   parser->allow_non_integral_constant_expression_p
7104     = saved_allow_non_integral_constant_expression_p;
7105   if (cxx_dialect >= cxx0x)
7106     {
7107       /* Require an rvalue constant expression here; that's what our
7108          callers expect.  Reference constant expressions are handled
7109          separately in e.g. cp_parser_template_argument.  */
7110       bool is_const = potential_rvalue_constant_expression (expression);
7111       parser->non_integral_constant_expression_p = !is_const;
7112       if (!is_const && !allow_non_constant_p)
7113         require_potential_rvalue_constant_expression (expression);
7114     }
7115   if (allow_non_constant_p)
7116     *non_constant_p = parser->non_integral_constant_expression_p;
7117   parser->non_integral_constant_expression_p
7118     = saved_non_integral_constant_expression_p;
7119
7120   return expression;
7121 }
7122
7123 /* Parse __builtin_offsetof.
7124
7125    offsetof-expression:
7126      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7127
7128    offsetof-member-designator:
7129      id-expression
7130      | offsetof-member-designator "." id-expression
7131      | offsetof-member-designator "[" expression "]"
7132      | offsetof-member-designator "->" id-expression  */
7133
7134 static tree
7135 cp_parser_builtin_offsetof (cp_parser *parser)
7136 {
7137   int save_ice_p, save_non_ice_p;
7138   tree type, expr;
7139   cp_id_kind dummy;
7140   cp_token *token;
7141
7142   /* We're about to accept non-integral-constant things, but will
7143      definitely yield an integral constant expression.  Save and
7144      restore these values around our local parsing.  */
7145   save_ice_p = parser->integral_constant_expression_p;
7146   save_non_ice_p = parser->non_integral_constant_expression_p;
7147
7148   /* Consume the "__builtin_offsetof" token.  */
7149   cp_lexer_consume_token (parser->lexer);
7150   /* Consume the opening `('.  */
7151   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7152   /* Parse the type-id.  */
7153   type = cp_parser_type_id (parser);
7154   /* Look for the `,'.  */
7155   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7156   token = cp_lexer_peek_token (parser->lexer);
7157
7158   /* Build the (type *)null that begins the traditional offsetof macro.  */
7159   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7160                             tf_warning_or_error);
7161
7162   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7163   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7164                                                  true, &dummy, token->location);
7165   while (true)
7166     {
7167       token = cp_lexer_peek_token (parser->lexer);
7168       switch (token->type)
7169         {
7170         case CPP_OPEN_SQUARE:
7171           /* offsetof-member-designator "[" expression "]" */
7172           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7173           break;
7174
7175         case CPP_DEREF:
7176           /* offsetof-member-designator "->" identifier */
7177           expr = grok_array_decl (expr, integer_zero_node);
7178           /* FALLTHRU */
7179
7180         case CPP_DOT:
7181           /* offsetof-member-designator "." identifier */
7182           cp_lexer_consume_token (parser->lexer);
7183           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7184                                                          expr, true, &dummy,
7185                                                          token->location);
7186           break;
7187
7188         case CPP_CLOSE_PAREN:
7189           /* Consume the ")" token.  */
7190           cp_lexer_consume_token (parser->lexer);
7191           goto success;
7192
7193         default:
7194           /* Error.  We know the following require will fail, but
7195              that gives the proper error message.  */
7196           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7197           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7198           expr = error_mark_node;
7199           goto failure;
7200         }
7201     }
7202
7203  success:
7204   /* If we're processing a template, we can't finish the semantics yet.
7205      Otherwise we can fold the entire expression now.  */
7206   if (processing_template_decl)
7207     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7208   else
7209     expr = finish_offsetof (expr);
7210
7211  failure:
7212   parser->integral_constant_expression_p = save_ice_p;
7213   parser->non_integral_constant_expression_p = save_non_ice_p;
7214
7215   return expr;
7216 }
7217
7218 /* Parse a trait expression.
7219
7220    Returns a representation of the expression, the underlying type
7221    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7222
7223 static tree
7224 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7225 {
7226   cp_trait_kind kind;
7227   tree type1, type2 = NULL_TREE;
7228   bool binary = false;
7229   cp_decl_specifier_seq decl_specs;
7230
7231   switch (keyword)
7232     {
7233     case RID_HAS_NOTHROW_ASSIGN:
7234       kind = CPTK_HAS_NOTHROW_ASSIGN;
7235       break;
7236     case RID_HAS_NOTHROW_CONSTRUCTOR:
7237       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7238       break;
7239     case RID_HAS_NOTHROW_COPY:
7240       kind = CPTK_HAS_NOTHROW_COPY;
7241       break;
7242     case RID_HAS_TRIVIAL_ASSIGN:
7243       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7244       break;
7245     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7246       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7247       break;
7248     case RID_HAS_TRIVIAL_COPY:
7249       kind = CPTK_HAS_TRIVIAL_COPY;
7250       break;
7251     case RID_HAS_TRIVIAL_DESTRUCTOR:
7252       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7253       break;
7254     case RID_HAS_VIRTUAL_DESTRUCTOR:
7255       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7256       break;
7257     case RID_IS_ABSTRACT:
7258       kind = CPTK_IS_ABSTRACT;
7259       break;
7260     case RID_IS_BASE_OF:
7261       kind = CPTK_IS_BASE_OF;
7262       binary = true;
7263       break;
7264     case RID_IS_CLASS:
7265       kind = CPTK_IS_CLASS;
7266       break;
7267     case RID_IS_CONVERTIBLE_TO:
7268       kind = CPTK_IS_CONVERTIBLE_TO;
7269       binary = true;
7270       break;
7271     case RID_IS_EMPTY:
7272       kind = CPTK_IS_EMPTY;
7273       break;
7274     case RID_IS_ENUM:
7275       kind = CPTK_IS_ENUM;
7276       break;
7277     case RID_IS_LITERAL_TYPE:
7278       kind = CPTK_IS_LITERAL_TYPE;
7279       break;
7280     case RID_IS_POD:
7281       kind = CPTK_IS_POD;
7282       break;
7283     case RID_IS_POLYMORPHIC:
7284       kind = CPTK_IS_POLYMORPHIC;
7285       break;
7286     case RID_IS_STD_LAYOUT:
7287       kind = CPTK_IS_STD_LAYOUT;
7288       break;
7289     case RID_IS_TRIVIAL:
7290       kind = CPTK_IS_TRIVIAL;
7291       break;
7292     case RID_IS_UNION:
7293       kind = CPTK_IS_UNION;
7294       break;
7295     case RID_UNDERLYING_TYPE:
7296       kind = CPTK_UNDERLYING_TYPE;
7297       break;
7298     default:
7299       gcc_unreachable ();
7300     }
7301
7302   /* Consume the token.  */
7303   cp_lexer_consume_token (parser->lexer);
7304
7305   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7306
7307   type1 = cp_parser_type_id (parser);
7308
7309   if (type1 == error_mark_node)
7310     return error_mark_node;
7311
7312   /* Build a trivial decl-specifier-seq.  */
7313   clear_decl_specs (&decl_specs);
7314   decl_specs.type = type1;
7315
7316   /* Call grokdeclarator to figure out what type this is.  */
7317   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7318                           /*initialized=*/0, /*attrlist=*/NULL);
7319
7320   if (binary)
7321     {
7322       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7323  
7324       type2 = cp_parser_type_id (parser);
7325
7326       if (type2 == error_mark_node)
7327         return error_mark_node;
7328
7329       /* Build a trivial decl-specifier-seq.  */
7330       clear_decl_specs (&decl_specs);
7331       decl_specs.type = type2;
7332
7333       /* Call grokdeclarator to figure out what type this is.  */
7334       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7335                               /*initialized=*/0, /*attrlist=*/NULL);
7336     }
7337
7338   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7339
7340   /* Complete the trait expression, which may mean either processing
7341      the trait expr now or saving it for template instantiation.  */
7342   return kind != CPTK_UNDERLYING_TYPE
7343     ? finish_trait_expr (kind, type1, type2)
7344     : finish_underlying_type (type1);
7345 }
7346
7347 /* Lambdas that appear in variable initializer or default argument scope
7348    get that in their mangling, so we need to record it.  We might as well
7349    use the count for function and namespace scopes as well.  */
7350 static GTY(()) tree lambda_scope;
7351 static GTY(()) int lambda_count;
7352 typedef struct GTY(()) tree_int
7353 {
7354   tree t;
7355   int i;
7356 } tree_int;
7357 DEF_VEC_O(tree_int);
7358 DEF_VEC_ALLOC_O(tree_int,gc);
7359 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7360
7361 static void
7362 start_lambda_scope (tree decl)
7363 {
7364   tree_int ti;
7365   gcc_assert (decl);
7366   /* Once we're inside a function, we ignore other scopes and just push
7367      the function again so that popping works properly.  */
7368   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7369     decl = current_function_decl;
7370   ti.t = lambda_scope;
7371   ti.i = lambda_count;
7372   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7373   if (lambda_scope != decl)
7374     {
7375       /* Don't reset the count if we're still in the same function.  */
7376       lambda_scope = decl;
7377       lambda_count = 0;
7378     }
7379 }
7380
7381 static void
7382 record_lambda_scope (tree lambda)
7383 {
7384   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7385   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7386 }
7387
7388 static void
7389 finish_lambda_scope (void)
7390 {
7391   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7392   if (lambda_scope != p->t)
7393     {
7394       lambda_scope = p->t;
7395       lambda_count = p->i;
7396     }
7397   VEC_pop (tree_int, lambda_scope_stack);
7398 }
7399
7400 /* Parse a lambda expression.
7401
7402    lambda-expression:
7403      lambda-introducer lambda-declarator [opt] compound-statement
7404
7405    Returns a representation of the expression.  */
7406
7407 static tree
7408 cp_parser_lambda_expression (cp_parser* parser)
7409 {
7410   tree lambda_expr = build_lambda_expr ();
7411   tree type;
7412   bool ok;
7413
7414   LAMBDA_EXPR_LOCATION (lambda_expr)
7415     = cp_lexer_peek_token (parser->lexer)->location;
7416
7417   if (cp_unevaluated_operand)
7418     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7419               "lambda-expression in unevaluated context");
7420
7421   /* We may be in the middle of deferred access check.  Disable
7422      it now.  */
7423   push_deferring_access_checks (dk_no_deferred);
7424
7425   cp_parser_lambda_introducer (parser, lambda_expr);
7426
7427   type = begin_lambda_type (lambda_expr);
7428
7429   record_lambda_scope (lambda_expr);
7430
7431   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7432   determine_visibility (TYPE_NAME (type));
7433
7434   /* Now that we've started the type, add the capture fields for any
7435      explicit captures.  */
7436   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7437
7438   {
7439     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7440     unsigned int saved_num_template_parameter_lists
7441         = parser->num_template_parameter_lists;
7442     unsigned char in_statement = parser->in_statement;
7443     bool in_switch_statement_p = parser->in_switch_statement_p;
7444
7445     parser->num_template_parameter_lists = 0;
7446     parser->in_statement = 0;
7447     parser->in_switch_statement_p = false;
7448
7449     /* By virtue of defining a local class, a lambda expression has access to
7450        the private variables of enclosing classes.  */
7451
7452     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7453
7454     if (ok)
7455       cp_parser_lambda_body (parser, lambda_expr);
7456     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7457       cp_parser_skip_to_end_of_block_or_statement (parser);
7458
7459     /* The capture list was built up in reverse order; fix that now.  */
7460     {
7461       tree newlist = NULL_TREE;
7462       tree elt, next;
7463
7464       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7465            elt; elt = next)
7466         {
7467           next = TREE_CHAIN (elt);
7468           TREE_CHAIN (elt) = newlist;
7469           newlist = elt;
7470         }
7471       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7472     }
7473
7474     if (ok)
7475       maybe_add_lambda_conv_op (type);
7476
7477     type = finish_struct (type, /*attributes=*/NULL_TREE);
7478
7479     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7480     parser->in_statement = in_statement;
7481     parser->in_switch_statement_p = in_switch_statement_p;
7482   }
7483
7484   pop_deferring_access_checks ();
7485
7486   /* This field is only used during parsing of the lambda.  */
7487   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7488
7489   /* This lambda shouldn't have any proxies left at this point.  */
7490   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7491   /* And now that we're done, push proxies for an enclosing lambda.  */
7492   insert_pending_capture_proxies ();
7493
7494   if (ok)
7495     return build_lambda_object (lambda_expr);
7496   else
7497     return error_mark_node;
7498 }
7499
7500 /* Parse the beginning of a lambda expression.
7501
7502    lambda-introducer:
7503      [ lambda-capture [opt] ]
7504
7505    LAMBDA_EXPR is the current representation of the lambda expression.  */
7506
7507 static void
7508 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7509 {
7510   /* Need commas after the first capture.  */
7511   bool first = true;
7512
7513   /* Eat the leading `['.  */
7514   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7515
7516   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7517   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7518       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7519     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7520   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7521     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7522
7523   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7524     {
7525       cp_lexer_consume_token (parser->lexer);
7526       first = false;
7527     }
7528
7529   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7530     {
7531       cp_token* capture_token;
7532       tree capture_id;
7533       tree capture_init_expr;
7534       cp_id_kind idk = CP_ID_KIND_NONE;
7535       bool explicit_init_p = false;
7536
7537       enum capture_kind_type
7538       {
7539         BY_COPY,
7540         BY_REFERENCE
7541       };
7542       enum capture_kind_type capture_kind = BY_COPY;
7543
7544       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7545         {
7546           error ("expected end of capture-list");
7547           return;
7548         }
7549
7550       if (first)
7551         first = false;
7552       else
7553         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7554
7555       /* Possibly capture `this'.  */
7556       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7557         {
7558           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7559           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7560             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7561                      "with by-copy capture default");
7562           cp_lexer_consume_token (parser->lexer);
7563           add_capture (lambda_expr,
7564                        /*id=*/this_identifier,
7565                        /*initializer=*/finish_this_expr(),
7566                        /*by_reference_p=*/false,
7567                        explicit_init_p);
7568           continue;
7569         }
7570
7571       /* Remember whether we want to capture as a reference or not.  */
7572       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7573         {
7574           capture_kind = BY_REFERENCE;
7575           cp_lexer_consume_token (parser->lexer);
7576         }
7577
7578       /* Get the identifier.  */
7579       capture_token = cp_lexer_peek_token (parser->lexer);
7580       capture_id = cp_parser_identifier (parser);
7581
7582       if (capture_id == error_mark_node)
7583         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7584            delimiters, but I modified this to stop on unnested ']' as well.  It
7585            was already changed to stop on unnested '}', so the
7586            "closing_parenthesis" name is no more misleading with my change.  */
7587         {
7588           cp_parser_skip_to_closing_parenthesis (parser,
7589                                                  /*recovering=*/true,
7590                                                  /*or_comma=*/true,
7591                                                  /*consume_paren=*/true);
7592           break;
7593         }
7594
7595       /* Find the initializer for this capture.  */
7596       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7597         {
7598           /* An explicit expression exists.  */
7599           cp_lexer_consume_token (parser->lexer);
7600           pedwarn (input_location, OPT_pedantic,
7601                    "ISO C++ does not allow initializers "
7602                    "in lambda expression capture lists");
7603           capture_init_expr = cp_parser_assignment_expression (parser,
7604                                                                /*cast_p=*/true,
7605                                                                &idk);
7606           explicit_init_p = true;
7607         }
7608       else
7609         {
7610           const char* error_msg;
7611
7612           /* Turn the identifier into an id-expression.  */
7613           capture_init_expr
7614             = cp_parser_lookup_name
7615                 (parser,
7616                  capture_id,
7617                  none_type,
7618                  /*is_template=*/false,
7619                  /*is_namespace=*/false,
7620                  /*check_dependency=*/true,
7621                  /*ambiguous_decls=*/NULL,
7622                  capture_token->location);
7623
7624           capture_init_expr
7625             = finish_id_expression
7626                 (capture_id,
7627                  capture_init_expr,
7628                  parser->scope,
7629                  &idk,
7630                  /*integral_constant_expression_p=*/false,
7631                  /*allow_non_integral_constant_expression_p=*/false,
7632                  /*non_integral_constant_expression_p=*/NULL,
7633                  /*template_p=*/false,
7634                  /*done=*/true,
7635                  /*address_p=*/false,
7636                  /*template_arg_p=*/false,
7637                  &error_msg,
7638                  capture_token->location);
7639         }
7640
7641       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7642         capture_init_expr
7643           = unqualified_name_lookup_error (capture_init_expr);
7644
7645       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7646           && !explicit_init_p)
7647         {
7648           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7649               && capture_kind == BY_COPY)
7650             pedwarn (capture_token->location, 0, "explicit by-copy capture "
7651                      "of %qD redundant with by-copy capture default",
7652                      capture_id);
7653           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7654               && capture_kind == BY_REFERENCE)
7655             pedwarn (capture_token->location, 0, "explicit by-reference "
7656                      "capture of %qD redundant with by-reference capture "
7657                      "default", capture_id);
7658         }
7659
7660       add_capture (lambda_expr,
7661                    capture_id,
7662                    capture_init_expr,
7663                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7664                    explicit_init_p);
7665     }
7666
7667   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7668 }
7669
7670 /* Parse the (optional) middle of a lambda expression.
7671
7672    lambda-declarator:
7673      ( parameter-declaration-clause [opt] )
7674        attribute-specifier [opt]
7675        mutable [opt]
7676        exception-specification [opt]
7677        lambda-return-type-clause [opt]
7678
7679    LAMBDA_EXPR is the current representation of the lambda expression.  */
7680
7681 static bool
7682 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7683 {
7684   /* 5.1.1.4 of the standard says:
7685        If a lambda-expression does not include a lambda-declarator, it is as if
7686        the lambda-declarator were ().
7687      This means an empty parameter list, no attributes, and no exception
7688      specification.  */
7689   tree param_list = void_list_node;
7690   tree attributes = NULL_TREE;
7691   tree exception_spec = NULL_TREE;
7692   tree t;
7693
7694   /* The lambda-declarator is optional, but must begin with an opening
7695      parenthesis if present.  */
7696   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7697     {
7698       cp_lexer_consume_token (parser->lexer);
7699
7700       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7701
7702       /* Parse parameters.  */
7703       param_list = cp_parser_parameter_declaration_clause (parser);
7704
7705       /* Default arguments shall not be specified in the
7706          parameter-declaration-clause of a lambda-declarator.  */
7707       for (t = param_list; t; t = TREE_CHAIN (t))
7708         if (TREE_PURPOSE (t))
7709           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7710                    "default argument specified for lambda parameter");
7711
7712       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7713
7714       attributes = cp_parser_attributes_opt (parser);
7715
7716       /* Parse optional `mutable' keyword.  */
7717       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7718         {
7719           cp_lexer_consume_token (parser->lexer);
7720           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7721         }
7722
7723       /* Parse optional exception specification.  */
7724       exception_spec = cp_parser_exception_specification_opt (parser);
7725
7726       /* Parse optional trailing return type.  */
7727       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7728         {
7729           cp_lexer_consume_token (parser->lexer);
7730           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7731         }
7732
7733       /* The function parameters must be in scope all the way until after the
7734          trailing-return-type in case of decltype.  */
7735       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7736         pop_binding (DECL_NAME (t), t);
7737
7738       leave_scope ();
7739     }
7740
7741   /* Create the function call operator.
7742
7743      Messing with declarators like this is no uglier than building up the
7744      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7745      other code.  */
7746   {
7747     cp_decl_specifier_seq return_type_specs;
7748     cp_declarator* declarator;
7749     tree fco;
7750     int quals;
7751     void *p;
7752
7753     clear_decl_specs (&return_type_specs);
7754     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7755       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7756     else
7757       /* Maybe we will deduce the return type later, but we can use void
7758          as a placeholder return type anyways.  */
7759       return_type_specs.type = void_type_node;
7760
7761     p = obstack_alloc (&declarator_obstack, 0);
7762
7763     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7764                                      sfk_none);
7765
7766     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7767              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7768     declarator = make_call_declarator (declarator, param_list, quals,
7769                                        VIRT_SPEC_UNSPECIFIED,
7770                                        exception_spec,
7771                                        /*late_return_type=*/NULL_TREE);
7772     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7773
7774     fco = grokmethod (&return_type_specs,
7775                       declarator,
7776                       attributes);
7777     if (fco != error_mark_node)
7778       {
7779         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7780         DECL_ARTIFICIAL (fco) = 1;
7781         /* Give the object parameter a different name.  */
7782         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7783       }
7784
7785     finish_member_declaration (fco);
7786
7787     obstack_free (&declarator_obstack, p);
7788
7789     return (fco != error_mark_node);
7790   }
7791 }
7792
7793 /* Parse the body of a lambda expression, which is simply
7794
7795    compound-statement
7796
7797    but which requires special handling.
7798    LAMBDA_EXPR is the current representation of the lambda expression.  */
7799
7800 static void
7801 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7802 {
7803   bool nested = (current_function_decl != NULL_TREE);
7804   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
7805   if (nested)
7806     push_function_context ();
7807   else
7808     /* Still increment function_depth so that we don't GC in the
7809        middle of an expression.  */
7810     ++function_depth;
7811   /* Clear this in case we're in the middle of a default argument.  */
7812   parser->local_variables_forbidden_p = false;
7813
7814   /* Finish the function call operator
7815      - class_specifier
7816      + late_parsing_for_member
7817      + function_definition_after_declarator
7818      + ctor_initializer_opt_and_function_body  */
7819   {
7820     tree fco = lambda_function (lambda_expr);
7821     tree body;
7822     bool done = false;
7823     tree compound_stmt;
7824     tree cap;
7825
7826     /* Let the front end know that we are going to be defining this
7827        function.  */
7828     start_preparsed_function (fco,
7829                               NULL_TREE,
7830                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7831
7832     start_lambda_scope (fco);
7833     body = begin_function_body ();
7834
7835     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7836       goto out;
7837
7838     /* Push the proxies for any explicit captures.  */
7839     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7840          cap = TREE_CHAIN (cap))
7841       build_capture_proxy (TREE_PURPOSE (cap));
7842
7843     compound_stmt = begin_compound_stmt (0);
7844
7845     /* 5.1.1.4 of the standard says:
7846          If a lambda-expression does not include a trailing-return-type, it
7847          is as if the trailing-return-type denotes the following type:
7848           * if the compound-statement is of the form
7849                { return attribute-specifier [opt] expression ; }
7850              the type of the returned expression after lvalue-to-rvalue
7851              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7852              (_conv.array_ 4.2), and function-to-pointer conversion
7853              (_conv.func_ 4.3);
7854           * otherwise, void.  */
7855
7856     /* In a lambda that has neither a lambda-return-type-clause
7857        nor a deducible form, errors should be reported for return statements
7858        in the body.  Since we used void as the placeholder return type, parsing
7859        the body as usual will give such desired behavior.  */
7860     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7861         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7862         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7863       {
7864         tree expr = NULL_TREE;
7865         cp_id_kind idk = CP_ID_KIND_NONE;
7866
7867         /* Parse tentatively in case there's more after the initial return
7868            statement.  */
7869         cp_parser_parse_tentatively (parser);
7870
7871         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7872
7873         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7874
7875         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7876         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7877
7878         if (cp_parser_parse_definitely (parser))
7879           {
7880             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7881
7882             /* Will get error here if type not deduced yet.  */
7883             finish_return_stmt (expr);
7884
7885             done = true;
7886           }
7887       }
7888
7889     if (!done)
7890       {
7891         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7892           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7893         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7894           cp_parser_label_declaration (parser);
7895         cp_parser_statement_seq_opt (parser, NULL_TREE);
7896         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7897         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7898       }
7899
7900     finish_compound_stmt (compound_stmt);
7901
7902   out:
7903     finish_function_body (body);
7904     finish_lambda_scope ();
7905
7906     /* Finish the function and generate code for it if necessary.  */
7907     expand_or_defer_fn (finish_function (/*inline*/2));
7908   }
7909
7910   parser->local_variables_forbidden_p = local_variables_forbidden_p;
7911   if (nested)
7912     pop_function_context();
7913   else
7914     --function_depth;
7915 }
7916
7917 /* Statements [gram.stmt.stmt]  */
7918
7919 /* Parse a statement.
7920
7921    statement:
7922      labeled-statement
7923      expression-statement
7924      compound-statement
7925      selection-statement
7926      iteration-statement
7927      jump-statement
7928      declaration-statement
7929      try-block
7930
7931   IN_COMPOUND is true when the statement is nested inside a
7932   cp_parser_compound_statement; this matters for certain pragmas.
7933
7934   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7935   is a (possibly labeled) if statement which is not enclosed in braces
7936   and has an else clause.  This is used to implement -Wparentheses.  */
7937
7938 static void
7939 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7940                      bool in_compound, bool *if_p)
7941 {
7942   tree statement;
7943   cp_token *token;
7944   location_t statement_location;
7945
7946  restart:
7947   if (if_p != NULL)
7948     *if_p = false;
7949   /* There is no statement yet.  */
7950   statement = NULL_TREE;
7951   /* Peek at the next token.  */
7952   token = cp_lexer_peek_token (parser->lexer);
7953   /* Remember the location of the first token in the statement.  */
7954   statement_location = token->location;
7955   /* If this is a keyword, then that will often determine what kind of
7956      statement we have.  */
7957   if (token->type == CPP_KEYWORD)
7958     {
7959       enum rid keyword = token->keyword;
7960
7961       switch (keyword)
7962         {
7963         case RID_CASE:
7964         case RID_DEFAULT:
7965           /* Looks like a labeled-statement with a case label.
7966              Parse the label, and then use tail recursion to parse
7967              the statement.  */
7968           cp_parser_label_for_labeled_statement (parser);
7969           goto restart;
7970
7971         case RID_IF:
7972         case RID_SWITCH:
7973           statement = cp_parser_selection_statement (parser, if_p);
7974           break;
7975
7976         case RID_WHILE:
7977         case RID_DO:
7978         case RID_FOR:
7979           statement = cp_parser_iteration_statement (parser);
7980           break;
7981
7982         case RID_BREAK:
7983         case RID_CONTINUE:
7984         case RID_RETURN:
7985         case RID_GOTO:
7986           statement = cp_parser_jump_statement (parser);
7987           break;
7988
7989           /* Objective-C++ exception-handling constructs.  */
7990         case RID_AT_TRY:
7991         case RID_AT_CATCH:
7992         case RID_AT_FINALLY:
7993         case RID_AT_SYNCHRONIZED:
7994         case RID_AT_THROW:
7995           statement = cp_parser_objc_statement (parser);
7996           break;
7997
7998         case RID_TRY:
7999           statement = cp_parser_try_block (parser);
8000           break;
8001
8002         case RID_NAMESPACE:
8003           /* This must be a namespace alias definition.  */
8004           cp_parser_declaration_statement (parser);
8005           return;
8006           
8007         default:
8008           /* It might be a keyword like `int' that can start a
8009              declaration-statement.  */
8010           break;
8011         }
8012     }
8013   else if (token->type == CPP_NAME)
8014     {
8015       /* If the next token is a `:', then we are looking at a
8016          labeled-statement.  */
8017       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8018       if (token->type == CPP_COLON)
8019         {
8020           /* Looks like a labeled-statement with an ordinary label.
8021              Parse the label, and then use tail recursion to parse
8022              the statement.  */
8023           cp_parser_label_for_labeled_statement (parser);
8024           goto restart;
8025         }
8026     }
8027   /* Anything that starts with a `{' must be a compound-statement.  */
8028   else if (token->type == CPP_OPEN_BRACE)
8029     statement = cp_parser_compound_statement (parser, NULL, false, false);
8030   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8031      a statement all its own.  */
8032   else if (token->type == CPP_PRAGMA)
8033     {
8034       /* Only certain OpenMP pragmas are attached to statements, and thus
8035          are considered statements themselves.  All others are not.  In
8036          the context of a compound, accept the pragma as a "statement" and
8037          return so that we can check for a close brace.  Otherwise we
8038          require a real statement and must go back and read one.  */
8039       if (in_compound)
8040         cp_parser_pragma (parser, pragma_compound);
8041       else if (!cp_parser_pragma (parser, pragma_stmt))
8042         goto restart;
8043       return;
8044     }
8045   else if (token->type == CPP_EOF)
8046     {
8047       cp_parser_error (parser, "expected statement");
8048       return;
8049     }
8050
8051   /* Everything else must be a declaration-statement or an
8052      expression-statement.  Try for the declaration-statement
8053      first, unless we are looking at a `;', in which case we know that
8054      we have an expression-statement.  */
8055   if (!statement)
8056     {
8057       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8058         {
8059           cp_parser_parse_tentatively (parser);
8060           /* Try to parse the declaration-statement.  */
8061           cp_parser_declaration_statement (parser);
8062           /* If that worked, we're done.  */
8063           if (cp_parser_parse_definitely (parser))
8064             return;
8065         }
8066       /* Look for an expression-statement instead.  */
8067       statement = cp_parser_expression_statement (parser, in_statement_expr);
8068     }
8069
8070   /* Set the line number for the statement.  */
8071   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8072     SET_EXPR_LOCATION (statement, statement_location);
8073 }
8074
8075 /* Parse the label for a labeled-statement, i.e.
8076
8077    identifier :
8078    case constant-expression :
8079    default :
8080
8081    GNU Extension:
8082    case constant-expression ... constant-expression : statement
8083
8084    When a label is parsed without errors, the label is added to the
8085    parse tree by the finish_* functions, so this function doesn't
8086    have to return the label.  */
8087
8088 static void
8089 cp_parser_label_for_labeled_statement (cp_parser* parser)
8090 {
8091   cp_token *token;
8092   tree label = NULL_TREE;
8093   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8094
8095   /* The next token should be an identifier.  */
8096   token = cp_lexer_peek_token (parser->lexer);
8097   if (token->type != CPP_NAME
8098       && token->type != CPP_KEYWORD)
8099     {
8100       cp_parser_error (parser, "expected labeled-statement");
8101       return;
8102     }
8103
8104   parser->colon_corrects_to_scope_p = false;
8105   switch (token->keyword)
8106     {
8107     case RID_CASE:
8108       {
8109         tree expr, expr_hi;
8110         cp_token *ellipsis;
8111
8112         /* Consume the `case' token.  */
8113         cp_lexer_consume_token (parser->lexer);
8114         /* Parse the constant-expression.  */
8115         expr = cp_parser_constant_expression (parser,
8116                                               /*allow_non_constant_p=*/false,
8117                                               NULL);
8118
8119         ellipsis = cp_lexer_peek_token (parser->lexer);
8120         if (ellipsis->type == CPP_ELLIPSIS)
8121           {
8122             /* Consume the `...' token.  */
8123             cp_lexer_consume_token (parser->lexer);
8124             expr_hi =
8125               cp_parser_constant_expression (parser,
8126                                              /*allow_non_constant_p=*/false,
8127                                              NULL);
8128             /* We don't need to emit warnings here, as the common code
8129                will do this for us.  */
8130           }
8131         else
8132           expr_hi = NULL_TREE;
8133
8134         if (parser->in_switch_statement_p)
8135           finish_case_label (token->location, expr, expr_hi);
8136         else
8137           error_at (token->location,
8138                     "case label %qE not within a switch statement",
8139                     expr);
8140       }
8141       break;
8142
8143     case RID_DEFAULT:
8144       /* Consume the `default' token.  */
8145       cp_lexer_consume_token (parser->lexer);
8146
8147       if (parser->in_switch_statement_p)
8148         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8149       else
8150         error_at (token->location, "case label not within a switch statement");
8151       break;
8152
8153     default:
8154       /* Anything else must be an ordinary label.  */
8155       label = finish_label_stmt (cp_parser_identifier (parser));
8156       break;
8157     }
8158
8159   /* Require the `:' token.  */
8160   cp_parser_require (parser, CPP_COLON, RT_COLON);
8161
8162   /* An ordinary label may optionally be followed by attributes.
8163      However, this is only permitted if the attributes are then
8164      followed by a semicolon.  This is because, for backward
8165      compatibility, when parsing
8166        lab: __attribute__ ((unused)) int i;
8167      we want the attribute to attach to "i", not "lab".  */
8168   if (label != NULL_TREE
8169       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8170     {
8171       tree attrs;
8172
8173       cp_parser_parse_tentatively (parser);
8174       attrs = cp_parser_attributes_opt (parser);
8175       if (attrs == NULL_TREE
8176           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8177         cp_parser_abort_tentative_parse (parser);
8178       else if (!cp_parser_parse_definitely (parser))
8179         ;
8180       else
8181         cplus_decl_attributes (&label, attrs, 0);
8182     }
8183
8184   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8185 }
8186
8187 /* Parse an expression-statement.
8188
8189    expression-statement:
8190      expression [opt] ;
8191
8192    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8193    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8194    indicates whether this expression-statement is part of an
8195    expression statement.  */
8196
8197 static tree
8198 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8199 {
8200   tree statement = NULL_TREE;
8201   cp_token *token = cp_lexer_peek_token (parser->lexer);
8202
8203   /* If the next token is a ';', then there is no expression
8204      statement.  */
8205   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8206     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8207
8208   /* Give a helpful message for "A<T>::type t;" and the like.  */
8209   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8210       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8211     {
8212       if (TREE_CODE (statement) == SCOPE_REF)
8213         error_at (token->location, "need %<typename%> before %qE because "
8214                   "%qT is a dependent scope",
8215                   statement, TREE_OPERAND (statement, 0));
8216       else if (is_overloaded_fn (statement)
8217                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8218         {
8219           /* A::A a; */
8220           tree fn = get_first_fn (statement);
8221           error_at (token->location,
8222                     "%<%T::%D%> names the constructor, not the type",
8223                     DECL_CONTEXT (fn), DECL_NAME (fn));
8224         }
8225     }
8226
8227   /* Consume the final `;'.  */
8228   cp_parser_consume_semicolon_at_end_of_statement (parser);
8229
8230   if (in_statement_expr
8231       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8232     /* This is the final expression statement of a statement
8233        expression.  */
8234     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8235   else if (statement)
8236     statement = finish_expr_stmt (statement);
8237   else
8238     finish_stmt ();
8239
8240   return statement;
8241 }
8242
8243 /* Parse a compound-statement.
8244
8245    compound-statement:
8246      { statement-seq [opt] }
8247
8248    GNU extension:
8249
8250    compound-statement:
8251      { label-declaration-seq [opt] statement-seq [opt] }
8252
8253    label-declaration-seq:
8254      label-declaration
8255      label-declaration-seq label-declaration
8256
8257    Returns a tree representing the statement.  */
8258
8259 static tree
8260 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8261                               bool in_try, bool function_body)
8262 {
8263   tree compound_stmt;
8264
8265   /* Consume the `{'.  */
8266   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8267     return error_mark_node;
8268   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8269       && !function_body)
8270     pedwarn (input_location, OPT_pedantic,
8271              "compound-statement in constexpr function");
8272   /* Begin the compound-statement.  */
8273   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8274   /* If the next keyword is `__label__' we have a label declaration.  */
8275   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8276     cp_parser_label_declaration (parser);
8277   /* Parse an (optional) statement-seq.  */
8278   cp_parser_statement_seq_opt (parser, in_statement_expr);
8279   /* Finish the compound-statement.  */
8280   finish_compound_stmt (compound_stmt);
8281   /* Consume the `}'.  */
8282   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8283
8284   return compound_stmt;
8285 }
8286
8287 /* Parse an (optional) statement-seq.
8288
8289    statement-seq:
8290      statement
8291      statement-seq [opt] statement  */
8292
8293 static void
8294 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8295 {
8296   /* Scan statements until there aren't any more.  */
8297   while (true)
8298     {
8299       cp_token *token = cp_lexer_peek_token (parser->lexer);
8300
8301       /* If we are looking at a `}', then we have run out of
8302          statements; the same is true if we have reached the end
8303          of file, or have stumbled upon a stray '@end'.  */
8304       if (token->type == CPP_CLOSE_BRACE
8305           || token->type == CPP_EOF
8306           || token->type == CPP_PRAGMA_EOL
8307           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8308         break;
8309       
8310       /* If we are in a compound statement and find 'else' then
8311          something went wrong.  */
8312       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8313         {
8314           if (parser->in_statement & IN_IF_STMT) 
8315             break;
8316           else
8317             {
8318               token = cp_lexer_consume_token (parser->lexer);
8319               error_at (token->location, "%<else%> without a previous %<if%>");
8320             }
8321         }
8322
8323       /* Parse the statement.  */
8324       cp_parser_statement (parser, in_statement_expr, true, NULL);
8325     }
8326 }
8327
8328 /* Parse a selection-statement.
8329
8330    selection-statement:
8331      if ( condition ) statement
8332      if ( condition ) statement else statement
8333      switch ( condition ) statement
8334
8335    Returns the new IF_STMT or SWITCH_STMT.
8336
8337    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8338    is a (possibly labeled) if statement which is not enclosed in
8339    braces and has an else clause.  This is used to implement
8340    -Wparentheses.  */
8341
8342 static tree
8343 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8344 {
8345   cp_token *token;
8346   enum rid keyword;
8347
8348   if (if_p != NULL)
8349     *if_p = false;
8350
8351   /* Peek at the next token.  */
8352   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8353
8354   /* See what kind of keyword it is.  */
8355   keyword = token->keyword;
8356   switch (keyword)
8357     {
8358     case RID_IF:
8359     case RID_SWITCH:
8360       {
8361         tree statement;
8362         tree condition;
8363
8364         /* Look for the `('.  */
8365         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8366           {
8367             cp_parser_skip_to_end_of_statement (parser);
8368             return error_mark_node;
8369           }
8370
8371         /* Begin the selection-statement.  */
8372         if (keyword == RID_IF)
8373           statement = begin_if_stmt ();
8374         else
8375           statement = begin_switch_stmt ();
8376
8377         /* Parse the condition.  */
8378         condition = cp_parser_condition (parser);
8379         /* Look for the `)'.  */
8380         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8381           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8382                                                  /*consume_paren=*/true);
8383
8384         if (keyword == RID_IF)
8385           {
8386             bool nested_if;
8387             unsigned char in_statement;
8388
8389             /* Add the condition.  */
8390             finish_if_stmt_cond (condition, statement);
8391
8392             /* Parse the then-clause.  */
8393             in_statement = parser->in_statement;
8394             parser->in_statement |= IN_IF_STMT;
8395             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8396               {
8397                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8398                 add_stmt (build_empty_stmt (loc));
8399                 cp_lexer_consume_token (parser->lexer);
8400                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8401                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8402                               "empty body in an %<if%> statement");
8403                 nested_if = false;
8404               }
8405             else
8406               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8407             parser->in_statement = in_statement;
8408
8409             finish_then_clause (statement);
8410
8411             /* If the next token is `else', parse the else-clause.  */
8412             if (cp_lexer_next_token_is_keyword (parser->lexer,
8413                                                 RID_ELSE))
8414               {
8415                 /* Consume the `else' keyword.  */
8416                 cp_lexer_consume_token (parser->lexer);
8417                 begin_else_clause (statement);
8418                 /* Parse the else-clause.  */
8419                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8420                   {
8421                     location_t loc;
8422                     loc = cp_lexer_peek_token (parser->lexer)->location;
8423                     warning_at (loc,
8424                                 OPT_Wempty_body, "suggest braces around "
8425                                 "empty body in an %<else%> statement");
8426                     add_stmt (build_empty_stmt (loc));
8427                     cp_lexer_consume_token (parser->lexer);
8428                   }
8429                 else
8430                   cp_parser_implicitly_scoped_statement (parser, NULL);
8431
8432                 finish_else_clause (statement);
8433
8434                 /* If we are currently parsing a then-clause, then
8435                    IF_P will not be NULL.  We set it to true to
8436                    indicate that this if statement has an else clause.
8437                    This may trigger the Wparentheses warning below
8438                    when we get back up to the parent if statement.  */
8439                 if (if_p != NULL)
8440                   *if_p = true;
8441               }
8442             else
8443               {
8444                 /* This if statement does not have an else clause.  If
8445                    NESTED_IF is true, then the then-clause is an if
8446                    statement which does have an else clause.  We warn
8447                    about the potential ambiguity.  */
8448                 if (nested_if)
8449                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8450                               "suggest explicit braces to avoid ambiguous"
8451                               " %<else%>");
8452               }
8453
8454             /* Now we're all done with the if-statement.  */
8455             finish_if_stmt (statement);
8456           }
8457         else
8458           {
8459             bool in_switch_statement_p;
8460             unsigned char in_statement;
8461
8462             /* Add the condition.  */
8463             finish_switch_cond (condition, statement);
8464
8465             /* Parse the body of the switch-statement.  */
8466             in_switch_statement_p = parser->in_switch_statement_p;
8467             in_statement = parser->in_statement;
8468             parser->in_switch_statement_p = true;
8469             parser->in_statement |= IN_SWITCH_STMT;
8470             cp_parser_implicitly_scoped_statement (parser, NULL);
8471             parser->in_switch_statement_p = in_switch_statement_p;
8472             parser->in_statement = in_statement;
8473
8474             /* Now we're all done with the switch-statement.  */
8475             finish_switch_stmt (statement);
8476           }
8477
8478         return statement;
8479       }
8480       break;
8481
8482     default:
8483       cp_parser_error (parser, "expected selection-statement");
8484       return error_mark_node;
8485     }
8486 }
8487
8488 /* Parse a condition.
8489
8490    condition:
8491      expression
8492      type-specifier-seq declarator = initializer-clause
8493      type-specifier-seq declarator braced-init-list
8494
8495    GNU Extension:
8496
8497    condition:
8498      type-specifier-seq declarator asm-specification [opt]
8499        attributes [opt] = assignment-expression
8500
8501    Returns the expression that should be tested.  */
8502
8503 static tree
8504 cp_parser_condition (cp_parser* parser)
8505 {
8506   cp_decl_specifier_seq type_specifiers;
8507   const char *saved_message;
8508   int declares_class_or_enum;
8509
8510   /* Try the declaration first.  */
8511   cp_parser_parse_tentatively (parser);
8512   /* New types are not allowed in the type-specifier-seq for a
8513      condition.  */
8514   saved_message = parser->type_definition_forbidden_message;
8515   parser->type_definition_forbidden_message
8516     = G_("types may not be defined in conditions");
8517   /* Parse the type-specifier-seq.  */
8518   cp_parser_decl_specifier_seq (parser,
8519                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8520                                 &type_specifiers,
8521                                 &declares_class_or_enum);
8522   /* Restore the saved message.  */
8523   parser->type_definition_forbidden_message = saved_message;
8524   /* If all is well, we might be looking at a declaration.  */
8525   if (!cp_parser_error_occurred (parser))
8526     {
8527       tree decl;
8528       tree asm_specification;
8529       tree attributes;
8530       cp_declarator *declarator;
8531       tree initializer = NULL_TREE;
8532
8533       /* Parse the declarator.  */
8534       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8535                                          /*ctor_dtor_or_conv_p=*/NULL,
8536                                          /*parenthesized_p=*/NULL,
8537                                          /*member_p=*/false);
8538       /* Parse the attributes.  */
8539       attributes = cp_parser_attributes_opt (parser);
8540       /* Parse the asm-specification.  */
8541       asm_specification = cp_parser_asm_specification_opt (parser);
8542       /* If the next token is not an `=' or '{', then we might still be
8543          looking at an expression.  For example:
8544
8545            if (A(a).x)
8546
8547          looks like a decl-specifier-seq and a declarator -- but then
8548          there is no `=', so this is an expression.  */
8549       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8550           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8551         cp_parser_simulate_error (parser);
8552         
8553       /* If we did see an `=' or '{', then we are looking at a declaration
8554          for sure.  */
8555       if (cp_parser_parse_definitely (parser))
8556         {
8557           tree pushed_scope;
8558           bool non_constant_p;
8559           bool flags = LOOKUP_ONLYCONVERTING;
8560
8561           /* Create the declaration.  */
8562           decl = start_decl (declarator, &type_specifiers,
8563                              /*initialized_p=*/true,
8564                              attributes, /*prefix_attributes=*/NULL_TREE,
8565                              &pushed_scope);
8566
8567           /* Parse the initializer.  */
8568           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8569             {
8570               initializer = cp_parser_braced_list (parser, &non_constant_p);
8571               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8572               flags = 0;
8573             }
8574           else
8575             {
8576               /* Consume the `='.  */
8577               cp_parser_require (parser, CPP_EQ, RT_EQ);
8578               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8579             }
8580           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8581             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8582
8583           /* Process the initializer.  */
8584           cp_finish_decl (decl,
8585                           initializer, !non_constant_p,
8586                           asm_specification,
8587                           flags);
8588
8589           if (pushed_scope)
8590             pop_scope (pushed_scope);
8591
8592           return convert_from_reference (decl);
8593         }
8594     }
8595   /* If we didn't even get past the declarator successfully, we are
8596      definitely not looking at a declaration.  */
8597   else
8598     cp_parser_abort_tentative_parse (parser);
8599
8600   /* Otherwise, we are looking at an expression.  */
8601   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8602 }
8603
8604 /* Parses a for-statement or range-for-statement until the closing ')',
8605    not included. */
8606
8607 static tree
8608 cp_parser_for (cp_parser *parser)
8609 {
8610   tree init, scope, decl;
8611   bool is_range_for;
8612
8613   /* Begin the for-statement.  */
8614   scope = begin_for_scope (&init);
8615
8616   /* Parse the initialization.  */
8617   is_range_for = cp_parser_for_init_statement (parser, &decl);
8618
8619   if (is_range_for)
8620     return cp_parser_range_for (parser, scope, init, decl);
8621   else
8622     return cp_parser_c_for (parser, scope, init);
8623 }
8624
8625 static tree
8626 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8627 {
8628   /* Normal for loop */
8629   tree condition = NULL_TREE;
8630   tree expression = NULL_TREE;
8631   tree stmt;
8632
8633   stmt = begin_for_stmt (scope, init);
8634   /* The for-init-statement has already been parsed in
8635      cp_parser_for_init_statement, so no work is needed here.  */
8636   finish_for_init_stmt (stmt);
8637
8638   /* If there's a condition, process it.  */
8639   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8640     condition = cp_parser_condition (parser);
8641   finish_for_cond (condition, stmt);
8642   /* Look for the `;'.  */
8643   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8644
8645   /* If there's an expression, process it.  */
8646   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8647     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8648   finish_for_expr (expression, stmt);
8649
8650   return stmt;
8651 }
8652
8653 /* Tries to parse a range-based for-statement:
8654
8655   range-based-for:
8656     decl-specifier-seq declarator : expression
8657
8658   The decl-specifier-seq declarator and the `:' are already parsed by
8659   cp_parser_for_init_statement. If processing_template_decl it returns a
8660   newly created RANGE_FOR_STMT; if not, it is converted to a
8661   regular FOR_STMT.  */
8662
8663 static tree
8664 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8665 {
8666   tree stmt, range_expr;
8667
8668   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8669     {
8670       bool expr_non_constant_p;
8671       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8672     }
8673   else
8674     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8675
8676   /* If in template, STMT is converted to a normal for-statement
8677      at instantiation. If not, it is done just ahead. */
8678   if (processing_template_decl)
8679     {
8680       stmt = begin_range_for_stmt (scope, init);
8681       finish_range_for_decl (stmt, range_decl, range_expr);
8682       do_range_for_auto_deduction (range_decl, range_expr);
8683     }
8684   else
8685     {
8686       stmt = begin_for_stmt (scope, init);
8687       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8688     }
8689   return stmt;
8690 }
8691
8692 /* Subroutine of cp_convert_range_for: given the initializer expression,
8693    builds up the range temporary.  */
8694
8695 static tree
8696 build_range_temp (tree range_expr)
8697 {
8698   tree range_type, range_temp;
8699
8700   /* Find out the type deduced by the declaration
8701      `auto &&__range = range_expr'.  */
8702   range_type = cp_build_reference_type (make_auto (), true);
8703   range_type = do_auto_deduction (range_type, range_expr,
8704                                   type_uses_auto (range_type));
8705
8706   /* Create the __range variable.  */
8707   range_temp = build_decl (input_location, VAR_DECL,
8708                            get_identifier ("__for_range"), range_type);
8709   TREE_USED (range_temp) = 1;
8710   DECL_ARTIFICIAL (range_temp) = 1;
8711
8712   return range_temp;
8713 }
8714
8715 /* Used by cp_parser_range_for in template context: we aren't going to
8716    do a full conversion yet, but we still need to resolve auto in the
8717    type of the for-range-declaration if present.  This is basically
8718    a shortcut version of cp_convert_range_for.  */
8719
8720 static void
8721 do_range_for_auto_deduction (tree decl, tree range_expr)
8722 {
8723   tree auto_node = type_uses_auto (TREE_TYPE (decl));
8724   if (auto_node)
8725     {
8726       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
8727       range_temp = convert_from_reference (build_range_temp (range_expr));
8728       iter_type = (cp_parser_perform_range_for_lookup
8729                    (range_temp, &begin_dummy, &end_dummy));
8730       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
8731       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
8732                                         tf_warning_or_error);
8733       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
8734                                             iter_decl, auto_node);
8735     }
8736 }
8737
8738 /* Converts a range-based for-statement into a normal
8739    for-statement, as per the definition.
8740
8741       for (RANGE_DECL : RANGE_EXPR)
8742         BLOCK
8743
8744    should be equivalent to:
8745
8746       {
8747         auto &&__range = RANGE_EXPR;
8748         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8749               __begin != __end;
8750               ++__begin)
8751           {
8752               RANGE_DECL = *__begin;
8753               BLOCK
8754           }
8755       }
8756
8757    If RANGE_EXPR is an array:
8758         BEGIN_EXPR = __range
8759         END_EXPR = __range + ARRAY_SIZE(__range)
8760    Else if RANGE_EXPR has a member 'begin' or 'end':
8761         BEGIN_EXPR = __range.begin()
8762         END_EXPR = __range.end()
8763    Else:
8764         BEGIN_EXPR = begin(__range)
8765         END_EXPR = end(__range);
8766
8767    If __range has a member 'begin' but not 'end', or vice versa, we must
8768    still use the second alternative (it will surely fail, however).
8769    When calling begin()/end() in the third alternative we must use
8770    argument dependent lookup, but always considering 'std' as an associated
8771    namespace.  */
8772
8773 tree
8774 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8775 {
8776   tree begin, end;
8777   tree iter_type, begin_expr, end_expr;
8778   tree condition, expression;
8779
8780   if (range_decl == error_mark_node || range_expr == error_mark_node)
8781     /* If an error happened previously do nothing or else a lot of
8782        unhelpful errors would be issued.  */
8783     begin_expr = end_expr = iter_type = error_mark_node;
8784   else
8785     {
8786       tree range_temp = build_range_temp (range_expr);
8787       pushdecl (range_temp);
8788       cp_finish_decl (range_temp, range_expr,
8789                       /*is_constant_init*/false, NULL_TREE,
8790                       LOOKUP_ONLYCONVERTING);
8791
8792       range_temp = convert_from_reference (range_temp);
8793       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8794                                                       &begin_expr, &end_expr);
8795     }
8796
8797   /* The new for initialization statement.  */
8798   begin = build_decl (input_location, VAR_DECL,
8799                       get_identifier ("__for_begin"), iter_type);
8800   TREE_USED (begin) = 1;
8801   DECL_ARTIFICIAL (begin) = 1;
8802   pushdecl (begin);
8803   cp_finish_decl (begin, begin_expr,
8804                   /*is_constant_init*/false, NULL_TREE,
8805                   LOOKUP_ONLYCONVERTING);
8806
8807   end = build_decl (input_location, VAR_DECL,
8808                     get_identifier ("__for_end"), iter_type);
8809   TREE_USED (end) = 1;
8810   DECL_ARTIFICIAL (end) = 1;
8811   pushdecl (end);
8812   cp_finish_decl (end, end_expr,
8813                   /*is_constant_init*/false, NULL_TREE,
8814                   LOOKUP_ONLYCONVERTING);
8815
8816   finish_for_init_stmt (statement);
8817
8818   /* The new for condition.  */
8819   condition = build_x_binary_op (NE_EXPR,
8820                                  begin, ERROR_MARK,
8821                                  end, ERROR_MARK,
8822                                  NULL, tf_warning_or_error);
8823   finish_for_cond (condition, statement);
8824
8825   /* The new increment expression.  */
8826   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8827   finish_for_expr (expression, statement);
8828
8829   /* The declaration is initialized with *__begin inside the loop body.  */
8830   cp_finish_decl (range_decl,
8831                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8832                   /*is_constant_init*/false, NULL_TREE,
8833                   LOOKUP_ONLYCONVERTING);
8834
8835   return statement;
8836 }
8837
8838 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8839    We need to solve both at the same time because the method used
8840    depends on the existence of members begin or end.
8841    Returns the type deduced for the iterator expression.  */
8842
8843 static tree
8844 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8845 {
8846   if (error_operand_p (range))
8847     {
8848       *begin = *end = error_mark_node;
8849       return error_mark_node;
8850     }
8851
8852   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8853     {
8854       error ("range-based %<for%> expression of type %qT "
8855              "has incomplete type", TREE_TYPE (range));
8856       *begin = *end = error_mark_node;
8857       return error_mark_node;
8858     }
8859   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8860     {
8861       /* If RANGE is an array, we will use pointer arithmetic.  */
8862       *begin = range;
8863       *end = build_binary_op (input_location, PLUS_EXPR,
8864                               range,
8865                               array_type_nelts_top (TREE_TYPE (range)),
8866                               0);
8867       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8868     }
8869   else
8870     {
8871       /* If it is not an array, we must do a bit of magic.  */
8872       tree id_begin, id_end;
8873       tree member_begin, member_end;
8874
8875       *begin = *end = error_mark_node;
8876
8877       id_begin = get_identifier ("begin");
8878       id_end = get_identifier ("end");
8879       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8880                                     /*protect=*/2, /*want_type=*/false);
8881       member_end = lookup_member (TREE_TYPE (range), id_end,
8882                                   /*protect=*/2, /*want_type=*/false);
8883
8884       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8885         {
8886           /* Use the member functions.  */
8887           if (member_begin != NULL_TREE)
8888             *begin = cp_parser_range_for_member_function (range, id_begin);
8889           else
8890             error ("range-based %<for%> expression of type %qT has an "
8891                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8892
8893           if (member_end != NULL_TREE)
8894             *end = cp_parser_range_for_member_function (range, id_end);
8895           else
8896             error ("range-based %<for%> expression of type %qT has a "
8897                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8898         }
8899       else
8900         {
8901           /* Use global functions with ADL.  */
8902           VEC(tree,gc) *vec;
8903           vec = make_tree_vector ();
8904
8905           VEC_safe_push (tree, gc, vec, range);
8906
8907           member_begin = perform_koenig_lookup (id_begin, vec,
8908                                                 /*include_std=*/true,
8909                                                 tf_warning_or_error);
8910           *begin = finish_call_expr (member_begin, &vec, false, true,
8911                                      tf_warning_or_error);
8912           member_end = perform_koenig_lookup (id_end, vec,
8913                                               /*include_std=*/true,
8914                                               tf_warning_or_error);
8915           *end = finish_call_expr (member_end, &vec, false, true,
8916                                    tf_warning_or_error);
8917
8918           release_tree_vector (vec);
8919         }
8920
8921       /* Last common checks.  */
8922       if (*begin == error_mark_node || *end == error_mark_node)
8923         {
8924           /* If one of the expressions is an error do no more checks.  */
8925           *begin = *end = error_mark_node;
8926           return error_mark_node;
8927         }
8928       else
8929         {
8930           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8931           /* The unqualified type of the __begin and __end temporaries should
8932              be the same, as required by the multiple auto declaration.  */
8933           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8934             error ("inconsistent begin/end types in range-based %<for%> "
8935                    "statement: %qT and %qT",
8936                    TREE_TYPE (*begin), TREE_TYPE (*end));
8937           return iter_type;
8938         }
8939     }
8940 }
8941
8942 /* Helper function for cp_parser_perform_range_for_lookup.
8943    Builds a tree for RANGE.IDENTIFIER().  */
8944
8945 static tree
8946 cp_parser_range_for_member_function (tree range, tree identifier)
8947 {
8948   tree member, res;
8949   VEC(tree,gc) *vec;
8950
8951   member = finish_class_member_access_expr (range, identifier,
8952                                             false, tf_warning_or_error);
8953   if (member == error_mark_node)
8954     return error_mark_node;
8955
8956   vec = make_tree_vector ();
8957   res = finish_call_expr (member, &vec,
8958                           /*disallow_virtual=*/false,
8959                           /*koenig_p=*/false,
8960                           tf_warning_or_error);
8961   release_tree_vector (vec);
8962   return res;
8963 }
8964
8965 /* Parse an iteration-statement.
8966
8967    iteration-statement:
8968      while ( condition ) statement
8969      do statement while ( expression ) ;
8970      for ( for-init-statement condition [opt] ; expression [opt] )
8971        statement
8972
8973    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8974
8975 static tree
8976 cp_parser_iteration_statement (cp_parser* parser)
8977 {
8978   cp_token *token;
8979   enum rid keyword;
8980   tree statement;
8981   unsigned char in_statement;
8982
8983   /* Peek at the next token.  */
8984   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8985   if (!token)
8986     return error_mark_node;
8987
8988   /* Remember whether or not we are already within an iteration
8989      statement.  */
8990   in_statement = parser->in_statement;
8991
8992   /* See what kind of keyword it is.  */
8993   keyword = token->keyword;
8994   switch (keyword)
8995     {
8996     case RID_WHILE:
8997       {
8998         tree condition;
8999
9000         /* Begin the while-statement.  */
9001         statement = begin_while_stmt ();
9002         /* Look for the `('.  */
9003         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9004         /* Parse the condition.  */
9005         condition = cp_parser_condition (parser);
9006         finish_while_stmt_cond (condition, statement);
9007         /* Look for the `)'.  */
9008         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9009         /* Parse the dependent statement.  */
9010         parser->in_statement = IN_ITERATION_STMT;
9011         cp_parser_already_scoped_statement (parser);
9012         parser->in_statement = in_statement;
9013         /* We're done with the while-statement.  */
9014         finish_while_stmt (statement);
9015       }
9016       break;
9017
9018     case RID_DO:
9019       {
9020         tree expression;
9021
9022         /* Begin the do-statement.  */
9023         statement = begin_do_stmt ();
9024         /* Parse the body of the do-statement.  */
9025         parser->in_statement = IN_ITERATION_STMT;
9026         cp_parser_implicitly_scoped_statement (parser, NULL);
9027         parser->in_statement = in_statement;
9028         finish_do_body (statement);
9029         /* Look for the `while' keyword.  */
9030         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9031         /* Look for the `('.  */
9032         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9033         /* Parse the expression.  */
9034         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9035         /* We're done with the do-statement.  */
9036         finish_do_stmt (expression, statement);
9037         /* Look for the `)'.  */
9038         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9039         /* Look for the `;'.  */
9040         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9041       }
9042       break;
9043
9044     case RID_FOR:
9045       {
9046         /* Look for the `('.  */
9047         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9048
9049         statement = cp_parser_for (parser);
9050
9051         /* Look for the `)'.  */
9052         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9053
9054         /* Parse the body of the for-statement.  */
9055         parser->in_statement = IN_ITERATION_STMT;
9056         cp_parser_already_scoped_statement (parser);
9057         parser->in_statement = in_statement;
9058
9059         /* We're done with the for-statement.  */
9060         finish_for_stmt (statement);
9061       }
9062       break;
9063
9064     default:
9065       cp_parser_error (parser, "expected iteration-statement");
9066       statement = error_mark_node;
9067       break;
9068     }
9069
9070   return statement;
9071 }
9072
9073 /* Parse a for-init-statement or the declarator of a range-based-for.
9074    Returns true if a range-based-for declaration is seen.
9075
9076    for-init-statement:
9077      expression-statement
9078      simple-declaration  */
9079
9080 static bool
9081 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9082 {
9083   /* If the next token is a `;', then we have an empty
9084      expression-statement.  Grammatically, this is also a
9085      simple-declaration, but an invalid one, because it does not
9086      declare anything.  Therefore, if we did not handle this case
9087      specially, we would issue an error message about an invalid
9088      declaration.  */
9089   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9090     {
9091       bool is_range_for = false;
9092       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9093
9094       parser->colon_corrects_to_scope_p = false;
9095
9096       /* We're going to speculatively look for a declaration, falling back
9097          to an expression, if necessary.  */
9098       cp_parser_parse_tentatively (parser);
9099       /* Parse the declaration.  */
9100       cp_parser_simple_declaration (parser,
9101                                     /*function_definition_allowed_p=*/false,
9102                                     decl);
9103       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9104       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9105         {
9106           /* It is a range-for, consume the ':' */
9107           cp_lexer_consume_token (parser->lexer);
9108           is_range_for = true;
9109           if (cxx_dialect < cxx0x)
9110             {
9111               error_at (cp_lexer_peek_token (parser->lexer)->location,
9112                         "range-based %<for%> loops are not allowed "
9113                         "in C++98 mode");
9114               *decl = error_mark_node;
9115             }
9116         }
9117       else
9118           /* The ';' is not consumed yet because we told
9119              cp_parser_simple_declaration not to.  */
9120           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9121
9122       if (cp_parser_parse_definitely (parser))
9123         return is_range_for;
9124       /* If the tentative parse failed, then we shall need to look for an
9125          expression-statement.  */
9126     }
9127   /* If we are here, it is an expression-statement.  */
9128   cp_parser_expression_statement (parser, NULL_TREE);
9129   return false;
9130 }
9131
9132 /* Parse a jump-statement.
9133
9134    jump-statement:
9135      break ;
9136      continue ;
9137      return expression [opt] ;
9138      return braced-init-list ;
9139      goto identifier ;
9140
9141    GNU extension:
9142
9143    jump-statement:
9144      goto * expression ;
9145
9146    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9147
9148 static tree
9149 cp_parser_jump_statement (cp_parser* parser)
9150 {
9151   tree statement = error_mark_node;
9152   cp_token *token;
9153   enum rid keyword;
9154   unsigned char in_statement;
9155
9156   /* Peek at the next token.  */
9157   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9158   if (!token)
9159     return error_mark_node;
9160
9161   /* See what kind of keyword it is.  */
9162   keyword = token->keyword;
9163   switch (keyword)
9164     {
9165     case RID_BREAK:
9166       in_statement = parser->in_statement & ~IN_IF_STMT;      
9167       switch (in_statement)
9168         {
9169         case 0:
9170           error_at (token->location, "break statement not within loop or switch");
9171           break;
9172         default:
9173           gcc_assert ((in_statement & IN_SWITCH_STMT)
9174                       || in_statement == IN_ITERATION_STMT);
9175           statement = finish_break_stmt ();
9176           break;
9177         case IN_OMP_BLOCK:
9178           error_at (token->location, "invalid exit from OpenMP structured block");
9179           break;
9180         case IN_OMP_FOR:
9181           error_at (token->location, "break statement used with OpenMP for loop");
9182           break;
9183         }
9184       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9185       break;
9186
9187     case RID_CONTINUE:
9188       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9189         {
9190         case 0:
9191           error_at (token->location, "continue statement not within a loop");
9192           break;
9193         case IN_ITERATION_STMT:
9194         case IN_OMP_FOR:
9195           statement = finish_continue_stmt ();
9196           break;
9197         case IN_OMP_BLOCK:
9198           error_at (token->location, "invalid exit from OpenMP structured block");
9199           break;
9200         default:
9201           gcc_unreachable ();
9202         }
9203       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9204       break;
9205
9206     case RID_RETURN:
9207       {
9208         tree expr;
9209         bool expr_non_constant_p;
9210
9211         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9212           {
9213             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9214             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9215           }
9216         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9217           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9218         else
9219           /* If the next token is a `;', then there is no
9220              expression.  */
9221           expr = NULL_TREE;
9222         /* Build the return-statement.  */
9223         statement = finish_return_stmt (expr);
9224         /* Look for the final `;'.  */
9225         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9226       }
9227       break;
9228
9229     case RID_GOTO:
9230       /* Create the goto-statement.  */
9231       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9232         {
9233           /* Issue a warning about this use of a GNU extension.  */
9234           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9235           /* Consume the '*' token.  */
9236           cp_lexer_consume_token (parser->lexer);
9237           /* Parse the dependent expression.  */
9238           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9239         }
9240       else
9241         finish_goto_stmt (cp_parser_identifier (parser));
9242       /* Look for the final `;'.  */
9243       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9244       break;
9245
9246     default:
9247       cp_parser_error (parser, "expected jump-statement");
9248       break;
9249     }
9250
9251   return statement;
9252 }
9253
9254 /* Parse a declaration-statement.
9255
9256    declaration-statement:
9257      block-declaration  */
9258
9259 static void
9260 cp_parser_declaration_statement (cp_parser* parser)
9261 {
9262   void *p;
9263
9264   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9265   p = obstack_alloc (&declarator_obstack, 0);
9266
9267  /* Parse the block-declaration.  */
9268   cp_parser_block_declaration (parser, /*statement_p=*/true);
9269
9270   /* Free any declarators allocated.  */
9271   obstack_free (&declarator_obstack, p);
9272
9273   /* Finish off the statement.  */
9274   finish_stmt ();
9275 }
9276
9277 /* Some dependent statements (like `if (cond) statement'), are
9278    implicitly in their own scope.  In other words, if the statement is
9279    a single statement (as opposed to a compound-statement), it is
9280    none-the-less treated as if it were enclosed in braces.  Any
9281    declarations appearing in the dependent statement are out of scope
9282    after control passes that point.  This function parses a statement,
9283    but ensures that is in its own scope, even if it is not a
9284    compound-statement.
9285
9286    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9287    is a (possibly labeled) if statement which is not enclosed in
9288    braces and has an else clause.  This is used to implement
9289    -Wparentheses.
9290
9291    Returns the new statement.  */
9292
9293 static tree
9294 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9295 {
9296   tree statement;
9297
9298   if (if_p != NULL)
9299     *if_p = false;
9300
9301   /* Mark if () ; with a special NOP_EXPR.  */
9302   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9303     {
9304       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9305       cp_lexer_consume_token (parser->lexer);
9306       statement = add_stmt (build_empty_stmt (loc));
9307     }
9308   /* if a compound is opened, we simply parse the statement directly.  */
9309   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9310     statement = cp_parser_compound_statement (parser, NULL, false, false);
9311   /* If the token is not a `{', then we must take special action.  */
9312   else
9313     {
9314       /* Create a compound-statement.  */
9315       statement = begin_compound_stmt (0);
9316       /* Parse the dependent-statement.  */
9317       cp_parser_statement (parser, NULL_TREE, false, if_p);
9318       /* Finish the dummy compound-statement.  */
9319       finish_compound_stmt (statement);
9320     }
9321
9322   /* Return the statement.  */
9323   return statement;
9324 }
9325
9326 /* For some dependent statements (like `while (cond) statement'), we
9327    have already created a scope.  Therefore, even if the dependent
9328    statement is a compound-statement, we do not want to create another
9329    scope.  */
9330
9331 static void
9332 cp_parser_already_scoped_statement (cp_parser* parser)
9333 {
9334   /* If the token is a `{', then we must take special action.  */
9335   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9336     cp_parser_statement (parser, NULL_TREE, false, NULL);
9337   else
9338     {
9339       /* Avoid calling cp_parser_compound_statement, so that we
9340          don't create a new scope.  Do everything else by hand.  */
9341       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9342       /* If the next keyword is `__label__' we have a label declaration.  */
9343       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9344         cp_parser_label_declaration (parser);
9345       /* Parse an (optional) statement-seq.  */
9346       cp_parser_statement_seq_opt (parser, NULL_TREE);
9347       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9348     }
9349 }
9350
9351 /* Declarations [gram.dcl.dcl] */
9352
9353 /* Parse an optional declaration-sequence.
9354
9355    declaration-seq:
9356      declaration
9357      declaration-seq declaration  */
9358
9359 static void
9360 cp_parser_declaration_seq_opt (cp_parser* parser)
9361 {
9362   while (true)
9363     {
9364       cp_token *token;
9365
9366       token = cp_lexer_peek_token (parser->lexer);
9367
9368       if (token->type == CPP_CLOSE_BRACE
9369           || token->type == CPP_EOF
9370           || token->type == CPP_PRAGMA_EOL)
9371         break;
9372
9373       if (token->type == CPP_SEMICOLON)
9374         {
9375           /* A declaration consisting of a single semicolon is
9376              invalid.  Allow it unless we're being pedantic.  */
9377           cp_lexer_consume_token (parser->lexer);
9378           if (!in_system_header)
9379             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9380           continue;
9381         }
9382
9383       /* If we're entering or exiting a region that's implicitly
9384          extern "C", modify the lang context appropriately.  */
9385       if (!parser->implicit_extern_c && token->implicit_extern_c)
9386         {
9387           push_lang_context (lang_name_c);
9388           parser->implicit_extern_c = true;
9389         }
9390       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9391         {
9392           pop_lang_context ();
9393           parser->implicit_extern_c = false;
9394         }
9395
9396       if (token->type == CPP_PRAGMA)
9397         {
9398           /* A top-level declaration can consist solely of a #pragma.
9399              A nested declaration cannot, so this is done here and not
9400              in cp_parser_declaration.  (A #pragma at block scope is
9401              handled in cp_parser_statement.)  */
9402           cp_parser_pragma (parser, pragma_external);
9403           continue;
9404         }
9405
9406       /* Parse the declaration itself.  */
9407       cp_parser_declaration (parser);
9408     }
9409 }
9410
9411 /* Parse a declaration.
9412
9413    declaration:
9414      block-declaration
9415      function-definition
9416      template-declaration
9417      explicit-instantiation
9418      explicit-specialization
9419      linkage-specification
9420      namespace-definition
9421
9422    GNU extension:
9423
9424    declaration:
9425       __extension__ declaration */
9426
9427 static void
9428 cp_parser_declaration (cp_parser* parser)
9429 {
9430   cp_token token1;
9431   cp_token token2;
9432   int saved_pedantic;
9433   void *p;
9434   tree attributes = NULL_TREE;
9435
9436   /* Check for the `__extension__' keyword.  */
9437   if (cp_parser_extension_opt (parser, &saved_pedantic))
9438     {
9439       /* Parse the qualified declaration.  */
9440       cp_parser_declaration (parser);
9441       /* Restore the PEDANTIC flag.  */
9442       pedantic = saved_pedantic;
9443
9444       return;
9445     }
9446
9447   /* Try to figure out what kind of declaration is present.  */
9448   token1 = *cp_lexer_peek_token (parser->lexer);
9449
9450   if (token1.type != CPP_EOF)
9451     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9452   else
9453     {
9454       token2.type = CPP_EOF;
9455       token2.keyword = RID_MAX;
9456     }
9457
9458   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9459   p = obstack_alloc (&declarator_obstack, 0);
9460
9461   /* If the next token is `extern' and the following token is a string
9462      literal, then we have a linkage specification.  */
9463   if (token1.keyword == RID_EXTERN
9464       && cp_parser_is_string_literal (&token2))
9465     cp_parser_linkage_specification (parser);
9466   /* If the next token is `template', then we have either a template
9467      declaration, an explicit instantiation, or an explicit
9468      specialization.  */
9469   else if (token1.keyword == RID_TEMPLATE)
9470     {
9471       /* `template <>' indicates a template specialization.  */
9472       if (token2.type == CPP_LESS
9473           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9474         cp_parser_explicit_specialization (parser);
9475       /* `template <' indicates a template declaration.  */
9476       else if (token2.type == CPP_LESS)
9477         cp_parser_template_declaration (parser, /*member_p=*/false);
9478       /* Anything else must be an explicit instantiation.  */
9479       else
9480         cp_parser_explicit_instantiation (parser);
9481     }
9482   /* If the next token is `export', then we have a template
9483      declaration.  */
9484   else if (token1.keyword == RID_EXPORT)
9485     cp_parser_template_declaration (parser, /*member_p=*/false);
9486   /* If the next token is `extern', 'static' or 'inline' and the one
9487      after that is `template', we have a GNU extended explicit
9488      instantiation directive.  */
9489   else if (cp_parser_allow_gnu_extensions_p (parser)
9490            && (token1.keyword == RID_EXTERN
9491                || token1.keyword == RID_STATIC
9492                || token1.keyword == RID_INLINE)
9493            && token2.keyword == RID_TEMPLATE)
9494     cp_parser_explicit_instantiation (parser);
9495   /* If the next token is `namespace', check for a named or unnamed
9496      namespace definition.  */
9497   else if (token1.keyword == RID_NAMESPACE
9498            && (/* A named namespace definition.  */
9499                (token2.type == CPP_NAME
9500                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9501                     != CPP_EQ))
9502                /* An unnamed namespace definition.  */
9503                || token2.type == CPP_OPEN_BRACE
9504                || token2.keyword == RID_ATTRIBUTE))
9505     cp_parser_namespace_definition (parser);
9506   /* An inline (associated) namespace definition.  */
9507   else if (token1.keyword == RID_INLINE
9508            && token2.keyword == RID_NAMESPACE)
9509     cp_parser_namespace_definition (parser);
9510   /* Objective-C++ declaration/definition.  */
9511   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9512     cp_parser_objc_declaration (parser, NULL_TREE);
9513   else if (c_dialect_objc ()
9514            && token1.keyword == RID_ATTRIBUTE
9515            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9516     cp_parser_objc_declaration (parser, attributes);
9517   /* We must have either a block declaration or a function
9518      definition.  */
9519   else
9520     /* Try to parse a block-declaration, or a function-definition.  */
9521     cp_parser_block_declaration (parser, /*statement_p=*/false);
9522
9523   /* Free any declarators allocated.  */
9524   obstack_free (&declarator_obstack, p);
9525 }
9526
9527 /* Parse a block-declaration.
9528
9529    block-declaration:
9530      simple-declaration
9531      asm-definition
9532      namespace-alias-definition
9533      using-declaration
9534      using-directive
9535
9536    GNU Extension:
9537
9538    block-declaration:
9539      __extension__ block-declaration
9540
9541    C++0x Extension:
9542
9543    block-declaration:
9544      static_assert-declaration
9545
9546    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9547    part of a declaration-statement.  */
9548
9549 static void
9550 cp_parser_block_declaration (cp_parser *parser,
9551                              bool      statement_p)
9552 {
9553   cp_token *token1;
9554   int saved_pedantic;
9555
9556   /* Check for the `__extension__' keyword.  */
9557   if (cp_parser_extension_opt (parser, &saved_pedantic))
9558     {
9559       /* Parse the qualified declaration.  */
9560       cp_parser_block_declaration (parser, statement_p);
9561       /* Restore the PEDANTIC flag.  */
9562       pedantic = saved_pedantic;
9563
9564       return;
9565     }
9566
9567   /* Peek at the next token to figure out which kind of declaration is
9568      present.  */
9569   token1 = cp_lexer_peek_token (parser->lexer);
9570
9571   /* If the next keyword is `asm', we have an asm-definition.  */
9572   if (token1->keyword == RID_ASM)
9573     {
9574       if (statement_p)
9575         cp_parser_commit_to_tentative_parse (parser);
9576       cp_parser_asm_definition (parser);
9577     }
9578   /* If the next keyword is `namespace', we have a
9579      namespace-alias-definition.  */
9580   else if (token1->keyword == RID_NAMESPACE)
9581     cp_parser_namespace_alias_definition (parser);
9582   /* If the next keyword is `using', we have either a
9583      using-declaration or a using-directive.  */
9584   else if (token1->keyword == RID_USING)
9585     {
9586       cp_token *token2;
9587
9588       if (statement_p)
9589         cp_parser_commit_to_tentative_parse (parser);
9590       /* If the token after `using' is `namespace', then we have a
9591          using-directive.  */
9592       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9593       if (token2->keyword == RID_NAMESPACE)
9594         cp_parser_using_directive (parser);
9595       /* Otherwise, it's a using-declaration.  */
9596       else
9597         cp_parser_using_declaration (parser,
9598                                      /*access_declaration_p=*/false);
9599     }
9600   /* If the next keyword is `__label__' we have a misplaced label
9601      declaration.  */
9602   else if (token1->keyword == RID_LABEL)
9603     {
9604       cp_lexer_consume_token (parser->lexer);
9605       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9606       cp_parser_skip_to_end_of_statement (parser);
9607       /* If the next token is now a `;', consume it.  */
9608       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9609         cp_lexer_consume_token (parser->lexer);
9610     }
9611   /* If the next token is `static_assert' we have a static assertion.  */
9612   else if (token1->keyword == RID_STATIC_ASSERT)
9613     cp_parser_static_assert (parser, /*member_p=*/false);
9614   /* Anything else must be a simple-declaration.  */
9615   else
9616     cp_parser_simple_declaration (parser, !statement_p,
9617                                   /*maybe_range_for_decl*/NULL);
9618 }
9619
9620 /* Parse a simple-declaration.
9621
9622    simple-declaration:
9623      decl-specifier-seq [opt] init-declarator-list [opt] ;
9624
9625    init-declarator-list:
9626      init-declarator
9627      init-declarator-list , init-declarator
9628
9629    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9630    function-definition as a simple-declaration.
9631
9632    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9633    parsed declaration if it is an uninitialized single declarator not followed
9634    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9635    if present, will not be consumed.  */
9636
9637 static void
9638 cp_parser_simple_declaration (cp_parser* parser,
9639                               bool function_definition_allowed_p,
9640                               tree *maybe_range_for_decl)
9641 {
9642   cp_decl_specifier_seq decl_specifiers;
9643   int declares_class_or_enum;
9644   bool saw_declarator;
9645
9646   if (maybe_range_for_decl)
9647     *maybe_range_for_decl = NULL_TREE;
9648
9649   /* Defer access checks until we know what is being declared; the
9650      checks for names appearing in the decl-specifier-seq should be
9651      done as if we were in the scope of the thing being declared.  */
9652   push_deferring_access_checks (dk_deferred);
9653
9654   /* Parse the decl-specifier-seq.  We have to keep track of whether
9655      or not the decl-specifier-seq declares a named class or
9656      enumeration type, since that is the only case in which the
9657      init-declarator-list is allowed to be empty.
9658
9659      [dcl.dcl]
9660
9661      In a simple-declaration, the optional init-declarator-list can be
9662      omitted only when declaring a class or enumeration, that is when
9663      the decl-specifier-seq contains either a class-specifier, an
9664      elaborated-type-specifier, or an enum-specifier.  */
9665   cp_parser_decl_specifier_seq (parser,
9666                                 CP_PARSER_FLAGS_OPTIONAL,
9667                                 &decl_specifiers,
9668                                 &declares_class_or_enum);
9669   /* We no longer need to defer access checks.  */
9670   stop_deferring_access_checks ();
9671
9672   /* In a block scope, a valid declaration must always have a
9673      decl-specifier-seq.  By not trying to parse declarators, we can
9674      resolve the declaration/expression ambiguity more quickly.  */
9675   if (!function_definition_allowed_p
9676       && !decl_specifiers.any_specifiers_p)
9677     {
9678       cp_parser_error (parser, "expected declaration");
9679       goto done;
9680     }
9681
9682   /* If the next two tokens are both identifiers, the code is
9683      erroneous. The usual cause of this situation is code like:
9684
9685        T t;
9686
9687      where "T" should name a type -- but does not.  */
9688   if (!decl_specifiers.any_type_specifiers_p
9689       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9690     {
9691       /* If parsing tentatively, we should commit; we really are
9692          looking at a declaration.  */
9693       cp_parser_commit_to_tentative_parse (parser);
9694       /* Give up.  */
9695       goto done;
9696     }
9697
9698   /* If we have seen at least one decl-specifier, and the next token
9699      is not a parenthesis, then we must be looking at a declaration.
9700      (After "int (" we might be looking at a functional cast.)  */
9701   if (decl_specifiers.any_specifiers_p
9702       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9703       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9704       && !cp_parser_error_occurred (parser))
9705     cp_parser_commit_to_tentative_parse (parser);
9706
9707   /* Keep going until we hit the `;' at the end of the simple
9708      declaration.  */
9709   saw_declarator = false;
9710   while (cp_lexer_next_token_is_not (parser->lexer,
9711                                      CPP_SEMICOLON))
9712     {
9713       cp_token *token;
9714       bool function_definition_p;
9715       tree decl;
9716
9717       if (saw_declarator)
9718         {
9719           /* If we are processing next declarator, coma is expected */
9720           token = cp_lexer_peek_token (parser->lexer);
9721           gcc_assert (token->type == CPP_COMMA);
9722           cp_lexer_consume_token (parser->lexer);
9723           if (maybe_range_for_decl)
9724             *maybe_range_for_decl = error_mark_node;
9725         }
9726       else
9727         saw_declarator = true;
9728
9729       /* Parse the init-declarator.  */
9730       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9731                                         /*checks=*/NULL,
9732                                         function_definition_allowed_p,
9733                                         /*member_p=*/false,
9734                                         declares_class_or_enum,
9735                                         &function_definition_p,
9736                                         maybe_range_for_decl);
9737       /* If an error occurred while parsing tentatively, exit quickly.
9738          (That usually happens when in the body of a function; each
9739          statement is treated as a declaration-statement until proven
9740          otherwise.)  */
9741       if (cp_parser_error_occurred (parser))
9742         goto done;
9743       /* Handle function definitions specially.  */
9744       if (function_definition_p)
9745         {
9746           /* If the next token is a `,', then we are probably
9747              processing something like:
9748
9749                void f() {}, *p;
9750
9751              which is erroneous.  */
9752           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9753             {
9754               cp_token *token = cp_lexer_peek_token (parser->lexer);
9755               error_at (token->location,
9756                         "mixing"
9757                         " declarations and function-definitions is forbidden");
9758             }
9759           /* Otherwise, we're done with the list of declarators.  */
9760           else
9761             {
9762               pop_deferring_access_checks ();
9763               return;
9764             }
9765         }
9766       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9767         *maybe_range_for_decl = decl;
9768       /* The next token should be either a `,' or a `;'.  */
9769       token = cp_lexer_peek_token (parser->lexer);
9770       /* If it's a `,', there are more declarators to come.  */
9771       if (token->type == CPP_COMMA)
9772         /* will be consumed next time around */;
9773       /* If it's a `;', we are done.  */
9774       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9775         break;
9776       /* Anything else is an error.  */
9777       else
9778         {
9779           /* If we have already issued an error message we don't need
9780              to issue another one.  */
9781           if (decl != error_mark_node
9782               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9783             cp_parser_error (parser, "expected %<,%> or %<;%>");
9784           /* Skip tokens until we reach the end of the statement.  */
9785           cp_parser_skip_to_end_of_statement (parser);
9786           /* If the next token is now a `;', consume it.  */
9787           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9788             cp_lexer_consume_token (parser->lexer);
9789           goto done;
9790         }
9791       /* After the first time around, a function-definition is not
9792          allowed -- even if it was OK at first.  For example:
9793
9794            int i, f() {}
9795
9796          is not valid.  */
9797       function_definition_allowed_p = false;
9798     }
9799
9800   /* Issue an error message if no declarators are present, and the
9801      decl-specifier-seq does not itself declare a class or
9802      enumeration.  */
9803   if (!saw_declarator)
9804     {
9805       if (cp_parser_declares_only_class_p (parser))
9806         shadow_tag (&decl_specifiers);
9807       /* Perform any deferred access checks.  */
9808       perform_deferred_access_checks ();
9809     }
9810
9811   /* Consume the `;'.  */
9812   if (!maybe_range_for_decl)
9813       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9814
9815  done:
9816   pop_deferring_access_checks ();
9817 }
9818
9819 /* Parse a decl-specifier-seq.
9820
9821    decl-specifier-seq:
9822      decl-specifier-seq [opt] decl-specifier
9823
9824    decl-specifier:
9825      storage-class-specifier
9826      type-specifier
9827      function-specifier
9828      friend
9829      typedef
9830
9831    GNU Extension:
9832
9833    decl-specifier:
9834      attributes
9835
9836    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9837
9838    The parser flags FLAGS is used to control type-specifier parsing.
9839
9840    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9841    flags:
9842
9843      1: one of the decl-specifiers is an elaborated-type-specifier
9844         (i.e., a type declaration)
9845      2: one of the decl-specifiers is an enum-specifier or a
9846         class-specifier (i.e., a type definition)
9847
9848    */
9849
9850 static void
9851 cp_parser_decl_specifier_seq (cp_parser* parser,
9852                               cp_parser_flags flags,
9853                               cp_decl_specifier_seq *decl_specs,
9854                               int* declares_class_or_enum)
9855 {
9856   bool constructor_possible_p = !parser->in_declarator_p;
9857   cp_token *start_token = NULL;
9858
9859   /* Clear DECL_SPECS.  */
9860   clear_decl_specs (decl_specs);
9861
9862   /* Assume no class or enumeration type is declared.  */
9863   *declares_class_or_enum = 0;
9864
9865   /* Keep reading specifiers until there are no more to read.  */
9866   while (true)
9867     {
9868       bool constructor_p;
9869       bool found_decl_spec;
9870       cp_token *token;
9871
9872       /* Peek at the next token.  */
9873       token = cp_lexer_peek_token (parser->lexer);
9874
9875       /* Save the first token of the decl spec list for error
9876          reporting.  */
9877       if (!start_token)
9878         start_token = token;
9879       /* Handle attributes.  */
9880       if (token->keyword == RID_ATTRIBUTE)
9881         {
9882           /* Parse the attributes.  */
9883           decl_specs->attributes
9884             = chainon (decl_specs->attributes,
9885                        cp_parser_attributes_opt (parser));
9886           continue;
9887         }
9888       /* Assume we will find a decl-specifier keyword.  */
9889       found_decl_spec = true;
9890       /* If the next token is an appropriate keyword, we can simply
9891          add it to the list.  */
9892       switch (token->keyword)
9893         {
9894           /* decl-specifier:
9895                friend
9896                constexpr */
9897         case RID_FRIEND:
9898           if (!at_class_scope_p ())
9899             {
9900               error_at (token->location, "%<friend%> used outside of class");
9901               cp_lexer_purge_token (parser->lexer);
9902             }
9903           else
9904             {
9905               ++decl_specs->specs[(int) ds_friend];
9906               /* Consume the token.  */
9907               cp_lexer_consume_token (parser->lexer);
9908             }
9909           break;
9910
9911         case RID_CONSTEXPR:
9912           ++decl_specs->specs[(int) ds_constexpr];
9913           cp_lexer_consume_token (parser->lexer);
9914           break;
9915
9916           /* function-specifier:
9917                inline
9918                virtual
9919                explicit  */
9920         case RID_INLINE:
9921         case RID_VIRTUAL:
9922         case RID_EXPLICIT:
9923           cp_parser_function_specifier_opt (parser, decl_specs);
9924           break;
9925
9926           /* decl-specifier:
9927                typedef  */
9928         case RID_TYPEDEF:
9929           ++decl_specs->specs[(int) ds_typedef];
9930           /* Consume the token.  */
9931           cp_lexer_consume_token (parser->lexer);
9932           /* A constructor declarator cannot appear in a typedef.  */
9933           constructor_possible_p = false;
9934           /* The "typedef" keyword can only occur in a declaration; we
9935              may as well commit at this point.  */
9936           cp_parser_commit_to_tentative_parse (parser);
9937
9938           if (decl_specs->storage_class != sc_none)
9939             decl_specs->conflicting_specifiers_p = true;
9940           break;
9941
9942           /* storage-class-specifier:
9943                auto
9944                register
9945                static
9946                extern
9947                mutable
9948
9949              GNU Extension:
9950                thread  */
9951         case RID_AUTO:
9952           if (cxx_dialect == cxx98) 
9953             {
9954               /* Consume the token.  */
9955               cp_lexer_consume_token (parser->lexer);
9956
9957               /* Complain about `auto' as a storage specifier, if
9958                  we're complaining about C++0x compatibility.  */
9959               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9960                           " will change meaning in C++0x; please remove it");
9961
9962               /* Set the storage class anyway.  */
9963               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9964                                            token->location);
9965             }
9966           else
9967             /* C++0x auto type-specifier.  */
9968             found_decl_spec = false;
9969           break;
9970
9971         case RID_REGISTER:
9972         case RID_STATIC:
9973         case RID_EXTERN:
9974         case RID_MUTABLE:
9975           /* Consume the token.  */
9976           cp_lexer_consume_token (parser->lexer);
9977           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9978                                        token->location);
9979           break;
9980         case RID_THREAD:
9981           /* Consume the token.  */
9982           cp_lexer_consume_token (parser->lexer);
9983           ++decl_specs->specs[(int) ds_thread];
9984           break;
9985
9986         default:
9987           /* We did not yet find a decl-specifier yet.  */
9988           found_decl_spec = false;
9989           break;
9990         }
9991
9992       if (found_decl_spec
9993           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9994           && token->keyword != RID_CONSTEXPR)
9995         error ("decl-specifier invalid in condition");
9996
9997       /* Constructors are a special case.  The `S' in `S()' is not a
9998          decl-specifier; it is the beginning of the declarator.  */
9999       constructor_p
10000         = (!found_decl_spec
10001            && constructor_possible_p
10002            && (cp_parser_constructor_declarator_p
10003                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10004
10005       /* If we don't have a DECL_SPEC yet, then we must be looking at
10006          a type-specifier.  */
10007       if (!found_decl_spec && !constructor_p)
10008         {
10009           int decl_spec_declares_class_or_enum;
10010           bool is_cv_qualifier;
10011           tree type_spec;
10012
10013           type_spec
10014             = cp_parser_type_specifier (parser, flags,
10015                                         decl_specs,
10016                                         /*is_declaration=*/true,
10017                                         &decl_spec_declares_class_or_enum,
10018                                         &is_cv_qualifier);
10019           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10020
10021           /* If this type-specifier referenced a user-defined type
10022              (a typedef, class-name, etc.), then we can't allow any
10023              more such type-specifiers henceforth.
10024
10025              [dcl.spec]
10026
10027              The longest sequence of decl-specifiers that could
10028              possibly be a type name is taken as the
10029              decl-specifier-seq of a declaration.  The sequence shall
10030              be self-consistent as described below.
10031
10032              [dcl.type]
10033
10034              As a general rule, at most one type-specifier is allowed
10035              in the complete decl-specifier-seq of a declaration.  The
10036              only exceptions are the following:
10037
10038              -- const or volatile can be combined with any other
10039                 type-specifier.
10040
10041              -- signed or unsigned can be combined with char, long,
10042                 short, or int.
10043
10044              -- ..
10045
10046              Example:
10047
10048                typedef char* Pc;
10049                void g (const int Pc);
10050
10051              Here, Pc is *not* part of the decl-specifier seq; it's
10052              the declarator.  Therefore, once we see a type-specifier
10053              (other than a cv-qualifier), we forbid any additional
10054              user-defined types.  We *do* still allow things like `int
10055              int' to be considered a decl-specifier-seq, and issue the
10056              error message later.  */
10057           if (type_spec && !is_cv_qualifier)
10058             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10059           /* A constructor declarator cannot follow a type-specifier.  */
10060           if (type_spec)
10061             {
10062               constructor_possible_p = false;
10063               found_decl_spec = true;
10064               if (!is_cv_qualifier)
10065                 decl_specs->any_type_specifiers_p = true;
10066             }
10067         }
10068
10069       /* If we still do not have a DECL_SPEC, then there are no more
10070          decl-specifiers.  */
10071       if (!found_decl_spec)
10072         break;
10073
10074       decl_specs->any_specifiers_p = true;
10075       /* After we see one decl-specifier, further decl-specifiers are
10076          always optional.  */
10077       flags |= CP_PARSER_FLAGS_OPTIONAL;
10078     }
10079
10080   cp_parser_check_decl_spec (decl_specs, start_token->location);
10081
10082   /* Don't allow a friend specifier with a class definition.  */
10083   if (decl_specs->specs[(int) ds_friend] != 0
10084       && (*declares_class_or_enum & 2))
10085     error_at (start_token->location,
10086               "class definition may not be declared a friend");
10087 }
10088
10089 /* Parse an (optional) storage-class-specifier.
10090
10091    storage-class-specifier:
10092      auto
10093      register
10094      static
10095      extern
10096      mutable
10097
10098    GNU Extension:
10099
10100    storage-class-specifier:
10101      thread
10102
10103    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10104
10105 static tree
10106 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10107 {
10108   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10109     {
10110     case RID_AUTO:
10111       if (cxx_dialect != cxx98)
10112         return NULL_TREE;
10113       /* Fall through for C++98.  */
10114
10115     case RID_REGISTER:
10116     case RID_STATIC:
10117     case RID_EXTERN:
10118     case RID_MUTABLE:
10119     case RID_THREAD:
10120       /* Consume the token.  */
10121       return cp_lexer_consume_token (parser->lexer)->u.value;
10122
10123     default:
10124       return NULL_TREE;
10125     }
10126 }
10127
10128 /* Parse an (optional) function-specifier.
10129
10130    function-specifier:
10131      inline
10132      virtual
10133      explicit
10134
10135    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10136    Updates DECL_SPECS, if it is non-NULL.  */
10137
10138 static tree
10139 cp_parser_function_specifier_opt (cp_parser* parser,
10140                                   cp_decl_specifier_seq *decl_specs)
10141 {
10142   cp_token *token = cp_lexer_peek_token (parser->lexer);
10143   switch (token->keyword)
10144     {
10145     case RID_INLINE:
10146       if (decl_specs)
10147         ++decl_specs->specs[(int) ds_inline];
10148       break;
10149
10150     case RID_VIRTUAL:
10151       /* 14.5.2.3 [temp.mem]
10152
10153          A member function template shall not be virtual.  */
10154       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10155         error_at (token->location, "templates may not be %<virtual%>");
10156       else if (decl_specs)
10157         ++decl_specs->specs[(int) ds_virtual];
10158       break;
10159
10160     case RID_EXPLICIT:
10161       if (decl_specs)
10162         ++decl_specs->specs[(int) ds_explicit];
10163       break;
10164
10165     default:
10166       return NULL_TREE;
10167     }
10168
10169   /* Consume the token.  */
10170   return cp_lexer_consume_token (parser->lexer)->u.value;
10171 }
10172
10173 /* Parse a linkage-specification.
10174
10175    linkage-specification:
10176      extern string-literal { declaration-seq [opt] }
10177      extern string-literal declaration  */
10178
10179 static void
10180 cp_parser_linkage_specification (cp_parser* parser)
10181 {
10182   tree linkage;
10183
10184   /* Look for the `extern' keyword.  */
10185   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10186
10187   /* Look for the string-literal.  */
10188   linkage = cp_parser_string_literal (parser, false, false);
10189
10190   /* Transform the literal into an identifier.  If the literal is a
10191      wide-character string, or contains embedded NULs, then we can't
10192      handle it as the user wants.  */
10193   if (strlen (TREE_STRING_POINTER (linkage))
10194       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10195     {
10196       cp_parser_error (parser, "invalid linkage-specification");
10197       /* Assume C++ linkage.  */
10198       linkage = lang_name_cplusplus;
10199     }
10200   else
10201     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10202
10203   /* We're now using the new linkage.  */
10204   push_lang_context (linkage);
10205
10206   /* If the next token is a `{', then we're using the first
10207      production.  */
10208   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10209     {
10210       /* Consume the `{' token.  */
10211       cp_lexer_consume_token (parser->lexer);
10212       /* Parse the declarations.  */
10213       cp_parser_declaration_seq_opt (parser);
10214       /* Look for the closing `}'.  */
10215       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10216     }
10217   /* Otherwise, there's just one declaration.  */
10218   else
10219     {
10220       bool saved_in_unbraced_linkage_specification_p;
10221
10222       saved_in_unbraced_linkage_specification_p
10223         = parser->in_unbraced_linkage_specification_p;
10224       parser->in_unbraced_linkage_specification_p = true;
10225       cp_parser_declaration (parser);
10226       parser->in_unbraced_linkage_specification_p
10227         = saved_in_unbraced_linkage_specification_p;
10228     }
10229
10230   /* We're done with the linkage-specification.  */
10231   pop_lang_context ();
10232 }
10233
10234 /* Parse a static_assert-declaration.
10235
10236    static_assert-declaration:
10237      static_assert ( constant-expression , string-literal ) ; 
10238
10239    If MEMBER_P, this static_assert is a class member.  */
10240
10241 static void 
10242 cp_parser_static_assert(cp_parser *parser, bool member_p)
10243 {
10244   tree condition;
10245   tree message;
10246   cp_token *token;
10247   location_t saved_loc;
10248   bool dummy;
10249
10250   /* Peek at the `static_assert' token so we can keep track of exactly
10251      where the static assertion started.  */
10252   token = cp_lexer_peek_token (parser->lexer);
10253   saved_loc = token->location;
10254
10255   /* Look for the `static_assert' keyword.  */
10256   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10257                                   RT_STATIC_ASSERT))
10258     return;
10259
10260   /*  We know we are in a static assertion; commit to any tentative
10261       parse.  */
10262   if (cp_parser_parsing_tentatively (parser))
10263     cp_parser_commit_to_tentative_parse (parser);
10264
10265   /* Parse the `(' starting the static assertion condition.  */
10266   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10267
10268   /* Parse the constant-expression.  Allow a non-constant expression
10269      here in order to give better diagnostics in finish_static_assert.  */
10270   condition = 
10271     cp_parser_constant_expression (parser,
10272                                    /*allow_non_constant_p=*/true,
10273                                    /*non_constant_p=*/&dummy);
10274
10275   /* Parse the separating `,'.  */
10276   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10277
10278   /* Parse the string-literal message.  */
10279   message = cp_parser_string_literal (parser, 
10280                                       /*translate=*/false,
10281                                       /*wide_ok=*/true);
10282
10283   /* A `)' completes the static assertion.  */
10284   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10285     cp_parser_skip_to_closing_parenthesis (parser, 
10286                                            /*recovering=*/true, 
10287                                            /*or_comma=*/false,
10288                                            /*consume_paren=*/true);
10289
10290   /* A semicolon terminates the declaration.  */
10291   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10292
10293   /* Complete the static assertion, which may mean either processing 
10294      the static assert now or saving it for template instantiation.  */
10295   finish_static_assert (condition, message, saved_loc, member_p);
10296 }
10297
10298 /* Parse a `decltype' type. Returns the type. 
10299
10300    simple-type-specifier:
10301      decltype ( expression )  */
10302
10303 static tree
10304 cp_parser_decltype (cp_parser *parser)
10305 {
10306   tree expr;
10307   bool id_expression_or_member_access_p = false;
10308   const char *saved_message;
10309   bool saved_integral_constant_expression_p;
10310   bool saved_non_integral_constant_expression_p;
10311   cp_token *id_expr_start_token;
10312   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10313
10314   if (start_token->type == CPP_DECLTYPE)
10315     {
10316       /* Already parsed.  */
10317       cp_lexer_consume_token (parser->lexer);
10318       return start_token->u.value;
10319     }
10320
10321   /* Look for the `decltype' token.  */
10322   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10323     return error_mark_node;
10324
10325   /* Types cannot be defined in a `decltype' expression.  Save away the
10326      old message.  */
10327   saved_message = parser->type_definition_forbidden_message;
10328
10329   /* And create the new one.  */
10330   parser->type_definition_forbidden_message
10331     = G_("types may not be defined in %<decltype%> expressions");
10332
10333   /* The restrictions on constant-expressions do not apply inside
10334      decltype expressions.  */
10335   saved_integral_constant_expression_p
10336     = parser->integral_constant_expression_p;
10337   saved_non_integral_constant_expression_p
10338     = parser->non_integral_constant_expression_p;
10339   parser->integral_constant_expression_p = false;
10340
10341   /* Do not actually evaluate the expression.  */
10342   ++cp_unevaluated_operand;
10343
10344   /* Do not warn about problems with the expression.  */
10345   ++c_inhibit_evaluation_warnings;
10346
10347   /* Parse the opening `('.  */
10348   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10349     return error_mark_node;
10350   
10351   /* First, try parsing an id-expression.  */
10352   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10353   cp_parser_parse_tentatively (parser);
10354   expr = cp_parser_id_expression (parser,
10355                                   /*template_keyword_p=*/false,
10356                                   /*check_dependency_p=*/true,
10357                                   /*template_p=*/NULL,
10358                                   /*declarator_p=*/false,
10359                                   /*optional_p=*/false);
10360
10361   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10362     {
10363       bool non_integral_constant_expression_p = false;
10364       tree id_expression = expr;
10365       cp_id_kind idk;
10366       const char *error_msg;
10367
10368       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10369         /* Lookup the name we got back from the id-expression.  */
10370         expr = cp_parser_lookup_name (parser, expr,
10371                                       none_type,
10372                                       /*is_template=*/false,
10373                                       /*is_namespace=*/false,
10374                                       /*check_dependency=*/true,
10375                                       /*ambiguous_decls=*/NULL,
10376                                       id_expr_start_token->location);
10377
10378       if (expr
10379           && expr != error_mark_node
10380           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10381           && TREE_CODE (expr) != TYPE_DECL
10382           && (TREE_CODE (expr) != BIT_NOT_EXPR
10383               || !TYPE_P (TREE_OPERAND (expr, 0)))
10384           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10385         {
10386           /* Complete lookup of the id-expression.  */
10387           expr = (finish_id_expression
10388                   (id_expression, expr, parser->scope, &idk,
10389                    /*integral_constant_expression_p=*/false,
10390                    /*allow_non_integral_constant_expression_p=*/true,
10391                    &non_integral_constant_expression_p,
10392                    /*template_p=*/false,
10393                    /*done=*/true,
10394                    /*address_p=*/false,
10395                    /*template_arg_p=*/false,
10396                    &error_msg,
10397                    id_expr_start_token->location));
10398
10399           if (expr == error_mark_node)
10400             /* We found an id-expression, but it was something that we
10401                should not have found. This is an error, not something
10402                we can recover from, so note that we found an
10403                id-expression and we'll recover as gracefully as
10404                possible.  */
10405             id_expression_or_member_access_p = true;
10406         }
10407
10408       if (expr 
10409           && expr != error_mark_node
10410           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10411         /* We have an id-expression.  */
10412         id_expression_or_member_access_p = true;
10413     }
10414
10415   if (!id_expression_or_member_access_p)
10416     {
10417       /* Abort the id-expression parse.  */
10418       cp_parser_abort_tentative_parse (parser);
10419
10420       /* Parsing tentatively, again.  */
10421       cp_parser_parse_tentatively (parser);
10422
10423       /* Parse a class member access.  */
10424       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10425                                            /*cast_p=*/false,
10426                                            /*member_access_only_p=*/true, NULL);
10427
10428       if (expr 
10429           && expr != error_mark_node
10430           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10431         /* We have an id-expression.  */
10432         id_expression_or_member_access_p = true;
10433     }
10434
10435   if (id_expression_or_member_access_p)
10436     /* We have parsed the complete id-expression or member access.  */
10437     cp_parser_parse_definitely (parser);
10438   else
10439     {
10440       bool saved_greater_than_is_operator_p;
10441
10442       /* Abort our attempt to parse an id-expression or member access
10443          expression.  */
10444       cp_parser_abort_tentative_parse (parser);
10445
10446       /* Within a parenthesized expression, a `>' token is always
10447          the greater-than operator.  */
10448       saved_greater_than_is_operator_p
10449         = parser->greater_than_is_operator_p;
10450       parser->greater_than_is_operator_p = true;
10451
10452       /* Parse a full expression.  */
10453       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10454
10455       /* The `>' token might be the end of a template-id or
10456          template-parameter-list now.  */
10457       parser->greater_than_is_operator_p
10458         = saved_greater_than_is_operator_p;
10459     }
10460
10461   /* Go back to evaluating expressions.  */
10462   --cp_unevaluated_operand;
10463   --c_inhibit_evaluation_warnings;
10464
10465   /* Restore the old message and the integral constant expression
10466      flags.  */
10467   parser->type_definition_forbidden_message = saved_message;
10468   parser->integral_constant_expression_p
10469     = saved_integral_constant_expression_p;
10470   parser->non_integral_constant_expression_p
10471     = saved_non_integral_constant_expression_p;
10472
10473   /* Parse to the closing `)'.  */
10474   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10475     {
10476       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10477                                              /*consume_paren=*/true);
10478       return error_mark_node;
10479     }
10480
10481   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
10482                                tf_warning_or_error);
10483
10484   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
10485      it again.  */
10486   start_token->type = CPP_DECLTYPE;
10487   start_token->u.value = expr;
10488   start_token->keyword = RID_MAX;
10489   cp_lexer_purge_tokens_after (parser->lexer, start_token);
10490
10491   return expr;
10492 }
10493
10494 /* Special member functions [gram.special] */
10495
10496 /* Parse a conversion-function-id.
10497
10498    conversion-function-id:
10499      operator conversion-type-id
10500
10501    Returns an IDENTIFIER_NODE representing the operator.  */
10502
10503 static tree
10504 cp_parser_conversion_function_id (cp_parser* parser)
10505 {
10506   tree type;
10507   tree saved_scope;
10508   tree saved_qualifying_scope;
10509   tree saved_object_scope;
10510   tree pushed_scope = NULL_TREE;
10511
10512   /* Look for the `operator' token.  */
10513   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10514     return error_mark_node;
10515   /* When we parse the conversion-type-id, the current scope will be
10516      reset.  However, we need that information in able to look up the
10517      conversion function later, so we save it here.  */
10518   saved_scope = parser->scope;
10519   saved_qualifying_scope = parser->qualifying_scope;
10520   saved_object_scope = parser->object_scope;
10521   /* We must enter the scope of the class so that the names of
10522      entities declared within the class are available in the
10523      conversion-type-id.  For example, consider:
10524
10525        struct S {
10526          typedef int I;
10527          operator I();
10528        };
10529
10530        S::operator I() { ... }
10531
10532      In order to see that `I' is a type-name in the definition, we
10533      must be in the scope of `S'.  */
10534   if (saved_scope)
10535     pushed_scope = push_scope (saved_scope);
10536   /* Parse the conversion-type-id.  */
10537   type = cp_parser_conversion_type_id (parser);
10538   /* Leave the scope of the class, if any.  */
10539   if (pushed_scope)
10540     pop_scope (pushed_scope);
10541   /* Restore the saved scope.  */
10542   parser->scope = saved_scope;
10543   parser->qualifying_scope = saved_qualifying_scope;
10544   parser->object_scope = saved_object_scope;
10545   /* If the TYPE is invalid, indicate failure.  */
10546   if (type == error_mark_node)
10547     return error_mark_node;
10548   return mangle_conv_op_name_for_type (type);
10549 }
10550
10551 /* Parse a conversion-type-id:
10552
10553    conversion-type-id:
10554      type-specifier-seq conversion-declarator [opt]
10555
10556    Returns the TYPE specified.  */
10557
10558 static tree
10559 cp_parser_conversion_type_id (cp_parser* parser)
10560 {
10561   tree attributes;
10562   cp_decl_specifier_seq type_specifiers;
10563   cp_declarator *declarator;
10564   tree type_specified;
10565
10566   /* Parse the attributes.  */
10567   attributes = cp_parser_attributes_opt (parser);
10568   /* Parse the type-specifiers.  */
10569   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10570                                 /*is_trailing_return=*/false,
10571                                 &type_specifiers);
10572   /* If that didn't work, stop.  */
10573   if (type_specifiers.type == error_mark_node)
10574     return error_mark_node;
10575   /* Parse the conversion-declarator.  */
10576   declarator = cp_parser_conversion_declarator_opt (parser);
10577
10578   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10579                                     /*initialized=*/0, &attributes);
10580   if (attributes)
10581     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10582
10583   /* Don't give this error when parsing tentatively.  This happens to
10584      work because we always parse this definitively once.  */
10585   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10586       && type_uses_auto (type_specified))
10587     {
10588       error ("invalid use of %<auto%> in conversion operator");
10589       return error_mark_node;
10590     }
10591
10592   return type_specified;
10593 }
10594
10595 /* Parse an (optional) conversion-declarator.
10596
10597    conversion-declarator:
10598      ptr-operator conversion-declarator [opt]
10599
10600    */
10601
10602 static cp_declarator *
10603 cp_parser_conversion_declarator_opt (cp_parser* parser)
10604 {
10605   enum tree_code code;
10606   tree class_type;
10607   cp_cv_quals cv_quals;
10608
10609   /* We don't know if there's a ptr-operator next, or not.  */
10610   cp_parser_parse_tentatively (parser);
10611   /* Try the ptr-operator.  */
10612   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10613   /* If it worked, look for more conversion-declarators.  */
10614   if (cp_parser_parse_definitely (parser))
10615     {
10616       cp_declarator *declarator;
10617
10618       /* Parse another optional declarator.  */
10619       declarator = cp_parser_conversion_declarator_opt (parser);
10620
10621       return cp_parser_make_indirect_declarator
10622         (code, class_type, cv_quals, declarator);
10623    }
10624
10625   return NULL;
10626 }
10627
10628 /* Parse an (optional) ctor-initializer.
10629
10630    ctor-initializer:
10631      : mem-initializer-list
10632
10633    Returns TRUE iff the ctor-initializer was actually present.  */
10634
10635 static bool
10636 cp_parser_ctor_initializer_opt (cp_parser* parser)
10637 {
10638   /* If the next token is not a `:', then there is no
10639      ctor-initializer.  */
10640   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10641     {
10642       /* Do default initialization of any bases and members.  */
10643       if (DECL_CONSTRUCTOR_P (current_function_decl))
10644         finish_mem_initializers (NULL_TREE);
10645
10646       return false;
10647     }
10648
10649   /* Consume the `:' token.  */
10650   cp_lexer_consume_token (parser->lexer);
10651   /* And the mem-initializer-list.  */
10652   cp_parser_mem_initializer_list (parser);
10653
10654   return true;
10655 }
10656
10657 /* Parse a mem-initializer-list.
10658
10659    mem-initializer-list:
10660      mem-initializer ... [opt]
10661      mem-initializer ... [opt] , mem-initializer-list  */
10662
10663 static void
10664 cp_parser_mem_initializer_list (cp_parser* parser)
10665 {
10666   tree mem_initializer_list = NULL_TREE;
10667   cp_token *token = cp_lexer_peek_token (parser->lexer);
10668
10669   /* Let the semantic analysis code know that we are starting the
10670      mem-initializer-list.  */
10671   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10672     error_at (token->location,
10673               "only constructors take member initializers");
10674
10675   /* Loop through the list.  */
10676   while (true)
10677     {
10678       tree mem_initializer;
10679
10680       token = cp_lexer_peek_token (parser->lexer);
10681       /* Parse the mem-initializer.  */
10682       mem_initializer = cp_parser_mem_initializer (parser);
10683       /* If the next token is a `...', we're expanding member initializers. */
10684       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10685         {
10686           /* Consume the `...'. */
10687           cp_lexer_consume_token (parser->lexer);
10688
10689           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10690              can be expanded but members cannot. */
10691           if (mem_initializer != error_mark_node
10692               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10693             {
10694               error_at (token->location,
10695                         "cannot expand initializer for member %<%D%>",
10696                         TREE_PURPOSE (mem_initializer));
10697               mem_initializer = error_mark_node;
10698             }
10699
10700           /* Construct the pack expansion type. */
10701           if (mem_initializer != error_mark_node)
10702             mem_initializer = make_pack_expansion (mem_initializer);
10703         }
10704       /* Add it to the list, unless it was erroneous.  */
10705       if (mem_initializer != error_mark_node)
10706         {
10707           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10708           mem_initializer_list = mem_initializer;
10709         }
10710       /* If the next token is not a `,', we're done.  */
10711       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10712         break;
10713       /* Consume the `,' token.  */
10714       cp_lexer_consume_token (parser->lexer);
10715     }
10716
10717   /* Perform semantic analysis.  */
10718   if (DECL_CONSTRUCTOR_P (current_function_decl))
10719     finish_mem_initializers (mem_initializer_list);
10720 }
10721
10722 /* Parse a mem-initializer.
10723
10724    mem-initializer:
10725      mem-initializer-id ( expression-list [opt] )
10726      mem-initializer-id braced-init-list
10727
10728    GNU extension:
10729
10730    mem-initializer:
10731      ( expression-list [opt] )
10732
10733    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10734    class) or FIELD_DECL (for a non-static data member) to initialize;
10735    the TREE_VALUE is the expression-list.  An empty initialization
10736    list is represented by void_list_node.  */
10737
10738 static tree
10739 cp_parser_mem_initializer (cp_parser* parser)
10740 {
10741   tree mem_initializer_id;
10742   tree expression_list;
10743   tree member;
10744   cp_token *token = cp_lexer_peek_token (parser->lexer);
10745
10746   /* Find out what is being initialized.  */
10747   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10748     {
10749       permerror (token->location,
10750                  "anachronistic old-style base class initializer");
10751       mem_initializer_id = NULL_TREE;
10752     }
10753   else
10754     {
10755       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10756       if (mem_initializer_id == error_mark_node)
10757         return mem_initializer_id;
10758     }
10759   member = expand_member_init (mem_initializer_id);
10760   if (member && !DECL_P (member))
10761     in_base_initializer = 1;
10762
10763   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10764     {
10765       bool expr_non_constant_p;
10766       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10767       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10768       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10769       expression_list = build_tree_list (NULL_TREE, expression_list);
10770     }
10771   else
10772     {
10773       VEC(tree,gc)* vec;
10774       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10775                                                      /*cast_p=*/false,
10776                                                      /*allow_expansion_p=*/true,
10777                                                      /*non_constant_p=*/NULL);
10778       if (vec == NULL)
10779         return error_mark_node;
10780       expression_list = build_tree_list_vec (vec);
10781       release_tree_vector (vec);
10782     }
10783
10784   if (expression_list == error_mark_node)
10785     return error_mark_node;
10786   if (!expression_list)
10787     expression_list = void_type_node;
10788
10789   in_base_initializer = 0;
10790
10791   return member ? build_tree_list (member, expression_list) : error_mark_node;
10792 }
10793
10794 /* Parse a mem-initializer-id.
10795
10796    mem-initializer-id:
10797      :: [opt] nested-name-specifier [opt] class-name
10798      identifier
10799
10800    Returns a TYPE indicating the class to be initializer for the first
10801    production.  Returns an IDENTIFIER_NODE indicating the data member
10802    to be initialized for the second production.  */
10803
10804 static tree
10805 cp_parser_mem_initializer_id (cp_parser* parser)
10806 {
10807   bool global_scope_p;
10808   bool nested_name_specifier_p;
10809   bool template_p = false;
10810   tree id;
10811
10812   cp_token *token = cp_lexer_peek_token (parser->lexer);
10813
10814   /* `typename' is not allowed in this context ([temp.res]).  */
10815   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10816     {
10817       error_at (token->location, 
10818                 "keyword %<typename%> not allowed in this context (a qualified "
10819                 "member initializer is implicitly a type)");
10820       cp_lexer_consume_token (parser->lexer);
10821     }
10822   /* Look for the optional `::' operator.  */
10823   global_scope_p
10824     = (cp_parser_global_scope_opt (parser,
10825                                    /*current_scope_valid_p=*/false)
10826        != NULL_TREE);
10827   /* Look for the optional nested-name-specifier.  The simplest way to
10828      implement:
10829
10830        [temp.res]
10831
10832        The keyword `typename' is not permitted in a base-specifier or
10833        mem-initializer; in these contexts a qualified name that
10834        depends on a template-parameter is implicitly assumed to be a
10835        type name.
10836
10837      is to assume that we have seen the `typename' keyword at this
10838      point.  */
10839   nested_name_specifier_p
10840     = (cp_parser_nested_name_specifier_opt (parser,
10841                                             /*typename_keyword_p=*/true,
10842                                             /*check_dependency_p=*/true,
10843                                             /*type_p=*/true,
10844                                             /*is_declaration=*/true)
10845        != NULL_TREE);
10846   if (nested_name_specifier_p)
10847     template_p = cp_parser_optional_template_keyword (parser);
10848   /* If there is a `::' operator or a nested-name-specifier, then we
10849      are definitely looking for a class-name.  */
10850   if (global_scope_p || nested_name_specifier_p)
10851     return cp_parser_class_name (parser,
10852                                  /*typename_keyword_p=*/true,
10853                                  /*template_keyword_p=*/template_p,
10854                                  typename_type,
10855                                  /*check_dependency_p=*/true,
10856                                  /*class_head_p=*/false,
10857                                  /*is_declaration=*/true);
10858   /* Otherwise, we could also be looking for an ordinary identifier.  */
10859   cp_parser_parse_tentatively (parser);
10860   /* Try a class-name.  */
10861   id = cp_parser_class_name (parser,
10862                              /*typename_keyword_p=*/true,
10863                              /*template_keyword_p=*/false,
10864                              none_type,
10865                              /*check_dependency_p=*/true,
10866                              /*class_head_p=*/false,
10867                              /*is_declaration=*/true);
10868   /* If we found one, we're done.  */
10869   if (cp_parser_parse_definitely (parser))
10870     return id;
10871   /* Otherwise, look for an ordinary identifier.  */
10872   return cp_parser_identifier (parser);
10873 }
10874
10875 /* Overloading [gram.over] */
10876
10877 /* Parse an operator-function-id.
10878
10879    operator-function-id:
10880      operator operator
10881
10882    Returns an IDENTIFIER_NODE for the operator which is a
10883    human-readable spelling of the identifier, e.g., `operator +'.  */
10884
10885 static tree
10886 cp_parser_operator_function_id (cp_parser* parser)
10887 {
10888   /* Look for the `operator' keyword.  */
10889   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10890     return error_mark_node;
10891   /* And then the name of the operator itself.  */
10892   return cp_parser_operator (parser);
10893 }
10894
10895 /* Parse an operator.
10896
10897    operator:
10898      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10899      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10900      || ++ -- , ->* -> () []
10901
10902    GNU Extensions:
10903
10904    operator:
10905      <? >? <?= >?=
10906
10907    Returns an IDENTIFIER_NODE for the operator which is a
10908    human-readable spelling of the identifier, e.g., `operator +'.  */
10909
10910 static tree
10911 cp_parser_operator (cp_parser* parser)
10912 {
10913   tree id = NULL_TREE;
10914   cp_token *token;
10915
10916   /* Peek at the next token.  */
10917   token = cp_lexer_peek_token (parser->lexer);
10918   /* Figure out which operator we have.  */
10919   switch (token->type)
10920     {
10921     case CPP_KEYWORD:
10922       {
10923         enum tree_code op;
10924
10925         /* The keyword should be either `new' or `delete'.  */
10926         if (token->keyword == RID_NEW)
10927           op = NEW_EXPR;
10928         else if (token->keyword == RID_DELETE)
10929           op = DELETE_EXPR;
10930         else
10931           break;
10932
10933         /* Consume the `new' or `delete' token.  */
10934         cp_lexer_consume_token (parser->lexer);
10935
10936         /* Peek at the next token.  */
10937         token = cp_lexer_peek_token (parser->lexer);
10938         /* If it's a `[' token then this is the array variant of the
10939            operator.  */
10940         if (token->type == CPP_OPEN_SQUARE)
10941           {
10942             /* Consume the `[' token.  */
10943             cp_lexer_consume_token (parser->lexer);
10944             /* Look for the `]' token.  */
10945             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10946             id = ansi_opname (op == NEW_EXPR
10947                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10948           }
10949         /* Otherwise, we have the non-array variant.  */
10950         else
10951           id = ansi_opname (op);
10952
10953         return id;
10954       }
10955
10956     case CPP_PLUS:
10957       id = ansi_opname (PLUS_EXPR);
10958       break;
10959
10960     case CPP_MINUS:
10961       id = ansi_opname (MINUS_EXPR);
10962       break;
10963
10964     case CPP_MULT:
10965       id = ansi_opname (MULT_EXPR);
10966       break;
10967
10968     case CPP_DIV:
10969       id = ansi_opname (TRUNC_DIV_EXPR);
10970       break;
10971
10972     case CPP_MOD:
10973       id = ansi_opname (TRUNC_MOD_EXPR);
10974       break;
10975
10976     case CPP_XOR:
10977       id = ansi_opname (BIT_XOR_EXPR);
10978       break;
10979
10980     case CPP_AND:
10981       id = ansi_opname (BIT_AND_EXPR);
10982       break;
10983
10984     case CPP_OR:
10985       id = ansi_opname (BIT_IOR_EXPR);
10986       break;
10987
10988     case CPP_COMPL:
10989       id = ansi_opname (BIT_NOT_EXPR);
10990       break;
10991
10992     case CPP_NOT:
10993       id = ansi_opname (TRUTH_NOT_EXPR);
10994       break;
10995
10996     case CPP_EQ:
10997       id = ansi_assopname (NOP_EXPR);
10998       break;
10999
11000     case CPP_LESS:
11001       id = ansi_opname (LT_EXPR);
11002       break;
11003
11004     case CPP_GREATER:
11005       id = ansi_opname (GT_EXPR);
11006       break;
11007
11008     case CPP_PLUS_EQ:
11009       id = ansi_assopname (PLUS_EXPR);
11010       break;
11011
11012     case CPP_MINUS_EQ:
11013       id = ansi_assopname (MINUS_EXPR);
11014       break;
11015
11016     case CPP_MULT_EQ:
11017       id = ansi_assopname (MULT_EXPR);
11018       break;
11019
11020     case CPP_DIV_EQ:
11021       id = ansi_assopname (TRUNC_DIV_EXPR);
11022       break;
11023
11024     case CPP_MOD_EQ:
11025       id = ansi_assopname (TRUNC_MOD_EXPR);
11026       break;
11027
11028     case CPP_XOR_EQ:
11029       id = ansi_assopname (BIT_XOR_EXPR);
11030       break;
11031
11032     case CPP_AND_EQ:
11033       id = ansi_assopname (BIT_AND_EXPR);
11034       break;
11035
11036     case CPP_OR_EQ:
11037       id = ansi_assopname (BIT_IOR_EXPR);
11038       break;
11039
11040     case CPP_LSHIFT:
11041       id = ansi_opname (LSHIFT_EXPR);
11042       break;
11043
11044     case CPP_RSHIFT:
11045       id = ansi_opname (RSHIFT_EXPR);
11046       break;
11047
11048     case CPP_LSHIFT_EQ:
11049       id = ansi_assopname (LSHIFT_EXPR);
11050       break;
11051
11052     case CPP_RSHIFT_EQ:
11053       id = ansi_assopname (RSHIFT_EXPR);
11054       break;
11055
11056     case CPP_EQ_EQ:
11057       id = ansi_opname (EQ_EXPR);
11058       break;
11059
11060     case CPP_NOT_EQ:
11061       id = ansi_opname (NE_EXPR);
11062       break;
11063
11064     case CPP_LESS_EQ:
11065       id = ansi_opname (LE_EXPR);
11066       break;
11067
11068     case CPP_GREATER_EQ:
11069       id = ansi_opname (GE_EXPR);
11070       break;
11071
11072     case CPP_AND_AND:
11073       id = ansi_opname (TRUTH_ANDIF_EXPR);
11074       break;
11075
11076     case CPP_OR_OR:
11077       id = ansi_opname (TRUTH_ORIF_EXPR);
11078       break;
11079
11080     case CPP_PLUS_PLUS:
11081       id = ansi_opname (POSTINCREMENT_EXPR);
11082       break;
11083
11084     case CPP_MINUS_MINUS:
11085       id = ansi_opname (PREDECREMENT_EXPR);
11086       break;
11087
11088     case CPP_COMMA:
11089       id = ansi_opname (COMPOUND_EXPR);
11090       break;
11091
11092     case CPP_DEREF_STAR:
11093       id = ansi_opname (MEMBER_REF);
11094       break;
11095
11096     case CPP_DEREF:
11097       id = ansi_opname (COMPONENT_REF);
11098       break;
11099
11100     case CPP_OPEN_PAREN:
11101       /* Consume the `('.  */
11102       cp_lexer_consume_token (parser->lexer);
11103       /* Look for the matching `)'.  */
11104       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11105       return ansi_opname (CALL_EXPR);
11106
11107     case CPP_OPEN_SQUARE:
11108       /* Consume the `['.  */
11109       cp_lexer_consume_token (parser->lexer);
11110       /* Look for the matching `]'.  */
11111       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11112       return ansi_opname (ARRAY_REF);
11113
11114     default:
11115       /* Anything else is an error.  */
11116       break;
11117     }
11118
11119   /* If we have selected an identifier, we need to consume the
11120      operator token.  */
11121   if (id)
11122     cp_lexer_consume_token (parser->lexer);
11123   /* Otherwise, no valid operator name was present.  */
11124   else
11125     {
11126       cp_parser_error (parser, "expected operator");
11127       id = error_mark_node;
11128     }
11129
11130   return id;
11131 }
11132
11133 /* Parse a template-declaration.
11134
11135    template-declaration:
11136      export [opt] template < template-parameter-list > declaration
11137
11138    If MEMBER_P is TRUE, this template-declaration occurs within a
11139    class-specifier.
11140
11141    The grammar rule given by the standard isn't correct.  What
11142    is really meant is:
11143
11144    template-declaration:
11145      export [opt] template-parameter-list-seq
11146        decl-specifier-seq [opt] init-declarator [opt] ;
11147      export [opt] template-parameter-list-seq
11148        function-definition
11149
11150    template-parameter-list-seq:
11151      template-parameter-list-seq [opt]
11152      template < template-parameter-list >  */
11153
11154 static void
11155 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11156 {
11157   /* Check for `export'.  */
11158   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11159     {
11160       /* Consume the `export' token.  */
11161       cp_lexer_consume_token (parser->lexer);
11162       /* Warn that we do not support `export'.  */
11163       warning (0, "keyword %<export%> not implemented, and will be ignored");
11164     }
11165
11166   cp_parser_template_declaration_after_export (parser, member_p);
11167 }
11168
11169 /* Parse a template-parameter-list.
11170
11171    template-parameter-list:
11172      template-parameter
11173      template-parameter-list , template-parameter
11174
11175    Returns a TREE_LIST.  Each node represents a template parameter.
11176    The nodes are connected via their TREE_CHAINs.  */
11177
11178 static tree
11179 cp_parser_template_parameter_list (cp_parser* parser)
11180 {
11181   tree parameter_list = NULL_TREE;
11182
11183   begin_template_parm_list ();
11184
11185   /* The loop below parses the template parms.  We first need to know
11186      the total number of template parms to be able to compute proper
11187      canonical types of each dependent type. So after the loop, when
11188      we know the total number of template parms,
11189      end_template_parm_list computes the proper canonical types and
11190      fixes up the dependent types accordingly.  */
11191   while (true)
11192     {
11193       tree parameter;
11194       bool is_non_type;
11195       bool is_parameter_pack;
11196       location_t parm_loc;
11197
11198       /* Parse the template-parameter.  */
11199       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11200       parameter = cp_parser_template_parameter (parser, 
11201                                                 &is_non_type,
11202                                                 &is_parameter_pack);
11203       /* Add it to the list.  */
11204       if (parameter != error_mark_node)
11205         parameter_list = process_template_parm (parameter_list,
11206                                                 parm_loc,
11207                                                 parameter,
11208                                                 is_non_type,
11209                                                 is_parameter_pack,
11210                                                 0);
11211       else
11212        {
11213          tree err_parm = build_tree_list (parameter, parameter);
11214          parameter_list = chainon (parameter_list, err_parm);
11215        }
11216
11217       /* If the next token is not a `,', we're done.  */
11218       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11219         break;
11220       /* Otherwise, consume the `,' token.  */
11221       cp_lexer_consume_token (parser->lexer);
11222     }
11223
11224   return end_template_parm_list (parameter_list);
11225 }
11226
11227 /* Parse a template-parameter.
11228
11229    template-parameter:
11230      type-parameter
11231      parameter-declaration
11232
11233    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11234    the parameter.  The TREE_PURPOSE is the default value, if any.
11235    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11236    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11237    set to true iff this parameter is a parameter pack. */
11238
11239 static tree
11240 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11241                               bool *is_parameter_pack)
11242 {
11243   cp_token *token;
11244   cp_parameter_declarator *parameter_declarator;
11245   cp_declarator *id_declarator;
11246   tree parm;
11247
11248   /* Assume it is a type parameter or a template parameter.  */
11249   *is_non_type = false;
11250   /* Assume it not a parameter pack. */
11251   *is_parameter_pack = false;
11252   /* Peek at the next token.  */
11253   token = cp_lexer_peek_token (parser->lexer);
11254   /* If it is `class' or `template', we have a type-parameter.  */
11255   if (token->keyword == RID_TEMPLATE)
11256     return cp_parser_type_parameter (parser, is_parameter_pack);
11257   /* If it is `class' or `typename' we do not know yet whether it is a
11258      type parameter or a non-type parameter.  Consider:
11259
11260        template <typename T, typename T::X X> ...
11261
11262      or:
11263
11264        template <class C, class D*> ...
11265
11266      Here, the first parameter is a type parameter, and the second is
11267      a non-type parameter.  We can tell by looking at the token after
11268      the identifier -- if it is a `,', `=', or `>' then we have a type
11269      parameter.  */
11270   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11271     {
11272       /* Peek at the token after `class' or `typename'.  */
11273       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11274       /* If it's an ellipsis, we have a template type parameter
11275          pack. */
11276       if (token->type == CPP_ELLIPSIS)
11277         return cp_parser_type_parameter (parser, is_parameter_pack);
11278       /* If it's an identifier, skip it.  */
11279       if (token->type == CPP_NAME)
11280         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11281       /* Now, see if the token looks like the end of a template
11282          parameter.  */
11283       if (token->type == CPP_COMMA
11284           || token->type == CPP_EQ
11285           || token->type == CPP_GREATER)
11286         return cp_parser_type_parameter (parser, is_parameter_pack);
11287     }
11288
11289   /* Otherwise, it is a non-type parameter.
11290
11291      [temp.param]
11292
11293      When parsing a default template-argument for a non-type
11294      template-parameter, the first non-nested `>' is taken as the end
11295      of the template parameter-list rather than a greater-than
11296      operator.  */
11297   *is_non_type = true;
11298   parameter_declarator
11299      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11300                                         /*parenthesized_p=*/NULL);
11301
11302   /* If the parameter declaration is marked as a parameter pack, set
11303      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11304      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11305      grokdeclarator. */
11306   if (parameter_declarator
11307       && parameter_declarator->declarator
11308       && parameter_declarator->declarator->parameter_pack_p)
11309     {
11310       *is_parameter_pack = true;
11311       parameter_declarator->declarator->parameter_pack_p = false;
11312     }
11313
11314   /* If the next token is an ellipsis, and we don't already have it
11315      marked as a parameter pack, then we have a parameter pack (that
11316      has no declarator).  */
11317   if (!*is_parameter_pack
11318       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11319       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11320     {
11321       /* Consume the `...'.  */
11322       cp_lexer_consume_token (parser->lexer);
11323       maybe_warn_variadic_templates ();
11324       
11325       *is_parameter_pack = true;
11326     }
11327   /* We might end up with a pack expansion as the type of the non-type
11328      template parameter, in which case this is a non-type template
11329      parameter pack.  */
11330   else if (parameter_declarator
11331            && parameter_declarator->decl_specifiers.type
11332            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11333     {
11334       *is_parameter_pack = true;
11335       parameter_declarator->decl_specifiers.type = 
11336         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11337     }
11338
11339   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11340     {
11341       /* Parameter packs cannot have default arguments.  However, a
11342          user may try to do so, so we'll parse them and give an
11343          appropriate diagnostic here.  */
11344
11345       /* Consume the `='.  */
11346       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11347       cp_lexer_consume_token (parser->lexer);
11348       
11349       /* Find the name of the parameter pack.  */     
11350       id_declarator = parameter_declarator->declarator;
11351       while (id_declarator && id_declarator->kind != cdk_id)
11352         id_declarator = id_declarator->declarator;
11353       
11354       if (id_declarator && id_declarator->kind == cdk_id)
11355         error_at (start_token->location,
11356                   "template parameter pack %qD cannot have a default argument",
11357                   id_declarator->u.id.unqualified_name);
11358       else
11359         error_at (start_token->location,
11360                   "template parameter pack cannot have a default argument");
11361       
11362       /* Parse the default argument, but throw away the result.  */
11363       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11364     }
11365
11366   parm = grokdeclarator (parameter_declarator->declarator,
11367                          &parameter_declarator->decl_specifiers,
11368                          TPARM, /*initialized=*/0,
11369                          /*attrlist=*/NULL);
11370   if (parm == error_mark_node)
11371     return error_mark_node;
11372
11373   return build_tree_list (parameter_declarator->default_argument, parm);
11374 }
11375
11376 /* Parse a type-parameter.
11377
11378    type-parameter:
11379      class identifier [opt]
11380      class identifier [opt] = type-id
11381      typename identifier [opt]
11382      typename identifier [opt] = type-id
11383      template < template-parameter-list > class identifier [opt]
11384      template < template-parameter-list > class identifier [opt]
11385        = id-expression
11386
11387    GNU Extension (variadic templates):
11388
11389    type-parameter:
11390      class ... identifier [opt]
11391      typename ... identifier [opt]
11392
11393    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11394    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11395    the declaration of the parameter.
11396
11397    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11398
11399 static tree
11400 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11401 {
11402   cp_token *token;
11403   tree parameter;
11404
11405   /* Look for a keyword to tell us what kind of parameter this is.  */
11406   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11407   if (!token)
11408     return error_mark_node;
11409
11410   switch (token->keyword)
11411     {
11412     case RID_CLASS:
11413     case RID_TYPENAME:
11414       {
11415         tree identifier;
11416         tree default_argument;
11417
11418         /* If the next token is an ellipsis, we have a template
11419            argument pack. */
11420         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11421           {
11422             /* Consume the `...' token. */
11423             cp_lexer_consume_token (parser->lexer);
11424             maybe_warn_variadic_templates ();
11425
11426             *is_parameter_pack = true;
11427           }
11428
11429         /* If the next token is an identifier, then it names the
11430            parameter.  */
11431         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11432           identifier = cp_parser_identifier (parser);
11433         else
11434           identifier = NULL_TREE;
11435
11436         /* Create the parameter.  */
11437         parameter = finish_template_type_parm (class_type_node, identifier);
11438
11439         /* If the next token is an `=', we have a default argument.  */
11440         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11441           {
11442             /* Consume the `=' token.  */
11443             cp_lexer_consume_token (parser->lexer);
11444             /* Parse the default-argument.  */
11445             push_deferring_access_checks (dk_no_deferred);
11446             default_argument = cp_parser_type_id (parser);
11447
11448             /* Template parameter packs cannot have default
11449                arguments. */
11450             if (*is_parameter_pack)
11451               {
11452                 if (identifier)
11453                   error_at (token->location,
11454                             "template parameter pack %qD cannot have a "
11455                             "default argument", identifier);
11456                 else
11457                   error_at (token->location,
11458                             "template parameter packs cannot have "
11459                             "default arguments");
11460                 default_argument = NULL_TREE;
11461               }
11462             pop_deferring_access_checks ();
11463           }
11464         else
11465           default_argument = NULL_TREE;
11466
11467         /* Create the combined representation of the parameter and the
11468            default argument.  */
11469         parameter = build_tree_list (default_argument, parameter);
11470       }
11471       break;
11472
11473     case RID_TEMPLATE:
11474       {
11475         tree identifier;
11476         tree default_argument;
11477
11478         /* Look for the `<'.  */
11479         cp_parser_require (parser, CPP_LESS, RT_LESS);
11480         /* Parse the template-parameter-list.  */
11481         cp_parser_template_parameter_list (parser);
11482         /* Look for the `>'.  */
11483         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11484         /* Look for the `class' keyword.  */
11485         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11486         /* If the next token is an ellipsis, we have a template
11487            argument pack. */
11488         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11489           {
11490             /* Consume the `...' token. */
11491             cp_lexer_consume_token (parser->lexer);
11492             maybe_warn_variadic_templates ();
11493
11494             *is_parameter_pack = true;
11495           }
11496         /* If the next token is an `=', then there is a
11497            default-argument.  If the next token is a `>', we are at
11498            the end of the parameter-list.  If the next token is a `,',
11499            then we are at the end of this parameter.  */
11500         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11501             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11502             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11503           {
11504             identifier = cp_parser_identifier (parser);
11505             /* Treat invalid names as if the parameter were nameless.  */
11506             if (identifier == error_mark_node)
11507               identifier = NULL_TREE;
11508           }
11509         else
11510           identifier = NULL_TREE;
11511
11512         /* Create the template parameter.  */
11513         parameter = finish_template_template_parm (class_type_node,
11514                                                    identifier);
11515
11516         /* If the next token is an `=', then there is a
11517            default-argument.  */
11518         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11519           {
11520             bool is_template;
11521
11522             /* Consume the `='.  */
11523             cp_lexer_consume_token (parser->lexer);
11524             /* Parse the id-expression.  */
11525             push_deferring_access_checks (dk_no_deferred);
11526             /* save token before parsing the id-expression, for error
11527                reporting */
11528             token = cp_lexer_peek_token (parser->lexer);
11529             default_argument
11530               = cp_parser_id_expression (parser,
11531                                          /*template_keyword_p=*/false,
11532                                          /*check_dependency_p=*/true,
11533                                          /*template_p=*/&is_template,
11534                                          /*declarator_p=*/false,
11535                                          /*optional_p=*/false);
11536             if (TREE_CODE (default_argument) == TYPE_DECL)
11537               /* If the id-expression was a template-id that refers to
11538                  a template-class, we already have the declaration here,
11539                  so no further lookup is needed.  */
11540                  ;
11541             else
11542               /* Look up the name.  */
11543               default_argument
11544                 = cp_parser_lookup_name (parser, default_argument,
11545                                          none_type,
11546                                          /*is_template=*/is_template,
11547                                          /*is_namespace=*/false,
11548                                          /*check_dependency=*/true,
11549                                          /*ambiguous_decls=*/NULL,
11550                                          token->location);
11551             /* See if the default argument is valid.  */
11552             default_argument
11553               = check_template_template_default_arg (default_argument);
11554
11555             /* Template parameter packs cannot have default
11556                arguments. */
11557             if (*is_parameter_pack)
11558               {
11559                 if (identifier)
11560                   error_at (token->location,
11561                             "template parameter pack %qD cannot "
11562                             "have a default argument",
11563                             identifier);
11564                 else
11565                   error_at (token->location, "template parameter packs cannot "
11566                             "have default arguments");
11567                 default_argument = NULL_TREE;
11568               }
11569             pop_deferring_access_checks ();
11570           }
11571         else
11572           default_argument = NULL_TREE;
11573
11574         /* Create the combined representation of the parameter and the
11575            default argument.  */
11576         parameter = build_tree_list (default_argument, parameter);
11577       }
11578       break;
11579
11580     default:
11581       gcc_unreachable ();
11582       break;
11583     }
11584
11585   return parameter;
11586 }
11587
11588 /* Parse a template-id.
11589
11590    template-id:
11591      template-name < template-argument-list [opt] >
11592
11593    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11594    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11595    returned.  Otherwise, if the template-name names a function, or set
11596    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11597    names a class, returns a TYPE_DECL for the specialization.
11598
11599    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11600    uninstantiated templates.  */
11601
11602 static tree
11603 cp_parser_template_id (cp_parser *parser,
11604                        bool template_keyword_p,
11605                        bool check_dependency_p,
11606                        bool is_declaration)
11607 {
11608   int i;
11609   tree templ;
11610   tree arguments;
11611   tree template_id;
11612   cp_token_position start_of_id = 0;
11613   deferred_access_check *chk;
11614   VEC (deferred_access_check,gc) *access_check;
11615   cp_token *next_token = NULL, *next_token_2 = NULL;
11616   bool is_identifier;
11617
11618   /* If the next token corresponds to a template-id, there is no need
11619      to reparse it.  */
11620   next_token = cp_lexer_peek_token (parser->lexer);
11621   if (next_token->type == CPP_TEMPLATE_ID)
11622     {
11623       struct tree_check *check_value;
11624
11625       /* Get the stored value.  */
11626       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11627       /* Perform any access checks that were deferred.  */
11628       access_check = check_value->checks;
11629       if (access_check)
11630         {
11631           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11632             perform_or_defer_access_check (chk->binfo,
11633                                            chk->decl,
11634                                            chk->diag_decl);
11635         }
11636       /* Return the stored value.  */
11637       return check_value->value;
11638     }
11639
11640   /* Avoid performing name lookup if there is no possibility of
11641      finding a template-id.  */
11642   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11643       || (next_token->type == CPP_NAME
11644           && !cp_parser_nth_token_starts_template_argument_list_p
11645                (parser, 2)))
11646     {
11647       cp_parser_error (parser, "expected template-id");
11648       return error_mark_node;
11649     }
11650
11651   /* Remember where the template-id starts.  */
11652   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11653     start_of_id = cp_lexer_token_position (parser->lexer, false);
11654
11655   push_deferring_access_checks (dk_deferred);
11656
11657   /* Parse the template-name.  */
11658   is_identifier = false;
11659   templ = cp_parser_template_name (parser, template_keyword_p,
11660                                    check_dependency_p,
11661                                    is_declaration,
11662                                    &is_identifier);
11663   if (templ == error_mark_node || is_identifier)
11664     {
11665       pop_deferring_access_checks ();
11666       return templ;
11667     }
11668
11669   /* If we find the sequence `[:' after a template-name, it's probably
11670      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11671      parse correctly the argument list.  */
11672   next_token = cp_lexer_peek_token (parser->lexer);
11673   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11674   if (next_token->type == CPP_OPEN_SQUARE
11675       && next_token->flags & DIGRAPH
11676       && next_token_2->type == CPP_COLON
11677       && !(next_token_2->flags & PREV_WHITE))
11678     {
11679       cp_parser_parse_tentatively (parser);
11680       /* Change `:' into `::'.  */
11681       next_token_2->type = CPP_SCOPE;
11682       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11683          CPP_LESS.  */
11684       cp_lexer_consume_token (parser->lexer);
11685
11686       /* Parse the arguments.  */
11687       arguments = cp_parser_enclosed_template_argument_list (parser);
11688       if (!cp_parser_parse_definitely (parser))
11689         {
11690           /* If we couldn't parse an argument list, then we revert our changes
11691              and return simply an error. Maybe this is not a template-id
11692              after all.  */
11693           next_token_2->type = CPP_COLON;
11694           cp_parser_error (parser, "expected %<<%>");
11695           pop_deferring_access_checks ();
11696           return error_mark_node;
11697         }
11698       /* Otherwise, emit an error about the invalid digraph, but continue
11699          parsing because we got our argument list.  */
11700       if (permerror (next_token->location,
11701                      "%<<::%> cannot begin a template-argument list"))
11702         {
11703           static bool hint = false;
11704           inform (next_token->location,
11705                   "%<<:%> is an alternate spelling for %<[%>."
11706                   " Insert whitespace between %<<%> and %<::%>");
11707           if (!hint && !flag_permissive)
11708             {
11709               inform (next_token->location, "(if you use %<-fpermissive%>"
11710                       " G++ will accept your code)");
11711               hint = true;
11712             }
11713         }
11714     }
11715   else
11716     {
11717       /* Look for the `<' that starts the template-argument-list.  */
11718       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11719         {
11720           pop_deferring_access_checks ();
11721           return error_mark_node;
11722         }
11723       /* Parse the arguments.  */
11724       arguments = cp_parser_enclosed_template_argument_list (parser);
11725     }
11726
11727   /* Build a representation of the specialization.  */
11728   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11729     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11730   else if (DECL_CLASS_TEMPLATE_P (templ)
11731            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11732     {
11733       bool entering_scope;
11734       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11735          template (rather than some instantiation thereof) only if
11736          is not nested within some other construct.  For example, in
11737          "template <typename T> void f(T) { A<T>::", A<T> is just an
11738          instantiation of A.  */
11739       entering_scope = (template_parm_scope_p ()
11740                         && cp_lexer_next_token_is (parser->lexer,
11741                                                    CPP_SCOPE));
11742       template_id
11743         = finish_template_type (templ, arguments, entering_scope);
11744     }
11745   else
11746     {
11747       /* If it's not a class-template or a template-template, it should be
11748          a function-template.  */
11749       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11750                    || TREE_CODE (templ) == OVERLOAD
11751                    || BASELINK_P (templ)));
11752
11753       template_id = lookup_template_function (templ, arguments);
11754     }
11755
11756   /* If parsing tentatively, replace the sequence of tokens that makes
11757      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11758      should we re-parse the token stream, we will not have to repeat
11759      the effort required to do the parse, nor will we issue duplicate
11760      error messages about problems during instantiation of the
11761      template.  */
11762   if (start_of_id)
11763     {
11764       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11765
11766       /* Reset the contents of the START_OF_ID token.  */
11767       token->type = CPP_TEMPLATE_ID;
11768       /* Retrieve any deferred checks.  Do not pop this access checks yet
11769          so the memory will not be reclaimed during token replacing below.  */
11770       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11771       token->u.tree_check_value->value = template_id;
11772       token->u.tree_check_value->checks = get_deferred_access_checks ();
11773       token->keyword = RID_MAX;
11774
11775       /* Purge all subsequent tokens.  */
11776       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11777
11778       /* ??? Can we actually assume that, if template_id ==
11779          error_mark_node, we will have issued a diagnostic to the
11780          user, as opposed to simply marking the tentative parse as
11781          failed?  */
11782       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11783         error_at (token->location, "parse error in template argument list");
11784     }
11785
11786   pop_deferring_access_checks ();
11787   return template_id;
11788 }
11789
11790 /* Parse a template-name.
11791
11792    template-name:
11793      identifier
11794
11795    The standard should actually say:
11796
11797    template-name:
11798      identifier
11799      operator-function-id
11800
11801    A defect report has been filed about this issue.
11802
11803    A conversion-function-id cannot be a template name because they cannot
11804    be part of a template-id. In fact, looking at this code:
11805
11806    a.operator K<int>()
11807
11808    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11809    It is impossible to call a templated conversion-function-id with an
11810    explicit argument list, since the only allowed template parameter is
11811    the type to which it is converting.
11812
11813    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11814    `template' keyword, in a construction like:
11815
11816      T::template f<3>()
11817
11818    In that case `f' is taken to be a template-name, even though there
11819    is no way of knowing for sure.
11820
11821    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11822    name refers to a set of overloaded functions, at least one of which
11823    is a template, or an IDENTIFIER_NODE with the name of the template,
11824    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11825    names are looked up inside uninstantiated templates.  */
11826
11827 static tree
11828 cp_parser_template_name (cp_parser* parser,
11829                          bool template_keyword_p,
11830                          bool check_dependency_p,
11831                          bool is_declaration,
11832                          bool *is_identifier)
11833 {
11834   tree identifier;
11835   tree decl;
11836   tree fns;
11837   cp_token *token = cp_lexer_peek_token (parser->lexer);
11838
11839   /* If the next token is `operator', then we have either an
11840      operator-function-id or a conversion-function-id.  */
11841   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11842     {
11843       /* We don't know whether we're looking at an
11844          operator-function-id or a conversion-function-id.  */
11845       cp_parser_parse_tentatively (parser);
11846       /* Try an operator-function-id.  */
11847       identifier = cp_parser_operator_function_id (parser);
11848       /* If that didn't work, try a conversion-function-id.  */
11849       if (!cp_parser_parse_definitely (parser))
11850         {
11851           cp_parser_error (parser, "expected template-name");
11852           return error_mark_node;
11853         }
11854     }
11855   /* Look for the identifier.  */
11856   else
11857     identifier = cp_parser_identifier (parser);
11858
11859   /* If we didn't find an identifier, we don't have a template-id.  */
11860   if (identifier == error_mark_node)
11861     return error_mark_node;
11862
11863   /* If the name immediately followed the `template' keyword, then it
11864      is a template-name.  However, if the next token is not `<', then
11865      we do not treat it as a template-name, since it is not being used
11866      as part of a template-id.  This enables us to handle constructs
11867      like:
11868
11869        template <typename T> struct S { S(); };
11870        template <typename T> S<T>::S();
11871
11872      correctly.  We would treat `S' as a template -- if it were `S<T>'
11873      -- but we do not if there is no `<'.  */
11874
11875   if (processing_template_decl
11876       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11877     {
11878       /* In a declaration, in a dependent context, we pretend that the
11879          "template" keyword was present in order to improve error
11880          recovery.  For example, given:
11881
11882            template <typename T> void f(T::X<int>);
11883
11884          we want to treat "X<int>" as a template-id.  */
11885       if (is_declaration
11886           && !template_keyword_p
11887           && parser->scope && TYPE_P (parser->scope)
11888           && check_dependency_p
11889           && dependent_scope_p (parser->scope)
11890           /* Do not do this for dtors (or ctors), since they never
11891              need the template keyword before their name.  */
11892           && !constructor_name_p (identifier, parser->scope))
11893         {
11894           cp_token_position start = 0;
11895
11896           /* Explain what went wrong.  */
11897           error_at (token->location, "non-template %qD used as template",
11898                     identifier);
11899           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11900                   parser->scope, identifier);
11901           /* If parsing tentatively, find the location of the "<" token.  */
11902           if (cp_parser_simulate_error (parser))
11903             start = cp_lexer_token_position (parser->lexer, true);
11904           /* Parse the template arguments so that we can issue error
11905              messages about them.  */
11906           cp_lexer_consume_token (parser->lexer);
11907           cp_parser_enclosed_template_argument_list (parser);
11908           /* Skip tokens until we find a good place from which to
11909              continue parsing.  */
11910           cp_parser_skip_to_closing_parenthesis (parser,
11911                                                  /*recovering=*/true,
11912                                                  /*or_comma=*/true,
11913                                                  /*consume_paren=*/false);
11914           /* If parsing tentatively, permanently remove the
11915              template argument list.  That will prevent duplicate
11916              error messages from being issued about the missing
11917              "template" keyword.  */
11918           if (start)
11919             cp_lexer_purge_tokens_after (parser->lexer, start);
11920           if (is_identifier)
11921             *is_identifier = true;
11922           return identifier;
11923         }
11924
11925       /* If the "template" keyword is present, then there is generally
11926          no point in doing name-lookup, so we just return IDENTIFIER.
11927          But, if the qualifying scope is non-dependent then we can
11928          (and must) do name-lookup normally.  */
11929       if (template_keyword_p
11930           && (!parser->scope
11931               || (TYPE_P (parser->scope)
11932                   && dependent_type_p (parser->scope))))
11933         return identifier;
11934     }
11935
11936   /* Look up the name.  */
11937   decl = cp_parser_lookup_name (parser, identifier,
11938                                 none_type,
11939                                 /*is_template=*/true,
11940                                 /*is_namespace=*/false,
11941                                 check_dependency_p,
11942                                 /*ambiguous_decls=*/NULL,
11943                                 token->location);
11944
11945   /* If DECL is a template, then the name was a template-name.  */
11946   if (TREE_CODE (decl) == TEMPLATE_DECL)
11947     ;
11948   else
11949     {
11950       tree fn = NULL_TREE;
11951
11952       /* The standard does not explicitly indicate whether a name that
11953          names a set of overloaded declarations, some of which are
11954          templates, is a template-name.  However, such a name should
11955          be a template-name; otherwise, there is no way to form a
11956          template-id for the overloaded templates.  */
11957       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11958       if (TREE_CODE (fns) == OVERLOAD)
11959         for (fn = fns; fn; fn = OVL_NEXT (fn))
11960           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11961             break;
11962
11963       if (!fn)
11964         {
11965           /* The name does not name a template.  */
11966           cp_parser_error (parser, "expected template-name");
11967           return error_mark_node;
11968         }
11969     }
11970
11971   /* If DECL is dependent, and refers to a function, then just return
11972      its name; we will look it up again during template instantiation.  */
11973   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11974     {
11975       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11976       if (TYPE_P (scope) && dependent_type_p (scope))
11977         return identifier;
11978     }
11979
11980   return decl;
11981 }
11982
11983 /* Parse a template-argument-list.
11984
11985    template-argument-list:
11986      template-argument ... [opt]
11987      template-argument-list , template-argument ... [opt]
11988
11989    Returns a TREE_VEC containing the arguments.  */
11990
11991 static tree
11992 cp_parser_template_argument_list (cp_parser* parser)
11993 {
11994   tree fixed_args[10];
11995   unsigned n_args = 0;
11996   unsigned alloced = 10;
11997   tree *arg_ary = fixed_args;
11998   tree vec;
11999   bool saved_in_template_argument_list_p;
12000   bool saved_ice_p;
12001   bool saved_non_ice_p;
12002
12003   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12004   parser->in_template_argument_list_p = true;
12005   /* Even if the template-id appears in an integral
12006      constant-expression, the contents of the argument list do
12007      not.  */
12008   saved_ice_p = parser->integral_constant_expression_p;
12009   parser->integral_constant_expression_p = false;
12010   saved_non_ice_p = parser->non_integral_constant_expression_p;
12011   parser->non_integral_constant_expression_p = false;
12012   /* Parse the arguments.  */
12013   do
12014     {
12015       tree argument;
12016
12017       if (n_args)
12018         /* Consume the comma.  */
12019         cp_lexer_consume_token (parser->lexer);
12020
12021       /* Parse the template-argument.  */
12022       argument = cp_parser_template_argument (parser);
12023
12024       /* If the next token is an ellipsis, we're expanding a template
12025          argument pack. */
12026       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12027         {
12028           if (argument == error_mark_node)
12029             {
12030               cp_token *token = cp_lexer_peek_token (parser->lexer);
12031               error_at (token->location,
12032                         "expected parameter pack before %<...%>");
12033             }
12034           /* Consume the `...' token. */
12035           cp_lexer_consume_token (parser->lexer);
12036
12037           /* Make the argument into a TYPE_PACK_EXPANSION or
12038              EXPR_PACK_EXPANSION. */
12039           argument = make_pack_expansion (argument);
12040         }
12041
12042       if (n_args == alloced)
12043         {
12044           alloced *= 2;
12045
12046           if (arg_ary == fixed_args)
12047             {
12048               arg_ary = XNEWVEC (tree, alloced);
12049               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12050             }
12051           else
12052             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12053         }
12054       arg_ary[n_args++] = argument;
12055     }
12056   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12057
12058   vec = make_tree_vec (n_args);
12059
12060   while (n_args--)
12061     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12062
12063   if (arg_ary != fixed_args)
12064     free (arg_ary);
12065   parser->non_integral_constant_expression_p = saved_non_ice_p;
12066   parser->integral_constant_expression_p = saved_ice_p;
12067   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12068 #ifdef ENABLE_CHECKING
12069   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12070 #endif
12071   return vec;
12072 }
12073
12074 /* Parse a template-argument.
12075
12076    template-argument:
12077      assignment-expression
12078      type-id
12079      id-expression
12080
12081    The representation is that of an assignment-expression, type-id, or
12082    id-expression -- except that the qualified id-expression is
12083    evaluated, so that the value returned is either a DECL or an
12084    OVERLOAD.
12085
12086    Although the standard says "assignment-expression", it forbids
12087    throw-expressions or assignments in the template argument.
12088    Therefore, we use "conditional-expression" instead.  */
12089
12090 static tree
12091 cp_parser_template_argument (cp_parser* parser)
12092 {
12093   tree argument;
12094   bool template_p;
12095   bool address_p;
12096   bool maybe_type_id = false;
12097   cp_token *token = NULL, *argument_start_token = NULL;
12098   cp_id_kind idk;
12099
12100   /* There's really no way to know what we're looking at, so we just
12101      try each alternative in order.
12102
12103        [temp.arg]
12104
12105        In a template-argument, an ambiguity between a type-id and an
12106        expression is resolved to a type-id, regardless of the form of
12107        the corresponding template-parameter.
12108
12109      Therefore, we try a type-id first.  */
12110   cp_parser_parse_tentatively (parser);
12111   argument = cp_parser_template_type_arg (parser);
12112   /* If there was no error parsing the type-id but the next token is a
12113      '>>', our behavior depends on which dialect of C++ we're
12114      parsing. In C++98, we probably found a typo for '> >'. But there
12115      are type-id which are also valid expressions. For instance:
12116
12117      struct X { int operator >> (int); };
12118      template <int V> struct Foo {};
12119      Foo<X () >> 5> r;
12120
12121      Here 'X()' is a valid type-id of a function type, but the user just
12122      wanted to write the expression "X() >> 5". Thus, we remember that we
12123      found a valid type-id, but we still try to parse the argument as an
12124      expression to see what happens. 
12125
12126      In C++0x, the '>>' will be considered two separate '>'
12127      tokens.  */
12128   if (!cp_parser_error_occurred (parser)
12129       && cxx_dialect == cxx98
12130       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12131     {
12132       maybe_type_id = true;
12133       cp_parser_abort_tentative_parse (parser);
12134     }
12135   else
12136     {
12137       /* If the next token isn't a `,' or a `>', then this argument wasn't
12138       really finished. This means that the argument is not a valid
12139       type-id.  */
12140       if (!cp_parser_next_token_ends_template_argument_p (parser))
12141         cp_parser_error (parser, "expected template-argument");
12142       /* If that worked, we're done.  */
12143       if (cp_parser_parse_definitely (parser))
12144         return argument;
12145     }
12146   /* We're still not sure what the argument will be.  */
12147   cp_parser_parse_tentatively (parser);
12148   /* Try a template.  */
12149   argument_start_token = cp_lexer_peek_token (parser->lexer);
12150   argument = cp_parser_id_expression (parser,
12151                                       /*template_keyword_p=*/false,
12152                                       /*check_dependency_p=*/true,
12153                                       &template_p,
12154                                       /*declarator_p=*/false,
12155                                       /*optional_p=*/false);
12156   /* If the next token isn't a `,' or a `>', then this argument wasn't
12157      really finished.  */
12158   if (!cp_parser_next_token_ends_template_argument_p (parser))
12159     cp_parser_error (parser, "expected template-argument");
12160   if (!cp_parser_error_occurred (parser))
12161     {
12162       /* Figure out what is being referred to.  If the id-expression
12163          was for a class template specialization, then we will have a
12164          TYPE_DECL at this point.  There is no need to do name lookup
12165          at this point in that case.  */
12166       if (TREE_CODE (argument) != TYPE_DECL)
12167         argument = cp_parser_lookup_name (parser, argument,
12168                                           none_type,
12169                                           /*is_template=*/template_p,
12170                                           /*is_namespace=*/false,
12171                                           /*check_dependency=*/true,
12172                                           /*ambiguous_decls=*/NULL,
12173                                           argument_start_token->location);
12174       if (TREE_CODE (argument) != TEMPLATE_DECL
12175           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12176         cp_parser_error (parser, "expected template-name");
12177     }
12178   if (cp_parser_parse_definitely (parser))
12179     return argument;
12180   /* It must be a non-type argument.  There permitted cases are given
12181      in [temp.arg.nontype]:
12182
12183      -- an integral constant-expression of integral or enumeration
12184         type; or
12185
12186      -- the name of a non-type template-parameter; or
12187
12188      -- the name of an object or function with external linkage...
12189
12190      -- the address of an object or function with external linkage...
12191
12192      -- a pointer to member...  */
12193   /* Look for a non-type template parameter.  */
12194   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12195     {
12196       cp_parser_parse_tentatively (parser);
12197       argument = cp_parser_primary_expression (parser,
12198                                                /*address_p=*/false,
12199                                                /*cast_p=*/false,
12200                                                /*template_arg_p=*/true,
12201                                                &idk);
12202       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12203           || !cp_parser_next_token_ends_template_argument_p (parser))
12204         cp_parser_simulate_error (parser);
12205       if (cp_parser_parse_definitely (parser))
12206         return argument;
12207     }
12208
12209   /* If the next token is "&", the argument must be the address of an
12210      object or function with external linkage.  */
12211   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12212   if (address_p)
12213     cp_lexer_consume_token (parser->lexer);
12214   /* See if we might have an id-expression.  */
12215   token = cp_lexer_peek_token (parser->lexer);
12216   if (token->type == CPP_NAME
12217       || token->keyword == RID_OPERATOR
12218       || token->type == CPP_SCOPE
12219       || token->type == CPP_TEMPLATE_ID
12220       || token->type == CPP_NESTED_NAME_SPECIFIER)
12221     {
12222       cp_parser_parse_tentatively (parser);
12223       argument = cp_parser_primary_expression (parser,
12224                                                address_p,
12225                                                /*cast_p=*/false,
12226                                                /*template_arg_p=*/true,
12227                                                &idk);
12228       if (cp_parser_error_occurred (parser)
12229           || !cp_parser_next_token_ends_template_argument_p (parser))
12230         cp_parser_abort_tentative_parse (parser);
12231       else
12232         {
12233           tree probe;
12234
12235           if (TREE_CODE (argument) == INDIRECT_REF)
12236             {
12237               gcc_assert (REFERENCE_REF_P (argument));
12238               argument = TREE_OPERAND (argument, 0);
12239             }
12240
12241           /* If we're in a template, we represent a qualified-id referring
12242              to a static data member as a SCOPE_REF even if the scope isn't
12243              dependent so that we can check access control later.  */
12244           probe = argument;
12245           if (TREE_CODE (probe) == SCOPE_REF)
12246             probe = TREE_OPERAND (probe, 1);
12247           if (TREE_CODE (probe) == VAR_DECL)
12248             {
12249               /* A variable without external linkage might still be a
12250                  valid constant-expression, so no error is issued here
12251                  if the external-linkage check fails.  */
12252               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12253                 cp_parser_simulate_error (parser);
12254             }
12255           else if (is_overloaded_fn (argument))
12256             /* All overloaded functions are allowed; if the external
12257                linkage test does not pass, an error will be issued
12258                later.  */
12259             ;
12260           else if (address_p
12261                    && (TREE_CODE (argument) == OFFSET_REF
12262                        || TREE_CODE (argument) == SCOPE_REF))
12263             /* A pointer-to-member.  */
12264             ;
12265           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12266             ;
12267           else
12268             cp_parser_simulate_error (parser);
12269
12270           if (cp_parser_parse_definitely (parser))
12271             {
12272               if (address_p)
12273                 argument = build_x_unary_op (ADDR_EXPR, argument,
12274                                              tf_warning_or_error);
12275               return argument;
12276             }
12277         }
12278     }
12279   /* If the argument started with "&", there are no other valid
12280      alternatives at this point.  */
12281   if (address_p)
12282     {
12283       cp_parser_error (parser, "invalid non-type template argument");
12284       return error_mark_node;
12285     }
12286
12287   /* If the argument wasn't successfully parsed as a type-id followed
12288      by '>>', the argument can only be a constant expression now.
12289      Otherwise, we try parsing the constant-expression tentatively,
12290      because the argument could really be a type-id.  */
12291   if (maybe_type_id)
12292     cp_parser_parse_tentatively (parser);
12293   argument = cp_parser_constant_expression (parser,
12294                                             /*allow_non_constant_p=*/false,
12295                                             /*non_constant_p=*/NULL);
12296   argument = fold_non_dependent_expr (argument);
12297   if (!maybe_type_id)
12298     return argument;
12299   if (!cp_parser_next_token_ends_template_argument_p (parser))
12300     cp_parser_error (parser, "expected template-argument");
12301   if (cp_parser_parse_definitely (parser))
12302     return argument;
12303   /* We did our best to parse the argument as a non type-id, but that
12304      was the only alternative that matched (albeit with a '>' after
12305      it). We can assume it's just a typo from the user, and a
12306      diagnostic will then be issued.  */
12307   return cp_parser_template_type_arg (parser);
12308 }
12309
12310 /* Parse an explicit-instantiation.
12311
12312    explicit-instantiation:
12313      template declaration
12314
12315    Although the standard says `declaration', what it really means is:
12316
12317    explicit-instantiation:
12318      template decl-specifier-seq [opt] declarator [opt] ;
12319
12320    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12321    supposed to be allowed.  A defect report has been filed about this
12322    issue.
12323
12324    GNU Extension:
12325
12326    explicit-instantiation:
12327      storage-class-specifier template
12328        decl-specifier-seq [opt] declarator [opt] ;
12329      function-specifier template
12330        decl-specifier-seq [opt] declarator [opt] ;  */
12331
12332 static void
12333 cp_parser_explicit_instantiation (cp_parser* parser)
12334 {
12335   int declares_class_or_enum;
12336   cp_decl_specifier_seq decl_specifiers;
12337   tree extension_specifier = NULL_TREE;
12338
12339   timevar_push (TV_TEMPLATE_INST);
12340
12341   /* Look for an (optional) storage-class-specifier or
12342      function-specifier.  */
12343   if (cp_parser_allow_gnu_extensions_p (parser))
12344     {
12345       extension_specifier
12346         = cp_parser_storage_class_specifier_opt (parser);
12347       if (!extension_specifier)
12348         extension_specifier
12349           = cp_parser_function_specifier_opt (parser,
12350                                               /*decl_specs=*/NULL);
12351     }
12352
12353   /* Look for the `template' keyword.  */
12354   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12355   /* Let the front end know that we are processing an explicit
12356      instantiation.  */
12357   begin_explicit_instantiation ();
12358   /* [temp.explicit] says that we are supposed to ignore access
12359      control while processing explicit instantiation directives.  */
12360   push_deferring_access_checks (dk_no_check);
12361   /* Parse a decl-specifier-seq.  */
12362   cp_parser_decl_specifier_seq (parser,
12363                                 CP_PARSER_FLAGS_OPTIONAL,
12364                                 &decl_specifiers,
12365                                 &declares_class_or_enum);
12366   /* If there was exactly one decl-specifier, and it declared a class,
12367      and there's no declarator, then we have an explicit type
12368      instantiation.  */
12369   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12370     {
12371       tree type;
12372
12373       type = check_tag_decl (&decl_specifiers);
12374       /* Turn access control back on for names used during
12375          template instantiation.  */
12376       pop_deferring_access_checks ();
12377       if (type)
12378         do_type_instantiation (type, extension_specifier,
12379                                /*complain=*/tf_error);
12380     }
12381   else
12382     {
12383       cp_declarator *declarator;
12384       tree decl;
12385
12386       /* Parse the declarator.  */
12387       declarator
12388         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12389                                 /*ctor_dtor_or_conv_p=*/NULL,
12390                                 /*parenthesized_p=*/NULL,
12391                                 /*member_p=*/false);
12392       if (declares_class_or_enum & 2)
12393         cp_parser_check_for_definition_in_return_type (declarator,
12394                                                        decl_specifiers.type,
12395                                                        decl_specifiers.type_location);
12396       if (declarator != cp_error_declarator)
12397         {
12398           if (decl_specifiers.specs[(int)ds_inline])
12399             permerror (input_location, "explicit instantiation shall not use"
12400                        " %<inline%> specifier");
12401           if (decl_specifiers.specs[(int)ds_constexpr])
12402             permerror (input_location, "explicit instantiation shall not use"
12403                        " %<constexpr%> specifier");
12404
12405           decl = grokdeclarator (declarator, &decl_specifiers,
12406                                  NORMAL, 0, &decl_specifiers.attributes);
12407           /* Turn access control back on for names used during
12408              template instantiation.  */
12409           pop_deferring_access_checks ();
12410           /* Do the explicit instantiation.  */
12411           do_decl_instantiation (decl, extension_specifier);
12412         }
12413       else
12414         {
12415           pop_deferring_access_checks ();
12416           /* Skip the body of the explicit instantiation.  */
12417           cp_parser_skip_to_end_of_statement (parser);
12418         }
12419     }
12420   /* We're done with the instantiation.  */
12421   end_explicit_instantiation ();
12422
12423   cp_parser_consume_semicolon_at_end_of_statement (parser);
12424
12425   timevar_pop (TV_TEMPLATE_INST);
12426 }
12427
12428 /* Parse an explicit-specialization.
12429
12430    explicit-specialization:
12431      template < > declaration
12432
12433    Although the standard says `declaration', what it really means is:
12434
12435    explicit-specialization:
12436      template <> decl-specifier [opt] init-declarator [opt] ;
12437      template <> function-definition
12438      template <> explicit-specialization
12439      template <> template-declaration  */
12440
12441 static void
12442 cp_parser_explicit_specialization (cp_parser* parser)
12443 {
12444   bool need_lang_pop;
12445   cp_token *token = cp_lexer_peek_token (parser->lexer);
12446
12447   /* Look for the `template' keyword.  */
12448   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12449   /* Look for the `<'.  */
12450   cp_parser_require (parser, CPP_LESS, RT_LESS);
12451   /* Look for the `>'.  */
12452   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12453   /* We have processed another parameter list.  */
12454   ++parser->num_template_parameter_lists;
12455   /* [temp]
12456
12457      A template ... explicit specialization ... shall not have C
12458      linkage.  */
12459   if (current_lang_name == lang_name_c)
12460     {
12461       error_at (token->location, "template specialization with C linkage");
12462       /* Give it C++ linkage to avoid confusing other parts of the
12463          front end.  */
12464       push_lang_context (lang_name_cplusplus);
12465       need_lang_pop = true;
12466     }
12467   else
12468     need_lang_pop = false;
12469   /* Let the front end know that we are beginning a specialization.  */
12470   if (!begin_specialization ())
12471     {
12472       end_specialization ();
12473       return;
12474     }
12475
12476   /* If the next keyword is `template', we need to figure out whether
12477      or not we're looking a template-declaration.  */
12478   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12479     {
12480       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12481           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12482         cp_parser_template_declaration_after_export (parser,
12483                                                      /*member_p=*/false);
12484       else
12485         cp_parser_explicit_specialization (parser);
12486     }
12487   else
12488     /* Parse the dependent declaration.  */
12489     cp_parser_single_declaration (parser,
12490                                   /*checks=*/NULL,
12491                                   /*member_p=*/false,
12492                                   /*explicit_specialization_p=*/true,
12493                                   /*friend_p=*/NULL);
12494   /* We're done with the specialization.  */
12495   end_specialization ();
12496   /* For the erroneous case of a template with C linkage, we pushed an
12497      implicit C++ linkage scope; exit that scope now.  */
12498   if (need_lang_pop)
12499     pop_lang_context ();
12500   /* We're done with this parameter list.  */
12501   --parser->num_template_parameter_lists;
12502 }
12503
12504 /* Parse a type-specifier.
12505
12506    type-specifier:
12507      simple-type-specifier
12508      class-specifier
12509      enum-specifier
12510      elaborated-type-specifier
12511      cv-qualifier
12512
12513    GNU Extension:
12514
12515    type-specifier:
12516      __complex__
12517
12518    Returns a representation of the type-specifier.  For a
12519    class-specifier, enum-specifier, or elaborated-type-specifier, a
12520    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12521
12522    The parser flags FLAGS is used to control type-specifier parsing.
12523
12524    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12525    in a decl-specifier-seq.
12526
12527    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12528    class-specifier, enum-specifier, or elaborated-type-specifier, then
12529    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12530    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12531    zero.
12532
12533    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12534    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12535    is set to FALSE.  */
12536
12537 static tree
12538 cp_parser_type_specifier (cp_parser* parser,
12539                           cp_parser_flags flags,
12540                           cp_decl_specifier_seq *decl_specs,
12541                           bool is_declaration,
12542                           int* declares_class_or_enum,
12543                           bool* is_cv_qualifier)
12544 {
12545   tree type_spec = NULL_TREE;
12546   cp_token *token;
12547   enum rid keyword;
12548   cp_decl_spec ds = ds_last;
12549
12550   /* Assume this type-specifier does not declare a new type.  */
12551   if (declares_class_or_enum)
12552     *declares_class_or_enum = 0;
12553   /* And that it does not specify a cv-qualifier.  */
12554   if (is_cv_qualifier)
12555     *is_cv_qualifier = false;
12556   /* Peek at the next token.  */
12557   token = cp_lexer_peek_token (parser->lexer);
12558
12559   /* If we're looking at a keyword, we can use that to guide the
12560      production we choose.  */
12561   keyword = token->keyword;
12562   switch (keyword)
12563     {
12564     case RID_ENUM:
12565       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12566         goto elaborated_type_specifier;
12567
12568       /* Look for the enum-specifier.  */
12569       type_spec = cp_parser_enum_specifier (parser);
12570       /* If that worked, we're done.  */
12571       if (type_spec)
12572         {
12573           if (declares_class_or_enum)
12574             *declares_class_or_enum = 2;
12575           if (decl_specs)
12576             cp_parser_set_decl_spec_type (decl_specs,
12577                                           type_spec,
12578                                           token->location,
12579                                           /*user_defined_p=*/true);
12580           return type_spec;
12581         }
12582       else
12583         goto elaborated_type_specifier;
12584
12585       /* Any of these indicate either a class-specifier, or an
12586          elaborated-type-specifier.  */
12587     case RID_CLASS:
12588     case RID_STRUCT:
12589     case RID_UNION:
12590       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12591         goto elaborated_type_specifier;
12592
12593       /* Parse tentatively so that we can back up if we don't find a
12594          class-specifier.  */
12595       cp_parser_parse_tentatively (parser);
12596       /* Look for the class-specifier.  */
12597       type_spec = cp_parser_class_specifier (parser);
12598       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12599       /* If that worked, we're done.  */
12600       if (cp_parser_parse_definitely (parser))
12601         {
12602           if (declares_class_or_enum)
12603             *declares_class_or_enum = 2;
12604           if (decl_specs)
12605             cp_parser_set_decl_spec_type (decl_specs,
12606                                           type_spec,
12607                                           token->location,
12608                                           /*user_defined_p=*/true);
12609           return type_spec;
12610         }
12611
12612       /* Fall through.  */
12613     elaborated_type_specifier:
12614       /* We're declaring (not defining) a class or enum.  */
12615       if (declares_class_or_enum)
12616         *declares_class_or_enum = 1;
12617
12618       /* Fall through.  */
12619     case RID_TYPENAME:
12620       /* Look for an elaborated-type-specifier.  */
12621       type_spec
12622         = (cp_parser_elaborated_type_specifier
12623            (parser,
12624             decl_specs && decl_specs->specs[(int) ds_friend],
12625             is_declaration));
12626       if (decl_specs)
12627         cp_parser_set_decl_spec_type (decl_specs,
12628                                       type_spec,
12629                                       token->location,
12630                                       /*user_defined_p=*/true);
12631       return type_spec;
12632
12633     case RID_CONST:
12634       ds = ds_const;
12635       if (is_cv_qualifier)
12636         *is_cv_qualifier = true;
12637       break;
12638
12639     case RID_VOLATILE:
12640       ds = ds_volatile;
12641       if (is_cv_qualifier)
12642         *is_cv_qualifier = true;
12643       break;
12644
12645     case RID_RESTRICT:
12646       ds = ds_restrict;
12647       if (is_cv_qualifier)
12648         *is_cv_qualifier = true;
12649       break;
12650
12651     case RID_COMPLEX:
12652       /* The `__complex__' keyword is a GNU extension.  */
12653       ds = ds_complex;
12654       break;
12655
12656     default:
12657       break;
12658     }
12659
12660   /* Handle simple keywords.  */
12661   if (ds != ds_last)
12662     {
12663       if (decl_specs)
12664         {
12665           ++decl_specs->specs[(int)ds];
12666           decl_specs->any_specifiers_p = true;
12667         }
12668       return cp_lexer_consume_token (parser->lexer)->u.value;
12669     }
12670
12671   /* If we do not already have a type-specifier, assume we are looking
12672      at a simple-type-specifier.  */
12673   type_spec = cp_parser_simple_type_specifier (parser,
12674                                                decl_specs,
12675                                                flags);
12676
12677   /* If we didn't find a type-specifier, and a type-specifier was not
12678      optional in this context, issue an error message.  */
12679   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12680     {
12681       cp_parser_error (parser, "expected type specifier");
12682       return error_mark_node;
12683     }
12684
12685   return type_spec;
12686 }
12687
12688 /* Parse a simple-type-specifier.
12689
12690    simple-type-specifier:
12691      :: [opt] nested-name-specifier [opt] type-name
12692      :: [opt] nested-name-specifier template template-id
12693      char
12694      wchar_t
12695      bool
12696      short
12697      int
12698      long
12699      signed
12700      unsigned
12701      float
12702      double
12703      void
12704
12705    C++0x Extension:
12706
12707    simple-type-specifier:
12708      auto
12709      decltype ( expression )   
12710      char16_t
12711      char32_t
12712      __underlying_type ( type-id )
12713
12714    GNU Extension:
12715
12716    simple-type-specifier:
12717      __int128
12718      __typeof__ unary-expression
12719      __typeof__ ( type-id )
12720
12721    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12722    appropriately updated.  */
12723
12724 static tree
12725 cp_parser_simple_type_specifier (cp_parser* parser,
12726                                  cp_decl_specifier_seq *decl_specs,
12727                                  cp_parser_flags flags)
12728 {
12729   tree type = NULL_TREE;
12730   cp_token *token;
12731
12732   /* Peek at the next token.  */
12733   token = cp_lexer_peek_token (parser->lexer);
12734
12735   /* If we're looking at a keyword, things are easy.  */
12736   switch (token->keyword)
12737     {
12738     case RID_CHAR:
12739       if (decl_specs)
12740         decl_specs->explicit_char_p = true;
12741       type = char_type_node;
12742       break;
12743     case RID_CHAR16:
12744       type = char16_type_node;
12745       break;
12746     case RID_CHAR32:
12747       type = char32_type_node;
12748       break;
12749     case RID_WCHAR:
12750       type = wchar_type_node;
12751       break;
12752     case RID_BOOL:
12753       type = boolean_type_node;
12754       break;
12755     case RID_SHORT:
12756       if (decl_specs)
12757         ++decl_specs->specs[(int) ds_short];
12758       type = short_integer_type_node;
12759       break;
12760     case RID_INT:
12761       if (decl_specs)
12762         decl_specs->explicit_int_p = true;
12763       type = integer_type_node;
12764       break;
12765     case RID_INT128:
12766       if (!int128_integer_type_node)
12767         break;
12768       if (decl_specs)
12769         decl_specs->explicit_int128_p = true;
12770       type = int128_integer_type_node;
12771       break;
12772     case RID_LONG:
12773       if (decl_specs)
12774         ++decl_specs->specs[(int) ds_long];
12775       type = long_integer_type_node;
12776       break;
12777     case RID_SIGNED:
12778       if (decl_specs)
12779         ++decl_specs->specs[(int) ds_signed];
12780       type = integer_type_node;
12781       break;
12782     case RID_UNSIGNED:
12783       if (decl_specs)
12784         ++decl_specs->specs[(int) ds_unsigned];
12785       type = unsigned_type_node;
12786       break;
12787     case RID_FLOAT:
12788       type = float_type_node;
12789       break;
12790     case RID_DOUBLE:
12791       type = double_type_node;
12792       break;
12793     case RID_VOID:
12794       type = void_type_node;
12795       break;
12796       
12797     case RID_AUTO:
12798       maybe_warn_cpp0x (CPP0X_AUTO);
12799       type = make_auto ();
12800       break;
12801
12802     case RID_DECLTYPE:
12803       /* Since DR 743, decltype can either be a simple-type-specifier by
12804          itself or begin a nested-name-specifier.  Parsing it will replace
12805          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
12806          handling below decide what to do.  */
12807       cp_parser_decltype (parser);
12808       cp_lexer_set_token_position (parser->lexer, token);
12809       break;
12810
12811     case RID_TYPEOF:
12812       /* Consume the `typeof' token.  */
12813       cp_lexer_consume_token (parser->lexer);
12814       /* Parse the operand to `typeof'.  */
12815       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12816       /* If it is not already a TYPE, take its type.  */
12817       if (!TYPE_P (type))
12818         type = finish_typeof (type);
12819
12820       if (decl_specs)
12821         cp_parser_set_decl_spec_type (decl_specs, type,
12822                                       token->location,
12823                                       /*user_defined_p=*/true);
12824
12825       return type;
12826
12827     case RID_UNDERLYING_TYPE:
12828       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12829
12830       if (decl_specs)
12831         cp_parser_set_decl_spec_type (decl_specs, type,
12832                                       token->location,
12833                                       /*user_defined_p=*/true);
12834
12835       return type;
12836
12837     default:
12838       break;
12839     }
12840
12841   /* If token is an already-parsed decltype not followed by ::,
12842      it's a simple-type-specifier.  */
12843   if (token->type == CPP_DECLTYPE
12844       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
12845     {
12846       type = token->u.value;
12847       if (decl_specs)
12848         cp_parser_set_decl_spec_type (decl_specs, type,
12849                                       token->location,
12850                                       /*user_defined_p=*/true);
12851       cp_lexer_consume_token (parser->lexer);
12852       return type;
12853     }
12854
12855   /* If the type-specifier was for a built-in type, we're done.  */
12856   if (type)
12857     {
12858       /* Record the type.  */
12859       if (decl_specs
12860           && (token->keyword != RID_SIGNED
12861               && token->keyword != RID_UNSIGNED
12862               && token->keyword != RID_SHORT
12863               && token->keyword != RID_LONG))
12864         cp_parser_set_decl_spec_type (decl_specs,
12865                                       type,
12866                                       token->location,
12867                                       /*user_defined=*/false);
12868       if (decl_specs)
12869         decl_specs->any_specifiers_p = true;
12870
12871       /* Consume the token.  */
12872       cp_lexer_consume_token (parser->lexer);
12873
12874       /* There is no valid C++ program where a non-template type is
12875          followed by a "<".  That usually indicates that the user thought
12876          that the type was a template.  */
12877       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12878
12879       return TYPE_NAME (type);
12880     }
12881
12882   /* The type-specifier must be a user-defined type.  */
12883   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12884     {
12885       bool qualified_p;
12886       bool global_p;
12887
12888       /* Don't gobble tokens or issue error messages if this is an
12889          optional type-specifier.  */
12890       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12891         cp_parser_parse_tentatively (parser);
12892
12893       /* Look for the optional `::' operator.  */
12894       global_p
12895         = (cp_parser_global_scope_opt (parser,
12896                                        /*current_scope_valid_p=*/false)
12897            != NULL_TREE);
12898       /* Look for the nested-name specifier.  */
12899       qualified_p
12900         = (cp_parser_nested_name_specifier_opt (parser,
12901                                                 /*typename_keyword_p=*/false,
12902                                                 /*check_dependency_p=*/true,
12903                                                 /*type_p=*/false,
12904                                                 /*is_declaration=*/false)
12905            != NULL_TREE);
12906       token = cp_lexer_peek_token (parser->lexer);
12907       /* If we have seen a nested-name-specifier, and the next token
12908          is `template', then we are using the template-id production.  */
12909       if (parser->scope
12910           && cp_parser_optional_template_keyword (parser))
12911         {
12912           /* Look for the template-id.  */
12913           type = cp_parser_template_id (parser,
12914                                         /*template_keyword_p=*/true,
12915                                         /*check_dependency_p=*/true,
12916                                         /*is_declaration=*/false);
12917           /* If the template-id did not name a type, we are out of
12918              luck.  */
12919           if (TREE_CODE (type) != TYPE_DECL)
12920             {
12921               cp_parser_error (parser, "expected template-id for type");
12922               type = NULL_TREE;
12923             }
12924         }
12925       /* Otherwise, look for a type-name.  */
12926       else
12927         type = cp_parser_type_name (parser);
12928       /* Keep track of all name-lookups performed in class scopes.  */
12929       if (type
12930           && !global_p
12931           && !qualified_p
12932           && TREE_CODE (type) == TYPE_DECL
12933           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12934         maybe_note_name_used_in_class (DECL_NAME (type), type);
12935       /* If it didn't work out, we don't have a TYPE.  */
12936       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12937           && !cp_parser_parse_definitely (parser))
12938         type = NULL_TREE;
12939       if (type && decl_specs)
12940         cp_parser_set_decl_spec_type (decl_specs, type,
12941                                       token->location,
12942                                       /*user_defined=*/true);
12943     }
12944
12945   /* If we didn't get a type-name, issue an error message.  */
12946   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12947     {
12948       cp_parser_error (parser, "expected type-name");
12949       return error_mark_node;
12950     }
12951
12952   if (type && type != error_mark_node)
12953     {
12954       /* See if TYPE is an Objective-C type, and if so, parse and
12955          accept any protocol references following it.  Do this before
12956          the cp_parser_check_for_invalid_template_id() call, because
12957          Objective-C types can be followed by '<...>' which would
12958          enclose protocol names rather than template arguments, and so
12959          everything is fine.  */
12960       if (c_dialect_objc () && !parser->scope
12961           && (objc_is_id (type) || objc_is_class_name (type)))
12962         {
12963           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12964           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12965
12966           /* Clobber the "unqualified" type previously entered into
12967              DECL_SPECS with the new, improved protocol-qualified version.  */
12968           if (decl_specs)
12969             decl_specs->type = qual_type;
12970
12971           return qual_type;
12972         }
12973
12974       /* There is no valid C++ program where a non-template type is
12975          followed by a "<".  That usually indicates that the user
12976          thought that the type was a template.  */
12977       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12978                                                token->location);
12979     }
12980
12981   return type;
12982 }
12983
12984 /* Parse a type-name.
12985
12986    type-name:
12987      class-name
12988      enum-name
12989      typedef-name
12990
12991    enum-name:
12992      identifier
12993
12994    typedef-name:
12995      identifier
12996
12997    Returns a TYPE_DECL for the type.  */
12998
12999 static tree
13000 cp_parser_type_name (cp_parser* parser)
13001 {
13002   tree type_decl;
13003
13004   /* We can't know yet whether it is a class-name or not.  */
13005   cp_parser_parse_tentatively (parser);
13006   /* Try a class-name.  */
13007   type_decl = cp_parser_class_name (parser,
13008                                     /*typename_keyword_p=*/false,
13009                                     /*template_keyword_p=*/false,
13010                                     none_type,
13011                                     /*check_dependency_p=*/true,
13012                                     /*class_head_p=*/false,
13013                                     /*is_declaration=*/false);
13014   /* If it's not a class-name, keep looking.  */
13015   if (!cp_parser_parse_definitely (parser))
13016     {
13017       /* It must be a typedef-name or an enum-name.  */
13018       return cp_parser_nonclass_name (parser);
13019     }
13020
13021   return type_decl;
13022 }
13023
13024 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13025
13026    enum-name:
13027      identifier
13028
13029    typedef-name:
13030      identifier
13031
13032    Returns a TYPE_DECL for the type.  */
13033
13034 static tree
13035 cp_parser_nonclass_name (cp_parser* parser)
13036 {
13037   tree type_decl;
13038   tree identifier;
13039
13040   cp_token *token = cp_lexer_peek_token (parser->lexer);
13041   identifier = cp_parser_identifier (parser);
13042   if (identifier == error_mark_node)
13043     return error_mark_node;
13044
13045   /* Look up the type-name.  */
13046   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13047
13048   if (TREE_CODE (type_decl) != TYPE_DECL
13049       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13050     {
13051       /* See if this is an Objective-C type.  */
13052       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13053       tree type = objc_get_protocol_qualified_type (identifier, protos);
13054       if (type)
13055         type_decl = TYPE_NAME (type);
13056     }
13057
13058   /* Issue an error if we did not find a type-name.  */
13059   if (TREE_CODE (type_decl) != TYPE_DECL
13060       /* In Objective-C, we have the complication that class names are
13061          normally type names and start declarations (eg, the
13062          "NSObject" in "NSObject *object;"), but can be used in an
13063          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13064          is an expression.  So, a classname followed by a dot is not a
13065          valid type-name.  */
13066       || (objc_is_class_name (TREE_TYPE (type_decl))
13067           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13068     {
13069       if (!cp_parser_simulate_error (parser))
13070         cp_parser_name_lookup_error (parser, identifier, type_decl,
13071                                      NLE_TYPE, token->location);
13072       return error_mark_node;
13073     }
13074   /* Remember that the name was used in the definition of the
13075      current class so that we can check later to see if the
13076      meaning would have been different after the class was
13077      entirely defined.  */
13078   else if (type_decl != error_mark_node
13079            && !parser->scope)
13080     maybe_note_name_used_in_class (identifier, type_decl);
13081   
13082   return type_decl;
13083 }
13084
13085 /* Parse an elaborated-type-specifier.  Note that the grammar given
13086    here incorporates the resolution to DR68.
13087
13088    elaborated-type-specifier:
13089      class-key :: [opt] nested-name-specifier [opt] identifier
13090      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13091      enum-key :: [opt] nested-name-specifier [opt] identifier
13092      typename :: [opt] nested-name-specifier identifier
13093      typename :: [opt] nested-name-specifier template [opt]
13094        template-id
13095
13096    GNU extension:
13097
13098    elaborated-type-specifier:
13099      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13100      class-key attributes :: [opt] nested-name-specifier [opt]
13101                template [opt] template-id
13102      enum attributes :: [opt] nested-name-specifier [opt] identifier
13103
13104    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13105    declared `friend'.  If IS_DECLARATION is TRUE, then this
13106    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13107    something is being declared.
13108
13109    Returns the TYPE specified.  */
13110
13111 static tree
13112 cp_parser_elaborated_type_specifier (cp_parser* parser,
13113                                      bool is_friend,
13114                                      bool is_declaration)
13115 {
13116   enum tag_types tag_type;
13117   tree identifier;
13118   tree type = NULL_TREE;
13119   tree attributes = NULL_TREE;
13120   tree globalscope;
13121   cp_token *token = NULL;
13122
13123   /* See if we're looking at the `enum' keyword.  */
13124   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13125     {
13126       /* Consume the `enum' token.  */
13127       cp_lexer_consume_token (parser->lexer);
13128       /* Remember that it's an enumeration type.  */
13129       tag_type = enum_type;
13130       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13131          enums) is used here.  */
13132       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13133           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13134         {
13135             pedwarn (input_location, 0, "elaborated-type-specifier "
13136                       "for a scoped enum must not use the %<%D%> keyword",
13137                       cp_lexer_peek_token (parser->lexer)->u.value);
13138           /* Consume the `struct' or `class' and parse it anyway.  */
13139           cp_lexer_consume_token (parser->lexer);
13140         }
13141       /* Parse the attributes.  */
13142       attributes = cp_parser_attributes_opt (parser);
13143     }
13144   /* Or, it might be `typename'.  */
13145   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13146                                            RID_TYPENAME))
13147     {
13148       /* Consume the `typename' token.  */
13149       cp_lexer_consume_token (parser->lexer);
13150       /* Remember that it's a `typename' type.  */
13151       tag_type = typename_type;
13152     }
13153   /* Otherwise it must be a class-key.  */
13154   else
13155     {
13156       tag_type = cp_parser_class_key (parser);
13157       if (tag_type == none_type)
13158         return error_mark_node;
13159       /* Parse the attributes.  */
13160       attributes = cp_parser_attributes_opt (parser);
13161     }
13162
13163   /* Look for the `::' operator.  */
13164   globalscope =  cp_parser_global_scope_opt (parser,
13165                                              /*current_scope_valid_p=*/false);
13166   /* Look for the nested-name-specifier.  */
13167   if (tag_type == typename_type && !globalscope)
13168     {
13169       if (!cp_parser_nested_name_specifier (parser,
13170                                            /*typename_keyword_p=*/true,
13171                                            /*check_dependency_p=*/true,
13172                                            /*type_p=*/true,
13173                                             is_declaration))
13174         return error_mark_node;
13175     }
13176   else
13177     /* Even though `typename' is not present, the proposed resolution
13178        to Core Issue 180 says that in `class A<T>::B', `B' should be
13179        considered a type-name, even if `A<T>' is dependent.  */
13180     cp_parser_nested_name_specifier_opt (parser,
13181                                          /*typename_keyword_p=*/true,
13182                                          /*check_dependency_p=*/true,
13183                                          /*type_p=*/true,
13184                                          is_declaration);
13185  /* For everything but enumeration types, consider a template-id.
13186     For an enumeration type, consider only a plain identifier.  */
13187   if (tag_type != enum_type)
13188     {
13189       bool template_p = false;
13190       tree decl;
13191
13192       /* Allow the `template' keyword.  */
13193       template_p = cp_parser_optional_template_keyword (parser);
13194       /* If we didn't see `template', we don't know if there's a
13195          template-id or not.  */
13196       if (!template_p)
13197         cp_parser_parse_tentatively (parser);
13198       /* Parse the template-id.  */
13199       token = cp_lexer_peek_token (parser->lexer);
13200       decl = cp_parser_template_id (parser, template_p,
13201                                     /*check_dependency_p=*/true,
13202                                     is_declaration);
13203       /* If we didn't find a template-id, look for an ordinary
13204          identifier.  */
13205       if (!template_p && !cp_parser_parse_definitely (parser))
13206         ;
13207       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13208          in effect, then we must assume that, upon instantiation, the
13209          template will correspond to a class.  */
13210       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13211                && tag_type == typename_type)
13212         type = make_typename_type (parser->scope, decl,
13213                                    typename_type,
13214                                    /*complain=*/tf_error);
13215       /* If the `typename' keyword is in effect and DECL is not a type
13216          decl. Then type is non existant.   */
13217       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13218         type = NULL_TREE; 
13219       else 
13220         type = TREE_TYPE (decl);
13221     }
13222
13223   if (!type)
13224     {
13225       token = cp_lexer_peek_token (parser->lexer);
13226       identifier = cp_parser_identifier (parser);
13227
13228       if (identifier == error_mark_node)
13229         {
13230           parser->scope = NULL_TREE;
13231           return error_mark_node;
13232         }
13233
13234       /* For a `typename', we needn't call xref_tag.  */
13235       if (tag_type == typename_type
13236           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13237         return cp_parser_make_typename_type (parser, parser->scope,
13238                                              identifier,
13239                                              token->location);
13240       /* Look up a qualified name in the usual way.  */
13241       if (parser->scope)
13242         {
13243           tree decl;
13244           tree ambiguous_decls;
13245
13246           decl = cp_parser_lookup_name (parser, identifier,
13247                                         tag_type,
13248                                         /*is_template=*/false,
13249                                         /*is_namespace=*/false,
13250                                         /*check_dependency=*/true,
13251                                         &ambiguous_decls,
13252                                         token->location);
13253
13254           /* If the lookup was ambiguous, an error will already have been
13255              issued.  */
13256           if (ambiguous_decls)
13257             return error_mark_node;
13258
13259           /* If we are parsing friend declaration, DECL may be a
13260              TEMPLATE_DECL tree node here.  However, we need to check
13261              whether this TEMPLATE_DECL results in valid code.  Consider
13262              the following example:
13263
13264                namespace N {
13265                  template <class T> class C {};
13266                }
13267                class X {
13268                  template <class T> friend class N::C; // #1, valid code
13269                };
13270                template <class T> class Y {
13271                  friend class N::C;                    // #2, invalid code
13272                };
13273
13274              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13275              name lookup of `N::C'.  We see that friend declaration must
13276              be template for the code to be valid.  Note that
13277              processing_template_decl does not work here since it is
13278              always 1 for the above two cases.  */
13279
13280           decl = (cp_parser_maybe_treat_template_as_class
13281                   (decl, /*tag_name_p=*/is_friend
13282                          && parser->num_template_parameter_lists));
13283
13284           if (TREE_CODE (decl) != TYPE_DECL)
13285             {
13286               cp_parser_diagnose_invalid_type_name (parser,
13287                                                     parser->scope,
13288                                                     identifier,
13289                                                     token->location);
13290               return error_mark_node;
13291             }
13292
13293           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13294             {
13295               bool allow_template = (parser->num_template_parameter_lists
13296                                       || DECL_SELF_REFERENCE_P (decl));
13297               type = check_elaborated_type_specifier (tag_type, decl, 
13298                                                       allow_template);
13299
13300               if (type == error_mark_node)
13301                 return error_mark_node;
13302             }
13303
13304           /* Forward declarations of nested types, such as
13305
13306                class C1::C2;
13307                class C1::C2::C3;
13308
13309              are invalid unless all components preceding the final '::'
13310              are complete.  If all enclosing types are complete, these
13311              declarations become merely pointless.
13312
13313              Invalid forward declarations of nested types are errors
13314              caught elsewhere in parsing.  Those that are pointless arrive
13315              here.  */
13316
13317           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13318               && !is_friend && !processing_explicit_instantiation)
13319             warning (0, "declaration %qD does not declare anything", decl);
13320
13321           type = TREE_TYPE (decl);
13322         }
13323       else
13324         {
13325           /* An elaborated-type-specifier sometimes introduces a new type and
13326              sometimes names an existing type.  Normally, the rule is that it
13327              introduces a new type only if there is not an existing type of
13328              the same name already in scope.  For example, given:
13329
13330                struct S {};
13331                void f() { struct S s; }
13332
13333              the `struct S' in the body of `f' is the same `struct S' as in
13334              the global scope; the existing definition is used.  However, if
13335              there were no global declaration, this would introduce a new
13336              local class named `S'.
13337
13338              An exception to this rule applies to the following code:
13339
13340                namespace N { struct S; }
13341
13342              Here, the elaborated-type-specifier names a new type
13343              unconditionally; even if there is already an `S' in the
13344              containing scope this declaration names a new type.
13345              This exception only applies if the elaborated-type-specifier
13346              forms the complete declaration:
13347
13348                [class.name]
13349
13350                A declaration consisting solely of `class-key identifier ;' is
13351                either a redeclaration of the name in the current scope or a
13352                forward declaration of the identifier as a class name.  It
13353                introduces the name into the current scope.
13354
13355              We are in this situation precisely when the next token is a `;'.
13356
13357              An exception to the exception is that a `friend' declaration does
13358              *not* name a new type; i.e., given:
13359
13360                struct S { friend struct T; };
13361
13362              `T' is not a new type in the scope of `S'.
13363
13364              Also, `new struct S' or `sizeof (struct S)' never results in the
13365              definition of a new type; a new type can only be declared in a
13366              declaration context.  */
13367
13368           tag_scope ts;
13369           bool template_p;
13370
13371           if (is_friend)
13372             /* Friends have special name lookup rules.  */
13373             ts = ts_within_enclosing_non_class;
13374           else if (is_declaration
13375                    && cp_lexer_next_token_is (parser->lexer,
13376                                               CPP_SEMICOLON))
13377             /* This is a `class-key identifier ;' */
13378             ts = ts_current;
13379           else
13380             ts = ts_global;
13381
13382           template_p =
13383             (parser->num_template_parameter_lists
13384              && (cp_parser_next_token_starts_class_definition_p (parser)
13385                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13386           /* An unqualified name was used to reference this type, so
13387              there were no qualifying templates.  */
13388           if (!cp_parser_check_template_parameters (parser,
13389                                                     /*num_templates=*/0,
13390                                                     token->location,
13391                                                     /*declarator=*/NULL))
13392             return error_mark_node;
13393           type = xref_tag (tag_type, identifier, ts, template_p);
13394         }
13395     }
13396
13397   if (type == error_mark_node)
13398     return error_mark_node;
13399
13400   /* Allow attributes on forward declarations of classes.  */
13401   if (attributes)
13402     {
13403       if (TREE_CODE (type) == TYPENAME_TYPE)
13404         warning (OPT_Wattributes,
13405                  "attributes ignored on uninstantiated type");
13406       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13407                && ! processing_explicit_instantiation)
13408         warning (OPT_Wattributes,
13409                  "attributes ignored on template instantiation");
13410       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13411         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13412       else
13413         warning (OPT_Wattributes,
13414                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13415     }
13416
13417   if (tag_type != enum_type)
13418     cp_parser_check_class_key (tag_type, type);
13419
13420   /* A "<" cannot follow an elaborated type specifier.  If that
13421      happens, the user was probably trying to form a template-id.  */
13422   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13423
13424   return type;
13425 }
13426
13427 /* Parse an enum-specifier.
13428
13429    enum-specifier:
13430      enum-head { enumerator-list [opt] }
13431
13432    enum-head:
13433      enum-key identifier [opt] enum-base [opt]
13434      enum-key nested-name-specifier identifier enum-base [opt]
13435
13436    enum-key:
13437      enum
13438      enum class   [C++0x]
13439      enum struct  [C++0x]
13440
13441    enum-base:   [C++0x]
13442      : type-specifier-seq
13443
13444    opaque-enum-specifier:
13445      enum-key identifier enum-base [opt] ;
13446
13447    GNU Extensions:
13448      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13449        { enumerator-list [opt] }attributes[opt]
13450
13451    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13452    if the token stream isn't an enum-specifier after all.  */
13453
13454 static tree
13455 cp_parser_enum_specifier (cp_parser* parser)
13456 {
13457   tree identifier;
13458   tree type = NULL_TREE;
13459   tree prev_scope;
13460   tree nested_name_specifier = NULL_TREE;
13461   tree attributes;
13462   bool scoped_enum_p = false;
13463   bool has_underlying_type = false;
13464   bool nested_being_defined = false;
13465   bool new_value_list = false;
13466   bool is_new_type = false;
13467   bool is_anonymous = false;
13468   tree underlying_type = NULL_TREE;
13469   cp_token *type_start_token = NULL;
13470   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13471
13472   parser->colon_corrects_to_scope_p = false;
13473
13474   /* Parse tentatively so that we can back up if we don't find a
13475      enum-specifier.  */
13476   cp_parser_parse_tentatively (parser);
13477
13478   /* Caller guarantees that the current token is 'enum', an identifier
13479      possibly follows, and the token after that is an opening brace.
13480      If we don't have an identifier, fabricate an anonymous name for
13481      the enumeration being defined.  */
13482   cp_lexer_consume_token (parser->lexer);
13483
13484   /* Parse the "class" or "struct", which indicates a scoped
13485      enumeration type in C++0x.  */
13486   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13487       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13488     {
13489       if (cxx_dialect < cxx0x)
13490         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13491
13492       /* Consume the `struct' or `class' token.  */
13493       cp_lexer_consume_token (parser->lexer);
13494
13495       scoped_enum_p = true;
13496     }
13497
13498   attributes = cp_parser_attributes_opt (parser);
13499
13500   /* Clear the qualification.  */
13501   parser->scope = NULL_TREE;
13502   parser->qualifying_scope = NULL_TREE;
13503   parser->object_scope = NULL_TREE;
13504
13505   /* Figure out in what scope the declaration is being placed.  */
13506   prev_scope = current_scope ();
13507
13508   type_start_token = cp_lexer_peek_token (parser->lexer);
13509
13510   push_deferring_access_checks (dk_no_check);
13511   nested_name_specifier
13512       = cp_parser_nested_name_specifier_opt (parser,
13513                                              /*typename_keyword_p=*/true,
13514                                              /*check_dependency_p=*/false,
13515                                              /*type_p=*/false,
13516                                              /*is_declaration=*/false);
13517
13518   if (nested_name_specifier)
13519     {
13520       tree name;
13521
13522       identifier = cp_parser_identifier (parser);
13523       name =  cp_parser_lookup_name (parser, identifier,
13524                                      enum_type,
13525                                      /*is_template=*/false,
13526                                      /*is_namespace=*/false,
13527                                      /*check_dependency=*/true,
13528                                      /*ambiguous_decls=*/NULL,
13529                                      input_location);
13530       if (name)
13531         {
13532           type = TREE_TYPE (name);
13533           if (TREE_CODE (type) == TYPENAME_TYPE)
13534             {
13535               /* Are template enums allowed in ISO? */
13536               if (template_parm_scope_p ())
13537                 pedwarn (type_start_token->location, OPT_pedantic,
13538                          "%qD is an enumeration template", name);
13539               /* ignore a typename reference, for it will be solved by name
13540                  in start_enum.  */
13541               type = NULL_TREE;
13542             }
13543         }
13544       else
13545         error_at (type_start_token->location,
13546                   "%qD is not an enumerator-name", identifier);
13547     }
13548   else
13549     {
13550       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13551         identifier = cp_parser_identifier (parser);
13552       else
13553         {
13554           identifier = make_anon_name ();
13555           is_anonymous = true;
13556         }
13557     }
13558   pop_deferring_access_checks ();
13559
13560   /* Check for the `:' that denotes a specified underlying type in C++0x.
13561      Note that a ':' could also indicate a bitfield width, however.  */
13562   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13563     {
13564       cp_decl_specifier_seq type_specifiers;
13565
13566       /* Consume the `:'.  */
13567       cp_lexer_consume_token (parser->lexer);
13568
13569       /* Parse the type-specifier-seq.  */
13570       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13571                                     /*is_trailing_return=*/false,
13572                                     &type_specifiers);
13573
13574       /* At this point this is surely not elaborated type specifier.  */
13575       if (!cp_parser_parse_definitely (parser))
13576         return NULL_TREE;
13577
13578       if (cxx_dialect < cxx0x)
13579         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13580
13581       has_underlying_type = true;
13582
13583       /* If that didn't work, stop.  */
13584       if (type_specifiers.type != error_mark_node)
13585         {
13586           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13587                                             /*initialized=*/0, NULL);
13588           if (underlying_type == error_mark_node)
13589             underlying_type = NULL_TREE;
13590         }
13591     }
13592
13593   /* Look for the `{' but don't consume it yet.  */
13594   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13595     {
13596       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13597         {
13598           cp_parser_error (parser, "expected %<{%>");
13599           if (has_underlying_type)
13600             {
13601               type = NULL_TREE;
13602               goto out;
13603             }
13604         }
13605       /* An opaque-enum-specifier must have a ';' here.  */
13606       if ((scoped_enum_p || underlying_type)
13607           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13608         {
13609           cp_parser_error (parser, "expected %<;%> or %<{%>");
13610           if (has_underlying_type)
13611             {
13612               type = NULL_TREE;
13613               goto out;
13614             }
13615         }
13616     }
13617
13618   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13619     return NULL_TREE;
13620
13621   if (nested_name_specifier)
13622     {
13623       if (CLASS_TYPE_P (nested_name_specifier))
13624         {
13625           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13626           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13627           push_scope (nested_name_specifier);
13628         }
13629       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13630         {
13631           push_nested_namespace (nested_name_specifier);
13632         }
13633     }
13634
13635   /* Issue an error message if type-definitions are forbidden here.  */
13636   if (!cp_parser_check_type_definition (parser))
13637     type = error_mark_node;
13638   else
13639     /* Create the new type.  We do this before consuming the opening
13640        brace so the enum will be recorded as being on the line of its
13641        tag (or the 'enum' keyword, if there is no tag).  */
13642     type = start_enum (identifier, type, underlying_type,
13643                        scoped_enum_p, &is_new_type);
13644
13645   /* If the next token is not '{' it is an opaque-enum-specifier or an
13646      elaborated-type-specifier.  */
13647   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13648     {
13649       timevar_push (TV_PARSE_ENUM);
13650       if (nested_name_specifier)
13651         {
13652           /* The following catches invalid code such as:
13653              enum class S<int>::E { A, B, C }; */
13654           if (!processing_specialization
13655               && CLASS_TYPE_P (nested_name_specifier)
13656               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13657             error_at (type_start_token->location, "cannot add an enumerator "
13658                       "list to a template instantiation");
13659
13660           /* If that scope does not contain the scope in which the
13661              class was originally declared, the program is invalid.  */
13662           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13663             {
13664               if (at_namespace_scope_p ())
13665                 error_at (type_start_token->location,
13666                           "declaration of %qD in namespace %qD which does not "
13667                           "enclose %qD",
13668                           type, prev_scope, nested_name_specifier);
13669               else
13670                 error_at (type_start_token->location,
13671                           "declaration of %qD in %qD which does not enclose %qD",
13672                           type, prev_scope, nested_name_specifier);
13673               type = error_mark_node;
13674             }
13675         }
13676
13677       if (scoped_enum_p)
13678         begin_scope (sk_scoped_enum, type);
13679
13680       /* Consume the opening brace.  */
13681       cp_lexer_consume_token (parser->lexer);
13682
13683       if (type == error_mark_node)
13684         ; /* Nothing to add */
13685       else if (OPAQUE_ENUM_P (type)
13686                || (cxx_dialect > cxx98 && processing_specialization))
13687         {
13688           new_value_list = true;
13689           SET_OPAQUE_ENUM_P (type, false);
13690           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13691         }
13692       else
13693         {
13694           error_at (type_start_token->location, "multiple definition of %q#T", type);
13695           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13696                     "previous definition here");
13697           type = error_mark_node;
13698         }
13699
13700       if (type == error_mark_node)
13701         cp_parser_skip_to_end_of_block_or_statement (parser);
13702       /* If the next token is not '}', then there are some enumerators.  */
13703       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13704         cp_parser_enumerator_list (parser, type);
13705
13706       /* Consume the final '}'.  */
13707       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13708
13709       if (scoped_enum_p)
13710         finish_scope ();
13711       timevar_pop (TV_PARSE_ENUM);
13712     }
13713   else
13714     {
13715       /* If a ';' follows, then it is an opaque-enum-specifier
13716         and additional restrictions apply.  */
13717       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13718         {
13719           if (is_anonymous)
13720             error_at (type_start_token->location,
13721                       "opaque-enum-specifier without name");
13722           else if (nested_name_specifier)
13723             error_at (type_start_token->location,
13724                       "opaque-enum-specifier must use a simple identifier");
13725         }
13726     }
13727
13728   /* Look for trailing attributes to apply to this enumeration, and
13729      apply them if appropriate.  */
13730   if (cp_parser_allow_gnu_extensions_p (parser))
13731     {
13732       tree trailing_attr = cp_parser_attributes_opt (parser);
13733       trailing_attr = chainon (trailing_attr, attributes);
13734       cplus_decl_attributes (&type,
13735                              trailing_attr,
13736                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13737     }
13738
13739   /* Finish up the enumeration.  */
13740   if (type != error_mark_node)
13741     {
13742       if (new_value_list)
13743         finish_enum_value_list (type);
13744       if (is_new_type)
13745         finish_enum (type);
13746     }
13747
13748   if (nested_name_specifier)
13749     {
13750       if (CLASS_TYPE_P (nested_name_specifier))
13751         {
13752           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13753           pop_scope (nested_name_specifier);
13754         }
13755       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13756         {
13757           pop_nested_namespace (nested_name_specifier);
13758         }
13759     }
13760  out:
13761   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13762   return type;
13763 }
13764
13765 /* Parse an enumerator-list.  The enumerators all have the indicated
13766    TYPE.
13767
13768    enumerator-list:
13769      enumerator-definition
13770      enumerator-list , enumerator-definition  */
13771
13772 static void
13773 cp_parser_enumerator_list (cp_parser* parser, tree type)
13774 {
13775   while (true)
13776     {
13777       /* Parse an enumerator-definition.  */
13778       cp_parser_enumerator_definition (parser, type);
13779
13780       /* If the next token is not a ',', we've reached the end of
13781          the list.  */
13782       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13783         break;
13784       /* Otherwise, consume the `,' and keep going.  */
13785       cp_lexer_consume_token (parser->lexer);
13786       /* If the next token is a `}', there is a trailing comma.  */
13787       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13788         {
13789           if (!in_system_header)
13790             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13791           break;
13792         }
13793     }
13794 }
13795
13796 /* Parse an enumerator-definition.  The enumerator has the indicated
13797    TYPE.
13798
13799    enumerator-definition:
13800      enumerator
13801      enumerator = constant-expression
13802
13803    enumerator:
13804      identifier  */
13805
13806 static void
13807 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13808 {
13809   tree identifier;
13810   tree value;
13811   location_t loc;
13812
13813   /* Save the input location because we are interested in the location
13814      of the identifier and not the location of the explicit value.  */
13815   loc = cp_lexer_peek_token (parser->lexer)->location;
13816
13817   /* Look for the identifier.  */
13818   identifier = cp_parser_identifier (parser);
13819   if (identifier == error_mark_node)
13820     return;
13821
13822   /* If the next token is an '=', then there is an explicit value.  */
13823   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13824     {
13825       /* Consume the `=' token.  */
13826       cp_lexer_consume_token (parser->lexer);
13827       /* Parse the value.  */
13828       value = cp_parser_constant_expression (parser,
13829                                              /*allow_non_constant_p=*/false,
13830                                              NULL);
13831     }
13832   else
13833     value = NULL_TREE;
13834
13835   /* If we are processing a template, make sure the initializer of the
13836      enumerator doesn't contain any bare template parameter pack.  */
13837   if (check_for_bare_parameter_packs (value))
13838     value = error_mark_node;
13839
13840   /* integral_constant_value will pull out this expression, so make sure
13841      it's folded as appropriate.  */
13842   value = fold_non_dependent_expr (value);
13843
13844   /* Create the enumerator.  */
13845   build_enumerator (identifier, value, type, loc);
13846 }
13847
13848 /* Parse a namespace-name.
13849
13850    namespace-name:
13851      original-namespace-name
13852      namespace-alias
13853
13854    Returns the NAMESPACE_DECL for the namespace.  */
13855
13856 static tree
13857 cp_parser_namespace_name (cp_parser* parser)
13858 {
13859   tree identifier;
13860   tree namespace_decl;
13861
13862   cp_token *token = cp_lexer_peek_token (parser->lexer);
13863
13864   /* Get the name of the namespace.  */
13865   identifier = cp_parser_identifier (parser);
13866   if (identifier == error_mark_node)
13867     return error_mark_node;
13868
13869   /* Look up the identifier in the currently active scope.  Look only
13870      for namespaces, due to:
13871
13872        [basic.lookup.udir]
13873
13874        When looking up a namespace-name in a using-directive or alias
13875        definition, only namespace names are considered.
13876
13877      And:
13878
13879        [basic.lookup.qual]
13880
13881        During the lookup of a name preceding the :: scope resolution
13882        operator, object, function, and enumerator names are ignored.
13883
13884      (Note that cp_parser_qualifying_entity only calls this
13885      function if the token after the name is the scope resolution
13886      operator.)  */
13887   namespace_decl = cp_parser_lookup_name (parser, identifier,
13888                                           none_type,
13889                                           /*is_template=*/false,
13890                                           /*is_namespace=*/true,
13891                                           /*check_dependency=*/true,
13892                                           /*ambiguous_decls=*/NULL,
13893                                           token->location);
13894   /* If it's not a namespace, issue an error.  */
13895   if (namespace_decl == error_mark_node
13896       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13897     {
13898       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13899         error_at (token->location, "%qD is not a namespace-name", identifier);
13900       cp_parser_error (parser, "expected namespace-name");
13901       namespace_decl = error_mark_node;
13902     }
13903
13904   return namespace_decl;
13905 }
13906
13907 /* Parse a namespace-definition.
13908
13909    namespace-definition:
13910      named-namespace-definition
13911      unnamed-namespace-definition
13912
13913    named-namespace-definition:
13914      original-namespace-definition
13915      extension-namespace-definition
13916
13917    original-namespace-definition:
13918      namespace identifier { namespace-body }
13919
13920    extension-namespace-definition:
13921      namespace original-namespace-name { namespace-body }
13922
13923    unnamed-namespace-definition:
13924      namespace { namespace-body } */
13925
13926 static void
13927 cp_parser_namespace_definition (cp_parser* parser)
13928 {
13929   tree identifier, attribs;
13930   bool has_visibility;
13931   bool is_inline;
13932
13933   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13934     {
13935       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13936       is_inline = true;
13937       cp_lexer_consume_token (parser->lexer);
13938     }
13939   else
13940     is_inline = false;
13941
13942   /* Look for the `namespace' keyword.  */
13943   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13944
13945   /* Get the name of the namespace.  We do not attempt to distinguish
13946      between an original-namespace-definition and an
13947      extension-namespace-definition at this point.  The semantic
13948      analysis routines are responsible for that.  */
13949   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13950     identifier = cp_parser_identifier (parser);
13951   else
13952     identifier = NULL_TREE;
13953
13954   /* Parse any specified attributes.  */
13955   attribs = cp_parser_attributes_opt (parser);
13956
13957   /* Look for the `{' to start the namespace.  */
13958   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13959   /* Start the namespace.  */
13960   push_namespace (identifier);
13961
13962   /* "inline namespace" is equivalent to a stub namespace definition
13963      followed by a strong using directive.  */
13964   if (is_inline)
13965     {
13966       tree name_space = current_namespace;
13967       /* Set up namespace association.  */
13968       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13969         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13970                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13971       /* Import the contents of the inline namespace.  */
13972       pop_namespace ();
13973       do_using_directive (name_space);
13974       push_namespace (identifier);
13975     }
13976
13977   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13978
13979   /* Parse the body of the namespace.  */
13980   cp_parser_namespace_body (parser);
13981
13982   if (has_visibility)
13983     pop_visibility (1);
13984
13985   /* Finish the namespace.  */
13986   pop_namespace ();
13987   /* Look for the final `}'.  */
13988   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13989 }
13990
13991 /* Parse a namespace-body.
13992
13993    namespace-body:
13994      declaration-seq [opt]  */
13995
13996 static void
13997 cp_parser_namespace_body (cp_parser* parser)
13998 {
13999   cp_parser_declaration_seq_opt (parser);
14000 }
14001
14002 /* Parse a namespace-alias-definition.
14003
14004    namespace-alias-definition:
14005      namespace identifier = qualified-namespace-specifier ;  */
14006
14007 static void
14008 cp_parser_namespace_alias_definition (cp_parser* parser)
14009 {
14010   tree identifier;
14011   tree namespace_specifier;
14012
14013   cp_token *token = cp_lexer_peek_token (parser->lexer);
14014
14015   /* Look for the `namespace' keyword.  */
14016   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14017   /* Look for the identifier.  */
14018   identifier = cp_parser_identifier (parser);
14019   if (identifier == error_mark_node)
14020     return;
14021   /* Look for the `=' token.  */
14022   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14023       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14024     {
14025       error_at (token->location, "%<namespace%> definition is not allowed here");
14026       /* Skip the definition.  */
14027       cp_lexer_consume_token (parser->lexer);
14028       if (cp_parser_skip_to_closing_brace (parser))
14029         cp_lexer_consume_token (parser->lexer);
14030       return;
14031     }
14032   cp_parser_require (parser, CPP_EQ, RT_EQ);
14033   /* Look for the qualified-namespace-specifier.  */
14034   namespace_specifier
14035     = cp_parser_qualified_namespace_specifier (parser);
14036   /* Look for the `;' token.  */
14037   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14038
14039   /* Register the alias in the symbol table.  */
14040   do_namespace_alias (identifier, namespace_specifier);
14041 }
14042
14043 /* Parse a qualified-namespace-specifier.
14044
14045    qualified-namespace-specifier:
14046      :: [opt] nested-name-specifier [opt] namespace-name
14047
14048    Returns a NAMESPACE_DECL corresponding to the specified
14049    namespace.  */
14050
14051 static tree
14052 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14053 {
14054   /* Look for the optional `::'.  */
14055   cp_parser_global_scope_opt (parser,
14056                               /*current_scope_valid_p=*/false);
14057
14058   /* Look for the optional nested-name-specifier.  */
14059   cp_parser_nested_name_specifier_opt (parser,
14060                                        /*typename_keyword_p=*/false,
14061                                        /*check_dependency_p=*/true,
14062                                        /*type_p=*/false,
14063                                        /*is_declaration=*/true);
14064
14065   return cp_parser_namespace_name (parser);
14066 }
14067
14068 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14069    access declaration.
14070
14071    using-declaration:
14072      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14073      using :: unqualified-id ;  
14074
14075    access-declaration:
14076      qualified-id ;  
14077
14078    */
14079
14080 static bool
14081 cp_parser_using_declaration (cp_parser* parser, 
14082                              bool access_declaration_p)
14083 {
14084   cp_token *token;
14085   bool typename_p = false;
14086   bool global_scope_p;
14087   tree decl;
14088   tree identifier;
14089   tree qscope;
14090
14091   if (access_declaration_p)
14092     cp_parser_parse_tentatively (parser);
14093   else
14094     {
14095       /* Look for the `using' keyword.  */
14096       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14097       
14098       /* Peek at the next token.  */
14099       token = cp_lexer_peek_token (parser->lexer);
14100       /* See if it's `typename'.  */
14101       if (token->keyword == RID_TYPENAME)
14102         {
14103           /* Remember that we've seen it.  */
14104           typename_p = true;
14105           /* Consume the `typename' token.  */
14106           cp_lexer_consume_token (parser->lexer);
14107         }
14108     }
14109
14110   /* Look for the optional global scope qualification.  */
14111   global_scope_p
14112     = (cp_parser_global_scope_opt (parser,
14113                                    /*current_scope_valid_p=*/false)
14114        != NULL_TREE);
14115
14116   /* If we saw `typename', or didn't see `::', then there must be a
14117      nested-name-specifier present.  */
14118   if (typename_p || !global_scope_p)
14119     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14120                                               /*check_dependency_p=*/true,
14121                                               /*type_p=*/false,
14122                                               /*is_declaration=*/true);
14123   /* Otherwise, we could be in either of the two productions.  In that
14124      case, treat the nested-name-specifier as optional.  */
14125   else
14126     qscope = cp_parser_nested_name_specifier_opt (parser,
14127                                                   /*typename_keyword_p=*/false,
14128                                                   /*check_dependency_p=*/true,
14129                                                   /*type_p=*/false,
14130                                                   /*is_declaration=*/true);
14131   if (!qscope)
14132     qscope = global_namespace;
14133
14134   if (access_declaration_p && cp_parser_error_occurred (parser))
14135     /* Something has already gone wrong; there's no need to parse
14136        further.  Since an error has occurred, the return value of
14137        cp_parser_parse_definitely will be false, as required.  */
14138     return cp_parser_parse_definitely (parser);
14139
14140   token = cp_lexer_peek_token (parser->lexer);
14141   /* Parse the unqualified-id.  */
14142   identifier = cp_parser_unqualified_id (parser,
14143                                          /*template_keyword_p=*/false,
14144                                          /*check_dependency_p=*/true,
14145                                          /*declarator_p=*/true,
14146                                          /*optional_p=*/false);
14147
14148   if (access_declaration_p)
14149     {
14150       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14151         cp_parser_simulate_error (parser);
14152       if (!cp_parser_parse_definitely (parser))
14153         return false;
14154     }
14155
14156   /* The function we call to handle a using-declaration is different
14157      depending on what scope we are in.  */
14158   if (qscope == error_mark_node || identifier == error_mark_node)
14159     ;
14160   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14161            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14162     /* [namespace.udecl]
14163
14164        A using declaration shall not name a template-id.  */
14165     error_at (token->location,
14166               "a template-id may not appear in a using-declaration");
14167   else
14168     {
14169       if (at_class_scope_p ())
14170         {
14171           /* Create the USING_DECL.  */
14172           decl = do_class_using_decl (parser->scope, identifier);
14173
14174           if (check_for_bare_parameter_packs (decl))
14175             return false;
14176           else
14177             /* Add it to the list of members in this class.  */
14178             finish_member_declaration (decl);
14179         }
14180       else
14181         {
14182           decl = cp_parser_lookup_name_simple (parser,
14183                                                identifier,
14184                                                token->location);
14185           if (decl == error_mark_node)
14186             cp_parser_name_lookup_error (parser, identifier,
14187                                          decl, NLE_NULL,
14188                                          token->location);
14189           else if (check_for_bare_parameter_packs (decl))
14190             return false;
14191           else if (!at_namespace_scope_p ())
14192             do_local_using_decl (decl, qscope, identifier);
14193           else
14194             do_toplevel_using_decl (decl, qscope, identifier);
14195         }
14196     }
14197
14198   /* Look for the final `;'.  */
14199   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14200   
14201   return true;
14202 }
14203
14204 /* Parse a using-directive.
14205
14206    using-directive:
14207      using namespace :: [opt] nested-name-specifier [opt]
14208        namespace-name ;  */
14209
14210 static void
14211 cp_parser_using_directive (cp_parser* parser)
14212 {
14213   tree namespace_decl;
14214   tree attribs;
14215
14216   /* Look for the `using' keyword.  */
14217   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14218   /* And the `namespace' keyword.  */
14219   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14220   /* Look for the optional `::' operator.  */
14221   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14222   /* And the optional nested-name-specifier.  */
14223   cp_parser_nested_name_specifier_opt (parser,
14224                                        /*typename_keyword_p=*/false,
14225                                        /*check_dependency_p=*/true,
14226                                        /*type_p=*/false,
14227                                        /*is_declaration=*/true);
14228   /* Get the namespace being used.  */
14229   namespace_decl = cp_parser_namespace_name (parser);
14230   /* And any specified attributes.  */
14231   attribs = cp_parser_attributes_opt (parser);
14232   /* Update the symbol table.  */
14233   parse_using_directive (namespace_decl, attribs);
14234   /* Look for the final `;'.  */
14235   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14236 }
14237
14238 /* Parse an asm-definition.
14239
14240    asm-definition:
14241      asm ( string-literal ) ;
14242
14243    GNU Extension:
14244
14245    asm-definition:
14246      asm volatile [opt] ( string-literal ) ;
14247      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14248      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14249                           : asm-operand-list [opt] ) ;
14250      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14251                           : asm-operand-list [opt]
14252                           : asm-clobber-list [opt] ) ;
14253      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14254                                : asm-clobber-list [opt]
14255                                : asm-goto-list ) ;  */
14256
14257 static void
14258 cp_parser_asm_definition (cp_parser* parser)
14259 {
14260   tree string;
14261   tree outputs = NULL_TREE;
14262   tree inputs = NULL_TREE;
14263   tree clobbers = NULL_TREE;
14264   tree labels = NULL_TREE;
14265   tree asm_stmt;
14266   bool volatile_p = false;
14267   bool extended_p = false;
14268   bool invalid_inputs_p = false;
14269   bool invalid_outputs_p = false;
14270   bool goto_p = false;
14271   required_token missing = RT_NONE;
14272
14273   /* Look for the `asm' keyword.  */
14274   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14275   /* See if the next token is `volatile'.  */
14276   if (cp_parser_allow_gnu_extensions_p (parser)
14277       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14278     {
14279       /* Remember that we saw the `volatile' keyword.  */
14280       volatile_p = true;
14281       /* Consume the token.  */
14282       cp_lexer_consume_token (parser->lexer);
14283     }
14284   if (cp_parser_allow_gnu_extensions_p (parser)
14285       && parser->in_function_body
14286       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14287     {
14288       /* Remember that we saw the `goto' keyword.  */
14289       goto_p = true;
14290       /* Consume the token.  */
14291       cp_lexer_consume_token (parser->lexer);
14292     }
14293   /* Look for the opening `('.  */
14294   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14295     return;
14296   /* Look for the string.  */
14297   string = cp_parser_string_literal (parser, false, false);
14298   if (string == error_mark_node)
14299     {
14300       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14301                                              /*consume_paren=*/true);
14302       return;
14303     }
14304
14305   /* If we're allowing GNU extensions, check for the extended assembly
14306      syntax.  Unfortunately, the `:' tokens need not be separated by
14307      a space in C, and so, for compatibility, we tolerate that here
14308      too.  Doing that means that we have to treat the `::' operator as
14309      two `:' tokens.  */
14310   if (cp_parser_allow_gnu_extensions_p (parser)
14311       && parser->in_function_body
14312       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14313           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14314     {
14315       bool inputs_p = false;
14316       bool clobbers_p = false;
14317       bool labels_p = false;
14318
14319       /* The extended syntax was used.  */
14320       extended_p = true;
14321
14322       /* Look for outputs.  */
14323       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14324         {
14325           /* Consume the `:'.  */
14326           cp_lexer_consume_token (parser->lexer);
14327           /* Parse the output-operands.  */
14328           if (cp_lexer_next_token_is_not (parser->lexer,
14329                                           CPP_COLON)
14330               && cp_lexer_next_token_is_not (parser->lexer,
14331                                              CPP_SCOPE)
14332               && cp_lexer_next_token_is_not (parser->lexer,
14333                                              CPP_CLOSE_PAREN)
14334               && !goto_p)
14335             outputs = cp_parser_asm_operand_list (parser);
14336
14337             if (outputs == error_mark_node)
14338               invalid_outputs_p = true;
14339         }
14340       /* If the next token is `::', there are no outputs, and the
14341          next token is the beginning of the inputs.  */
14342       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14343         /* The inputs are coming next.  */
14344         inputs_p = true;
14345
14346       /* Look for inputs.  */
14347       if (inputs_p
14348           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14349         {
14350           /* Consume the `:' or `::'.  */
14351           cp_lexer_consume_token (parser->lexer);
14352           /* Parse the output-operands.  */
14353           if (cp_lexer_next_token_is_not (parser->lexer,
14354                                           CPP_COLON)
14355               && cp_lexer_next_token_is_not (parser->lexer,
14356                                              CPP_SCOPE)
14357               && cp_lexer_next_token_is_not (parser->lexer,
14358                                              CPP_CLOSE_PAREN))
14359             inputs = cp_parser_asm_operand_list (parser);
14360
14361             if (inputs == error_mark_node)
14362               invalid_inputs_p = true;
14363         }
14364       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14365         /* The clobbers are coming next.  */
14366         clobbers_p = true;
14367
14368       /* Look for clobbers.  */
14369       if (clobbers_p
14370           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14371         {
14372           clobbers_p = true;
14373           /* Consume the `:' or `::'.  */
14374           cp_lexer_consume_token (parser->lexer);
14375           /* Parse the clobbers.  */
14376           if (cp_lexer_next_token_is_not (parser->lexer,
14377                                           CPP_COLON)
14378               && cp_lexer_next_token_is_not (parser->lexer,
14379                                              CPP_CLOSE_PAREN))
14380             clobbers = cp_parser_asm_clobber_list (parser);
14381         }
14382       else if (goto_p
14383                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14384         /* The labels are coming next.  */
14385         labels_p = true;
14386
14387       /* Look for labels.  */
14388       if (labels_p
14389           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14390         {
14391           labels_p = true;
14392           /* Consume the `:' or `::'.  */
14393           cp_lexer_consume_token (parser->lexer);
14394           /* Parse the labels.  */
14395           labels = cp_parser_asm_label_list (parser);
14396         }
14397
14398       if (goto_p && !labels_p)
14399         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14400     }
14401   else if (goto_p)
14402     missing = RT_COLON_SCOPE;
14403
14404   /* Look for the closing `)'.  */
14405   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14406                           missing ? missing : RT_CLOSE_PAREN))
14407     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14408                                            /*consume_paren=*/true);
14409   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14410
14411   if (!invalid_inputs_p && !invalid_outputs_p)
14412     {
14413       /* Create the ASM_EXPR.  */
14414       if (parser->in_function_body)
14415         {
14416           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14417                                       inputs, clobbers, labels);
14418           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14419           if (!extended_p)
14420             {
14421               tree temp = asm_stmt;
14422               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14423                 temp = TREE_OPERAND (temp, 0);
14424
14425               ASM_INPUT_P (temp) = 1;
14426             }
14427         }
14428       else
14429         cgraph_add_asm_node (string);
14430     }
14431 }
14432
14433 /* Declarators [gram.dcl.decl] */
14434
14435 /* Parse an init-declarator.
14436
14437    init-declarator:
14438      declarator initializer [opt]
14439
14440    GNU Extension:
14441
14442    init-declarator:
14443      declarator asm-specification [opt] attributes [opt] initializer [opt]
14444
14445    function-definition:
14446      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14447        function-body
14448      decl-specifier-seq [opt] declarator function-try-block
14449
14450    GNU Extension:
14451
14452    function-definition:
14453      __extension__ function-definition
14454
14455    The DECL_SPECIFIERS apply to this declarator.  Returns a
14456    representation of the entity declared.  If MEMBER_P is TRUE, then
14457    this declarator appears in a class scope.  The new DECL created by
14458    this declarator is returned.
14459
14460    The CHECKS are access checks that should be performed once we know
14461    what entity is being declared (and, therefore, what classes have
14462    befriended it).
14463
14464    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14465    for a function-definition here as well.  If the declarator is a
14466    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14467    be TRUE upon return.  By that point, the function-definition will
14468    have been completely parsed.
14469
14470    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14471    is FALSE.
14472
14473    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14474    parsed declaration if it is an uninitialized single declarator not followed
14475    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14476    if present, will not be consumed.  If returned, this declarator will be
14477    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14478
14479 static tree
14480 cp_parser_init_declarator (cp_parser* parser,
14481                            cp_decl_specifier_seq *decl_specifiers,
14482                            VEC (deferred_access_check,gc)* checks,
14483                            bool function_definition_allowed_p,
14484                            bool member_p,
14485                            int declares_class_or_enum,
14486                            bool* function_definition_p,
14487                            tree* maybe_range_for_decl)
14488 {
14489   cp_token *token = NULL, *asm_spec_start_token = NULL,
14490            *attributes_start_token = NULL;
14491   cp_declarator *declarator;
14492   tree prefix_attributes;
14493   tree attributes;
14494   tree asm_specification;
14495   tree initializer;
14496   tree decl = NULL_TREE;
14497   tree scope;
14498   int is_initialized;
14499   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14500      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14501      "(...)".  */
14502   enum cpp_ttype initialization_kind;
14503   bool is_direct_init = false;
14504   bool is_non_constant_init;
14505   int ctor_dtor_or_conv_p;
14506   bool friend_p;
14507   tree pushed_scope = NULL_TREE;
14508   bool range_for_decl_p = false;
14509
14510   /* Gather the attributes that were provided with the
14511      decl-specifiers.  */
14512   prefix_attributes = decl_specifiers->attributes;
14513
14514   /* Assume that this is not the declarator for a function
14515      definition.  */
14516   if (function_definition_p)
14517     *function_definition_p = false;
14518
14519   /* Defer access checks while parsing the declarator; we cannot know
14520      what names are accessible until we know what is being
14521      declared.  */
14522   resume_deferring_access_checks ();
14523
14524   /* Parse the declarator.  */
14525   token = cp_lexer_peek_token (parser->lexer);
14526   declarator
14527     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14528                             &ctor_dtor_or_conv_p,
14529                             /*parenthesized_p=*/NULL,
14530                             member_p);
14531   /* Gather up the deferred checks.  */
14532   stop_deferring_access_checks ();
14533
14534   /* If the DECLARATOR was erroneous, there's no need to go
14535      further.  */
14536   if (declarator == cp_error_declarator)
14537     return error_mark_node;
14538
14539   /* Check that the number of template-parameter-lists is OK.  */
14540   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14541                                                        token->location))
14542     return error_mark_node;
14543
14544   if (declares_class_or_enum & 2)
14545     cp_parser_check_for_definition_in_return_type (declarator,
14546                                                    decl_specifiers->type,
14547                                                    decl_specifiers->type_location);
14548
14549   /* Figure out what scope the entity declared by the DECLARATOR is
14550      located in.  `grokdeclarator' sometimes changes the scope, so
14551      we compute it now.  */
14552   scope = get_scope_of_declarator (declarator);
14553
14554   /* Perform any lookups in the declared type which were thought to be
14555      dependent, but are not in the scope of the declarator.  */
14556   decl_specifiers->type
14557     = maybe_update_decl_type (decl_specifiers->type, scope);
14558
14559   /* If we're allowing GNU extensions, look for an asm-specification
14560      and attributes.  */
14561   if (cp_parser_allow_gnu_extensions_p (parser))
14562     {
14563       /* Look for an asm-specification.  */
14564       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14565       asm_specification = cp_parser_asm_specification_opt (parser);
14566       /* And attributes.  */
14567       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14568       attributes = cp_parser_attributes_opt (parser);
14569     }
14570   else
14571     {
14572       asm_specification = NULL_TREE;
14573       attributes = NULL_TREE;
14574     }
14575
14576   /* Peek at the next token.  */
14577   token = cp_lexer_peek_token (parser->lexer);
14578   /* Check to see if the token indicates the start of a
14579      function-definition.  */
14580   if (function_declarator_p (declarator)
14581       && cp_parser_token_starts_function_definition_p (token))
14582     {
14583       if (!function_definition_allowed_p)
14584         {
14585           /* If a function-definition should not appear here, issue an
14586              error message.  */
14587           cp_parser_error (parser,
14588                            "a function-definition is not allowed here");
14589           return error_mark_node;
14590         }
14591       else
14592         {
14593           location_t func_brace_location
14594             = cp_lexer_peek_token (parser->lexer)->location;
14595
14596           /* Neither attributes nor an asm-specification are allowed
14597              on a function-definition.  */
14598           if (asm_specification)
14599             error_at (asm_spec_start_token->location,
14600                       "an asm-specification is not allowed "
14601                       "on a function-definition");
14602           if (attributes)
14603             error_at (attributes_start_token->location,
14604                       "attributes are not allowed on a function-definition");
14605           /* This is a function-definition.  */
14606           *function_definition_p = true;
14607
14608           /* Parse the function definition.  */
14609           if (member_p)
14610             decl = cp_parser_save_member_function_body (parser,
14611                                                         decl_specifiers,
14612                                                         declarator,
14613                                                         prefix_attributes);
14614           else
14615             decl
14616               = (cp_parser_function_definition_from_specifiers_and_declarator
14617                  (parser, decl_specifiers, prefix_attributes, declarator));
14618
14619           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14620             {
14621               /* This is where the prologue starts...  */
14622               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14623                 = func_brace_location;
14624             }
14625
14626           return decl;
14627         }
14628     }
14629
14630   /* [dcl.dcl]
14631
14632      Only in function declarations for constructors, destructors, and
14633      type conversions can the decl-specifier-seq be omitted.
14634
14635      We explicitly postpone this check past the point where we handle
14636      function-definitions because we tolerate function-definitions
14637      that are missing their return types in some modes.  */
14638   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14639     {
14640       cp_parser_error (parser,
14641                        "expected constructor, destructor, or type conversion");
14642       return error_mark_node;
14643     }
14644
14645   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14646   if (token->type == CPP_EQ
14647       || token->type == CPP_OPEN_PAREN
14648       || token->type == CPP_OPEN_BRACE)
14649     {
14650       is_initialized = SD_INITIALIZED;
14651       initialization_kind = token->type;
14652       if (maybe_range_for_decl)
14653         *maybe_range_for_decl = error_mark_node;
14654
14655       if (token->type == CPP_EQ
14656           && function_declarator_p (declarator))
14657         {
14658           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14659           if (t2->keyword == RID_DEFAULT)
14660             is_initialized = SD_DEFAULTED;
14661           else if (t2->keyword == RID_DELETE)
14662             is_initialized = SD_DELETED;
14663         }
14664     }
14665   else
14666     {
14667       /* If the init-declarator isn't initialized and isn't followed by a
14668          `,' or `;', it's not a valid init-declarator.  */
14669       if (token->type != CPP_COMMA
14670           && token->type != CPP_SEMICOLON)
14671         {
14672           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14673             range_for_decl_p = true;
14674           else
14675             {
14676               cp_parser_error (parser, "expected initializer");
14677               return error_mark_node;
14678             }
14679         }
14680       is_initialized = SD_UNINITIALIZED;
14681       initialization_kind = CPP_EOF;
14682     }
14683
14684   /* Because start_decl has side-effects, we should only call it if we
14685      know we're going ahead.  By this point, we know that we cannot
14686      possibly be looking at any other construct.  */
14687   cp_parser_commit_to_tentative_parse (parser);
14688
14689   /* If the decl specifiers were bad, issue an error now that we're
14690      sure this was intended to be a declarator.  Then continue
14691      declaring the variable(s), as int, to try to cut down on further
14692      errors.  */
14693   if (decl_specifiers->any_specifiers_p
14694       && decl_specifiers->type == error_mark_node)
14695     {
14696       cp_parser_error (parser, "invalid type in declaration");
14697       decl_specifiers->type = integer_type_node;
14698     }
14699
14700   /* Check to see whether or not this declaration is a friend.  */
14701   friend_p = cp_parser_friend_p (decl_specifiers);
14702
14703   /* Enter the newly declared entry in the symbol table.  If we're
14704      processing a declaration in a class-specifier, we wait until
14705      after processing the initializer.  */
14706   if (!member_p)
14707     {
14708       if (parser->in_unbraced_linkage_specification_p)
14709         decl_specifiers->storage_class = sc_extern;
14710       decl = start_decl (declarator, decl_specifiers,
14711                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14712                          attributes, prefix_attributes,
14713                          &pushed_scope);
14714       /* Adjust location of decl if declarator->id_loc is more appropriate:
14715          set, and decl wasn't merged with another decl, in which case its
14716          location would be different from input_location, and more accurate.  */
14717       if (DECL_P (decl)
14718           && declarator->id_loc != UNKNOWN_LOCATION
14719           && DECL_SOURCE_LOCATION (decl) == input_location)
14720         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14721     }
14722   else if (scope)
14723     /* Enter the SCOPE.  That way unqualified names appearing in the
14724        initializer will be looked up in SCOPE.  */
14725     pushed_scope = push_scope (scope);
14726
14727   /* Perform deferred access control checks, now that we know in which
14728      SCOPE the declared entity resides.  */
14729   if (!member_p && decl)
14730     {
14731       tree saved_current_function_decl = NULL_TREE;
14732
14733       /* If the entity being declared is a function, pretend that we
14734          are in its scope.  If it is a `friend', it may have access to
14735          things that would not otherwise be accessible.  */
14736       if (TREE_CODE (decl) == FUNCTION_DECL)
14737         {
14738           saved_current_function_decl = current_function_decl;
14739           current_function_decl = decl;
14740         }
14741
14742       /* Perform access checks for template parameters.  */
14743       cp_parser_perform_template_parameter_access_checks (checks);
14744
14745       /* Perform the access control checks for the declarator and the
14746          decl-specifiers.  */
14747       perform_deferred_access_checks ();
14748
14749       /* Restore the saved value.  */
14750       if (TREE_CODE (decl) == FUNCTION_DECL)
14751         current_function_decl = saved_current_function_decl;
14752     }
14753
14754   /* Parse the initializer.  */
14755   initializer = NULL_TREE;
14756   is_direct_init = false;
14757   is_non_constant_init = true;
14758   if (is_initialized)
14759     {
14760       if (function_declarator_p (declarator))
14761         {
14762           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14763            if (initialization_kind == CPP_EQ)
14764              initializer = cp_parser_pure_specifier (parser);
14765            else
14766              {
14767                /* If the declaration was erroneous, we don't really
14768                   know what the user intended, so just silently
14769                   consume the initializer.  */
14770                if (decl != error_mark_node)
14771                  error_at (initializer_start_token->location,
14772                            "initializer provided for function");
14773                cp_parser_skip_to_closing_parenthesis (parser,
14774                                                       /*recovering=*/true,
14775                                                       /*or_comma=*/false,
14776                                                       /*consume_paren=*/true);
14777              }
14778         }
14779       else
14780         {
14781           /* We want to record the extra mangling scope for in-class
14782              initializers of class members and initializers of static data
14783              member templates.  The former is a C++0x feature which isn't
14784              implemented yet, and I expect it will involve deferring
14785              parsing of the initializer until end of class as with default
14786              arguments.  So right here we only handle the latter.  */
14787           if (!member_p && processing_template_decl)
14788             start_lambda_scope (decl);
14789           initializer = cp_parser_initializer (parser,
14790                                                &is_direct_init,
14791                                                &is_non_constant_init);
14792           if (!member_p && processing_template_decl)
14793             finish_lambda_scope ();
14794         }
14795     }
14796
14797   /* The old parser allows attributes to appear after a parenthesized
14798      initializer.  Mark Mitchell proposed removing this functionality
14799      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14800      attributes -- but ignores them.  */
14801   if (cp_parser_allow_gnu_extensions_p (parser)
14802       && initialization_kind == CPP_OPEN_PAREN)
14803     if (cp_parser_attributes_opt (parser))
14804       warning (OPT_Wattributes,
14805                "attributes after parenthesized initializer ignored");
14806
14807   /* For an in-class declaration, use `grokfield' to create the
14808      declaration.  */
14809   if (member_p)
14810     {
14811       if (pushed_scope)
14812         {
14813           pop_scope (pushed_scope);
14814           pushed_scope = NULL_TREE;
14815         }
14816       decl = grokfield (declarator, decl_specifiers,
14817                         initializer, !is_non_constant_init,
14818                         /*asmspec=*/NULL_TREE,
14819                         prefix_attributes);
14820       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14821         cp_parser_save_default_args (parser, decl);
14822     }
14823
14824   /* Finish processing the declaration.  But, skip member
14825      declarations.  */
14826   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14827     {
14828       cp_finish_decl (decl,
14829                       initializer, !is_non_constant_init,
14830                       asm_specification,
14831                       /* If the initializer is in parentheses, then this is
14832                          a direct-initialization, which means that an
14833                          `explicit' constructor is OK.  Otherwise, an
14834                          `explicit' constructor cannot be used.  */
14835                       ((is_direct_init || !is_initialized)
14836                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14837     }
14838   else if ((cxx_dialect != cxx98) && friend_p
14839            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14840     /* Core issue #226 (C++0x only): A default template-argument
14841        shall not be specified in a friend class template
14842        declaration. */
14843     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14844                              /*is_partial=*/0, /*is_friend_decl=*/1);
14845
14846   if (!friend_p && pushed_scope)
14847     pop_scope (pushed_scope);
14848
14849   return decl;
14850 }
14851
14852 /* Parse a declarator.
14853
14854    declarator:
14855      direct-declarator
14856      ptr-operator declarator
14857
14858    abstract-declarator:
14859      ptr-operator abstract-declarator [opt]
14860      direct-abstract-declarator
14861
14862    GNU Extensions:
14863
14864    declarator:
14865      attributes [opt] direct-declarator
14866      attributes [opt] ptr-operator declarator
14867
14868    abstract-declarator:
14869      attributes [opt] ptr-operator abstract-declarator [opt]
14870      attributes [opt] direct-abstract-declarator
14871
14872    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14873    detect constructor, destructor or conversion operators. It is set
14874    to -1 if the declarator is a name, and +1 if it is a
14875    function. Otherwise it is set to zero. Usually you just want to
14876    test for >0, but internally the negative value is used.
14877
14878    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14879    a decl-specifier-seq unless it declares a constructor, destructor,
14880    or conversion.  It might seem that we could check this condition in
14881    semantic analysis, rather than parsing, but that makes it difficult
14882    to handle something like `f()'.  We want to notice that there are
14883    no decl-specifiers, and therefore realize that this is an
14884    expression, not a declaration.)
14885
14886    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14887    the declarator is a direct-declarator of the form "(...)".
14888
14889    MEMBER_P is true iff this declarator is a member-declarator.  */
14890
14891 static cp_declarator *
14892 cp_parser_declarator (cp_parser* parser,
14893                       cp_parser_declarator_kind dcl_kind,
14894                       int* ctor_dtor_or_conv_p,
14895                       bool* parenthesized_p,
14896                       bool member_p)
14897 {
14898   cp_declarator *declarator;
14899   enum tree_code code;
14900   cp_cv_quals cv_quals;
14901   tree class_type;
14902   tree attributes = NULL_TREE;
14903
14904   /* Assume this is not a constructor, destructor, or type-conversion
14905      operator.  */
14906   if (ctor_dtor_or_conv_p)
14907     *ctor_dtor_or_conv_p = 0;
14908
14909   if (cp_parser_allow_gnu_extensions_p (parser))
14910     attributes = cp_parser_attributes_opt (parser);
14911
14912   /* Check for the ptr-operator production.  */
14913   cp_parser_parse_tentatively (parser);
14914   /* Parse the ptr-operator.  */
14915   code = cp_parser_ptr_operator (parser,
14916                                  &class_type,
14917                                  &cv_quals);
14918   /* If that worked, then we have a ptr-operator.  */
14919   if (cp_parser_parse_definitely (parser))
14920     {
14921       /* If a ptr-operator was found, then this declarator was not
14922          parenthesized.  */
14923       if (parenthesized_p)
14924         *parenthesized_p = true;
14925       /* The dependent declarator is optional if we are parsing an
14926          abstract-declarator.  */
14927       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14928         cp_parser_parse_tentatively (parser);
14929
14930       /* Parse the dependent declarator.  */
14931       declarator = cp_parser_declarator (parser, dcl_kind,
14932                                          /*ctor_dtor_or_conv_p=*/NULL,
14933                                          /*parenthesized_p=*/NULL,
14934                                          /*member_p=*/false);
14935
14936       /* If we are parsing an abstract-declarator, we must handle the
14937          case where the dependent declarator is absent.  */
14938       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14939           && !cp_parser_parse_definitely (parser))
14940         declarator = NULL;
14941
14942       declarator = cp_parser_make_indirect_declarator
14943         (code, class_type, cv_quals, declarator);
14944     }
14945   /* Everything else is a direct-declarator.  */
14946   else
14947     {
14948       if (parenthesized_p)
14949         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14950                                                    CPP_OPEN_PAREN);
14951       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14952                                                 ctor_dtor_or_conv_p,
14953                                                 member_p);
14954     }
14955
14956   if (attributes && declarator && declarator != cp_error_declarator)
14957     declarator->attributes = attributes;
14958
14959   return declarator;
14960 }
14961
14962 /* Parse a direct-declarator or direct-abstract-declarator.
14963
14964    direct-declarator:
14965      declarator-id
14966      direct-declarator ( parameter-declaration-clause )
14967        cv-qualifier-seq [opt]
14968        exception-specification [opt]
14969      direct-declarator [ constant-expression [opt] ]
14970      ( declarator )
14971
14972    direct-abstract-declarator:
14973      direct-abstract-declarator [opt]
14974        ( parameter-declaration-clause )
14975        cv-qualifier-seq [opt]
14976        exception-specification [opt]
14977      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14978      ( abstract-declarator )
14979
14980    Returns a representation of the declarator.  DCL_KIND is
14981    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14982    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14983    we are parsing a direct-declarator.  It is
14984    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14985    of ambiguity we prefer an abstract declarator, as per
14986    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14987    cp_parser_declarator.  */
14988
14989 static cp_declarator *
14990 cp_parser_direct_declarator (cp_parser* parser,
14991                              cp_parser_declarator_kind dcl_kind,
14992                              int* ctor_dtor_or_conv_p,
14993                              bool member_p)
14994 {
14995   cp_token *token;
14996   cp_declarator *declarator = NULL;
14997   tree scope = NULL_TREE;
14998   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14999   bool saved_in_declarator_p = parser->in_declarator_p;
15000   bool first = true;
15001   tree pushed_scope = NULL_TREE;
15002
15003   while (true)
15004     {
15005       /* Peek at the next token.  */
15006       token = cp_lexer_peek_token (parser->lexer);
15007       if (token->type == CPP_OPEN_PAREN)
15008         {
15009           /* This is either a parameter-declaration-clause, or a
15010              parenthesized declarator. When we know we are parsing a
15011              named declarator, it must be a parenthesized declarator
15012              if FIRST is true. For instance, `(int)' is a
15013              parameter-declaration-clause, with an omitted
15014              direct-abstract-declarator. But `((*))', is a
15015              parenthesized abstract declarator. Finally, when T is a
15016              template parameter `(T)' is a
15017              parameter-declaration-clause, and not a parenthesized
15018              named declarator.
15019
15020              We first try and parse a parameter-declaration-clause,
15021              and then try a nested declarator (if FIRST is true).
15022
15023              It is not an error for it not to be a
15024              parameter-declaration-clause, even when FIRST is
15025              false. Consider,
15026
15027                int i (int);
15028                int i (3);
15029
15030              The first is the declaration of a function while the
15031              second is the definition of a variable, including its
15032              initializer.
15033
15034              Having seen only the parenthesis, we cannot know which of
15035              these two alternatives should be selected.  Even more
15036              complex are examples like:
15037
15038                int i (int (a));
15039                int i (int (3));
15040
15041              The former is a function-declaration; the latter is a
15042              variable initialization.
15043
15044              Thus again, we try a parameter-declaration-clause, and if
15045              that fails, we back out and return.  */
15046
15047           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15048             {
15049               tree params;
15050               unsigned saved_num_template_parameter_lists;
15051               bool is_declarator = false;
15052               tree t;
15053
15054               /* In a member-declarator, the only valid interpretation
15055                  of a parenthesis is the start of a
15056                  parameter-declaration-clause.  (It is invalid to
15057                  initialize a static data member with a parenthesized
15058                  initializer; only the "=" form of initialization is
15059                  permitted.)  */
15060               if (!member_p)
15061                 cp_parser_parse_tentatively (parser);
15062
15063               /* Consume the `('.  */
15064               cp_lexer_consume_token (parser->lexer);
15065               if (first)
15066                 {
15067                   /* If this is going to be an abstract declarator, we're
15068                      in a declarator and we can't have default args.  */
15069                   parser->default_arg_ok_p = false;
15070                   parser->in_declarator_p = true;
15071                 }
15072
15073               /* Inside the function parameter list, surrounding
15074                  template-parameter-lists do not apply.  */
15075               saved_num_template_parameter_lists
15076                 = parser->num_template_parameter_lists;
15077               parser->num_template_parameter_lists = 0;
15078
15079               begin_scope (sk_function_parms, NULL_TREE);
15080
15081               /* Parse the parameter-declaration-clause.  */
15082               params = cp_parser_parameter_declaration_clause (parser);
15083
15084               parser->num_template_parameter_lists
15085                 = saved_num_template_parameter_lists;
15086
15087               /* Consume the `)'.  */
15088               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15089
15090               /* If all went well, parse the cv-qualifier-seq and the
15091                  exception-specification.  */
15092               if (member_p || cp_parser_parse_definitely (parser))
15093                 {
15094                   cp_cv_quals cv_quals;
15095                   cp_virt_specifiers virt_specifiers;
15096                   tree exception_specification;
15097                   tree late_return;
15098
15099                   is_declarator = true;
15100
15101                   if (ctor_dtor_or_conv_p)
15102                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15103                   first = false;
15104
15105                   /* Parse the cv-qualifier-seq.  */
15106                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15107                   /* And the exception-specification.  */
15108                   exception_specification
15109                     = cp_parser_exception_specification_opt (parser);
15110                   /* Parse the virt-specifier-seq.  */
15111                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15112
15113                   late_return = (cp_parser_late_return_type_opt
15114                                  (parser, member_p ? cv_quals : -1));
15115
15116                   /* Create the function-declarator.  */
15117                   declarator = make_call_declarator (declarator,
15118                                                      params,
15119                                                      cv_quals,
15120                                                      virt_specifiers,
15121                                                      exception_specification,
15122                                                      late_return);
15123                   /* Any subsequent parameter lists are to do with
15124                      return type, so are not those of the declared
15125                      function.  */
15126                   parser->default_arg_ok_p = false;
15127                 }
15128
15129               /* Remove the function parms from scope.  */
15130               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15131                 pop_binding (DECL_NAME (t), t);
15132               leave_scope();
15133
15134               if (is_declarator)
15135                 /* Repeat the main loop.  */
15136                 continue;
15137             }
15138
15139           /* If this is the first, we can try a parenthesized
15140              declarator.  */
15141           if (first)
15142             {
15143               bool saved_in_type_id_in_expr_p;
15144
15145               parser->default_arg_ok_p = saved_default_arg_ok_p;
15146               parser->in_declarator_p = saved_in_declarator_p;
15147
15148               /* Consume the `('.  */
15149               cp_lexer_consume_token (parser->lexer);
15150               /* Parse the nested declarator.  */
15151               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15152               parser->in_type_id_in_expr_p = true;
15153               declarator
15154                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15155                                         /*parenthesized_p=*/NULL,
15156                                         member_p);
15157               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15158               first = false;
15159               /* Expect a `)'.  */
15160               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15161                 declarator = cp_error_declarator;
15162               if (declarator == cp_error_declarator)
15163                 break;
15164
15165               goto handle_declarator;
15166             }
15167           /* Otherwise, we must be done.  */
15168           else
15169             break;
15170         }
15171       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15172                && token->type == CPP_OPEN_SQUARE)
15173         {
15174           /* Parse an array-declarator.  */
15175           tree bounds;
15176
15177           if (ctor_dtor_or_conv_p)
15178             *ctor_dtor_or_conv_p = 0;
15179
15180           first = false;
15181           parser->default_arg_ok_p = false;
15182           parser->in_declarator_p = true;
15183           /* Consume the `['.  */
15184           cp_lexer_consume_token (parser->lexer);
15185           /* Peek at the next token.  */
15186           token = cp_lexer_peek_token (parser->lexer);
15187           /* If the next token is `]', then there is no
15188              constant-expression.  */
15189           if (token->type != CPP_CLOSE_SQUARE)
15190             {
15191               bool non_constant_p;
15192
15193               bounds
15194                 = cp_parser_constant_expression (parser,
15195                                                  /*allow_non_constant=*/true,
15196                                                  &non_constant_p);
15197               if (!non_constant_p)
15198                 /* OK */;
15199               /* Normally, the array bound must be an integral constant
15200                  expression.  However, as an extension, we allow VLAs
15201                  in function scopes as long as they aren't part of a
15202                  parameter declaration.  */
15203               else if (!parser->in_function_body
15204                        || current_binding_level->kind == sk_function_parms)
15205                 {
15206                   cp_parser_error (parser,
15207                                    "array bound is not an integer constant");
15208                   bounds = error_mark_node;
15209                 }
15210               else if (processing_template_decl && !error_operand_p (bounds))
15211                 {
15212                   /* Remember this wasn't a constant-expression.  */
15213                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15214                   TREE_SIDE_EFFECTS (bounds) = 1;
15215                 }
15216             }
15217           else
15218             bounds = NULL_TREE;
15219           /* Look for the closing `]'.  */
15220           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15221             {
15222               declarator = cp_error_declarator;
15223               break;
15224             }
15225
15226           declarator = make_array_declarator (declarator, bounds);
15227         }
15228       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15229         {
15230           {
15231             tree qualifying_scope;
15232             tree unqualified_name;
15233             special_function_kind sfk;
15234             bool abstract_ok;
15235             bool pack_expansion_p = false;
15236             cp_token *declarator_id_start_token;
15237
15238             /* Parse a declarator-id */
15239             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15240             if (abstract_ok)
15241               {
15242                 cp_parser_parse_tentatively (parser);
15243
15244                 /* If we see an ellipsis, we should be looking at a
15245                    parameter pack. */
15246                 if (token->type == CPP_ELLIPSIS)
15247                   {
15248                     /* Consume the `...' */
15249                     cp_lexer_consume_token (parser->lexer);
15250
15251                     pack_expansion_p = true;
15252                   }
15253               }
15254
15255             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15256             unqualified_name
15257               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15258             qualifying_scope = parser->scope;
15259             if (abstract_ok)
15260               {
15261                 bool okay = false;
15262
15263                 if (!unqualified_name && pack_expansion_p)
15264                   {
15265                     /* Check whether an error occurred. */
15266                     okay = !cp_parser_error_occurred (parser);
15267
15268                     /* We already consumed the ellipsis to mark a
15269                        parameter pack, but we have no way to report it,
15270                        so abort the tentative parse. We will be exiting
15271                        immediately anyway. */
15272                     cp_parser_abort_tentative_parse (parser);
15273                   }
15274                 else
15275                   okay = cp_parser_parse_definitely (parser);
15276
15277                 if (!okay)
15278                   unqualified_name = error_mark_node;
15279                 else if (unqualified_name
15280                          && (qualifying_scope
15281                              || (TREE_CODE (unqualified_name)
15282                                  != IDENTIFIER_NODE)))
15283                   {
15284                     cp_parser_error (parser, "expected unqualified-id");
15285                     unqualified_name = error_mark_node;
15286                   }
15287               }
15288
15289             if (!unqualified_name)
15290               return NULL;
15291             if (unqualified_name == error_mark_node)
15292               {
15293                 declarator = cp_error_declarator;
15294                 pack_expansion_p = false;
15295                 declarator->parameter_pack_p = false;
15296                 break;
15297               }
15298
15299             if (qualifying_scope && at_namespace_scope_p ()
15300                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15301               {
15302                 /* In the declaration of a member of a template class
15303                    outside of the class itself, the SCOPE will sometimes
15304                    be a TYPENAME_TYPE.  For example, given:
15305
15306                    template <typename T>
15307                    int S<T>::R::i = 3;
15308
15309                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15310                    this context, we must resolve S<T>::R to an ordinary
15311                    type, rather than a typename type.
15312
15313                    The reason we normally avoid resolving TYPENAME_TYPEs
15314                    is that a specialization of `S' might render
15315                    `S<T>::R' not a type.  However, if `S' is
15316                    specialized, then this `i' will not be used, so there
15317                    is no harm in resolving the types here.  */
15318                 tree type;
15319
15320                 /* Resolve the TYPENAME_TYPE.  */
15321                 type = resolve_typename_type (qualifying_scope,
15322                                               /*only_current_p=*/false);
15323                 /* If that failed, the declarator is invalid.  */
15324                 if (TREE_CODE (type) == TYPENAME_TYPE)
15325                   {
15326                     if (typedef_variant_p (type))
15327                       error_at (declarator_id_start_token->location,
15328                                 "cannot define member of dependent typedef "
15329                                 "%qT", type);
15330                     else
15331                       error_at (declarator_id_start_token->location,
15332                                 "%<%T::%E%> is not a type",
15333                                 TYPE_CONTEXT (qualifying_scope),
15334                                 TYPE_IDENTIFIER (qualifying_scope));
15335                   }
15336                 qualifying_scope = type;
15337               }
15338
15339             sfk = sfk_none;
15340
15341             if (unqualified_name)
15342               {
15343                 tree class_type;
15344
15345                 if (qualifying_scope
15346                     && CLASS_TYPE_P (qualifying_scope))
15347                   class_type = qualifying_scope;
15348                 else
15349                   class_type = current_class_type;
15350
15351                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15352                   {
15353                     tree name_type = TREE_TYPE (unqualified_name);
15354                     if (class_type && same_type_p (name_type, class_type))
15355                       {
15356                         if (qualifying_scope
15357                             && CLASSTYPE_USE_TEMPLATE (name_type))
15358                           {
15359                             error_at (declarator_id_start_token->location,
15360                                       "invalid use of constructor as a template");
15361                             inform (declarator_id_start_token->location,
15362                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15363                                     "name the constructor in a qualified name",
15364                                     class_type,
15365                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15366                                     class_type, name_type);
15367                             declarator = cp_error_declarator;
15368                             break;
15369                           }
15370                         else
15371                           unqualified_name = constructor_name (class_type);
15372                       }
15373                     else
15374                       {
15375                         /* We do not attempt to print the declarator
15376                            here because we do not have enough
15377                            information about its original syntactic
15378                            form.  */
15379                         cp_parser_error (parser, "invalid declarator");
15380                         declarator = cp_error_declarator;
15381                         break;
15382                       }
15383                   }
15384
15385                 if (class_type)
15386                   {
15387                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15388                       sfk = sfk_destructor;
15389                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15390                       sfk = sfk_conversion;
15391                     else if (/* There's no way to declare a constructor
15392                                 for an anonymous type, even if the type
15393                                 got a name for linkage purposes.  */
15394                              !TYPE_WAS_ANONYMOUS (class_type)
15395                              && constructor_name_p (unqualified_name,
15396                                                     class_type))
15397                       {
15398                         unqualified_name = constructor_name (class_type);
15399                         sfk = sfk_constructor;
15400                       }
15401                     else if (is_overloaded_fn (unqualified_name)
15402                              && DECL_CONSTRUCTOR_P (get_first_fn
15403                                                     (unqualified_name)))
15404                       sfk = sfk_constructor;
15405
15406                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15407                       *ctor_dtor_or_conv_p = -1;
15408                   }
15409               }
15410             declarator = make_id_declarator (qualifying_scope,
15411                                              unqualified_name,
15412                                              sfk);
15413             declarator->id_loc = token->location;
15414             declarator->parameter_pack_p = pack_expansion_p;
15415
15416             if (pack_expansion_p)
15417               maybe_warn_variadic_templates ();
15418           }
15419
15420         handle_declarator:;
15421           scope = get_scope_of_declarator (declarator);
15422           if (scope)
15423             /* Any names that appear after the declarator-id for a
15424                member are looked up in the containing scope.  */
15425             pushed_scope = push_scope (scope);
15426           parser->in_declarator_p = true;
15427           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15428               || (declarator && declarator->kind == cdk_id))
15429             /* Default args are only allowed on function
15430                declarations.  */
15431             parser->default_arg_ok_p = saved_default_arg_ok_p;
15432           else
15433             parser->default_arg_ok_p = false;
15434
15435           first = false;
15436         }
15437       /* We're done.  */
15438       else
15439         break;
15440     }
15441
15442   /* For an abstract declarator, we might wind up with nothing at this
15443      point.  That's an error; the declarator is not optional.  */
15444   if (!declarator)
15445     cp_parser_error (parser, "expected declarator");
15446
15447   /* If we entered a scope, we must exit it now.  */
15448   if (pushed_scope)
15449     pop_scope (pushed_scope);
15450
15451   parser->default_arg_ok_p = saved_default_arg_ok_p;
15452   parser->in_declarator_p = saved_in_declarator_p;
15453
15454   return declarator;
15455 }
15456
15457 /* Parse a ptr-operator.
15458
15459    ptr-operator:
15460      * cv-qualifier-seq [opt]
15461      &
15462      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15463
15464    GNU Extension:
15465
15466    ptr-operator:
15467      & cv-qualifier-seq [opt]
15468
15469    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15470    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15471    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15472    filled in with the TYPE containing the member.  *CV_QUALS is
15473    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15474    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15475    Note that the tree codes returned by this function have nothing
15476    to do with the types of trees that will be eventually be created
15477    to represent the pointer or reference type being parsed. They are
15478    just constants with suggestive names. */
15479 static enum tree_code
15480 cp_parser_ptr_operator (cp_parser* parser,
15481                         tree* type,
15482                         cp_cv_quals *cv_quals)
15483 {
15484   enum tree_code code = ERROR_MARK;
15485   cp_token *token;
15486
15487   /* Assume that it's not a pointer-to-member.  */
15488   *type = NULL_TREE;
15489   /* And that there are no cv-qualifiers.  */
15490   *cv_quals = TYPE_UNQUALIFIED;
15491
15492   /* Peek at the next token.  */
15493   token = cp_lexer_peek_token (parser->lexer);
15494
15495   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15496   if (token->type == CPP_MULT)
15497     code = INDIRECT_REF;
15498   else if (token->type == CPP_AND)
15499     code = ADDR_EXPR;
15500   else if ((cxx_dialect != cxx98) &&
15501            token->type == CPP_AND_AND) /* C++0x only */
15502     code = NON_LVALUE_EXPR;
15503
15504   if (code != ERROR_MARK)
15505     {
15506       /* Consume the `*', `&' or `&&'.  */
15507       cp_lexer_consume_token (parser->lexer);
15508
15509       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15510          `&', if we are allowing GNU extensions.  (The only qualifier
15511          that can legally appear after `&' is `restrict', but that is
15512          enforced during semantic analysis.  */
15513       if (code == INDIRECT_REF
15514           || cp_parser_allow_gnu_extensions_p (parser))
15515         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15516     }
15517   else
15518     {
15519       /* Try the pointer-to-member case.  */
15520       cp_parser_parse_tentatively (parser);
15521       /* Look for the optional `::' operator.  */
15522       cp_parser_global_scope_opt (parser,
15523                                   /*current_scope_valid_p=*/false);
15524       /* Look for the nested-name specifier.  */
15525       token = cp_lexer_peek_token (parser->lexer);
15526       cp_parser_nested_name_specifier (parser,
15527                                        /*typename_keyword_p=*/false,
15528                                        /*check_dependency_p=*/true,
15529                                        /*type_p=*/false,
15530                                        /*is_declaration=*/false);
15531       /* If we found it, and the next token is a `*', then we are
15532          indeed looking at a pointer-to-member operator.  */
15533       if (!cp_parser_error_occurred (parser)
15534           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15535         {
15536           /* Indicate that the `*' operator was used.  */
15537           code = INDIRECT_REF;
15538
15539           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15540             error_at (token->location, "%qD is a namespace", parser->scope);
15541           else
15542             {
15543               /* The type of which the member is a member is given by the
15544                  current SCOPE.  */
15545               *type = parser->scope;
15546               /* The next name will not be qualified.  */
15547               parser->scope = NULL_TREE;
15548               parser->qualifying_scope = NULL_TREE;
15549               parser->object_scope = NULL_TREE;
15550               /* Look for the optional cv-qualifier-seq.  */
15551               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15552             }
15553         }
15554       /* If that didn't work we don't have a ptr-operator.  */
15555       if (!cp_parser_parse_definitely (parser))
15556         cp_parser_error (parser, "expected ptr-operator");
15557     }
15558
15559   return code;
15560 }
15561
15562 /* Parse an (optional) cv-qualifier-seq.
15563
15564    cv-qualifier-seq:
15565      cv-qualifier cv-qualifier-seq [opt]
15566
15567    cv-qualifier:
15568      const
15569      volatile
15570
15571    GNU Extension:
15572
15573    cv-qualifier:
15574      __restrict__
15575
15576    Returns a bitmask representing the cv-qualifiers.  */
15577
15578 static cp_cv_quals
15579 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15580 {
15581   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15582
15583   while (true)
15584     {
15585       cp_token *token;
15586       cp_cv_quals cv_qualifier;
15587
15588       /* Peek at the next token.  */
15589       token = cp_lexer_peek_token (parser->lexer);
15590       /* See if it's a cv-qualifier.  */
15591       switch (token->keyword)
15592         {
15593         case RID_CONST:
15594           cv_qualifier = TYPE_QUAL_CONST;
15595           break;
15596
15597         case RID_VOLATILE:
15598           cv_qualifier = TYPE_QUAL_VOLATILE;
15599           break;
15600
15601         case RID_RESTRICT:
15602           cv_qualifier = TYPE_QUAL_RESTRICT;
15603           break;
15604
15605         default:
15606           cv_qualifier = TYPE_UNQUALIFIED;
15607           break;
15608         }
15609
15610       if (!cv_qualifier)
15611         break;
15612
15613       if (cv_quals & cv_qualifier)
15614         {
15615           error_at (token->location, "duplicate cv-qualifier");
15616           cp_lexer_purge_token (parser->lexer);
15617         }
15618       else
15619         {
15620           cp_lexer_consume_token (parser->lexer);
15621           cv_quals |= cv_qualifier;
15622         }
15623     }
15624
15625   return cv_quals;
15626 }
15627
15628 /* Parse an (optional) virt-specifier-seq.
15629
15630    virt-specifier-seq:
15631      virt-specifier virt-specifier-seq [opt]
15632
15633    virt-specifier:
15634      override
15635      final
15636
15637    Returns a bitmask representing the virt-specifiers.  */
15638
15639 static cp_virt_specifiers
15640 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15641 {
15642   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15643
15644   while (true)
15645     {
15646       cp_token *token;
15647       cp_virt_specifiers virt_specifier;
15648
15649       /* Peek at the next token.  */
15650       token = cp_lexer_peek_token (parser->lexer);
15651       /* See if it's a virt-specifier-qualifier.  */
15652       if (token->type != CPP_NAME)
15653         break;
15654       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15655         {
15656           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15657           virt_specifier = VIRT_SPEC_OVERRIDE;
15658         }
15659       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15660         {
15661           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
15662           virt_specifier = VIRT_SPEC_FINAL;
15663         }
15664       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
15665         {
15666           virt_specifier = VIRT_SPEC_FINAL;
15667         }
15668       else
15669         break;
15670
15671       if (virt_specifiers & virt_specifier)
15672         {
15673           error_at (token->location, "duplicate virt-specifier");
15674           cp_lexer_purge_token (parser->lexer);
15675         }
15676       else
15677         {
15678           cp_lexer_consume_token (parser->lexer);
15679           virt_specifiers |= virt_specifier;
15680         }
15681     }
15682   return virt_specifiers;
15683 }
15684
15685 /* Parse a late-specified return type, if any.  This is not a separate
15686    non-terminal, but part of a function declarator, which looks like
15687
15688    -> trailing-type-specifier-seq abstract-declarator(opt)
15689
15690    Returns the type indicated by the type-id.
15691
15692    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15693    function.  */
15694
15695 static tree
15696 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15697 {
15698   cp_token *token;
15699   tree type;
15700
15701   /* Peek at the next token.  */
15702   token = cp_lexer_peek_token (parser->lexer);
15703   /* A late-specified return type is indicated by an initial '->'. */
15704   if (token->type != CPP_DEREF)
15705     return NULL_TREE;
15706
15707   /* Consume the ->.  */
15708   cp_lexer_consume_token (parser->lexer);
15709
15710   if (quals >= 0)
15711     {
15712       /* DR 1207: 'this' is in scope in the trailing return type.  */
15713       tree this_parm = build_this_parm (current_class_type, quals);
15714       gcc_assert (current_class_ptr == NULL_TREE);
15715       current_class_ref
15716         = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15717       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
15718       current_class_ptr = this_parm;
15719     }
15720
15721   type = cp_parser_trailing_type_id (parser);
15722
15723   if (current_class_type)
15724     current_class_ptr = current_class_ref = NULL_TREE;
15725
15726   return type;
15727 }
15728
15729 /* Parse a declarator-id.
15730
15731    declarator-id:
15732      id-expression
15733      :: [opt] nested-name-specifier [opt] type-name
15734
15735    In the `id-expression' case, the value returned is as for
15736    cp_parser_id_expression if the id-expression was an unqualified-id.
15737    If the id-expression was a qualified-id, then a SCOPE_REF is
15738    returned.  The first operand is the scope (either a NAMESPACE_DECL
15739    or TREE_TYPE), but the second is still just a representation of an
15740    unqualified-id.  */
15741
15742 static tree
15743 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15744 {
15745   tree id;
15746   /* The expression must be an id-expression.  Assume that qualified
15747      names are the names of types so that:
15748
15749        template <class T>
15750        int S<T>::R::i = 3;
15751
15752      will work; we must treat `S<T>::R' as the name of a type.
15753      Similarly, assume that qualified names are templates, where
15754      required, so that:
15755
15756        template <class T>
15757        int S<T>::R<T>::i = 3;
15758
15759      will work, too.  */
15760   id = cp_parser_id_expression (parser,
15761                                 /*template_keyword_p=*/false,
15762                                 /*check_dependency_p=*/false,
15763                                 /*template_p=*/NULL,
15764                                 /*declarator_p=*/true,
15765                                 optional_p);
15766   if (id && BASELINK_P (id))
15767     id = BASELINK_FUNCTIONS (id);
15768   return id;
15769 }
15770
15771 /* Parse a type-id.
15772
15773    type-id:
15774      type-specifier-seq abstract-declarator [opt]
15775
15776    Returns the TYPE specified.  */
15777
15778 static tree
15779 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15780                      bool is_trailing_return)
15781 {
15782   cp_decl_specifier_seq type_specifier_seq;
15783   cp_declarator *abstract_declarator;
15784
15785   /* Parse the type-specifier-seq.  */
15786   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15787                                 is_trailing_return,
15788                                 &type_specifier_seq);
15789   if (type_specifier_seq.type == error_mark_node)
15790     return error_mark_node;
15791
15792   /* There might or might not be an abstract declarator.  */
15793   cp_parser_parse_tentatively (parser);
15794   /* Look for the declarator.  */
15795   abstract_declarator
15796     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15797                             /*parenthesized_p=*/NULL,
15798                             /*member_p=*/false);
15799   /* Check to see if there really was a declarator.  */
15800   if (!cp_parser_parse_definitely (parser))
15801     abstract_declarator = NULL;
15802
15803   if (type_specifier_seq.type
15804       && type_uses_auto (type_specifier_seq.type))
15805     {
15806       /* A type-id with type 'auto' is only ok if the abstract declarator
15807          is a function declarator with a late-specified return type.  */
15808       if (abstract_declarator
15809           && abstract_declarator->kind == cdk_function
15810           && abstract_declarator->u.function.late_return_type)
15811         /* OK */;
15812       else
15813         {
15814           error ("invalid use of %<auto%>");
15815           return error_mark_node;
15816         }
15817     }
15818   
15819   return groktypename (&type_specifier_seq, abstract_declarator,
15820                        is_template_arg);
15821 }
15822
15823 static tree cp_parser_type_id (cp_parser *parser)
15824 {
15825   return cp_parser_type_id_1 (parser, false, false);
15826 }
15827
15828 static tree cp_parser_template_type_arg (cp_parser *parser)
15829 {
15830   tree r;
15831   const char *saved_message = parser->type_definition_forbidden_message;
15832   parser->type_definition_forbidden_message
15833     = G_("types may not be defined in template arguments");
15834   r = cp_parser_type_id_1 (parser, true, false);
15835   parser->type_definition_forbidden_message = saved_message;
15836   return r;
15837 }
15838
15839 static tree cp_parser_trailing_type_id (cp_parser *parser)
15840 {
15841   return cp_parser_type_id_1 (parser, false, true);
15842 }
15843
15844 /* Parse a type-specifier-seq.
15845
15846    type-specifier-seq:
15847      type-specifier type-specifier-seq [opt]
15848
15849    GNU extension:
15850
15851    type-specifier-seq:
15852      attributes type-specifier-seq [opt]
15853
15854    If IS_DECLARATION is true, we are at the start of a "condition" or
15855    exception-declaration, so we might be followed by a declarator-id.
15856
15857    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15858    i.e. we've just seen "->".
15859
15860    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15861
15862 static void
15863 cp_parser_type_specifier_seq (cp_parser* parser,
15864                               bool is_declaration,
15865                               bool is_trailing_return,
15866                               cp_decl_specifier_seq *type_specifier_seq)
15867 {
15868   bool seen_type_specifier = false;
15869   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15870   cp_token *start_token = NULL;
15871
15872   /* Clear the TYPE_SPECIFIER_SEQ.  */
15873   clear_decl_specs (type_specifier_seq);
15874
15875   /* In the context of a trailing return type, enum E { } is an
15876      elaborated-type-specifier followed by a function-body, not an
15877      enum-specifier.  */
15878   if (is_trailing_return)
15879     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15880
15881   /* Parse the type-specifiers and attributes.  */
15882   while (true)
15883     {
15884       tree type_specifier;
15885       bool is_cv_qualifier;
15886
15887       /* Check for attributes first.  */
15888       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15889         {
15890           type_specifier_seq->attributes =
15891             chainon (type_specifier_seq->attributes,
15892                      cp_parser_attributes_opt (parser));
15893           continue;
15894         }
15895
15896       /* record the token of the beginning of the type specifier seq,
15897          for error reporting purposes*/
15898      if (!start_token)
15899        start_token = cp_lexer_peek_token (parser->lexer);
15900
15901       /* Look for the type-specifier.  */
15902       type_specifier = cp_parser_type_specifier (parser,
15903                                                  flags,
15904                                                  type_specifier_seq,
15905                                                  /*is_declaration=*/false,
15906                                                  NULL,
15907                                                  &is_cv_qualifier);
15908       if (!type_specifier)
15909         {
15910           /* If the first type-specifier could not be found, this is not a
15911              type-specifier-seq at all.  */
15912           if (!seen_type_specifier)
15913             {
15914               cp_parser_error (parser, "expected type-specifier");
15915               type_specifier_seq->type = error_mark_node;
15916               return;
15917             }
15918           /* If subsequent type-specifiers could not be found, the
15919              type-specifier-seq is complete.  */
15920           break;
15921         }
15922
15923       seen_type_specifier = true;
15924       /* The standard says that a condition can be:
15925
15926             type-specifier-seq declarator = assignment-expression
15927
15928          However, given:
15929
15930            struct S {};
15931            if (int S = ...)
15932
15933          we should treat the "S" as a declarator, not as a
15934          type-specifier.  The standard doesn't say that explicitly for
15935          type-specifier-seq, but it does say that for
15936          decl-specifier-seq in an ordinary declaration.  Perhaps it
15937          would be clearer just to allow a decl-specifier-seq here, and
15938          then add a semantic restriction that if any decl-specifiers
15939          that are not type-specifiers appear, the program is invalid.  */
15940       if (is_declaration && !is_cv_qualifier)
15941         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15942     }
15943
15944   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15945 }
15946
15947 /* Parse a parameter-declaration-clause.
15948
15949    parameter-declaration-clause:
15950      parameter-declaration-list [opt] ... [opt]
15951      parameter-declaration-list , ...
15952
15953    Returns a representation for the parameter declarations.  A return
15954    value of NULL indicates a parameter-declaration-clause consisting
15955    only of an ellipsis.  */
15956
15957 static tree
15958 cp_parser_parameter_declaration_clause (cp_parser* parser)
15959 {
15960   tree parameters;
15961   cp_token *token;
15962   bool ellipsis_p;
15963   bool is_error;
15964
15965   /* Peek at the next token.  */
15966   token = cp_lexer_peek_token (parser->lexer);
15967   /* Check for trivial parameter-declaration-clauses.  */
15968   if (token->type == CPP_ELLIPSIS)
15969     {
15970       /* Consume the `...' token.  */
15971       cp_lexer_consume_token (parser->lexer);
15972       return NULL_TREE;
15973     }
15974   else if (token->type == CPP_CLOSE_PAREN)
15975     /* There are no parameters.  */
15976     {
15977 #ifndef NO_IMPLICIT_EXTERN_C
15978       if (in_system_header && current_class_type == NULL
15979           && current_lang_name == lang_name_c)
15980         return NULL_TREE;
15981       else
15982 #endif
15983         return void_list_node;
15984     }
15985   /* Check for `(void)', too, which is a special case.  */
15986   else if (token->keyword == RID_VOID
15987            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15988                == CPP_CLOSE_PAREN))
15989     {
15990       /* Consume the `void' token.  */
15991       cp_lexer_consume_token (parser->lexer);
15992       /* There are no parameters.  */
15993       return void_list_node;
15994     }
15995
15996   /* Parse the parameter-declaration-list.  */
15997   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15998   /* If a parse error occurred while parsing the
15999      parameter-declaration-list, then the entire
16000      parameter-declaration-clause is erroneous.  */
16001   if (is_error)
16002     return NULL;
16003
16004   /* Peek at the next token.  */
16005   token = cp_lexer_peek_token (parser->lexer);
16006   /* If it's a `,', the clause should terminate with an ellipsis.  */
16007   if (token->type == CPP_COMMA)
16008     {
16009       /* Consume the `,'.  */
16010       cp_lexer_consume_token (parser->lexer);
16011       /* Expect an ellipsis.  */
16012       ellipsis_p
16013         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16014     }
16015   /* It might also be `...' if the optional trailing `,' was
16016      omitted.  */
16017   else if (token->type == CPP_ELLIPSIS)
16018     {
16019       /* Consume the `...' token.  */
16020       cp_lexer_consume_token (parser->lexer);
16021       /* And remember that we saw it.  */
16022       ellipsis_p = true;
16023     }
16024   else
16025     ellipsis_p = false;
16026
16027   /* Finish the parameter list.  */
16028   if (!ellipsis_p)
16029     parameters = chainon (parameters, void_list_node);
16030
16031   return parameters;
16032 }
16033
16034 /* Parse a parameter-declaration-list.
16035
16036    parameter-declaration-list:
16037      parameter-declaration
16038      parameter-declaration-list , parameter-declaration
16039
16040    Returns a representation of the parameter-declaration-list, as for
16041    cp_parser_parameter_declaration_clause.  However, the
16042    `void_list_node' is never appended to the list.  Upon return,
16043    *IS_ERROR will be true iff an error occurred.  */
16044
16045 static tree
16046 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16047 {
16048   tree parameters = NULL_TREE;
16049   tree *tail = &parameters; 
16050   bool saved_in_unbraced_linkage_specification_p;
16051   int index = 0;
16052
16053   /* Assume all will go well.  */
16054   *is_error = false;
16055   /* The special considerations that apply to a function within an
16056      unbraced linkage specifications do not apply to the parameters
16057      to the function.  */
16058   saved_in_unbraced_linkage_specification_p 
16059     = parser->in_unbraced_linkage_specification_p;
16060   parser->in_unbraced_linkage_specification_p = false;
16061
16062   /* Look for more parameters.  */
16063   while (true)
16064     {
16065       cp_parameter_declarator *parameter;
16066       tree decl = error_mark_node;
16067       bool parenthesized_p = false;
16068       /* Parse the parameter.  */
16069       parameter
16070         = cp_parser_parameter_declaration (parser,
16071                                            /*template_parm_p=*/false,
16072                                            &parenthesized_p);
16073
16074       /* We don't know yet if the enclosing context is deprecated, so wait
16075          and warn in grokparms if appropriate.  */
16076       deprecated_state = DEPRECATED_SUPPRESS;
16077
16078       if (parameter)
16079         decl = grokdeclarator (parameter->declarator,
16080                                &parameter->decl_specifiers,
16081                                PARM,
16082                                parameter->default_argument != NULL_TREE,
16083                                &parameter->decl_specifiers.attributes);
16084
16085       deprecated_state = DEPRECATED_NORMAL;
16086
16087       /* If a parse error occurred parsing the parameter declaration,
16088          then the entire parameter-declaration-list is erroneous.  */
16089       if (decl == error_mark_node)
16090         {
16091           *is_error = true;
16092           parameters = error_mark_node;
16093           break;
16094         }
16095
16096       if (parameter->decl_specifiers.attributes)
16097         cplus_decl_attributes (&decl,
16098                                parameter->decl_specifiers.attributes,
16099                                0);
16100       if (DECL_NAME (decl))
16101         decl = pushdecl (decl);
16102
16103       if (decl != error_mark_node)
16104         {
16105           retrofit_lang_decl (decl);
16106           DECL_PARM_INDEX (decl) = ++index;
16107           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16108         }
16109
16110       /* Add the new parameter to the list.  */
16111       *tail = build_tree_list (parameter->default_argument, decl);
16112       tail = &TREE_CHAIN (*tail);
16113
16114       /* Peek at the next token.  */
16115       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16116           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16117           /* These are for Objective-C++ */
16118           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16119           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16120         /* The parameter-declaration-list is complete.  */
16121         break;
16122       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16123         {
16124           cp_token *token;
16125
16126           /* Peek at the next token.  */
16127           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16128           /* If it's an ellipsis, then the list is complete.  */
16129           if (token->type == CPP_ELLIPSIS)
16130             break;
16131           /* Otherwise, there must be more parameters.  Consume the
16132              `,'.  */
16133           cp_lexer_consume_token (parser->lexer);
16134           /* When parsing something like:
16135
16136                 int i(float f, double d)
16137
16138              we can tell after seeing the declaration for "f" that we
16139              are not looking at an initialization of a variable "i",
16140              but rather at the declaration of a function "i".
16141
16142              Due to the fact that the parsing of template arguments
16143              (as specified to a template-id) requires backtracking we
16144              cannot use this technique when inside a template argument
16145              list.  */
16146           if (!parser->in_template_argument_list_p
16147               && !parser->in_type_id_in_expr_p
16148               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16149               /* However, a parameter-declaration of the form
16150                  "foat(f)" (which is a valid declaration of a
16151                  parameter "f") can also be interpreted as an
16152                  expression (the conversion of "f" to "float").  */
16153               && !parenthesized_p)
16154             cp_parser_commit_to_tentative_parse (parser);
16155         }
16156       else
16157         {
16158           cp_parser_error (parser, "expected %<,%> or %<...%>");
16159           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16160             cp_parser_skip_to_closing_parenthesis (parser,
16161                                                    /*recovering=*/true,
16162                                                    /*or_comma=*/false,
16163                                                    /*consume_paren=*/false);
16164           break;
16165         }
16166     }
16167
16168   parser->in_unbraced_linkage_specification_p
16169     = saved_in_unbraced_linkage_specification_p;
16170
16171   return parameters;
16172 }
16173
16174 /* Parse a parameter declaration.
16175
16176    parameter-declaration:
16177      decl-specifier-seq ... [opt] declarator
16178      decl-specifier-seq declarator = assignment-expression
16179      decl-specifier-seq ... [opt] abstract-declarator [opt]
16180      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16181
16182    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16183    declares a template parameter.  (In that case, a non-nested `>'
16184    token encountered during the parsing of the assignment-expression
16185    is not interpreted as a greater-than operator.)
16186
16187    Returns a representation of the parameter, or NULL if an error
16188    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16189    true iff the declarator is of the form "(p)".  */
16190
16191 static cp_parameter_declarator *
16192 cp_parser_parameter_declaration (cp_parser *parser,
16193                                  bool template_parm_p,
16194                                  bool *parenthesized_p)
16195 {
16196   int declares_class_or_enum;
16197   cp_decl_specifier_seq decl_specifiers;
16198   cp_declarator *declarator;
16199   tree default_argument;
16200   cp_token *token = NULL, *declarator_token_start = NULL;
16201   const char *saved_message;
16202
16203   /* In a template parameter, `>' is not an operator.
16204
16205      [temp.param]
16206
16207      When parsing a default template-argument for a non-type
16208      template-parameter, the first non-nested `>' is taken as the end
16209      of the template parameter-list rather than a greater-than
16210      operator.  */
16211
16212   /* Type definitions may not appear in parameter types.  */
16213   saved_message = parser->type_definition_forbidden_message;
16214   parser->type_definition_forbidden_message
16215     = G_("types may not be defined in parameter types");
16216
16217   /* Parse the declaration-specifiers.  */
16218   cp_parser_decl_specifier_seq (parser,
16219                                 CP_PARSER_FLAGS_NONE,
16220                                 &decl_specifiers,
16221                                 &declares_class_or_enum);
16222
16223   /* Complain about missing 'typename' or other invalid type names.  */
16224   if (!decl_specifiers.any_type_specifiers_p)
16225     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16226
16227   /* If an error occurred, there's no reason to attempt to parse the
16228      rest of the declaration.  */
16229   if (cp_parser_error_occurred (parser))
16230     {
16231       parser->type_definition_forbidden_message = saved_message;
16232       return NULL;
16233     }
16234
16235   /* Peek at the next token.  */
16236   token = cp_lexer_peek_token (parser->lexer);
16237
16238   /* If the next token is a `)', `,', `=', `>', or `...', then there
16239      is no declarator. However, when variadic templates are enabled,
16240      there may be a declarator following `...'.  */
16241   if (token->type == CPP_CLOSE_PAREN
16242       || token->type == CPP_COMMA
16243       || token->type == CPP_EQ
16244       || token->type == CPP_GREATER)
16245     {
16246       declarator = NULL;
16247       if (parenthesized_p)
16248         *parenthesized_p = false;
16249     }
16250   /* Otherwise, there should be a declarator.  */
16251   else
16252     {
16253       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16254       parser->default_arg_ok_p = false;
16255
16256       /* After seeing a decl-specifier-seq, if the next token is not a
16257          "(", there is no possibility that the code is a valid
16258          expression.  Therefore, if parsing tentatively, we commit at
16259          this point.  */
16260       if (!parser->in_template_argument_list_p
16261           /* In an expression context, having seen:
16262
16263                (int((char ...
16264
16265              we cannot be sure whether we are looking at a
16266              function-type (taking a "char" as a parameter) or a cast
16267              of some object of type "char" to "int".  */
16268           && !parser->in_type_id_in_expr_p
16269           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16270           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16271           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16272         cp_parser_commit_to_tentative_parse (parser);
16273       /* Parse the declarator.  */
16274       declarator_token_start = token;
16275       declarator = cp_parser_declarator (parser,
16276                                          CP_PARSER_DECLARATOR_EITHER,
16277                                          /*ctor_dtor_or_conv_p=*/NULL,
16278                                          parenthesized_p,
16279                                          /*member_p=*/false);
16280       parser->default_arg_ok_p = saved_default_arg_ok_p;
16281       /* After the declarator, allow more attributes.  */
16282       decl_specifiers.attributes
16283         = chainon (decl_specifiers.attributes,
16284                    cp_parser_attributes_opt (parser));
16285     }
16286
16287   /* If the next token is an ellipsis, and we have not seen a
16288      declarator name, and the type of the declarator contains parameter
16289      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16290      a parameter pack expansion expression. Otherwise, leave the
16291      ellipsis for a C-style variadic function. */
16292   token = cp_lexer_peek_token (parser->lexer);
16293   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16294     {
16295       tree type = decl_specifiers.type;
16296
16297       if (type && DECL_P (type))
16298         type = TREE_TYPE (type);
16299
16300       if (type
16301           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16302           && declarator_can_be_parameter_pack (declarator)
16303           && (!declarator || !declarator->parameter_pack_p)
16304           && uses_parameter_packs (type))
16305         {
16306           /* Consume the `...'. */
16307           cp_lexer_consume_token (parser->lexer);
16308           maybe_warn_variadic_templates ();
16309           
16310           /* Build a pack expansion type */
16311           if (declarator)
16312             declarator->parameter_pack_p = true;
16313           else
16314             decl_specifiers.type = make_pack_expansion (type);
16315         }
16316     }
16317
16318   /* The restriction on defining new types applies only to the type
16319      of the parameter, not to the default argument.  */
16320   parser->type_definition_forbidden_message = saved_message;
16321
16322   /* If the next token is `=', then process a default argument.  */
16323   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16324     {
16325       /* Consume the `='.  */
16326       cp_lexer_consume_token (parser->lexer);
16327
16328       /* If we are defining a class, then the tokens that make up the
16329          default argument must be saved and processed later.  */
16330       if (!template_parm_p && at_class_scope_p ()
16331           && TYPE_BEING_DEFINED (current_class_type)
16332           && !LAMBDA_TYPE_P (current_class_type))
16333         {
16334           unsigned depth = 0;
16335           int maybe_template_id = 0;
16336           cp_token *first_token;
16337           cp_token *token;
16338
16339           /* Add tokens until we have processed the entire default
16340              argument.  We add the range [first_token, token).  */
16341           first_token = cp_lexer_peek_token (parser->lexer);
16342           while (true)
16343             {
16344               bool done = false;
16345
16346               /* Peek at the next token.  */
16347               token = cp_lexer_peek_token (parser->lexer);
16348               /* What we do depends on what token we have.  */
16349               switch (token->type)
16350                 {
16351                   /* In valid code, a default argument must be
16352                      immediately followed by a `,' `)', or `...'.  */
16353                 case CPP_COMMA:
16354                   if (depth == 0 && maybe_template_id)
16355                     {
16356                       /* If we've seen a '<', we might be in a
16357                          template-argument-list.  Until Core issue 325 is
16358                          resolved, we don't know how this situation ought
16359                          to be handled, so try to DTRT.  We check whether
16360                          what comes after the comma is a valid parameter
16361                          declaration list.  If it is, then the comma ends
16362                          the default argument; otherwise the default
16363                          argument continues.  */
16364                       bool error = false;
16365                       tree t;
16366
16367                       /* Set ITALP so cp_parser_parameter_declaration_list
16368                          doesn't decide to commit to this parse.  */
16369                       bool saved_italp = parser->in_template_argument_list_p;
16370                       parser->in_template_argument_list_p = true;
16371
16372                       cp_parser_parse_tentatively (parser);
16373                       cp_lexer_consume_token (parser->lexer);
16374                       begin_scope (sk_function_parms, NULL_TREE);
16375                       cp_parser_parameter_declaration_list (parser, &error);
16376                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16377                         pop_binding (DECL_NAME (t), t);
16378                       leave_scope ();
16379                       if (!cp_parser_error_occurred (parser) && !error)
16380                         done = true;
16381                       cp_parser_abort_tentative_parse (parser);
16382
16383                       parser->in_template_argument_list_p = saved_italp;
16384                       break;
16385                     }
16386                 case CPP_CLOSE_PAREN:
16387                 case CPP_ELLIPSIS:
16388                   /* If we run into a non-nested `;', `}', or `]',
16389                      then the code is invalid -- but the default
16390                      argument is certainly over.  */
16391                 case CPP_SEMICOLON:
16392                 case CPP_CLOSE_BRACE:
16393                 case CPP_CLOSE_SQUARE:
16394                   if (depth == 0)
16395                     done = true;
16396                   /* Update DEPTH, if necessary.  */
16397                   else if (token->type == CPP_CLOSE_PAREN
16398                            || token->type == CPP_CLOSE_BRACE
16399                            || token->type == CPP_CLOSE_SQUARE)
16400                     --depth;
16401                   break;
16402
16403                 case CPP_OPEN_PAREN:
16404                 case CPP_OPEN_SQUARE:
16405                 case CPP_OPEN_BRACE:
16406                   ++depth;
16407                   break;
16408
16409                 case CPP_LESS:
16410                   if (depth == 0)
16411                     /* This might be the comparison operator, or it might
16412                        start a template argument list.  */
16413                     ++maybe_template_id;
16414                   break;
16415
16416                 case CPP_RSHIFT:
16417                   if (cxx_dialect == cxx98)
16418                     break;
16419                   /* Fall through for C++0x, which treats the `>>'
16420                      operator like two `>' tokens in certain
16421                      cases.  */
16422
16423                 case CPP_GREATER:
16424                   if (depth == 0)
16425                     {
16426                       /* This might be an operator, or it might close a
16427                          template argument list.  But if a previous '<'
16428                          started a template argument list, this will have
16429                          closed it, so we can't be in one anymore.  */
16430                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16431                       if (maybe_template_id < 0)
16432                         maybe_template_id = 0;
16433                     }
16434                   break;
16435
16436                   /* If we run out of tokens, issue an error message.  */
16437                 case CPP_EOF:
16438                 case CPP_PRAGMA_EOL:
16439                   error_at (token->location, "file ends in default argument");
16440                   done = true;
16441                   break;
16442
16443                 case CPP_NAME:
16444                 case CPP_SCOPE:
16445                   /* In these cases, we should look for template-ids.
16446                      For example, if the default argument is
16447                      `X<int, double>()', we need to do name lookup to
16448                      figure out whether or not `X' is a template; if
16449                      so, the `,' does not end the default argument.
16450
16451                      That is not yet done.  */
16452                   break;
16453
16454                 default:
16455                   break;
16456                 }
16457
16458               /* If we've reached the end, stop.  */
16459               if (done)
16460                 break;
16461
16462               /* Add the token to the token block.  */
16463               token = cp_lexer_consume_token (parser->lexer);
16464             }
16465
16466           /* Create a DEFAULT_ARG to represent the unparsed default
16467              argument.  */
16468           default_argument = make_node (DEFAULT_ARG);
16469           DEFARG_TOKENS (default_argument)
16470             = cp_token_cache_new (first_token, token);
16471           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16472         }
16473       /* Outside of a class definition, we can just parse the
16474          assignment-expression.  */
16475       else
16476         {
16477           token = cp_lexer_peek_token (parser->lexer);
16478           default_argument 
16479             = cp_parser_default_argument (parser, template_parm_p);
16480         }
16481
16482       if (!parser->default_arg_ok_p)
16483         {
16484           if (flag_permissive)
16485             warning (0, "deprecated use of default argument for parameter of non-function");
16486           else
16487             {
16488               error_at (token->location,
16489                         "default arguments are only "
16490                         "permitted for function parameters");
16491               default_argument = NULL_TREE;
16492             }
16493         }
16494       else if ((declarator && declarator->parameter_pack_p)
16495                || (decl_specifiers.type
16496                    && PACK_EXPANSION_P (decl_specifiers.type)))
16497         {
16498           /* Find the name of the parameter pack.  */     
16499           cp_declarator *id_declarator = declarator;
16500           while (id_declarator && id_declarator->kind != cdk_id)
16501             id_declarator = id_declarator->declarator;
16502           
16503           if (id_declarator && id_declarator->kind == cdk_id)
16504             error_at (declarator_token_start->location,
16505                       template_parm_p 
16506                       ? "template parameter pack %qD"
16507                       " cannot have a default argument"
16508                       : "parameter pack %qD cannot have a default argument",
16509                       id_declarator->u.id.unqualified_name);
16510           else
16511             error_at (declarator_token_start->location,
16512                       template_parm_p 
16513                       ? "template parameter pack cannot have a default argument"
16514                       : "parameter pack cannot have a default argument");
16515           
16516           default_argument = NULL_TREE;
16517         }
16518     }
16519   else
16520     default_argument = NULL_TREE;
16521
16522   return make_parameter_declarator (&decl_specifiers,
16523                                     declarator,
16524                                     default_argument);
16525 }
16526
16527 /* Parse a default argument and return it.
16528
16529    TEMPLATE_PARM_P is true if this is a default argument for a
16530    non-type template parameter.  */
16531 static tree
16532 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16533 {
16534   tree default_argument = NULL_TREE;
16535   bool saved_greater_than_is_operator_p;
16536   bool saved_local_variables_forbidden_p;
16537
16538   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16539      set correctly.  */
16540   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16541   parser->greater_than_is_operator_p = !template_parm_p;
16542   /* Local variable names (and the `this' keyword) may not
16543      appear in a default argument.  */
16544   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16545   parser->local_variables_forbidden_p = true;
16546   /* Parse the assignment-expression.  */
16547   if (template_parm_p)
16548     push_deferring_access_checks (dk_no_deferred);
16549   default_argument
16550     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16551   if (template_parm_p)
16552     pop_deferring_access_checks ();
16553   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16554   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16555
16556   return default_argument;
16557 }
16558
16559 /* Parse a function-body.
16560
16561    function-body:
16562      compound_statement  */
16563
16564 static void
16565 cp_parser_function_body (cp_parser *parser)
16566 {
16567   cp_parser_compound_statement (parser, NULL, false, true);
16568 }
16569
16570 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16571    true if a ctor-initializer was present.  */
16572
16573 static bool
16574 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16575 {
16576   tree body, list;
16577   bool ctor_initializer_p;
16578   const bool check_body_p =
16579      DECL_CONSTRUCTOR_P (current_function_decl)
16580      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16581   tree last = NULL;
16582
16583   /* Begin the function body.  */
16584   body = begin_function_body ();
16585   /* Parse the optional ctor-initializer.  */
16586   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16587
16588   /* If we're parsing a constexpr constructor definition, we need
16589      to check that the constructor body is indeed empty.  However,
16590      before we get to cp_parser_function_body lot of junk has been
16591      generated, so we can't just check that we have an empty block.
16592      Rather we take a snapshot of the outermost block, and check whether
16593      cp_parser_function_body changed its state.  */
16594   if (check_body_p)
16595     {
16596       list = body;
16597       if (TREE_CODE (list) == BIND_EXPR)
16598         list = BIND_EXPR_BODY (list);
16599       if (TREE_CODE (list) == STATEMENT_LIST
16600           && STATEMENT_LIST_TAIL (list) != NULL)
16601         last = STATEMENT_LIST_TAIL (list)->stmt;
16602     }
16603   /* Parse the function-body.  */
16604   cp_parser_function_body (parser);
16605   if (check_body_p)
16606     check_constexpr_ctor_body (last, list);
16607   /* Finish the function body.  */
16608   finish_function_body (body);
16609
16610   return ctor_initializer_p;
16611 }
16612
16613 /* Parse an initializer.
16614
16615    initializer:
16616      = initializer-clause
16617      ( expression-list )
16618
16619    Returns an expression representing the initializer.  If no
16620    initializer is present, NULL_TREE is returned.
16621
16622    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16623    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16624    set to TRUE if there is no initializer present.  If there is an
16625    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16626    is set to true; otherwise it is set to false.  */
16627
16628 static tree
16629 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16630                        bool* non_constant_p)
16631 {
16632   cp_token *token;
16633   tree init;
16634
16635   /* Peek at the next token.  */
16636   token = cp_lexer_peek_token (parser->lexer);
16637
16638   /* Let our caller know whether or not this initializer was
16639      parenthesized.  */
16640   *is_direct_init = (token->type != CPP_EQ);
16641   /* Assume that the initializer is constant.  */
16642   *non_constant_p = false;
16643
16644   if (token->type == CPP_EQ)
16645     {
16646       /* Consume the `='.  */
16647       cp_lexer_consume_token (parser->lexer);
16648       /* Parse the initializer-clause.  */
16649       init = cp_parser_initializer_clause (parser, non_constant_p);
16650     }
16651   else if (token->type == CPP_OPEN_PAREN)
16652     {
16653       VEC(tree,gc) *vec;
16654       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16655                                                      /*cast_p=*/false,
16656                                                      /*allow_expansion_p=*/true,
16657                                                      non_constant_p);
16658       if (vec == NULL)
16659         return error_mark_node;
16660       init = build_tree_list_vec (vec);
16661       release_tree_vector (vec);
16662     }
16663   else if (token->type == CPP_OPEN_BRACE)
16664     {
16665       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16666       init = cp_parser_braced_list (parser, non_constant_p);
16667       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16668     }
16669   else
16670     {
16671       /* Anything else is an error.  */
16672       cp_parser_error (parser, "expected initializer");
16673       init = error_mark_node;
16674     }
16675
16676   return init;
16677 }
16678
16679 /* Parse an initializer-clause.
16680
16681    initializer-clause:
16682      assignment-expression
16683      braced-init-list
16684
16685    Returns an expression representing the initializer.
16686
16687    If the `assignment-expression' production is used the value
16688    returned is simply a representation for the expression.
16689
16690    Otherwise, calls cp_parser_braced_list.  */
16691
16692 static tree
16693 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16694 {
16695   tree initializer;
16696
16697   /* Assume the expression is constant.  */
16698   *non_constant_p = false;
16699
16700   /* If it is not a `{', then we are looking at an
16701      assignment-expression.  */
16702   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16703     {
16704       initializer
16705         = cp_parser_constant_expression (parser,
16706                                         /*allow_non_constant_p=*/true,
16707                                         non_constant_p);
16708     }
16709   else
16710     initializer = cp_parser_braced_list (parser, non_constant_p);
16711
16712   return initializer;
16713 }
16714
16715 /* Parse a brace-enclosed initializer list.
16716
16717    braced-init-list:
16718      { initializer-list , [opt] }
16719      { }
16720
16721    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16722    the elements of the initializer-list (or NULL, if the last
16723    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16724    NULL_TREE.  There is no way to detect whether or not the optional
16725    trailing `,' was provided.  NON_CONSTANT_P is as for
16726    cp_parser_initializer.  */     
16727
16728 static tree
16729 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16730 {
16731   tree initializer;
16732
16733   /* Consume the `{' token.  */
16734   cp_lexer_consume_token (parser->lexer);
16735   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16736   initializer = make_node (CONSTRUCTOR);
16737   /* If it's not a `}', then there is a non-trivial initializer.  */
16738   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16739     {
16740       /* Parse the initializer list.  */
16741       CONSTRUCTOR_ELTS (initializer)
16742         = cp_parser_initializer_list (parser, non_constant_p);
16743       /* A trailing `,' token is allowed.  */
16744       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16745         cp_lexer_consume_token (parser->lexer);
16746     }
16747   /* Now, there should be a trailing `}'.  */
16748   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16749   TREE_TYPE (initializer) = init_list_type_node;
16750   return initializer;
16751 }
16752
16753 /* Parse an initializer-list.
16754
16755    initializer-list:
16756      initializer-clause ... [opt]
16757      initializer-list , initializer-clause ... [opt]
16758
16759    GNU Extension:
16760
16761    initializer-list:
16762      designation initializer-clause ...[opt]
16763      initializer-list , designation initializer-clause ...[opt]
16764
16765    designation:
16766      . identifier =
16767      identifier :
16768      [ constant-expression ] =
16769
16770    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16771    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16772    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16773    as for cp_parser_initializer.  */
16774
16775 static VEC(constructor_elt,gc) *
16776 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16777 {
16778   VEC(constructor_elt,gc) *v = NULL;
16779
16780   /* Assume all of the expressions are constant.  */
16781   *non_constant_p = false;
16782
16783   /* Parse the rest of the list.  */
16784   while (true)
16785     {
16786       cp_token *token;
16787       tree designator;
16788       tree initializer;
16789       bool clause_non_constant_p;
16790
16791       /* If the next token is an identifier and the following one is a
16792          colon, we are looking at the GNU designated-initializer
16793          syntax.  */
16794       if (cp_parser_allow_gnu_extensions_p (parser)
16795           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16796           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16797         {
16798           /* Warn the user that they are using an extension.  */
16799           pedwarn (input_location, OPT_pedantic, 
16800                    "ISO C++ does not allow designated initializers");
16801           /* Consume the identifier.  */
16802           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16803           /* Consume the `:'.  */
16804           cp_lexer_consume_token (parser->lexer);
16805         }
16806       /* Also handle the C99 syntax, '. id ='.  */
16807       else if (cp_parser_allow_gnu_extensions_p (parser)
16808                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
16809                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
16810                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
16811         {
16812           /* Warn the user that they are using an extension.  */
16813           pedwarn (input_location, OPT_pedantic,
16814                    "ISO C++ does not allow C99 designated initializers");
16815           /* Consume the `.'.  */
16816           cp_lexer_consume_token (parser->lexer);
16817           /* Consume the identifier.  */
16818           designator = cp_lexer_consume_token (parser->lexer)->u.value;
16819           /* Consume the `='.  */
16820           cp_lexer_consume_token (parser->lexer);
16821         }
16822       /* Also handle C99 array designators, '[ const ] ='.  */
16823       else if (cp_parser_allow_gnu_extensions_p (parser)
16824                && !c_dialect_objc ()
16825                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
16826         {
16827           cp_lexer_consume_token (parser->lexer);
16828           designator = cp_parser_constant_expression (parser, false, NULL);
16829           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
16830           cp_parser_require (parser, CPP_EQ, RT_EQ);
16831         }
16832       else
16833         designator = NULL_TREE;
16834
16835       /* Parse the initializer.  */
16836       initializer = cp_parser_initializer_clause (parser,
16837                                                   &clause_non_constant_p);
16838       /* If any clause is non-constant, so is the entire initializer.  */
16839       if (clause_non_constant_p)
16840         *non_constant_p = true;
16841
16842       /* If we have an ellipsis, this is an initializer pack
16843          expansion.  */
16844       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16845         {
16846           /* Consume the `...'.  */
16847           cp_lexer_consume_token (parser->lexer);
16848
16849           /* Turn the initializer into an initializer expansion.  */
16850           initializer = make_pack_expansion (initializer);
16851         }
16852
16853       /* Add it to the vector.  */
16854       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
16855
16856       /* If the next token is not a comma, we have reached the end of
16857          the list.  */
16858       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16859         break;
16860
16861       /* Peek at the next token.  */
16862       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16863       /* If the next token is a `}', then we're still done.  An
16864          initializer-clause can have a trailing `,' after the
16865          initializer-list and before the closing `}'.  */
16866       if (token->type == CPP_CLOSE_BRACE)
16867         break;
16868
16869       /* Consume the `,' token.  */
16870       cp_lexer_consume_token (parser->lexer);
16871     }
16872
16873   return v;
16874 }
16875
16876 /* Classes [gram.class] */
16877
16878 /* Parse a class-name.
16879
16880    class-name:
16881      identifier
16882      template-id
16883
16884    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16885    to indicate that names looked up in dependent types should be
16886    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16887    keyword has been used to indicate that the name that appears next
16888    is a template.  TAG_TYPE indicates the explicit tag given before
16889    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16890    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16891    is the class being defined in a class-head.
16892
16893    Returns the TYPE_DECL representing the class.  */
16894
16895 static tree
16896 cp_parser_class_name (cp_parser *parser,
16897                       bool typename_keyword_p,
16898                       bool template_keyword_p,
16899                       enum tag_types tag_type,
16900                       bool check_dependency_p,
16901                       bool class_head_p,
16902                       bool is_declaration)
16903 {
16904   tree decl;
16905   tree scope;
16906   bool typename_p;
16907   cp_token *token;
16908   tree identifier = NULL_TREE;
16909
16910   /* All class-names start with an identifier.  */
16911   token = cp_lexer_peek_token (parser->lexer);
16912   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16913     {
16914       cp_parser_error (parser, "expected class-name");
16915       return error_mark_node;
16916     }
16917
16918   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16919      to a template-id, so we save it here.  */
16920   scope = parser->scope;
16921   if (scope == error_mark_node)
16922     return error_mark_node;
16923
16924   /* Any name names a type if we're following the `typename' keyword
16925      in a qualified name where the enclosing scope is type-dependent.  */
16926   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16927                 && dependent_type_p (scope));
16928   /* Handle the common case (an identifier, but not a template-id)
16929      efficiently.  */
16930   if (token->type == CPP_NAME
16931       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16932     {
16933       cp_token *identifier_token;
16934       bool ambiguous_p;
16935
16936       /* Look for the identifier.  */
16937       identifier_token = cp_lexer_peek_token (parser->lexer);
16938       ambiguous_p = identifier_token->ambiguous_p;
16939       identifier = cp_parser_identifier (parser);
16940       /* If the next token isn't an identifier, we are certainly not
16941          looking at a class-name.  */
16942       if (identifier == error_mark_node)
16943         decl = error_mark_node;
16944       /* If we know this is a type-name, there's no need to look it
16945          up.  */
16946       else if (typename_p)
16947         decl = identifier;
16948       else
16949         {
16950           tree ambiguous_decls;
16951           /* If we already know that this lookup is ambiguous, then
16952              we've already issued an error message; there's no reason
16953              to check again.  */
16954           if (ambiguous_p)
16955             {
16956               cp_parser_simulate_error (parser);
16957               return error_mark_node;
16958             }
16959           /* If the next token is a `::', then the name must be a type
16960              name.
16961
16962              [basic.lookup.qual]
16963
16964              During the lookup for a name preceding the :: scope
16965              resolution operator, object, function, and enumerator
16966              names are ignored.  */
16967           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16968             tag_type = typename_type;
16969           /* Look up the name.  */
16970           decl = cp_parser_lookup_name (parser, identifier,
16971                                         tag_type,
16972                                         /*is_template=*/false,
16973                                         /*is_namespace=*/false,
16974                                         check_dependency_p,
16975                                         &ambiguous_decls,
16976                                         identifier_token->location);
16977           if (ambiguous_decls)
16978             {
16979               if (cp_parser_parsing_tentatively (parser))
16980                 cp_parser_simulate_error (parser);
16981               return error_mark_node;
16982             }
16983         }
16984     }
16985   else
16986     {
16987       /* Try a template-id.  */
16988       decl = cp_parser_template_id (parser, template_keyword_p,
16989                                     check_dependency_p,
16990                                     is_declaration);
16991       if (decl == error_mark_node)
16992         return error_mark_node;
16993     }
16994
16995   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16996
16997   /* If this is a typename, create a TYPENAME_TYPE.  */
16998   if (typename_p && decl != error_mark_node)
16999     {
17000       decl = make_typename_type (scope, decl, typename_type,
17001                                  /*complain=*/tf_error);
17002       if (decl != error_mark_node)
17003         decl = TYPE_NAME (decl);
17004     }
17005
17006   /* Check to see that it is really the name of a class.  */
17007   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17008       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17009       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17010     /* Situations like this:
17011
17012          template <typename T> struct A {
17013            typename T::template X<int>::I i;
17014          };
17015
17016        are problematic.  Is `T::template X<int>' a class-name?  The
17017        standard does not seem to be definitive, but there is no other
17018        valid interpretation of the following `::'.  Therefore, those
17019        names are considered class-names.  */
17020     {
17021       decl = make_typename_type (scope, decl, tag_type, tf_error);
17022       if (decl != error_mark_node)
17023         decl = TYPE_NAME (decl);
17024     }
17025   else if (TREE_CODE (decl) != TYPE_DECL
17026            || TREE_TYPE (decl) == error_mark_node
17027            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17028            /* In Objective-C 2.0, a classname followed by '.' starts a
17029               dot-syntax expression, and it's not a type-name.  */
17030            || (c_dialect_objc ()
17031                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17032                && objc_is_class_name (decl)))
17033     decl = error_mark_node;
17034
17035   if (decl == error_mark_node)
17036     cp_parser_error (parser, "expected class-name");
17037   else if (identifier && !parser->scope)
17038     maybe_note_name_used_in_class (identifier, decl);
17039
17040   return decl;
17041 }
17042
17043 /* Parse a class-specifier.
17044
17045    class-specifier:
17046      class-head { member-specification [opt] }
17047
17048    Returns the TREE_TYPE representing the class.  */
17049
17050 static tree
17051 cp_parser_class_specifier_1 (cp_parser* parser)
17052 {
17053   tree type;
17054   tree attributes = NULL_TREE;
17055   bool nested_name_specifier_p;
17056   unsigned saved_num_template_parameter_lists;
17057   bool saved_in_function_body;
17058   unsigned char in_statement;
17059   bool in_switch_statement_p;
17060   bool saved_in_unbraced_linkage_specification_p;
17061   tree old_scope = NULL_TREE;
17062   tree scope = NULL_TREE;
17063   tree bases;
17064   cp_token *closing_brace;
17065
17066   push_deferring_access_checks (dk_no_deferred);
17067
17068   /* Parse the class-head.  */
17069   type = cp_parser_class_head (parser,
17070                                &nested_name_specifier_p,
17071                                &attributes,
17072                                &bases);
17073   /* If the class-head was a semantic disaster, skip the entire body
17074      of the class.  */
17075   if (!type)
17076     {
17077       cp_parser_skip_to_end_of_block_or_statement (parser);
17078       pop_deferring_access_checks ();
17079       return error_mark_node;
17080     }
17081
17082   /* Look for the `{'.  */
17083   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17084     {
17085       pop_deferring_access_checks ();
17086       return error_mark_node;
17087     }
17088
17089   /* Process the base classes. If they're invalid, skip the 
17090      entire class body.  */
17091   if (!xref_basetypes (type, bases))
17092     {
17093       /* Consuming the closing brace yields better error messages
17094          later on.  */
17095       if (cp_parser_skip_to_closing_brace (parser))
17096         cp_lexer_consume_token (parser->lexer);
17097       pop_deferring_access_checks ();
17098       return error_mark_node;
17099     }
17100
17101   /* Issue an error message if type-definitions are forbidden here.  */
17102   cp_parser_check_type_definition (parser);
17103   /* Remember that we are defining one more class.  */
17104   ++parser->num_classes_being_defined;
17105   /* Inside the class, surrounding template-parameter-lists do not
17106      apply.  */
17107   saved_num_template_parameter_lists
17108     = parser->num_template_parameter_lists;
17109   parser->num_template_parameter_lists = 0;
17110   /* We are not in a function body.  */
17111   saved_in_function_body = parser->in_function_body;
17112   parser->in_function_body = false;
17113   /* Or in a loop.  */
17114   in_statement = parser->in_statement;
17115   parser->in_statement = 0;
17116   /* Or in a switch.  */
17117   in_switch_statement_p = parser->in_switch_statement_p;
17118   parser->in_switch_statement_p = false;
17119   /* We are not immediately inside an extern "lang" block.  */
17120   saved_in_unbraced_linkage_specification_p
17121     = parser->in_unbraced_linkage_specification_p;
17122   parser->in_unbraced_linkage_specification_p = false;
17123
17124   /* Start the class.  */
17125   if (nested_name_specifier_p)
17126     {
17127       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17128       old_scope = push_inner_scope (scope);
17129     }
17130   type = begin_class_definition (type, attributes);
17131
17132   if (type == error_mark_node)
17133     /* If the type is erroneous, skip the entire body of the class.  */
17134     cp_parser_skip_to_closing_brace (parser);
17135   else
17136     /* Parse the member-specification.  */
17137     cp_parser_member_specification_opt (parser);
17138
17139   /* Look for the trailing `}'.  */
17140   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17141   /* Look for trailing attributes to apply to this class.  */
17142   if (cp_parser_allow_gnu_extensions_p (parser))
17143     attributes = cp_parser_attributes_opt (parser);
17144   if (type != error_mark_node)
17145     type = finish_struct (type, attributes);
17146   if (nested_name_specifier_p)
17147     pop_inner_scope (old_scope, scope);
17148
17149   /* We've finished a type definition.  Check for the common syntax
17150      error of forgetting a semicolon after the definition.  We need to
17151      be careful, as we can't just check for not-a-semicolon and be done
17152      with it; the user might have typed:
17153
17154      class X { } c = ...;
17155      class X { } *p = ...;
17156
17157      and so forth.  Instead, enumerate all the possible tokens that
17158      might follow this production; if we don't see one of them, then
17159      complain and silently insert the semicolon.  */
17160   {
17161     cp_token *token = cp_lexer_peek_token (parser->lexer);
17162     bool want_semicolon = true;
17163
17164     switch (token->type)
17165       {
17166       case CPP_NAME:
17167       case CPP_SEMICOLON:
17168       case CPP_MULT:
17169       case CPP_AND:
17170       case CPP_OPEN_PAREN:
17171       case CPP_CLOSE_PAREN:
17172       case CPP_COMMA:
17173         want_semicolon = false;
17174         break;
17175
17176         /* While it's legal for type qualifiers and storage class
17177            specifiers to follow type definitions in the grammar, only
17178            compiler testsuites contain code like that.  Assume that if
17179            we see such code, then what we're really seeing is a case
17180            like:
17181
17182            class X { }
17183            const <type> var = ...;
17184
17185            or
17186
17187            class Y { }
17188            static <type> func (...) ...
17189
17190            i.e. the qualifier or specifier applies to the next
17191            declaration.  To do so, however, we need to look ahead one
17192            more token to see if *that* token is a type specifier.
17193
17194            This code could be improved to handle:
17195
17196            class Z { }
17197            static const <type> var = ...;  */
17198       case CPP_KEYWORD:
17199         if (keyword_is_decl_specifier (token->keyword))
17200           {
17201             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17202
17203             /* Handling user-defined types here would be nice, but very
17204                tricky.  */
17205             want_semicolon
17206               = (lookahead->type == CPP_KEYWORD
17207                  && keyword_begins_type_specifier (lookahead->keyword));
17208           }
17209         break;
17210       default:
17211         break;
17212       }
17213
17214     /* If we don't have a type, then something is very wrong and we
17215        shouldn't try to do anything clever.  Likewise for not seeing the
17216        closing brace.  */
17217     if (closing_brace && TYPE_P (type) && want_semicolon)
17218       {
17219         cp_token_position prev
17220           = cp_lexer_previous_token_position (parser->lexer);
17221         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17222         location_t loc = prev_token->location;
17223
17224         if (CLASSTYPE_DECLARED_CLASS (type))
17225           error_at (loc, "expected %<;%> after class definition");
17226         else if (TREE_CODE (type) == RECORD_TYPE)
17227           error_at (loc, "expected %<;%> after struct definition");
17228         else if (TREE_CODE (type) == UNION_TYPE)
17229           error_at (loc, "expected %<;%> after union definition");
17230         else
17231           gcc_unreachable ();
17232
17233         /* Unget one token and smash it to look as though we encountered
17234            a semicolon in the input stream.  */
17235         cp_lexer_set_token_position (parser->lexer, prev);
17236         token = cp_lexer_peek_token (parser->lexer);
17237         token->type = CPP_SEMICOLON;
17238         token->keyword = RID_MAX;
17239       }
17240   }
17241
17242   /* If this class is not itself within the scope of another class,
17243      then we need to parse the bodies of all of the queued function
17244      definitions.  Note that the queued functions defined in a class
17245      are not always processed immediately following the
17246      class-specifier for that class.  Consider:
17247
17248        struct A {
17249          struct B { void f() { sizeof (A); } };
17250        };
17251
17252      If `f' were processed before the processing of `A' were
17253      completed, there would be no way to compute the size of `A'.
17254      Note that the nesting we are interested in here is lexical --
17255      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17256      for:
17257
17258        struct A { struct B; };
17259        struct A::B { void f() { } };
17260
17261      there is no need to delay the parsing of `A::B::f'.  */
17262   if (--parser->num_classes_being_defined == 0)
17263     {
17264       tree fn;
17265       tree class_type = NULL_TREE;
17266       tree pushed_scope = NULL_TREE;
17267       unsigned ix;
17268       cp_default_arg_entry *e;
17269
17270       /* In a first pass, parse default arguments to the functions.
17271          Then, in a second pass, parse the bodies of the functions.
17272          This two-phased approach handles cases like:
17273
17274             struct S {
17275               void f() { g(); }
17276               void g(int i = 3);
17277             };
17278
17279          */
17280       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17281                         ix, e)
17282         {
17283           fn = e->decl;
17284           /* If there are default arguments that have not yet been processed,
17285              take care of them now.  */
17286           if (class_type != e->class_type)
17287             {
17288               if (pushed_scope)
17289                 pop_scope (pushed_scope);
17290               class_type = e->class_type;
17291               pushed_scope = push_scope (class_type);
17292             }
17293           /* Make sure that any template parameters are in scope.  */
17294           maybe_begin_member_template_processing (fn);
17295           /* Parse the default argument expressions.  */
17296           cp_parser_late_parsing_default_args (parser, fn);
17297           /* Remove any template parameters from the symbol table.  */
17298           maybe_end_member_template_processing ();
17299         }
17300       if (pushed_scope)
17301         pop_scope (pushed_scope);
17302       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17303       /* Now parse the body of the functions.  */
17304       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17305         cp_parser_late_parsing_for_member (parser, fn);
17306       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17307     }
17308
17309   /* Put back any saved access checks.  */
17310   pop_deferring_access_checks ();
17311
17312   /* Restore saved state.  */
17313   parser->in_switch_statement_p = in_switch_statement_p;
17314   parser->in_statement = in_statement;
17315   parser->in_function_body = saved_in_function_body;
17316   parser->num_template_parameter_lists
17317     = saved_num_template_parameter_lists;
17318   parser->in_unbraced_linkage_specification_p
17319     = saved_in_unbraced_linkage_specification_p;
17320
17321   return type;
17322 }
17323
17324 static tree
17325 cp_parser_class_specifier (cp_parser* parser)
17326 {
17327   tree ret;
17328   timevar_push (TV_PARSE_STRUCT);
17329   ret = cp_parser_class_specifier_1 (parser);
17330   timevar_pop (TV_PARSE_STRUCT);
17331   return ret;
17332 }
17333
17334 /* Parse a class-head.
17335
17336    class-head:
17337      class-key identifier [opt] base-clause [opt]
17338      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17339      class-key nested-name-specifier [opt] template-id
17340        base-clause [opt]
17341
17342    class-virt-specifier:
17343      final
17344
17345    GNU Extensions:
17346      class-key attributes identifier [opt] base-clause [opt]
17347      class-key attributes nested-name-specifier identifier base-clause [opt]
17348      class-key attributes nested-name-specifier [opt] template-id
17349        base-clause [opt]
17350
17351    Upon return BASES is initialized to the list of base classes (or
17352    NULL, if there are none) in the same form returned by
17353    cp_parser_base_clause.
17354
17355    Returns the TYPE of the indicated class.  Sets
17356    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17357    involving a nested-name-specifier was used, and FALSE otherwise.
17358
17359    Returns error_mark_node if this is not a class-head.
17360
17361    Returns NULL_TREE if the class-head is syntactically valid, but
17362    semantically invalid in a way that means we should skip the entire
17363    body of the class.  */
17364
17365 static tree
17366 cp_parser_class_head (cp_parser* parser,
17367                       bool* nested_name_specifier_p,
17368                       tree *attributes_p,
17369                       tree *bases)
17370 {
17371   tree nested_name_specifier;
17372   enum tag_types class_key;
17373   tree id = NULL_TREE;
17374   tree type = NULL_TREE;
17375   tree attributes;
17376   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17377   bool template_id_p = false;
17378   bool qualified_p = false;
17379   bool invalid_nested_name_p = false;
17380   bool invalid_explicit_specialization_p = false;
17381   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17382   tree pushed_scope = NULL_TREE;
17383   unsigned num_templates;
17384   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17385   /* Assume no nested-name-specifier will be present.  */
17386   *nested_name_specifier_p = false;
17387   /* Assume no template parameter lists will be used in defining the
17388      type.  */
17389   num_templates = 0;
17390   parser->colon_corrects_to_scope_p = false;
17391
17392   *bases = NULL_TREE;
17393
17394   /* Look for the class-key.  */
17395   class_key = cp_parser_class_key (parser);
17396   if (class_key == none_type)
17397     return error_mark_node;
17398
17399   /* Parse the attributes.  */
17400   attributes = cp_parser_attributes_opt (parser);
17401
17402   /* If the next token is `::', that is invalid -- but sometimes
17403      people do try to write:
17404
17405        struct ::S {};
17406
17407      Handle this gracefully by accepting the extra qualifier, and then
17408      issuing an error about it later if this really is a
17409      class-head.  If it turns out just to be an elaborated type
17410      specifier, remain silent.  */
17411   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17412     qualified_p = true;
17413
17414   push_deferring_access_checks (dk_no_check);
17415
17416   /* Determine the name of the class.  Begin by looking for an
17417      optional nested-name-specifier.  */
17418   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17419   nested_name_specifier
17420     = cp_parser_nested_name_specifier_opt (parser,
17421                                            /*typename_keyword_p=*/false,
17422                                            /*check_dependency_p=*/false,
17423                                            /*type_p=*/false,
17424                                            /*is_declaration=*/false);
17425   /* If there was a nested-name-specifier, then there *must* be an
17426      identifier.  */
17427   if (nested_name_specifier)
17428     {
17429       type_start_token = cp_lexer_peek_token (parser->lexer);
17430       /* Although the grammar says `identifier', it really means
17431          `class-name' or `template-name'.  You are only allowed to
17432          define a class that has already been declared with this
17433          syntax.
17434
17435          The proposed resolution for Core Issue 180 says that wherever
17436          you see `class T::X' you should treat `X' as a type-name.
17437
17438          It is OK to define an inaccessible class; for example:
17439
17440            class A { class B; };
17441            class A::B {};
17442
17443          We do not know if we will see a class-name, or a
17444          template-name.  We look for a class-name first, in case the
17445          class-name is a template-id; if we looked for the
17446          template-name first we would stop after the template-name.  */
17447       cp_parser_parse_tentatively (parser);
17448       type = cp_parser_class_name (parser,
17449                                    /*typename_keyword_p=*/false,
17450                                    /*template_keyword_p=*/false,
17451                                    class_type,
17452                                    /*check_dependency_p=*/false,
17453                                    /*class_head_p=*/true,
17454                                    /*is_declaration=*/false);
17455       /* If that didn't work, ignore the nested-name-specifier.  */
17456       if (!cp_parser_parse_definitely (parser))
17457         {
17458           invalid_nested_name_p = true;
17459           type_start_token = cp_lexer_peek_token (parser->lexer);
17460           id = cp_parser_identifier (parser);
17461           if (id == error_mark_node)
17462             id = NULL_TREE;
17463         }
17464       /* If we could not find a corresponding TYPE, treat this
17465          declaration like an unqualified declaration.  */
17466       if (type == error_mark_node)
17467         nested_name_specifier = NULL_TREE;
17468       /* Otherwise, count the number of templates used in TYPE and its
17469          containing scopes.  */
17470       else
17471         {
17472           tree scope;
17473
17474           for (scope = TREE_TYPE (type);
17475                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17476                scope = (TYPE_P (scope)
17477                         ? TYPE_CONTEXT (scope)
17478                         : DECL_CONTEXT (scope)))
17479             if (TYPE_P (scope)
17480                 && CLASS_TYPE_P (scope)
17481                 && CLASSTYPE_TEMPLATE_INFO (scope)
17482                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17483                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17484               ++num_templates;
17485         }
17486     }
17487   /* Otherwise, the identifier is optional.  */
17488   else
17489     {
17490       /* We don't know whether what comes next is a template-id,
17491          an identifier, or nothing at all.  */
17492       cp_parser_parse_tentatively (parser);
17493       /* Check for a template-id.  */
17494       type_start_token = cp_lexer_peek_token (parser->lexer);
17495       id = cp_parser_template_id (parser,
17496                                   /*template_keyword_p=*/false,
17497                                   /*check_dependency_p=*/true,
17498                                   /*is_declaration=*/true);
17499       /* If that didn't work, it could still be an identifier.  */
17500       if (!cp_parser_parse_definitely (parser))
17501         {
17502           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17503             {
17504               type_start_token = cp_lexer_peek_token (parser->lexer);
17505               id = cp_parser_identifier (parser);
17506             }
17507           else
17508             id = NULL_TREE;
17509         }
17510       else
17511         {
17512           template_id_p = true;
17513           ++num_templates;
17514         }
17515     }
17516
17517   pop_deferring_access_checks ();
17518
17519   if (id)
17520     {
17521       cp_parser_check_for_invalid_template_id (parser, id,
17522                                                type_start_token->location);
17523       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17524     }
17525
17526   /* If it's not a `:' or a `{' then we can't really be looking at a
17527      class-head, since a class-head only appears as part of a
17528      class-specifier.  We have to detect this situation before calling
17529      xref_tag, since that has irreversible side-effects.  */
17530   if (!cp_parser_next_token_starts_class_definition_p (parser))
17531     {
17532       cp_parser_error (parser, "expected %<{%> or %<:%>");
17533       type = error_mark_node;
17534       goto out;
17535     }
17536
17537   /* At this point, we're going ahead with the class-specifier, even
17538      if some other problem occurs.  */
17539   cp_parser_commit_to_tentative_parse (parser);
17540   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17541     {
17542       cp_parser_error (parser,
17543                        "cannot specify %<override%> for a class");
17544       type = error_mark_node;
17545       goto out;
17546     }
17547   /* Issue the error about the overly-qualified name now.  */
17548   if (qualified_p)
17549     {
17550       cp_parser_error (parser,
17551                        "global qualification of class name is invalid");
17552       type = error_mark_node;
17553       goto out;
17554     }
17555   else if (invalid_nested_name_p)
17556     {
17557       cp_parser_error (parser,
17558                        "qualified name does not name a class");
17559       type = error_mark_node;
17560       goto out;
17561     }
17562   else if (nested_name_specifier)
17563     {
17564       tree scope;
17565
17566       /* Reject typedef-names in class heads.  */
17567       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17568         {
17569           error_at (type_start_token->location,
17570                     "invalid class name in declaration of %qD",
17571                     type);
17572           type = NULL_TREE;
17573           goto done;
17574         }
17575
17576       /* Figure out in what scope the declaration is being placed.  */
17577       scope = current_scope ();
17578       /* If that scope does not contain the scope in which the
17579          class was originally declared, the program is invalid.  */
17580       if (scope && !is_ancestor (scope, nested_name_specifier))
17581         {
17582           if (at_namespace_scope_p ())
17583             error_at (type_start_token->location,
17584                       "declaration of %qD in namespace %qD which does not "
17585                       "enclose %qD",
17586                       type, scope, nested_name_specifier);
17587           else
17588             error_at (type_start_token->location,
17589                       "declaration of %qD in %qD which does not enclose %qD",
17590                       type, scope, nested_name_specifier);
17591           type = NULL_TREE;
17592           goto done;
17593         }
17594       /* [dcl.meaning]
17595
17596          A declarator-id shall not be qualified except for the
17597          definition of a ... nested class outside of its class
17598          ... [or] the definition or explicit instantiation of a
17599          class member of a namespace outside of its namespace.  */
17600       if (scope == nested_name_specifier)
17601         {
17602           permerror (nested_name_specifier_token_start->location,
17603                      "extra qualification not allowed");
17604           nested_name_specifier = NULL_TREE;
17605           num_templates = 0;
17606         }
17607     }
17608   /* An explicit-specialization must be preceded by "template <>".  If
17609      it is not, try to recover gracefully.  */
17610   if (at_namespace_scope_p ()
17611       && parser->num_template_parameter_lists == 0
17612       && template_id_p)
17613     {
17614       error_at (type_start_token->location,
17615                 "an explicit specialization must be preceded by %<template <>%>");
17616       invalid_explicit_specialization_p = true;
17617       /* Take the same action that would have been taken by
17618          cp_parser_explicit_specialization.  */
17619       ++parser->num_template_parameter_lists;
17620       begin_specialization ();
17621     }
17622   /* There must be no "return" statements between this point and the
17623      end of this function; set "type "to the correct return value and
17624      use "goto done;" to return.  */
17625   /* Make sure that the right number of template parameters were
17626      present.  */
17627   if (!cp_parser_check_template_parameters (parser, num_templates,
17628                                             type_start_token->location,
17629                                             /*declarator=*/NULL))
17630     {
17631       /* If something went wrong, there is no point in even trying to
17632          process the class-definition.  */
17633       type = NULL_TREE;
17634       goto done;
17635     }
17636
17637   /* Look up the type.  */
17638   if (template_id_p)
17639     {
17640       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17641           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17642               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17643         {
17644           error_at (type_start_token->location,
17645                     "function template %qD redeclared as a class template", id);
17646           type = error_mark_node;
17647         }
17648       else
17649         {
17650           type = TREE_TYPE (id);
17651           type = maybe_process_partial_specialization (type);
17652         }
17653       if (nested_name_specifier)
17654         pushed_scope = push_scope (nested_name_specifier);
17655     }
17656   else if (nested_name_specifier)
17657     {
17658       tree class_type;
17659
17660       /* Given:
17661
17662             template <typename T> struct S { struct T };
17663             template <typename T> struct S<T>::T { };
17664
17665          we will get a TYPENAME_TYPE when processing the definition of
17666          `S::T'.  We need to resolve it to the actual type before we
17667          try to define it.  */
17668       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17669         {
17670           class_type = resolve_typename_type (TREE_TYPE (type),
17671                                               /*only_current_p=*/false);
17672           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17673             type = TYPE_NAME (class_type);
17674           else
17675             {
17676               cp_parser_error (parser, "could not resolve typename type");
17677               type = error_mark_node;
17678             }
17679         }
17680
17681       if (maybe_process_partial_specialization (TREE_TYPE (type))
17682           == error_mark_node)
17683         {
17684           type = NULL_TREE;
17685           goto done;
17686         }
17687
17688       class_type = current_class_type;
17689       /* Enter the scope indicated by the nested-name-specifier.  */
17690       pushed_scope = push_scope (nested_name_specifier);
17691       /* Get the canonical version of this type.  */
17692       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17693       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17694           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17695         {
17696           type = push_template_decl (type);
17697           if (type == error_mark_node)
17698             {
17699               type = NULL_TREE;
17700               goto done;
17701             }
17702         }
17703
17704       type = TREE_TYPE (type);
17705       *nested_name_specifier_p = true;
17706     }
17707   else      /* The name is not a nested name.  */
17708     {
17709       /* If the class was unnamed, create a dummy name.  */
17710       if (!id)
17711         id = make_anon_name ();
17712       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17713                        parser->num_template_parameter_lists);
17714     }
17715
17716   /* Indicate whether this class was declared as a `class' or as a
17717      `struct'.  */
17718   if (TREE_CODE (type) == RECORD_TYPE)
17719     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17720   cp_parser_check_class_key (class_key, type);
17721
17722   /* If this type was already complete, and we see another definition,
17723      that's an error.  */
17724   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17725     {
17726       error_at (type_start_token->location, "redefinition of %q#T",
17727                 type);
17728       error_at (type_start_token->location, "previous definition of %q+#T",
17729                 type);
17730       type = NULL_TREE;
17731       goto done;
17732     }
17733   else if (type == error_mark_node)
17734     type = NULL_TREE;
17735
17736   /* We will have entered the scope containing the class; the names of
17737      base classes should be looked up in that context.  For example:
17738
17739        struct A { struct B {}; struct C; };
17740        struct A::C : B {};
17741
17742      is valid.  */
17743
17744   /* Get the list of base-classes, if there is one.  */
17745   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17746     *bases = cp_parser_base_clause (parser);
17747
17748  done:
17749   /* Leave the scope given by the nested-name-specifier.  We will
17750      enter the class scope itself while processing the members.  */
17751   if (pushed_scope)
17752     pop_scope (pushed_scope);
17753
17754   if (invalid_explicit_specialization_p)
17755     {
17756       end_specialization ();
17757       --parser->num_template_parameter_lists;
17758     }
17759
17760   if (type)
17761     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17762   *attributes_p = attributes;
17763   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17764     CLASSTYPE_FINAL (type) = 1;
17765  out:
17766   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17767   return type;
17768 }
17769
17770 /* Parse a class-key.
17771
17772    class-key:
17773      class
17774      struct
17775      union
17776
17777    Returns the kind of class-key specified, or none_type to indicate
17778    error.  */
17779
17780 static enum tag_types
17781 cp_parser_class_key (cp_parser* parser)
17782 {
17783   cp_token *token;
17784   enum tag_types tag_type;
17785
17786   /* Look for the class-key.  */
17787   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17788   if (!token)
17789     return none_type;
17790
17791   /* Check to see if the TOKEN is a class-key.  */
17792   tag_type = cp_parser_token_is_class_key (token);
17793   if (!tag_type)
17794     cp_parser_error (parser, "expected class-key");
17795   return tag_type;
17796 }
17797
17798 /* Parse an (optional) member-specification.
17799
17800    member-specification:
17801      member-declaration member-specification [opt]
17802      access-specifier : member-specification [opt]  */
17803
17804 static void
17805 cp_parser_member_specification_opt (cp_parser* parser)
17806 {
17807   while (true)
17808     {
17809       cp_token *token;
17810       enum rid keyword;
17811
17812       /* Peek at the next token.  */
17813       token = cp_lexer_peek_token (parser->lexer);
17814       /* If it's a `}', or EOF then we've seen all the members.  */
17815       if (token->type == CPP_CLOSE_BRACE
17816           || token->type == CPP_EOF
17817           || token->type == CPP_PRAGMA_EOL)
17818         break;
17819
17820       /* See if this token is a keyword.  */
17821       keyword = token->keyword;
17822       switch (keyword)
17823         {
17824         case RID_PUBLIC:
17825         case RID_PROTECTED:
17826         case RID_PRIVATE:
17827           /* Consume the access-specifier.  */
17828           cp_lexer_consume_token (parser->lexer);
17829           /* Remember which access-specifier is active.  */
17830           current_access_specifier = token->u.value;
17831           /* Look for the `:'.  */
17832           cp_parser_require (parser, CPP_COLON, RT_COLON);
17833           break;
17834
17835         default:
17836           /* Accept #pragmas at class scope.  */
17837           if (token->type == CPP_PRAGMA)
17838             {
17839               cp_parser_pragma (parser, pragma_external);
17840               break;
17841             }
17842
17843           /* Otherwise, the next construction must be a
17844              member-declaration.  */
17845           cp_parser_member_declaration (parser);
17846         }
17847     }
17848 }
17849
17850 /* Parse a member-declaration.
17851
17852    member-declaration:
17853      decl-specifier-seq [opt] member-declarator-list [opt] ;
17854      function-definition ; [opt]
17855      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17856      using-declaration
17857      template-declaration
17858
17859    member-declarator-list:
17860      member-declarator
17861      member-declarator-list , member-declarator
17862
17863    member-declarator:
17864      declarator pure-specifier [opt]
17865      declarator constant-initializer [opt]
17866      identifier [opt] : constant-expression
17867
17868    GNU Extensions:
17869
17870    member-declaration:
17871      __extension__ member-declaration
17872
17873    member-declarator:
17874      declarator attributes [opt] pure-specifier [opt]
17875      declarator attributes [opt] constant-initializer [opt]
17876      identifier [opt] attributes [opt] : constant-expression  
17877
17878    C++0x Extensions:
17879
17880    member-declaration:
17881      static_assert-declaration  */
17882
17883 static void
17884 cp_parser_member_declaration (cp_parser* parser)
17885 {
17886   cp_decl_specifier_seq decl_specifiers;
17887   tree prefix_attributes;
17888   tree decl;
17889   int declares_class_or_enum;
17890   bool friend_p;
17891   cp_token *token = NULL;
17892   cp_token *decl_spec_token_start = NULL;
17893   cp_token *initializer_token_start = NULL;
17894   int saved_pedantic;
17895   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17896
17897   /* Check for the `__extension__' keyword.  */
17898   if (cp_parser_extension_opt (parser, &saved_pedantic))
17899     {
17900       /* Recurse.  */
17901       cp_parser_member_declaration (parser);
17902       /* Restore the old value of the PEDANTIC flag.  */
17903       pedantic = saved_pedantic;
17904
17905       return;
17906     }
17907
17908   /* Check for a template-declaration.  */
17909   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17910     {
17911       /* An explicit specialization here is an error condition, and we
17912          expect the specialization handler to detect and report this.  */
17913       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17914           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17915         cp_parser_explicit_specialization (parser);
17916       else
17917         cp_parser_template_declaration (parser, /*member_p=*/true);
17918
17919       return;
17920     }
17921
17922   /* Check for a using-declaration.  */
17923   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17924     {
17925       /* Parse the using-declaration.  */
17926       cp_parser_using_declaration (parser,
17927                                    /*access_declaration_p=*/false);
17928       return;
17929     }
17930
17931   /* Check for @defs.  */
17932   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17933     {
17934       tree ivar, member;
17935       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17936       ivar = ivar_chains;
17937       while (ivar)
17938         {
17939           member = ivar;
17940           ivar = TREE_CHAIN (member);
17941           TREE_CHAIN (member) = NULL_TREE;
17942           finish_member_declaration (member);
17943         }
17944       return;
17945     }
17946
17947   /* If the next token is `static_assert' we have a static assertion.  */
17948   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17949     {
17950       cp_parser_static_assert (parser, /*member_p=*/true);
17951       return;
17952     }
17953
17954   parser->colon_corrects_to_scope_p = false;
17955
17956   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17957     goto out;
17958
17959   /* Parse the decl-specifier-seq.  */
17960   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17961   cp_parser_decl_specifier_seq (parser,
17962                                 CP_PARSER_FLAGS_OPTIONAL,
17963                                 &decl_specifiers,
17964                                 &declares_class_or_enum);
17965   prefix_attributes = decl_specifiers.attributes;
17966   decl_specifiers.attributes = NULL_TREE;
17967   /* Check for an invalid type-name.  */
17968   if (!decl_specifiers.any_type_specifiers_p
17969       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17970     goto out;
17971   /* If there is no declarator, then the decl-specifier-seq should
17972      specify a type.  */
17973   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17974     {
17975       /* If there was no decl-specifier-seq, and the next token is a
17976          `;', then we have something like:
17977
17978            struct S { ; };
17979
17980          [class.mem]
17981
17982          Each member-declaration shall declare at least one member
17983          name of the class.  */
17984       if (!decl_specifiers.any_specifiers_p)
17985         {
17986           cp_token *token = cp_lexer_peek_token (parser->lexer);
17987           if (!in_system_header_at (token->location))
17988             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17989         }
17990       else
17991         {
17992           tree type;
17993
17994           /* See if this declaration is a friend.  */
17995           friend_p = cp_parser_friend_p (&decl_specifiers);
17996           /* If there were decl-specifiers, check to see if there was
17997              a class-declaration.  */
17998           type = check_tag_decl (&decl_specifiers);
17999           /* Nested classes have already been added to the class, but
18000              a `friend' needs to be explicitly registered.  */
18001           if (friend_p)
18002             {
18003               /* If the `friend' keyword was present, the friend must
18004                  be introduced with a class-key.  */
18005                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18006                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18007                           "in C++03 a class-key must be used "
18008                           "when declaring a friend");
18009                /* In this case:
18010
18011                     template <typename T> struct A {
18012                       friend struct A<T>::B;
18013                     };
18014
18015                   A<T>::B will be represented by a TYPENAME_TYPE, and
18016                   therefore not recognized by check_tag_decl.  */
18017                if (!type)
18018                  {
18019                    type = decl_specifiers.type;
18020                    if (type && TREE_CODE (type) == TYPE_DECL)
18021                      type = TREE_TYPE (type);
18022                  }
18023                if (!type || !TYPE_P (type))
18024                  error_at (decl_spec_token_start->location,
18025                            "friend declaration does not name a class or "
18026                            "function");
18027                else
18028                  make_friend_class (current_class_type, type,
18029                                     /*complain=*/true);
18030             }
18031           /* If there is no TYPE, an error message will already have
18032              been issued.  */
18033           else if (!type || type == error_mark_node)
18034             ;
18035           /* An anonymous aggregate has to be handled specially; such
18036              a declaration really declares a data member (with a
18037              particular type), as opposed to a nested class.  */
18038           else if (ANON_AGGR_TYPE_P (type))
18039             {
18040               /* Remove constructors and such from TYPE, now that we
18041                  know it is an anonymous aggregate.  */
18042               fixup_anonymous_aggr (type);
18043               /* And make the corresponding data member.  */
18044               decl = build_decl (decl_spec_token_start->location,
18045                                  FIELD_DECL, NULL_TREE, type);
18046               /* Add it to the class.  */
18047               finish_member_declaration (decl);
18048             }
18049           else
18050             cp_parser_check_access_in_redeclaration
18051                                               (TYPE_NAME (type),
18052                                                decl_spec_token_start->location);
18053         }
18054     }
18055   else
18056     {
18057       bool assume_semicolon = false;
18058
18059       /* See if these declarations will be friends.  */
18060       friend_p = cp_parser_friend_p (&decl_specifiers);
18061
18062       /* Keep going until we hit the `;' at the end of the
18063          declaration.  */
18064       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18065         {
18066           tree attributes = NULL_TREE;
18067           tree first_attribute;
18068
18069           /* Peek at the next token.  */
18070           token = cp_lexer_peek_token (parser->lexer);
18071
18072           /* Check for a bitfield declaration.  */
18073           if (token->type == CPP_COLON
18074               || (token->type == CPP_NAME
18075                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18076                   == CPP_COLON))
18077             {
18078               tree identifier;
18079               tree width;
18080
18081               /* Get the name of the bitfield.  Note that we cannot just
18082                  check TOKEN here because it may have been invalidated by
18083                  the call to cp_lexer_peek_nth_token above.  */
18084               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18085                 identifier = cp_parser_identifier (parser);
18086               else
18087                 identifier = NULL_TREE;
18088
18089               /* Consume the `:' token.  */
18090               cp_lexer_consume_token (parser->lexer);
18091               /* Get the width of the bitfield.  */
18092               width
18093                 = cp_parser_constant_expression (parser,
18094                                                  /*allow_non_constant=*/false,
18095                                                  NULL);
18096
18097               /* Look for attributes that apply to the bitfield.  */
18098               attributes = cp_parser_attributes_opt (parser);
18099               /* Remember which attributes are prefix attributes and
18100                  which are not.  */
18101               first_attribute = attributes;
18102               /* Combine the attributes.  */
18103               attributes = chainon (prefix_attributes, attributes);
18104
18105               /* Create the bitfield declaration.  */
18106               decl = grokbitfield (identifier
18107                                    ? make_id_declarator (NULL_TREE,
18108                                                          identifier,
18109                                                          sfk_none)
18110                                    : NULL,
18111                                    &decl_specifiers,
18112                                    width,
18113                                    attributes);
18114             }
18115           else
18116             {
18117               cp_declarator *declarator;
18118               tree initializer;
18119               tree asm_specification;
18120               int ctor_dtor_or_conv_p;
18121
18122               /* Parse the declarator.  */
18123               declarator
18124                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18125                                         &ctor_dtor_or_conv_p,
18126                                         /*parenthesized_p=*/NULL,
18127                                         /*member_p=*/true);
18128
18129               /* If something went wrong parsing the declarator, make sure
18130                  that we at least consume some tokens.  */
18131               if (declarator == cp_error_declarator)
18132                 {
18133                   /* Skip to the end of the statement.  */
18134                   cp_parser_skip_to_end_of_statement (parser);
18135                   /* If the next token is not a semicolon, that is
18136                      probably because we just skipped over the body of
18137                      a function.  So, we consume a semicolon if
18138                      present, but do not issue an error message if it
18139                      is not present.  */
18140                   if (cp_lexer_next_token_is (parser->lexer,
18141                                               CPP_SEMICOLON))
18142                     cp_lexer_consume_token (parser->lexer);
18143                   goto out;
18144                 }
18145
18146               if (declares_class_or_enum & 2)
18147                 cp_parser_check_for_definition_in_return_type
18148                                             (declarator, decl_specifiers.type,
18149                                              decl_specifiers.type_location);
18150
18151               /* Look for an asm-specification.  */
18152               asm_specification = cp_parser_asm_specification_opt (parser);
18153               /* Look for attributes that apply to the declaration.  */
18154               attributes = cp_parser_attributes_opt (parser);
18155               /* Remember which attributes are prefix attributes and
18156                  which are not.  */
18157               first_attribute = attributes;
18158               /* Combine the attributes.  */
18159               attributes = chainon (prefix_attributes, attributes);
18160
18161               /* If it's an `=', then we have a constant-initializer or a
18162                  pure-specifier.  It is not correct to parse the
18163                  initializer before registering the member declaration
18164                  since the member declaration should be in scope while
18165                  its initializer is processed.  However, the rest of the
18166                  front end does not yet provide an interface that allows
18167                  us to handle this correctly.  */
18168               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18169                 {
18170                   /* In [class.mem]:
18171
18172                      A pure-specifier shall be used only in the declaration of
18173                      a virtual function.
18174
18175                      A member-declarator can contain a constant-initializer
18176                      only if it declares a static member of integral or
18177                      enumeration type.
18178
18179                      Therefore, if the DECLARATOR is for a function, we look
18180                      for a pure-specifier; otherwise, we look for a
18181                      constant-initializer.  When we call `grokfield', it will
18182                      perform more stringent semantics checks.  */
18183                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18184                   if (function_declarator_p (declarator))
18185                     initializer = cp_parser_pure_specifier (parser);
18186                   else
18187                     /* Parse the initializer.  */
18188                     initializer = cp_parser_constant_initializer (parser);
18189                 }
18190               /* Otherwise, there is no initializer.  */
18191               else
18192                 initializer = NULL_TREE;
18193
18194               /* See if we are probably looking at a function
18195                  definition.  We are certainly not looking at a
18196                  member-declarator.  Calling `grokfield' has
18197                  side-effects, so we must not do it unless we are sure
18198                  that we are looking at a member-declarator.  */
18199               if (cp_parser_token_starts_function_definition_p
18200                   (cp_lexer_peek_token (parser->lexer)))
18201                 {
18202                   /* The grammar does not allow a pure-specifier to be
18203                      used when a member function is defined.  (It is
18204                      possible that this fact is an oversight in the
18205                      standard, since a pure function may be defined
18206                      outside of the class-specifier.  */
18207                   if (initializer)
18208                     error_at (initializer_token_start->location,
18209                               "pure-specifier on function-definition");
18210                   decl = cp_parser_save_member_function_body (parser,
18211                                                               &decl_specifiers,
18212                                                               declarator,
18213                                                               attributes);
18214                   /* If the member was not a friend, declare it here.  */
18215                   if (!friend_p)
18216                     finish_member_declaration (decl);
18217                   /* Peek at the next token.  */
18218                   token = cp_lexer_peek_token (parser->lexer);
18219                   /* If the next token is a semicolon, consume it.  */
18220                   if (token->type == CPP_SEMICOLON)
18221                     cp_lexer_consume_token (parser->lexer);
18222                   goto out;
18223                 }
18224               else
18225                 if (declarator->kind == cdk_function)
18226                   declarator->id_loc = token->location;
18227                 /* Create the declaration.  */
18228                 decl = grokfield (declarator, &decl_specifiers,
18229                                   initializer, /*init_const_expr_p=*/true,
18230                                   asm_specification,
18231                                   attributes);
18232             }
18233
18234           /* Reset PREFIX_ATTRIBUTES.  */
18235           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18236             attributes = TREE_CHAIN (attributes);
18237           if (attributes)
18238             TREE_CHAIN (attributes) = NULL_TREE;
18239
18240           /* If there is any qualification still in effect, clear it
18241              now; we will be starting fresh with the next declarator.  */
18242           parser->scope = NULL_TREE;
18243           parser->qualifying_scope = NULL_TREE;
18244           parser->object_scope = NULL_TREE;
18245           /* If it's a `,', then there are more declarators.  */
18246           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18247             cp_lexer_consume_token (parser->lexer);
18248           /* If the next token isn't a `;', then we have a parse error.  */
18249           else if (cp_lexer_next_token_is_not (parser->lexer,
18250                                                CPP_SEMICOLON))
18251             {
18252               /* The next token might be a ways away from where the
18253                  actual semicolon is missing.  Find the previous token
18254                  and use that for our error position.  */
18255               cp_token *token = cp_lexer_previous_token (parser->lexer);
18256               error_at (token->location,
18257                         "expected %<;%> at end of member declaration");
18258
18259               /* Assume that the user meant to provide a semicolon.  If
18260                  we were to cp_parser_skip_to_end_of_statement, we might
18261                  skip to a semicolon inside a member function definition
18262                  and issue nonsensical error messages.  */
18263               assume_semicolon = true;
18264             }
18265
18266           if (decl)
18267             {
18268               /* Add DECL to the list of members.  */
18269               if (!friend_p)
18270                 finish_member_declaration (decl);
18271
18272               if (TREE_CODE (decl) == FUNCTION_DECL)
18273                 cp_parser_save_default_args (parser, decl);
18274             }
18275
18276           if (assume_semicolon)
18277             goto out;
18278         }
18279     }
18280
18281   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18282  out:
18283   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18284 }
18285
18286 /* Parse a pure-specifier.
18287
18288    pure-specifier:
18289      = 0
18290
18291    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18292    Otherwise, ERROR_MARK_NODE is returned.  */
18293
18294 static tree
18295 cp_parser_pure_specifier (cp_parser* parser)
18296 {
18297   cp_token *token;
18298
18299   /* Look for the `=' token.  */
18300   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18301     return error_mark_node;
18302   /* Look for the `0' token.  */
18303   token = cp_lexer_peek_token (parser->lexer);
18304
18305   if (token->type == CPP_EOF
18306       || token->type == CPP_PRAGMA_EOL)
18307     return error_mark_node;
18308
18309   cp_lexer_consume_token (parser->lexer);
18310
18311   /* Accept = default or = delete in c++0x mode.  */
18312   if (token->keyword == RID_DEFAULT
18313       || token->keyword == RID_DELETE)
18314     {
18315       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18316       return token->u.value;
18317     }
18318
18319   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18320   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18321     {
18322       cp_parser_error (parser,
18323                        "invalid pure specifier (only %<= 0%> is allowed)");
18324       cp_parser_skip_to_end_of_statement (parser);
18325       return error_mark_node;
18326     }
18327   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18328     {
18329       error_at (token->location, "templates may not be %<virtual%>");
18330       return error_mark_node;
18331     }
18332
18333   return integer_zero_node;
18334 }
18335
18336 /* Parse a constant-initializer.
18337
18338    constant-initializer:
18339      = constant-expression
18340
18341    Returns a representation of the constant-expression.  */
18342
18343 static tree
18344 cp_parser_constant_initializer (cp_parser* parser)
18345 {
18346   /* Look for the `=' token.  */
18347   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18348     return error_mark_node;
18349
18350   /* It is invalid to write:
18351
18352        struct S { static const int i = { 7 }; };
18353
18354      */
18355   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18356     {
18357       cp_parser_error (parser,
18358                        "a brace-enclosed initializer is not allowed here");
18359       /* Consume the opening brace.  */
18360       cp_lexer_consume_token (parser->lexer);
18361       /* Skip the initializer.  */
18362       cp_parser_skip_to_closing_brace (parser);
18363       /* Look for the trailing `}'.  */
18364       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18365
18366       return error_mark_node;
18367     }
18368
18369   return cp_parser_constant_expression (parser,
18370                                         /*allow_non_constant=*/false,
18371                                         NULL);
18372 }
18373
18374 /* Derived classes [gram.class.derived] */
18375
18376 /* Parse a base-clause.
18377
18378    base-clause:
18379      : base-specifier-list
18380
18381    base-specifier-list:
18382      base-specifier ... [opt]
18383      base-specifier-list , base-specifier ... [opt]
18384
18385    Returns a TREE_LIST representing the base-classes, in the order in
18386    which they were declared.  The representation of each node is as
18387    described by cp_parser_base_specifier.
18388
18389    In the case that no bases are specified, this function will return
18390    NULL_TREE, not ERROR_MARK_NODE.  */
18391
18392 static tree
18393 cp_parser_base_clause (cp_parser* parser)
18394 {
18395   tree bases = NULL_TREE;
18396
18397   /* Look for the `:' that begins the list.  */
18398   cp_parser_require (parser, CPP_COLON, RT_COLON);
18399
18400   /* Scan the base-specifier-list.  */
18401   while (true)
18402     {
18403       cp_token *token;
18404       tree base;
18405       bool pack_expansion_p = false;
18406
18407       /* Look for the base-specifier.  */
18408       base = cp_parser_base_specifier (parser);
18409       /* Look for the (optional) ellipsis. */
18410       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18411         {
18412           /* Consume the `...'. */
18413           cp_lexer_consume_token (parser->lexer);
18414
18415           pack_expansion_p = true;
18416         }
18417
18418       /* Add BASE to the front of the list.  */
18419       if (base && base != error_mark_node)
18420         {
18421           if (pack_expansion_p)
18422             /* Make this a pack expansion type. */
18423             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18424
18425           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18426             {
18427               TREE_CHAIN (base) = bases;
18428               bases = base;
18429             }
18430         }
18431       /* Peek at the next token.  */
18432       token = cp_lexer_peek_token (parser->lexer);
18433       /* If it's not a comma, then the list is complete.  */
18434       if (token->type != CPP_COMMA)
18435         break;
18436       /* Consume the `,'.  */
18437       cp_lexer_consume_token (parser->lexer);
18438     }
18439
18440   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18441      base class had a qualified name.  However, the next name that
18442      appears is certainly not qualified.  */
18443   parser->scope = NULL_TREE;
18444   parser->qualifying_scope = NULL_TREE;
18445   parser->object_scope = NULL_TREE;
18446
18447   return nreverse (bases);
18448 }
18449
18450 /* Parse a base-specifier.
18451
18452    base-specifier:
18453      :: [opt] nested-name-specifier [opt] class-name
18454      virtual access-specifier [opt] :: [opt] nested-name-specifier
18455        [opt] class-name
18456      access-specifier virtual [opt] :: [opt] nested-name-specifier
18457        [opt] class-name
18458
18459    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18460    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18461    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18462    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18463
18464 static tree
18465 cp_parser_base_specifier (cp_parser* parser)
18466 {
18467   cp_token *token;
18468   bool done = false;
18469   bool virtual_p = false;
18470   bool duplicate_virtual_error_issued_p = false;
18471   bool duplicate_access_error_issued_p = false;
18472   bool class_scope_p, template_p;
18473   tree access = access_default_node;
18474   tree type;
18475
18476   /* Process the optional `virtual' and `access-specifier'.  */
18477   while (!done)
18478     {
18479       /* Peek at the next token.  */
18480       token = cp_lexer_peek_token (parser->lexer);
18481       /* Process `virtual'.  */
18482       switch (token->keyword)
18483         {
18484         case RID_VIRTUAL:
18485           /* If `virtual' appears more than once, issue an error.  */
18486           if (virtual_p && !duplicate_virtual_error_issued_p)
18487             {
18488               cp_parser_error (parser,
18489                                "%<virtual%> specified more than once in base-specified");
18490               duplicate_virtual_error_issued_p = true;
18491             }
18492
18493           virtual_p = true;
18494
18495           /* Consume the `virtual' token.  */
18496           cp_lexer_consume_token (parser->lexer);
18497
18498           break;
18499
18500         case RID_PUBLIC:
18501         case RID_PROTECTED:
18502         case RID_PRIVATE:
18503           /* If more than one access specifier appears, issue an
18504              error.  */
18505           if (access != access_default_node
18506               && !duplicate_access_error_issued_p)
18507             {
18508               cp_parser_error (parser,
18509                                "more than one access specifier in base-specified");
18510               duplicate_access_error_issued_p = true;
18511             }
18512
18513           access = ridpointers[(int) token->keyword];
18514
18515           /* Consume the access-specifier.  */
18516           cp_lexer_consume_token (parser->lexer);
18517
18518           break;
18519
18520         default:
18521           done = true;
18522           break;
18523         }
18524     }
18525   /* It is not uncommon to see programs mechanically, erroneously, use
18526      the 'typename' keyword to denote (dependent) qualified types
18527      as base classes.  */
18528   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18529     {
18530       token = cp_lexer_peek_token (parser->lexer);
18531       if (!processing_template_decl)
18532         error_at (token->location,
18533                   "keyword %<typename%> not allowed outside of templates");
18534       else
18535         error_at (token->location,
18536                   "keyword %<typename%> not allowed in this context "
18537                   "(the base class is implicitly a type)");
18538       cp_lexer_consume_token (parser->lexer);
18539     }
18540
18541   /* Look for the optional `::' operator.  */
18542   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18543   /* Look for the nested-name-specifier.  The simplest way to
18544      implement:
18545
18546        [temp.res]
18547
18548        The keyword `typename' is not permitted in a base-specifier or
18549        mem-initializer; in these contexts a qualified name that
18550        depends on a template-parameter is implicitly assumed to be a
18551        type name.
18552
18553      is to pretend that we have seen the `typename' keyword at this
18554      point.  */
18555   cp_parser_nested_name_specifier_opt (parser,
18556                                        /*typename_keyword_p=*/true,
18557                                        /*check_dependency_p=*/true,
18558                                        typename_type,
18559                                        /*is_declaration=*/true);
18560   /* If the base class is given by a qualified name, assume that names
18561      we see are type names or templates, as appropriate.  */
18562   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18563   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18564
18565   if (!parser->scope
18566       && cp_lexer_next_token_is_decltype (parser->lexer))
18567     /* DR 950 allows decltype as a base-specifier.  */
18568     type = cp_parser_decltype (parser);
18569   else
18570     {
18571       /* Otherwise, look for the class-name.  */
18572       type = cp_parser_class_name (parser,
18573                                    class_scope_p,
18574                                    template_p,
18575                                    typename_type,
18576                                    /*check_dependency_p=*/true,
18577                                    /*class_head_p=*/false,
18578                                    /*is_declaration=*/true);
18579       type = TREE_TYPE (type);
18580     }
18581
18582   if (type == error_mark_node)
18583     return error_mark_node;
18584
18585   return finish_base_specifier (type, access, virtual_p);
18586 }
18587
18588 /* Exception handling [gram.exception] */
18589
18590 /* Parse an (optional) exception-specification.
18591
18592    exception-specification:
18593      throw ( type-id-list [opt] )
18594
18595    Returns a TREE_LIST representing the exception-specification.  The
18596    TREE_VALUE of each node is a type.  */
18597
18598 static tree
18599 cp_parser_exception_specification_opt (cp_parser* parser)
18600 {
18601   cp_token *token;
18602   tree type_id_list;
18603   const char *saved_message;
18604
18605   /* Peek at the next token.  */
18606   token = cp_lexer_peek_token (parser->lexer);
18607
18608   /* Is it a noexcept-specification?  */
18609   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18610     {
18611       tree expr;
18612       cp_lexer_consume_token (parser->lexer);
18613
18614       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18615         {
18616           cp_lexer_consume_token (parser->lexer);
18617
18618           /* Types may not be defined in an exception-specification.  */
18619           saved_message = parser->type_definition_forbidden_message;
18620           parser->type_definition_forbidden_message
18621             = G_("types may not be defined in an exception-specification");
18622
18623           expr = cp_parser_constant_expression (parser, false, NULL);
18624
18625           /* Restore the saved message.  */
18626           parser->type_definition_forbidden_message = saved_message;
18627
18628           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18629         }
18630       else
18631         expr = boolean_true_node;
18632
18633       return build_noexcept_spec (expr, tf_warning_or_error);
18634     }
18635
18636   /* If it's not `throw', then there's no exception-specification.  */
18637   if (!cp_parser_is_keyword (token, RID_THROW))
18638     return NULL_TREE;
18639
18640 #if 0
18641   /* Enable this once a lot of code has transitioned to noexcept?  */
18642   if (cxx_dialect == cxx0x && !in_system_header)
18643     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18644              "deprecated in C++0x; use %<noexcept%> instead");
18645 #endif
18646
18647   /* Consume the `throw'.  */
18648   cp_lexer_consume_token (parser->lexer);
18649
18650   /* Look for the `('.  */
18651   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18652
18653   /* Peek at the next token.  */
18654   token = cp_lexer_peek_token (parser->lexer);
18655   /* If it's not a `)', then there is a type-id-list.  */
18656   if (token->type != CPP_CLOSE_PAREN)
18657     {
18658       /* Types may not be defined in an exception-specification.  */
18659       saved_message = parser->type_definition_forbidden_message;
18660       parser->type_definition_forbidden_message
18661         = G_("types may not be defined in an exception-specification");
18662       /* Parse the type-id-list.  */
18663       type_id_list = cp_parser_type_id_list (parser);
18664       /* Restore the saved message.  */
18665       parser->type_definition_forbidden_message = saved_message;
18666     }
18667   else
18668     type_id_list = empty_except_spec;
18669
18670   /* Look for the `)'.  */
18671   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18672
18673   return type_id_list;
18674 }
18675
18676 /* Parse an (optional) type-id-list.
18677
18678    type-id-list:
18679      type-id ... [opt]
18680      type-id-list , type-id ... [opt]
18681
18682    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18683    in the order that the types were presented.  */
18684
18685 static tree
18686 cp_parser_type_id_list (cp_parser* parser)
18687 {
18688   tree types = NULL_TREE;
18689
18690   while (true)
18691     {
18692       cp_token *token;
18693       tree type;
18694
18695       /* Get the next type-id.  */
18696       type = cp_parser_type_id (parser);
18697       /* Parse the optional ellipsis. */
18698       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18699         {
18700           /* Consume the `...'. */
18701           cp_lexer_consume_token (parser->lexer);
18702
18703           /* Turn the type into a pack expansion expression. */
18704           type = make_pack_expansion (type);
18705         }
18706       /* Add it to the list.  */
18707       types = add_exception_specifier (types, type, /*complain=*/1);
18708       /* Peek at the next token.  */
18709       token = cp_lexer_peek_token (parser->lexer);
18710       /* If it is not a `,', we are done.  */
18711       if (token->type != CPP_COMMA)
18712         break;
18713       /* Consume the `,'.  */
18714       cp_lexer_consume_token (parser->lexer);
18715     }
18716
18717   return nreverse (types);
18718 }
18719
18720 /* Parse a try-block.
18721
18722    try-block:
18723      try compound-statement handler-seq  */
18724
18725 static tree
18726 cp_parser_try_block (cp_parser* parser)
18727 {
18728   tree try_block;
18729
18730   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18731   try_block = begin_try_block ();
18732   cp_parser_compound_statement (parser, NULL, true, false);
18733   finish_try_block (try_block);
18734   cp_parser_handler_seq (parser);
18735   finish_handler_sequence (try_block);
18736
18737   return try_block;
18738 }
18739
18740 /* Parse a function-try-block.
18741
18742    function-try-block:
18743      try ctor-initializer [opt] function-body handler-seq  */
18744
18745 static bool
18746 cp_parser_function_try_block (cp_parser* parser)
18747 {
18748   tree compound_stmt;
18749   tree try_block;
18750   bool ctor_initializer_p;
18751
18752   /* Look for the `try' keyword.  */
18753   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18754     return false;
18755   /* Let the rest of the front end know where we are.  */
18756   try_block = begin_function_try_block (&compound_stmt);
18757   /* Parse the function-body.  */
18758   ctor_initializer_p
18759     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18760   /* We're done with the `try' part.  */
18761   finish_function_try_block (try_block);
18762   /* Parse the handlers.  */
18763   cp_parser_handler_seq (parser);
18764   /* We're done with the handlers.  */
18765   finish_function_handler_sequence (try_block, compound_stmt);
18766
18767   return ctor_initializer_p;
18768 }
18769
18770 /* Parse a handler-seq.
18771
18772    handler-seq:
18773      handler handler-seq [opt]  */
18774
18775 static void
18776 cp_parser_handler_seq (cp_parser* parser)
18777 {
18778   while (true)
18779     {
18780       cp_token *token;
18781
18782       /* Parse the handler.  */
18783       cp_parser_handler (parser);
18784       /* Peek at the next token.  */
18785       token = cp_lexer_peek_token (parser->lexer);
18786       /* If it's not `catch' then there are no more handlers.  */
18787       if (!cp_parser_is_keyword (token, RID_CATCH))
18788         break;
18789     }
18790 }
18791
18792 /* Parse a handler.
18793
18794    handler:
18795      catch ( exception-declaration ) compound-statement  */
18796
18797 static void
18798 cp_parser_handler (cp_parser* parser)
18799 {
18800   tree handler;
18801   tree declaration;
18802
18803   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18804   handler = begin_handler ();
18805   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18806   declaration = cp_parser_exception_declaration (parser);
18807   finish_handler_parms (declaration, handler);
18808   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18809   cp_parser_compound_statement (parser, NULL, false, false);
18810   finish_handler (handler);
18811 }
18812
18813 /* Parse an exception-declaration.
18814
18815    exception-declaration:
18816      type-specifier-seq declarator
18817      type-specifier-seq abstract-declarator
18818      type-specifier-seq
18819      ...
18820
18821    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18822    ellipsis variant is used.  */
18823
18824 static tree
18825 cp_parser_exception_declaration (cp_parser* parser)
18826 {
18827   cp_decl_specifier_seq type_specifiers;
18828   cp_declarator *declarator;
18829   const char *saved_message;
18830
18831   /* If it's an ellipsis, it's easy to handle.  */
18832   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18833     {
18834       /* Consume the `...' token.  */
18835       cp_lexer_consume_token (parser->lexer);
18836       return NULL_TREE;
18837     }
18838
18839   /* Types may not be defined in exception-declarations.  */
18840   saved_message = parser->type_definition_forbidden_message;
18841   parser->type_definition_forbidden_message
18842     = G_("types may not be defined in exception-declarations");
18843
18844   /* Parse the type-specifier-seq.  */
18845   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18846                                 /*is_trailing_return=*/false,
18847                                 &type_specifiers);
18848   /* If it's a `)', then there is no declarator.  */
18849   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18850     declarator = NULL;
18851   else
18852     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18853                                        /*ctor_dtor_or_conv_p=*/NULL,
18854                                        /*parenthesized_p=*/NULL,
18855                                        /*member_p=*/false);
18856
18857   /* Restore the saved message.  */
18858   parser->type_definition_forbidden_message = saved_message;
18859
18860   if (!type_specifiers.any_specifiers_p)
18861     return error_mark_node;
18862
18863   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18864 }
18865
18866 /* Parse a throw-expression.
18867
18868    throw-expression:
18869      throw assignment-expression [opt]
18870
18871    Returns a THROW_EXPR representing the throw-expression.  */
18872
18873 static tree
18874 cp_parser_throw_expression (cp_parser* parser)
18875 {
18876   tree expression;
18877   cp_token* token;
18878
18879   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18880   token = cp_lexer_peek_token (parser->lexer);
18881   /* Figure out whether or not there is an assignment-expression
18882      following the "throw" keyword.  */
18883   if (token->type == CPP_COMMA
18884       || token->type == CPP_SEMICOLON
18885       || token->type == CPP_CLOSE_PAREN
18886       || token->type == CPP_CLOSE_SQUARE
18887       || token->type == CPP_CLOSE_BRACE
18888       || token->type == CPP_COLON)
18889     expression = NULL_TREE;
18890   else
18891     expression = cp_parser_assignment_expression (parser,
18892                                                   /*cast_p=*/false, NULL);
18893
18894   return build_throw (expression);
18895 }
18896
18897 /* GNU Extensions */
18898
18899 /* Parse an (optional) asm-specification.
18900
18901    asm-specification:
18902      asm ( string-literal )
18903
18904    If the asm-specification is present, returns a STRING_CST
18905    corresponding to the string-literal.  Otherwise, returns
18906    NULL_TREE.  */
18907
18908 static tree
18909 cp_parser_asm_specification_opt (cp_parser* parser)
18910 {
18911   cp_token *token;
18912   tree asm_specification;
18913
18914   /* Peek at the next token.  */
18915   token = cp_lexer_peek_token (parser->lexer);
18916   /* If the next token isn't the `asm' keyword, then there's no
18917      asm-specification.  */
18918   if (!cp_parser_is_keyword (token, RID_ASM))
18919     return NULL_TREE;
18920
18921   /* Consume the `asm' token.  */
18922   cp_lexer_consume_token (parser->lexer);
18923   /* Look for the `('.  */
18924   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18925
18926   /* Look for the string-literal.  */
18927   asm_specification = cp_parser_string_literal (parser, false, false);
18928
18929   /* Look for the `)'.  */
18930   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18931
18932   return asm_specification;
18933 }
18934
18935 /* Parse an asm-operand-list.
18936
18937    asm-operand-list:
18938      asm-operand
18939      asm-operand-list , asm-operand
18940
18941    asm-operand:
18942      string-literal ( expression )
18943      [ string-literal ] string-literal ( expression )
18944
18945    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18946    each node is the expression.  The TREE_PURPOSE is itself a
18947    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18948    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18949    is a STRING_CST for the string literal before the parenthesis. Returns
18950    ERROR_MARK_NODE if any of the operands are invalid.  */
18951
18952 static tree
18953 cp_parser_asm_operand_list (cp_parser* parser)
18954 {
18955   tree asm_operands = NULL_TREE;
18956   bool invalid_operands = false;
18957
18958   while (true)
18959     {
18960       tree string_literal;
18961       tree expression;
18962       tree name;
18963
18964       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18965         {
18966           /* Consume the `[' token.  */
18967           cp_lexer_consume_token (parser->lexer);
18968           /* Read the operand name.  */
18969           name = cp_parser_identifier (parser);
18970           if (name != error_mark_node)
18971             name = build_string (IDENTIFIER_LENGTH (name),
18972                                  IDENTIFIER_POINTER (name));
18973           /* Look for the closing `]'.  */
18974           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18975         }
18976       else
18977         name = NULL_TREE;
18978       /* Look for the string-literal.  */
18979       string_literal = cp_parser_string_literal (parser, false, false);
18980
18981       /* Look for the `('.  */
18982       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18983       /* Parse the expression.  */
18984       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18985       /* Look for the `)'.  */
18986       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18987
18988       if (name == error_mark_node 
18989           || string_literal == error_mark_node 
18990           || expression == error_mark_node)
18991         invalid_operands = true;
18992
18993       /* Add this operand to the list.  */
18994       asm_operands = tree_cons (build_tree_list (name, string_literal),
18995                                 expression,
18996                                 asm_operands);
18997       /* If the next token is not a `,', there are no more
18998          operands.  */
18999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19000         break;
19001       /* Consume the `,'.  */
19002       cp_lexer_consume_token (parser->lexer);
19003     }
19004
19005   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19006 }
19007
19008 /* Parse an asm-clobber-list.
19009
19010    asm-clobber-list:
19011      string-literal
19012      asm-clobber-list , string-literal
19013
19014    Returns a TREE_LIST, indicating the clobbers in the order that they
19015    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19016
19017 static tree
19018 cp_parser_asm_clobber_list (cp_parser* parser)
19019 {
19020   tree clobbers = NULL_TREE;
19021
19022   while (true)
19023     {
19024       tree string_literal;
19025
19026       /* Look for the string literal.  */
19027       string_literal = cp_parser_string_literal (parser, false, false);
19028       /* Add it to the list.  */
19029       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19030       /* If the next token is not a `,', then the list is
19031          complete.  */
19032       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19033         break;
19034       /* Consume the `,' token.  */
19035       cp_lexer_consume_token (parser->lexer);
19036     }
19037
19038   return clobbers;
19039 }
19040
19041 /* Parse an asm-label-list.
19042
19043    asm-label-list:
19044      identifier
19045      asm-label-list , identifier
19046
19047    Returns a TREE_LIST, indicating the labels in the order that they
19048    appeared.  The TREE_VALUE of each node is a label.  */
19049
19050 static tree
19051 cp_parser_asm_label_list (cp_parser* parser)
19052 {
19053   tree labels = NULL_TREE;
19054
19055   while (true)
19056     {
19057       tree identifier, label, name;
19058
19059       /* Look for the identifier.  */
19060       identifier = cp_parser_identifier (parser);
19061       if (!error_operand_p (identifier))
19062         {
19063           label = lookup_label (identifier);
19064           if (TREE_CODE (label) == LABEL_DECL)
19065             {
19066               TREE_USED (label) = 1;
19067               check_goto (label);
19068               name = build_string (IDENTIFIER_LENGTH (identifier),
19069                                    IDENTIFIER_POINTER (identifier));
19070               labels = tree_cons (name, label, labels);
19071             }
19072         }
19073       /* If the next token is not a `,', then the list is
19074          complete.  */
19075       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19076         break;
19077       /* Consume the `,' token.  */
19078       cp_lexer_consume_token (parser->lexer);
19079     }
19080
19081   return nreverse (labels);
19082 }
19083
19084 /* Parse an (optional) series of attributes.
19085
19086    attributes:
19087      attributes attribute
19088
19089    attribute:
19090      __attribute__ (( attribute-list [opt] ))
19091
19092    The return value is as for cp_parser_attribute_list.  */
19093
19094 static tree
19095 cp_parser_attributes_opt (cp_parser* parser)
19096 {
19097   tree attributes = NULL_TREE;
19098
19099   while (true)
19100     {
19101       cp_token *token;
19102       tree attribute_list;
19103
19104       /* Peek at the next token.  */
19105       token = cp_lexer_peek_token (parser->lexer);
19106       /* If it's not `__attribute__', then we're done.  */
19107       if (token->keyword != RID_ATTRIBUTE)
19108         break;
19109
19110       /* Consume the `__attribute__' keyword.  */
19111       cp_lexer_consume_token (parser->lexer);
19112       /* Look for the two `(' tokens.  */
19113       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19114       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19115
19116       /* Peek at the next token.  */
19117       token = cp_lexer_peek_token (parser->lexer);
19118       if (token->type != CPP_CLOSE_PAREN)
19119         /* Parse the attribute-list.  */
19120         attribute_list = cp_parser_attribute_list (parser);
19121       else
19122         /* If the next token is a `)', then there is no attribute
19123            list.  */
19124         attribute_list = NULL;
19125
19126       /* Look for the two `)' tokens.  */
19127       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19128       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19129
19130       /* Add these new attributes to the list.  */
19131       attributes = chainon (attributes, attribute_list);
19132     }
19133
19134   return attributes;
19135 }
19136
19137 /* Parse an attribute-list.
19138
19139    attribute-list:
19140      attribute
19141      attribute-list , attribute
19142
19143    attribute:
19144      identifier
19145      identifier ( identifier )
19146      identifier ( identifier , expression-list )
19147      identifier ( expression-list )
19148
19149    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19150    to an attribute.  The TREE_PURPOSE of each node is the identifier
19151    indicating which attribute is in use.  The TREE_VALUE represents
19152    the arguments, if any.  */
19153
19154 static tree
19155 cp_parser_attribute_list (cp_parser* parser)
19156 {
19157   tree attribute_list = NULL_TREE;
19158   bool save_translate_strings_p = parser->translate_strings_p;
19159
19160   parser->translate_strings_p = false;
19161   while (true)
19162     {
19163       cp_token *token;
19164       tree identifier;
19165       tree attribute;
19166
19167       /* Look for the identifier.  We also allow keywords here; for
19168          example `__attribute__ ((const))' is legal.  */
19169       token = cp_lexer_peek_token (parser->lexer);
19170       if (token->type == CPP_NAME
19171           || token->type == CPP_KEYWORD)
19172         {
19173           tree arguments = NULL_TREE;
19174
19175           /* Consume the token.  */
19176           token = cp_lexer_consume_token (parser->lexer);
19177
19178           /* Save away the identifier that indicates which attribute
19179              this is.  */
19180           identifier = (token->type == CPP_KEYWORD) 
19181             /* For keywords, use the canonical spelling, not the
19182                parsed identifier.  */
19183             ? ridpointers[(int) token->keyword]
19184             : token->u.value;
19185           
19186           attribute = build_tree_list (identifier, NULL_TREE);
19187
19188           /* Peek at the next token.  */
19189           token = cp_lexer_peek_token (parser->lexer);
19190           /* If it's an `(', then parse the attribute arguments.  */
19191           if (token->type == CPP_OPEN_PAREN)
19192             {
19193               VEC(tree,gc) *vec;
19194               int attr_flag = (attribute_takes_identifier_p (identifier)
19195                                ? id_attr : normal_attr);
19196               vec = cp_parser_parenthesized_expression_list
19197                     (parser, attr_flag, /*cast_p=*/false,
19198                      /*allow_expansion_p=*/false,
19199                      /*non_constant_p=*/NULL);
19200               if (vec == NULL)
19201                 arguments = error_mark_node;
19202               else
19203                 {
19204                   arguments = build_tree_list_vec (vec);
19205                   release_tree_vector (vec);
19206                 }
19207               /* Save the arguments away.  */
19208               TREE_VALUE (attribute) = arguments;
19209             }
19210
19211           if (arguments != error_mark_node)
19212             {
19213               /* Add this attribute to the list.  */
19214               TREE_CHAIN (attribute) = attribute_list;
19215               attribute_list = attribute;
19216             }
19217
19218           token = cp_lexer_peek_token (parser->lexer);
19219         }
19220       /* Now, look for more attributes.  If the next token isn't a
19221          `,', we're done.  */
19222       if (token->type != CPP_COMMA)
19223         break;
19224
19225       /* Consume the comma and keep going.  */
19226       cp_lexer_consume_token (parser->lexer);
19227     }
19228   parser->translate_strings_p = save_translate_strings_p;
19229
19230   /* We built up the list in reverse order.  */
19231   return nreverse (attribute_list);
19232 }
19233
19234 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19235    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19236    current value of the PEDANTIC flag, regardless of whether or not
19237    the `__extension__' keyword is present.  The caller is responsible
19238    for restoring the value of the PEDANTIC flag.  */
19239
19240 static bool
19241 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19242 {
19243   /* Save the old value of the PEDANTIC flag.  */
19244   *saved_pedantic = pedantic;
19245
19246   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19247     {
19248       /* Consume the `__extension__' token.  */
19249       cp_lexer_consume_token (parser->lexer);
19250       /* We're not being pedantic while the `__extension__' keyword is
19251          in effect.  */
19252       pedantic = 0;
19253
19254       return true;
19255     }
19256
19257   return false;
19258 }
19259
19260 /* Parse a label declaration.
19261
19262    label-declaration:
19263      __label__ label-declarator-seq ;
19264
19265    label-declarator-seq:
19266      identifier , label-declarator-seq
19267      identifier  */
19268
19269 static void
19270 cp_parser_label_declaration (cp_parser* parser)
19271 {
19272   /* Look for the `__label__' keyword.  */
19273   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19274
19275   while (true)
19276     {
19277       tree identifier;
19278
19279       /* Look for an identifier.  */
19280       identifier = cp_parser_identifier (parser);
19281       /* If we failed, stop.  */
19282       if (identifier == error_mark_node)
19283         break;
19284       /* Declare it as a label.  */
19285       finish_label_decl (identifier);
19286       /* If the next token is a `;', stop.  */
19287       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19288         break;
19289       /* Look for the `,' separating the label declarations.  */
19290       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19291     }
19292
19293   /* Look for the final `;'.  */
19294   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19295 }
19296
19297 /* Support Functions */
19298
19299 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19300    NAME should have one of the representations used for an
19301    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19302    is returned.  If PARSER->SCOPE is a dependent type, then a
19303    SCOPE_REF is returned.
19304
19305    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19306    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19307    was formed.  Abstractly, such entities should not be passed to this
19308    function, because they do not need to be looked up, but it is
19309    simpler to check for this special case here, rather than at the
19310    call-sites.
19311
19312    In cases not explicitly covered above, this function returns a
19313    DECL, OVERLOAD, or baselink representing the result of the lookup.
19314    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19315    is returned.
19316
19317    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19318    (e.g., "struct") that was used.  In that case bindings that do not
19319    refer to types are ignored.
19320
19321    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19322    ignored.
19323
19324    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19325    are ignored.
19326
19327    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19328    types.
19329
19330    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19331    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19332    NULL_TREE otherwise.  */
19333
19334 static tree
19335 cp_parser_lookup_name (cp_parser *parser, tree name,
19336                        enum tag_types tag_type,
19337                        bool is_template,
19338                        bool is_namespace,
19339                        bool check_dependency,
19340                        tree *ambiguous_decls,
19341                        location_t name_location)
19342 {
19343   int flags = 0;
19344   tree decl;
19345   tree object_type = parser->context->object_type;
19346
19347   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19348     flags |= LOOKUP_COMPLAIN;
19349
19350   /* Assume that the lookup will be unambiguous.  */
19351   if (ambiguous_decls)
19352     *ambiguous_decls = NULL_TREE;
19353
19354   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19355      no longer valid.  Note that if we are parsing tentatively, and
19356      the parse fails, OBJECT_TYPE will be automatically restored.  */
19357   parser->context->object_type = NULL_TREE;
19358
19359   if (name == error_mark_node)
19360     return error_mark_node;
19361
19362   /* A template-id has already been resolved; there is no lookup to
19363      do.  */
19364   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19365     return name;
19366   if (BASELINK_P (name))
19367     {
19368       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19369                   == TEMPLATE_ID_EXPR);
19370       return name;
19371     }
19372
19373   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19374      it should already have been checked to make sure that the name
19375      used matches the type being destroyed.  */
19376   if (TREE_CODE (name) == BIT_NOT_EXPR)
19377     {
19378       tree type;
19379
19380       /* Figure out to which type this destructor applies.  */
19381       if (parser->scope)
19382         type = parser->scope;
19383       else if (object_type)
19384         type = object_type;
19385       else
19386         type = current_class_type;
19387       /* If that's not a class type, there is no destructor.  */
19388       if (!type || !CLASS_TYPE_P (type))
19389         return error_mark_node;
19390       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19391         lazily_declare_fn (sfk_destructor, type);
19392       if (!CLASSTYPE_DESTRUCTORS (type))
19393           return error_mark_node;
19394       /* If it was a class type, return the destructor.  */
19395       return CLASSTYPE_DESTRUCTORS (type);
19396     }
19397
19398   /* By this point, the NAME should be an ordinary identifier.  If
19399      the id-expression was a qualified name, the qualifying scope is
19400      stored in PARSER->SCOPE at this point.  */
19401   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19402
19403   /* Perform the lookup.  */
19404   if (parser->scope)
19405     {
19406       bool dependent_p;
19407
19408       if (parser->scope == error_mark_node)
19409         return error_mark_node;
19410
19411       /* If the SCOPE is dependent, the lookup must be deferred until
19412          the template is instantiated -- unless we are explicitly
19413          looking up names in uninstantiated templates.  Even then, we
19414          cannot look up the name if the scope is not a class type; it
19415          might, for example, be a template type parameter.  */
19416       dependent_p = (TYPE_P (parser->scope)
19417                      && dependent_scope_p (parser->scope));
19418       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19419           && dependent_p)
19420         /* Defer lookup.  */
19421         decl = error_mark_node;
19422       else
19423         {
19424           tree pushed_scope = NULL_TREE;
19425
19426           /* If PARSER->SCOPE is a dependent type, then it must be a
19427              class type, and we must not be checking dependencies;
19428              otherwise, we would have processed this lookup above.  So
19429              that PARSER->SCOPE is not considered a dependent base by
19430              lookup_member, we must enter the scope here.  */
19431           if (dependent_p)
19432             pushed_scope = push_scope (parser->scope);
19433
19434           /* If the PARSER->SCOPE is a template specialization, it
19435              may be instantiated during name lookup.  In that case,
19436              errors may be issued.  Even if we rollback the current
19437              tentative parse, those errors are valid.  */
19438           decl = lookup_qualified_name (parser->scope, name,
19439                                         tag_type != none_type,
19440                                         /*complain=*/true);
19441
19442           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19443              lookup result and the nested-name-specifier nominates a class C:
19444                * if the name specified after the nested-name-specifier, when
19445                looked up in C, is the injected-class-name of C (Clause 9), or
19446                * if the name specified after the nested-name-specifier is the
19447                same as the identifier or the simple-template-id's template-
19448                name in the last component of the nested-name-specifier,
19449              the name is instead considered to name the constructor of
19450              class C. [ Note: for example, the constructor is not an
19451              acceptable lookup result in an elaborated-type-specifier so
19452              the constructor would not be used in place of the
19453              injected-class-name. --end note ] Such a constructor name
19454              shall be used only in the declarator-id of a declaration that
19455              names a constructor or in a using-declaration.  */
19456           if (tag_type == none_type
19457               && DECL_SELF_REFERENCE_P (decl)
19458               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19459             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19460                                           tag_type != none_type,
19461                                           /*complain=*/true);
19462
19463           /* If we have a single function from a using decl, pull it out.  */
19464           if (TREE_CODE (decl) == OVERLOAD
19465               && !really_overloaded_fn (decl))
19466             decl = OVL_FUNCTION (decl);
19467
19468           if (pushed_scope)
19469             pop_scope (pushed_scope);
19470         }
19471
19472       /* If the scope is a dependent type and either we deferred lookup or
19473          we did lookup but didn't find the name, rememeber the name.  */
19474       if (decl == error_mark_node && TYPE_P (parser->scope)
19475           && dependent_type_p (parser->scope))
19476         {
19477           if (tag_type)
19478             {
19479               tree type;
19480
19481               /* The resolution to Core Issue 180 says that `struct
19482                  A::B' should be considered a type-name, even if `A'
19483                  is dependent.  */
19484               type = make_typename_type (parser->scope, name, tag_type,
19485                                          /*complain=*/tf_error);
19486               decl = TYPE_NAME (type);
19487             }
19488           else if (is_template
19489                    && (cp_parser_next_token_ends_template_argument_p (parser)
19490                        || cp_lexer_next_token_is (parser->lexer,
19491                                                   CPP_CLOSE_PAREN)))
19492             decl = make_unbound_class_template (parser->scope,
19493                                                 name, NULL_TREE,
19494                                                 /*complain=*/tf_error);
19495           else
19496             decl = build_qualified_name (/*type=*/NULL_TREE,
19497                                          parser->scope, name,
19498                                          is_template);
19499         }
19500       parser->qualifying_scope = parser->scope;
19501       parser->object_scope = NULL_TREE;
19502     }
19503   else if (object_type)
19504     {
19505       tree object_decl = NULL_TREE;
19506       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19507          OBJECT_TYPE is not a class.  */
19508       if (CLASS_TYPE_P (object_type))
19509         /* If the OBJECT_TYPE is a template specialization, it may
19510            be instantiated during name lookup.  In that case, errors
19511            may be issued.  Even if we rollback the current tentative
19512            parse, those errors are valid.  */
19513         object_decl = lookup_member (object_type,
19514                                      name,
19515                                      /*protect=*/0,
19516                                      tag_type != none_type);
19517       /* Look it up in the enclosing context, too.  */
19518       decl = lookup_name_real (name, tag_type != none_type,
19519                                /*nonclass=*/0,
19520                                /*block_p=*/true, is_namespace, flags);
19521       parser->object_scope = object_type;
19522       parser->qualifying_scope = NULL_TREE;
19523       if (object_decl)
19524         decl = object_decl;
19525     }
19526   else
19527     {
19528       decl = lookup_name_real (name, tag_type != none_type,
19529                                /*nonclass=*/0,
19530                                /*block_p=*/true, is_namespace, flags);
19531       parser->qualifying_scope = NULL_TREE;
19532       parser->object_scope = NULL_TREE;
19533     }
19534
19535   /* If the lookup failed, let our caller know.  */
19536   if (!decl || decl == error_mark_node)
19537     return error_mark_node;
19538
19539   /* Pull out the template from an injected-class-name (or multiple).  */
19540   if (is_template)
19541     decl = maybe_get_template_decl_from_type_decl (decl);
19542
19543   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19544   if (TREE_CODE (decl) == TREE_LIST)
19545     {
19546       if (ambiguous_decls)
19547         *ambiguous_decls = decl;
19548       /* The error message we have to print is too complicated for
19549          cp_parser_error, so we incorporate its actions directly.  */
19550       if (!cp_parser_simulate_error (parser))
19551         {
19552           error_at (name_location, "reference to %qD is ambiguous",
19553                     name);
19554           print_candidates (decl);
19555         }
19556       return error_mark_node;
19557     }
19558
19559   gcc_assert (DECL_P (decl)
19560               || TREE_CODE (decl) == OVERLOAD
19561               || TREE_CODE (decl) == SCOPE_REF
19562               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19563               || BASELINK_P (decl));
19564
19565   /* If we have resolved the name of a member declaration, check to
19566      see if the declaration is accessible.  When the name resolves to
19567      set of overloaded functions, accessibility is checked when
19568      overload resolution is done.
19569
19570      During an explicit instantiation, access is not checked at all,
19571      as per [temp.explicit].  */
19572   if (DECL_P (decl))
19573     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19574
19575   return decl;
19576 }
19577
19578 /* Like cp_parser_lookup_name, but for use in the typical case where
19579    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19580    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19581
19582 static tree
19583 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19584 {
19585   return cp_parser_lookup_name (parser, name,
19586                                 none_type,
19587                                 /*is_template=*/false,
19588                                 /*is_namespace=*/false,
19589                                 /*check_dependency=*/true,
19590                                 /*ambiguous_decls=*/NULL,
19591                                 location);
19592 }
19593
19594 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19595    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19596    true, the DECL indicates the class being defined in a class-head,
19597    or declared in an elaborated-type-specifier.
19598
19599    Otherwise, return DECL.  */
19600
19601 static tree
19602 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19603 {
19604   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19605      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19606
19607        struct A {
19608          template <typename T> struct B;
19609        };
19610
19611        template <typename T> struct A::B {};
19612
19613      Similarly, in an elaborated-type-specifier:
19614
19615        namespace N { struct X{}; }
19616
19617        struct A {
19618          template <typename T> friend struct N::X;
19619        };
19620
19621      However, if the DECL refers to a class type, and we are in
19622      the scope of the class, then the name lookup automatically
19623      finds the TYPE_DECL created by build_self_reference rather
19624      than a TEMPLATE_DECL.  For example, in:
19625
19626        template <class T> struct S {
19627          S s;
19628        };
19629
19630      there is no need to handle such case.  */
19631
19632   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19633     return DECL_TEMPLATE_RESULT (decl);
19634
19635   return decl;
19636 }
19637
19638 /* If too many, or too few, template-parameter lists apply to the
19639    declarator, issue an error message.  Returns TRUE if all went well,
19640    and FALSE otherwise.  */
19641
19642 static bool
19643 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19644                                                 cp_declarator *declarator,
19645                                                 location_t declarator_location)
19646 {
19647   unsigned num_templates;
19648
19649   /* We haven't seen any classes that involve template parameters yet.  */
19650   num_templates = 0;
19651
19652   switch (declarator->kind)
19653     {
19654     case cdk_id:
19655       if (declarator->u.id.qualifying_scope)
19656         {
19657           tree scope;
19658
19659           scope = declarator->u.id.qualifying_scope;
19660
19661           while (scope && CLASS_TYPE_P (scope))
19662             {
19663               /* You're supposed to have one `template <...>'
19664                  for every template class, but you don't need one
19665                  for a full specialization.  For example:
19666
19667                  template <class T> struct S{};
19668                  template <> struct S<int> { void f(); };
19669                  void S<int>::f () {}
19670
19671                  is correct; there shouldn't be a `template <>' for
19672                  the definition of `S<int>::f'.  */
19673               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19674                 /* If SCOPE does not have template information of any
19675                    kind, then it is not a template, nor is it nested
19676                    within a template.  */
19677                 break;
19678               if (explicit_class_specialization_p (scope))
19679                 break;
19680               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19681                 ++num_templates;
19682
19683               scope = TYPE_CONTEXT (scope);
19684             }
19685         }
19686       else if (TREE_CODE (declarator->u.id.unqualified_name)
19687                == TEMPLATE_ID_EXPR)
19688         /* If the DECLARATOR has the form `X<y>' then it uses one
19689            additional level of template parameters.  */
19690         ++num_templates;
19691
19692       return cp_parser_check_template_parameters 
19693         (parser, num_templates, declarator_location, declarator);
19694
19695
19696     case cdk_function:
19697     case cdk_array:
19698     case cdk_pointer:
19699     case cdk_reference:
19700     case cdk_ptrmem:
19701       return (cp_parser_check_declarator_template_parameters
19702               (parser, declarator->declarator, declarator_location));
19703
19704     case cdk_error:
19705       return true;
19706
19707     default:
19708       gcc_unreachable ();
19709     }
19710   return false;
19711 }
19712
19713 /* NUM_TEMPLATES were used in the current declaration.  If that is
19714    invalid, return FALSE and issue an error messages.  Otherwise,
19715    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19716    declarator and we can print more accurate diagnostics.  */
19717
19718 static bool
19719 cp_parser_check_template_parameters (cp_parser* parser,
19720                                      unsigned num_templates,
19721                                      location_t location,
19722                                      cp_declarator *declarator)
19723 {
19724   /* If there are the same number of template classes and parameter
19725      lists, that's OK.  */
19726   if (parser->num_template_parameter_lists == num_templates)
19727     return true;
19728   /* If there are more, but only one more, then we are referring to a
19729      member template.  That's OK too.  */
19730   if (parser->num_template_parameter_lists == num_templates + 1)
19731     return true;
19732   /* If there are more template classes than parameter lists, we have
19733      something like:
19734
19735        template <class T> void S<T>::R<T>::f ();  */
19736   if (parser->num_template_parameter_lists < num_templates)
19737     {
19738       if (declarator && !current_function_decl)
19739         error_at (location, "specializing member %<%T::%E%> "
19740                   "requires %<template<>%> syntax", 
19741                   declarator->u.id.qualifying_scope,
19742                   declarator->u.id.unqualified_name);
19743       else if (declarator)
19744         error_at (location, "invalid declaration of %<%T::%E%>",
19745                   declarator->u.id.qualifying_scope,
19746                   declarator->u.id.unqualified_name);
19747       else 
19748         error_at (location, "too few template-parameter-lists");
19749       return false;
19750     }
19751   /* Otherwise, there are too many template parameter lists.  We have
19752      something like:
19753
19754      template <class T> template <class U> void S::f();  */
19755   error_at (location, "too many template-parameter-lists");
19756   return false;
19757 }
19758
19759 /* Parse an optional `::' token indicating that the following name is
19760    from the global namespace.  If so, PARSER->SCOPE is set to the
19761    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19762    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19763    Returns the new value of PARSER->SCOPE, if the `::' token is
19764    present, and NULL_TREE otherwise.  */
19765
19766 static tree
19767 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19768 {
19769   cp_token *token;
19770
19771   /* Peek at the next token.  */
19772   token = cp_lexer_peek_token (parser->lexer);
19773   /* If we're looking at a `::' token then we're starting from the
19774      global namespace, not our current location.  */
19775   if (token->type == CPP_SCOPE)
19776     {
19777       /* Consume the `::' token.  */
19778       cp_lexer_consume_token (parser->lexer);
19779       /* Set the SCOPE so that we know where to start the lookup.  */
19780       parser->scope = global_namespace;
19781       parser->qualifying_scope = global_namespace;
19782       parser->object_scope = NULL_TREE;
19783
19784       return parser->scope;
19785     }
19786   else if (!current_scope_valid_p)
19787     {
19788       parser->scope = NULL_TREE;
19789       parser->qualifying_scope = NULL_TREE;
19790       parser->object_scope = NULL_TREE;
19791     }
19792
19793   return NULL_TREE;
19794 }
19795
19796 /* Returns TRUE if the upcoming token sequence is the start of a
19797    constructor declarator.  If FRIEND_P is true, the declarator is
19798    preceded by the `friend' specifier.  */
19799
19800 static bool
19801 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19802 {
19803   bool constructor_p;
19804   tree nested_name_specifier;
19805   cp_token *next_token;
19806
19807   /* The common case is that this is not a constructor declarator, so
19808      try to avoid doing lots of work if at all possible.  It's not
19809      valid declare a constructor at function scope.  */
19810   if (parser->in_function_body)
19811     return false;
19812   /* And only certain tokens can begin a constructor declarator.  */
19813   next_token = cp_lexer_peek_token (parser->lexer);
19814   if (next_token->type != CPP_NAME
19815       && next_token->type != CPP_SCOPE
19816       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19817       && next_token->type != CPP_TEMPLATE_ID)
19818     return false;
19819
19820   /* Parse tentatively; we are going to roll back all of the tokens
19821      consumed here.  */
19822   cp_parser_parse_tentatively (parser);
19823   /* Assume that we are looking at a constructor declarator.  */
19824   constructor_p = true;
19825
19826   /* Look for the optional `::' operator.  */
19827   cp_parser_global_scope_opt (parser,
19828                               /*current_scope_valid_p=*/false);
19829   /* Look for the nested-name-specifier.  */
19830   nested_name_specifier
19831     = (cp_parser_nested_name_specifier_opt (parser,
19832                                             /*typename_keyword_p=*/false,
19833                                             /*check_dependency_p=*/false,
19834                                             /*type_p=*/false,
19835                                             /*is_declaration=*/false));
19836   /* Outside of a class-specifier, there must be a
19837      nested-name-specifier.  */
19838   if (!nested_name_specifier &&
19839       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19840        || friend_p))
19841     constructor_p = false;
19842   else if (nested_name_specifier == error_mark_node)
19843     constructor_p = false;
19844
19845   /* If we have a class scope, this is easy; DR 147 says that S::S always
19846      names the constructor, and no other qualified name could.  */
19847   if (constructor_p && nested_name_specifier
19848       && CLASS_TYPE_P (nested_name_specifier))
19849     {
19850       tree id = cp_parser_unqualified_id (parser,
19851                                           /*template_keyword_p=*/false,
19852                                           /*check_dependency_p=*/false,
19853                                           /*declarator_p=*/true,
19854                                           /*optional_p=*/false);
19855       if (is_overloaded_fn (id))
19856         id = DECL_NAME (get_first_fn (id));
19857       if (!constructor_name_p (id, nested_name_specifier))
19858         constructor_p = false;
19859     }
19860   /* If we still think that this might be a constructor-declarator,
19861      look for a class-name.  */
19862   else if (constructor_p)
19863     {
19864       /* If we have:
19865
19866            template <typename T> struct S {
19867              S();
19868            };
19869
19870          we must recognize that the nested `S' names a class.  */
19871       tree type_decl;
19872       type_decl = cp_parser_class_name (parser,
19873                                         /*typename_keyword_p=*/false,
19874                                         /*template_keyword_p=*/false,
19875                                         none_type,
19876                                         /*check_dependency_p=*/false,
19877                                         /*class_head_p=*/false,
19878                                         /*is_declaration=*/false);
19879       /* If there was no class-name, then this is not a constructor.  */
19880       constructor_p = !cp_parser_error_occurred (parser);
19881
19882       /* If we're still considering a constructor, we have to see a `(',
19883          to begin the parameter-declaration-clause, followed by either a
19884          `)', an `...', or a decl-specifier.  We need to check for a
19885          type-specifier to avoid being fooled into thinking that:
19886
19887            S (f) (int);
19888
19889          is a constructor.  (It is actually a function named `f' that
19890          takes one parameter (of type `int') and returns a value of type
19891          `S'.  */
19892       if (constructor_p
19893           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19894         constructor_p = false;
19895
19896       if (constructor_p
19897           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19898           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19899           /* A parameter declaration begins with a decl-specifier,
19900              which is either the "attribute" keyword, a storage class
19901              specifier, or (usually) a type-specifier.  */
19902           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19903         {
19904           tree type;
19905           tree pushed_scope = NULL_TREE;
19906           unsigned saved_num_template_parameter_lists;
19907
19908           /* Names appearing in the type-specifier should be looked up
19909              in the scope of the class.  */
19910           if (current_class_type)
19911             type = NULL_TREE;
19912           else
19913             {
19914               type = TREE_TYPE (type_decl);
19915               if (TREE_CODE (type) == TYPENAME_TYPE)
19916                 {
19917                   type = resolve_typename_type (type,
19918                                                 /*only_current_p=*/false);
19919                   if (TREE_CODE (type) == TYPENAME_TYPE)
19920                     {
19921                       cp_parser_abort_tentative_parse (parser);
19922                       return false;
19923                     }
19924                 }
19925               pushed_scope = push_scope (type);
19926             }
19927
19928           /* Inside the constructor parameter list, surrounding
19929              template-parameter-lists do not apply.  */
19930           saved_num_template_parameter_lists
19931             = parser->num_template_parameter_lists;
19932           parser->num_template_parameter_lists = 0;
19933
19934           /* Look for the type-specifier.  */
19935           cp_parser_type_specifier (parser,
19936                                     CP_PARSER_FLAGS_NONE,
19937                                     /*decl_specs=*/NULL,
19938                                     /*is_declarator=*/true,
19939                                     /*declares_class_or_enum=*/NULL,
19940                                     /*is_cv_qualifier=*/NULL);
19941
19942           parser->num_template_parameter_lists
19943             = saved_num_template_parameter_lists;
19944
19945           /* Leave the scope of the class.  */
19946           if (pushed_scope)
19947             pop_scope (pushed_scope);
19948
19949           constructor_p = !cp_parser_error_occurred (parser);
19950         }
19951     }
19952
19953   /* We did not really want to consume any tokens.  */
19954   cp_parser_abort_tentative_parse (parser);
19955
19956   return constructor_p;
19957 }
19958
19959 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19960    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19961    they must be performed once we are in the scope of the function.
19962
19963    Returns the function defined.  */
19964
19965 static tree
19966 cp_parser_function_definition_from_specifiers_and_declarator
19967   (cp_parser* parser,
19968    cp_decl_specifier_seq *decl_specifiers,
19969    tree attributes,
19970    const cp_declarator *declarator)
19971 {
19972   tree fn;
19973   bool success_p;
19974
19975   /* Begin the function-definition.  */
19976   success_p = start_function (decl_specifiers, declarator, attributes);
19977
19978   /* The things we're about to see are not directly qualified by any
19979      template headers we've seen thus far.  */
19980   reset_specialization ();
19981
19982   /* If there were names looked up in the decl-specifier-seq that we
19983      did not check, check them now.  We must wait until we are in the
19984      scope of the function to perform the checks, since the function
19985      might be a friend.  */
19986   perform_deferred_access_checks ();
19987
19988   if (!success_p)
19989     {
19990       /* Skip the entire function.  */
19991       cp_parser_skip_to_end_of_block_or_statement (parser);
19992       fn = error_mark_node;
19993     }
19994   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19995     {
19996       /* Seen already, skip it.  An error message has already been output.  */
19997       cp_parser_skip_to_end_of_block_or_statement (parser);
19998       fn = current_function_decl;
19999       current_function_decl = NULL_TREE;
20000       /* If this is a function from a class, pop the nested class.  */
20001       if (current_class_name)
20002         pop_nested_class ();
20003     }
20004   else
20005     {
20006       timevar_id_t tv;
20007       if (DECL_DECLARED_INLINE_P (current_function_decl))
20008         tv = TV_PARSE_INLINE;
20009       else
20010         tv = TV_PARSE_FUNC;
20011       timevar_push (tv);
20012       fn = cp_parser_function_definition_after_declarator (parser,
20013                                                          /*inline_p=*/false);
20014       timevar_pop (tv);
20015     }
20016
20017   return fn;
20018 }
20019
20020 /* Parse the part of a function-definition that follows the
20021    declarator.  INLINE_P is TRUE iff this function is an inline
20022    function defined within a class-specifier.
20023
20024    Returns the function defined.  */
20025
20026 static tree
20027 cp_parser_function_definition_after_declarator (cp_parser* parser,
20028                                                 bool inline_p)
20029 {
20030   tree fn;
20031   bool ctor_initializer_p = false;
20032   bool saved_in_unbraced_linkage_specification_p;
20033   bool saved_in_function_body;
20034   unsigned saved_num_template_parameter_lists;
20035   cp_token *token;
20036
20037   saved_in_function_body = parser->in_function_body;
20038   parser->in_function_body = true;
20039   /* If the next token is `return', then the code may be trying to
20040      make use of the "named return value" extension that G++ used to
20041      support.  */
20042   token = cp_lexer_peek_token (parser->lexer);
20043   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20044     {
20045       /* Consume the `return' keyword.  */
20046       cp_lexer_consume_token (parser->lexer);
20047       /* Look for the identifier that indicates what value is to be
20048          returned.  */
20049       cp_parser_identifier (parser);
20050       /* Issue an error message.  */
20051       error_at (token->location,
20052                 "named return values are no longer supported");
20053       /* Skip tokens until we reach the start of the function body.  */
20054       while (true)
20055         {
20056           cp_token *token = cp_lexer_peek_token (parser->lexer);
20057           if (token->type == CPP_OPEN_BRACE
20058               || token->type == CPP_EOF
20059               || token->type == CPP_PRAGMA_EOL)
20060             break;
20061           cp_lexer_consume_token (parser->lexer);
20062         }
20063     }
20064   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20065      anything declared inside `f'.  */
20066   saved_in_unbraced_linkage_specification_p
20067     = parser->in_unbraced_linkage_specification_p;
20068   parser->in_unbraced_linkage_specification_p = false;
20069   /* Inside the function, surrounding template-parameter-lists do not
20070      apply.  */
20071   saved_num_template_parameter_lists
20072     = parser->num_template_parameter_lists;
20073   parser->num_template_parameter_lists = 0;
20074
20075   start_lambda_scope (current_function_decl);
20076
20077   /* If the next token is `try', then we are looking at a
20078      function-try-block.  */
20079   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20080     ctor_initializer_p = cp_parser_function_try_block (parser);
20081   /* A function-try-block includes the function-body, so we only do
20082      this next part if we're not processing a function-try-block.  */
20083   else
20084     ctor_initializer_p
20085       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20086
20087   finish_lambda_scope ();
20088
20089   /* Finish the function.  */
20090   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20091                         (inline_p ? 2 : 0));
20092   /* Generate code for it, if necessary.  */
20093   expand_or_defer_fn (fn);
20094   /* Restore the saved values.  */
20095   parser->in_unbraced_linkage_specification_p
20096     = saved_in_unbraced_linkage_specification_p;
20097   parser->num_template_parameter_lists
20098     = saved_num_template_parameter_lists;
20099   parser->in_function_body = saved_in_function_body;
20100
20101   return fn;
20102 }
20103
20104 /* Parse a template-declaration, assuming that the `export' (and
20105    `extern') keywords, if present, has already been scanned.  MEMBER_P
20106    is as for cp_parser_template_declaration.  */
20107
20108 static void
20109 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20110 {
20111   tree decl = NULL_TREE;
20112   VEC (deferred_access_check,gc) *checks;
20113   tree parameter_list;
20114   bool friend_p = false;
20115   bool need_lang_pop;
20116   cp_token *token;
20117
20118   /* Look for the `template' keyword.  */
20119   token = cp_lexer_peek_token (parser->lexer);
20120   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20121     return;
20122
20123   /* And the `<'.  */
20124   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20125     return;
20126   if (at_class_scope_p () && current_function_decl)
20127     {
20128       /* 14.5.2.2 [temp.mem]
20129
20130          A local class shall not have member templates.  */
20131       error_at (token->location,
20132                 "invalid declaration of member template in local class");
20133       cp_parser_skip_to_end_of_block_or_statement (parser);
20134       return;
20135     }
20136   /* [temp]
20137
20138      A template ... shall not have C linkage.  */
20139   if (current_lang_name == lang_name_c)
20140     {
20141       error_at (token->location, "template with C linkage");
20142       /* Give it C++ linkage to avoid confusing other parts of the
20143          front end.  */
20144       push_lang_context (lang_name_cplusplus);
20145       need_lang_pop = true;
20146     }
20147   else
20148     need_lang_pop = false;
20149
20150   /* We cannot perform access checks on the template parameter
20151      declarations until we know what is being declared, just as we
20152      cannot check the decl-specifier list.  */
20153   push_deferring_access_checks (dk_deferred);
20154
20155   /* If the next token is `>', then we have an invalid
20156      specialization.  Rather than complain about an invalid template
20157      parameter, issue an error message here.  */
20158   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20159     {
20160       cp_parser_error (parser, "invalid explicit specialization");
20161       begin_specialization ();
20162       parameter_list = NULL_TREE;
20163     }
20164   else
20165     {
20166       /* Parse the template parameters.  */
20167       parameter_list = cp_parser_template_parameter_list (parser);
20168       fixup_template_parms ();
20169     }
20170
20171   /* Get the deferred access checks from the parameter list.  These
20172      will be checked once we know what is being declared, as for a
20173      member template the checks must be performed in the scope of the
20174      class containing the member.  */
20175   checks = get_deferred_access_checks ();
20176
20177   /* Look for the `>'.  */
20178   cp_parser_skip_to_end_of_template_parameter_list (parser);
20179   /* We just processed one more parameter list.  */
20180   ++parser->num_template_parameter_lists;
20181   /* If the next token is `template', there are more template
20182      parameters.  */
20183   if (cp_lexer_next_token_is_keyword (parser->lexer,
20184                                       RID_TEMPLATE))
20185     cp_parser_template_declaration_after_export (parser, member_p);
20186   else
20187     {
20188       /* There are no access checks when parsing a template, as we do not
20189          know if a specialization will be a friend.  */
20190       push_deferring_access_checks (dk_no_check);
20191       token = cp_lexer_peek_token (parser->lexer);
20192       decl = cp_parser_single_declaration (parser,
20193                                            checks,
20194                                            member_p,
20195                                            /*explicit_specialization_p=*/false,
20196                                            &friend_p);
20197       pop_deferring_access_checks ();
20198
20199       /* If this is a member template declaration, let the front
20200          end know.  */
20201       if (member_p && !friend_p && decl)
20202         {
20203           if (TREE_CODE (decl) == TYPE_DECL)
20204             cp_parser_check_access_in_redeclaration (decl, token->location);
20205
20206           decl = finish_member_template_decl (decl);
20207         }
20208       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20209         make_friend_class (current_class_type, TREE_TYPE (decl),
20210                            /*complain=*/true);
20211     }
20212   /* We are done with the current parameter list.  */
20213   --parser->num_template_parameter_lists;
20214
20215   pop_deferring_access_checks ();
20216
20217   /* Finish up.  */
20218   finish_template_decl (parameter_list);
20219
20220   /* Register member declarations.  */
20221   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20222     finish_member_declaration (decl);
20223   /* For the erroneous case of a template with C linkage, we pushed an
20224      implicit C++ linkage scope; exit that scope now.  */
20225   if (need_lang_pop)
20226     pop_lang_context ();
20227   /* If DECL is a function template, we must return to parse it later.
20228      (Even though there is no definition, there might be default
20229      arguments that need handling.)  */
20230   if (member_p && decl
20231       && (TREE_CODE (decl) == FUNCTION_DECL
20232           || DECL_FUNCTION_TEMPLATE_P (decl)))
20233     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20234 }
20235
20236 /* Perform the deferred access checks from a template-parameter-list.
20237    CHECKS is a TREE_LIST of access checks, as returned by
20238    get_deferred_access_checks.  */
20239
20240 static void
20241 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20242 {
20243   ++processing_template_parmlist;
20244   perform_access_checks (checks);
20245   --processing_template_parmlist;
20246 }
20247
20248 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20249    `function-definition' sequence.  MEMBER_P is true, this declaration
20250    appears in a class scope.
20251
20252    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20253    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20254
20255 static tree
20256 cp_parser_single_declaration (cp_parser* parser,
20257                               VEC (deferred_access_check,gc)* checks,
20258                               bool member_p,
20259                               bool explicit_specialization_p,
20260                               bool* friend_p)
20261 {
20262   int declares_class_or_enum;
20263   tree decl = NULL_TREE;
20264   cp_decl_specifier_seq decl_specifiers;
20265   bool function_definition_p = false;
20266   cp_token *decl_spec_token_start;
20267
20268   /* This function is only used when processing a template
20269      declaration.  */
20270   gcc_assert (innermost_scope_kind () == sk_template_parms
20271               || innermost_scope_kind () == sk_template_spec);
20272
20273   /* Defer access checks until we know what is being declared.  */
20274   push_deferring_access_checks (dk_deferred);
20275
20276   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20277      alternative.  */
20278   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20279   cp_parser_decl_specifier_seq (parser,
20280                                 CP_PARSER_FLAGS_OPTIONAL,
20281                                 &decl_specifiers,
20282                                 &declares_class_or_enum);
20283   if (friend_p)
20284     *friend_p = cp_parser_friend_p (&decl_specifiers);
20285
20286   /* There are no template typedefs.  */
20287   if (decl_specifiers.specs[(int) ds_typedef])
20288     {
20289       error_at (decl_spec_token_start->location,
20290                 "template declaration of %<typedef%>");
20291       decl = error_mark_node;
20292     }
20293
20294   /* Gather up the access checks that occurred the
20295      decl-specifier-seq.  */
20296   stop_deferring_access_checks ();
20297
20298   /* Check for the declaration of a template class.  */
20299   if (declares_class_or_enum)
20300     {
20301       if (cp_parser_declares_only_class_p (parser))
20302         {
20303           decl = shadow_tag (&decl_specifiers);
20304
20305           /* In this case:
20306
20307                struct C {
20308                  friend template <typename T> struct A<T>::B;
20309                };
20310
20311              A<T>::B will be represented by a TYPENAME_TYPE, and
20312              therefore not recognized by shadow_tag.  */
20313           if (friend_p && *friend_p
20314               && !decl
20315               && decl_specifiers.type
20316               && TYPE_P (decl_specifiers.type))
20317             decl = decl_specifiers.type;
20318
20319           if (decl && decl != error_mark_node)
20320             decl = TYPE_NAME (decl);
20321           else
20322             decl = error_mark_node;
20323
20324           /* Perform access checks for template parameters.  */
20325           cp_parser_perform_template_parameter_access_checks (checks);
20326         }
20327     }
20328
20329   /* Complain about missing 'typename' or other invalid type names.  */
20330   if (!decl_specifiers.any_type_specifiers_p
20331       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20332     {
20333       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20334          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20335          the rest of this declaration.  */
20336       decl = error_mark_node;
20337       goto out;
20338     }
20339
20340   /* If it's not a template class, try for a template function.  If
20341      the next token is a `;', then this declaration does not declare
20342      anything.  But, if there were errors in the decl-specifiers, then
20343      the error might well have come from an attempted class-specifier.
20344      In that case, there's no need to warn about a missing declarator.  */
20345   if (!decl
20346       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20347           || decl_specifiers.type != error_mark_node))
20348     {
20349       decl = cp_parser_init_declarator (parser,
20350                                         &decl_specifiers,
20351                                         checks,
20352                                         /*function_definition_allowed_p=*/true,
20353                                         member_p,
20354                                         declares_class_or_enum,
20355                                         &function_definition_p,
20356                                         NULL);
20357
20358     /* 7.1.1-1 [dcl.stc]
20359
20360        A storage-class-specifier shall not be specified in an explicit
20361        specialization...  */
20362     if (decl
20363         && explicit_specialization_p
20364         && decl_specifiers.storage_class != sc_none)
20365       {
20366         error_at (decl_spec_token_start->location,
20367                   "explicit template specialization cannot have a storage class");
20368         decl = error_mark_node;
20369       }
20370     }
20371
20372   /* Look for a trailing `;' after the declaration.  */
20373   if (!function_definition_p
20374       && (decl == error_mark_node
20375           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20376     cp_parser_skip_to_end_of_block_or_statement (parser);
20377
20378  out:
20379   pop_deferring_access_checks ();
20380
20381   /* Clear any current qualification; whatever comes next is the start
20382      of something new.  */
20383   parser->scope = NULL_TREE;
20384   parser->qualifying_scope = NULL_TREE;
20385   parser->object_scope = NULL_TREE;
20386
20387   return decl;
20388 }
20389
20390 /* Parse a cast-expression that is not the operand of a unary "&".  */
20391
20392 static tree
20393 cp_parser_simple_cast_expression (cp_parser *parser)
20394 {
20395   return cp_parser_cast_expression (parser, /*address_p=*/false,
20396                                     /*cast_p=*/false, NULL);
20397 }
20398
20399 /* Parse a functional cast to TYPE.  Returns an expression
20400    representing the cast.  */
20401
20402 static tree
20403 cp_parser_functional_cast (cp_parser* parser, tree type)
20404 {
20405   VEC(tree,gc) *vec;
20406   tree expression_list;
20407   tree cast;
20408   bool nonconst_p;
20409
20410   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20411     {
20412       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20413       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20414       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20415       if (TREE_CODE (type) == TYPE_DECL)
20416         type = TREE_TYPE (type);
20417       return finish_compound_literal (type, expression_list,
20418                                       tf_warning_or_error);
20419     }
20420
20421
20422   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20423                                                  /*cast_p=*/true,
20424                                                  /*allow_expansion_p=*/true,
20425                                                  /*non_constant_p=*/NULL);
20426   if (vec == NULL)
20427     expression_list = error_mark_node;
20428   else
20429     {
20430       expression_list = build_tree_list_vec (vec);
20431       release_tree_vector (vec);
20432     }
20433
20434   cast = build_functional_cast (type, expression_list,
20435                                 tf_warning_or_error);
20436   /* [expr.const]/1: In an integral constant expression "only type
20437      conversions to integral or enumeration type can be used".  */
20438   if (TREE_CODE (type) == TYPE_DECL)
20439     type = TREE_TYPE (type);
20440   if (cast != error_mark_node
20441       && !cast_valid_in_integral_constant_expression_p (type)
20442       && cp_parser_non_integral_constant_expression (parser,
20443                                                      NIC_CONSTRUCTOR))
20444     return error_mark_node;
20445   return cast;
20446 }
20447
20448 /* Save the tokens that make up the body of a member function defined
20449    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20450    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20451    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20452    for the member function.  */
20453
20454 static tree
20455 cp_parser_save_member_function_body (cp_parser* parser,
20456                                      cp_decl_specifier_seq *decl_specifiers,
20457                                      cp_declarator *declarator,
20458                                      tree attributes)
20459 {
20460   cp_token *first;
20461   cp_token *last;
20462   tree fn;
20463
20464   /* Create the FUNCTION_DECL.  */
20465   fn = grokmethod (decl_specifiers, declarator, attributes);
20466   /* If something went badly wrong, bail out now.  */
20467   if (fn == error_mark_node)
20468     {
20469       /* If there's a function-body, skip it.  */
20470       if (cp_parser_token_starts_function_definition_p
20471           (cp_lexer_peek_token (parser->lexer)))
20472         cp_parser_skip_to_end_of_block_or_statement (parser);
20473       return error_mark_node;
20474     }
20475
20476   /* Remember it, if there default args to post process.  */
20477   cp_parser_save_default_args (parser, fn);
20478
20479   /* Save away the tokens that make up the body of the
20480      function.  */
20481   first = parser->lexer->next_token;
20482   /* We can have braced-init-list mem-initializers before the fn body.  */
20483   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20484     {
20485       cp_lexer_consume_token (parser->lexer);
20486       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20487              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20488         {
20489           /* cache_group will stop after an un-nested { } pair, too.  */
20490           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20491             break;
20492
20493           /* variadic mem-inits have ... after the ')'.  */
20494           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20495             cp_lexer_consume_token (parser->lexer);
20496         }
20497     }
20498   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20499   /* Handle function try blocks.  */
20500   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20501     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20502   last = parser->lexer->next_token;
20503
20504   /* Save away the inline definition; we will process it when the
20505      class is complete.  */
20506   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20507   DECL_PENDING_INLINE_P (fn) = 1;
20508
20509   /* We need to know that this was defined in the class, so that
20510      friend templates are handled correctly.  */
20511   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20512
20513   /* Add FN to the queue of functions to be parsed later.  */
20514   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20515
20516   return fn;
20517 }
20518
20519 /* Parse a template-argument-list, as well as the trailing ">" (but
20520    not the opening ">").  See cp_parser_template_argument_list for the
20521    return value.  */
20522
20523 static tree
20524 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20525 {
20526   tree arguments;
20527   tree saved_scope;
20528   tree saved_qualifying_scope;
20529   tree saved_object_scope;
20530   bool saved_greater_than_is_operator_p;
20531   int saved_unevaluated_operand;
20532   int saved_inhibit_evaluation_warnings;
20533
20534   /* [temp.names]
20535
20536      When parsing a template-id, the first non-nested `>' is taken as
20537      the end of the template-argument-list rather than a greater-than
20538      operator.  */
20539   saved_greater_than_is_operator_p
20540     = parser->greater_than_is_operator_p;
20541   parser->greater_than_is_operator_p = false;
20542   /* Parsing the argument list may modify SCOPE, so we save it
20543      here.  */
20544   saved_scope = parser->scope;
20545   saved_qualifying_scope = parser->qualifying_scope;
20546   saved_object_scope = parser->object_scope;
20547   /* We need to evaluate the template arguments, even though this
20548      template-id may be nested within a "sizeof".  */
20549   saved_unevaluated_operand = cp_unevaluated_operand;
20550   cp_unevaluated_operand = 0;
20551   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20552   c_inhibit_evaluation_warnings = 0;
20553   /* Parse the template-argument-list itself.  */
20554   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20555       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20556     arguments = NULL_TREE;
20557   else
20558     arguments = cp_parser_template_argument_list (parser);
20559   /* Look for the `>' that ends the template-argument-list. If we find
20560      a '>>' instead, it's probably just a typo.  */
20561   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20562     {
20563       if (cxx_dialect != cxx98)
20564         {
20565           /* In C++0x, a `>>' in a template argument list or cast
20566              expression is considered to be two separate `>'
20567              tokens. So, change the current token to a `>', but don't
20568              consume it: it will be consumed later when the outer
20569              template argument list (or cast expression) is parsed.
20570              Note that this replacement of `>' for `>>' is necessary
20571              even if we are parsing tentatively: in the tentative
20572              case, after calling
20573              cp_parser_enclosed_template_argument_list we will always
20574              throw away all of the template arguments and the first
20575              closing `>', either because the template argument list
20576              was erroneous or because we are replacing those tokens
20577              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20578              not have been thrown away) is needed either to close an
20579              outer template argument list or to complete a new-style
20580              cast.  */
20581           cp_token *token = cp_lexer_peek_token (parser->lexer);
20582           token->type = CPP_GREATER;
20583         }
20584       else if (!saved_greater_than_is_operator_p)
20585         {
20586           /* If we're in a nested template argument list, the '>>' has
20587             to be a typo for '> >'. We emit the error message, but we
20588             continue parsing and we push a '>' as next token, so that
20589             the argument list will be parsed correctly.  Note that the
20590             global source location is still on the token before the
20591             '>>', so we need to say explicitly where we want it.  */
20592           cp_token *token = cp_lexer_peek_token (parser->lexer);
20593           error_at (token->location, "%<>>%> should be %<> >%> "
20594                     "within a nested template argument list");
20595
20596           token->type = CPP_GREATER;
20597         }
20598       else
20599         {
20600           /* If this is not a nested template argument list, the '>>'
20601             is a typo for '>'. Emit an error message and continue.
20602             Same deal about the token location, but here we can get it
20603             right by consuming the '>>' before issuing the diagnostic.  */
20604           cp_token *token = cp_lexer_consume_token (parser->lexer);
20605           error_at (token->location,
20606                     "spurious %<>>%>, use %<>%> to terminate "
20607                     "a template argument list");
20608         }
20609     }
20610   else
20611     cp_parser_skip_to_end_of_template_parameter_list (parser);
20612   /* The `>' token might be a greater-than operator again now.  */
20613   parser->greater_than_is_operator_p
20614     = saved_greater_than_is_operator_p;
20615   /* Restore the SAVED_SCOPE.  */
20616   parser->scope = saved_scope;
20617   parser->qualifying_scope = saved_qualifying_scope;
20618   parser->object_scope = saved_object_scope;
20619   cp_unevaluated_operand = saved_unevaluated_operand;
20620   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20621
20622   return arguments;
20623 }
20624
20625 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20626    arguments, or the body of the function have not yet been parsed,
20627    parse them now.  */
20628
20629 static void
20630 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20631 {
20632   timevar_push (TV_PARSE_INMETH);
20633   /* If this member is a template, get the underlying
20634      FUNCTION_DECL.  */
20635   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20636     member_function = DECL_TEMPLATE_RESULT (member_function);
20637
20638   /* There should not be any class definitions in progress at this
20639      point; the bodies of members are only parsed outside of all class
20640      definitions.  */
20641   gcc_assert (parser->num_classes_being_defined == 0);
20642   /* While we're parsing the member functions we might encounter more
20643      classes.  We want to handle them right away, but we don't want
20644      them getting mixed up with functions that are currently in the
20645      queue.  */
20646   push_unparsed_function_queues (parser);
20647
20648   /* Make sure that any template parameters are in scope.  */
20649   maybe_begin_member_template_processing (member_function);
20650
20651   /* If the body of the function has not yet been parsed, parse it
20652      now.  */
20653   if (DECL_PENDING_INLINE_P (member_function))
20654     {
20655       tree function_scope;
20656       cp_token_cache *tokens;
20657
20658       /* The function is no longer pending; we are processing it.  */
20659       tokens = DECL_PENDING_INLINE_INFO (member_function);
20660       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20661       DECL_PENDING_INLINE_P (member_function) = 0;
20662
20663       /* If this is a local class, enter the scope of the containing
20664          function.  */
20665       function_scope = current_function_decl;
20666       if (function_scope)
20667         push_function_context ();
20668
20669       /* Push the body of the function onto the lexer stack.  */
20670       cp_parser_push_lexer_for_tokens (parser, tokens);
20671
20672       /* Let the front end know that we going to be defining this
20673          function.  */
20674       start_preparsed_function (member_function, NULL_TREE,
20675                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20676
20677       /* Don't do access checking if it is a templated function.  */
20678       if (processing_template_decl)
20679         push_deferring_access_checks (dk_no_check);
20680
20681       /* Now, parse the body of the function.  */
20682       cp_parser_function_definition_after_declarator (parser,
20683                                                       /*inline_p=*/true);
20684
20685       if (processing_template_decl)
20686         pop_deferring_access_checks ();
20687
20688       /* Leave the scope of the containing function.  */
20689       if (function_scope)
20690         pop_function_context ();
20691       cp_parser_pop_lexer (parser);
20692     }
20693
20694   /* Remove any template parameters from the symbol table.  */
20695   maybe_end_member_template_processing ();
20696
20697   /* Restore the queue.  */
20698   pop_unparsed_function_queues (parser);
20699   timevar_pop (TV_PARSE_INMETH);
20700 }
20701
20702 /* If DECL contains any default args, remember it on the unparsed
20703    functions queue.  */
20704
20705 static void
20706 cp_parser_save_default_args (cp_parser* parser, tree decl)
20707 {
20708   tree probe;
20709
20710   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20711        probe;
20712        probe = TREE_CHAIN (probe))
20713     if (TREE_PURPOSE (probe))
20714       {
20715         cp_default_arg_entry *entry
20716           = VEC_safe_push (cp_default_arg_entry, gc,
20717                            unparsed_funs_with_default_args, NULL);
20718         entry->class_type = current_class_type;
20719         entry->decl = decl;
20720         break;
20721       }
20722 }
20723
20724 /* FN is a FUNCTION_DECL which may contains a parameter with an
20725    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20726    assumes that the current scope is the scope in which the default
20727    argument should be processed.  */
20728
20729 static void
20730 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20731 {
20732   bool saved_local_variables_forbidden_p;
20733   tree parm, parmdecl;
20734
20735   /* While we're parsing the default args, we might (due to the
20736      statement expression extension) encounter more classes.  We want
20737      to handle them right away, but we don't want them getting mixed
20738      up with default args that are currently in the queue.  */
20739   push_unparsed_function_queues (parser);
20740
20741   /* Local variable names (and the `this' keyword) may not appear
20742      in a default argument.  */
20743   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20744   parser->local_variables_forbidden_p = true;
20745
20746   push_defarg_context (fn);
20747
20748   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20749          parmdecl = DECL_ARGUMENTS (fn);
20750        parm && parm != void_list_node;
20751        parm = TREE_CHAIN (parm),
20752          parmdecl = DECL_CHAIN (parmdecl))
20753     {
20754       cp_token_cache *tokens;
20755       tree default_arg = TREE_PURPOSE (parm);
20756       tree parsed_arg;
20757       VEC(tree,gc) *insts;
20758       tree copy;
20759       unsigned ix;
20760
20761       if (!default_arg)
20762         continue;
20763
20764       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20765         /* This can happen for a friend declaration for a function
20766            already declared with default arguments.  */
20767         continue;
20768
20769        /* Push the saved tokens for the default argument onto the parser's
20770           lexer stack.  */
20771       tokens = DEFARG_TOKENS (default_arg);
20772       cp_parser_push_lexer_for_tokens (parser, tokens);
20773
20774       start_lambda_scope (parmdecl);
20775
20776       /* Parse the assignment-expression.  */
20777       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20778       if (parsed_arg == error_mark_node)
20779         {
20780           cp_parser_pop_lexer (parser);
20781           continue;
20782         }
20783
20784       if (!processing_template_decl)
20785         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20786
20787       TREE_PURPOSE (parm) = parsed_arg;
20788
20789       /* Update any instantiations we've already created.  */
20790       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20791            VEC_iterate (tree, insts, ix, copy); ix++)
20792         TREE_PURPOSE (copy) = parsed_arg;
20793
20794       finish_lambda_scope ();
20795
20796       /* If the token stream has not been completely used up, then
20797          there was extra junk after the end of the default
20798          argument.  */
20799       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20800         cp_parser_error (parser, "expected %<,%>");
20801
20802       /* Revert to the main lexer.  */
20803       cp_parser_pop_lexer (parser);
20804     }
20805
20806   pop_defarg_context ();
20807
20808   /* Make sure no default arg is missing.  */
20809   check_default_args (fn);
20810
20811   /* Restore the state of local_variables_forbidden_p.  */
20812   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20813
20814   /* Restore the queue.  */
20815   pop_unparsed_function_queues (parser);
20816 }
20817
20818 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20819    either a TYPE or an expression, depending on the form of the
20820    input.  The KEYWORD indicates which kind of expression we have
20821    encountered.  */
20822
20823 static tree
20824 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20825 {
20826   tree expr = NULL_TREE;
20827   const char *saved_message;
20828   char *tmp;
20829   bool saved_integral_constant_expression_p;
20830   bool saved_non_integral_constant_expression_p;
20831   bool pack_expansion_p = false;
20832
20833   /* Types cannot be defined in a `sizeof' expression.  Save away the
20834      old message.  */
20835   saved_message = parser->type_definition_forbidden_message;
20836   /* And create the new one.  */
20837   tmp = concat ("types may not be defined in %<",
20838                 IDENTIFIER_POINTER (ridpointers[keyword]),
20839                 "%> expressions", NULL);
20840   parser->type_definition_forbidden_message = tmp;
20841
20842   /* The restrictions on constant-expressions do not apply inside
20843      sizeof expressions.  */
20844   saved_integral_constant_expression_p
20845     = parser->integral_constant_expression_p;
20846   saved_non_integral_constant_expression_p
20847     = parser->non_integral_constant_expression_p;
20848   parser->integral_constant_expression_p = false;
20849
20850   /* If it's a `...', then we are computing the length of a parameter
20851      pack.  */
20852   if (keyword == RID_SIZEOF
20853       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20854     {
20855       /* Consume the `...'.  */
20856       cp_lexer_consume_token (parser->lexer);
20857       maybe_warn_variadic_templates ();
20858
20859       /* Note that this is an expansion.  */
20860       pack_expansion_p = true;
20861     }
20862
20863   /* Do not actually evaluate the expression.  */
20864   ++cp_unevaluated_operand;
20865   ++c_inhibit_evaluation_warnings;
20866   /* If it's a `(', then we might be looking at the type-id
20867      construction.  */
20868   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20869     {
20870       tree type;
20871       bool saved_in_type_id_in_expr_p;
20872
20873       /* We can't be sure yet whether we're looking at a type-id or an
20874          expression.  */
20875       cp_parser_parse_tentatively (parser);
20876       /* Consume the `('.  */
20877       cp_lexer_consume_token (parser->lexer);
20878       /* Parse the type-id.  */
20879       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20880       parser->in_type_id_in_expr_p = true;
20881       type = cp_parser_type_id (parser);
20882       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20883       /* Now, look for the trailing `)'.  */
20884       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20885       /* If all went well, then we're done.  */
20886       if (cp_parser_parse_definitely (parser))
20887         {
20888           cp_decl_specifier_seq decl_specs;
20889
20890           /* Build a trivial decl-specifier-seq.  */
20891           clear_decl_specs (&decl_specs);
20892           decl_specs.type = type;
20893
20894           /* Call grokdeclarator to figure out what type this is.  */
20895           expr = grokdeclarator (NULL,
20896                                  &decl_specs,
20897                                  TYPENAME,
20898                                  /*initialized=*/0,
20899                                  /*attrlist=*/NULL);
20900         }
20901     }
20902
20903   /* If the type-id production did not work out, then we must be
20904      looking at the unary-expression production.  */
20905   if (!expr)
20906     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20907                                        /*cast_p=*/false, NULL);
20908
20909   if (pack_expansion_p)
20910     /* Build a pack expansion. */
20911     expr = make_pack_expansion (expr);
20912
20913   /* Go back to evaluating expressions.  */
20914   --cp_unevaluated_operand;
20915   --c_inhibit_evaluation_warnings;
20916
20917   /* Free the message we created.  */
20918   free (tmp);
20919   /* And restore the old one.  */
20920   parser->type_definition_forbidden_message = saved_message;
20921   parser->integral_constant_expression_p
20922     = saved_integral_constant_expression_p;
20923   parser->non_integral_constant_expression_p
20924     = saved_non_integral_constant_expression_p;
20925
20926   return expr;
20927 }
20928
20929 /* If the current declaration has no declarator, return true.  */
20930
20931 static bool
20932 cp_parser_declares_only_class_p (cp_parser *parser)
20933 {
20934   /* If the next token is a `;' or a `,' then there is no
20935      declarator.  */
20936   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20937           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20938 }
20939
20940 /* Update the DECL_SPECS to reflect the storage class indicated by
20941    KEYWORD.  */
20942
20943 static void
20944 cp_parser_set_storage_class (cp_parser *parser,
20945                              cp_decl_specifier_seq *decl_specs,
20946                              enum rid keyword,
20947                              location_t location)
20948 {
20949   cp_storage_class storage_class;
20950
20951   if (parser->in_unbraced_linkage_specification_p)
20952     {
20953       error_at (location, "invalid use of %qD in linkage specification",
20954                 ridpointers[keyword]);
20955       return;
20956     }
20957   else if (decl_specs->storage_class != sc_none)
20958     {
20959       decl_specs->conflicting_specifiers_p = true;
20960       return;
20961     }
20962
20963   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20964       && decl_specs->specs[(int) ds_thread])
20965     {
20966       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20967       decl_specs->specs[(int) ds_thread] = 0;
20968     }
20969
20970   switch (keyword)
20971     {
20972     case RID_AUTO:
20973       storage_class = sc_auto;
20974       break;
20975     case RID_REGISTER:
20976       storage_class = sc_register;
20977       break;
20978     case RID_STATIC:
20979       storage_class = sc_static;
20980       break;
20981     case RID_EXTERN:
20982       storage_class = sc_extern;
20983       break;
20984     case RID_MUTABLE:
20985       storage_class = sc_mutable;
20986       break;
20987     default:
20988       gcc_unreachable ();
20989     }
20990   decl_specs->storage_class = storage_class;
20991
20992   /* A storage class specifier cannot be applied alongside a typedef 
20993      specifier. If there is a typedef specifier present then set 
20994      conflicting_specifiers_p which will trigger an error later
20995      on in grokdeclarator. */
20996   if (decl_specs->specs[(int)ds_typedef])
20997     decl_specs->conflicting_specifiers_p = true;
20998 }
20999
21000 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
21001    is true, the type is a user-defined type; otherwise it is a
21002    built-in type specified by a keyword.  */
21003
21004 static void
21005 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21006                               tree type_spec,
21007                               location_t location,
21008                               bool user_defined_p)
21009 {
21010   decl_specs->any_specifiers_p = true;
21011
21012   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21013      (with, for example, in "typedef int wchar_t;") we remember that
21014      this is what happened.  In system headers, we ignore these
21015      declarations so that G++ can work with system headers that are not
21016      C++-safe.  */
21017   if (decl_specs->specs[(int) ds_typedef]
21018       && !user_defined_p
21019       && (type_spec == boolean_type_node
21020           || type_spec == char16_type_node
21021           || type_spec == char32_type_node
21022           || type_spec == wchar_type_node)
21023       && (decl_specs->type
21024           || decl_specs->specs[(int) ds_long]
21025           || decl_specs->specs[(int) ds_short]
21026           || decl_specs->specs[(int) ds_unsigned]
21027           || decl_specs->specs[(int) ds_signed]))
21028     {
21029       decl_specs->redefined_builtin_type = type_spec;
21030       if (!decl_specs->type)
21031         {
21032           decl_specs->type = type_spec;
21033           decl_specs->user_defined_type_p = false;
21034           decl_specs->type_location = location;
21035         }
21036     }
21037   else if (decl_specs->type)
21038     decl_specs->multiple_types_p = true;
21039   else
21040     {
21041       decl_specs->type = type_spec;
21042       decl_specs->user_defined_type_p = user_defined_p;
21043       decl_specs->redefined_builtin_type = NULL_TREE;
21044       decl_specs->type_location = location;
21045     }
21046 }
21047
21048 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21049    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
21050
21051 static bool
21052 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21053 {
21054   return decl_specifiers->specs[(int) ds_friend] != 0;
21055 }
21056
21057 /* Issue an error message indicating that TOKEN_DESC was expected.
21058    If KEYWORD is true, it indicated this function is called by
21059    cp_parser_require_keword and the required token can only be
21060    a indicated keyword. */
21061
21062 static void
21063 cp_parser_required_error (cp_parser *parser,
21064                           required_token token_desc,
21065                           bool keyword)
21066 {
21067   switch (token_desc)
21068     {
21069       case RT_NEW:
21070         cp_parser_error (parser, "expected %<new%>");
21071         return;
21072       case RT_DELETE:
21073         cp_parser_error (parser, "expected %<delete%>");
21074         return;
21075       case RT_RETURN:
21076         cp_parser_error (parser, "expected %<return%>");
21077         return;
21078       case RT_WHILE:
21079         cp_parser_error (parser, "expected %<while%>");
21080         return;
21081       case RT_EXTERN:
21082         cp_parser_error (parser, "expected %<extern%>");
21083         return;
21084       case RT_STATIC_ASSERT:
21085         cp_parser_error (parser, "expected %<static_assert%>");
21086         return;
21087       case RT_DECLTYPE:
21088         cp_parser_error (parser, "expected %<decltype%>");
21089         return;
21090       case RT_OPERATOR:
21091         cp_parser_error (parser, "expected %<operator%>");
21092         return;
21093       case RT_CLASS:
21094         cp_parser_error (parser, "expected %<class%>");
21095         return;
21096       case RT_TEMPLATE:
21097         cp_parser_error (parser, "expected %<template%>");
21098         return;
21099       case RT_NAMESPACE:
21100         cp_parser_error (parser, "expected %<namespace%>");
21101         return;
21102       case RT_USING:
21103         cp_parser_error (parser, "expected %<using%>");
21104         return;
21105       case RT_ASM:
21106         cp_parser_error (parser, "expected %<asm%>");
21107         return;
21108       case RT_TRY:
21109         cp_parser_error (parser, "expected %<try%>");
21110         return;
21111       case RT_CATCH:
21112         cp_parser_error (parser, "expected %<catch%>");
21113         return;
21114       case RT_THROW:
21115         cp_parser_error (parser, "expected %<throw%>");
21116         return;
21117       case RT_LABEL:
21118         cp_parser_error (parser, "expected %<__label__%>");
21119         return;
21120       case RT_AT_TRY:
21121         cp_parser_error (parser, "expected %<@try%>");
21122         return;
21123       case RT_AT_SYNCHRONIZED:
21124         cp_parser_error (parser, "expected %<@synchronized%>");
21125         return;
21126       case RT_AT_THROW:
21127         cp_parser_error (parser, "expected %<@throw%>");
21128         return;
21129       default:
21130         break;
21131     }
21132   if (!keyword)
21133     {
21134       switch (token_desc)
21135         {
21136           case RT_SEMICOLON:
21137             cp_parser_error (parser, "expected %<;%>");
21138             return;
21139           case RT_OPEN_PAREN:
21140             cp_parser_error (parser, "expected %<(%>");
21141             return;
21142           case RT_CLOSE_BRACE:
21143             cp_parser_error (parser, "expected %<}%>");
21144             return;
21145           case RT_OPEN_BRACE:
21146             cp_parser_error (parser, "expected %<{%>");
21147             return;
21148           case RT_CLOSE_SQUARE:
21149             cp_parser_error (parser, "expected %<]%>");
21150             return;
21151           case RT_OPEN_SQUARE:
21152             cp_parser_error (parser, "expected %<[%>");
21153             return;
21154           case RT_COMMA:
21155             cp_parser_error (parser, "expected %<,%>");
21156             return;
21157           case RT_SCOPE:
21158             cp_parser_error (parser, "expected %<::%>");
21159             return;
21160           case RT_LESS:
21161             cp_parser_error (parser, "expected %<<%>");
21162             return;
21163           case RT_GREATER:
21164             cp_parser_error (parser, "expected %<>%>");
21165             return;
21166           case RT_EQ:
21167             cp_parser_error (parser, "expected %<=%>");
21168             return;
21169           case RT_ELLIPSIS:
21170             cp_parser_error (parser, "expected %<...%>");
21171             return;
21172           case RT_MULT:
21173             cp_parser_error (parser, "expected %<*%>");
21174             return;
21175           case RT_COMPL:
21176             cp_parser_error (parser, "expected %<~%>");
21177             return;
21178           case RT_COLON:
21179             cp_parser_error (parser, "expected %<:%>");
21180             return;
21181           case RT_COLON_SCOPE:
21182             cp_parser_error (parser, "expected %<:%> or %<::%>");
21183             return;
21184           case RT_CLOSE_PAREN:
21185             cp_parser_error (parser, "expected %<)%>");
21186             return;
21187           case RT_COMMA_CLOSE_PAREN:
21188             cp_parser_error (parser, "expected %<,%> or %<)%>");
21189             return;
21190           case RT_PRAGMA_EOL:
21191             cp_parser_error (parser, "expected end of line");
21192             return;
21193           case RT_NAME:
21194             cp_parser_error (parser, "expected identifier");
21195             return;
21196           case RT_SELECT:
21197             cp_parser_error (parser, "expected selection-statement");
21198             return;
21199           case RT_INTERATION:
21200             cp_parser_error (parser, "expected iteration-statement");
21201             return;
21202           case RT_JUMP:
21203             cp_parser_error (parser, "expected jump-statement");
21204             return;
21205           case RT_CLASS_KEY:
21206             cp_parser_error (parser, "expected class-key");
21207             return;
21208           case RT_CLASS_TYPENAME_TEMPLATE:
21209             cp_parser_error (parser,
21210                  "expected %<class%>, %<typename%>, or %<template%>");
21211             return;
21212           default:
21213             gcc_unreachable ();
21214         }
21215     }
21216   else
21217     gcc_unreachable ();
21218 }
21219
21220
21221
21222 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
21223    issue an error message indicating that TOKEN_DESC was expected.
21224
21225    Returns the token consumed, if the token had the appropriate type.
21226    Otherwise, returns NULL.  */
21227
21228 static cp_token *
21229 cp_parser_require (cp_parser* parser,
21230                    enum cpp_ttype type,
21231                    required_token token_desc)
21232 {
21233   if (cp_lexer_next_token_is (parser->lexer, type))
21234     return cp_lexer_consume_token (parser->lexer);
21235   else
21236     {
21237       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21238       if (!cp_parser_simulate_error (parser))
21239         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21240       return NULL;
21241     }
21242 }
21243
21244 /* An error message is produced if the next token is not '>'.
21245    All further tokens are skipped until the desired token is
21246    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21247
21248 static void
21249 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21250 {
21251   /* Current level of '< ... >'.  */
21252   unsigned level = 0;
21253   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21254   unsigned nesting_depth = 0;
21255
21256   /* Are we ready, yet?  If not, issue error message.  */
21257   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21258     return;
21259
21260   /* Skip tokens until the desired token is found.  */
21261   while (true)
21262     {
21263       /* Peek at the next token.  */
21264       switch (cp_lexer_peek_token (parser->lexer)->type)
21265         {
21266         case CPP_LESS:
21267           if (!nesting_depth)
21268             ++level;
21269           break;
21270
21271         case CPP_RSHIFT:
21272           if (cxx_dialect == cxx98)
21273             /* C++0x views the `>>' operator as two `>' tokens, but
21274                C++98 does not. */
21275             break;
21276           else if (!nesting_depth && level-- == 0)
21277             {
21278               /* We've hit a `>>' where the first `>' closes the
21279                  template argument list, and the second `>' is
21280                  spurious.  Just consume the `>>' and stop; we've
21281                  already produced at least one error.  */
21282               cp_lexer_consume_token (parser->lexer);
21283               return;
21284             }
21285           /* Fall through for C++0x, so we handle the second `>' in
21286              the `>>'.  */
21287
21288         case CPP_GREATER:
21289           if (!nesting_depth && level-- == 0)
21290             {
21291               /* We've reached the token we want, consume it and stop.  */
21292               cp_lexer_consume_token (parser->lexer);
21293               return;
21294             }
21295           break;
21296
21297         case CPP_OPEN_PAREN:
21298         case CPP_OPEN_SQUARE:
21299           ++nesting_depth;
21300           break;
21301
21302         case CPP_CLOSE_PAREN:
21303         case CPP_CLOSE_SQUARE:
21304           if (nesting_depth-- == 0)
21305             return;
21306           break;
21307
21308         case CPP_EOF:
21309         case CPP_PRAGMA_EOL:
21310         case CPP_SEMICOLON:
21311         case CPP_OPEN_BRACE:
21312         case CPP_CLOSE_BRACE:
21313           /* The '>' was probably forgotten, don't look further.  */
21314           return;
21315
21316         default:
21317           break;
21318         }
21319
21320       /* Consume this token.  */
21321       cp_lexer_consume_token (parser->lexer);
21322     }
21323 }
21324
21325 /* If the next token is the indicated keyword, consume it.  Otherwise,
21326    issue an error message indicating that TOKEN_DESC was expected.
21327
21328    Returns the token consumed, if the token had the appropriate type.
21329    Otherwise, returns NULL.  */
21330
21331 static cp_token *
21332 cp_parser_require_keyword (cp_parser* parser,
21333                            enum rid keyword,
21334                            required_token token_desc)
21335 {
21336   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21337
21338   if (token && token->keyword != keyword)
21339     {
21340       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21341       return NULL;
21342     }
21343
21344   return token;
21345 }
21346
21347 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21348    function-definition.  */
21349
21350 static bool
21351 cp_parser_token_starts_function_definition_p (cp_token* token)
21352 {
21353   return (/* An ordinary function-body begins with an `{'.  */
21354           token->type == CPP_OPEN_BRACE
21355           /* A ctor-initializer begins with a `:'.  */
21356           || token->type == CPP_COLON
21357           /* A function-try-block begins with `try'.  */
21358           || token->keyword == RID_TRY
21359           /* The named return value extension begins with `return'.  */
21360           || token->keyword == RID_RETURN);
21361 }
21362
21363 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21364    definition.  */
21365
21366 static bool
21367 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21368 {
21369   cp_token *token;
21370
21371   token = cp_lexer_peek_token (parser->lexer);
21372   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21373 }
21374
21375 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21376    C++0x) ending a template-argument.  */
21377
21378 static bool
21379 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21380 {
21381   cp_token *token;
21382
21383   token = cp_lexer_peek_token (parser->lexer);
21384   return (token->type == CPP_COMMA 
21385           || token->type == CPP_GREATER
21386           || token->type == CPP_ELLIPSIS
21387           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21388 }
21389
21390 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21391    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21392
21393 static bool
21394 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21395                                                      size_t n)
21396 {
21397   cp_token *token;
21398
21399   token = cp_lexer_peek_nth_token (parser->lexer, n);
21400   if (token->type == CPP_LESS)
21401     return true;
21402   /* Check for the sequence `<::' in the original code. It would be lexed as
21403      `[:', where `[' is a digraph, and there is no whitespace before
21404      `:'.  */
21405   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21406     {
21407       cp_token *token2;
21408       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21409       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21410         return true;
21411     }
21412   return false;
21413 }
21414
21415 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21416    or none_type otherwise.  */
21417
21418 static enum tag_types
21419 cp_parser_token_is_class_key (cp_token* token)
21420 {
21421   switch (token->keyword)
21422     {
21423     case RID_CLASS:
21424       return class_type;
21425     case RID_STRUCT:
21426       return record_type;
21427     case RID_UNION:
21428       return union_type;
21429
21430     default:
21431       return none_type;
21432     }
21433 }
21434
21435 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21436
21437 static void
21438 cp_parser_check_class_key (enum tag_types class_key, tree type)
21439 {
21440   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21441     permerror (input_location, "%qs tag used in naming %q#T",
21442             class_key == union_type ? "union"
21443              : class_key == record_type ? "struct" : "class",
21444              type);
21445 }
21446
21447 /* Issue an error message if DECL is redeclared with different
21448    access than its original declaration [class.access.spec/3].
21449    This applies to nested classes and nested class templates.
21450    [class.mem/1].  */
21451
21452 static void
21453 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21454 {
21455   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21456     return;
21457
21458   if ((TREE_PRIVATE (decl)
21459        != (current_access_specifier == access_private_node))
21460       || (TREE_PROTECTED (decl)
21461           != (current_access_specifier == access_protected_node)))
21462     error_at (location, "%qD redeclared with different access", decl);
21463 }
21464
21465 /* Look for the `template' keyword, as a syntactic disambiguator.
21466    Return TRUE iff it is present, in which case it will be
21467    consumed.  */
21468
21469 static bool
21470 cp_parser_optional_template_keyword (cp_parser *parser)
21471 {
21472   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21473     {
21474       /* The `template' keyword can only be used within templates;
21475          outside templates the parser can always figure out what is a
21476          template and what is not.  */
21477       if (!processing_template_decl)
21478         {
21479           cp_token *token = cp_lexer_peek_token (parser->lexer);
21480           error_at (token->location,
21481                     "%<template%> (as a disambiguator) is only allowed "
21482                     "within templates");
21483           /* If this part of the token stream is rescanned, the same
21484              error message would be generated.  So, we purge the token
21485              from the stream.  */
21486           cp_lexer_purge_token (parser->lexer);
21487           return false;
21488         }
21489       else
21490         {
21491           /* Consume the `template' keyword.  */
21492           cp_lexer_consume_token (parser->lexer);
21493           return true;
21494         }
21495     }
21496
21497   return false;
21498 }
21499
21500 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21501    set PARSER->SCOPE, and perform other related actions.  */
21502
21503 static void
21504 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21505 {
21506   int i;
21507   struct tree_check *check_value;
21508   deferred_access_check *chk;
21509   VEC (deferred_access_check,gc) *checks;
21510
21511   /* Get the stored value.  */
21512   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21513   /* Perform any access checks that were deferred.  */
21514   checks = check_value->checks;
21515   if (checks)
21516     {
21517       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21518         perform_or_defer_access_check (chk->binfo,
21519                                        chk->decl,
21520                                        chk->diag_decl);
21521     }
21522   /* Set the scope from the stored value.  */
21523   parser->scope = check_value->value;
21524   parser->qualifying_scope = check_value->qualifying_scope;
21525   parser->object_scope = NULL_TREE;
21526 }
21527
21528 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21529    encounter the end of a block before what we were looking for.  */
21530
21531 static bool
21532 cp_parser_cache_group (cp_parser *parser,
21533                        enum cpp_ttype end,
21534                        unsigned depth)
21535 {
21536   while (true)
21537     {
21538       cp_token *token = cp_lexer_peek_token (parser->lexer);
21539
21540       /* Abort a parenthesized expression if we encounter a semicolon.  */
21541       if ((end == CPP_CLOSE_PAREN || depth == 0)
21542           && token->type == CPP_SEMICOLON)
21543         return true;
21544       /* If we've reached the end of the file, stop.  */
21545       if (token->type == CPP_EOF
21546           || (end != CPP_PRAGMA_EOL
21547               && token->type == CPP_PRAGMA_EOL))
21548         return true;
21549       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21550         /* We've hit the end of an enclosing block, so there's been some
21551            kind of syntax error.  */
21552         return true;
21553
21554       /* Consume the token.  */
21555       cp_lexer_consume_token (parser->lexer);
21556       /* See if it starts a new group.  */
21557       if (token->type == CPP_OPEN_BRACE)
21558         {
21559           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21560           /* In theory this should probably check end == '}', but
21561              cp_parser_save_member_function_body needs it to exit
21562              after either '}' or ')' when called with ')'.  */
21563           if (depth == 0)
21564             return false;
21565         }
21566       else if (token->type == CPP_OPEN_PAREN)
21567         {
21568           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21569           if (depth == 0 && end == CPP_CLOSE_PAREN)
21570             return false;
21571         }
21572       else if (token->type == CPP_PRAGMA)
21573         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21574       else if (token->type == end)
21575         return false;
21576     }
21577 }
21578
21579 /* Begin parsing tentatively.  We always save tokens while parsing
21580    tentatively so that if the tentative parsing fails we can restore the
21581    tokens.  */
21582
21583 static void
21584 cp_parser_parse_tentatively (cp_parser* parser)
21585 {
21586   /* Enter a new parsing context.  */
21587   parser->context = cp_parser_context_new (parser->context);
21588   /* Begin saving tokens.  */
21589   cp_lexer_save_tokens (parser->lexer);
21590   /* In order to avoid repetitive access control error messages,
21591      access checks are queued up until we are no longer parsing
21592      tentatively.  */
21593   push_deferring_access_checks (dk_deferred);
21594 }
21595
21596 /* Commit to the currently active tentative parse.  */
21597
21598 static void
21599 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21600 {
21601   cp_parser_context *context;
21602   cp_lexer *lexer;
21603
21604   /* Mark all of the levels as committed.  */
21605   lexer = parser->lexer;
21606   for (context = parser->context; context->next; context = context->next)
21607     {
21608       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21609         break;
21610       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21611       while (!cp_lexer_saving_tokens (lexer))
21612         lexer = lexer->next;
21613       cp_lexer_commit_tokens (lexer);
21614     }
21615 }
21616
21617 /* Abort the currently active tentative parse.  All consumed tokens
21618    will be rolled back, and no diagnostics will be issued.  */
21619
21620 static void
21621 cp_parser_abort_tentative_parse (cp_parser* parser)
21622 {
21623   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21624               || errorcount > 0);
21625   cp_parser_simulate_error (parser);
21626   /* Now, pretend that we want to see if the construct was
21627      successfully parsed.  */
21628   cp_parser_parse_definitely (parser);
21629 }
21630
21631 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21632    token stream.  Otherwise, commit to the tokens we have consumed.
21633    Returns true if no error occurred; false otherwise.  */
21634
21635 static bool
21636 cp_parser_parse_definitely (cp_parser* parser)
21637 {
21638   bool error_occurred;
21639   cp_parser_context *context;
21640
21641   /* Remember whether or not an error occurred, since we are about to
21642      destroy that information.  */
21643   error_occurred = cp_parser_error_occurred (parser);
21644   /* Remove the topmost context from the stack.  */
21645   context = parser->context;
21646   parser->context = context->next;
21647   /* If no parse errors occurred, commit to the tentative parse.  */
21648   if (!error_occurred)
21649     {
21650       /* Commit to the tokens read tentatively, unless that was
21651          already done.  */
21652       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21653         cp_lexer_commit_tokens (parser->lexer);
21654
21655       pop_to_parent_deferring_access_checks ();
21656     }
21657   /* Otherwise, if errors occurred, roll back our state so that things
21658      are just as they were before we began the tentative parse.  */
21659   else
21660     {
21661       cp_lexer_rollback_tokens (parser->lexer);
21662       pop_deferring_access_checks ();
21663     }
21664   /* Add the context to the front of the free list.  */
21665   context->next = cp_parser_context_free_list;
21666   cp_parser_context_free_list = context;
21667
21668   return !error_occurred;
21669 }
21670
21671 /* Returns true if we are parsing tentatively and are not committed to
21672    this tentative parse.  */
21673
21674 static bool
21675 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21676 {
21677   return (cp_parser_parsing_tentatively (parser)
21678           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21679 }
21680
21681 /* Returns nonzero iff an error has occurred during the most recent
21682    tentative parse.  */
21683
21684 static bool
21685 cp_parser_error_occurred (cp_parser* parser)
21686 {
21687   return (cp_parser_parsing_tentatively (parser)
21688           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21689 }
21690
21691 /* Returns nonzero if GNU extensions are allowed.  */
21692
21693 static bool
21694 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21695 {
21696   return parser->allow_gnu_extensions_p;
21697 }
21698 \f
21699 /* Objective-C++ Productions */
21700
21701
21702 /* Parse an Objective-C expression, which feeds into a primary-expression
21703    above.
21704
21705    objc-expression:
21706      objc-message-expression
21707      objc-string-literal
21708      objc-encode-expression
21709      objc-protocol-expression
21710      objc-selector-expression
21711
21712   Returns a tree representation of the expression.  */
21713
21714 static tree
21715 cp_parser_objc_expression (cp_parser* parser)
21716 {
21717   /* Try to figure out what kind of declaration is present.  */
21718   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21719
21720   switch (kwd->type)
21721     {
21722     case CPP_OPEN_SQUARE:
21723       return cp_parser_objc_message_expression (parser);
21724
21725     case CPP_OBJC_STRING:
21726       kwd = cp_lexer_consume_token (parser->lexer);
21727       return objc_build_string_object (kwd->u.value);
21728
21729     case CPP_KEYWORD:
21730       switch (kwd->keyword)
21731         {
21732         case RID_AT_ENCODE:
21733           return cp_parser_objc_encode_expression (parser);
21734
21735         case RID_AT_PROTOCOL:
21736           return cp_parser_objc_protocol_expression (parser);
21737
21738         case RID_AT_SELECTOR:
21739           return cp_parser_objc_selector_expression (parser);
21740
21741         default:
21742           break;
21743         }
21744     default:
21745       error_at (kwd->location,
21746                 "misplaced %<@%D%> Objective-C++ construct",
21747                 kwd->u.value);
21748       cp_parser_skip_to_end_of_block_or_statement (parser);
21749     }
21750
21751   return error_mark_node;
21752 }
21753
21754 /* Parse an Objective-C message expression.
21755
21756    objc-message-expression:
21757      [ objc-message-receiver objc-message-args ]
21758
21759    Returns a representation of an Objective-C message.  */
21760
21761 static tree
21762 cp_parser_objc_message_expression (cp_parser* parser)
21763 {
21764   tree receiver, messageargs;
21765
21766   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21767   receiver = cp_parser_objc_message_receiver (parser);
21768   messageargs = cp_parser_objc_message_args (parser);
21769   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21770
21771   return objc_build_message_expr (receiver, messageargs);
21772 }
21773
21774 /* Parse an objc-message-receiver.
21775
21776    objc-message-receiver:
21777      expression
21778      simple-type-specifier
21779
21780   Returns a representation of the type or expression.  */
21781
21782 static tree
21783 cp_parser_objc_message_receiver (cp_parser* parser)
21784 {
21785   tree rcv;
21786
21787   /* An Objective-C message receiver may be either (1) a type
21788      or (2) an expression.  */
21789   cp_parser_parse_tentatively (parser);
21790   rcv = cp_parser_expression (parser, false, NULL);
21791
21792   if (cp_parser_parse_definitely (parser))
21793     return rcv;
21794
21795   rcv = cp_parser_simple_type_specifier (parser,
21796                                          /*decl_specs=*/NULL,
21797                                          CP_PARSER_FLAGS_NONE);
21798
21799   return objc_get_class_reference (rcv);
21800 }
21801
21802 /* Parse the arguments and selectors comprising an Objective-C message.
21803
21804    objc-message-args:
21805      objc-selector
21806      objc-selector-args
21807      objc-selector-args , objc-comma-args
21808
21809    objc-selector-args:
21810      objc-selector [opt] : assignment-expression
21811      objc-selector-args objc-selector [opt] : assignment-expression
21812
21813    objc-comma-args:
21814      assignment-expression
21815      objc-comma-args , assignment-expression
21816
21817    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21818    selector arguments and TREE_VALUE containing a list of comma
21819    arguments.  */
21820
21821 static tree
21822 cp_parser_objc_message_args (cp_parser* parser)
21823 {
21824   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21825   bool maybe_unary_selector_p = true;
21826   cp_token *token = cp_lexer_peek_token (parser->lexer);
21827
21828   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21829     {
21830       tree selector = NULL_TREE, arg;
21831
21832       if (token->type != CPP_COLON)
21833         selector = cp_parser_objc_selector (parser);
21834
21835       /* Detect if we have a unary selector.  */
21836       if (maybe_unary_selector_p
21837           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21838         return build_tree_list (selector, NULL_TREE);
21839
21840       maybe_unary_selector_p = false;
21841       cp_parser_require (parser, CPP_COLON, RT_COLON);
21842       arg = cp_parser_assignment_expression (parser, false, NULL);
21843
21844       sel_args
21845         = chainon (sel_args,
21846                    build_tree_list (selector, arg));
21847
21848       token = cp_lexer_peek_token (parser->lexer);
21849     }
21850
21851   /* Handle non-selector arguments, if any. */
21852   while (token->type == CPP_COMMA)
21853     {
21854       tree arg;
21855
21856       cp_lexer_consume_token (parser->lexer);
21857       arg = cp_parser_assignment_expression (parser, false, NULL);
21858
21859       addl_args
21860         = chainon (addl_args,
21861                    build_tree_list (NULL_TREE, arg));
21862
21863       token = cp_lexer_peek_token (parser->lexer);
21864     }
21865
21866   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21867     {
21868       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21869       return build_tree_list (error_mark_node, error_mark_node);
21870     }
21871
21872   return build_tree_list (sel_args, addl_args);
21873 }
21874
21875 /* Parse an Objective-C encode expression.
21876
21877    objc-encode-expression:
21878      @encode objc-typename
21879
21880    Returns an encoded representation of the type argument.  */
21881
21882 static tree
21883 cp_parser_objc_encode_expression (cp_parser* parser)
21884 {
21885   tree type;
21886   cp_token *token;
21887
21888   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21889   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21890   token = cp_lexer_peek_token (parser->lexer);
21891   type = complete_type (cp_parser_type_id (parser));
21892   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21893
21894   if (!type)
21895     {
21896       error_at (token->location, 
21897                 "%<@encode%> must specify a type as an argument");
21898       return error_mark_node;
21899     }
21900
21901   /* This happens if we find @encode(T) (where T is a template
21902      typename or something dependent on a template typename) when
21903      parsing a template.  In that case, we can't compile it
21904      immediately, but we rather create an AT_ENCODE_EXPR which will
21905      need to be instantiated when the template is used.
21906   */
21907   if (dependent_type_p (type))
21908     {
21909       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21910       TREE_READONLY (value) = 1;
21911       return value;
21912     }
21913
21914   return objc_build_encode_expr (type);
21915 }
21916
21917 /* Parse an Objective-C @defs expression.  */
21918
21919 static tree
21920 cp_parser_objc_defs_expression (cp_parser *parser)
21921 {
21922   tree name;
21923
21924   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21925   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21926   name = cp_parser_identifier (parser);
21927   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21928
21929   return objc_get_class_ivars (name);
21930 }
21931
21932 /* Parse an Objective-C protocol expression.
21933
21934   objc-protocol-expression:
21935     @protocol ( identifier )
21936
21937   Returns a representation of the protocol expression.  */
21938
21939 static tree
21940 cp_parser_objc_protocol_expression (cp_parser* parser)
21941 {
21942   tree proto;
21943
21944   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21945   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21946   proto = cp_parser_identifier (parser);
21947   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21948
21949   return objc_build_protocol_expr (proto);
21950 }
21951
21952 /* Parse an Objective-C selector expression.
21953
21954    objc-selector-expression:
21955      @selector ( objc-method-signature )
21956
21957    objc-method-signature:
21958      objc-selector
21959      objc-selector-seq
21960
21961    objc-selector-seq:
21962      objc-selector :
21963      objc-selector-seq objc-selector :
21964
21965   Returns a representation of the method selector.  */
21966
21967 static tree
21968 cp_parser_objc_selector_expression (cp_parser* parser)
21969 {
21970   tree sel_seq = NULL_TREE;
21971   bool maybe_unary_selector_p = true;
21972   cp_token *token;
21973   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21974
21975   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21976   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21977   token = cp_lexer_peek_token (parser->lexer);
21978
21979   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21980          || token->type == CPP_SCOPE)
21981     {
21982       tree selector = NULL_TREE;
21983
21984       if (token->type != CPP_COLON
21985           || token->type == CPP_SCOPE)
21986         selector = cp_parser_objc_selector (parser);
21987
21988       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21989           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21990         {
21991           /* Detect if we have a unary selector.  */
21992           if (maybe_unary_selector_p)
21993             {
21994               sel_seq = selector;
21995               goto finish_selector;
21996             }
21997           else
21998             {
21999               cp_parser_error (parser, "expected %<:%>");
22000             }
22001         }
22002       maybe_unary_selector_p = false;
22003       token = cp_lexer_consume_token (parser->lexer);
22004
22005       if (token->type == CPP_SCOPE)
22006         {
22007           sel_seq
22008             = chainon (sel_seq,
22009                        build_tree_list (selector, NULL_TREE));
22010           sel_seq
22011             = chainon (sel_seq,
22012                        build_tree_list (NULL_TREE, NULL_TREE));
22013         }
22014       else
22015         sel_seq
22016           = chainon (sel_seq,
22017                      build_tree_list (selector, NULL_TREE));
22018
22019       token = cp_lexer_peek_token (parser->lexer);
22020     }
22021
22022  finish_selector:
22023   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22024
22025   return objc_build_selector_expr (loc, sel_seq);
22026 }
22027
22028 /* Parse a list of identifiers.
22029
22030    objc-identifier-list:
22031      identifier
22032      objc-identifier-list , identifier
22033
22034    Returns a TREE_LIST of identifier nodes.  */
22035
22036 static tree
22037 cp_parser_objc_identifier_list (cp_parser* parser)
22038 {
22039   tree identifier;
22040   tree list;
22041   cp_token *sep;
22042
22043   identifier = cp_parser_identifier (parser);
22044   if (identifier == error_mark_node)
22045     return error_mark_node;      
22046
22047   list = build_tree_list (NULL_TREE, identifier);
22048   sep = cp_lexer_peek_token (parser->lexer);
22049
22050   while (sep->type == CPP_COMMA)
22051     {
22052       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22053       identifier = cp_parser_identifier (parser);
22054       if (identifier == error_mark_node)
22055         return list;
22056
22057       list = chainon (list, build_tree_list (NULL_TREE,
22058                                              identifier));
22059       sep = cp_lexer_peek_token (parser->lexer);
22060     }
22061   
22062   return list;
22063 }
22064
22065 /* Parse an Objective-C alias declaration.
22066
22067    objc-alias-declaration:
22068      @compatibility_alias identifier identifier ;
22069
22070    This function registers the alias mapping with the Objective-C front end.
22071    It returns nothing.  */
22072
22073 static void
22074 cp_parser_objc_alias_declaration (cp_parser* parser)
22075 {
22076   tree alias, orig;
22077
22078   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22079   alias = cp_parser_identifier (parser);
22080   orig = cp_parser_identifier (parser);
22081   objc_declare_alias (alias, orig);
22082   cp_parser_consume_semicolon_at_end_of_statement (parser);
22083 }
22084
22085 /* Parse an Objective-C class forward-declaration.
22086
22087    objc-class-declaration:
22088      @class objc-identifier-list ;
22089
22090    The function registers the forward declarations with the Objective-C
22091    front end.  It returns nothing.  */
22092
22093 static void
22094 cp_parser_objc_class_declaration (cp_parser* parser)
22095 {
22096   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22097   while (true)
22098     {
22099       tree id;
22100       
22101       id = cp_parser_identifier (parser);
22102       if (id == error_mark_node)
22103         break;
22104       
22105       objc_declare_class (id);
22106
22107       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22108         cp_lexer_consume_token (parser->lexer);
22109       else
22110         break;
22111     }
22112   cp_parser_consume_semicolon_at_end_of_statement (parser);
22113 }
22114
22115 /* Parse a list of Objective-C protocol references.
22116
22117    objc-protocol-refs-opt:
22118      objc-protocol-refs [opt]
22119
22120    objc-protocol-refs:
22121      < objc-identifier-list >
22122
22123    Returns a TREE_LIST of identifiers, if any.  */
22124
22125 static tree
22126 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22127 {
22128   tree protorefs = NULL_TREE;
22129
22130   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22131     {
22132       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22133       protorefs = cp_parser_objc_identifier_list (parser);
22134       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22135     }
22136
22137   return protorefs;
22138 }
22139
22140 /* Parse a Objective-C visibility specification.  */
22141
22142 static void
22143 cp_parser_objc_visibility_spec (cp_parser* parser)
22144 {
22145   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22146
22147   switch (vis->keyword)
22148     {
22149     case RID_AT_PRIVATE:
22150       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22151       break;
22152     case RID_AT_PROTECTED:
22153       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22154       break;
22155     case RID_AT_PUBLIC:
22156       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22157       break;
22158     case RID_AT_PACKAGE:
22159       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22160       break;
22161     default:
22162       return;
22163     }
22164
22165   /* Eat '@private'/'@protected'/'@public'.  */
22166   cp_lexer_consume_token (parser->lexer);
22167 }
22168
22169 /* Parse an Objective-C method type.  Return 'true' if it is a class
22170    (+) method, and 'false' if it is an instance (-) method.  */
22171
22172 static inline bool
22173 cp_parser_objc_method_type (cp_parser* parser)
22174 {
22175   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22176     return true;
22177   else
22178     return false;
22179 }
22180
22181 /* Parse an Objective-C protocol qualifier.  */
22182
22183 static tree
22184 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
22185 {
22186   tree quals = NULL_TREE, node;
22187   cp_token *token = cp_lexer_peek_token (parser->lexer);
22188
22189   node = token->u.value;
22190
22191   while (node && TREE_CODE (node) == IDENTIFIER_NODE
22192          && (node == ridpointers [(int) RID_IN]
22193              || node == ridpointers [(int) RID_OUT]
22194              || node == ridpointers [(int) RID_INOUT]
22195              || node == ridpointers [(int) RID_BYCOPY]
22196              || node == ridpointers [(int) RID_BYREF]
22197              || node == ridpointers [(int) RID_ONEWAY]))
22198     {
22199       quals = tree_cons (NULL_TREE, node, quals);
22200       cp_lexer_consume_token (parser->lexer);
22201       token = cp_lexer_peek_token (parser->lexer);
22202       node = token->u.value;
22203     }
22204
22205   return quals;
22206 }
22207
22208 /* Parse an Objective-C typename.  */
22209
22210 static tree
22211 cp_parser_objc_typename (cp_parser* parser)
22212 {
22213   tree type_name = NULL_TREE;
22214
22215   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22216     {
22217       tree proto_quals, cp_type = NULL_TREE;
22218
22219       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22220       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22221
22222       /* An ObjC type name may consist of just protocol qualifiers, in which
22223          case the type shall default to 'id'.  */
22224       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22225         {
22226           cp_type = cp_parser_type_id (parser);
22227           
22228           /* If the type could not be parsed, an error has already
22229              been produced.  For error recovery, behave as if it had
22230              not been specified, which will use the default type
22231              'id'.  */
22232           if (cp_type == error_mark_node)
22233             {
22234               cp_type = NULL_TREE;
22235               /* We need to skip to the closing parenthesis as
22236                  cp_parser_type_id() does not seem to do it for
22237                  us.  */
22238               cp_parser_skip_to_closing_parenthesis (parser,
22239                                                      /*recovering=*/true,
22240                                                      /*or_comma=*/false,
22241                                                      /*consume_paren=*/false);
22242             }
22243         }
22244
22245       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22246       type_name = build_tree_list (proto_quals, cp_type);
22247     }
22248
22249   return type_name;
22250 }
22251
22252 /* Check to see if TYPE refers to an Objective-C selector name.  */
22253
22254 static bool
22255 cp_parser_objc_selector_p (enum cpp_ttype type)
22256 {
22257   return (type == CPP_NAME || type == CPP_KEYWORD
22258           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22259           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22260           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22261           || type == CPP_XOR || type == CPP_XOR_EQ);
22262 }
22263
22264 /* Parse an Objective-C selector.  */
22265
22266 static tree
22267 cp_parser_objc_selector (cp_parser* parser)
22268 {
22269   cp_token *token = cp_lexer_consume_token (parser->lexer);
22270
22271   if (!cp_parser_objc_selector_p (token->type))
22272     {
22273       error_at (token->location, "invalid Objective-C++ selector name");
22274       return error_mark_node;
22275     }
22276
22277   /* C++ operator names are allowed to appear in ObjC selectors.  */
22278   switch (token->type)
22279     {
22280     case CPP_AND_AND: return get_identifier ("and");
22281     case CPP_AND_EQ: return get_identifier ("and_eq");
22282     case CPP_AND: return get_identifier ("bitand");
22283     case CPP_OR: return get_identifier ("bitor");
22284     case CPP_COMPL: return get_identifier ("compl");
22285     case CPP_NOT: return get_identifier ("not");
22286     case CPP_NOT_EQ: return get_identifier ("not_eq");
22287     case CPP_OR_OR: return get_identifier ("or");
22288     case CPP_OR_EQ: return get_identifier ("or_eq");
22289     case CPP_XOR: return get_identifier ("xor");
22290     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22291     default: return token->u.value;
22292     }
22293 }
22294
22295 /* Parse an Objective-C params list.  */
22296
22297 static tree
22298 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22299 {
22300   tree params = NULL_TREE;
22301   bool maybe_unary_selector_p = true;
22302   cp_token *token = cp_lexer_peek_token (parser->lexer);
22303
22304   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22305     {
22306       tree selector = NULL_TREE, type_name, identifier;
22307       tree parm_attr = NULL_TREE;
22308
22309       if (token->keyword == RID_ATTRIBUTE)
22310         break;
22311
22312       if (token->type != CPP_COLON)
22313         selector = cp_parser_objc_selector (parser);
22314
22315       /* Detect if we have a unary selector.  */
22316       if (maybe_unary_selector_p
22317           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22318         {
22319           params = selector; /* Might be followed by attributes.  */
22320           break;
22321         }
22322
22323       maybe_unary_selector_p = false;
22324       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22325         {
22326           /* Something went quite wrong.  There should be a colon
22327              here, but there is not.  Stop parsing parameters.  */
22328           break;
22329         }
22330       type_name = cp_parser_objc_typename (parser);
22331       /* New ObjC allows attributes on parameters too.  */
22332       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22333         parm_attr = cp_parser_attributes_opt (parser);
22334       identifier = cp_parser_identifier (parser);
22335
22336       params
22337         = chainon (params,
22338                    objc_build_keyword_decl (selector,
22339                                             type_name,
22340                                             identifier,
22341                                             parm_attr));
22342
22343       token = cp_lexer_peek_token (parser->lexer);
22344     }
22345
22346   if (params == NULL_TREE)
22347     {
22348       cp_parser_error (parser, "objective-c++ method declaration is expected");
22349       return error_mark_node;
22350     }
22351
22352   /* We allow tail attributes for the method.  */
22353   if (token->keyword == RID_ATTRIBUTE)
22354     {
22355       *attributes = cp_parser_attributes_opt (parser);
22356       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22357           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22358         return params;
22359       cp_parser_error (parser, 
22360                        "method attributes must be specified at the end");
22361       return error_mark_node;
22362     }
22363
22364   if (params == NULL_TREE)
22365     {
22366       cp_parser_error (parser, "objective-c++ method declaration is expected");
22367       return error_mark_node;
22368     }
22369   return params;
22370 }
22371
22372 /* Parse the non-keyword Objective-C params.  */
22373
22374 static tree
22375 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22376                                        tree* attributes)
22377 {
22378   tree params = make_node (TREE_LIST);
22379   cp_token *token = cp_lexer_peek_token (parser->lexer);
22380   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22381
22382   while (token->type == CPP_COMMA)
22383     {
22384       cp_parameter_declarator *parmdecl;
22385       tree parm;
22386
22387       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22388       token = cp_lexer_peek_token (parser->lexer);
22389
22390       if (token->type == CPP_ELLIPSIS)
22391         {
22392           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22393           *ellipsisp = true;
22394           token = cp_lexer_peek_token (parser->lexer);
22395           break;
22396         }
22397
22398       /* TODO: parse attributes for tail parameters.  */
22399       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22400       parm = grokdeclarator (parmdecl->declarator,
22401                              &parmdecl->decl_specifiers,
22402                              PARM, /*initialized=*/0,
22403                              /*attrlist=*/NULL);
22404
22405       chainon (params, build_tree_list (NULL_TREE, parm));
22406       token = cp_lexer_peek_token (parser->lexer);
22407     }
22408
22409   /* We allow tail attributes for the method.  */
22410   if (token->keyword == RID_ATTRIBUTE)
22411     {
22412       if (*attributes == NULL_TREE)
22413         {
22414           *attributes = cp_parser_attributes_opt (parser);
22415           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22416               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22417             return params;
22418         }
22419       else        
22420         /* We have an error, but parse the attributes, so that we can 
22421            carry on.  */
22422         *attributes = cp_parser_attributes_opt (parser);
22423
22424       cp_parser_error (parser, 
22425                        "method attributes must be specified at the end");
22426       return error_mark_node;
22427     }
22428
22429   return params;
22430 }
22431
22432 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22433
22434 static void
22435 cp_parser_objc_interstitial_code (cp_parser* parser)
22436 {
22437   cp_token *token = cp_lexer_peek_token (parser->lexer);
22438
22439   /* If the next token is `extern' and the following token is a string
22440      literal, then we have a linkage specification.  */
22441   if (token->keyword == RID_EXTERN
22442       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22443     cp_parser_linkage_specification (parser);
22444   /* Handle #pragma, if any.  */
22445   else if (token->type == CPP_PRAGMA)
22446     cp_parser_pragma (parser, pragma_external);
22447   /* Allow stray semicolons.  */
22448   else if (token->type == CPP_SEMICOLON)
22449     cp_lexer_consume_token (parser->lexer);
22450   /* Mark methods as optional or required, when building protocols.  */
22451   else if (token->keyword == RID_AT_OPTIONAL)
22452     {
22453       cp_lexer_consume_token (parser->lexer);
22454       objc_set_method_opt (true);
22455     }
22456   else if (token->keyword == RID_AT_REQUIRED)
22457     {
22458       cp_lexer_consume_token (parser->lexer);
22459       objc_set_method_opt (false);
22460     }
22461   else if (token->keyword == RID_NAMESPACE)
22462     cp_parser_namespace_definition (parser);
22463   /* Other stray characters must generate errors.  */
22464   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22465     {
22466       cp_lexer_consume_token (parser->lexer);
22467       error ("stray %qs between Objective-C++ methods",
22468              token->type == CPP_OPEN_BRACE ? "{" : "}");
22469     }
22470   /* Finally, try to parse a block-declaration, or a function-definition.  */
22471   else
22472     cp_parser_block_declaration (parser, /*statement_p=*/false);
22473 }
22474
22475 /* Parse a method signature.  */
22476
22477 static tree
22478 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22479 {
22480   tree rettype, kwdparms, optparms;
22481   bool ellipsis = false;
22482   bool is_class_method;
22483
22484   is_class_method = cp_parser_objc_method_type (parser);
22485   rettype = cp_parser_objc_typename (parser);
22486   *attributes = NULL_TREE;
22487   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22488   if (kwdparms == error_mark_node)
22489     return error_mark_node;
22490   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22491   if (optparms == error_mark_node)
22492     return error_mark_node;
22493
22494   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22495 }
22496
22497 static bool
22498 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22499 {
22500   tree tattr;  
22501   cp_lexer_save_tokens (parser->lexer);
22502   tattr = cp_parser_attributes_opt (parser);
22503   gcc_assert (tattr) ;
22504   
22505   /* If the attributes are followed by a method introducer, this is not allowed.
22506      Dump the attributes and flag the situation.  */
22507   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22508       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22509     return true;
22510
22511   /* Otherwise, the attributes introduce some interstitial code, possibly so
22512      rewind to allow that check.  */
22513   cp_lexer_rollback_tokens (parser->lexer);
22514   return false;  
22515 }
22516
22517 /* Parse an Objective-C method prototype list.  */
22518
22519 static void
22520 cp_parser_objc_method_prototype_list (cp_parser* parser)
22521 {
22522   cp_token *token = cp_lexer_peek_token (parser->lexer);
22523
22524   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22525     {
22526       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22527         {
22528           tree attributes, sig;
22529           bool is_class_method;
22530           if (token->type == CPP_PLUS)
22531             is_class_method = true;
22532           else
22533             is_class_method = false;
22534           sig = cp_parser_objc_method_signature (parser, &attributes);
22535           if (sig == error_mark_node)
22536             {
22537               cp_parser_skip_to_end_of_block_or_statement (parser);
22538               token = cp_lexer_peek_token (parser->lexer);
22539               continue;
22540             }
22541           objc_add_method_declaration (is_class_method, sig, attributes);
22542           cp_parser_consume_semicolon_at_end_of_statement (parser);
22543         }
22544       else if (token->keyword == RID_AT_PROPERTY)
22545         cp_parser_objc_at_property_declaration (parser);
22546       else if (token->keyword == RID_ATTRIBUTE 
22547                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22548         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22549                     OPT_Wattributes, 
22550                     "prefix attributes are ignored for methods");
22551       else
22552         /* Allow for interspersed non-ObjC++ code.  */
22553         cp_parser_objc_interstitial_code (parser);
22554
22555       token = cp_lexer_peek_token (parser->lexer);
22556     }
22557
22558   if (token->type != CPP_EOF)
22559     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22560   else
22561     cp_parser_error (parser, "expected %<@end%>");
22562
22563   objc_finish_interface ();
22564 }
22565
22566 /* Parse an Objective-C method definition list.  */
22567
22568 static void
22569 cp_parser_objc_method_definition_list (cp_parser* parser)
22570 {
22571   cp_token *token = cp_lexer_peek_token (parser->lexer);
22572
22573   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22574     {
22575       tree meth;
22576
22577       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22578         {
22579           cp_token *ptk;
22580           tree sig, attribute;
22581           bool is_class_method;
22582           if (token->type == CPP_PLUS)
22583             is_class_method = true;
22584           else
22585             is_class_method = false;
22586           push_deferring_access_checks (dk_deferred);
22587           sig = cp_parser_objc_method_signature (parser, &attribute);
22588           if (sig == error_mark_node)
22589             {
22590               cp_parser_skip_to_end_of_block_or_statement (parser);
22591               token = cp_lexer_peek_token (parser->lexer);
22592               continue;
22593             }
22594           objc_start_method_definition (is_class_method, sig, attribute,
22595                                         NULL_TREE);
22596
22597           /* For historical reasons, we accept an optional semicolon.  */
22598           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22599             cp_lexer_consume_token (parser->lexer);
22600
22601           ptk = cp_lexer_peek_token (parser->lexer);
22602           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22603                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22604             {
22605               perform_deferred_access_checks ();
22606               stop_deferring_access_checks ();
22607               meth = cp_parser_function_definition_after_declarator (parser,
22608                                                                      false);
22609               pop_deferring_access_checks ();
22610               objc_finish_method_definition (meth);
22611             }
22612         }
22613       /* The following case will be removed once @synthesize is
22614          completely implemented.  */
22615       else if (token->keyword == RID_AT_PROPERTY)
22616         cp_parser_objc_at_property_declaration (parser);
22617       else if (token->keyword == RID_AT_SYNTHESIZE)
22618         cp_parser_objc_at_synthesize_declaration (parser);
22619       else if (token->keyword == RID_AT_DYNAMIC)
22620         cp_parser_objc_at_dynamic_declaration (parser);
22621       else if (token->keyword == RID_ATTRIBUTE 
22622                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22623         warning_at (token->location, OPT_Wattributes,
22624                     "prefix attributes are ignored for methods");
22625       else
22626         /* Allow for interspersed non-ObjC++ code.  */
22627         cp_parser_objc_interstitial_code (parser);
22628
22629       token = cp_lexer_peek_token (parser->lexer);
22630     }
22631
22632   if (token->type != CPP_EOF)
22633     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22634   else
22635     cp_parser_error (parser, "expected %<@end%>");
22636
22637   objc_finish_implementation ();
22638 }
22639
22640 /* Parse Objective-C ivars.  */
22641
22642 static void
22643 cp_parser_objc_class_ivars (cp_parser* parser)
22644 {
22645   cp_token *token = cp_lexer_peek_token (parser->lexer);
22646
22647   if (token->type != CPP_OPEN_BRACE)
22648     return;     /* No ivars specified.  */
22649
22650   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22651   token = cp_lexer_peek_token (parser->lexer);
22652
22653   while (token->type != CPP_CLOSE_BRACE 
22654         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22655     {
22656       cp_decl_specifier_seq declspecs;
22657       int decl_class_or_enum_p;
22658       tree prefix_attributes;
22659
22660       cp_parser_objc_visibility_spec (parser);
22661
22662       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22663         break;
22664
22665       cp_parser_decl_specifier_seq (parser,
22666                                     CP_PARSER_FLAGS_OPTIONAL,
22667                                     &declspecs,
22668                                     &decl_class_or_enum_p);
22669
22670       /* auto, register, static, extern, mutable.  */
22671       if (declspecs.storage_class != sc_none)
22672         {
22673           cp_parser_error (parser, "invalid type for instance variable");         
22674           declspecs.storage_class = sc_none;
22675         }
22676
22677       /* __thread.  */
22678       if (declspecs.specs[(int) ds_thread])
22679         {
22680           cp_parser_error (parser, "invalid type for instance variable");
22681           declspecs.specs[(int) ds_thread] = 0;
22682         }
22683       
22684       /* typedef.  */
22685       if (declspecs.specs[(int) ds_typedef])
22686         {
22687           cp_parser_error (parser, "invalid type for instance variable");
22688           declspecs.specs[(int) ds_typedef] = 0;
22689         }
22690
22691       prefix_attributes = declspecs.attributes;
22692       declspecs.attributes = NULL_TREE;
22693
22694       /* Keep going until we hit the `;' at the end of the
22695          declaration.  */
22696       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22697         {
22698           tree width = NULL_TREE, attributes, first_attribute, decl;
22699           cp_declarator *declarator = NULL;
22700           int ctor_dtor_or_conv_p;
22701
22702           /* Check for a (possibly unnamed) bitfield declaration.  */
22703           token = cp_lexer_peek_token (parser->lexer);
22704           if (token->type == CPP_COLON)
22705             goto eat_colon;
22706
22707           if (token->type == CPP_NAME
22708               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22709                   == CPP_COLON))
22710             {
22711               /* Get the name of the bitfield.  */
22712               declarator = make_id_declarator (NULL_TREE,
22713                                                cp_parser_identifier (parser),
22714                                                sfk_none);
22715
22716              eat_colon:
22717               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22718               /* Get the width of the bitfield.  */
22719               width
22720                 = cp_parser_constant_expression (parser,
22721                                                  /*allow_non_constant=*/false,
22722                                                  NULL);
22723             }
22724           else
22725             {
22726               /* Parse the declarator.  */
22727               declarator
22728                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22729                                         &ctor_dtor_or_conv_p,
22730                                         /*parenthesized_p=*/NULL,
22731                                         /*member_p=*/false);
22732             }
22733
22734           /* Look for attributes that apply to the ivar.  */
22735           attributes = cp_parser_attributes_opt (parser);
22736           /* Remember which attributes are prefix attributes and
22737              which are not.  */
22738           first_attribute = attributes;
22739           /* Combine the attributes.  */
22740           attributes = chainon (prefix_attributes, attributes);
22741
22742           if (width)
22743               /* Create the bitfield declaration.  */
22744               decl = grokbitfield (declarator, &declspecs,
22745                                    width,
22746                                    attributes);
22747           else
22748             decl = grokfield (declarator, &declspecs,
22749                               NULL_TREE, /*init_const_expr_p=*/false,
22750                               NULL_TREE, attributes);
22751
22752           /* Add the instance variable.  */
22753           if (decl != error_mark_node && decl != NULL_TREE)
22754             objc_add_instance_variable (decl);
22755
22756           /* Reset PREFIX_ATTRIBUTES.  */
22757           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22758             attributes = TREE_CHAIN (attributes);
22759           if (attributes)
22760             TREE_CHAIN (attributes) = NULL_TREE;
22761
22762           token = cp_lexer_peek_token (parser->lexer);
22763
22764           if (token->type == CPP_COMMA)
22765             {
22766               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22767               continue;
22768             }
22769           break;
22770         }
22771
22772       cp_parser_consume_semicolon_at_end_of_statement (parser);
22773       token = cp_lexer_peek_token (parser->lexer);
22774     }
22775
22776   if (token->keyword == RID_AT_END)
22777     cp_parser_error (parser, "expected %<}%>");
22778
22779   /* Do not consume the RID_AT_END, so it will be read again as terminating
22780      the @interface of @implementation.  */ 
22781   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22782     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22783     
22784   /* For historical reasons, we accept an optional semicolon.  */
22785   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22786     cp_lexer_consume_token (parser->lexer);
22787 }
22788
22789 /* Parse an Objective-C protocol declaration.  */
22790
22791 static void
22792 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22793 {
22794   tree proto, protorefs;
22795   cp_token *tok;
22796
22797   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22798   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22799     {
22800       tok = cp_lexer_peek_token (parser->lexer);
22801       error_at (tok->location, "identifier expected after %<@protocol%>");
22802       cp_parser_consume_semicolon_at_end_of_statement (parser);
22803       return;
22804     }
22805
22806   /* See if we have a forward declaration or a definition.  */
22807   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22808
22809   /* Try a forward declaration first.  */
22810   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22811     {
22812       while (true)
22813         {
22814           tree id;
22815           
22816           id = cp_parser_identifier (parser);
22817           if (id == error_mark_node)
22818             break;
22819           
22820           objc_declare_protocol (id, attributes);
22821           
22822           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22823             cp_lexer_consume_token (parser->lexer);
22824           else
22825             break;
22826         }
22827       cp_parser_consume_semicolon_at_end_of_statement (parser);
22828     }
22829
22830   /* Ok, we got a full-fledged definition (or at least should).  */
22831   else
22832     {
22833       proto = cp_parser_identifier (parser);
22834       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22835       objc_start_protocol (proto, protorefs, attributes);
22836       cp_parser_objc_method_prototype_list (parser);
22837     }
22838 }
22839
22840 /* Parse an Objective-C superclass or category.  */
22841
22842 static void
22843 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22844                                        bool iface_p,
22845                                        tree *super,
22846                                        tree *categ, bool *is_class_extension)
22847 {
22848   cp_token *next = cp_lexer_peek_token (parser->lexer);
22849
22850   *super = *categ = NULL_TREE;
22851   *is_class_extension = false;
22852   if (next->type == CPP_COLON)
22853     {
22854       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22855       *super = cp_parser_identifier (parser);
22856     }
22857   else if (next->type == CPP_OPEN_PAREN)
22858     {
22859       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22860
22861       /* If there is no category name, and this is an @interface, we
22862          have a class extension.  */
22863       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22864         {
22865           *categ = NULL_TREE;
22866           *is_class_extension = true;
22867         }
22868       else
22869         *categ = cp_parser_identifier (parser);
22870
22871       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22872     }
22873 }
22874
22875 /* Parse an Objective-C class interface.  */
22876
22877 static void
22878 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22879 {
22880   tree name, super, categ, protos;
22881   bool is_class_extension;
22882
22883   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22884   name = cp_parser_identifier (parser);
22885   if (name == error_mark_node)
22886     {
22887       /* It's hard to recover because even if valid @interface stuff
22888          is to follow, we can't compile it (or validate it) if we
22889          don't even know which class it refers to.  Let's assume this
22890          was a stray '@interface' token in the stream and skip it.
22891       */
22892       return;
22893     }
22894   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22895                                          &is_class_extension);
22896   protos = cp_parser_objc_protocol_refs_opt (parser);
22897
22898   /* We have either a class or a category on our hands.  */
22899   if (categ || is_class_extension)
22900     objc_start_category_interface (name, categ, protos, attributes);
22901   else
22902     {
22903       objc_start_class_interface (name, super, protos, attributes);
22904       /* Handle instance variable declarations, if any.  */
22905       cp_parser_objc_class_ivars (parser);
22906       objc_continue_interface ();
22907     }
22908
22909   cp_parser_objc_method_prototype_list (parser);
22910 }
22911
22912 /* Parse an Objective-C class implementation.  */
22913
22914 static void
22915 cp_parser_objc_class_implementation (cp_parser* parser)
22916 {
22917   tree name, super, categ;
22918   bool is_class_extension;
22919
22920   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22921   name = cp_parser_identifier (parser);
22922   if (name == error_mark_node)
22923     {
22924       /* It's hard to recover because even if valid @implementation
22925          stuff is to follow, we can't compile it (or validate it) if
22926          we don't even know which class it refers to.  Let's assume
22927          this was a stray '@implementation' token in the stream and
22928          skip it.
22929       */
22930       return;
22931     }
22932   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22933                                          &is_class_extension);
22934
22935   /* We have either a class or a category on our hands.  */
22936   if (categ)
22937     objc_start_category_implementation (name, categ);
22938   else
22939     {
22940       objc_start_class_implementation (name, super);
22941       /* Handle instance variable declarations, if any.  */
22942       cp_parser_objc_class_ivars (parser);
22943       objc_continue_implementation ();
22944     }
22945
22946   cp_parser_objc_method_definition_list (parser);
22947 }
22948
22949 /* Consume the @end token and finish off the implementation.  */
22950
22951 static void
22952 cp_parser_objc_end_implementation (cp_parser* parser)
22953 {
22954   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22955   objc_finish_implementation ();
22956 }
22957
22958 /* Parse an Objective-C declaration.  */
22959
22960 static void
22961 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22962 {
22963   /* Try to figure out what kind of declaration is present.  */
22964   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22965
22966   if (attributes)
22967     switch (kwd->keyword)
22968       {
22969         case RID_AT_ALIAS:
22970         case RID_AT_CLASS:
22971         case RID_AT_END:
22972           error_at (kwd->location, "attributes may not be specified before"
22973                     " the %<@%D%> Objective-C++ keyword",
22974                     kwd->u.value);
22975           attributes = NULL;
22976           break;
22977         case RID_AT_IMPLEMENTATION:
22978           warning_at (kwd->location, OPT_Wattributes,
22979                       "prefix attributes are ignored before %<@%D%>",
22980                       kwd->u.value);
22981           attributes = NULL;
22982         default:
22983           break;
22984       }
22985
22986   switch (kwd->keyword)
22987     {
22988     case RID_AT_ALIAS:
22989       cp_parser_objc_alias_declaration (parser);
22990       break;
22991     case RID_AT_CLASS:
22992       cp_parser_objc_class_declaration (parser);
22993       break;
22994     case RID_AT_PROTOCOL:
22995       cp_parser_objc_protocol_declaration (parser, attributes);
22996       break;
22997     case RID_AT_INTERFACE:
22998       cp_parser_objc_class_interface (parser, attributes);
22999       break;
23000     case RID_AT_IMPLEMENTATION:
23001       cp_parser_objc_class_implementation (parser);
23002       break;
23003     case RID_AT_END:
23004       cp_parser_objc_end_implementation (parser);
23005       break;
23006     default:
23007       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23008                 kwd->u.value);
23009       cp_parser_skip_to_end_of_block_or_statement (parser);
23010     }
23011 }
23012
23013 /* Parse an Objective-C try-catch-finally statement.
23014
23015    objc-try-catch-finally-stmt:
23016      @try compound-statement objc-catch-clause-seq [opt]
23017        objc-finally-clause [opt]
23018
23019    objc-catch-clause-seq:
23020      objc-catch-clause objc-catch-clause-seq [opt]
23021
23022    objc-catch-clause:
23023      @catch ( objc-exception-declaration ) compound-statement
23024
23025    objc-finally-clause:
23026      @finally compound-statement
23027
23028    objc-exception-declaration:
23029      parameter-declaration
23030      '...'
23031
23032    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23033
23034    Returns NULL_TREE.
23035
23036    PS: This function is identical to c_parser_objc_try_catch_finally_statement
23037    for C.  Keep them in sync.  */   
23038
23039 static tree
23040 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23041 {
23042   location_t location;
23043   tree stmt;
23044
23045   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23046   location = cp_lexer_peek_token (parser->lexer)->location;
23047   objc_maybe_warn_exceptions (location);
23048   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23049      node, lest it get absorbed into the surrounding block.  */
23050   stmt = push_stmt_list ();
23051   cp_parser_compound_statement (parser, NULL, false, false);
23052   objc_begin_try_stmt (location, pop_stmt_list (stmt));
23053
23054   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23055     {
23056       cp_parameter_declarator *parm;
23057       tree parameter_declaration = error_mark_node;
23058       bool seen_open_paren = false;
23059
23060       cp_lexer_consume_token (parser->lexer);
23061       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23062         seen_open_paren = true;
23063       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23064         {
23065           /* We have "@catch (...)" (where the '...' are literally
23066              what is in the code).  Skip the '...'.
23067              parameter_declaration is set to NULL_TREE, and
23068              objc_being_catch_clauses() knows that that means
23069              '...'.  */
23070           cp_lexer_consume_token (parser->lexer);
23071           parameter_declaration = NULL_TREE;
23072         }
23073       else
23074         {
23075           /* We have "@catch (NSException *exception)" or something
23076              like that.  Parse the parameter declaration.  */
23077           parm = cp_parser_parameter_declaration (parser, false, NULL);
23078           if (parm == NULL)
23079             parameter_declaration = error_mark_node;
23080           else
23081             parameter_declaration = grokdeclarator (parm->declarator,
23082                                                     &parm->decl_specifiers,
23083                                                     PARM, /*initialized=*/0,
23084                                                     /*attrlist=*/NULL);
23085         }
23086       if (seen_open_paren)
23087         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23088       else
23089         {
23090           /* If there was no open parenthesis, we are recovering from
23091              an error, and we are trying to figure out what mistake
23092              the user has made.  */
23093
23094           /* If there is an immediate closing parenthesis, the user
23095              probably forgot the opening one (ie, they typed "@catch
23096              NSException *e)".  Parse the closing parenthesis and keep
23097              going.  */
23098           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23099             cp_lexer_consume_token (parser->lexer);
23100           
23101           /* If these is no immediate closing parenthesis, the user
23102              probably doesn't know that parenthesis are required at
23103              all (ie, they typed "@catch NSException *e").  So, just
23104              forget about the closing parenthesis and keep going.  */
23105         }
23106       objc_begin_catch_clause (parameter_declaration);
23107       cp_parser_compound_statement (parser, NULL, false, false);
23108       objc_finish_catch_clause ();
23109     }
23110   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23111     {
23112       cp_lexer_consume_token (parser->lexer);
23113       location = cp_lexer_peek_token (parser->lexer)->location;
23114       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23115          node, lest it get absorbed into the surrounding block.  */
23116       stmt = push_stmt_list ();
23117       cp_parser_compound_statement (parser, NULL, false, false);
23118       objc_build_finally_clause (location, pop_stmt_list (stmt));
23119     }
23120
23121   return objc_finish_try_stmt ();
23122 }
23123
23124 /* Parse an Objective-C synchronized statement.
23125
23126    objc-synchronized-stmt:
23127      @synchronized ( expression ) compound-statement
23128
23129    Returns NULL_TREE.  */
23130
23131 static tree
23132 cp_parser_objc_synchronized_statement (cp_parser *parser)
23133 {
23134   location_t location;
23135   tree lock, stmt;
23136
23137   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23138
23139   location = cp_lexer_peek_token (parser->lexer)->location;
23140   objc_maybe_warn_exceptions (location);
23141   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23142   lock = cp_parser_expression (parser, false, NULL);
23143   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23144
23145   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23146      node, lest it get absorbed into the surrounding block.  */
23147   stmt = push_stmt_list ();
23148   cp_parser_compound_statement (parser, NULL, false, false);
23149
23150   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23151 }
23152
23153 /* Parse an Objective-C throw statement.
23154
23155    objc-throw-stmt:
23156      @throw assignment-expression [opt] ;
23157
23158    Returns a constructed '@throw' statement.  */
23159
23160 static tree
23161 cp_parser_objc_throw_statement (cp_parser *parser)
23162 {
23163   tree expr = NULL_TREE;
23164   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23165
23166   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23167
23168   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23169     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23170
23171   cp_parser_consume_semicolon_at_end_of_statement (parser);
23172
23173   return objc_build_throw_stmt (loc, expr);
23174 }
23175
23176 /* Parse an Objective-C statement.  */
23177
23178 static tree
23179 cp_parser_objc_statement (cp_parser * parser)
23180 {
23181   /* Try to figure out what kind of declaration is present.  */
23182   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23183
23184   switch (kwd->keyword)
23185     {
23186     case RID_AT_TRY:
23187       return cp_parser_objc_try_catch_finally_statement (parser);
23188     case RID_AT_SYNCHRONIZED:
23189       return cp_parser_objc_synchronized_statement (parser);
23190     case RID_AT_THROW:
23191       return cp_parser_objc_throw_statement (parser);
23192     default:
23193       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23194                kwd->u.value);
23195       cp_parser_skip_to_end_of_block_or_statement (parser);
23196     }
23197
23198   return error_mark_node;
23199 }
23200
23201 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
23202    look ahead to see if an objc keyword follows the attributes.  This
23203    is to detect the use of prefix attributes on ObjC @interface and 
23204    @protocol.  */
23205
23206 static bool
23207 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23208 {
23209   cp_lexer_save_tokens (parser->lexer);
23210   *attrib = cp_parser_attributes_opt (parser);
23211   gcc_assert (*attrib);
23212   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23213     {
23214       cp_lexer_commit_tokens (parser->lexer);
23215       return true;
23216     }
23217   cp_lexer_rollback_tokens (parser->lexer);
23218   return false;  
23219 }
23220
23221 /* This routine is a minimal replacement for
23222    c_parser_struct_declaration () used when parsing the list of
23223    types/names or ObjC++ properties.  For example, when parsing the
23224    code
23225
23226    @property (readonly) int a, b, c;
23227
23228    this function is responsible for parsing "int a, int b, int c" and
23229    returning the declarations as CHAIN of DECLs.
23230
23231    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23232    similar parsing.  */
23233 static tree
23234 cp_parser_objc_struct_declaration (cp_parser *parser)
23235 {
23236   tree decls = NULL_TREE;
23237   cp_decl_specifier_seq declspecs;
23238   int decl_class_or_enum_p;
23239   tree prefix_attributes;
23240
23241   cp_parser_decl_specifier_seq (parser,
23242                                 CP_PARSER_FLAGS_NONE,
23243                                 &declspecs,
23244                                 &decl_class_or_enum_p);
23245
23246   if (declspecs.type == error_mark_node)
23247     return error_mark_node;
23248
23249   /* auto, register, static, extern, mutable.  */
23250   if (declspecs.storage_class != sc_none)
23251     {
23252       cp_parser_error (parser, "invalid type for property");
23253       declspecs.storage_class = sc_none;
23254     }
23255   
23256   /* __thread.  */
23257   if (declspecs.specs[(int) ds_thread])
23258     {
23259       cp_parser_error (parser, "invalid type for property");
23260       declspecs.specs[(int) ds_thread] = 0;
23261     }
23262   
23263   /* typedef.  */
23264   if (declspecs.specs[(int) ds_typedef])
23265     {
23266       cp_parser_error (parser, "invalid type for property");
23267       declspecs.specs[(int) ds_typedef] = 0;
23268     }
23269
23270   prefix_attributes = declspecs.attributes;
23271   declspecs.attributes = NULL_TREE;
23272
23273   /* Keep going until we hit the `;' at the end of the declaration. */
23274   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23275     {
23276       tree attributes, first_attribute, decl;
23277       cp_declarator *declarator;
23278       cp_token *token;
23279
23280       /* Parse the declarator.  */
23281       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23282                                          NULL, NULL, false);
23283
23284       /* Look for attributes that apply to the ivar.  */
23285       attributes = cp_parser_attributes_opt (parser);
23286       /* Remember which attributes are prefix attributes and
23287          which are not.  */
23288       first_attribute = attributes;
23289       /* Combine the attributes.  */
23290       attributes = chainon (prefix_attributes, attributes);
23291       
23292       decl = grokfield (declarator, &declspecs,
23293                         NULL_TREE, /*init_const_expr_p=*/false,
23294                         NULL_TREE, attributes);
23295
23296       if (decl == error_mark_node || decl == NULL_TREE)
23297         return error_mark_node;
23298       
23299       /* Reset PREFIX_ATTRIBUTES.  */
23300       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23301         attributes = TREE_CHAIN (attributes);
23302       if (attributes)
23303         TREE_CHAIN (attributes) = NULL_TREE;
23304
23305       DECL_CHAIN (decl) = decls;
23306       decls = decl;
23307
23308       token = cp_lexer_peek_token (parser->lexer);
23309       if (token->type == CPP_COMMA)
23310         {
23311           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23312           continue;
23313         }
23314       else
23315         break;
23316     }
23317   return decls;
23318 }
23319
23320 /* Parse an Objective-C @property declaration.  The syntax is:
23321
23322    objc-property-declaration:
23323      '@property' objc-property-attributes[opt] struct-declaration ;
23324
23325    objc-property-attributes:
23326     '(' objc-property-attribute-list ')'
23327
23328    objc-property-attribute-list:
23329      objc-property-attribute
23330      objc-property-attribute-list, objc-property-attribute
23331
23332    objc-property-attribute
23333      'getter' = identifier
23334      'setter' = identifier
23335      'readonly'
23336      'readwrite'
23337      'assign'
23338      'retain'
23339      'copy'
23340      'nonatomic'
23341
23342   For example:
23343     @property NSString *name;
23344     @property (readonly) id object;
23345     @property (retain, nonatomic, getter=getTheName) id name;
23346     @property int a, b, c;
23347
23348    PS: This function is identical to
23349    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23350 static void 
23351 cp_parser_objc_at_property_declaration (cp_parser *parser)
23352 {
23353   /* The following variables hold the attributes of the properties as
23354      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23355      seen.  When we see an attribute, we set them to 'true' (if they
23356      are boolean properties) or to the identifier (if they have an
23357      argument, ie, for getter and setter).  Note that here we only
23358      parse the list of attributes, check the syntax and accumulate the
23359      attributes that we find.  objc_add_property_declaration() will
23360      then process the information.  */
23361   bool property_assign = false;
23362   bool property_copy = false;
23363   tree property_getter_ident = NULL_TREE;
23364   bool property_nonatomic = false;
23365   bool property_readonly = false;
23366   bool property_readwrite = false;
23367   bool property_retain = false;
23368   tree property_setter_ident = NULL_TREE;
23369
23370   /* 'properties' is the list of properties that we read.  Usually a
23371      single one, but maybe more (eg, in "@property int a, b, c;" there
23372      are three).  */
23373   tree properties;
23374   location_t loc;
23375
23376   loc = cp_lexer_peek_token (parser->lexer)->location;
23377
23378   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23379
23380   /* Parse the optional attribute list...  */
23381   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23382     {
23383       /* Eat the '('.  */
23384       cp_lexer_consume_token (parser->lexer);
23385
23386       while (true)
23387         {
23388           bool syntax_error = false;
23389           cp_token *token = cp_lexer_peek_token (parser->lexer);
23390           enum rid keyword;
23391
23392           if (token->type != CPP_NAME)
23393             {
23394               cp_parser_error (parser, "expected identifier");
23395               break;
23396             }
23397           keyword = C_RID_CODE (token->u.value);
23398           cp_lexer_consume_token (parser->lexer);
23399           switch (keyword)
23400             {
23401             case RID_ASSIGN:    property_assign = true;    break;
23402             case RID_COPY:      property_copy = true;      break;
23403             case RID_NONATOMIC: property_nonatomic = true; break;
23404             case RID_READONLY:  property_readonly = true;  break;
23405             case RID_READWRITE: property_readwrite = true; break;
23406             case RID_RETAIN:    property_retain = true;    break;
23407
23408             case RID_GETTER:
23409             case RID_SETTER:
23410               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23411                 {
23412                   if (keyword == RID_GETTER)
23413                     cp_parser_error (parser,
23414                                      "missing %<=%> (after %<getter%> attribute)");
23415                   else
23416                     cp_parser_error (parser,
23417                                      "missing %<=%> (after %<setter%> attribute)");
23418                   syntax_error = true;
23419                   break;
23420                 }
23421               cp_lexer_consume_token (parser->lexer); /* eat the = */
23422               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23423                 {
23424                   cp_parser_error (parser, "expected identifier");
23425                   syntax_error = true;
23426                   break;
23427                 }
23428               if (keyword == RID_SETTER)
23429                 {
23430                   if (property_setter_ident != NULL_TREE)
23431                     {
23432                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23433                       cp_lexer_consume_token (parser->lexer);
23434                     }
23435                   else
23436                     property_setter_ident = cp_parser_objc_selector (parser);
23437                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23438                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23439                   else
23440                     cp_lexer_consume_token (parser->lexer);
23441                 }
23442               else
23443                 {
23444                   if (property_getter_ident != NULL_TREE)
23445                     {
23446                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23447                       cp_lexer_consume_token (parser->lexer);
23448                     }
23449                   else
23450                     property_getter_ident = cp_parser_objc_selector (parser);
23451                 }
23452               break;
23453             default:
23454               cp_parser_error (parser, "unknown property attribute");
23455               syntax_error = true;
23456               break;
23457             }
23458
23459           if (syntax_error)
23460             break;
23461
23462           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23463             cp_lexer_consume_token (parser->lexer);
23464           else
23465             break;
23466         }
23467
23468       /* FIXME: "@property (setter, assign);" will generate a spurious
23469          "error: expected â€˜)’ before â€˜,’ token".  This is because
23470          cp_parser_require, unlike the C counterpart, will produce an
23471          error even if we are in error recovery.  */
23472       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23473         {
23474           cp_parser_skip_to_closing_parenthesis (parser,
23475                                                  /*recovering=*/true,
23476                                                  /*or_comma=*/false,
23477                                                  /*consume_paren=*/true);
23478         }
23479     }
23480
23481   /* ... and the property declaration(s).  */
23482   properties = cp_parser_objc_struct_declaration (parser);
23483
23484   if (properties == error_mark_node)
23485     {
23486       cp_parser_skip_to_end_of_statement (parser);
23487       /* If the next token is now a `;', consume it.  */
23488       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23489         cp_lexer_consume_token (parser->lexer);
23490       return;
23491     }
23492
23493   if (properties == NULL_TREE)
23494     cp_parser_error (parser, "expected identifier");
23495   else
23496     {
23497       /* Comma-separated properties are chained together in
23498          reverse order; add them one by one.  */
23499       properties = nreverse (properties);
23500       
23501       for (; properties; properties = TREE_CHAIN (properties))
23502         objc_add_property_declaration (loc, copy_node (properties),
23503                                        property_readonly, property_readwrite,
23504                                        property_assign, property_retain,
23505                                        property_copy, property_nonatomic,
23506                                        property_getter_ident, property_setter_ident);
23507     }
23508   
23509   cp_parser_consume_semicolon_at_end_of_statement (parser);
23510 }
23511
23512 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23513
23514    objc-synthesize-declaration:
23515      @synthesize objc-synthesize-identifier-list ;
23516
23517    objc-synthesize-identifier-list:
23518      objc-synthesize-identifier
23519      objc-synthesize-identifier-list, objc-synthesize-identifier
23520
23521    objc-synthesize-identifier
23522      identifier
23523      identifier = identifier
23524
23525   For example:
23526     @synthesize MyProperty;
23527     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23528
23529   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23530   for C.  Keep them in sync.
23531 */
23532 static void 
23533 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23534 {
23535   tree list = NULL_TREE;
23536   location_t loc;
23537   loc = cp_lexer_peek_token (parser->lexer)->location;
23538
23539   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23540   while (true)
23541     {
23542       tree property, ivar;
23543       property = cp_parser_identifier (parser);
23544       if (property == error_mark_node)
23545         {
23546           cp_parser_consume_semicolon_at_end_of_statement (parser);
23547           return;
23548         }
23549       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23550         {
23551           cp_lexer_consume_token (parser->lexer);
23552           ivar = cp_parser_identifier (parser);
23553           if (ivar == error_mark_node)
23554             {
23555               cp_parser_consume_semicolon_at_end_of_statement (parser);
23556               return;
23557             }
23558         }
23559       else
23560         ivar = NULL_TREE;
23561       list = chainon (list, build_tree_list (ivar, property));
23562       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23563         cp_lexer_consume_token (parser->lexer);
23564       else
23565         break;
23566     }
23567   cp_parser_consume_semicolon_at_end_of_statement (parser);
23568   objc_add_synthesize_declaration (loc, list);
23569 }
23570
23571 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23572
23573    objc-dynamic-declaration:
23574      @dynamic identifier-list ;
23575
23576    For example:
23577      @dynamic MyProperty;
23578      @dynamic MyProperty, AnotherProperty;
23579
23580   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23581   for C.  Keep them in sync.
23582 */
23583 static void 
23584 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23585 {
23586   tree list = NULL_TREE;
23587   location_t loc;
23588   loc = cp_lexer_peek_token (parser->lexer)->location;
23589
23590   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23591   while (true)
23592     {
23593       tree property;
23594       property = cp_parser_identifier (parser);
23595       if (property == error_mark_node)
23596         {
23597           cp_parser_consume_semicolon_at_end_of_statement (parser);
23598           return;
23599         }
23600       list = chainon (list, build_tree_list (NULL, property));
23601       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23602         cp_lexer_consume_token (parser->lexer);
23603       else
23604         break;
23605     }
23606   cp_parser_consume_semicolon_at_end_of_statement (parser);
23607   objc_add_dynamic_declaration (loc, list);
23608 }
23609
23610 \f
23611 /* OpenMP 2.5 parsing routines.  */
23612
23613 /* Returns name of the next clause.
23614    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23615    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23616    returned and the token is consumed.  */
23617
23618 static pragma_omp_clause
23619 cp_parser_omp_clause_name (cp_parser *parser)
23620 {
23621   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23622
23623   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23624     result = PRAGMA_OMP_CLAUSE_IF;
23625   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23626     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23627   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23628     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23629   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23630     {
23631       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23632       const char *p = IDENTIFIER_POINTER (id);
23633
23634       switch (p[0])
23635         {
23636         case 'c':
23637           if (!strcmp ("collapse", p))
23638             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23639           else if (!strcmp ("copyin", p))
23640             result = PRAGMA_OMP_CLAUSE_COPYIN;
23641           else if (!strcmp ("copyprivate", p))
23642             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23643           break;
23644         case 'f':
23645           if (!strcmp ("final", p))
23646             result = PRAGMA_OMP_CLAUSE_FINAL;
23647           else if (!strcmp ("firstprivate", p))
23648             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23649           break;
23650         case 'l':
23651           if (!strcmp ("lastprivate", p))
23652             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23653           break;
23654         case 'm':
23655           if (!strcmp ("mergeable", p))
23656             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
23657           break;
23658         case 'n':
23659           if (!strcmp ("nowait", p))
23660             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23661           else if (!strcmp ("num_threads", p))
23662             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23663           break;
23664         case 'o':
23665           if (!strcmp ("ordered", p))
23666             result = PRAGMA_OMP_CLAUSE_ORDERED;
23667           break;
23668         case 'r':
23669           if (!strcmp ("reduction", p))
23670             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23671           break;
23672         case 's':
23673           if (!strcmp ("schedule", p))
23674             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23675           else if (!strcmp ("shared", p))
23676             result = PRAGMA_OMP_CLAUSE_SHARED;
23677           break;
23678         case 'u':
23679           if (!strcmp ("untied", p))
23680             result = PRAGMA_OMP_CLAUSE_UNTIED;
23681           break;
23682         }
23683     }
23684
23685   if (result != PRAGMA_OMP_CLAUSE_NONE)
23686     cp_lexer_consume_token (parser->lexer);
23687
23688   return result;
23689 }
23690
23691 /* Validate that a clause of the given type does not already exist.  */
23692
23693 static void
23694 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23695                            const char *name, location_t location)
23696 {
23697   tree c;
23698
23699   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23700     if (OMP_CLAUSE_CODE (c) == code)
23701       {
23702         error_at (location, "too many %qs clauses", name);
23703         break;
23704       }
23705 }
23706
23707 /* OpenMP 2.5:
23708    variable-list:
23709      identifier
23710      variable-list , identifier
23711
23712    In addition, we match a closing parenthesis.  An opening parenthesis
23713    will have been consumed by the caller.
23714
23715    If KIND is nonzero, create the appropriate node and install the decl
23716    in OMP_CLAUSE_DECL and add the node to the head of the list.
23717
23718    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23719    return the list created.  */
23720
23721 static tree
23722 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23723                                 tree list)
23724 {
23725   cp_token *token;
23726   while (1)
23727     {
23728       tree name, decl;
23729
23730       token = cp_lexer_peek_token (parser->lexer);
23731       name = cp_parser_id_expression (parser, /*template_p=*/false,
23732                                       /*check_dependency_p=*/true,
23733                                       /*template_p=*/NULL,
23734                                       /*declarator_p=*/false,
23735                                       /*optional_p=*/false);
23736       if (name == error_mark_node)
23737         goto skip_comma;
23738
23739       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23740       if (decl == error_mark_node)
23741         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23742                                      token->location);
23743       else if (kind != 0)
23744         {
23745           tree u = build_omp_clause (token->location, kind);
23746           OMP_CLAUSE_DECL (u) = decl;
23747           OMP_CLAUSE_CHAIN (u) = list;
23748           list = u;
23749         }
23750       else
23751         list = tree_cons (decl, NULL_TREE, list);
23752
23753     get_comma:
23754       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23755         break;
23756       cp_lexer_consume_token (parser->lexer);
23757     }
23758
23759   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23760     {
23761       int ending;
23762
23763       /* Try to resync to an unnested comma.  Copied from
23764          cp_parser_parenthesized_expression_list.  */
23765     skip_comma:
23766       ending = cp_parser_skip_to_closing_parenthesis (parser,
23767                                                       /*recovering=*/true,
23768                                                       /*or_comma=*/true,
23769                                                       /*consume_paren=*/true);
23770       if (ending < 0)
23771         goto get_comma;
23772     }
23773
23774   return list;
23775 }
23776
23777 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23778    common case for omp clauses.  */
23779
23780 static tree
23781 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23782 {
23783   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23784     return cp_parser_omp_var_list_no_open (parser, kind, list);
23785   return list;
23786 }
23787
23788 /* OpenMP 3.0:
23789    collapse ( constant-expression ) */
23790
23791 static tree
23792 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23793 {
23794   tree c, num;
23795   location_t loc;
23796   HOST_WIDE_INT n;
23797
23798   loc = cp_lexer_peek_token (parser->lexer)->location;
23799   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23800     return list;
23801
23802   num = cp_parser_constant_expression (parser, false, NULL);
23803
23804   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23805     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23806                                            /*or_comma=*/false,
23807                                            /*consume_paren=*/true);
23808
23809   if (num == error_mark_node)
23810     return list;
23811   num = fold_non_dependent_expr (num);
23812   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23813       || !host_integerp (num, 0)
23814       || (n = tree_low_cst (num, 0)) <= 0
23815       || (int) n != n)
23816     {
23817       error_at (loc, "collapse argument needs positive constant integer expression");
23818       return list;
23819     }
23820
23821   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23822   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23823   OMP_CLAUSE_CHAIN (c) = list;
23824   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23825
23826   return c;
23827 }
23828
23829 /* OpenMP 2.5:
23830    default ( shared | none ) */
23831
23832 static tree
23833 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23834 {
23835   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23836   tree c;
23837
23838   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23839     return list;
23840   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23841     {
23842       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23843       const char *p = IDENTIFIER_POINTER (id);
23844
23845       switch (p[0])
23846         {
23847         case 'n':
23848           if (strcmp ("none", p) != 0)
23849             goto invalid_kind;
23850           kind = OMP_CLAUSE_DEFAULT_NONE;
23851           break;
23852
23853         case 's':
23854           if (strcmp ("shared", p) != 0)
23855             goto invalid_kind;
23856           kind = OMP_CLAUSE_DEFAULT_SHARED;
23857           break;
23858
23859         default:
23860           goto invalid_kind;
23861         }
23862
23863       cp_lexer_consume_token (parser->lexer);
23864     }
23865   else
23866     {
23867     invalid_kind:
23868       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23869     }
23870
23871   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23872     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23873                                            /*or_comma=*/false,
23874                                            /*consume_paren=*/true);
23875
23876   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23877     return list;
23878
23879   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23880   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23881   OMP_CLAUSE_CHAIN (c) = list;
23882   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23883
23884   return c;
23885 }
23886
23887 /* OpenMP 3.1:
23888    final ( expression ) */
23889
23890 static tree
23891 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
23892 {
23893   tree t, c;
23894
23895   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23896     return list;
23897
23898   t = cp_parser_condition (parser);
23899
23900   if (t == error_mark_node
23901       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23902     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23903                                            /*or_comma=*/false,
23904                                            /*consume_paren=*/true);
23905
23906   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
23907
23908   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
23909   OMP_CLAUSE_FINAL_EXPR (c) = t;
23910   OMP_CLAUSE_CHAIN (c) = list;
23911
23912   return c;
23913 }
23914
23915 /* OpenMP 2.5:
23916    if ( expression ) */
23917
23918 static tree
23919 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23920 {
23921   tree t, c;
23922
23923   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23924     return list;
23925
23926   t = cp_parser_condition (parser);
23927
23928   if (t == error_mark_node
23929       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23930     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23931                                            /*or_comma=*/false,
23932                                            /*consume_paren=*/true);
23933
23934   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23935
23936   c = build_omp_clause (location, OMP_CLAUSE_IF);
23937   OMP_CLAUSE_IF_EXPR (c) = t;
23938   OMP_CLAUSE_CHAIN (c) = list;
23939
23940   return c;
23941 }
23942
23943 /* OpenMP 3.1:
23944    mergeable */
23945
23946 static tree
23947 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
23948                                 tree list, location_t location)
23949 {
23950   tree c;
23951
23952   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
23953                              location);
23954
23955   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
23956   OMP_CLAUSE_CHAIN (c) = list;
23957   return c;
23958 }
23959
23960 /* OpenMP 2.5:
23961    nowait */
23962
23963 static tree
23964 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23965                              tree list, location_t location)
23966 {
23967   tree c;
23968
23969   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23970
23971   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23972   OMP_CLAUSE_CHAIN (c) = list;
23973   return c;
23974 }
23975
23976 /* OpenMP 2.5:
23977    num_threads ( expression ) */
23978
23979 static tree
23980 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23981                                   location_t location)
23982 {
23983   tree t, c;
23984
23985   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23986     return list;
23987
23988   t = cp_parser_expression (parser, false, NULL);
23989
23990   if (t == error_mark_node
23991       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23992     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23993                                            /*or_comma=*/false,
23994                                            /*consume_paren=*/true);
23995
23996   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23997                              "num_threads", location);
23998
23999   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24000   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24001   OMP_CLAUSE_CHAIN (c) = list;
24002
24003   return c;
24004 }
24005
24006 /* OpenMP 2.5:
24007    ordered */
24008
24009 static tree
24010 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24011                               tree list, location_t location)
24012 {
24013   tree c;
24014
24015   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24016                              "ordered", location);
24017
24018   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24019   OMP_CLAUSE_CHAIN (c) = list;
24020   return c;
24021 }
24022
24023 /* OpenMP 2.5:
24024    reduction ( reduction-operator : variable-list )
24025
24026    reduction-operator:
24027      One of: + * - & ^ | && ||
24028
24029    OpenMP 3.1:
24030
24031    reduction-operator:
24032      One of: + * - & ^ | && || min max  */
24033
24034 static tree
24035 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24036 {
24037   enum tree_code code;
24038   tree nlist, c;
24039
24040   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24041     return list;
24042
24043   switch (cp_lexer_peek_token (parser->lexer)->type)
24044     {
24045     case CPP_PLUS:
24046       code = PLUS_EXPR;
24047       break;
24048     case CPP_MULT:
24049       code = MULT_EXPR;
24050       break;
24051     case CPP_MINUS:
24052       code = MINUS_EXPR;
24053       break;
24054     case CPP_AND:
24055       code = BIT_AND_EXPR;
24056       break;
24057     case CPP_XOR:
24058       code = BIT_XOR_EXPR;
24059       break;
24060     case CPP_OR:
24061       code = BIT_IOR_EXPR;
24062       break;
24063     case CPP_AND_AND:
24064       code = TRUTH_ANDIF_EXPR;
24065       break;
24066     case CPP_OR_OR:
24067       code = TRUTH_ORIF_EXPR;
24068       break;
24069     case CPP_NAME:
24070       {
24071         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24072         const char *p = IDENTIFIER_POINTER (id);
24073
24074         if (strcmp (p, "min") == 0)
24075           {
24076             code = MIN_EXPR;
24077             break;
24078           }
24079         if (strcmp (p, "max") == 0)
24080           {
24081             code = MAX_EXPR;
24082             break;
24083           }
24084       }
24085       /* FALLTHROUGH */
24086     default:
24087       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24088                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24089     resync_fail:
24090       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24091                                              /*or_comma=*/false,
24092                                              /*consume_paren=*/true);
24093       return list;
24094     }
24095   cp_lexer_consume_token (parser->lexer);
24096
24097   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24098     goto resync_fail;
24099
24100   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24101   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24102     OMP_CLAUSE_REDUCTION_CODE (c) = code;
24103
24104   return nlist;
24105 }
24106
24107 /* OpenMP 2.5:
24108    schedule ( schedule-kind )
24109    schedule ( schedule-kind , expression )
24110
24111    schedule-kind:
24112      static | dynamic | guided | runtime | auto  */
24113
24114 static tree
24115 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24116 {
24117   tree c, t;
24118
24119   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24120     return list;
24121
24122   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24123
24124   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24125     {
24126       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24127       const char *p = IDENTIFIER_POINTER (id);
24128
24129       switch (p[0])
24130         {
24131         case 'd':
24132           if (strcmp ("dynamic", p) != 0)
24133             goto invalid_kind;
24134           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24135           break;
24136
24137         case 'g':
24138           if (strcmp ("guided", p) != 0)
24139             goto invalid_kind;
24140           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24141           break;
24142
24143         case 'r':
24144           if (strcmp ("runtime", p) != 0)
24145             goto invalid_kind;
24146           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24147           break;
24148
24149         default:
24150           goto invalid_kind;
24151         }
24152     }
24153   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24154     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24155   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24156     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24157   else
24158     goto invalid_kind;
24159   cp_lexer_consume_token (parser->lexer);
24160
24161   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24162     {
24163       cp_token *token;
24164       cp_lexer_consume_token (parser->lexer);
24165
24166       token = cp_lexer_peek_token (parser->lexer);
24167       t = cp_parser_assignment_expression (parser, false, NULL);
24168
24169       if (t == error_mark_node)
24170         goto resync_fail;
24171       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24172         error_at (token->location, "schedule %<runtime%> does not take "
24173                   "a %<chunk_size%> parameter");
24174       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24175         error_at (token->location, "schedule %<auto%> does not take "
24176                   "a %<chunk_size%> parameter");
24177       else
24178         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24179
24180       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24181         goto resync_fail;
24182     }
24183   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
24184     goto resync_fail;
24185
24186   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
24187   OMP_CLAUSE_CHAIN (c) = list;
24188   return c;
24189
24190  invalid_kind:
24191   cp_parser_error (parser, "invalid schedule kind");
24192  resync_fail:
24193   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24194                                          /*or_comma=*/false,
24195                                          /*consume_paren=*/true);
24196   return list;
24197 }
24198
24199 /* OpenMP 3.0:
24200    untied */
24201
24202 static tree
24203 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
24204                              tree list, location_t location)
24205 {
24206   tree c;
24207
24208   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
24209
24210   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
24211   OMP_CLAUSE_CHAIN (c) = list;
24212   return c;
24213 }
24214
24215 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
24216    is a bitmask in MASK.  Return the list of clauses found; the result
24217    of clause default goes in *pdefault.  */
24218
24219 static tree
24220 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
24221                            const char *where, cp_token *pragma_tok)
24222 {
24223   tree clauses = NULL;
24224   bool first = true;
24225   cp_token *token = NULL;
24226
24227   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
24228     {
24229       pragma_omp_clause c_kind;
24230       const char *c_name;
24231       tree prev = clauses;
24232
24233       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24234         cp_lexer_consume_token (parser->lexer);
24235
24236       token = cp_lexer_peek_token (parser->lexer);
24237       c_kind = cp_parser_omp_clause_name (parser);
24238       first = false;
24239
24240       switch (c_kind)
24241         {
24242         case PRAGMA_OMP_CLAUSE_COLLAPSE:
24243           clauses = cp_parser_omp_clause_collapse (parser, clauses,
24244                                                    token->location);
24245           c_name = "collapse";
24246           break;
24247         case PRAGMA_OMP_CLAUSE_COPYIN:
24248           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
24249           c_name = "copyin";
24250           break;
24251         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
24252           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
24253                                             clauses);
24254           c_name = "copyprivate";
24255           break;
24256         case PRAGMA_OMP_CLAUSE_DEFAULT:
24257           clauses = cp_parser_omp_clause_default (parser, clauses,
24258                                                   token->location);
24259           c_name = "default";
24260           break;
24261         case PRAGMA_OMP_CLAUSE_FINAL:
24262           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
24263           c_name = "final";
24264           break;
24265         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
24266           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
24267                                             clauses);
24268           c_name = "firstprivate";
24269           break;
24270         case PRAGMA_OMP_CLAUSE_IF:
24271           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
24272           c_name = "if";
24273           break;
24274         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24275           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24276                                             clauses);
24277           c_name = "lastprivate";
24278           break;
24279         case PRAGMA_OMP_CLAUSE_MERGEABLE:
24280           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
24281                                                     token->location);
24282           c_name = "mergeable";
24283           break;
24284         case PRAGMA_OMP_CLAUSE_NOWAIT:
24285           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24286           c_name = "nowait";
24287           break;
24288         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24289           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24290                                                       token->location);
24291           c_name = "num_threads";
24292           break;
24293         case PRAGMA_OMP_CLAUSE_ORDERED:
24294           clauses = cp_parser_omp_clause_ordered (parser, clauses,
24295                                                   token->location);
24296           c_name = "ordered";
24297           break;
24298         case PRAGMA_OMP_CLAUSE_PRIVATE:
24299           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24300                                             clauses);
24301           c_name = "private";
24302           break;
24303         case PRAGMA_OMP_CLAUSE_REDUCTION:
24304           clauses = cp_parser_omp_clause_reduction (parser, clauses);
24305           c_name = "reduction";
24306           break;
24307         case PRAGMA_OMP_CLAUSE_SCHEDULE:
24308           clauses = cp_parser_omp_clause_schedule (parser, clauses,
24309                                                    token->location);
24310           c_name = "schedule";
24311           break;
24312         case PRAGMA_OMP_CLAUSE_SHARED:
24313           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24314                                             clauses);
24315           c_name = "shared";
24316           break;
24317         case PRAGMA_OMP_CLAUSE_UNTIED:
24318           clauses = cp_parser_omp_clause_untied (parser, clauses,
24319                                                  token->location);
24320           c_name = "nowait";
24321           break;
24322         default:
24323           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24324           goto saw_error;
24325         }
24326
24327       if (((mask >> c_kind) & 1) == 0)
24328         {
24329           /* Remove the invalid clause(s) from the list to avoid
24330              confusing the rest of the compiler.  */
24331           clauses = prev;
24332           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24333         }
24334     }
24335  saw_error:
24336   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24337   return finish_omp_clauses (clauses);
24338 }
24339
24340 /* OpenMP 2.5:
24341    structured-block:
24342      statement
24343
24344    In practice, we're also interested in adding the statement to an
24345    outer node.  So it is convenient if we work around the fact that
24346    cp_parser_statement calls add_stmt.  */
24347
24348 static unsigned
24349 cp_parser_begin_omp_structured_block (cp_parser *parser)
24350 {
24351   unsigned save = parser->in_statement;
24352
24353   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24354      This preserves the "not within loop or switch" style error messages
24355      for nonsense cases like
24356         void foo() {
24357         #pragma omp single
24358           break;
24359         }
24360   */
24361   if (parser->in_statement)
24362     parser->in_statement = IN_OMP_BLOCK;
24363
24364   return save;
24365 }
24366
24367 static void
24368 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24369 {
24370   parser->in_statement = save;
24371 }
24372
24373 static tree
24374 cp_parser_omp_structured_block (cp_parser *parser)
24375 {
24376   tree stmt = begin_omp_structured_block ();
24377   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24378
24379   cp_parser_statement (parser, NULL_TREE, false, NULL);
24380
24381   cp_parser_end_omp_structured_block (parser, save);
24382   return finish_omp_structured_block (stmt);
24383 }
24384
24385 /* OpenMP 2.5:
24386    # pragma omp atomic new-line
24387      expression-stmt
24388
24389    expression-stmt:
24390      x binop= expr | x++ | ++x | x-- | --x
24391    binop:
24392      +, *, -, /, &, ^, |, <<, >>
24393
24394   where x is an lvalue expression with scalar type.
24395
24396    OpenMP 3.1:
24397    # pragma omp atomic new-line
24398      update-stmt
24399
24400    # pragma omp atomic read new-line
24401      read-stmt
24402
24403    # pragma omp atomic write new-line
24404      write-stmt
24405
24406    # pragma omp atomic update new-line
24407      update-stmt
24408
24409    # pragma omp atomic capture new-line
24410      capture-stmt
24411
24412    # pragma omp atomic capture new-line
24413      capture-block
24414
24415    read-stmt:
24416      v = x
24417    write-stmt:
24418      x = expr
24419    update-stmt:
24420      expression-stmt | x = x binop expr
24421    capture-stmt:
24422      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
24423    capture-block:
24424      { v = x; update-stmt; } | { update-stmt; v = x; }
24425
24426   where x and v are lvalue expressions with scalar type.  */
24427
24428 static void
24429 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24430 {
24431   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
24432   tree rhs1 = NULL_TREE, orig_lhs;
24433   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
24434   bool structured_block = false;
24435
24436   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24437     {
24438       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24439       const char *p = IDENTIFIER_POINTER (id);
24440
24441       if (!strcmp (p, "read"))
24442         code = OMP_ATOMIC_READ;
24443       else if (!strcmp (p, "write"))
24444         code = NOP_EXPR;
24445       else if (!strcmp (p, "update"))
24446         code = OMP_ATOMIC;
24447       else if (!strcmp (p, "capture"))
24448         code = OMP_ATOMIC_CAPTURE_NEW;
24449       else
24450         p = NULL;
24451       if (p)
24452         cp_lexer_consume_token (parser->lexer);
24453     }
24454   cp_parser_require_pragma_eol (parser, pragma_tok);
24455
24456   switch (code)
24457     {
24458     case OMP_ATOMIC_READ:
24459     case NOP_EXPR: /* atomic write */
24460       v = cp_parser_unary_expression (parser, /*address_p=*/false,
24461                                       /*cast_p=*/false, NULL);
24462       if (v == error_mark_node)
24463         goto saw_error;
24464       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24465         goto saw_error;
24466       if (code == NOP_EXPR)
24467         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24468       else
24469         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24470                                           /*cast_p=*/false, NULL);
24471       if (lhs == error_mark_node)
24472         goto saw_error;
24473       if (code == NOP_EXPR)
24474         {
24475           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
24476              opcode.  */
24477           code = OMP_ATOMIC;
24478           rhs = lhs;
24479           lhs = v;
24480           v = NULL_TREE;
24481         }
24482       goto done;
24483     case OMP_ATOMIC_CAPTURE_NEW:
24484       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24485         {
24486           cp_lexer_consume_token (parser->lexer);
24487           structured_block = true;
24488         }
24489       else
24490         {
24491           v = cp_parser_unary_expression (parser, /*address_p=*/false,
24492                                           /*cast_p=*/false, NULL);
24493           if (v == error_mark_node)
24494             goto saw_error;
24495           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24496             goto saw_error;
24497         }
24498     default:
24499       break;
24500     }
24501
24502 restart:
24503   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24504                                     /*cast_p=*/false, NULL);
24505   orig_lhs = lhs;
24506   switch (TREE_CODE (lhs))
24507     {
24508     case ERROR_MARK:
24509       goto saw_error;
24510
24511     case POSTINCREMENT_EXPR:
24512       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24513         code = OMP_ATOMIC_CAPTURE_OLD;
24514       /* FALLTHROUGH */
24515     case PREINCREMENT_EXPR:
24516       lhs = TREE_OPERAND (lhs, 0);
24517       opcode = PLUS_EXPR;
24518       rhs = integer_one_node;
24519       break;
24520
24521     case POSTDECREMENT_EXPR:
24522       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
24523         code = OMP_ATOMIC_CAPTURE_OLD;
24524       /* FALLTHROUGH */
24525     case PREDECREMENT_EXPR:
24526       lhs = TREE_OPERAND (lhs, 0);
24527       opcode = MINUS_EXPR;
24528       rhs = integer_one_node;
24529       break;
24530
24531     case COMPOUND_EXPR:
24532       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24533          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24534          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24535          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24536          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24537                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24538             == BOOLEAN_TYPE)
24539        /* Undo effects of boolean_increment for post {in,de}crement.  */
24540        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24541       /* FALLTHRU */
24542     case MODIFY_EXPR:
24543       if (TREE_CODE (lhs) == MODIFY_EXPR
24544          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24545         {
24546           /* Undo effects of boolean_increment.  */
24547           if (integer_onep (TREE_OPERAND (lhs, 1)))
24548             {
24549               /* This is pre or post increment.  */
24550               rhs = TREE_OPERAND (lhs, 1);
24551               lhs = TREE_OPERAND (lhs, 0);
24552               opcode = NOP_EXPR;
24553               if (code == OMP_ATOMIC_CAPTURE_NEW
24554                   && !structured_block
24555                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
24556                 code = OMP_ATOMIC_CAPTURE_OLD;
24557               break;
24558             }
24559         }
24560       /* FALLTHRU */
24561     default:
24562       switch (cp_lexer_peek_token (parser->lexer)->type)
24563         {
24564         case CPP_MULT_EQ:
24565           opcode = MULT_EXPR;
24566           break;
24567         case CPP_DIV_EQ:
24568           opcode = TRUNC_DIV_EXPR;
24569           break;
24570         case CPP_PLUS_EQ:
24571           opcode = PLUS_EXPR;
24572           break;
24573         case CPP_MINUS_EQ:
24574           opcode = MINUS_EXPR;
24575           break;
24576         case CPP_LSHIFT_EQ:
24577           opcode = LSHIFT_EXPR;
24578           break;
24579         case CPP_RSHIFT_EQ:
24580           opcode = RSHIFT_EXPR;
24581           break;
24582         case CPP_AND_EQ:
24583           opcode = BIT_AND_EXPR;
24584           break;
24585         case CPP_OR_EQ:
24586           opcode = BIT_IOR_EXPR;
24587           break;
24588         case CPP_XOR_EQ:
24589           opcode = BIT_XOR_EXPR;
24590           break;
24591         case CPP_EQ:
24592           if (structured_block || code == OMP_ATOMIC)
24593             {
24594               enum cp_parser_prec oprec;
24595               cp_token *token;
24596               cp_lexer_consume_token (parser->lexer);
24597               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24598                                                  /*cast_p=*/false, NULL);
24599               if (rhs1 == error_mark_node)
24600                 goto saw_error;
24601               token = cp_lexer_peek_token (parser->lexer);
24602               switch (token->type)
24603                 {
24604                 case CPP_SEMICOLON:
24605                   if (code == OMP_ATOMIC_CAPTURE_NEW)
24606                     {
24607                       code = OMP_ATOMIC_CAPTURE_OLD;
24608                       v = lhs;
24609                       lhs = NULL_TREE;
24610                       lhs1 = rhs1;
24611                       rhs1 = NULL_TREE;
24612                       cp_lexer_consume_token (parser->lexer);
24613                       goto restart;
24614                     }
24615                   cp_parser_error (parser,
24616                                    "invalid form of %<#pragma omp atomic%>");
24617                   goto saw_error;
24618                 case CPP_MULT:
24619                   opcode = MULT_EXPR;
24620                   break;
24621                 case CPP_DIV:
24622                   opcode = TRUNC_DIV_EXPR;
24623                   break;
24624                 case CPP_PLUS:
24625                   opcode = PLUS_EXPR;
24626                   break;
24627                 case CPP_MINUS:
24628                   opcode = MINUS_EXPR;
24629                   break;
24630                 case CPP_LSHIFT:
24631                   opcode = LSHIFT_EXPR;
24632                   break;
24633                 case CPP_RSHIFT:
24634                   opcode = RSHIFT_EXPR;
24635                   break;
24636                 case CPP_AND:
24637                   opcode = BIT_AND_EXPR;
24638                   break;
24639                 case CPP_OR:
24640                   opcode = BIT_IOR_EXPR;
24641                   break;
24642                 case CPP_XOR:
24643                   opcode = BIT_XOR_EXPR;
24644                   break;
24645                 default:
24646                   cp_parser_error (parser,
24647                                    "invalid operator for %<#pragma omp atomic%>");
24648                   goto saw_error;
24649                 }
24650               oprec = TOKEN_PRECEDENCE (token);
24651               gcc_assert (oprec != PREC_NOT_OPERATOR);
24652               if (commutative_tree_code (opcode))
24653                 oprec = (enum cp_parser_prec) (oprec - 1);
24654               cp_lexer_consume_token (parser->lexer);
24655               rhs = cp_parser_binary_expression (parser, false, false,
24656                                                  oprec, NULL);
24657               if (rhs == error_mark_node)
24658                 goto saw_error;
24659               goto stmt_done;
24660             }
24661           /* FALLTHROUGH */
24662         default:
24663           cp_parser_error (parser,
24664                            "invalid operator for %<#pragma omp atomic%>");
24665           goto saw_error;
24666         }
24667       cp_lexer_consume_token (parser->lexer);
24668
24669       rhs = cp_parser_expression (parser, false, NULL);
24670       if (rhs == error_mark_node)
24671         goto saw_error;
24672       break;
24673     }
24674 stmt_done:
24675   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
24676     {
24677       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
24678         goto saw_error;
24679       v = cp_parser_unary_expression (parser, /*address_p=*/false,
24680                                       /*cast_p=*/false, NULL);
24681       if (v == error_mark_node)
24682         goto saw_error;
24683       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
24684         goto saw_error;
24685       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
24686                                          /*cast_p=*/false, NULL);
24687       if (lhs1 == error_mark_node)
24688         goto saw_error;
24689     }
24690   if (structured_block)
24691     {
24692       cp_parser_consume_semicolon_at_end_of_statement (parser);
24693       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24694     }
24695 done:
24696   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
24697   if (!structured_block)
24698     cp_parser_consume_semicolon_at_end_of_statement (parser);
24699   return;
24700
24701  saw_error:
24702   cp_parser_skip_to_end_of_block_or_statement (parser);
24703   if (structured_block)
24704     {
24705       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24706         cp_lexer_consume_token (parser->lexer);
24707       else if (code == OMP_ATOMIC_CAPTURE_NEW)
24708         {
24709           cp_parser_skip_to_end_of_block_or_statement (parser);
24710           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24711             cp_lexer_consume_token (parser->lexer);
24712         }
24713     }
24714 }
24715
24716
24717 /* OpenMP 2.5:
24718    # pragma omp barrier new-line  */
24719
24720 static void
24721 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24722 {
24723   cp_parser_require_pragma_eol (parser, pragma_tok);
24724   finish_omp_barrier ();
24725 }
24726
24727 /* OpenMP 2.5:
24728    # pragma omp critical [(name)] new-line
24729      structured-block  */
24730
24731 static tree
24732 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24733 {
24734   tree stmt, name = NULL;
24735
24736   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24737     {
24738       cp_lexer_consume_token (parser->lexer);
24739
24740       name = cp_parser_identifier (parser);
24741
24742       if (name == error_mark_node
24743           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24744         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24745                                                /*or_comma=*/false,
24746                                                /*consume_paren=*/true);
24747       if (name == error_mark_node)
24748         name = NULL;
24749     }
24750   cp_parser_require_pragma_eol (parser, pragma_tok);
24751
24752   stmt = cp_parser_omp_structured_block (parser);
24753   return c_finish_omp_critical (input_location, stmt, name);
24754 }
24755
24756 /* OpenMP 2.5:
24757    # pragma omp flush flush-vars[opt] new-line
24758
24759    flush-vars:
24760      ( variable-list ) */
24761
24762 static void
24763 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24764 {
24765   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24766     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24767   cp_parser_require_pragma_eol (parser, pragma_tok);
24768
24769   finish_omp_flush ();
24770 }
24771
24772 /* Helper function, to parse omp for increment expression.  */
24773
24774 static tree
24775 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24776 {
24777   tree cond = cp_parser_binary_expression (parser, false, true,
24778                                            PREC_NOT_OPERATOR, NULL);
24779   if (cond == error_mark_node
24780       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24781     {
24782       cp_parser_skip_to_end_of_statement (parser);
24783       return error_mark_node;
24784     }
24785
24786   switch (TREE_CODE (cond))
24787     {
24788     case GT_EXPR:
24789     case GE_EXPR:
24790     case LT_EXPR:
24791     case LE_EXPR:
24792       break;
24793     default:
24794       return error_mark_node;
24795     }
24796
24797   /* If decl is an iterator, preserve LHS and RHS of the relational
24798      expr until finish_omp_for.  */
24799   if (decl
24800       && (type_dependent_expression_p (decl)
24801           || CLASS_TYPE_P (TREE_TYPE (decl))))
24802     return cond;
24803
24804   return build_x_binary_op (TREE_CODE (cond),
24805                             TREE_OPERAND (cond, 0), ERROR_MARK,
24806                             TREE_OPERAND (cond, 1), ERROR_MARK,
24807                             /*overload=*/NULL, tf_warning_or_error);
24808 }
24809
24810 /* Helper function, to parse omp for increment expression.  */
24811
24812 static tree
24813 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24814 {
24815   cp_token *token = cp_lexer_peek_token (parser->lexer);
24816   enum tree_code op;
24817   tree lhs, rhs;
24818   cp_id_kind idk;
24819   bool decl_first;
24820
24821   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24822     {
24823       op = (token->type == CPP_PLUS_PLUS
24824             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24825       cp_lexer_consume_token (parser->lexer);
24826       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24827       if (lhs != decl)
24828         return error_mark_node;
24829       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24830     }
24831
24832   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24833   if (lhs != decl)
24834     return error_mark_node;
24835
24836   token = cp_lexer_peek_token (parser->lexer);
24837   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24838     {
24839       op = (token->type == CPP_PLUS_PLUS
24840             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24841       cp_lexer_consume_token (parser->lexer);
24842       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24843     }
24844
24845   op = cp_parser_assignment_operator_opt (parser);
24846   if (op == ERROR_MARK)
24847     return error_mark_node;
24848
24849   if (op != NOP_EXPR)
24850     {
24851       rhs = cp_parser_assignment_expression (parser, false, NULL);
24852       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24853       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24854     }
24855
24856   lhs = cp_parser_binary_expression (parser, false, false,
24857                                      PREC_ADDITIVE_EXPRESSION, NULL);
24858   token = cp_lexer_peek_token (parser->lexer);
24859   decl_first = lhs == decl;
24860   if (decl_first)
24861     lhs = NULL_TREE;
24862   if (token->type != CPP_PLUS
24863       && token->type != CPP_MINUS)
24864     return error_mark_node;
24865
24866   do
24867     {
24868       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24869       cp_lexer_consume_token (parser->lexer);
24870       rhs = cp_parser_binary_expression (parser, false, false,
24871                                          PREC_ADDITIVE_EXPRESSION, NULL);
24872       token = cp_lexer_peek_token (parser->lexer);
24873       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24874         {
24875           if (lhs == NULL_TREE)
24876             {
24877               if (op == PLUS_EXPR)
24878                 lhs = rhs;
24879               else
24880                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24881             }
24882           else
24883             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24884                                      NULL, tf_warning_or_error);
24885         }
24886     }
24887   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24888
24889   if (!decl_first)
24890     {
24891       if (rhs != decl || op == MINUS_EXPR)
24892         return error_mark_node;
24893       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24894     }
24895   else
24896     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24897
24898   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24899 }
24900
24901 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24902
24903 static tree
24904 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24905 {
24906   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24907   tree real_decl, initv, condv, incrv, declv;
24908   tree this_pre_body, cl;
24909   location_t loc_first;
24910   bool collapse_err = false;
24911   int i, collapse = 1, nbraces = 0;
24912   VEC(tree,gc) *for_block = make_tree_vector ();
24913
24914   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24915     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24916       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24917
24918   gcc_assert (collapse >= 1);
24919
24920   declv = make_tree_vec (collapse);
24921   initv = make_tree_vec (collapse);
24922   condv = make_tree_vec (collapse);
24923   incrv = make_tree_vec (collapse);
24924
24925   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24926
24927   for (i = 0; i < collapse; i++)
24928     {
24929       int bracecount = 0;
24930       bool add_private_clause = false;
24931       location_t loc;
24932
24933       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24934         {
24935           cp_parser_error (parser, "for statement expected");
24936           return NULL;
24937         }
24938       loc = cp_lexer_consume_token (parser->lexer)->location;
24939
24940       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24941         return NULL;
24942
24943       init = decl = real_decl = NULL;
24944       this_pre_body = push_stmt_list ();
24945       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24946         {
24947           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24948
24949              init-expr:
24950                        var = lb
24951                        integer-type var = lb
24952                        random-access-iterator-type var = lb
24953                        pointer-type var = lb
24954           */
24955           cp_decl_specifier_seq type_specifiers;
24956
24957           /* First, try to parse as an initialized declaration.  See
24958              cp_parser_condition, from whence the bulk of this is copied.  */
24959
24960           cp_parser_parse_tentatively (parser);
24961           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24962                                         /*is_trailing_return=*/false,
24963                                         &type_specifiers);
24964           if (cp_parser_parse_definitely (parser))
24965             {
24966               /* If parsing a type specifier seq succeeded, then this
24967                  MUST be a initialized declaration.  */
24968               tree asm_specification, attributes;
24969               cp_declarator *declarator;
24970
24971               declarator = cp_parser_declarator (parser,
24972                                                  CP_PARSER_DECLARATOR_NAMED,
24973                                                  /*ctor_dtor_or_conv_p=*/NULL,
24974                                                  /*parenthesized_p=*/NULL,
24975                                                  /*member_p=*/false);
24976               attributes = cp_parser_attributes_opt (parser);
24977               asm_specification = cp_parser_asm_specification_opt (parser);
24978
24979               if (declarator == cp_error_declarator) 
24980                 cp_parser_skip_to_end_of_statement (parser);
24981
24982               else 
24983                 {
24984                   tree pushed_scope, auto_node;
24985
24986                   decl = start_decl (declarator, &type_specifiers,
24987                                      SD_INITIALIZED, attributes,
24988                                      /*prefix_attributes=*/NULL_TREE,
24989                                      &pushed_scope);
24990
24991                   auto_node = type_uses_auto (TREE_TYPE (decl));
24992                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24993                     {
24994                       if (cp_lexer_next_token_is (parser->lexer, 
24995                                                   CPP_OPEN_PAREN))
24996                         error ("parenthesized initialization is not allowed in "
24997                                "OpenMP %<for%> loop");
24998                       else
24999                         /* Trigger an error.  */
25000                         cp_parser_require (parser, CPP_EQ, RT_EQ);
25001
25002                       init = error_mark_node;
25003                       cp_parser_skip_to_end_of_statement (parser);
25004                     }
25005                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
25006                            || type_dependent_expression_p (decl)
25007                            || auto_node)
25008                     {
25009                       bool is_direct_init, is_non_constant_init;
25010
25011                       init = cp_parser_initializer (parser,
25012                                                     &is_direct_init,
25013                                                     &is_non_constant_init);
25014
25015                       if (auto_node)
25016                         {
25017                           TREE_TYPE (decl)
25018                             = do_auto_deduction (TREE_TYPE (decl), init,
25019                                                  auto_node);
25020
25021                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
25022                               && !type_dependent_expression_p (decl))
25023                             goto non_class;
25024                         }
25025                       
25026                       cp_finish_decl (decl, init, !is_non_constant_init,
25027                                       asm_specification,
25028                                       LOOKUP_ONLYCONVERTING);
25029                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
25030                         {
25031                           VEC_safe_push (tree, gc, for_block, this_pre_body);
25032                           init = NULL_TREE;
25033                         }
25034                       else
25035                         init = pop_stmt_list (this_pre_body);
25036                       this_pre_body = NULL_TREE;
25037                     }
25038                   else
25039                     {
25040                       /* Consume '='.  */
25041                       cp_lexer_consume_token (parser->lexer);
25042                       init = cp_parser_assignment_expression (parser, false, NULL);
25043
25044                     non_class:
25045                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25046                         init = error_mark_node;
25047                       else
25048                         cp_finish_decl (decl, NULL_TREE,
25049                                         /*init_const_expr_p=*/false,
25050                                         asm_specification,
25051                                         LOOKUP_ONLYCONVERTING);
25052                     }
25053
25054                   if (pushed_scope)
25055                     pop_scope (pushed_scope);
25056                 }
25057             }
25058           else 
25059             {
25060               cp_id_kind idk;
25061               /* If parsing a type specifier sequence failed, then
25062                  this MUST be a simple expression.  */
25063               cp_parser_parse_tentatively (parser);
25064               decl = cp_parser_primary_expression (parser, false, false,
25065                                                    false, &idk);
25066               if (!cp_parser_error_occurred (parser)
25067                   && decl
25068                   && DECL_P (decl)
25069                   && CLASS_TYPE_P (TREE_TYPE (decl)))
25070                 {
25071                   tree rhs;
25072
25073                   cp_parser_parse_definitely (parser);
25074                   cp_parser_require (parser, CPP_EQ, RT_EQ);
25075                   rhs = cp_parser_assignment_expression (parser, false, NULL);
25076                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25077                                                          rhs,
25078                                                          tf_warning_or_error));
25079                   add_private_clause = true;
25080                 }
25081               else
25082                 {
25083                   decl = NULL;
25084                   cp_parser_abort_tentative_parse (parser);
25085                   init = cp_parser_expression (parser, false, NULL);
25086                   if (init)
25087                     {
25088                       if (TREE_CODE (init) == MODIFY_EXPR
25089                           || TREE_CODE (init) == MODOP_EXPR)
25090                         real_decl = TREE_OPERAND (init, 0);
25091                     }
25092                 }
25093             }
25094         }
25095       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25096       if (this_pre_body)
25097         {
25098           this_pre_body = pop_stmt_list (this_pre_body);
25099           if (pre_body)
25100             {
25101               tree t = pre_body;
25102               pre_body = push_stmt_list ();
25103               add_stmt (t);
25104               add_stmt (this_pre_body);
25105               pre_body = pop_stmt_list (pre_body);
25106             }
25107           else
25108             pre_body = this_pre_body;
25109         }
25110
25111       if (decl)
25112         real_decl = decl;
25113       if (par_clauses != NULL && real_decl != NULL_TREE)
25114         {
25115           tree *c;
25116           for (c = par_clauses; *c ; )
25117             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25118                 && OMP_CLAUSE_DECL (*c) == real_decl)
25119               {
25120                 error_at (loc, "iteration variable %qD"
25121                           " should not be firstprivate", real_decl);
25122                 *c = OMP_CLAUSE_CHAIN (*c);
25123               }
25124             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25125                      && OMP_CLAUSE_DECL (*c) == real_decl)
25126               {
25127                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25128                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
25129                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25130                 OMP_CLAUSE_DECL (l) = real_decl;
25131                 OMP_CLAUSE_CHAIN (l) = clauses;
25132                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25133                 clauses = l;
25134                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25135                 CP_OMP_CLAUSE_INFO (*c) = NULL;
25136                 add_private_clause = false;
25137               }
25138             else
25139               {
25140                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25141                     && OMP_CLAUSE_DECL (*c) == real_decl)
25142                   add_private_clause = false;
25143                 c = &OMP_CLAUSE_CHAIN (*c);
25144               }
25145         }
25146
25147       if (add_private_clause)
25148         {
25149           tree c;
25150           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25151             {
25152               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25153                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25154                   && OMP_CLAUSE_DECL (c) == decl)
25155                 break;
25156               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25157                        && OMP_CLAUSE_DECL (c) == decl)
25158                 error_at (loc, "iteration variable %qD "
25159                           "should not be firstprivate",
25160                           decl);
25161               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25162                        && OMP_CLAUSE_DECL (c) == decl)
25163                 error_at (loc, "iteration variable %qD should not be reduction",
25164                           decl);
25165             }
25166           if (c == NULL)
25167             {
25168               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25169               OMP_CLAUSE_DECL (c) = decl;
25170               c = finish_omp_clauses (c);
25171               if (c)
25172                 {
25173                   OMP_CLAUSE_CHAIN (c) = clauses;
25174                   clauses = c;
25175                 }
25176             }
25177         }
25178
25179       cond = NULL;
25180       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25181         cond = cp_parser_omp_for_cond (parser, decl);
25182       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25183
25184       incr = NULL;
25185       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
25186         {
25187           /* If decl is an iterator, preserve the operator on decl
25188              until finish_omp_for.  */
25189           if (decl
25190               && ((type_dependent_expression_p (decl)
25191                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
25192                   || CLASS_TYPE_P (TREE_TYPE (decl))))
25193             incr = cp_parser_omp_for_incr (parser, decl);
25194           else
25195             incr = cp_parser_expression (parser, false, NULL);
25196         }
25197
25198       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25199         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25200                                                /*or_comma=*/false,
25201                                                /*consume_paren=*/true);
25202
25203       TREE_VEC_ELT (declv, i) = decl;
25204       TREE_VEC_ELT (initv, i) = init;
25205       TREE_VEC_ELT (condv, i) = cond;
25206       TREE_VEC_ELT (incrv, i) = incr;
25207
25208       if (i == collapse - 1)
25209         break;
25210
25211       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
25212          in between the collapsed for loops to be still considered perfectly
25213          nested.  Hopefully the final version clarifies this.
25214          For now handle (multiple) {'s and empty statements.  */
25215       cp_parser_parse_tentatively (parser);
25216       do
25217         {
25218           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25219             break;
25220           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25221             {
25222               cp_lexer_consume_token (parser->lexer);
25223               bracecount++;
25224             }
25225           else if (bracecount
25226                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25227             cp_lexer_consume_token (parser->lexer);
25228           else
25229             {
25230               loc = cp_lexer_peek_token (parser->lexer)->location;
25231               error_at (loc, "not enough collapsed for loops");
25232               collapse_err = true;
25233               cp_parser_abort_tentative_parse (parser);
25234               declv = NULL_TREE;
25235               break;
25236             }
25237         }
25238       while (1);
25239
25240       if (declv)
25241         {
25242           cp_parser_parse_definitely (parser);
25243           nbraces += bracecount;
25244         }
25245     }
25246
25247   /* Note that we saved the original contents of this flag when we entered
25248      the structured block, and so we don't need to re-save it here.  */
25249   parser->in_statement = IN_OMP_FOR;
25250
25251   /* Note that the grammar doesn't call for a structured block here,
25252      though the loop as a whole is a structured block.  */
25253   body = push_stmt_list ();
25254   cp_parser_statement (parser, NULL_TREE, false, NULL);
25255   body = pop_stmt_list (body);
25256
25257   if (declv == NULL_TREE)
25258     ret = NULL_TREE;
25259   else
25260     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
25261                           pre_body, clauses);
25262
25263   while (nbraces)
25264     {
25265       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25266         {
25267           cp_lexer_consume_token (parser->lexer);
25268           nbraces--;
25269         }
25270       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
25271         cp_lexer_consume_token (parser->lexer);
25272       else
25273         {
25274           if (!collapse_err)
25275             {
25276               error_at (cp_lexer_peek_token (parser->lexer)->location,
25277                         "collapsed loops not perfectly nested");
25278             }
25279           collapse_err = true;
25280           cp_parser_statement_seq_opt (parser, NULL);
25281           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
25282             break;
25283         }
25284     }
25285
25286   while (!VEC_empty (tree, for_block))
25287     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
25288   release_tree_vector (for_block);
25289
25290   return ret;
25291 }
25292
25293 /* OpenMP 2.5:
25294    #pragma omp for for-clause[optseq] new-line
25295      for-loop  */
25296
25297 #define OMP_FOR_CLAUSE_MASK                             \
25298         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25299         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25300         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25301         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25302         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
25303         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
25304         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
25305         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
25306
25307 static tree
25308 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
25309 {
25310   tree clauses, sb, ret;
25311   unsigned int save;
25312
25313   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
25314                                        "#pragma omp for", pragma_tok);
25315
25316   sb = begin_omp_structured_block ();
25317   save = cp_parser_begin_omp_structured_block (parser);
25318
25319   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
25320
25321   cp_parser_end_omp_structured_block (parser, save);
25322   add_stmt (finish_omp_structured_block (sb));
25323
25324   return ret;
25325 }
25326
25327 /* OpenMP 2.5:
25328    # pragma omp master new-line
25329      structured-block  */
25330
25331 static tree
25332 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
25333 {
25334   cp_parser_require_pragma_eol (parser, pragma_tok);
25335   return c_finish_omp_master (input_location,
25336                               cp_parser_omp_structured_block (parser));
25337 }
25338
25339 /* OpenMP 2.5:
25340    # pragma omp ordered new-line
25341      structured-block  */
25342
25343 static tree
25344 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
25345 {
25346   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25347   cp_parser_require_pragma_eol (parser, pragma_tok);
25348   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
25349 }
25350
25351 /* OpenMP 2.5:
25352
25353    section-scope:
25354      { section-sequence }
25355
25356    section-sequence:
25357      section-directive[opt] structured-block
25358      section-sequence section-directive structured-block  */
25359
25360 static tree
25361 cp_parser_omp_sections_scope (cp_parser *parser)
25362 {
25363   tree stmt, substmt;
25364   bool error_suppress = false;
25365   cp_token *tok;
25366
25367   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
25368     return NULL_TREE;
25369
25370   stmt = push_stmt_list ();
25371
25372   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
25373     {
25374       unsigned save;
25375
25376       substmt = begin_omp_structured_block ();
25377       save = cp_parser_begin_omp_structured_block (parser);
25378
25379       while (1)
25380         {
25381           cp_parser_statement (parser, NULL_TREE, false, NULL);
25382
25383           tok = cp_lexer_peek_token (parser->lexer);
25384           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25385             break;
25386           if (tok->type == CPP_CLOSE_BRACE)
25387             break;
25388           if (tok->type == CPP_EOF)
25389             break;
25390         }
25391
25392       cp_parser_end_omp_structured_block (parser, save);
25393       substmt = finish_omp_structured_block (substmt);
25394       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25395       add_stmt (substmt);
25396     }
25397
25398   while (1)
25399     {
25400       tok = cp_lexer_peek_token (parser->lexer);
25401       if (tok->type == CPP_CLOSE_BRACE)
25402         break;
25403       if (tok->type == CPP_EOF)
25404         break;
25405
25406       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
25407         {
25408           cp_lexer_consume_token (parser->lexer);
25409           cp_parser_require_pragma_eol (parser, tok);
25410           error_suppress = false;
25411         }
25412       else if (!error_suppress)
25413         {
25414           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
25415           error_suppress = true;
25416         }
25417
25418       substmt = cp_parser_omp_structured_block (parser);
25419       substmt = build1 (OMP_SECTION, void_type_node, substmt);
25420       add_stmt (substmt);
25421     }
25422   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25423
25424   substmt = pop_stmt_list (stmt);
25425
25426   stmt = make_node (OMP_SECTIONS);
25427   TREE_TYPE (stmt) = void_type_node;
25428   OMP_SECTIONS_BODY (stmt) = substmt;
25429
25430   add_stmt (stmt);
25431   return stmt;
25432 }
25433
25434 /* OpenMP 2.5:
25435    # pragma omp sections sections-clause[optseq] newline
25436      sections-scope  */
25437
25438 #define OMP_SECTIONS_CLAUSE_MASK                        \
25439         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25440         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25441         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
25442         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25443         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25444
25445 static tree
25446 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
25447 {
25448   tree clauses, ret;
25449
25450   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
25451                                        "#pragma omp sections", pragma_tok);
25452
25453   ret = cp_parser_omp_sections_scope (parser);
25454   if (ret)
25455     OMP_SECTIONS_CLAUSES (ret) = clauses;
25456
25457   return ret;
25458 }
25459
25460 /* OpenMP 2.5:
25461    # pragma parallel parallel-clause new-line
25462    # pragma parallel for parallel-for-clause new-line
25463    # pragma parallel sections parallel-sections-clause new-line  */
25464
25465 #define OMP_PARALLEL_CLAUSE_MASK                        \
25466         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25467         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25468         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25469         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25470         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25471         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
25472         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
25473         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
25474
25475 static tree
25476 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
25477 {
25478   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
25479   const char *p_name = "#pragma omp parallel";
25480   tree stmt, clauses, par_clause, ws_clause, block;
25481   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
25482   unsigned int save;
25483   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
25484
25485   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25486     {
25487       cp_lexer_consume_token (parser->lexer);
25488       p_kind = PRAGMA_OMP_PARALLEL_FOR;
25489       p_name = "#pragma omp parallel for";
25490       mask |= OMP_FOR_CLAUSE_MASK;
25491       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25492     }
25493   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25494     {
25495       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25496       const char *p = IDENTIFIER_POINTER (id);
25497       if (strcmp (p, "sections") == 0)
25498         {
25499           cp_lexer_consume_token (parser->lexer);
25500           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25501           p_name = "#pragma omp parallel sections";
25502           mask |= OMP_SECTIONS_CLAUSE_MASK;
25503           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25504         }
25505     }
25506
25507   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25508   block = begin_omp_parallel ();
25509   save = cp_parser_begin_omp_structured_block (parser);
25510
25511   switch (p_kind)
25512     {
25513     case PRAGMA_OMP_PARALLEL:
25514       cp_parser_statement (parser, NULL_TREE, false, NULL);
25515       par_clause = clauses;
25516       break;
25517
25518     case PRAGMA_OMP_PARALLEL_FOR:
25519       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25520       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25521       break;
25522
25523     case PRAGMA_OMP_PARALLEL_SECTIONS:
25524       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25525       stmt = cp_parser_omp_sections_scope (parser);
25526       if (stmt)
25527         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25528       break;
25529
25530     default:
25531       gcc_unreachable ();
25532     }
25533
25534   cp_parser_end_omp_structured_block (parser, save);
25535   stmt = finish_omp_parallel (par_clause, block);
25536   if (p_kind != PRAGMA_OMP_PARALLEL)
25537     OMP_PARALLEL_COMBINED (stmt) = 1;
25538   return stmt;
25539 }
25540
25541 /* OpenMP 2.5:
25542    # pragma omp single single-clause[optseq] new-line
25543      structured-block  */
25544
25545 #define OMP_SINGLE_CLAUSE_MASK                          \
25546         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25547         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25548         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25549         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25550
25551 static tree
25552 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25553 {
25554   tree stmt = make_node (OMP_SINGLE);
25555   TREE_TYPE (stmt) = void_type_node;
25556
25557   OMP_SINGLE_CLAUSES (stmt)
25558     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25559                                  "#pragma omp single", pragma_tok);
25560   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25561
25562   return add_stmt (stmt);
25563 }
25564
25565 /* OpenMP 3.0:
25566    # pragma omp task task-clause[optseq] new-line
25567      structured-block  */
25568
25569 #define OMP_TASK_CLAUSE_MASK                            \
25570         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25571         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25572         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25573         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25574         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25575         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
25576         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
25577         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
25578
25579 static tree
25580 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25581 {
25582   tree clauses, block;
25583   unsigned int save;
25584
25585   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25586                                        "#pragma omp task", pragma_tok);
25587   block = begin_omp_task ();
25588   save = cp_parser_begin_omp_structured_block (parser);
25589   cp_parser_statement (parser, NULL_TREE, false, NULL);
25590   cp_parser_end_omp_structured_block (parser, save);
25591   return finish_omp_task (clauses, block);
25592 }
25593
25594 /* OpenMP 3.0:
25595    # pragma omp taskwait new-line  */
25596
25597 static void
25598 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25599 {
25600   cp_parser_require_pragma_eol (parser, pragma_tok);
25601   finish_omp_taskwait ();
25602 }
25603
25604 /* OpenMP 3.1:
25605    # pragma omp taskyield new-line  */
25606
25607 static void
25608 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
25609 {
25610   cp_parser_require_pragma_eol (parser, pragma_tok);
25611   finish_omp_taskyield ();
25612 }
25613
25614 /* OpenMP 2.5:
25615    # pragma omp threadprivate (variable-list) */
25616
25617 static void
25618 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25619 {
25620   tree vars;
25621
25622   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25623   cp_parser_require_pragma_eol (parser, pragma_tok);
25624
25625   finish_omp_threadprivate (vars);
25626 }
25627
25628 /* Main entry point to OpenMP statement pragmas.  */
25629
25630 static void
25631 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25632 {
25633   tree stmt;
25634
25635   switch (pragma_tok->pragma_kind)
25636     {
25637     case PRAGMA_OMP_ATOMIC:
25638       cp_parser_omp_atomic (parser, pragma_tok);
25639       return;
25640     case PRAGMA_OMP_CRITICAL:
25641       stmt = cp_parser_omp_critical (parser, pragma_tok);
25642       break;
25643     case PRAGMA_OMP_FOR:
25644       stmt = cp_parser_omp_for (parser, pragma_tok);
25645       break;
25646     case PRAGMA_OMP_MASTER:
25647       stmt = cp_parser_omp_master (parser, pragma_tok);
25648       break;
25649     case PRAGMA_OMP_ORDERED:
25650       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25651       break;
25652     case PRAGMA_OMP_PARALLEL:
25653       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25654       break;
25655     case PRAGMA_OMP_SECTIONS:
25656       stmt = cp_parser_omp_sections (parser, pragma_tok);
25657       break;
25658     case PRAGMA_OMP_SINGLE:
25659       stmt = cp_parser_omp_single (parser, pragma_tok);
25660       break;
25661     case PRAGMA_OMP_TASK:
25662       stmt = cp_parser_omp_task (parser, pragma_tok);
25663       break;
25664     default:
25665       gcc_unreachable ();
25666     }
25667
25668   if (stmt)
25669     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25670 }
25671 \f
25672 /* The parser.  */
25673
25674 static GTY (()) cp_parser *the_parser;
25675
25676 \f
25677 /* Special handling for the first token or line in the file.  The first
25678    thing in the file might be #pragma GCC pch_preprocess, which loads a
25679    PCH file, which is a GC collection point.  So we need to handle this
25680    first pragma without benefit of an existing lexer structure.
25681
25682    Always returns one token to the caller in *FIRST_TOKEN.  This is
25683    either the true first token of the file, or the first token after
25684    the initial pragma.  */
25685
25686 static void
25687 cp_parser_initial_pragma (cp_token *first_token)
25688 {
25689   tree name = NULL;
25690
25691   cp_lexer_get_preprocessor_token (NULL, first_token);
25692   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25693     return;
25694
25695   cp_lexer_get_preprocessor_token (NULL, first_token);
25696   if (first_token->type == CPP_STRING)
25697     {
25698       name = first_token->u.value;
25699
25700       cp_lexer_get_preprocessor_token (NULL, first_token);
25701       if (first_token->type != CPP_PRAGMA_EOL)
25702         error_at (first_token->location,
25703                   "junk at end of %<#pragma GCC pch_preprocess%>");
25704     }
25705   else
25706     error_at (first_token->location, "expected string literal");
25707
25708   /* Skip to the end of the pragma.  */
25709   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25710     cp_lexer_get_preprocessor_token (NULL, first_token);
25711
25712   /* Now actually load the PCH file.  */
25713   if (name)
25714     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25715
25716   /* Read one more token to return to our caller.  We have to do this
25717      after reading the PCH file in, since its pointers have to be
25718      live.  */
25719   cp_lexer_get_preprocessor_token (NULL, first_token);
25720 }
25721
25722 /* Normal parsing of a pragma token.  Here we can (and must) use the
25723    regular lexer.  */
25724
25725 static bool
25726 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25727 {
25728   cp_token *pragma_tok;
25729   unsigned int id;
25730
25731   pragma_tok = cp_lexer_consume_token (parser->lexer);
25732   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25733   parser->lexer->in_pragma = true;
25734
25735   id = pragma_tok->pragma_kind;
25736   switch (id)
25737     {
25738     case PRAGMA_GCC_PCH_PREPROCESS:
25739       error_at (pragma_tok->location,
25740                 "%<#pragma GCC pch_preprocess%> must be first");
25741       break;
25742
25743     case PRAGMA_OMP_BARRIER:
25744       switch (context)
25745         {
25746         case pragma_compound:
25747           cp_parser_omp_barrier (parser, pragma_tok);
25748           return false;
25749         case pragma_stmt:
25750           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25751                     "used in compound statements");
25752           break;
25753         default:
25754           goto bad_stmt;
25755         }
25756       break;
25757
25758     case PRAGMA_OMP_FLUSH:
25759       switch (context)
25760         {
25761         case pragma_compound:
25762           cp_parser_omp_flush (parser, pragma_tok);
25763           return false;
25764         case pragma_stmt:
25765           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25766                     "used in compound statements");
25767           break;
25768         default:
25769           goto bad_stmt;
25770         }
25771       break;
25772
25773     case PRAGMA_OMP_TASKWAIT:
25774       switch (context)
25775         {
25776         case pragma_compound:
25777           cp_parser_omp_taskwait (parser, pragma_tok);
25778           return false;
25779         case pragma_stmt:
25780           error_at (pragma_tok->location,
25781                     "%<#pragma omp taskwait%> may only be "
25782                     "used in compound statements");
25783           break;
25784         default:
25785           goto bad_stmt;
25786         }
25787       break;
25788
25789     case PRAGMA_OMP_TASKYIELD:
25790       switch (context)
25791         {
25792         case pragma_compound:
25793           cp_parser_omp_taskyield (parser, pragma_tok);
25794           return false;
25795         case pragma_stmt:
25796           error_at (pragma_tok->location,
25797                     "%<#pragma omp taskyield%> may only be "
25798                     "used in compound statements");
25799           break;
25800         default:
25801           goto bad_stmt;
25802         }
25803       break;
25804
25805     case PRAGMA_OMP_THREADPRIVATE:
25806       cp_parser_omp_threadprivate (parser, pragma_tok);
25807       return false;
25808
25809     case PRAGMA_OMP_ATOMIC:
25810     case PRAGMA_OMP_CRITICAL:
25811     case PRAGMA_OMP_FOR:
25812     case PRAGMA_OMP_MASTER:
25813     case PRAGMA_OMP_ORDERED:
25814     case PRAGMA_OMP_PARALLEL:
25815     case PRAGMA_OMP_SECTIONS:
25816     case PRAGMA_OMP_SINGLE:
25817     case PRAGMA_OMP_TASK:
25818       if (context == pragma_external)
25819         goto bad_stmt;
25820       cp_parser_omp_construct (parser, pragma_tok);
25821       return true;
25822
25823     case PRAGMA_OMP_SECTION:
25824       error_at (pragma_tok->location, 
25825                 "%<#pragma omp section%> may only be used in "
25826                 "%<#pragma omp sections%> construct");
25827       break;
25828
25829     default:
25830       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25831       c_invoke_pragma_handler (id);
25832       break;
25833
25834     bad_stmt:
25835       cp_parser_error (parser, "expected declaration specifiers");
25836       break;
25837     }
25838
25839   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25840   return false;
25841 }
25842
25843 /* The interface the pragma parsers have to the lexer.  */
25844
25845 enum cpp_ttype
25846 pragma_lex (tree *value)
25847 {
25848   cp_token *tok;
25849   enum cpp_ttype ret;
25850
25851   tok = cp_lexer_peek_token (the_parser->lexer);
25852
25853   ret = tok->type;
25854   *value = tok->u.value;
25855
25856   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25857     ret = CPP_EOF;
25858   else if (ret == CPP_STRING)
25859     *value = cp_parser_string_literal (the_parser, false, false);
25860   else
25861     {
25862       cp_lexer_consume_token (the_parser->lexer);
25863       if (ret == CPP_KEYWORD)
25864         ret = CPP_NAME;
25865     }
25866
25867   return ret;
25868 }
25869
25870 \f
25871 /* External interface.  */
25872
25873 /* Parse one entire translation unit.  */
25874
25875 void
25876 c_parse_file (void)
25877 {
25878   static bool already_called = false;
25879
25880   if (already_called)
25881     {
25882       sorry ("inter-module optimizations not implemented for C++");
25883       return;
25884     }
25885   already_called = true;
25886
25887   the_parser = cp_parser_new ();
25888   push_deferring_access_checks (flag_access_control
25889                                 ? dk_no_deferred : dk_no_check);
25890   cp_parser_translation_unit (the_parser);
25891   the_parser = NULL;
25892 }
25893
25894 #include "gt-cp-parser.h"