OSDN Git Service

PR c++/49691
[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 /* Return a pointer to the Nth token in the token stream.  If N is 1,
667    then this is precisely equivalent to cp_lexer_peek_token (except
668    that it is not inline).  One would like to disallow that case, but
669    there is one case (cp_parser_nth_token_starts_template_id) where
670    the caller passes a variable for N and it might be 1.  */
671
672 static cp_token *
673 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
674 {
675   cp_token *token;
676
677   /* N is 1-based, not zero-based.  */
678   gcc_assert (n > 0);
679
680   if (cp_lexer_debugging_p (lexer))
681     fprintf (cp_lexer_debug_stream,
682              "cp_lexer: peeking ahead %ld at token: ", (long)n);
683
684   --n;
685   token = lexer->next_token;
686   gcc_assert (!n || token != &eof_token);
687   while (n != 0)
688     {
689       ++token;
690       if (token == lexer->last_token)
691         {
692           token = &eof_token;
693           break;
694         }
695
696       if (!token->purged_p)
697         --n;
698     }
699
700   if (cp_lexer_debugging_p (lexer))
701     {
702       cp_lexer_print_token (cp_lexer_debug_stream, token);
703       putc ('\n', cp_lexer_debug_stream);
704     }
705
706   return token;
707 }
708
709 /* Return the next token, and advance the lexer's next_token pointer
710    to point to the next non-purged token.  */
711
712 static cp_token *
713 cp_lexer_consume_token (cp_lexer* lexer)
714 {
715   cp_token *token = lexer->next_token;
716
717   gcc_assert (token != &eof_token);
718   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
719
720   do
721     {
722       lexer->next_token++;
723       if (lexer->next_token == lexer->last_token)
724         {
725           lexer->next_token = &eof_token;
726           break;
727         }
728
729     }
730   while (lexer->next_token->purged_p);
731
732   cp_lexer_set_source_position_from_token (token);
733
734   /* Provide debugging output.  */
735   if (cp_lexer_debugging_p (lexer))
736     {
737       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
738       cp_lexer_print_token (cp_lexer_debug_stream, token);
739       putc ('\n', cp_lexer_debug_stream);
740     }
741
742   return token;
743 }
744
745 /* Permanently remove the next token from the token stream, and
746    advance the next_token pointer to refer to the next non-purged
747    token.  */
748
749 static void
750 cp_lexer_purge_token (cp_lexer *lexer)
751 {
752   cp_token *tok = lexer->next_token;
753
754   gcc_assert (tok != &eof_token);
755   tok->purged_p = true;
756   tok->location = UNKNOWN_LOCATION;
757   tok->u.value = NULL_TREE;
758   tok->keyword = RID_MAX;
759
760   do
761     {
762       tok++;
763       if (tok == lexer->last_token)
764         {
765           tok = &eof_token;
766           break;
767         }
768     }
769   while (tok->purged_p);
770   lexer->next_token = tok;
771 }
772
773 /* Permanently remove all tokens after TOK, up to, but not
774    including, the token that will be returned next by
775    cp_lexer_peek_token.  */
776
777 static void
778 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
779 {
780   cp_token *peek = lexer->next_token;
781
782   if (peek == &eof_token)
783     peek = lexer->last_token;
784
785   gcc_assert (tok < peek);
786
787   for ( tok += 1; tok != peek; tok += 1)
788     {
789       tok->purged_p = true;
790       tok->location = UNKNOWN_LOCATION;
791       tok->u.value = NULL_TREE;
792       tok->keyword = RID_MAX;
793     }
794 }
795
796 /* Begin saving tokens.  All tokens consumed after this point will be
797    preserved.  */
798
799 static void
800 cp_lexer_save_tokens (cp_lexer* lexer)
801 {
802   /* Provide debugging output.  */
803   if (cp_lexer_debugging_p (lexer))
804     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
805
806   VEC_safe_push (cp_token_position, heap,
807                  lexer->saved_tokens, lexer->next_token);
808 }
809
810 /* Commit to the portion of the token stream most recently saved.  */
811
812 static void
813 cp_lexer_commit_tokens (cp_lexer* lexer)
814 {
815   /* Provide debugging output.  */
816   if (cp_lexer_debugging_p (lexer))
817     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
818
819   VEC_pop (cp_token_position, lexer->saved_tokens);
820 }
821
822 /* Return all tokens saved since the last call to cp_lexer_save_tokens
823    to the token stream.  Stop saving tokens.  */
824
825 static void
826 cp_lexer_rollback_tokens (cp_lexer* lexer)
827 {
828   /* Provide debugging output.  */
829   if (cp_lexer_debugging_p (lexer))
830     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
831
832   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
833 }
834
835 /* Print a representation of the TOKEN on the STREAM.  */
836
837 #ifdef ENABLE_CHECKING
838
839 static void
840 cp_lexer_print_token (FILE * stream, cp_token *token)
841 {
842   /* We don't use cpp_type2name here because the parser defines
843      a few tokens of its own.  */
844   static const char *const token_names[] = {
845     /* cpplib-defined token types */
846 #define OP(e, s) #e,
847 #define TK(e, s) #e,
848     TTYPE_TABLE
849 #undef OP
850 #undef TK
851     /* C++ parser token types - see "Manifest constants", above.  */
852     "KEYWORD",
853     "TEMPLATE_ID",
854     "NESTED_NAME_SPECIFIER",
855   };
856
857   /* For some tokens, print the associated data.  */
858   switch (token->type)
859     {
860     case CPP_KEYWORD:
861       /* Some keywords have a value that is not an IDENTIFIER_NODE.
862          For example, `struct' is mapped to an INTEGER_CST.  */
863       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
864         break;
865       /* else fall through */
866     case CPP_NAME:
867       fputs (IDENTIFIER_POINTER (token->u.value), stream);
868       break;
869
870     case CPP_STRING:
871     case CPP_STRING16:
872     case CPP_STRING32:
873     case CPP_WSTRING:
874     case CPP_UTF8STRING:
875       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
876       break;
877
878     case CPP_NUMBER:
879       print_generic_expr (stream, token->u.value, 0);
880       break;
881
882     default:
883       /* If we have a name for the token, print it out.  Otherwise, we
884          simply give the numeric code.  */
885       if (token->type < ARRAY_SIZE(token_names))
886         fputs (token_names[token->type], stream);
887       else
888         fprintf (stream, "[%d]", token->type);
889       break;
890     }
891 }
892
893 /* Start emitting debugging information.  */
894
895 static void
896 cp_lexer_start_debugging (cp_lexer* lexer)
897 {
898   lexer->debugging_p = true;
899 }
900
901 /* Stop emitting debugging information.  */
902
903 static void
904 cp_lexer_stop_debugging (cp_lexer* lexer)
905 {
906   lexer->debugging_p = false;
907 }
908
909 #endif /* ENABLE_CHECKING */
910
911 /* Create a new cp_token_cache, representing a range of tokens.  */
912
913 static cp_token_cache *
914 cp_token_cache_new (cp_token *first, cp_token *last)
915 {
916   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
917   cache->first = first;
918   cache->last = last;
919   return cache;
920 }
921
922 \f
923 /* Decl-specifiers.  */
924
925 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
926
927 static void
928 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
929 {
930   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
931 }
932
933 /* Declarators.  */
934
935 /* Nothing other than the parser should be creating declarators;
936    declarators are a semi-syntactic representation of C++ entities.
937    Other parts of the front end that need to create entities (like
938    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
939
940 static cp_declarator *make_call_declarator
941   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
942 static cp_declarator *make_array_declarator
943   (cp_declarator *, tree);
944 static cp_declarator *make_pointer_declarator
945   (cp_cv_quals, cp_declarator *);
946 static cp_declarator *make_reference_declarator
947   (cp_cv_quals, cp_declarator *, bool);
948 static cp_parameter_declarator *make_parameter_declarator
949   (cp_decl_specifier_seq *, cp_declarator *, tree);
950 static cp_declarator *make_ptrmem_declarator
951   (cp_cv_quals, tree, cp_declarator *);
952
953 /* An erroneous declarator.  */
954 static cp_declarator *cp_error_declarator;
955
956 /* The obstack on which declarators and related data structures are
957    allocated.  */
958 static struct obstack declarator_obstack;
959
960 /* Alloc BYTES from the declarator memory pool.  */
961
962 static inline void *
963 alloc_declarator (size_t bytes)
964 {
965   return obstack_alloc (&declarator_obstack, bytes);
966 }
967
968 /* Allocate a declarator of the indicated KIND.  Clear fields that are
969    common to all declarators.  */
970
971 static cp_declarator *
972 make_declarator (cp_declarator_kind kind)
973 {
974   cp_declarator *declarator;
975
976   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
977   declarator->kind = kind;
978   declarator->attributes = NULL_TREE;
979   declarator->declarator = NULL;
980   declarator->parameter_pack_p = false;
981   declarator->id_loc = UNKNOWN_LOCATION;
982
983   return declarator;
984 }
985
986 /* Make a declarator for a generalized identifier.  If
987    QUALIFYING_SCOPE is non-NULL, the identifier is
988    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
989    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
990    is, if any.   */
991
992 static cp_declarator *
993 make_id_declarator (tree qualifying_scope, tree unqualified_name,
994                     special_function_kind sfk)
995 {
996   cp_declarator *declarator;
997
998   /* It is valid to write:
999
1000        class C { void f(); };
1001        typedef C D;
1002        void D::f();
1003
1004      The standard is not clear about whether `typedef const C D' is
1005      legal; as of 2002-09-15 the committee is considering that
1006      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1007      well.  */
1008   if (qualifying_scope && TYPE_P (qualifying_scope))
1009     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1010
1011   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1012               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1013               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1014
1015   declarator = make_declarator (cdk_id);
1016   declarator->u.id.qualifying_scope = qualifying_scope;
1017   declarator->u.id.unqualified_name = unqualified_name;
1018   declarator->u.id.sfk = sfk;
1019   
1020   return declarator;
1021 }
1022
1023 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1024    of modifiers such as const or volatile to apply to the pointer
1025    type, represented as identifiers.  */
1026
1027 cp_declarator *
1028 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1029 {
1030   cp_declarator *declarator;
1031
1032   declarator = make_declarator (cdk_pointer);
1033   declarator->declarator = target;
1034   declarator->u.pointer.qualifiers = cv_qualifiers;
1035   declarator->u.pointer.class_type = NULL_TREE;
1036   if (target)
1037     {
1038       declarator->id_loc = target->id_loc;
1039       declarator->parameter_pack_p = target->parameter_pack_p;
1040       target->parameter_pack_p = false;
1041     }
1042   else
1043     declarator->parameter_pack_p = false;
1044
1045   return declarator;
1046 }
1047
1048 /* Like make_pointer_declarator -- but for references.  */
1049
1050 cp_declarator *
1051 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1052                            bool rvalue_ref)
1053 {
1054   cp_declarator *declarator;
1055
1056   declarator = make_declarator (cdk_reference);
1057   declarator->declarator = target;
1058   declarator->u.reference.qualifiers = cv_qualifiers;
1059   declarator->u.reference.rvalue_ref = rvalue_ref;
1060   if (target)
1061     {
1062       declarator->id_loc = target->id_loc;
1063       declarator->parameter_pack_p = target->parameter_pack_p;
1064       target->parameter_pack_p = false;
1065     }
1066   else
1067     declarator->parameter_pack_p = false;
1068
1069   return declarator;
1070 }
1071
1072 /* Like make_pointer_declarator -- but for a pointer to a non-static
1073    member of CLASS_TYPE.  */
1074
1075 cp_declarator *
1076 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1077                         cp_declarator *pointee)
1078 {
1079   cp_declarator *declarator;
1080
1081   declarator = make_declarator (cdk_ptrmem);
1082   declarator->declarator = pointee;
1083   declarator->u.pointer.qualifiers = cv_qualifiers;
1084   declarator->u.pointer.class_type = class_type;
1085
1086   if (pointee)
1087     {
1088       declarator->parameter_pack_p = pointee->parameter_pack_p;
1089       pointee->parameter_pack_p = false;
1090     }
1091   else
1092     declarator->parameter_pack_p = false;
1093
1094   return declarator;
1095 }
1096
1097 /* Make a declarator for the function given by TARGET, with the
1098    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1099    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1100    indicates what exceptions can be thrown.  */
1101
1102 cp_declarator *
1103 make_call_declarator (cp_declarator *target,
1104                       tree parms,
1105                       cp_cv_quals cv_qualifiers,
1106                       cp_virt_specifiers virt_specifiers,
1107                       tree exception_specification,
1108                       tree late_return_type)
1109 {
1110   cp_declarator *declarator;
1111
1112   declarator = make_declarator (cdk_function);
1113   declarator->declarator = target;
1114   declarator->u.function.parameters = parms;
1115   declarator->u.function.qualifiers = cv_qualifiers;
1116   declarator->u.function.virt_specifiers = virt_specifiers;
1117   declarator->u.function.exception_specification = exception_specification;
1118   declarator->u.function.late_return_type = late_return_type;
1119   if (target)
1120     {
1121       declarator->id_loc = target->id_loc;
1122       declarator->parameter_pack_p = target->parameter_pack_p;
1123       target->parameter_pack_p = false;
1124     }
1125   else
1126     declarator->parameter_pack_p = false;
1127
1128   return declarator;
1129 }
1130
1131 /* Make a declarator for an array of BOUNDS elements, each of which is
1132    defined by ELEMENT.  */
1133
1134 cp_declarator *
1135 make_array_declarator (cp_declarator *element, tree bounds)
1136 {
1137   cp_declarator *declarator;
1138
1139   declarator = make_declarator (cdk_array);
1140   declarator->declarator = element;
1141   declarator->u.array.bounds = bounds;
1142   if (element)
1143     {
1144       declarator->id_loc = element->id_loc;
1145       declarator->parameter_pack_p = element->parameter_pack_p;
1146       element->parameter_pack_p = false;
1147     }
1148   else
1149     declarator->parameter_pack_p = false;
1150
1151   return declarator;
1152 }
1153
1154 /* Determine whether the declarator we've seen so far can be a
1155    parameter pack, when followed by an ellipsis.  */
1156 static bool 
1157 declarator_can_be_parameter_pack (cp_declarator *declarator)
1158 {
1159   /* Search for a declarator name, or any other declarator that goes
1160      after the point where the ellipsis could appear in a parameter
1161      pack. If we find any of these, then this declarator can not be
1162      made into a parameter pack.  */
1163   bool found = false;
1164   while (declarator && !found)
1165     {
1166       switch ((int)declarator->kind)
1167         {
1168         case cdk_id:
1169         case cdk_array:
1170           found = true;
1171           break;
1172
1173         case cdk_error:
1174           return true;
1175
1176         default:
1177           declarator = declarator->declarator;
1178           break;
1179         }
1180     }
1181
1182   return !found;
1183 }
1184
1185 cp_parameter_declarator *no_parameters;
1186
1187 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1188    DECLARATOR and DEFAULT_ARGUMENT.  */
1189
1190 cp_parameter_declarator *
1191 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1192                            cp_declarator *declarator,
1193                            tree default_argument)
1194 {
1195   cp_parameter_declarator *parameter;
1196
1197   parameter = ((cp_parameter_declarator *)
1198                alloc_declarator (sizeof (cp_parameter_declarator)));
1199   parameter->next = NULL;
1200   if (decl_specifiers)
1201     parameter->decl_specifiers = *decl_specifiers;
1202   else
1203     clear_decl_specs (&parameter->decl_specifiers);
1204   parameter->declarator = declarator;
1205   parameter->default_argument = default_argument;
1206   parameter->ellipsis_p = false;
1207
1208   return parameter;
1209 }
1210
1211 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1212
1213 static bool
1214 function_declarator_p (const cp_declarator *declarator)
1215 {
1216   while (declarator)
1217     {
1218       if (declarator->kind == cdk_function
1219           && declarator->declarator->kind == cdk_id)
1220         return true;
1221       if (declarator->kind == cdk_id
1222           || declarator->kind == cdk_error)
1223         return false;
1224       declarator = declarator->declarator;
1225     }
1226   return false;
1227 }
1228  
1229 /* The parser.  */
1230
1231 /* Overview
1232    --------
1233
1234    A cp_parser parses the token stream as specified by the C++
1235    grammar.  Its job is purely parsing, not semantic analysis.  For
1236    example, the parser breaks the token stream into declarators,
1237    expressions, statements, and other similar syntactic constructs.
1238    It does not check that the types of the expressions on either side
1239    of an assignment-statement are compatible, or that a function is
1240    not declared with a parameter of type `void'.
1241
1242    The parser invokes routines elsewhere in the compiler to perform
1243    semantic analysis and to build up the abstract syntax tree for the
1244    code processed.
1245
1246    The parser (and the template instantiation code, which is, in a
1247    way, a close relative of parsing) are the only parts of the
1248    compiler that should be calling push_scope and pop_scope, or
1249    related functions.  The parser (and template instantiation code)
1250    keeps track of what scope is presently active; everything else
1251    should simply honor that.  (The code that generates static
1252    initializers may also need to set the scope, in order to check
1253    access control correctly when emitting the initializers.)
1254
1255    Methodology
1256    -----------
1257
1258    The parser is of the standard recursive-descent variety.  Upcoming
1259    tokens in the token stream are examined in order to determine which
1260    production to use when parsing a non-terminal.  Some C++ constructs
1261    require arbitrary look ahead to disambiguate.  For example, it is
1262    impossible, in the general case, to tell whether a statement is an
1263    expression or declaration without scanning the entire statement.
1264    Therefore, the parser is capable of "parsing tentatively."  When the
1265    parser is not sure what construct comes next, it enters this mode.
1266    Then, while we attempt to parse the construct, the parser queues up
1267    error messages, rather than issuing them immediately, and saves the
1268    tokens it consumes.  If the construct is parsed successfully, the
1269    parser "commits", i.e., it issues any queued error messages and
1270    the tokens that were being preserved are permanently discarded.
1271    If, however, the construct is not parsed successfully, the parser
1272    rolls back its state completely so that it can resume parsing using
1273    a different alternative.
1274
1275    Future Improvements
1276    -------------------
1277
1278    The performance of the parser could probably be improved substantially.
1279    We could often eliminate the need to parse tentatively by looking ahead
1280    a little bit.  In some places, this approach might not entirely eliminate
1281    the need to parse tentatively, but it might still speed up the average
1282    case.  */
1283
1284 /* Flags that are passed to some parsing functions.  These values can
1285    be bitwise-ored together.  */
1286
1287 enum
1288 {
1289   /* No flags.  */
1290   CP_PARSER_FLAGS_NONE = 0x0,
1291   /* The construct is optional.  If it is not present, then no error
1292      should be issued.  */
1293   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1294   /* When parsing a type-specifier, treat user-defined type-names
1295      as non-type identifiers.  */
1296   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1297   /* When parsing a type-specifier, do not try to parse a class-specifier
1298      or enum-specifier.  */
1299   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1300   /* When parsing a decl-specifier-seq, only allow type-specifier or
1301      constexpr.  */
1302   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1303 };
1304
1305 /* This type is used for parameters and variables which hold
1306    combinations of the above flags.  */
1307 typedef int cp_parser_flags;
1308
1309 /* The different kinds of declarators we want to parse.  */
1310
1311 typedef enum cp_parser_declarator_kind
1312 {
1313   /* We want an abstract declarator.  */
1314   CP_PARSER_DECLARATOR_ABSTRACT,
1315   /* We want a named declarator.  */
1316   CP_PARSER_DECLARATOR_NAMED,
1317   /* We don't mind, but the name must be an unqualified-id.  */
1318   CP_PARSER_DECLARATOR_EITHER
1319 } cp_parser_declarator_kind;
1320
1321 /* The precedence values used to parse binary expressions.  The minimum value
1322    of PREC must be 1, because zero is reserved to quickly discriminate
1323    binary operators from other tokens.  */
1324
1325 enum cp_parser_prec
1326 {
1327   PREC_NOT_OPERATOR,
1328   PREC_LOGICAL_OR_EXPRESSION,
1329   PREC_LOGICAL_AND_EXPRESSION,
1330   PREC_INCLUSIVE_OR_EXPRESSION,
1331   PREC_EXCLUSIVE_OR_EXPRESSION,
1332   PREC_AND_EXPRESSION,
1333   PREC_EQUALITY_EXPRESSION,
1334   PREC_RELATIONAL_EXPRESSION,
1335   PREC_SHIFT_EXPRESSION,
1336   PREC_ADDITIVE_EXPRESSION,
1337   PREC_MULTIPLICATIVE_EXPRESSION,
1338   PREC_PM_EXPRESSION,
1339   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1340 };
1341
1342 /* A mapping from a token type to a corresponding tree node type, with a
1343    precedence value.  */
1344
1345 typedef struct cp_parser_binary_operations_map_node
1346 {
1347   /* The token type.  */
1348   enum cpp_ttype token_type;
1349   /* The corresponding tree code.  */
1350   enum tree_code tree_type;
1351   /* The precedence of this operator.  */
1352   enum cp_parser_prec prec;
1353 } cp_parser_binary_operations_map_node;
1354
1355 typedef struct cp_parser_expression_stack_entry
1356 {
1357   /* Left hand side of the binary operation we are currently
1358      parsing.  */
1359   tree lhs;
1360   /* Original tree code for left hand side, if it was a binary
1361      expression itself (used for -Wparentheses).  */
1362   enum tree_code lhs_type;
1363   /* Tree code for the binary operation we are parsing.  */
1364   enum tree_code tree_type;
1365   /* Precedence of the binary operation we are parsing.  */
1366   enum cp_parser_prec prec;
1367 } cp_parser_expression_stack_entry;
1368
1369 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1370    entries because precedence levels on the stack are monotonically
1371    increasing.  */
1372 typedef struct cp_parser_expression_stack_entry
1373   cp_parser_expression_stack[NUM_PREC_VALUES];
1374
1375 /* Prototypes.  */
1376
1377 /* Constructors and destructors.  */
1378
1379 static cp_parser_context *cp_parser_context_new
1380   (cp_parser_context *);
1381
1382 /* Class variables.  */
1383
1384 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1385
1386 /* The operator-precedence table used by cp_parser_binary_expression.
1387    Transformed into an associative array (binops_by_token) by
1388    cp_parser_new.  */
1389
1390 static const cp_parser_binary_operations_map_node binops[] = {
1391   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1392   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1393
1394   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1395   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1396   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1397
1398   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1399   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1400
1401   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1402   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1403
1404   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1405   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1406   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1407   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1408
1409   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1410   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1411
1412   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1413
1414   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1415
1416   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1417
1418   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1419
1420   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1421 };
1422
1423 /* The same as binops, but initialized by cp_parser_new so that
1424    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1425    for speed.  */
1426 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1427
1428 /* Constructors and destructors.  */
1429
1430 /* Construct a new context.  The context below this one on the stack
1431    is given by NEXT.  */
1432
1433 static cp_parser_context *
1434 cp_parser_context_new (cp_parser_context* next)
1435 {
1436   cp_parser_context *context;
1437
1438   /* Allocate the storage.  */
1439   if (cp_parser_context_free_list != NULL)
1440     {
1441       /* Pull the first entry from the free list.  */
1442       context = cp_parser_context_free_list;
1443       cp_parser_context_free_list = context->next;
1444       memset (context, 0, sizeof (*context));
1445     }
1446   else
1447     context = ggc_alloc_cleared_cp_parser_context ();
1448
1449   /* No errors have occurred yet in this context.  */
1450   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1451   /* If this is not the bottommost context, copy information that we
1452      need from the previous context.  */
1453   if (next)
1454     {
1455       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1456          expression, then we are parsing one in this context, too.  */
1457       context->object_type = next->object_type;
1458       /* Thread the stack.  */
1459       context->next = next;
1460     }
1461
1462   return context;
1463 }
1464
1465 /* Managing the unparsed function queues.  */
1466
1467 #define unparsed_funs_with_default_args \
1468   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1469 #define unparsed_funs_with_definitions \
1470   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1471
1472 static void
1473 push_unparsed_function_queues (cp_parser *parser)
1474 {
1475   VEC_safe_push (cp_unparsed_functions_entry, gc,
1476                  parser->unparsed_queues, NULL);
1477   unparsed_funs_with_default_args = NULL;
1478   unparsed_funs_with_definitions = make_tree_vector ();
1479 }
1480
1481 static void
1482 pop_unparsed_function_queues (cp_parser *parser)
1483 {
1484   release_tree_vector (unparsed_funs_with_definitions);
1485   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1486 }
1487
1488 /* Prototypes.  */
1489
1490 /* Constructors and destructors.  */
1491
1492 static cp_parser *cp_parser_new
1493   (void);
1494
1495 /* Routines to parse various constructs.
1496
1497    Those that return `tree' will return the error_mark_node (rather
1498    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1499    Sometimes, they will return an ordinary node if error-recovery was
1500    attempted, even though a parse error occurred.  So, to check
1501    whether or not a parse error occurred, you should always use
1502    cp_parser_error_occurred.  If the construct is optional (indicated
1503    either by an `_opt' in the name of the function that does the
1504    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1505    the construct is not present.  */
1506
1507 /* Lexical conventions [gram.lex]  */
1508
1509 static tree cp_parser_identifier
1510   (cp_parser *);
1511 static tree cp_parser_string_literal
1512   (cp_parser *, bool, bool);
1513
1514 /* Basic concepts [gram.basic]  */
1515
1516 static bool cp_parser_translation_unit
1517   (cp_parser *);
1518
1519 /* Expressions [gram.expr]  */
1520
1521 static tree cp_parser_primary_expression
1522   (cp_parser *, bool, bool, bool, cp_id_kind *);
1523 static tree cp_parser_id_expression
1524   (cp_parser *, bool, bool, bool *, bool, bool);
1525 static tree cp_parser_unqualified_id
1526   (cp_parser *, bool, bool, bool, bool);
1527 static tree cp_parser_nested_name_specifier_opt
1528   (cp_parser *, bool, bool, bool, bool);
1529 static tree cp_parser_nested_name_specifier
1530   (cp_parser *, bool, bool, bool, bool);
1531 static tree cp_parser_qualifying_entity
1532   (cp_parser *, bool, bool, bool, bool, bool);
1533 static tree cp_parser_postfix_expression
1534   (cp_parser *, bool, bool, bool, cp_id_kind *);
1535 static tree cp_parser_postfix_open_square_expression
1536   (cp_parser *, tree, bool);
1537 static tree cp_parser_postfix_dot_deref_expression
1538   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1539 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1540   (cp_parser *, int, bool, bool, bool *);
1541 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1542 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1543 static void cp_parser_pseudo_destructor_name
1544   (cp_parser *, tree *, tree *);
1545 static tree cp_parser_unary_expression
1546   (cp_parser *, bool, bool, cp_id_kind *);
1547 static enum tree_code cp_parser_unary_operator
1548   (cp_token *);
1549 static tree cp_parser_new_expression
1550   (cp_parser *);
1551 static VEC(tree,gc) *cp_parser_new_placement
1552   (cp_parser *);
1553 static tree cp_parser_new_type_id
1554   (cp_parser *, tree *);
1555 static cp_declarator *cp_parser_new_declarator_opt
1556   (cp_parser *);
1557 static cp_declarator *cp_parser_direct_new_declarator
1558   (cp_parser *);
1559 static VEC(tree,gc) *cp_parser_new_initializer
1560   (cp_parser *);
1561 static tree cp_parser_delete_expression
1562   (cp_parser *);
1563 static tree cp_parser_cast_expression
1564   (cp_parser *, bool, bool, cp_id_kind *);
1565 static tree cp_parser_binary_expression
1566   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1567 static tree cp_parser_question_colon_clause
1568   (cp_parser *, tree);
1569 static tree cp_parser_assignment_expression
1570   (cp_parser *, bool, cp_id_kind *);
1571 static enum tree_code cp_parser_assignment_operator_opt
1572   (cp_parser *);
1573 static tree cp_parser_expression
1574   (cp_parser *, bool, cp_id_kind *);
1575 static tree cp_parser_constant_expression
1576   (cp_parser *, bool, bool *);
1577 static tree cp_parser_builtin_offsetof
1578   (cp_parser *);
1579 static tree cp_parser_lambda_expression
1580   (cp_parser *);
1581 static void cp_parser_lambda_introducer
1582   (cp_parser *, tree);
1583 static bool cp_parser_lambda_declarator_opt
1584   (cp_parser *, tree);
1585 static void cp_parser_lambda_body
1586   (cp_parser *, tree);
1587
1588 /* Statements [gram.stmt.stmt]  */
1589
1590 static void cp_parser_statement
1591   (cp_parser *, tree, bool, bool *);
1592 static void cp_parser_label_for_labeled_statement
1593   (cp_parser *);
1594 static tree cp_parser_expression_statement
1595   (cp_parser *, tree);
1596 static tree cp_parser_compound_statement
1597   (cp_parser *, tree, bool, bool);
1598 static void cp_parser_statement_seq_opt
1599   (cp_parser *, tree);
1600 static tree cp_parser_selection_statement
1601   (cp_parser *, bool *);
1602 static tree cp_parser_condition
1603   (cp_parser *);
1604 static tree cp_parser_iteration_statement
1605   (cp_parser *);
1606 static bool cp_parser_for_init_statement
1607   (cp_parser *, tree *decl);
1608 static tree cp_parser_for
1609   (cp_parser *);
1610 static tree cp_parser_c_for
1611   (cp_parser *, tree, tree);
1612 static tree cp_parser_range_for
1613   (cp_parser *, tree, tree, tree);
1614 static tree cp_parser_perform_range_for_lookup
1615   (tree, tree *, tree *);
1616 static tree cp_parser_range_for_member_function
1617   (tree, tree);
1618 static tree cp_parser_jump_statement
1619   (cp_parser *);
1620 static void cp_parser_declaration_statement
1621   (cp_parser *);
1622
1623 static tree cp_parser_implicitly_scoped_statement
1624   (cp_parser *, bool *);
1625 static void cp_parser_already_scoped_statement
1626   (cp_parser *);
1627
1628 /* Declarations [gram.dcl.dcl] */
1629
1630 static void cp_parser_declaration_seq_opt
1631   (cp_parser *);
1632 static void cp_parser_declaration
1633   (cp_parser *);
1634 static void cp_parser_block_declaration
1635   (cp_parser *, bool);
1636 static void cp_parser_simple_declaration
1637   (cp_parser *, bool, tree *);
1638 static void cp_parser_decl_specifier_seq
1639   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1640 static tree cp_parser_storage_class_specifier_opt
1641   (cp_parser *);
1642 static tree cp_parser_function_specifier_opt
1643   (cp_parser *, cp_decl_specifier_seq *);
1644 static tree cp_parser_type_specifier
1645   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1646    int *, bool *);
1647 static tree cp_parser_simple_type_specifier
1648   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1649 static tree cp_parser_type_name
1650   (cp_parser *);
1651 static tree cp_parser_nonclass_name 
1652   (cp_parser* parser);
1653 static tree cp_parser_elaborated_type_specifier
1654   (cp_parser *, bool, bool);
1655 static tree cp_parser_enum_specifier
1656   (cp_parser *);
1657 static void cp_parser_enumerator_list
1658   (cp_parser *, tree);
1659 static void cp_parser_enumerator_definition
1660   (cp_parser *, tree);
1661 static tree cp_parser_namespace_name
1662   (cp_parser *);
1663 static void cp_parser_namespace_definition
1664   (cp_parser *);
1665 static void cp_parser_namespace_body
1666   (cp_parser *);
1667 static tree cp_parser_qualified_namespace_specifier
1668   (cp_parser *);
1669 static void cp_parser_namespace_alias_definition
1670   (cp_parser *);
1671 static bool cp_parser_using_declaration
1672   (cp_parser *, bool);
1673 static void cp_parser_using_directive
1674   (cp_parser *);
1675 static void cp_parser_asm_definition
1676   (cp_parser *);
1677 static void cp_parser_linkage_specification
1678   (cp_parser *);
1679 static void cp_parser_static_assert
1680   (cp_parser *, bool);
1681 static tree cp_parser_decltype
1682   (cp_parser *);
1683
1684 /* Declarators [gram.dcl.decl] */
1685
1686 static tree cp_parser_init_declarator
1687   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1688 static cp_declarator *cp_parser_declarator
1689   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1690 static cp_declarator *cp_parser_direct_declarator
1691   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1692 static enum tree_code cp_parser_ptr_operator
1693   (cp_parser *, tree *, cp_cv_quals *);
1694 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1695   (cp_parser *);
1696 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1697   (cp_parser *);
1698 static tree cp_parser_late_return_type_opt
1699   (cp_parser *, cp_cv_quals);
1700 static tree cp_parser_declarator_id
1701   (cp_parser *, bool);
1702 static tree cp_parser_type_id
1703   (cp_parser *);
1704 static tree cp_parser_template_type_arg
1705   (cp_parser *);
1706 static tree cp_parser_trailing_type_id (cp_parser *);
1707 static tree cp_parser_type_id_1
1708   (cp_parser *, bool, bool);
1709 static void cp_parser_type_specifier_seq
1710   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1711 static tree cp_parser_parameter_declaration_clause
1712   (cp_parser *);
1713 static tree cp_parser_parameter_declaration_list
1714   (cp_parser *, bool *);
1715 static cp_parameter_declarator *cp_parser_parameter_declaration
1716   (cp_parser *, bool, bool *);
1717 static tree cp_parser_default_argument 
1718   (cp_parser *, bool);
1719 static void cp_parser_function_body
1720   (cp_parser *);
1721 static tree cp_parser_initializer
1722   (cp_parser *, bool *, bool *);
1723 static tree cp_parser_initializer_clause
1724   (cp_parser *, bool *);
1725 static tree cp_parser_braced_list
1726   (cp_parser*, bool*);
1727 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1728   (cp_parser *, bool *);
1729
1730 static bool cp_parser_ctor_initializer_opt_and_function_body
1731   (cp_parser *);
1732
1733 /* Classes [gram.class] */
1734
1735 static tree cp_parser_class_name
1736   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
1737 static tree cp_parser_class_specifier
1738   (cp_parser *);
1739 static tree cp_parser_class_head
1740   (cp_parser *, bool *, tree *, tree *);
1741 static enum tag_types cp_parser_class_key
1742   (cp_parser *);
1743 static void cp_parser_member_specification_opt
1744   (cp_parser *);
1745 static void cp_parser_member_declaration
1746   (cp_parser *);
1747 static tree cp_parser_pure_specifier
1748   (cp_parser *);
1749 static tree cp_parser_constant_initializer
1750   (cp_parser *);
1751
1752 /* Derived classes [gram.class.derived] */
1753
1754 static tree cp_parser_base_clause
1755   (cp_parser *);
1756 static tree cp_parser_base_specifier
1757   (cp_parser *);
1758
1759 /* Special member functions [gram.special] */
1760
1761 static tree cp_parser_conversion_function_id
1762   (cp_parser *);
1763 static tree cp_parser_conversion_type_id
1764   (cp_parser *);
1765 static cp_declarator *cp_parser_conversion_declarator_opt
1766   (cp_parser *);
1767 static bool cp_parser_ctor_initializer_opt
1768   (cp_parser *);
1769 static void cp_parser_mem_initializer_list
1770   (cp_parser *);
1771 static tree cp_parser_mem_initializer
1772   (cp_parser *);
1773 static tree cp_parser_mem_initializer_id
1774   (cp_parser *);
1775
1776 /* Overloading [gram.over] */
1777
1778 static tree cp_parser_operator_function_id
1779   (cp_parser *);
1780 static tree cp_parser_operator
1781   (cp_parser *);
1782
1783 /* Templates [gram.temp] */
1784
1785 static void cp_parser_template_declaration
1786   (cp_parser *, bool);
1787 static tree cp_parser_template_parameter_list
1788   (cp_parser *);
1789 static tree cp_parser_template_parameter
1790   (cp_parser *, bool *, bool *);
1791 static tree cp_parser_type_parameter
1792   (cp_parser *, bool *);
1793 static tree cp_parser_template_id
1794   (cp_parser *, bool, bool, bool);
1795 static tree cp_parser_template_name
1796   (cp_parser *, bool, bool, bool, bool *);
1797 static tree cp_parser_template_argument_list
1798   (cp_parser *);
1799 static tree cp_parser_template_argument
1800   (cp_parser *);
1801 static void cp_parser_explicit_instantiation
1802   (cp_parser *);
1803 static void cp_parser_explicit_specialization
1804   (cp_parser *);
1805
1806 /* Exception handling [gram.exception] */
1807
1808 static tree cp_parser_try_block
1809   (cp_parser *);
1810 static bool cp_parser_function_try_block
1811   (cp_parser *);
1812 static void cp_parser_handler_seq
1813   (cp_parser *);
1814 static void cp_parser_handler
1815   (cp_parser *);
1816 static tree cp_parser_exception_declaration
1817   (cp_parser *);
1818 static tree cp_parser_throw_expression
1819   (cp_parser *);
1820 static tree cp_parser_exception_specification_opt
1821   (cp_parser *);
1822 static tree cp_parser_type_id_list
1823   (cp_parser *);
1824
1825 /* GNU Extensions */
1826
1827 static tree cp_parser_asm_specification_opt
1828   (cp_parser *);
1829 static tree cp_parser_asm_operand_list
1830   (cp_parser *);
1831 static tree cp_parser_asm_clobber_list
1832   (cp_parser *);
1833 static tree cp_parser_asm_label_list
1834   (cp_parser *);
1835 static tree cp_parser_attributes_opt
1836   (cp_parser *);
1837 static tree cp_parser_attribute_list
1838   (cp_parser *);
1839 static bool cp_parser_extension_opt
1840   (cp_parser *, int *);
1841 static void cp_parser_label_declaration
1842   (cp_parser *);
1843
1844 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
1845 static bool cp_parser_pragma
1846   (cp_parser *, enum pragma_context);
1847
1848 /* Objective-C++ Productions */
1849
1850 static tree cp_parser_objc_message_receiver
1851   (cp_parser *);
1852 static tree cp_parser_objc_message_args
1853   (cp_parser *);
1854 static tree cp_parser_objc_message_expression
1855   (cp_parser *);
1856 static tree cp_parser_objc_encode_expression
1857   (cp_parser *);
1858 static tree cp_parser_objc_defs_expression
1859   (cp_parser *);
1860 static tree cp_parser_objc_protocol_expression
1861   (cp_parser *);
1862 static tree cp_parser_objc_selector_expression
1863   (cp_parser *);
1864 static tree cp_parser_objc_expression
1865   (cp_parser *);
1866 static bool cp_parser_objc_selector_p
1867   (enum cpp_ttype);
1868 static tree cp_parser_objc_selector
1869   (cp_parser *);
1870 static tree cp_parser_objc_protocol_refs_opt
1871   (cp_parser *);
1872 static void cp_parser_objc_declaration
1873   (cp_parser *, tree);
1874 static tree cp_parser_objc_statement
1875   (cp_parser *);
1876 static bool cp_parser_objc_valid_prefix_attributes
1877   (cp_parser *, tree *);
1878 static void cp_parser_objc_at_property_declaration 
1879   (cp_parser *) ;
1880 static void cp_parser_objc_at_synthesize_declaration 
1881   (cp_parser *) ;
1882 static void cp_parser_objc_at_dynamic_declaration
1883   (cp_parser *) ;
1884 static tree cp_parser_objc_struct_declaration
1885   (cp_parser *) ;
1886
1887 /* Utility Routines */
1888
1889 static tree cp_parser_lookup_name
1890   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
1891 static tree cp_parser_lookup_name_simple
1892   (cp_parser *, tree, location_t);
1893 static tree cp_parser_maybe_treat_template_as_class
1894   (tree, bool);
1895 static bool cp_parser_check_declarator_template_parameters
1896   (cp_parser *, cp_declarator *, location_t);
1897 static bool cp_parser_check_template_parameters
1898   (cp_parser *, unsigned, location_t, cp_declarator *);
1899 static tree cp_parser_simple_cast_expression
1900   (cp_parser *);
1901 static tree cp_parser_global_scope_opt
1902   (cp_parser *, bool);
1903 static bool cp_parser_constructor_declarator_p
1904   (cp_parser *, bool);
1905 static tree cp_parser_function_definition_from_specifiers_and_declarator
1906   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
1907 static tree cp_parser_function_definition_after_declarator
1908   (cp_parser *, bool);
1909 static void cp_parser_template_declaration_after_export
1910   (cp_parser *, bool);
1911 static void cp_parser_perform_template_parameter_access_checks
1912   (VEC (deferred_access_check,gc)*);
1913 static tree cp_parser_single_declaration
1914   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
1915 static tree cp_parser_functional_cast
1916   (cp_parser *, tree);
1917 static tree cp_parser_save_member_function_body
1918   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
1919 static tree cp_parser_enclosed_template_argument_list
1920   (cp_parser *);
1921 static void cp_parser_save_default_args
1922   (cp_parser *, tree);
1923 static void cp_parser_late_parsing_for_member
1924   (cp_parser *, tree);
1925 static void cp_parser_late_parsing_default_args
1926   (cp_parser *, tree);
1927 static tree cp_parser_sizeof_operand
1928   (cp_parser *, enum rid);
1929 static tree cp_parser_trait_expr
1930   (cp_parser *, enum rid);
1931 static bool cp_parser_declares_only_class_p
1932   (cp_parser *);
1933 static void cp_parser_set_storage_class
1934   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
1935 static void cp_parser_set_decl_spec_type
1936   (cp_decl_specifier_seq *, tree, location_t, bool);
1937 static bool cp_parser_friend_p
1938   (const cp_decl_specifier_seq *);
1939 static void cp_parser_required_error
1940   (cp_parser *, required_token, bool);
1941 static cp_token *cp_parser_require
1942   (cp_parser *, enum cpp_ttype, required_token);
1943 static cp_token *cp_parser_require_keyword
1944   (cp_parser *, enum rid, required_token);
1945 static bool cp_parser_token_starts_function_definition_p
1946   (cp_token *);
1947 static bool cp_parser_next_token_starts_class_definition_p
1948   (cp_parser *);
1949 static bool cp_parser_next_token_ends_template_argument_p
1950   (cp_parser *);
1951 static bool cp_parser_nth_token_starts_template_argument_list_p
1952   (cp_parser *, size_t);
1953 static enum tag_types cp_parser_token_is_class_key
1954   (cp_token *);
1955 static void cp_parser_check_class_key
1956   (enum tag_types, tree type);
1957 static void cp_parser_check_access_in_redeclaration
1958   (tree type, location_t location);
1959 static bool cp_parser_optional_template_keyword
1960   (cp_parser *);
1961 static void cp_parser_pre_parsed_nested_name_specifier
1962   (cp_parser *);
1963 static bool cp_parser_cache_group
1964   (cp_parser *, enum cpp_ttype, unsigned);
1965 static void cp_parser_parse_tentatively
1966   (cp_parser *);
1967 static void cp_parser_commit_to_tentative_parse
1968   (cp_parser *);
1969 static void cp_parser_abort_tentative_parse
1970   (cp_parser *);
1971 static bool cp_parser_parse_definitely
1972   (cp_parser *);
1973 static inline bool cp_parser_parsing_tentatively
1974   (cp_parser *);
1975 static bool cp_parser_uncommitted_to_tentative_parse_p
1976   (cp_parser *);
1977 static void cp_parser_error
1978   (cp_parser *, const char *);
1979 static void cp_parser_name_lookup_error
1980   (cp_parser *, tree, tree, name_lookup_error, location_t);
1981 static bool cp_parser_simulate_error
1982   (cp_parser *);
1983 static bool cp_parser_check_type_definition
1984   (cp_parser *);
1985 static void cp_parser_check_for_definition_in_return_type
1986   (cp_declarator *, tree, location_t type_location);
1987 static void cp_parser_check_for_invalid_template_id
1988   (cp_parser *, tree, location_t location);
1989 static bool cp_parser_non_integral_constant_expression
1990   (cp_parser *, non_integral_constant);
1991 static void cp_parser_diagnose_invalid_type_name
1992   (cp_parser *, tree, tree, location_t);
1993 static bool cp_parser_parse_and_diagnose_invalid_type_name
1994   (cp_parser *);
1995 static int cp_parser_skip_to_closing_parenthesis
1996   (cp_parser *, bool, bool, bool);
1997 static void cp_parser_skip_to_end_of_statement
1998   (cp_parser *);
1999 static void cp_parser_consume_semicolon_at_end_of_statement
2000   (cp_parser *);
2001 static void cp_parser_skip_to_end_of_block_or_statement
2002   (cp_parser *);
2003 static bool cp_parser_skip_to_closing_brace
2004   (cp_parser *);
2005 static void cp_parser_skip_to_end_of_template_parameter_list
2006   (cp_parser *);
2007 static void cp_parser_skip_to_pragma_eol
2008   (cp_parser*, cp_token *);
2009 static bool cp_parser_error_occurred
2010   (cp_parser *);
2011 static bool cp_parser_allow_gnu_extensions_p
2012   (cp_parser *);
2013 static bool cp_parser_is_string_literal
2014   (cp_token *);
2015 static bool cp_parser_is_keyword
2016   (cp_token *, enum rid);
2017 static tree cp_parser_make_typename_type
2018   (cp_parser *, tree, tree, location_t location);
2019 static cp_declarator * cp_parser_make_indirect_declarator
2020   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2021
2022 /* Returns nonzero if we are parsing tentatively.  */
2023
2024 static inline bool
2025 cp_parser_parsing_tentatively (cp_parser* parser)
2026 {
2027   return parser->context->next != NULL;
2028 }
2029
2030 /* Returns nonzero if TOKEN is a string literal.  */
2031
2032 static bool
2033 cp_parser_is_string_literal (cp_token* token)
2034 {
2035   return (token->type == CPP_STRING ||
2036           token->type == CPP_STRING16 ||
2037           token->type == CPP_STRING32 ||
2038           token->type == CPP_WSTRING ||
2039           token->type == CPP_UTF8STRING);
2040 }
2041
2042 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2043
2044 static bool
2045 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2046 {
2047   return token->keyword == keyword;
2048 }
2049
2050 /* If not parsing tentatively, issue a diagnostic of the form
2051       FILE:LINE: MESSAGE before TOKEN
2052    where TOKEN is the next token in the input stream.  MESSAGE
2053    (specified by the caller) is usually of the form "expected
2054    OTHER-TOKEN".  */
2055
2056 static void
2057 cp_parser_error (cp_parser* parser, const char* gmsgid)
2058 {
2059   if (!cp_parser_simulate_error (parser))
2060     {
2061       cp_token *token = cp_lexer_peek_token (parser->lexer);
2062       /* This diagnostic makes more sense if it is tagged to the line
2063          of the token we just peeked at.  */
2064       cp_lexer_set_source_position_from_token (token);
2065
2066       if (token->type == CPP_PRAGMA)
2067         {
2068           error_at (token->location,
2069                     "%<#pragma%> is not allowed here");
2070           cp_parser_skip_to_pragma_eol (parser, token);
2071           return;
2072         }
2073
2074       c_parse_error (gmsgid,
2075                      /* Because c_parser_error does not understand
2076                         CPP_KEYWORD, keywords are treated like
2077                         identifiers.  */
2078                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2079                      token->u.value, token->flags);
2080     }
2081 }
2082
2083 /* Issue an error about name-lookup failing.  NAME is the
2084    IDENTIFIER_NODE DECL is the result of
2085    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2086    the thing that we hoped to find.  */
2087
2088 static void
2089 cp_parser_name_lookup_error (cp_parser* parser,
2090                              tree name,
2091                              tree decl,
2092                              name_lookup_error desired,
2093                              location_t location)
2094 {
2095   /* If name lookup completely failed, tell the user that NAME was not
2096      declared.  */
2097   if (decl == error_mark_node)
2098     {
2099       if (parser->scope && parser->scope != global_namespace)
2100         error_at (location, "%<%E::%E%> has not been declared",
2101                   parser->scope, name);
2102       else if (parser->scope == global_namespace)
2103         error_at (location, "%<::%E%> has not been declared", name);
2104       else if (parser->object_scope
2105                && !CLASS_TYPE_P (parser->object_scope))
2106         error_at (location, "request for member %qE in non-class type %qT",
2107                   name, parser->object_scope);
2108       else if (parser->object_scope)
2109         error_at (location, "%<%T::%E%> has not been declared",
2110                   parser->object_scope, name);
2111       else
2112         error_at (location, "%qE has not been declared", name);
2113     }
2114   else if (parser->scope && parser->scope != global_namespace)
2115     {
2116       switch (desired)
2117         {
2118           case NLE_TYPE:
2119             error_at (location, "%<%E::%E%> is not a type",
2120                                 parser->scope, name);
2121             break;
2122           case NLE_CXX98:
2123             error_at (location, "%<%E::%E%> is not a class or namespace",
2124                                 parser->scope, name);
2125             break;
2126           case NLE_NOT_CXX98:
2127             error_at (location,
2128                       "%<%E::%E%> is not a class, namespace, or enumeration",
2129                       parser->scope, name);
2130             break;
2131           default:
2132             gcc_unreachable ();
2133             
2134         }
2135     }
2136   else if (parser->scope == global_namespace)
2137     {
2138       switch (desired)
2139         {
2140           case NLE_TYPE:
2141             error_at (location, "%<::%E%> is not a type", name);
2142             break;
2143           case NLE_CXX98:
2144             error_at (location, "%<::%E%> is not a class or namespace", name);
2145             break;
2146           case NLE_NOT_CXX98:
2147             error_at (location,
2148                       "%<::%E%> is not a class, namespace, or enumeration",
2149                       name);
2150             break;
2151           default:
2152             gcc_unreachable ();
2153         }
2154     }
2155   else
2156     {
2157       switch (desired)
2158         {
2159           case NLE_TYPE:
2160             error_at (location, "%qE is not a type", name);
2161             break;
2162           case NLE_CXX98:
2163             error_at (location, "%qE is not a class or namespace", name);
2164             break;
2165           case NLE_NOT_CXX98:
2166             error_at (location,
2167                       "%qE is not a class, namespace, or enumeration", name);
2168             break;
2169           default:
2170             gcc_unreachable ();
2171         }
2172     }
2173 }
2174
2175 /* If we are parsing tentatively, remember that an error has occurred
2176    during this tentative parse.  Returns true if the error was
2177    simulated; false if a message should be issued by the caller.  */
2178
2179 static bool
2180 cp_parser_simulate_error (cp_parser* parser)
2181 {
2182   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2183     {
2184       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2185       return true;
2186     }
2187   return false;
2188 }
2189
2190 /* Check for repeated decl-specifiers.  */
2191
2192 static void
2193 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2194                            location_t location)
2195 {
2196   int ds;
2197
2198   for (ds = ds_first; ds != ds_last; ++ds)
2199     {
2200       unsigned count = decl_specs->specs[ds];
2201       if (count < 2)
2202         continue;
2203       /* The "long" specifier is a special case because of "long long".  */
2204       if (ds == ds_long)
2205         {
2206           if (count > 2)
2207             error_at (location, "%<long long long%> is too long for GCC");
2208           else 
2209             pedwarn_cxx98 (location, OPT_Wlong_long, 
2210                            "ISO C++ 1998 does not support %<long long%>");
2211         }
2212       else if (count > 1)
2213         {
2214           static const char *const decl_spec_names[] = {
2215             "signed",
2216             "unsigned",
2217             "short",
2218             "long",
2219             "const",
2220             "volatile",
2221             "restrict",
2222             "inline",
2223             "virtual",
2224             "explicit",
2225             "friend",
2226             "typedef",
2227             "constexpr",
2228             "__complex",
2229             "__thread"
2230           };
2231           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2232         }
2233     }
2234 }
2235
2236 /* This function is called when a type is defined.  If type
2237    definitions are forbidden at this point, an error message is
2238    issued.  */
2239
2240 static bool
2241 cp_parser_check_type_definition (cp_parser* parser)
2242 {
2243   /* If types are forbidden here, issue a message.  */
2244   if (parser->type_definition_forbidden_message)
2245     {
2246       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2247          in the message need to be interpreted.  */
2248       error (parser->type_definition_forbidden_message);
2249       return false;
2250     }
2251   return true;
2252 }
2253
2254 /* This function is called when the DECLARATOR is processed.  The TYPE
2255    was a type defined in the decl-specifiers.  If it is invalid to
2256    define a type in the decl-specifiers for DECLARATOR, an error is
2257    issued. TYPE_LOCATION is the location of TYPE and is used
2258    for error reporting.  */
2259
2260 static void
2261 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2262                                                tree type, location_t type_location)
2263 {
2264   /* [dcl.fct] forbids type definitions in return types.
2265      Unfortunately, it's not easy to know whether or not we are
2266      processing a return type until after the fact.  */
2267   while (declarator
2268          && (declarator->kind == cdk_pointer
2269              || declarator->kind == cdk_reference
2270              || declarator->kind == cdk_ptrmem))
2271     declarator = declarator->declarator;
2272   if (declarator
2273       && declarator->kind == cdk_function)
2274     {
2275       error_at (type_location,
2276                 "new types may not be defined in a return type");
2277       inform (type_location, 
2278               "(perhaps a semicolon is missing after the definition of %qT)",
2279               type);
2280     }
2281 }
2282
2283 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2284    "<" in any valid C++ program.  If the next token is indeed "<",
2285    issue a message warning the user about what appears to be an
2286    invalid attempt to form a template-id. LOCATION is the location
2287    of the type-specifier (TYPE) */
2288
2289 static void
2290 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2291                                          tree type, location_t location)
2292 {
2293   cp_token_position start = 0;
2294
2295   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2296     {
2297       if (TYPE_P (type))
2298         error_at (location, "%qT is not a template", type);
2299       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2300         error_at (location, "%qE is not a template", type);
2301       else
2302         error_at (location, "invalid template-id");
2303       /* Remember the location of the invalid "<".  */
2304       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2305         start = cp_lexer_token_position (parser->lexer, true);
2306       /* Consume the "<".  */
2307       cp_lexer_consume_token (parser->lexer);
2308       /* Parse the template arguments.  */
2309       cp_parser_enclosed_template_argument_list (parser);
2310       /* Permanently remove the invalid template arguments so that
2311          this error message is not issued again.  */
2312       if (start)
2313         cp_lexer_purge_tokens_after (parser->lexer, start);
2314     }
2315 }
2316
2317 /* If parsing an integral constant-expression, issue an error message
2318    about the fact that THING appeared and return true.  Otherwise,
2319    return false.  In either case, set
2320    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2321
2322 static bool
2323 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2324                                             non_integral_constant thing)
2325 {
2326   parser->non_integral_constant_expression_p = true;
2327   if (parser->integral_constant_expression_p)
2328     {
2329       if (!parser->allow_non_integral_constant_expression_p)
2330         {
2331           const char *msg = NULL;
2332           switch (thing)
2333             {
2334               case NIC_FLOAT:
2335                 error ("floating-point literal "
2336                        "cannot appear in a constant-expression");
2337                 return true;
2338               case NIC_CAST:
2339                 error ("a cast to a type other than an integral or "
2340                        "enumeration type cannot appear in a "
2341                        "constant-expression");
2342                 return true;
2343               case NIC_TYPEID:
2344                 error ("%<typeid%> operator "
2345                        "cannot appear in a constant-expression");
2346                 return true;
2347               case NIC_NCC:
2348                 error ("non-constant compound literals "
2349                        "cannot appear in a constant-expression");
2350                 return true;
2351               case NIC_FUNC_CALL:
2352                 error ("a function call "
2353                        "cannot appear in a constant-expression");
2354                 return true;
2355               case NIC_INC:
2356                 error ("an increment "
2357                        "cannot appear in a constant-expression");
2358                 return true;
2359               case NIC_DEC:
2360                 error ("an decrement "
2361                        "cannot appear in a constant-expression");
2362                 return true;
2363               case NIC_ARRAY_REF:
2364                 error ("an array reference "
2365                        "cannot appear in a constant-expression");
2366                 return true;
2367               case NIC_ADDR_LABEL:
2368                 error ("the address of a label "
2369                        "cannot appear in a constant-expression");
2370                 return true;
2371               case NIC_OVERLOADED:
2372                 error ("calls to overloaded operators "
2373                        "cannot appear in a constant-expression");
2374                 return true;
2375               case NIC_ASSIGNMENT:
2376                 error ("an assignment cannot appear in a constant-expression");
2377                 return true;
2378               case NIC_COMMA:
2379                 error ("a comma operator "
2380                        "cannot appear in a constant-expression");
2381                 return true;
2382               case NIC_CONSTRUCTOR:
2383                 error ("a call to a constructor "
2384                        "cannot appear in a constant-expression");
2385                 return true;
2386               case NIC_THIS:
2387                 msg = "this";
2388                 break;
2389               case NIC_FUNC_NAME:
2390                 msg = "__FUNCTION__";
2391                 break;
2392               case NIC_PRETTY_FUNC:
2393                 msg = "__PRETTY_FUNCTION__";
2394                 break;
2395               case NIC_C99_FUNC:
2396                 msg = "__func__";
2397                 break;
2398               case NIC_VA_ARG:
2399                 msg = "va_arg";
2400                 break;
2401               case NIC_ARROW:
2402                 msg = "->";
2403                 break;
2404               case NIC_POINT:
2405                 msg = ".";
2406                 break;
2407               case NIC_STAR:
2408                 msg = "*";
2409                 break;
2410               case NIC_ADDR:
2411                 msg = "&";
2412                 break;
2413               case NIC_PREINCREMENT:
2414                 msg = "++";
2415                 break;
2416               case NIC_PREDECREMENT:
2417                 msg = "--";
2418                 break;
2419               case NIC_NEW:
2420                 msg = "new";
2421                 break;
2422               case NIC_DEL:
2423                 msg = "delete";
2424                 break;
2425               default:
2426                 gcc_unreachable ();
2427             }
2428           if (msg)
2429             error ("%qs cannot appear in a constant-expression", msg);
2430           return true;
2431         }
2432     }
2433   return false;
2434 }
2435
2436 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2437    qualifying scope (or NULL, if none) for ID.  This function commits
2438    to the current active tentative parse, if any.  (Otherwise, the
2439    problematic construct might be encountered again later, resulting
2440    in duplicate error messages.) LOCATION is the location of ID.  */
2441
2442 static void
2443 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2444                                       tree scope, tree id,
2445                                       location_t location)
2446 {
2447   tree decl, old_scope;
2448   cp_parser_commit_to_tentative_parse (parser);
2449   /* Try to lookup the identifier.  */
2450   old_scope = parser->scope;
2451   parser->scope = scope;
2452   decl = cp_parser_lookup_name_simple (parser, id, location);
2453   parser->scope = old_scope;
2454   /* If the lookup found a template-name, it means that the user forgot
2455   to specify an argument list. Emit a useful error message.  */
2456   if (TREE_CODE (decl) == TEMPLATE_DECL)
2457     error_at (location,
2458               "invalid use of template-name %qE without an argument list",
2459               decl);
2460   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2461     error_at (location, "invalid use of destructor %qD as a type", id);
2462   else if (TREE_CODE (decl) == TYPE_DECL)
2463     /* Something like 'unsigned A a;'  */
2464     error_at (location, "invalid combination of multiple type-specifiers");
2465   else if (!parser->scope)
2466     {
2467       /* Issue an error message.  */
2468       error_at (location, "%qE does not name a type", id);
2469       /* If we're in a template class, it's possible that the user was
2470          referring to a type from a base class.  For example:
2471
2472            template <typename T> struct A { typedef T X; };
2473            template <typename T> struct B : public A<T> { X x; };
2474
2475          The user should have said "typename A<T>::X".  */
2476       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2477         inform (location, "C++0x %<constexpr%> only available with "
2478                 "-std=c++0x or -std=gnu++0x");
2479       else if (processing_template_decl && current_class_type
2480                && TYPE_BINFO (current_class_type))
2481         {
2482           tree b;
2483
2484           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2485                b;
2486                b = TREE_CHAIN (b))
2487             {
2488               tree base_type = BINFO_TYPE (b);
2489               if (CLASS_TYPE_P (base_type)
2490                   && dependent_type_p (base_type))
2491                 {
2492                   tree field;
2493                   /* Go from a particular instantiation of the
2494                      template (which will have an empty TYPE_FIELDs),
2495                      to the main version.  */
2496                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2497                   for (field = TYPE_FIELDS (base_type);
2498                        field;
2499                        field = DECL_CHAIN (field))
2500                     if (TREE_CODE (field) == TYPE_DECL
2501                         && DECL_NAME (field) == id)
2502                       {
2503                         inform (location, 
2504                                 "(perhaps %<typename %T::%E%> was intended)",
2505                                 BINFO_TYPE (b), id);
2506                         break;
2507                       }
2508                   if (field)
2509                     break;
2510                 }
2511             }
2512         }
2513     }
2514   /* Here we diagnose qualified-ids where the scope is actually correct,
2515      but the identifier does not resolve to a valid type name.  */
2516   else if (parser->scope != error_mark_node)
2517     {
2518       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2519         error_at (location, "%qE in namespace %qE does not name a type",
2520                   id, parser->scope);
2521       else if (CLASS_TYPE_P (parser->scope)
2522                && constructor_name_p (id, parser->scope))
2523         {
2524           /* A<T>::A<T>() */
2525           error_at (location, "%<%T::%E%> names the constructor, not"
2526                     " the type", parser->scope, id);
2527           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2528             error_at (location, "and %qT has no template constructors",
2529                       parser->scope);
2530         }
2531       else if (TYPE_P (parser->scope)
2532                && dependent_scope_p (parser->scope))
2533         error_at (location, "need %<typename%> before %<%T::%E%> because "
2534                   "%qT is a dependent scope",
2535                   parser->scope, id, parser->scope);
2536       else if (TYPE_P (parser->scope))
2537         error_at (location, "%qE in %q#T does not name a type",
2538                   id, parser->scope);
2539       else
2540         gcc_unreachable ();
2541     }
2542 }
2543
2544 /* Check for a common situation where a type-name should be present,
2545    but is not, and issue a sensible error message.  Returns true if an
2546    invalid type-name was detected.
2547
2548    The situation handled by this function are variable declarations of the
2549    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2550    Usually, `ID' should name a type, but if we got here it means that it
2551    does not. We try to emit the best possible error message depending on
2552    how exactly the id-expression looks like.  */
2553
2554 static bool
2555 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2556 {
2557   tree id;
2558   cp_token *token = cp_lexer_peek_token (parser->lexer);
2559
2560   /* Avoid duplicate error about ambiguous lookup.  */
2561   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2562     {
2563       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2564       if (next->type == CPP_NAME && next->ambiguous_p)
2565         goto out;
2566     }
2567
2568   cp_parser_parse_tentatively (parser);
2569   id = cp_parser_id_expression (parser,
2570                                 /*template_keyword_p=*/false,
2571                                 /*check_dependency_p=*/true,
2572                                 /*template_p=*/NULL,
2573                                 /*declarator_p=*/true,
2574                                 /*optional_p=*/false);
2575   /* If the next token is a (, this is a function with no explicit return
2576      type, i.e. constructor, destructor or conversion op.  */
2577   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2578       || TREE_CODE (id) == TYPE_DECL)
2579     {
2580       cp_parser_abort_tentative_parse (parser);
2581       return false;
2582     }
2583   if (!cp_parser_parse_definitely (parser))
2584     return false;
2585
2586   /* Emit a diagnostic for the invalid type.  */
2587   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2588                                         id, token->location);
2589  out:
2590   /* If we aren't in the middle of a declarator (i.e. in a
2591      parameter-declaration-clause), skip to the end of the declaration;
2592      there's no point in trying to process it.  */
2593   if (!parser->in_declarator_p)
2594     cp_parser_skip_to_end_of_block_or_statement (parser);
2595   return true;
2596 }
2597
2598 /* Consume tokens up to, and including, the next non-nested closing `)'.
2599    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2600    are doing error recovery. Returns -1 if OR_COMMA is true and we
2601    found an unnested comma.  */
2602
2603 static int
2604 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2605                                        bool recovering,
2606                                        bool or_comma,
2607                                        bool consume_paren)
2608 {
2609   unsigned paren_depth = 0;
2610   unsigned brace_depth = 0;
2611   unsigned square_depth = 0;
2612
2613   if (recovering && !or_comma
2614       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2615     return 0;
2616
2617   while (true)
2618     {
2619       cp_token * token = cp_lexer_peek_token (parser->lexer);
2620
2621       switch (token->type)
2622         {
2623         case CPP_EOF:
2624         case CPP_PRAGMA_EOL:
2625           /* If we've run out of tokens, then there is no closing `)'.  */
2626           return 0;
2627
2628         /* This is good for lambda expression capture-lists.  */
2629         case CPP_OPEN_SQUARE:
2630           ++square_depth;
2631           break;
2632         case CPP_CLOSE_SQUARE:
2633           if (!square_depth--)
2634             return 0;
2635           break;
2636
2637         case CPP_SEMICOLON:
2638           /* This matches the processing in skip_to_end_of_statement.  */
2639           if (!brace_depth)
2640             return 0;
2641           break;
2642
2643         case CPP_OPEN_BRACE:
2644           ++brace_depth;
2645           break;
2646         case CPP_CLOSE_BRACE:
2647           if (!brace_depth--)
2648             return 0;
2649           break;
2650
2651         case CPP_COMMA:
2652           if (recovering && or_comma && !brace_depth && !paren_depth
2653               && !square_depth)
2654             return -1;
2655           break;
2656
2657         case CPP_OPEN_PAREN:
2658           if (!brace_depth)
2659             ++paren_depth;
2660           break;
2661
2662         case CPP_CLOSE_PAREN:
2663           if (!brace_depth && !paren_depth--)
2664             {
2665               if (consume_paren)
2666                 cp_lexer_consume_token (parser->lexer);
2667               return 1;
2668             }
2669           break;
2670
2671         default:
2672           break;
2673         }
2674
2675       /* Consume the token.  */
2676       cp_lexer_consume_token (parser->lexer);
2677     }
2678 }
2679
2680 /* Consume tokens until we reach the end of the current statement.
2681    Normally, that will be just before consuming a `;'.  However, if a
2682    non-nested `}' comes first, then we stop before consuming that.  */
2683
2684 static void
2685 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2686 {
2687   unsigned nesting_depth = 0;
2688
2689   while (true)
2690     {
2691       cp_token *token = cp_lexer_peek_token (parser->lexer);
2692
2693       switch (token->type)
2694         {
2695         case CPP_EOF:
2696         case CPP_PRAGMA_EOL:
2697           /* If we've run out of tokens, stop.  */
2698           return;
2699
2700         case CPP_SEMICOLON:
2701           /* If the next token is a `;', we have reached the end of the
2702              statement.  */
2703           if (!nesting_depth)
2704             return;
2705           break;
2706
2707         case CPP_CLOSE_BRACE:
2708           /* If this is a non-nested '}', stop before consuming it.
2709              That way, when confronted with something like:
2710
2711                { 3 + }
2712
2713              we stop before consuming the closing '}', even though we
2714              have not yet reached a `;'.  */
2715           if (nesting_depth == 0)
2716             return;
2717
2718           /* If it is the closing '}' for a block that we have
2719              scanned, stop -- but only after consuming the token.
2720              That way given:
2721
2722                 void f g () { ... }
2723                 typedef int I;
2724
2725              we will stop after the body of the erroneously declared
2726              function, but before consuming the following `typedef'
2727              declaration.  */
2728           if (--nesting_depth == 0)
2729             {
2730               cp_lexer_consume_token (parser->lexer);
2731               return;
2732             }
2733
2734         case CPP_OPEN_BRACE:
2735           ++nesting_depth;
2736           break;
2737
2738         default:
2739           break;
2740         }
2741
2742       /* Consume the token.  */
2743       cp_lexer_consume_token (parser->lexer);
2744     }
2745 }
2746
2747 /* This function is called at the end of a statement or declaration.
2748    If the next token is a semicolon, it is consumed; otherwise, error
2749    recovery is attempted.  */
2750
2751 static void
2752 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
2753 {
2754   /* Look for the trailing `;'.  */
2755   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
2756     {
2757       /* If there is additional (erroneous) input, skip to the end of
2758          the statement.  */
2759       cp_parser_skip_to_end_of_statement (parser);
2760       /* If the next token is now a `;', consume it.  */
2761       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
2762         cp_lexer_consume_token (parser->lexer);
2763     }
2764 }
2765
2766 /* Skip tokens until we have consumed an entire block, or until we
2767    have consumed a non-nested `;'.  */
2768
2769 static void
2770 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
2771 {
2772   int nesting_depth = 0;
2773
2774   while (nesting_depth >= 0)
2775     {
2776       cp_token *token = cp_lexer_peek_token (parser->lexer);
2777
2778       switch (token->type)
2779         {
2780         case CPP_EOF:
2781         case CPP_PRAGMA_EOL:
2782           /* If we've run out of tokens, stop.  */
2783           return;
2784
2785         case CPP_SEMICOLON:
2786           /* Stop if this is an unnested ';'. */
2787           if (!nesting_depth)
2788             nesting_depth = -1;
2789           break;
2790
2791         case CPP_CLOSE_BRACE:
2792           /* Stop if this is an unnested '}', or closes the outermost
2793              nesting level.  */
2794           nesting_depth--;
2795           if (nesting_depth < 0)
2796             return;
2797           if (!nesting_depth)
2798             nesting_depth = -1;
2799           break;
2800
2801         case CPP_OPEN_BRACE:
2802           /* Nest. */
2803           nesting_depth++;
2804           break;
2805
2806         default:
2807           break;
2808         }
2809
2810       /* Consume the token.  */
2811       cp_lexer_consume_token (parser->lexer);
2812     }
2813 }
2814
2815 /* Skip tokens until a non-nested closing curly brace is the next
2816    token, or there are no more tokens. Return true in the first case,
2817    false otherwise.  */
2818
2819 static bool
2820 cp_parser_skip_to_closing_brace (cp_parser *parser)
2821 {
2822   unsigned nesting_depth = 0;
2823
2824   while (true)
2825     {
2826       cp_token *token = cp_lexer_peek_token (parser->lexer);
2827
2828       switch (token->type)
2829         {
2830         case CPP_EOF:
2831         case CPP_PRAGMA_EOL:
2832           /* If we've run out of tokens, stop.  */
2833           return false;
2834
2835         case CPP_CLOSE_BRACE:
2836           /* If the next token is a non-nested `}', then we have reached
2837              the end of the current block.  */
2838           if (nesting_depth-- == 0)
2839             return true;
2840           break;
2841
2842         case CPP_OPEN_BRACE:
2843           /* If it the next token is a `{', then we are entering a new
2844              block.  Consume the entire block.  */
2845           ++nesting_depth;
2846           break;
2847
2848         default:
2849           break;
2850         }
2851
2852       /* Consume the token.  */
2853       cp_lexer_consume_token (parser->lexer);
2854     }
2855 }
2856
2857 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
2858    parameter is the PRAGMA token, allowing us to purge the entire pragma
2859    sequence.  */
2860
2861 static void
2862 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
2863 {
2864   cp_token *token;
2865
2866   parser->lexer->in_pragma = false;
2867
2868   do
2869     token = cp_lexer_consume_token (parser->lexer);
2870   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
2871
2872   /* Ensure that the pragma is not parsed again.  */
2873   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
2874 }
2875
2876 /* Require pragma end of line, resyncing with it as necessary.  The
2877    arguments are as for cp_parser_skip_to_pragma_eol.  */
2878
2879 static void
2880 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
2881 {
2882   parser->lexer->in_pragma = false;
2883   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
2884     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
2885 }
2886
2887 /* This is a simple wrapper around make_typename_type. When the id is
2888    an unresolved identifier node, we can provide a superior diagnostic
2889    using cp_parser_diagnose_invalid_type_name.  */
2890
2891 static tree
2892 cp_parser_make_typename_type (cp_parser *parser, tree scope,
2893                               tree id, location_t id_location)
2894 {
2895   tree result;
2896   if (TREE_CODE (id) == IDENTIFIER_NODE)
2897     {
2898       result = make_typename_type (scope, id, typename_type,
2899                                    /*complain=*/tf_none);
2900       if (result == error_mark_node)
2901         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
2902       return result;
2903     }
2904   return make_typename_type (scope, id, typename_type, tf_error);
2905 }
2906
2907 /* This is a wrapper around the
2908    make_{pointer,ptrmem,reference}_declarator functions that decides
2909    which one to call based on the CODE and CLASS_TYPE arguments. The
2910    CODE argument should be one of the values returned by
2911    cp_parser_ptr_operator. */
2912 static cp_declarator *
2913 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
2914                                     cp_cv_quals cv_qualifiers,
2915                                     cp_declarator *target)
2916 {
2917   if (code == ERROR_MARK)
2918     return cp_error_declarator;
2919
2920   if (code == INDIRECT_REF)
2921     if (class_type == NULL_TREE)
2922       return make_pointer_declarator (cv_qualifiers, target);
2923     else
2924       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
2925   else if (code == ADDR_EXPR && class_type == NULL_TREE)
2926     return make_reference_declarator (cv_qualifiers, target, false);
2927   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
2928     return make_reference_declarator (cv_qualifiers, target, true);
2929   gcc_unreachable ();
2930 }
2931
2932 /* Create a new C++ parser.  */
2933
2934 static cp_parser *
2935 cp_parser_new (void)
2936 {
2937   cp_parser *parser;
2938   cp_lexer *lexer;
2939   unsigned i;
2940
2941   /* cp_lexer_new_main is called before doing GC allocation because
2942      cp_lexer_new_main might load a PCH file.  */
2943   lexer = cp_lexer_new_main ();
2944
2945   /* Initialize the binops_by_token so that we can get the tree
2946      directly from the token.  */
2947   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
2948     binops_by_token[binops[i].token_type] = binops[i];
2949
2950   parser = ggc_alloc_cleared_cp_parser ();
2951   parser->lexer = lexer;
2952   parser->context = cp_parser_context_new (NULL);
2953
2954   /* For now, we always accept GNU extensions.  */
2955   parser->allow_gnu_extensions_p = 1;
2956
2957   /* The `>' token is a greater-than operator, not the end of a
2958      template-id.  */
2959   parser->greater_than_is_operator_p = true;
2960
2961   parser->default_arg_ok_p = true;
2962
2963   /* We are not parsing a constant-expression.  */
2964   parser->integral_constant_expression_p = false;
2965   parser->allow_non_integral_constant_expression_p = false;
2966   parser->non_integral_constant_expression_p = false;
2967
2968   /* Local variable names are not forbidden.  */
2969   parser->local_variables_forbidden_p = false;
2970
2971   /* We are not processing an `extern "C"' declaration.  */
2972   parser->in_unbraced_linkage_specification_p = false;
2973
2974   /* We are not processing a declarator.  */
2975   parser->in_declarator_p = false;
2976
2977   /* We are not processing a template-argument-list.  */
2978   parser->in_template_argument_list_p = false;
2979
2980   /* We are not in an iteration statement.  */
2981   parser->in_statement = 0;
2982
2983   /* We are not in a switch statement.  */
2984   parser->in_switch_statement_p = false;
2985
2986   /* We are not parsing a type-id inside an expression.  */
2987   parser->in_type_id_in_expr_p = false;
2988
2989   /* Declarations aren't implicitly extern "C".  */
2990   parser->implicit_extern_c = false;
2991
2992   /* String literals should be translated to the execution character set.  */
2993   parser->translate_strings_p = true;
2994
2995   /* We are not parsing a function body.  */
2996   parser->in_function_body = false;
2997
2998   /* We can correct until told otherwise.  */
2999   parser->colon_corrects_to_scope_p = true;
3000
3001   /* The unparsed function queue is empty.  */
3002   push_unparsed_function_queues (parser);
3003
3004   /* There are no classes being defined.  */
3005   parser->num_classes_being_defined = 0;
3006
3007   /* No template parameters apply.  */
3008   parser->num_template_parameter_lists = 0;
3009
3010   return parser;
3011 }
3012
3013 /* Create a cp_lexer structure which will emit the tokens in CACHE
3014    and push it onto the parser's lexer stack.  This is used for delayed
3015    parsing of in-class method bodies and default arguments, and should
3016    not be confused with tentative parsing.  */
3017 static void
3018 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3019 {
3020   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3021   lexer->next = parser->lexer;
3022   parser->lexer = lexer;
3023
3024   /* Move the current source position to that of the first token in the
3025      new lexer.  */
3026   cp_lexer_set_source_position_from_token (lexer->next_token);
3027 }
3028
3029 /* Pop the top lexer off the parser stack.  This is never used for the
3030    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3031 static void
3032 cp_parser_pop_lexer (cp_parser *parser)
3033 {
3034   cp_lexer *lexer = parser->lexer;
3035   parser->lexer = lexer->next;
3036   cp_lexer_destroy (lexer);
3037
3038   /* Put the current source position back where it was before this
3039      lexer was pushed.  */
3040   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3041 }
3042
3043 /* Lexical conventions [gram.lex]  */
3044
3045 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3046    identifier.  */
3047
3048 static tree
3049 cp_parser_identifier (cp_parser* parser)
3050 {
3051   cp_token *token;
3052
3053   /* Look for the identifier.  */
3054   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3055   /* Return the value.  */
3056   return token ? token->u.value : error_mark_node;
3057 }
3058
3059 /* Parse a sequence of adjacent string constants.  Returns a
3060    TREE_STRING representing the combined, nul-terminated string
3061    constant.  If TRANSLATE is true, translate the string to the
3062    execution character set.  If WIDE_OK is true, a wide string is
3063    invalid here.
3064
3065    C++98 [lex.string] says that if a narrow string literal token is
3066    adjacent to a wide string literal token, the behavior is undefined.
3067    However, C99 6.4.5p4 says that this results in a wide string literal.
3068    We follow C99 here, for consistency with the C front end.
3069
3070    This code is largely lifted from lex_string() in c-lex.c.
3071
3072    FUTURE: ObjC++ will need to handle @-strings here.  */
3073 static tree
3074 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3075 {
3076   tree value;
3077   size_t count;
3078   struct obstack str_ob;
3079   cpp_string str, istr, *strs;
3080   cp_token *tok;
3081   enum cpp_ttype type;
3082
3083   tok = cp_lexer_peek_token (parser->lexer);
3084   if (!cp_parser_is_string_literal (tok))
3085     {
3086       cp_parser_error (parser, "expected string-literal");
3087       return error_mark_node;
3088     }
3089
3090   type = tok->type;
3091
3092   /* Try to avoid the overhead of creating and destroying an obstack
3093      for the common case of just one string.  */
3094   if (!cp_parser_is_string_literal
3095       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3096     {
3097       cp_lexer_consume_token (parser->lexer);
3098
3099       str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3100       str.len = TREE_STRING_LENGTH (tok->u.value);
3101       count = 1;
3102
3103       strs = &str;
3104     }
3105   else
3106     {
3107       gcc_obstack_init (&str_ob);
3108       count = 0;
3109
3110       do
3111         {
3112           cp_lexer_consume_token (parser->lexer);
3113           count++;
3114           str.text = (const unsigned char *)TREE_STRING_POINTER (tok->u.value);
3115           str.len = TREE_STRING_LENGTH (tok->u.value);
3116
3117           if (type != tok->type)
3118             {
3119               if (type == CPP_STRING)
3120                 type = tok->type;
3121               else if (tok->type != CPP_STRING)
3122                 error_at (tok->location,
3123                           "unsupported non-standard concatenation "
3124                           "of string literals");
3125             }
3126
3127           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3128
3129           tok = cp_lexer_peek_token (parser->lexer);
3130         }
3131       while (cp_parser_is_string_literal (tok));
3132
3133       strs = (cpp_string *) obstack_finish (&str_ob);
3134     }
3135
3136   if (type != CPP_STRING && !wide_ok)
3137     {
3138       cp_parser_error (parser, "a wide string is invalid in this context");
3139       type = CPP_STRING;
3140     }
3141
3142   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3143       (parse_in, strs, count, &istr, type))
3144     {
3145       value = build_string (istr.len, (const char *)istr.text);
3146       free (CONST_CAST (unsigned char *, istr.text));
3147
3148       switch (type)
3149         {
3150         default:
3151         case CPP_STRING:
3152         case CPP_UTF8STRING:
3153           TREE_TYPE (value) = char_array_type_node;
3154           break;
3155         case CPP_STRING16:
3156           TREE_TYPE (value) = char16_array_type_node;
3157           break;
3158         case CPP_STRING32:
3159           TREE_TYPE (value) = char32_array_type_node;
3160           break;
3161         case CPP_WSTRING:
3162           TREE_TYPE (value) = wchar_array_type_node;
3163           break;
3164         }
3165
3166       value = fix_string_type (value);
3167     }
3168   else
3169     /* cpp_interpret_string has issued an error.  */
3170     value = error_mark_node;
3171
3172   if (count > 1)
3173     obstack_free (&str_ob, 0);
3174
3175   return value;
3176 }
3177
3178
3179 /* Basic concepts [gram.basic]  */
3180
3181 /* Parse a translation-unit.
3182
3183    translation-unit:
3184      declaration-seq [opt]
3185
3186    Returns TRUE if all went well.  */
3187
3188 static bool
3189 cp_parser_translation_unit (cp_parser* parser)
3190 {
3191   /* The address of the first non-permanent object on the declarator
3192      obstack.  */
3193   static void *declarator_obstack_base;
3194
3195   bool success;
3196
3197   /* Create the declarator obstack, if necessary.  */
3198   if (!cp_error_declarator)
3199     {
3200       gcc_obstack_init (&declarator_obstack);
3201       /* Create the error declarator.  */
3202       cp_error_declarator = make_declarator (cdk_error);
3203       /* Create the empty parameter list.  */
3204       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3205       /* Remember where the base of the declarator obstack lies.  */
3206       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3207     }
3208
3209   cp_parser_declaration_seq_opt (parser);
3210
3211   /* If there are no tokens left then all went well.  */
3212   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3213     {
3214       /* Get rid of the token array; we don't need it any more.  */
3215       cp_lexer_destroy (parser->lexer);
3216       parser->lexer = NULL;
3217
3218       /* This file might have been a context that's implicitly extern
3219          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3220       if (parser->implicit_extern_c)
3221         {
3222           pop_lang_context ();
3223           parser->implicit_extern_c = false;
3224         }
3225
3226       /* Finish up.  */
3227       finish_translation_unit ();
3228
3229       success = true;
3230     }
3231   else
3232     {
3233       cp_parser_error (parser, "expected declaration");
3234       success = false;
3235     }
3236
3237   /* Make sure the declarator obstack was fully cleaned up.  */
3238   gcc_assert (obstack_next_free (&declarator_obstack)
3239               == declarator_obstack_base);
3240
3241   /* All went well.  */
3242   return success;
3243 }
3244
3245 /* Expressions [gram.expr] */
3246
3247 /* Parse a primary-expression.
3248
3249    primary-expression:
3250      literal
3251      this
3252      ( expression )
3253      id-expression
3254
3255    GNU Extensions:
3256
3257    primary-expression:
3258      ( compound-statement )
3259      __builtin_va_arg ( assignment-expression , type-id )
3260      __builtin_offsetof ( type-id , offsetof-expression )
3261
3262    C++ Extensions:
3263      __has_nothrow_assign ( type-id )   
3264      __has_nothrow_constructor ( type-id )
3265      __has_nothrow_copy ( type-id )
3266      __has_trivial_assign ( type-id )   
3267      __has_trivial_constructor ( type-id )
3268      __has_trivial_copy ( type-id )
3269      __has_trivial_destructor ( type-id )
3270      __has_virtual_destructor ( type-id )     
3271      __is_abstract ( type-id )
3272      __is_base_of ( type-id , type-id )
3273      __is_class ( type-id )
3274      __is_convertible_to ( type-id , type-id )     
3275      __is_empty ( type-id )
3276      __is_enum ( type-id )
3277      __is_literal_type ( type-id )
3278      __is_pod ( type-id )
3279      __is_polymorphic ( type-id )
3280      __is_std_layout ( type-id )
3281      __is_trivial ( type-id )
3282      __is_union ( type-id )
3283
3284    Objective-C++ Extension:
3285
3286    primary-expression:
3287      objc-expression
3288
3289    literal:
3290      __null
3291
3292    ADDRESS_P is true iff this expression was immediately preceded by
3293    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3294    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3295    true iff this expression is a template argument.
3296
3297    Returns a representation of the expression.  Upon return, *IDK
3298    indicates what kind of id-expression (if any) was present.  */
3299
3300 static tree
3301 cp_parser_primary_expression (cp_parser *parser,
3302                               bool address_p,
3303                               bool cast_p,
3304                               bool template_arg_p,
3305                               cp_id_kind *idk)
3306 {
3307   cp_token *token = NULL;
3308
3309   /* Assume the primary expression is not an id-expression.  */
3310   *idk = CP_ID_KIND_NONE;
3311
3312   /* Peek at the next token.  */
3313   token = cp_lexer_peek_token (parser->lexer);
3314   switch (token->type)
3315     {
3316       /* literal:
3317            integer-literal
3318            character-literal
3319            floating-literal
3320            string-literal
3321            boolean-literal  */
3322     case CPP_CHAR:
3323     case CPP_CHAR16:
3324     case CPP_CHAR32:
3325     case CPP_WCHAR:
3326     case CPP_NUMBER:
3327       token = cp_lexer_consume_token (parser->lexer);
3328       if (TREE_CODE (token->u.value) == FIXED_CST)
3329         {
3330           error_at (token->location,
3331                     "fixed-point types not supported in C++");
3332           return error_mark_node;
3333         }
3334       /* Floating-point literals are only allowed in an integral
3335          constant expression if they are cast to an integral or
3336          enumeration type.  */
3337       if (TREE_CODE (token->u.value) == REAL_CST
3338           && parser->integral_constant_expression_p
3339           && pedantic)
3340         {
3341           /* CAST_P will be set even in invalid code like "int(2.7 +
3342              ...)".   Therefore, we have to check that the next token
3343              is sure to end the cast.  */
3344           if (cast_p)
3345             {
3346               cp_token *next_token;
3347
3348               next_token = cp_lexer_peek_token (parser->lexer);
3349               if (/* The comma at the end of an
3350                      enumerator-definition.  */
3351                   next_token->type != CPP_COMMA
3352                   /* The curly brace at the end of an enum-specifier.  */
3353                   && next_token->type != CPP_CLOSE_BRACE
3354                   /* The end of a statement.  */
3355                   && next_token->type != CPP_SEMICOLON
3356                   /* The end of the cast-expression.  */
3357                   && next_token->type != CPP_CLOSE_PAREN
3358                   /* The end of an array bound.  */
3359                   && next_token->type != CPP_CLOSE_SQUARE
3360                   /* The closing ">" in a template-argument-list.  */
3361                   && (next_token->type != CPP_GREATER
3362                       || parser->greater_than_is_operator_p)
3363                   /* C++0x only: A ">>" treated like two ">" tokens,
3364                      in a template-argument-list.  */
3365                   && (next_token->type != CPP_RSHIFT
3366                       || (cxx_dialect == cxx98)
3367                       || parser->greater_than_is_operator_p))
3368                 cast_p = false;
3369             }
3370
3371           /* If we are within a cast, then the constraint that the
3372              cast is to an integral or enumeration type will be
3373              checked at that point.  If we are not within a cast, then
3374              this code is invalid.  */
3375           if (!cast_p)
3376             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3377         }
3378       return token->u.value;
3379
3380     case CPP_STRING:
3381     case CPP_STRING16:
3382     case CPP_STRING32:
3383     case CPP_WSTRING:
3384     case CPP_UTF8STRING:
3385       /* ??? Should wide strings be allowed when parser->translate_strings_p
3386          is false (i.e. in attributes)?  If not, we can kill the third
3387          argument to cp_parser_string_literal.  */
3388       return cp_parser_string_literal (parser,
3389                                        parser->translate_strings_p,
3390                                        true);
3391
3392     case CPP_OPEN_PAREN:
3393       {
3394         tree expr;
3395         bool saved_greater_than_is_operator_p;
3396
3397         /* Consume the `('.  */
3398         cp_lexer_consume_token (parser->lexer);
3399         /* Within a parenthesized expression, a `>' token is always
3400            the greater-than operator.  */
3401         saved_greater_than_is_operator_p
3402           = parser->greater_than_is_operator_p;
3403         parser->greater_than_is_operator_p = true;
3404         /* If we see `( { ' then we are looking at the beginning of
3405            a GNU statement-expression.  */
3406         if (cp_parser_allow_gnu_extensions_p (parser)
3407             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3408           {
3409             /* Statement-expressions are not allowed by the standard.  */
3410             pedwarn (token->location, OPT_pedantic, 
3411                      "ISO C++ forbids braced-groups within expressions");
3412
3413             /* And they're not allowed outside of a function-body; you
3414                cannot, for example, write:
3415
3416                  int i = ({ int j = 3; j + 1; });
3417
3418                at class or namespace scope.  */
3419             if (!parser->in_function_body
3420                 || parser->in_template_argument_list_p)
3421               {
3422                 error_at (token->location,
3423                           "statement-expressions are not allowed outside "
3424                           "functions nor in template-argument lists");
3425                 cp_parser_skip_to_end_of_block_or_statement (parser);
3426                 expr = error_mark_node;
3427               }
3428             else
3429               {
3430                 /* Start the statement-expression.  */
3431                 expr = begin_stmt_expr ();
3432                 /* Parse the compound-statement.  */
3433                 cp_parser_compound_statement (parser, expr, false, false);
3434                 /* Finish up.  */
3435                 expr = finish_stmt_expr (expr, false);
3436               }
3437           }
3438         else
3439           {
3440             /* Parse the parenthesized expression.  */
3441             expr = cp_parser_expression (parser, cast_p, idk);
3442             /* Let the front end know that this expression was
3443                enclosed in parentheses. This matters in case, for
3444                example, the expression is of the form `A::B', since
3445                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3446                not.  */
3447             finish_parenthesized_expr (expr);
3448             /* DR 705: Wrapping an unqualified name in parentheses
3449                suppresses arg-dependent lookup.  We want to pass back
3450                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3451                (c++/37862), but none of the others.  */
3452             if (*idk != CP_ID_KIND_QUALIFIED)
3453               *idk = CP_ID_KIND_NONE;
3454           }
3455         /* The `>' token might be the end of a template-id or
3456            template-parameter-list now.  */
3457         parser->greater_than_is_operator_p
3458           = saved_greater_than_is_operator_p;
3459         /* Consume the `)'.  */
3460         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
3461           cp_parser_skip_to_end_of_statement (parser);
3462
3463         return expr;
3464       }
3465
3466     case CPP_OPEN_SQUARE:
3467       if (c_dialect_objc ())
3468         /* We have an Objective-C++ message. */
3469         return cp_parser_objc_expression (parser);
3470       {
3471         tree lam = cp_parser_lambda_expression (parser);
3472         /* Don't warn about a failed tentative parse.  */
3473         if (cp_parser_error_occurred (parser))
3474           return error_mark_node;
3475         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
3476         return lam;
3477       }
3478
3479     case CPP_OBJC_STRING:
3480       if (c_dialect_objc ())
3481         /* We have an Objective-C++ string literal. */
3482         return cp_parser_objc_expression (parser);
3483       cp_parser_error (parser, "expected primary-expression");
3484       return error_mark_node;
3485
3486     case CPP_KEYWORD:
3487       switch (token->keyword)
3488         {
3489           /* These two are the boolean literals.  */
3490         case RID_TRUE:
3491           cp_lexer_consume_token (parser->lexer);
3492           return boolean_true_node;
3493         case RID_FALSE:
3494           cp_lexer_consume_token (parser->lexer);
3495           return boolean_false_node;
3496
3497           /* The `__null' literal.  */
3498         case RID_NULL:
3499           cp_lexer_consume_token (parser->lexer);
3500           return null_node;
3501
3502           /* The `nullptr' literal.  */
3503         case RID_NULLPTR:
3504           cp_lexer_consume_token (parser->lexer);
3505           return nullptr_node;
3506
3507           /* Recognize the `this' keyword.  */
3508         case RID_THIS:
3509           cp_lexer_consume_token (parser->lexer);
3510           if (parser->local_variables_forbidden_p)
3511             {
3512               error_at (token->location,
3513                         "%<this%> may not be used in this context");
3514               return error_mark_node;
3515             }
3516           /* Pointers cannot appear in constant-expressions.  */
3517           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
3518             return error_mark_node;
3519           return finish_this_expr ();
3520
3521           /* The `operator' keyword can be the beginning of an
3522              id-expression.  */
3523         case RID_OPERATOR:
3524           goto id_expression;
3525
3526         case RID_FUNCTION_NAME:
3527         case RID_PRETTY_FUNCTION_NAME:
3528         case RID_C99_FUNCTION_NAME:
3529           {
3530             non_integral_constant name;
3531
3532             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
3533                __func__ are the names of variables -- but they are
3534                treated specially.  Therefore, they are handled here,
3535                rather than relying on the generic id-expression logic
3536                below.  Grammatically, these names are id-expressions.
3537
3538                Consume the token.  */
3539             token = cp_lexer_consume_token (parser->lexer);
3540
3541             switch (token->keyword)
3542               {
3543               case RID_FUNCTION_NAME:
3544                 name = NIC_FUNC_NAME;
3545                 break;
3546               case RID_PRETTY_FUNCTION_NAME:
3547                 name = NIC_PRETTY_FUNC;
3548                 break;
3549               case RID_C99_FUNCTION_NAME:
3550                 name = NIC_C99_FUNC;
3551                 break;
3552               default:
3553                 gcc_unreachable ();
3554               }
3555
3556             if (cp_parser_non_integral_constant_expression (parser, name))
3557               return error_mark_node;
3558
3559             /* Look up the name.  */
3560             return finish_fname (token->u.value);
3561           }
3562
3563         case RID_VA_ARG:
3564           {
3565             tree expression;
3566             tree type;
3567
3568             /* The `__builtin_va_arg' construct is used to handle
3569                `va_arg'.  Consume the `__builtin_va_arg' token.  */
3570             cp_lexer_consume_token (parser->lexer);
3571             /* Look for the opening `('.  */
3572             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
3573             /* Now, parse the assignment-expression.  */
3574             expression = cp_parser_assignment_expression (parser,
3575                                                           /*cast_p=*/false, NULL);
3576             /* Look for the `,'.  */
3577             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
3578             /* Parse the type-id.  */
3579             type = cp_parser_type_id (parser);
3580             /* Look for the closing `)'.  */
3581             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
3582             /* Using `va_arg' in a constant-expression is not
3583                allowed.  */
3584             if (cp_parser_non_integral_constant_expression (parser,
3585                                                             NIC_VA_ARG))
3586               return error_mark_node;
3587             return build_x_va_arg (expression, type);
3588           }
3589
3590         case RID_OFFSETOF:
3591           return cp_parser_builtin_offsetof (parser);
3592
3593         case RID_HAS_NOTHROW_ASSIGN:
3594         case RID_HAS_NOTHROW_CONSTRUCTOR:
3595         case RID_HAS_NOTHROW_COPY:        
3596         case RID_HAS_TRIVIAL_ASSIGN:
3597         case RID_HAS_TRIVIAL_CONSTRUCTOR:
3598         case RID_HAS_TRIVIAL_COPY:        
3599         case RID_HAS_TRIVIAL_DESTRUCTOR:
3600         case RID_HAS_VIRTUAL_DESTRUCTOR:
3601         case RID_IS_ABSTRACT:
3602         case RID_IS_BASE_OF:
3603         case RID_IS_CLASS:
3604         case RID_IS_CONVERTIBLE_TO:
3605         case RID_IS_EMPTY:
3606         case RID_IS_ENUM:
3607         case RID_IS_LITERAL_TYPE:
3608         case RID_IS_POD:
3609         case RID_IS_POLYMORPHIC:
3610         case RID_IS_STD_LAYOUT:
3611         case RID_IS_TRIVIAL:
3612         case RID_IS_UNION:
3613           return cp_parser_trait_expr (parser, token->keyword);
3614
3615         /* Objective-C++ expressions.  */
3616         case RID_AT_ENCODE:
3617         case RID_AT_PROTOCOL:
3618         case RID_AT_SELECTOR:
3619           return cp_parser_objc_expression (parser);
3620
3621         case RID_TEMPLATE:
3622           if (parser->in_function_body
3623               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
3624                   == CPP_LESS))
3625             {
3626               error_at (token->location,
3627                         "a template declaration cannot appear at block scope");
3628               cp_parser_skip_to_end_of_block_or_statement (parser);
3629               return error_mark_node;
3630             }
3631         default:
3632           cp_parser_error (parser, "expected primary-expression");
3633           return error_mark_node;
3634         }
3635
3636       /* An id-expression can start with either an identifier, a
3637          `::' as the beginning of a qualified-id, or the "operator"
3638          keyword.  */
3639     case CPP_NAME:
3640     case CPP_SCOPE:
3641     case CPP_TEMPLATE_ID:
3642     case CPP_NESTED_NAME_SPECIFIER:
3643       {
3644         tree id_expression;
3645         tree decl;
3646         const char *error_msg;
3647         bool template_p;
3648         bool done;
3649         cp_token *id_expr_token;
3650
3651       id_expression:
3652         /* Parse the id-expression.  */
3653         id_expression
3654           = cp_parser_id_expression (parser,
3655                                      /*template_keyword_p=*/false,
3656                                      /*check_dependency_p=*/true,
3657                                      &template_p,
3658                                      /*declarator_p=*/false,
3659                                      /*optional_p=*/false);
3660         if (id_expression == error_mark_node)
3661           return error_mark_node;
3662         id_expr_token = token;
3663         token = cp_lexer_peek_token (parser->lexer);
3664         done = (token->type != CPP_OPEN_SQUARE
3665                 && token->type != CPP_OPEN_PAREN
3666                 && token->type != CPP_DOT
3667                 && token->type != CPP_DEREF
3668                 && token->type != CPP_PLUS_PLUS
3669                 && token->type != CPP_MINUS_MINUS);
3670         /* If we have a template-id, then no further lookup is
3671            required.  If the template-id was for a template-class, we
3672            will sometimes have a TYPE_DECL at this point.  */
3673         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
3674                  || TREE_CODE (id_expression) == TYPE_DECL)
3675           decl = id_expression;
3676         /* Look up the name.  */
3677         else
3678           {
3679             tree ambiguous_decls;
3680
3681             /* If we already know that this lookup is ambiguous, then
3682                we've already issued an error message; there's no reason
3683                to check again.  */
3684             if (id_expr_token->type == CPP_NAME
3685                 && id_expr_token->ambiguous_p)
3686               {
3687                 cp_parser_simulate_error (parser);
3688                 return error_mark_node;
3689               }
3690
3691             decl = cp_parser_lookup_name (parser, id_expression,
3692                                           none_type,
3693                                           template_p,
3694                                           /*is_namespace=*/false,
3695                                           /*check_dependency=*/true,
3696                                           &ambiguous_decls,
3697                                           id_expr_token->location);
3698             /* If the lookup was ambiguous, an error will already have
3699                been issued.  */
3700             if (ambiguous_decls)
3701               return error_mark_node;
3702
3703             /* In Objective-C++, we may have an Objective-C 2.0
3704                dot-syntax for classes here.  */
3705             if (c_dialect_objc ()
3706                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
3707                 && TREE_CODE (decl) == TYPE_DECL
3708                 && objc_is_class_name (decl))
3709               {
3710                 tree component;
3711                 cp_lexer_consume_token (parser->lexer);
3712                 component = cp_parser_identifier (parser);
3713                 if (component == error_mark_node)
3714                   return error_mark_node;
3715
3716                 return objc_build_class_component_ref (id_expression, component);
3717               }
3718
3719             /* In Objective-C++, an instance variable (ivar) may be preferred
3720                to whatever cp_parser_lookup_name() found.  */
3721             decl = objc_lookup_ivar (decl, id_expression);
3722
3723             /* If name lookup gives us a SCOPE_REF, then the
3724                qualifying scope was dependent.  */
3725             if (TREE_CODE (decl) == SCOPE_REF)
3726               {
3727                 /* At this point, we do not know if DECL is a valid
3728                    integral constant expression.  We assume that it is
3729                    in fact such an expression, so that code like:
3730
3731                       template <int N> struct A {
3732                         int a[B<N>::i];
3733                       };
3734                      
3735                    is accepted.  At template-instantiation time, we
3736                    will check that B<N>::i is actually a constant.  */
3737                 return decl;
3738               }
3739             /* Check to see if DECL is a local variable in a context
3740                where that is forbidden.  */
3741             if (parser->local_variables_forbidden_p
3742                 && local_variable_p (decl))
3743               {
3744                 /* It might be that we only found DECL because we are
3745                    trying to be generous with pre-ISO scoping rules.
3746                    For example, consider:
3747
3748                      int i;
3749                      void g() {
3750                        for (int i = 0; i < 10; ++i) {}
3751                        extern void f(int j = i);
3752                      }
3753
3754                    Here, name look up will originally find the out
3755                    of scope `i'.  We need to issue a warning message,
3756                    but then use the global `i'.  */
3757                 decl = check_for_out_of_scope_variable (decl);
3758                 if (local_variable_p (decl))
3759                   {
3760                     error_at (id_expr_token->location,
3761                               "local variable %qD may not appear in this context",
3762                               decl);
3763                     return error_mark_node;
3764                   }
3765               }
3766           }
3767
3768         decl = (finish_id_expression
3769                 (id_expression, decl, parser->scope,
3770                  idk,
3771                  parser->integral_constant_expression_p,
3772                  parser->allow_non_integral_constant_expression_p,
3773                  &parser->non_integral_constant_expression_p,
3774                  template_p, done, address_p,
3775                  template_arg_p,
3776                  &error_msg,
3777                  id_expr_token->location));
3778         if (error_msg)
3779           cp_parser_error (parser, error_msg);
3780         return decl;
3781       }
3782
3783       /* Anything else is an error.  */
3784     default:
3785       cp_parser_error (parser, "expected primary-expression");
3786       return error_mark_node;
3787     }
3788 }
3789
3790 /* Parse an id-expression.
3791
3792    id-expression:
3793      unqualified-id
3794      qualified-id
3795
3796    qualified-id:
3797      :: [opt] nested-name-specifier template [opt] unqualified-id
3798      :: identifier
3799      :: operator-function-id
3800      :: template-id
3801
3802    Return a representation of the unqualified portion of the
3803    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
3804    a `::' or nested-name-specifier.
3805
3806    Often, if the id-expression was a qualified-id, the caller will
3807    want to make a SCOPE_REF to represent the qualified-id.  This
3808    function does not do this in order to avoid wastefully creating
3809    SCOPE_REFs when they are not required.
3810
3811    If TEMPLATE_KEYWORD_P is true, then we have just seen the
3812    `template' keyword.
3813
3814    If CHECK_DEPENDENCY_P is false, then names are looked up inside
3815    uninstantiated templates.
3816
3817    If *TEMPLATE_P is non-NULL, it is set to true iff the
3818    `template' keyword is used to explicitly indicate that the entity
3819    named is a template.
3820
3821    If DECLARATOR_P is true, the id-expression is appearing as part of
3822    a declarator, rather than as part of an expression.  */
3823
3824 static tree
3825 cp_parser_id_expression (cp_parser *parser,
3826                          bool template_keyword_p,
3827                          bool check_dependency_p,
3828                          bool *template_p,
3829                          bool declarator_p,
3830                          bool optional_p)
3831 {
3832   bool global_scope_p;
3833   bool nested_name_specifier_p;
3834
3835   /* Assume the `template' keyword was not used.  */
3836   if (template_p)
3837     *template_p = template_keyword_p;
3838
3839   /* Look for the optional `::' operator.  */
3840   global_scope_p
3841     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
3842        != NULL_TREE);
3843   /* Look for the optional nested-name-specifier.  */
3844   nested_name_specifier_p
3845     = (cp_parser_nested_name_specifier_opt (parser,
3846                                             /*typename_keyword_p=*/false,
3847                                             check_dependency_p,
3848                                             /*type_p=*/false,
3849                                             declarator_p)
3850        != NULL_TREE);
3851   /* If there is a nested-name-specifier, then we are looking at
3852      the first qualified-id production.  */
3853   if (nested_name_specifier_p)
3854     {
3855       tree saved_scope;
3856       tree saved_object_scope;
3857       tree saved_qualifying_scope;
3858       tree unqualified_id;
3859       bool is_template;
3860
3861       /* See if the next token is the `template' keyword.  */
3862       if (!template_p)
3863         template_p = &is_template;
3864       *template_p = cp_parser_optional_template_keyword (parser);
3865       /* Name lookup we do during the processing of the
3866          unqualified-id might obliterate SCOPE.  */
3867       saved_scope = parser->scope;
3868       saved_object_scope = parser->object_scope;
3869       saved_qualifying_scope = parser->qualifying_scope;
3870       /* Process the final unqualified-id.  */
3871       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
3872                                                  check_dependency_p,
3873                                                  declarator_p,
3874                                                  /*optional_p=*/false);
3875       /* Restore the SAVED_SCOPE for our caller.  */
3876       parser->scope = saved_scope;
3877       parser->object_scope = saved_object_scope;
3878       parser->qualifying_scope = saved_qualifying_scope;
3879
3880       return unqualified_id;
3881     }
3882   /* Otherwise, if we are in global scope, then we are looking at one
3883      of the other qualified-id productions.  */
3884   else if (global_scope_p)
3885     {
3886       cp_token *token;
3887       tree id;
3888
3889       /* Peek at the next token.  */
3890       token = cp_lexer_peek_token (parser->lexer);
3891
3892       /* If it's an identifier, and the next token is not a "<", then
3893          we can avoid the template-id case.  This is an optimization
3894          for this common case.  */
3895       if (token->type == CPP_NAME
3896           && !cp_parser_nth_token_starts_template_argument_list_p
3897                (parser, 2))
3898         return cp_parser_identifier (parser);
3899
3900       cp_parser_parse_tentatively (parser);
3901       /* Try a template-id.  */
3902       id = cp_parser_template_id (parser,
3903                                   /*template_keyword_p=*/false,
3904                                   /*check_dependency_p=*/true,
3905                                   declarator_p);
3906       /* If that worked, we're done.  */
3907       if (cp_parser_parse_definitely (parser))
3908         return id;
3909
3910       /* Peek at the next token.  (Changes in the token buffer may
3911          have invalidated the pointer obtained above.)  */
3912       token = cp_lexer_peek_token (parser->lexer);
3913
3914       switch (token->type)
3915         {
3916         case CPP_NAME:
3917           return cp_parser_identifier (parser);
3918
3919         case CPP_KEYWORD:
3920           if (token->keyword == RID_OPERATOR)
3921             return cp_parser_operator_function_id (parser);
3922           /* Fall through.  */
3923
3924         default:
3925           cp_parser_error (parser, "expected id-expression");
3926           return error_mark_node;
3927         }
3928     }
3929   else
3930     return cp_parser_unqualified_id (parser, template_keyword_p,
3931                                      /*check_dependency_p=*/true,
3932                                      declarator_p,
3933                                      optional_p);
3934 }
3935
3936 /* Parse an unqualified-id.
3937
3938    unqualified-id:
3939      identifier
3940      operator-function-id
3941      conversion-function-id
3942      ~ class-name
3943      template-id
3944
3945    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
3946    keyword, in a construct like `A::template ...'.
3947
3948    Returns a representation of unqualified-id.  For the `identifier'
3949    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
3950    production a BIT_NOT_EXPR is returned; the operand of the
3951    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
3952    other productions, see the documentation accompanying the
3953    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
3954    names are looked up in uninstantiated templates.  If DECLARATOR_P
3955    is true, the unqualified-id is appearing as part of a declarator,
3956    rather than as part of an expression.  */
3957
3958 static tree
3959 cp_parser_unqualified_id (cp_parser* parser,
3960                           bool template_keyword_p,
3961                           bool check_dependency_p,
3962                           bool declarator_p,
3963                           bool optional_p)
3964 {
3965   cp_token *token;
3966
3967   /* Peek at the next token.  */
3968   token = cp_lexer_peek_token (parser->lexer);
3969
3970   switch (token->type)
3971     {
3972     case CPP_NAME:
3973       {
3974         tree id;
3975
3976         /* We don't know yet whether or not this will be a
3977            template-id.  */
3978         cp_parser_parse_tentatively (parser);
3979         /* Try a template-id.  */
3980         id = cp_parser_template_id (parser, template_keyword_p,
3981                                     check_dependency_p,
3982                                     declarator_p);
3983         /* If it worked, we're done.  */
3984         if (cp_parser_parse_definitely (parser))
3985           return id;
3986         /* Otherwise, it's an ordinary identifier.  */
3987         return cp_parser_identifier (parser);
3988       }
3989
3990     case CPP_TEMPLATE_ID:
3991       return cp_parser_template_id (parser, template_keyword_p,
3992                                     check_dependency_p,
3993                                     declarator_p);
3994
3995     case CPP_COMPL:
3996       {
3997         tree type_decl;
3998         tree qualifying_scope;
3999         tree object_scope;
4000         tree scope;
4001         bool done;
4002
4003         /* Consume the `~' token.  */
4004         cp_lexer_consume_token (parser->lexer);
4005         /* Parse the class-name.  The standard, as written, seems to
4006            say that:
4007
4008              template <typename T> struct S { ~S (); };
4009              template <typename T> S<T>::~S() {}
4010
4011            is invalid, since `~' must be followed by a class-name, but
4012            `S<T>' is dependent, and so not known to be a class.
4013            That's not right; we need to look in uninstantiated
4014            templates.  A further complication arises from:
4015
4016              template <typename T> void f(T t) {
4017                t.T::~T();
4018              }
4019
4020            Here, it is not possible to look up `T' in the scope of `T'
4021            itself.  We must look in both the current scope, and the
4022            scope of the containing complete expression.
4023
4024            Yet another issue is:
4025
4026              struct S {
4027                int S;
4028                ~S();
4029              };
4030
4031              S::~S() {}
4032
4033            The standard does not seem to say that the `S' in `~S'
4034            should refer to the type `S' and not the data member
4035            `S::S'.  */
4036
4037         /* DR 244 says that we look up the name after the "~" in the
4038            same scope as we looked up the qualifying name.  That idea
4039            isn't fully worked out; it's more complicated than that.  */
4040         scope = parser->scope;
4041         object_scope = parser->object_scope;
4042         qualifying_scope = parser->qualifying_scope;
4043
4044         /* Check for invalid scopes.  */
4045         if (scope == error_mark_node)
4046           {
4047             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4048               cp_lexer_consume_token (parser->lexer);
4049             return error_mark_node;
4050           }
4051         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4052           {
4053             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4054               error_at (token->location,
4055                         "scope %qT before %<~%> is not a class-name",
4056                         scope);
4057             cp_parser_simulate_error (parser);
4058             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4059               cp_lexer_consume_token (parser->lexer);
4060             return error_mark_node;
4061           }
4062         gcc_assert (!scope || TYPE_P (scope));
4063
4064         /* If the name is of the form "X::~X" it's OK even if X is a
4065            typedef.  */
4066         token = cp_lexer_peek_token (parser->lexer);
4067         if (scope
4068             && token->type == CPP_NAME
4069             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4070                 != CPP_LESS)
4071             && (token->u.value == TYPE_IDENTIFIER (scope)
4072                 || (CLASS_TYPE_P (scope)
4073                     && constructor_name_p (token->u.value, scope))))
4074           {
4075             cp_lexer_consume_token (parser->lexer);
4076             return build_nt (BIT_NOT_EXPR, scope);
4077           }
4078
4079         /* If there was an explicit qualification (S::~T), first look
4080            in the scope given by the qualification (i.e., S).
4081
4082            Note: in the calls to cp_parser_class_name below we pass
4083            typename_type so that lookup finds the injected-class-name
4084            rather than the constructor.  */
4085         done = false;
4086         type_decl = NULL_TREE;
4087         if (scope)
4088           {
4089             cp_parser_parse_tentatively (parser);
4090             type_decl = cp_parser_class_name (parser,
4091                                               /*typename_keyword_p=*/false,
4092                                               /*template_keyword_p=*/false,
4093                                               typename_type,
4094                                               /*check_dependency=*/false,
4095                                               /*class_head_p=*/false,
4096                                               declarator_p);
4097             if (cp_parser_parse_definitely (parser))
4098               done = true;
4099           }
4100         /* In "N::S::~S", look in "N" as well.  */
4101         if (!done && scope && qualifying_scope)
4102           {
4103             cp_parser_parse_tentatively (parser);
4104             parser->scope = qualifying_scope;
4105             parser->object_scope = NULL_TREE;
4106             parser->qualifying_scope = NULL_TREE;
4107             type_decl
4108               = cp_parser_class_name (parser,
4109                                       /*typename_keyword_p=*/false,
4110                                       /*template_keyword_p=*/false,
4111                                       typename_type,
4112                                       /*check_dependency=*/false,
4113                                       /*class_head_p=*/false,
4114                                       declarator_p);
4115             if (cp_parser_parse_definitely (parser))
4116               done = true;
4117           }
4118         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4119         else if (!done && object_scope)
4120           {
4121             cp_parser_parse_tentatively (parser);
4122             parser->scope = object_scope;
4123             parser->object_scope = NULL_TREE;
4124             parser->qualifying_scope = NULL_TREE;
4125             type_decl
4126               = cp_parser_class_name (parser,
4127                                       /*typename_keyword_p=*/false,
4128                                       /*template_keyword_p=*/false,
4129                                       typename_type,
4130                                       /*check_dependency=*/false,
4131                                       /*class_head_p=*/false,
4132                                       declarator_p);
4133             if (cp_parser_parse_definitely (parser))
4134               done = true;
4135           }
4136         /* Look in the surrounding context.  */
4137         if (!done)
4138           {
4139             parser->scope = NULL_TREE;
4140             parser->object_scope = NULL_TREE;
4141             parser->qualifying_scope = NULL_TREE;
4142             if (processing_template_decl)
4143               cp_parser_parse_tentatively (parser);
4144             type_decl
4145               = cp_parser_class_name (parser,
4146                                       /*typename_keyword_p=*/false,
4147                                       /*template_keyword_p=*/false,
4148                                       typename_type,
4149                                       /*check_dependency=*/false,
4150                                       /*class_head_p=*/false,
4151                                       declarator_p);
4152             if (processing_template_decl
4153                 && ! cp_parser_parse_definitely (parser))
4154               {
4155                 /* We couldn't find a type with this name, so just accept
4156                    it and check for a match at instantiation time.  */
4157                 type_decl = cp_parser_identifier (parser);
4158                 if (type_decl != error_mark_node)
4159                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4160                 return type_decl;
4161               }
4162           }
4163         /* If an error occurred, assume that the name of the
4164            destructor is the same as the name of the qualifying
4165            class.  That allows us to keep parsing after running
4166            into ill-formed destructor names.  */
4167         if (type_decl == error_mark_node && scope)
4168           return build_nt (BIT_NOT_EXPR, scope);
4169         else if (type_decl == error_mark_node)
4170           return error_mark_node;
4171
4172         /* Check that destructor name and scope match.  */
4173         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4174           {
4175             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4176               error_at (token->location,
4177                         "declaration of %<~%T%> as member of %qT",
4178                         type_decl, scope);
4179             cp_parser_simulate_error (parser);
4180             return error_mark_node;
4181           }
4182
4183         /* [class.dtor]
4184
4185            A typedef-name that names a class shall not be used as the
4186            identifier in the declarator for a destructor declaration.  */
4187         if (declarator_p
4188             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4189             && !DECL_SELF_REFERENCE_P (type_decl)
4190             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4191           error_at (token->location,
4192                     "typedef-name %qD used as destructor declarator",
4193                     type_decl);
4194
4195         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4196       }
4197
4198     case CPP_KEYWORD:
4199       if (token->keyword == RID_OPERATOR)
4200         {
4201           tree id;
4202
4203           /* This could be a template-id, so we try that first.  */
4204           cp_parser_parse_tentatively (parser);
4205           /* Try a template-id.  */
4206           id = cp_parser_template_id (parser, template_keyword_p,
4207                                       /*check_dependency_p=*/true,
4208                                       declarator_p);
4209           /* If that worked, we're done.  */
4210           if (cp_parser_parse_definitely (parser))
4211             return id;
4212           /* We still don't know whether we're looking at an
4213              operator-function-id or a conversion-function-id.  */
4214           cp_parser_parse_tentatively (parser);
4215           /* Try an operator-function-id.  */
4216           id = cp_parser_operator_function_id (parser);
4217           /* If that didn't work, try a conversion-function-id.  */
4218           if (!cp_parser_parse_definitely (parser))
4219             id = cp_parser_conversion_function_id (parser);
4220
4221           return id;
4222         }
4223       /* Fall through.  */
4224
4225     default:
4226       if (optional_p)
4227         return NULL_TREE;
4228       cp_parser_error (parser, "expected unqualified-id");
4229       return error_mark_node;
4230     }
4231 }
4232
4233 /* Parse an (optional) nested-name-specifier.
4234
4235    nested-name-specifier: [C++98]
4236      class-or-namespace-name :: nested-name-specifier [opt]
4237      class-or-namespace-name :: template nested-name-specifier [opt]
4238
4239    nested-name-specifier: [C++0x]
4240      type-name ::
4241      namespace-name ::
4242      nested-name-specifier identifier ::
4243      nested-name-specifier template [opt] simple-template-id ::
4244
4245    PARSER->SCOPE should be set appropriately before this function is
4246    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4247    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4248    in name lookups.
4249
4250    Sets PARSER->SCOPE to the class (TYPE) or namespace
4251    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4252    it unchanged if there is no nested-name-specifier.  Returns the new
4253    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4254
4255    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4256    part of a declaration and/or decl-specifier.  */
4257
4258 static tree
4259 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4260                                      bool typename_keyword_p,
4261                                      bool check_dependency_p,
4262                                      bool type_p,
4263                                      bool is_declaration)
4264 {
4265   bool success = false;
4266   cp_token_position start = 0;
4267   cp_token *token;
4268
4269   /* Remember where the nested-name-specifier starts.  */
4270   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4271     {
4272       start = cp_lexer_token_position (parser->lexer, false);
4273       push_deferring_access_checks (dk_deferred);
4274     }
4275
4276   while (true)
4277     {
4278       tree new_scope;
4279       tree old_scope;
4280       tree saved_qualifying_scope;
4281       bool template_keyword_p;
4282
4283       /* Spot cases that cannot be the beginning of a
4284          nested-name-specifier.  */
4285       token = cp_lexer_peek_token (parser->lexer);
4286
4287       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4288          the already parsed nested-name-specifier.  */
4289       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4290         {
4291           /* Grab the nested-name-specifier and continue the loop.  */
4292           cp_parser_pre_parsed_nested_name_specifier (parser);
4293           /* If we originally encountered this nested-name-specifier
4294              with IS_DECLARATION set to false, we will not have
4295              resolved TYPENAME_TYPEs, so we must do so here.  */
4296           if (is_declaration
4297               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4298             {
4299               new_scope = resolve_typename_type (parser->scope,
4300                                                  /*only_current_p=*/false);
4301               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4302                 parser->scope = new_scope;
4303             }
4304           success = true;
4305           continue;
4306         }
4307
4308       /* Spot cases that cannot be the beginning of a
4309          nested-name-specifier.  On the second and subsequent times
4310          through the loop, we look for the `template' keyword.  */
4311       if (success && token->keyword == RID_TEMPLATE)
4312         ;
4313       /* A template-id can start a nested-name-specifier.  */
4314       else if (token->type == CPP_TEMPLATE_ID)
4315         ;
4316       else
4317         {
4318           /* If the next token is not an identifier, then it is
4319              definitely not a type-name or namespace-name.  */
4320           if (token->type != CPP_NAME)
4321             break;
4322           /* If the following token is neither a `<' (to begin a
4323              template-id), nor a `::', then we are not looking at a
4324              nested-name-specifier.  */
4325           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4326
4327           if (token->type == CPP_COLON
4328               && parser->colon_corrects_to_scope_p
4329               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4330             {
4331               error_at (token->location,
4332                         "found %<:%> in nested-name-specifier, expected %<::%>");
4333               token->type = CPP_SCOPE;
4334             }
4335
4336           if (token->type != CPP_SCOPE
4337               && !cp_parser_nth_token_starts_template_argument_list_p
4338                   (parser, 2))
4339             break;
4340         }
4341
4342       /* The nested-name-specifier is optional, so we parse
4343          tentatively.  */
4344       cp_parser_parse_tentatively (parser);
4345
4346       /* Look for the optional `template' keyword, if this isn't the
4347          first time through the loop.  */
4348       if (success)
4349         template_keyword_p = cp_parser_optional_template_keyword (parser);
4350       else
4351         template_keyword_p = false;
4352
4353       /* Save the old scope since the name lookup we are about to do
4354          might destroy it.  */
4355       old_scope = parser->scope;
4356       saved_qualifying_scope = parser->qualifying_scope;
4357       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4358          look up names in "X<T>::I" in order to determine that "Y" is
4359          a template.  So, if we have a typename at this point, we make
4360          an effort to look through it.  */
4361       if (is_declaration
4362           && !typename_keyword_p
4363           && parser->scope
4364           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4365         parser->scope = resolve_typename_type (parser->scope,
4366                                                /*only_current_p=*/false);
4367       /* Parse the qualifying entity.  */
4368       new_scope
4369         = cp_parser_qualifying_entity (parser,
4370                                        typename_keyword_p,
4371                                        template_keyword_p,
4372                                        check_dependency_p,
4373                                        type_p,
4374                                        is_declaration);
4375       /* Look for the `::' token.  */
4376       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4377
4378       /* If we found what we wanted, we keep going; otherwise, we're
4379          done.  */
4380       if (!cp_parser_parse_definitely (parser))
4381         {
4382           bool error_p = false;
4383
4384           /* Restore the OLD_SCOPE since it was valid before the
4385              failed attempt at finding the last
4386              class-or-namespace-name.  */
4387           parser->scope = old_scope;
4388           parser->qualifying_scope = saved_qualifying_scope;
4389           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4390             break;
4391           /* If the next token is an identifier, and the one after
4392              that is a `::', then any valid interpretation would have
4393              found a class-or-namespace-name.  */
4394           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4395                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4396                      == CPP_SCOPE)
4397                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4398                      != CPP_COMPL))
4399             {
4400               token = cp_lexer_consume_token (parser->lexer);
4401               if (!error_p)
4402                 {
4403                   if (!token->ambiguous_p)
4404                     {
4405                       tree decl;
4406                       tree ambiguous_decls;
4407
4408                       decl = cp_parser_lookup_name (parser, token->u.value,
4409                                                     none_type,
4410                                                     /*is_template=*/false,
4411                                                     /*is_namespace=*/false,
4412                                                     /*check_dependency=*/true,
4413                                                     &ambiguous_decls,
4414                                                     token->location);
4415                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4416                         error_at (token->location,
4417                                   "%qD used without template parameters",
4418                                   decl);
4419                       else if (ambiguous_decls)
4420                         {
4421                           error_at (token->location,
4422                                     "reference to %qD is ambiguous",
4423                                     token->u.value);
4424                           print_candidates (ambiguous_decls);
4425                           decl = error_mark_node;
4426                         }
4427                       else
4428                         {
4429                           if (cxx_dialect != cxx98)
4430                             cp_parser_name_lookup_error
4431                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4432                              token->location);
4433                           else
4434                             cp_parser_name_lookup_error
4435                             (parser, token->u.value, decl, NLE_CXX98,
4436                              token->location);
4437                         }
4438                     }
4439                   parser->scope = error_mark_node;
4440                   error_p = true;
4441                   /* Treat this as a successful nested-name-specifier
4442                      due to:
4443
4444                      [basic.lookup.qual]
4445
4446                      If the name found is not a class-name (clause
4447                      _class_) or namespace-name (_namespace.def_), the
4448                      program is ill-formed.  */
4449                   success = true;
4450                 }
4451               cp_lexer_consume_token (parser->lexer);
4452             }
4453           break;
4454         }
4455       /* We've found one valid nested-name-specifier.  */
4456       success = true;
4457       /* Name lookup always gives us a DECL.  */
4458       if (TREE_CODE (new_scope) == TYPE_DECL)
4459         new_scope = TREE_TYPE (new_scope);
4460       /* Uses of "template" must be followed by actual templates.  */
4461       if (template_keyword_p
4462           && !(CLASS_TYPE_P (new_scope)
4463                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4464                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4465                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4466           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4467                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4468                    == TEMPLATE_ID_EXPR)))
4469         permerror (input_location, TYPE_P (new_scope)
4470                    ? "%qT is not a template"
4471                    : "%qD is not a template",
4472                    new_scope);
4473       /* If it is a class scope, try to complete it; we are about to
4474          be looking up names inside the class.  */
4475       if (TYPE_P (new_scope)
4476           /* Since checking types for dependency can be expensive,
4477              avoid doing it if the type is already complete.  */
4478           && !COMPLETE_TYPE_P (new_scope)
4479           /* Do not try to complete dependent types.  */
4480           && !dependent_type_p (new_scope))
4481         {
4482           new_scope = complete_type (new_scope);
4483           /* If it is a typedef to current class, use the current
4484              class instead, as the typedef won't have any names inside
4485              it yet.  */
4486           if (!COMPLETE_TYPE_P (new_scope)
4487               && currently_open_class (new_scope))
4488             new_scope = TYPE_MAIN_VARIANT (new_scope);
4489         }
4490       /* Make sure we look in the right scope the next time through
4491          the loop.  */
4492       parser->scope = new_scope;
4493     }
4494
4495   /* If parsing tentatively, replace the sequence of tokens that makes
4496      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4497      token.  That way, should we re-parse the token stream, we will
4498      not have to repeat the effort required to do the parse, nor will
4499      we issue duplicate error messages.  */
4500   if (success && start)
4501     {
4502       cp_token *token;
4503
4504       token = cp_lexer_token_at (parser->lexer, start);
4505       /* Reset the contents of the START token.  */
4506       token->type = CPP_NESTED_NAME_SPECIFIER;
4507       /* Retrieve any deferred checks.  Do not pop this access checks yet
4508          so the memory will not be reclaimed during token replacing below.  */
4509       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4510       token->u.tree_check_value->value = parser->scope;
4511       token->u.tree_check_value->checks = get_deferred_access_checks ();
4512       token->u.tree_check_value->qualifying_scope =
4513         parser->qualifying_scope;
4514       token->keyword = RID_MAX;
4515
4516       /* Purge all subsequent tokens.  */
4517       cp_lexer_purge_tokens_after (parser->lexer, start);
4518     }
4519
4520   if (start)
4521     pop_to_parent_deferring_access_checks ();
4522
4523   return success ? parser->scope : NULL_TREE;
4524 }
4525
4526 /* Parse a nested-name-specifier.  See
4527    cp_parser_nested_name_specifier_opt for details.  This function
4528    behaves identically, except that it will an issue an error if no
4529    nested-name-specifier is present.  */
4530
4531 static tree
4532 cp_parser_nested_name_specifier (cp_parser *parser,
4533                                  bool typename_keyword_p,
4534                                  bool check_dependency_p,
4535                                  bool type_p,
4536                                  bool is_declaration)
4537 {
4538   tree scope;
4539
4540   /* Look for the nested-name-specifier.  */
4541   scope = cp_parser_nested_name_specifier_opt (parser,
4542                                                typename_keyword_p,
4543                                                check_dependency_p,
4544                                                type_p,
4545                                                is_declaration);
4546   /* If it was not present, issue an error message.  */
4547   if (!scope)
4548     {
4549       cp_parser_error (parser, "expected nested-name-specifier");
4550       parser->scope = NULL_TREE;
4551     }
4552
4553   return scope;
4554 }
4555
4556 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4557    this is either a class-name or a namespace-name (which corresponds
4558    to the class-or-namespace-name production in the grammar). For
4559    C++0x, it can also be a type-name that refers to an enumeration
4560    type.
4561
4562    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4563    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4564    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4565    TYPE_P is TRUE iff the next name should be taken as a class-name,
4566    even the same name is declared to be another entity in the same
4567    scope.
4568
4569    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4570    specified by the class-or-namespace-name.  If neither is found the
4571    ERROR_MARK_NODE is returned.  */
4572
4573 static tree
4574 cp_parser_qualifying_entity (cp_parser *parser,
4575                              bool typename_keyword_p,
4576                              bool template_keyword_p,
4577                              bool check_dependency_p,
4578                              bool type_p,
4579                              bool is_declaration)
4580 {
4581   tree saved_scope;
4582   tree saved_qualifying_scope;
4583   tree saved_object_scope;
4584   tree scope;
4585   bool only_class_p;
4586   bool successful_parse_p;
4587
4588   /* Before we try to parse the class-name, we must save away the
4589      current PARSER->SCOPE since cp_parser_class_name will destroy
4590      it.  */
4591   saved_scope = parser->scope;
4592   saved_qualifying_scope = parser->qualifying_scope;
4593   saved_object_scope = parser->object_scope;
4594   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4595      there is no need to look for a namespace-name.  */
4596   only_class_p = template_keyword_p 
4597     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4598   if (!only_class_p)
4599     cp_parser_parse_tentatively (parser);
4600   scope = cp_parser_class_name (parser,
4601                                 typename_keyword_p,
4602                                 template_keyword_p,
4603                                 type_p ? class_type : none_type,
4604                                 check_dependency_p,
4605                                 /*class_head_p=*/false,
4606                                 is_declaration);
4607   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4608   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4609   if (!only_class_p 
4610       && cxx_dialect != cxx98
4611       && !successful_parse_p)
4612     {
4613       /* Restore the saved scope.  */
4614       parser->scope = saved_scope;
4615       parser->qualifying_scope = saved_qualifying_scope;
4616       parser->object_scope = saved_object_scope;
4617
4618       /* Parse tentatively.  */
4619       cp_parser_parse_tentatively (parser);
4620      
4621       /* Parse a typedef-name or enum-name.  */
4622       scope = cp_parser_nonclass_name (parser);
4623
4624       /* "If the name found does not designate a namespace or a class,
4625          enumeration, or dependent type, the program is ill-formed."
4626
4627          We cover classes and dependent types above and namespaces below,
4628          so this code is only looking for enums.  */
4629       if (!scope || TREE_CODE (scope) != TYPE_DECL
4630           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4631         cp_parser_simulate_error (parser);
4632
4633       successful_parse_p = cp_parser_parse_definitely (parser);
4634     }
4635   /* If that didn't work, try for a namespace-name.  */
4636   if (!only_class_p && !successful_parse_p)
4637     {
4638       /* Restore the saved scope.  */
4639       parser->scope = saved_scope;
4640       parser->qualifying_scope = saved_qualifying_scope;
4641       parser->object_scope = saved_object_scope;
4642       /* If we are not looking at an identifier followed by the scope
4643          resolution operator, then this is not part of a
4644          nested-name-specifier.  (Note that this function is only used
4645          to parse the components of a nested-name-specifier.)  */
4646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4647           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4648         return error_mark_node;
4649       scope = cp_parser_namespace_name (parser);
4650     }
4651
4652   return scope;
4653 }
4654
4655 /* Parse a postfix-expression.
4656
4657    postfix-expression:
4658      primary-expression
4659      postfix-expression [ expression ]
4660      postfix-expression ( expression-list [opt] )
4661      simple-type-specifier ( expression-list [opt] )
4662      typename :: [opt] nested-name-specifier identifier
4663        ( expression-list [opt] )
4664      typename :: [opt] nested-name-specifier template [opt] template-id
4665        ( expression-list [opt] )
4666      postfix-expression . template [opt] id-expression
4667      postfix-expression -> template [opt] id-expression
4668      postfix-expression . pseudo-destructor-name
4669      postfix-expression -> pseudo-destructor-name
4670      postfix-expression ++
4671      postfix-expression --
4672      dynamic_cast < type-id > ( expression )
4673      static_cast < type-id > ( expression )
4674      reinterpret_cast < type-id > ( expression )
4675      const_cast < type-id > ( expression )
4676      typeid ( expression )
4677      typeid ( type-id )
4678
4679    GNU Extension:
4680
4681    postfix-expression:
4682      ( type-id ) { initializer-list , [opt] }
4683
4684    This extension is a GNU version of the C99 compound-literal
4685    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4686    but they are essentially the same concept.)
4687
4688    If ADDRESS_P is true, the postfix expression is the operand of the
4689    `&' operator.  CAST_P is true if this expression is the target of a
4690    cast.
4691
4692    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4693    class member access expressions [expr.ref].
4694
4695    Returns a representation of the expression.  */
4696
4697 static tree
4698 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4699                               bool member_access_only_p,
4700                               cp_id_kind * pidk_return)
4701 {
4702   cp_token *token;
4703   enum rid keyword;
4704   cp_id_kind idk = CP_ID_KIND_NONE;
4705   tree postfix_expression = NULL_TREE;
4706   bool is_member_access = false;
4707
4708   /* Peek at the next token.  */
4709   token = cp_lexer_peek_token (parser->lexer);
4710   /* Some of the productions are determined by keywords.  */
4711   keyword = token->keyword;
4712   switch (keyword)
4713     {
4714     case RID_DYNCAST:
4715     case RID_STATCAST:
4716     case RID_REINTCAST:
4717     case RID_CONSTCAST:
4718       {
4719         tree type;
4720         tree expression;
4721         const char *saved_message;
4722
4723         /* All of these can be handled in the same way from the point
4724            of view of parsing.  Begin by consuming the token
4725            identifying the cast.  */
4726         cp_lexer_consume_token (parser->lexer);
4727
4728         /* New types cannot be defined in the cast.  */
4729         saved_message = parser->type_definition_forbidden_message;
4730         parser->type_definition_forbidden_message
4731           = G_("types may not be defined in casts");
4732
4733         /* Look for the opening `<'.  */
4734         cp_parser_require (parser, CPP_LESS, RT_LESS);
4735         /* Parse the type to which we are casting.  */
4736         type = cp_parser_type_id (parser);
4737         /* Look for the closing `>'.  */
4738         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4739         /* Restore the old message.  */
4740         parser->type_definition_forbidden_message = saved_message;
4741
4742         /* And the expression which is being cast.  */
4743         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4744         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4745         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4746
4747         /* Only type conversions to integral or enumeration types
4748            can be used in constant-expressions.  */
4749         if (!cast_valid_in_integral_constant_expression_p (type)
4750             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4751           return error_mark_node;
4752
4753         switch (keyword)
4754           {
4755           case RID_DYNCAST:
4756             postfix_expression
4757               = build_dynamic_cast (type, expression, tf_warning_or_error);
4758             break;
4759           case RID_STATCAST:
4760             postfix_expression
4761               = build_static_cast (type, expression, tf_warning_or_error);
4762             break;
4763           case RID_REINTCAST:
4764             postfix_expression
4765               = build_reinterpret_cast (type, expression, 
4766                                         tf_warning_or_error);
4767             break;
4768           case RID_CONSTCAST:
4769             postfix_expression
4770               = build_const_cast (type, expression, tf_warning_or_error);
4771             break;
4772           default:
4773             gcc_unreachable ();
4774           }
4775       }
4776       break;
4777
4778     case RID_TYPEID:
4779       {
4780         tree type;
4781         const char *saved_message;
4782         bool saved_in_type_id_in_expr_p;
4783
4784         /* Consume the `typeid' token.  */
4785         cp_lexer_consume_token (parser->lexer);
4786         /* Look for the `(' token.  */
4787         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4788         /* Types cannot be defined in a `typeid' expression.  */
4789         saved_message = parser->type_definition_forbidden_message;
4790         parser->type_definition_forbidden_message
4791           = G_("types may not be defined in a %<typeid%> expression");
4792         /* We can't be sure yet whether we're looking at a type-id or an
4793            expression.  */
4794         cp_parser_parse_tentatively (parser);
4795         /* Try a type-id first.  */
4796         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4797         parser->in_type_id_in_expr_p = true;
4798         type = cp_parser_type_id (parser);
4799         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4800         /* Look for the `)' token.  Otherwise, we can't be sure that
4801            we're not looking at an expression: consider `typeid (int
4802            (3))', for example.  */
4803         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4804         /* If all went well, simply lookup the type-id.  */
4805         if (cp_parser_parse_definitely (parser))
4806           postfix_expression = get_typeid (type);
4807         /* Otherwise, fall back to the expression variant.  */
4808         else
4809           {
4810             tree expression;
4811
4812             /* Look for an expression.  */
4813             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4814             /* Compute its typeid.  */
4815             postfix_expression = build_typeid (expression);
4816             /* Look for the `)' token.  */
4817             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4818           }
4819         /* Restore the saved message.  */
4820         parser->type_definition_forbidden_message = saved_message;
4821         /* `typeid' may not appear in an integral constant expression.  */
4822         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4823           return error_mark_node;
4824       }
4825       break;
4826
4827     case RID_TYPENAME:
4828       {
4829         tree type;
4830         /* The syntax permitted here is the same permitted for an
4831            elaborated-type-specifier.  */
4832         type = cp_parser_elaborated_type_specifier (parser,
4833                                                     /*is_friend=*/false,
4834                                                     /*is_declaration=*/false);
4835         postfix_expression = cp_parser_functional_cast (parser, type);
4836       }
4837       break;
4838
4839     default:
4840       {
4841         tree type;
4842
4843         /* If the next thing is a simple-type-specifier, we may be
4844            looking at a functional cast.  We could also be looking at
4845            an id-expression.  So, we try the functional cast, and if
4846            that doesn't work we fall back to the primary-expression.  */
4847         cp_parser_parse_tentatively (parser);
4848         /* Look for the simple-type-specifier.  */
4849         type = cp_parser_simple_type_specifier (parser,
4850                                                 /*decl_specs=*/NULL,
4851                                                 CP_PARSER_FLAGS_NONE);
4852         /* Parse the cast itself.  */
4853         if (!cp_parser_error_occurred (parser))
4854           postfix_expression
4855             = cp_parser_functional_cast (parser, type);
4856         /* If that worked, we're done.  */
4857         if (cp_parser_parse_definitely (parser))
4858           break;
4859
4860         /* If the functional-cast didn't work out, try a
4861            compound-literal.  */
4862         if (cp_parser_allow_gnu_extensions_p (parser)
4863             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4864           {
4865             VEC(constructor_elt,gc) *initializer_list = NULL;
4866             bool saved_in_type_id_in_expr_p;
4867
4868             cp_parser_parse_tentatively (parser);
4869             /* Consume the `('.  */
4870             cp_lexer_consume_token (parser->lexer);
4871             /* Parse the type.  */
4872             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4873             parser->in_type_id_in_expr_p = true;
4874             type = cp_parser_type_id (parser);
4875             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4876             /* Look for the `)'.  */
4877             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4878             /* Look for the `{'.  */
4879             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4880             /* If things aren't going well, there's no need to
4881                keep going.  */
4882             if (!cp_parser_error_occurred (parser))
4883               {
4884                 bool non_constant_p;
4885                 /* Parse the initializer-list.  */
4886                 initializer_list
4887                   = cp_parser_initializer_list (parser, &non_constant_p);
4888                 /* Allow a trailing `,'.  */
4889                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4890                   cp_lexer_consume_token (parser->lexer);
4891                 /* Look for the final `}'.  */
4892                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4893               }
4894             /* If that worked, we're definitely looking at a
4895                compound-literal expression.  */
4896             if (cp_parser_parse_definitely (parser))
4897               {
4898                 /* Warn the user that a compound literal is not
4899                    allowed in standard C++.  */
4900                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4901                 /* For simplicity, we disallow compound literals in
4902                    constant-expressions.  We could
4903                    allow compound literals of integer type, whose
4904                    initializer was a constant, in constant
4905                    expressions.  Permitting that usage, as a further
4906                    extension, would not change the meaning of any
4907                    currently accepted programs.  (Of course, as
4908                    compound literals are not part of ISO C++, the
4909                    standard has nothing to say.)  */
4910                 if (cp_parser_non_integral_constant_expression (parser,
4911                                                                 NIC_NCC))
4912                   {
4913                     postfix_expression = error_mark_node;
4914                     break;
4915                   }
4916                 /* Form the representation of the compound-literal.  */
4917                 postfix_expression
4918                   = (finish_compound_literal
4919                      (type, build_constructor (init_list_type_node,
4920                                                initializer_list),
4921                       tf_warning_or_error));
4922                 break;
4923               }
4924           }
4925
4926         /* It must be a primary-expression.  */
4927         postfix_expression
4928           = cp_parser_primary_expression (parser, address_p, cast_p,
4929                                           /*template_arg_p=*/false,
4930                                           &idk);
4931       }
4932       break;
4933     }
4934
4935   /* Keep looping until the postfix-expression is complete.  */
4936   while (true)
4937     {
4938       if (idk == CP_ID_KIND_UNQUALIFIED
4939           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4940           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4941         /* It is not a Koenig lookup function call.  */
4942         postfix_expression
4943           = unqualified_name_lookup_error (postfix_expression);
4944
4945       /* Peek at the next token.  */
4946       token = cp_lexer_peek_token (parser->lexer);
4947
4948       switch (token->type)
4949         {
4950         case CPP_OPEN_SQUARE:
4951           postfix_expression
4952             = cp_parser_postfix_open_square_expression (parser,
4953                                                         postfix_expression,
4954                                                         false);
4955           idk = CP_ID_KIND_NONE;
4956           is_member_access = false;
4957           break;
4958
4959         case CPP_OPEN_PAREN:
4960           /* postfix-expression ( expression-list [opt] ) */
4961           {
4962             bool koenig_p;
4963             bool is_builtin_constant_p;
4964             bool saved_integral_constant_expression_p = false;
4965             bool saved_non_integral_constant_expression_p = false;
4966             VEC(tree,gc) *args;
4967
4968             is_member_access = false;
4969
4970             is_builtin_constant_p
4971               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4972             if (is_builtin_constant_p)
4973               {
4974                 /* The whole point of __builtin_constant_p is to allow
4975                    non-constant expressions to appear as arguments.  */
4976                 saved_integral_constant_expression_p
4977                   = parser->integral_constant_expression_p;
4978                 saved_non_integral_constant_expression_p
4979                   = parser->non_integral_constant_expression_p;
4980                 parser->integral_constant_expression_p = false;
4981               }
4982             args = (cp_parser_parenthesized_expression_list
4983                     (parser, non_attr,
4984                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4985                      /*non_constant_p=*/NULL));
4986             if (is_builtin_constant_p)
4987               {
4988                 parser->integral_constant_expression_p
4989                   = saved_integral_constant_expression_p;
4990                 parser->non_integral_constant_expression_p
4991                   = saved_non_integral_constant_expression_p;
4992               }
4993
4994             if (args == NULL)
4995               {
4996                 postfix_expression = error_mark_node;
4997                 break;
4998               }
4999
5000             /* Function calls are not permitted in
5001                constant-expressions.  */
5002             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5003                 && cp_parser_non_integral_constant_expression (parser,
5004                                                                NIC_FUNC_CALL))
5005               {
5006                 postfix_expression = error_mark_node;
5007                 release_tree_vector (args);
5008                 break;
5009               }
5010
5011             koenig_p = false;
5012             if (idk == CP_ID_KIND_UNQUALIFIED
5013                 || idk == CP_ID_KIND_TEMPLATE_ID)
5014               {
5015                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5016                   {
5017                     if (!VEC_empty (tree, args))
5018                       {
5019                         koenig_p = true;
5020                         if (!any_type_dependent_arguments_p (args))
5021                           postfix_expression
5022                             = perform_koenig_lookup (postfix_expression, args,
5023                                                      /*include_std=*/false,
5024                                                      tf_warning_or_error);
5025                       }
5026                     else
5027                       postfix_expression
5028                         = unqualified_fn_lookup_error (postfix_expression);
5029                   }
5030                 /* We do not perform argument-dependent lookup if
5031                    normal lookup finds a non-function, in accordance
5032                    with the expected resolution of DR 218.  */
5033                 else if (!VEC_empty (tree, args)
5034                          && is_overloaded_fn (postfix_expression))
5035                   {
5036                     tree fn = get_first_fn (postfix_expression);
5037                     fn = STRIP_TEMPLATE (fn);
5038
5039                     /* Do not do argument dependent lookup if regular
5040                        lookup finds a member function or a block-scope
5041                        function declaration.  [basic.lookup.argdep]/3  */
5042                     if (!DECL_FUNCTION_MEMBER_P (fn)
5043                         && !DECL_LOCAL_FUNCTION_P (fn))
5044                       {
5045                         koenig_p = true;
5046                         if (!any_type_dependent_arguments_p (args))
5047                           postfix_expression
5048                             = perform_koenig_lookup (postfix_expression, args,
5049                                                      /*include_std=*/false,
5050                                                      tf_warning_or_error);
5051                       }
5052                   }
5053               }
5054
5055             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5056               {
5057                 tree instance = TREE_OPERAND (postfix_expression, 0);
5058                 tree fn = TREE_OPERAND (postfix_expression, 1);
5059
5060                 if (processing_template_decl
5061                     && (type_dependent_expression_p (instance)
5062                         || (!BASELINK_P (fn)
5063                             && TREE_CODE (fn) != FIELD_DECL)
5064                         || type_dependent_expression_p (fn)
5065                         || any_type_dependent_arguments_p (args)))
5066                   {
5067                     postfix_expression
5068                       = build_nt_call_vec (postfix_expression, args);
5069                     release_tree_vector (args);
5070                     break;
5071                   }
5072
5073                 if (BASELINK_P (fn))
5074                   {
5075                   postfix_expression
5076                     = (build_new_method_call
5077                        (instance, fn, &args, NULL_TREE,
5078                         (idk == CP_ID_KIND_QUALIFIED
5079                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5080                          : LOOKUP_NORMAL),
5081                         /*fn_p=*/NULL,
5082                         tf_warning_or_error));
5083                   }
5084                 else
5085                   postfix_expression
5086                     = finish_call_expr (postfix_expression, &args,
5087                                         /*disallow_virtual=*/false,
5088                                         /*koenig_p=*/false,
5089                                         tf_warning_or_error);
5090               }
5091             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5092                      || TREE_CODE (postfix_expression) == MEMBER_REF
5093                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5094               postfix_expression = (build_offset_ref_call_from_tree
5095                                     (postfix_expression, &args));
5096             else if (idk == CP_ID_KIND_QUALIFIED)
5097               /* A call to a static class member, or a namespace-scope
5098                  function.  */
5099               postfix_expression
5100                 = finish_call_expr (postfix_expression, &args,
5101                                     /*disallow_virtual=*/true,
5102                                     koenig_p,
5103                                     tf_warning_or_error);
5104             else
5105               /* All other function calls.  */
5106               postfix_expression
5107                 = finish_call_expr (postfix_expression, &args,
5108                                     /*disallow_virtual=*/false,
5109                                     koenig_p,
5110                                     tf_warning_or_error);
5111
5112             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5113             idk = CP_ID_KIND_NONE;
5114
5115             release_tree_vector (args);
5116           }
5117           break;
5118
5119         case CPP_DOT:
5120         case CPP_DEREF:
5121           /* postfix-expression . template [opt] id-expression
5122              postfix-expression . pseudo-destructor-name
5123              postfix-expression -> template [opt] id-expression
5124              postfix-expression -> pseudo-destructor-name */
5125
5126           /* Consume the `.' or `->' operator.  */
5127           cp_lexer_consume_token (parser->lexer);
5128
5129           postfix_expression
5130             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5131                                                       postfix_expression,
5132                                                       false, &idk,
5133                                                       token->location);
5134
5135           is_member_access = true;
5136           break;
5137
5138         case CPP_PLUS_PLUS:
5139           /* postfix-expression ++  */
5140           /* Consume the `++' token.  */
5141           cp_lexer_consume_token (parser->lexer);
5142           /* Generate a representation for the complete expression.  */
5143           postfix_expression
5144             = finish_increment_expr (postfix_expression,
5145                                      POSTINCREMENT_EXPR);
5146           /* Increments may not appear in constant-expressions.  */
5147           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5148             postfix_expression = error_mark_node;
5149           idk = CP_ID_KIND_NONE;
5150           is_member_access = false;
5151           break;
5152
5153         case CPP_MINUS_MINUS:
5154           /* postfix-expression -- */
5155           /* Consume the `--' token.  */
5156           cp_lexer_consume_token (parser->lexer);
5157           /* Generate a representation for the complete expression.  */
5158           postfix_expression
5159             = finish_increment_expr (postfix_expression,
5160                                      POSTDECREMENT_EXPR);
5161           /* Decrements may not appear in constant-expressions.  */
5162           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5163             postfix_expression = error_mark_node;
5164           idk = CP_ID_KIND_NONE;
5165           is_member_access = false;
5166           break;
5167
5168         default:
5169           if (pidk_return != NULL)
5170             * pidk_return = idk;
5171           if (member_access_only_p)
5172             return is_member_access? postfix_expression : error_mark_node;
5173           else
5174             return postfix_expression;
5175         }
5176     }
5177
5178   /* We should never get here.  */
5179   gcc_unreachable ();
5180   return error_mark_node;
5181 }
5182
5183 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5184    by cp_parser_builtin_offsetof.  We're looking for
5185
5186      postfix-expression [ expression ]
5187
5188    FOR_OFFSETOF is set if we're being called in that context, which
5189    changes how we deal with integer constant expressions.  */
5190
5191 static tree
5192 cp_parser_postfix_open_square_expression (cp_parser *parser,
5193                                           tree postfix_expression,
5194                                           bool for_offsetof)
5195 {
5196   tree index;
5197
5198   /* Consume the `[' token.  */
5199   cp_lexer_consume_token (parser->lexer);
5200
5201   /* Parse the index expression.  */
5202   /* ??? For offsetof, there is a question of what to allow here.  If
5203      offsetof is not being used in an integral constant expression context,
5204      then we *could* get the right answer by computing the value at runtime.
5205      If we are in an integral constant expression context, then we might
5206      could accept any constant expression; hard to say without analysis.
5207      Rather than open the barn door too wide right away, allow only integer
5208      constant expressions here.  */
5209   if (for_offsetof)
5210     index = cp_parser_constant_expression (parser, false, NULL);
5211   else
5212     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5213
5214   /* Look for the closing `]'.  */
5215   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5216
5217   /* Build the ARRAY_REF.  */
5218   postfix_expression = grok_array_decl (postfix_expression, index);
5219
5220   /* When not doing offsetof, array references are not permitted in
5221      constant-expressions.  */
5222   if (!for_offsetof
5223       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5224     postfix_expression = error_mark_node;
5225
5226   return postfix_expression;
5227 }
5228
5229 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5230    by cp_parser_builtin_offsetof.  We're looking for
5231
5232      postfix-expression . template [opt] id-expression
5233      postfix-expression . pseudo-destructor-name
5234      postfix-expression -> template [opt] id-expression
5235      postfix-expression -> pseudo-destructor-name
5236
5237    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5238    limits what of the above we'll actually accept, but nevermind.
5239    TOKEN_TYPE is the "." or "->" token, which will already have been
5240    removed from the stream.  */
5241
5242 static tree
5243 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5244                                         enum cpp_ttype token_type,
5245                                         tree postfix_expression,
5246                                         bool for_offsetof, cp_id_kind *idk,
5247                                         location_t location)
5248 {
5249   tree name;
5250   bool dependent_p;
5251   bool pseudo_destructor_p;
5252   tree scope = NULL_TREE;
5253
5254   /* If this is a `->' operator, dereference the pointer.  */
5255   if (token_type == CPP_DEREF)
5256     postfix_expression = build_x_arrow (postfix_expression);
5257   /* Check to see whether or not the expression is type-dependent.  */
5258   dependent_p = type_dependent_expression_p (postfix_expression);
5259   /* The identifier following the `->' or `.' is not qualified.  */
5260   parser->scope = NULL_TREE;
5261   parser->qualifying_scope = NULL_TREE;
5262   parser->object_scope = NULL_TREE;
5263   *idk = CP_ID_KIND_NONE;
5264
5265   /* Enter the scope corresponding to the type of the object
5266      given by the POSTFIX_EXPRESSION.  */
5267   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5268     {
5269       scope = TREE_TYPE (postfix_expression);
5270       /* According to the standard, no expression should ever have
5271          reference type.  Unfortunately, we do not currently match
5272          the standard in this respect in that our internal representation
5273          of an expression may have reference type even when the standard
5274          says it does not.  Therefore, we have to manually obtain the
5275          underlying type here.  */
5276       scope = non_reference (scope);
5277       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5278       if (scope == unknown_type_node)
5279         {
5280           error_at (location, "%qE does not have class type",
5281                     postfix_expression);
5282           scope = NULL_TREE;
5283         }
5284       /* Unlike the object expression in other contexts, *this is not
5285          required to be of complete type for purposes of class member
5286          access (5.2.5) outside the member function body.  */
5287       else if (scope != current_class_ref
5288                && !(processing_template_decl && scope == current_class_type))
5289         scope = complete_type_or_else (scope, NULL_TREE);
5290       /* Let the name lookup machinery know that we are processing a
5291          class member access expression.  */
5292       parser->context->object_type = scope;
5293       /* If something went wrong, we want to be able to discern that case,
5294          as opposed to the case where there was no SCOPE due to the type
5295          of expression being dependent.  */
5296       if (!scope)
5297         scope = error_mark_node;
5298       /* If the SCOPE was erroneous, make the various semantic analysis
5299          functions exit quickly -- and without issuing additional error
5300          messages.  */
5301       if (scope == error_mark_node)
5302         postfix_expression = error_mark_node;
5303     }
5304
5305   /* Assume this expression is not a pseudo-destructor access.  */
5306   pseudo_destructor_p = false;
5307
5308   /* If the SCOPE is a scalar type, then, if this is a valid program,
5309      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5310      is type dependent, it can be pseudo-destructor-name or something else.
5311      Try to parse it as pseudo-destructor-name first.  */
5312   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5313     {
5314       tree s;
5315       tree type;
5316
5317       cp_parser_parse_tentatively (parser);
5318       /* Parse the pseudo-destructor-name.  */
5319       s = NULL_TREE;
5320       cp_parser_pseudo_destructor_name (parser, &s, &type);
5321       if (dependent_p
5322           && (cp_parser_error_occurred (parser)
5323               || TREE_CODE (type) != TYPE_DECL
5324               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5325         cp_parser_abort_tentative_parse (parser);
5326       else if (cp_parser_parse_definitely (parser))
5327         {
5328           pseudo_destructor_p = true;
5329           postfix_expression
5330             = finish_pseudo_destructor_expr (postfix_expression,
5331                                              s, TREE_TYPE (type));
5332         }
5333     }
5334
5335   if (!pseudo_destructor_p)
5336     {
5337       /* If the SCOPE is not a scalar type, we are looking at an
5338          ordinary class member access expression, rather than a
5339          pseudo-destructor-name.  */
5340       bool template_p;
5341       cp_token *token = cp_lexer_peek_token (parser->lexer);
5342       /* Parse the id-expression.  */
5343       name = (cp_parser_id_expression
5344               (parser,
5345                cp_parser_optional_template_keyword (parser),
5346                /*check_dependency_p=*/true,
5347                &template_p,
5348                /*declarator_p=*/false,
5349                /*optional_p=*/false));
5350       /* In general, build a SCOPE_REF if the member name is qualified.
5351          However, if the name was not dependent and has already been
5352          resolved; there is no need to build the SCOPE_REF.  For example;
5353
5354              struct X { void f(); };
5355              template <typename T> void f(T* t) { t->X::f(); }
5356
5357          Even though "t" is dependent, "X::f" is not and has been resolved
5358          to a BASELINK; there is no need to include scope information.  */
5359
5360       /* But we do need to remember that there was an explicit scope for
5361          virtual function calls.  */
5362       if (parser->scope)
5363         *idk = CP_ID_KIND_QUALIFIED;
5364
5365       /* If the name is a template-id that names a type, we will get a
5366          TYPE_DECL here.  That is invalid code.  */
5367       if (TREE_CODE (name) == TYPE_DECL)
5368         {
5369           error_at (token->location, "invalid use of %qD", name);
5370           postfix_expression = error_mark_node;
5371         }
5372       else
5373         {
5374           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5375             {
5376               name = build_qualified_name (/*type=*/NULL_TREE,
5377                                            parser->scope,
5378                                            name,
5379                                            template_p);
5380               parser->scope = NULL_TREE;
5381               parser->qualifying_scope = NULL_TREE;
5382               parser->object_scope = NULL_TREE;
5383             }
5384           if (scope && name && BASELINK_P (name))
5385             adjust_result_of_qualified_name_lookup
5386               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5387           postfix_expression
5388             = finish_class_member_access_expr (postfix_expression, name,
5389                                                template_p, 
5390                                                tf_warning_or_error);
5391         }
5392     }
5393
5394   /* We no longer need to look up names in the scope of the object on
5395      the left-hand side of the `.' or `->' operator.  */
5396   parser->context->object_type = NULL_TREE;
5397
5398   /* Outside of offsetof, these operators may not appear in
5399      constant-expressions.  */
5400   if (!for_offsetof
5401       && (cp_parser_non_integral_constant_expression
5402           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5403     postfix_expression = error_mark_node;
5404
5405   return postfix_expression;
5406 }
5407
5408 /* Parse a parenthesized expression-list.
5409
5410    expression-list:
5411      assignment-expression
5412      expression-list, assignment-expression
5413
5414    attribute-list:
5415      expression-list
5416      identifier
5417      identifier, expression-list
5418
5419    CAST_P is true if this expression is the target of a cast.
5420
5421    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5422    argument pack.
5423
5424    Returns a vector of trees.  Each element is a representation of an
5425    assignment-expression.  NULL is returned if the ( and or ) are
5426    missing.  An empty, but allocated, vector is returned on no
5427    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5428    if we are parsing an attribute list for an attribute that wants a
5429    plain identifier argument, normal_attr for an attribute that wants
5430    an expression, or non_attr if we aren't parsing an attribute list.  If
5431    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5432    not all of the expressions in the list were constant.  */
5433
5434 static VEC(tree,gc) *
5435 cp_parser_parenthesized_expression_list (cp_parser* parser,
5436                                          int is_attribute_list,
5437                                          bool cast_p,
5438                                          bool allow_expansion_p,
5439                                          bool *non_constant_p)
5440 {
5441   VEC(tree,gc) *expression_list;
5442   bool fold_expr_p = is_attribute_list != non_attr;
5443   tree identifier = NULL_TREE;
5444   bool saved_greater_than_is_operator_p;
5445
5446   /* Assume all the expressions will be constant.  */
5447   if (non_constant_p)
5448     *non_constant_p = false;
5449
5450   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5451     return NULL;
5452
5453   expression_list = make_tree_vector ();
5454
5455   /* Within a parenthesized expression, a `>' token is always
5456      the greater-than operator.  */
5457   saved_greater_than_is_operator_p
5458     = parser->greater_than_is_operator_p;
5459   parser->greater_than_is_operator_p = true;
5460
5461   /* Consume expressions until there are no more.  */
5462   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5463     while (true)
5464       {
5465         tree expr;
5466
5467         /* At the beginning of attribute lists, check to see if the
5468            next token is an identifier.  */
5469         if (is_attribute_list == id_attr
5470             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5471           {
5472             cp_token *token;
5473
5474             /* Consume the identifier.  */
5475             token = cp_lexer_consume_token (parser->lexer);
5476             /* Save the identifier.  */
5477             identifier = token->u.value;
5478           }
5479         else
5480           {
5481             bool expr_non_constant_p;
5482
5483             /* Parse the next assignment-expression.  */
5484             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5485               {
5486                 /* A braced-init-list.  */
5487                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5488                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5489                 if (non_constant_p && expr_non_constant_p)
5490                   *non_constant_p = true;
5491               }
5492             else if (non_constant_p)
5493               {
5494                 expr = (cp_parser_constant_expression
5495                         (parser, /*allow_non_constant_p=*/true,
5496                          &expr_non_constant_p));
5497                 if (expr_non_constant_p)
5498                   *non_constant_p = true;
5499               }
5500             else
5501               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5502
5503             if (fold_expr_p)
5504               expr = fold_non_dependent_expr (expr);
5505
5506             /* If we have an ellipsis, then this is an expression
5507                expansion.  */
5508             if (allow_expansion_p
5509                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5510               {
5511                 /* Consume the `...'.  */
5512                 cp_lexer_consume_token (parser->lexer);
5513
5514                 /* Build the argument pack.  */
5515                 expr = make_pack_expansion (expr);
5516               }
5517
5518              /* Add it to the list.  We add error_mark_node
5519                 expressions to the list, so that we can still tell if
5520                 the correct form for a parenthesized expression-list
5521                 is found. That gives better errors.  */
5522             VEC_safe_push (tree, gc, expression_list, expr);
5523
5524             if (expr == error_mark_node)
5525               goto skip_comma;
5526           }
5527
5528         /* After the first item, attribute lists look the same as
5529            expression lists.  */
5530         is_attribute_list = non_attr;
5531
5532       get_comma:;
5533         /* If the next token isn't a `,', then we are done.  */
5534         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5535           break;
5536
5537         /* Otherwise, consume the `,' and keep going.  */
5538         cp_lexer_consume_token (parser->lexer);
5539       }
5540
5541   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5542     {
5543       int ending;
5544
5545     skip_comma:;
5546       /* We try and resync to an unnested comma, as that will give the
5547          user better diagnostics.  */
5548       ending = cp_parser_skip_to_closing_parenthesis (parser,
5549                                                       /*recovering=*/true,
5550                                                       /*or_comma=*/true,
5551                                                       /*consume_paren=*/true);
5552       if (ending < 0)
5553         goto get_comma;
5554       if (!ending)
5555         {
5556           parser->greater_than_is_operator_p
5557             = saved_greater_than_is_operator_p;
5558           return NULL;
5559         }
5560     }
5561
5562   parser->greater_than_is_operator_p
5563     = saved_greater_than_is_operator_p;
5564
5565   if (identifier)
5566     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5567
5568   return expression_list;
5569 }
5570
5571 /* Parse a pseudo-destructor-name.
5572
5573    pseudo-destructor-name:
5574      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5575      :: [opt] nested-name-specifier template template-id :: ~ type-name
5576      :: [opt] nested-name-specifier [opt] ~ type-name
5577
5578    If either of the first two productions is used, sets *SCOPE to the
5579    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5580    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5581    or ERROR_MARK_NODE if the parse fails.  */
5582
5583 static void
5584 cp_parser_pseudo_destructor_name (cp_parser* parser,
5585                                   tree* scope,
5586                                   tree* type)
5587 {
5588   bool nested_name_specifier_p;
5589
5590   /* Assume that things will not work out.  */
5591   *type = error_mark_node;
5592
5593   /* Look for the optional `::' operator.  */
5594   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5595   /* Look for the optional nested-name-specifier.  */
5596   nested_name_specifier_p
5597     = (cp_parser_nested_name_specifier_opt (parser,
5598                                             /*typename_keyword_p=*/false,
5599                                             /*check_dependency_p=*/true,
5600                                             /*type_p=*/false,
5601                                             /*is_declaration=*/false)
5602        != NULL_TREE);
5603   /* Now, if we saw a nested-name-specifier, we might be doing the
5604      second production.  */
5605   if (nested_name_specifier_p
5606       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5607     {
5608       /* Consume the `template' keyword.  */
5609       cp_lexer_consume_token (parser->lexer);
5610       /* Parse the template-id.  */
5611       cp_parser_template_id (parser,
5612                              /*template_keyword_p=*/true,
5613                              /*check_dependency_p=*/false,
5614                              /*is_declaration=*/true);
5615       /* Look for the `::' token.  */
5616       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5617     }
5618   /* If the next token is not a `~', then there might be some
5619      additional qualification.  */
5620   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5621     {
5622       /* At this point, we're looking for "type-name :: ~".  The type-name
5623          must not be a class-name, since this is a pseudo-destructor.  So,
5624          it must be either an enum-name, or a typedef-name -- both of which
5625          are just identifiers.  So, we peek ahead to check that the "::"
5626          and "~" tokens are present; if they are not, then we can avoid
5627          calling type_name.  */
5628       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5629           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5630           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5631         {
5632           cp_parser_error (parser, "non-scalar type");
5633           return;
5634         }
5635
5636       /* Look for the type-name.  */
5637       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5638       if (*scope == error_mark_node)
5639         return;
5640
5641       /* Look for the `::' token.  */
5642       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5643     }
5644   else
5645     *scope = NULL_TREE;
5646
5647   /* Look for the `~'.  */
5648   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5649
5650   /* Once we see the ~, this has to be a pseudo-destructor.  */
5651   if (!processing_template_decl && !cp_parser_error_occurred (parser))
5652     cp_parser_commit_to_tentative_parse (parser);
5653
5654   /* Look for the type-name again.  We are not responsible for
5655      checking that it matches the first type-name.  */
5656   *type = cp_parser_nonclass_name (parser);
5657 }
5658
5659 /* Parse a unary-expression.
5660
5661    unary-expression:
5662      postfix-expression
5663      ++ cast-expression
5664      -- cast-expression
5665      unary-operator cast-expression
5666      sizeof unary-expression
5667      sizeof ( type-id )
5668      alignof ( type-id )  [C++0x]
5669      new-expression
5670      delete-expression
5671
5672    GNU Extensions:
5673
5674    unary-expression:
5675      __extension__ cast-expression
5676      __alignof__ unary-expression
5677      __alignof__ ( type-id )
5678      alignof unary-expression  [C++0x]
5679      __real__ cast-expression
5680      __imag__ cast-expression
5681      && identifier
5682
5683    ADDRESS_P is true iff the unary-expression is appearing as the
5684    operand of the `&' operator.   CAST_P is true if this expression is
5685    the target of a cast.
5686
5687    Returns a representation of the expression.  */
5688
5689 static tree
5690 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5691                             cp_id_kind * pidk)
5692 {
5693   cp_token *token;
5694   enum tree_code unary_operator;
5695
5696   /* Peek at the next token.  */
5697   token = cp_lexer_peek_token (parser->lexer);
5698   /* Some keywords give away the kind of expression.  */
5699   if (token->type == CPP_KEYWORD)
5700     {
5701       enum rid keyword = token->keyword;
5702
5703       switch (keyword)
5704         {
5705         case RID_ALIGNOF:
5706         case RID_SIZEOF:
5707           {
5708             tree operand;
5709             enum tree_code op;
5710
5711             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5712             /* Consume the token.  */
5713             cp_lexer_consume_token (parser->lexer);
5714             /* Parse the operand.  */
5715             operand = cp_parser_sizeof_operand (parser, keyword);
5716
5717             if (TYPE_P (operand))
5718               return cxx_sizeof_or_alignof_type (operand, op, true);
5719             else
5720               {
5721                 /* ISO C++ defines alignof only with types, not with
5722                    expressions. So pedwarn if alignof is used with a non-
5723                    type expression. However, __alignof__ is ok.  */
5724                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5725                   pedwarn (token->location, OPT_pedantic,
5726                            "ISO C++ does not allow %<alignof%> "
5727                            "with a non-type");
5728
5729                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5730               }
5731           }
5732
5733         case RID_NEW:
5734           return cp_parser_new_expression (parser);
5735
5736         case RID_DELETE:
5737           return cp_parser_delete_expression (parser);
5738
5739         case RID_EXTENSION:
5740           {
5741             /* The saved value of the PEDANTIC flag.  */
5742             int saved_pedantic;
5743             tree expr;
5744
5745             /* Save away the PEDANTIC flag.  */
5746             cp_parser_extension_opt (parser, &saved_pedantic);
5747             /* Parse the cast-expression.  */
5748             expr = cp_parser_simple_cast_expression (parser);
5749             /* Restore the PEDANTIC flag.  */
5750             pedantic = saved_pedantic;
5751
5752             return expr;
5753           }
5754
5755         case RID_REALPART:
5756         case RID_IMAGPART:
5757           {
5758             tree expression;
5759
5760             /* Consume the `__real__' or `__imag__' token.  */
5761             cp_lexer_consume_token (parser->lexer);
5762             /* Parse the cast-expression.  */
5763             expression = cp_parser_simple_cast_expression (parser);
5764             /* Create the complete representation.  */
5765             return build_x_unary_op ((keyword == RID_REALPART
5766                                       ? REALPART_EXPR : IMAGPART_EXPR),
5767                                      expression,
5768                                      tf_warning_or_error);
5769           }
5770           break;
5771
5772         case RID_NOEXCEPT:
5773           {
5774             tree expr;
5775             const char *saved_message;
5776             bool saved_integral_constant_expression_p;
5777             bool saved_non_integral_constant_expression_p;
5778             bool saved_greater_than_is_operator_p;
5779
5780             cp_lexer_consume_token (parser->lexer);
5781             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5782
5783             saved_message = parser->type_definition_forbidden_message;
5784             parser->type_definition_forbidden_message
5785               = G_("types may not be defined in %<noexcept%> expressions");
5786
5787             saved_integral_constant_expression_p
5788               = parser->integral_constant_expression_p;
5789             saved_non_integral_constant_expression_p
5790               = parser->non_integral_constant_expression_p;
5791             parser->integral_constant_expression_p = false;
5792
5793             saved_greater_than_is_operator_p
5794               = parser->greater_than_is_operator_p;
5795             parser->greater_than_is_operator_p = true;
5796
5797             ++cp_unevaluated_operand;
5798             ++c_inhibit_evaluation_warnings;
5799             expr = cp_parser_expression (parser, false, NULL);
5800             --c_inhibit_evaluation_warnings;
5801             --cp_unevaluated_operand;
5802
5803             parser->greater_than_is_operator_p
5804               = saved_greater_than_is_operator_p;
5805
5806             parser->integral_constant_expression_p
5807               = saved_integral_constant_expression_p;
5808             parser->non_integral_constant_expression_p
5809               = saved_non_integral_constant_expression_p;
5810
5811             parser->type_definition_forbidden_message = saved_message;
5812
5813             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5814             return finish_noexcept_expr (expr, tf_warning_or_error);
5815           }
5816
5817         default:
5818           break;
5819         }
5820     }
5821
5822   /* Look for the `:: new' and `:: delete', which also signal the
5823      beginning of a new-expression, or delete-expression,
5824      respectively.  If the next token is `::', then it might be one of
5825      these.  */
5826   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5827     {
5828       enum rid keyword;
5829
5830       /* See if the token after the `::' is one of the keywords in
5831          which we're interested.  */
5832       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5833       /* If it's `new', we have a new-expression.  */
5834       if (keyword == RID_NEW)
5835         return cp_parser_new_expression (parser);
5836       /* Similarly, for `delete'.  */
5837       else if (keyword == RID_DELETE)
5838         return cp_parser_delete_expression (parser);
5839     }
5840
5841   /* Look for a unary operator.  */
5842   unary_operator = cp_parser_unary_operator (token);
5843   /* The `++' and `--' operators can be handled similarly, even though
5844      they are not technically unary-operators in the grammar.  */
5845   if (unary_operator == ERROR_MARK)
5846     {
5847       if (token->type == CPP_PLUS_PLUS)
5848         unary_operator = PREINCREMENT_EXPR;
5849       else if (token->type == CPP_MINUS_MINUS)
5850         unary_operator = PREDECREMENT_EXPR;
5851       /* Handle the GNU address-of-label extension.  */
5852       else if (cp_parser_allow_gnu_extensions_p (parser)
5853                && token->type == CPP_AND_AND)
5854         {
5855           tree identifier;
5856           tree expression;
5857           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5858
5859           /* Consume the '&&' token.  */
5860           cp_lexer_consume_token (parser->lexer);
5861           /* Look for the identifier.  */
5862           identifier = cp_parser_identifier (parser);
5863           /* Create an expression representing the address.  */
5864           expression = finish_label_address_expr (identifier, loc);
5865           if (cp_parser_non_integral_constant_expression (parser,
5866                                                           NIC_ADDR_LABEL))
5867             expression = error_mark_node;
5868           return expression;
5869         }
5870     }
5871   if (unary_operator != ERROR_MARK)
5872     {
5873       tree cast_expression;
5874       tree expression = error_mark_node;
5875       non_integral_constant non_constant_p = NIC_NONE;
5876
5877       /* Consume the operator token.  */
5878       token = cp_lexer_consume_token (parser->lexer);
5879       /* Parse the cast-expression.  */
5880       cast_expression
5881         = cp_parser_cast_expression (parser,
5882                                      unary_operator == ADDR_EXPR,
5883                                      /*cast_p=*/false, pidk);
5884       /* Now, build an appropriate representation.  */
5885       switch (unary_operator)
5886         {
5887         case INDIRECT_REF:
5888           non_constant_p = NIC_STAR;
5889           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5890                                              tf_warning_or_error);
5891           break;
5892
5893         case ADDR_EXPR:
5894            non_constant_p = NIC_ADDR;
5895           /* Fall through.  */
5896         case BIT_NOT_EXPR:
5897           expression = build_x_unary_op (unary_operator, cast_expression,
5898                                          tf_warning_or_error);
5899           break;
5900
5901         case PREINCREMENT_EXPR:
5902         case PREDECREMENT_EXPR:
5903           non_constant_p = unary_operator == PREINCREMENT_EXPR
5904                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5905           /* Fall through.  */
5906         case UNARY_PLUS_EXPR:
5907         case NEGATE_EXPR:
5908         case TRUTH_NOT_EXPR:
5909           expression = finish_unary_op_expr (unary_operator, cast_expression);
5910           break;
5911
5912         default:
5913           gcc_unreachable ();
5914         }
5915
5916       if (non_constant_p != NIC_NONE
5917           && cp_parser_non_integral_constant_expression (parser,
5918                                                          non_constant_p))
5919         expression = error_mark_node;
5920
5921       return expression;
5922     }
5923
5924   return cp_parser_postfix_expression (parser, address_p, cast_p,
5925                                        /*member_access_only_p=*/false,
5926                                        pidk);
5927 }
5928
5929 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5930    unary-operator, the corresponding tree code is returned.  */
5931
5932 static enum tree_code
5933 cp_parser_unary_operator (cp_token* token)
5934 {
5935   switch (token->type)
5936     {
5937     case CPP_MULT:
5938       return INDIRECT_REF;
5939
5940     case CPP_AND:
5941       return ADDR_EXPR;
5942
5943     case CPP_PLUS:
5944       return UNARY_PLUS_EXPR;
5945
5946     case CPP_MINUS:
5947       return NEGATE_EXPR;
5948
5949     case CPP_NOT:
5950       return TRUTH_NOT_EXPR;
5951
5952     case CPP_COMPL:
5953       return BIT_NOT_EXPR;
5954
5955     default:
5956       return ERROR_MARK;
5957     }
5958 }
5959
5960 /* Parse a new-expression.
5961
5962    new-expression:
5963      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5964      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5965
5966    Returns a representation of the expression.  */
5967
5968 static tree
5969 cp_parser_new_expression (cp_parser* parser)
5970 {
5971   bool global_scope_p;
5972   VEC(tree,gc) *placement;
5973   tree type;
5974   VEC(tree,gc) *initializer;
5975   tree nelts;
5976   tree ret;
5977
5978   /* Look for the optional `::' operator.  */
5979   global_scope_p
5980     = (cp_parser_global_scope_opt (parser,
5981                                    /*current_scope_valid_p=*/false)
5982        != NULL_TREE);
5983   /* Look for the `new' operator.  */
5984   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5985   /* There's no easy way to tell a new-placement from the
5986      `( type-id )' construct.  */
5987   cp_parser_parse_tentatively (parser);
5988   /* Look for a new-placement.  */
5989   placement = cp_parser_new_placement (parser);
5990   /* If that didn't work out, there's no new-placement.  */
5991   if (!cp_parser_parse_definitely (parser))
5992     {
5993       if (placement != NULL)
5994         release_tree_vector (placement);
5995       placement = NULL;
5996     }
5997
5998   /* If the next token is a `(', then we have a parenthesized
5999      type-id.  */
6000   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6001     {
6002       cp_token *token;
6003       /* Consume the `('.  */
6004       cp_lexer_consume_token (parser->lexer);
6005       /* Parse the type-id.  */
6006       type = cp_parser_type_id (parser);
6007       /* Look for the closing `)'.  */
6008       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6009       token = cp_lexer_peek_token (parser->lexer);
6010       /* There should not be a direct-new-declarator in this production,
6011          but GCC used to allowed this, so we check and emit a sensible error
6012          message for this case.  */
6013       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6014         {
6015           error_at (token->location,
6016                     "array bound forbidden after parenthesized type-id");
6017           inform (token->location, 
6018                   "try removing the parentheses around the type-id");
6019           cp_parser_direct_new_declarator (parser);
6020         }
6021       nelts = NULL_TREE;
6022     }
6023   /* Otherwise, there must be a new-type-id.  */
6024   else
6025     type = cp_parser_new_type_id (parser, &nelts);
6026
6027   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6028   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6029       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6030     initializer = cp_parser_new_initializer (parser);
6031   else
6032     initializer = NULL;
6033
6034   /* A new-expression may not appear in an integral constant
6035      expression.  */
6036   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6037     ret = error_mark_node;
6038   else
6039     {
6040       /* Create a representation of the new-expression.  */
6041       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6042                        tf_warning_or_error);
6043     }
6044
6045   if (placement != NULL)
6046     release_tree_vector (placement);
6047   if (initializer != NULL)
6048     release_tree_vector (initializer);
6049
6050   return ret;
6051 }
6052
6053 /* Parse a new-placement.
6054
6055    new-placement:
6056      ( expression-list )
6057
6058    Returns the same representation as for an expression-list.  */
6059
6060 static VEC(tree,gc) *
6061 cp_parser_new_placement (cp_parser* parser)
6062 {
6063   VEC(tree,gc) *expression_list;
6064
6065   /* Parse the expression-list.  */
6066   expression_list = (cp_parser_parenthesized_expression_list
6067                      (parser, non_attr, /*cast_p=*/false,
6068                       /*allow_expansion_p=*/true,
6069                       /*non_constant_p=*/NULL));
6070
6071   return expression_list;
6072 }
6073
6074 /* Parse a new-type-id.
6075
6076    new-type-id:
6077      type-specifier-seq new-declarator [opt]
6078
6079    Returns the TYPE allocated.  If the new-type-id indicates an array
6080    type, *NELTS is set to the number of elements in the last array
6081    bound; the TYPE will not include the last array bound.  */
6082
6083 static tree
6084 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6085 {
6086   cp_decl_specifier_seq type_specifier_seq;
6087   cp_declarator *new_declarator;
6088   cp_declarator *declarator;
6089   cp_declarator *outer_declarator;
6090   const char *saved_message;
6091   tree type;
6092
6093   /* The type-specifier sequence must not contain type definitions.
6094      (It cannot contain declarations of new types either, but if they
6095      are not definitions we will catch that because they are not
6096      complete.)  */
6097   saved_message = parser->type_definition_forbidden_message;
6098   parser->type_definition_forbidden_message
6099     = G_("types may not be defined in a new-type-id");
6100   /* Parse the type-specifier-seq.  */
6101   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6102                                 /*is_trailing_return=*/false,
6103                                 &type_specifier_seq);
6104   /* Restore the old message.  */
6105   parser->type_definition_forbidden_message = saved_message;
6106   /* Parse the new-declarator.  */
6107   new_declarator = cp_parser_new_declarator_opt (parser);
6108
6109   /* Determine the number of elements in the last array dimension, if
6110      any.  */
6111   *nelts = NULL_TREE;
6112   /* Skip down to the last array dimension.  */
6113   declarator = new_declarator;
6114   outer_declarator = NULL;
6115   while (declarator && (declarator->kind == cdk_pointer
6116                         || declarator->kind == cdk_ptrmem))
6117     {
6118       outer_declarator = declarator;
6119       declarator = declarator->declarator;
6120     }
6121   while (declarator
6122          && declarator->kind == cdk_array
6123          && declarator->declarator
6124          && declarator->declarator->kind == cdk_array)
6125     {
6126       outer_declarator = declarator;
6127       declarator = declarator->declarator;
6128     }
6129
6130   if (declarator && declarator->kind == cdk_array)
6131     {
6132       *nelts = declarator->u.array.bounds;
6133       if (*nelts == error_mark_node)
6134         *nelts = integer_one_node;
6135
6136       if (outer_declarator)
6137         outer_declarator->declarator = declarator->declarator;
6138       else
6139         new_declarator = NULL;
6140     }
6141
6142   type = groktypename (&type_specifier_seq, new_declarator, false);
6143   return type;
6144 }
6145
6146 /* Parse an (optional) new-declarator.
6147
6148    new-declarator:
6149      ptr-operator new-declarator [opt]
6150      direct-new-declarator
6151
6152    Returns the declarator.  */
6153
6154 static cp_declarator *
6155 cp_parser_new_declarator_opt (cp_parser* parser)
6156 {
6157   enum tree_code code;
6158   tree type;
6159   cp_cv_quals cv_quals;
6160
6161   /* We don't know if there's a ptr-operator next, or not.  */
6162   cp_parser_parse_tentatively (parser);
6163   /* Look for a ptr-operator.  */
6164   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6165   /* If that worked, look for more new-declarators.  */
6166   if (cp_parser_parse_definitely (parser))
6167     {
6168       cp_declarator *declarator;
6169
6170       /* Parse another optional declarator.  */
6171       declarator = cp_parser_new_declarator_opt (parser);
6172
6173       return cp_parser_make_indirect_declarator
6174         (code, type, cv_quals, declarator);
6175     }
6176
6177   /* If the next token is a `[', there is a direct-new-declarator.  */
6178   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6179     return cp_parser_direct_new_declarator (parser);
6180
6181   return NULL;
6182 }
6183
6184 /* Parse a direct-new-declarator.
6185
6186    direct-new-declarator:
6187      [ expression ]
6188      direct-new-declarator [constant-expression]
6189
6190    */
6191
6192 static cp_declarator *
6193 cp_parser_direct_new_declarator (cp_parser* parser)
6194 {
6195   cp_declarator *declarator = NULL;
6196
6197   while (true)
6198     {
6199       tree expression;
6200
6201       /* Look for the opening `['.  */
6202       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6203       /* The first expression is not required to be constant.  */
6204       if (!declarator)
6205         {
6206           cp_token *token = cp_lexer_peek_token (parser->lexer);
6207           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6208           /* The standard requires that the expression have integral
6209              type.  DR 74 adds enumeration types.  We believe that the
6210              real intent is that these expressions be handled like the
6211              expression in a `switch' condition, which also allows
6212              classes with a single conversion to integral or
6213              enumeration type.  */
6214           if (!processing_template_decl)
6215             {
6216               expression
6217                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6218                                               expression,
6219                                               /*complain=*/true);
6220               if (!expression)
6221                 {
6222                   error_at (token->location,
6223                             "expression in new-declarator must have integral "
6224                             "or enumeration type");
6225                   expression = error_mark_node;
6226                 }
6227             }
6228         }
6229       /* But all the other expressions must be.  */
6230       else
6231         expression
6232           = cp_parser_constant_expression (parser,
6233                                            /*allow_non_constant=*/false,
6234                                            NULL);
6235       /* Look for the closing `]'.  */
6236       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6237
6238       /* Add this bound to the declarator.  */
6239       declarator = make_array_declarator (declarator, expression);
6240
6241       /* If the next token is not a `[', then there are no more
6242          bounds.  */
6243       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6244         break;
6245     }
6246
6247   return declarator;
6248 }
6249
6250 /* Parse a new-initializer.
6251
6252    new-initializer:
6253      ( expression-list [opt] )
6254      braced-init-list
6255
6256    Returns a representation of the expression-list.  */
6257
6258 static VEC(tree,gc) *
6259 cp_parser_new_initializer (cp_parser* parser)
6260 {
6261   VEC(tree,gc) *expression_list;
6262
6263   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6264     {
6265       tree t;
6266       bool expr_non_constant_p;
6267       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6268       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6269       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6270       expression_list = make_tree_vector_single (t);
6271     }
6272   else
6273     expression_list = (cp_parser_parenthesized_expression_list
6274                        (parser, non_attr, /*cast_p=*/false,
6275                         /*allow_expansion_p=*/true,
6276                         /*non_constant_p=*/NULL));
6277
6278   return expression_list;
6279 }
6280
6281 /* Parse a delete-expression.
6282
6283    delete-expression:
6284      :: [opt] delete cast-expression
6285      :: [opt] delete [ ] cast-expression
6286
6287    Returns a representation of the expression.  */
6288
6289 static tree
6290 cp_parser_delete_expression (cp_parser* parser)
6291 {
6292   bool global_scope_p;
6293   bool array_p;
6294   tree expression;
6295
6296   /* Look for the optional `::' operator.  */
6297   global_scope_p
6298     = (cp_parser_global_scope_opt (parser,
6299                                    /*current_scope_valid_p=*/false)
6300        != NULL_TREE);
6301   /* Look for the `delete' keyword.  */
6302   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6303   /* See if the array syntax is in use.  */
6304   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6305     {
6306       /* Consume the `[' token.  */
6307       cp_lexer_consume_token (parser->lexer);
6308       /* Look for the `]' token.  */
6309       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6310       /* Remember that this is the `[]' construct.  */
6311       array_p = true;
6312     }
6313   else
6314     array_p = false;
6315
6316   /* Parse the cast-expression.  */
6317   expression = cp_parser_simple_cast_expression (parser);
6318
6319   /* A delete-expression may not appear in an integral constant
6320      expression.  */
6321   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6322     return error_mark_node;
6323
6324   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6325                         tf_warning_or_error);
6326 }
6327
6328 /* Returns true if TOKEN may start a cast-expression and false
6329    otherwise.  */
6330
6331 static bool
6332 cp_parser_token_starts_cast_expression (cp_token *token)
6333 {
6334   switch (token->type)
6335     {
6336     case CPP_COMMA:
6337     case CPP_SEMICOLON:
6338     case CPP_QUERY:
6339     case CPP_COLON:
6340     case CPP_CLOSE_SQUARE:
6341     case CPP_CLOSE_PAREN:
6342     case CPP_CLOSE_BRACE:
6343     case CPP_DOT:
6344     case CPP_DOT_STAR:
6345     case CPP_DEREF:
6346     case CPP_DEREF_STAR:
6347     case CPP_DIV:
6348     case CPP_MOD:
6349     case CPP_LSHIFT:
6350     case CPP_RSHIFT:
6351     case CPP_LESS:
6352     case CPP_GREATER:
6353     case CPP_LESS_EQ:
6354     case CPP_GREATER_EQ:
6355     case CPP_EQ_EQ:
6356     case CPP_NOT_EQ:
6357     case CPP_EQ:
6358     case CPP_MULT_EQ:
6359     case CPP_DIV_EQ:
6360     case CPP_MOD_EQ:
6361     case CPP_PLUS_EQ:
6362     case CPP_MINUS_EQ:
6363     case CPP_RSHIFT_EQ:
6364     case CPP_LSHIFT_EQ:
6365     case CPP_AND_EQ:
6366     case CPP_XOR_EQ:
6367     case CPP_OR_EQ:
6368     case CPP_XOR:
6369     case CPP_OR:
6370     case CPP_OR_OR:
6371     case CPP_EOF:
6372       return false;
6373
6374       /* '[' may start a primary-expression in obj-c++.  */
6375     case CPP_OPEN_SQUARE:
6376       return c_dialect_objc ();
6377
6378     default:
6379       return true;
6380     }
6381 }
6382
6383 /* Parse a cast-expression.
6384
6385    cast-expression:
6386      unary-expression
6387      ( type-id ) cast-expression
6388
6389    ADDRESS_P is true iff the unary-expression is appearing as the
6390    operand of the `&' operator.   CAST_P is true if this expression is
6391    the target of a cast.
6392
6393    Returns a representation of the expression.  */
6394
6395 static tree
6396 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6397                            cp_id_kind * pidk)
6398 {
6399   /* If it's a `(', then we might be looking at a cast.  */
6400   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6401     {
6402       tree type = NULL_TREE;
6403       tree expr = NULL_TREE;
6404       bool compound_literal_p;
6405       const char *saved_message;
6406
6407       /* There's no way to know yet whether or not this is a cast.
6408          For example, `(int (3))' is a unary-expression, while `(int)
6409          3' is a cast.  So, we resort to parsing tentatively.  */
6410       cp_parser_parse_tentatively (parser);
6411       /* Types may not be defined in a cast.  */
6412       saved_message = parser->type_definition_forbidden_message;
6413       parser->type_definition_forbidden_message
6414         = G_("types may not be defined in casts");
6415       /* Consume the `('.  */
6416       cp_lexer_consume_token (parser->lexer);
6417       /* A very tricky bit is that `(struct S) { 3 }' is a
6418          compound-literal (which we permit in C++ as an extension).
6419          But, that construct is not a cast-expression -- it is a
6420          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6421          is legal; if the compound-literal were a cast-expression,
6422          you'd need an extra set of parentheses.)  But, if we parse
6423          the type-id, and it happens to be a class-specifier, then we
6424          will commit to the parse at that point, because we cannot
6425          undo the action that is done when creating a new class.  So,
6426          then we cannot back up and do a postfix-expression.
6427
6428          Therefore, we scan ahead to the closing `)', and check to see
6429          if the token after the `)' is a `{'.  If so, we are not
6430          looking at a cast-expression.
6431
6432          Save tokens so that we can put them back.  */
6433       cp_lexer_save_tokens (parser->lexer);
6434       /* Skip tokens until the next token is a closing parenthesis.
6435          If we find the closing `)', and the next token is a `{', then
6436          we are looking at a compound-literal.  */
6437       compound_literal_p
6438         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6439                                                   /*consume_paren=*/true)
6440            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6441       /* Roll back the tokens we skipped.  */
6442       cp_lexer_rollback_tokens (parser->lexer);
6443       /* If we were looking at a compound-literal, simulate an error
6444          so that the call to cp_parser_parse_definitely below will
6445          fail.  */
6446       if (compound_literal_p)
6447         cp_parser_simulate_error (parser);
6448       else
6449         {
6450           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6451           parser->in_type_id_in_expr_p = true;
6452           /* Look for the type-id.  */
6453           type = cp_parser_type_id (parser);
6454           /* Look for the closing `)'.  */
6455           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6456           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6457         }
6458
6459       /* Restore the saved message.  */
6460       parser->type_definition_forbidden_message = saved_message;
6461
6462       /* At this point this can only be either a cast or a
6463          parenthesized ctor such as `(T ())' that looks like a cast to
6464          function returning T.  */
6465       if (!cp_parser_error_occurred (parser)
6466           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6467                                                      (parser->lexer)))
6468         {
6469           cp_parser_parse_definitely (parser);
6470           expr = cp_parser_cast_expression (parser,
6471                                             /*address_p=*/false,
6472                                             /*cast_p=*/true, pidk);
6473
6474           /* Warn about old-style casts, if so requested.  */
6475           if (warn_old_style_cast
6476               && !in_system_header
6477               && !VOID_TYPE_P (type)
6478               && current_lang_name != lang_name_c)
6479             warning (OPT_Wold_style_cast, "use of old-style cast");
6480
6481           /* Only type conversions to integral or enumeration types
6482              can be used in constant-expressions.  */
6483           if (!cast_valid_in_integral_constant_expression_p (type)
6484               && cp_parser_non_integral_constant_expression (parser,
6485                                                              NIC_CAST))
6486             return error_mark_node;
6487
6488           /* Perform the cast.  */
6489           expr = build_c_cast (input_location, type, expr);
6490           return expr;
6491         }
6492       else 
6493         cp_parser_abort_tentative_parse (parser);
6494     }
6495
6496   /* If we get here, then it's not a cast, so it must be a
6497      unary-expression.  */
6498   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6499 }
6500
6501 /* Parse a binary expression of the general form:
6502
6503    pm-expression:
6504      cast-expression
6505      pm-expression .* cast-expression
6506      pm-expression ->* cast-expression
6507
6508    multiplicative-expression:
6509      pm-expression
6510      multiplicative-expression * pm-expression
6511      multiplicative-expression / pm-expression
6512      multiplicative-expression % pm-expression
6513
6514    additive-expression:
6515      multiplicative-expression
6516      additive-expression + multiplicative-expression
6517      additive-expression - multiplicative-expression
6518
6519    shift-expression:
6520      additive-expression
6521      shift-expression << additive-expression
6522      shift-expression >> additive-expression
6523
6524    relational-expression:
6525      shift-expression
6526      relational-expression < shift-expression
6527      relational-expression > shift-expression
6528      relational-expression <= shift-expression
6529      relational-expression >= shift-expression
6530
6531   GNU Extension:
6532
6533    relational-expression:
6534      relational-expression <? shift-expression
6535      relational-expression >? shift-expression
6536
6537    equality-expression:
6538      relational-expression
6539      equality-expression == relational-expression
6540      equality-expression != relational-expression
6541
6542    and-expression:
6543      equality-expression
6544      and-expression & equality-expression
6545
6546    exclusive-or-expression:
6547      and-expression
6548      exclusive-or-expression ^ and-expression
6549
6550    inclusive-or-expression:
6551      exclusive-or-expression
6552      inclusive-or-expression | exclusive-or-expression
6553
6554    logical-and-expression:
6555      inclusive-or-expression
6556      logical-and-expression && inclusive-or-expression
6557
6558    logical-or-expression:
6559      logical-and-expression
6560      logical-or-expression || logical-and-expression
6561
6562    All these are implemented with a single function like:
6563
6564    binary-expression:
6565      simple-cast-expression
6566      binary-expression <token> binary-expression
6567
6568    CAST_P is true if this expression is the target of a cast.
6569
6570    The binops_by_token map is used to get the tree codes for each <token> type.
6571    binary-expressions are associated according to a precedence table.  */
6572
6573 #define TOKEN_PRECEDENCE(token)                              \
6574 (((token->type == CPP_GREATER                                \
6575    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6576   && !parser->greater_than_is_operator_p)                    \
6577  ? PREC_NOT_OPERATOR                                         \
6578  : binops_by_token[token->type].prec)
6579
6580 static tree
6581 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6582                              bool no_toplevel_fold_p,
6583                              enum cp_parser_prec prec,
6584                              cp_id_kind * pidk)
6585 {
6586   cp_parser_expression_stack stack;
6587   cp_parser_expression_stack_entry *sp = &stack[0];
6588   tree lhs, rhs;
6589   cp_token *token;
6590   enum tree_code tree_type, lhs_type, rhs_type;
6591   enum cp_parser_prec new_prec, lookahead_prec;
6592   tree overload;
6593
6594   /* Parse the first expression.  */
6595   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6596   lhs_type = ERROR_MARK;
6597
6598   for (;;)
6599     {
6600       /* Get an operator token.  */
6601       token = cp_lexer_peek_token (parser->lexer);
6602
6603       if (warn_cxx0x_compat
6604           && token->type == CPP_RSHIFT
6605           && !parser->greater_than_is_operator_p)
6606         {
6607           if (warning_at (token->location, OPT_Wc__0x_compat, 
6608                           "%<>>%> operator will be treated as"
6609                           " two right angle brackets in C++0x"))
6610             inform (token->location,
6611                     "suggest parentheses around %<>>%> expression");
6612         }
6613
6614       new_prec = TOKEN_PRECEDENCE (token);
6615
6616       /* Popping an entry off the stack means we completed a subexpression:
6617          - either we found a token which is not an operator (`>' where it is not
6618            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6619            will happen repeatedly;
6620          - or, we found an operator which has lower priority.  This is the case
6621            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6622            parsing `3 * 4'.  */
6623       if (new_prec <= prec)
6624         {
6625           if (sp == stack)
6626             break;
6627           else
6628             goto pop;
6629         }
6630
6631      get_rhs:
6632       tree_type = binops_by_token[token->type].tree_type;
6633
6634       /* We used the operator token.  */
6635       cp_lexer_consume_token (parser->lexer);
6636
6637       /* For "false && x" or "true || x", x will never be executed;
6638          disable warnings while evaluating it.  */
6639       if (tree_type == TRUTH_ANDIF_EXPR)
6640         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6641       else if (tree_type == TRUTH_ORIF_EXPR)
6642         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6643
6644       /* Extract another operand.  It may be the RHS of this expression
6645          or the LHS of a new, higher priority expression.  */
6646       rhs = cp_parser_simple_cast_expression (parser);
6647       rhs_type = ERROR_MARK;
6648
6649       /* Get another operator token.  Look up its precedence to avoid
6650          building a useless (immediately popped) stack entry for common
6651          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6652       token = cp_lexer_peek_token (parser->lexer);
6653       lookahead_prec = TOKEN_PRECEDENCE (token);
6654       if (lookahead_prec > new_prec)
6655         {
6656           /* ... and prepare to parse the RHS of the new, higher priority
6657              expression.  Since precedence levels on the stack are
6658              monotonically increasing, we do not have to care about
6659              stack overflows.  */
6660           sp->prec = prec;
6661           sp->tree_type = tree_type;
6662           sp->lhs = lhs;
6663           sp->lhs_type = lhs_type;
6664           sp++;
6665           lhs = rhs;
6666           lhs_type = rhs_type;
6667           prec = new_prec;
6668           new_prec = lookahead_prec;
6669           goto get_rhs;
6670
6671          pop:
6672           lookahead_prec = new_prec;
6673           /* If the stack is not empty, we have parsed into LHS the right side
6674              (`4' in the example above) of an expression we had suspended.
6675              We can use the information on the stack to recover the LHS (`3')
6676              from the stack together with the tree code (`MULT_EXPR'), and
6677              the precedence of the higher level subexpression
6678              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6679              which will be used to actually build the additive expression.  */
6680           --sp;
6681           prec = sp->prec;
6682           tree_type = sp->tree_type;
6683           rhs = lhs;
6684           rhs_type = lhs_type;
6685           lhs = sp->lhs;
6686           lhs_type = sp->lhs_type;
6687         }
6688
6689       /* Undo the disabling of warnings done above.  */
6690       if (tree_type == TRUTH_ANDIF_EXPR)
6691         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6692       else if (tree_type == TRUTH_ORIF_EXPR)
6693         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6694
6695       overload = NULL;
6696       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6697          ERROR_MARK for everything that is not a binary expression.
6698          This makes warn_about_parentheses miss some warnings that
6699          involve unary operators.  For unary expressions we should
6700          pass the correct tree_code unless the unary expression was
6701          surrounded by parentheses.
6702       */
6703       if (no_toplevel_fold_p
6704           && lookahead_prec <= prec
6705           && sp == stack
6706           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6707         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6708       else
6709         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6710                                  &overload, tf_warning_or_error);
6711       lhs_type = tree_type;
6712
6713       /* If the binary operator required the use of an overloaded operator,
6714          then this expression cannot be an integral constant-expression.
6715          An overloaded operator can be used even if both operands are
6716          otherwise permissible in an integral constant-expression if at
6717          least one of the operands is of enumeration type.  */
6718
6719       if (overload
6720           && cp_parser_non_integral_constant_expression (parser,
6721                                                          NIC_OVERLOADED))
6722         return error_mark_node;
6723     }
6724
6725   return lhs;
6726 }
6727
6728
6729 /* Parse the `? expression : assignment-expression' part of a
6730    conditional-expression.  The LOGICAL_OR_EXPR is the
6731    logical-or-expression that started the conditional-expression.
6732    Returns a representation of the entire conditional-expression.
6733
6734    This routine is used by cp_parser_assignment_expression.
6735
6736      ? expression : assignment-expression
6737
6738    GNU Extensions:
6739
6740      ? : assignment-expression */
6741
6742 static tree
6743 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6744 {
6745   tree expr;
6746   tree assignment_expr;
6747   struct cp_token *token;
6748
6749   /* Consume the `?' token.  */
6750   cp_lexer_consume_token (parser->lexer);
6751   token = cp_lexer_peek_token (parser->lexer);
6752   if (cp_parser_allow_gnu_extensions_p (parser)
6753       && token->type == CPP_COLON)
6754     {
6755       pedwarn (token->location, OPT_pedantic, 
6756                "ISO C++ does not allow ?: with omitted middle operand");
6757       /* Implicit true clause.  */
6758       expr = NULL_TREE;
6759       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6760       warn_for_omitted_condop (token->location, logical_or_expr);
6761     }
6762   else
6763     {
6764       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6765       parser->colon_corrects_to_scope_p = false;
6766       /* Parse the expression.  */
6767       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6768       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6769       c_inhibit_evaluation_warnings +=
6770         ((logical_or_expr == truthvalue_true_node)
6771          - (logical_or_expr == truthvalue_false_node));
6772       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6773     }
6774
6775   /* The next token should be a `:'.  */
6776   cp_parser_require (parser, CPP_COLON, RT_COLON);
6777   /* Parse the assignment-expression.  */
6778   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6779   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6780
6781   /* Build the conditional-expression.  */
6782   return build_x_conditional_expr (logical_or_expr,
6783                                    expr,
6784                                    assignment_expr,
6785                                    tf_warning_or_error);
6786 }
6787
6788 /* Parse an assignment-expression.
6789
6790    assignment-expression:
6791      conditional-expression
6792      logical-or-expression assignment-operator assignment_expression
6793      throw-expression
6794
6795    CAST_P is true if this expression is the target of a cast.
6796
6797    Returns a representation for the expression.  */
6798
6799 static tree
6800 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6801                                  cp_id_kind * pidk)
6802 {
6803   tree expr;
6804
6805   /* If the next token is the `throw' keyword, then we're looking at
6806      a throw-expression.  */
6807   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6808     expr = cp_parser_throw_expression (parser);
6809   /* Otherwise, it must be that we are looking at a
6810      logical-or-expression.  */
6811   else
6812     {
6813       /* Parse the binary expressions (logical-or-expression).  */
6814       expr = cp_parser_binary_expression (parser, cast_p, false,
6815                                           PREC_NOT_OPERATOR, pidk);
6816       /* If the next token is a `?' then we're actually looking at a
6817          conditional-expression.  */
6818       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6819         return cp_parser_question_colon_clause (parser, expr);
6820       else
6821         {
6822           enum tree_code assignment_operator;
6823
6824           /* If it's an assignment-operator, we're using the second
6825              production.  */
6826           assignment_operator
6827             = cp_parser_assignment_operator_opt (parser);
6828           if (assignment_operator != ERROR_MARK)
6829             {
6830               bool non_constant_p;
6831
6832               /* Parse the right-hand side of the assignment.  */
6833               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6834
6835               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6836                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6837
6838               /* An assignment may not appear in a
6839                  constant-expression.  */
6840               if (cp_parser_non_integral_constant_expression (parser,
6841                                                               NIC_ASSIGNMENT))
6842                 return error_mark_node;
6843               /* Build the assignment expression.  */
6844               expr = build_x_modify_expr (expr,
6845                                           assignment_operator,
6846                                           rhs,
6847                                           tf_warning_or_error);
6848             }
6849         }
6850     }
6851
6852   return expr;
6853 }
6854
6855 /* Parse an (optional) assignment-operator.
6856
6857    assignment-operator: one of
6858      = *= /= %= += -= >>= <<= &= ^= |=
6859
6860    GNU Extension:
6861
6862    assignment-operator: one of
6863      <?= >?=
6864
6865    If the next token is an assignment operator, the corresponding tree
6866    code is returned, and the token is consumed.  For example, for
6867    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6868    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6869    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6870    operator, ERROR_MARK is returned.  */
6871
6872 static enum tree_code
6873 cp_parser_assignment_operator_opt (cp_parser* parser)
6874 {
6875   enum tree_code op;
6876   cp_token *token;
6877
6878   /* Peek at the next token.  */
6879   token = cp_lexer_peek_token (parser->lexer);
6880
6881   switch (token->type)
6882     {
6883     case CPP_EQ:
6884       op = NOP_EXPR;
6885       break;
6886
6887     case CPP_MULT_EQ:
6888       op = MULT_EXPR;
6889       break;
6890
6891     case CPP_DIV_EQ:
6892       op = TRUNC_DIV_EXPR;
6893       break;
6894
6895     case CPP_MOD_EQ:
6896       op = TRUNC_MOD_EXPR;
6897       break;
6898
6899     case CPP_PLUS_EQ:
6900       op = PLUS_EXPR;
6901       break;
6902
6903     case CPP_MINUS_EQ:
6904       op = MINUS_EXPR;
6905       break;
6906
6907     case CPP_RSHIFT_EQ:
6908       op = RSHIFT_EXPR;
6909       break;
6910
6911     case CPP_LSHIFT_EQ:
6912       op = LSHIFT_EXPR;
6913       break;
6914
6915     case CPP_AND_EQ:
6916       op = BIT_AND_EXPR;
6917       break;
6918
6919     case CPP_XOR_EQ:
6920       op = BIT_XOR_EXPR;
6921       break;
6922
6923     case CPP_OR_EQ:
6924       op = BIT_IOR_EXPR;
6925       break;
6926
6927     default:
6928       /* Nothing else is an assignment operator.  */
6929       op = ERROR_MARK;
6930     }
6931
6932   /* If it was an assignment operator, consume it.  */
6933   if (op != ERROR_MARK)
6934     cp_lexer_consume_token (parser->lexer);
6935
6936   return op;
6937 }
6938
6939 /* Parse an expression.
6940
6941    expression:
6942      assignment-expression
6943      expression , assignment-expression
6944
6945    CAST_P is true if this expression is the target of a cast.
6946
6947    Returns a representation of the expression.  */
6948
6949 static tree
6950 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6951 {
6952   tree expression = NULL_TREE;
6953
6954   while (true)
6955     {
6956       tree assignment_expression;
6957
6958       /* Parse the next assignment-expression.  */
6959       assignment_expression
6960         = cp_parser_assignment_expression (parser, cast_p, pidk);
6961       /* If this is the first assignment-expression, we can just
6962          save it away.  */
6963       if (!expression)
6964         expression = assignment_expression;
6965       else
6966         expression = build_x_compound_expr (expression,
6967                                             assignment_expression,
6968                                             tf_warning_or_error);
6969       /* If the next token is not a comma, then we are done with the
6970          expression.  */
6971       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6972         break;
6973       /* Consume the `,'.  */
6974       cp_lexer_consume_token (parser->lexer);
6975       /* A comma operator cannot appear in a constant-expression.  */
6976       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6977         expression = error_mark_node;
6978     }
6979
6980   return expression;
6981 }
6982
6983 /* Parse a constant-expression.
6984
6985    constant-expression:
6986      conditional-expression
6987
6988   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6989   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6990   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6991   is false, NON_CONSTANT_P should be NULL.  */
6992
6993 static tree
6994 cp_parser_constant_expression (cp_parser* parser,
6995                                bool allow_non_constant_p,
6996                                bool *non_constant_p)
6997 {
6998   bool saved_integral_constant_expression_p;
6999   bool saved_allow_non_integral_constant_expression_p;
7000   bool saved_non_integral_constant_expression_p;
7001   tree expression;
7002
7003   /* It might seem that we could simply parse the
7004      conditional-expression, and then check to see if it were
7005      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7006      one that the compiler can figure out is constant, possibly after
7007      doing some simplifications or optimizations.  The standard has a
7008      precise definition of constant-expression, and we must honor
7009      that, even though it is somewhat more restrictive.
7010
7011      For example:
7012
7013        int i[(2, 3)];
7014
7015      is not a legal declaration, because `(2, 3)' is not a
7016      constant-expression.  The `,' operator is forbidden in a
7017      constant-expression.  However, GCC's constant-folding machinery
7018      will fold this operation to an INTEGER_CST for `3'.  */
7019
7020   /* Save the old settings.  */
7021   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7022   saved_allow_non_integral_constant_expression_p
7023     = parser->allow_non_integral_constant_expression_p;
7024   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7025   /* We are now parsing a constant-expression.  */
7026   parser->integral_constant_expression_p = true;
7027   parser->allow_non_integral_constant_expression_p
7028     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7029   parser->non_integral_constant_expression_p = false;
7030   /* Although the grammar says "conditional-expression", we parse an
7031      "assignment-expression", which also permits "throw-expression"
7032      and the use of assignment operators.  In the case that
7033      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7034      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7035      actually essential that we look for an assignment-expression.
7036      For example, cp_parser_initializer_clauses uses this function to
7037      determine whether a particular assignment-expression is in fact
7038      constant.  */
7039   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7040   /* Restore the old settings.  */
7041   parser->integral_constant_expression_p
7042     = saved_integral_constant_expression_p;
7043   parser->allow_non_integral_constant_expression_p
7044     = saved_allow_non_integral_constant_expression_p;
7045   if (cxx_dialect >= cxx0x)
7046     {
7047       /* Require an rvalue constant expression here; that's what our
7048          callers expect.  Reference constant expressions are handled
7049          separately in e.g. cp_parser_template_argument.  */
7050       bool is_const = potential_rvalue_constant_expression (expression);
7051       parser->non_integral_constant_expression_p = !is_const;
7052       if (!is_const && !allow_non_constant_p)
7053         require_potential_rvalue_constant_expression (expression);
7054     }
7055   if (allow_non_constant_p)
7056     *non_constant_p = parser->non_integral_constant_expression_p;
7057   parser->non_integral_constant_expression_p
7058     = saved_non_integral_constant_expression_p;
7059
7060   return expression;
7061 }
7062
7063 /* Parse __builtin_offsetof.
7064
7065    offsetof-expression:
7066      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7067
7068    offsetof-member-designator:
7069      id-expression
7070      | offsetof-member-designator "." id-expression
7071      | offsetof-member-designator "[" expression "]"
7072      | offsetof-member-designator "->" id-expression  */
7073
7074 static tree
7075 cp_parser_builtin_offsetof (cp_parser *parser)
7076 {
7077   int save_ice_p, save_non_ice_p;
7078   tree type, expr;
7079   cp_id_kind dummy;
7080   cp_token *token;
7081
7082   /* We're about to accept non-integral-constant things, but will
7083      definitely yield an integral constant expression.  Save and
7084      restore these values around our local parsing.  */
7085   save_ice_p = parser->integral_constant_expression_p;
7086   save_non_ice_p = parser->non_integral_constant_expression_p;
7087
7088   /* Consume the "__builtin_offsetof" token.  */
7089   cp_lexer_consume_token (parser->lexer);
7090   /* Consume the opening `('.  */
7091   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7092   /* Parse the type-id.  */
7093   type = cp_parser_type_id (parser);
7094   /* Look for the `,'.  */
7095   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7096   token = cp_lexer_peek_token (parser->lexer);
7097
7098   /* Build the (type *)null that begins the traditional offsetof macro.  */
7099   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7100                             tf_warning_or_error);
7101
7102   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7103   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7104                                                  true, &dummy, token->location);
7105   while (true)
7106     {
7107       token = cp_lexer_peek_token (parser->lexer);
7108       switch (token->type)
7109         {
7110         case CPP_OPEN_SQUARE:
7111           /* offsetof-member-designator "[" expression "]" */
7112           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7113           break;
7114
7115         case CPP_DEREF:
7116           /* offsetof-member-designator "->" identifier */
7117           expr = grok_array_decl (expr, integer_zero_node);
7118           /* FALLTHRU */
7119
7120         case CPP_DOT:
7121           /* offsetof-member-designator "." identifier */
7122           cp_lexer_consume_token (parser->lexer);
7123           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7124                                                          expr, true, &dummy,
7125                                                          token->location);
7126           break;
7127
7128         case CPP_CLOSE_PAREN:
7129           /* Consume the ")" token.  */
7130           cp_lexer_consume_token (parser->lexer);
7131           goto success;
7132
7133         default:
7134           /* Error.  We know the following require will fail, but
7135              that gives the proper error message.  */
7136           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7137           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7138           expr = error_mark_node;
7139           goto failure;
7140         }
7141     }
7142
7143  success:
7144   /* If we're processing a template, we can't finish the semantics yet.
7145      Otherwise we can fold the entire expression now.  */
7146   if (processing_template_decl)
7147     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7148   else
7149     expr = finish_offsetof (expr);
7150
7151  failure:
7152   parser->integral_constant_expression_p = save_ice_p;
7153   parser->non_integral_constant_expression_p = save_non_ice_p;
7154
7155   return expr;
7156 }
7157
7158 /* Parse a trait expression.
7159
7160    Returns a representation of the expression, the underlying type
7161    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7162
7163 static tree
7164 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7165 {
7166   cp_trait_kind kind;
7167   tree type1, type2 = NULL_TREE;
7168   bool binary = false;
7169   cp_decl_specifier_seq decl_specs;
7170
7171   switch (keyword)
7172     {
7173     case RID_HAS_NOTHROW_ASSIGN:
7174       kind = CPTK_HAS_NOTHROW_ASSIGN;
7175       break;
7176     case RID_HAS_NOTHROW_CONSTRUCTOR:
7177       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7178       break;
7179     case RID_HAS_NOTHROW_COPY:
7180       kind = CPTK_HAS_NOTHROW_COPY;
7181       break;
7182     case RID_HAS_TRIVIAL_ASSIGN:
7183       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7184       break;
7185     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7186       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7187       break;
7188     case RID_HAS_TRIVIAL_COPY:
7189       kind = CPTK_HAS_TRIVIAL_COPY;
7190       break;
7191     case RID_HAS_TRIVIAL_DESTRUCTOR:
7192       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7193       break;
7194     case RID_HAS_VIRTUAL_DESTRUCTOR:
7195       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7196       break;
7197     case RID_IS_ABSTRACT:
7198       kind = CPTK_IS_ABSTRACT;
7199       break;
7200     case RID_IS_BASE_OF:
7201       kind = CPTK_IS_BASE_OF;
7202       binary = true;
7203       break;
7204     case RID_IS_CLASS:
7205       kind = CPTK_IS_CLASS;
7206       break;
7207     case RID_IS_CONVERTIBLE_TO:
7208       kind = CPTK_IS_CONVERTIBLE_TO;
7209       binary = true;
7210       break;
7211     case RID_IS_EMPTY:
7212       kind = CPTK_IS_EMPTY;
7213       break;
7214     case RID_IS_ENUM:
7215       kind = CPTK_IS_ENUM;
7216       break;
7217     case RID_IS_LITERAL_TYPE:
7218       kind = CPTK_IS_LITERAL_TYPE;
7219       break;
7220     case RID_IS_POD:
7221       kind = CPTK_IS_POD;
7222       break;
7223     case RID_IS_POLYMORPHIC:
7224       kind = CPTK_IS_POLYMORPHIC;
7225       break;
7226     case RID_IS_STD_LAYOUT:
7227       kind = CPTK_IS_STD_LAYOUT;
7228       break;
7229     case RID_IS_TRIVIAL:
7230       kind = CPTK_IS_TRIVIAL;
7231       break;
7232     case RID_IS_UNION:
7233       kind = CPTK_IS_UNION;
7234       break;
7235     case RID_UNDERLYING_TYPE:
7236       kind = CPTK_UNDERLYING_TYPE;
7237       break;
7238     default:
7239       gcc_unreachable ();
7240     }
7241
7242   /* Consume the token.  */
7243   cp_lexer_consume_token (parser->lexer);
7244
7245   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7246
7247   type1 = cp_parser_type_id (parser);
7248
7249   if (type1 == error_mark_node)
7250     return error_mark_node;
7251
7252   /* Build a trivial decl-specifier-seq.  */
7253   clear_decl_specs (&decl_specs);
7254   decl_specs.type = type1;
7255
7256   /* Call grokdeclarator to figure out what type this is.  */
7257   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7258                           /*initialized=*/0, /*attrlist=*/NULL);
7259
7260   if (binary)
7261     {
7262       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7263  
7264       type2 = cp_parser_type_id (parser);
7265
7266       if (type2 == error_mark_node)
7267         return error_mark_node;
7268
7269       /* Build a trivial decl-specifier-seq.  */
7270       clear_decl_specs (&decl_specs);
7271       decl_specs.type = type2;
7272
7273       /* Call grokdeclarator to figure out what type this is.  */
7274       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7275                               /*initialized=*/0, /*attrlist=*/NULL);
7276     }
7277
7278   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7279
7280   /* Complete the trait expression, which may mean either processing
7281      the trait expr now or saving it for template instantiation.  */
7282   return kind != CPTK_UNDERLYING_TYPE
7283     ? finish_trait_expr (kind, type1, type2)
7284     : finish_underlying_type (type1);
7285 }
7286
7287 /* Lambdas that appear in variable initializer or default argument scope
7288    get that in their mangling, so we need to record it.  We might as well
7289    use the count for function and namespace scopes as well.  */
7290 static GTY(()) tree lambda_scope;
7291 static GTY(()) int lambda_count;
7292 typedef struct GTY(()) tree_int
7293 {
7294   tree t;
7295   int i;
7296 } tree_int;
7297 DEF_VEC_O(tree_int);
7298 DEF_VEC_ALLOC_O(tree_int,gc);
7299 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7300
7301 static void
7302 start_lambda_scope (tree decl)
7303 {
7304   tree_int ti;
7305   gcc_assert (decl);
7306   /* Once we're inside a function, we ignore other scopes and just push
7307      the function again so that popping works properly.  */
7308   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7309     decl = current_function_decl;
7310   ti.t = lambda_scope;
7311   ti.i = lambda_count;
7312   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7313   if (lambda_scope != decl)
7314     {
7315       /* Don't reset the count if we're still in the same function.  */
7316       lambda_scope = decl;
7317       lambda_count = 0;
7318     }
7319 }
7320
7321 static void
7322 record_lambda_scope (tree lambda)
7323 {
7324   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7325   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7326 }
7327
7328 static void
7329 finish_lambda_scope (void)
7330 {
7331   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7332   if (lambda_scope != p->t)
7333     {
7334       lambda_scope = p->t;
7335       lambda_count = p->i;
7336     }
7337   VEC_pop (tree_int, lambda_scope_stack);
7338 }
7339
7340 /* Parse a lambda expression.
7341
7342    lambda-expression:
7343      lambda-introducer lambda-declarator [opt] compound-statement
7344
7345    Returns a representation of the expression.  */
7346
7347 static tree
7348 cp_parser_lambda_expression (cp_parser* parser)
7349 {
7350   tree lambda_expr = build_lambda_expr ();
7351   tree type;
7352   bool ok;
7353
7354   LAMBDA_EXPR_LOCATION (lambda_expr)
7355     = cp_lexer_peek_token (parser->lexer)->location;
7356
7357   if (cp_unevaluated_operand)
7358     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7359               "lambda-expression in unevaluated context");
7360
7361   /* We may be in the middle of deferred access check.  Disable
7362      it now.  */
7363   push_deferring_access_checks (dk_no_deferred);
7364
7365   cp_parser_lambda_introducer (parser, lambda_expr);
7366
7367   type = begin_lambda_type (lambda_expr);
7368
7369   record_lambda_scope (lambda_expr);
7370
7371   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7372   determine_visibility (TYPE_NAME (type));
7373
7374   /* Now that we've started the type, add the capture fields for any
7375      explicit captures.  */
7376   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7377
7378   {
7379     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7380     unsigned int saved_num_template_parameter_lists
7381         = parser->num_template_parameter_lists;
7382
7383     parser->num_template_parameter_lists = 0;
7384
7385     /* By virtue of defining a local class, a lambda expression has access to
7386        the private variables of enclosing classes.  */
7387
7388     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7389
7390     if (ok)
7391       cp_parser_lambda_body (parser, lambda_expr);
7392     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7393       cp_parser_skip_to_end_of_block_or_statement (parser);
7394
7395     /* The capture list was built up in reverse order; fix that now.  */
7396     {
7397       tree newlist = NULL_TREE;
7398       tree elt, next;
7399
7400       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7401            elt; elt = next)
7402         {
7403           next = TREE_CHAIN (elt);
7404           TREE_CHAIN (elt) = newlist;
7405           newlist = elt;
7406         }
7407       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7408     }
7409
7410     if (ok)
7411       maybe_add_lambda_conv_op (type);
7412
7413     type = finish_struct (type, /*attributes=*/NULL_TREE);
7414
7415     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7416   }
7417
7418   pop_deferring_access_checks ();
7419
7420   /* This field is only used during parsing of the lambda.  */
7421   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
7422
7423   /* This lambda shouldn't have any proxies left at this point.  */
7424   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
7425   /* And now that we're done, push proxies for an enclosing lambda.  */
7426   insert_pending_capture_proxies ();
7427
7428   if (ok)
7429     return build_lambda_object (lambda_expr);
7430   else
7431     return error_mark_node;
7432 }
7433
7434 /* Parse the beginning of a lambda expression.
7435
7436    lambda-introducer:
7437      [ lambda-capture [opt] ]
7438
7439    LAMBDA_EXPR is the current representation of the lambda expression.  */
7440
7441 static void
7442 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7443 {
7444   /* Need commas after the first capture.  */
7445   bool first = true;
7446
7447   /* Eat the leading `['.  */
7448   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7449
7450   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7451   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7452       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7453     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7454   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7455     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7456
7457   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7458     {
7459       cp_lexer_consume_token (parser->lexer);
7460       first = false;
7461     }
7462
7463   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7464     {
7465       cp_token* capture_token;
7466       tree capture_id;
7467       tree capture_init_expr;
7468       cp_id_kind idk = CP_ID_KIND_NONE;
7469       bool explicit_init_p = false;
7470
7471       enum capture_kind_type
7472       {
7473         BY_COPY,
7474         BY_REFERENCE
7475       };
7476       enum capture_kind_type capture_kind = BY_COPY;
7477
7478       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7479         {
7480           error ("expected end of capture-list");
7481           return;
7482         }
7483
7484       if (first)
7485         first = false;
7486       else
7487         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7488
7489       /* Possibly capture `this'.  */
7490       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7491         {
7492           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
7493           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
7494             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
7495                      "with by-copy capture default");
7496           cp_lexer_consume_token (parser->lexer);
7497           add_capture (lambda_expr,
7498                        /*id=*/this_identifier,
7499                        /*initializer=*/finish_this_expr(),
7500                        /*by_reference_p=*/false,
7501                        explicit_init_p);
7502           continue;
7503         }
7504
7505       /* Remember whether we want to capture as a reference or not.  */
7506       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7507         {
7508           capture_kind = BY_REFERENCE;
7509           cp_lexer_consume_token (parser->lexer);
7510         }
7511
7512       /* Get the identifier.  */
7513       capture_token = cp_lexer_peek_token (parser->lexer);
7514       capture_id = cp_parser_identifier (parser);
7515
7516       if (capture_id == error_mark_node)
7517         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7518            delimiters, but I modified this to stop on unnested ']' as well.  It
7519            was already changed to stop on unnested '}', so the
7520            "closing_parenthesis" name is no more misleading with my change.  */
7521         {
7522           cp_parser_skip_to_closing_parenthesis (parser,
7523                                                  /*recovering=*/true,
7524                                                  /*or_comma=*/true,
7525                                                  /*consume_paren=*/true);
7526           break;
7527         }
7528
7529       /* Find the initializer for this capture.  */
7530       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7531         {
7532           /* An explicit expression exists.  */
7533           cp_lexer_consume_token (parser->lexer);
7534           pedwarn (input_location, OPT_pedantic,
7535                    "ISO C++ does not allow initializers "
7536                    "in lambda expression capture lists");
7537           capture_init_expr = cp_parser_assignment_expression (parser,
7538                                                                /*cast_p=*/true,
7539                                                                &idk);
7540           explicit_init_p = true;
7541         }
7542       else
7543         {
7544           const char* error_msg;
7545
7546           /* Turn the identifier into an id-expression.  */
7547           capture_init_expr
7548             = cp_parser_lookup_name
7549                 (parser,
7550                  capture_id,
7551                  none_type,
7552                  /*is_template=*/false,
7553                  /*is_namespace=*/false,
7554                  /*check_dependency=*/true,
7555                  /*ambiguous_decls=*/NULL,
7556                  capture_token->location);
7557
7558           capture_init_expr
7559             = finish_id_expression
7560                 (capture_id,
7561                  capture_init_expr,
7562                  parser->scope,
7563                  &idk,
7564                  /*integral_constant_expression_p=*/false,
7565                  /*allow_non_integral_constant_expression_p=*/false,
7566                  /*non_integral_constant_expression_p=*/NULL,
7567                  /*template_p=*/false,
7568                  /*done=*/true,
7569                  /*address_p=*/false,
7570                  /*template_arg_p=*/false,
7571                  &error_msg,
7572                  capture_token->location);
7573         }
7574
7575       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7576         capture_init_expr
7577           = unqualified_name_lookup_error (capture_init_expr);
7578
7579       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
7580           && !explicit_init_p)
7581         {
7582           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
7583               && capture_kind == BY_COPY)
7584             pedwarn (capture_token->location, 0, "explicit by-copy capture "
7585                      "of %qD redundant with by-copy capture default",
7586                      capture_id);
7587           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
7588               && capture_kind == BY_REFERENCE)
7589             pedwarn (capture_token->location, 0, "explicit by-reference "
7590                      "capture of %qD redundant with by-reference capture "
7591                      "default", capture_id);
7592         }
7593
7594       add_capture (lambda_expr,
7595                    capture_id,
7596                    capture_init_expr,
7597                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7598                    explicit_init_p);
7599     }
7600
7601   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7602 }
7603
7604 /* Parse the (optional) middle of a lambda expression.
7605
7606    lambda-declarator:
7607      ( parameter-declaration-clause [opt] )
7608        attribute-specifier [opt]
7609        mutable [opt]
7610        exception-specification [opt]
7611        lambda-return-type-clause [opt]
7612
7613    LAMBDA_EXPR is the current representation of the lambda expression.  */
7614
7615 static bool
7616 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7617 {
7618   /* 5.1.1.4 of the standard says:
7619        If a lambda-expression does not include a lambda-declarator, it is as if
7620        the lambda-declarator were ().
7621      This means an empty parameter list, no attributes, and no exception
7622      specification.  */
7623   tree param_list = void_list_node;
7624   tree attributes = NULL_TREE;
7625   tree exception_spec = NULL_TREE;
7626   tree t;
7627
7628   /* The lambda-declarator is optional, but must begin with an opening
7629      parenthesis if present.  */
7630   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7631     {
7632       cp_lexer_consume_token (parser->lexer);
7633
7634       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7635
7636       /* Parse parameters.  */
7637       param_list = cp_parser_parameter_declaration_clause (parser);
7638
7639       /* Default arguments shall not be specified in the
7640          parameter-declaration-clause of a lambda-declarator.  */
7641       for (t = param_list; t; t = TREE_CHAIN (t))
7642         if (TREE_PURPOSE (t))
7643           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7644                    "default argument specified for lambda parameter");
7645
7646       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7647
7648       attributes = cp_parser_attributes_opt (parser);
7649
7650       /* Parse optional `mutable' keyword.  */
7651       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7652         {
7653           cp_lexer_consume_token (parser->lexer);
7654           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7655         }
7656
7657       /* Parse optional exception specification.  */
7658       exception_spec = cp_parser_exception_specification_opt (parser);
7659
7660       /* Parse optional trailing return type.  */
7661       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7662         {
7663           cp_lexer_consume_token (parser->lexer);
7664           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7665         }
7666
7667       /* The function parameters must be in scope all the way until after the
7668          trailing-return-type in case of decltype.  */
7669       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7670         pop_binding (DECL_NAME (t), t);
7671
7672       leave_scope ();
7673     }
7674
7675   /* Create the function call operator.
7676
7677      Messing with declarators like this is no uglier than building up the
7678      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7679      other code.  */
7680   {
7681     cp_decl_specifier_seq return_type_specs;
7682     cp_declarator* declarator;
7683     tree fco;
7684     int quals;
7685     void *p;
7686
7687     clear_decl_specs (&return_type_specs);
7688     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7689       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7690     else
7691       /* Maybe we will deduce the return type later, but we can use void
7692          as a placeholder return type anyways.  */
7693       return_type_specs.type = void_type_node;
7694
7695     p = obstack_alloc (&declarator_obstack, 0);
7696
7697     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7698                                      sfk_none);
7699
7700     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7701              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7702     declarator = make_call_declarator (declarator, param_list, quals,
7703                                        VIRT_SPEC_UNSPECIFIED,
7704                                        exception_spec,
7705                                        /*late_return_type=*/NULL_TREE);
7706     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7707
7708     fco = grokmethod (&return_type_specs,
7709                       declarator,
7710                       attributes);
7711     if (fco != error_mark_node)
7712       {
7713         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7714         DECL_ARTIFICIAL (fco) = 1;
7715         /* Give the object parameter a different name.  */
7716         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
7717       }
7718
7719     finish_member_declaration (fco);
7720
7721     obstack_free (&declarator_obstack, p);
7722
7723     return (fco != error_mark_node);
7724   }
7725 }
7726
7727 /* Parse the body of a lambda expression, which is simply
7728
7729    compound-statement
7730
7731    but which requires special handling.
7732    LAMBDA_EXPR is the current representation of the lambda expression.  */
7733
7734 static void
7735 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7736 {
7737   bool nested = (current_function_decl != NULL_TREE);
7738   if (nested)
7739     push_function_context ();
7740
7741   /* Finish the function call operator
7742      - class_specifier
7743      + late_parsing_for_member
7744      + function_definition_after_declarator
7745      + ctor_initializer_opt_and_function_body  */
7746   {
7747     tree fco = lambda_function (lambda_expr);
7748     tree body;
7749     bool done = false;
7750     tree compound_stmt;
7751     tree cap;
7752
7753     /* Let the front end know that we are going to be defining this
7754        function.  */
7755     start_preparsed_function (fco,
7756                               NULL_TREE,
7757                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7758
7759     start_lambda_scope (fco);
7760     body = begin_function_body ();
7761
7762     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
7763       goto out;
7764
7765     /* Push the proxies for any explicit captures.  */
7766     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
7767          cap = TREE_CHAIN (cap))
7768       build_capture_proxy (TREE_PURPOSE (cap));
7769
7770     compound_stmt = begin_compound_stmt (0);
7771
7772     /* 5.1.1.4 of the standard says:
7773          If a lambda-expression does not include a trailing-return-type, it
7774          is as if the trailing-return-type denotes the following type:
7775           * if the compound-statement is of the form
7776                { return attribute-specifier [opt] expression ; }
7777              the type of the returned expression after lvalue-to-rvalue
7778              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7779              (_conv.array_ 4.2), and function-to-pointer conversion
7780              (_conv.func_ 4.3);
7781           * otherwise, void.  */
7782
7783     /* In a lambda that has neither a lambda-return-type-clause
7784        nor a deducible form, errors should be reported for return statements
7785        in the body.  Since we used void as the placeholder return type, parsing
7786        the body as usual will give such desired behavior.  */
7787     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7788         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
7789         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
7790       {
7791         tree expr = NULL_TREE;
7792         cp_id_kind idk = CP_ID_KIND_NONE;
7793
7794         /* Parse tentatively in case there's more after the initial return
7795            statement.  */
7796         cp_parser_parse_tentatively (parser);
7797
7798         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7799
7800         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7801
7802         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7803         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7804
7805         if (cp_parser_parse_definitely (parser))
7806           {
7807             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7808
7809             /* Will get error here if type not deduced yet.  */
7810             finish_return_stmt (expr);
7811
7812             done = true;
7813           }
7814       }
7815
7816     if (!done)
7817       {
7818         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7819           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7820         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
7821           cp_parser_label_declaration (parser);
7822         cp_parser_statement_seq_opt (parser, NULL_TREE);
7823         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7824         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7825       }
7826
7827     finish_compound_stmt (compound_stmt);
7828
7829   out:
7830     finish_function_body (body);
7831     finish_lambda_scope ();
7832
7833     /* Finish the function and generate code for it if necessary.  */
7834     expand_or_defer_fn (finish_function (/*inline*/2));
7835   }
7836
7837   if (nested)
7838     pop_function_context();
7839 }
7840
7841 /* Statements [gram.stmt.stmt]  */
7842
7843 /* Parse a statement.
7844
7845    statement:
7846      labeled-statement
7847      expression-statement
7848      compound-statement
7849      selection-statement
7850      iteration-statement
7851      jump-statement
7852      declaration-statement
7853      try-block
7854
7855   IN_COMPOUND is true when the statement is nested inside a
7856   cp_parser_compound_statement; this matters for certain pragmas.
7857
7858   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7859   is a (possibly labeled) if statement which is not enclosed in braces
7860   and has an else clause.  This is used to implement -Wparentheses.  */
7861
7862 static void
7863 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7864                      bool in_compound, bool *if_p)
7865 {
7866   tree statement;
7867   cp_token *token;
7868   location_t statement_location;
7869
7870  restart:
7871   if (if_p != NULL)
7872     *if_p = false;
7873   /* There is no statement yet.  */
7874   statement = NULL_TREE;
7875   /* Peek at the next token.  */
7876   token = cp_lexer_peek_token (parser->lexer);
7877   /* Remember the location of the first token in the statement.  */
7878   statement_location = token->location;
7879   /* If this is a keyword, then that will often determine what kind of
7880      statement we have.  */
7881   if (token->type == CPP_KEYWORD)
7882     {
7883       enum rid keyword = token->keyword;
7884
7885       switch (keyword)
7886         {
7887         case RID_CASE:
7888         case RID_DEFAULT:
7889           /* Looks like a labeled-statement with a case label.
7890              Parse the label, and then use tail recursion to parse
7891              the statement.  */
7892           cp_parser_label_for_labeled_statement (parser);
7893           goto restart;
7894
7895         case RID_IF:
7896         case RID_SWITCH:
7897           statement = cp_parser_selection_statement (parser, if_p);
7898           break;
7899
7900         case RID_WHILE:
7901         case RID_DO:
7902         case RID_FOR:
7903           statement = cp_parser_iteration_statement (parser);
7904           break;
7905
7906         case RID_BREAK:
7907         case RID_CONTINUE:
7908         case RID_RETURN:
7909         case RID_GOTO:
7910           statement = cp_parser_jump_statement (parser);
7911           break;
7912
7913           /* Objective-C++ exception-handling constructs.  */
7914         case RID_AT_TRY:
7915         case RID_AT_CATCH:
7916         case RID_AT_FINALLY:
7917         case RID_AT_SYNCHRONIZED:
7918         case RID_AT_THROW:
7919           statement = cp_parser_objc_statement (parser);
7920           break;
7921
7922         case RID_TRY:
7923           statement = cp_parser_try_block (parser);
7924           break;
7925
7926         case RID_NAMESPACE:
7927           /* This must be a namespace alias definition.  */
7928           cp_parser_declaration_statement (parser);
7929           return;
7930           
7931         default:
7932           /* It might be a keyword like `int' that can start a
7933              declaration-statement.  */
7934           break;
7935         }
7936     }
7937   else if (token->type == CPP_NAME)
7938     {
7939       /* If the next token is a `:', then we are looking at a
7940          labeled-statement.  */
7941       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7942       if (token->type == CPP_COLON)
7943         {
7944           /* Looks like a labeled-statement with an ordinary label.
7945              Parse the label, and then use tail recursion to parse
7946              the statement.  */
7947           cp_parser_label_for_labeled_statement (parser);
7948           goto restart;
7949         }
7950     }
7951   /* Anything that starts with a `{' must be a compound-statement.  */
7952   else if (token->type == CPP_OPEN_BRACE)
7953     statement = cp_parser_compound_statement (parser, NULL, false, false);
7954   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7955      a statement all its own.  */
7956   else if (token->type == CPP_PRAGMA)
7957     {
7958       /* Only certain OpenMP pragmas are attached to statements, and thus
7959          are considered statements themselves.  All others are not.  In
7960          the context of a compound, accept the pragma as a "statement" and
7961          return so that we can check for a close brace.  Otherwise we
7962          require a real statement and must go back and read one.  */
7963       if (in_compound)
7964         cp_parser_pragma (parser, pragma_compound);
7965       else if (!cp_parser_pragma (parser, pragma_stmt))
7966         goto restart;
7967       return;
7968     }
7969   else if (token->type == CPP_EOF)
7970     {
7971       cp_parser_error (parser, "expected statement");
7972       return;
7973     }
7974
7975   /* Everything else must be a declaration-statement or an
7976      expression-statement.  Try for the declaration-statement
7977      first, unless we are looking at a `;', in which case we know that
7978      we have an expression-statement.  */
7979   if (!statement)
7980     {
7981       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7982         {
7983           cp_parser_parse_tentatively (parser);
7984           /* Try to parse the declaration-statement.  */
7985           cp_parser_declaration_statement (parser);
7986           /* If that worked, we're done.  */
7987           if (cp_parser_parse_definitely (parser))
7988             return;
7989         }
7990       /* Look for an expression-statement instead.  */
7991       statement = cp_parser_expression_statement (parser, in_statement_expr);
7992     }
7993
7994   /* Set the line number for the statement.  */
7995   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7996     SET_EXPR_LOCATION (statement, statement_location);
7997 }
7998
7999 /* Parse the label for a labeled-statement, i.e.
8000
8001    identifier :
8002    case constant-expression :
8003    default :
8004
8005    GNU Extension:
8006    case constant-expression ... constant-expression : statement
8007
8008    When a label is parsed without errors, the label is added to the
8009    parse tree by the finish_* functions, so this function doesn't
8010    have to return the label.  */
8011
8012 static void
8013 cp_parser_label_for_labeled_statement (cp_parser* parser)
8014 {
8015   cp_token *token;
8016   tree label = NULL_TREE;
8017   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8018
8019   /* The next token should be an identifier.  */
8020   token = cp_lexer_peek_token (parser->lexer);
8021   if (token->type != CPP_NAME
8022       && token->type != CPP_KEYWORD)
8023     {
8024       cp_parser_error (parser, "expected labeled-statement");
8025       return;
8026     }
8027
8028   parser->colon_corrects_to_scope_p = false;
8029   switch (token->keyword)
8030     {
8031     case RID_CASE:
8032       {
8033         tree expr, expr_hi;
8034         cp_token *ellipsis;
8035
8036         /* Consume the `case' token.  */
8037         cp_lexer_consume_token (parser->lexer);
8038         /* Parse the constant-expression.  */
8039         expr = cp_parser_constant_expression (parser,
8040                                               /*allow_non_constant_p=*/false,
8041                                               NULL);
8042
8043         ellipsis = cp_lexer_peek_token (parser->lexer);
8044         if (ellipsis->type == CPP_ELLIPSIS)
8045           {
8046             /* Consume the `...' token.  */
8047             cp_lexer_consume_token (parser->lexer);
8048             expr_hi =
8049               cp_parser_constant_expression (parser,
8050                                              /*allow_non_constant_p=*/false,
8051                                              NULL);
8052             /* We don't need to emit warnings here, as the common code
8053                will do this for us.  */
8054           }
8055         else
8056           expr_hi = NULL_TREE;
8057
8058         if (parser->in_switch_statement_p)
8059           finish_case_label (token->location, expr, expr_hi);
8060         else
8061           error_at (token->location,
8062                     "case label %qE not within a switch statement",
8063                     expr);
8064       }
8065       break;
8066
8067     case RID_DEFAULT:
8068       /* Consume the `default' token.  */
8069       cp_lexer_consume_token (parser->lexer);
8070
8071       if (parser->in_switch_statement_p)
8072         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8073       else
8074         error_at (token->location, "case label not within a switch statement");
8075       break;
8076
8077     default:
8078       /* Anything else must be an ordinary label.  */
8079       label = finish_label_stmt (cp_parser_identifier (parser));
8080       break;
8081     }
8082
8083   /* Require the `:' token.  */
8084   cp_parser_require (parser, CPP_COLON, RT_COLON);
8085
8086   /* An ordinary label may optionally be followed by attributes.
8087      However, this is only permitted if the attributes are then
8088      followed by a semicolon.  This is because, for backward
8089      compatibility, when parsing
8090        lab: __attribute__ ((unused)) int i;
8091      we want the attribute to attach to "i", not "lab".  */
8092   if (label != NULL_TREE
8093       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8094     {
8095       tree attrs;
8096
8097       cp_parser_parse_tentatively (parser);
8098       attrs = cp_parser_attributes_opt (parser);
8099       if (attrs == NULL_TREE
8100           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8101         cp_parser_abort_tentative_parse (parser);
8102       else if (!cp_parser_parse_definitely (parser))
8103         ;
8104       else
8105         cplus_decl_attributes (&label, attrs, 0);
8106     }
8107
8108   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8109 }
8110
8111 /* Parse an expression-statement.
8112
8113    expression-statement:
8114      expression [opt] ;
8115
8116    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8117    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8118    indicates whether this expression-statement is part of an
8119    expression statement.  */
8120
8121 static tree
8122 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8123 {
8124   tree statement = NULL_TREE;
8125   cp_token *token = cp_lexer_peek_token (parser->lexer);
8126
8127   /* If the next token is a ';', then there is no expression
8128      statement.  */
8129   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8130     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8131
8132   /* Give a helpful message for "A<T>::type t;" and the like.  */
8133   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8134       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8135     {
8136       if (TREE_CODE (statement) == SCOPE_REF)
8137         error_at (token->location, "need %<typename%> before %qE because "
8138                   "%qT is a dependent scope",
8139                   statement, TREE_OPERAND (statement, 0));
8140       else if (is_overloaded_fn (statement)
8141                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8142         {
8143           /* A::A a; */
8144           tree fn = get_first_fn (statement);
8145           error_at (token->location,
8146                     "%<%T::%D%> names the constructor, not the type",
8147                     DECL_CONTEXT (fn), DECL_NAME (fn));
8148         }
8149     }
8150
8151   /* Consume the final `;'.  */
8152   cp_parser_consume_semicolon_at_end_of_statement (parser);
8153
8154   if (in_statement_expr
8155       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8156     /* This is the final expression statement of a statement
8157        expression.  */
8158     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8159   else if (statement)
8160     statement = finish_expr_stmt (statement);
8161   else
8162     finish_stmt ();
8163
8164   return statement;
8165 }
8166
8167 /* Parse a compound-statement.
8168
8169    compound-statement:
8170      { statement-seq [opt] }
8171
8172    GNU extension:
8173
8174    compound-statement:
8175      { label-declaration-seq [opt] statement-seq [opt] }
8176
8177    label-declaration-seq:
8178      label-declaration
8179      label-declaration-seq label-declaration
8180
8181    Returns a tree representing the statement.  */
8182
8183 static tree
8184 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8185                               bool in_try, bool function_body)
8186 {
8187   tree compound_stmt;
8188
8189   /* Consume the `{'.  */
8190   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8191     return error_mark_node;
8192   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8193       && !function_body)
8194     pedwarn (input_location, OPT_pedantic,
8195              "compound-statement in constexpr function");
8196   /* Begin the compound-statement.  */
8197   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8198   /* If the next keyword is `__label__' we have a label declaration.  */
8199   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8200     cp_parser_label_declaration (parser);
8201   /* Parse an (optional) statement-seq.  */
8202   cp_parser_statement_seq_opt (parser, in_statement_expr);
8203   /* Finish the compound-statement.  */
8204   finish_compound_stmt (compound_stmt);
8205   /* Consume the `}'.  */
8206   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8207
8208   return compound_stmt;
8209 }
8210
8211 /* Parse an (optional) statement-seq.
8212
8213    statement-seq:
8214      statement
8215      statement-seq [opt] statement  */
8216
8217 static void
8218 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8219 {
8220   /* Scan statements until there aren't any more.  */
8221   while (true)
8222     {
8223       cp_token *token = cp_lexer_peek_token (parser->lexer);
8224
8225       /* If we are looking at a `}', then we have run out of
8226          statements; the same is true if we have reached the end
8227          of file, or have stumbled upon a stray '@end'.  */
8228       if (token->type == CPP_CLOSE_BRACE
8229           || token->type == CPP_EOF
8230           || token->type == CPP_PRAGMA_EOL
8231           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8232         break;
8233       
8234       /* If we are in a compound statement and find 'else' then
8235          something went wrong.  */
8236       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8237         {
8238           if (parser->in_statement & IN_IF_STMT) 
8239             break;
8240           else
8241             {
8242               token = cp_lexer_consume_token (parser->lexer);
8243               error_at (token->location, "%<else%> without a previous %<if%>");
8244             }
8245         }
8246
8247       /* Parse the statement.  */
8248       cp_parser_statement (parser, in_statement_expr, true, NULL);
8249     }
8250 }
8251
8252 /* Parse a selection-statement.
8253
8254    selection-statement:
8255      if ( condition ) statement
8256      if ( condition ) statement else statement
8257      switch ( condition ) statement
8258
8259    Returns the new IF_STMT or SWITCH_STMT.
8260
8261    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8262    is a (possibly labeled) if statement which is not enclosed in
8263    braces and has an else clause.  This is used to implement
8264    -Wparentheses.  */
8265
8266 static tree
8267 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8268 {
8269   cp_token *token;
8270   enum rid keyword;
8271
8272   if (if_p != NULL)
8273     *if_p = false;
8274
8275   /* Peek at the next token.  */
8276   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8277
8278   /* See what kind of keyword it is.  */
8279   keyword = token->keyword;
8280   switch (keyword)
8281     {
8282     case RID_IF:
8283     case RID_SWITCH:
8284       {
8285         tree statement;
8286         tree condition;
8287
8288         /* Look for the `('.  */
8289         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8290           {
8291             cp_parser_skip_to_end_of_statement (parser);
8292             return error_mark_node;
8293           }
8294
8295         /* Begin the selection-statement.  */
8296         if (keyword == RID_IF)
8297           statement = begin_if_stmt ();
8298         else
8299           statement = begin_switch_stmt ();
8300
8301         /* Parse the condition.  */
8302         condition = cp_parser_condition (parser);
8303         /* Look for the `)'.  */
8304         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8305           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8306                                                  /*consume_paren=*/true);
8307
8308         if (keyword == RID_IF)
8309           {
8310             bool nested_if;
8311             unsigned char in_statement;
8312
8313             /* Add the condition.  */
8314             finish_if_stmt_cond (condition, statement);
8315
8316             /* Parse the then-clause.  */
8317             in_statement = parser->in_statement;
8318             parser->in_statement |= IN_IF_STMT;
8319             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8320               {
8321                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8322                 add_stmt (build_empty_stmt (loc));
8323                 cp_lexer_consume_token (parser->lexer);
8324                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8325                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8326                               "empty body in an %<if%> statement");
8327                 nested_if = false;
8328               }
8329             else
8330               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8331             parser->in_statement = in_statement;
8332
8333             finish_then_clause (statement);
8334
8335             /* If the next token is `else', parse the else-clause.  */
8336             if (cp_lexer_next_token_is_keyword (parser->lexer,
8337                                                 RID_ELSE))
8338               {
8339                 /* Consume the `else' keyword.  */
8340                 cp_lexer_consume_token (parser->lexer);
8341                 begin_else_clause (statement);
8342                 /* Parse the else-clause.  */
8343                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8344                   {
8345                     location_t loc;
8346                     loc = cp_lexer_peek_token (parser->lexer)->location;
8347                     warning_at (loc,
8348                                 OPT_Wempty_body, "suggest braces around "
8349                                 "empty body in an %<else%> statement");
8350                     add_stmt (build_empty_stmt (loc));
8351                     cp_lexer_consume_token (parser->lexer);
8352                   }
8353                 else
8354                   cp_parser_implicitly_scoped_statement (parser, NULL);
8355
8356                 finish_else_clause (statement);
8357
8358                 /* If we are currently parsing a then-clause, then
8359                    IF_P will not be NULL.  We set it to true to
8360                    indicate that this if statement has an else clause.
8361                    This may trigger the Wparentheses warning below
8362                    when we get back up to the parent if statement.  */
8363                 if (if_p != NULL)
8364                   *if_p = true;
8365               }
8366             else
8367               {
8368                 /* This if statement does not have an else clause.  If
8369                    NESTED_IF is true, then the then-clause is an if
8370                    statement which does have an else clause.  We warn
8371                    about the potential ambiguity.  */
8372                 if (nested_if)
8373                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8374                               "suggest explicit braces to avoid ambiguous"
8375                               " %<else%>");
8376               }
8377
8378             /* Now we're all done with the if-statement.  */
8379             finish_if_stmt (statement);
8380           }
8381         else
8382           {
8383             bool in_switch_statement_p;
8384             unsigned char in_statement;
8385
8386             /* Add the condition.  */
8387             finish_switch_cond (condition, statement);
8388
8389             /* Parse the body of the switch-statement.  */
8390             in_switch_statement_p = parser->in_switch_statement_p;
8391             in_statement = parser->in_statement;
8392             parser->in_switch_statement_p = true;
8393             parser->in_statement |= IN_SWITCH_STMT;
8394             cp_parser_implicitly_scoped_statement (parser, NULL);
8395             parser->in_switch_statement_p = in_switch_statement_p;
8396             parser->in_statement = in_statement;
8397
8398             /* Now we're all done with the switch-statement.  */
8399             finish_switch_stmt (statement);
8400           }
8401
8402         return statement;
8403       }
8404       break;
8405
8406     default:
8407       cp_parser_error (parser, "expected selection-statement");
8408       return error_mark_node;
8409     }
8410 }
8411
8412 /* Parse a condition.
8413
8414    condition:
8415      expression
8416      type-specifier-seq declarator = initializer-clause
8417      type-specifier-seq declarator braced-init-list
8418
8419    GNU Extension:
8420
8421    condition:
8422      type-specifier-seq declarator asm-specification [opt]
8423        attributes [opt] = assignment-expression
8424
8425    Returns the expression that should be tested.  */
8426
8427 static tree
8428 cp_parser_condition (cp_parser* parser)
8429 {
8430   cp_decl_specifier_seq type_specifiers;
8431   const char *saved_message;
8432   int declares_class_or_enum;
8433
8434   /* Try the declaration first.  */
8435   cp_parser_parse_tentatively (parser);
8436   /* New types are not allowed in the type-specifier-seq for a
8437      condition.  */
8438   saved_message = parser->type_definition_forbidden_message;
8439   parser->type_definition_forbidden_message
8440     = G_("types may not be defined in conditions");
8441   /* Parse the type-specifier-seq.  */
8442   cp_parser_decl_specifier_seq (parser,
8443                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8444                                 &type_specifiers,
8445                                 &declares_class_or_enum);
8446   /* Restore the saved message.  */
8447   parser->type_definition_forbidden_message = saved_message;
8448   /* If all is well, we might be looking at a declaration.  */
8449   if (!cp_parser_error_occurred (parser))
8450     {
8451       tree decl;
8452       tree asm_specification;
8453       tree attributes;
8454       cp_declarator *declarator;
8455       tree initializer = NULL_TREE;
8456
8457       /* Parse the declarator.  */
8458       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8459                                          /*ctor_dtor_or_conv_p=*/NULL,
8460                                          /*parenthesized_p=*/NULL,
8461                                          /*member_p=*/false);
8462       /* Parse the attributes.  */
8463       attributes = cp_parser_attributes_opt (parser);
8464       /* Parse the asm-specification.  */
8465       asm_specification = cp_parser_asm_specification_opt (parser);
8466       /* If the next token is not an `=' or '{', then we might still be
8467          looking at an expression.  For example:
8468
8469            if (A(a).x)
8470
8471          looks like a decl-specifier-seq and a declarator -- but then
8472          there is no `=', so this is an expression.  */
8473       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8474           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8475         cp_parser_simulate_error (parser);
8476         
8477       /* If we did see an `=' or '{', then we are looking at a declaration
8478          for sure.  */
8479       if (cp_parser_parse_definitely (parser))
8480         {
8481           tree pushed_scope;
8482           bool non_constant_p;
8483           bool flags = LOOKUP_ONLYCONVERTING;
8484
8485           /* Create the declaration.  */
8486           decl = start_decl (declarator, &type_specifiers,
8487                              /*initialized_p=*/true,
8488                              attributes, /*prefix_attributes=*/NULL_TREE,
8489                              &pushed_scope);
8490
8491           /* Parse the initializer.  */
8492           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8493             {
8494               initializer = cp_parser_braced_list (parser, &non_constant_p);
8495               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8496               flags = 0;
8497             }
8498           else
8499             {
8500               /* Consume the `='.  */
8501               cp_parser_require (parser, CPP_EQ, RT_EQ);
8502               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8503             }
8504           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8505             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8506
8507           /* Process the initializer.  */
8508           cp_finish_decl (decl,
8509                           initializer, !non_constant_p,
8510                           asm_specification,
8511                           flags);
8512
8513           if (pushed_scope)
8514             pop_scope (pushed_scope);
8515
8516           return convert_from_reference (decl);
8517         }
8518     }
8519   /* If we didn't even get past the declarator successfully, we are
8520      definitely not looking at a declaration.  */
8521   else
8522     cp_parser_abort_tentative_parse (parser);
8523
8524   /* Otherwise, we are looking at an expression.  */
8525   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8526 }
8527
8528 /* Parses a for-statement or range-for-statement until the closing ')',
8529    not included. */
8530
8531 static tree
8532 cp_parser_for (cp_parser *parser)
8533 {
8534   tree init, scope, decl;
8535   bool is_range_for;
8536
8537   /* Begin the for-statement.  */
8538   scope = begin_for_scope (&init);
8539
8540   /* Parse the initialization.  */
8541   is_range_for = cp_parser_for_init_statement (parser, &decl);
8542
8543   if (is_range_for)
8544     return cp_parser_range_for (parser, scope, init, decl);
8545   else
8546     return cp_parser_c_for (parser, scope, init);
8547 }
8548
8549 static tree
8550 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8551 {
8552   /* Normal for loop */
8553   tree condition = NULL_TREE;
8554   tree expression = NULL_TREE;
8555   tree stmt;
8556
8557   stmt = begin_for_stmt (scope, init);
8558   /* The for-init-statement has already been parsed in
8559      cp_parser_for_init_statement, so no work is needed here.  */
8560   finish_for_init_stmt (stmt);
8561
8562   /* If there's a condition, process it.  */
8563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8564     condition = cp_parser_condition (parser);
8565   finish_for_cond (condition, stmt);
8566   /* Look for the `;'.  */
8567   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8568
8569   /* If there's an expression, process it.  */
8570   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8571     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8572   finish_for_expr (expression, stmt);
8573
8574   return stmt;
8575 }
8576
8577 /* Tries to parse a range-based for-statement:
8578
8579   range-based-for:
8580     decl-specifier-seq declarator : expression
8581
8582   The decl-specifier-seq declarator and the `:' are already parsed by
8583   cp_parser_for_init_statement. If processing_template_decl it returns a
8584   newly created RANGE_FOR_STMT; if not, it is converted to a
8585   regular FOR_STMT.  */
8586
8587 static tree
8588 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8589 {
8590   tree stmt, range_expr;
8591
8592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8593     {
8594       bool expr_non_constant_p;
8595       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8596     }
8597   else
8598     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8599
8600   /* If in template, STMT is converted to a normal for-statement
8601      at instantiation. If not, it is done just ahead. */
8602   if (processing_template_decl)
8603     {
8604       stmt = begin_range_for_stmt (scope, init);
8605       finish_range_for_decl (stmt, range_decl, range_expr);
8606     }
8607   else
8608     {
8609       stmt = begin_for_stmt (scope, init);
8610       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8611     }
8612   return stmt;
8613 }
8614
8615 /* Converts a range-based for-statement into a normal
8616    for-statement, as per the definition.
8617
8618       for (RANGE_DECL : RANGE_EXPR)
8619         BLOCK
8620
8621    should be equivalent to:
8622
8623       {
8624         auto &&__range = RANGE_EXPR;
8625         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8626               __begin != __end;
8627               ++__begin)
8628           {
8629               RANGE_DECL = *__begin;
8630               BLOCK
8631           }
8632       }
8633
8634    If RANGE_EXPR is an array:
8635         BEGIN_EXPR = __range
8636         END_EXPR = __range + ARRAY_SIZE(__range)
8637    Else if RANGE_EXPR has a member 'begin' or 'end':
8638         BEGIN_EXPR = __range.begin()
8639         END_EXPR = __range.end()
8640    Else:
8641         BEGIN_EXPR = begin(__range)
8642         END_EXPR = end(__range);
8643
8644    If __range has a member 'begin' but not 'end', or vice versa, we must
8645    still use the second alternative (it will surely fail, however).
8646    When calling begin()/end() in the third alternative we must use
8647    argument dependent lookup, but always considering 'std' as an associated
8648    namespace.  */
8649
8650 tree
8651 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8652 {
8653   tree range_type, range_temp;
8654   tree begin, end;
8655   tree iter_type, begin_expr, end_expr;
8656   tree condition, expression;
8657
8658   if (range_decl == error_mark_node || range_expr == error_mark_node)
8659     /* If an error happened previously do nothing or else a lot of
8660        unhelpful errors would be issued.  */
8661     begin_expr = end_expr = iter_type = error_mark_node;
8662   else
8663     {
8664       /* Find out the type deduced by the declaration
8665          `auto &&__range = range_expr'.  */
8666       range_type = cp_build_reference_type (make_auto (), true);
8667       range_type = do_auto_deduction (range_type, range_expr,
8668                                       type_uses_auto (range_type));
8669
8670       /* Create the __range variable.  */
8671       range_temp = build_decl (input_location, VAR_DECL,
8672                                get_identifier ("__for_range"), range_type);
8673       TREE_USED (range_temp) = 1;
8674       DECL_ARTIFICIAL (range_temp) = 1;
8675       pushdecl (range_temp);
8676       cp_finish_decl (range_temp, range_expr,
8677                       /*is_constant_init*/false, NULL_TREE,
8678                       LOOKUP_ONLYCONVERTING);
8679
8680       range_temp = convert_from_reference (range_temp);
8681       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8682                                                       &begin_expr, &end_expr);
8683     }
8684
8685   /* The new for initialization statement.  */
8686   begin = build_decl (input_location, VAR_DECL,
8687                       get_identifier ("__for_begin"), iter_type);
8688   TREE_USED (begin) = 1;
8689   DECL_ARTIFICIAL (begin) = 1;
8690   pushdecl (begin);
8691   cp_finish_decl (begin, begin_expr,
8692                   /*is_constant_init*/false, NULL_TREE,
8693                   LOOKUP_ONLYCONVERTING);
8694
8695   end = build_decl (input_location, VAR_DECL,
8696                     get_identifier ("__for_end"), iter_type);
8697   TREE_USED (end) = 1;
8698   DECL_ARTIFICIAL (end) = 1;
8699   pushdecl (end);
8700   cp_finish_decl (end, end_expr,
8701                   /*is_constant_init*/false, NULL_TREE,
8702                   LOOKUP_ONLYCONVERTING);
8703
8704   finish_for_init_stmt (statement);
8705
8706   /* The new for condition.  */
8707   condition = build_x_binary_op (NE_EXPR,
8708                                  begin, ERROR_MARK,
8709                                  end, ERROR_MARK,
8710                                  NULL, tf_warning_or_error);
8711   finish_for_cond (condition, statement);
8712
8713   /* The new increment expression.  */
8714   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8715   finish_for_expr (expression, statement);
8716
8717   /* The declaration is initialized with *__begin inside the loop body.  */
8718   cp_finish_decl (range_decl,
8719                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8720                   /*is_constant_init*/false, NULL_TREE,
8721                   LOOKUP_ONLYCONVERTING);
8722
8723   return statement;
8724 }
8725
8726 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8727    We need to solve both at the same time because the method used
8728    depends on the existence of members begin or end.
8729    Returns the type deduced for the iterator expression.  */
8730
8731 static tree
8732 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8733 {
8734   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8735     {
8736       error ("range-based %<for%> expression of type %qT "
8737              "has incomplete type", TREE_TYPE (range));
8738       *begin = *end = error_mark_node;
8739       return error_mark_node;
8740     }
8741   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8742     {
8743       /* If RANGE is an array, we will use pointer arithmetic.  */
8744       *begin = range;
8745       *end = build_binary_op (input_location, PLUS_EXPR,
8746                               range,
8747                               array_type_nelts_top (TREE_TYPE (range)),
8748                               0);
8749       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8750     }
8751   else
8752     {
8753       /* If it is not an array, we must do a bit of magic.  */
8754       tree id_begin, id_end;
8755       tree member_begin, member_end;
8756
8757       *begin = *end = error_mark_node;
8758
8759       id_begin = get_identifier ("begin");
8760       id_end = get_identifier ("end");
8761       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8762                                     /*protect=*/2, /*want_type=*/false);
8763       member_end = lookup_member (TREE_TYPE (range), id_end,
8764                                   /*protect=*/2, /*want_type=*/false);
8765
8766       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8767         {
8768           /* Use the member functions.  */
8769           if (member_begin != NULL_TREE)
8770             *begin = cp_parser_range_for_member_function (range, id_begin);
8771           else
8772             error ("range-based %<for%> expression of type %qT has an "
8773                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8774
8775           if (member_end != NULL_TREE)
8776             *end = cp_parser_range_for_member_function (range, id_end);
8777           else
8778             error ("range-based %<for%> expression of type %qT has a "
8779                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8780         }
8781       else
8782         {
8783           /* Use global functions with ADL.  */
8784           VEC(tree,gc) *vec;
8785           vec = make_tree_vector ();
8786
8787           VEC_safe_push (tree, gc, vec, range);
8788
8789           member_begin = perform_koenig_lookup (id_begin, vec,
8790                                                 /*include_std=*/true,
8791                                                 tf_warning_or_error);
8792           *begin = finish_call_expr (member_begin, &vec, false, true,
8793                                      tf_warning_or_error);
8794           member_end = perform_koenig_lookup (id_end, vec,
8795                                               /*include_std=*/true,
8796                                               tf_warning_or_error);
8797           *end = finish_call_expr (member_end, &vec, false, true,
8798                                    tf_warning_or_error);
8799
8800           release_tree_vector (vec);
8801         }
8802
8803       /* Last common checks.  */
8804       if (*begin == error_mark_node || *end == error_mark_node)
8805         {
8806           /* If one of the expressions is an error do no more checks.  */
8807           *begin = *end = error_mark_node;
8808           return error_mark_node;
8809         }
8810       else
8811         {
8812           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8813           /* The unqualified type of the __begin and __end temporaries should
8814              be the same, as required by the multiple auto declaration.  */
8815           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8816             error ("inconsistent begin/end types in range-based %<for%> "
8817                    "statement: %qT and %qT",
8818                    TREE_TYPE (*begin), TREE_TYPE (*end));
8819           return iter_type;
8820         }
8821     }
8822 }
8823
8824 /* Helper function for cp_parser_perform_range_for_lookup.
8825    Builds a tree for RANGE.IDENTIFIER().  */
8826
8827 static tree
8828 cp_parser_range_for_member_function (tree range, tree identifier)
8829 {
8830   tree member, res;
8831   VEC(tree,gc) *vec;
8832
8833   member = finish_class_member_access_expr (range, identifier,
8834                                             false, tf_warning_or_error);
8835   if (member == error_mark_node)
8836     return error_mark_node;
8837
8838   vec = make_tree_vector ();
8839   res = finish_call_expr (member, &vec,
8840                           /*disallow_virtual=*/false,
8841                           /*koenig_p=*/false,
8842                           tf_warning_or_error);
8843   release_tree_vector (vec);
8844   return res;
8845 }
8846
8847 /* Parse an iteration-statement.
8848
8849    iteration-statement:
8850      while ( condition ) statement
8851      do statement while ( expression ) ;
8852      for ( for-init-statement condition [opt] ; expression [opt] )
8853        statement
8854
8855    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8856
8857 static tree
8858 cp_parser_iteration_statement (cp_parser* parser)
8859 {
8860   cp_token *token;
8861   enum rid keyword;
8862   tree statement;
8863   unsigned char in_statement;
8864
8865   /* Peek at the next token.  */
8866   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8867   if (!token)
8868     return error_mark_node;
8869
8870   /* Remember whether or not we are already within an iteration
8871      statement.  */
8872   in_statement = parser->in_statement;
8873
8874   /* See what kind of keyword it is.  */
8875   keyword = token->keyword;
8876   switch (keyword)
8877     {
8878     case RID_WHILE:
8879       {
8880         tree condition;
8881
8882         /* Begin the while-statement.  */
8883         statement = begin_while_stmt ();
8884         /* Look for the `('.  */
8885         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8886         /* Parse the condition.  */
8887         condition = cp_parser_condition (parser);
8888         finish_while_stmt_cond (condition, statement);
8889         /* Look for the `)'.  */
8890         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8891         /* Parse the dependent statement.  */
8892         parser->in_statement = IN_ITERATION_STMT;
8893         cp_parser_already_scoped_statement (parser);
8894         parser->in_statement = in_statement;
8895         /* We're done with the while-statement.  */
8896         finish_while_stmt (statement);
8897       }
8898       break;
8899
8900     case RID_DO:
8901       {
8902         tree expression;
8903
8904         /* Begin the do-statement.  */
8905         statement = begin_do_stmt ();
8906         /* Parse the body of the do-statement.  */
8907         parser->in_statement = IN_ITERATION_STMT;
8908         cp_parser_implicitly_scoped_statement (parser, NULL);
8909         parser->in_statement = in_statement;
8910         finish_do_body (statement);
8911         /* Look for the `while' keyword.  */
8912         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8913         /* Look for the `('.  */
8914         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8915         /* Parse the expression.  */
8916         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8917         /* We're done with the do-statement.  */
8918         finish_do_stmt (expression, statement);
8919         /* Look for the `)'.  */
8920         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8921         /* Look for the `;'.  */
8922         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8923       }
8924       break;
8925
8926     case RID_FOR:
8927       {
8928         /* Look for the `('.  */
8929         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8930
8931         statement = cp_parser_for (parser);
8932
8933         /* Look for the `)'.  */
8934         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8935
8936         /* Parse the body of the for-statement.  */
8937         parser->in_statement = IN_ITERATION_STMT;
8938         cp_parser_already_scoped_statement (parser);
8939         parser->in_statement = in_statement;
8940
8941         /* We're done with the for-statement.  */
8942         finish_for_stmt (statement);
8943       }
8944       break;
8945
8946     default:
8947       cp_parser_error (parser, "expected iteration-statement");
8948       statement = error_mark_node;
8949       break;
8950     }
8951
8952   return statement;
8953 }
8954
8955 /* Parse a for-init-statement or the declarator of a range-based-for.
8956    Returns true if a range-based-for declaration is seen.
8957
8958    for-init-statement:
8959      expression-statement
8960      simple-declaration  */
8961
8962 static bool
8963 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8964 {
8965   /* If the next token is a `;', then we have an empty
8966      expression-statement.  Grammatically, this is also a
8967      simple-declaration, but an invalid one, because it does not
8968      declare anything.  Therefore, if we did not handle this case
8969      specially, we would issue an error message about an invalid
8970      declaration.  */
8971   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8972     {
8973       bool is_range_for = false;
8974       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8975
8976       parser->colon_corrects_to_scope_p = false;
8977
8978       /* We're going to speculatively look for a declaration, falling back
8979          to an expression, if necessary.  */
8980       cp_parser_parse_tentatively (parser);
8981       /* Parse the declaration.  */
8982       cp_parser_simple_declaration (parser,
8983                                     /*function_definition_allowed_p=*/false,
8984                                     decl);
8985       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8986       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8987         {
8988           /* It is a range-for, consume the ':' */
8989           cp_lexer_consume_token (parser->lexer);
8990           is_range_for = true;
8991           if (cxx_dialect < cxx0x)
8992             {
8993               error_at (cp_lexer_peek_token (parser->lexer)->location,
8994                         "range-based %<for%> loops are not allowed "
8995                         "in C++98 mode");
8996               *decl = error_mark_node;
8997             }
8998         }
8999       else
9000           /* The ';' is not consumed yet because we told
9001              cp_parser_simple_declaration not to.  */
9002           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9003
9004       if (cp_parser_parse_definitely (parser))
9005         return is_range_for;
9006       /* If the tentative parse failed, then we shall need to look for an
9007          expression-statement.  */
9008     }
9009   /* If we are here, it is an expression-statement.  */
9010   cp_parser_expression_statement (parser, NULL_TREE);
9011   return false;
9012 }
9013
9014 /* Parse a jump-statement.
9015
9016    jump-statement:
9017      break ;
9018      continue ;
9019      return expression [opt] ;
9020      return braced-init-list ;
9021      goto identifier ;
9022
9023    GNU extension:
9024
9025    jump-statement:
9026      goto * expression ;
9027
9028    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9029
9030 static tree
9031 cp_parser_jump_statement (cp_parser* parser)
9032 {
9033   tree statement = error_mark_node;
9034   cp_token *token;
9035   enum rid keyword;
9036   unsigned char in_statement;
9037
9038   /* Peek at the next token.  */
9039   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9040   if (!token)
9041     return error_mark_node;
9042
9043   /* See what kind of keyword it is.  */
9044   keyword = token->keyword;
9045   switch (keyword)
9046     {
9047     case RID_BREAK:
9048       in_statement = parser->in_statement & ~IN_IF_STMT;      
9049       switch (in_statement)
9050         {
9051         case 0:
9052           error_at (token->location, "break statement not within loop or switch");
9053           break;
9054         default:
9055           gcc_assert ((in_statement & IN_SWITCH_STMT)
9056                       || in_statement == IN_ITERATION_STMT);
9057           statement = finish_break_stmt ();
9058           break;
9059         case IN_OMP_BLOCK:
9060           error_at (token->location, "invalid exit from OpenMP structured block");
9061           break;
9062         case IN_OMP_FOR:
9063           error_at (token->location, "break statement used with OpenMP for loop");
9064           break;
9065         }
9066       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9067       break;
9068
9069     case RID_CONTINUE:
9070       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9071         {
9072         case 0:
9073           error_at (token->location, "continue statement not within a loop");
9074           break;
9075         case IN_ITERATION_STMT:
9076         case IN_OMP_FOR:
9077           statement = finish_continue_stmt ();
9078           break;
9079         case IN_OMP_BLOCK:
9080           error_at (token->location, "invalid exit from OpenMP structured block");
9081           break;
9082         default:
9083           gcc_unreachable ();
9084         }
9085       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9086       break;
9087
9088     case RID_RETURN:
9089       {
9090         tree expr;
9091         bool expr_non_constant_p;
9092
9093         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9094           {
9095             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9096             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9097           }
9098         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9099           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9100         else
9101           /* If the next token is a `;', then there is no
9102              expression.  */
9103           expr = NULL_TREE;
9104         /* Build the return-statement.  */
9105         statement = finish_return_stmt (expr);
9106         /* Look for the final `;'.  */
9107         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9108       }
9109       break;
9110
9111     case RID_GOTO:
9112       /* Create the goto-statement.  */
9113       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9114         {
9115           /* Issue a warning about this use of a GNU extension.  */
9116           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9117           /* Consume the '*' token.  */
9118           cp_lexer_consume_token (parser->lexer);
9119           /* Parse the dependent expression.  */
9120           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9121         }
9122       else
9123         finish_goto_stmt (cp_parser_identifier (parser));
9124       /* Look for the final `;'.  */
9125       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9126       break;
9127
9128     default:
9129       cp_parser_error (parser, "expected jump-statement");
9130       break;
9131     }
9132
9133   return statement;
9134 }
9135
9136 /* Parse a declaration-statement.
9137
9138    declaration-statement:
9139      block-declaration  */
9140
9141 static void
9142 cp_parser_declaration_statement (cp_parser* parser)
9143 {
9144   void *p;
9145
9146   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9147   p = obstack_alloc (&declarator_obstack, 0);
9148
9149  /* Parse the block-declaration.  */
9150   cp_parser_block_declaration (parser, /*statement_p=*/true);
9151
9152   /* Free any declarators allocated.  */
9153   obstack_free (&declarator_obstack, p);
9154
9155   /* Finish off the statement.  */
9156   finish_stmt ();
9157 }
9158
9159 /* Some dependent statements (like `if (cond) statement'), are
9160    implicitly in their own scope.  In other words, if the statement is
9161    a single statement (as opposed to a compound-statement), it is
9162    none-the-less treated as if it were enclosed in braces.  Any
9163    declarations appearing in the dependent statement are out of scope
9164    after control passes that point.  This function parses a statement,
9165    but ensures that is in its own scope, even if it is not a
9166    compound-statement.
9167
9168    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9169    is a (possibly labeled) if statement which is not enclosed in
9170    braces and has an else clause.  This is used to implement
9171    -Wparentheses.
9172
9173    Returns the new statement.  */
9174
9175 static tree
9176 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9177 {
9178   tree statement;
9179
9180   if (if_p != NULL)
9181     *if_p = false;
9182
9183   /* Mark if () ; with a special NOP_EXPR.  */
9184   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9185     {
9186       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9187       cp_lexer_consume_token (parser->lexer);
9188       statement = add_stmt (build_empty_stmt (loc));
9189     }
9190   /* if a compound is opened, we simply parse the statement directly.  */
9191   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9192     statement = cp_parser_compound_statement (parser, NULL, false, false);
9193   /* If the token is not a `{', then we must take special action.  */
9194   else
9195     {
9196       /* Create a compound-statement.  */
9197       statement = begin_compound_stmt (0);
9198       /* Parse the dependent-statement.  */
9199       cp_parser_statement (parser, NULL_TREE, false, if_p);
9200       /* Finish the dummy compound-statement.  */
9201       finish_compound_stmt (statement);
9202     }
9203
9204   /* Return the statement.  */
9205   return statement;
9206 }
9207
9208 /* For some dependent statements (like `while (cond) statement'), we
9209    have already created a scope.  Therefore, even if the dependent
9210    statement is a compound-statement, we do not want to create another
9211    scope.  */
9212
9213 static void
9214 cp_parser_already_scoped_statement (cp_parser* parser)
9215 {
9216   /* If the token is a `{', then we must take special action.  */
9217   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9218     cp_parser_statement (parser, NULL_TREE, false, NULL);
9219   else
9220     {
9221       /* Avoid calling cp_parser_compound_statement, so that we
9222          don't create a new scope.  Do everything else by hand.  */
9223       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9224       /* If the next keyword is `__label__' we have a label declaration.  */
9225       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9226         cp_parser_label_declaration (parser);
9227       /* Parse an (optional) statement-seq.  */
9228       cp_parser_statement_seq_opt (parser, NULL_TREE);
9229       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9230     }
9231 }
9232
9233 /* Declarations [gram.dcl.dcl] */
9234
9235 /* Parse an optional declaration-sequence.
9236
9237    declaration-seq:
9238      declaration
9239      declaration-seq declaration  */
9240
9241 static void
9242 cp_parser_declaration_seq_opt (cp_parser* parser)
9243 {
9244   while (true)
9245     {
9246       cp_token *token;
9247
9248       token = cp_lexer_peek_token (parser->lexer);
9249
9250       if (token->type == CPP_CLOSE_BRACE
9251           || token->type == CPP_EOF
9252           || token->type == CPP_PRAGMA_EOL)
9253         break;
9254
9255       if (token->type == CPP_SEMICOLON)
9256         {
9257           /* A declaration consisting of a single semicolon is
9258              invalid.  Allow it unless we're being pedantic.  */
9259           cp_lexer_consume_token (parser->lexer);
9260           if (!in_system_header)
9261             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9262           continue;
9263         }
9264
9265       /* If we're entering or exiting a region that's implicitly
9266          extern "C", modify the lang context appropriately.  */
9267       if (!parser->implicit_extern_c && token->implicit_extern_c)
9268         {
9269           push_lang_context (lang_name_c);
9270           parser->implicit_extern_c = true;
9271         }
9272       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9273         {
9274           pop_lang_context ();
9275           parser->implicit_extern_c = false;
9276         }
9277
9278       if (token->type == CPP_PRAGMA)
9279         {
9280           /* A top-level declaration can consist solely of a #pragma.
9281              A nested declaration cannot, so this is done here and not
9282              in cp_parser_declaration.  (A #pragma at block scope is
9283              handled in cp_parser_statement.)  */
9284           cp_parser_pragma (parser, pragma_external);
9285           continue;
9286         }
9287
9288       /* Parse the declaration itself.  */
9289       cp_parser_declaration (parser);
9290     }
9291 }
9292
9293 /* Parse a declaration.
9294
9295    declaration:
9296      block-declaration
9297      function-definition
9298      template-declaration
9299      explicit-instantiation
9300      explicit-specialization
9301      linkage-specification
9302      namespace-definition
9303
9304    GNU extension:
9305
9306    declaration:
9307       __extension__ declaration */
9308
9309 static void
9310 cp_parser_declaration (cp_parser* parser)
9311 {
9312   cp_token token1;
9313   cp_token token2;
9314   int saved_pedantic;
9315   void *p;
9316   tree attributes = NULL_TREE;
9317
9318   /* Check for the `__extension__' keyword.  */
9319   if (cp_parser_extension_opt (parser, &saved_pedantic))
9320     {
9321       /* Parse the qualified declaration.  */
9322       cp_parser_declaration (parser);
9323       /* Restore the PEDANTIC flag.  */
9324       pedantic = saved_pedantic;
9325
9326       return;
9327     }
9328
9329   /* Try to figure out what kind of declaration is present.  */
9330   token1 = *cp_lexer_peek_token (parser->lexer);
9331
9332   if (token1.type != CPP_EOF)
9333     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9334   else
9335     {
9336       token2.type = CPP_EOF;
9337       token2.keyword = RID_MAX;
9338     }
9339
9340   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9341   p = obstack_alloc (&declarator_obstack, 0);
9342
9343   /* If the next token is `extern' and the following token is a string
9344      literal, then we have a linkage specification.  */
9345   if (token1.keyword == RID_EXTERN
9346       && cp_parser_is_string_literal (&token2))
9347     cp_parser_linkage_specification (parser);
9348   /* If the next token is `template', then we have either a template
9349      declaration, an explicit instantiation, or an explicit
9350      specialization.  */
9351   else if (token1.keyword == RID_TEMPLATE)
9352     {
9353       /* `template <>' indicates a template specialization.  */
9354       if (token2.type == CPP_LESS
9355           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9356         cp_parser_explicit_specialization (parser);
9357       /* `template <' indicates a template declaration.  */
9358       else if (token2.type == CPP_LESS)
9359         cp_parser_template_declaration (parser, /*member_p=*/false);
9360       /* Anything else must be an explicit instantiation.  */
9361       else
9362         cp_parser_explicit_instantiation (parser);
9363     }
9364   /* If the next token is `export', then we have a template
9365      declaration.  */
9366   else if (token1.keyword == RID_EXPORT)
9367     cp_parser_template_declaration (parser, /*member_p=*/false);
9368   /* If the next token is `extern', 'static' or 'inline' and the one
9369      after that is `template', we have a GNU extended explicit
9370      instantiation directive.  */
9371   else if (cp_parser_allow_gnu_extensions_p (parser)
9372            && (token1.keyword == RID_EXTERN
9373                || token1.keyword == RID_STATIC
9374                || token1.keyword == RID_INLINE)
9375            && token2.keyword == RID_TEMPLATE)
9376     cp_parser_explicit_instantiation (parser);
9377   /* If the next token is `namespace', check for a named or unnamed
9378      namespace definition.  */
9379   else if (token1.keyword == RID_NAMESPACE
9380            && (/* A named namespace definition.  */
9381                (token2.type == CPP_NAME
9382                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9383                     != CPP_EQ))
9384                /* An unnamed namespace definition.  */
9385                || token2.type == CPP_OPEN_BRACE
9386                || token2.keyword == RID_ATTRIBUTE))
9387     cp_parser_namespace_definition (parser);
9388   /* An inline (associated) namespace definition.  */
9389   else if (token1.keyword == RID_INLINE
9390            && token2.keyword == RID_NAMESPACE)
9391     cp_parser_namespace_definition (parser);
9392   /* Objective-C++ declaration/definition.  */
9393   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9394     cp_parser_objc_declaration (parser, NULL_TREE);
9395   else if (c_dialect_objc ()
9396            && token1.keyword == RID_ATTRIBUTE
9397            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9398     cp_parser_objc_declaration (parser, attributes);
9399   /* We must have either a block declaration or a function
9400      definition.  */
9401   else
9402     /* Try to parse a block-declaration, or a function-definition.  */
9403     cp_parser_block_declaration (parser, /*statement_p=*/false);
9404
9405   /* Free any declarators allocated.  */
9406   obstack_free (&declarator_obstack, p);
9407 }
9408
9409 /* Parse a block-declaration.
9410
9411    block-declaration:
9412      simple-declaration
9413      asm-definition
9414      namespace-alias-definition
9415      using-declaration
9416      using-directive
9417
9418    GNU Extension:
9419
9420    block-declaration:
9421      __extension__ block-declaration
9422
9423    C++0x Extension:
9424
9425    block-declaration:
9426      static_assert-declaration
9427
9428    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9429    part of a declaration-statement.  */
9430
9431 static void
9432 cp_parser_block_declaration (cp_parser *parser,
9433                              bool      statement_p)
9434 {
9435   cp_token *token1;
9436   int saved_pedantic;
9437
9438   /* Check for the `__extension__' keyword.  */
9439   if (cp_parser_extension_opt (parser, &saved_pedantic))
9440     {
9441       /* Parse the qualified declaration.  */
9442       cp_parser_block_declaration (parser, statement_p);
9443       /* Restore the PEDANTIC flag.  */
9444       pedantic = saved_pedantic;
9445
9446       return;
9447     }
9448
9449   /* Peek at the next token to figure out which kind of declaration is
9450      present.  */
9451   token1 = cp_lexer_peek_token (parser->lexer);
9452
9453   /* If the next keyword is `asm', we have an asm-definition.  */
9454   if (token1->keyword == RID_ASM)
9455     {
9456       if (statement_p)
9457         cp_parser_commit_to_tentative_parse (parser);
9458       cp_parser_asm_definition (parser);
9459     }
9460   /* If the next keyword is `namespace', we have a
9461      namespace-alias-definition.  */
9462   else if (token1->keyword == RID_NAMESPACE)
9463     cp_parser_namespace_alias_definition (parser);
9464   /* If the next keyword is `using', we have either a
9465      using-declaration or a using-directive.  */
9466   else if (token1->keyword == RID_USING)
9467     {
9468       cp_token *token2;
9469
9470       if (statement_p)
9471         cp_parser_commit_to_tentative_parse (parser);
9472       /* If the token after `using' is `namespace', then we have a
9473          using-directive.  */
9474       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9475       if (token2->keyword == RID_NAMESPACE)
9476         cp_parser_using_directive (parser);
9477       /* Otherwise, it's a using-declaration.  */
9478       else
9479         cp_parser_using_declaration (parser,
9480                                      /*access_declaration_p=*/false);
9481     }
9482   /* If the next keyword is `__label__' we have a misplaced label
9483      declaration.  */
9484   else if (token1->keyword == RID_LABEL)
9485     {
9486       cp_lexer_consume_token (parser->lexer);
9487       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9488       cp_parser_skip_to_end_of_statement (parser);
9489       /* If the next token is now a `;', consume it.  */
9490       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9491         cp_lexer_consume_token (parser->lexer);
9492     }
9493   /* If the next token is `static_assert' we have a static assertion.  */
9494   else if (token1->keyword == RID_STATIC_ASSERT)
9495     cp_parser_static_assert (parser, /*member_p=*/false);
9496   /* Anything else must be a simple-declaration.  */
9497   else
9498     cp_parser_simple_declaration (parser, !statement_p,
9499                                   /*maybe_range_for_decl*/NULL);
9500 }
9501
9502 /* Parse a simple-declaration.
9503
9504    simple-declaration:
9505      decl-specifier-seq [opt] init-declarator-list [opt] ;
9506
9507    init-declarator-list:
9508      init-declarator
9509      init-declarator-list , init-declarator
9510
9511    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9512    function-definition as a simple-declaration.
9513
9514    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9515    parsed declaration if it is an uninitialized single declarator not followed
9516    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9517    if present, will not be consumed.  */
9518
9519 static void
9520 cp_parser_simple_declaration (cp_parser* parser,
9521                               bool function_definition_allowed_p,
9522                               tree *maybe_range_for_decl)
9523 {
9524   cp_decl_specifier_seq decl_specifiers;
9525   int declares_class_or_enum;
9526   bool saw_declarator;
9527
9528   if (maybe_range_for_decl)
9529     *maybe_range_for_decl = NULL_TREE;
9530
9531   /* Defer access checks until we know what is being declared; the
9532      checks for names appearing in the decl-specifier-seq should be
9533      done as if we were in the scope of the thing being declared.  */
9534   push_deferring_access_checks (dk_deferred);
9535
9536   /* Parse the decl-specifier-seq.  We have to keep track of whether
9537      or not the decl-specifier-seq declares a named class or
9538      enumeration type, since that is the only case in which the
9539      init-declarator-list is allowed to be empty.
9540
9541      [dcl.dcl]
9542
9543      In a simple-declaration, the optional init-declarator-list can be
9544      omitted only when declaring a class or enumeration, that is when
9545      the decl-specifier-seq contains either a class-specifier, an
9546      elaborated-type-specifier, or an enum-specifier.  */
9547   cp_parser_decl_specifier_seq (parser,
9548                                 CP_PARSER_FLAGS_OPTIONAL,
9549                                 &decl_specifiers,
9550                                 &declares_class_or_enum);
9551   /* We no longer need to defer access checks.  */
9552   stop_deferring_access_checks ();
9553
9554   /* In a block scope, a valid declaration must always have a
9555      decl-specifier-seq.  By not trying to parse declarators, we can
9556      resolve the declaration/expression ambiguity more quickly.  */
9557   if (!function_definition_allowed_p
9558       && !decl_specifiers.any_specifiers_p)
9559     {
9560       cp_parser_error (parser, "expected declaration");
9561       goto done;
9562     }
9563
9564   /* If the next two tokens are both identifiers, the code is
9565      erroneous. The usual cause of this situation is code like:
9566
9567        T t;
9568
9569      where "T" should name a type -- but does not.  */
9570   if (!decl_specifiers.any_type_specifiers_p
9571       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9572     {
9573       /* If parsing tentatively, we should commit; we really are
9574          looking at a declaration.  */
9575       cp_parser_commit_to_tentative_parse (parser);
9576       /* Give up.  */
9577       goto done;
9578     }
9579
9580   /* If we have seen at least one decl-specifier, and the next token
9581      is not a parenthesis, then we must be looking at a declaration.
9582      (After "int (" we might be looking at a functional cast.)  */
9583   if (decl_specifiers.any_specifiers_p
9584       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9585       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9586       && !cp_parser_error_occurred (parser))
9587     cp_parser_commit_to_tentative_parse (parser);
9588
9589   /* Keep going until we hit the `;' at the end of the simple
9590      declaration.  */
9591   saw_declarator = false;
9592   while (cp_lexer_next_token_is_not (parser->lexer,
9593                                      CPP_SEMICOLON))
9594     {
9595       cp_token *token;
9596       bool function_definition_p;
9597       tree decl;
9598
9599       if (saw_declarator)
9600         {
9601           /* If we are processing next declarator, coma is expected */
9602           token = cp_lexer_peek_token (parser->lexer);
9603           gcc_assert (token->type == CPP_COMMA);
9604           cp_lexer_consume_token (parser->lexer);
9605           if (maybe_range_for_decl)
9606             *maybe_range_for_decl = error_mark_node;
9607         }
9608       else
9609         saw_declarator = true;
9610
9611       /* Parse the init-declarator.  */
9612       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9613                                         /*checks=*/NULL,
9614                                         function_definition_allowed_p,
9615                                         /*member_p=*/false,
9616                                         declares_class_or_enum,
9617                                         &function_definition_p,
9618                                         maybe_range_for_decl);
9619       /* If an error occurred while parsing tentatively, exit quickly.
9620          (That usually happens when in the body of a function; each
9621          statement is treated as a declaration-statement until proven
9622          otherwise.)  */
9623       if (cp_parser_error_occurred (parser))
9624         goto done;
9625       /* Handle function definitions specially.  */
9626       if (function_definition_p)
9627         {
9628           /* If the next token is a `,', then we are probably
9629              processing something like:
9630
9631                void f() {}, *p;
9632
9633              which is erroneous.  */
9634           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9635             {
9636               cp_token *token = cp_lexer_peek_token (parser->lexer);
9637               error_at (token->location,
9638                         "mixing"
9639                         " declarations and function-definitions is forbidden");
9640             }
9641           /* Otherwise, we're done with the list of declarators.  */
9642           else
9643             {
9644               pop_deferring_access_checks ();
9645               return;
9646             }
9647         }
9648       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9649         *maybe_range_for_decl = decl;
9650       /* The next token should be either a `,' or a `;'.  */
9651       token = cp_lexer_peek_token (parser->lexer);
9652       /* If it's a `,', there are more declarators to come.  */
9653       if (token->type == CPP_COMMA)
9654         /* will be consumed next time around */;
9655       /* If it's a `;', we are done.  */
9656       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9657         break;
9658       /* Anything else is an error.  */
9659       else
9660         {
9661           /* If we have already issued an error message we don't need
9662              to issue another one.  */
9663           if (decl != error_mark_node
9664               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9665             cp_parser_error (parser, "expected %<,%> or %<;%>");
9666           /* Skip tokens until we reach the end of the statement.  */
9667           cp_parser_skip_to_end_of_statement (parser);
9668           /* If the next token is now a `;', consume it.  */
9669           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9670             cp_lexer_consume_token (parser->lexer);
9671           goto done;
9672         }
9673       /* After the first time around, a function-definition is not
9674          allowed -- even if it was OK at first.  For example:
9675
9676            int i, f() {}
9677
9678          is not valid.  */
9679       function_definition_allowed_p = false;
9680     }
9681
9682   /* Issue an error message if no declarators are present, and the
9683      decl-specifier-seq does not itself declare a class or
9684      enumeration.  */
9685   if (!saw_declarator)
9686     {
9687       if (cp_parser_declares_only_class_p (parser))
9688         shadow_tag (&decl_specifiers);
9689       /* Perform any deferred access checks.  */
9690       perform_deferred_access_checks ();
9691     }
9692
9693   /* Consume the `;'.  */
9694   if (!maybe_range_for_decl)
9695       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9696
9697  done:
9698   pop_deferring_access_checks ();
9699 }
9700
9701 /* Parse a decl-specifier-seq.
9702
9703    decl-specifier-seq:
9704      decl-specifier-seq [opt] decl-specifier
9705
9706    decl-specifier:
9707      storage-class-specifier
9708      type-specifier
9709      function-specifier
9710      friend
9711      typedef
9712
9713    GNU Extension:
9714
9715    decl-specifier:
9716      attributes
9717
9718    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9719
9720    The parser flags FLAGS is used to control type-specifier parsing.
9721
9722    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9723    flags:
9724
9725      1: one of the decl-specifiers is an elaborated-type-specifier
9726         (i.e., a type declaration)
9727      2: one of the decl-specifiers is an enum-specifier or a
9728         class-specifier (i.e., a type definition)
9729
9730    */
9731
9732 static void
9733 cp_parser_decl_specifier_seq (cp_parser* parser,
9734                               cp_parser_flags flags,
9735                               cp_decl_specifier_seq *decl_specs,
9736                               int* declares_class_or_enum)
9737 {
9738   bool constructor_possible_p = !parser->in_declarator_p;
9739   cp_token *start_token = NULL;
9740
9741   /* Clear DECL_SPECS.  */
9742   clear_decl_specs (decl_specs);
9743
9744   /* Assume no class or enumeration type is declared.  */
9745   *declares_class_or_enum = 0;
9746
9747   /* Keep reading specifiers until there are no more to read.  */
9748   while (true)
9749     {
9750       bool constructor_p;
9751       bool found_decl_spec;
9752       cp_token *token;
9753
9754       /* Peek at the next token.  */
9755       token = cp_lexer_peek_token (parser->lexer);
9756
9757       /* Save the first token of the decl spec list for error
9758          reporting.  */
9759       if (!start_token)
9760         start_token = token;
9761       /* Handle attributes.  */
9762       if (token->keyword == RID_ATTRIBUTE)
9763         {
9764           /* Parse the attributes.  */
9765           decl_specs->attributes
9766             = chainon (decl_specs->attributes,
9767                        cp_parser_attributes_opt (parser));
9768           continue;
9769         }
9770       /* Assume we will find a decl-specifier keyword.  */
9771       found_decl_spec = true;
9772       /* If the next token is an appropriate keyword, we can simply
9773          add it to the list.  */
9774       switch (token->keyword)
9775         {
9776           /* decl-specifier:
9777                friend
9778                constexpr */
9779         case RID_FRIEND:
9780           if (!at_class_scope_p ())
9781             {
9782               error_at (token->location, "%<friend%> used outside of class");
9783               cp_lexer_purge_token (parser->lexer);
9784             }
9785           else
9786             {
9787               ++decl_specs->specs[(int) ds_friend];
9788               /* Consume the token.  */
9789               cp_lexer_consume_token (parser->lexer);
9790             }
9791           break;
9792
9793         case RID_CONSTEXPR:
9794           ++decl_specs->specs[(int) ds_constexpr];
9795           cp_lexer_consume_token (parser->lexer);
9796           break;
9797
9798           /* function-specifier:
9799                inline
9800                virtual
9801                explicit  */
9802         case RID_INLINE:
9803         case RID_VIRTUAL:
9804         case RID_EXPLICIT:
9805           cp_parser_function_specifier_opt (parser, decl_specs);
9806           break;
9807
9808           /* decl-specifier:
9809                typedef  */
9810         case RID_TYPEDEF:
9811           ++decl_specs->specs[(int) ds_typedef];
9812           /* Consume the token.  */
9813           cp_lexer_consume_token (parser->lexer);
9814           /* A constructor declarator cannot appear in a typedef.  */
9815           constructor_possible_p = false;
9816           /* The "typedef" keyword can only occur in a declaration; we
9817              may as well commit at this point.  */
9818           cp_parser_commit_to_tentative_parse (parser);
9819
9820           if (decl_specs->storage_class != sc_none)
9821             decl_specs->conflicting_specifiers_p = true;
9822           break;
9823
9824           /* storage-class-specifier:
9825                auto
9826                register
9827                static
9828                extern
9829                mutable
9830
9831              GNU Extension:
9832                thread  */
9833         case RID_AUTO:
9834           if (cxx_dialect == cxx98) 
9835             {
9836               /* Consume the token.  */
9837               cp_lexer_consume_token (parser->lexer);
9838
9839               /* Complain about `auto' as a storage specifier, if
9840                  we're complaining about C++0x compatibility.  */
9841               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9842                           " will change meaning in C++0x; please remove it");
9843
9844               /* Set the storage class anyway.  */
9845               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9846                                            token->location);
9847             }
9848           else
9849             /* C++0x auto type-specifier.  */
9850             found_decl_spec = false;
9851           break;
9852
9853         case RID_REGISTER:
9854         case RID_STATIC:
9855         case RID_EXTERN:
9856         case RID_MUTABLE:
9857           /* Consume the token.  */
9858           cp_lexer_consume_token (parser->lexer);
9859           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9860                                        token->location);
9861           break;
9862         case RID_THREAD:
9863           /* Consume the token.  */
9864           cp_lexer_consume_token (parser->lexer);
9865           ++decl_specs->specs[(int) ds_thread];
9866           break;
9867
9868         default:
9869           /* We did not yet find a decl-specifier yet.  */
9870           found_decl_spec = false;
9871           break;
9872         }
9873
9874       if (found_decl_spec
9875           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9876           && token->keyword != RID_CONSTEXPR)
9877         error ("decl-specifier invalid in condition");
9878
9879       /* Constructors are a special case.  The `S' in `S()' is not a
9880          decl-specifier; it is the beginning of the declarator.  */
9881       constructor_p
9882         = (!found_decl_spec
9883            && constructor_possible_p
9884            && (cp_parser_constructor_declarator_p
9885                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9886
9887       /* If we don't have a DECL_SPEC yet, then we must be looking at
9888          a type-specifier.  */
9889       if (!found_decl_spec && !constructor_p)
9890         {
9891           int decl_spec_declares_class_or_enum;
9892           bool is_cv_qualifier;
9893           tree type_spec;
9894
9895           type_spec
9896             = cp_parser_type_specifier (parser, flags,
9897                                         decl_specs,
9898                                         /*is_declaration=*/true,
9899                                         &decl_spec_declares_class_or_enum,
9900                                         &is_cv_qualifier);
9901           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9902
9903           /* If this type-specifier referenced a user-defined type
9904              (a typedef, class-name, etc.), then we can't allow any
9905              more such type-specifiers henceforth.
9906
9907              [dcl.spec]
9908
9909              The longest sequence of decl-specifiers that could
9910              possibly be a type name is taken as the
9911              decl-specifier-seq of a declaration.  The sequence shall
9912              be self-consistent as described below.
9913
9914              [dcl.type]
9915
9916              As a general rule, at most one type-specifier is allowed
9917              in the complete decl-specifier-seq of a declaration.  The
9918              only exceptions are the following:
9919
9920              -- const or volatile can be combined with any other
9921                 type-specifier.
9922
9923              -- signed or unsigned can be combined with char, long,
9924                 short, or int.
9925
9926              -- ..
9927
9928              Example:
9929
9930                typedef char* Pc;
9931                void g (const int Pc);
9932
9933              Here, Pc is *not* part of the decl-specifier seq; it's
9934              the declarator.  Therefore, once we see a type-specifier
9935              (other than a cv-qualifier), we forbid any additional
9936              user-defined types.  We *do* still allow things like `int
9937              int' to be considered a decl-specifier-seq, and issue the
9938              error message later.  */
9939           if (type_spec && !is_cv_qualifier)
9940             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9941           /* A constructor declarator cannot follow a type-specifier.  */
9942           if (type_spec)
9943             {
9944               constructor_possible_p = false;
9945               found_decl_spec = true;
9946               if (!is_cv_qualifier)
9947                 decl_specs->any_type_specifiers_p = true;
9948             }
9949         }
9950
9951       /* If we still do not have a DECL_SPEC, then there are no more
9952          decl-specifiers.  */
9953       if (!found_decl_spec)
9954         break;
9955
9956       decl_specs->any_specifiers_p = true;
9957       /* After we see one decl-specifier, further decl-specifiers are
9958          always optional.  */
9959       flags |= CP_PARSER_FLAGS_OPTIONAL;
9960     }
9961
9962   cp_parser_check_decl_spec (decl_specs, start_token->location);
9963
9964   /* Don't allow a friend specifier with a class definition.  */
9965   if (decl_specs->specs[(int) ds_friend] != 0
9966       && (*declares_class_or_enum & 2))
9967     error_at (start_token->location,
9968               "class definition may not be declared a friend");
9969 }
9970
9971 /* Parse an (optional) storage-class-specifier.
9972
9973    storage-class-specifier:
9974      auto
9975      register
9976      static
9977      extern
9978      mutable
9979
9980    GNU Extension:
9981
9982    storage-class-specifier:
9983      thread
9984
9985    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9986
9987 static tree
9988 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9989 {
9990   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9991     {
9992     case RID_AUTO:
9993       if (cxx_dialect != cxx98)
9994         return NULL_TREE;
9995       /* Fall through for C++98.  */
9996
9997     case RID_REGISTER:
9998     case RID_STATIC:
9999     case RID_EXTERN:
10000     case RID_MUTABLE:
10001     case RID_THREAD:
10002       /* Consume the token.  */
10003       return cp_lexer_consume_token (parser->lexer)->u.value;
10004
10005     default:
10006       return NULL_TREE;
10007     }
10008 }
10009
10010 /* Parse an (optional) function-specifier.
10011
10012    function-specifier:
10013      inline
10014      virtual
10015      explicit
10016
10017    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10018    Updates DECL_SPECS, if it is non-NULL.  */
10019
10020 static tree
10021 cp_parser_function_specifier_opt (cp_parser* parser,
10022                                   cp_decl_specifier_seq *decl_specs)
10023 {
10024   cp_token *token = cp_lexer_peek_token (parser->lexer);
10025   switch (token->keyword)
10026     {
10027     case RID_INLINE:
10028       if (decl_specs)
10029         ++decl_specs->specs[(int) ds_inline];
10030       break;
10031
10032     case RID_VIRTUAL:
10033       /* 14.5.2.3 [temp.mem]
10034
10035          A member function template shall not be virtual.  */
10036       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10037         error_at (token->location, "templates may not be %<virtual%>");
10038       else if (decl_specs)
10039         ++decl_specs->specs[(int) ds_virtual];
10040       break;
10041
10042     case RID_EXPLICIT:
10043       if (decl_specs)
10044         ++decl_specs->specs[(int) ds_explicit];
10045       break;
10046
10047     default:
10048       return NULL_TREE;
10049     }
10050
10051   /* Consume the token.  */
10052   return cp_lexer_consume_token (parser->lexer)->u.value;
10053 }
10054
10055 /* Parse a linkage-specification.
10056
10057    linkage-specification:
10058      extern string-literal { declaration-seq [opt] }
10059      extern string-literal declaration  */
10060
10061 static void
10062 cp_parser_linkage_specification (cp_parser* parser)
10063 {
10064   tree linkage;
10065
10066   /* Look for the `extern' keyword.  */
10067   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10068
10069   /* Look for the string-literal.  */
10070   linkage = cp_parser_string_literal (parser, false, false);
10071
10072   /* Transform the literal into an identifier.  If the literal is a
10073      wide-character string, or contains embedded NULs, then we can't
10074      handle it as the user wants.  */
10075   if (strlen (TREE_STRING_POINTER (linkage))
10076       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10077     {
10078       cp_parser_error (parser, "invalid linkage-specification");
10079       /* Assume C++ linkage.  */
10080       linkage = lang_name_cplusplus;
10081     }
10082   else
10083     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10084
10085   /* We're now using the new linkage.  */
10086   push_lang_context (linkage);
10087
10088   /* If the next token is a `{', then we're using the first
10089      production.  */
10090   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10091     {
10092       /* Consume the `{' token.  */
10093       cp_lexer_consume_token (parser->lexer);
10094       /* Parse the declarations.  */
10095       cp_parser_declaration_seq_opt (parser);
10096       /* Look for the closing `}'.  */
10097       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10098     }
10099   /* Otherwise, there's just one declaration.  */
10100   else
10101     {
10102       bool saved_in_unbraced_linkage_specification_p;
10103
10104       saved_in_unbraced_linkage_specification_p
10105         = parser->in_unbraced_linkage_specification_p;
10106       parser->in_unbraced_linkage_specification_p = true;
10107       cp_parser_declaration (parser);
10108       parser->in_unbraced_linkage_specification_p
10109         = saved_in_unbraced_linkage_specification_p;
10110     }
10111
10112   /* We're done with the linkage-specification.  */
10113   pop_lang_context ();
10114 }
10115
10116 /* Parse a static_assert-declaration.
10117
10118    static_assert-declaration:
10119      static_assert ( constant-expression , string-literal ) ; 
10120
10121    If MEMBER_P, this static_assert is a class member.  */
10122
10123 static void 
10124 cp_parser_static_assert(cp_parser *parser, bool member_p)
10125 {
10126   tree condition;
10127   tree message;
10128   cp_token *token;
10129   location_t saved_loc;
10130   bool dummy;
10131
10132   /* Peek at the `static_assert' token so we can keep track of exactly
10133      where the static assertion started.  */
10134   token = cp_lexer_peek_token (parser->lexer);
10135   saved_loc = token->location;
10136
10137   /* Look for the `static_assert' keyword.  */
10138   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10139                                   RT_STATIC_ASSERT))
10140     return;
10141
10142   /*  We know we are in a static assertion; commit to any tentative
10143       parse.  */
10144   if (cp_parser_parsing_tentatively (parser))
10145     cp_parser_commit_to_tentative_parse (parser);
10146
10147   /* Parse the `(' starting the static assertion condition.  */
10148   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10149
10150   /* Parse the constant-expression.  Allow a non-constant expression
10151      here in order to give better diagnostics in finish_static_assert.  */
10152   condition = 
10153     cp_parser_constant_expression (parser,
10154                                    /*allow_non_constant_p=*/true,
10155                                    /*non_constant_p=*/&dummy);
10156
10157   /* Parse the separating `,'.  */
10158   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10159
10160   /* Parse the string-literal message.  */
10161   message = cp_parser_string_literal (parser, 
10162                                       /*translate=*/false,
10163                                       /*wide_ok=*/true);
10164
10165   /* A `)' completes the static assertion.  */
10166   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10167     cp_parser_skip_to_closing_parenthesis (parser, 
10168                                            /*recovering=*/true, 
10169                                            /*or_comma=*/false,
10170                                            /*consume_paren=*/true);
10171
10172   /* A semicolon terminates the declaration.  */
10173   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10174
10175   /* Complete the static assertion, which may mean either processing 
10176      the static assert now or saving it for template instantiation.  */
10177   finish_static_assert (condition, message, saved_loc, member_p);
10178 }
10179
10180 /* Parse a `decltype' type. Returns the type. 
10181
10182    simple-type-specifier:
10183      decltype ( expression )  */
10184
10185 static tree
10186 cp_parser_decltype (cp_parser *parser)
10187 {
10188   tree expr;
10189   bool id_expression_or_member_access_p = false;
10190   const char *saved_message;
10191   bool saved_integral_constant_expression_p;
10192   bool saved_non_integral_constant_expression_p;
10193   cp_token *id_expr_start_token;
10194
10195   /* Look for the `decltype' token.  */
10196   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10197     return error_mark_node;
10198
10199   /* Types cannot be defined in a `decltype' expression.  Save away the
10200      old message.  */
10201   saved_message = parser->type_definition_forbidden_message;
10202
10203   /* And create the new one.  */
10204   parser->type_definition_forbidden_message
10205     = G_("types may not be defined in %<decltype%> expressions");
10206
10207   /* The restrictions on constant-expressions do not apply inside
10208      decltype expressions.  */
10209   saved_integral_constant_expression_p
10210     = parser->integral_constant_expression_p;
10211   saved_non_integral_constant_expression_p
10212     = parser->non_integral_constant_expression_p;
10213   parser->integral_constant_expression_p = false;
10214
10215   /* Do not actually evaluate the expression.  */
10216   ++cp_unevaluated_operand;
10217
10218   /* Do not warn about problems with the expression.  */
10219   ++c_inhibit_evaluation_warnings;
10220
10221   /* Parse the opening `('.  */
10222   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10223     return error_mark_node;
10224   
10225   /* First, try parsing an id-expression.  */
10226   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10227   cp_parser_parse_tentatively (parser);
10228   expr = cp_parser_id_expression (parser,
10229                                   /*template_keyword_p=*/false,
10230                                   /*check_dependency_p=*/true,
10231                                   /*template_p=*/NULL,
10232                                   /*declarator_p=*/false,
10233                                   /*optional_p=*/false);
10234
10235   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10236     {
10237       bool non_integral_constant_expression_p = false;
10238       tree id_expression = expr;
10239       cp_id_kind idk;
10240       const char *error_msg;
10241
10242       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10243         /* Lookup the name we got back from the id-expression.  */
10244         expr = cp_parser_lookup_name (parser, expr,
10245                                       none_type,
10246                                       /*is_template=*/false,
10247                                       /*is_namespace=*/false,
10248                                       /*check_dependency=*/true,
10249                                       /*ambiguous_decls=*/NULL,
10250                                       id_expr_start_token->location);
10251
10252       if (expr
10253           && expr != error_mark_node
10254           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10255           && TREE_CODE (expr) != TYPE_DECL
10256           && (TREE_CODE (expr) != BIT_NOT_EXPR
10257               || !TYPE_P (TREE_OPERAND (expr, 0)))
10258           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10259         {
10260           /* Complete lookup of the id-expression.  */
10261           expr = (finish_id_expression
10262                   (id_expression, expr, parser->scope, &idk,
10263                    /*integral_constant_expression_p=*/false,
10264                    /*allow_non_integral_constant_expression_p=*/true,
10265                    &non_integral_constant_expression_p,
10266                    /*template_p=*/false,
10267                    /*done=*/true,
10268                    /*address_p=*/false,
10269                    /*template_arg_p=*/false,
10270                    &error_msg,
10271                    id_expr_start_token->location));
10272
10273           if (expr == error_mark_node)
10274             /* We found an id-expression, but it was something that we
10275                should not have found. This is an error, not something
10276                we can recover from, so note that we found an
10277                id-expression and we'll recover as gracefully as
10278                possible.  */
10279             id_expression_or_member_access_p = true;
10280         }
10281
10282       if (expr 
10283           && expr != error_mark_node
10284           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10285         /* We have an id-expression.  */
10286         id_expression_or_member_access_p = true;
10287     }
10288
10289   if (!id_expression_or_member_access_p)
10290     {
10291       /* Abort the id-expression parse.  */
10292       cp_parser_abort_tentative_parse (parser);
10293
10294       /* Parsing tentatively, again.  */
10295       cp_parser_parse_tentatively (parser);
10296
10297       /* Parse a class member access.  */
10298       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10299                                            /*cast_p=*/false,
10300                                            /*member_access_only_p=*/true, NULL);
10301
10302       if (expr 
10303           && expr != error_mark_node
10304           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10305         /* We have an id-expression.  */
10306         id_expression_or_member_access_p = true;
10307     }
10308
10309   if (id_expression_or_member_access_p)
10310     /* We have parsed the complete id-expression or member access.  */
10311     cp_parser_parse_definitely (parser);
10312   else
10313     {
10314       bool saved_greater_than_is_operator_p;
10315
10316       /* Abort our attempt to parse an id-expression or member access
10317          expression.  */
10318       cp_parser_abort_tentative_parse (parser);
10319
10320       /* Within a parenthesized expression, a `>' token is always
10321          the greater-than operator.  */
10322       saved_greater_than_is_operator_p
10323         = parser->greater_than_is_operator_p;
10324       parser->greater_than_is_operator_p = true;
10325
10326       /* Parse a full expression.  */
10327       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10328
10329       /* The `>' token might be the end of a template-id or
10330          template-parameter-list now.  */
10331       parser->greater_than_is_operator_p
10332         = saved_greater_than_is_operator_p;
10333     }
10334
10335   /* Go back to evaluating expressions.  */
10336   --cp_unevaluated_operand;
10337   --c_inhibit_evaluation_warnings;
10338
10339   /* Restore the old message and the integral constant expression
10340      flags.  */
10341   parser->type_definition_forbidden_message = saved_message;
10342   parser->integral_constant_expression_p
10343     = saved_integral_constant_expression_p;
10344   parser->non_integral_constant_expression_p
10345     = saved_non_integral_constant_expression_p;
10346
10347   if (expr == error_mark_node)
10348     {
10349       /* Skip everything up to the closing `)'.  */
10350       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10351                                              /*consume_paren=*/true);
10352       return error_mark_node;
10353     }
10354   
10355   /* Parse to the closing `)'.  */
10356   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10357     {
10358       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10359                                              /*consume_paren=*/true);
10360       return error_mark_node;
10361     }
10362
10363   return finish_decltype_type (expr, id_expression_or_member_access_p,
10364                                tf_warning_or_error);
10365 }
10366
10367 /* Special member functions [gram.special] */
10368
10369 /* Parse a conversion-function-id.
10370
10371    conversion-function-id:
10372      operator conversion-type-id
10373
10374    Returns an IDENTIFIER_NODE representing the operator.  */
10375
10376 static tree
10377 cp_parser_conversion_function_id (cp_parser* parser)
10378 {
10379   tree type;
10380   tree saved_scope;
10381   tree saved_qualifying_scope;
10382   tree saved_object_scope;
10383   tree pushed_scope = NULL_TREE;
10384
10385   /* Look for the `operator' token.  */
10386   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10387     return error_mark_node;
10388   /* When we parse the conversion-type-id, the current scope will be
10389      reset.  However, we need that information in able to look up the
10390      conversion function later, so we save it here.  */
10391   saved_scope = parser->scope;
10392   saved_qualifying_scope = parser->qualifying_scope;
10393   saved_object_scope = parser->object_scope;
10394   /* We must enter the scope of the class so that the names of
10395      entities declared within the class are available in the
10396      conversion-type-id.  For example, consider:
10397
10398        struct S {
10399          typedef int I;
10400          operator I();
10401        };
10402
10403        S::operator I() { ... }
10404
10405      In order to see that `I' is a type-name in the definition, we
10406      must be in the scope of `S'.  */
10407   if (saved_scope)
10408     pushed_scope = push_scope (saved_scope);
10409   /* Parse the conversion-type-id.  */
10410   type = cp_parser_conversion_type_id (parser);
10411   /* Leave the scope of the class, if any.  */
10412   if (pushed_scope)
10413     pop_scope (pushed_scope);
10414   /* Restore the saved scope.  */
10415   parser->scope = saved_scope;
10416   parser->qualifying_scope = saved_qualifying_scope;
10417   parser->object_scope = saved_object_scope;
10418   /* If the TYPE is invalid, indicate failure.  */
10419   if (type == error_mark_node)
10420     return error_mark_node;
10421   return mangle_conv_op_name_for_type (type);
10422 }
10423
10424 /* Parse a conversion-type-id:
10425
10426    conversion-type-id:
10427      type-specifier-seq conversion-declarator [opt]
10428
10429    Returns the TYPE specified.  */
10430
10431 static tree
10432 cp_parser_conversion_type_id (cp_parser* parser)
10433 {
10434   tree attributes;
10435   cp_decl_specifier_seq type_specifiers;
10436   cp_declarator *declarator;
10437   tree type_specified;
10438
10439   /* Parse the attributes.  */
10440   attributes = cp_parser_attributes_opt (parser);
10441   /* Parse the type-specifiers.  */
10442   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10443                                 /*is_trailing_return=*/false,
10444                                 &type_specifiers);
10445   /* If that didn't work, stop.  */
10446   if (type_specifiers.type == error_mark_node)
10447     return error_mark_node;
10448   /* Parse the conversion-declarator.  */
10449   declarator = cp_parser_conversion_declarator_opt (parser);
10450
10451   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10452                                     /*initialized=*/0, &attributes);
10453   if (attributes)
10454     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10455
10456   /* Don't give this error when parsing tentatively.  This happens to
10457      work because we always parse this definitively once.  */
10458   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10459       && type_uses_auto (type_specified))
10460     {
10461       error ("invalid use of %<auto%> in conversion operator");
10462       return error_mark_node;
10463     }
10464
10465   return type_specified;
10466 }
10467
10468 /* Parse an (optional) conversion-declarator.
10469
10470    conversion-declarator:
10471      ptr-operator conversion-declarator [opt]
10472
10473    */
10474
10475 static cp_declarator *
10476 cp_parser_conversion_declarator_opt (cp_parser* parser)
10477 {
10478   enum tree_code code;
10479   tree class_type;
10480   cp_cv_quals cv_quals;
10481
10482   /* We don't know if there's a ptr-operator next, or not.  */
10483   cp_parser_parse_tentatively (parser);
10484   /* Try the ptr-operator.  */
10485   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10486   /* If it worked, look for more conversion-declarators.  */
10487   if (cp_parser_parse_definitely (parser))
10488     {
10489       cp_declarator *declarator;
10490
10491       /* Parse another optional declarator.  */
10492       declarator = cp_parser_conversion_declarator_opt (parser);
10493
10494       return cp_parser_make_indirect_declarator
10495         (code, class_type, cv_quals, declarator);
10496    }
10497
10498   return NULL;
10499 }
10500
10501 /* Parse an (optional) ctor-initializer.
10502
10503    ctor-initializer:
10504      : mem-initializer-list
10505
10506    Returns TRUE iff the ctor-initializer was actually present.  */
10507
10508 static bool
10509 cp_parser_ctor_initializer_opt (cp_parser* parser)
10510 {
10511   /* If the next token is not a `:', then there is no
10512      ctor-initializer.  */
10513   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10514     {
10515       /* Do default initialization of any bases and members.  */
10516       if (DECL_CONSTRUCTOR_P (current_function_decl))
10517         finish_mem_initializers (NULL_TREE);
10518
10519       return false;
10520     }
10521
10522   /* Consume the `:' token.  */
10523   cp_lexer_consume_token (parser->lexer);
10524   /* And the mem-initializer-list.  */
10525   cp_parser_mem_initializer_list (parser);
10526
10527   return true;
10528 }
10529
10530 /* Parse a mem-initializer-list.
10531
10532    mem-initializer-list:
10533      mem-initializer ... [opt]
10534      mem-initializer ... [opt] , mem-initializer-list  */
10535
10536 static void
10537 cp_parser_mem_initializer_list (cp_parser* parser)
10538 {
10539   tree mem_initializer_list = NULL_TREE;
10540   cp_token *token = cp_lexer_peek_token (parser->lexer);
10541
10542   /* Let the semantic analysis code know that we are starting the
10543      mem-initializer-list.  */
10544   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10545     error_at (token->location,
10546               "only constructors take member initializers");
10547
10548   /* Loop through the list.  */
10549   while (true)
10550     {
10551       tree mem_initializer;
10552
10553       token = cp_lexer_peek_token (parser->lexer);
10554       /* Parse the mem-initializer.  */
10555       mem_initializer = cp_parser_mem_initializer (parser);
10556       /* If the next token is a `...', we're expanding member initializers. */
10557       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10558         {
10559           /* Consume the `...'. */
10560           cp_lexer_consume_token (parser->lexer);
10561
10562           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10563              can be expanded but members cannot. */
10564           if (mem_initializer != error_mark_node
10565               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10566             {
10567               error_at (token->location,
10568                         "cannot expand initializer for member %<%D%>",
10569                         TREE_PURPOSE (mem_initializer));
10570               mem_initializer = error_mark_node;
10571             }
10572
10573           /* Construct the pack expansion type. */
10574           if (mem_initializer != error_mark_node)
10575             mem_initializer = make_pack_expansion (mem_initializer);
10576         }
10577       /* Add it to the list, unless it was erroneous.  */
10578       if (mem_initializer != error_mark_node)
10579         {
10580           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10581           mem_initializer_list = mem_initializer;
10582         }
10583       /* If the next token is not a `,', we're done.  */
10584       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10585         break;
10586       /* Consume the `,' token.  */
10587       cp_lexer_consume_token (parser->lexer);
10588     }
10589
10590   /* Perform semantic analysis.  */
10591   if (DECL_CONSTRUCTOR_P (current_function_decl))
10592     finish_mem_initializers (mem_initializer_list);
10593 }
10594
10595 /* Parse a mem-initializer.
10596
10597    mem-initializer:
10598      mem-initializer-id ( expression-list [opt] )
10599      mem-initializer-id braced-init-list
10600
10601    GNU extension:
10602
10603    mem-initializer:
10604      ( expression-list [opt] )
10605
10606    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10607    class) or FIELD_DECL (for a non-static data member) to initialize;
10608    the TREE_VALUE is the expression-list.  An empty initialization
10609    list is represented by void_list_node.  */
10610
10611 static tree
10612 cp_parser_mem_initializer (cp_parser* parser)
10613 {
10614   tree mem_initializer_id;
10615   tree expression_list;
10616   tree member;
10617   cp_token *token = cp_lexer_peek_token (parser->lexer);
10618
10619   /* Find out what is being initialized.  */
10620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10621     {
10622       permerror (token->location,
10623                  "anachronistic old-style base class initializer");
10624       mem_initializer_id = NULL_TREE;
10625     }
10626   else
10627     {
10628       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10629       if (mem_initializer_id == error_mark_node)
10630         return mem_initializer_id;
10631     }
10632   member = expand_member_init (mem_initializer_id);
10633   if (member && !DECL_P (member))
10634     in_base_initializer = 1;
10635
10636   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10637     {
10638       bool expr_non_constant_p;
10639       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10640       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10641       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10642       expression_list = build_tree_list (NULL_TREE, expression_list);
10643     }
10644   else
10645     {
10646       VEC(tree,gc)* vec;
10647       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10648                                                      /*cast_p=*/false,
10649                                                      /*allow_expansion_p=*/true,
10650                                                      /*non_constant_p=*/NULL);
10651       if (vec == NULL)
10652         return error_mark_node;
10653       expression_list = build_tree_list_vec (vec);
10654       release_tree_vector (vec);
10655     }
10656
10657   if (expression_list == error_mark_node)
10658     return error_mark_node;
10659   if (!expression_list)
10660     expression_list = void_type_node;
10661
10662   in_base_initializer = 0;
10663
10664   return member ? build_tree_list (member, expression_list) : error_mark_node;
10665 }
10666
10667 /* Parse a mem-initializer-id.
10668
10669    mem-initializer-id:
10670      :: [opt] nested-name-specifier [opt] class-name
10671      identifier
10672
10673    Returns a TYPE indicating the class to be initializer for the first
10674    production.  Returns an IDENTIFIER_NODE indicating the data member
10675    to be initialized for the second production.  */
10676
10677 static tree
10678 cp_parser_mem_initializer_id (cp_parser* parser)
10679 {
10680   bool global_scope_p;
10681   bool nested_name_specifier_p;
10682   bool template_p = false;
10683   tree id;
10684
10685   cp_token *token = cp_lexer_peek_token (parser->lexer);
10686
10687   /* `typename' is not allowed in this context ([temp.res]).  */
10688   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10689     {
10690       error_at (token->location, 
10691                 "keyword %<typename%> not allowed in this context (a qualified "
10692                 "member initializer is implicitly a type)");
10693       cp_lexer_consume_token (parser->lexer);
10694     }
10695   /* Look for the optional `::' operator.  */
10696   global_scope_p
10697     = (cp_parser_global_scope_opt (parser,
10698                                    /*current_scope_valid_p=*/false)
10699        != NULL_TREE);
10700   /* Look for the optional nested-name-specifier.  The simplest way to
10701      implement:
10702
10703        [temp.res]
10704
10705        The keyword `typename' is not permitted in a base-specifier or
10706        mem-initializer; in these contexts a qualified name that
10707        depends on a template-parameter is implicitly assumed to be a
10708        type name.
10709
10710      is to assume that we have seen the `typename' keyword at this
10711      point.  */
10712   nested_name_specifier_p
10713     = (cp_parser_nested_name_specifier_opt (parser,
10714                                             /*typename_keyword_p=*/true,
10715                                             /*check_dependency_p=*/true,
10716                                             /*type_p=*/true,
10717                                             /*is_declaration=*/true)
10718        != NULL_TREE);
10719   if (nested_name_specifier_p)
10720     template_p = cp_parser_optional_template_keyword (parser);
10721   /* If there is a `::' operator or a nested-name-specifier, then we
10722      are definitely looking for a class-name.  */
10723   if (global_scope_p || nested_name_specifier_p)
10724     return cp_parser_class_name (parser,
10725                                  /*typename_keyword_p=*/true,
10726                                  /*template_keyword_p=*/template_p,
10727                                  typename_type,
10728                                  /*check_dependency_p=*/true,
10729                                  /*class_head_p=*/false,
10730                                  /*is_declaration=*/true);
10731   /* Otherwise, we could also be looking for an ordinary identifier.  */
10732   cp_parser_parse_tentatively (parser);
10733   /* Try a class-name.  */
10734   id = cp_parser_class_name (parser,
10735                              /*typename_keyword_p=*/true,
10736                              /*template_keyword_p=*/false,
10737                              none_type,
10738                              /*check_dependency_p=*/true,
10739                              /*class_head_p=*/false,
10740                              /*is_declaration=*/true);
10741   /* If we found one, we're done.  */
10742   if (cp_parser_parse_definitely (parser))
10743     return id;
10744   /* Otherwise, look for an ordinary identifier.  */
10745   return cp_parser_identifier (parser);
10746 }
10747
10748 /* Overloading [gram.over] */
10749
10750 /* Parse an operator-function-id.
10751
10752    operator-function-id:
10753      operator operator
10754
10755    Returns an IDENTIFIER_NODE for the operator which is a
10756    human-readable spelling of the identifier, e.g., `operator +'.  */
10757
10758 static tree
10759 cp_parser_operator_function_id (cp_parser* parser)
10760 {
10761   /* Look for the `operator' keyword.  */
10762   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10763     return error_mark_node;
10764   /* And then the name of the operator itself.  */
10765   return cp_parser_operator (parser);
10766 }
10767
10768 /* Parse an operator.
10769
10770    operator:
10771      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10772      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10773      || ++ -- , ->* -> () []
10774
10775    GNU Extensions:
10776
10777    operator:
10778      <? >? <?= >?=
10779
10780    Returns an IDENTIFIER_NODE for the operator which is a
10781    human-readable spelling of the identifier, e.g., `operator +'.  */
10782
10783 static tree
10784 cp_parser_operator (cp_parser* parser)
10785 {
10786   tree id = NULL_TREE;
10787   cp_token *token;
10788
10789   /* Peek at the next token.  */
10790   token = cp_lexer_peek_token (parser->lexer);
10791   /* Figure out which operator we have.  */
10792   switch (token->type)
10793     {
10794     case CPP_KEYWORD:
10795       {
10796         enum tree_code op;
10797
10798         /* The keyword should be either `new' or `delete'.  */
10799         if (token->keyword == RID_NEW)
10800           op = NEW_EXPR;
10801         else if (token->keyword == RID_DELETE)
10802           op = DELETE_EXPR;
10803         else
10804           break;
10805
10806         /* Consume the `new' or `delete' token.  */
10807         cp_lexer_consume_token (parser->lexer);
10808
10809         /* Peek at the next token.  */
10810         token = cp_lexer_peek_token (parser->lexer);
10811         /* If it's a `[' token then this is the array variant of the
10812            operator.  */
10813         if (token->type == CPP_OPEN_SQUARE)
10814           {
10815             /* Consume the `[' token.  */
10816             cp_lexer_consume_token (parser->lexer);
10817             /* Look for the `]' token.  */
10818             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10819             id = ansi_opname (op == NEW_EXPR
10820                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10821           }
10822         /* Otherwise, we have the non-array variant.  */
10823         else
10824           id = ansi_opname (op);
10825
10826         return id;
10827       }
10828
10829     case CPP_PLUS:
10830       id = ansi_opname (PLUS_EXPR);
10831       break;
10832
10833     case CPP_MINUS:
10834       id = ansi_opname (MINUS_EXPR);
10835       break;
10836
10837     case CPP_MULT:
10838       id = ansi_opname (MULT_EXPR);
10839       break;
10840
10841     case CPP_DIV:
10842       id = ansi_opname (TRUNC_DIV_EXPR);
10843       break;
10844
10845     case CPP_MOD:
10846       id = ansi_opname (TRUNC_MOD_EXPR);
10847       break;
10848
10849     case CPP_XOR:
10850       id = ansi_opname (BIT_XOR_EXPR);
10851       break;
10852
10853     case CPP_AND:
10854       id = ansi_opname (BIT_AND_EXPR);
10855       break;
10856
10857     case CPP_OR:
10858       id = ansi_opname (BIT_IOR_EXPR);
10859       break;
10860
10861     case CPP_COMPL:
10862       id = ansi_opname (BIT_NOT_EXPR);
10863       break;
10864
10865     case CPP_NOT:
10866       id = ansi_opname (TRUTH_NOT_EXPR);
10867       break;
10868
10869     case CPP_EQ:
10870       id = ansi_assopname (NOP_EXPR);
10871       break;
10872
10873     case CPP_LESS:
10874       id = ansi_opname (LT_EXPR);
10875       break;
10876
10877     case CPP_GREATER:
10878       id = ansi_opname (GT_EXPR);
10879       break;
10880
10881     case CPP_PLUS_EQ:
10882       id = ansi_assopname (PLUS_EXPR);
10883       break;
10884
10885     case CPP_MINUS_EQ:
10886       id = ansi_assopname (MINUS_EXPR);
10887       break;
10888
10889     case CPP_MULT_EQ:
10890       id = ansi_assopname (MULT_EXPR);
10891       break;
10892
10893     case CPP_DIV_EQ:
10894       id = ansi_assopname (TRUNC_DIV_EXPR);
10895       break;
10896
10897     case CPP_MOD_EQ:
10898       id = ansi_assopname (TRUNC_MOD_EXPR);
10899       break;
10900
10901     case CPP_XOR_EQ:
10902       id = ansi_assopname (BIT_XOR_EXPR);
10903       break;
10904
10905     case CPP_AND_EQ:
10906       id = ansi_assopname (BIT_AND_EXPR);
10907       break;
10908
10909     case CPP_OR_EQ:
10910       id = ansi_assopname (BIT_IOR_EXPR);
10911       break;
10912
10913     case CPP_LSHIFT:
10914       id = ansi_opname (LSHIFT_EXPR);
10915       break;
10916
10917     case CPP_RSHIFT:
10918       id = ansi_opname (RSHIFT_EXPR);
10919       break;
10920
10921     case CPP_LSHIFT_EQ:
10922       id = ansi_assopname (LSHIFT_EXPR);
10923       break;
10924
10925     case CPP_RSHIFT_EQ:
10926       id = ansi_assopname (RSHIFT_EXPR);
10927       break;
10928
10929     case CPP_EQ_EQ:
10930       id = ansi_opname (EQ_EXPR);
10931       break;
10932
10933     case CPP_NOT_EQ:
10934       id = ansi_opname (NE_EXPR);
10935       break;
10936
10937     case CPP_LESS_EQ:
10938       id = ansi_opname (LE_EXPR);
10939       break;
10940
10941     case CPP_GREATER_EQ:
10942       id = ansi_opname (GE_EXPR);
10943       break;
10944
10945     case CPP_AND_AND:
10946       id = ansi_opname (TRUTH_ANDIF_EXPR);
10947       break;
10948
10949     case CPP_OR_OR:
10950       id = ansi_opname (TRUTH_ORIF_EXPR);
10951       break;
10952
10953     case CPP_PLUS_PLUS:
10954       id = ansi_opname (POSTINCREMENT_EXPR);
10955       break;
10956
10957     case CPP_MINUS_MINUS:
10958       id = ansi_opname (PREDECREMENT_EXPR);
10959       break;
10960
10961     case CPP_COMMA:
10962       id = ansi_opname (COMPOUND_EXPR);
10963       break;
10964
10965     case CPP_DEREF_STAR:
10966       id = ansi_opname (MEMBER_REF);
10967       break;
10968
10969     case CPP_DEREF:
10970       id = ansi_opname (COMPONENT_REF);
10971       break;
10972
10973     case CPP_OPEN_PAREN:
10974       /* Consume the `('.  */
10975       cp_lexer_consume_token (parser->lexer);
10976       /* Look for the matching `)'.  */
10977       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10978       return ansi_opname (CALL_EXPR);
10979
10980     case CPP_OPEN_SQUARE:
10981       /* Consume the `['.  */
10982       cp_lexer_consume_token (parser->lexer);
10983       /* Look for the matching `]'.  */
10984       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10985       return ansi_opname (ARRAY_REF);
10986
10987     default:
10988       /* Anything else is an error.  */
10989       break;
10990     }
10991
10992   /* If we have selected an identifier, we need to consume the
10993      operator token.  */
10994   if (id)
10995     cp_lexer_consume_token (parser->lexer);
10996   /* Otherwise, no valid operator name was present.  */
10997   else
10998     {
10999       cp_parser_error (parser, "expected operator");
11000       id = error_mark_node;
11001     }
11002
11003   return id;
11004 }
11005
11006 /* Parse a template-declaration.
11007
11008    template-declaration:
11009      export [opt] template < template-parameter-list > declaration
11010
11011    If MEMBER_P is TRUE, this template-declaration occurs within a
11012    class-specifier.
11013
11014    The grammar rule given by the standard isn't correct.  What
11015    is really meant is:
11016
11017    template-declaration:
11018      export [opt] template-parameter-list-seq
11019        decl-specifier-seq [opt] init-declarator [opt] ;
11020      export [opt] template-parameter-list-seq
11021        function-definition
11022
11023    template-parameter-list-seq:
11024      template-parameter-list-seq [opt]
11025      template < template-parameter-list >  */
11026
11027 static void
11028 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11029 {
11030   /* Check for `export'.  */
11031   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11032     {
11033       /* Consume the `export' token.  */
11034       cp_lexer_consume_token (parser->lexer);
11035       /* Warn that we do not support `export'.  */
11036       warning (0, "keyword %<export%> not implemented, and will be ignored");
11037     }
11038
11039   cp_parser_template_declaration_after_export (parser, member_p);
11040 }
11041
11042 /* Parse a template-parameter-list.
11043
11044    template-parameter-list:
11045      template-parameter
11046      template-parameter-list , template-parameter
11047
11048    Returns a TREE_LIST.  Each node represents a template parameter.
11049    The nodes are connected via their TREE_CHAINs.  */
11050
11051 static tree
11052 cp_parser_template_parameter_list (cp_parser* parser)
11053 {
11054   tree parameter_list = NULL_TREE;
11055
11056   begin_template_parm_list ();
11057
11058   /* The loop below parses the template parms.  We first need to know
11059      the total number of template parms to be able to compute proper
11060      canonical types of each dependent type. So after the loop, when
11061      we know the total number of template parms,
11062      end_template_parm_list computes the proper canonical types and
11063      fixes up the dependent types accordingly.  */
11064   while (true)
11065     {
11066       tree parameter;
11067       bool is_non_type;
11068       bool is_parameter_pack;
11069       location_t parm_loc;
11070
11071       /* Parse the template-parameter.  */
11072       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11073       parameter = cp_parser_template_parameter (parser, 
11074                                                 &is_non_type,
11075                                                 &is_parameter_pack);
11076       /* Add it to the list.  */
11077       if (parameter != error_mark_node)
11078         parameter_list = process_template_parm (parameter_list,
11079                                                 parm_loc,
11080                                                 parameter,
11081                                                 is_non_type,
11082                                                 is_parameter_pack,
11083                                                 0);
11084       else
11085        {
11086          tree err_parm = build_tree_list (parameter, parameter);
11087          parameter_list = chainon (parameter_list, err_parm);
11088        }
11089
11090       /* If the next token is not a `,', we're done.  */
11091       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11092         break;
11093       /* Otherwise, consume the `,' token.  */
11094       cp_lexer_consume_token (parser->lexer);
11095     }
11096
11097   return end_template_parm_list (parameter_list);
11098 }
11099
11100 /* Parse a template-parameter.
11101
11102    template-parameter:
11103      type-parameter
11104      parameter-declaration
11105
11106    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11107    the parameter.  The TREE_PURPOSE is the default value, if any.
11108    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11109    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11110    set to true iff this parameter is a parameter pack. */
11111
11112 static tree
11113 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11114                               bool *is_parameter_pack)
11115 {
11116   cp_token *token;
11117   cp_parameter_declarator *parameter_declarator;
11118   cp_declarator *id_declarator;
11119   tree parm;
11120
11121   /* Assume it is a type parameter or a template parameter.  */
11122   *is_non_type = false;
11123   /* Assume it not a parameter pack. */
11124   *is_parameter_pack = false;
11125   /* Peek at the next token.  */
11126   token = cp_lexer_peek_token (parser->lexer);
11127   /* If it is `class' or `template', we have a type-parameter.  */
11128   if (token->keyword == RID_TEMPLATE)
11129     return cp_parser_type_parameter (parser, is_parameter_pack);
11130   /* If it is `class' or `typename' we do not know yet whether it is a
11131      type parameter or a non-type parameter.  Consider:
11132
11133        template <typename T, typename T::X X> ...
11134
11135      or:
11136
11137        template <class C, class D*> ...
11138
11139      Here, the first parameter is a type parameter, and the second is
11140      a non-type parameter.  We can tell by looking at the token after
11141      the identifier -- if it is a `,', `=', or `>' then we have a type
11142      parameter.  */
11143   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11144     {
11145       /* Peek at the token after `class' or `typename'.  */
11146       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11147       /* If it's an ellipsis, we have a template type parameter
11148          pack. */
11149       if (token->type == CPP_ELLIPSIS)
11150         return cp_parser_type_parameter (parser, is_parameter_pack);
11151       /* If it's an identifier, skip it.  */
11152       if (token->type == CPP_NAME)
11153         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11154       /* Now, see if the token looks like the end of a template
11155          parameter.  */
11156       if (token->type == CPP_COMMA
11157           || token->type == CPP_EQ
11158           || token->type == CPP_GREATER)
11159         return cp_parser_type_parameter (parser, is_parameter_pack);
11160     }
11161
11162   /* Otherwise, it is a non-type parameter.
11163
11164      [temp.param]
11165
11166      When parsing a default template-argument for a non-type
11167      template-parameter, the first non-nested `>' is taken as the end
11168      of the template parameter-list rather than a greater-than
11169      operator.  */
11170   *is_non_type = true;
11171   parameter_declarator
11172      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11173                                         /*parenthesized_p=*/NULL);
11174
11175   /* If the parameter declaration is marked as a parameter pack, set
11176      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11177      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11178      grokdeclarator. */
11179   if (parameter_declarator
11180       && parameter_declarator->declarator
11181       && parameter_declarator->declarator->parameter_pack_p)
11182     {
11183       *is_parameter_pack = true;
11184       parameter_declarator->declarator->parameter_pack_p = false;
11185     }
11186
11187   /* If the next token is an ellipsis, and we don't already have it
11188      marked as a parameter pack, then we have a parameter pack (that
11189      has no declarator).  */
11190   if (!*is_parameter_pack
11191       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11192       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11193     {
11194       /* Consume the `...'.  */
11195       cp_lexer_consume_token (parser->lexer);
11196       maybe_warn_variadic_templates ();
11197       
11198       *is_parameter_pack = true;
11199     }
11200   /* We might end up with a pack expansion as the type of the non-type
11201      template parameter, in which case this is a non-type template
11202      parameter pack.  */
11203   else if (parameter_declarator
11204            && parameter_declarator->decl_specifiers.type
11205            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11206     {
11207       *is_parameter_pack = true;
11208       parameter_declarator->decl_specifiers.type = 
11209         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11210     }
11211
11212   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11213     {
11214       /* Parameter packs cannot have default arguments.  However, a
11215          user may try to do so, so we'll parse them and give an
11216          appropriate diagnostic here.  */
11217
11218       /* Consume the `='.  */
11219       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11220       cp_lexer_consume_token (parser->lexer);
11221       
11222       /* Find the name of the parameter pack.  */     
11223       id_declarator = parameter_declarator->declarator;
11224       while (id_declarator && id_declarator->kind != cdk_id)
11225         id_declarator = id_declarator->declarator;
11226       
11227       if (id_declarator && id_declarator->kind == cdk_id)
11228         error_at (start_token->location,
11229                   "template parameter pack %qD cannot have a default argument",
11230                   id_declarator->u.id.unqualified_name);
11231       else
11232         error_at (start_token->location,
11233                   "template parameter pack cannot have a default argument");
11234       
11235       /* Parse the default argument, but throw away the result.  */
11236       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11237     }
11238
11239   parm = grokdeclarator (parameter_declarator->declarator,
11240                          &parameter_declarator->decl_specifiers,
11241                          TPARM, /*initialized=*/0,
11242                          /*attrlist=*/NULL);
11243   if (parm == error_mark_node)
11244     return error_mark_node;
11245
11246   return build_tree_list (parameter_declarator->default_argument, parm);
11247 }
11248
11249 /* Parse a type-parameter.
11250
11251    type-parameter:
11252      class identifier [opt]
11253      class identifier [opt] = type-id
11254      typename identifier [opt]
11255      typename identifier [opt] = type-id
11256      template < template-parameter-list > class identifier [opt]
11257      template < template-parameter-list > class identifier [opt]
11258        = id-expression
11259
11260    GNU Extension (variadic templates):
11261
11262    type-parameter:
11263      class ... identifier [opt]
11264      typename ... identifier [opt]
11265
11266    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11267    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11268    the declaration of the parameter.
11269
11270    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11271
11272 static tree
11273 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11274 {
11275   cp_token *token;
11276   tree parameter;
11277
11278   /* Look for a keyword to tell us what kind of parameter this is.  */
11279   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11280   if (!token)
11281     return error_mark_node;
11282
11283   switch (token->keyword)
11284     {
11285     case RID_CLASS:
11286     case RID_TYPENAME:
11287       {
11288         tree identifier;
11289         tree default_argument;
11290
11291         /* If the next token is an ellipsis, we have a template
11292            argument pack. */
11293         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11294           {
11295             /* Consume the `...' token. */
11296             cp_lexer_consume_token (parser->lexer);
11297             maybe_warn_variadic_templates ();
11298
11299             *is_parameter_pack = true;
11300           }
11301
11302         /* If the next token is an identifier, then it names the
11303            parameter.  */
11304         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11305           identifier = cp_parser_identifier (parser);
11306         else
11307           identifier = NULL_TREE;
11308
11309         /* Create the parameter.  */
11310         parameter = finish_template_type_parm (class_type_node, identifier);
11311
11312         /* If the next token is an `=', we have a default argument.  */
11313         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11314           {
11315             /* Consume the `=' token.  */
11316             cp_lexer_consume_token (parser->lexer);
11317             /* Parse the default-argument.  */
11318             push_deferring_access_checks (dk_no_deferred);
11319             default_argument = cp_parser_type_id (parser);
11320
11321             /* Template parameter packs cannot have default
11322                arguments. */
11323             if (*is_parameter_pack)
11324               {
11325                 if (identifier)
11326                   error_at (token->location,
11327                             "template parameter pack %qD cannot have a "
11328                             "default argument", identifier);
11329                 else
11330                   error_at (token->location,
11331                             "template parameter packs cannot have "
11332                             "default arguments");
11333                 default_argument = NULL_TREE;
11334               }
11335             pop_deferring_access_checks ();
11336           }
11337         else
11338           default_argument = NULL_TREE;
11339
11340         /* Create the combined representation of the parameter and the
11341            default argument.  */
11342         parameter = build_tree_list (default_argument, parameter);
11343       }
11344       break;
11345
11346     case RID_TEMPLATE:
11347       {
11348         tree identifier;
11349         tree default_argument;
11350
11351         /* Look for the `<'.  */
11352         cp_parser_require (parser, CPP_LESS, RT_LESS);
11353         /* Parse the template-parameter-list.  */
11354         cp_parser_template_parameter_list (parser);
11355         /* Look for the `>'.  */
11356         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11357         /* Look for the `class' keyword.  */
11358         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11359         /* If the next token is an ellipsis, we have a template
11360            argument pack. */
11361         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11362           {
11363             /* Consume the `...' token. */
11364             cp_lexer_consume_token (parser->lexer);
11365             maybe_warn_variadic_templates ();
11366
11367             *is_parameter_pack = true;
11368           }
11369         /* If the next token is an `=', then there is a
11370            default-argument.  If the next token is a `>', we are at
11371            the end of the parameter-list.  If the next token is a `,',
11372            then we are at the end of this parameter.  */
11373         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11374             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11375             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11376           {
11377             identifier = cp_parser_identifier (parser);
11378             /* Treat invalid names as if the parameter were nameless.  */
11379             if (identifier == error_mark_node)
11380               identifier = NULL_TREE;
11381           }
11382         else
11383           identifier = NULL_TREE;
11384
11385         /* Create the template parameter.  */
11386         parameter = finish_template_template_parm (class_type_node,
11387                                                    identifier);
11388
11389         /* If the next token is an `=', then there is a
11390            default-argument.  */
11391         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11392           {
11393             bool is_template;
11394
11395             /* Consume the `='.  */
11396             cp_lexer_consume_token (parser->lexer);
11397             /* Parse the id-expression.  */
11398             push_deferring_access_checks (dk_no_deferred);
11399             /* save token before parsing the id-expression, for error
11400                reporting */
11401             token = cp_lexer_peek_token (parser->lexer);
11402             default_argument
11403               = cp_parser_id_expression (parser,
11404                                          /*template_keyword_p=*/false,
11405                                          /*check_dependency_p=*/true,
11406                                          /*template_p=*/&is_template,
11407                                          /*declarator_p=*/false,
11408                                          /*optional_p=*/false);
11409             if (TREE_CODE (default_argument) == TYPE_DECL)
11410               /* If the id-expression was a template-id that refers to
11411                  a template-class, we already have the declaration here,
11412                  so no further lookup is needed.  */
11413                  ;
11414             else
11415               /* Look up the name.  */
11416               default_argument
11417                 = cp_parser_lookup_name (parser, default_argument,
11418                                          none_type,
11419                                          /*is_template=*/is_template,
11420                                          /*is_namespace=*/false,
11421                                          /*check_dependency=*/true,
11422                                          /*ambiguous_decls=*/NULL,
11423                                          token->location);
11424             /* See if the default argument is valid.  */
11425             default_argument
11426               = check_template_template_default_arg (default_argument);
11427
11428             /* Template parameter packs cannot have default
11429                arguments. */
11430             if (*is_parameter_pack)
11431               {
11432                 if (identifier)
11433                   error_at (token->location,
11434                             "template parameter pack %qD cannot "
11435                             "have a default argument",
11436                             identifier);
11437                 else
11438                   error_at (token->location, "template parameter packs cannot "
11439                             "have default arguments");
11440                 default_argument = NULL_TREE;
11441               }
11442             pop_deferring_access_checks ();
11443           }
11444         else
11445           default_argument = NULL_TREE;
11446
11447         /* Create the combined representation of the parameter and the
11448            default argument.  */
11449         parameter = build_tree_list (default_argument, parameter);
11450       }
11451       break;
11452
11453     default:
11454       gcc_unreachable ();
11455       break;
11456     }
11457
11458   return parameter;
11459 }
11460
11461 /* Parse a template-id.
11462
11463    template-id:
11464      template-name < template-argument-list [opt] >
11465
11466    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11467    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11468    returned.  Otherwise, if the template-name names a function, or set
11469    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11470    names a class, returns a TYPE_DECL for the specialization.
11471
11472    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11473    uninstantiated templates.  */
11474
11475 static tree
11476 cp_parser_template_id (cp_parser *parser,
11477                        bool template_keyword_p,
11478                        bool check_dependency_p,
11479                        bool is_declaration)
11480 {
11481   int i;
11482   tree templ;
11483   tree arguments;
11484   tree template_id;
11485   cp_token_position start_of_id = 0;
11486   deferred_access_check *chk;
11487   VEC (deferred_access_check,gc) *access_check;
11488   cp_token *next_token = NULL, *next_token_2 = NULL;
11489   bool is_identifier;
11490
11491   /* If the next token corresponds to a template-id, there is no need
11492      to reparse it.  */
11493   next_token = cp_lexer_peek_token (parser->lexer);
11494   if (next_token->type == CPP_TEMPLATE_ID)
11495     {
11496       struct tree_check *check_value;
11497
11498       /* Get the stored value.  */
11499       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11500       /* Perform any access checks that were deferred.  */
11501       access_check = check_value->checks;
11502       if (access_check)
11503         {
11504           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11505             perform_or_defer_access_check (chk->binfo,
11506                                            chk->decl,
11507                                            chk->diag_decl);
11508         }
11509       /* Return the stored value.  */
11510       return check_value->value;
11511     }
11512
11513   /* Avoid performing name lookup if there is no possibility of
11514      finding a template-id.  */
11515   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11516       || (next_token->type == CPP_NAME
11517           && !cp_parser_nth_token_starts_template_argument_list_p
11518                (parser, 2)))
11519     {
11520       cp_parser_error (parser, "expected template-id");
11521       return error_mark_node;
11522     }
11523
11524   /* Remember where the template-id starts.  */
11525   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11526     start_of_id = cp_lexer_token_position (parser->lexer, false);
11527
11528   push_deferring_access_checks (dk_deferred);
11529
11530   /* Parse the template-name.  */
11531   is_identifier = false;
11532   templ = cp_parser_template_name (parser, template_keyword_p,
11533                                    check_dependency_p,
11534                                    is_declaration,
11535                                    &is_identifier);
11536   if (templ == error_mark_node || is_identifier)
11537     {
11538       pop_deferring_access_checks ();
11539       return templ;
11540     }
11541
11542   /* If we find the sequence `[:' after a template-name, it's probably
11543      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11544      parse correctly the argument list.  */
11545   next_token = cp_lexer_peek_token (parser->lexer);
11546   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11547   if (next_token->type == CPP_OPEN_SQUARE
11548       && next_token->flags & DIGRAPH
11549       && next_token_2->type == CPP_COLON
11550       && !(next_token_2->flags & PREV_WHITE))
11551     {
11552       cp_parser_parse_tentatively (parser);
11553       /* Change `:' into `::'.  */
11554       next_token_2->type = CPP_SCOPE;
11555       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11556          CPP_LESS.  */
11557       cp_lexer_consume_token (parser->lexer);
11558
11559       /* Parse the arguments.  */
11560       arguments = cp_parser_enclosed_template_argument_list (parser);
11561       if (!cp_parser_parse_definitely (parser))
11562         {
11563           /* If we couldn't parse an argument list, then we revert our changes
11564              and return simply an error. Maybe this is not a template-id
11565              after all.  */
11566           next_token_2->type = CPP_COLON;
11567           cp_parser_error (parser, "expected %<<%>");
11568           pop_deferring_access_checks ();
11569           return error_mark_node;
11570         }
11571       /* Otherwise, emit an error about the invalid digraph, but continue
11572          parsing because we got our argument list.  */
11573       if (permerror (next_token->location,
11574                      "%<<::%> cannot begin a template-argument list"))
11575         {
11576           static bool hint = false;
11577           inform (next_token->location,
11578                   "%<<:%> is an alternate spelling for %<[%>."
11579                   " Insert whitespace between %<<%> and %<::%>");
11580           if (!hint && !flag_permissive)
11581             {
11582               inform (next_token->location, "(if you use %<-fpermissive%>"
11583                       " G++ will accept your code)");
11584               hint = true;
11585             }
11586         }
11587     }
11588   else
11589     {
11590       /* Look for the `<' that starts the template-argument-list.  */
11591       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11592         {
11593           pop_deferring_access_checks ();
11594           return error_mark_node;
11595         }
11596       /* Parse the arguments.  */
11597       arguments = cp_parser_enclosed_template_argument_list (parser);
11598     }
11599
11600   /* Build a representation of the specialization.  */
11601   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11602     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11603   else if (DECL_CLASS_TEMPLATE_P (templ)
11604            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11605     {
11606       bool entering_scope;
11607       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11608          template (rather than some instantiation thereof) only if
11609          is not nested within some other construct.  For example, in
11610          "template <typename T> void f(T) { A<T>::", A<T> is just an
11611          instantiation of A.  */
11612       entering_scope = (template_parm_scope_p ()
11613                         && cp_lexer_next_token_is (parser->lexer,
11614                                                    CPP_SCOPE));
11615       template_id
11616         = finish_template_type (templ, arguments, entering_scope);
11617     }
11618   else
11619     {
11620       /* If it's not a class-template or a template-template, it should be
11621          a function-template.  */
11622       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11623                    || TREE_CODE (templ) == OVERLOAD
11624                    || BASELINK_P (templ)));
11625
11626       template_id = lookup_template_function (templ, arguments);
11627     }
11628
11629   /* If parsing tentatively, replace the sequence of tokens that makes
11630      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11631      should we re-parse the token stream, we will not have to repeat
11632      the effort required to do the parse, nor will we issue duplicate
11633      error messages about problems during instantiation of the
11634      template.  */
11635   if (start_of_id)
11636     {
11637       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11638
11639       /* Reset the contents of the START_OF_ID token.  */
11640       token->type = CPP_TEMPLATE_ID;
11641       /* Retrieve any deferred checks.  Do not pop this access checks yet
11642          so the memory will not be reclaimed during token replacing below.  */
11643       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11644       token->u.tree_check_value->value = template_id;
11645       token->u.tree_check_value->checks = get_deferred_access_checks ();
11646       token->keyword = RID_MAX;
11647
11648       /* Purge all subsequent tokens.  */
11649       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11650
11651       /* ??? Can we actually assume that, if template_id ==
11652          error_mark_node, we will have issued a diagnostic to the
11653          user, as opposed to simply marking the tentative parse as
11654          failed?  */
11655       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11656         error_at (token->location, "parse error in template argument list");
11657     }
11658
11659   pop_deferring_access_checks ();
11660   return template_id;
11661 }
11662
11663 /* Parse a template-name.
11664
11665    template-name:
11666      identifier
11667
11668    The standard should actually say:
11669
11670    template-name:
11671      identifier
11672      operator-function-id
11673
11674    A defect report has been filed about this issue.
11675
11676    A conversion-function-id cannot be a template name because they cannot
11677    be part of a template-id. In fact, looking at this code:
11678
11679    a.operator K<int>()
11680
11681    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11682    It is impossible to call a templated conversion-function-id with an
11683    explicit argument list, since the only allowed template parameter is
11684    the type to which it is converting.
11685
11686    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11687    `template' keyword, in a construction like:
11688
11689      T::template f<3>()
11690
11691    In that case `f' is taken to be a template-name, even though there
11692    is no way of knowing for sure.
11693
11694    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11695    name refers to a set of overloaded functions, at least one of which
11696    is a template, or an IDENTIFIER_NODE with the name of the template,
11697    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11698    names are looked up inside uninstantiated templates.  */
11699
11700 static tree
11701 cp_parser_template_name (cp_parser* parser,
11702                          bool template_keyword_p,
11703                          bool check_dependency_p,
11704                          bool is_declaration,
11705                          bool *is_identifier)
11706 {
11707   tree identifier;
11708   tree decl;
11709   tree fns;
11710   cp_token *token = cp_lexer_peek_token (parser->lexer);
11711
11712   /* If the next token is `operator', then we have either an
11713      operator-function-id or a conversion-function-id.  */
11714   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11715     {
11716       /* We don't know whether we're looking at an
11717          operator-function-id or a conversion-function-id.  */
11718       cp_parser_parse_tentatively (parser);
11719       /* Try an operator-function-id.  */
11720       identifier = cp_parser_operator_function_id (parser);
11721       /* If that didn't work, try a conversion-function-id.  */
11722       if (!cp_parser_parse_definitely (parser))
11723         {
11724           cp_parser_error (parser, "expected template-name");
11725           return error_mark_node;
11726         }
11727     }
11728   /* Look for the identifier.  */
11729   else
11730     identifier = cp_parser_identifier (parser);
11731
11732   /* If we didn't find an identifier, we don't have a template-id.  */
11733   if (identifier == error_mark_node)
11734     return error_mark_node;
11735
11736   /* If the name immediately followed the `template' keyword, then it
11737      is a template-name.  However, if the next token is not `<', then
11738      we do not treat it as a template-name, since it is not being used
11739      as part of a template-id.  This enables us to handle constructs
11740      like:
11741
11742        template <typename T> struct S { S(); };
11743        template <typename T> S<T>::S();
11744
11745      correctly.  We would treat `S' as a template -- if it were `S<T>'
11746      -- but we do not if there is no `<'.  */
11747
11748   if (processing_template_decl
11749       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11750     {
11751       /* In a declaration, in a dependent context, we pretend that the
11752          "template" keyword was present in order to improve error
11753          recovery.  For example, given:
11754
11755            template <typename T> void f(T::X<int>);
11756
11757          we want to treat "X<int>" as a template-id.  */
11758       if (is_declaration
11759           && !template_keyword_p
11760           && parser->scope && TYPE_P (parser->scope)
11761           && check_dependency_p
11762           && dependent_scope_p (parser->scope)
11763           /* Do not do this for dtors (or ctors), since they never
11764              need the template keyword before their name.  */
11765           && !constructor_name_p (identifier, parser->scope))
11766         {
11767           cp_token_position start = 0;
11768
11769           /* Explain what went wrong.  */
11770           error_at (token->location, "non-template %qD used as template",
11771                     identifier);
11772           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11773                   parser->scope, identifier);
11774           /* If parsing tentatively, find the location of the "<" token.  */
11775           if (cp_parser_simulate_error (parser))
11776             start = cp_lexer_token_position (parser->lexer, true);
11777           /* Parse the template arguments so that we can issue error
11778              messages about them.  */
11779           cp_lexer_consume_token (parser->lexer);
11780           cp_parser_enclosed_template_argument_list (parser);
11781           /* Skip tokens until we find a good place from which to
11782              continue parsing.  */
11783           cp_parser_skip_to_closing_parenthesis (parser,
11784                                                  /*recovering=*/true,
11785                                                  /*or_comma=*/true,
11786                                                  /*consume_paren=*/false);
11787           /* If parsing tentatively, permanently remove the
11788              template argument list.  That will prevent duplicate
11789              error messages from being issued about the missing
11790              "template" keyword.  */
11791           if (start)
11792             cp_lexer_purge_tokens_after (parser->lexer, start);
11793           if (is_identifier)
11794             *is_identifier = true;
11795           return identifier;
11796         }
11797
11798       /* If the "template" keyword is present, then there is generally
11799          no point in doing name-lookup, so we just return IDENTIFIER.
11800          But, if the qualifying scope is non-dependent then we can
11801          (and must) do name-lookup normally.  */
11802       if (template_keyword_p
11803           && (!parser->scope
11804               || (TYPE_P (parser->scope)
11805                   && dependent_type_p (parser->scope))))
11806         return identifier;
11807     }
11808
11809   /* Look up the name.  */
11810   decl = cp_parser_lookup_name (parser, identifier,
11811                                 none_type,
11812                                 /*is_template=*/true,
11813                                 /*is_namespace=*/false,
11814                                 check_dependency_p,
11815                                 /*ambiguous_decls=*/NULL,
11816                                 token->location);
11817
11818   /* If DECL is a template, then the name was a template-name.  */
11819   if (TREE_CODE (decl) == TEMPLATE_DECL)
11820     ;
11821   else
11822     {
11823       tree fn = NULL_TREE;
11824
11825       /* The standard does not explicitly indicate whether a name that
11826          names a set of overloaded declarations, some of which are
11827          templates, is a template-name.  However, such a name should
11828          be a template-name; otherwise, there is no way to form a
11829          template-id for the overloaded templates.  */
11830       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11831       if (TREE_CODE (fns) == OVERLOAD)
11832         for (fn = fns; fn; fn = OVL_NEXT (fn))
11833           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11834             break;
11835
11836       if (!fn)
11837         {
11838           /* The name does not name a template.  */
11839           cp_parser_error (parser, "expected template-name");
11840           return error_mark_node;
11841         }
11842     }
11843
11844   /* If DECL is dependent, and refers to a function, then just return
11845      its name; we will look it up again during template instantiation.  */
11846   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11847     {
11848       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11849       if (TYPE_P (scope) && dependent_type_p (scope))
11850         return identifier;
11851     }
11852
11853   return decl;
11854 }
11855
11856 /* Parse a template-argument-list.
11857
11858    template-argument-list:
11859      template-argument ... [opt]
11860      template-argument-list , template-argument ... [opt]
11861
11862    Returns a TREE_VEC containing the arguments.  */
11863
11864 static tree
11865 cp_parser_template_argument_list (cp_parser* parser)
11866 {
11867   tree fixed_args[10];
11868   unsigned n_args = 0;
11869   unsigned alloced = 10;
11870   tree *arg_ary = fixed_args;
11871   tree vec;
11872   bool saved_in_template_argument_list_p;
11873   bool saved_ice_p;
11874   bool saved_non_ice_p;
11875
11876   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11877   parser->in_template_argument_list_p = true;
11878   /* Even if the template-id appears in an integral
11879      constant-expression, the contents of the argument list do
11880      not.  */
11881   saved_ice_p = parser->integral_constant_expression_p;
11882   parser->integral_constant_expression_p = false;
11883   saved_non_ice_p = parser->non_integral_constant_expression_p;
11884   parser->non_integral_constant_expression_p = false;
11885   /* Parse the arguments.  */
11886   do
11887     {
11888       tree argument;
11889
11890       if (n_args)
11891         /* Consume the comma.  */
11892         cp_lexer_consume_token (parser->lexer);
11893
11894       /* Parse the template-argument.  */
11895       argument = cp_parser_template_argument (parser);
11896
11897       /* If the next token is an ellipsis, we're expanding a template
11898          argument pack. */
11899       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11900         {
11901           if (argument == error_mark_node)
11902             {
11903               cp_token *token = cp_lexer_peek_token (parser->lexer);
11904               error_at (token->location,
11905                         "expected parameter pack before %<...%>");
11906             }
11907           /* Consume the `...' token. */
11908           cp_lexer_consume_token (parser->lexer);
11909
11910           /* Make the argument into a TYPE_PACK_EXPANSION or
11911              EXPR_PACK_EXPANSION. */
11912           argument = make_pack_expansion (argument);
11913         }
11914
11915       if (n_args == alloced)
11916         {
11917           alloced *= 2;
11918
11919           if (arg_ary == fixed_args)
11920             {
11921               arg_ary = XNEWVEC (tree, alloced);
11922               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11923             }
11924           else
11925             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11926         }
11927       arg_ary[n_args++] = argument;
11928     }
11929   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11930
11931   vec = make_tree_vec (n_args);
11932
11933   while (n_args--)
11934     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11935
11936   if (arg_ary != fixed_args)
11937     free (arg_ary);
11938   parser->non_integral_constant_expression_p = saved_non_ice_p;
11939   parser->integral_constant_expression_p = saved_ice_p;
11940   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11941 #ifdef ENABLE_CHECKING
11942   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11943 #endif
11944   return vec;
11945 }
11946
11947 /* Parse a template-argument.
11948
11949    template-argument:
11950      assignment-expression
11951      type-id
11952      id-expression
11953
11954    The representation is that of an assignment-expression, type-id, or
11955    id-expression -- except that the qualified id-expression is
11956    evaluated, so that the value returned is either a DECL or an
11957    OVERLOAD.
11958
11959    Although the standard says "assignment-expression", it forbids
11960    throw-expressions or assignments in the template argument.
11961    Therefore, we use "conditional-expression" instead.  */
11962
11963 static tree
11964 cp_parser_template_argument (cp_parser* parser)
11965 {
11966   tree argument;
11967   bool template_p;
11968   bool address_p;
11969   bool maybe_type_id = false;
11970   cp_token *token = NULL, *argument_start_token = NULL;
11971   cp_id_kind idk;
11972
11973   /* There's really no way to know what we're looking at, so we just
11974      try each alternative in order.
11975
11976        [temp.arg]
11977
11978        In a template-argument, an ambiguity between a type-id and an
11979        expression is resolved to a type-id, regardless of the form of
11980        the corresponding template-parameter.
11981
11982      Therefore, we try a type-id first.  */
11983   cp_parser_parse_tentatively (parser);
11984   argument = cp_parser_template_type_arg (parser);
11985   /* If there was no error parsing the type-id but the next token is a
11986      '>>', our behavior depends on which dialect of C++ we're
11987      parsing. In C++98, we probably found a typo for '> >'. But there
11988      are type-id which are also valid expressions. For instance:
11989
11990      struct X { int operator >> (int); };
11991      template <int V> struct Foo {};
11992      Foo<X () >> 5> r;
11993
11994      Here 'X()' is a valid type-id of a function type, but the user just
11995      wanted to write the expression "X() >> 5". Thus, we remember that we
11996      found a valid type-id, but we still try to parse the argument as an
11997      expression to see what happens. 
11998
11999      In C++0x, the '>>' will be considered two separate '>'
12000      tokens.  */
12001   if (!cp_parser_error_occurred (parser)
12002       && cxx_dialect == cxx98
12003       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12004     {
12005       maybe_type_id = true;
12006       cp_parser_abort_tentative_parse (parser);
12007     }
12008   else
12009     {
12010       /* If the next token isn't a `,' or a `>', then this argument wasn't
12011       really finished. This means that the argument is not a valid
12012       type-id.  */
12013       if (!cp_parser_next_token_ends_template_argument_p (parser))
12014         cp_parser_error (parser, "expected template-argument");
12015       /* If that worked, we're done.  */
12016       if (cp_parser_parse_definitely (parser))
12017         return argument;
12018     }
12019   /* We're still not sure what the argument will be.  */
12020   cp_parser_parse_tentatively (parser);
12021   /* Try a template.  */
12022   argument_start_token = cp_lexer_peek_token (parser->lexer);
12023   argument = cp_parser_id_expression (parser,
12024                                       /*template_keyword_p=*/false,
12025                                       /*check_dependency_p=*/true,
12026                                       &template_p,
12027                                       /*declarator_p=*/false,
12028                                       /*optional_p=*/false);
12029   /* If the next token isn't a `,' or a `>', then this argument wasn't
12030      really finished.  */
12031   if (!cp_parser_next_token_ends_template_argument_p (parser))
12032     cp_parser_error (parser, "expected template-argument");
12033   if (!cp_parser_error_occurred (parser))
12034     {
12035       /* Figure out what is being referred to.  If the id-expression
12036          was for a class template specialization, then we will have a
12037          TYPE_DECL at this point.  There is no need to do name lookup
12038          at this point in that case.  */
12039       if (TREE_CODE (argument) != TYPE_DECL)
12040         argument = cp_parser_lookup_name (parser, argument,
12041                                           none_type,
12042                                           /*is_template=*/template_p,
12043                                           /*is_namespace=*/false,
12044                                           /*check_dependency=*/true,
12045                                           /*ambiguous_decls=*/NULL,
12046                                           argument_start_token->location);
12047       if (TREE_CODE (argument) != TEMPLATE_DECL
12048           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12049         cp_parser_error (parser, "expected template-name");
12050     }
12051   if (cp_parser_parse_definitely (parser))
12052     return argument;
12053   /* It must be a non-type argument.  There permitted cases are given
12054      in [temp.arg.nontype]:
12055
12056      -- an integral constant-expression of integral or enumeration
12057         type; or
12058
12059      -- the name of a non-type template-parameter; or
12060
12061      -- the name of an object or function with external linkage...
12062
12063      -- the address of an object or function with external linkage...
12064
12065      -- a pointer to member...  */
12066   /* Look for a non-type template parameter.  */
12067   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12068     {
12069       cp_parser_parse_tentatively (parser);
12070       argument = cp_parser_primary_expression (parser,
12071                                                /*address_p=*/false,
12072                                                /*cast_p=*/false,
12073                                                /*template_arg_p=*/true,
12074                                                &idk);
12075       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12076           || !cp_parser_next_token_ends_template_argument_p (parser))
12077         cp_parser_simulate_error (parser);
12078       if (cp_parser_parse_definitely (parser))
12079         return argument;
12080     }
12081
12082   /* If the next token is "&", the argument must be the address of an
12083      object or function with external linkage.  */
12084   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12085   if (address_p)
12086     cp_lexer_consume_token (parser->lexer);
12087   /* See if we might have an id-expression.  */
12088   token = cp_lexer_peek_token (parser->lexer);
12089   if (token->type == CPP_NAME
12090       || token->keyword == RID_OPERATOR
12091       || token->type == CPP_SCOPE
12092       || token->type == CPP_TEMPLATE_ID
12093       || token->type == CPP_NESTED_NAME_SPECIFIER)
12094     {
12095       cp_parser_parse_tentatively (parser);
12096       argument = cp_parser_primary_expression (parser,
12097                                                address_p,
12098                                                /*cast_p=*/false,
12099                                                /*template_arg_p=*/true,
12100                                                &idk);
12101       if (cp_parser_error_occurred (parser)
12102           || !cp_parser_next_token_ends_template_argument_p (parser))
12103         cp_parser_abort_tentative_parse (parser);
12104       else
12105         {
12106           tree probe;
12107
12108           if (TREE_CODE (argument) == INDIRECT_REF)
12109             {
12110               gcc_assert (REFERENCE_REF_P (argument));
12111               argument = TREE_OPERAND (argument, 0);
12112             }
12113
12114           /* If we're in a template, we represent a qualified-id referring
12115              to a static data member as a SCOPE_REF even if the scope isn't
12116              dependent so that we can check access control later.  */
12117           probe = argument;
12118           if (TREE_CODE (probe) == SCOPE_REF)
12119             probe = TREE_OPERAND (probe, 1);
12120           if (TREE_CODE (probe) == VAR_DECL)
12121             {
12122               /* A variable without external linkage might still be a
12123                  valid constant-expression, so no error is issued here
12124                  if the external-linkage check fails.  */
12125               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12126                 cp_parser_simulate_error (parser);
12127             }
12128           else if (is_overloaded_fn (argument))
12129             /* All overloaded functions are allowed; if the external
12130                linkage test does not pass, an error will be issued
12131                later.  */
12132             ;
12133           else if (address_p
12134                    && (TREE_CODE (argument) == OFFSET_REF
12135                        || TREE_CODE (argument) == SCOPE_REF))
12136             /* A pointer-to-member.  */
12137             ;
12138           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12139             ;
12140           else
12141             cp_parser_simulate_error (parser);
12142
12143           if (cp_parser_parse_definitely (parser))
12144             {
12145               if (address_p)
12146                 argument = build_x_unary_op (ADDR_EXPR, argument,
12147                                              tf_warning_or_error);
12148               return argument;
12149             }
12150         }
12151     }
12152   /* If the argument started with "&", there are no other valid
12153      alternatives at this point.  */
12154   if (address_p)
12155     {
12156       cp_parser_error (parser, "invalid non-type template argument");
12157       return error_mark_node;
12158     }
12159
12160   /* If the argument wasn't successfully parsed as a type-id followed
12161      by '>>', the argument can only be a constant expression now.
12162      Otherwise, we try parsing the constant-expression tentatively,
12163      because the argument could really be a type-id.  */
12164   if (maybe_type_id)
12165     cp_parser_parse_tentatively (parser);
12166   argument = cp_parser_constant_expression (parser,
12167                                             /*allow_non_constant_p=*/false,
12168                                             /*non_constant_p=*/NULL);
12169   argument = fold_non_dependent_expr (argument);
12170   if (!maybe_type_id)
12171     return argument;
12172   if (!cp_parser_next_token_ends_template_argument_p (parser))
12173     cp_parser_error (parser, "expected template-argument");
12174   if (cp_parser_parse_definitely (parser))
12175     return argument;
12176   /* We did our best to parse the argument as a non type-id, but that
12177      was the only alternative that matched (albeit with a '>' after
12178      it). We can assume it's just a typo from the user, and a
12179      diagnostic will then be issued.  */
12180   return cp_parser_template_type_arg (parser);
12181 }
12182
12183 /* Parse an explicit-instantiation.
12184
12185    explicit-instantiation:
12186      template declaration
12187
12188    Although the standard says `declaration', what it really means is:
12189
12190    explicit-instantiation:
12191      template decl-specifier-seq [opt] declarator [opt] ;
12192
12193    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12194    supposed to be allowed.  A defect report has been filed about this
12195    issue.
12196
12197    GNU Extension:
12198
12199    explicit-instantiation:
12200      storage-class-specifier template
12201        decl-specifier-seq [opt] declarator [opt] ;
12202      function-specifier template
12203        decl-specifier-seq [opt] declarator [opt] ;  */
12204
12205 static void
12206 cp_parser_explicit_instantiation (cp_parser* parser)
12207 {
12208   int declares_class_or_enum;
12209   cp_decl_specifier_seq decl_specifiers;
12210   tree extension_specifier = NULL_TREE;
12211
12212   timevar_push (TV_TEMPLATE_INST);
12213
12214   /* Look for an (optional) storage-class-specifier or
12215      function-specifier.  */
12216   if (cp_parser_allow_gnu_extensions_p (parser))
12217     {
12218       extension_specifier
12219         = cp_parser_storage_class_specifier_opt (parser);
12220       if (!extension_specifier)
12221         extension_specifier
12222           = cp_parser_function_specifier_opt (parser,
12223                                               /*decl_specs=*/NULL);
12224     }
12225
12226   /* Look for the `template' keyword.  */
12227   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12228   /* Let the front end know that we are processing an explicit
12229      instantiation.  */
12230   begin_explicit_instantiation ();
12231   /* [temp.explicit] says that we are supposed to ignore access
12232      control while processing explicit instantiation directives.  */
12233   push_deferring_access_checks (dk_no_check);
12234   /* Parse a decl-specifier-seq.  */
12235   cp_parser_decl_specifier_seq (parser,
12236                                 CP_PARSER_FLAGS_OPTIONAL,
12237                                 &decl_specifiers,
12238                                 &declares_class_or_enum);
12239   /* If there was exactly one decl-specifier, and it declared a class,
12240      and there's no declarator, then we have an explicit type
12241      instantiation.  */
12242   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12243     {
12244       tree type;
12245
12246       type = check_tag_decl (&decl_specifiers);
12247       /* Turn access control back on for names used during
12248          template instantiation.  */
12249       pop_deferring_access_checks ();
12250       if (type)
12251         do_type_instantiation (type, extension_specifier,
12252                                /*complain=*/tf_error);
12253     }
12254   else
12255     {
12256       cp_declarator *declarator;
12257       tree decl;
12258
12259       /* Parse the declarator.  */
12260       declarator
12261         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12262                                 /*ctor_dtor_or_conv_p=*/NULL,
12263                                 /*parenthesized_p=*/NULL,
12264                                 /*member_p=*/false);
12265       if (declares_class_or_enum & 2)
12266         cp_parser_check_for_definition_in_return_type (declarator,
12267                                                        decl_specifiers.type,
12268                                                        decl_specifiers.type_location);
12269       if (declarator != cp_error_declarator)
12270         {
12271           if (decl_specifiers.specs[(int)ds_inline])
12272             permerror (input_location, "explicit instantiation shall not use"
12273                        " %<inline%> specifier");
12274           if (decl_specifiers.specs[(int)ds_constexpr])
12275             permerror (input_location, "explicit instantiation shall not use"
12276                        " %<constexpr%> specifier");
12277
12278           decl = grokdeclarator (declarator, &decl_specifiers,
12279                                  NORMAL, 0, &decl_specifiers.attributes);
12280           /* Turn access control back on for names used during
12281              template instantiation.  */
12282           pop_deferring_access_checks ();
12283           /* Do the explicit instantiation.  */
12284           do_decl_instantiation (decl, extension_specifier);
12285         }
12286       else
12287         {
12288           pop_deferring_access_checks ();
12289           /* Skip the body of the explicit instantiation.  */
12290           cp_parser_skip_to_end_of_statement (parser);
12291         }
12292     }
12293   /* We're done with the instantiation.  */
12294   end_explicit_instantiation ();
12295
12296   cp_parser_consume_semicolon_at_end_of_statement (parser);
12297
12298   timevar_pop (TV_TEMPLATE_INST);
12299 }
12300
12301 /* Parse an explicit-specialization.
12302
12303    explicit-specialization:
12304      template < > declaration
12305
12306    Although the standard says `declaration', what it really means is:
12307
12308    explicit-specialization:
12309      template <> decl-specifier [opt] init-declarator [opt] ;
12310      template <> function-definition
12311      template <> explicit-specialization
12312      template <> template-declaration  */
12313
12314 static void
12315 cp_parser_explicit_specialization (cp_parser* parser)
12316 {
12317   bool need_lang_pop;
12318   cp_token *token = cp_lexer_peek_token (parser->lexer);
12319
12320   /* Look for the `template' keyword.  */
12321   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12322   /* Look for the `<'.  */
12323   cp_parser_require (parser, CPP_LESS, RT_LESS);
12324   /* Look for the `>'.  */
12325   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12326   /* We have processed another parameter list.  */
12327   ++parser->num_template_parameter_lists;
12328   /* [temp]
12329
12330      A template ... explicit specialization ... shall not have C
12331      linkage.  */
12332   if (current_lang_name == lang_name_c)
12333     {
12334       error_at (token->location, "template specialization with C linkage");
12335       /* Give it C++ linkage to avoid confusing other parts of the
12336          front end.  */
12337       push_lang_context (lang_name_cplusplus);
12338       need_lang_pop = true;
12339     }
12340   else
12341     need_lang_pop = false;
12342   /* Let the front end know that we are beginning a specialization.  */
12343   if (!begin_specialization ())
12344     {
12345       end_specialization ();
12346       return;
12347     }
12348
12349   /* If the next keyword is `template', we need to figure out whether
12350      or not we're looking a template-declaration.  */
12351   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12352     {
12353       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12354           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12355         cp_parser_template_declaration_after_export (parser,
12356                                                      /*member_p=*/false);
12357       else
12358         cp_parser_explicit_specialization (parser);
12359     }
12360   else
12361     /* Parse the dependent declaration.  */
12362     cp_parser_single_declaration (parser,
12363                                   /*checks=*/NULL,
12364                                   /*member_p=*/false,
12365                                   /*explicit_specialization_p=*/true,
12366                                   /*friend_p=*/NULL);
12367   /* We're done with the specialization.  */
12368   end_specialization ();
12369   /* For the erroneous case of a template with C linkage, we pushed an
12370      implicit C++ linkage scope; exit that scope now.  */
12371   if (need_lang_pop)
12372     pop_lang_context ();
12373   /* We're done with this parameter list.  */
12374   --parser->num_template_parameter_lists;
12375 }
12376
12377 /* Parse a type-specifier.
12378
12379    type-specifier:
12380      simple-type-specifier
12381      class-specifier
12382      enum-specifier
12383      elaborated-type-specifier
12384      cv-qualifier
12385
12386    GNU Extension:
12387
12388    type-specifier:
12389      __complex__
12390
12391    Returns a representation of the type-specifier.  For a
12392    class-specifier, enum-specifier, or elaborated-type-specifier, a
12393    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12394
12395    The parser flags FLAGS is used to control type-specifier parsing.
12396
12397    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12398    in a decl-specifier-seq.
12399
12400    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12401    class-specifier, enum-specifier, or elaborated-type-specifier, then
12402    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12403    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12404    zero.
12405
12406    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12407    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12408    is set to FALSE.  */
12409
12410 static tree
12411 cp_parser_type_specifier (cp_parser* parser,
12412                           cp_parser_flags flags,
12413                           cp_decl_specifier_seq *decl_specs,
12414                           bool is_declaration,
12415                           int* declares_class_or_enum,
12416                           bool* is_cv_qualifier)
12417 {
12418   tree type_spec = NULL_TREE;
12419   cp_token *token;
12420   enum rid keyword;
12421   cp_decl_spec ds = ds_last;
12422
12423   /* Assume this type-specifier does not declare a new type.  */
12424   if (declares_class_or_enum)
12425     *declares_class_or_enum = 0;
12426   /* And that it does not specify a cv-qualifier.  */
12427   if (is_cv_qualifier)
12428     *is_cv_qualifier = false;
12429   /* Peek at the next token.  */
12430   token = cp_lexer_peek_token (parser->lexer);
12431
12432   /* If we're looking at a keyword, we can use that to guide the
12433      production we choose.  */
12434   keyword = token->keyword;
12435   switch (keyword)
12436     {
12437     case RID_ENUM:
12438       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12439         goto elaborated_type_specifier;
12440
12441       /* Look for the enum-specifier.  */
12442       type_spec = cp_parser_enum_specifier (parser);
12443       /* If that worked, we're done.  */
12444       if (type_spec)
12445         {
12446           if (declares_class_or_enum)
12447             *declares_class_or_enum = 2;
12448           if (decl_specs)
12449             cp_parser_set_decl_spec_type (decl_specs,
12450                                           type_spec,
12451                                           token->location,
12452                                           /*user_defined_p=*/true);
12453           return type_spec;
12454         }
12455       else
12456         goto elaborated_type_specifier;
12457
12458       /* Any of these indicate either a class-specifier, or an
12459          elaborated-type-specifier.  */
12460     case RID_CLASS:
12461     case RID_STRUCT:
12462     case RID_UNION:
12463       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12464         goto elaborated_type_specifier;
12465
12466       /* Parse tentatively so that we can back up if we don't find a
12467          class-specifier.  */
12468       cp_parser_parse_tentatively (parser);
12469       /* Look for the class-specifier.  */
12470       type_spec = cp_parser_class_specifier (parser);
12471       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12472       /* If that worked, we're done.  */
12473       if (cp_parser_parse_definitely (parser))
12474         {
12475           if (declares_class_or_enum)
12476             *declares_class_or_enum = 2;
12477           if (decl_specs)
12478             cp_parser_set_decl_spec_type (decl_specs,
12479                                           type_spec,
12480                                           token->location,
12481                                           /*user_defined_p=*/true);
12482           return type_spec;
12483         }
12484
12485       /* Fall through.  */
12486     elaborated_type_specifier:
12487       /* We're declaring (not defining) a class or enum.  */
12488       if (declares_class_or_enum)
12489         *declares_class_or_enum = 1;
12490
12491       /* Fall through.  */
12492     case RID_TYPENAME:
12493       /* Look for an elaborated-type-specifier.  */
12494       type_spec
12495         = (cp_parser_elaborated_type_specifier
12496            (parser,
12497             decl_specs && decl_specs->specs[(int) ds_friend],
12498             is_declaration));
12499       if (decl_specs)
12500         cp_parser_set_decl_spec_type (decl_specs,
12501                                       type_spec,
12502                                       token->location,
12503                                       /*user_defined_p=*/true);
12504       return type_spec;
12505
12506     case RID_CONST:
12507       ds = ds_const;
12508       if (is_cv_qualifier)
12509         *is_cv_qualifier = true;
12510       break;
12511
12512     case RID_VOLATILE:
12513       ds = ds_volatile;
12514       if (is_cv_qualifier)
12515         *is_cv_qualifier = true;
12516       break;
12517
12518     case RID_RESTRICT:
12519       ds = ds_restrict;
12520       if (is_cv_qualifier)
12521         *is_cv_qualifier = true;
12522       break;
12523
12524     case RID_COMPLEX:
12525       /* The `__complex__' keyword is a GNU extension.  */
12526       ds = ds_complex;
12527       break;
12528
12529     default:
12530       break;
12531     }
12532
12533   /* Handle simple keywords.  */
12534   if (ds != ds_last)
12535     {
12536       if (decl_specs)
12537         {
12538           ++decl_specs->specs[(int)ds];
12539           decl_specs->any_specifiers_p = true;
12540         }
12541       return cp_lexer_consume_token (parser->lexer)->u.value;
12542     }
12543
12544   /* If we do not already have a type-specifier, assume we are looking
12545      at a simple-type-specifier.  */
12546   type_spec = cp_parser_simple_type_specifier (parser,
12547                                                decl_specs,
12548                                                flags);
12549
12550   /* If we didn't find a type-specifier, and a type-specifier was not
12551      optional in this context, issue an error message.  */
12552   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12553     {
12554       cp_parser_error (parser, "expected type specifier");
12555       return error_mark_node;
12556     }
12557
12558   return type_spec;
12559 }
12560
12561 /* Parse a simple-type-specifier.
12562
12563    simple-type-specifier:
12564      :: [opt] nested-name-specifier [opt] type-name
12565      :: [opt] nested-name-specifier template template-id
12566      char
12567      wchar_t
12568      bool
12569      short
12570      int
12571      long
12572      signed
12573      unsigned
12574      float
12575      double
12576      void
12577
12578    C++0x Extension:
12579
12580    simple-type-specifier:
12581      auto
12582      decltype ( expression )   
12583      char16_t
12584      char32_t
12585      __underlying_type ( type-id )
12586
12587    GNU Extension:
12588
12589    simple-type-specifier:
12590      __int128
12591      __typeof__ unary-expression
12592      __typeof__ ( type-id )
12593
12594    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12595    appropriately updated.  */
12596
12597 static tree
12598 cp_parser_simple_type_specifier (cp_parser* parser,
12599                                  cp_decl_specifier_seq *decl_specs,
12600                                  cp_parser_flags flags)
12601 {
12602   tree type = NULL_TREE;
12603   cp_token *token;
12604
12605   /* Peek at the next token.  */
12606   token = cp_lexer_peek_token (parser->lexer);
12607
12608   /* If we're looking at a keyword, things are easy.  */
12609   switch (token->keyword)
12610     {
12611     case RID_CHAR:
12612       if (decl_specs)
12613         decl_specs->explicit_char_p = true;
12614       type = char_type_node;
12615       break;
12616     case RID_CHAR16:
12617       type = char16_type_node;
12618       break;
12619     case RID_CHAR32:
12620       type = char32_type_node;
12621       break;
12622     case RID_WCHAR:
12623       type = wchar_type_node;
12624       break;
12625     case RID_BOOL:
12626       type = boolean_type_node;
12627       break;
12628     case RID_SHORT:
12629       if (decl_specs)
12630         ++decl_specs->specs[(int) ds_short];
12631       type = short_integer_type_node;
12632       break;
12633     case RID_INT:
12634       if (decl_specs)
12635         decl_specs->explicit_int_p = true;
12636       type = integer_type_node;
12637       break;
12638     case RID_INT128:
12639       if (!int128_integer_type_node)
12640         break;
12641       if (decl_specs)
12642         decl_specs->explicit_int128_p = true;
12643       type = int128_integer_type_node;
12644       break;
12645     case RID_LONG:
12646       if (decl_specs)
12647         ++decl_specs->specs[(int) ds_long];
12648       type = long_integer_type_node;
12649       break;
12650     case RID_SIGNED:
12651       if (decl_specs)
12652         ++decl_specs->specs[(int) ds_signed];
12653       type = integer_type_node;
12654       break;
12655     case RID_UNSIGNED:
12656       if (decl_specs)
12657         ++decl_specs->specs[(int) ds_unsigned];
12658       type = unsigned_type_node;
12659       break;
12660     case RID_FLOAT:
12661       type = float_type_node;
12662       break;
12663     case RID_DOUBLE:
12664       type = double_type_node;
12665       break;
12666     case RID_VOID:
12667       type = void_type_node;
12668       break;
12669       
12670     case RID_AUTO:
12671       maybe_warn_cpp0x (CPP0X_AUTO);
12672       type = make_auto ();
12673       break;
12674
12675     case RID_DECLTYPE:
12676       /* Parse the `decltype' type.  */
12677       type = cp_parser_decltype (parser);
12678
12679       if (decl_specs)
12680         cp_parser_set_decl_spec_type (decl_specs, type,
12681                                       token->location,
12682                                       /*user_defined_p=*/true);
12683
12684       return type;
12685
12686     case RID_TYPEOF:
12687       /* Consume the `typeof' token.  */
12688       cp_lexer_consume_token (parser->lexer);
12689       /* Parse the operand to `typeof'.  */
12690       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12691       /* If it is not already a TYPE, take its type.  */
12692       if (!TYPE_P (type))
12693         type = finish_typeof (type);
12694
12695       if (decl_specs)
12696         cp_parser_set_decl_spec_type (decl_specs, type,
12697                                       token->location,
12698                                       /*user_defined_p=*/true);
12699
12700       return type;
12701
12702     case RID_UNDERLYING_TYPE:
12703       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12704
12705       if (decl_specs)
12706         cp_parser_set_decl_spec_type (decl_specs, type,
12707                                       token->location,
12708                                       /*user_defined_p=*/true);
12709
12710       return type;
12711
12712     default:
12713       break;
12714     }
12715
12716   /* If the type-specifier was for a built-in type, we're done.  */
12717   if (type)
12718     {
12719       /* Record the type.  */
12720       if (decl_specs
12721           && (token->keyword != RID_SIGNED
12722               && token->keyword != RID_UNSIGNED
12723               && token->keyword != RID_SHORT
12724               && token->keyword != RID_LONG))
12725         cp_parser_set_decl_spec_type (decl_specs,
12726                                       type,
12727                                       token->location,
12728                                       /*user_defined=*/false);
12729       if (decl_specs)
12730         decl_specs->any_specifiers_p = true;
12731
12732       /* Consume the token.  */
12733       cp_lexer_consume_token (parser->lexer);
12734
12735       /* There is no valid C++ program where a non-template type is
12736          followed by a "<".  That usually indicates that the user thought
12737          that the type was a template.  */
12738       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12739
12740       return TYPE_NAME (type);
12741     }
12742
12743   /* The type-specifier must be a user-defined type.  */
12744   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12745     {
12746       bool qualified_p;
12747       bool global_p;
12748
12749       /* Don't gobble tokens or issue error messages if this is an
12750          optional type-specifier.  */
12751       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12752         cp_parser_parse_tentatively (parser);
12753
12754       /* Look for the optional `::' operator.  */
12755       global_p
12756         = (cp_parser_global_scope_opt (parser,
12757                                        /*current_scope_valid_p=*/false)
12758            != NULL_TREE);
12759       /* Look for the nested-name specifier.  */
12760       qualified_p
12761         = (cp_parser_nested_name_specifier_opt (parser,
12762                                                 /*typename_keyword_p=*/false,
12763                                                 /*check_dependency_p=*/true,
12764                                                 /*type_p=*/false,
12765                                                 /*is_declaration=*/false)
12766            != NULL_TREE);
12767       token = cp_lexer_peek_token (parser->lexer);
12768       /* If we have seen a nested-name-specifier, and the next token
12769          is `template', then we are using the template-id production.  */
12770       if (parser->scope
12771           && cp_parser_optional_template_keyword (parser))
12772         {
12773           /* Look for the template-id.  */
12774           type = cp_parser_template_id (parser,
12775                                         /*template_keyword_p=*/true,
12776                                         /*check_dependency_p=*/true,
12777                                         /*is_declaration=*/false);
12778           /* If the template-id did not name a type, we are out of
12779              luck.  */
12780           if (TREE_CODE (type) != TYPE_DECL)
12781             {
12782               cp_parser_error (parser, "expected template-id for type");
12783               type = NULL_TREE;
12784             }
12785         }
12786       /* Otherwise, look for a type-name.  */
12787       else
12788         type = cp_parser_type_name (parser);
12789       /* Keep track of all name-lookups performed in class scopes.  */
12790       if (type
12791           && !global_p
12792           && !qualified_p
12793           && TREE_CODE (type) == TYPE_DECL
12794           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12795         maybe_note_name_used_in_class (DECL_NAME (type), type);
12796       /* If it didn't work out, we don't have a TYPE.  */
12797       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12798           && !cp_parser_parse_definitely (parser))
12799         type = NULL_TREE;
12800       if (type && decl_specs)
12801         cp_parser_set_decl_spec_type (decl_specs, type,
12802                                       token->location,
12803                                       /*user_defined=*/true);
12804     }
12805
12806   /* If we didn't get a type-name, issue an error message.  */
12807   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12808     {
12809       cp_parser_error (parser, "expected type-name");
12810       return error_mark_node;
12811     }
12812
12813   if (type && type != error_mark_node)
12814     {
12815       /* See if TYPE is an Objective-C type, and if so, parse and
12816          accept any protocol references following it.  Do this before
12817          the cp_parser_check_for_invalid_template_id() call, because
12818          Objective-C types can be followed by '<...>' which would
12819          enclose protocol names rather than template arguments, and so
12820          everything is fine.  */
12821       if (c_dialect_objc () && !parser->scope
12822           && (objc_is_id (type) || objc_is_class_name (type)))
12823         {
12824           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12825           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12826
12827           /* Clobber the "unqualified" type previously entered into
12828              DECL_SPECS with the new, improved protocol-qualified version.  */
12829           if (decl_specs)
12830             decl_specs->type = qual_type;
12831
12832           return qual_type;
12833         }
12834
12835       /* There is no valid C++ program where a non-template type is
12836          followed by a "<".  That usually indicates that the user
12837          thought that the type was a template.  */
12838       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12839                                                token->location);
12840     }
12841
12842   return type;
12843 }
12844
12845 /* Parse a type-name.
12846
12847    type-name:
12848      class-name
12849      enum-name
12850      typedef-name
12851
12852    enum-name:
12853      identifier
12854
12855    typedef-name:
12856      identifier
12857
12858    Returns a TYPE_DECL for the type.  */
12859
12860 static tree
12861 cp_parser_type_name (cp_parser* parser)
12862 {
12863   tree type_decl;
12864
12865   /* We can't know yet whether it is a class-name or not.  */
12866   cp_parser_parse_tentatively (parser);
12867   /* Try a class-name.  */
12868   type_decl = cp_parser_class_name (parser,
12869                                     /*typename_keyword_p=*/false,
12870                                     /*template_keyword_p=*/false,
12871                                     none_type,
12872                                     /*check_dependency_p=*/true,
12873                                     /*class_head_p=*/false,
12874                                     /*is_declaration=*/false);
12875   /* If it's not a class-name, keep looking.  */
12876   if (!cp_parser_parse_definitely (parser))
12877     {
12878       /* It must be a typedef-name or an enum-name.  */
12879       return cp_parser_nonclass_name (parser);
12880     }
12881
12882   return type_decl;
12883 }
12884
12885 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12886
12887    enum-name:
12888      identifier
12889
12890    typedef-name:
12891      identifier
12892
12893    Returns a TYPE_DECL for the type.  */
12894
12895 static tree
12896 cp_parser_nonclass_name (cp_parser* parser)
12897 {
12898   tree type_decl;
12899   tree identifier;
12900
12901   cp_token *token = cp_lexer_peek_token (parser->lexer);
12902   identifier = cp_parser_identifier (parser);
12903   if (identifier == error_mark_node)
12904     return error_mark_node;
12905
12906   /* Look up the type-name.  */
12907   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12908
12909   if (TREE_CODE (type_decl) != TYPE_DECL
12910       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12911     {
12912       /* See if this is an Objective-C type.  */
12913       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12914       tree type = objc_get_protocol_qualified_type (identifier, protos);
12915       if (type)
12916         type_decl = TYPE_NAME (type);
12917     }
12918
12919   /* Issue an error if we did not find a type-name.  */
12920   if (TREE_CODE (type_decl) != TYPE_DECL
12921       /* In Objective-C, we have the complication that class names are
12922          normally type names and start declarations (eg, the
12923          "NSObject" in "NSObject *object;"), but can be used in an
12924          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12925          is an expression.  So, a classname followed by a dot is not a
12926          valid type-name.  */
12927       || (objc_is_class_name (TREE_TYPE (type_decl))
12928           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12929     {
12930       if (!cp_parser_simulate_error (parser))
12931         cp_parser_name_lookup_error (parser, identifier, type_decl,
12932                                      NLE_TYPE, token->location);
12933       return error_mark_node;
12934     }
12935   /* Remember that the name was used in the definition of the
12936      current class so that we can check later to see if the
12937      meaning would have been different after the class was
12938      entirely defined.  */
12939   else if (type_decl != error_mark_node
12940            && !parser->scope)
12941     maybe_note_name_used_in_class (identifier, type_decl);
12942   
12943   return type_decl;
12944 }
12945
12946 /* Parse an elaborated-type-specifier.  Note that the grammar given
12947    here incorporates the resolution to DR68.
12948
12949    elaborated-type-specifier:
12950      class-key :: [opt] nested-name-specifier [opt] identifier
12951      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12952      enum-key :: [opt] nested-name-specifier [opt] identifier
12953      typename :: [opt] nested-name-specifier identifier
12954      typename :: [opt] nested-name-specifier template [opt]
12955        template-id
12956
12957    GNU extension:
12958
12959    elaborated-type-specifier:
12960      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12961      class-key attributes :: [opt] nested-name-specifier [opt]
12962                template [opt] template-id
12963      enum attributes :: [opt] nested-name-specifier [opt] identifier
12964
12965    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12966    declared `friend'.  If IS_DECLARATION is TRUE, then this
12967    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12968    something is being declared.
12969
12970    Returns the TYPE specified.  */
12971
12972 static tree
12973 cp_parser_elaborated_type_specifier (cp_parser* parser,
12974                                      bool is_friend,
12975                                      bool is_declaration)
12976 {
12977   enum tag_types tag_type;
12978   tree identifier;
12979   tree type = NULL_TREE;
12980   tree attributes = NULL_TREE;
12981   tree globalscope;
12982   cp_token *token = NULL;
12983
12984   /* See if we're looking at the `enum' keyword.  */
12985   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12986     {
12987       /* Consume the `enum' token.  */
12988       cp_lexer_consume_token (parser->lexer);
12989       /* Remember that it's an enumeration type.  */
12990       tag_type = enum_type;
12991       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12992          enums) is used here.  */
12993       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12994           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12995         {
12996             pedwarn (input_location, 0, "elaborated-type-specifier "
12997                       "for a scoped enum must not use the %<%D%> keyword",
12998                       cp_lexer_peek_token (parser->lexer)->u.value);
12999           /* Consume the `struct' or `class' and parse it anyway.  */
13000           cp_lexer_consume_token (parser->lexer);
13001         }
13002       /* Parse the attributes.  */
13003       attributes = cp_parser_attributes_opt (parser);
13004     }
13005   /* Or, it might be `typename'.  */
13006   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13007                                            RID_TYPENAME))
13008     {
13009       /* Consume the `typename' token.  */
13010       cp_lexer_consume_token (parser->lexer);
13011       /* Remember that it's a `typename' type.  */
13012       tag_type = typename_type;
13013     }
13014   /* Otherwise it must be a class-key.  */
13015   else
13016     {
13017       tag_type = cp_parser_class_key (parser);
13018       if (tag_type == none_type)
13019         return error_mark_node;
13020       /* Parse the attributes.  */
13021       attributes = cp_parser_attributes_opt (parser);
13022     }
13023
13024   /* Look for the `::' operator.  */
13025   globalscope =  cp_parser_global_scope_opt (parser,
13026                                              /*current_scope_valid_p=*/false);
13027   /* Look for the nested-name-specifier.  */
13028   if (tag_type == typename_type && !globalscope)
13029     {
13030       if (!cp_parser_nested_name_specifier (parser,
13031                                            /*typename_keyword_p=*/true,
13032                                            /*check_dependency_p=*/true,
13033                                            /*type_p=*/true,
13034                                             is_declaration))
13035         return error_mark_node;
13036     }
13037   else
13038     /* Even though `typename' is not present, the proposed resolution
13039        to Core Issue 180 says that in `class A<T>::B', `B' should be
13040        considered a type-name, even if `A<T>' is dependent.  */
13041     cp_parser_nested_name_specifier_opt (parser,
13042                                          /*typename_keyword_p=*/true,
13043                                          /*check_dependency_p=*/true,
13044                                          /*type_p=*/true,
13045                                          is_declaration);
13046  /* For everything but enumeration types, consider a template-id.
13047     For an enumeration type, consider only a plain identifier.  */
13048   if (tag_type != enum_type)
13049     {
13050       bool template_p = false;
13051       tree decl;
13052
13053       /* Allow the `template' keyword.  */
13054       template_p = cp_parser_optional_template_keyword (parser);
13055       /* If we didn't see `template', we don't know if there's a
13056          template-id or not.  */
13057       if (!template_p)
13058         cp_parser_parse_tentatively (parser);
13059       /* Parse the template-id.  */
13060       token = cp_lexer_peek_token (parser->lexer);
13061       decl = cp_parser_template_id (parser, template_p,
13062                                     /*check_dependency_p=*/true,
13063                                     is_declaration);
13064       /* If we didn't find a template-id, look for an ordinary
13065          identifier.  */
13066       if (!template_p && !cp_parser_parse_definitely (parser))
13067         ;
13068       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13069          in effect, then we must assume that, upon instantiation, the
13070          template will correspond to a class.  */
13071       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13072                && tag_type == typename_type)
13073         type = make_typename_type (parser->scope, decl,
13074                                    typename_type,
13075                                    /*complain=*/tf_error);
13076       /* If the `typename' keyword is in effect and DECL is not a type
13077          decl. Then type is non existant.   */
13078       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13079         type = NULL_TREE; 
13080       else 
13081         type = TREE_TYPE (decl);
13082     }
13083
13084   if (!type)
13085     {
13086       token = cp_lexer_peek_token (parser->lexer);
13087       identifier = cp_parser_identifier (parser);
13088
13089       if (identifier == error_mark_node)
13090         {
13091           parser->scope = NULL_TREE;
13092           return error_mark_node;
13093         }
13094
13095       /* For a `typename', we needn't call xref_tag.  */
13096       if (tag_type == typename_type
13097           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13098         return cp_parser_make_typename_type (parser, parser->scope,
13099                                              identifier,
13100                                              token->location);
13101       /* Look up a qualified name in the usual way.  */
13102       if (parser->scope)
13103         {
13104           tree decl;
13105           tree ambiguous_decls;
13106
13107           decl = cp_parser_lookup_name (parser, identifier,
13108                                         tag_type,
13109                                         /*is_template=*/false,
13110                                         /*is_namespace=*/false,
13111                                         /*check_dependency=*/true,
13112                                         &ambiguous_decls,
13113                                         token->location);
13114
13115           /* If the lookup was ambiguous, an error will already have been
13116              issued.  */
13117           if (ambiguous_decls)
13118             return error_mark_node;
13119
13120           /* If we are parsing friend declaration, DECL may be a
13121              TEMPLATE_DECL tree node here.  However, we need to check
13122              whether this TEMPLATE_DECL results in valid code.  Consider
13123              the following example:
13124
13125                namespace N {
13126                  template <class T> class C {};
13127                }
13128                class X {
13129                  template <class T> friend class N::C; // #1, valid code
13130                };
13131                template <class T> class Y {
13132                  friend class N::C;                    // #2, invalid code
13133                };
13134
13135              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13136              name lookup of `N::C'.  We see that friend declaration must
13137              be template for the code to be valid.  Note that
13138              processing_template_decl does not work here since it is
13139              always 1 for the above two cases.  */
13140
13141           decl = (cp_parser_maybe_treat_template_as_class
13142                   (decl, /*tag_name_p=*/is_friend
13143                          && parser->num_template_parameter_lists));
13144
13145           if (TREE_CODE (decl) != TYPE_DECL)
13146             {
13147               cp_parser_diagnose_invalid_type_name (parser,
13148                                                     parser->scope,
13149                                                     identifier,
13150                                                     token->location);
13151               return error_mark_node;
13152             }
13153
13154           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13155             {
13156               bool allow_template = (parser->num_template_parameter_lists
13157                                       || DECL_SELF_REFERENCE_P (decl));
13158               type = check_elaborated_type_specifier (tag_type, decl, 
13159                                                       allow_template);
13160
13161               if (type == error_mark_node)
13162                 return error_mark_node;
13163             }
13164
13165           /* Forward declarations of nested types, such as
13166
13167                class C1::C2;
13168                class C1::C2::C3;
13169
13170              are invalid unless all components preceding the final '::'
13171              are complete.  If all enclosing types are complete, these
13172              declarations become merely pointless.
13173
13174              Invalid forward declarations of nested types are errors
13175              caught elsewhere in parsing.  Those that are pointless arrive
13176              here.  */
13177
13178           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13179               && !is_friend && !processing_explicit_instantiation)
13180             warning (0, "declaration %qD does not declare anything", decl);
13181
13182           type = TREE_TYPE (decl);
13183         }
13184       else
13185         {
13186           /* An elaborated-type-specifier sometimes introduces a new type and
13187              sometimes names an existing type.  Normally, the rule is that it
13188              introduces a new type only if there is not an existing type of
13189              the same name already in scope.  For example, given:
13190
13191                struct S {};
13192                void f() { struct S s; }
13193
13194              the `struct S' in the body of `f' is the same `struct S' as in
13195              the global scope; the existing definition is used.  However, if
13196              there were no global declaration, this would introduce a new
13197              local class named `S'.
13198
13199              An exception to this rule applies to the following code:
13200
13201                namespace N { struct S; }
13202
13203              Here, the elaborated-type-specifier names a new type
13204              unconditionally; even if there is already an `S' in the
13205              containing scope this declaration names a new type.
13206              This exception only applies if the elaborated-type-specifier
13207              forms the complete declaration:
13208
13209                [class.name]
13210
13211                A declaration consisting solely of `class-key identifier ;' is
13212                either a redeclaration of the name in the current scope or a
13213                forward declaration of the identifier as a class name.  It
13214                introduces the name into the current scope.
13215
13216              We are in this situation precisely when the next token is a `;'.
13217
13218              An exception to the exception is that a `friend' declaration does
13219              *not* name a new type; i.e., given:
13220
13221                struct S { friend struct T; };
13222
13223              `T' is not a new type in the scope of `S'.
13224
13225              Also, `new struct S' or `sizeof (struct S)' never results in the
13226              definition of a new type; a new type can only be declared in a
13227              declaration context.  */
13228
13229           tag_scope ts;
13230           bool template_p;
13231
13232           if (is_friend)
13233             /* Friends have special name lookup rules.  */
13234             ts = ts_within_enclosing_non_class;
13235           else if (is_declaration
13236                    && cp_lexer_next_token_is (parser->lexer,
13237                                               CPP_SEMICOLON))
13238             /* This is a `class-key identifier ;' */
13239             ts = ts_current;
13240           else
13241             ts = ts_global;
13242
13243           template_p =
13244             (parser->num_template_parameter_lists
13245              && (cp_parser_next_token_starts_class_definition_p (parser)
13246                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13247           /* An unqualified name was used to reference this type, so
13248              there were no qualifying templates.  */
13249           if (!cp_parser_check_template_parameters (parser,
13250                                                     /*num_templates=*/0,
13251                                                     token->location,
13252                                                     /*declarator=*/NULL))
13253             return error_mark_node;
13254           type = xref_tag (tag_type, identifier, ts, template_p);
13255         }
13256     }
13257
13258   if (type == error_mark_node)
13259     return error_mark_node;
13260
13261   /* Allow attributes on forward declarations of classes.  */
13262   if (attributes)
13263     {
13264       if (TREE_CODE (type) == TYPENAME_TYPE)
13265         warning (OPT_Wattributes,
13266                  "attributes ignored on uninstantiated type");
13267       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13268                && ! processing_explicit_instantiation)
13269         warning (OPT_Wattributes,
13270                  "attributes ignored on template instantiation");
13271       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13272         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13273       else
13274         warning (OPT_Wattributes,
13275                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13276     }
13277
13278   if (tag_type != enum_type)
13279     cp_parser_check_class_key (tag_type, type);
13280
13281   /* A "<" cannot follow an elaborated type specifier.  If that
13282      happens, the user was probably trying to form a template-id.  */
13283   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13284
13285   return type;
13286 }
13287
13288 /* Parse an enum-specifier.
13289
13290    enum-specifier:
13291      enum-head { enumerator-list [opt] }
13292
13293    enum-head:
13294      enum-key identifier [opt] enum-base [opt]
13295      enum-key nested-name-specifier identifier enum-base [opt]
13296
13297    enum-key:
13298      enum
13299      enum class   [C++0x]
13300      enum struct  [C++0x]
13301
13302    enum-base:   [C++0x]
13303      : type-specifier-seq
13304
13305    opaque-enum-specifier:
13306      enum-key identifier enum-base [opt] ;
13307
13308    GNU Extensions:
13309      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13310        { enumerator-list [opt] }attributes[opt]
13311
13312    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13313    if the token stream isn't an enum-specifier after all.  */
13314
13315 static tree
13316 cp_parser_enum_specifier (cp_parser* parser)
13317 {
13318   tree identifier;
13319   tree type = NULL_TREE;
13320   tree prev_scope;
13321   tree nested_name_specifier = NULL_TREE;
13322   tree attributes;
13323   bool scoped_enum_p = false;
13324   bool has_underlying_type = false;
13325   bool nested_being_defined = false;
13326   bool new_value_list = false;
13327   bool is_new_type = false;
13328   bool is_anonymous = false;
13329   tree underlying_type = NULL_TREE;
13330   cp_token *type_start_token = NULL;
13331   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13332
13333   parser->colon_corrects_to_scope_p = false;
13334
13335   /* Parse tentatively so that we can back up if we don't find a
13336      enum-specifier.  */
13337   cp_parser_parse_tentatively (parser);
13338
13339   /* Caller guarantees that the current token is 'enum', an identifier
13340      possibly follows, and the token after that is an opening brace.
13341      If we don't have an identifier, fabricate an anonymous name for
13342      the enumeration being defined.  */
13343   cp_lexer_consume_token (parser->lexer);
13344
13345   /* Parse the "class" or "struct", which indicates a scoped
13346      enumeration type in C++0x.  */
13347   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13348       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13349     {
13350       if (cxx_dialect < cxx0x)
13351         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13352
13353       /* Consume the `struct' or `class' token.  */
13354       cp_lexer_consume_token (parser->lexer);
13355
13356       scoped_enum_p = true;
13357     }
13358
13359   attributes = cp_parser_attributes_opt (parser);
13360
13361   /* Clear the qualification.  */
13362   parser->scope = NULL_TREE;
13363   parser->qualifying_scope = NULL_TREE;
13364   parser->object_scope = NULL_TREE;
13365
13366   /* Figure out in what scope the declaration is being placed.  */
13367   prev_scope = current_scope ();
13368
13369   type_start_token = cp_lexer_peek_token (parser->lexer);
13370
13371   push_deferring_access_checks (dk_no_check);
13372   nested_name_specifier
13373       = cp_parser_nested_name_specifier_opt (parser,
13374                                              /*typename_keyword_p=*/true,
13375                                              /*check_dependency_p=*/false,
13376                                              /*type_p=*/false,
13377                                              /*is_declaration=*/false);
13378
13379   if (nested_name_specifier)
13380     {
13381       tree name;
13382
13383       identifier = cp_parser_identifier (parser);
13384       name =  cp_parser_lookup_name (parser, identifier,
13385                                      enum_type,
13386                                      /*is_template=*/false,
13387                                      /*is_namespace=*/false,
13388                                      /*check_dependency=*/true,
13389                                      /*ambiguous_decls=*/NULL,
13390                                      input_location);
13391       if (name)
13392         {
13393           type = TREE_TYPE (name);
13394           if (TREE_CODE (type) == TYPENAME_TYPE)
13395             {
13396               /* Are template enums allowed in ISO? */
13397               if (template_parm_scope_p ())
13398                 pedwarn (type_start_token->location, OPT_pedantic,
13399                          "%qD is an enumeration template", name);
13400               /* ignore a typename reference, for it will be solved by name
13401                  in start_enum.  */
13402               type = NULL_TREE;
13403             }
13404         }
13405       else
13406         error_at (type_start_token->location,
13407                   "%qD is not an enumerator-name", identifier);
13408     }
13409   else
13410     {
13411       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13412         identifier = cp_parser_identifier (parser);
13413       else
13414         {
13415           identifier = make_anon_name ();
13416           is_anonymous = true;
13417         }
13418     }
13419   pop_deferring_access_checks ();
13420
13421   /* Check for the `:' that denotes a specified underlying type in C++0x.
13422      Note that a ':' could also indicate a bitfield width, however.  */
13423   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13424     {
13425       cp_decl_specifier_seq type_specifiers;
13426
13427       /* Consume the `:'.  */
13428       cp_lexer_consume_token (parser->lexer);
13429
13430       /* Parse the type-specifier-seq.  */
13431       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13432                                     /*is_trailing_return=*/false,
13433                                     &type_specifiers);
13434
13435       /* At this point this is surely not elaborated type specifier.  */
13436       if (!cp_parser_parse_definitely (parser))
13437         return NULL_TREE;
13438
13439       if (cxx_dialect < cxx0x)
13440         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13441
13442       has_underlying_type = true;
13443
13444       /* If that didn't work, stop.  */
13445       if (type_specifiers.type != error_mark_node)
13446         {
13447           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13448                                             /*initialized=*/0, NULL);
13449           if (underlying_type == error_mark_node)
13450             underlying_type = NULL_TREE;
13451         }
13452     }
13453
13454   /* Look for the `{' but don't consume it yet.  */
13455   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13456     {
13457       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13458         {
13459           cp_parser_error (parser, "expected %<{%>");
13460           if (has_underlying_type)
13461             {
13462               type = NULL_TREE;
13463               goto out;
13464             }
13465         }
13466       /* An opaque-enum-specifier must have a ';' here.  */
13467       if ((scoped_enum_p || underlying_type)
13468           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13469         {
13470           cp_parser_error (parser, "expected %<;%> or %<{%>");
13471           if (has_underlying_type)
13472             {
13473               type = NULL_TREE;
13474               goto out;
13475             }
13476         }
13477     }
13478
13479   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13480     return NULL_TREE;
13481
13482   if (nested_name_specifier)
13483     {
13484       if (CLASS_TYPE_P (nested_name_specifier))
13485         {
13486           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13487           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13488           push_scope (nested_name_specifier);
13489         }
13490       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13491         {
13492           push_nested_namespace (nested_name_specifier);
13493         }
13494     }
13495
13496   /* Issue an error message if type-definitions are forbidden here.  */
13497   if (!cp_parser_check_type_definition (parser))
13498     type = error_mark_node;
13499   else
13500     /* Create the new type.  We do this before consuming the opening
13501        brace so the enum will be recorded as being on the line of its
13502        tag (or the 'enum' keyword, if there is no tag).  */
13503     type = start_enum (identifier, type, underlying_type,
13504                        scoped_enum_p, &is_new_type);
13505
13506   /* If the next token is not '{' it is an opaque-enum-specifier or an
13507      elaborated-type-specifier.  */
13508   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13509     {
13510       timevar_push (TV_PARSE_ENUM);
13511       if (nested_name_specifier)
13512         {
13513           /* The following catches invalid code such as:
13514              enum class S<int>::E { A, B, C }; */
13515           if (!processing_specialization
13516               && CLASS_TYPE_P (nested_name_specifier)
13517               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13518             error_at (type_start_token->location, "cannot add an enumerator "
13519                       "list to a template instantiation");
13520
13521           /* If that scope does not contain the scope in which the
13522              class was originally declared, the program is invalid.  */
13523           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13524             {
13525               if (at_namespace_scope_p ())
13526                 error_at (type_start_token->location,
13527                           "declaration of %qD in namespace %qD which does not "
13528                           "enclose %qD",
13529                           type, prev_scope, nested_name_specifier);
13530               else
13531                 error_at (type_start_token->location,
13532                           "declaration of %qD in %qD which does not enclose %qD",
13533                           type, prev_scope, nested_name_specifier);
13534               type = error_mark_node;
13535             }
13536         }
13537
13538       if (scoped_enum_p)
13539         begin_scope (sk_scoped_enum, type);
13540
13541       /* Consume the opening brace.  */
13542       cp_lexer_consume_token (parser->lexer);
13543
13544       if (type == error_mark_node)
13545         ; /* Nothing to add */
13546       else if (OPAQUE_ENUM_P (type)
13547                || (cxx_dialect > cxx98 && processing_specialization))
13548         {
13549           new_value_list = true;
13550           SET_OPAQUE_ENUM_P (type, false);
13551           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13552         }
13553       else
13554         {
13555           error_at (type_start_token->location, "multiple definition of %q#T", type);
13556           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13557                     "previous definition here");
13558           type = error_mark_node;
13559         }
13560
13561       if (type == error_mark_node)
13562         cp_parser_skip_to_end_of_block_or_statement (parser);
13563       /* If the next token is not '}', then there are some enumerators.  */
13564       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13565         cp_parser_enumerator_list (parser, type);
13566
13567       /* Consume the final '}'.  */
13568       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13569
13570       if (scoped_enum_p)
13571         finish_scope ();
13572       timevar_pop (TV_PARSE_ENUM);
13573     }
13574   else
13575     {
13576       /* If a ';' follows, then it is an opaque-enum-specifier
13577         and additional restrictions apply.  */
13578       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13579         {
13580           if (is_anonymous)
13581             error_at (type_start_token->location,
13582                       "opaque-enum-specifier without name");
13583           else if (nested_name_specifier)
13584             error_at (type_start_token->location,
13585                       "opaque-enum-specifier must use a simple identifier");
13586         }
13587     }
13588
13589   /* Look for trailing attributes to apply to this enumeration, and
13590      apply them if appropriate.  */
13591   if (cp_parser_allow_gnu_extensions_p (parser))
13592     {
13593       tree trailing_attr = cp_parser_attributes_opt (parser);
13594       trailing_attr = chainon (trailing_attr, attributes);
13595       cplus_decl_attributes (&type,
13596                              trailing_attr,
13597                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13598     }
13599
13600   /* Finish up the enumeration.  */
13601   if (type != error_mark_node)
13602     {
13603       if (new_value_list)
13604         finish_enum_value_list (type);
13605       if (is_new_type)
13606         finish_enum (type);
13607     }
13608
13609   if (nested_name_specifier)
13610     {
13611       if (CLASS_TYPE_P (nested_name_specifier))
13612         {
13613           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13614           pop_scope (nested_name_specifier);
13615         }
13616       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13617         {
13618           pop_nested_namespace (nested_name_specifier);
13619         }
13620     }
13621  out:
13622   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13623   return type;
13624 }
13625
13626 /* Parse an enumerator-list.  The enumerators all have the indicated
13627    TYPE.
13628
13629    enumerator-list:
13630      enumerator-definition
13631      enumerator-list , enumerator-definition  */
13632
13633 static void
13634 cp_parser_enumerator_list (cp_parser* parser, tree type)
13635 {
13636   while (true)
13637     {
13638       /* Parse an enumerator-definition.  */
13639       cp_parser_enumerator_definition (parser, type);
13640
13641       /* If the next token is not a ',', we've reached the end of
13642          the list.  */
13643       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13644         break;
13645       /* Otherwise, consume the `,' and keep going.  */
13646       cp_lexer_consume_token (parser->lexer);
13647       /* If the next token is a `}', there is a trailing comma.  */
13648       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13649         {
13650           if (!in_system_header)
13651             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13652           break;
13653         }
13654     }
13655 }
13656
13657 /* Parse an enumerator-definition.  The enumerator has the indicated
13658    TYPE.
13659
13660    enumerator-definition:
13661      enumerator
13662      enumerator = constant-expression
13663
13664    enumerator:
13665      identifier  */
13666
13667 static void
13668 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13669 {
13670   tree identifier;
13671   tree value;
13672   location_t loc;
13673
13674   /* Save the input location because we are interested in the location
13675      of the identifier and not the location of the explicit value.  */
13676   loc = cp_lexer_peek_token (parser->lexer)->location;
13677
13678   /* Look for the identifier.  */
13679   identifier = cp_parser_identifier (parser);
13680   if (identifier == error_mark_node)
13681     return;
13682
13683   /* If the next token is an '=', then there is an explicit value.  */
13684   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13685     {
13686       /* Consume the `=' token.  */
13687       cp_lexer_consume_token (parser->lexer);
13688       /* Parse the value.  */
13689       value = cp_parser_constant_expression (parser,
13690                                              /*allow_non_constant_p=*/false,
13691                                              NULL);
13692     }
13693   else
13694     value = NULL_TREE;
13695
13696   /* If we are processing a template, make sure the initializer of the
13697      enumerator doesn't contain any bare template parameter pack.  */
13698   if (check_for_bare_parameter_packs (value))
13699     value = error_mark_node;
13700
13701   /* integral_constant_value will pull out this expression, so make sure
13702      it's folded as appropriate.  */
13703   value = fold_non_dependent_expr (value);
13704
13705   /* Create the enumerator.  */
13706   build_enumerator (identifier, value, type, loc);
13707 }
13708
13709 /* Parse a namespace-name.
13710
13711    namespace-name:
13712      original-namespace-name
13713      namespace-alias
13714
13715    Returns the NAMESPACE_DECL for the namespace.  */
13716
13717 static tree
13718 cp_parser_namespace_name (cp_parser* parser)
13719 {
13720   tree identifier;
13721   tree namespace_decl;
13722
13723   cp_token *token = cp_lexer_peek_token (parser->lexer);
13724
13725   /* Get the name of the namespace.  */
13726   identifier = cp_parser_identifier (parser);
13727   if (identifier == error_mark_node)
13728     return error_mark_node;
13729
13730   /* Look up the identifier in the currently active scope.  Look only
13731      for namespaces, due to:
13732
13733        [basic.lookup.udir]
13734
13735        When looking up a namespace-name in a using-directive or alias
13736        definition, only namespace names are considered.
13737
13738      And:
13739
13740        [basic.lookup.qual]
13741
13742        During the lookup of a name preceding the :: scope resolution
13743        operator, object, function, and enumerator names are ignored.
13744
13745      (Note that cp_parser_qualifying_entity only calls this
13746      function if the token after the name is the scope resolution
13747      operator.)  */
13748   namespace_decl = cp_parser_lookup_name (parser, identifier,
13749                                           none_type,
13750                                           /*is_template=*/false,
13751                                           /*is_namespace=*/true,
13752                                           /*check_dependency=*/true,
13753                                           /*ambiguous_decls=*/NULL,
13754                                           token->location);
13755   /* If it's not a namespace, issue an error.  */
13756   if (namespace_decl == error_mark_node
13757       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13758     {
13759       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13760         error_at (token->location, "%qD is not a namespace-name", identifier);
13761       cp_parser_error (parser, "expected namespace-name");
13762       namespace_decl = error_mark_node;
13763     }
13764
13765   return namespace_decl;
13766 }
13767
13768 /* Parse a namespace-definition.
13769
13770    namespace-definition:
13771      named-namespace-definition
13772      unnamed-namespace-definition
13773
13774    named-namespace-definition:
13775      original-namespace-definition
13776      extension-namespace-definition
13777
13778    original-namespace-definition:
13779      namespace identifier { namespace-body }
13780
13781    extension-namespace-definition:
13782      namespace original-namespace-name { namespace-body }
13783
13784    unnamed-namespace-definition:
13785      namespace { namespace-body } */
13786
13787 static void
13788 cp_parser_namespace_definition (cp_parser* parser)
13789 {
13790   tree identifier, attribs;
13791   bool has_visibility;
13792   bool is_inline;
13793
13794   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13795     {
13796       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13797       is_inline = true;
13798       cp_lexer_consume_token (parser->lexer);
13799     }
13800   else
13801     is_inline = false;
13802
13803   /* Look for the `namespace' keyword.  */
13804   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13805
13806   /* Get the name of the namespace.  We do not attempt to distinguish
13807      between an original-namespace-definition and an
13808      extension-namespace-definition at this point.  The semantic
13809      analysis routines are responsible for that.  */
13810   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13811     identifier = cp_parser_identifier (parser);
13812   else
13813     identifier = NULL_TREE;
13814
13815   /* Parse any specified attributes.  */
13816   attribs = cp_parser_attributes_opt (parser);
13817
13818   /* Look for the `{' to start the namespace.  */
13819   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13820   /* Start the namespace.  */
13821   push_namespace (identifier);
13822
13823   /* "inline namespace" is equivalent to a stub namespace definition
13824      followed by a strong using directive.  */
13825   if (is_inline)
13826     {
13827       tree name_space = current_namespace;
13828       /* Set up namespace association.  */
13829       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13830         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13831                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13832       /* Import the contents of the inline namespace.  */
13833       pop_namespace ();
13834       do_using_directive (name_space);
13835       push_namespace (identifier);
13836     }
13837
13838   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13839
13840   /* Parse the body of the namespace.  */
13841   cp_parser_namespace_body (parser);
13842
13843   if (has_visibility)
13844     pop_visibility (1);
13845
13846   /* Finish the namespace.  */
13847   pop_namespace ();
13848   /* Look for the final `}'.  */
13849   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13850 }
13851
13852 /* Parse a namespace-body.
13853
13854    namespace-body:
13855      declaration-seq [opt]  */
13856
13857 static void
13858 cp_parser_namespace_body (cp_parser* parser)
13859 {
13860   cp_parser_declaration_seq_opt (parser);
13861 }
13862
13863 /* Parse a namespace-alias-definition.
13864
13865    namespace-alias-definition:
13866      namespace identifier = qualified-namespace-specifier ;  */
13867
13868 static void
13869 cp_parser_namespace_alias_definition (cp_parser* parser)
13870 {
13871   tree identifier;
13872   tree namespace_specifier;
13873
13874   cp_token *token = cp_lexer_peek_token (parser->lexer);
13875
13876   /* Look for the `namespace' keyword.  */
13877   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13878   /* Look for the identifier.  */
13879   identifier = cp_parser_identifier (parser);
13880   if (identifier == error_mark_node)
13881     return;
13882   /* Look for the `=' token.  */
13883   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13884       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13885     {
13886       error_at (token->location, "%<namespace%> definition is not allowed here");
13887       /* Skip the definition.  */
13888       cp_lexer_consume_token (parser->lexer);
13889       if (cp_parser_skip_to_closing_brace (parser))
13890         cp_lexer_consume_token (parser->lexer);
13891       return;
13892     }
13893   cp_parser_require (parser, CPP_EQ, RT_EQ);
13894   /* Look for the qualified-namespace-specifier.  */
13895   namespace_specifier
13896     = cp_parser_qualified_namespace_specifier (parser);
13897   /* Look for the `;' token.  */
13898   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13899
13900   /* Register the alias in the symbol table.  */
13901   do_namespace_alias (identifier, namespace_specifier);
13902 }
13903
13904 /* Parse a qualified-namespace-specifier.
13905
13906    qualified-namespace-specifier:
13907      :: [opt] nested-name-specifier [opt] namespace-name
13908
13909    Returns a NAMESPACE_DECL corresponding to the specified
13910    namespace.  */
13911
13912 static tree
13913 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13914 {
13915   /* Look for the optional `::'.  */
13916   cp_parser_global_scope_opt (parser,
13917                               /*current_scope_valid_p=*/false);
13918
13919   /* Look for the optional nested-name-specifier.  */
13920   cp_parser_nested_name_specifier_opt (parser,
13921                                        /*typename_keyword_p=*/false,
13922                                        /*check_dependency_p=*/true,
13923                                        /*type_p=*/false,
13924                                        /*is_declaration=*/true);
13925
13926   return cp_parser_namespace_name (parser);
13927 }
13928
13929 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13930    access declaration.
13931
13932    using-declaration:
13933      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13934      using :: unqualified-id ;  
13935
13936    access-declaration:
13937      qualified-id ;  
13938
13939    */
13940
13941 static bool
13942 cp_parser_using_declaration (cp_parser* parser, 
13943                              bool access_declaration_p)
13944 {
13945   cp_token *token;
13946   bool typename_p = false;
13947   bool global_scope_p;
13948   tree decl;
13949   tree identifier;
13950   tree qscope;
13951
13952   if (access_declaration_p)
13953     cp_parser_parse_tentatively (parser);
13954   else
13955     {
13956       /* Look for the `using' keyword.  */
13957       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13958       
13959       /* Peek at the next token.  */
13960       token = cp_lexer_peek_token (parser->lexer);
13961       /* See if it's `typename'.  */
13962       if (token->keyword == RID_TYPENAME)
13963         {
13964           /* Remember that we've seen it.  */
13965           typename_p = true;
13966           /* Consume the `typename' token.  */
13967           cp_lexer_consume_token (parser->lexer);
13968         }
13969     }
13970
13971   /* Look for the optional global scope qualification.  */
13972   global_scope_p
13973     = (cp_parser_global_scope_opt (parser,
13974                                    /*current_scope_valid_p=*/false)
13975        != NULL_TREE);
13976
13977   /* If we saw `typename', or didn't see `::', then there must be a
13978      nested-name-specifier present.  */
13979   if (typename_p || !global_scope_p)
13980     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13981                                               /*check_dependency_p=*/true,
13982                                               /*type_p=*/false,
13983                                               /*is_declaration=*/true);
13984   /* Otherwise, we could be in either of the two productions.  In that
13985      case, treat the nested-name-specifier as optional.  */
13986   else
13987     qscope = cp_parser_nested_name_specifier_opt (parser,
13988                                                   /*typename_keyword_p=*/false,
13989                                                   /*check_dependency_p=*/true,
13990                                                   /*type_p=*/false,
13991                                                   /*is_declaration=*/true);
13992   if (!qscope)
13993     qscope = global_namespace;
13994
13995   if (access_declaration_p && cp_parser_error_occurred (parser))
13996     /* Something has already gone wrong; there's no need to parse
13997        further.  Since an error has occurred, the return value of
13998        cp_parser_parse_definitely will be false, as required.  */
13999     return cp_parser_parse_definitely (parser);
14000
14001   token = cp_lexer_peek_token (parser->lexer);
14002   /* Parse the unqualified-id.  */
14003   identifier = cp_parser_unqualified_id (parser,
14004                                          /*template_keyword_p=*/false,
14005                                          /*check_dependency_p=*/true,
14006                                          /*declarator_p=*/true,
14007                                          /*optional_p=*/false);
14008
14009   if (access_declaration_p)
14010     {
14011       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14012         cp_parser_simulate_error (parser);
14013       if (!cp_parser_parse_definitely (parser))
14014         return false;
14015     }
14016
14017   /* The function we call to handle a using-declaration is different
14018      depending on what scope we are in.  */
14019   if (qscope == error_mark_node || identifier == error_mark_node)
14020     ;
14021   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14022            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14023     /* [namespace.udecl]
14024
14025        A using declaration shall not name a template-id.  */
14026     error_at (token->location,
14027               "a template-id may not appear in a using-declaration");
14028   else
14029     {
14030       if (at_class_scope_p ())
14031         {
14032           /* Create the USING_DECL.  */
14033           decl = do_class_using_decl (parser->scope, identifier);
14034
14035           if (check_for_bare_parameter_packs (decl))
14036             return false;
14037           else
14038             /* Add it to the list of members in this class.  */
14039             finish_member_declaration (decl);
14040         }
14041       else
14042         {
14043           decl = cp_parser_lookup_name_simple (parser,
14044                                                identifier,
14045                                                token->location);
14046           if (decl == error_mark_node)
14047             cp_parser_name_lookup_error (parser, identifier,
14048                                          decl, NLE_NULL,
14049                                          token->location);
14050           else if (check_for_bare_parameter_packs (decl))
14051             return false;
14052           else if (!at_namespace_scope_p ())
14053             do_local_using_decl (decl, qscope, identifier);
14054           else
14055             do_toplevel_using_decl (decl, qscope, identifier);
14056         }
14057     }
14058
14059   /* Look for the final `;'.  */
14060   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14061   
14062   return true;
14063 }
14064
14065 /* Parse a using-directive.
14066
14067    using-directive:
14068      using namespace :: [opt] nested-name-specifier [opt]
14069        namespace-name ;  */
14070
14071 static void
14072 cp_parser_using_directive (cp_parser* parser)
14073 {
14074   tree namespace_decl;
14075   tree attribs;
14076
14077   /* Look for the `using' keyword.  */
14078   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14079   /* And the `namespace' keyword.  */
14080   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14081   /* Look for the optional `::' operator.  */
14082   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14083   /* And the optional nested-name-specifier.  */
14084   cp_parser_nested_name_specifier_opt (parser,
14085                                        /*typename_keyword_p=*/false,
14086                                        /*check_dependency_p=*/true,
14087                                        /*type_p=*/false,
14088                                        /*is_declaration=*/true);
14089   /* Get the namespace being used.  */
14090   namespace_decl = cp_parser_namespace_name (parser);
14091   /* And any specified attributes.  */
14092   attribs = cp_parser_attributes_opt (parser);
14093   /* Update the symbol table.  */
14094   parse_using_directive (namespace_decl, attribs);
14095   /* Look for the final `;'.  */
14096   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14097 }
14098
14099 /* Parse an asm-definition.
14100
14101    asm-definition:
14102      asm ( string-literal ) ;
14103
14104    GNU Extension:
14105
14106    asm-definition:
14107      asm volatile [opt] ( string-literal ) ;
14108      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14109      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14110                           : asm-operand-list [opt] ) ;
14111      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14112                           : asm-operand-list [opt]
14113                           : asm-clobber-list [opt] ) ;
14114      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14115                                : asm-clobber-list [opt]
14116                                : asm-goto-list ) ;  */
14117
14118 static void
14119 cp_parser_asm_definition (cp_parser* parser)
14120 {
14121   tree string;
14122   tree outputs = NULL_TREE;
14123   tree inputs = NULL_TREE;
14124   tree clobbers = NULL_TREE;
14125   tree labels = NULL_TREE;
14126   tree asm_stmt;
14127   bool volatile_p = false;
14128   bool extended_p = false;
14129   bool invalid_inputs_p = false;
14130   bool invalid_outputs_p = false;
14131   bool goto_p = false;
14132   required_token missing = RT_NONE;
14133
14134   /* Look for the `asm' keyword.  */
14135   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14136   /* See if the next token is `volatile'.  */
14137   if (cp_parser_allow_gnu_extensions_p (parser)
14138       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14139     {
14140       /* Remember that we saw the `volatile' keyword.  */
14141       volatile_p = true;
14142       /* Consume the token.  */
14143       cp_lexer_consume_token (parser->lexer);
14144     }
14145   if (cp_parser_allow_gnu_extensions_p (parser)
14146       && parser->in_function_body
14147       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14148     {
14149       /* Remember that we saw the `goto' keyword.  */
14150       goto_p = true;
14151       /* Consume the token.  */
14152       cp_lexer_consume_token (parser->lexer);
14153     }
14154   /* Look for the opening `('.  */
14155   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14156     return;
14157   /* Look for the string.  */
14158   string = cp_parser_string_literal (parser, false, false);
14159   if (string == error_mark_node)
14160     {
14161       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14162                                              /*consume_paren=*/true);
14163       return;
14164     }
14165
14166   /* If we're allowing GNU extensions, check for the extended assembly
14167      syntax.  Unfortunately, the `:' tokens need not be separated by
14168      a space in C, and so, for compatibility, we tolerate that here
14169      too.  Doing that means that we have to treat the `::' operator as
14170      two `:' tokens.  */
14171   if (cp_parser_allow_gnu_extensions_p (parser)
14172       && parser->in_function_body
14173       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14174           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14175     {
14176       bool inputs_p = false;
14177       bool clobbers_p = false;
14178       bool labels_p = false;
14179
14180       /* The extended syntax was used.  */
14181       extended_p = true;
14182
14183       /* Look for outputs.  */
14184       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14185         {
14186           /* Consume the `:'.  */
14187           cp_lexer_consume_token (parser->lexer);
14188           /* Parse the output-operands.  */
14189           if (cp_lexer_next_token_is_not (parser->lexer,
14190                                           CPP_COLON)
14191               && cp_lexer_next_token_is_not (parser->lexer,
14192                                              CPP_SCOPE)
14193               && cp_lexer_next_token_is_not (parser->lexer,
14194                                              CPP_CLOSE_PAREN)
14195               && !goto_p)
14196             outputs = cp_parser_asm_operand_list (parser);
14197
14198             if (outputs == error_mark_node)
14199               invalid_outputs_p = true;
14200         }
14201       /* If the next token is `::', there are no outputs, and the
14202          next token is the beginning of the inputs.  */
14203       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14204         /* The inputs are coming next.  */
14205         inputs_p = true;
14206
14207       /* Look for inputs.  */
14208       if (inputs_p
14209           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14210         {
14211           /* Consume the `:' or `::'.  */
14212           cp_lexer_consume_token (parser->lexer);
14213           /* Parse the output-operands.  */
14214           if (cp_lexer_next_token_is_not (parser->lexer,
14215                                           CPP_COLON)
14216               && cp_lexer_next_token_is_not (parser->lexer,
14217                                              CPP_SCOPE)
14218               && cp_lexer_next_token_is_not (parser->lexer,
14219                                              CPP_CLOSE_PAREN))
14220             inputs = cp_parser_asm_operand_list (parser);
14221
14222             if (inputs == error_mark_node)
14223               invalid_inputs_p = true;
14224         }
14225       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14226         /* The clobbers are coming next.  */
14227         clobbers_p = true;
14228
14229       /* Look for clobbers.  */
14230       if (clobbers_p
14231           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14232         {
14233           clobbers_p = true;
14234           /* Consume the `:' or `::'.  */
14235           cp_lexer_consume_token (parser->lexer);
14236           /* Parse the clobbers.  */
14237           if (cp_lexer_next_token_is_not (parser->lexer,
14238                                           CPP_COLON)
14239               && cp_lexer_next_token_is_not (parser->lexer,
14240                                              CPP_CLOSE_PAREN))
14241             clobbers = cp_parser_asm_clobber_list (parser);
14242         }
14243       else if (goto_p
14244                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14245         /* The labels are coming next.  */
14246         labels_p = true;
14247
14248       /* Look for labels.  */
14249       if (labels_p
14250           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14251         {
14252           labels_p = true;
14253           /* Consume the `:' or `::'.  */
14254           cp_lexer_consume_token (parser->lexer);
14255           /* Parse the labels.  */
14256           labels = cp_parser_asm_label_list (parser);
14257         }
14258
14259       if (goto_p && !labels_p)
14260         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14261     }
14262   else if (goto_p)
14263     missing = RT_COLON_SCOPE;
14264
14265   /* Look for the closing `)'.  */
14266   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14267                           missing ? missing : RT_CLOSE_PAREN))
14268     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14269                                            /*consume_paren=*/true);
14270   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14271
14272   if (!invalid_inputs_p && !invalid_outputs_p)
14273     {
14274       /* Create the ASM_EXPR.  */
14275       if (parser->in_function_body)
14276         {
14277           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14278                                       inputs, clobbers, labels);
14279           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14280           if (!extended_p)
14281             {
14282               tree temp = asm_stmt;
14283               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14284                 temp = TREE_OPERAND (temp, 0);
14285
14286               ASM_INPUT_P (temp) = 1;
14287             }
14288         }
14289       else
14290         cgraph_add_asm_node (string);
14291     }
14292 }
14293
14294 /* Declarators [gram.dcl.decl] */
14295
14296 /* Parse an init-declarator.
14297
14298    init-declarator:
14299      declarator initializer [opt]
14300
14301    GNU Extension:
14302
14303    init-declarator:
14304      declarator asm-specification [opt] attributes [opt] initializer [opt]
14305
14306    function-definition:
14307      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14308        function-body
14309      decl-specifier-seq [opt] declarator function-try-block
14310
14311    GNU Extension:
14312
14313    function-definition:
14314      __extension__ function-definition
14315
14316    The DECL_SPECIFIERS apply to this declarator.  Returns a
14317    representation of the entity declared.  If MEMBER_P is TRUE, then
14318    this declarator appears in a class scope.  The new DECL created by
14319    this declarator is returned.
14320
14321    The CHECKS are access checks that should be performed once we know
14322    what entity is being declared (and, therefore, what classes have
14323    befriended it).
14324
14325    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14326    for a function-definition here as well.  If the declarator is a
14327    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14328    be TRUE upon return.  By that point, the function-definition will
14329    have been completely parsed.
14330
14331    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14332    is FALSE.
14333
14334    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14335    parsed declaration if it is an uninitialized single declarator not followed
14336    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14337    if present, will not be consumed.  If returned, this declarator will be
14338    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14339
14340 static tree
14341 cp_parser_init_declarator (cp_parser* parser,
14342                            cp_decl_specifier_seq *decl_specifiers,
14343                            VEC (deferred_access_check,gc)* checks,
14344                            bool function_definition_allowed_p,
14345                            bool member_p,
14346                            int declares_class_or_enum,
14347                            bool* function_definition_p,
14348                            tree* maybe_range_for_decl)
14349 {
14350   cp_token *token = NULL, *asm_spec_start_token = NULL,
14351            *attributes_start_token = NULL;
14352   cp_declarator *declarator;
14353   tree prefix_attributes;
14354   tree attributes;
14355   tree asm_specification;
14356   tree initializer;
14357   tree decl = NULL_TREE;
14358   tree scope;
14359   int is_initialized;
14360   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14361      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14362      "(...)".  */
14363   enum cpp_ttype initialization_kind;
14364   bool is_direct_init = false;
14365   bool is_non_constant_init;
14366   int ctor_dtor_or_conv_p;
14367   bool friend_p;
14368   tree pushed_scope = NULL_TREE;
14369   bool range_for_decl_p = false;
14370
14371   /* Gather the attributes that were provided with the
14372      decl-specifiers.  */
14373   prefix_attributes = decl_specifiers->attributes;
14374
14375   /* Assume that this is not the declarator for a function
14376      definition.  */
14377   if (function_definition_p)
14378     *function_definition_p = false;
14379
14380   /* Defer access checks while parsing the declarator; we cannot know
14381      what names are accessible until we know what is being
14382      declared.  */
14383   resume_deferring_access_checks ();
14384
14385   /* Parse the declarator.  */
14386   token = cp_lexer_peek_token (parser->lexer);
14387   declarator
14388     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14389                             &ctor_dtor_or_conv_p,
14390                             /*parenthesized_p=*/NULL,
14391                             member_p);
14392   /* Gather up the deferred checks.  */
14393   stop_deferring_access_checks ();
14394
14395   /* If the DECLARATOR was erroneous, there's no need to go
14396      further.  */
14397   if (declarator == cp_error_declarator)
14398     return error_mark_node;
14399
14400   /* Check that the number of template-parameter-lists is OK.  */
14401   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14402                                                        token->location))
14403     return error_mark_node;
14404
14405   if (declares_class_or_enum & 2)
14406     cp_parser_check_for_definition_in_return_type (declarator,
14407                                                    decl_specifiers->type,
14408                                                    decl_specifiers->type_location);
14409
14410   /* Figure out what scope the entity declared by the DECLARATOR is
14411      located in.  `grokdeclarator' sometimes changes the scope, so
14412      we compute it now.  */
14413   scope = get_scope_of_declarator (declarator);
14414
14415   /* Perform any lookups in the declared type which were thought to be
14416      dependent, but are not in the scope of the declarator.  */
14417   decl_specifiers->type
14418     = maybe_update_decl_type (decl_specifiers->type, scope);
14419
14420   /* If we're allowing GNU extensions, look for an asm-specification
14421      and attributes.  */
14422   if (cp_parser_allow_gnu_extensions_p (parser))
14423     {
14424       /* Look for an asm-specification.  */
14425       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14426       asm_specification = cp_parser_asm_specification_opt (parser);
14427       /* And attributes.  */
14428       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14429       attributes = cp_parser_attributes_opt (parser);
14430     }
14431   else
14432     {
14433       asm_specification = NULL_TREE;
14434       attributes = NULL_TREE;
14435     }
14436
14437   /* Peek at the next token.  */
14438   token = cp_lexer_peek_token (parser->lexer);
14439   /* Check to see if the token indicates the start of a
14440      function-definition.  */
14441   if (function_declarator_p (declarator)
14442       && cp_parser_token_starts_function_definition_p (token))
14443     {
14444       if (!function_definition_allowed_p)
14445         {
14446           /* If a function-definition should not appear here, issue an
14447              error message.  */
14448           cp_parser_error (parser,
14449                            "a function-definition is not allowed here");
14450           return error_mark_node;
14451         }
14452       else
14453         {
14454           location_t func_brace_location
14455             = cp_lexer_peek_token (parser->lexer)->location;
14456
14457           /* Neither attributes nor an asm-specification are allowed
14458              on a function-definition.  */
14459           if (asm_specification)
14460             error_at (asm_spec_start_token->location,
14461                       "an asm-specification is not allowed "
14462                       "on a function-definition");
14463           if (attributes)
14464             error_at (attributes_start_token->location,
14465                       "attributes are not allowed on a function-definition");
14466           /* This is a function-definition.  */
14467           *function_definition_p = true;
14468
14469           /* Parse the function definition.  */
14470           if (member_p)
14471             decl = cp_parser_save_member_function_body (parser,
14472                                                         decl_specifiers,
14473                                                         declarator,
14474                                                         prefix_attributes);
14475           else
14476             decl
14477               = (cp_parser_function_definition_from_specifiers_and_declarator
14478                  (parser, decl_specifiers, prefix_attributes, declarator));
14479
14480           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14481             {
14482               /* This is where the prologue starts...  */
14483               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14484                 = func_brace_location;
14485             }
14486
14487           return decl;
14488         }
14489     }
14490
14491   /* [dcl.dcl]
14492
14493      Only in function declarations for constructors, destructors, and
14494      type conversions can the decl-specifier-seq be omitted.
14495
14496      We explicitly postpone this check past the point where we handle
14497      function-definitions because we tolerate function-definitions
14498      that are missing their return types in some modes.  */
14499   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14500     {
14501       cp_parser_error (parser,
14502                        "expected constructor, destructor, or type conversion");
14503       return error_mark_node;
14504     }
14505
14506   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14507   if (token->type == CPP_EQ
14508       || token->type == CPP_OPEN_PAREN
14509       || token->type == CPP_OPEN_BRACE)
14510     {
14511       is_initialized = SD_INITIALIZED;
14512       initialization_kind = token->type;
14513       if (maybe_range_for_decl)
14514         *maybe_range_for_decl = error_mark_node;
14515
14516       if (token->type == CPP_EQ
14517           && function_declarator_p (declarator))
14518         {
14519           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14520           if (t2->keyword == RID_DEFAULT)
14521             is_initialized = SD_DEFAULTED;
14522           else if (t2->keyword == RID_DELETE)
14523             is_initialized = SD_DELETED;
14524         }
14525     }
14526   else
14527     {
14528       /* If the init-declarator isn't initialized and isn't followed by a
14529          `,' or `;', it's not a valid init-declarator.  */
14530       if (token->type != CPP_COMMA
14531           && token->type != CPP_SEMICOLON)
14532         {
14533           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14534             range_for_decl_p = true;
14535           else
14536             {
14537               cp_parser_error (parser, "expected initializer");
14538               return error_mark_node;
14539             }
14540         }
14541       is_initialized = SD_UNINITIALIZED;
14542       initialization_kind = CPP_EOF;
14543     }
14544
14545   /* Because start_decl has side-effects, we should only call it if we
14546      know we're going ahead.  By this point, we know that we cannot
14547      possibly be looking at any other construct.  */
14548   cp_parser_commit_to_tentative_parse (parser);
14549
14550   /* If the decl specifiers were bad, issue an error now that we're
14551      sure this was intended to be a declarator.  Then continue
14552      declaring the variable(s), as int, to try to cut down on further
14553      errors.  */
14554   if (decl_specifiers->any_specifiers_p
14555       && decl_specifiers->type == error_mark_node)
14556     {
14557       cp_parser_error (parser, "invalid type in declaration");
14558       decl_specifiers->type = integer_type_node;
14559     }
14560
14561   /* Check to see whether or not this declaration is a friend.  */
14562   friend_p = cp_parser_friend_p (decl_specifiers);
14563
14564   /* Enter the newly declared entry in the symbol table.  If we're
14565      processing a declaration in a class-specifier, we wait until
14566      after processing the initializer.  */
14567   if (!member_p)
14568     {
14569       if (parser->in_unbraced_linkage_specification_p)
14570         decl_specifiers->storage_class = sc_extern;
14571       decl = start_decl (declarator, decl_specifiers,
14572                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14573                          attributes, prefix_attributes,
14574                          &pushed_scope);
14575       /* Adjust location of decl if declarator->id_loc is more appropriate:
14576          set, and decl wasn't merged with another decl, in which case its
14577          location would be different from input_location, and more accurate.  */
14578       if (DECL_P (decl)
14579           && declarator->id_loc != UNKNOWN_LOCATION
14580           && DECL_SOURCE_LOCATION (decl) == input_location)
14581         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14582     }
14583   else if (scope)
14584     /* Enter the SCOPE.  That way unqualified names appearing in the
14585        initializer will be looked up in SCOPE.  */
14586     pushed_scope = push_scope (scope);
14587
14588   /* Perform deferred access control checks, now that we know in which
14589      SCOPE the declared entity resides.  */
14590   if (!member_p && decl)
14591     {
14592       tree saved_current_function_decl = NULL_TREE;
14593
14594       /* If the entity being declared is a function, pretend that we
14595          are in its scope.  If it is a `friend', it may have access to
14596          things that would not otherwise be accessible.  */
14597       if (TREE_CODE (decl) == FUNCTION_DECL)
14598         {
14599           saved_current_function_decl = current_function_decl;
14600           current_function_decl = decl;
14601         }
14602
14603       /* Perform access checks for template parameters.  */
14604       cp_parser_perform_template_parameter_access_checks (checks);
14605
14606       /* Perform the access control checks for the declarator and the
14607          decl-specifiers.  */
14608       perform_deferred_access_checks ();
14609
14610       /* Restore the saved value.  */
14611       if (TREE_CODE (decl) == FUNCTION_DECL)
14612         current_function_decl = saved_current_function_decl;
14613     }
14614
14615   /* Parse the initializer.  */
14616   initializer = NULL_TREE;
14617   is_direct_init = false;
14618   is_non_constant_init = true;
14619   if (is_initialized)
14620     {
14621       if (function_declarator_p (declarator))
14622         {
14623           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14624            if (initialization_kind == CPP_EQ)
14625              initializer = cp_parser_pure_specifier (parser);
14626            else
14627              {
14628                /* If the declaration was erroneous, we don't really
14629                   know what the user intended, so just silently
14630                   consume the initializer.  */
14631                if (decl != error_mark_node)
14632                  error_at (initializer_start_token->location,
14633                            "initializer provided for function");
14634                cp_parser_skip_to_closing_parenthesis (parser,
14635                                                       /*recovering=*/true,
14636                                                       /*or_comma=*/false,
14637                                                       /*consume_paren=*/true);
14638              }
14639         }
14640       else
14641         {
14642           /* We want to record the extra mangling scope for in-class
14643              initializers of class members and initializers of static data
14644              member templates.  The former is a C++0x feature which isn't
14645              implemented yet, and I expect it will involve deferring
14646              parsing of the initializer until end of class as with default
14647              arguments.  So right here we only handle the latter.  */
14648           if (!member_p && processing_template_decl)
14649             start_lambda_scope (decl);
14650           initializer = cp_parser_initializer (parser,
14651                                                &is_direct_init,
14652                                                &is_non_constant_init);
14653           if (!member_p && processing_template_decl)
14654             finish_lambda_scope ();
14655         }
14656     }
14657
14658   /* The old parser allows attributes to appear after a parenthesized
14659      initializer.  Mark Mitchell proposed removing this functionality
14660      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14661      attributes -- but ignores them.  */
14662   if (cp_parser_allow_gnu_extensions_p (parser)
14663       && initialization_kind == CPP_OPEN_PAREN)
14664     if (cp_parser_attributes_opt (parser))
14665       warning (OPT_Wattributes,
14666                "attributes after parenthesized initializer ignored");
14667
14668   /* For an in-class declaration, use `grokfield' to create the
14669      declaration.  */
14670   if (member_p)
14671     {
14672       if (pushed_scope)
14673         {
14674           pop_scope (pushed_scope);
14675           pushed_scope = NULL_TREE;
14676         }
14677       decl = grokfield (declarator, decl_specifiers,
14678                         initializer, !is_non_constant_init,
14679                         /*asmspec=*/NULL_TREE,
14680                         prefix_attributes);
14681       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14682         cp_parser_save_default_args (parser, decl);
14683     }
14684
14685   /* Finish processing the declaration.  But, skip member
14686      declarations.  */
14687   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14688     {
14689       cp_finish_decl (decl,
14690                       initializer, !is_non_constant_init,
14691                       asm_specification,
14692                       /* If the initializer is in parentheses, then this is
14693                          a direct-initialization, which means that an
14694                          `explicit' constructor is OK.  Otherwise, an
14695                          `explicit' constructor cannot be used.  */
14696                       ((is_direct_init || !is_initialized)
14697                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14698     }
14699   else if ((cxx_dialect != cxx98) && friend_p
14700            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14701     /* Core issue #226 (C++0x only): A default template-argument
14702        shall not be specified in a friend class template
14703        declaration. */
14704     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14705                              /*is_partial=*/0, /*is_friend_decl=*/1);
14706
14707   if (!friend_p && pushed_scope)
14708     pop_scope (pushed_scope);
14709
14710   return decl;
14711 }
14712
14713 /* Parse a declarator.
14714
14715    declarator:
14716      direct-declarator
14717      ptr-operator declarator
14718
14719    abstract-declarator:
14720      ptr-operator abstract-declarator [opt]
14721      direct-abstract-declarator
14722
14723    GNU Extensions:
14724
14725    declarator:
14726      attributes [opt] direct-declarator
14727      attributes [opt] ptr-operator declarator
14728
14729    abstract-declarator:
14730      attributes [opt] ptr-operator abstract-declarator [opt]
14731      attributes [opt] direct-abstract-declarator
14732
14733    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14734    detect constructor, destructor or conversion operators. It is set
14735    to -1 if the declarator is a name, and +1 if it is a
14736    function. Otherwise it is set to zero. Usually you just want to
14737    test for >0, but internally the negative value is used.
14738
14739    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14740    a decl-specifier-seq unless it declares a constructor, destructor,
14741    or conversion.  It might seem that we could check this condition in
14742    semantic analysis, rather than parsing, but that makes it difficult
14743    to handle something like `f()'.  We want to notice that there are
14744    no decl-specifiers, and therefore realize that this is an
14745    expression, not a declaration.)
14746
14747    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14748    the declarator is a direct-declarator of the form "(...)".
14749
14750    MEMBER_P is true iff this declarator is a member-declarator.  */
14751
14752 static cp_declarator *
14753 cp_parser_declarator (cp_parser* parser,
14754                       cp_parser_declarator_kind dcl_kind,
14755                       int* ctor_dtor_or_conv_p,
14756                       bool* parenthesized_p,
14757                       bool member_p)
14758 {
14759   cp_declarator *declarator;
14760   enum tree_code code;
14761   cp_cv_quals cv_quals;
14762   tree class_type;
14763   tree attributes = NULL_TREE;
14764
14765   /* Assume this is not a constructor, destructor, or type-conversion
14766      operator.  */
14767   if (ctor_dtor_or_conv_p)
14768     *ctor_dtor_or_conv_p = 0;
14769
14770   if (cp_parser_allow_gnu_extensions_p (parser))
14771     attributes = cp_parser_attributes_opt (parser);
14772
14773   /* Check for the ptr-operator production.  */
14774   cp_parser_parse_tentatively (parser);
14775   /* Parse the ptr-operator.  */
14776   code = cp_parser_ptr_operator (parser,
14777                                  &class_type,
14778                                  &cv_quals);
14779   /* If that worked, then we have a ptr-operator.  */
14780   if (cp_parser_parse_definitely (parser))
14781     {
14782       /* If a ptr-operator was found, then this declarator was not
14783          parenthesized.  */
14784       if (parenthesized_p)
14785         *parenthesized_p = true;
14786       /* The dependent declarator is optional if we are parsing an
14787          abstract-declarator.  */
14788       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14789         cp_parser_parse_tentatively (parser);
14790
14791       /* Parse the dependent declarator.  */
14792       declarator = cp_parser_declarator (parser, dcl_kind,
14793                                          /*ctor_dtor_or_conv_p=*/NULL,
14794                                          /*parenthesized_p=*/NULL,
14795                                          /*member_p=*/false);
14796
14797       /* If we are parsing an abstract-declarator, we must handle the
14798          case where the dependent declarator is absent.  */
14799       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14800           && !cp_parser_parse_definitely (parser))
14801         declarator = NULL;
14802
14803       declarator = cp_parser_make_indirect_declarator
14804         (code, class_type, cv_quals, declarator);
14805     }
14806   /* Everything else is a direct-declarator.  */
14807   else
14808     {
14809       if (parenthesized_p)
14810         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14811                                                    CPP_OPEN_PAREN);
14812       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14813                                                 ctor_dtor_or_conv_p,
14814                                                 member_p);
14815     }
14816
14817   if (attributes && declarator && declarator != cp_error_declarator)
14818     declarator->attributes = attributes;
14819
14820   return declarator;
14821 }
14822
14823 /* Parse a direct-declarator or direct-abstract-declarator.
14824
14825    direct-declarator:
14826      declarator-id
14827      direct-declarator ( parameter-declaration-clause )
14828        cv-qualifier-seq [opt]
14829        exception-specification [opt]
14830      direct-declarator [ constant-expression [opt] ]
14831      ( declarator )
14832
14833    direct-abstract-declarator:
14834      direct-abstract-declarator [opt]
14835        ( parameter-declaration-clause )
14836        cv-qualifier-seq [opt]
14837        exception-specification [opt]
14838      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14839      ( abstract-declarator )
14840
14841    Returns a representation of the declarator.  DCL_KIND is
14842    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14843    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14844    we are parsing a direct-declarator.  It is
14845    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14846    of ambiguity we prefer an abstract declarator, as per
14847    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14848    cp_parser_declarator.  */
14849
14850 static cp_declarator *
14851 cp_parser_direct_declarator (cp_parser* parser,
14852                              cp_parser_declarator_kind dcl_kind,
14853                              int* ctor_dtor_or_conv_p,
14854                              bool member_p)
14855 {
14856   cp_token *token;
14857   cp_declarator *declarator = NULL;
14858   tree scope = NULL_TREE;
14859   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14860   bool saved_in_declarator_p = parser->in_declarator_p;
14861   bool first = true;
14862   tree pushed_scope = NULL_TREE;
14863
14864   while (true)
14865     {
14866       /* Peek at the next token.  */
14867       token = cp_lexer_peek_token (parser->lexer);
14868       if (token->type == CPP_OPEN_PAREN)
14869         {
14870           /* This is either a parameter-declaration-clause, or a
14871              parenthesized declarator. When we know we are parsing a
14872              named declarator, it must be a parenthesized declarator
14873              if FIRST is true. For instance, `(int)' is a
14874              parameter-declaration-clause, with an omitted
14875              direct-abstract-declarator. But `((*))', is a
14876              parenthesized abstract declarator. Finally, when T is a
14877              template parameter `(T)' is a
14878              parameter-declaration-clause, and not a parenthesized
14879              named declarator.
14880
14881              We first try and parse a parameter-declaration-clause,
14882              and then try a nested declarator (if FIRST is true).
14883
14884              It is not an error for it not to be a
14885              parameter-declaration-clause, even when FIRST is
14886              false. Consider,
14887
14888                int i (int);
14889                int i (3);
14890
14891              The first is the declaration of a function while the
14892              second is the definition of a variable, including its
14893              initializer.
14894
14895              Having seen only the parenthesis, we cannot know which of
14896              these two alternatives should be selected.  Even more
14897              complex are examples like:
14898
14899                int i (int (a));
14900                int i (int (3));
14901
14902              The former is a function-declaration; the latter is a
14903              variable initialization.
14904
14905              Thus again, we try a parameter-declaration-clause, and if
14906              that fails, we back out and return.  */
14907
14908           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14909             {
14910               tree params;
14911               unsigned saved_num_template_parameter_lists;
14912               bool is_declarator = false;
14913               tree t;
14914
14915               /* In a member-declarator, the only valid interpretation
14916                  of a parenthesis is the start of a
14917                  parameter-declaration-clause.  (It is invalid to
14918                  initialize a static data member with a parenthesized
14919                  initializer; only the "=" form of initialization is
14920                  permitted.)  */
14921               if (!member_p)
14922                 cp_parser_parse_tentatively (parser);
14923
14924               /* Consume the `('.  */
14925               cp_lexer_consume_token (parser->lexer);
14926               if (first)
14927                 {
14928                   /* If this is going to be an abstract declarator, we're
14929                      in a declarator and we can't have default args.  */
14930                   parser->default_arg_ok_p = false;
14931                   parser->in_declarator_p = true;
14932                 }
14933
14934               /* Inside the function parameter list, surrounding
14935                  template-parameter-lists do not apply.  */
14936               saved_num_template_parameter_lists
14937                 = parser->num_template_parameter_lists;
14938               parser->num_template_parameter_lists = 0;
14939
14940               begin_scope (sk_function_parms, NULL_TREE);
14941
14942               /* Parse the parameter-declaration-clause.  */
14943               params = cp_parser_parameter_declaration_clause (parser);
14944
14945               parser->num_template_parameter_lists
14946                 = saved_num_template_parameter_lists;
14947
14948               /* Consume the `)'.  */
14949               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14950
14951               /* If all went well, parse the cv-qualifier-seq and the
14952                  exception-specification.  */
14953               if (member_p || cp_parser_parse_definitely (parser))
14954                 {
14955                   cp_cv_quals cv_quals;
14956                   cp_virt_specifiers virt_specifiers;
14957                   tree exception_specification;
14958                   tree late_return;
14959
14960                   is_declarator = true;
14961
14962                   if (ctor_dtor_or_conv_p)
14963                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14964                   first = false;
14965
14966                   /* Parse the cv-qualifier-seq.  */
14967                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14968                   /* And the exception-specification.  */
14969                   exception_specification
14970                     = cp_parser_exception_specification_opt (parser);
14971                   /* Parse the virt-specifier-seq.  */
14972                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14973
14974                   late_return = (cp_parser_late_return_type_opt
14975                                  (parser, member_p ? cv_quals : -1));
14976
14977                   /* Create the function-declarator.  */
14978                   declarator = make_call_declarator (declarator,
14979                                                      params,
14980                                                      cv_quals,
14981                                                      virt_specifiers,
14982                                                      exception_specification,
14983                                                      late_return);
14984                   /* Any subsequent parameter lists are to do with
14985                      return type, so are not those of the declared
14986                      function.  */
14987                   parser->default_arg_ok_p = false;
14988                 }
14989
14990               /* Remove the function parms from scope.  */
14991               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14992                 pop_binding (DECL_NAME (t), t);
14993               leave_scope();
14994
14995               if (is_declarator)
14996                 /* Repeat the main loop.  */
14997                 continue;
14998             }
14999
15000           /* If this is the first, we can try a parenthesized
15001              declarator.  */
15002           if (first)
15003             {
15004               bool saved_in_type_id_in_expr_p;
15005
15006               parser->default_arg_ok_p = saved_default_arg_ok_p;
15007               parser->in_declarator_p = saved_in_declarator_p;
15008
15009               /* Consume the `('.  */
15010               cp_lexer_consume_token (parser->lexer);
15011               /* Parse the nested declarator.  */
15012               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15013               parser->in_type_id_in_expr_p = true;
15014               declarator
15015                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15016                                         /*parenthesized_p=*/NULL,
15017                                         member_p);
15018               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15019               first = false;
15020               /* Expect a `)'.  */
15021               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15022                 declarator = cp_error_declarator;
15023               if (declarator == cp_error_declarator)
15024                 break;
15025
15026               goto handle_declarator;
15027             }
15028           /* Otherwise, we must be done.  */
15029           else
15030             break;
15031         }
15032       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15033                && token->type == CPP_OPEN_SQUARE)
15034         {
15035           /* Parse an array-declarator.  */
15036           tree bounds;
15037
15038           if (ctor_dtor_or_conv_p)
15039             *ctor_dtor_or_conv_p = 0;
15040
15041           first = false;
15042           parser->default_arg_ok_p = false;
15043           parser->in_declarator_p = true;
15044           /* Consume the `['.  */
15045           cp_lexer_consume_token (parser->lexer);
15046           /* Peek at the next token.  */
15047           token = cp_lexer_peek_token (parser->lexer);
15048           /* If the next token is `]', then there is no
15049              constant-expression.  */
15050           if (token->type != CPP_CLOSE_SQUARE)
15051             {
15052               bool non_constant_p;
15053
15054               bounds
15055                 = cp_parser_constant_expression (parser,
15056                                                  /*allow_non_constant=*/true,
15057                                                  &non_constant_p);
15058               if (!non_constant_p)
15059                 /* OK */;
15060               /* Normally, the array bound must be an integral constant
15061                  expression.  However, as an extension, we allow VLAs
15062                  in function scopes as long as they aren't part of a
15063                  parameter declaration.  */
15064               else if (!parser->in_function_body
15065                        || current_binding_level->kind == sk_function_parms)
15066                 {
15067                   cp_parser_error (parser,
15068                                    "array bound is not an integer constant");
15069                   bounds = error_mark_node;
15070                 }
15071               else if (processing_template_decl && !error_operand_p (bounds))
15072                 {
15073                   /* Remember this wasn't a constant-expression.  */
15074                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15075                   TREE_SIDE_EFFECTS (bounds) = 1;
15076                 }
15077             }
15078           else
15079             bounds = NULL_TREE;
15080           /* Look for the closing `]'.  */
15081           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15082             {
15083               declarator = cp_error_declarator;
15084               break;
15085             }
15086
15087           declarator = make_array_declarator (declarator, bounds);
15088         }
15089       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15090         {
15091           {
15092             tree qualifying_scope;
15093             tree unqualified_name;
15094             special_function_kind sfk;
15095             bool abstract_ok;
15096             bool pack_expansion_p = false;
15097             cp_token *declarator_id_start_token;
15098
15099             /* Parse a declarator-id */
15100             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15101             if (abstract_ok)
15102               {
15103                 cp_parser_parse_tentatively (parser);
15104
15105                 /* If we see an ellipsis, we should be looking at a
15106                    parameter pack. */
15107                 if (token->type == CPP_ELLIPSIS)
15108                   {
15109                     /* Consume the `...' */
15110                     cp_lexer_consume_token (parser->lexer);
15111
15112                     pack_expansion_p = true;
15113                   }
15114               }
15115
15116             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15117             unqualified_name
15118               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15119             qualifying_scope = parser->scope;
15120             if (abstract_ok)
15121               {
15122                 bool okay = false;
15123
15124                 if (!unqualified_name && pack_expansion_p)
15125                   {
15126                     /* Check whether an error occurred. */
15127                     okay = !cp_parser_error_occurred (parser);
15128
15129                     /* We already consumed the ellipsis to mark a
15130                        parameter pack, but we have no way to report it,
15131                        so abort the tentative parse. We will be exiting
15132                        immediately anyway. */
15133                     cp_parser_abort_tentative_parse (parser);
15134                   }
15135                 else
15136                   okay = cp_parser_parse_definitely (parser);
15137
15138                 if (!okay)
15139                   unqualified_name = error_mark_node;
15140                 else if (unqualified_name
15141                          && (qualifying_scope
15142                              || (TREE_CODE (unqualified_name)
15143                                  != IDENTIFIER_NODE)))
15144                   {
15145                     cp_parser_error (parser, "expected unqualified-id");
15146                     unqualified_name = error_mark_node;
15147                   }
15148               }
15149
15150             if (!unqualified_name)
15151               return NULL;
15152             if (unqualified_name == error_mark_node)
15153               {
15154                 declarator = cp_error_declarator;
15155                 pack_expansion_p = false;
15156                 declarator->parameter_pack_p = false;
15157                 break;
15158               }
15159
15160             if (qualifying_scope && at_namespace_scope_p ()
15161                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15162               {
15163                 /* In the declaration of a member of a template class
15164                    outside of the class itself, the SCOPE will sometimes
15165                    be a TYPENAME_TYPE.  For example, given:
15166
15167                    template <typename T>
15168                    int S<T>::R::i = 3;
15169
15170                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15171                    this context, we must resolve S<T>::R to an ordinary
15172                    type, rather than a typename type.
15173
15174                    The reason we normally avoid resolving TYPENAME_TYPEs
15175                    is that a specialization of `S' might render
15176                    `S<T>::R' not a type.  However, if `S' is
15177                    specialized, then this `i' will not be used, so there
15178                    is no harm in resolving the types here.  */
15179                 tree type;
15180
15181                 /* Resolve the TYPENAME_TYPE.  */
15182                 type = resolve_typename_type (qualifying_scope,
15183                                               /*only_current_p=*/false);
15184                 /* If that failed, the declarator is invalid.  */
15185                 if (TREE_CODE (type) == TYPENAME_TYPE)
15186                   {
15187                     if (typedef_variant_p (type))
15188                       error_at (declarator_id_start_token->location,
15189                                 "cannot define member of dependent typedef "
15190                                 "%qT", type);
15191                     else
15192                       error_at (declarator_id_start_token->location,
15193                                 "%<%T::%E%> is not a type",
15194                                 TYPE_CONTEXT (qualifying_scope),
15195                                 TYPE_IDENTIFIER (qualifying_scope));
15196                   }
15197                 qualifying_scope = type;
15198               }
15199
15200             sfk = sfk_none;
15201
15202             if (unqualified_name)
15203               {
15204                 tree class_type;
15205
15206                 if (qualifying_scope
15207                     && CLASS_TYPE_P (qualifying_scope))
15208                   class_type = qualifying_scope;
15209                 else
15210                   class_type = current_class_type;
15211
15212                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15213                   {
15214                     tree name_type = TREE_TYPE (unqualified_name);
15215                     if (class_type && same_type_p (name_type, class_type))
15216                       {
15217                         if (qualifying_scope
15218                             && CLASSTYPE_USE_TEMPLATE (name_type))
15219                           {
15220                             error_at (declarator_id_start_token->location,
15221                                       "invalid use of constructor as a template");
15222                             inform (declarator_id_start_token->location,
15223                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15224                                     "name the constructor in a qualified name",
15225                                     class_type,
15226                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15227                                     class_type, name_type);
15228                             declarator = cp_error_declarator;
15229                             break;
15230                           }
15231                         else
15232                           unqualified_name = constructor_name (class_type);
15233                       }
15234                     else
15235                       {
15236                         /* We do not attempt to print the declarator
15237                            here because we do not have enough
15238                            information about its original syntactic
15239                            form.  */
15240                         cp_parser_error (parser, "invalid declarator");
15241                         declarator = cp_error_declarator;
15242                         break;
15243                       }
15244                   }
15245
15246                 if (class_type)
15247                   {
15248                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15249                       sfk = sfk_destructor;
15250                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15251                       sfk = sfk_conversion;
15252                     else if (/* There's no way to declare a constructor
15253                                 for an anonymous type, even if the type
15254                                 got a name for linkage purposes.  */
15255                              !TYPE_WAS_ANONYMOUS (class_type)
15256                              && constructor_name_p (unqualified_name,
15257                                                     class_type))
15258                       {
15259                         unqualified_name = constructor_name (class_type);
15260                         sfk = sfk_constructor;
15261                       }
15262                     else if (is_overloaded_fn (unqualified_name)
15263                              && DECL_CONSTRUCTOR_P (get_first_fn
15264                                                     (unqualified_name)))
15265                       sfk = sfk_constructor;
15266
15267                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15268                       *ctor_dtor_or_conv_p = -1;
15269                   }
15270               }
15271             declarator = make_id_declarator (qualifying_scope,
15272                                              unqualified_name,
15273                                              sfk);
15274             declarator->id_loc = token->location;
15275             declarator->parameter_pack_p = pack_expansion_p;
15276
15277             if (pack_expansion_p)
15278               maybe_warn_variadic_templates ();
15279           }
15280
15281         handle_declarator:;
15282           scope = get_scope_of_declarator (declarator);
15283           if (scope)
15284             /* Any names that appear after the declarator-id for a
15285                member are looked up in the containing scope.  */
15286             pushed_scope = push_scope (scope);
15287           parser->in_declarator_p = true;
15288           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15289               || (declarator && declarator->kind == cdk_id))
15290             /* Default args are only allowed on function
15291                declarations.  */
15292             parser->default_arg_ok_p = saved_default_arg_ok_p;
15293           else
15294             parser->default_arg_ok_p = false;
15295
15296           first = false;
15297         }
15298       /* We're done.  */
15299       else
15300         break;
15301     }
15302
15303   /* For an abstract declarator, we might wind up with nothing at this
15304      point.  That's an error; the declarator is not optional.  */
15305   if (!declarator)
15306     cp_parser_error (parser, "expected declarator");
15307
15308   /* If we entered a scope, we must exit it now.  */
15309   if (pushed_scope)
15310     pop_scope (pushed_scope);
15311
15312   parser->default_arg_ok_p = saved_default_arg_ok_p;
15313   parser->in_declarator_p = saved_in_declarator_p;
15314
15315   return declarator;
15316 }
15317
15318 /* Parse a ptr-operator.
15319
15320    ptr-operator:
15321      * cv-qualifier-seq [opt]
15322      &
15323      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15324
15325    GNU Extension:
15326
15327    ptr-operator:
15328      & cv-qualifier-seq [opt]
15329
15330    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15331    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15332    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15333    filled in with the TYPE containing the member.  *CV_QUALS is
15334    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15335    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15336    Note that the tree codes returned by this function have nothing
15337    to do with the types of trees that will be eventually be created
15338    to represent the pointer or reference type being parsed. They are
15339    just constants with suggestive names. */
15340 static enum tree_code
15341 cp_parser_ptr_operator (cp_parser* parser,
15342                         tree* type,
15343                         cp_cv_quals *cv_quals)
15344 {
15345   enum tree_code code = ERROR_MARK;
15346   cp_token *token;
15347
15348   /* Assume that it's not a pointer-to-member.  */
15349   *type = NULL_TREE;
15350   /* And that there are no cv-qualifiers.  */
15351   *cv_quals = TYPE_UNQUALIFIED;
15352
15353   /* Peek at the next token.  */
15354   token = cp_lexer_peek_token (parser->lexer);
15355
15356   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15357   if (token->type == CPP_MULT)
15358     code = INDIRECT_REF;
15359   else if (token->type == CPP_AND)
15360     code = ADDR_EXPR;
15361   else if ((cxx_dialect != cxx98) &&
15362            token->type == CPP_AND_AND) /* C++0x only */
15363     code = NON_LVALUE_EXPR;
15364
15365   if (code != ERROR_MARK)
15366     {
15367       /* Consume the `*', `&' or `&&'.  */
15368       cp_lexer_consume_token (parser->lexer);
15369
15370       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15371          `&', if we are allowing GNU extensions.  (The only qualifier
15372          that can legally appear after `&' is `restrict', but that is
15373          enforced during semantic analysis.  */
15374       if (code == INDIRECT_REF
15375           || cp_parser_allow_gnu_extensions_p (parser))
15376         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15377     }
15378   else
15379     {
15380       /* Try the pointer-to-member case.  */
15381       cp_parser_parse_tentatively (parser);
15382       /* Look for the optional `::' operator.  */
15383       cp_parser_global_scope_opt (parser,
15384                                   /*current_scope_valid_p=*/false);
15385       /* Look for the nested-name specifier.  */
15386       token = cp_lexer_peek_token (parser->lexer);
15387       cp_parser_nested_name_specifier (parser,
15388                                        /*typename_keyword_p=*/false,
15389                                        /*check_dependency_p=*/true,
15390                                        /*type_p=*/false,
15391                                        /*is_declaration=*/false);
15392       /* If we found it, and the next token is a `*', then we are
15393          indeed looking at a pointer-to-member operator.  */
15394       if (!cp_parser_error_occurred (parser)
15395           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15396         {
15397           /* Indicate that the `*' operator was used.  */
15398           code = INDIRECT_REF;
15399
15400           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15401             error_at (token->location, "%qD is a namespace", parser->scope);
15402           else
15403             {
15404               /* The type of which the member is a member is given by the
15405                  current SCOPE.  */
15406               *type = parser->scope;
15407               /* The next name will not be qualified.  */
15408               parser->scope = NULL_TREE;
15409               parser->qualifying_scope = NULL_TREE;
15410               parser->object_scope = NULL_TREE;
15411               /* Look for the optional cv-qualifier-seq.  */
15412               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15413             }
15414         }
15415       /* If that didn't work we don't have a ptr-operator.  */
15416       if (!cp_parser_parse_definitely (parser))
15417         cp_parser_error (parser, "expected ptr-operator");
15418     }
15419
15420   return code;
15421 }
15422
15423 /* Parse an (optional) cv-qualifier-seq.
15424
15425    cv-qualifier-seq:
15426      cv-qualifier cv-qualifier-seq [opt]
15427
15428    cv-qualifier:
15429      const
15430      volatile
15431
15432    GNU Extension:
15433
15434    cv-qualifier:
15435      __restrict__
15436
15437    Returns a bitmask representing the cv-qualifiers.  */
15438
15439 static cp_cv_quals
15440 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15441 {
15442   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15443
15444   while (true)
15445     {
15446       cp_token *token;
15447       cp_cv_quals cv_qualifier;
15448
15449       /* Peek at the next token.  */
15450       token = cp_lexer_peek_token (parser->lexer);
15451       /* See if it's a cv-qualifier.  */
15452       switch (token->keyword)
15453         {
15454         case RID_CONST:
15455           cv_qualifier = TYPE_QUAL_CONST;
15456           break;
15457
15458         case RID_VOLATILE:
15459           cv_qualifier = TYPE_QUAL_VOLATILE;
15460           break;
15461
15462         case RID_RESTRICT:
15463           cv_qualifier = TYPE_QUAL_RESTRICT;
15464           break;
15465
15466         default:
15467           cv_qualifier = TYPE_UNQUALIFIED;
15468           break;
15469         }
15470
15471       if (!cv_qualifier)
15472         break;
15473
15474       if (cv_quals & cv_qualifier)
15475         {
15476           error_at (token->location, "duplicate cv-qualifier");
15477           cp_lexer_purge_token (parser->lexer);
15478         }
15479       else
15480         {
15481           cp_lexer_consume_token (parser->lexer);
15482           cv_quals |= cv_qualifier;
15483         }
15484     }
15485
15486   return cv_quals;
15487 }
15488
15489 /* Parse an (optional) virt-specifier-seq.
15490
15491    virt-specifier-seq:
15492      virt-specifier virt-specifier-seq [opt]
15493
15494    virt-specifier:
15495      override
15496      final
15497
15498    Returns a bitmask representing the virt-specifiers.  */
15499
15500 static cp_virt_specifiers
15501 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15502 {
15503   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15504
15505   while (true)
15506     {
15507       cp_token *token;
15508       cp_virt_specifiers virt_specifier;
15509
15510       /* Peek at the next token.  */
15511       token = cp_lexer_peek_token (parser->lexer);
15512       /* See if it's a virt-specifier-qualifier.  */
15513       if (token->type != CPP_NAME)
15514         break;
15515       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15516         virt_specifier = VIRT_SPEC_OVERRIDE;
15517       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15518         virt_specifier = VIRT_SPEC_FINAL;
15519       else
15520         break;
15521
15522       if (virt_specifiers & virt_specifier)
15523         {
15524           error_at (token->location, "duplicate virt-specifier");
15525           cp_lexer_purge_token (parser->lexer);
15526         }
15527       else
15528         {
15529           cp_lexer_consume_token (parser->lexer);
15530           virt_specifiers |= virt_specifier;
15531         }
15532     }
15533   return virt_specifiers;
15534 }
15535
15536 /* Parse a late-specified return type, if any.  This is not a separate
15537    non-terminal, but part of a function declarator, which looks like
15538
15539    -> trailing-type-specifier-seq abstract-declarator(opt)
15540
15541    Returns the type indicated by the type-id.
15542
15543    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
15544    function.  */
15545
15546 static tree
15547 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
15548 {
15549   cp_token *token;
15550   tree type;
15551
15552   /* Peek at the next token.  */
15553   token = cp_lexer_peek_token (parser->lexer);
15554   /* A late-specified return type is indicated by an initial '->'. */
15555   if (token->type != CPP_DEREF)
15556     return NULL_TREE;
15557
15558   /* Consume the ->.  */
15559   cp_lexer_consume_token (parser->lexer);
15560
15561   if (quals >= 0)
15562     {
15563       /* DR 1207: 'this' is in scope in the trailing return type.  */
15564       tree this_parm = build_this_parm (current_class_type, quals);
15565       gcc_assert (current_class_ptr == NULL_TREE);
15566       current_class_ref
15567         = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
15568       /* Set this second to avoid shortcut in cp_build_indirect_ref.  */
15569       current_class_ptr = this_parm;
15570     }
15571
15572   type = cp_parser_trailing_type_id (parser);
15573
15574   if (current_class_type)
15575     current_class_ptr = current_class_ref = NULL_TREE;
15576
15577   return type;
15578 }
15579
15580 /* Parse a declarator-id.
15581
15582    declarator-id:
15583      id-expression
15584      :: [opt] nested-name-specifier [opt] type-name
15585
15586    In the `id-expression' case, the value returned is as for
15587    cp_parser_id_expression if the id-expression was an unqualified-id.
15588    If the id-expression was a qualified-id, then a SCOPE_REF is
15589    returned.  The first operand is the scope (either a NAMESPACE_DECL
15590    or TREE_TYPE), but the second is still just a representation of an
15591    unqualified-id.  */
15592
15593 static tree
15594 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15595 {
15596   tree id;
15597   /* The expression must be an id-expression.  Assume that qualified
15598      names are the names of types so that:
15599
15600        template <class T>
15601        int S<T>::R::i = 3;
15602
15603      will work; we must treat `S<T>::R' as the name of a type.
15604      Similarly, assume that qualified names are templates, where
15605      required, so that:
15606
15607        template <class T>
15608        int S<T>::R<T>::i = 3;
15609
15610      will work, too.  */
15611   id = cp_parser_id_expression (parser,
15612                                 /*template_keyword_p=*/false,
15613                                 /*check_dependency_p=*/false,
15614                                 /*template_p=*/NULL,
15615                                 /*declarator_p=*/true,
15616                                 optional_p);
15617   if (id && BASELINK_P (id))
15618     id = BASELINK_FUNCTIONS (id);
15619   return id;
15620 }
15621
15622 /* Parse a type-id.
15623
15624    type-id:
15625      type-specifier-seq abstract-declarator [opt]
15626
15627    Returns the TYPE specified.  */
15628
15629 static tree
15630 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15631                      bool is_trailing_return)
15632 {
15633   cp_decl_specifier_seq type_specifier_seq;
15634   cp_declarator *abstract_declarator;
15635
15636   /* Parse the type-specifier-seq.  */
15637   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15638                                 is_trailing_return,
15639                                 &type_specifier_seq);
15640   if (type_specifier_seq.type == error_mark_node)
15641     return error_mark_node;
15642
15643   /* There might or might not be an abstract declarator.  */
15644   cp_parser_parse_tentatively (parser);
15645   /* Look for the declarator.  */
15646   abstract_declarator
15647     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15648                             /*parenthesized_p=*/NULL,
15649                             /*member_p=*/false);
15650   /* Check to see if there really was a declarator.  */
15651   if (!cp_parser_parse_definitely (parser))
15652     abstract_declarator = NULL;
15653
15654   if (type_specifier_seq.type
15655       && type_uses_auto (type_specifier_seq.type))
15656     {
15657       /* A type-id with type 'auto' is only ok if the abstract declarator
15658          is a function declarator with a late-specified return type.  */
15659       if (abstract_declarator
15660           && abstract_declarator->kind == cdk_function
15661           && abstract_declarator->u.function.late_return_type)
15662         /* OK */;
15663       else
15664         {
15665           error ("invalid use of %<auto%>");
15666           return error_mark_node;
15667         }
15668     }
15669   
15670   return groktypename (&type_specifier_seq, abstract_declarator,
15671                        is_template_arg);
15672 }
15673
15674 static tree cp_parser_type_id (cp_parser *parser)
15675 {
15676   return cp_parser_type_id_1 (parser, false, false);
15677 }
15678
15679 static tree cp_parser_template_type_arg (cp_parser *parser)
15680 {
15681   tree r;
15682   const char *saved_message = parser->type_definition_forbidden_message;
15683   parser->type_definition_forbidden_message
15684     = G_("types may not be defined in template arguments");
15685   r = cp_parser_type_id_1 (parser, true, false);
15686   parser->type_definition_forbidden_message = saved_message;
15687   return r;
15688 }
15689
15690 static tree cp_parser_trailing_type_id (cp_parser *parser)
15691 {
15692   return cp_parser_type_id_1 (parser, false, true);
15693 }
15694
15695 /* Parse a type-specifier-seq.
15696
15697    type-specifier-seq:
15698      type-specifier type-specifier-seq [opt]
15699
15700    GNU extension:
15701
15702    type-specifier-seq:
15703      attributes type-specifier-seq [opt]
15704
15705    If IS_DECLARATION is true, we are at the start of a "condition" or
15706    exception-declaration, so we might be followed by a declarator-id.
15707
15708    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15709    i.e. we've just seen "->".
15710
15711    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15712
15713 static void
15714 cp_parser_type_specifier_seq (cp_parser* parser,
15715                               bool is_declaration,
15716                               bool is_trailing_return,
15717                               cp_decl_specifier_seq *type_specifier_seq)
15718 {
15719   bool seen_type_specifier = false;
15720   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15721   cp_token *start_token = NULL;
15722
15723   /* Clear the TYPE_SPECIFIER_SEQ.  */
15724   clear_decl_specs (type_specifier_seq);
15725
15726   /* In the context of a trailing return type, enum E { } is an
15727      elaborated-type-specifier followed by a function-body, not an
15728      enum-specifier.  */
15729   if (is_trailing_return)
15730     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15731
15732   /* Parse the type-specifiers and attributes.  */
15733   while (true)
15734     {
15735       tree type_specifier;
15736       bool is_cv_qualifier;
15737
15738       /* Check for attributes first.  */
15739       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15740         {
15741           type_specifier_seq->attributes =
15742             chainon (type_specifier_seq->attributes,
15743                      cp_parser_attributes_opt (parser));
15744           continue;
15745         }
15746
15747       /* record the token of the beginning of the type specifier seq,
15748          for error reporting purposes*/
15749      if (!start_token)
15750        start_token = cp_lexer_peek_token (parser->lexer);
15751
15752       /* Look for the type-specifier.  */
15753       type_specifier = cp_parser_type_specifier (parser,
15754                                                  flags,
15755                                                  type_specifier_seq,
15756                                                  /*is_declaration=*/false,
15757                                                  NULL,
15758                                                  &is_cv_qualifier);
15759       if (!type_specifier)
15760         {
15761           /* If the first type-specifier could not be found, this is not a
15762              type-specifier-seq at all.  */
15763           if (!seen_type_specifier)
15764             {
15765               cp_parser_error (parser, "expected type-specifier");
15766               type_specifier_seq->type = error_mark_node;
15767               return;
15768             }
15769           /* If subsequent type-specifiers could not be found, the
15770              type-specifier-seq is complete.  */
15771           break;
15772         }
15773
15774       seen_type_specifier = true;
15775       /* The standard says that a condition can be:
15776
15777             type-specifier-seq declarator = assignment-expression
15778
15779          However, given:
15780
15781            struct S {};
15782            if (int S = ...)
15783
15784          we should treat the "S" as a declarator, not as a
15785          type-specifier.  The standard doesn't say that explicitly for
15786          type-specifier-seq, but it does say that for
15787          decl-specifier-seq in an ordinary declaration.  Perhaps it
15788          would be clearer just to allow a decl-specifier-seq here, and
15789          then add a semantic restriction that if any decl-specifiers
15790          that are not type-specifiers appear, the program is invalid.  */
15791       if (is_declaration && !is_cv_qualifier)
15792         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15793     }
15794
15795   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15796 }
15797
15798 /* Parse a parameter-declaration-clause.
15799
15800    parameter-declaration-clause:
15801      parameter-declaration-list [opt] ... [opt]
15802      parameter-declaration-list , ...
15803
15804    Returns a representation for the parameter declarations.  A return
15805    value of NULL indicates a parameter-declaration-clause consisting
15806    only of an ellipsis.  */
15807
15808 static tree
15809 cp_parser_parameter_declaration_clause (cp_parser* parser)
15810 {
15811   tree parameters;
15812   cp_token *token;
15813   bool ellipsis_p;
15814   bool is_error;
15815
15816   /* Peek at the next token.  */
15817   token = cp_lexer_peek_token (parser->lexer);
15818   /* Check for trivial parameter-declaration-clauses.  */
15819   if (token->type == CPP_ELLIPSIS)
15820     {
15821       /* Consume the `...' token.  */
15822       cp_lexer_consume_token (parser->lexer);
15823       return NULL_TREE;
15824     }
15825   else if (token->type == CPP_CLOSE_PAREN)
15826     /* There are no parameters.  */
15827     {
15828 #ifndef NO_IMPLICIT_EXTERN_C
15829       if (in_system_header && current_class_type == NULL
15830           && current_lang_name == lang_name_c)
15831         return NULL_TREE;
15832       else
15833 #endif
15834         return void_list_node;
15835     }
15836   /* Check for `(void)', too, which is a special case.  */
15837   else if (token->keyword == RID_VOID
15838            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15839                == CPP_CLOSE_PAREN))
15840     {
15841       /* Consume the `void' token.  */
15842       cp_lexer_consume_token (parser->lexer);
15843       /* There are no parameters.  */
15844       return void_list_node;
15845     }
15846
15847   /* Parse the parameter-declaration-list.  */
15848   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15849   /* If a parse error occurred while parsing the
15850      parameter-declaration-list, then the entire
15851      parameter-declaration-clause is erroneous.  */
15852   if (is_error)
15853     return NULL;
15854
15855   /* Peek at the next token.  */
15856   token = cp_lexer_peek_token (parser->lexer);
15857   /* If it's a `,', the clause should terminate with an ellipsis.  */
15858   if (token->type == CPP_COMMA)
15859     {
15860       /* Consume the `,'.  */
15861       cp_lexer_consume_token (parser->lexer);
15862       /* Expect an ellipsis.  */
15863       ellipsis_p
15864         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15865     }
15866   /* It might also be `...' if the optional trailing `,' was
15867      omitted.  */
15868   else if (token->type == CPP_ELLIPSIS)
15869     {
15870       /* Consume the `...' token.  */
15871       cp_lexer_consume_token (parser->lexer);
15872       /* And remember that we saw it.  */
15873       ellipsis_p = true;
15874     }
15875   else
15876     ellipsis_p = false;
15877
15878   /* Finish the parameter list.  */
15879   if (!ellipsis_p)
15880     parameters = chainon (parameters, void_list_node);
15881
15882   return parameters;
15883 }
15884
15885 /* Parse a parameter-declaration-list.
15886
15887    parameter-declaration-list:
15888      parameter-declaration
15889      parameter-declaration-list , parameter-declaration
15890
15891    Returns a representation of the parameter-declaration-list, as for
15892    cp_parser_parameter_declaration_clause.  However, the
15893    `void_list_node' is never appended to the list.  Upon return,
15894    *IS_ERROR will be true iff an error occurred.  */
15895
15896 static tree
15897 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15898 {
15899   tree parameters = NULL_TREE;
15900   tree *tail = &parameters; 
15901   bool saved_in_unbraced_linkage_specification_p;
15902   int index = 0;
15903
15904   /* Assume all will go well.  */
15905   *is_error = false;
15906   /* The special considerations that apply to a function within an
15907      unbraced linkage specifications do not apply to the parameters
15908      to the function.  */
15909   saved_in_unbraced_linkage_specification_p 
15910     = parser->in_unbraced_linkage_specification_p;
15911   parser->in_unbraced_linkage_specification_p = false;
15912
15913   /* Look for more parameters.  */
15914   while (true)
15915     {
15916       cp_parameter_declarator *parameter;
15917       tree decl = error_mark_node;
15918       bool parenthesized_p;
15919       /* Parse the parameter.  */
15920       parameter
15921         = cp_parser_parameter_declaration (parser,
15922                                            /*template_parm_p=*/false,
15923                                            &parenthesized_p);
15924
15925       /* We don't know yet if the enclosing context is deprecated, so wait
15926          and warn in grokparms if appropriate.  */
15927       deprecated_state = DEPRECATED_SUPPRESS;
15928
15929       if (parameter)
15930         decl = grokdeclarator (parameter->declarator,
15931                                &parameter->decl_specifiers,
15932                                PARM,
15933                                parameter->default_argument != NULL_TREE,
15934                                &parameter->decl_specifiers.attributes);
15935
15936       deprecated_state = DEPRECATED_NORMAL;
15937
15938       /* If a parse error occurred parsing the parameter declaration,
15939          then the entire parameter-declaration-list is erroneous.  */
15940       if (decl == error_mark_node)
15941         {
15942           *is_error = true;
15943           parameters = error_mark_node;
15944           break;
15945         }
15946
15947       if (parameter->decl_specifiers.attributes)
15948         cplus_decl_attributes (&decl,
15949                                parameter->decl_specifiers.attributes,
15950                                0);
15951       if (DECL_NAME (decl))
15952         decl = pushdecl (decl);
15953
15954       if (decl != error_mark_node)
15955         {
15956           retrofit_lang_decl (decl);
15957           DECL_PARM_INDEX (decl) = ++index;
15958           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15959         }
15960
15961       /* Add the new parameter to the list.  */
15962       *tail = build_tree_list (parameter->default_argument, decl);
15963       tail = &TREE_CHAIN (*tail);
15964
15965       /* Peek at the next token.  */
15966       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15967           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15968           /* These are for Objective-C++ */
15969           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15970           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15971         /* The parameter-declaration-list is complete.  */
15972         break;
15973       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15974         {
15975           cp_token *token;
15976
15977           /* Peek at the next token.  */
15978           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15979           /* If it's an ellipsis, then the list is complete.  */
15980           if (token->type == CPP_ELLIPSIS)
15981             break;
15982           /* Otherwise, there must be more parameters.  Consume the
15983              `,'.  */
15984           cp_lexer_consume_token (parser->lexer);
15985           /* When parsing something like:
15986
15987                 int i(float f, double d)
15988
15989              we can tell after seeing the declaration for "f" that we
15990              are not looking at an initialization of a variable "i",
15991              but rather at the declaration of a function "i".
15992
15993              Due to the fact that the parsing of template arguments
15994              (as specified to a template-id) requires backtracking we
15995              cannot use this technique when inside a template argument
15996              list.  */
15997           if (!parser->in_template_argument_list_p
15998               && !parser->in_type_id_in_expr_p
15999               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16000               /* However, a parameter-declaration of the form
16001                  "foat(f)" (which is a valid declaration of a
16002                  parameter "f") can also be interpreted as an
16003                  expression (the conversion of "f" to "float").  */
16004               && !parenthesized_p)
16005             cp_parser_commit_to_tentative_parse (parser);
16006         }
16007       else
16008         {
16009           cp_parser_error (parser, "expected %<,%> or %<...%>");
16010           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16011             cp_parser_skip_to_closing_parenthesis (parser,
16012                                                    /*recovering=*/true,
16013                                                    /*or_comma=*/false,
16014                                                    /*consume_paren=*/false);
16015           break;
16016         }
16017     }
16018
16019   parser->in_unbraced_linkage_specification_p
16020     = saved_in_unbraced_linkage_specification_p;
16021
16022   return parameters;
16023 }
16024
16025 /* Parse a parameter declaration.
16026
16027    parameter-declaration:
16028      decl-specifier-seq ... [opt] declarator
16029      decl-specifier-seq declarator = assignment-expression
16030      decl-specifier-seq ... [opt] abstract-declarator [opt]
16031      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16032
16033    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16034    declares a template parameter.  (In that case, a non-nested `>'
16035    token encountered during the parsing of the assignment-expression
16036    is not interpreted as a greater-than operator.)
16037
16038    Returns a representation of the parameter, or NULL if an error
16039    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16040    true iff the declarator is of the form "(p)".  */
16041
16042 static cp_parameter_declarator *
16043 cp_parser_parameter_declaration (cp_parser *parser,
16044                                  bool template_parm_p,
16045                                  bool *parenthesized_p)
16046 {
16047   int declares_class_or_enum;
16048   cp_decl_specifier_seq decl_specifiers;
16049   cp_declarator *declarator;
16050   tree default_argument;
16051   cp_token *token = NULL, *declarator_token_start = NULL;
16052   const char *saved_message;
16053
16054   /* In a template parameter, `>' is not an operator.
16055
16056      [temp.param]
16057
16058      When parsing a default template-argument for a non-type
16059      template-parameter, the first non-nested `>' is taken as the end
16060      of the template parameter-list rather than a greater-than
16061      operator.  */
16062
16063   /* Type definitions may not appear in parameter types.  */
16064   saved_message = parser->type_definition_forbidden_message;
16065   parser->type_definition_forbidden_message
16066     = G_("types may not be defined in parameter types");
16067
16068   /* Parse the declaration-specifiers.  */
16069   cp_parser_decl_specifier_seq (parser,
16070                                 CP_PARSER_FLAGS_NONE,
16071                                 &decl_specifiers,
16072                                 &declares_class_or_enum);
16073
16074   /* Complain about missing 'typename' or other invalid type names.  */
16075   if (!decl_specifiers.any_type_specifiers_p)
16076     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16077
16078   /* If an error occurred, there's no reason to attempt to parse the
16079      rest of the declaration.  */
16080   if (cp_parser_error_occurred (parser))
16081     {
16082       parser->type_definition_forbidden_message = saved_message;
16083       return NULL;
16084     }
16085
16086   /* Peek at the next token.  */
16087   token = cp_lexer_peek_token (parser->lexer);
16088
16089   /* If the next token is a `)', `,', `=', `>', or `...', then there
16090      is no declarator. However, when variadic templates are enabled,
16091      there may be a declarator following `...'.  */
16092   if (token->type == CPP_CLOSE_PAREN
16093       || token->type == CPP_COMMA
16094       || token->type == CPP_EQ
16095       || token->type == CPP_GREATER)
16096     {
16097       declarator = NULL;
16098       if (parenthesized_p)
16099         *parenthesized_p = false;
16100     }
16101   /* Otherwise, there should be a declarator.  */
16102   else
16103     {
16104       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16105       parser->default_arg_ok_p = false;
16106
16107       /* After seeing a decl-specifier-seq, if the next token is not a
16108          "(", there is no possibility that the code is a valid
16109          expression.  Therefore, if parsing tentatively, we commit at
16110          this point.  */
16111       if (!parser->in_template_argument_list_p
16112           /* In an expression context, having seen:
16113
16114                (int((char ...
16115
16116              we cannot be sure whether we are looking at a
16117              function-type (taking a "char" as a parameter) or a cast
16118              of some object of type "char" to "int".  */
16119           && !parser->in_type_id_in_expr_p
16120           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16121           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16122           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16123         cp_parser_commit_to_tentative_parse (parser);
16124       /* Parse the declarator.  */
16125       declarator_token_start = token;
16126       declarator = cp_parser_declarator (parser,
16127                                          CP_PARSER_DECLARATOR_EITHER,
16128                                          /*ctor_dtor_or_conv_p=*/NULL,
16129                                          parenthesized_p,
16130                                          /*member_p=*/false);
16131       parser->default_arg_ok_p = saved_default_arg_ok_p;
16132       /* After the declarator, allow more attributes.  */
16133       decl_specifiers.attributes
16134         = chainon (decl_specifiers.attributes,
16135                    cp_parser_attributes_opt (parser));
16136     }
16137
16138   /* If the next token is an ellipsis, and we have not seen a
16139      declarator name, and the type of the declarator contains parameter
16140      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16141      a parameter pack expansion expression. Otherwise, leave the
16142      ellipsis for a C-style variadic function. */
16143   token = cp_lexer_peek_token (parser->lexer);
16144   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16145     {
16146       tree type = decl_specifiers.type;
16147
16148       if (type && DECL_P (type))
16149         type = TREE_TYPE (type);
16150
16151       if (type
16152           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16153           && declarator_can_be_parameter_pack (declarator)
16154           && (!declarator || !declarator->parameter_pack_p)
16155           && uses_parameter_packs (type))
16156         {
16157           /* Consume the `...'. */
16158           cp_lexer_consume_token (parser->lexer);
16159           maybe_warn_variadic_templates ();
16160           
16161           /* Build a pack expansion type */
16162           if (declarator)
16163             declarator->parameter_pack_p = true;
16164           else
16165             decl_specifiers.type = make_pack_expansion (type);
16166         }
16167     }
16168
16169   /* The restriction on defining new types applies only to the type
16170      of the parameter, not to the default argument.  */
16171   parser->type_definition_forbidden_message = saved_message;
16172
16173   /* If the next token is `=', then process a default argument.  */
16174   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16175     {
16176       /* Consume the `='.  */
16177       cp_lexer_consume_token (parser->lexer);
16178
16179       /* If we are defining a class, then the tokens that make up the
16180          default argument must be saved and processed later.  */
16181       if (!template_parm_p && at_class_scope_p ()
16182           && TYPE_BEING_DEFINED (current_class_type)
16183           && !LAMBDA_TYPE_P (current_class_type))
16184         {
16185           unsigned depth = 0;
16186           int maybe_template_id = 0;
16187           cp_token *first_token;
16188           cp_token *token;
16189
16190           /* Add tokens until we have processed the entire default
16191              argument.  We add the range [first_token, token).  */
16192           first_token = cp_lexer_peek_token (parser->lexer);
16193           while (true)
16194             {
16195               bool done = false;
16196
16197               /* Peek at the next token.  */
16198               token = cp_lexer_peek_token (parser->lexer);
16199               /* What we do depends on what token we have.  */
16200               switch (token->type)
16201                 {
16202                   /* In valid code, a default argument must be
16203                      immediately followed by a `,' `)', or `...'.  */
16204                 case CPP_COMMA:
16205                   if (depth == 0 && maybe_template_id)
16206                     {
16207                       /* If we've seen a '<', we might be in a
16208                          template-argument-list.  Until Core issue 325 is
16209                          resolved, we don't know how this situation ought
16210                          to be handled, so try to DTRT.  We check whether
16211                          what comes after the comma is a valid parameter
16212                          declaration list.  If it is, then the comma ends
16213                          the default argument; otherwise the default
16214                          argument continues.  */
16215                       bool error = false;
16216                       tree t;
16217
16218                       /* Set ITALP so cp_parser_parameter_declaration_list
16219                          doesn't decide to commit to this parse.  */
16220                       bool saved_italp = parser->in_template_argument_list_p;
16221                       parser->in_template_argument_list_p = true;
16222
16223                       cp_parser_parse_tentatively (parser);
16224                       cp_lexer_consume_token (parser->lexer);
16225                       begin_scope (sk_function_parms, NULL_TREE);
16226                       cp_parser_parameter_declaration_list (parser, &error);
16227                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16228                         pop_binding (DECL_NAME (t), t);
16229                       leave_scope ();
16230                       if (!cp_parser_error_occurred (parser) && !error)
16231                         done = true;
16232                       cp_parser_abort_tentative_parse (parser);
16233
16234                       parser->in_template_argument_list_p = saved_italp;
16235                       break;
16236                     }
16237                 case CPP_CLOSE_PAREN:
16238                 case CPP_ELLIPSIS:
16239                   /* If we run into a non-nested `;', `}', or `]',
16240                      then the code is invalid -- but the default
16241                      argument is certainly over.  */
16242                 case CPP_SEMICOLON:
16243                 case CPP_CLOSE_BRACE:
16244                 case CPP_CLOSE_SQUARE:
16245                   if (depth == 0)
16246                     done = true;
16247                   /* Update DEPTH, if necessary.  */
16248                   else if (token->type == CPP_CLOSE_PAREN
16249                            || token->type == CPP_CLOSE_BRACE
16250                            || token->type == CPP_CLOSE_SQUARE)
16251                     --depth;
16252                   break;
16253
16254                 case CPP_OPEN_PAREN:
16255                 case CPP_OPEN_SQUARE:
16256                 case CPP_OPEN_BRACE:
16257                   ++depth;
16258                   break;
16259
16260                 case CPP_LESS:
16261                   if (depth == 0)
16262                     /* This might be the comparison operator, or it might
16263                        start a template argument list.  */
16264                     ++maybe_template_id;
16265                   break;
16266
16267                 case CPP_RSHIFT:
16268                   if (cxx_dialect == cxx98)
16269                     break;
16270                   /* Fall through for C++0x, which treats the `>>'
16271                      operator like two `>' tokens in certain
16272                      cases.  */
16273
16274                 case CPP_GREATER:
16275                   if (depth == 0)
16276                     {
16277                       /* This might be an operator, or it might close a
16278                          template argument list.  But if a previous '<'
16279                          started a template argument list, this will have
16280                          closed it, so we can't be in one anymore.  */
16281                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16282                       if (maybe_template_id < 0)
16283                         maybe_template_id = 0;
16284                     }
16285                   break;
16286
16287                   /* If we run out of tokens, issue an error message.  */
16288                 case CPP_EOF:
16289                 case CPP_PRAGMA_EOL:
16290                   error_at (token->location, "file ends in default argument");
16291                   done = true;
16292                   break;
16293
16294                 case CPP_NAME:
16295                 case CPP_SCOPE:
16296                   /* In these cases, we should look for template-ids.
16297                      For example, if the default argument is
16298                      `X<int, double>()', we need to do name lookup to
16299                      figure out whether or not `X' is a template; if
16300                      so, the `,' does not end the default argument.
16301
16302                      That is not yet done.  */
16303                   break;
16304
16305                 default:
16306                   break;
16307                 }
16308
16309               /* If we've reached the end, stop.  */
16310               if (done)
16311                 break;
16312
16313               /* Add the token to the token block.  */
16314               token = cp_lexer_consume_token (parser->lexer);
16315             }
16316
16317           /* Create a DEFAULT_ARG to represent the unparsed default
16318              argument.  */
16319           default_argument = make_node (DEFAULT_ARG);
16320           DEFARG_TOKENS (default_argument)
16321             = cp_token_cache_new (first_token, token);
16322           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16323         }
16324       /* Outside of a class definition, we can just parse the
16325          assignment-expression.  */
16326       else
16327         {
16328           token = cp_lexer_peek_token (parser->lexer);
16329           default_argument 
16330             = cp_parser_default_argument (parser, template_parm_p);
16331         }
16332
16333       if (!parser->default_arg_ok_p)
16334         {
16335           if (flag_permissive)
16336             warning (0, "deprecated use of default argument for parameter of non-function");
16337           else
16338             {
16339               error_at (token->location,
16340                         "default arguments are only "
16341                         "permitted for function parameters");
16342               default_argument = NULL_TREE;
16343             }
16344         }
16345       else if ((declarator && declarator->parameter_pack_p)
16346                || (decl_specifiers.type
16347                    && PACK_EXPANSION_P (decl_specifiers.type)))
16348         {
16349           /* Find the name of the parameter pack.  */     
16350           cp_declarator *id_declarator = declarator;
16351           while (id_declarator && id_declarator->kind != cdk_id)
16352             id_declarator = id_declarator->declarator;
16353           
16354           if (id_declarator && id_declarator->kind == cdk_id)
16355             error_at (declarator_token_start->location,
16356                       template_parm_p 
16357                       ? "template parameter pack %qD"
16358                       " cannot have a default argument"
16359                       : "parameter pack %qD cannot have a default argument",
16360                       id_declarator->u.id.unqualified_name);
16361           else
16362             error_at (declarator_token_start->location,
16363                       template_parm_p 
16364                       ? "template parameter pack cannot have a default argument"
16365                       : "parameter pack cannot have a default argument");
16366           
16367           default_argument = NULL_TREE;
16368         }
16369     }
16370   else
16371     default_argument = NULL_TREE;
16372
16373   return make_parameter_declarator (&decl_specifiers,
16374                                     declarator,
16375                                     default_argument);
16376 }
16377
16378 /* Parse a default argument and return it.
16379
16380    TEMPLATE_PARM_P is true if this is a default argument for a
16381    non-type template parameter.  */
16382 static tree
16383 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16384 {
16385   tree default_argument = NULL_TREE;
16386   bool saved_greater_than_is_operator_p;
16387   bool saved_local_variables_forbidden_p;
16388
16389   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16390      set correctly.  */
16391   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16392   parser->greater_than_is_operator_p = !template_parm_p;
16393   /* Local variable names (and the `this' keyword) may not
16394      appear in a default argument.  */
16395   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16396   parser->local_variables_forbidden_p = true;
16397   /* Parse the assignment-expression.  */
16398   if (template_parm_p)
16399     push_deferring_access_checks (dk_no_deferred);
16400   default_argument
16401     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16402   if (template_parm_p)
16403     pop_deferring_access_checks ();
16404   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16405   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16406
16407   return default_argument;
16408 }
16409
16410 /* Parse a function-body.
16411
16412    function-body:
16413      compound_statement  */
16414
16415 static void
16416 cp_parser_function_body (cp_parser *parser)
16417 {
16418   cp_parser_compound_statement (parser, NULL, false, true);
16419 }
16420
16421 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16422    true if a ctor-initializer was present.  */
16423
16424 static bool
16425 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16426 {
16427   tree body, list;
16428   bool ctor_initializer_p;
16429   const bool check_body_p =
16430      DECL_CONSTRUCTOR_P (current_function_decl)
16431      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16432   tree last = NULL;
16433
16434   /* Begin the function body.  */
16435   body = begin_function_body ();
16436   /* Parse the optional ctor-initializer.  */
16437   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16438
16439   /* If we're parsing a constexpr constructor definition, we need
16440      to check that the constructor body is indeed empty.  However,
16441      before we get to cp_parser_function_body lot of junk has been
16442      generated, so we can't just check that we have an empty block.
16443      Rather we take a snapshot of the outermost block, and check whether
16444      cp_parser_function_body changed its state.  */
16445   if (check_body_p)
16446     {
16447       list = body;
16448       if (TREE_CODE (list) == BIND_EXPR)
16449         list = BIND_EXPR_BODY (list);
16450       if (TREE_CODE (list) == STATEMENT_LIST
16451           && STATEMENT_LIST_TAIL (list) != NULL)
16452         last = STATEMENT_LIST_TAIL (list)->stmt;
16453     }
16454   /* Parse the function-body.  */
16455   cp_parser_function_body (parser);
16456   if (check_body_p)
16457     check_constexpr_ctor_body (last, list);
16458   /* Finish the function body.  */
16459   finish_function_body (body);
16460
16461   return ctor_initializer_p;
16462 }
16463
16464 /* Parse an initializer.
16465
16466    initializer:
16467      = initializer-clause
16468      ( expression-list )
16469
16470    Returns an expression representing the initializer.  If no
16471    initializer is present, NULL_TREE is returned.
16472
16473    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16474    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16475    set to TRUE if there is no initializer present.  If there is an
16476    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16477    is set to true; otherwise it is set to false.  */
16478
16479 static tree
16480 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16481                        bool* non_constant_p)
16482 {
16483   cp_token *token;
16484   tree init;
16485
16486   /* Peek at the next token.  */
16487   token = cp_lexer_peek_token (parser->lexer);
16488
16489   /* Let our caller know whether or not this initializer was
16490      parenthesized.  */
16491   *is_direct_init = (token->type != CPP_EQ);
16492   /* Assume that the initializer is constant.  */
16493   *non_constant_p = false;
16494
16495   if (token->type == CPP_EQ)
16496     {
16497       /* Consume the `='.  */
16498       cp_lexer_consume_token (parser->lexer);
16499       /* Parse the initializer-clause.  */
16500       init = cp_parser_initializer_clause (parser, non_constant_p);
16501     }
16502   else if (token->type == CPP_OPEN_PAREN)
16503     {
16504       VEC(tree,gc) *vec;
16505       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16506                                                      /*cast_p=*/false,
16507                                                      /*allow_expansion_p=*/true,
16508                                                      non_constant_p);
16509       if (vec == NULL)
16510         return error_mark_node;
16511       init = build_tree_list_vec (vec);
16512       release_tree_vector (vec);
16513     }
16514   else if (token->type == CPP_OPEN_BRACE)
16515     {
16516       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16517       init = cp_parser_braced_list (parser, non_constant_p);
16518       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16519     }
16520   else
16521     {
16522       /* Anything else is an error.  */
16523       cp_parser_error (parser, "expected initializer");
16524       init = error_mark_node;
16525     }
16526
16527   return init;
16528 }
16529
16530 /* Parse an initializer-clause.
16531
16532    initializer-clause:
16533      assignment-expression
16534      braced-init-list
16535
16536    Returns an expression representing the initializer.
16537
16538    If the `assignment-expression' production is used the value
16539    returned is simply a representation for the expression.
16540
16541    Otherwise, calls cp_parser_braced_list.  */
16542
16543 static tree
16544 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16545 {
16546   tree initializer;
16547
16548   /* Assume the expression is constant.  */
16549   *non_constant_p = false;
16550
16551   /* If it is not a `{', then we are looking at an
16552      assignment-expression.  */
16553   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16554     {
16555       initializer
16556         = cp_parser_constant_expression (parser,
16557                                         /*allow_non_constant_p=*/true,
16558                                         non_constant_p);
16559     }
16560   else
16561     initializer = cp_parser_braced_list (parser, non_constant_p);
16562
16563   return initializer;
16564 }
16565
16566 /* Parse a brace-enclosed initializer list.
16567
16568    braced-init-list:
16569      { initializer-list , [opt] }
16570      { }
16571
16572    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16573    the elements of the initializer-list (or NULL, if the last
16574    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16575    NULL_TREE.  There is no way to detect whether or not the optional
16576    trailing `,' was provided.  NON_CONSTANT_P is as for
16577    cp_parser_initializer.  */     
16578
16579 static tree
16580 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16581 {
16582   tree initializer;
16583
16584   /* Consume the `{' token.  */
16585   cp_lexer_consume_token (parser->lexer);
16586   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16587   initializer = make_node (CONSTRUCTOR);
16588   /* If it's not a `}', then there is a non-trivial initializer.  */
16589   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16590     {
16591       /* Parse the initializer list.  */
16592       CONSTRUCTOR_ELTS (initializer)
16593         = cp_parser_initializer_list (parser, non_constant_p);
16594       /* A trailing `,' token is allowed.  */
16595       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16596         cp_lexer_consume_token (parser->lexer);
16597     }
16598   /* Now, there should be a trailing `}'.  */
16599   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16600   TREE_TYPE (initializer) = init_list_type_node;
16601   return initializer;
16602 }
16603
16604 /* Parse an initializer-list.
16605
16606    initializer-list:
16607      initializer-clause ... [opt]
16608      initializer-list , initializer-clause ... [opt]
16609
16610    GNU Extension:
16611
16612    initializer-list:
16613      identifier : initializer-clause
16614      initializer-list, identifier : initializer-clause
16615
16616    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16617    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16618    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16619    as for cp_parser_initializer.  */
16620
16621 static VEC(constructor_elt,gc) *
16622 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16623 {
16624   VEC(constructor_elt,gc) *v = NULL;
16625
16626   /* Assume all of the expressions are constant.  */
16627   *non_constant_p = false;
16628
16629   /* Parse the rest of the list.  */
16630   while (true)
16631     {
16632       cp_token *token;
16633       tree identifier;
16634       tree initializer;
16635       bool clause_non_constant_p;
16636
16637       /* If the next token is an identifier and the following one is a
16638          colon, we are looking at the GNU designated-initializer
16639          syntax.  */
16640       if (cp_parser_allow_gnu_extensions_p (parser)
16641           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16642           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16643         {
16644           /* Warn the user that they are using an extension.  */
16645           pedwarn (input_location, OPT_pedantic, 
16646                    "ISO C++ does not allow designated initializers");
16647           /* Consume the identifier.  */
16648           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16649           /* Consume the `:'.  */
16650           cp_lexer_consume_token (parser->lexer);
16651         }
16652       else
16653         identifier = NULL_TREE;
16654
16655       /* Parse the initializer.  */
16656       initializer = cp_parser_initializer_clause (parser,
16657                                                   &clause_non_constant_p);
16658       /* If any clause is non-constant, so is the entire initializer.  */
16659       if (clause_non_constant_p)
16660         *non_constant_p = true;
16661
16662       /* If we have an ellipsis, this is an initializer pack
16663          expansion.  */
16664       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16665         {
16666           /* Consume the `...'.  */
16667           cp_lexer_consume_token (parser->lexer);
16668
16669           /* Turn the initializer into an initializer expansion.  */
16670           initializer = make_pack_expansion (initializer);
16671         }
16672
16673       /* Add it to the vector.  */
16674       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16675
16676       /* If the next token is not a comma, we have reached the end of
16677          the list.  */
16678       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16679         break;
16680
16681       /* Peek at the next token.  */
16682       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16683       /* If the next token is a `}', then we're still done.  An
16684          initializer-clause can have a trailing `,' after the
16685          initializer-list and before the closing `}'.  */
16686       if (token->type == CPP_CLOSE_BRACE)
16687         break;
16688
16689       /* Consume the `,' token.  */
16690       cp_lexer_consume_token (parser->lexer);
16691     }
16692
16693   return v;
16694 }
16695
16696 /* Classes [gram.class] */
16697
16698 /* Parse a class-name.
16699
16700    class-name:
16701      identifier
16702      template-id
16703
16704    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16705    to indicate that names looked up in dependent types should be
16706    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16707    keyword has been used to indicate that the name that appears next
16708    is a template.  TAG_TYPE indicates the explicit tag given before
16709    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16710    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16711    is the class being defined in a class-head.
16712
16713    Returns the TYPE_DECL representing the class.  */
16714
16715 static tree
16716 cp_parser_class_name (cp_parser *parser,
16717                       bool typename_keyword_p,
16718                       bool template_keyword_p,
16719                       enum tag_types tag_type,
16720                       bool check_dependency_p,
16721                       bool class_head_p,
16722                       bool is_declaration)
16723 {
16724   tree decl;
16725   tree scope;
16726   bool typename_p;
16727   cp_token *token;
16728   tree identifier = NULL_TREE;
16729
16730   /* All class-names start with an identifier.  */
16731   token = cp_lexer_peek_token (parser->lexer);
16732   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16733     {
16734       cp_parser_error (parser, "expected class-name");
16735       return error_mark_node;
16736     }
16737
16738   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16739      to a template-id, so we save it here.  */
16740   scope = parser->scope;
16741   if (scope == error_mark_node)
16742     return error_mark_node;
16743
16744   /* Any name names a type if we're following the `typename' keyword
16745      in a qualified name where the enclosing scope is type-dependent.  */
16746   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16747                 && dependent_type_p (scope));
16748   /* Handle the common case (an identifier, but not a template-id)
16749      efficiently.  */
16750   if (token->type == CPP_NAME
16751       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16752     {
16753       cp_token *identifier_token;
16754       bool ambiguous_p;
16755
16756       /* Look for the identifier.  */
16757       identifier_token = cp_lexer_peek_token (parser->lexer);
16758       ambiguous_p = identifier_token->ambiguous_p;
16759       identifier = cp_parser_identifier (parser);
16760       /* If the next token isn't an identifier, we are certainly not
16761          looking at a class-name.  */
16762       if (identifier == error_mark_node)
16763         decl = error_mark_node;
16764       /* If we know this is a type-name, there's no need to look it
16765          up.  */
16766       else if (typename_p)
16767         decl = identifier;
16768       else
16769         {
16770           tree ambiguous_decls;
16771           /* If we already know that this lookup is ambiguous, then
16772              we've already issued an error message; there's no reason
16773              to check again.  */
16774           if (ambiguous_p)
16775             {
16776               cp_parser_simulate_error (parser);
16777               return error_mark_node;
16778             }
16779           /* If the next token is a `::', then the name must be a type
16780              name.
16781
16782              [basic.lookup.qual]
16783
16784              During the lookup for a name preceding the :: scope
16785              resolution operator, object, function, and enumerator
16786              names are ignored.  */
16787           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16788             tag_type = typename_type;
16789           /* Look up the name.  */
16790           decl = cp_parser_lookup_name (parser, identifier,
16791                                         tag_type,
16792                                         /*is_template=*/false,
16793                                         /*is_namespace=*/false,
16794                                         check_dependency_p,
16795                                         &ambiguous_decls,
16796                                         identifier_token->location);
16797           if (ambiguous_decls)
16798             {
16799               if (cp_parser_parsing_tentatively (parser))
16800                 cp_parser_simulate_error (parser);
16801               return error_mark_node;
16802             }
16803         }
16804     }
16805   else
16806     {
16807       /* Try a template-id.  */
16808       decl = cp_parser_template_id (parser, template_keyword_p,
16809                                     check_dependency_p,
16810                                     is_declaration);
16811       if (decl == error_mark_node)
16812         return error_mark_node;
16813     }
16814
16815   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16816
16817   /* If this is a typename, create a TYPENAME_TYPE.  */
16818   if (typename_p && decl != error_mark_node)
16819     {
16820       decl = make_typename_type (scope, decl, typename_type,
16821                                  /*complain=*/tf_error);
16822       if (decl != error_mark_node)
16823         decl = TYPE_NAME (decl);
16824     }
16825
16826   /* Check to see that it is really the name of a class.  */
16827   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16828       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16829       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16830     /* Situations like this:
16831
16832          template <typename T> struct A {
16833            typename T::template X<int>::I i;
16834          };
16835
16836        are problematic.  Is `T::template X<int>' a class-name?  The
16837        standard does not seem to be definitive, but there is no other
16838        valid interpretation of the following `::'.  Therefore, those
16839        names are considered class-names.  */
16840     {
16841       decl = make_typename_type (scope, decl, tag_type, tf_error);
16842       if (decl != error_mark_node)
16843         decl = TYPE_NAME (decl);
16844     }
16845   else if (TREE_CODE (decl) != TYPE_DECL
16846            || TREE_TYPE (decl) == error_mark_node
16847            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16848            /* In Objective-C 2.0, a classname followed by '.' starts a
16849               dot-syntax expression, and it's not a type-name.  */
16850            || (c_dialect_objc ()
16851                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16852                && objc_is_class_name (decl)))
16853     decl = error_mark_node;
16854
16855   if (decl == error_mark_node)
16856     cp_parser_error (parser, "expected class-name");
16857   else if (identifier && !parser->scope)
16858     maybe_note_name_used_in_class (identifier, decl);
16859
16860   return decl;
16861 }
16862
16863 /* Parse a class-specifier.
16864
16865    class-specifier:
16866      class-head { member-specification [opt] }
16867
16868    Returns the TREE_TYPE representing the class.  */
16869
16870 static tree
16871 cp_parser_class_specifier_1 (cp_parser* parser)
16872 {
16873   tree type;
16874   tree attributes = NULL_TREE;
16875   bool nested_name_specifier_p;
16876   unsigned saved_num_template_parameter_lists;
16877   bool saved_in_function_body;
16878   bool saved_in_unbraced_linkage_specification_p;
16879   tree old_scope = NULL_TREE;
16880   tree scope = NULL_TREE;
16881   tree bases;
16882   cp_token *closing_brace;
16883
16884   push_deferring_access_checks (dk_no_deferred);
16885
16886   /* Parse the class-head.  */
16887   type = cp_parser_class_head (parser,
16888                                &nested_name_specifier_p,
16889                                &attributes,
16890                                &bases);
16891   /* If the class-head was a semantic disaster, skip the entire body
16892      of the class.  */
16893   if (!type)
16894     {
16895       cp_parser_skip_to_end_of_block_or_statement (parser);
16896       pop_deferring_access_checks ();
16897       return error_mark_node;
16898     }
16899
16900   /* Look for the `{'.  */
16901   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16902     {
16903       pop_deferring_access_checks ();
16904       return error_mark_node;
16905     }
16906
16907   /* Process the base classes. If they're invalid, skip the 
16908      entire class body.  */
16909   if (!xref_basetypes (type, bases))
16910     {
16911       /* Consuming the closing brace yields better error messages
16912          later on.  */
16913       if (cp_parser_skip_to_closing_brace (parser))
16914         cp_lexer_consume_token (parser->lexer);
16915       pop_deferring_access_checks ();
16916       return error_mark_node;
16917     }
16918
16919   /* Issue an error message if type-definitions are forbidden here.  */
16920   cp_parser_check_type_definition (parser);
16921   /* Remember that we are defining one more class.  */
16922   ++parser->num_classes_being_defined;
16923   /* Inside the class, surrounding template-parameter-lists do not
16924      apply.  */
16925   saved_num_template_parameter_lists
16926     = parser->num_template_parameter_lists;
16927   parser->num_template_parameter_lists = 0;
16928   /* We are not in a function body.  */
16929   saved_in_function_body = parser->in_function_body;
16930   parser->in_function_body = false;
16931   /* We are not immediately inside an extern "lang" block.  */
16932   saved_in_unbraced_linkage_specification_p
16933     = parser->in_unbraced_linkage_specification_p;
16934   parser->in_unbraced_linkage_specification_p = false;
16935
16936   /* Start the class.  */
16937   if (nested_name_specifier_p)
16938     {
16939       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16940       old_scope = push_inner_scope (scope);
16941     }
16942   type = begin_class_definition (type, attributes);
16943
16944   if (type == error_mark_node)
16945     /* If the type is erroneous, skip the entire body of the class.  */
16946     cp_parser_skip_to_closing_brace (parser);
16947   else
16948     /* Parse the member-specification.  */
16949     cp_parser_member_specification_opt (parser);
16950
16951   /* Look for the trailing `}'.  */
16952   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16953   /* Look for trailing attributes to apply to this class.  */
16954   if (cp_parser_allow_gnu_extensions_p (parser))
16955     attributes = cp_parser_attributes_opt (parser);
16956   if (type != error_mark_node)
16957     type = finish_struct (type, attributes);
16958   if (nested_name_specifier_p)
16959     pop_inner_scope (old_scope, scope);
16960
16961   /* We've finished a type definition.  Check for the common syntax
16962      error of forgetting a semicolon after the definition.  We need to
16963      be careful, as we can't just check for not-a-semicolon and be done
16964      with it; the user might have typed:
16965
16966      class X { } c = ...;
16967      class X { } *p = ...;
16968
16969      and so forth.  Instead, enumerate all the possible tokens that
16970      might follow this production; if we don't see one of them, then
16971      complain and silently insert the semicolon.  */
16972   {
16973     cp_token *token = cp_lexer_peek_token (parser->lexer);
16974     bool want_semicolon = true;
16975
16976     switch (token->type)
16977       {
16978       case CPP_NAME:
16979       case CPP_SEMICOLON:
16980       case CPP_MULT:
16981       case CPP_AND:
16982       case CPP_OPEN_PAREN:
16983       case CPP_CLOSE_PAREN:
16984       case CPP_COMMA:
16985         want_semicolon = false;
16986         break;
16987
16988         /* While it's legal for type qualifiers and storage class
16989            specifiers to follow type definitions in the grammar, only
16990            compiler testsuites contain code like that.  Assume that if
16991            we see such code, then what we're really seeing is a case
16992            like:
16993
16994            class X { }
16995            const <type> var = ...;
16996
16997            or
16998
16999            class Y { }
17000            static <type> func (...) ...
17001
17002            i.e. the qualifier or specifier applies to the next
17003            declaration.  To do so, however, we need to look ahead one
17004            more token to see if *that* token is a type specifier.
17005
17006            This code could be improved to handle:
17007
17008            class Z { }
17009            static const <type> var = ...;  */
17010       case CPP_KEYWORD:
17011         if (keyword_is_decl_specifier (token->keyword))
17012           {
17013             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17014
17015             /* Handling user-defined types here would be nice, but very
17016                tricky.  */
17017             want_semicolon
17018               = (lookahead->type == CPP_KEYWORD
17019                  && keyword_begins_type_specifier (lookahead->keyword));
17020           }
17021         break;
17022       default:
17023         break;
17024       }
17025
17026     /* If we don't have a type, then something is very wrong and we
17027        shouldn't try to do anything clever.  Likewise for not seeing the
17028        closing brace.  */
17029     if (closing_brace && TYPE_P (type) && want_semicolon)
17030       {
17031         cp_token_position prev
17032           = cp_lexer_previous_token_position (parser->lexer);
17033         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17034         location_t loc = prev_token->location;
17035
17036         if (CLASSTYPE_DECLARED_CLASS (type))
17037           error_at (loc, "expected %<;%> after class definition");
17038         else if (TREE_CODE (type) == RECORD_TYPE)
17039           error_at (loc, "expected %<;%> after struct definition");
17040         else if (TREE_CODE (type) == UNION_TYPE)
17041           error_at (loc, "expected %<;%> after union definition");
17042         else
17043           gcc_unreachable ();
17044
17045         /* Unget one token and smash it to look as though we encountered
17046            a semicolon in the input stream.  */
17047         cp_lexer_set_token_position (parser->lexer, prev);
17048         token = cp_lexer_peek_token (parser->lexer);
17049         token->type = CPP_SEMICOLON;
17050         token->keyword = RID_MAX;
17051       }
17052   }
17053
17054   /* If this class is not itself within the scope of another class,
17055      then we need to parse the bodies of all of the queued function
17056      definitions.  Note that the queued functions defined in a class
17057      are not always processed immediately following the
17058      class-specifier for that class.  Consider:
17059
17060        struct A {
17061          struct B { void f() { sizeof (A); } };
17062        };
17063
17064      If `f' were processed before the processing of `A' were
17065      completed, there would be no way to compute the size of `A'.
17066      Note that the nesting we are interested in here is lexical --
17067      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17068      for:
17069
17070        struct A { struct B; };
17071        struct A::B { void f() { } };
17072
17073      there is no need to delay the parsing of `A::B::f'.  */
17074   if (--parser->num_classes_being_defined == 0)
17075     {
17076       tree fn;
17077       tree class_type = NULL_TREE;
17078       tree pushed_scope = NULL_TREE;
17079       unsigned ix;
17080       cp_default_arg_entry *e;
17081
17082       /* In a first pass, parse default arguments to the functions.
17083          Then, in a second pass, parse the bodies of the functions.
17084          This two-phased approach handles cases like:
17085
17086             struct S {
17087               void f() { g(); }
17088               void g(int i = 3);
17089             };
17090
17091          */
17092       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17093                         ix, e)
17094         {
17095           fn = e->decl;
17096           /* If there are default arguments that have not yet been processed,
17097              take care of them now.  */
17098           if (class_type != e->class_type)
17099             {
17100               if (pushed_scope)
17101                 pop_scope (pushed_scope);
17102               class_type = e->class_type;
17103               pushed_scope = push_scope (class_type);
17104             }
17105           /* Make sure that any template parameters are in scope.  */
17106           maybe_begin_member_template_processing (fn);
17107           /* Parse the default argument expressions.  */
17108           cp_parser_late_parsing_default_args (parser, fn);
17109           /* Remove any template parameters from the symbol table.  */
17110           maybe_end_member_template_processing ();
17111         }
17112       if (pushed_scope)
17113         pop_scope (pushed_scope);
17114       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17115       /* Now parse the body of the functions.  */
17116       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17117         cp_parser_late_parsing_for_member (parser, fn);
17118       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17119     }
17120
17121   /* Put back any saved access checks.  */
17122   pop_deferring_access_checks ();
17123
17124   /* Restore saved state.  */
17125   parser->in_function_body = saved_in_function_body;
17126   parser->num_template_parameter_lists
17127     = saved_num_template_parameter_lists;
17128   parser->in_unbraced_linkage_specification_p
17129     = saved_in_unbraced_linkage_specification_p;
17130
17131   return type;
17132 }
17133
17134 static tree
17135 cp_parser_class_specifier (cp_parser* parser)
17136 {
17137   tree ret;
17138   timevar_push (TV_PARSE_STRUCT);
17139   ret = cp_parser_class_specifier_1 (parser);
17140   timevar_pop (TV_PARSE_STRUCT);
17141   return ret;
17142 }
17143
17144 /* Parse a class-head.
17145
17146    class-head:
17147      class-key identifier [opt] base-clause [opt]
17148      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17149      class-key nested-name-specifier [opt] template-id
17150        base-clause [opt]
17151
17152    class-virt-specifier:
17153      final
17154
17155    GNU Extensions:
17156      class-key attributes identifier [opt] base-clause [opt]
17157      class-key attributes nested-name-specifier identifier base-clause [opt]
17158      class-key attributes nested-name-specifier [opt] template-id
17159        base-clause [opt]
17160
17161    Upon return BASES is initialized to the list of base classes (or
17162    NULL, if there are none) in the same form returned by
17163    cp_parser_base_clause.
17164
17165    Returns the TYPE of the indicated class.  Sets
17166    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17167    involving a nested-name-specifier was used, and FALSE otherwise.
17168
17169    Returns error_mark_node if this is not a class-head.
17170
17171    Returns NULL_TREE if the class-head is syntactically valid, but
17172    semantically invalid in a way that means we should skip the entire
17173    body of the class.  */
17174
17175 static tree
17176 cp_parser_class_head (cp_parser* parser,
17177                       bool* nested_name_specifier_p,
17178                       tree *attributes_p,
17179                       tree *bases)
17180 {
17181   tree nested_name_specifier;
17182   enum tag_types class_key;
17183   tree id = NULL_TREE;
17184   tree type = NULL_TREE;
17185   tree attributes;
17186   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17187   bool template_id_p = false;
17188   bool qualified_p = false;
17189   bool invalid_nested_name_p = false;
17190   bool invalid_explicit_specialization_p = false;
17191   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17192   tree pushed_scope = NULL_TREE;
17193   unsigned num_templates;
17194   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17195   /* Assume no nested-name-specifier will be present.  */
17196   *nested_name_specifier_p = false;
17197   /* Assume no template parameter lists will be used in defining the
17198      type.  */
17199   num_templates = 0;
17200   parser->colon_corrects_to_scope_p = false;
17201
17202   *bases = NULL_TREE;
17203
17204   /* Look for the class-key.  */
17205   class_key = cp_parser_class_key (parser);
17206   if (class_key == none_type)
17207     return error_mark_node;
17208
17209   /* Parse the attributes.  */
17210   attributes = cp_parser_attributes_opt (parser);
17211
17212   /* If the next token is `::', that is invalid -- but sometimes
17213      people do try to write:
17214
17215        struct ::S {};
17216
17217      Handle this gracefully by accepting the extra qualifier, and then
17218      issuing an error about it later if this really is a
17219      class-head.  If it turns out just to be an elaborated type
17220      specifier, remain silent.  */
17221   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17222     qualified_p = true;
17223
17224   push_deferring_access_checks (dk_no_check);
17225
17226   /* Determine the name of the class.  Begin by looking for an
17227      optional nested-name-specifier.  */
17228   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17229   nested_name_specifier
17230     = cp_parser_nested_name_specifier_opt (parser,
17231                                            /*typename_keyword_p=*/false,
17232                                            /*check_dependency_p=*/false,
17233                                            /*type_p=*/false,
17234                                            /*is_declaration=*/false);
17235   /* If there was a nested-name-specifier, then there *must* be an
17236      identifier.  */
17237   if (nested_name_specifier)
17238     {
17239       type_start_token = cp_lexer_peek_token (parser->lexer);
17240       /* Although the grammar says `identifier', it really means
17241          `class-name' or `template-name'.  You are only allowed to
17242          define a class that has already been declared with this
17243          syntax.
17244
17245          The proposed resolution for Core Issue 180 says that wherever
17246          you see `class T::X' you should treat `X' as a type-name.
17247
17248          It is OK to define an inaccessible class; for example:
17249
17250            class A { class B; };
17251            class A::B {};
17252
17253          We do not know if we will see a class-name, or a
17254          template-name.  We look for a class-name first, in case the
17255          class-name is a template-id; if we looked for the
17256          template-name first we would stop after the template-name.  */
17257       cp_parser_parse_tentatively (parser);
17258       type = cp_parser_class_name (parser,
17259                                    /*typename_keyword_p=*/false,
17260                                    /*template_keyword_p=*/false,
17261                                    class_type,
17262                                    /*check_dependency_p=*/false,
17263                                    /*class_head_p=*/true,
17264                                    /*is_declaration=*/false);
17265       /* If that didn't work, ignore the nested-name-specifier.  */
17266       if (!cp_parser_parse_definitely (parser))
17267         {
17268           invalid_nested_name_p = true;
17269           type_start_token = cp_lexer_peek_token (parser->lexer);
17270           id = cp_parser_identifier (parser);
17271           if (id == error_mark_node)
17272             id = NULL_TREE;
17273         }
17274       /* If we could not find a corresponding TYPE, treat this
17275          declaration like an unqualified declaration.  */
17276       if (type == error_mark_node)
17277         nested_name_specifier = NULL_TREE;
17278       /* Otherwise, count the number of templates used in TYPE and its
17279          containing scopes.  */
17280       else
17281         {
17282           tree scope;
17283
17284           for (scope = TREE_TYPE (type);
17285                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17286                scope = (TYPE_P (scope)
17287                         ? TYPE_CONTEXT (scope)
17288                         : DECL_CONTEXT (scope)))
17289             if (TYPE_P (scope)
17290                 && CLASS_TYPE_P (scope)
17291                 && CLASSTYPE_TEMPLATE_INFO (scope)
17292                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17293                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17294               ++num_templates;
17295         }
17296     }
17297   /* Otherwise, the identifier is optional.  */
17298   else
17299     {
17300       /* We don't know whether what comes next is a template-id,
17301          an identifier, or nothing at all.  */
17302       cp_parser_parse_tentatively (parser);
17303       /* Check for a template-id.  */
17304       type_start_token = cp_lexer_peek_token (parser->lexer);
17305       id = cp_parser_template_id (parser,
17306                                   /*template_keyword_p=*/false,
17307                                   /*check_dependency_p=*/true,
17308                                   /*is_declaration=*/true);
17309       /* If that didn't work, it could still be an identifier.  */
17310       if (!cp_parser_parse_definitely (parser))
17311         {
17312           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17313             {
17314               type_start_token = cp_lexer_peek_token (parser->lexer);
17315               id = cp_parser_identifier (parser);
17316             }
17317           else
17318             id = NULL_TREE;
17319         }
17320       else
17321         {
17322           template_id_p = true;
17323           ++num_templates;
17324         }
17325     }
17326
17327   pop_deferring_access_checks ();
17328
17329   if (id)
17330     {
17331       cp_parser_check_for_invalid_template_id (parser, id,
17332                                                type_start_token->location);
17333       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17334     }
17335
17336   /* If it's not a `:' or a `{' then we can't really be looking at a
17337      class-head, since a class-head only appears as part of a
17338      class-specifier.  We have to detect this situation before calling
17339      xref_tag, since that has irreversible side-effects.  */
17340   if (!cp_parser_next_token_starts_class_definition_p (parser))
17341     {
17342       cp_parser_error (parser, "expected %<{%> or %<:%>");
17343       type = error_mark_node;
17344       goto out;
17345     }
17346
17347   /* At this point, we're going ahead with the class-specifier, even
17348      if some other problem occurs.  */
17349   cp_parser_commit_to_tentative_parse (parser);
17350   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17351     {
17352       cp_parser_error (parser,
17353                        "cannot specify %<override%> for a class");
17354       type = error_mark_node;
17355       goto out;
17356     }
17357   /* Issue the error about the overly-qualified name now.  */
17358   if (qualified_p)
17359     {
17360       cp_parser_error (parser,
17361                        "global qualification of class name is invalid");
17362       type = error_mark_node;
17363       goto out;
17364     }
17365   else if (invalid_nested_name_p)
17366     {
17367       cp_parser_error (parser,
17368                        "qualified name does not name a class");
17369       type = error_mark_node;
17370       goto out;
17371     }
17372   else if (nested_name_specifier)
17373     {
17374       tree scope;
17375
17376       /* Reject typedef-names in class heads.  */
17377       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17378         {
17379           error_at (type_start_token->location,
17380                     "invalid class name in declaration of %qD",
17381                     type);
17382           type = NULL_TREE;
17383           goto done;
17384         }
17385
17386       /* Figure out in what scope the declaration is being placed.  */
17387       scope = current_scope ();
17388       /* If that scope does not contain the scope in which the
17389          class was originally declared, the program is invalid.  */
17390       if (scope && !is_ancestor (scope, nested_name_specifier))
17391         {
17392           if (at_namespace_scope_p ())
17393             error_at (type_start_token->location,
17394                       "declaration of %qD in namespace %qD which does not "
17395                       "enclose %qD",
17396                       type, scope, nested_name_specifier);
17397           else
17398             error_at (type_start_token->location,
17399                       "declaration of %qD in %qD which does not enclose %qD",
17400                       type, scope, nested_name_specifier);
17401           type = NULL_TREE;
17402           goto done;
17403         }
17404       /* [dcl.meaning]
17405
17406          A declarator-id shall not be qualified except for the
17407          definition of a ... nested class outside of its class
17408          ... [or] the definition or explicit instantiation of a
17409          class member of a namespace outside of its namespace.  */
17410       if (scope == nested_name_specifier)
17411         {
17412           permerror (nested_name_specifier_token_start->location,
17413                      "extra qualification not allowed");
17414           nested_name_specifier = NULL_TREE;
17415           num_templates = 0;
17416         }
17417     }
17418   /* An explicit-specialization must be preceded by "template <>".  If
17419      it is not, try to recover gracefully.  */
17420   if (at_namespace_scope_p ()
17421       && parser->num_template_parameter_lists == 0
17422       && template_id_p)
17423     {
17424       error_at (type_start_token->location,
17425                 "an explicit specialization must be preceded by %<template <>%>");
17426       invalid_explicit_specialization_p = true;
17427       /* Take the same action that would have been taken by
17428          cp_parser_explicit_specialization.  */
17429       ++parser->num_template_parameter_lists;
17430       begin_specialization ();
17431     }
17432   /* There must be no "return" statements between this point and the
17433      end of this function; set "type "to the correct return value and
17434      use "goto done;" to return.  */
17435   /* Make sure that the right number of template parameters were
17436      present.  */
17437   if (!cp_parser_check_template_parameters (parser, num_templates,
17438                                             type_start_token->location,
17439                                             /*declarator=*/NULL))
17440     {
17441       /* If something went wrong, there is no point in even trying to
17442          process the class-definition.  */
17443       type = NULL_TREE;
17444       goto done;
17445     }
17446
17447   /* Look up the type.  */
17448   if (template_id_p)
17449     {
17450       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17451           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17452               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17453         {
17454           error_at (type_start_token->location,
17455                     "function template %qD redeclared as a class template", id);
17456           type = error_mark_node;
17457         }
17458       else
17459         {
17460           type = TREE_TYPE (id);
17461           type = maybe_process_partial_specialization (type);
17462         }
17463       if (nested_name_specifier)
17464         pushed_scope = push_scope (nested_name_specifier);
17465     }
17466   else if (nested_name_specifier)
17467     {
17468       tree class_type;
17469
17470       /* Given:
17471
17472             template <typename T> struct S { struct T };
17473             template <typename T> struct S<T>::T { };
17474
17475          we will get a TYPENAME_TYPE when processing the definition of
17476          `S::T'.  We need to resolve it to the actual type before we
17477          try to define it.  */
17478       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17479         {
17480           class_type = resolve_typename_type (TREE_TYPE (type),
17481                                               /*only_current_p=*/false);
17482           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17483             type = TYPE_NAME (class_type);
17484           else
17485             {
17486               cp_parser_error (parser, "could not resolve typename type");
17487               type = error_mark_node;
17488             }
17489         }
17490
17491       if (maybe_process_partial_specialization (TREE_TYPE (type))
17492           == error_mark_node)
17493         {
17494           type = NULL_TREE;
17495           goto done;
17496         }
17497
17498       class_type = current_class_type;
17499       /* Enter the scope indicated by the nested-name-specifier.  */
17500       pushed_scope = push_scope (nested_name_specifier);
17501       /* Get the canonical version of this type.  */
17502       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17503       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17504           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17505         {
17506           type = push_template_decl (type);
17507           if (type == error_mark_node)
17508             {
17509               type = NULL_TREE;
17510               goto done;
17511             }
17512         }
17513
17514       type = TREE_TYPE (type);
17515       *nested_name_specifier_p = true;
17516     }
17517   else      /* The name is not a nested name.  */
17518     {
17519       /* If the class was unnamed, create a dummy name.  */
17520       if (!id)
17521         id = make_anon_name ();
17522       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17523                        parser->num_template_parameter_lists);
17524     }
17525
17526   /* Indicate whether this class was declared as a `class' or as a
17527      `struct'.  */
17528   if (TREE_CODE (type) == RECORD_TYPE)
17529     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17530   cp_parser_check_class_key (class_key, type);
17531
17532   /* If this type was already complete, and we see another definition,
17533      that's an error.  */
17534   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17535     {
17536       error_at (type_start_token->location, "redefinition of %q#T",
17537                 type);
17538       error_at (type_start_token->location, "previous definition of %q+#T",
17539                 type);
17540       type = NULL_TREE;
17541       goto done;
17542     }
17543   else if (type == error_mark_node)
17544     type = NULL_TREE;
17545
17546   /* We will have entered the scope containing the class; the names of
17547      base classes should be looked up in that context.  For example:
17548
17549        struct A { struct B {}; struct C; };
17550        struct A::C : B {};
17551
17552      is valid.  */
17553
17554   /* Get the list of base-classes, if there is one.  */
17555   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17556     *bases = cp_parser_base_clause (parser);
17557
17558  done:
17559   /* Leave the scope given by the nested-name-specifier.  We will
17560      enter the class scope itself while processing the members.  */
17561   if (pushed_scope)
17562     pop_scope (pushed_scope);
17563
17564   if (invalid_explicit_specialization_p)
17565     {
17566       end_specialization ();
17567       --parser->num_template_parameter_lists;
17568     }
17569
17570   if (type)
17571     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17572   *attributes_p = attributes;
17573   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17574     CLASSTYPE_FINAL (type) = 1;
17575  out:
17576   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17577   return type;
17578 }
17579
17580 /* Parse a class-key.
17581
17582    class-key:
17583      class
17584      struct
17585      union
17586
17587    Returns the kind of class-key specified, or none_type to indicate
17588    error.  */
17589
17590 static enum tag_types
17591 cp_parser_class_key (cp_parser* parser)
17592 {
17593   cp_token *token;
17594   enum tag_types tag_type;
17595
17596   /* Look for the class-key.  */
17597   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17598   if (!token)
17599     return none_type;
17600
17601   /* Check to see if the TOKEN is a class-key.  */
17602   tag_type = cp_parser_token_is_class_key (token);
17603   if (!tag_type)
17604     cp_parser_error (parser, "expected class-key");
17605   return tag_type;
17606 }
17607
17608 /* Parse an (optional) member-specification.
17609
17610    member-specification:
17611      member-declaration member-specification [opt]
17612      access-specifier : member-specification [opt]  */
17613
17614 static void
17615 cp_parser_member_specification_opt (cp_parser* parser)
17616 {
17617   while (true)
17618     {
17619       cp_token *token;
17620       enum rid keyword;
17621
17622       /* Peek at the next token.  */
17623       token = cp_lexer_peek_token (parser->lexer);
17624       /* If it's a `}', or EOF then we've seen all the members.  */
17625       if (token->type == CPP_CLOSE_BRACE
17626           || token->type == CPP_EOF
17627           || token->type == CPP_PRAGMA_EOL)
17628         break;
17629
17630       /* See if this token is a keyword.  */
17631       keyword = token->keyword;
17632       switch (keyword)
17633         {
17634         case RID_PUBLIC:
17635         case RID_PROTECTED:
17636         case RID_PRIVATE:
17637           /* Consume the access-specifier.  */
17638           cp_lexer_consume_token (parser->lexer);
17639           /* Remember which access-specifier is active.  */
17640           current_access_specifier = token->u.value;
17641           /* Look for the `:'.  */
17642           cp_parser_require (parser, CPP_COLON, RT_COLON);
17643           break;
17644
17645         default:
17646           /* Accept #pragmas at class scope.  */
17647           if (token->type == CPP_PRAGMA)
17648             {
17649               cp_parser_pragma (parser, pragma_external);
17650               break;
17651             }
17652
17653           /* Otherwise, the next construction must be a
17654              member-declaration.  */
17655           cp_parser_member_declaration (parser);
17656         }
17657     }
17658 }
17659
17660 /* Parse a member-declaration.
17661
17662    member-declaration:
17663      decl-specifier-seq [opt] member-declarator-list [opt] ;
17664      function-definition ; [opt]
17665      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17666      using-declaration
17667      template-declaration
17668
17669    member-declarator-list:
17670      member-declarator
17671      member-declarator-list , member-declarator
17672
17673    member-declarator:
17674      declarator pure-specifier [opt]
17675      declarator constant-initializer [opt]
17676      identifier [opt] : constant-expression
17677
17678    GNU Extensions:
17679
17680    member-declaration:
17681      __extension__ member-declaration
17682
17683    member-declarator:
17684      declarator attributes [opt] pure-specifier [opt]
17685      declarator attributes [opt] constant-initializer [opt]
17686      identifier [opt] attributes [opt] : constant-expression  
17687
17688    C++0x Extensions:
17689
17690    member-declaration:
17691      static_assert-declaration  */
17692
17693 static void
17694 cp_parser_member_declaration (cp_parser* parser)
17695 {
17696   cp_decl_specifier_seq decl_specifiers;
17697   tree prefix_attributes;
17698   tree decl;
17699   int declares_class_or_enum;
17700   bool friend_p;
17701   cp_token *token = NULL;
17702   cp_token *decl_spec_token_start = NULL;
17703   cp_token *initializer_token_start = NULL;
17704   int saved_pedantic;
17705   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17706
17707   /* Check for the `__extension__' keyword.  */
17708   if (cp_parser_extension_opt (parser, &saved_pedantic))
17709     {
17710       /* Recurse.  */
17711       cp_parser_member_declaration (parser);
17712       /* Restore the old value of the PEDANTIC flag.  */
17713       pedantic = saved_pedantic;
17714
17715       return;
17716     }
17717
17718   /* Check for a template-declaration.  */
17719   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17720     {
17721       /* An explicit specialization here is an error condition, and we
17722          expect the specialization handler to detect and report this.  */
17723       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17724           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17725         cp_parser_explicit_specialization (parser);
17726       else
17727         cp_parser_template_declaration (parser, /*member_p=*/true);
17728
17729       return;
17730     }
17731
17732   /* Check for a using-declaration.  */
17733   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17734     {
17735       /* Parse the using-declaration.  */
17736       cp_parser_using_declaration (parser,
17737                                    /*access_declaration_p=*/false);
17738       return;
17739     }
17740
17741   /* Check for @defs.  */
17742   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17743     {
17744       tree ivar, member;
17745       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17746       ivar = ivar_chains;
17747       while (ivar)
17748         {
17749           member = ivar;
17750           ivar = TREE_CHAIN (member);
17751           TREE_CHAIN (member) = NULL_TREE;
17752           finish_member_declaration (member);
17753         }
17754       return;
17755     }
17756
17757   /* If the next token is `static_assert' we have a static assertion.  */
17758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17759     {
17760       cp_parser_static_assert (parser, /*member_p=*/true);
17761       return;
17762     }
17763
17764   parser->colon_corrects_to_scope_p = false;
17765
17766   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17767     goto out;
17768
17769   /* Parse the decl-specifier-seq.  */
17770   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17771   cp_parser_decl_specifier_seq (parser,
17772                                 CP_PARSER_FLAGS_OPTIONAL,
17773                                 &decl_specifiers,
17774                                 &declares_class_or_enum);
17775   prefix_attributes = decl_specifiers.attributes;
17776   decl_specifiers.attributes = NULL_TREE;
17777   /* Check for an invalid type-name.  */
17778   if (!decl_specifiers.any_type_specifiers_p
17779       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17780     goto out;
17781   /* If there is no declarator, then the decl-specifier-seq should
17782      specify a type.  */
17783   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17784     {
17785       /* If there was no decl-specifier-seq, and the next token is a
17786          `;', then we have something like:
17787
17788            struct S { ; };
17789
17790          [class.mem]
17791
17792          Each member-declaration shall declare at least one member
17793          name of the class.  */
17794       if (!decl_specifiers.any_specifiers_p)
17795         {
17796           cp_token *token = cp_lexer_peek_token (parser->lexer);
17797           if (!in_system_header_at (token->location))
17798             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17799         }
17800       else
17801         {
17802           tree type;
17803
17804           /* See if this declaration is a friend.  */
17805           friend_p = cp_parser_friend_p (&decl_specifiers);
17806           /* If there were decl-specifiers, check to see if there was
17807              a class-declaration.  */
17808           type = check_tag_decl (&decl_specifiers);
17809           /* Nested classes have already been added to the class, but
17810              a `friend' needs to be explicitly registered.  */
17811           if (friend_p)
17812             {
17813               /* If the `friend' keyword was present, the friend must
17814                  be introduced with a class-key.  */
17815                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17816                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17817                           "in C++03 a class-key must be used "
17818                           "when declaring a friend");
17819                /* In this case:
17820
17821                     template <typename T> struct A {
17822                       friend struct A<T>::B;
17823                     };
17824
17825                   A<T>::B will be represented by a TYPENAME_TYPE, and
17826                   therefore not recognized by check_tag_decl.  */
17827                if (!type)
17828                  {
17829                    type = decl_specifiers.type;
17830                    if (type && TREE_CODE (type) == TYPE_DECL)
17831                      type = TREE_TYPE (type);
17832                  }
17833                if (!type || !TYPE_P (type))
17834                  error_at (decl_spec_token_start->location,
17835                            "friend declaration does not name a class or "
17836                            "function");
17837                else
17838                  make_friend_class (current_class_type, type,
17839                                     /*complain=*/true);
17840             }
17841           /* If there is no TYPE, an error message will already have
17842              been issued.  */
17843           else if (!type || type == error_mark_node)
17844             ;
17845           /* An anonymous aggregate has to be handled specially; such
17846              a declaration really declares a data member (with a
17847              particular type), as opposed to a nested class.  */
17848           else if (ANON_AGGR_TYPE_P (type))
17849             {
17850               /* Remove constructors and such from TYPE, now that we
17851                  know it is an anonymous aggregate.  */
17852               fixup_anonymous_aggr (type);
17853               /* And make the corresponding data member.  */
17854               decl = build_decl (decl_spec_token_start->location,
17855                                  FIELD_DECL, NULL_TREE, type);
17856               /* Add it to the class.  */
17857               finish_member_declaration (decl);
17858             }
17859           else
17860             cp_parser_check_access_in_redeclaration
17861                                               (TYPE_NAME (type),
17862                                                decl_spec_token_start->location);
17863         }
17864     }
17865   else
17866     {
17867       bool assume_semicolon = false;
17868
17869       /* See if these declarations will be friends.  */
17870       friend_p = cp_parser_friend_p (&decl_specifiers);
17871
17872       /* Keep going until we hit the `;' at the end of the
17873          declaration.  */
17874       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17875         {
17876           tree attributes = NULL_TREE;
17877           tree first_attribute;
17878
17879           /* Peek at the next token.  */
17880           token = cp_lexer_peek_token (parser->lexer);
17881
17882           /* Check for a bitfield declaration.  */
17883           if (token->type == CPP_COLON
17884               || (token->type == CPP_NAME
17885                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17886                   == CPP_COLON))
17887             {
17888               tree identifier;
17889               tree width;
17890
17891               /* Get the name of the bitfield.  Note that we cannot just
17892                  check TOKEN here because it may have been invalidated by
17893                  the call to cp_lexer_peek_nth_token above.  */
17894               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17895                 identifier = cp_parser_identifier (parser);
17896               else
17897                 identifier = NULL_TREE;
17898
17899               /* Consume the `:' token.  */
17900               cp_lexer_consume_token (parser->lexer);
17901               /* Get the width of the bitfield.  */
17902               width
17903                 = cp_parser_constant_expression (parser,
17904                                                  /*allow_non_constant=*/false,
17905                                                  NULL);
17906
17907               /* Look for attributes that apply to the bitfield.  */
17908               attributes = cp_parser_attributes_opt (parser);
17909               /* Remember which attributes are prefix attributes and
17910                  which are not.  */
17911               first_attribute = attributes;
17912               /* Combine the attributes.  */
17913               attributes = chainon (prefix_attributes, attributes);
17914
17915               /* Create the bitfield declaration.  */
17916               decl = grokbitfield (identifier
17917                                    ? make_id_declarator (NULL_TREE,
17918                                                          identifier,
17919                                                          sfk_none)
17920                                    : NULL,
17921                                    &decl_specifiers,
17922                                    width,
17923                                    attributes);
17924             }
17925           else
17926             {
17927               cp_declarator *declarator;
17928               tree initializer;
17929               tree asm_specification;
17930               int ctor_dtor_or_conv_p;
17931
17932               /* Parse the declarator.  */
17933               declarator
17934                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17935                                         &ctor_dtor_or_conv_p,
17936                                         /*parenthesized_p=*/NULL,
17937                                         /*member_p=*/true);
17938
17939               /* If something went wrong parsing the declarator, make sure
17940                  that we at least consume some tokens.  */
17941               if (declarator == cp_error_declarator)
17942                 {
17943                   /* Skip to the end of the statement.  */
17944                   cp_parser_skip_to_end_of_statement (parser);
17945                   /* If the next token is not a semicolon, that is
17946                      probably because we just skipped over the body of
17947                      a function.  So, we consume a semicolon if
17948                      present, but do not issue an error message if it
17949                      is not present.  */
17950                   if (cp_lexer_next_token_is (parser->lexer,
17951                                               CPP_SEMICOLON))
17952                     cp_lexer_consume_token (parser->lexer);
17953                   goto out;
17954                 }
17955
17956               if (declares_class_or_enum & 2)
17957                 cp_parser_check_for_definition_in_return_type
17958                                             (declarator, decl_specifiers.type,
17959                                              decl_specifiers.type_location);
17960
17961               /* Look for an asm-specification.  */
17962               asm_specification = cp_parser_asm_specification_opt (parser);
17963               /* Look for attributes that apply to the declaration.  */
17964               attributes = cp_parser_attributes_opt (parser);
17965               /* Remember which attributes are prefix attributes and
17966                  which are not.  */
17967               first_attribute = attributes;
17968               /* Combine the attributes.  */
17969               attributes = chainon (prefix_attributes, attributes);
17970
17971               /* If it's an `=', then we have a constant-initializer or a
17972                  pure-specifier.  It is not correct to parse the
17973                  initializer before registering the member declaration
17974                  since the member declaration should be in scope while
17975                  its initializer is processed.  However, the rest of the
17976                  front end does not yet provide an interface that allows
17977                  us to handle this correctly.  */
17978               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17979                 {
17980                   /* In [class.mem]:
17981
17982                      A pure-specifier shall be used only in the declaration of
17983                      a virtual function.
17984
17985                      A member-declarator can contain a constant-initializer
17986                      only if it declares a static member of integral or
17987                      enumeration type.
17988
17989                      Therefore, if the DECLARATOR is for a function, we look
17990                      for a pure-specifier; otherwise, we look for a
17991                      constant-initializer.  When we call `grokfield', it will
17992                      perform more stringent semantics checks.  */
17993                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17994                   if (function_declarator_p (declarator))
17995                     initializer = cp_parser_pure_specifier (parser);
17996                   else
17997                     /* Parse the initializer.  */
17998                     initializer = cp_parser_constant_initializer (parser);
17999                 }
18000               /* Otherwise, there is no initializer.  */
18001               else
18002                 initializer = NULL_TREE;
18003
18004               /* See if we are probably looking at a function
18005                  definition.  We are certainly not looking at a
18006                  member-declarator.  Calling `grokfield' has
18007                  side-effects, so we must not do it unless we are sure
18008                  that we are looking at a member-declarator.  */
18009               if (cp_parser_token_starts_function_definition_p
18010                   (cp_lexer_peek_token (parser->lexer)))
18011                 {
18012                   /* The grammar does not allow a pure-specifier to be
18013                      used when a member function is defined.  (It is
18014                      possible that this fact is an oversight in the
18015                      standard, since a pure function may be defined
18016                      outside of the class-specifier.  */
18017                   if (initializer)
18018                     error_at (initializer_token_start->location,
18019                               "pure-specifier on function-definition");
18020                   decl = cp_parser_save_member_function_body (parser,
18021                                                               &decl_specifiers,
18022                                                               declarator,
18023                                                               attributes);
18024                   /* If the member was not a friend, declare it here.  */
18025                   if (!friend_p)
18026                     finish_member_declaration (decl);
18027                   /* Peek at the next token.  */
18028                   token = cp_lexer_peek_token (parser->lexer);
18029                   /* If the next token is a semicolon, consume it.  */
18030                   if (token->type == CPP_SEMICOLON)
18031                     cp_lexer_consume_token (parser->lexer);
18032                   goto out;
18033                 }
18034               else
18035                 if (declarator->kind == cdk_function)
18036                   declarator->id_loc = token->location;
18037                 /* Create the declaration.  */
18038                 decl = grokfield (declarator, &decl_specifiers,
18039                                   initializer, /*init_const_expr_p=*/true,
18040                                   asm_specification,
18041                                   attributes);
18042             }
18043
18044           /* Reset PREFIX_ATTRIBUTES.  */
18045           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18046             attributes = TREE_CHAIN (attributes);
18047           if (attributes)
18048             TREE_CHAIN (attributes) = NULL_TREE;
18049
18050           /* If there is any qualification still in effect, clear it
18051              now; we will be starting fresh with the next declarator.  */
18052           parser->scope = NULL_TREE;
18053           parser->qualifying_scope = NULL_TREE;
18054           parser->object_scope = NULL_TREE;
18055           /* If it's a `,', then there are more declarators.  */
18056           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18057             cp_lexer_consume_token (parser->lexer);
18058           /* If the next token isn't a `;', then we have a parse error.  */
18059           else if (cp_lexer_next_token_is_not (parser->lexer,
18060                                                CPP_SEMICOLON))
18061             {
18062               /* The next token might be a ways away from where the
18063                  actual semicolon is missing.  Find the previous token
18064                  and use that for our error position.  */
18065               cp_token *token = cp_lexer_previous_token (parser->lexer);
18066               error_at (token->location,
18067                         "expected %<;%> at end of member declaration");
18068
18069               /* Assume that the user meant to provide a semicolon.  If
18070                  we were to cp_parser_skip_to_end_of_statement, we might
18071                  skip to a semicolon inside a member function definition
18072                  and issue nonsensical error messages.  */
18073               assume_semicolon = true;
18074             }
18075
18076           if (decl)
18077             {
18078               /* Add DECL to the list of members.  */
18079               if (!friend_p)
18080                 finish_member_declaration (decl);
18081
18082               if (TREE_CODE (decl) == FUNCTION_DECL)
18083                 cp_parser_save_default_args (parser, decl);
18084             }
18085
18086           if (assume_semicolon)
18087             goto out;
18088         }
18089     }
18090
18091   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18092  out:
18093   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18094 }
18095
18096 /* Parse a pure-specifier.
18097
18098    pure-specifier:
18099      = 0
18100
18101    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18102    Otherwise, ERROR_MARK_NODE is returned.  */
18103
18104 static tree
18105 cp_parser_pure_specifier (cp_parser* parser)
18106 {
18107   cp_token *token;
18108
18109   /* Look for the `=' token.  */
18110   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18111     return error_mark_node;
18112   /* Look for the `0' token.  */
18113   token = cp_lexer_peek_token (parser->lexer);
18114
18115   if (token->type == CPP_EOF
18116       || token->type == CPP_PRAGMA_EOL)
18117     return error_mark_node;
18118
18119   cp_lexer_consume_token (parser->lexer);
18120
18121   /* Accept = default or = delete in c++0x mode.  */
18122   if (token->keyword == RID_DEFAULT
18123       || token->keyword == RID_DELETE)
18124     {
18125       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18126       return token->u.value;
18127     }
18128
18129   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18130   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18131     {
18132       cp_parser_error (parser,
18133                        "invalid pure specifier (only %<= 0%> is allowed)");
18134       cp_parser_skip_to_end_of_statement (parser);
18135       return error_mark_node;
18136     }
18137   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18138     {
18139       error_at (token->location, "templates may not be %<virtual%>");
18140       return error_mark_node;
18141     }
18142
18143   return integer_zero_node;
18144 }
18145
18146 /* Parse a constant-initializer.
18147
18148    constant-initializer:
18149      = constant-expression
18150
18151    Returns a representation of the constant-expression.  */
18152
18153 static tree
18154 cp_parser_constant_initializer (cp_parser* parser)
18155 {
18156   /* Look for the `=' token.  */
18157   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18158     return error_mark_node;
18159
18160   /* It is invalid to write:
18161
18162        struct S { static const int i = { 7 }; };
18163
18164      */
18165   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18166     {
18167       cp_parser_error (parser,
18168                        "a brace-enclosed initializer is not allowed here");
18169       /* Consume the opening brace.  */
18170       cp_lexer_consume_token (parser->lexer);
18171       /* Skip the initializer.  */
18172       cp_parser_skip_to_closing_brace (parser);
18173       /* Look for the trailing `}'.  */
18174       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18175
18176       return error_mark_node;
18177     }
18178
18179   return cp_parser_constant_expression (parser,
18180                                         /*allow_non_constant=*/false,
18181                                         NULL);
18182 }
18183
18184 /* Derived classes [gram.class.derived] */
18185
18186 /* Parse a base-clause.
18187
18188    base-clause:
18189      : base-specifier-list
18190
18191    base-specifier-list:
18192      base-specifier ... [opt]
18193      base-specifier-list , base-specifier ... [opt]
18194
18195    Returns a TREE_LIST representing the base-classes, in the order in
18196    which they were declared.  The representation of each node is as
18197    described by cp_parser_base_specifier.
18198
18199    In the case that no bases are specified, this function will return
18200    NULL_TREE, not ERROR_MARK_NODE.  */
18201
18202 static tree
18203 cp_parser_base_clause (cp_parser* parser)
18204 {
18205   tree bases = NULL_TREE;
18206
18207   /* Look for the `:' that begins the list.  */
18208   cp_parser_require (parser, CPP_COLON, RT_COLON);
18209
18210   /* Scan the base-specifier-list.  */
18211   while (true)
18212     {
18213       cp_token *token;
18214       tree base;
18215       bool pack_expansion_p = false;
18216
18217       /* Look for the base-specifier.  */
18218       base = cp_parser_base_specifier (parser);
18219       /* Look for the (optional) ellipsis. */
18220       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18221         {
18222           /* Consume the `...'. */
18223           cp_lexer_consume_token (parser->lexer);
18224
18225           pack_expansion_p = true;
18226         }
18227
18228       /* Add BASE to the front of the list.  */
18229       if (base != error_mark_node)
18230         {
18231           if (pack_expansion_p)
18232             /* Make this a pack expansion type. */
18233             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18234           
18235
18236           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18237             {
18238               TREE_CHAIN (base) = bases;
18239               bases = base;
18240             }
18241         }
18242       /* Peek at the next token.  */
18243       token = cp_lexer_peek_token (parser->lexer);
18244       /* If it's not a comma, then the list is complete.  */
18245       if (token->type != CPP_COMMA)
18246         break;
18247       /* Consume the `,'.  */
18248       cp_lexer_consume_token (parser->lexer);
18249     }
18250
18251   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18252      base class had a qualified name.  However, the next name that
18253      appears is certainly not qualified.  */
18254   parser->scope = NULL_TREE;
18255   parser->qualifying_scope = NULL_TREE;
18256   parser->object_scope = NULL_TREE;
18257
18258   return nreverse (bases);
18259 }
18260
18261 /* Parse a base-specifier.
18262
18263    base-specifier:
18264      :: [opt] nested-name-specifier [opt] class-name
18265      virtual access-specifier [opt] :: [opt] nested-name-specifier
18266        [opt] class-name
18267      access-specifier virtual [opt] :: [opt] nested-name-specifier
18268        [opt] class-name
18269
18270    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18271    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18272    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18273    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18274
18275 static tree
18276 cp_parser_base_specifier (cp_parser* parser)
18277 {
18278   cp_token *token;
18279   bool done = false;
18280   bool virtual_p = false;
18281   bool duplicate_virtual_error_issued_p = false;
18282   bool duplicate_access_error_issued_p = false;
18283   bool class_scope_p, template_p;
18284   tree access = access_default_node;
18285   tree type;
18286
18287   /* Process the optional `virtual' and `access-specifier'.  */
18288   while (!done)
18289     {
18290       /* Peek at the next token.  */
18291       token = cp_lexer_peek_token (parser->lexer);
18292       /* Process `virtual'.  */
18293       switch (token->keyword)
18294         {
18295         case RID_VIRTUAL:
18296           /* If `virtual' appears more than once, issue an error.  */
18297           if (virtual_p && !duplicate_virtual_error_issued_p)
18298             {
18299               cp_parser_error (parser,
18300                                "%<virtual%> specified more than once in base-specified");
18301               duplicate_virtual_error_issued_p = true;
18302             }
18303
18304           virtual_p = true;
18305
18306           /* Consume the `virtual' token.  */
18307           cp_lexer_consume_token (parser->lexer);
18308
18309           break;
18310
18311         case RID_PUBLIC:
18312         case RID_PROTECTED:
18313         case RID_PRIVATE:
18314           /* If more than one access specifier appears, issue an
18315              error.  */
18316           if (access != access_default_node
18317               && !duplicate_access_error_issued_p)
18318             {
18319               cp_parser_error (parser,
18320                                "more than one access specifier in base-specified");
18321               duplicate_access_error_issued_p = true;
18322             }
18323
18324           access = ridpointers[(int) token->keyword];
18325
18326           /* Consume the access-specifier.  */
18327           cp_lexer_consume_token (parser->lexer);
18328
18329           break;
18330
18331         default:
18332           done = true;
18333           break;
18334         }
18335     }
18336   /* It is not uncommon to see programs mechanically, erroneously, use
18337      the 'typename' keyword to denote (dependent) qualified types
18338      as base classes.  */
18339   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18340     {
18341       token = cp_lexer_peek_token (parser->lexer);
18342       if (!processing_template_decl)
18343         error_at (token->location,
18344                   "keyword %<typename%> not allowed outside of templates");
18345       else
18346         error_at (token->location,
18347                   "keyword %<typename%> not allowed in this context "
18348                   "(the base class is implicitly a type)");
18349       cp_lexer_consume_token (parser->lexer);
18350     }
18351
18352   /* Look for the optional `::' operator.  */
18353   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18354   /* Look for the nested-name-specifier.  The simplest way to
18355      implement:
18356
18357        [temp.res]
18358
18359        The keyword `typename' is not permitted in a base-specifier or
18360        mem-initializer; in these contexts a qualified name that
18361        depends on a template-parameter is implicitly assumed to be a
18362        type name.
18363
18364      is to pretend that we have seen the `typename' keyword at this
18365      point.  */
18366   cp_parser_nested_name_specifier_opt (parser,
18367                                        /*typename_keyword_p=*/true,
18368                                        /*check_dependency_p=*/true,
18369                                        typename_type,
18370                                        /*is_declaration=*/true);
18371   /* If the base class is given by a qualified name, assume that names
18372      we see are type names or templates, as appropriate.  */
18373   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18374   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18375
18376   /* Finally, look for the class-name.  */
18377   type = cp_parser_class_name (parser,
18378                                class_scope_p,
18379                                template_p,
18380                                typename_type,
18381                                /*check_dependency_p=*/true,
18382                                /*class_head_p=*/false,
18383                                /*is_declaration=*/true);
18384
18385   if (type == error_mark_node)
18386     return error_mark_node;
18387
18388   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18389 }
18390
18391 /* Exception handling [gram.exception] */
18392
18393 /* Parse an (optional) exception-specification.
18394
18395    exception-specification:
18396      throw ( type-id-list [opt] )
18397
18398    Returns a TREE_LIST representing the exception-specification.  The
18399    TREE_VALUE of each node is a type.  */
18400
18401 static tree
18402 cp_parser_exception_specification_opt (cp_parser* parser)
18403 {
18404   cp_token *token;
18405   tree type_id_list;
18406   const char *saved_message;
18407
18408   /* Peek at the next token.  */
18409   token = cp_lexer_peek_token (parser->lexer);
18410
18411   /* Is it a noexcept-specification?  */
18412   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18413     {
18414       tree expr;
18415       cp_lexer_consume_token (parser->lexer);
18416
18417       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18418         {
18419           cp_lexer_consume_token (parser->lexer);
18420
18421           /* Types may not be defined in an exception-specification.  */
18422           saved_message = parser->type_definition_forbidden_message;
18423           parser->type_definition_forbidden_message
18424             = G_("types may not be defined in an exception-specification");
18425
18426           expr = cp_parser_constant_expression (parser, false, NULL);
18427
18428           /* Restore the saved message.  */
18429           parser->type_definition_forbidden_message = saved_message;
18430
18431           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18432         }
18433       else
18434         expr = boolean_true_node;
18435
18436       return build_noexcept_spec (expr, tf_warning_or_error);
18437     }
18438
18439   /* If it's not `throw', then there's no exception-specification.  */
18440   if (!cp_parser_is_keyword (token, RID_THROW))
18441     return NULL_TREE;
18442
18443 #if 0
18444   /* Enable this once a lot of code has transitioned to noexcept?  */
18445   if (cxx_dialect == cxx0x && !in_system_header)
18446     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18447              "deprecated in C++0x; use %<noexcept%> instead");
18448 #endif
18449
18450   /* Consume the `throw'.  */
18451   cp_lexer_consume_token (parser->lexer);
18452
18453   /* Look for the `('.  */
18454   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18455
18456   /* Peek at the next token.  */
18457   token = cp_lexer_peek_token (parser->lexer);
18458   /* If it's not a `)', then there is a type-id-list.  */
18459   if (token->type != CPP_CLOSE_PAREN)
18460     {
18461       /* Types may not be defined in an exception-specification.  */
18462       saved_message = parser->type_definition_forbidden_message;
18463       parser->type_definition_forbidden_message
18464         = G_("types may not be defined in an exception-specification");
18465       /* Parse the type-id-list.  */
18466       type_id_list = cp_parser_type_id_list (parser);
18467       /* Restore the saved message.  */
18468       parser->type_definition_forbidden_message = saved_message;
18469     }
18470   else
18471     type_id_list = empty_except_spec;
18472
18473   /* Look for the `)'.  */
18474   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18475
18476   return type_id_list;
18477 }
18478
18479 /* Parse an (optional) type-id-list.
18480
18481    type-id-list:
18482      type-id ... [opt]
18483      type-id-list , type-id ... [opt]
18484
18485    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18486    in the order that the types were presented.  */
18487
18488 static tree
18489 cp_parser_type_id_list (cp_parser* parser)
18490 {
18491   tree types = NULL_TREE;
18492
18493   while (true)
18494     {
18495       cp_token *token;
18496       tree type;
18497
18498       /* Get the next type-id.  */
18499       type = cp_parser_type_id (parser);
18500       /* Parse the optional ellipsis. */
18501       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18502         {
18503           /* Consume the `...'. */
18504           cp_lexer_consume_token (parser->lexer);
18505
18506           /* Turn the type into a pack expansion expression. */
18507           type = make_pack_expansion (type);
18508         }
18509       /* Add it to the list.  */
18510       types = add_exception_specifier (types, type, /*complain=*/1);
18511       /* Peek at the next token.  */
18512       token = cp_lexer_peek_token (parser->lexer);
18513       /* If it is not a `,', we are done.  */
18514       if (token->type != CPP_COMMA)
18515         break;
18516       /* Consume the `,'.  */
18517       cp_lexer_consume_token (parser->lexer);
18518     }
18519
18520   return nreverse (types);
18521 }
18522
18523 /* Parse a try-block.
18524
18525    try-block:
18526      try compound-statement handler-seq  */
18527
18528 static tree
18529 cp_parser_try_block (cp_parser* parser)
18530 {
18531   tree try_block;
18532
18533   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18534   try_block = begin_try_block ();
18535   cp_parser_compound_statement (parser, NULL, true, false);
18536   finish_try_block (try_block);
18537   cp_parser_handler_seq (parser);
18538   finish_handler_sequence (try_block);
18539
18540   return try_block;
18541 }
18542
18543 /* Parse a function-try-block.
18544
18545    function-try-block:
18546      try ctor-initializer [opt] function-body handler-seq  */
18547
18548 static bool
18549 cp_parser_function_try_block (cp_parser* parser)
18550 {
18551   tree compound_stmt;
18552   tree try_block;
18553   bool ctor_initializer_p;
18554
18555   /* Look for the `try' keyword.  */
18556   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18557     return false;
18558   /* Let the rest of the front end know where we are.  */
18559   try_block = begin_function_try_block (&compound_stmt);
18560   /* Parse the function-body.  */
18561   ctor_initializer_p
18562     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18563   /* We're done with the `try' part.  */
18564   finish_function_try_block (try_block);
18565   /* Parse the handlers.  */
18566   cp_parser_handler_seq (parser);
18567   /* We're done with the handlers.  */
18568   finish_function_handler_sequence (try_block, compound_stmt);
18569
18570   return ctor_initializer_p;
18571 }
18572
18573 /* Parse a handler-seq.
18574
18575    handler-seq:
18576      handler handler-seq [opt]  */
18577
18578 static void
18579 cp_parser_handler_seq (cp_parser* parser)
18580 {
18581   while (true)
18582     {
18583       cp_token *token;
18584
18585       /* Parse the handler.  */
18586       cp_parser_handler (parser);
18587       /* Peek at the next token.  */
18588       token = cp_lexer_peek_token (parser->lexer);
18589       /* If it's not `catch' then there are no more handlers.  */
18590       if (!cp_parser_is_keyword (token, RID_CATCH))
18591         break;
18592     }
18593 }
18594
18595 /* Parse a handler.
18596
18597    handler:
18598      catch ( exception-declaration ) compound-statement  */
18599
18600 static void
18601 cp_parser_handler (cp_parser* parser)
18602 {
18603   tree handler;
18604   tree declaration;
18605
18606   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18607   handler = begin_handler ();
18608   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18609   declaration = cp_parser_exception_declaration (parser);
18610   finish_handler_parms (declaration, handler);
18611   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18612   cp_parser_compound_statement (parser, NULL, false, false);
18613   finish_handler (handler);
18614 }
18615
18616 /* Parse an exception-declaration.
18617
18618    exception-declaration:
18619      type-specifier-seq declarator
18620      type-specifier-seq abstract-declarator
18621      type-specifier-seq
18622      ...
18623
18624    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18625    ellipsis variant is used.  */
18626
18627 static tree
18628 cp_parser_exception_declaration (cp_parser* parser)
18629 {
18630   cp_decl_specifier_seq type_specifiers;
18631   cp_declarator *declarator;
18632   const char *saved_message;
18633
18634   /* If it's an ellipsis, it's easy to handle.  */
18635   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18636     {
18637       /* Consume the `...' token.  */
18638       cp_lexer_consume_token (parser->lexer);
18639       return NULL_TREE;
18640     }
18641
18642   /* Types may not be defined in exception-declarations.  */
18643   saved_message = parser->type_definition_forbidden_message;
18644   parser->type_definition_forbidden_message
18645     = G_("types may not be defined in exception-declarations");
18646
18647   /* Parse the type-specifier-seq.  */
18648   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18649                                 /*is_trailing_return=*/false,
18650                                 &type_specifiers);
18651   /* If it's a `)', then there is no declarator.  */
18652   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18653     declarator = NULL;
18654   else
18655     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18656                                        /*ctor_dtor_or_conv_p=*/NULL,
18657                                        /*parenthesized_p=*/NULL,
18658                                        /*member_p=*/false);
18659
18660   /* Restore the saved message.  */
18661   parser->type_definition_forbidden_message = saved_message;
18662
18663   if (!type_specifiers.any_specifiers_p)
18664     return error_mark_node;
18665
18666   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18667 }
18668
18669 /* Parse a throw-expression.
18670
18671    throw-expression:
18672      throw assignment-expression [opt]
18673
18674    Returns a THROW_EXPR representing the throw-expression.  */
18675
18676 static tree
18677 cp_parser_throw_expression (cp_parser* parser)
18678 {
18679   tree expression;
18680   cp_token* token;
18681
18682   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18683   token = cp_lexer_peek_token (parser->lexer);
18684   /* Figure out whether or not there is an assignment-expression
18685      following the "throw" keyword.  */
18686   if (token->type == CPP_COMMA
18687       || token->type == CPP_SEMICOLON
18688       || token->type == CPP_CLOSE_PAREN
18689       || token->type == CPP_CLOSE_SQUARE
18690       || token->type == CPP_CLOSE_BRACE
18691       || token->type == CPP_COLON)
18692     expression = NULL_TREE;
18693   else
18694     expression = cp_parser_assignment_expression (parser,
18695                                                   /*cast_p=*/false, NULL);
18696
18697   return build_throw (expression);
18698 }
18699
18700 /* GNU Extensions */
18701
18702 /* Parse an (optional) asm-specification.
18703
18704    asm-specification:
18705      asm ( string-literal )
18706
18707    If the asm-specification is present, returns a STRING_CST
18708    corresponding to the string-literal.  Otherwise, returns
18709    NULL_TREE.  */
18710
18711 static tree
18712 cp_parser_asm_specification_opt (cp_parser* parser)
18713 {
18714   cp_token *token;
18715   tree asm_specification;
18716
18717   /* Peek at the next token.  */
18718   token = cp_lexer_peek_token (parser->lexer);
18719   /* If the next token isn't the `asm' keyword, then there's no
18720      asm-specification.  */
18721   if (!cp_parser_is_keyword (token, RID_ASM))
18722     return NULL_TREE;
18723
18724   /* Consume the `asm' token.  */
18725   cp_lexer_consume_token (parser->lexer);
18726   /* Look for the `('.  */
18727   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18728
18729   /* Look for the string-literal.  */
18730   asm_specification = cp_parser_string_literal (parser, false, false);
18731
18732   /* Look for the `)'.  */
18733   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18734
18735   return asm_specification;
18736 }
18737
18738 /* Parse an asm-operand-list.
18739
18740    asm-operand-list:
18741      asm-operand
18742      asm-operand-list , asm-operand
18743
18744    asm-operand:
18745      string-literal ( expression )
18746      [ string-literal ] string-literal ( expression )
18747
18748    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18749    each node is the expression.  The TREE_PURPOSE is itself a
18750    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18751    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18752    is a STRING_CST for the string literal before the parenthesis. Returns
18753    ERROR_MARK_NODE if any of the operands are invalid.  */
18754
18755 static tree
18756 cp_parser_asm_operand_list (cp_parser* parser)
18757 {
18758   tree asm_operands = NULL_TREE;
18759   bool invalid_operands = false;
18760
18761   while (true)
18762     {
18763       tree string_literal;
18764       tree expression;
18765       tree name;
18766
18767       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18768         {
18769           /* Consume the `[' token.  */
18770           cp_lexer_consume_token (parser->lexer);
18771           /* Read the operand name.  */
18772           name = cp_parser_identifier (parser);
18773           if (name != error_mark_node)
18774             name = build_string (IDENTIFIER_LENGTH (name),
18775                                  IDENTIFIER_POINTER (name));
18776           /* Look for the closing `]'.  */
18777           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18778         }
18779       else
18780         name = NULL_TREE;
18781       /* Look for the string-literal.  */
18782       string_literal = cp_parser_string_literal (parser, false, false);
18783
18784       /* Look for the `('.  */
18785       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18786       /* Parse the expression.  */
18787       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18788       /* Look for the `)'.  */
18789       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18790
18791       if (name == error_mark_node 
18792           || string_literal == error_mark_node 
18793           || expression == error_mark_node)
18794         invalid_operands = true;
18795
18796       /* Add this operand to the list.  */
18797       asm_operands = tree_cons (build_tree_list (name, string_literal),
18798                                 expression,
18799                                 asm_operands);
18800       /* If the next token is not a `,', there are no more
18801          operands.  */
18802       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18803         break;
18804       /* Consume the `,'.  */
18805       cp_lexer_consume_token (parser->lexer);
18806     }
18807
18808   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18809 }
18810
18811 /* Parse an asm-clobber-list.
18812
18813    asm-clobber-list:
18814      string-literal
18815      asm-clobber-list , string-literal
18816
18817    Returns a TREE_LIST, indicating the clobbers in the order that they
18818    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18819
18820 static tree
18821 cp_parser_asm_clobber_list (cp_parser* parser)
18822 {
18823   tree clobbers = NULL_TREE;
18824
18825   while (true)
18826     {
18827       tree string_literal;
18828
18829       /* Look for the string literal.  */
18830       string_literal = cp_parser_string_literal (parser, false, false);
18831       /* Add it to the list.  */
18832       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18833       /* If the next token is not a `,', then the list is
18834          complete.  */
18835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18836         break;
18837       /* Consume the `,' token.  */
18838       cp_lexer_consume_token (parser->lexer);
18839     }
18840
18841   return clobbers;
18842 }
18843
18844 /* Parse an asm-label-list.
18845
18846    asm-label-list:
18847      identifier
18848      asm-label-list , identifier
18849
18850    Returns a TREE_LIST, indicating the labels in the order that they
18851    appeared.  The TREE_VALUE of each node is a label.  */
18852
18853 static tree
18854 cp_parser_asm_label_list (cp_parser* parser)
18855 {
18856   tree labels = NULL_TREE;
18857
18858   while (true)
18859     {
18860       tree identifier, label, name;
18861
18862       /* Look for the identifier.  */
18863       identifier = cp_parser_identifier (parser);
18864       if (!error_operand_p (identifier))
18865         {
18866           label = lookup_label (identifier);
18867           if (TREE_CODE (label) == LABEL_DECL)
18868             {
18869               TREE_USED (label) = 1;
18870               check_goto (label);
18871               name = build_string (IDENTIFIER_LENGTH (identifier),
18872                                    IDENTIFIER_POINTER (identifier));
18873               labels = tree_cons (name, label, labels);
18874             }
18875         }
18876       /* If the next token is not a `,', then the list is
18877          complete.  */
18878       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18879         break;
18880       /* Consume the `,' token.  */
18881       cp_lexer_consume_token (parser->lexer);
18882     }
18883
18884   return nreverse (labels);
18885 }
18886
18887 /* Parse an (optional) series of attributes.
18888
18889    attributes:
18890      attributes attribute
18891
18892    attribute:
18893      __attribute__ (( attribute-list [opt] ))
18894
18895    The return value is as for cp_parser_attribute_list.  */
18896
18897 static tree
18898 cp_parser_attributes_opt (cp_parser* parser)
18899 {
18900   tree attributes = NULL_TREE;
18901
18902   while (true)
18903     {
18904       cp_token *token;
18905       tree attribute_list;
18906
18907       /* Peek at the next token.  */
18908       token = cp_lexer_peek_token (parser->lexer);
18909       /* If it's not `__attribute__', then we're done.  */
18910       if (token->keyword != RID_ATTRIBUTE)
18911         break;
18912
18913       /* Consume the `__attribute__' keyword.  */
18914       cp_lexer_consume_token (parser->lexer);
18915       /* Look for the two `(' tokens.  */
18916       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18917       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18918
18919       /* Peek at the next token.  */
18920       token = cp_lexer_peek_token (parser->lexer);
18921       if (token->type != CPP_CLOSE_PAREN)
18922         /* Parse the attribute-list.  */
18923         attribute_list = cp_parser_attribute_list (parser);
18924       else
18925         /* If the next token is a `)', then there is no attribute
18926            list.  */
18927         attribute_list = NULL;
18928
18929       /* Look for the two `)' tokens.  */
18930       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18931       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18932
18933       /* Add these new attributes to the list.  */
18934       attributes = chainon (attributes, attribute_list);
18935     }
18936
18937   return attributes;
18938 }
18939
18940 /* Parse an attribute-list.
18941
18942    attribute-list:
18943      attribute
18944      attribute-list , attribute
18945
18946    attribute:
18947      identifier
18948      identifier ( identifier )
18949      identifier ( identifier , expression-list )
18950      identifier ( expression-list )
18951
18952    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18953    to an attribute.  The TREE_PURPOSE of each node is the identifier
18954    indicating which attribute is in use.  The TREE_VALUE represents
18955    the arguments, if any.  */
18956
18957 static tree
18958 cp_parser_attribute_list (cp_parser* parser)
18959 {
18960   tree attribute_list = NULL_TREE;
18961   bool save_translate_strings_p = parser->translate_strings_p;
18962
18963   parser->translate_strings_p = false;
18964   while (true)
18965     {
18966       cp_token *token;
18967       tree identifier;
18968       tree attribute;
18969
18970       /* Look for the identifier.  We also allow keywords here; for
18971          example `__attribute__ ((const))' is legal.  */
18972       token = cp_lexer_peek_token (parser->lexer);
18973       if (token->type == CPP_NAME
18974           || token->type == CPP_KEYWORD)
18975         {
18976           tree arguments = NULL_TREE;
18977
18978           /* Consume the token.  */
18979           token = cp_lexer_consume_token (parser->lexer);
18980
18981           /* Save away the identifier that indicates which attribute
18982              this is.  */
18983           identifier = (token->type == CPP_KEYWORD) 
18984             /* For keywords, use the canonical spelling, not the
18985                parsed identifier.  */
18986             ? ridpointers[(int) token->keyword]
18987             : token->u.value;
18988           
18989           attribute = build_tree_list (identifier, NULL_TREE);
18990
18991           /* Peek at the next token.  */
18992           token = cp_lexer_peek_token (parser->lexer);
18993           /* If it's an `(', then parse the attribute arguments.  */
18994           if (token->type == CPP_OPEN_PAREN)
18995             {
18996               VEC(tree,gc) *vec;
18997               int attr_flag = (attribute_takes_identifier_p (identifier)
18998                                ? id_attr : normal_attr);
18999               vec = cp_parser_parenthesized_expression_list
19000                     (parser, attr_flag, /*cast_p=*/false,
19001                      /*allow_expansion_p=*/false,
19002                      /*non_constant_p=*/NULL);
19003               if (vec == NULL)
19004                 arguments = error_mark_node;
19005               else
19006                 {
19007                   arguments = build_tree_list_vec (vec);
19008                   release_tree_vector (vec);
19009                 }
19010               /* Save the arguments away.  */
19011               TREE_VALUE (attribute) = arguments;
19012             }
19013
19014           if (arguments != error_mark_node)
19015             {
19016               /* Add this attribute to the list.  */
19017               TREE_CHAIN (attribute) = attribute_list;
19018               attribute_list = attribute;
19019             }
19020
19021           token = cp_lexer_peek_token (parser->lexer);
19022         }
19023       /* Now, look for more attributes.  If the next token isn't a
19024          `,', we're done.  */
19025       if (token->type != CPP_COMMA)
19026         break;
19027
19028       /* Consume the comma and keep going.  */
19029       cp_lexer_consume_token (parser->lexer);
19030     }
19031   parser->translate_strings_p = save_translate_strings_p;
19032
19033   /* We built up the list in reverse order.  */
19034   return nreverse (attribute_list);
19035 }
19036
19037 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19038    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19039    current value of the PEDANTIC flag, regardless of whether or not
19040    the `__extension__' keyword is present.  The caller is responsible
19041    for restoring the value of the PEDANTIC flag.  */
19042
19043 static bool
19044 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19045 {
19046   /* Save the old value of the PEDANTIC flag.  */
19047   *saved_pedantic = pedantic;
19048
19049   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19050     {
19051       /* Consume the `__extension__' token.  */
19052       cp_lexer_consume_token (parser->lexer);
19053       /* We're not being pedantic while the `__extension__' keyword is
19054          in effect.  */
19055       pedantic = 0;
19056
19057       return true;
19058     }
19059
19060   return false;
19061 }
19062
19063 /* Parse a label declaration.
19064
19065    label-declaration:
19066      __label__ label-declarator-seq ;
19067
19068    label-declarator-seq:
19069      identifier , label-declarator-seq
19070      identifier  */
19071
19072 static void
19073 cp_parser_label_declaration (cp_parser* parser)
19074 {
19075   /* Look for the `__label__' keyword.  */
19076   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19077
19078   while (true)
19079     {
19080       tree identifier;
19081
19082       /* Look for an identifier.  */
19083       identifier = cp_parser_identifier (parser);
19084       /* If we failed, stop.  */
19085       if (identifier == error_mark_node)
19086         break;
19087       /* Declare it as a label.  */
19088       finish_label_decl (identifier);
19089       /* If the next token is a `;', stop.  */
19090       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19091         break;
19092       /* Look for the `,' separating the label declarations.  */
19093       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19094     }
19095
19096   /* Look for the final `;'.  */
19097   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19098 }
19099
19100 /* Support Functions */
19101
19102 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19103    NAME should have one of the representations used for an
19104    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19105    is returned.  If PARSER->SCOPE is a dependent type, then a
19106    SCOPE_REF is returned.
19107
19108    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19109    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19110    was formed.  Abstractly, such entities should not be passed to this
19111    function, because they do not need to be looked up, but it is
19112    simpler to check for this special case here, rather than at the
19113    call-sites.
19114
19115    In cases not explicitly covered above, this function returns a
19116    DECL, OVERLOAD, or baselink representing the result of the lookup.
19117    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19118    is returned.
19119
19120    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19121    (e.g., "struct") that was used.  In that case bindings that do not
19122    refer to types are ignored.
19123
19124    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19125    ignored.
19126
19127    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19128    are ignored.
19129
19130    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19131    types.
19132
19133    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19134    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19135    NULL_TREE otherwise.  */
19136
19137 static tree
19138 cp_parser_lookup_name (cp_parser *parser, tree name,
19139                        enum tag_types tag_type,
19140                        bool is_template,
19141                        bool is_namespace,
19142                        bool check_dependency,
19143                        tree *ambiguous_decls,
19144                        location_t name_location)
19145 {
19146   int flags = 0;
19147   tree decl;
19148   tree object_type = parser->context->object_type;
19149
19150   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19151     flags |= LOOKUP_COMPLAIN;
19152
19153   /* Assume that the lookup will be unambiguous.  */
19154   if (ambiguous_decls)
19155     *ambiguous_decls = NULL_TREE;
19156
19157   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19158      no longer valid.  Note that if we are parsing tentatively, and
19159      the parse fails, OBJECT_TYPE will be automatically restored.  */
19160   parser->context->object_type = NULL_TREE;
19161
19162   if (name == error_mark_node)
19163     return error_mark_node;
19164
19165   /* A template-id has already been resolved; there is no lookup to
19166      do.  */
19167   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19168     return name;
19169   if (BASELINK_P (name))
19170     {
19171       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19172                   == TEMPLATE_ID_EXPR);
19173       return name;
19174     }
19175
19176   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19177      it should already have been checked to make sure that the name
19178      used matches the type being destroyed.  */
19179   if (TREE_CODE (name) == BIT_NOT_EXPR)
19180     {
19181       tree type;
19182
19183       /* Figure out to which type this destructor applies.  */
19184       if (parser->scope)
19185         type = parser->scope;
19186       else if (object_type)
19187         type = object_type;
19188       else
19189         type = current_class_type;
19190       /* If that's not a class type, there is no destructor.  */
19191       if (!type || !CLASS_TYPE_P (type))
19192         return error_mark_node;
19193       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19194         lazily_declare_fn (sfk_destructor, type);
19195       if (!CLASSTYPE_DESTRUCTORS (type))
19196           return error_mark_node;
19197       /* If it was a class type, return the destructor.  */
19198       return CLASSTYPE_DESTRUCTORS (type);
19199     }
19200
19201   /* By this point, the NAME should be an ordinary identifier.  If
19202      the id-expression was a qualified name, the qualifying scope is
19203      stored in PARSER->SCOPE at this point.  */
19204   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19205
19206   /* Perform the lookup.  */
19207   if (parser->scope)
19208     {
19209       bool dependent_p;
19210
19211       if (parser->scope == error_mark_node)
19212         return error_mark_node;
19213
19214       /* If the SCOPE is dependent, the lookup must be deferred until
19215          the template is instantiated -- unless we are explicitly
19216          looking up names in uninstantiated templates.  Even then, we
19217          cannot look up the name if the scope is not a class type; it
19218          might, for example, be a template type parameter.  */
19219       dependent_p = (TYPE_P (parser->scope)
19220                      && dependent_scope_p (parser->scope));
19221       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19222           && dependent_p)
19223         /* Defer lookup.  */
19224         decl = error_mark_node;
19225       else
19226         {
19227           tree pushed_scope = NULL_TREE;
19228
19229           /* If PARSER->SCOPE is a dependent type, then it must be a
19230              class type, and we must not be checking dependencies;
19231              otherwise, we would have processed this lookup above.  So
19232              that PARSER->SCOPE is not considered a dependent base by
19233              lookup_member, we must enter the scope here.  */
19234           if (dependent_p)
19235             pushed_scope = push_scope (parser->scope);
19236
19237           /* If the PARSER->SCOPE is a template specialization, it
19238              may be instantiated during name lookup.  In that case,
19239              errors may be issued.  Even if we rollback the current
19240              tentative parse, those errors are valid.  */
19241           decl = lookup_qualified_name (parser->scope, name,
19242                                         tag_type != none_type,
19243                                         /*complain=*/true);
19244
19245           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19246              lookup result and the nested-name-specifier nominates a class C:
19247                * if the name specified after the nested-name-specifier, when
19248                looked up in C, is the injected-class-name of C (Clause 9), or
19249                * if the name specified after the nested-name-specifier is the
19250                same as the identifier or the simple-template-id's template-
19251                name in the last component of the nested-name-specifier,
19252              the name is instead considered to name the constructor of
19253              class C. [ Note: for example, the constructor is not an
19254              acceptable lookup result in an elaborated-type-specifier so
19255              the constructor would not be used in place of the
19256              injected-class-name. --end note ] Such a constructor name
19257              shall be used only in the declarator-id of a declaration that
19258              names a constructor or in a using-declaration.  */
19259           if (tag_type == none_type
19260               && DECL_SELF_REFERENCE_P (decl)
19261               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19262             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19263                                           tag_type != none_type,
19264                                           /*complain=*/true);
19265
19266           /* If we have a single function from a using decl, pull it out.  */
19267           if (TREE_CODE (decl) == OVERLOAD
19268               && !really_overloaded_fn (decl))
19269             decl = OVL_FUNCTION (decl);
19270
19271           if (pushed_scope)
19272             pop_scope (pushed_scope);
19273         }
19274
19275       /* If the scope is a dependent type and either we deferred lookup or
19276          we did lookup but didn't find the name, rememeber the name.  */
19277       if (decl == error_mark_node && TYPE_P (parser->scope)
19278           && dependent_type_p (parser->scope))
19279         {
19280           if (tag_type)
19281             {
19282               tree type;
19283
19284               /* The resolution to Core Issue 180 says that `struct
19285                  A::B' should be considered a type-name, even if `A'
19286                  is dependent.  */
19287               type = make_typename_type (parser->scope, name, tag_type,
19288                                          /*complain=*/tf_error);
19289               decl = TYPE_NAME (type);
19290             }
19291           else if (is_template
19292                    && (cp_parser_next_token_ends_template_argument_p (parser)
19293                        || cp_lexer_next_token_is (parser->lexer,
19294                                                   CPP_CLOSE_PAREN)))
19295             decl = make_unbound_class_template (parser->scope,
19296                                                 name, NULL_TREE,
19297                                                 /*complain=*/tf_error);
19298           else
19299             decl = build_qualified_name (/*type=*/NULL_TREE,
19300                                          parser->scope, name,
19301                                          is_template);
19302         }
19303       parser->qualifying_scope = parser->scope;
19304       parser->object_scope = NULL_TREE;
19305     }
19306   else if (object_type)
19307     {
19308       tree object_decl = NULL_TREE;
19309       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19310          OBJECT_TYPE is not a class.  */
19311       if (CLASS_TYPE_P (object_type))
19312         /* If the OBJECT_TYPE is a template specialization, it may
19313            be instantiated during name lookup.  In that case, errors
19314            may be issued.  Even if we rollback the current tentative
19315            parse, those errors are valid.  */
19316         object_decl = lookup_member (object_type,
19317                                      name,
19318                                      /*protect=*/0,
19319                                      tag_type != none_type);
19320       /* Look it up in the enclosing context, too.  */
19321       decl = lookup_name_real (name, tag_type != none_type,
19322                                /*nonclass=*/0,
19323                                /*block_p=*/true, is_namespace, flags);
19324       parser->object_scope = object_type;
19325       parser->qualifying_scope = NULL_TREE;
19326       if (object_decl)
19327         decl = object_decl;
19328     }
19329   else
19330     {
19331       decl = lookup_name_real (name, tag_type != none_type,
19332                                /*nonclass=*/0,
19333                                /*block_p=*/true, is_namespace, flags);
19334       parser->qualifying_scope = NULL_TREE;
19335       parser->object_scope = NULL_TREE;
19336     }
19337
19338   /* If the lookup failed, let our caller know.  */
19339   if (!decl || decl == error_mark_node)
19340     return error_mark_node;
19341
19342   /* Pull out the template from an injected-class-name (or multiple).  */
19343   if (is_template)
19344     decl = maybe_get_template_decl_from_type_decl (decl);
19345
19346   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19347   if (TREE_CODE (decl) == TREE_LIST)
19348     {
19349       if (ambiguous_decls)
19350         *ambiguous_decls = decl;
19351       /* The error message we have to print is too complicated for
19352          cp_parser_error, so we incorporate its actions directly.  */
19353       if (!cp_parser_simulate_error (parser))
19354         {
19355           error_at (name_location, "reference to %qD is ambiguous",
19356                     name);
19357           print_candidates (decl);
19358         }
19359       return error_mark_node;
19360     }
19361
19362   gcc_assert (DECL_P (decl)
19363               || TREE_CODE (decl) == OVERLOAD
19364               || TREE_CODE (decl) == SCOPE_REF
19365               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19366               || BASELINK_P (decl));
19367
19368   /* If we have resolved the name of a member declaration, check to
19369      see if the declaration is accessible.  When the name resolves to
19370      set of overloaded functions, accessibility is checked when
19371      overload resolution is done.
19372
19373      During an explicit instantiation, access is not checked at all,
19374      as per [temp.explicit].  */
19375   if (DECL_P (decl))
19376     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19377
19378   return decl;
19379 }
19380
19381 /* Like cp_parser_lookup_name, but for use in the typical case where
19382    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19383    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19384
19385 static tree
19386 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19387 {
19388   return cp_parser_lookup_name (parser, name,
19389                                 none_type,
19390                                 /*is_template=*/false,
19391                                 /*is_namespace=*/false,
19392                                 /*check_dependency=*/true,
19393                                 /*ambiguous_decls=*/NULL,
19394                                 location);
19395 }
19396
19397 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19398    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19399    true, the DECL indicates the class being defined in a class-head,
19400    or declared in an elaborated-type-specifier.
19401
19402    Otherwise, return DECL.  */
19403
19404 static tree
19405 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19406 {
19407   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19408      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19409
19410        struct A {
19411          template <typename T> struct B;
19412        };
19413
19414        template <typename T> struct A::B {};
19415
19416      Similarly, in an elaborated-type-specifier:
19417
19418        namespace N { struct X{}; }
19419
19420        struct A {
19421          template <typename T> friend struct N::X;
19422        };
19423
19424      However, if the DECL refers to a class type, and we are in
19425      the scope of the class, then the name lookup automatically
19426      finds the TYPE_DECL created by build_self_reference rather
19427      than a TEMPLATE_DECL.  For example, in:
19428
19429        template <class T> struct S {
19430          S s;
19431        };
19432
19433      there is no need to handle such case.  */
19434
19435   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19436     return DECL_TEMPLATE_RESULT (decl);
19437
19438   return decl;
19439 }
19440
19441 /* If too many, or too few, template-parameter lists apply to the
19442    declarator, issue an error message.  Returns TRUE if all went well,
19443    and FALSE otherwise.  */
19444
19445 static bool
19446 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19447                                                 cp_declarator *declarator,
19448                                                 location_t declarator_location)
19449 {
19450   unsigned num_templates;
19451
19452   /* We haven't seen any classes that involve template parameters yet.  */
19453   num_templates = 0;
19454
19455   switch (declarator->kind)
19456     {
19457     case cdk_id:
19458       if (declarator->u.id.qualifying_scope)
19459         {
19460           tree scope;
19461
19462           scope = declarator->u.id.qualifying_scope;
19463
19464           while (scope && CLASS_TYPE_P (scope))
19465             {
19466               /* You're supposed to have one `template <...>'
19467                  for every template class, but you don't need one
19468                  for a full specialization.  For example:
19469
19470                  template <class T> struct S{};
19471                  template <> struct S<int> { void f(); };
19472                  void S<int>::f () {}
19473
19474                  is correct; there shouldn't be a `template <>' for
19475                  the definition of `S<int>::f'.  */
19476               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19477                 /* If SCOPE does not have template information of any
19478                    kind, then it is not a template, nor is it nested
19479                    within a template.  */
19480                 break;
19481               if (explicit_class_specialization_p (scope))
19482                 break;
19483               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19484                 ++num_templates;
19485
19486               scope = TYPE_CONTEXT (scope);
19487             }
19488         }
19489       else if (TREE_CODE (declarator->u.id.unqualified_name)
19490                == TEMPLATE_ID_EXPR)
19491         /* If the DECLARATOR has the form `X<y>' then it uses one
19492            additional level of template parameters.  */
19493         ++num_templates;
19494
19495       return cp_parser_check_template_parameters 
19496         (parser, num_templates, declarator_location, declarator);
19497
19498
19499     case cdk_function:
19500     case cdk_array:
19501     case cdk_pointer:
19502     case cdk_reference:
19503     case cdk_ptrmem:
19504       return (cp_parser_check_declarator_template_parameters
19505               (parser, declarator->declarator, declarator_location));
19506
19507     case cdk_error:
19508       return true;
19509
19510     default:
19511       gcc_unreachable ();
19512     }
19513   return false;
19514 }
19515
19516 /* NUM_TEMPLATES were used in the current declaration.  If that is
19517    invalid, return FALSE and issue an error messages.  Otherwise,
19518    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19519    declarator and we can print more accurate diagnostics.  */
19520
19521 static bool
19522 cp_parser_check_template_parameters (cp_parser* parser,
19523                                      unsigned num_templates,
19524                                      location_t location,
19525                                      cp_declarator *declarator)
19526 {
19527   /* If there are the same number of template classes and parameter
19528      lists, that's OK.  */
19529   if (parser->num_template_parameter_lists == num_templates)
19530     return true;
19531   /* If there are more, but only one more, then we are referring to a
19532      member template.  That's OK too.  */
19533   if (parser->num_template_parameter_lists == num_templates + 1)
19534     return true;
19535   /* If there are more template classes than parameter lists, we have
19536      something like:
19537
19538        template <class T> void S<T>::R<T>::f ();  */
19539   if (parser->num_template_parameter_lists < num_templates)
19540     {
19541       if (declarator && !current_function_decl)
19542         error_at (location, "specializing member %<%T::%E%> "
19543                   "requires %<template<>%> syntax", 
19544                   declarator->u.id.qualifying_scope,
19545                   declarator->u.id.unqualified_name);
19546       else if (declarator)
19547         error_at (location, "invalid declaration of %<%T::%E%>",
19548                   declarator->u.id.qualifying_scope,
19549                   declarator->u.id.unqualified_name);
19550       else 
19551         error_at (location, "too few template-parameter-lists");
19552       return false;
19553     }
19554   /* Otherwise, there are too many template parameter lists.  We have
19555      something like:
19556
19557      template <class T> template <class U> void S::f();  */
19558   error_at (location, "too many template-parameter-lists");
19559   return false;
19560 }
19561
19562 /* Parse an optional `::' token indicating that the following name is
19563    from the global namespace.  If so, PARSER->SCOPE is set to the
19564    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19565    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19566    Returns the new value of PARSER->SCOPE, if the `::' token is
19567    present, and NULL_TREE otherwise.  */
19568
19569 static tree
19570 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19571 {
19572   cp_token *token;
19573
19574   /* Peek at the next token.  */
19575   token = cp_lexer_peek_token (parser->lexer);
19576   /* If we're looking at a `::' token then we're starting from the
19577      global namespace, not our current location.  */
19578   if (token->type == CPP_SCOPE)
19579     {
19580       /* Consume the `::' token.  */
19581       cp_lexer_consume_token (parser->lexer);
19582       /* Set the SCOPE so that we know where to start the lookup.  */
19583       parser->scope = global_namespace;
19584       parser->qualifying_scope = global_namespace;
19585       parser->object_scope = NULL_TREE;
19586
19587       return parser->scope;
19588     }
19589   else if (!current_scope_valid_p)
19590     {
19591       parser->scope = NULL_TREE;
19592       parser->qualifying_scope = NULL_TREE;
19593       parser->object_scope = NULL_TREE;
19594     }
19595
19596   return NULL_TREE;
19597 }
19598
19599 /* Returns TRUE if the upcoming token sequence is the start of a
19600    constructor declarator.  If FRIEND_P is true, the declarator is
19601    preceded by the `friend' specifier.  */
19602
19603 static bool
19604 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19605 {
19606   bool constructor_p;
19607   tree nested_name_specifier;
19608   cp_token *next_token;
19609
19610   /* The common case is that this is not a constructor declarator, so
19611      try to avoid doing lots of work if at all possible.  It's not
19612      valid declare a constructor at function scope.  */
19613   if (parser->in_function_body)
19614     return false;
19615   /* And only certain tokens can begin a constructor declarator.  */
19616   next_token = cp_lexer_peek_token (parser->lexer);
19617   if (next_token->type != CPP_NAME
19618       && next_token->type != CPP_SCOPE
19619       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19620       && next_token->type != CPP_TEMPLATE_ID)
19621     return false;
19622
19623   /* Parse tentatively; we are going to roll back all of the tokens
19624      consumed here.  */
19625   cp_parser_parse_tentatively (parser);
19626   /* Assume that we are looking at a constructor declarator.  */
19627   constructor_p = true;
19628
19629   /* Look for the optional `::' operator.  */
19630   cp_parser_global_scope_opt (parser,
19631                               /*current_scope_valid_p=*/false);
19632   /* Look for the nested-name-specifier.  */
19633   nested_name_specifier
19634     = (cp_parser_nested_name_specifier_opt (parser,
19635                                             /*typename_keyword_p=*/false,
19636                                             /*check_dependency_p=*/false,
19637                                             /*type_p=*/false,
19638                                             /*is_declaration=*/false));
19639   /* Outside of a class-specifier, there must be a
19640      nested-name-specifier.  */
19641   if (!nested_name_specifier &&
19642       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19643        || friend_p))
19644     constructor_p = false;
19645   else if (nested_name_specifier == error_mark_node)
19646     constructor_p = false;
19647
19648   /* If we have a class scope, this is easy; DR 147 says that S::S always
19649      names the constructor, and no other qualified name could.  */
19650   if (constructor_p && nested_name_specifier
19651       && CLASS_TYPE_P (nested_name_specifier))
19652     {
19653       tree id = cp_parser_unqualified_id (parser,
19654                                           /*template_keyword_p=*/false,
19655                                           /*check_dependency_p=*/false,
19656                                           /*declarator_p=*/true,
19657                                           /*optional_p=*/false);
19658       if (is_overloaded_fn (id))
19659         id = DECL_NAME (get_first_fn (id));
19660       if (!constructor_name_p (id, nested_name_specifier))
19661         constructor_p = false;
19662     }
19663   /* If we still think that this might be a constructor-declarator,
19664      look for a class-name.  */
19665   else if (constructor_p)
19666     {
19667       /* If we have:
19668
19669            template <typename T> struct S {
19670              S();
19671            };
19672
19673          we must recognize that the nested `S' names a class.  */
19674       tree type_decl;
19675       type_decl = cp_parser_class_name (parser,
19676                                         /*typename_keyword_p=*/false,
19677                                         /*template_keyword_p=*/false,
19678                                         none_type,
19679                                         /*check_dependency_p=*/false,
19680                                         /*class_head_p=*/false,
19681                                         /*is_declaration=*/false);
19682       /* If there was no class-name, then this is not a constructor.  */
19683       constructor_p = !cp_parser_error_occurred (parser);
19684
19685       /* If we're still considering a constructor, we have to see a `(',
19686          to begin the parameter-declaration-clause, followed by either a
19687          `)', an `...', or a decl-specifier.  We need to check for a
19688          type-specifier to avoid being fooled into thinking that:
19689
19690            S (f) (int);
19691
19692          is a constructor.  (It is actually a function named `f' that
19693          takes one parameter (of type `int') and returns a value of type
19694          `S'.  */
19695       if (constructor_p
19696           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19697         constructor_p = false;
19698
19699       if (constructor_p
19700           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19701           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19702           /* A parameter declaration begins with a decl-specifier,
19703              which is either the "attribute" keyword, a storage class
19704              specifier, or (usually) a type-specifier.  */
19705           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19706         {
19707           tree type;
19708           tree pushed_scope = NULL_TREE;
19709           unsigned saved_num_template_parameter_lists;
19710
19711           /* Names appearing in the type-specifier should be looked up
19712              in the scope of the class.  */
19713           if (current_class_type)
19714             type = NULL_TREE;
19715           else
19716             {
19717               type = TREE_TYPE (type_decl);
19718               if (TREE_CODE (type) == TYPENAME_TYPE)
19719                 {
19720                   type = resolve_typename_type (type,
19721                                                 /*only_current_p=*/false);
19722                   if (TREE_CODE (type) == TYPENAME_TYPE)
19723                     {
19724                       cp_parser_abort_tentative_parse (parser);
19725                       return false;
19726                     }
19727                 }
19728               pushed_scope = push_scope (type);
19729             }
19730
19731           /* Inside the constructor parameter list, surrounding
19732              template-parameter-lists do not apply.  */
19733           saved_num_template_parameter_lists
19734             = parser->num_template_parameter_lists;
19735           parser->num_template_parameter_lists = 0;
19736
19737           /* Look for the type-specifier.  */
19738           cp_parser_type_specifier (parser,
19739                                     CP_PARSER_FLAGS_NONE,
19740                                     /*decl_specs=*/NULL,
19741                                     /*is_declarator=*/true,
19742                                     /*declares_class_or_enum=*/NULL,
19743                                     /*is_cv_qualifier=*/NULL);
19744
19745           parser->num_template_parameter_lists
19746             = saved_num_template_parameter_lists;
19747
19748           /* Leave the scope of the class.  */
19749           if (pushed_scope)
19750             pop_scope (pushed_scope);
19751
19752           constructor_p = !cp_parser_error_occurred (parser);
19753         }
19754     }
19755
19756   /* We did not really want to consume any tokens.  */
19757   cp_parser_abort_tentative_parse (parser);
19758
19759   return constructor_p;
19760 }
19761
19762 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19763    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19764    they must be performed once we are in the scope of the function.
19765
19766    Returns the function defined.  */
19767
19768 static tree
19769 cp_parser_function_definition_from_specifiers_and_declarator
19770   (cp_parser* parser,
19771    cp_decl_specifier_seq *decl_specifiers,
19772    tree attributes,
19773    const cp_declarator *declarator)
19774 {
19775   tree fn;
19776   bool success_p;
19777
19778   /* Begin the function-definition.  */
19779   success_p = start_function (decl_specifiers, declarator, attributes);
19780
19781   /* The things we're about to see are not directly qualified by any
19782      template headers we've seen thus far.  */
19783   reset_specialization ();
19784
19785   /* If there were names looked up in the decl-specifier-seq that we
19786      did not check, check them now.  We must wait until we are in the
19787      scope of the function to perform the checks, since the function
19788      might be a friend.  */
19789   perform_deferred_access_checks ();
19790
19791   if (!success_p)
19792     {
19793       /* Skip the entire function.  */
19794       cp_parser_skip_to_end_of_block_or_statement (parser);
19795       fn = error_mark_node;
19796     }
19797   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19798     {
19799       /* Seen already, skip it.  An error message has already been output.  */
19800       cp_parser_skip_to_end_of_block_or_statement (parser);
19801       fn = current_function_decl;
19802       current_function_decl = NULL_TREE;
19803       /* If this is a function from a class, pop the nested class.  */
19804       if (current_class_name)
19805         pop_nested_class ();
19806     }
19807   else
19808     {
19809       timevar_id_t tv;
19810       if (DECL_DECLARED_INLINE_P (current_function_decl))
19811         tv = TV_PARSE_INLINE;
19812       else
19813         tv = TV_PARSE_FUNC;
19814       timevar_push (tv);
19815       fn = cp_parser_function_definition_after_declarator (parser,
19816                                                          /*inline_p=*/false);
19817       timevar_pop (tv);
19818     }
19819
19820   return fn;
19821 }
19822
19823 /* Parse the part of a function-definition that follows the
19824    declarator.  INLINE_P is TRUE iff this function is an inline
19825    function defined within a class-specifier.
19826
19827    Returns the function defined.  */
19828
19829 static tree
19830 cp_parser_function_definition_after_declarator (cp_parser* parser,
19831                                                 bool inline_p)
19832 {
19833   tree fn;
19834   bool ctor_initializer_p = false;
19835   bool saved_in_unbraced_linkage_specification_p;
19836   bool saved_in_function_body;
19837   unsigned saved_num_template_parameter_lists;
19838   cp_token *token;
19839
19840   saved_in_function_body = parser->in_function_body;
19841   parser->in_function_body = true;
19842   /* If the next token is `return', then the code may be trying to
19843      make use of the "named return value" extension that G++ used to
19844      support.  */
19845   token = cp_lexer_peek_token (parser->lexer);
19846   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19847     {
19848       /* Consume the `return' keyword.  */
19849       cp_lexer_consume_token (parser->lexer);
19850       /* Look for the identifier that indicates what value is to be
19851          returned.  */
19852       cp_parser_identifier (parser);
19853       /* Issue an error message.  */
19854       error_at (token->location,
19855                 "named return values are no longer supported");
19856       /* Skip tokens until we reach the start of the function body.  */
19857       while (true)
19858         {
19859           cp_token *token = cp_lexer_peek_token (parser->lexer);
19860           if (token->type == CPP_OPEN_BRACE
19861               || token->type == CPP_EOF
19862               || token->type == CPP_PRAGMA_EOL)
19863             break;
19864           cp_lexer_consume_token (parser->lexer);
19865         }
19866     }
19867   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19868      anything declared inside `f'.  */
19869   saved_in_unbraced_linkage_specification_p
19870     = parser->in_unbraced_linkage_specification_p;
19871   parser->in_unbraced_linkage_specification_p = false;
19872   /* Inside the function, surrounding template-parameter-lists do not
19873      apply.  */
19874   saved_num_template_parameter_lists
19875     = parser->num_template_parameter_lists;
19876   parser->num_template_parameter_lists = 0;
19877
19878   start_lambda_scope (current_function_decl);
19879
19880   /* If the next token is `try', then we are looking at a
19881      function-try-block.  */
19882   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19883     ctor_initializer_p = cp_parser_function_try_block (parser);
19884   /* A function-try-block includes the function-body, so we only do
19885      this next part if we're not processing a function-try-block.  */
19886   else
19887     ctor_initializer_p
19888       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19889
19890   finish_lambda_scope ();
19891
19892   /* Finish the function.  */
19893   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19894                         (inline_p ? 2 : 0));
19895   /* Generate code for it, if necessary.  */
19896   expand_or_defer_fn (fn);
19897   /* Restore the saved values.  */
19898   parser->in_unbraced_linkage_specification_p
19899     = saved_in_unbraced_linkage_specification_p;
19900   parser->num_template_parameter_lists
19901     = saved_num_template_parameter_lists;
19902   parser->in_function_body = saved_in_function_body;
19903
19904   return fn;
19905 }
19906
19907 /* Parse a template-declaration, assuming that the `export' (and
19908    `extern') keywords, if present, has already been scanned.  MEMBER_P
19909    is as for cp_parser_template_declaration.  */
19910
19911 static void
19912 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19913 {
19914   tree decl = NULL_TREE;
19915   VEC (deferred_access_check,gc) *checks;
19916   tree parameter_list;
19917   bool friend_p = false;
19918   bool need_lang_pop;
19919   cp_token *token;
19920
19921   /* Look for the `template' keyword.  */
19922   token = cp_lexer_peek_token (parser->lexer);
19923   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19924     return;
19925
19926   /* And the `<'.  */
19927   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19928     return;
19929   if (at_class_scope_p () && current_function_decl)
19930     {
19931       /* 14.5.2.2 [temp.mem]
19932
19933          A local class shall not have member templates.  */
19934       error_at (token->location,
19935                 "invalid declaration of member template in local class");
19936       cp_parser_skip_to_end_of_block_or_statement (parser);
19937       return;
19938     }
19939   /* [temp]
19940
19941      A template ... shall not have C linkage.  */
19942   if (current_lang_name == lang_name_c)
19943     {
19944       error_at (token->location, "template with C linkage");
19945       /* Give it C++ linkage to avoid confusing other parts of the
19946          front end.  */
19947       push_lang_context (lang_name_cplusplus);
19948       need_lang_pop = true;
19949     }
19950   else
19951     need_lang_pop = false;
19952
19953   /* We cannot perform access checks on the template parameter
19954      declarations until we know what is being declared, just as we
19955      cannot check the decl-specifier list.  */
19956   push_deferring_access_checks (dk_deferred);
19957
19958   /* If the next token is `>', then we have an invalid
19959      specialization.  Rather than complain about an invalid template
19960      parameter, issue an error message here.  */
19961   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19962     {
19963       cp_parser_error (parser, "invalid explicit specialization");
19964       begin_specialization ();
19965       parameter_list = NULL_TREE;
19966     }
19967   else
19968     {
19969       /* Parse the template parameters.  */
19970       parameter_list = cp_parser_template_parameter_list (parser);
19971       fixup_template_parms ();
19972     }
19973
19974   /* Get the deferred access checks from the parameter list.  These
19975      will be checked once we know what is being declared, as for a
19976      member template the checks must be performed in the scope of the
19977      class containing the member.  */
19978   checks = get_deferred_access_checks ();
19979
19980   /* Look for the `>'.  */
19981   cp_parser_skip_to_end_of_template_parameter_list (parser);
19982   /* We just processed one more parameter list.  */
19983   ++parser->num_template_parameter_lists;
19984   /* If the next token is `template', there are more template
19985      parameters.  */
19986   if (cp_lexer_next_token_is_keyword (parser->lexer,
19987                                       RID_TEMPLATE))
19988     cp_parser_template_declaration_after_export (parser, member_p);
19989   else
19990     {
19991       /* There are no access checks when parsing a template, as we do not
19992          know if a specialization will be a friend.  */
19993       push_deferring_access_checks (dk_no_check);
19994       token = cp_lexer_peek_token (parser->lexer);
19995       decl = cp_parser_single_declaration (parser,
19996                                            checks,
19997                                            member_p,
19998                                            /*explicit_specialization_p=*/false,
19999                                            &friend_p);
20000       pop_deferring_access_checks ();
20001
20002       /* If this is a member template declaration, let the front
20003          end know.  */
20004       if (member_p && !friend_p && decl)
20005         {
20006           if (TREE_CODE (decl) == TYPE_DECL)
20007             cp_parser_check_access_in_redeclaration (decl, token->location);
20008
20009           decl = finish_member_template_decl (decl);
20010         }
20011       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20012         make_friend_class (current_class_type, TREE_TYPE (decl),
20013                            /*complain=*/true);
20014     }
20015   /* We are done with the current parameter list.  */
20016   --parser->num_template_parameter_lists;
20017
20018   pop_deferring_access_checks ();
20019
20020   /* Finish up.  */
20021   finish_template_decl (parameter_list);
20022
20023   /* Register member declarations.  */
20024   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20025     finish_member_declaration (decl);
20026   /* For the erroneous case of a template with C linkage, we pushed an
20027      implicit C++ linkage scope; exit that scope now.  */
20028   if (need_lang_pop)
20029     pop_lang_context ();
20030   /* If DECL is a function template, we must return to parse it later.
20031      (Even though there is no definition, there might be default
20032      arguments that need handling.)  */
20033   if (member_p && decl
20034       && (TREE_CODE (decl) == FUNCTION_DECL
20035           || DECL_FUNCTION_TEMPLATE_P (decl)))
20036     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20037 }
20038
20039 /* Perform the deferred access checks from a template-parameter-list.
20040    CHECKS is a TREE_LIST of access checks, as returned by
20041    get_deferred_access_checks.  */
20042
20043 static void
20044 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20045 {
20046   ++processing_template_parmlist;
20047   perform_access_checks (checks);
20048   --processing_template_parmlist;
20049 }
20050
20051 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20052    `function-definition' sequence.  MEMBER_P is true, this declaration
20053    appears in a class scope.
20054
20055    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20056    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20057
20058 static tree
20059 cp_parser_single_declaration (cp_parser* parser,
20060                               VEC (deferred_access_check,gc)* checks,
20061                               bool member_p,
20062                               bool explicit_specialization_p,
20063                               bool* friend_p)
20064 {
20065   int declares_class_or_enum;
20066   tree decl = NULL_TREE;
20067   cp_decl_specifier_seq decl_specifiers;
20068   bool function_definition_p = false;
20069   cp_token *decl_spec_token_start;
20070
20071   /* This function is only used when processing a template
20072      declaration.  */
20073   gcc_assert (innermost_scope_kind () == sk_template_parms
20074               || innermost_scope_kind () == sk_template_spec);
20075
20076   /* Defer access checks until we know what is being declared.  */
20077   push_deferring_access_checks (dk_deferred);
20078
20079   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20080      alternative.  */
20081   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20082   cp_parser_decl_specifier_seq (parser,
20083                                 CP_PARSER_FLAGS_OPTIONAL,
20084                                 &decl_specifiers,
20085                                 &declares_class_or_enum);
20086   if (friend_p)
20087     *friend_p = cp_parser_friend_p (&decl_specifiers);
20088
20089   /* There are no template typedefs.  */
20090   if (decl_specifiers.specs[(int) ds_typedef])
20091     {
20092       error_at (decl_spec_token_start->location,
20093                 "template declaration of %<typedef%>");
20094       decl = error_mark_node;
20095     }
20096
20097   /* Gather up the access checks that occurred the
20098      decl-specifier-seq.  */
20099   stop_deferring_access_checks ();
20100
20101   /* Check for the declaration of a template class.  */
20102   if (declares_class_or_enum)
20103     {
20104       if (cp_parser_declares_only_class_p (parser))
20105         {
20106           decl = shadow_tag (&decl_specifiers);
20107
20108           /* In this case:
20109
20110                struct C {
20111                  friend template <typename T> struct A<T>::B;
20112                };
20113
20114              A<T>::B will be represented by a TYPENAME_TYPE, and
20115              therefore not recognized by shadow_tag.  */
20116           if (friend_p && *friend_p
20117               && !decl
20118               && decl_specifiers.type
20119               && TYPE_P (decl_specifiers.type))
20120             decl = decl_specifiers.type;
20121
20122           if (decl && decl != error_mark_node)
20123             decl = TYPE_NAME (decl);
20124           else
20125             decl = error_mark_node;
20126
20127           /* Perform access checks for template parameters.  */
20128           cp_parser_perform_template_parameter_access_checks (checks);
20129         }
20130     }
20131
20132   /* Complain about missing 'typename' or other invalid type names.  */
20133   if (!decl_specifiers.any_type_specifiers_p
20134       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20135     {
20136       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20137          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20138          the rest of this declaration.  */
20139       decl = error_mark_node;
20140       goto out;
20141     }
20142
20143   /* If it's not a template class, try for a template function.  If
20144      the next token is a `;', then this declaration does not declare
20145      anything.  But, if there were errors in the decl-specifiers, then
20146      the error might well have come from an attempted class-specifier.
20147      In that case, there's no need to warn about a missing declarator.  */
20148   if (!decl
20149       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20150           || decl_specifiers.type != error_mark_node))
20151     {
20152       decl = cp_parser_init_declarator (parser,
20153                                         &decl_specifiers,
20154                                         checks,
20155                                         /*function_definition_allowed_p=*/true,
20156                                         member_p,
20157                                         declares_class_or_enum,
20158                                         &function_definition_p,
20159                                         NULL);
20160
20161     /* 7.1.1-1 [dcl.stc]
20162
20163        A storage-class-specifier shall not be specified in an explicit
20164        specialization...  */
20165     if (decl
20166         && explicit_specialization_p
20167         && decl_specifiers.storage_class != sc_none)
20168       {
20169         error_at (decl_spec_token_start->location,
20170                   "explicit template specialization cannot have a storage class");
20171         decl = error_mark_node;
20172       }
20173     }
20174
20175   /* Look for a trailing `;' after the declaration.  */
20176   if (!function_definition_p
20177       && (decl == error_mark_node
20178           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20179     cp_parser_skip_to_end_of_block_or_statement (parser);
20180
20181  out:
20182   pop_deferring_access_checks ();
20183
20184   /* Clear any current qualification; whatever comes next is the start
20185      of something new.  */
20186   parser->scope = NULL_TREE;
20187   parser->qualifying_scope = NULL_TREE;
20188   parser->object_scope = NULL_TREE;
20189
20190   return decl;
20191 }
20192
20193 /* Parse a cast-expression that is not the operand of a unary "&".  */
20194
20195 static tree
20196 cp_parser_simple_cast_expression (cp_parser *parser)
20197 {
20198   return cp_parser_cast_expression (parser, /*address_p=*/false,
20199                                     /*cast_p=*/false, NULL);
20200 }
20201
20202 /* Parse a functional cast to TYPE.  Returns an expression
20203    representing the cast.  */
20204
20205 static tree
20206 cp_parser_functional_cast (cp_parser* parser, tree type)
20207 {
20208   VEC(tree,gc) *vec;
20209   tree expression_list;
20210   tree cast;
20211   bool nonconst_p;
20212
20213   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20214     {
20215       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20216       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20217       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20218       if (TREE_CODE (type) == TYPE_DECL)
20219         type = TREE_TYPE (type);
20220       return finish_compound_literal (type, expression_list,
20221                                       tf_warning_or_error);
20222     }
20223
20224
20225   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20226                                                  /*cast_p=*/true,
20227                                                  /*allow_expansion_p=*/true,
20228                                                  /*non_constant_p=*/NULL);
20229   if (vec == NULL)
20230     expression_list = error_mark_node;
20231   else
20232     {
20233       expression_list = build_tree_list_vec (vec);
20234       release_tree_vector (vec);
20235     }
20236
20237   cast = build_functional_cast (type, expression_list,
20238                                 tf_warning_or_error);
20239   /* [expr.const]/1: In an integral constant expression "only type
20240      conversions to integral or enumeration type can be used".  */
20241   if (TREE_CODE (type) == TYPE_DECL)
20242     type = TREE_TYPE (type);
20243   if (cast != error_mark_node
20244       && !cast_valid_in_integral_constant_expression_p (type)
20245       && cp_parser_non_integral_constant_expression (parser,
20246                                                      NIC_CONSTRUCTOR))
20247     return error_mark_node;
20248   return cast;
20249 }
20250
20251 /* Save the tokens that make up the body of a member function defined
20252    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20253    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20254    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20255    for the member function.  */
20256
20257 static tree
20258 cp_parser_save_member_function_body (cp_parser* parser,
20259                                      cp_decl_specifier_seq *decl_specifiers,
20260                                      cp_declarator *declarator,
20261                                      tree attributes)
20262 {
20263   cp_token *first;
20264   cp_token *last;
20265   tree fn;
20266
20267   /* Create the FUNCTION_DECL.  */
20268   fn = grokmethod (decl_specifiers, declarator, attributes);
20269   /* If something went badly wrong, bail out now.  */
20270   if (fn == error_mark_node)
20271     {
20272       /* If there's a function-body, skip it.  */
20273       if (cp_parser_token_starts_function_definition_p
20274           (cp_lexer_peek_token (parser->lexer)))
20275         cp_parser_skip_to_end_of_block_or_statement (parser);
20276       return error_mark_node;
20277     }
20278
20279   /* Remember it, if there default args to post process.  */
20280   cp_parser_save_default_args (parser, fn);
20281
20282   /* Save away the tokens that make up the body of the
20283      function.  */
20284   first = parser->lexer->next_token;
20285   /* We can have braced-init-list mem-initializers before the fn body.  */
20286   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20287     {
20288       cp_lexer_consume_token (parser->lexer);
20289       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20290              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20291         {
20292           /* cache_group will stop after an un-nested { } pair, too.  */
20293           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20294             break;
20295
20296           /* variadic mem-inits have ... after the ')'.  */
20297           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20298             cp_lexer_consume_token (parser->lexer);
20299         }
20300     }
20301   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20302   /* Handle function try blocks.  */
20303   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20304     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20305   last = parser->lexer->next_token;
20306
20307   /* Save away the inline definition; we will process it when the
20308      class is complete.  */
20309   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20310   DECL_PENDING_INLINE_P (fn) = 1;
20311
20312   /* We need to know that this was defined in the class, so that
20313      friend templates are handled correctly.  */
20314   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20315
20316   /* Add FN to the queue of functions to be parsed later.  */
20317   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20318
20319   return fn;
20320 }
20321
20322 /* Parse a template-argument-list, as well as the trailing ">" (but
20323    not the opening ">").  See cp_parser_template_argument_list for the
20324    return value.  */
20325
20326 static tree
20327 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20328 {
20329   tree arguments;
20330   tree saved_scope;
20331   tree saved_qualifying_scope;
20332   tree saved_object_scope;
20333   bool saved_greater_than_is_operator_p;
20334   int saved_unevaluated_operand;
20335   int saved_inhibit_evaluation_warnings;
20336
20337   /* [temp.names]
20338
20339      When parsing a template-id, the first non-nested `>' is taken as
20340      the end of the template-argument-list rather than a greater-than
20341      operator.  */
20342   saved_greater_than_is_operator_p
20343     = parser->greater_than_is_operator_p;
20344   parser->greater_than_is_operator_p = false;
20345   /* Parsing the argument list may modify SCOPE, so we save it
20346      here.  */
20347   saved_scope = parser->scope;
20348   saved_qualifying_scope = parser->qualifying_scope;
20349   saved_object_scope = parser->object_scope;
20350   /* We need to evaluate the template arguments, even though this
20351      template-id may be nested within a "sizeof".  */
20352   saved_unevaluated_operand = cp_unevaluated_operand;
20353   cp_unevaluated_operand = 0;
20354   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20355   c_inhibit_evaluation_warnings = 0;
20356   /* Parse the template-argument-list itself.  */
20357   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20358       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20359     arguments = NULL_TREE;
20360   else
20361     arguments = cp_parser_template_argument_list (parser);
20362   /* Look for the `>' that ends the template-argument-list. If we find
20363      a '>>' instead, it's probably just a typo.  */
20364   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20365     {
20366       if (cxx_dialect != cxx98)
20367         {
20368           /* In C++0x, a `>>' in a template argument list or cast
20369              expression is considered to be two separate `>'
20370              tokens. So, change the current token to a `>', but don't
20371              consume it: it will be consumed later when the outer
20372              template argument list (or cast expression) is parsed.
20373              Note that this replacement of `>' for `>>' is necessary
20374              even if we are parsing tentatively: in the tentative
20375              case, after calling
20376              cp_parser_enclosed_template_argument_list we will always
20377              throw away all of the template arguments and the first
20378              closing `>', either because the template argument list
20379              was erroneous or because we are replacing those tokens
20380              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20381              not have been thrown away) is needed either to close an
20382              outer template argument list or to complete a new-style
20383              cast.  */
20384           cp_token *token = cp_lexer_peek_token (parser->lexer);
20385           token->type = CPP_GREATER;
20386         }
20387       else if (!saved_greater_than_is_operator_p)
20388         {
20389           /* If we're in a nested template argument list, the '>>' has
20390             to be a typo for '> >'. We emit the error message, but we
20391             continue parsing and we push a '>' as next token, so that
20392             the argument list will be parsed correctly.  Note that the
20393             global source location is still on the token before the
20394             '>>', so we need to say explicitly where we want it.  */
20395           cp_token *token = cp_lexer_peek_token (parser->lexer);
20396           error_at (token->location, "%<>>%> should be %<> >%> "
20397                     "within a nested template argument list");
20398
20399           token->type = CPP_GREATER;
20400         }
20401       else
20402         {
20403           /* If this is not a nested template argument list, the '>>'
20404             is a typo for '>'. Emit an error message and continue.
20405             Same deal about the token location, but here we can get it
20406             right by consuming the '>>' before issuing the diagnostic.  */
20407           cp_token *token = cp_lexer_consume_token (parser->lexer);
20408           error_at (token->location,
20409                     "spurious %<>>%>, use %<>%> to terminate "
20410                     "a template argument list");
20411         }
20412     }
20413   else
20414     cp_parser_skip_to_end_of_template_parameter_list (parser);
20415   /* The `>' token might be a greater-than operator again now.  */
20416   parser->greater_than_is_operator_p
20417     = saved_greater_than_is_operator_p;
20418   /* Restore the SAVED_SCOPE.  */
20419   parser->scope = saved_scope;
20420   parser->qualifying_scope = saved_qualifying_scope;
20421   parser->object_scope = saved_object_scope;
20422   cp_unevaluated_operand = saved_unevaluated_operand;
20423   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20424
20425   return arguments;
20426 }
20427
20428 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20429    arguments, or the body of the function have not yet been parsed,
20430    parse them now.  */
20431
20432 static void
20433 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20434 {
20435   timevar_push (TV_PARSE_INMETH);
20436   /* If this member is a template, get the underlying
20437      FUNCTION_DECL.  */
20438   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20439     member_function = DECL_TEMPLATE_RESULT (member_function);
20440
20441   /* There should not be any class definitions in progress at this
20442      point; the bodies of members are only parsed outside of all class
20443      definitions.  */
20444   gcc_assert (parser->num_classes_being_defined == 0);
20445   /* While we're parsing the member functions we might encounter more
20446      classes.  We want to handle them right away, but we don't want
20447      them getting mixed up with functions that are currently in the
20448      queue.  */
20449   push_unparsed_function_queues (parser);
20450
20451   /* Make sure that any template parameters are in scope.  */
20452   maybe_begin_member_template_processing (member_function);
20453
20454   /* If the body of the function has not yet been parsed, parse it
20455      now.  */
20456   if (DECL_PENDING_INLINE_P (member_function))
20457     {
20458       tree function_scope;
20459       cp_token_cache *tokens;
20460
20461       /* The function is no longer pending; we are processing it.  */
20462       tokens = DECL_PENDING_INLINE_INFO (member_function);
20463       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20464       DECL_PENDING_INLINE_P (member_function) = 0;
20465
20466       /* If this is a local class, enter the scope of the containing
20467          function.  */
20468       function_scope = current_function_decl;
20469       if (function_scope)
20470         push_function_context ();
20471
20472       /* Push the body of the function onto the lexer stack.  */
20473       cp_parser_push_lexer_for_tokens (parser, tokens);
20474
20475       /* Let the front end know that we going to be defining this
20476          function.  */
20477       start_preparsed_function (member_function, NULL_TREE,
20478                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20479
20480       /* Don't do access checking if it is a templated function.  */
20481       if (processing_template_decl)
20482         push_deferring_access_checks (dk_no_check);
20483
20484       /* Now, parse the body of the function.  */
20485       cp_parser_function_definition_after_declarator (parser,
20486                                                       /*inline_p=*/true);
20487
20488       if (processing_template_decl)
20489         pop_deferring_access_checks ();
20490
20491       /* Leave the scope of the containing function.  */
20492       if (function_scope)
20493         pop_function_context ();
20494       cp_parser_pop_lexer (parser);
20495     }
20496
20497   /* Remove any template parameters from the symbol table.  */
20498   maybe_end_member_template_processing ();
20499
20500   /* Restore the queue.  */
20501   pop_unparsed_function_queues (parser);
20502   timevar_pop (TV_PARSE_INMETH);
20503 }
20504
20505 /* If DECL contains any default args, remember it on the unparsed
20506    functions queue.  */
20507
20508 static void
20509 cp_parser_save_default_args (cp_parser* parser, tree decl)
20510 {
20511   tree probe;
20512
20513   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20514        probe;
20515        probe = TREE_CHAIN (probe))
20516     if (TREE_PURPOSE (probe))
20517       {
20518         cp_default_arg_entry *entry
20519           = VEC_safe_push (cp_default_arg_entry, gc,
20520                            unparsed_funs_with_default_args, NULL);
20521         entry->class_type = current_class_type;
20522         entry->decl = decl;
20523         break;
20524       }
20525 }
20526
20527 /* FN is a FUNCTION_DECL which may contains a parameter with an
20528    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20529    assumes that the current scope is the scope in which the default
20530    argument should be processed.  */
20531
20532 static void
20533 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20534 {
20535   bool saved_local_variables_forbidden_p;
20536   tree parm, parmdecl;
20537
20538   /* While we're parsing the default args, we might (due to the
20539      statement expression extension) encounter more classes.  We want
20540      to handle them right away, but we don't want them getting mixed
20541      up with default args that are currently in the queue.  */
20542   push_unparsed_function_queues (parser);
20543
20544   /* Local variable names (and the `this' keyword) may not appear
20545      in a default argument.  */
20546   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20547   parser->local_variables_forbidden_p = true;
20548
20549   push_defarg_context (fn);
20550
20551   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20552          parmdecl = DECL_ARGUMENTS (fn);
20553        parm && parm != void_list_node;
20554        parm = TREE_CHAIN (parm),
20555          parmdecl = DECL_CHAIN (parmdecl))
20556     {
20557       cp_token_cache *tokens;
20558       tree default_arg = TREE_PURPOSE (parm);
20559       tree parsed_arg;
20560       VEC(tree,gc) *insts;
20561       tree copy;
20562       unsigned ix;
20563
20564       if (!default_arg)
20565         continue;
20566
20567       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20568         /* This can happen for a friend declaration for a function
20569            already declared with default arguments.  */
20570         continue;
20571
20572        /* Push the saved tokens for the default argument onto the parser's
20573           lexer stack.  */
20574       tokens = DEFARG_TOKENS (default_arg);
20575       cp_parser_push_lexer_for_tokens (parser, tokens);
20576
20577       start_lambda_scope (parmdecl);
20578
20579       /* Parse the assignment-expression.  */
20580       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20581       if (parsed_arg == error_mark_node)
20582         {
20583           cp_parser_pop_lexer (parser);
20584           continue;
20585         }
20586
20587       if (!processing_template_decl)
20588         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20589
20590       TREE_PURPOSE (parm) = parsed_arg;
20591
20592       /* Update any instantiations we've already created.  */
20593       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20594            VEC_iterate (tree, insts, ix, copy); ix++)
20595         TREE_PURPOSE (copy) = parsed_arg;
20596
20597       finish_lambda_scope ();
20598
20599       /* If the token stream has not been completely used up, then
20600          there was extra junk after the end of the default
20601          argument.  */
20602       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20603         cp_parser_error (parser, "expected %<,%>");
20604
20605       /* Revert to the main lexer.  */
20606       cp_parser_pop_lexer (parser);
20607     }
20608
20609   pop_defarg_context ();
20610
20611   /* Make sure no default arg is missing.  */
20612   check_default_args (fn);
20613
20614   /* Restore the state of local_variables_forbidden_p.  */
20615   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20616
20617   /* Restore the queue.  */
20618   pop_unparsed_function_queues (parser);
20619 }
20620
20621 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20622    either a TYPE or an expression, depending on the form of the
20623    input.  The KEYWORD indicates which kind of expression we have
20624    encountered.  */
20625
20626 static tree
20627 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20628 {
20629   tree expr = NULL_TREE;
20630   const char *saved_message;
20631   char *tmp;
20632   bool saved_integral_constant_expression_p;
20633   bool saved_non_integral_constant_expression_p;
20634   bool pack_expansion_p = false;
20635
20636   /* Types cannot be defined in a `sizeof' expression.  Save away the
20637      old message.  */
20638   saved_message = parser->type_definition_forbidden_message;
20639   /* And create the new one.  */
20640   tmp = concat ("types may not be defined in %<",
20641                 IDENTIFIER_POINTER (ridpointers[keyword]),
20642                 "%> expressions", NULL);
20643   parser->type_definition_forbidden_message = tmp;
20644
20645   /* The restrictions on constant-expressions do not apply inside
20646      sizeof expressions.  */
20647   saved_integral_constant_expression_p
20648     = parser->integral_constant_expression_p;
20649   saved_non_integral_constant_expression_p
20650     = parser->non_integral_constant_expression_p;
20651   parser->integral_constant_expression_p = false;
20652
20653   /* If it's a `...', then we are computing the length of a parameter
20654      pack.  */
20655   if (keyword == RID_SIZEOF
20656       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20657     {
20658       /* Consume the `...'.  */
20659       cp_lexer_consume_token (parser->lexer);
20660       maybe_warn_variadic_templates ();
20661
20662       /* Note that this is an expansion.  */
20663       pack_expansion_p = true;
20664     }
20665
20666   /* Do not actually evaluate the expression.  */
20667   ++cp_unevaluated_operand;
20668   ++c_inhibit_evaluation_warnings;
20669   /* If it's a `(', then we might be looking at the type-id
20670      construction.  */
20671   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20672     {
20673       tree type;
20674       bool saved_in_type_id_in_expr_p;
20675
20676       /* We can't be sure yet whether we're looking at a type-id or an
20677          expression.  */
20678       cp_parser_parse_tentatively (parser);
20679       /* Consume the `('.  */
20680       cp_lexer_consume_token (parser->lexer);
20681       /* Parse the type-id.  */
20682       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20683       parser->in_type_id_in_expr_p = true;
20684       type = cp_parser_type_id (parser);
20685       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20686       /* Now, look for the trailing `)'.  */
20687       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20688       /* If all went well, then we're done.  */
20689       if (cp_parser_parse_definitely (parser))
20690         {
20691           cp_decl_specifier_seq decl_specs;
20692
20693           /* Build a trivial decl-specifier-seq.  */
20694           clear_decl_specs (&decl_specs);
20695           decl_specs.type = type;
20696
20697           /* Call grokdeclarator to figure out what type this is.  */
20698           expr = grokdeclarator (NULL,
20699                                  &decl_specs,
20700                                  TYPENAME,
20701                                  /*initialized=*/0,
20702                                  /*attrlist=*/NULL);
20703         }
20704     }
20705
20706   /* If the type-id production did not work out, then we must be
20707      looking at the unary-expression production.  */
20708   if (!expr)
20709     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20710                                        /*cast_p=*/false, NULL);
20711
20712   if (pack_expansion_p)
20713     /* Build a pack expansion. */
20714     expr = make_pack_expansion (expr);
20715
20716   /* Go back to evaluating expressions.  */
20717   --cp_unevaluated_operand;
20718   --c_inhibit_evaluation_warnings;
20719
20720   /* Free the message we created.  */
20721   free (tmp);
20722   /* And restore the old one.  */
20723   parser->type_definition_forbidden_message = saved_message;
20724   parser->integral_constant_expression_p
20725     = saved_integral_constant_expression_p;
20726   parser->non_integral_constant_expression_p
20727     = saved_non_integral_constant_expression_p;
20728
20729   return expr;
20730 }
20731
20732 /* If the current declaration has no declarator, return true.  */
20733
20734 static bool
20735 cp_parser_declares_only_class_p (cp_parser *parser)
20736 {
20737   /* If the next token is a `;' or a `,' then there is no
20738      declarator.  */
20739   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20740           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20741 }
20742
20743 /* Update the DECL_SPECS to reflect the storage class indicated by
20744    KEYWORD.  */
20745
20746 static void
20747 cp_parser_set_storage_class (cp_parser *parser,
20748                              cp_decl_specifier_seq *decl_specs,
20749                              enum rid keyword,
20750                              location_t location)
20751 {
20752   cp_storage_class storage_class;
20753
20754   if (parser->in_unbraced_linkage_specification_p)
20755     {
20756       error_at (location, "invalid use of %qD in linkage specification",
20757                 ridpointers[keyword]);
20758       return;
20759     }
20760   else if (decl_specs->storage_class != sc_none)
20761     {
20762       decl_specs->conflicting_specifiers_p = true;
20763       return;
20764     }
20765
20766   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20767       && decl_specs->specs[(int) ds_thread])
20768     {
20769       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20770       decl_specs->specs[(int) ds_thread] = 0;
20771     }
20772
20773   switch (keyword)
20774     {
20775     case RID_AUTO:
20776       storage_class = sc_auto;
20777       break;
20778     case RID_REGISTER:
20779       storage_class = sc_register;
20780       break;
20781     case RID_STATIC:
20782       storage_class = sc_static;
20783       break;
20784     case RID_EXTERN:
20785       storage_class = sc_extern;
20786       break;
20787     case RID_MUTABLE:
20788       storage_class = sc_mutable;
20789       break;
20790     default:
20791       gcc_unreachable ();
20792     }
20793   decl_specs->storage_class = storage_class;
20794
20795   /* A storage class specifier cannot be applied alongside a typedef 
20796      specifier. If there is a typedef specifier present then set 
20797      conflicting_specifiers_p which will trigger an error later
20798      on in grokdeclarator. */
20799   if (decl_specs->specs[(int)ds_typedef])
20800     decl_specs->conflicting_specifiers_p = true;
20801 }
20802
20803 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20804    is true, the type is a user-defined type; otherwise it is a
20805    built-in type specified by a keyword.  */
20806
20807 static void
20808 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20809                               tree type_spec,
20810                               location_t location,
20811                               bool user_defined_p)
20812 {
20813   decl_specs->any_specifiers_p = true;
20814
20815   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20816      (with, for example, in "typedef int wchar_t;") we remember that
20817      this is what happened.  In system headers, we ignore these
20818      declarations so that G++ can work with system headers that are not
20819      C++-safe.  */
20820   if (decl_specs->specs[(int) ds_typedef]
20821       && !user_defined_p
20822       && (type_spec == boolean_type_node
20823           || type_spec == char16_type_node
20824           || type_spec == char32_type_node
20825           || type_spec == wchar_type_node)
20826       && (decl_specs->type
20827           || decl_specs->specs[(int) ds_long]
20828           || decl_specs->specs[(int) ds_short]
20829           || decl_specs->specs[(int) ds_unsigned]
20830           || decl_specs->specs[(int) ds_signed]))
20831     {
20832       decl_specs->redefined_builtin_type = type_spec;
20833       if (!decl_specs->type)
20834         {
20835           decl_specs->type = type_spec;
20836           decl_specs->user_defined_type_p = false;
20837           decl_specs->type_location = location;
20838         }
20839     }
20840   else if (decl_specs->type)
20841     decl_specs->multiple_types_p = true;
20842   else
20843     {
20844       decl_specs->type = type_spec;
20845       decl_specs->user_defined_type_p = user_defined_p;
20846       decl_specs->redefined_builtin_type = NULL_TREE;
20847       decl_specs->type_location = location;
20848     }
20849 }
20850
20851 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20852    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20853
20854 static bool
20855 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20856 {
20857   return decl_specifiers->specs[(int) ds_friend] != 0;
20858 }
20859
20860 /* Issue an error message indicating that TOKEN_DESC was expected.
20861    If KEYWORD is true, it indicated this function is called by
20862    cp_parser_require_keword and the required token can only be
20863    a indicated keyword. */
20864
20865 static void
20866 cp_parser_required_error (cp_parser *parser,
20867                           required_token token_desc,
20868                           bool keyword)
20869 {
20870   switch (token_desc)
20871     {
20872       case RT_NEW:
20873         cp_parser_error (parser, "expected %<new%>");
20874         return;
20875       case RT_DELETE:
20876         cp_parser_error (parser, "expected %<delete%>");
20877         return;
20878       case RT_RETURN:
20879         cp_parser_error (parser, "expected %<return%>");
20880         return;
20881       case RT_WHILE:
20882         cp_parser_error (parser, "expected %<while%>");
20883         return;
20884       case RT_EXTERN:
20885         cp_parser_error (parser, "expected %<extern%>");
20886         return;
20887       case RT_STATIC_ASSERT:
20888         cp_parser_error (parser, "expected %<static_assert%>");
20889         return;
20890       case RT_DECLTYPE:
20891         cp_parser_error (parser, "expected %<decltype%>");
20892         return;
20893       case RT_OPERATOR:
20894         cp_parser_error (parser, "expected %<operator%>");
20895         return;
20896       case RT_CLASS:
20897         cp_parser_error (parser, "expected %<class%>");
20898         return;
20899       case RT_TEMPLATE:
20900         cp_parser_error (parser, "expected %<template%>");
20901         return;
20902       case RT_NAMESPACE:
20903         cp_parser_error (parser, "expected %<namespace%>");
20904         return;
20905       case RT_USING:
20906         cp_parser_error (parser, "expected %<using%>");
20907         return;
20908       case RT_ASM:
20909         cp_parser_error (parser, "expected %<asm%>");
20910         return;
20911       case RT_TRY:
20912         cp_parser_error (parser, "expected %<try%>");
20913         return;
20914       case RT_CATCH:
20915         cp_parser_error (parser, "expected %<catch%>");
20916         return;
20917       case RT_THROW:
20918         cp_parser_error (parser, "expected %<throw%>");
20919         return;
20920       case RT_LABEL:
20921         cp_parser_error (parser, "expected %<__label__%>");
20922         return;
20923       case RT_AT_TRY:
20924         cp_parser_error (parser, "expected %<@try%>");
20925         return;
20926       case RT_AT_SYNCHRONIZED:
20927         cp_parser_error (parser, "expected %<@synchronized%>");
20928         return;
20929       case RT_AT_THROW:
20930         cp_parser_error (parser, "expected %<@throw%>");
20931         return;
20932       default:
20933         break;
20934     }
20935   if (!keyword)
20936     {
20937       switch (token_desc)
20938         {
20939           case RT_SEMICOLON:
20940             cp_parser_error (parser, "expected %<;%>");
20941             return;
20942           case RT_OPEN_PAREN:
20943             cp_parser_error (parser, "expected %<(%>");
20944             return;
20945           case RT_CLOSE_BRACE:
20946             cp_parser_error (parser, "expected %<}%>");
20947             return;
20948           case RT_OPEN_BRACE:
20949             cp_parser_error (parser, "expected %<{%>");
20950             return;
20951           case RT_CLOSE_SQUARE:
20952             cp_parser_error (parser, "expected %<]%>");
20953             return;
20954           case RT_OPEN_SQUARE:
20955             cp_parser_error (parser, "expected %<[%>");
20956             return;
20957           case RT_COMMA:
20958             cp_parser_error (parser, "expected %<,%>");
20959             return;
20960           case RT_SCOPE:
20961             cp_parser_error (parser, "expected %<::%>");
20962             return;
20963           case RT_LESS:
20964             cp_parser_error (parser, "expected %<<%>");
20965             return;
20966           case RT_GREATER:
20967             cp_parser_error (parser, "expected %<>%>");
20968             return;
20969           case RT_EQ:
20970             cp_parser_error (parser, "expected %<=%>");
20971             return;
20972           case RT_ELLIPSIS:
20973             cp_parser_error (parser, "expected %<...%>");
20974             return;
20975           case RT_MULT:
20976             cp_parser_error (parser, "expected %<*%>");
20977             return;
20978           case RT_COMPL:
20979             cp_parser_error (parser, "expected %<~%>");
20980             return;
20981           case RT_COLON:
20982             cp_parser_error (parser, "expected %<:%>");
20983             return;
20984           case RT_COLON_SCOPE:
20985             cp_parser_error (parser, "expected %<:%> or %<::%>");
20986             return;
20987           case RT_CLOSE_PAREN:
20988             cp_parser_error (parser, "expected %<)%>");
20989             return;
20990           case RT_COMMA_CLOSE_PAREN:
20991             cp_parser_error (parser, "expected %<,%> or %<)%>");
20992             return;
20993           case RT_PRAGMA_EOL:
20994             cp_parser_error (parser, "expected end of line");
20995             return;
20996           case RT_NAME:
20997             cp_parser_error (parser, "expected identifier");
20998             return;
20999           case RT_SELECT:
21000             cp_parser_error (parser, "expected selection-statement");
21001             return;
21002           case RT_INTERATION:
21003             cp_parser_error (parser, "expected iteration-statement");
21004             return;
21005           case RT_JUMP:
21006             cp_parser_error (parser, "expected jump-statement");
21007             return;
21008           case RT_CLASS_KEY:
21009             cp_parser_error (parser, "expected class-key");
21010             return;
21011           case RT_CLASS_TYPENAME_TEMPLATE:
21012             cp_parser_error (parser,
21013                  "expected %<class%>, %<typename%>, or %<template%>");
21014             return;
21015           default:
21016             gcc_unreachable ();
21017         }
21018     }
21019   else
21020     gcc_unreachable ();
21021 }
21022
21023
21024
21025 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
21026    issue an error message indicating that TOKEN_DESC was expected.
21027
21028    Returns the token consumed, if the token had the appropriate type.
21029    Otherwise, returns NULL.  */
21030
21031 static cp_token *
21032 cp_parser_require (cp_parser* parser,
21033                    enum cpp_ttype type,
21034                    required_token token_desc)
21035 {
21036   if (cp_lexer_next_token_is (parser->lexer, type))
21037     return cp_lexer_consume_token (parser->lexer);
21038   else
21039     {
21040       /* Output the MESSAGE -- unless we're parsing tentatively.  */
21041       if (!cp_parser_simulate_error (parser))
21042         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
21043       return NULL;
21044     }
21045 }
21046
21047 /* An error message is produced if the next token is not '>'.
21048    All further tokens are skipped until the desired token is
21049    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
21050
21051 static void
21052 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
21053 {
21054   /* Current level of '< ... >'.  */
21055   unsigned level = 0;
21056   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21057   unsigned nesting_depth = 0;
21058
21059   /* Are we ready, yet?  If not, issue error message.  */
21060   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21061     return;
21062
21063   /* Skip tokens until the desired token is found.  */
21064   while (true)
21065     {
21066       /* Peek at the next token.  */
21067       switch (cp_lexer_peek_token (parser->lexer)->type)
21068         {
21069         case CPP_LESS:
21070           if (!nesting_depth)
21071             ++level;
21072           break;
21073
21074         case CPP_RSHIFT:
21075           if (cxx_dialect == cxx98)
21076             /* C++0x views the `>>' operator as two `>' tokens, but
21077                C++98 does not. */
21078             break;
21079           else if (!nesting_depth && level-- == 0)
21080             {
21081               /* We've hit a `>>' where the first `>' closes the
21082                  template argument list, and the second `>' is
21083                  spurious.  Just consume the `>>' and stop; we've
21084                  already produced at least one error.  */
21085               cp_lexer_consume_token (parser->lexer);
21086               return;
21087             }
21088           /* Fall through for C++0x, so we handle the second `>' in
21089              the `>>'.  */
21090
21091         case CPP_GREATER:
21092           if (!nesting_depth && level-- == 0)
21093             {
21094               /* We've reached the token we want, consume it and stop.  */
21095               cp_lexer_consume_token (parser->lexer);
21096               return;
21097             }
21098           break;
21099
21100         case CPP_OPEN_PAREN:
21101         case CPP_OPEN_SQUARE:
21102           ++nesting_depth;
21103           break;
21104
21105         case CPP_CLOSE_PAREN:
21106         case CPP_CLOSE_SQUARE:
21107           if (nesting_depth-- == 0)
21108             return;
21109           break;
21110
21111         case CPP_EOF:
21112         case CPP_PRAGMA_EOL:
21113         case CPP_SEMICOLON:
21114         case CPP_OPEN_BRACE:
21115         case CPP_CLOSE_BRACE:
21116           /* The '>' was probably forgotten, don't look further.  */
21117           return;
21118
21119         default:
21120           break;
21121         }
21122
21123       /* Consume this token.  */
21124       cp_lexer_consume_token (parser->lexer);
21125     }
21126 }
21127
21128 /* If the next token is the indicated keyword, consume it.  Otherwise,
21129    issue an error message indicating that TOKEN_DESC was expected.
21130
21131    Returns the token consumed, if the token had the appropriate type.
21132    Otherwise, returns NULL.  */
21133
21134 static cp_token *
21135 cp_parser_require_keyword (cp_parser* parser,
21136                            enum rid keyword,
21137                            required_token token_desc)
21138 {
21139   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21140
21141   if (token && token->keyword != keyword)
21142     {
21143       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21144       return NULL;
21145     }
21146
21147   return token;
21148 }
21149
21150 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21151    function-definition.  */
21152
21153 static bool
21154 cp_parser_token_starts_function_definition_p (cp_token* token)
21155 {
21156   return (/* An ordinary function-body begins with an `{'.  */
21157           token->type == CPP_OPEN_BRACE
21158           /* A ctor-initializer begins with a `:'.  */
21159           || token->type == CPP_COLON
21160           /* A function-try-block begins with `try'.  */
21161           || token->keyword == RID_TRY
21162           /* The named return value extension begins with `return'.  */
21163           || token->keyword == RID_RETURN);
21164 }
21165
21166 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21167    definition.  */
21168
21169 static bool
21170 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21171 {
21172   cp_token *token;
21173
21174   token = cp_lexer_peek_token (parser->lexer);
21175   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21176 }
21177
21178 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21179    C++0x) ending a template-argument.  */
21180
21181 static bool
21182 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21183 {
21184   cp_token *token;
21185
21186   token = cp_lexer_peek_token (parser->lexer);
21187   return (token->type == CPP_COMMA 
21188           || token->type == CPP_GREATER
21189           || token->type == CPP_ELLIPSIS
21190           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21191 }
21192
21193 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21194    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21195
21196 static bool
21197 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21198                                                      size_t n)
21199 {
21200   cp_token *token;
21201
21202   token = cp_lexer_peek_nth_token (parser->lexer, n);
21203   if (token->type == CPP_LESS)
21204     return true;
21205   /* Check for the sequence `<::' in the original code. It would be lexed as
21206      `[:', where `[' is a digraph, and there is no whitespace before
21207      `:'.  */
21208   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21209     {
21210       cp_token *token2;
21211       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21212       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21213         return true;
21214     }
21215   return false;
21216 }
21217
21218 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21219    or none_type otherwise.  */
21220
21221 static enum tag_types
21222 cp_parser_token_is_class_key (cp_token* token)
21223 {
21224   switch (token->keyword)
21225     {
21226     case RID_CLASS:
21227       return class_type;
21228     case RID_STRUCT:
21229       return record_type;
21230     case RID_UNION:
21231       return union_type;
21232
21233     default:
21234       return none_type;
21235     }
21236 }
21237
21238 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21239
21240 static void
21241 cp_parser_check_class_key (enum tag_types class_key, tree type)
21242 {
21243   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21244     permerror (input_location, "%qs tag used in naming %q#T",
21245             class_key == union_type ? "union"
21246              : class_key == record_type ? "struct" : "class",
21247              type);
21248 }
21249
21250 /* Issue an error message if DECL is redeclared with different
21251    access than its original declaration [class.access.spec/3].
21252    This applies to nested classes and nested class templates.
21253    [class.mem/1].  */
21254
21255 static void
21256 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21257 {
21258   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21259     return;
21260
21261   if ((TREE_PRIVATE (decl)
21262        != (current_access_specifier == access_private_node))
21263       || (TREE_PROTECTED (decl)
21264           != (current_access_specifier == access_protected_node)))
21265     error_at (location, "%qD redeclared with different access", decl);
21266 }
21267
21268 /* Look for the `template' keyword, as a syntactic disambiguator.
21269    Return TRUE iff it is present, in which case it will be
21270    consumed.  */
21271
21272 static bool
21273 cp_parser_optional_template_keyword (cp_parser *parser)
21274 {
21275   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21276     {
21277       /* The `template' keyword can only be used within templates;
21278          outside templates the parser can always figure out what is a
21279          template and what is not.  */
21280       if (!processing_template_decl)
21281         {
21282           cp_token *token = cp_lexer_peek_token (parser->lexer);
21283           error_at (token->location,
21284                     "%<template%> (as a disambiguator) is only allowed "
21285                     "within templates");
21286           /* If this part of the token stream is rescanned, the same
21287              error message would be generated.  So, we purge the token
21288              from the stream.  */
21289           cp_lexer_purge_token (parser->lexer);
21290           return false;
21291         }
21292       else
21293         {
21294           /* Consume the `template' keyword.  */
21295           cp_lexer_consume_token (parser->lexer);
21296           return true;
21297         }
21298     }
21299
21300   return false;
21301 }
21302
21303 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21304    set PARSER->SCOPE, and perform other related actions.  */
21305
21306 static void
21307 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21308 {
21309   int i;
21310   struct tree_check *check_value;
21311   deferred_access_check *chk;
21312   VEC (deferred_access_check,gc) *checks;
21313
21314   /* Get the stored value.  */
21315   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21316   /* Perform any access checks that were deferred.  */
21317   checks = check_value->checks;
21318   if (checks)
21319     {
21320       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21321         perform_or_defer_access_check (chk->binfo,
21322                                        chk->decl,
21323                                        chk->diag_decl);
21324     }
21325   /* Set the scope from the stored value.  */
21326   parser->scope = check_value->value;
21327   parser->qualifying_scope = check_value->qualifying_scope;
21328   parser->object_scope = NULL_TREE;
21329 }
21330
21331 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21332    encounter the end of a block before what we were looking for.  */
21333
21334 static bool
21335 cp_parser_cache_group (cp_parser *parser,
21336                        enum cpp_ttype end,
21337                        unsigned depth)
21338 {
21339   while (true)
21340     {
21341       cp_token *token = cp_lexer_peek_token (parser->lexer);
21342
21343       /* Abort a parenthesized expression if we encounter a semicolon.  */
21344       if ((end == CPP_CLOSE_PAREN || depth == 0)
21345           && token->type == CPP_SEMICOLON)
21346         return true;
21347       /* If we've reached the end of the file, stop.  */
21348       if (token->type == CPP_EOF
21349           || (end != CPP_PRAGMA_EOL
21350               && token->type == CPP_PRAGMA_EOL))
21351         return true;
21352       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21353         /* We've hit the end of an enclosing block, so there's been some
21354            kind of syntax error.  */
21355         return true;
21356
21357       /* Consume the token.  */
21358       cp_lexer_consume_token (parser->lexer);
21359       /* See if it starts a new group.  */
21360       if (token->type == CPP_OPEN_BRACE)
21361         {
21362           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21363           /* In theory this should probably check end == '}', but
21364              cp_parser_save_member_function_body needs it to exit
21365              after either '}' or ')' when called with ')'.  */
21366           if (depth == 0)
21367             return false;
21368         }
21369       else if (token->type == CPP_OPEN_PAREN)
21370         {
21371           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21372           if (depth == 0 && end == CPP_CLOSE_PAREN)
21373             return false;
21374         }
21375       else if (token->type == CPP_PRAGMA)
21376         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21377       else if (token->type == end)
21378         return false;
21379     }
21380 }
21381
21382 /* Begin parsing tentatively.  We always save tokens while parsing
21383    tentatively so that if the tentative parsing fails we can restore the
21384    tokens.  */
21385
21386 static void
21387 cp_parser_parse_tentatively (cp_parser* parser)
21388 {
21389   /* Enter a new parsing context.  */
21390   parser->context = cp_parser_context_new (parser->context);
21391   /* Begin saving tokens.  */
21392   cp_lexer_save_tokens (parser->lexer);
21393   /* In order to avoid repetitive access control error messages,
21394      access checks are queued up until we are no longer parsing
21395      tentatively.  */
21396   push_deferring_access_checks (dk_deferred);
21397 }
21398
21399 /* Commit to the currently active tentative parse.  */
21400
21401 static void
21402 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21403 {
21404   cp_parser_context *context;
21405   cp_lexer *lexer;
21406
21407   /* Mark all of the levels as committed.  */
21408   lexer = parser->lexer;
21409   for (context = parser->context; context->next; context = context->next)
21410     {
21411       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21412         break;
21413       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21414       while (!cp_lexer_saving_tokens (lexer))
21415         lexer = lexer->next;
21416       cp_lexer_commit_tokens (lexer);
21417     }
21418 }
21419
21420 /* Abort the currently active tentative parse.  All consumed tokens
21421    will be rolled back, and no diagnostics will be issued.  */
21422
21423 static void
21424 cp_parser_abort_tentative_parse (cp_parser* parser)
21425 {
21426   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21427               || errorcount > 0);
21428   cp_parser_simulate_error (parser);
21429   /* Now, pretend that we want to see if the construct was
21430      successfully parsed.  */
21431   cp_parser_parse_definitely (parser);
21432 }
21433
21434 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21435    token stream.  Otherwise, commit to the tokens we have consumed.
21436    Returns true if no error occurred; false otherwise.  */
21437
21438 static bool
21439 cp_parser_parse_definitely (cp_parser* parser)
21440 {
21441   bool error_occurred;
21442   cp_parser_context *context;
21443
21444   /* Remember whether or not an error occurred, since we are about to
21445      destroy that information.  */
21446   error_occurred = cp_parser_error_occurred (parser);
21447   /* Remove the topmost context from the stack.  */
21448   context = parser->context;
21449   parser->context = context->next;
21450   /* If no parse errors occurred, commit to the tentative parse.  */
21451   if (!error_occurred)
21452     {
21453       /* Commit to the tokens read tentatively, unless that was
21454          already done.  */
21455       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21456         cp_lexer_commit_tokens (parser->lexer);
21457
21458       pop_to_parent_deferring_access_checks ();
21459     }
21460   /* Otherwise, if errors occurred, roll back our state so that things
21461      are just as they were before we began the tentative parse.  */
21462   else
21463     {
21464       cp_lexer_rollback_tokens (parser->lexer);
21465       pop_deferring_access_checks ();
21466     }
21467   /* Add the context to the front of the free list.  */
21468   context->next = cp_parser_context_free_list;
21469   cp_parser_context_free_list = context;
21470
21471   return !error_occurred;
21472 }
21473
21474 /* Returns true if we are parsing tentatively and are not committed to
21475    this tentative parse.  */
21476
21477 static bool
21478 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21479 {
21480   return (cp_parser_parsing_tentatively (parser)
21481           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21482 }
21483
21484 /* Returns nonzero iff an error has occurred during the most recent
21485    tentative parse.  */
21486
21487 static bool
21488 cp_parser_error_occurred (cp_parser* parser)
21489 {
21490   return (cp_parser_parsing_tentatively (parser)
21491           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21492 }
21493
21494 /* Returns nonzero if GNU extensions are allowed.  */
21495
21496 static bool
21497 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21498 {
21499   return parser->allow_gnu_extensions_p;
21500 }
21501 \f
21502 /* Objective-C++ Productions */
21503
21504
21505 /* Parse an Objective-C expression, which feeds into a primary-expression
21506    above.
21507
21508    objc-expression:
21509      objc-message-expression
21510      objc-string-literal
21511      objc-encode-expression
21512      objc-protocol-expression
21513      objc-selector-expression
21514
21515   Returns a tree representation of the expression.  */
21516
21517 static tree
21518 cp_parser_objc_expression (cp_parser* parser)
21519 {
21520   /* Try to figure out what kind of declaration is present.  */
21521   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21522
21523   switch (kwd->type)
21524     {
21525     case CPP_OPEN_SQUARE:
21526       return cp_parser_objc_message_expression (parser);
21527
21528     case CPP_OBJC_STRING:
21529       kwd = cp_lexer_consume_token (parser->lexer);
21530       return objc_build_string_object (kwd->u.value);
21531
21532     case CPP_KEYWORD:
21533       switch (kwd->keyword)
21534         {
21535         case RID_AT_ENCODE:
21536           return cp_parser_objc_encode_expression (parser);
21537
21538         case RID_AT_PROTOCOL:
21539           return cp_parser_objc_protocol_expression (parser);
21540
21541         case RID_AT_SELECTOR:
21542           return cp_parser_objc_selector_expression (parser);
21543
21544         default:
21545           break;
21546         }
21547     default:
21548       error_at (kwd->location,
21549                 "misplaced %<@%D%> Objective-C++ construct",
21550                 kwd->u.value);
21551       cp_parser_skip_to_end_of_block_or_statement (parser);
21552     }
21553
21554   return error_mark_node;
21555 }
21556
21557 /* Parse an Objective-C message expression.
21558
21559    objc-message-expression:
21560      [ objc-message-receiver objc-message-args ]
21561
21562    Returns a representation of an Objective-C message.  */
21563
21564 static tree
21565 cp_parser_objc_message_expression (cp_parser* parser)
21566 {
21567   tree receiver, messageargs;
21568
21569   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21570   receiver = cp_parser_objc_message_receiver (parser);
21571   messageargs = cp_parser_objc_message_args (parser);
21572   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21573
21574   return objc_build_message_expr (receiver, messageargs);
21575 }
21576
21577 /* Parse an objc-message-receiver.
21578
21579    objc-message-receiver:
21580      expression
21581      simple-type-specifier
21582
21583   Returns a representation of the type or expression.  */
21584
21585 static tree
21586 cp_parser_objc_message_receiver (cp_parser* parser)
21587 {
21588   tree rcv;
21589
21590   /* An Objective-C message receiver may be either (1) a type
21591      or (2) an expression.  */
21592   cp_parser_parse_tentatively (parser);
21593   rcv = cp_parser_expression (parser, false, NULL);
21594
21595   if (cp_parser_parse_definitely (parser))
21596     return rcv;
21597
21598   rcv = cp_parser_simple_type_specifier (parser,
21599                                          /*decl_specs=*/NULL,
21600                                          CP_PARSER_FLAGS_NONE);
21601
21602   return objc_get_class_reference (rcv);
21603 }
21604
21605 /* Parse the arguments and selectors comprising an Objective-C message.
21606
21607    objc-message-args:
21608      objc-selector
21609      objc-selector-args
21610      objc-selector-args , objc-comma-args
21611
21612    objc-selector-args:
21613      objc-selector [opt] : assignment-expression
21614      objc-selector-args objc-selector [opt] : assignment-expression
21615
21616    objc-comma-args:
21617      assignment-expression
21618      objc-comma-args , assignment-expression
21619
21620    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21621    selector arguments and TREE_VALUE containing a list of comma
21622    arguments.  */
21623
21624 static tree
21625 cp_parser_objc_message_args (cp_parser* parser)
21626 {
21627   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21628   bool maybe_unary_selector_p = true;
21629   cp_token *token = cp_lexer_peek_token (parser->lexer);
21630
21631   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21632     {
21633       tree selector = NULL_TREE, arg;
21634
21635       if (token->type != CPP_COLON)
21636         selector = cp_parser_objc_selector (parser);
21637
21638       /* Detect if we have a unary selector.  */
21639       if (maybe_unary_selector_p
21640           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21641         return build_tree_list (selector, NULL_TREE);
21642
21643       maybe_unary_selector_p = false;
21644       cp_parser_require (parser, CPP_COLON, RT_COLON);
21645       arg = cp_parser_assignment_expression (parser, false, NULL);
21646
21647       sel_args
21648         = chainon (sel_args,
21649                    build_tree_list (selector, arg));
21650
21651       token = cp_lexer_peek_token (parser->lexer);
21652     }
21653
21654   /* Handle non-selector arguments, if any. */
21655   while (token->type == CPP_COMMA)
21656     {
21657       tree arg;
21658
21659       cp_lexer_consume_token (parser->lexer);
21660       arg = cp_parser_assignment_expression (parser, false, NULL);
21661
21662       addl_args
21663         = chainon (addl_args,
21664                    build_tree_list (NULL_TREE, arg));
21665
21666       token = cp_lexer_peek_token (parser->lexer);
21667     }
21668
21669   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21670     {
21671       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21672       return build_tree_list (error_mark_node, error_mark_node);
21673     }
21674
21675   return build_tree_list (sel_args, addl_args);
21676 }
21677
21678 /* Parse an Objective-C encode expression.
21679
21680    objc-encode-expression:
21681      @encode objc-typename
21682
21683    Returns an encoded representation of the type argument.  */
21684
21685 static tree
21686 cp_parser_objc_encode_expression (cp_parser* parser)
21687 {
21688   tree type;
21689   cp_token *token;
21690
21691   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21692   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21693   token = cp_lexer_peek_token (parser->lexer);
21694   type = complete_type (cp_parser_type_id (parser));
21695   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21696
21697   if (!type)
21698     {
21699       error_at (token->location, 
21700                 "%<@encode%> must specify a type as an argument");
21701       return error_mark_node;
21702     }
21703
21704   /* This happens if we find @encode(T) (where T is a template
21705      typename or something dependent on a template typename) when
21706      parsing a template.  In that case, we can't compile it
21707      immediately, but we rather create an AT_ENCODE_EXPR which will
21708      need to be instantiated when the template is used.
21709   */
21710   if (dependent_type_p (type))
21711     {
21712       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21713       TREE_READONLY (value) = 1;
21714       return value;
21715     }
21716
21717   return objc_build_encode_expr (type);
21718 }
21719
21720 /* Parse an Objective-C @defs expression.  */
21721
21722 static tree
21723 cp_parser_objc_defs_expression (cp_parser *parser)
21724 {
21725   tree name;
21726
21727   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21728   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21729   name = cp_parser_identifier (parser);
21730   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21731
21732   return objc_get_class_ivars (name);
21733 }
21734
21735 /* Parse an Objective-C protocol expression.
21736
21737   objc-protocol-expression:
21738     @protocol ( identifier )
21739
21740   Returns a representation of the protocol expression.  */
21741
21742 static tree
21743 cp_parser_objc_protocol_expression (cp_parser* parser)
21744 {
21745   tree proto;
21746
21747   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21748   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21749   proto = cp_parser_identifier (parser);
21750   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21751
21752   return objc_build_protocol_expr (proto);
21753 }
21754
21755 /* Parse an Objective-C selector expression.
21756
21757    objc-selector-expression:
21758      @selector ( objc-method-signature )
21759
21760    objc-method-signature:
21761      objc-selector
21762      objc-selector-seq
21763
21764    objc-selector-seq:
21765      objc-selector :
21766      objc-selector-seq objc-selector :
21767
21768   Returns a representation of the method selector.  */
21769
21770 static tree
21771 cp_parser_objc_selector_expression (cp_parser* parser)
21772 {
21773   tree sel_seq = NULL_TREE;
21774   bool maybe_unary_selector_p = true;
21775   cp_token *token;
21776   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21777
21778   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21779   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21780   token = cp_lexer_peek_token (parser->lexer);
21781
21782   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21783          || token->type == CPP_SCOPE)
21784     {
21785       tree selector = NULL_TREE;
21786
21787       if (token->type != CPP_COLON
21788           || token->type == CPP_SCOPE)
21789         selector = cp_parser_objc_selector (parser);
21790
21791       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21792           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21793         {
21794           /* Detect if we have a unary selector.  */
21795           if (maybe_unary_selector_p)
21796             {
21797               sel_seq = selector;
21798               goto finish_selector;
21799             }
21800           else
21801             {
21802               cp_parser_error (parser, "expected %<:%>");
21803             }
21804         }
21805       maybe_unary_selector_p = false;
21806       token = cp_lexer_consume_token (parser->lexer);
21807
21808       if (token->type == CPP_SCOPE)
21809         {
21810           sel_seq
21811             = chainon (sel_seq,
21812                        build_tree_list (selector, NULL_TREE));
21813           sel_seq
21814             = chainon (sel_seq,
21815                        build_tree_list (NULL_TREE, NULL_TREE));
21816         }
21817       else
21818         sel_seq
21819           = chainon (sel_seq,
21820                      build_tree_list (selector, NULL_TREE));
21821
21822       token = cp_lexer_peek_token (parser->lexer);
21823     }
21824
21825  finish_selector:
21826   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21827
21828   return objc_build_selector_expr (loc, sel_seq);
21829 }
21830
21831 /* Parse a list of identifiers.
21832
21833    objc-identifier-list:
21834      identifier
21835      objc-identifier-list , identifier
21836
21837    Returns a TREE_LIST of identifier nodes.  */
21838
21839 static tree
21840 cp_parser_objc_identifier_list (cp_parser* parser)
21841 {
21842   tree identifier;
21843   tree list;
21844   cp_token *sep;
21845
21846   identifier = cp_parser_identifier (parser);
21847   if (identifier == error_mark_node)
21848     return error_mark_node;      
21849
21850   list = build_tree_list (NULL_TREE, identifier);
21851   sep = cp_lexer_peek_token (parser->lexer);
21852
21853   while (sep->type == CPP_COMMA)
21854     {
21855       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21856       identifier = cp_parser_identifier (parser);
21857       if (identifier == error_mark_node)
21858         return list;
21859
21860       list = chainon (list, build_tree_list (NULL_TREE,
21861                                              identifier));
21862       sep = cp_lexer_peek_token (parser->lexer);
21863     }
21864   
21865   return list;
21866 }
21867
21868 /* Parse an Objective-C alias declaration.
21869
21870    objc-alias-declaration:
21871      @compatibility_alias identifier identifier ;
21872
21873    This function registers the alias mapping with the Objective-C front end.
21874    It returns nothing.  */
21875
21876 static void
21877 cp_parser_objc_alias_declaration (cp_parser* parser)
21878 {
21879   tree alias, orig;
21880
21881   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21882   alias = cp_parser_identifier (parser);
21883   orig = cp_parser_identifier (parser);
21884   objc_declare_alias (alias, orig);
21885   cp_parser_consume_semicolon_at_end_of_statement (parser);
21886 }
21887
21888 /* Parse an Objective-C class forward-declaration.
21889
21890    objc-class-declaration:
21891      @class objc-identifier-list ;
21892
21893    The function registers the forward declarations with the Objective-C
21894    front end.  It returns nothing.  */
21895
21896 static void
21897 cp_parser_objc_class_declaration (cp_parser* parser)
21898 {
21899   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21900   while (true)
21901     {
21902       tree id;
21903       
21904       id = cp_parser_identifier (parser);
21905       if (id == error_mark_node)
21906         break;
21907       
21908       objc_declare_class (id);
21909
21910       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21911         cp_lexer_consume_token (parser->lexer);
21912       else
21913         break;
21914     }
21915   cp_parser_consume_semicolon_at_end_of_statement (parser);
21916 }
21917
21918 /* Parse a list of Objective-C protocol references.
21919
21920    objc-protocol-refs-opt:
21921      objc-protocol-refs [opt]
21922
21923    objc-protocol-refs:
21924      < objc-identifier-list >
21925
21926    Returns a TREE_LIST of identifiers, if any.  */
21927
21928 static tree
21929 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21930 {
21931   tree protorefs = NULL_TREE;
21932
21933   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21934     {
21935       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21936       protorefs = cp_parser_objc_identifier_list (parser);
21937       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21938     }
21939
21940   return protorefs;
21941 }
21942
21943 /* Parse a Objective-C visibility specification.  */
21944
21945 static void
21946 cp_parser_objc_visibility_spec (cp_parser* parser)
21947 {
21948   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21949
21950   switch (vis->keyword)
21951     {
21952     case RID_AT_PRIVATE:
21953       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21954       break;
21955     case RID_AT_PROTECTED:
21956       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21957       break;
21958     case RID_AT_PUBLIC:
21959       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21960       break;
21961     case RID_AT_PACKAGE:
21962       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21963       break;
21964     default:
21965       return;
21966     }
21967
21968   /* Eat '@private'/'@protected'/'@public'.  */
21969   cp_lexer_consume_token (parser->lexer);
21970 }
21971
21972 /* Parse an Objective-C method type.  Return 'true' if it is a class
21973    (+) method, and 'false' if it is an instance (-) method.  */
21974
21975 static inline bool
21976 cp_parser_objc_method_type (cp_parser* parser)
21977 {
21978   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21979     return true;
21980   else
21981     return false;
21982 }
21983
21984 /* Parse an Objective-C protocol qualifier.  */
21985
21986 static tree
21987 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21988 {
21989   tree quals = NULL_TREE, node;
21990   cp_token *token = cp_lexer_peek_token (parser->lexer);
21991
21992   node = token->u.value;
21993
21994   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21995          && (node == ridpointers [(int) RID_IN]
21996              || node == ridpointers [(int) RID_OUT]
21997              || node == ridpointers [(int) RID_INOUT]
21998              || node == ridpointers [(int) RID_BYCOPY]
21999              || node == ridpointers [(int) RID_BYREF]
22000              || node == ridpointers [(int) RID_ONEWAY]))
22001     {
22002       quals = tree_cons (NULL_TREE, node, quals);
22003       cp_lexer_consume_token (parser->lexer);
22004       token = cp_lexer_peek_token (parser->lexer);
22005       node = token->u.value;
22006     }
22007
22008   return quals;
22009 }
22010
22011 /* Parse an Objective-C typename.  */
22012
22013 static tree
22014 cp_parser_objc_typename (cp_parser* parser)
22015 {
22016   tree type_name = NULL_TREE;
22017
22018   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22019     {
22020       tree proto_quals, cp_type = NULL_TREE;
22021
22022       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22023       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
22024
22025       /* An ObjC type name may consist of just protocol qualifiers, in which
22026          case the type shall default to 'id'.  */
22027       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
22028         {
22029           cp_type = cp_parser_type_id (parser);
22030           
22031           /* If the type could not be parsed, an error has already
22032              been produced.  For error recovery, behave as if it had
22033              not been specified, which will use the default type
22034              'id'.  */
22035           if (cp_type == error_mark_node)
22036             {
22037               cp_type = NULL_TREE;
22038               /* We need to skip to the closing parenthesis as
22039                  cp_parser_type_id() does not seem to do it for
22040                  us.  */
22041               cp_parser_skip_to_closing_parenthesis (parser,
22042                                                      /*recovering=*/true,
22043                                                      /*or_comma=*/false,
22044                                                      /*consume_paren=*/false);
22045             }
22046         }
22047
22048       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22049       type_name = build_tree_list (proto_quals, cp_type);
22050     }
22051
22052   return type_name;
22053 }
22054
22055 /* Check to see if TYPE refers to an Objective-C selector name.  */
22056
22057 static bool
22058 cp_parser_objc_selector_p (enum cpp_ttype type)
22059 {
22060   return (type == CPP_NAME || type == CPP_KEYWORD
22061           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22062           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22063           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22064           || type == CPP_XOR || type == CPP_XOR_EQ);
22065 }
22066
22067 /* Parse an Objective-C selector.  */
22068
22069 static tree
22070 cp_parser_objc_selector (cp_parser* parser)
22071 {
22072   cp_token *token = cp_lexer_consume_token (parser->lexer);
22073
22074   if (!cp_parser_objc_selector_p (token->type))
22075     {
22076       error_at (token->location, "invalid Objective-C++ selector name");
22077       return error_mark_node;
22078     }
22079
22080   /* C++ operator names are allowed to appear in ObjC selectors.  */
22081   switch (token->type)
22082     {
22083     case CPP_AND_AND: return get_identifier ("and");
22084     case CPP_AND_EQ: return get_identifier ("and_eq");
22085     case CPP_AND: return get_identifier ("bitand");
22086     case CPP_OR: return get_identifier ("bitor");
22087     case CPP_COMPL: return get_identifier ("compl");
22088     case CPP_NOT: return get_identifier ("not");
22089     case CPP_NOT_EQ: return get_identifier ("not_eq");
22090     case CPP_OR_OR: return get_identifier ("or");
22091     case CPP_OR_EQ: return get_identifier ("or_eq");
22092     case CPP_XOR: return get_identifier ("xor");
22093     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22094     default: return token->u.value;
22095     }
22096 }
22097
22098 /* Parse an Objective-C params list.  */
22099
22100 static tree
22101 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22102 {
22103   tree params = NULL_TREE;
22104   bool maybe_unary_selector_p = true;
22105   cp_token *token = cp_lexer_peek_token (parser->lexer);
22106
22107   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22108     {
22109       tree selector = NULL_TREE, type_name, identifier;
22110       tree parm_attr = NULL_TREE;
22111
22112       if (token->keyword == RID_ATTRIBUTE)
22113         break;
22114
22115       if (token->type != CPP_COLON)
22116         selector = cp_parser_objc_selector (parser);
22117
22118       /* Detect if we have a unary selector.  */
22119       if (maybe_unary_selector_p
22120           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22121         {
22122           params = selector; /* Might be followed by attributes.  */
22123           break;
22124         }
22125
22126       maybe_unary_selector_p = false;
22127       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22128         {
22129           /* Something went quite wrong.  There should be a colon
22130              here, but there is not.  Stop parsing parameters.  */
22131           break;
22132         }
22133       type_name = cp_parser_objc_typename (parser);
22134       /* New ObjC allows attributes on parameters too.  */
22135       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22136         parm_attr = cp_parser_attributes_opt (parser);
22137       identifier = cp_parser_identifier (parser);
22138
22139       params
22140         = chainon (params,
22141                    objc_build_keyword_decl (selector,
22142                                             type_name,
22143                                             identifier,
22144                                             parm_attr));
22145
22146       token = cp_lexer_peek_token (parser->lexer);
22147     }
22148
22149   if (params == NULL_TREE)
22150     {
22151       cp_parser_error (parser, "objective-c++ method declaration is expected");
22152       return error_mark_node;
22153     }
22154
22155   /* We allow tail attributes for the method.  */
22156   if (token->keyword == RID_ATTRIBUTE)
22157     {
22158       *attributes = cp_parser_attributes_opt (parser);
22159       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22160           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22161         return params;
22162       cp_parser_error (parser, 
22163                        "method attributes must be specified at the end");
22164       return error_mark_node;
22165     }
22166
22167   if (params == NULL_TREE)
22168     {
22169       cp_parser_error (parser, "objective-c++ method declaration is expected");
22170       return error_mark_node;
22171     }
22172   return params;
22173 }
22174
22175 /* Parse the non-keyword Objective-C params.  */
22176
22177 static tree
22178 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22179                                        tree* attributes)
22180 {
22181   tree params = make_node (TREE_LIST);
22182   cp_token *token = cp_lexer_peek_token (parser->lexer);
22183   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22184
22185   while (token->type == CPP_COMMA)
22186     {
22187       cp_parameter_declarator *parmdecl;
22188       tree parm;
22189
22190       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22191       token = cp_lexer_peek_token (parser->lexer);
22192
22193       if (token->type == CPP_ELLIPSIS)
22194         {
22195           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22196           *ellipsisp = true;
22197           token = cp_lexer_peek_token (parser->lexer);
22198           break;
22199         }
22200
22201       /* TODO: parse attributes for tail parameters.  */
22202       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22203       parm = grokdeclarator (parmdecl->declarator,
22204                              &parmdecl->decl_specifiers,
22205                              PARM, /*initialized=*/0,
22206                              /*attrlist=*/NULL);
22207
22208       chainon (params, build_tree_list (NULL_TREE, parm));
22209       token = cp_lexer_peek_token (parser->lexer);
22210     }
22211
22212   /* We allow tail attributes for the method.  */
22213   if (token->keyword == RID_ATTRIBUTE)
22214     {
22215       if (*attributes == NULL_TREE)
22216         {
22217           *attributes = cp_parser_attributes_opt (parser);
22218           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22219               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22220             return params;
22221         }
22222       else        
22223         /* We have an error, but parse the attributes, so that we can 
22224            carry on.  */
22225         *attributes = cp_parser_attributes_opt (parser);
22226
22227       cp_parser_error (parser, 
22228                        "method attributes must be specified at the end");
22229       return error_mark_node;
22230     }
22231
22232   return params;
22233 }
22234
22235 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22236
22237 static void
22238 cp_parser_objc_interstitial_code (cp_parser* parser)
22239 {
22240   cp_token *token = cp_lexer_peek_token (parser->lexer);
22241
22242   /* If the next token is `extern' and the following token is a string
22243      literal, then we have a linkage specification.  */
22244   if (token->keyword == RID_EXTERN
22245       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22246     cp_parser_linkage_specification (parser);
22247   /* Handle #pragma, if any.  */
22248   else if (token->type == CPP_PRAGMA)
22249     cp_parser_pragma (parser, pragma_external);
22250   /* Allow stray semicolons.  */
22251   else if (token->type == CPP_SEMICOLON)
22252     cp_lexer_consume_token (parser->lexer);
22253   /* Mark methods as optional or required, when building protocols.  */
22254   else if (token->keyword == RID_AT_OPTIONAL)
22255     {
22256       cp_lexer_consume_token (parser->lexer);
22257       objc_set_method_opt (true);
22258     }
22259   else if (token->keyword == RID_AT_REQUIRED)
22260     {
22261       cp_lexer_consume_token (parser->lexer);
22262       objc_set_method_opt (false);
22263     }
22264   else if (token->keyword == RID_NAMESPACE)
22265     cp_parser_namespace_definition (parser);
22266   /* Other stray characters must generate errors.  */
22267   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22268     {
22269       cp_lexer_consume_token (parser->lexer);
22270       error ("stray %qs between Objective-C++ methods",
22271              token->type == CPP_OPEN_BRACE ? "{" : "}");
22272     }
22273   /* Finally, try to parse a block-declaration, or a function-definition.  */
22274   else
22275     cp_parser_block_declaration (parser, /*statement_p=*/false);
22276 }
22277
22278 /* Parse a method signature.  */
22279
22280 static tree
22281 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22282 {
22283   tree rettype, kwdparms, optparms;
22284   bool ellipsis = false;
22285   bool is_class_method;
22286
22287   is_class_method = cp_parser_objc_method_type (parser);
22288   rettype = cp_parser_objc_typename (parser);
22289   *attributes = NULL_TREE;
22290   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22291   if (kwdparms == error_mark_node)
22292     return error_mark_node;
22293   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22294   if (optparms == error_mark_node)
22295     return error_mark_node;
22296
22297   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22298 }
22299
22300 static bool
22301 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22302 {
22303   tree tattr;  
22304   cp_lexer_save_tokens (parser->lexer);
22305   tattr = cp_parser_attributes_opt (parser);
22306   gcc_assert (tattr) ;
22307   
22308   /* If the attributes are followed by a method introducer, this is not allowed.
22309      Dump the attributes and flag the situation.  */
22310   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22311       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22312     return true;
22313
22314   /* Otherwise, the attributes introduce some interstitial code, possibly so
22315      rewind to allow that check.  */
22316   cp_lexer_rollback_tokens (parser->lexer);
22317   return false;  
22318 }
22319
22320 /* Parse an Objective-C method prototype list.  */
22321
22322 static void
22323 cp_parser_objc_method_prototype_list (cp_parser* parser)
22324 {
22325   cp_token *token = cp_lexer_peek_token (parser->lexer);
22326
22327   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22328     {
22329       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22330         {
22331           tree attributes, sig;
22332           bool is_class_method;
22333           if (token->type == CPP_PLUS)
22334             is_class_method = true;
22335           else
22336             is_class_method = false;
22337           sig = cp_parser_objc_method_signature (parser, &attributes);
22338           if (sig == error_mark_node)
22339             {
22340               cp_parser_skip_to_end_of_block_or_statement (parser);
22341               token = cp_lexer_peek_token (parser->lexer);
22342               continue;
22343             }
22344           objc_add_method_declaration (is_class_method, sig, attributes);
22345           cp_parser_consume_semicolon_at_end_of_statement (parser);
22346         }
22347       else if (token->keyword == RID_AT_PROPERTY)
22348         cp_parser_objc_at_property_declaration (parser);
22349       else if (token->keyword == RID_ATTRIBUTE 
22350                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22351         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22352                     OPT_Wattributes, 
22353                     "prefix attributes are ignored for methods");
22354       else
22355         /* Allow for interspersed non-ObjC++ code.  */
22356         cp_parser_objc_interstitial_code (parser);
22357
22358       token = cp_lexer_peek_token (parser->lexer);
22359     }
22360
22361   if (token->type != CPP_EOF)
22362     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22363   else
22364     cp_parser_error (parser, "expected %<@end%>");
22365
22366   objc_finish_interface ();
22367 }
22368
22369 /* Parse an Objective-C method definition list.  */
22370
22371 static void
22372 cp_parser_objc_method_definition_list (cp_parser* parser)
22373 {
22374   cp_token *token = cp_lexer_peek_token (parser->lexer);
22375
22376   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22377     {
22378       tree meth;
22379
22380       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22381         {
22382           cp_token *ptk;
22383           tree sig, attribute;
22384           bool is_class_method;
22385           if (token->type == CPP_PLUS)
22386             is_class_method = true;
22387           else
22388             is_class_method = false;
22389           push_deferring_access_checks (dk_deferred);
22390           sig = cp_parser_objc_method_signature (parser, &attribute);
22391           if (sig == error_mark_node)
22392             {
22393               cp_parser_skip_to_end_of_block_or_statement (parser);
22394               token = cp_lexer_peek_token (parser->lexer);
22395               continue;
22396             }
22397           objc_start_method_definition (is_class_method, sig, attribute,
22398                                         NULL_TREE);
22399
22400           /* For historical reasons, we accept an optional semicolon.  */
22401           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22402             cp_lexer_consume_token (parser->lexer);
22403
22404           ptk = cp_lexer_peek_token (parser->lexer);
22405           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22406                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22407             {
22408               perform_deferred_access_checks ();
22409               stop_deferring_access_checks ();
22410               meth = cp_parser_function_definition_after_declarator (parser,
22411                                                                      false);
22412               pop_deferring_access_checks ();
22413               objc_finish_method_definition (meth);
22414             }
22415         }
22416       /* The following case will be removed once @synthesize is
22417          completely implemented.  */
22418       else if (token->keyword == RID_AT_PROPERTY)
22419         cp_parser_objc_at_property_declaration (parser);
22420       else if (token->keyword == RID_AT_SYNTHESIZE)
22421         cp_parser_objc_at_synthesize_declaration (parser);
22422       else if (token->keyword == RID_AT_DYNAMIC)
22423         cp_parser_objc_at_dynamic_declaration (parser);
22424       else if (token->keyword == RID_ATTRIBUTE 
22425                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22426         warning_at (token->location, OPT_Wattributes,
22427                     "prefix attributes are ignored for methods");
22428       else
22429         /* Allow for interspersed non-ObjC++ code.  */
22430         cp_parser_objc_interstitial_code (parser);
22431
22432       token = cp_lexer_peek_token (parser->lexer);
22433     }
22434
22435   if (token->type != CPP_EOF)
22436     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22437   else
22438     cp_parser_error (parser, "expected %<@end%>");
22439
22440   objc_finish_implementation ();
22441 }
22442
22443 /* Parse Objective-C ivars.  */
22444
22445 static void
22446 cp_parser_objc_class_ivars (cp_parser* parser)
22447 {
22448   cp_token *token = cp_lexer_peek_token (parser->lexer);
22449
22450   if (token->type != CPP_OPEN_BRACE)
22451     return;     /* No ivars specified.  */
22452
22453   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22454   token = cp_lexer_peek_token (parser->lexer);
22455
22456   while (token->type != CPP_CLOSE_BRACE 
22457         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22458     {
22459       cp_decl_specifier_seq declspecs;
22460       int decl_class_or_enum_p;
22461       tree prefix_attributes;
22462
22463       cp_parser_objc_visibility_spec (parser);
22464
22465       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22466         break;
22467
22468       cp_parser_decl_specifier_seq (parser,
22469                                     CP_PARSER_FLAGS_OPTIONAL,
22470                                     &declspecs,
22471                                     &decl_class_or_enum_p);
22472
22473       /* auto, register, static, extern, mutable.  */
22474       if (declspecs.storage_class != sc_none)
22475         {
22476           cp_parser_error (parser, "invalid type for instance variable");         
22477           declspecs.storage_class = sc_none;
22478         }
22479
22480       /* __thread.  */
22481       if (declspecs.specs[(int) ds_thread])
22482         {
22483           cp_parser_error (parser, "invalid type for instance variable");
22484           declspecs.specs[(int) ds_thread] = 0;
22485         }
22486       
22487       /* typedef.  */
22488       if (declspecs.specs[(int) ds_typedef])
22489         {
22490           cp_parser_error (parser, "invalid type for instance variable");
22491           declspecs.specs[(int) ds_typedef] = 0;
22492         }
22493
22494       prefix_attributes = declspecs.attributes;
22495       declspecs.attributes = NULL_TREE;
22496
22497       /* Keep going until we hit the `;' at the end of the
22498          declaration.  */
22499       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22500         {
22501           tree width = NULL_TREE, attributes, first_attribute, decl;
22502           cp_declarator *declarator = NULL;
22503           int ctor_dtor_or_conv_p;
22504
22505           /* Check for a (possibly unnamed) bitfield declaration.  */
22506           token = cp_lexer_peek_token (parser->lexer);
22507           if (token->type == CPP_COLON)
22508             goto eat_colon;
22509
22510           if (token->type == CPP_NAME
22511               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22512                   == CPP_COLON))
22513             {
22514               /* Get the name of the bitfield.  */
22515               declarator = make_id_declarator (NULL_TREE,
22516                                                cp_parser_identifier (parser),
22517                                                sfk_none);
22518
22519              eat_colon:
22520               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22521               /* Get the width of the bitfield.  */
22522               width
22523                 = cp_parser_constant_expression (parser,
22524                                                  /*allow_non_constant=*/false,
22525                                                  NULL);
22526             }
22527           else
22528             {
22529               /* Parse the declarator.  */
22530               declarator
22531                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22532                                         &ctor_dtor_or_conv_p,
22533                                         /*parenthesized_p=*/NULL,
22534                                         /*member_p=*/false);
22535             }
22536
22537           /* Look for attributes that apply to the ivar.  */
22538           attributes = cp_parser_attributes_opt (parser);
22539           /* Remember which attributes are prefix attributes and
22540              which are not.  */
22541           first_attribute = attributes;
22542           /* Combine the attributes.  */
22543           attributes = chainon (prefix_attributes, attributes);
22544
22545           if (width)
22546               /* Create the bitfield declaration.  */
22547               decl = grokbitfield (declarator, &declspecs,
22548                                    width,
22549                                    attributes);
22550           else
22551             decl = grokfield (declarator, &declspecs,
22552                               NULL_TREE, /*init_const_expr_p=*/false,
22553                               NULL_TREE, attributes);
22554
22555           /* Add the instance variable.  */
22556           if (decl != error_mark_node && decl != NULL_TREE)
22557             objc_add_instance_variable (decl);
22558
22559           /* Reset PREFIX_ATTRIBUTES.  */
22560           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22561             attributes = TREE_CHAIN (attributes);
22562           if (attributes)
22563             TREE_CHAIN (attributes) = NULL_TREE;
22564
22565           token = cp_lexer_peek_token (parser->lexer);
22566
22567           if (token->type == CPP_COMMA)
22568             {
22569               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22570               continue;
22571             }
22572           break;
22573         }
22574
22575       cp_parser_consume_semicolon_at_end_of_statement (parser);
22576       token = cp_lexer_peek_token (parser->lexer);
22577     }
22578
22579   if (token->keyword == RID_AT_END)
22580     cp_parser_error (parser, "expected %<}%>");
22581
22582   /* Do not consume the RID_AT_END, so it will be read again as terminating
22583      the @interface of @implementation.  */ 
22584   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22585     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22586     
22587   /* For historical reasons, we accept an optional semicolon.  */
22588   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22589     cp_lexer_consume_token (parser->lexer);
22590 }
22591
22592 /* Parse an Objective-C protocol declaration.  */
22593
22594 static void
22595 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22596 {
22597   tree proto, protorefs;
22598   cp_token *tok;
22599
22600   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22601   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22602     {
22603       tok = cp_lexer_peek_token (parser->lexer);
22604       error_at (tok->location, "identifier expected after %<@protocol%>");
22605       cp_parser_consume_semicolon_at_end_of_statement (parser);
22606       return;
22607     }
22608
22609   /* See if we have a forward declaration or a definition.  */
22610   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22611
22612   /* Try a forward declaration first.  */
22613   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22614     {
22615       while (true)
22616         {
22617           tree id;
22618           
22619           id = cp_parser_identifier (parser);
22620           if (id == error_mark_node)
22621             break;
22622           
22623           objc_declare_protocol (id, attributes);
22624           
22625           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22626             cp_lexer_consume_token (parser->lexer);
22627           else
22628             break;
22629         }
22630       cp_parser_consume_semicolon_at_end_of_statement (parser);
22631     }
22632
22633   /* Ok, we got a full-fledged definition (or at least should).  */
22634   else
22635     {
22636       proto = cp_parser_identifier (parser);
22637       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22638       objc_start_protocol (proto, protorefs, attributes);
22639       cp_parser_objc_method_prototype_list (parser);
22640     }
22641 }
22642
22643 /* Parse an Objective-C superclass or category.  */
22644
22645 static void
22646 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22647                                        bool iface_p,
22648                                        tree *super,
22649                                        tree *categ, bool *is_class_extension)
22650 {
22651   cp_token *next = cp_lexer_peek_token (parser->lexer);
22652
22653   *super = *categ = NULL_TREE;
22654   *is_class_extension = false;
22655   if (next->type == CPP_COLON)
22656     {
22657       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22658       *super = cp_parser_identifier (parser);
22659     }
22660   else if (next->type == CPP_OPEN_PAREN)
22661     {
22662       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22663
22664       /* If there is no category name, and this is an @interface, we
22665          have a class extension.  */
22666       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22667         {
22668           *categ = NULL_TREE;
22669           *is_class_extension = true;
22670         }
22671       else
22672         *categ = cp_parser_identifier (parser);
22673
22674       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22675     }
22676 }
22677
22678 /* Parse an Objective-C class interface.  */
22679
22680 static void
22681 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22682 {
22683   tree name, super, categ, protos;
22684   bool is_class_extension;
22685
22686   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22687   name = cp_parser_identifier (parser);
22688   if (name == error_mark_node)
22689     {
22690       /* It's hard to recover because even if valid @interface stuff
22691          is to follow, we can't compile it (or validate it) if we
22692          don't even know which class it refers to.  Let's assume this
22693          was a stray '@interface' token in the stream and skip it.
22694       */
22695       return;
22696     }
22697   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22698                                          &is_class_extension);
22699   protos = cp_parser_objc_protocol_refs_opt (parser);
22700
22701   /* We have either a class or a category on our hands.  */
22702   if (categ || is_class_extension)
22703     objc_start_category_interface (name, categ, protos, attributes);
22704   else
22705     {
22706       objc_start_class_interface (name, super, protos, attributes);
22707       /* Handle instance variable declarations, if any.  */
22708       cp_parser_objc_class_ivars (parser);
22709       objc_continue_interface ();
22710     }
22711
22712   cp_parser_objc_method_prototype_list (parser);
22713 }
22714
22715 /* Parse an Objective-C class implementation.  */
22716
22717 static void
22718 cp_parser_objc_class_implementation (cp_parser* parser)
22719 {
22720   tree name, super, categ;
22721   bool is_class_extension;
22722
22723   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22724   name = cp_parser_identifier (parser);
22725   if (name == error_mark_node)
22726     {
22727       /* It's hard to recover because even if valid @implementation
22728          stuff is to follow, we can't compile it (or validate it) if
22729          we don't even know which class it refers to.  Let's assume
22730          this was a stray '@implementation' token in the stream and
22731          skip it.
22732       */
22733       return;
22734     }
22735   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22736                                          &is_class_extension);
22737
22738   /* We have either a class or a category on our hands.  */
22739   if (categ)
22740     objc_start_category_implementation (name, categ);
22741   else
22742     {
22743       objc_start_class_implementation (name, super);
22744       /* Handle instance variable declarations, if any.  */
22745       cp_parser_objc_class_ivars (parser);
22746       objc_continue_implementation ();
22747     }
22748
22749   cp_parser_objc_method_definition_list (parser);
22750 }
22751
22752 /* Consume the @end token and finish off the implementation.  */
22753
22754 static void
22755 cp_parser_objc_end_implementation (cp_parser* parser)
22756 {
22757   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22758   objc_finish_implementation ();
22759 }
22760
22761 /* Parse an Objective-C declaration.  */
22762
22763 static void
22764 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22765 {
22766   /* Try to figure out what kind of declaration is present.  */
22767   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22768
22769   if (attributes)
22770     switch (kwd->keyword)
22771       {
22772         case RID_AT_ALIAS:
22773         case RID_AT_CLASS:
22774         case RID_AT_END:
22775           error_at (kwd->location, "attributes may not be specified before"
22776                     " the %<@%D%> Objective-C++ keyword",
22777                     kwd->u.value);
22778           attributes = NULL;
22779           break;
22780         case RID_AT_IMPLEMENTATION:
22781           warning_at (kwd->location, OPT_Wattributes,
22782                       "prefix attributes are ignored before %<@%D%>",
22783                       kwd->u.value);
22784           attributes = NULL;
22785         default:
22786           break;
22787       }
22788
22789   switch (kwd->keyword)
22790     {
22791     case RID_AT_ALIAS:
22792       cp_parser_objc_alias_declaration (parser);
22793       break;
22794     case RID_AT_CLASS:
22795       cp_parser_objc_class_declaration (parser);
22796       break;
22797     case RID_AT_PROTOCOL:
22798       cp_parser_objc_protocol_declaration (parser, attributes);
22799       break;
22800     case RID_AT_INTERFACE:
22801       cp_parser_objc_class_interface (parser, attributes);
22802       break;
22803     case RID_AT_IMPLEMENTATION:
22804       cp_parser_objc_class_implementation (parser);
22805       break;
22806     case RID_AT_END:
22807       cp_parser_objc_end_implementation (parser);
22808       break;
22809     default:
22810       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22811                 kwd->u.value);
22812       cp_parser_skip_to_end_of_block_or_statement (parser);
22813     }
22814 }
22815
22816 /* Parse an Objective-C try-catch-finally statement.
22817
22818    objc-try-catch-finally-stmt:
22819      @try compound-statement objc-catch-clause-seq [opt]
22820        objc-finally-clause [opt]
22821
22822    objc-catch-clause-seq:
22823      objc-catch-clause objc-catch-clause-seq [opt]
22824
22825    objc-catch-clause:
22826      @catch ( objc-exception-declaration ) compound-statement
22827
22828    objc-finally-clause:
22829      @finally compound-statement
22830
22831    objc-exception-declaration:
22832      parameter-declaration
22833      '...'
22834
22835    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22836
22837    Returns NULL_TREE.
22838
22839    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22840    for C.  Keep them in sync.  */   
22841
22842 static tree
22843 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22844 {
22845   location_t location;
22846   tree stmt;
22847
22848   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22849   location = cp_lexer_peek_token (parser->lexer)->location;
22850   objc_maybe_warn_exceptions (location);
22851   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22852      node, lest it get absorbed into the surrounding block.  */
22853   stmt = push_stmt_list ();
22854   cp_parser_compound_statement (parser, NULL, false, false);
22855   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22856
22857   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22858     {
22859       cp_parameter_declarator *parm;
22860       tree parameter_declaration = error_mark_node;
22861       bool seen_open_paren = false;
22862
22863       cp_lexer_consume_token (parser->lexer);
22864       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22865         seen_open_paren = true;
22866       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22867         {
22868           /* We have "@catch (...)" (where the '...' are literally
22869              what is in the code).  Skip the '...'.
22870              parameter_declaration is set to NULL_TREE, and
22871              objc_being_catch_clauses() knows that that means
22872              '...'.  */
22873           cp_lexer_consume_token (parser->lexer);
22874           parameter_declaration = NULL_TREE;
22875         }
22876       else
22877         {
22878           /* We have "@catch (NSException *exception)" or something
22879              like that.  Parse the parameter declaration.  */
22880           parm = cp_parser_parameter_declaration (parser, false, NULL);
22881           if (parm == NULL)
22882             parameter_declaration = error_mark_node;
22883           else
22884             parameter_declaration = grokdeclarator (parm->declarator,
22885                                                     &parm->decl_specifiers,
22886                                                     PARM, /*initialized=*/0,
22887                                                     /*attrlist=*/NULL);
22888         }
22889       if (seen_open_paren)
22890         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22891       else
22892         {
22893           /* If there was no open parenthesis, we are recovering from
22894              an error, and we are trying to figure out what mistake
22895              the user has made.  */
22896
22897           /* If there is an immediate closing parenthesis, the user
22898              probably forgot the opening one (ie, they typed "@catch
22899              NSException *e)".  Parse the closing parenthesis and keep
22900              going.  */
22901           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22902             cp_lexer_consume_token (parser->lexer);
22903           
22904           /* If these is no immediate closing parenthesis, the user
22905              probably doesn't know that parenthesis are required at
22906              all (ie, they typed "@catch NSException *e").  So, just
22907              forget about the closing parenthesis and keep going.  */
22908         }
22909       objc_begin_catch_clause (parameter_declaration);
22910       cp_parser_compound_statement (parser, NULL, false, false);
22911       objc_finish_catch_clause ();
22912     }
22913   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22914     {
22915       cp_lexer_consume_token (parser->lexer);
22916       location = cp_lexer_peek_token (parser->lexer)->location;
22917       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22918          node, lest it get absorbed into the surrounding block.  */
22919       stmt = push_stmt_list ();
22920       cp_parser_compound_statement (parser, NULL, false, false);
22921       objc_build_finally_clause (location, pop_stmt_list (stmt));
22922     }
22923
22924   return objc_finish_try_stmt ();
22925 }
22926
22927 /* Parse an Objective-C synchronized statement.
22928
22929    objc-synchronized-stmt:
22930      @synchronized ( expression ) compound-statement
22931
22932    Returns NULL_TREE.  */
22933
22934 static tree
22935 cp_parser_objc_synchronized_statement (cp_parser *parser)
22936 {
22937   location_t location;
22938   tree lock, stmt;
22939
22940   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22941
22942   location = cp_lexer_peek_token (parser->lexer)->location;
22943   objc_maybe_warn_exceptions (location);
22944   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22945   lock = cp_parser_expression (parser, false, NULL);
22946   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22947
22948   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22949      node, lest it get absorbed into the surrounding block.  */
22950   stmt = push_stmt_list ();
22951   cp_parser_compound_statement (parser, NULL, false, false);
22952
22953   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22954 }
22955
22956 /* Parse an Objective-C throw statement.
22957
22958    objc-throw-stmt:
22959      @throw assignment-expression [opt] ;
22960
22961    Returns a constructed '@throw' statement.  */
22962
22963 static tree
22964 cp_parser_objc_throw_statement (cp_parser *parser)
22965 {
22966   tree expr = NULL_TREE;
22967   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22968
22969   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22970
22971   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22972     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22973
22974   cp_parser_consume_semicolon_at_end_of_statement (parser);
22975
22976   return objc_build_throw_stmt (loc, expr);
22977 }
22978
22979 /* Parse an Objective-C statement.  */
22980
22981 static tree
22982 cp_parser_objc_statement (cp_parser * parser)
22983 {
22984   /* Try to figure out what kind of declaration is present.  */
22985   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22986
22987   switch (kwd->keyword)
22988     {
22989     case RID_AT_TRY:
22990       return cp_parser_objc_try_catch_finally_statement (parser);
22991     case RID_AT_SYNCHRONIZED:
22992       return cp_parser_objc_synchronized_statement (parser);
22993     case RID_AT_THROW:
22994       return cp_parser_objc_throw_statement (parser);
22995     default:
22996       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22997                kwd->u.value);
22998       cp_parser_skip_to_end_of_block_or_statement (parser);
22999     }
23000
23001   return error_mark_node;
23002 }
23003
23004 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
23005    look ahead to see if an objc keyword follows the attributes.  This
23006    is to detect the use of prefix attributes on ObjC @interface and 
23007    @protocol.  */
23008
23009 static bool
23010 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
23011 {
23012   cp_lexer_save_tokens (parser->lexer);
23013   *attrib = cp_parser_attributes_opt (parser);
23014   gcc_assert (*attrib);
23015   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
23016     {
23017       cp_lexer_commit_tokens (parser->lexer);
23018       return true;
23019     }
23020   cp_lexer_rollback_tokens (parser->lexer);
23021   return false;  
23022 }
23023
23024 /* This routine is a minimal replacement for
23025    c_parser_struct_declaration () used when parsing the list of
23026    types/names or ObjC++ properties.  For example, when parsing the
23027    code
23028
23029    @property (readonly) int a, b, c;
23030
23031    this function is responsible for parsing "int a, int b, int c" and
23032    returning the declarations as CHAIN of DECLs.
23033
23034    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
23035    similar parsing.  */
23036 static tree
23037 cp_parser_objc_struct_declaration (cp_parser *parser)
23038 {
23039   tree decls = NULL_TREE;
23040   cp_decl_specifier_seq declspecs;
23041   int decl_class_or_enum_p;
23042   tree prefix_attributes;
23043
23044   cp_parser_decl_specifier_seq (parser,
23045                                 CP_PARSER_FLAGS_NONE,
23046                                 &declspecs,
23047                                 &decl_class_or_enum_p);
23048
23049   if (declspecs.type == error_mark_node)
23050     return error_mark_node;
23051
23052   /* auto, register, static, extern, mutable.  */
23053   if (declspecs.storage_class != sc_none)
23054     {
23055       cp_parser_error (parser, "invalid type for property");
23056       declspecs.storage_class = sc_none;
23057     }
23058   
23059   /* __thread.  */
23060   if (declspecs.specs[(int) ds_thread])
23061     {
23062       cp_parser_error (parser, "invalid type for property");
23063       declspecs.specs[(int) ds_thread] = 0;
23064     }
23065   
23066   /* typedef.  */
23067   if (declspecs.specs[(int) ds_typedef])
23068     {
23069       cp_parser_error (parser, "invalid type for property");
23070       declspecs.specs[(int) ds_typedef] = 0;
23071     }
23072
23073   prefix_attributes = declspecs.attributes;
23074   declspecs.attributes = NULL_TREE;
23075
23076   /* Keep going until we hit the `;' at the end of the declaration. */
23077   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23078     {
23079       tree attributes, first_attribute, decl;
23080       cp_declarator *declarator;
23081       cp_token *token;
23082
23083       /* Parse the declarator.  */
23084       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23085                                          NULL, NULL, false);
23086
23087       /* Look for attributes that apply to the ivar.  */
23088       attributes = cp_parser_attributes_opt (parser);
23089       /* Remember which attributes are prefix attributes and
23090          which are not.  */
23091       first_attribute = attributes;
23092       /* Combine the attributes.  */
23093       attributes = chainon (prefix_attributes, attributes);
23094       
23095       decl = grokfield (declarator, &declspecs,
23096                         NULL_TREE, /*init_const_expr_p=*/false,
23097                         NULL_TREE, attributes);
23098
23099       if (decl == error_mark_node || decl == NULL_TREE)
23100         return error_mark_node;
23101       
23102       /* Reset PREFIX_ATTRIBUTES.  */
23103       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23104         attributes = TREE_CHAIN (attributes);
23105       if (attributes)
23106         TREE_CHAIN (attributes) = NULL_TREE;
23107
23108       DECL_CHAIN (decl) = decls;
23109       decls = decl;
23110
23111       token = cp_lexer_peek_token (parser->lexer);
23112       if (token->type == CPP_COMMA)
23113         {
23114           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23115           continue;
23116         }
23117       else
23118         break;
23119     }
23120   return decls;
23121 }
23122
23123 /* Parse an Objective-C @property declaration.  The syntax is:
23124
23125    objc-property-declaration:
23126      '@property' objc-property-attributes[opt] struct-declaration ;
23127
23128    objc-property-attributes:
23129     '(' objc-property-attribute-list ')'
23130
23131    objc-property-attribute-list:
23132      objc-property-attribute
23133      objc-property-attribute-list, objc-property-attribute
23134
23135    objc-property-attribute
23136      'getter' = identifier
23137      'setter' = identifier
23138      'readonly'
23139      'readwrite'
23140      'assign'
23141      'retain'
23142      'copy'
23143      'nonatomic'
23144
23145   For example:
23146     @property NSString *name;
23147     @property (readonly) id object;
23148     @property (retain, nonatomic, getter=getTheName) id name;
23149     @property int a, b, c;
23150
23151    PS: This function is identical to
23152    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23153 static void 
23154 cp_parser_objc_at_property_declaration (cp_parser *parser)
23155 {
23156   /* The following variables hold the attributes of the properties as
23157      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23158      seen.  When we see an attribute, we set them to 'true' (if they
23159      are boolean properties) or to the identifier (if they have an
23160      argument, ie, for getter and setter).  Note that here we only
23161      parse the list of attributes, check the syntax and accumulate the
23162      attributes that we find.  objc_add_property_declaration() will
23163      then process the information.  */
23164   bool property_assign = false;
23165   bool property_copy = false;
23166   tree property_getter_ident = NULL_TREE;
23167   bool property_nonatomic = false;
23168   bool property_readonly = false;
23169   bool property_readwrite = false;
23170   bool property_retain = false;
23171   tree property_setter_ident = NULL_TREE;
23172
23173   /* 'properties' is the list of properties that we read.  Usually a
23174      single one, but maybe more (eg, in "@property int a, b, c;" there
23175      are three).  */
23176   tree properties;
23177   location_t loc;
23178
23179   loc = cp_lexer_peek_token (parser->lexer)->location;
23180
23181   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23182
23183   /* Parse the optional attribute list...  */
23184   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23185     {
23186       /* Eat the '('.  */
23187       cp_lexer_consume_token (parser->lexer);
23188
23189       while (true)
23190         {
23191           bool syntax_error = false;
23192           cp_token *token = cp_lexer_peek_token (parser->lexer);
23193           enum rid keyword;
23194
23195           if (token->type != CPP_NAME)
23196             {
23197               cp_parser_error (parser, "expected identifier");
23198               break;
23199             }
23200           keyword = C_RID_CODE (token->u.value);
23201           cp_lexer_consume_token (parser->lexer);
23202           switch (keyword)
23203             {
23204             case RID_ASSIGN:    property_assign = true;    break;
23205             case RID_COPY:      property_copy = true;      break;
23206             case RID_NONATOMIC: property_nonatomic = true; break;
23207             case RID_READONLY:  property_readonly = true;  break;
23208             case RID_READWRITE: property_readwrite = true; break;
23209             case RID_RETAIN:    property_retain = true;    break;
23210
23211             case RID_GETTER:
23212             case RID_SETTER:
23213               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23214                 {
23215                   if (keyword == RID_GETTER)
23216                     cp_parser_error (parser,
23217                                      "missing %<=%> (after %<getter%> attribute)");
23218                   else
23219                     cp_parser_error (parser,
23220                                      "missing %<=%> (after %<setter%> attribute)");
23221                   syntax_error = true;
23222                   break;
23223                 }
23224               cp_lexer_consume_token (parser->lexer); /* eat the = */
23225               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
23226                 {
23227                   cp_parser_error (parser, "expected identifier");
23228                   syntax_error = true;
23229                   break;
23230                 }
23231               if (keyword == RID_SETTER)
23232                 {
23233                   if (property_setter_ident != NULL_TREE)
23234                     {
23235                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23236                       cp_lexer_consume_token (parser->lexer);
23237                     }
23238                   else
23239                     property_setter_ident = cp_parser_objc_selector (parser);
23240                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23241                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23242                   else
23243                     cp_lexer_consume_token (parser->lexer);
23244                 }
23245               else
23246                 {
23247                   if (property_getter_ident != NULL_TREE)
23248                     {
23249                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23250                       cp_lexer_consume_token (parser->lexer);
23251                     }
23252                   else
23253                     property_getter_ident = cp_parser_objc_selector (parser);
23254                 }
23255               break;
23256             default:
23257               cp_parser_error (parser, "unknown property attribute");
23258               syntax_error = true;
23259               break;
23260             }
23261
23262           if (syntax_error)
23263             break;
23264
23265           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23266             cp_lexer_consume_token (parser->lexer);
23267           else
23268             break;
23269         }
23270
23271       /* FIXME: "@property (setter, assign);" will generate a spurious
23272          "error: expected â€˜)’ before â€˜,’ token".  This is because
23273          cp_parser_require, unlike the C counterpart, will produce an
23274          error even if we are in error recovery.  */
23275       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23276         {
23277           cp_parser_skip_to_closing_parenthesis (parser,
23278                                                  /*recovering=*/true,
23279                                                  /*or_comma=*/false,
23280                                                  /*consume_paren=*/true);
23281         }
23282     }
23283
23284   /* ... and the property declaration(s).  */
23285   properties = cp_parser_objc_struct_declaration (parser);
23286
23287   if (properties == error_mark_node)
23288     {
23289       cp_parser_skip_to_end_of_statement (parser);
23290       /* If the next token is now a `;', consume it.  */
23291       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23292         cp_lexer_consume_token (parser->lexer);
23293       return;
23294     }
23295
23296   if (properties == NULL_TREE)
23297     cp_parser_error (parser, "expected identifier");
23298   else
23299     {
23300       /* Comma-separated properties are chained together in
23301          reverse order; add them one by one.  */
23302       properties = nreverse (properties);
23303       
23304       for (; properties; properties = TREE_CHAIN (properties))
23305         objc_add_property_declaration (loc, copy_node (properties),
23306                                        property_readonly, property_readwrite,
23307                                        property_assign, property_retain,
23308                                        property_copy, property_nonatomic,
23309                                        property_getter_ident, property_setter_ident);
23310     }
23311   
23312   cp_parser_consume_semicolon_at_end_of_statement (parser);
23313 }
23314
23315 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23316
23317    objc-synthesize-declaration:
23318      @synthesize objc-synthesize-identifier-list ;
23319
23320    objc-synthesize-identifier-list:
23321      objc-synthesize-identifier
23322      objc-synthesize-identifier-list, objc-synthesize-identifier
23323
23324    objc-synthesize-identifier
23325      identifier
23326      identifier = identifier
23327
23328   For example:
23329     @synthesize MyProperty;
23330     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23331
23332   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23333   for C.  Keep them in sync.
23334 */
23335 static void 
23336 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23337 {
23338   tree list = NULL_TREE;
23339   location_t loc;
23340   loc = cp_lexer_peek_token (parser->lexer)->location;
23341
23342   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23343   while (true)
23344     {
23345       tree property, ivar;
23346       property = cp_parser_identifier (parser);
23347       if (property == error_mark_node)
23348         {
23349           cp_parser_consume_semicolon_at_end_of_statement (parser);
23350           return;
23351         }
23352       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23353         {
23354           cp_lexer_consume_token (parser->lexer);
23355           ivar = cp_parser_identifier (parser);
23356           if (ivar == error_mark_node)
23357             {
23358               cp_parser_consume_semicolon_at_end_of_statement (parser);
23359               return;
23360             }
23361         }
23362       else
23363         ivar = NULL_TREE;
23364       list = chainon (list, build_tree_list (ivar, property));
23365       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23366         cp_lexer_consume_token (parser->lexer);
23367       else
23368         break;
23369     }
23370   cp_parser_consume_semicolon_at_end_of_statement (parser);
23371   objc_add_synthesize_declaration (loc, list);
23372 }
23373
23374 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23375
23376    objc-dynamic-declaration:
23377      @dynamic identifier-list ;
23378
23379    For example:
23380      @dynamic MyProperty;
23381      @dynamic MyProperty, AnotherProperty;
23382
23383   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23384   for C.  Keep them in sync.
23385 */
23386 static void 
23387 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23388 {
23389   tree list = NULL_TREE;
23390   location_t loc;
23391   loc = cp_lexer_peek_token (parser->lexer)->location;
23392
23393   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23394   while (true)
23395     {
23396       tree property;
23397       property = cp_parser_identifier (parser);
23398       if (property == error_mark_node)
23399         {
23400           cp_parser_consume_semicolon_at_end_of_statement (parser);
23401           return;
23402         }
23403       list = chainon (list, build_tree_list (NULL, property));
23404       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23405         cp_lexer_consume_token (parser->lexer);
23406       else
23407         break;
23408     }
23409   cp_parser_consume_semicolon_at_end_of_statement (parser);
23410   objc_add_dynamic_declaration (loc, list);
23411 }
23412
23413 \f
23414 /* OpenMP 2.5 parsing routines.  */
23415
23416 /* Returns name of the next clause.
23417    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23418    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23419    returned and the token is consumed.  */
23420
23421 static pragma_omp_clause
23422 cp_parser_omp_clause_name (cp_parser *parser)
23423 {
23424   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23425
23426   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23427     result = PRAGMA_OMP_CLAUSE_IF;
23428   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23429     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23430   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23431     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23432   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23433     {
23434       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23435       const char *p = IDENTIFIER_POINTER (id);
23436
23437       switch (p[0])
23438         {
23439         case 'c':
23440           if (!strcmp ("collapse", p))
23441             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23442           else if (!strcmp ("copyin", p))
23443             result = PRAGMA_OMP_CLAUSE_COPYIN;
23444           else if (!strcmp ("copyprivate", p))
23445             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23446           break;
23447         case 'f':
23448           if (!strcmp ("firstprivate", p))
23449             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23450           break;
23451         case 'l':
23452           if (!strcmp ("lastprivate", p))
23453             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23454           break;
23455         case 'n':
23456           if (!strcmp ("nowait", p))
23457             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23458           else if (!strcmp ("num_threads", p))
23459             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23460           break;
23461         case 'o':
23462           if (!strcmp ("ordered", p))
23463             result = PRAGMA_OMP_CLAUSE_ORDERED;
23464           break;
23465         case 'r':
23466           if (!strcmp ("reduction", p))
23467             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23468           break;
23469         case 's':
23470           if (!strcmp ("schedule", p))
23471             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23472           else if (!strcmp ("shared", p))
23473             result = PRAGMA_OMP_CLAUSE_SHARED;
23474           break;
23475         case 'u':
23476           if (!strcmp ("untied", p))
23477             result = PRAGMA_OMP_CLAUSE_UNTIED;
23478           break;
23479         }
23480     }
23481
23482   if (result != PRAGMA_OMP_CLAUSE_NONE)
23483     cp_lexer_consume_token (parser->lexer);
23484
23485   return result;
23486 }
23487
23488 /* Validate that a clause of the given type does not already exist.  */
23489
23490 static void
23491 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23492                            const char *name, location_t location)
23493 {
23494   tree c;
23495
23496   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23497     if (OMP_CLAUSE_CODE (c) == code)
23498       {
23499         error_at (location, "too many %qs clauses", name);
23500         break;
23501       }
23502 }
23503
23504 /* OpenMP 2.5:
23505    variable-list:
23506      identifier
23507      variable-list , identifier
23508
23509    In addition, we match a closing parenthesis.  An opening parenthesis
23510    will have been consumed by the caller.
23511
23512    If KIND is nonzero, create the appropriate node and install the decl
23513    in OMP_CLAUSE_DECL and add the node to the head of the list.
23514
23515    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23516    return the list created.  */
23517
23518 static tree
23519 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23520                                 tree list)
23521 {
23522   cp_token *token;
23523   while (1)
23524     {
23525       tree name, decl;
23526
23527       token = cp_lexer_peek_token (parser->lexer);
23528       name = cp_parser_id_expression (parser, /*template_p=*/false,
23529                                       /*check_dependency_p=*/true,
23530                                       /*template_p=*/NULL,
23531                                       /*declarator_p=*/false,
23532                                       /*optional_p=*/false);
23533       if (name == error_mark_node)
23534         goto skip_comma;
23535
23536       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23537       if (decl == error_mark_node)
23538         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23539                                      token->location);
23540       else if (kind != 0)
23541         {
23542           tree u = build_omp_clause (token->location, kind);
23543           OMP_CLAUSE_DECL (u) = decl;
23544           OMP_CLAUSE_CHAIN (u) = list;
23545           list = u;
23546         }
23547       else
23548         list = tree_cons (decl, NULL_TREE, list);
23549
23550     get_comma:
23551       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23552         break;
23553       cp_lexer_consume_token (parser->lexer);
23554     }
23555
23556   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23557     {
23558       int ending;
23559
23560       /* Try to resync to an unnested comma.  Copied from
23561          cp_parser_parenthesized_expression_list.  */
23562     skip_comma:
23563       ending = cp_parser_skip_to_closing_parenthesis (parser,
23564                                                       /*recovering=*/true,
23565                                                       /*or_comma=*/true,
23566                                                       /*consume_paren=*/true);
23567       if (ending < 0)
23568         goto get_comma;
23569     }
23570
23571   return list;
23572 }
23573
23574 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23575    common case for omp clauses.  */
23576
23577 static tree
23578 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23579 {
23580   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23581     return cp_parser_omp_var_list_no_open (parser, kind, list);
23582   return list;
23583 }
23584
23585 /* OpenMP 3.0:
23586    collapse ( constant-expression ) */
23587
23588 static tree
23589 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23590 {
23591   tree c, num;
23592   location_t loc;
23593   HOST_WIDE_INT n;
23594
23595   loc = cp_lexer_peek_token (parser->lexer)->location;
23596   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23597     return list;
23598
23599   num = cp_parser_constant_expression (parser, false, NULL);
23600
23601   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23602     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23603                                            /*or_comma=*/false,
23604                                            /*consume_paren=*/true);
23605
23606   if (num == error_mark_node)
23607     return list;
23608   num = fold_non_dependent_expr (num);
23609   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23610       || !host_integerp (num, 0)
23611       || (n = tree_low_cst (num, 0)) <= 0
23612       || (int) n != n)
23613     {
23614       error_at (loc, "collapse argument needs positive constant integer expression");
23615       return list;
23616     }
23617
23618   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23619   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23620   OMP_CLAUSE_CHAIN (c) = list;
23621   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23622
23623   return c;
23624 }
23625
23626 /* OpenMP 2.5:
23627    default ( shared | none ) */
23628
23629 static tree
23630 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23631 {
23632   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23633   tree c;
23634
23635   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23636     return list;
23637   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23638     {
23639       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23640       const char *p = IDENTIFIER_POINTER (id);
23641
23642       switch (p[0])
23643         {
23644         case 'n':
23645           if (strcmp ("none", p) != 0)
23646             goto invalid_kind;
23647           kind = OMP_CLAUSE_DEFAULT_NONE;
23648           break;
23649
23650         case 's':
23651           if (strcmp ("shared", p) != 0)
23652             goto invalid_kind;
23653           kind = OMP_CLAUSE_DEFAULT_SHARED;
23654           break;
23655
23656         default:
23657           goto invalid_kind;
23658         }
23659
23660       cp_lexer_consume_token (parser->lexer);
23661     }
23662   else
23663     {
23664     invalid_kind:
23665       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23666     }
23667
23668   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23669     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23670                                            /*or_comma=*/false,
23671                                            /*consume_paren=*/true);
23672
23673   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23674     return list;
23675
23676   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23677   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23678   OMP_CLAUSE_CHAIN (c) = list;
23679   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23680
23681   return c;
23682 }
23683
23684 /* OpenMP 2.5:
23685    if ( expression ) */
23686
23687 static tree
23688 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23689 {
23690   tree t, c;
23691
23692   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23693     return list;
23694
23695   t = cp_parser_condition (parser);
23696
23697   if (t == error_mark_node
23698       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23699     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23700                                            /*or_comma=*/false,
23701                                            /*consume_paren=*/true);
23702
23703   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23704
23705   c = build_omp_clause (location, OMP_CLAUSE_IF);
23706   OMP_CLAUSE_IF_EXPR (c) = t;
23707   OMP_CLAUSE_CHAIN (c) = list;
23708
23709   return c;
23710 }
23711
23712 /* OpenMP 2.5:
23713    nowait */
23714
23715 static tree
23716 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23717                              tree list, location_t location)
23718 {
23719   tree c;
23720
23721   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23722
23723   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23724   OMP_CLAUSE_CHAIN (c) = list;
23725   return c;
23726 }
23727
23728 /* OpenMP 2.5:
23729    num_threads ( expression ) */
23730
23731 static tree
23732 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23733                                   location_t location)
23734 {
23735   tree t, c;
23736
23737   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23738     return list;
23739
23740   t = cp_parser_expression (parser, false, NULL);
23741
23742   if (t == error_mark_node
23743       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23744     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23745                                            /*or_comma=*/false,
23746                                            /*consume_paren=*/true);
23747
23748   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23749                              "num_threads", location);
23750
23751   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23752   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23753   OMP_CLAUSE_CHAIN (c) = list;
23754
23755   return c;
23756 }
23757
23758 /* OpenMP 2.5:
23759    ordered */
23760
23761 static tree
23762 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23763                               tree list, location_t location)
23764 {
23765   tree c;
23766
23767   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23768                              "ordered", location);
23769
23770   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23771   OMP_CLAUSE_CHAIN (c) = list;
23772   return c;
23773 }
23774
23775 /* OpenMP 2.5:
23776    reduction ( reduction-operator : variable-list )
23777
23778    reduction-operator:
23779      One of: + * - & ^ | && || */
23780
23781 static tree
23782 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23783 {
23784   enum tree_code code;
23785   tree nlist, c;
23786
23787   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23788     return list;
23789
23790   switch (cp_lexer_peek_token (parser->lexer)->type)
23791     {
23792     case CPP_PLUS:
23793       code = PLUS_EXPR;
23794       break;
23795     case CPP_MULT:
23796       code = MULT_EXPR;
23797       break;
23798     case CPP_MINUS:
23799       code = MINUS_EXPR;
23800       break;
23801     case CPP_AND:
23802       code = BIT_AND_EXPR;
23803       break;
23804     case CPP_XOR:
23805       code = BIT_XOR_EXPR;
23806       break;
23807     case CPP_OR:
23808       code = BIT_IOR_EXPR;
23809       break;
23810     case CPP_AND_AND:
23811       code = TRUTH_ANDIF_EXPR;
23812       break;
23813     case CPP_OR_OR:
23814       code = TRUTH_ORIF_EXPR;
23815       break;
23816     default:
23817       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23818                                "%<|%>, %<&&%>, or %<||%>");
23819     resync_fail:
23820       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23821                                              /*or_comma=*/false,
23822                                              /*consume_paren=*/true);
23823       return list;
23824     }
23825   cp_lexer_consume_token (parser->lexer);
23826
23827   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23828     goto resync_fail;
23829
23830   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23831   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23832     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23833
23834   return nlist;
23835 }
23836
23837 /* OpenMP 2.5:
23838    schedule ( schedule-kind )
23839    schedule ( schedule-kind , expression )
23840
23841    schedule-kind:
23842      static | dynamic | guided | runtime | auto  */
23843
23844 static tree
23845 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23846 {
23847   tree c, t;
23848
23849   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23850     return list;
23851
23852   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23853
23854   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23855     {
23856       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23857       const char *p = IDENTIFIER_POINTER (id);
23858
23859       switch (p[0])
23860         {
23861         case 'd':
23862           if (strcmp ("dynamic", p) != 0)
23863             goto invalid_kind;
23864           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23865           break;
23866
23867         case 'g':
23868           if (strcmp ("guided", p) != 0)
23869             goto invalid_kind;
23870           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23871           break;
23872
23873         case 'r':
23874           if (strcmp ("runtime", p) != 0)
23875             goto invalid_kind;
23876           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23877           break;
23878
23879         default:
23880           goto invalid_kind;
23881         }
23882     }
23883   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23884     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23885   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23886     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23887   else
23888     goto invalid_kind;
23889   cp_lexer_consume_token (parser->lexer);
23890
23891   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23892     {
23893       cp_token *token;
23894       cp_lexer_consume_token (parser->lexer);
23895
23896       token = cp_lexer_peek_token (parser->lexer);
23897       t = cp_parser_assignment_expression (parser, false, NULL);
23898
23899       if (t == error_mark_node)
23900         goto resync_fail;
23901       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23902         error_at (token->location, "schedule %<runtime%> does not take "
23903                   "a %<chunk_size%> parameter");
23904       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23905         error_at (token->location, "schedule %<auto%> does not take "
23906                   "a %<chunk_size%> parameter");
23907       else
23908         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23909
23910       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23911         goto resync_fail;
23912     }
23913   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23914     goto resync_fail;
23915
23916   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23917   OMP_CLAUSE_CHAIN (c) = list;
23918   return c;
23919
23920  invalid_kind:
23921   cp_parser_error (parser, "invalid schedule kind");
23922  resync_fail:
23923   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23924                                          /*or_comma=*/false,
23925                                          /*consume_paren=*/true);
23926   return list;
23927 }
23928
23929 /* OpenMP 3.0:
23930    untied */
23931
23932 static tree
23933 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23934                              tree list, location_t location)
23935 {
23936   tree c;
23937
23938   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23939
23940   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23941   OMP_CLAUSE_CHAIN (c) = list;
23942   return c;
23943 }
23944
23945 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23946    is a bitmask in MASK.  Return the list of clauses found; the result
23947    of clause default goes in *pdefault.  */
23948
23949 static tree
23950 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23951                            const char *where, cp_token *pragma_tok)
23952 {
23953   tree clauses = NULL;
23954   bool first = true;
23955   cp_token *token = NULL;
23956
23957   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23958     {
23959       pragma_omp_clause c_kind;
23960       const char *c_name;
23961       tree prev = clauses;
23962
23963       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23964         cp_lexer_consume_token (parser->lexer);
23965
23966       token = cp_lexer_peek_token (parser->lexer);
23967       c_kind = cp_parser_omp_clause_name (parser);
23968       first = false;
23969
23970       switch (c_kind)
23971         {
23972         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23973           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23974                                                    token->location);
23975           c_name = "collapse";
23976           break;
23977         case PRAGMA_OMP_CLAUSE_COPYIN:
23978           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23979           c_name = "copyin";
23980           break;
23981         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23982           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23983                                             clauses);
23984           c_name = "copyprivate";
23985           break;
23986         case PRAGMA_OMP_CLAUSE_DEFAULT:
23987           clauses = cp_parser_omp_clause_default (parser, clauses,
23988                                                   token->location);
23989           c_name = "default";
23990           break;
23991         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23992           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23993                                             clauses);
23994           c_name = "firstprivate";
23995           break;
23996         case PRAGMA_OMP_CLAUSE_IF:
23997           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23998           c_name = "if";
23999           break;
24000         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
24001           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
24002                                             clauses);
24003           c_name = "lastprivate";
24004           break;
24005         case PRAGMA_OMP_CLAUSE_NOWAIT:
24006           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
24007           c_name = "nowait";
24008           break;
24009         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
24010           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
24011                                                       token->location);
24012           c_name = "num_threads";
24013           break;
24014         case PRAGMA_OMP_CLAUSE_ORDERED:
24015           clauses = cp_parser_omp_clause_ordered (parser, clauses,
24016                                                   token->location);
24017           c_name = "ordered";
24018           break;
24019         case PRAGMA_OMP_CLAUSE_PRIVATE:
24020           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
24021                                             clauses);
24022           c_name = "private";
24023           break;
24024         case PRAGMA_OMP_CLAUSE_REDUCTION:
24025           clauses = cp_parser_omp_clause_reduction (parser, clauses);
24026           c_name = "reduction";
24027           break;
24028         case PRAGMA_OMP_CLAUSE_SCHEDULE:
24029           clauses = cp_parser_omp_clause_schedule (parser, clauses,
24030                                                    token->location);
24031           c_name = "schedule";
24032           break;
24033         case PRAGMA_OMP_CLAUSE_SHARED:
24034           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
24035                                             clauses);
24036           c_name = "shared";
24037           break;
24038         case PRAGMA_OMP_CLAUSE_UNTIED:
24039           clauses = cp_parser_omp_clause_untied (parser, clauses,
24040                                                  token->location);
24041           c_name = "nowait";
24042           break;
24043         default:
24044           cp_parser_error (parser, "expected %<#pragma omp%> clause");
24045           goto saw_error;
24046         }
24047
24048       if (((mask >> c_kind) & 1) == 0)
24049         {
24050           /* Remove the invalid clause(s) from the list to avoid
24051              confusing the rest of the compiler.  */
24052           clauses = prev;
24053           error_at (token->location, "%qs is not valid for %qs", c_name, where);
24054         }
24055     }
24056  saw_error:
24057   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24058   return finish_omp_clauses (clauses);
24059 }
24060
24061 /* OpenMP 2.5:
24062    structured-block:
24063      statement
24064
24065    In practice, we're also interested in adding the statement to an
24066    outer node.  So it is convenient if we work around the fact that
24067    cp_parser_statement calls add_stmt.  */
24068
24069 static unsigned
24070 cp_parser_begin_omp_structured_block (cp_parser *parser)
24071 {
24072   unsigned save = parser->in_statement;
24073
24074   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24075      This preserves the "not within loop or switch" style error messages
24076      for nonsense cases like
24077         void foo() {
24078         #pragma omp single
24079           break;
24080         }
24081   */
24082   if (parser->in_statement)
24083     parser->in_statement = IN_OMP_BLOCK;
24084
24085   return save;
24086 }
24087
24088 static void
24089 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24090 {
24091   parser->in_statement = save;
24092 }
24093
24094 static tree
24095 cp_parser_omp_structured_block (cp_parser *parser)
24096 {
24097   tree stmt = begin_omp_structured_block ();
24098   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24099
24100   cp_parser_statement (parser, NULL_TREE, false, NULL);
24101
24102   cp_parser_end_omp_structured_block (parser, save);
24103   return finish_omp_structured_block (stmt);
24104 }
24105
24106 /* OpenMP 2.5:
24107    # pragma omp atomic new-line
24108      expression-stmt
24109
24110    expression-stmt:
24111      x binop= expr | x++ | ++x | x-- | --x
24112    binop:
24113      +, *, -, /, &, ^, |, <<, >>
24114
24115   where x is an lvalue expression with scalar type.  */
24116
24117 static void
24118 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24119 {
24120   tree lhs, rhs;
24121   enum tree_code code;
24122
24123   cp_parser_require_pragma_eol (parser, pragma_tok);
24124
24125   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24126                                     /*cast_p=*/false, NULL);
24127   switch (TREE_CODE (lhs))
24128     {
24129     case ERROR_MARK:
24130       goto saw_error;
24131
24132     case PREINCREMENT_EXPR:
24133     case POSTINCREMENT_EXPR:
24134       lhs = TREE_OPERAND (lhs, 0);
24135       code = PLUS_EXPR;
24136       rhs = integer_one_node;
24137       break;
24138
24139     case PREDECREMENT_EXPR:
24140     case POSTDECREMENT_EXPR:
24141       lhs = TREE_OPERAND (lhs, 0);
24142       code = MINUS_EXPR;
24143       rhs = integer_one_node;
24144       break;
24145
24146     case COMPOUND_EXPR:
24147       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24148          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24149          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24150          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24151          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24152                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24153             == BOOLEAN_TYPE)
24154        /* Undo effects of boolean_increment for post {in,de}crement.  */
24155        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24156       /* FALLTHRU */
24157     case MODIFY_EXPR:
24158       if (TREE_CODE (lhs) == MODIFY_EXPR
24159          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24160        {
24161          /* Undo effects of boolean_increment.  */
24162          if (integer_onep (TREE_OPERAND (lhs, 1)))
24163            {
24164              /* This is pre or post increment.  */
24165              rhs = TREE_OPERAND (lhs, 1);
24166              lhs = TREE_OPERAND (lhs, 0);
24167              code = NOP_EXPR;
24168              break;
24169            }
24170        }
24171       /* FALLTHRU */
24172     default:
24173       switch (cp_lexer_peek_token (parser->lexer)->type)
24174         {
24175         case CPP_MULT_EQ:
24176           code = MULT_EXPR;
24177           break;
24178         case CPP_DIV_EQ:
24179           code = TRUNC_DIV_EXPR;
24180           break;
24181         case CPP_PLUS_EQ:
24182           code = PLUS_EXPR;
24183           break;
24184         case CPP_MINUS_EQ:
24185           code = MINUS_EXPR;
24186           break;
24187         case CPP_LSHIFT_EQ:
24188           code = LSHIFT_EXPR;
24189           break;
24190         case CPP_RSHIFT_EQ:
24191           code = RSHIFT_EXPR;
24192           break;
24193         case CPP_AND_EQ:
24194           code = BIT_AND_EXPR;
24195           break;
24196         case CPP_OR_EQ:
24197           code = BIT_IOR_EXPR;
24198           break;
24199         case CPP_XOR_EQ:
24200           code = BIT_XOR_EXPR;
24201           break;
24202         default:
24203           cp_parser_error (parser,
24204                            "invalid operator for %<#pragma omp atomic%>");
24205           goto saw_error;
24206         }
24207       cp_lexer_consume_token (parser->lexer);
24208
24209       rhs = cp_parser_expression (parser, false, NULL);
24210       if (rhs == error_mark_node)
24211         goto saw_error;
24212       break;
24213     }
24214   finish_omp_atomic (code, lhs, rhs);
24215   cp_parser_consume_semicolon_at_end_of_statement (parser);
24216   return;
24217
24218  saw_error:
24219   cp_parser_skip_to_end_of_block_or_statement (parser);
24220 }
24221
24222
24223 /* OpenMP 2.5:
24224    # pragma omp barrier new-line  */
24225
24226 static void
24227 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24228 {
24229   cp_parser_require_pragma_eol (parser, pragma_tok);
24230   finish_omp_barrier ();
24231 }
24232
24233 /* OpenMP 2.5:
24234    # pragma omp critical [(name)] new-line
24235      structured-block  */
24236
24237 static tree
24238 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24239 {
24240   tree stmt, name = NULL;
24241
24242   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24243     {
24244       cp_lexer_consume_token (parser->lexer);
24245
24246       name = cp_parser_identifier (parser);
24247
24248       if (name == error_mark_node
24249           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24250         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24251                                                /*or_comma=*/false,
24252                                                /*consume_paren=*/true);
24253       if (name == error_mark_node)
24254         name = NULL;
24255     }
24256   cp_parser_require_pragma_eol (parser, pragma_tok);
24257
24258   stmt = cp_parser_omp_structured_block (parser);
24259   return c_finish_omp_critical (input_location, stmt, name);
24260 }
24261
24262 /* OpenMP 2.5:
24263    # pragma omp flush flush-vars[opt] new-line
24264
24265    flush-vars:
24266      ( variable-list ) */
24267
24268 static void
24269 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24270 {
24271   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24272     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24273   cp_parser_require_pragma_eol (parser, pragma_tok);
24274
24275   finish_omp_flush ();
24276 }
24277
24278 /* Helper function, to parse omp for increment expression.  */
24279
24280 static tree
24281 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24282 {
24283   tree cond = cp_parser_binary_expression (parser, false, true,
24284                                            PREC_NOT_OPERATOR, NULL);
24285   if (cond == error_mark_node
24286       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24287     {
24288       cp_parser_skip_to_end_of_statement (parser);
24289       return error_mark_node;
24290     }
24291
24292   switch (TREE_CODE (cond))
24293     {
24294     case GT_EXPR:
24295     case GE_EXPR:
24296     case LT_EXPR:
24297     case LE_EXPR:
24298       break;
24299     default:
24300       return error_mark_node;
24301     }
24302
24303   /* If decl is an iterator, preserve LHS and RHS of the relational
24304      expr until finish_omp_for.  */
24305   if (decl
24306       && (type_dependent_expression_p (decl)
24307           || CLASS_TYPE_P (TREE_TYPE (decl))))
24308     return cond;
24309
24310   return build_x_binary_op (TREE_CODE (cond),
24311                             TREE_OPERAND (cond, 0), ERROR_MARK,
24312                             TREE_OPERAND (cond, 1), ERROR_MARK,
24313                             /*overload=*/NULL, tf_warning_or_error);
24314 }
24315
24316 /* Helper function, to parse omp for increment expression.  */
24317
24318 static tree
24319 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24320 {
24321   cp_token *token = cp_lexer_peek_token (parser->lexer);
24322   enum tree_code op;
24323   tree lhs, rhs;
24324   cp_id_kind idk;
24325   bool decl_first;
24326
24327   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24328     {
24329       op = (token->type == CPP_PLUS_PLUS
24330             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24331       cp_lexer_consume_token (parser->lexer);
24332       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24333       if (lhs != decl)
24334         return error_mark_node;
24335       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24336     }
24337
24338   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24339   if (lhs != decl)
24340     return error_mark_node;
24341
24342   token = cp_lexer_peek_token (parser->lexer);
24343   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24344     {
24345       op = (token->type == CPP_PLUS_PLUS
24346             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24347       cp_lexer_consume_token (parser->lexer);
24348       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24349     }
24350
24351   op = cp_parser_assignment_operator_opt (parser);
24352   if (op == ERROR_MARK)
24353     return error_mark_node;
24354
24355   if (op != NOP_EXPR)
24356     {
24357       rhs = cp_parser_assignment_expression (parser, false, NULL);
24358       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24359       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24360     }
24361
24362   lhs = cp_parser_binary_expression (parser, false, false,
24363                                      PREC_ADDITIVE_EXPRESSION, NULL);
24364   token = cp_lexer_peek_token (parser->lexer);
24365   decl_first = lhs == decl;
24366   if (decl_first)
24367     lhs = NULL_TREE;
24368   if (token->type != CPP_PLUS
24369       && token->type != CPP_MINUS)
24370     return error_mark_node;
24371
24372   do
24373     {
24374       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24375       cp_lexer_consume_token (parser->lexer);
24376       rhs = cp_parser_binary_expression (parser, false, false,
24377                                          PREC_ADDITIVE_EXPRESSION, NULL);
24378       token = cp_lexer_peek_token (parser->lexer);
24379       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24380         {
24381           if (lhs == NULL_TREE)
24382             {
24383               if (op == PLUS_EXPR)
24384                 lhs = rhs;
24385               else
24386                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24387             }
24388           else
24389             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24390                                      NULL, tf_warning_or_error);
24391         }
24392     }
24393   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24394
24395   if (!decl_first)
24396     {
24397       if (rhs != decl || op == MINUS_EXPR)
24398         return error_mark_node;
24399       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24400     }
24401   else
24402     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24403
24404   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24405 }
24406
24407 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24408
24409 static tree
24410 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24411 {
24412   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24413   tree real_decl, initv, condv, incrv, declv;
24414   tree this_pre_body, cl;
24415   location_t loc_first;
24416   bool collapse_err = false;
24417   int i, collapse = 1, nbraces = 0;
24418   VEC(tree,gc) *for_block = make_tree_vector ();
24419
24420   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24421     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24422       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24423
24424   gcc_assert (collapse >= 1);
24425
24426   declv = make_tree_vec (collapse);
24427   initv = make_tree_vec (collapse);
24428   condv = make_tree_vec (collapse);
24429   incrv = make_tree_vec (collapse);
24430
24431   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24432
24433   for (i = 0; i < collapse; i++)
24434     {
24435       int bracecount = 0;
24436       bool add_private_clause = false;
24437       location_t loc;
24438
24439       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24440         {
24441           cp_parser_error (parser, "for statement expected");
24442           return NULL;
24443         }
24444       loc = cp_lexer_consume_token (parser->lexer)->location;
24445
24446       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24447         return NULL;
24448
24449       init = decl = real_decl = NULL;
24450       this_pre_body = push_stmt_list ();
24451       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24452         {
24453           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24454
24455              init-expr:
24456                        var = lb
24457                        integer-type var = lb
24458                        random-access-iterator-type var = lb
24459                        pointer-type var = lb
24460           */
24461           cp_decl_specifier_seq type_specifiers;
24462
24463           /* First, try to parse as an initialized declaration.  See
24464              cp_parser_condition, from whence the bulk of this is copied.  */
24465
24466           cp_parser_parse_tentatively (parser);
24467           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24468                                         /*is_trailing_return=*/false,
24469                                         &type_specifiers);
24470           if (cp_parser_parse_definitely (parser))
24471             {
24472               /* If parsing a type specifier seq succeeded, then this
24473                  MUST be a initialized declaration.  */
24474               tree asm_specification, attributes;
24475               cp_declarator *declarator;
24476
24477               declarator = cp_parser_declarator (parser,
24478                                                  CP_PARSER_DECLARATOR_NAMED,
24479                                                  /*ctor_dtor_or_conv_p=*/NULL,
24480                                                  /*parenthesized_p=*/NULL,
24481                                                  /*member_p=*/false);
24482               attributes = cp_parser_attributes_opt (parser);
24483               asm_specification = cp_parser_asm_specification_opt (parser);
24484
24485               if (declarator == cp_error_declarator) 
24486                 cp_parser_skip_to_end_of_statement (parser);
24487
24488               else 
24489                 {
24490                   tree pushed_scope, auto_node;
24491
24492                   decl = start_decl (declarator, &type_specifiers,
24493                                      SD_INITIALIZED, attributes,
24494                                      /*prefix_attributes=*/NULL_TREE,
24495                                      &pushed_scope);
24496
24497                   auto_node = type_uses_auto (TREE_TYPE (decl));
24498                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24499                     {
24500                       if (cp_lexer_next_token_is (parser->lexer, 
24501                                                   CPP_OPEN_PAREN))
24502                         error ("parenthesized initialization is not allowed in "
24503                                "OpenMP %<for%> loop");
24504                       else
24505                         /* Trigger an error.  */
24506                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24507
24508                       init = error_mark_node;
24509                       cp_parser_skip_to_end_of_statement (parser);
24510                     }
24511                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24512                            || type_dependent_expression_p (decl)
24513                            || auto_node)
24514                     {
24515                       bool is_direct_init, is_non_constant_init;
24516
24517                       init = cp_parser_initializer (parser,
24518                                                     &is_direct_init,
24519                                                     &is_non_constant_init);
24520
24521                       if (auto_node)
24522                         {
24523                           TREE_TYPE (decl)
24524                             = do_auto_deduction (TREE_TYPE (decl), init,
24525                                                  auto_node);
24526
24527                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24528                               && !type_dependent_expression_p (decl))
24529                             goto non_class;
24530                         }
24531                       
24532                       cp_finish_decl (decl, init, !is_non_constant_init,
24533                                       asm_specification,
24534                                       LOOKUP_ONLYCONVERTING);
24535                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24536                         {
24537                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24538                           init = NULL_TREE;
24539                         }
24540                       else
24541                         init = pop_stmt_list (this_pre_body);
24542                       this_pre_body = NULL_TREE;
24543                     }
24544                   else
24545                     {
24546                       /* Consume '='.  */
24547                       cp_lexer_consume_token (parser->lexer);
24548                       init = cp_parser_assignment_expression (parser, false, NULL);
24549
24550                     non_class:
24551                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24552                         init = error_mark_node;
24553                       else
24554                         cp_finish_decl (decl, NULL_TREE,
24555                                         /*init_const_expr_p=*/false,
24556                                         asm_specification,
24557                                         LOOKUP_ONLYCONVERTING);
24558                     }
24559
24560                   if (pushed_scope)
24561                     pop_scope (pushed_scope);
24562                 }
24563             }
24564           else 
24565             {
24566               cp_id_kind idk;
24567               /* If parsing a type specifier sequence failed, then
24568                  this MUST be a simple expression.  */
24569               cp_parser_parse_tentatively (parser);
24570               decl = cp_parser_primary_expression (parser, false, false,
24571                                                    false, &idk);
24572               if (!cp_parser_error_occurred (parser)
24573                   && decl
24574                   && DECL_P (decl)
24575                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24576                 {
24577                   tree rhs;
24578
24579                   cp_parser_parse_definitely (parser);
24580                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24581                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24582                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24583                                                          rhs,
24584                                                          tf_warning_or_error));
24585                   add_private_clause = true;
24586                 }
24587               else
24588                 {
24589                   decl = NULL;
24590                   cp_parser_abort_tentative_parse (parser);
24591                   init = cp_parser_expression (parser, false, NULL);
24592                   if (init)
24593                     {
24594                       if (TREE_CODE (init) == MODIFY_EXPR
24595                           || TREE_CODE (init) == MODOP_EXPR)
24596                         real_decl = TREE_OPERAND (init, 0);
24597                     }
24598                 }
24599             }
24600         }
24601       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24602       if (this_pre_body)
24603         {
24604           this_pre_body = pop_stmt_list (this_pre_body);
24605           if (pre_body)
24606             {
24607               tree t = pre_body;
24608               pre_body = push_stmt_list ();
24609               add_stmt (t);
24610               add_stmt (this_pre_body);
24611               pre_body = pop_stmt_list (pre_body);
24612             }
24613           else
24614             pre_body = this_pre_body;
24615         }
24616
24617       if (decl)
24618         real_decl = decl;
24619       if (par_clauses != NULL && real_decl != NULL_TREE)
24620         {
24621           tree *c;
24622           for (c = par_clauses; *c ; )
24623             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24624                 && OMP_CLAUSE_DECL (*c) == real_decl)
24625               {
24626                 error_at (loc, "iteration variable %qD"
24627                           " should not be firstprivate", real_decl);
24628                 *c = OMP_CLAUSE_CHAIN (*c);
24629               }
24630             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24631                      && OMP_CLAUSE_DECL (*c) == real_decl)
24632               {
24633                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24634                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24635                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24636                 OMP_CLAUSE_DECL (l) = real_decl;
24637                 OMP_CLAUSE_CHAIN (l) = clauses;
24638                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24639                 clauses = l;
24640                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24641                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24642                 add_private_clause = false;
24643               }
24644             else
24645               {
24646                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24647                     && OMP_CLAUSE_DECL (*c) == real_decl)
24648                   add_private_clause = false;
24649                 c = &OMP_CLAUSE_CHAIN (*c);
24650               }
24651         }
24652
24653       if (add_private_clause)
24654         {
24655           tree c;
24656           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24657             {
24658               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24659                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24660                   && OMP_CLAUSE_DECL (c) == decl)
24661                 break;
24662               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24663                        && OMP_CLAUSE_DECL (c) == decl)
24664                 error_at (loc, "iteration variable %qD "
24665                           "should not be firstprivate",
24666                           decl);
24667               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24668                        && OMP_CLAUSE_DECL (c) == decl)
24669                 error_at (loc, "iteration variable %qD should not be reduction",
24670                           decl);
24671             }
24672           if (c == NULL)
24673             {
24674               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24675               OMP_CLAUSE_DECL (c) = decl;
24676               c = finish_omp_clauses (c);
24677               if (c)
24678                 {
24679                   OMP_CLAUSE_CHAIN (c) = clauses;
24680                   clauses = c;
24681                 }
24682             }
24683         }
24684
24685       cond = NULL;
24686       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24687         cond = cp_parser_omp_for_cond (parser, decl);
24688       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24689
24690       incr = NULL;
24691       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24692         {
24693           /* If decl is an iterator, preserve the operator on decl
24694              until finish_omp_for.  */
24695           if (decl
24696               && ((type_dependent_expression_p (decl)
24697                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24698                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24699             incr = cp_parser_omp_for_incr (parser, decl);
24700           else
24701             incr = cp_parser_expression (parser, false, NULL);
24702         }
24703
24704       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24705         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24706                                                /*or_comma=*/false,
24707                                                /*consume_paren=*/true);
24708
24709       TREE_VEC_ELT (declv, i) = decl;
24710       TREE_VEC_ELT (initv, i) = init;
24711       TREE_VEC_ELT (condv, i) = cond;
24712       TREE_VEC_ELT (incrv, i) = incr;
24713
24714       if (i == collapse - 1)
24715         break;
24716
24717       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24718          in between the collapsed for loops to be still considered perfectly
24719          nested.  Hopefully the final version clarifies this.
24720          For now handle (multiple) {'s and empty statements.  */
24721       cp_parser_parse_tentatively (parser);
24722       do
24723         {
24724           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24725             break;
24726           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24727             {
24728               cp_lexer_consume_token (parser->lexer);
24729               bracecount++;
24730             }
24731           else if (bracecount
24732                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24733             cp_lexer_consume_token (parser->lexer);
24734           else
24735             {
24736               loc = cp_lexer_peek_token (parser->lexer)->location;
24737               error_at (loc, "not enough collapsed for loops");
24738               collapse_err = true;
24739               cp_parser_abort_tentative_parse (parser);
24740               declv = NULL_TREE;
24741               break;
24742             }
24743         }
24744       while (1);
24745
24746       if (declv)
24747         {
24748           cp_parser_parse_definitely (parser);
24749           nbraces += bracecount;
24750         }
24751     }
24752
24753   /* Note that we saved the original contents of this flag when we entered
24754      the structured block, and so we don't need to re-save it here.  */
24755   parser->in_statement = IN_OMP_FOR;
24756
24757   /* Note that the grammar doesn't call for a structured block here,
24758      though the loop as a whole is a structured block.  */
24759   body = push_stmt_list ();
24760   cp_parser_statement (parser, NULL_TREE, false, NULL);
24761   body = pop_stmt_list (body);
24762
24763   if (declv == NULL_TREE)
24764     ret = NULL_TREE;
24765   else
24766     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24767                           pre_body, clauses);
24768
24769   while (nbraces)
24770     {
24771       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24772         {
24773           cp_lexer_consume_token (parser->lexer);
24774           nbraces--;
24775         }
24776       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24777         cp_lexer_consume_token (parser->lexer);
24778       else
24779         {
24780           if (!collapse_err)
24781             {
24782               error_at (cp_lexer_peek_token (parser->lexer)->location,
24783                         "collapsed loops not perfectly nested");
24784             }
24785           collapse_err = true;
24786           cp_parser_statement_seq_opt (parser, NULL);
24787           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24788             break;
24789         }
24790     }
24791
24792   while (!VEC_empty (tree, for_block))
24793     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24794   release_tree_vector (for_block);
24795
24796   return ret;
24797 }
24798
24799 /* OpenMP 2.5:
24800    #pragma omp for for-clause[optseq] new-line
24801      for-loop  */
24802
24803 #define OMP_FOR_CLAUSE_MASK                             \
24804         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24805         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24806         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24807         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24808         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24809         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24810         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24811         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24812
24813 static tree
24814 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24815 {
24816   tree clauses, sb, ret;
24817   unsigned int save;
24818
24819   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24820                                        "#pragma omp for", pragma_tok);
24821
24822   sb = begin_omp_structured_block ();
24823   save = cp_parser_begin_omp_structured_block (parser);
24824
24825   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24826
24827   cp_parser_end_omp_structured_block (parser, save);
24828   add_stmt (finish_omp_structured_block (sb));
24829
24830   return ret;
24831 }
24832
24833 /* OpenMP 2.5:
24834    # pragma omp master new-line
24835      structured-block  */
24836
24837 static tree
24838 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24839 {
24840   cp_parser_require_pragma_eol (parser, pragma_tok);
24841   return c_finish_omp_master (input_location,
24842                               cp_parser_omp_structured_block (parser));
24843 }
24844
24845 /* OpenMP 2.5:
24846    # pragma omp ordered new-line
24847      structured-block  */
24848
24849 static tree
24850 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24851 {
24852   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24853   cp_parser_require_pragma_eol (parser, pragma_tok);
24854   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24855 }
24856
24857 /* OpenMP 2.5:
24858
24859    section-scope:
24860      { section-sequence }
24861
24862    section-sequence:
24863      section-directive[opt] structured-block
24864      section-sequence section-directive structured-block  */
24865
24866 static tree
24867 cp_parser_omp_sections_scope (cp_parser *parser)
24868 {
24869   tree stmt, substmt;
24870   bool error_suppress = false;
24871   cp_token *tok;
24872
24873   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24874     return NULL_TREE;
24875
24876   stmt = push_stmt_list ();
24877
24878   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24879     {
24880       unsigned save;
24881
24882       substmt = begin_omp_structured_block ();
24883       save = cp_parser_begin_omp_structured_block (parser);
24884
24885       while (1)
24886         {
24887           cp_parser_statement (parser, NULL_TREE, false, NULL);
24888
24889           tok = cp_lexer_peek_token (parser->lexer);
24890           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24891             break;
24892           if (tok->type == CPP_CLOSE_BRACE)
24893             break;
24894           if (tok->type == CPP_EOF)
24895             break;
24896         }
24897
24898       cp_parser_end_omp_structured_block (parser, save);
24899       substmt = finish_omp_structured_block (substmt);
24900       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24901       add_stmt (substmt);
24902     }
24903
24904   while (1)
24905     {
24906       tok = cp_lexer_peek_token (parser->lexer);
24907       if (tok->type == CPP_CLOSE_BRACE)
24908         break;
24909       if (tok->type == CPP_EOF)
24910         break;
24911
24912       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24913         {
24914           cp_lexer_consume_token (parser->lexer);
24915           cp_parser_require_pragma_eol (parser, tok);
24916           error_suppress = false;
24917         }
24918       else if (!error_suppress)
24919         {
24920           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24921           error_suppress = true;
24922         }
24923
24924       substmt = cp_parser_omp_structured_block (parser);
24925       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24926       add_stmt (substmt);
24927     }
24928   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24929
24930   substmt = pop_stmt_list (stmt);
24931
24932   stmt = make_node (OMP_SECTIONS);
24933   TREE_TYPE (stmt) = void_type_node;
24934   OMP_SECTIONS_BODY (stmt) = substmt;
24935
24936   add_stmt (stmt);
24937   return stmt;
24938 }
24939
24940 /* OpenMP 2.5:
24941    # pragma omp sections sections-clause[optseq] newline
24942      sections-scope  */
24943
24944 #define OMP_SECTIONS_CLAUSE_MASK                        \
24945         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24946         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24947         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24948         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24949         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24950
24951 static tree
24952 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24953 {
24954   tree clauses, ret;
24955
24956   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24957                                        "#pragma omp sections", pragma_tok);
24958
24959   ret = cp_parser_omp_sections_scope (parser);
24960   if (ret)
24961     OMP_SECTIONS_CLAUSES (ret) = clauses;
24962
24963   return ret;
24964 }
24965
24966 /* OpenMP 2.5:
24967    # pragma parallel parallel-clause new-line
24968    # pragma parallel for parallel-for-clause new-line
24969    # pragma parallel sections parallel-sections-clause new-line  */
24970
24971 #define OMP_PARALLEL_CLAUSE_MASK                        \
24972         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24973         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24974         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24975         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24976         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24977         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24978         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24979         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24980
24981 static tree
24982 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24983 {
24984   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24985   const char *p_name = "#pragma omp parallel";
24986   tree stmt, clauses, par_clause, ws_clause, block;
24987   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24988   unsigned int save;
24989   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24990
24991   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24992     {
24993       cp_lexer_consume_token (parser->lexer);
24994       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24995       p_name = "#pragma omp parallel for";
24996       mask |= OMP_FOR_CLAUSE_MASK;
24997       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24998     }
24999   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25000     {
25001       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25002       const char *p = IDENTIFIER_POINTER (id);
25003       if (strcmp (p, "sections") == 0)
25004         {
25005           cp_lexer_consume_token (parser->lexer);
25006           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
25007           p_name = "#pragma omp parallel sections";
25008           mask |= OMP_SECTIONS_CLAUSE_MASK;
25009           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
25010         }
25011     }
25012
25013   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
25014   block = begin_omp_parallel ();
25015   save = cp_parser_begin_omp_structured_block (parser);
25016
25017   switch (p_kind)
25018     {
25019     case PRAGMA_OMP_PARALLEL:
25020       cp_parser_statement (parser, NULL_TREE, false, NULL);
25021       par_clause = clauses;
25022       break;
25023
25024     case PRAGMA_OMP_PARALLEL_FOR:
25025       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25026       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
25027       break;
25028
25029     case PRAGMA_OMP_PARALLEL_SECTIONS:
25030       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
25031       stmt = cp_parser_omp_sections_scope (parser);
25032       if (stmt)
25033         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
25034       break;
25035
25036     default:
25037       gcc_unreachable ();
25038     }
25039
25040   cp_parser_end_omp_structured_block (parser, save);
25041   stmt = finish_omp_parallel (par_clause, block);
25042   if (p_kind != PRAGMA_OMP_PARALLEL)
25043     OMP_PARALLEL_COMBINED (stmt) = 1;
25044   return stmt;
25045 }
25046
25047 /* OpenMP 2.5:
25048    # pragma omp single single-clause[optseq] new-line
25049      structured-block  */
25050
25051 #define OMP_SINGLE_CLAUSE_MASK                          \
25052         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25053         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25054         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
25055         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25056
25057 static tree
25058 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25059 {
25060   tree stmt = make_node (OMP_SINGLE);
25061   TREE_TYPE (stmt) = void_type_node;
25062
25063   OMP_SINGLE_CLAUSES (stmt)
25064     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25065                                  "#pragma omp single", pragma_tok);
25066   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25067
25068   return add_stmt (stmt);
25069 }
25070
25071 /* OpenMP 3.0:
25072    # pragma omp task task-clause[optseq] new-line
25073      structured-block  */
25074
25075 #define OMP_TASK_CLAUSE_MASK                            \
25076         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25077         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25078         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25079         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25080         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25081         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25082
25083 static tree
25084 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25085 {
25086   tree clauses, block;
25087   unsigned int save;
25088
25089   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25090                                        "#pragma omp task", pragma_tok);
25091   block = begin_omp_task ();
25092   save = cp_parser_begin_omp_structured_block (parser);
25093   cp_parser_statement (parser, NULL_TREE, false, NULL);
25094   cp_parser_end_omp_structured_block (parser, save);
25095   return finish_omp_task (clauses, block);
25096 }
25097
25098 /* OpenMP 3.0:
25099    # pragma omp taskwait new-line  */
25100
25101 static void
25102 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25103 {
25104   cp_parser_require_pragma_eol (parser, pragma_tok);
25105   finish_omp_taskwait ();
25106 }
25107
25108 /* OpenMP 2.5:
25109    # pragma omp threadprivate (variable-list) */
25110
25111 static void
25112 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25113 {
25114   tree vars;
25115
25116   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25117   cp_parser_require_pragma_eol (parser, pragma_tok);
25118
25119   finish_omp_threadprivate (vars);
25120 }
25121
25122 /* Main entry point to OpenMP statement pragmas.  */
25123
25124 static void
25125 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25126 {
25127   tree stmt;
25128
25129   switch (pragma_tok->pragma_kind)
25130     {
25131     case PRAGMA_OMP_ATOMIC:
25132       cp_parser_omp_atomic (parser, pragma_tok);
25133       return;
25134     case PRAGMA_OMP_CRITICAL:
25135       stmt = cp_parser_omp_critical (parser, pragma_tok);
25136       break;
25137     case PRAGMA_OMP_FOR:
25138       stmt = cp_parser_omp_for (parser, pragma_tok);
25139       break;
25140     case PRAGMA_OMP_MASTER:
25141       stmt = cp_parser_omp_master (parser, pragma_tok);
25142       break;
25143     case PRAGMA_OMP_ORDERED:
25144       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25145       break;
25146     case PRAGMA_OMP_PARALLEL:
25147       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25148       break;
25149     case PRAGMA_OMP_SECTIONS:
25150       stmt = cp_parser_omp_sections (parser, pragma_tok);
25151       break;
25152     case PRAGMA_OMP_SINGLE:
25153       stmt = cp_parser_omp_single (parser, pragma_tok);
25154       break;
25155     case PRAGMA_OMP_TASK:
25156       stmt = cp_parser_omp_task (parser, pragma_tok);
25157       break;
25158     default:
25159       gcc_unreachable ();
25160     }
25161
25162   if (stmt)
25163     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25164 }
25165 \f
25166 /* The parser.  */
25167
25168 static GTY (()) cp_parser *the_parser;
25169
25170 \f
25171 /* Special handling for the first token or line in the file.  The first
25172    thing in the file might be #pragma GCC pch_preprocess, which loads a
25173    PCH file, which is a GC collection point.  So we need to handle this
25174    first pragma without benefit of an existing lexer structure.
25175
25176    Always returns one token to the caller in *FIRST_TOKEN.  This is
25177    either the true first token of the file, or the first token after
25178    the initial pragma.  */
25179
25180 static void
25181 cp_parser_initial_pragma (cp_token *first_token)
25182 {
25183   tree name = NULL;
25184
25185   cp_lexer_get_preprocessor_token (NULL, first_token);
25186   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25187     return;
25188
25189   cp_lexer_get_preprocessor_token (NULL, first_token);
25190   if (first_token->type == CPP_STRING)
25191     {
25192       name = first_token->u.value;
25193
25194       cp_lexer_get_preprocessor_token (NULL, first_token);
25195       if (first_token->type != CPP_PRAGMA_EOL)
25196         error_at (first_token->location,
25197                   "junk at end of %<#pragma GCC pch_preprocess%>");
25198     }
25199   else
25200     error_at (first_token->location, "expected string literal");
25201
25202   /* Skip to the end of the pragma.  */
25203   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25204     cp_lexer_get_preprocessor_token (NULL, first_token);
25205
25206   /* Now actually load the PCH file.  */
25207   if (name)
25208     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25209
25210   /* Read one more token to return to our caller.  We have to do this
25211      after reading the PCH file in, since its pointers have to be
25212      live.  */
25213   cp_lexer_get_preprocessor_token (NULL, first_token);
25214 }
25215
25216 /* Normal parsing of a pragma token.  Here we can (and must) use the
25217    regular lexer.  */
25218
25219 static bool
25220 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25221 {
25222   cp_token *pragma_tok;
25223   unsigned int id;
25224
25225   pragma_tok = cp_lexer_consume_token (parser->lexer);
25226   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25227   parser->lexer->in_pragma = true;
25228
25229   id = pragma_tok->pragma_kind;
25230   switch (id)
25231     {
25232     case PRAGMA_GCC_PCH_PREPROCESS:
25233       error_at (pragma_tok->location,
25234                 "%<#pragma GCC pch_preprocess%> must be first");
25235       break;
25236
25237     case PRAGMA_OMP_BARRIER:
25238       switch (context)
25239         {
25240         case pragma_compound:
25241           cp_parser_omp_barrier (parser, pragma_tok);
25242           return false;
25243         case pragma_stmt:
25244           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25245                     "used in compound statements");
25246           break;
25247         default:
25248           goto bad_stmt;
25249         }
25250       break;
25251
25252     case PRAGMA_OMP_FLUSH:
25253       switch (context)
25254         {
25255         case pragma_compound:
25256           cp_parser_omp_flush (parser, pragma_tok);
25257           return false;
25258         case pragma_stmt:
25259           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25260                     "used in compound statements");
25261           break;
25262         default:
25263           goto bad_stmt;
25264         }
25265       break;
25266
25267     case PRAGMA_OMP_TASKWAIT:
25268       switch (context)
25269         {
25270         case pragma_compound:
25271           cp_parser_omp_taskwait (parser, pragma_tok);
25272           return false;
25273         case pragma_stmt:
25274           error_at (pragma_tok->location,
25275                     "%<#pragma omp taskwait%> may only be "
25276                     "used in compound statements");
25277           break;
25278         default:
25279           goto bad_stmt;
25280         }
25281       break;
25282
25283     case PRAGMA_OMP_THREADPRIVATE:
25284       cp_parser_omp_threadprivate (parser, pragma_tok);
25285       return false;
25286
25287     case PRAGMA_OMP_ATOMIC:
25288     case PRAGMA_OMP_CRITICAL:
25289     case PRAGMA_OMP_FOR:
25290     case PRAGMA_OMP_MASTER:
25291     case PRAGMA_OMP_ORDERED:
25292     case PRAGMA_OMP_PARALLEL:
25293     case PRAGMA_OMP_SECTIONS:
25294     case PRAGMA_OMP_SINGLE:
25295     case PRAGMA_OMP_TASK:
25296       if (context == pragma_external)
25297         goto bad_stmt;
25298       cp_parser_omp_construct (parser, pragma_tok);
25299       return true;
25300
25301     case PRAGMA_OMP_SECTION:
25302       error_at (pragma_tok->location, 
25303                 "%<#pragma omp section%> may only be used in "
25304                 "%<#pragma omp sections%> construct");
25305       break;
25306
25307     default:
25308       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25309       c_invoke_pragma_handler (id);
25310       break;
25311
25312     bad_stmt:
25313       cp_parser_error (parser, "expected declaration specifiers");
25314       break;
25315     }
25316
25317   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25318   return false;
25319 }
25320
25321 /* The interface the pragma parsers have to the lexer.  */
25322
25323 enum cpp_ttype
25324 pragma_lex (tree *value)
25325 {
25326   cp_token *tok;
25327   enum cpp_ttype ret;
25328
25329   tok = cp_lexer_peek_token (the_parser->lexer);
25330
25331   ret = tok->type;
25332   *value = tok->u.value;
25333
25334   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25335     ret = CPP_EOF;
25336   else if (ret == CPP_STRING)
25337     *value = cp_parser_string_literal (the_parser, false, false);
25338   else
25339     {
25340       cp_lexer_consume_token (the_parser->lexer);
25341       if (ret == CPP_KEYWORD)
25342         ret = CPP_NAME;
25343     }
25344
25345   return ret;
25346 }
25347
25348 \f
25349 /* External interface.  */
25350
25351 /* Parse one entire translation unit.  */
25352
25353 void
25354 c_parse_file (void)
25355 {
25356   static bool already_called = false;
25357
25358   if (already_called)
25359     {
25360       sorry ("inter-module optimizations not implemented for C++");
25361       return;
25362     }
25363   already_called = true;
25364
25365   the_parser = cp_parser_new ();
25366   push_deferring_access_checks (flag_access_control
25367                                 ? dk_no_deferred : dk_no_check);
25368   cp_parser_translation_unit (the_parser);
25369   the_parser = NULL;
25370 }
25371
25372 #include "gt-cp-parser.h"