OSDN Git Service

135ab145fb86989544acedbd061ec61ff467155c
[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 void 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 *);
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                 || constructor_name_p (token->u.value, scope)))
4073           {
4074             cp_lexer_consume_token (parser->lexer);
4075             return build_nt (BIT_NOT_EXPR, scope);
4076           }
4077
4078         /* If there was an explicit qualification (S::~T), first look
4079            in the scope given by the qualification (i.e., S).
4080
4081            Note: in the calls to cp_parser_class_name below we pass
4082            typename_type so that lookup finds the injected-class-name
4083            rather than the constructor.  */
4084         done = false;
4085         type_decl = NULL_TREE;
4086         if (scope)
4087           {
4088             cp_parser_parse_tentatively (parser);
4089             type_decl = cp_parser_class_name (parser,
4090                                               /*typename_keyword_p=*/false,
4091                                               /*template_keyword_p=*/false,
4092                                               typename_type,
4093                                               /*check_dependency=*/false,
4094                                               /*class_head_p=*/false,
4095                                               declarator_p);
4096             if (cp_parser_parse_definitely (parser))
4097               done = true;
4098           }
4099         /* In "N::S::~S", look in "N" as well.  */
4100         if (!done && scope && qualifying_scope)
4101           {
4102             cp_parser_parse_tentatively (parser);
4103             parser->scope = qualifying_scope;
4104             parser->object_scope = NULL_TREE;
4105             parser->qualifying_scope = NULL_TREE;
4106             type_decl
4107               = cp_parser_class_name (parser,
4108                                       /*typename_keyword_p=*/false,
4109                                       /*template_keyword_p=*/false,
4110                                       typename_type,
4111                                       /*check_dependency=*/false,
4112                                       /*class_head_p=*/false,
4113                                       declarator_p);
4114             if (cp_parser_parse_definitely (parser))
4115               done = true;
4116           }
4117         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4118         else if (!done && object_scope)
4119           {
4120             cp_parser_parse_tentatively (parser);
4121             parser->scope = object_scope;
4122             parser->object_scope = NULL_TREE;
4123             parser->qualifying_scope = NULL_TREE;
4124             type_decl
4125               = cp_parser_class_name (parser,
4126                                       /*typename_keyword_p=*/false,
4127                                       /*template_keyword_p=*/false,
4128                                       typename_type,
4129                                       /*check_dependency=*/false,
4130                                       /*class_head_p=*/false,
4131                                       declarator_p);
4132             if (cp_parser_parse_definitely (parser))
4133               done = true;
4134           }
4135         /* Look in the surrounding context.  */
4136         if (!done)
4137           {
4138             parser->scope = NULL_TREE;
4139             parser->object_scope = NULL_TREE;
4140             parser->qualifying_scope = NULL_TREE;
4141             if (processing_template_decl)
4142               cp_parser_parse_tentatively (parser);
4143             type_decl
4144               = cp_parser_class_name (parser,
4145                                       /*typename_keyword_p=*/false,
4146                                       /*template_keyword_p=*/false,
4147                                       typename_type,
4148                                       /*check_dependency=*/false,
4149                                       /*class_head_p=*/false,
4150                                       declarator_p);
4151             if (processing_template_decl
4152                 && ! cp_parser_parse_definitely (parser))
4153               {
4154                 /* We couldn't find a type with this name, so just accept
4155                    it and check for a match at instantiation time.  */
4156                 type_decl = cp_parser_identifier (parser);
4157                 if (type_decl != error_mark_node)
4158                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4159                 return type_decl;
4160               }
4161           }
4162         /* If an error occurred, assume that the name of the
4163            destructor is the same as the name of the qualifying
4164            class.  That allows us to keep parsing after running
4165            into ill-formed destructor names.  */
4166         if (type_decl == error_mark_node && scope)
4167           return build_nt (BIT_NOT_EXPR, scope);
4168         else if (type_decl == error_mark_node)
4169           return error_mark_node;
4170
4171         /* Check that destructor name and scope match.  */
4172         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4173           {
4174             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4175               error_at (token->location,
4176                         "declaration of %<~%T%> as member of %qT",
4177                         type_decl, scope);
4178             cp_parser_simulate_error (parser);
4179             return error_mark_node;
4180           }
4181
4182         /* [class.dtor]
4183
4184            A typedef-name that names a class shall not be used as the
4185            identifier in the declarator for a destructor declaration.  */
4186         if (declarator_p
4187             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4188             && !DECL_SELF_REFERENCE_P (type_decl)
4189             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4190           error_at (token->location,
4191                     "typedef-name %qD used as destructor declarator",
4192                     type_decl);
4193
4194         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4195       }
4196
4197     case CPP_KEYWORD:
4198       if (token->keyword == RID_OPERATOR)
4199         {
4200           tree id;
4201
4202           /* This could be a template-id, so we try that first.  */
4203           cp_parser_parse_tentatively (parser);
4204           /* Try a template-id.  */
4205           id = cp_parser_template_id (parser, template_keyword_p,
4206                                       /*check_dependency_p=*/true,
4207                                       declarator_p);
4208           /* If that worked, we're done.  */
4209           if (cp_parser_parse_definitely (parser))
4210             return id;
4211           /* We still don't know whether we're looking at an
4212              operator-function-id or a conversion-function-id.  */
4213           cp_parser_parse_tentatively (parser);
4214           /* Try an operator-function-id.  */
4215           id = cp_parser_operator_function_id (parser);
4216           /* If that didn't work, try a conversion-function-id.  */
4217           if (!cp_parser_parse_definitely (parser))
4218             id = cp_parser_conversion_function_id (parser);
4219
4220           return id;
4221         }
4222       /* Fall through.  */
4223
4224     default:
4225       if (optional_p)
4226         return NULL_TREE;
4227       cp_parser_error (parser, "expected unqualified-id");
4228       return error_mark_node;
4229     }
4230 }
4231
4232 /* Parse an (optional) nested-name-specifier.
4233
4234    nested-name-specifier: [C++98]
4235      class-or-namespace-name :: nested-name-specifier [opt]
4236      class-or-namespace-name :: template nested-name-specifier [opt]
4237
4238    nested-name-specifier: [C++0x]
4239      type-name ::
4240      namespace-name ::
4241      nested-name-specifier identifier ::
4242      nested-name-specifier template [opt] simple-template-id ::
4243
4244    PARSER->SCOPE should be set appropriately before this function is
4245    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4246    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4247    in name lookups.
4248
4249    Sets PARSER->SCOPE to the class (TYPE) or namespace
4250    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4251    it unchanged if there is no nested-name-specifier.  Returns the new
4252    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4253
4254    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4255    part of a declaration and/or decl-specifier.  */
4256
4257 static tree
4258 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4259                                      bool typename_keyword_p,
4260                                      bool check_dependency_p,
4261                                      bool type_p,
4262                                      bool is_declaration)
4263 {
4264   bool success = false;
4265   cp_token_position start = 0;
4266   cp_token *token;
4267
4268   /* Remember where the nested-name-specifier starts.  */
4269   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4270     {
4271       start = cp_lexer_token_position (parser->lexer, false);
4272       push_deferring_access_checks (dk_deferred);
4273     }
4274
4275   while (true)
4276     {
4277       tree new_scope;
4278       tree old_scope;
4279       tree saved_qualifying_scope;
4280       bool template_keyword_p;
4281
4282       /* Spot cases that cannot be the beginning of a
4283          nested-name-specifier.  */
4284       token = cp_lexer_peek_token (parser->lexer);
4285
4286       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4287          the already parsed nested-name-specifier.  */
4288       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4289         {
4290           /* Grab the nested-name-specifier and continue the loop.  */
4291           cp_parser_pre_parsed_nested_name_specifier (parser);
4292           /* If we originally encountered this nested-name-specifier
4293              with IS_DECLARATION set to false, we will not have
4294              resolved TYPENAME_TYPEs, so we must do so here.  */
4295           if (is_declaration
4296               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4297             {
4298               new_scope = resolve_typename_type (parser->scope,
4299                                                  /*only_current_p=*/false);
4300               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4301                 parser->scope = new_scope;
4302             }
4303           success = true;
4304           continue;
4305         }
4306
4307       /* Spot cases that cannot be the beginning of a
4308          nested-name-specifier.  On the second and subsequent times
4309          through the loop, we look for the `template' keyword.  */
4310       if (success && token->keyword == RID_TEMPLATE)
4311         ;
4312       /* A template-id can start a nested-name-specifier.  */
4313       else if (token->type == CPP_TEMPLATE_ID)
4314         ;
4315       else
4316         {
4317           /* If the next token is not an identifier, then it is
4318              definitely not a type-name or namespace-name.  */
4319           if (token->type != CPP_NAME)
4320             break;
4321           /* If the following token is neither a `<' (to begin a
4322              template-id), nor a `::', then we are not looking at a
4323              nested-name-specifier.  */
4324           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4325
4326           if (token->type == CPP_COLON
4327               && parser->colon_corrects_to_scope_p
4328               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4329             {
4330               error_at (token->location,
4331                         "found %<:%> in nested-name-specifier, expected %<::%>");
4332               token->type = CPP_SCOPE;
4333             }
4334
4335           if (token->type != CPP_SCOPE
4336               && !cp_parser_nth_token_starts_template_argument_list_p
4337                   (parser, 2))
4338             break;
4339         }
4340
4341       /* The nested-name-specifier is optional, so we parse
4342          tentatively.  */
4343       cp_parser_parse_tentatively (parser);
4344
4345       /* Look for the optional `template' keyword, if this isn't the
4346          first time through the loop.  */
4347       if (success)
4348         template_keyword_p = cp_parser_optional_template_keyword (parser);
4349       else
4350         template_keyword_p = false;
4351
4352       /* Save the old scope since the name lookup we are about to do
4353          might destroy it.  */
4354       old_scope = parser->scope;
4355       saved_qualifying_scope = parser->qualifying_scope;
4356       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4357          look up names in "X<T>::I" in order to determine that "Y" is
4358          a template.  So, if we have a typename at this point, we make
4359          an effort to look through it.  */
4360       if (is_declaration
4361           && !typename_keyword_p
4362           && parser->scope
4363           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4364         parser->scope = resolve_typename_type (parser->scope,
4365                                                /*only_current_p=*/false);
4366       /* Parse the qualifying entity.  */
4367       new_scope
4368         = cp_parser_qualifying_entity (parser,
4369                                        typename_keyword_p,
4370                                        template_keyword_p,
4371                                        check_dependency_p,
4372                                        type_p,
4373                                        is_declaration);
4374       /* Look for the `::' token.  */
4375       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4376
4377       /* If we found what we wanted, we keep going; otherwise, we're
4378          done.  */
4379       if (!cp_parser_parse_definitely (parser))
4380         {
4381           bool error_p = false;
4382
4383           /* Restore the OLD_SCOPE since it was valid before the
4384              failed attempt at finding the last
4385              class-or-namespace-name.  */
4386           parser->scope = old_scope;
4387           parser->qualifying_scope = saved_qualifying_scope;
4388           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4389             break;
4390           /* If the next token is an identifier, and the one after
4391              that is a `::', then any valid interpretation would have
4392              found a class-or-namespace-name.  */
4393           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4394                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4395                      == CPP_SCOPE)
4396                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4397                      != CPP_COMPL))
4398             {
4399               token = cp_lexer_consume_token (parser->lexer);
4400               if (!error_p)
4401                 {
4402                   if (!token->ambiguous_p)
4403                     {
4404                       tree decl;
4405                       tree ambiguous_decls;
4406
4407                       decl = cp_parser_lookup_name (parser, token->u.value,
4408                                                     none_type,
4409                                                     /*is_template=*/false,
4410                                                     /*is_namespace=*/false,
4411                                                     /*check_dependency=*/true,
4412                                                     &ambiguous_decls,
4413                                                     token->location);
4414                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4415                         error_at (token->location,
4416                                   "%qD used without template parameters",
4417                                   decl);
4418                       else if (ambiguous_decls)
4419                         {
4420                           error_at (token->location,
4421                                     "reference to %qD is ambiguous",
4422                                     token->u.value);
4423                           print_candidates (ambiguous_decls);
4424                           decl = error_mark_node;
4425                         }
4426                       else
4427                         {
4428                           if (cxx_dialect != cxx98)
4429                             cp_parser_name_lookup_error
4430                             (parser, token->u.value, decl, NLE_NOT_CXX98,
4431                              token->location);
4432                           else
4433                             cp_parser_name_lookup_error
4434                             (parser, token->u.value, decl, NLE_CXX98,
4435                              token->location);
4436                         }
4437                     }
4438                   parser->scope = error_mark_node;
4439                   error_p = true;
4440                   /* Treat this as a successful nested-name-specifier
4441                      due to:
4442
4443                      [basic.lookup.qual]
4444
4445                      If the name found is not a class-name (clause
4446                      _class_) or namespace-name (_namespace.def_), the
4447                      program is ill-formed.  */
4448                   success = true;
4449                 }
4450               cp_lexer_consume_token (parser->lexer);
4451             }
4452           break;
4453         }
4454       /* We've found one valid nested-name-specifier.  */
4455       success = true;
4456       /* Name lookup always gives us a DECL.  */
4457       if (TREE_CODE (new_scope) == TYPE_DECL)
4458         new_scope = TREE_TYPE (new_scope);
4459       /* Uses of "template" must be followed by actual templates.  */
4460       if (template_keyword_p
4461           && !(CLASS_TYPE_P (new_scope)
4462                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
4463                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
4464                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
4465           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
4466                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
4467                    == TEMPLATE_ID_EXPR)))
4468         permerror (input_location, TYPE_P (new_scope)
4469                    ? "%qT is not a template"
4470                    : "%qD is not a template",
4471                    new_scope);
4472       /* If it is a class scope, try to complete it; we are about to
4473          be looking up names inside the class.  */
4474       if (TYPE_P (new_scope)
4475           /* Since checking types for dependency can be expensive,
4476              avoid doing it if the type is already complete.  */
4477           && !COMPLETE_TYPE_P (new_scope)
4478           /* Do not try to complete dependent types.  */
4479           && !dependent_type_p (new_scope))
4480         {
4481           new_scope = complete_type (new_scope);
4482           /* If it is a typedef to current class, use the current
4483              class instead, as the typedef won't have any names inside
4484              it yet.  */
4485           if (!COMPLETE_TYPE_P (new_scope)
4486               && currently_open_class (new_scope))
4487             new_scope = TYPE_MAIN_VARIANT (new_scope);
4488         }
4489       /* Make sure we look in the right scope the next time through
4490          the loop.  */
4491       parser->scope = new_scope;
4492     }
4493
4494   /* If parsing tentatively, replace the sequence of tokens that makes
4495      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
4496      token.  That way, should we re-parse the token stream, we will
4497      not have to repeat the effort required to do the parse, nor will
4498      we issue duplicate error messages.  */
4499   if (success && start)
4500     {
4501       cp_token *token;
4502
4503       token = cp_lexer_token_at (parser->lexer, start);
4504       /* Reset the contents of the START token.  */
4505       token->type = CPP_NESTED_NAME_SPECIFIER;
4506       /* Retrieve any deferred checks.  Do not pop this access checks yet
4507          so the memory will not be reclaimed during token replacing below.  */
4508       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
4509       token->u.tree_check_value->value = parser->scope;
4510       token->u.tree_check_value->checks = get_deferred_access_checks ();
4511       token->u.tree_check_value->qualifying_scope =
4512         parser->qualifying_scope;
4513       token->keyword = RID_MAX;
4514
4515       /* Purge all subsequent tokens.  */
4516       cp_lexer_purge_tokens_after (parser->lexer, start);
4517     }
4518
4519   if (start)
4520     pop_to_parent_deferring_access_checks ();
4521
4522   return success ? parser->scope : NULL_TREE;
4523 }
4524
4525 /* Parse a nested-name-specifier.  See
4526    cp_parser_nested_name_specifier_opt for details.  This function
4527    behaves identically, except that it will an issue an error if no
4528    nested-name-specifier is present.  */
4529
4530 static tree
4531 cp_parser_nested_name_specifier (cp_parser *parser,
4532                                  bool typename_keyword_p,
4533                                  bool check_dependency_p,
4534                                  bool type_p,
4535                                  bool is_declaration)
4536 {
4537   tree scope;
4538
4539   /* Look for the nested-name-specifier.  */
4540   scope = cp_parser_nested_name_specifier_opt (parser,
4541                                                typename_keyword_p,
4542                                                check_dependency_p,
4543                                                type_p,
4544                                                is_declaration);
4545   /* If it was not present, issue an error message.  */
4546   if (!scope)
4547     {
4548       cp_parser_error (parser, "expected nested-name-specifier");
4549       parser->scope = NULL_TREE;
4550     }
4551
4552   return scope;
4553 }
4554
4555 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
4556    this is either a class-name or a namespace-name (which corresponds
4557    to the class-or-namespace-name production in the grammar). For
4558    C++0x, it can also be a type-name that refers to an enumeration
4559    type.
4560
4561    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
4562    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
4563    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
4564    TYPE_P is TRUE iff the next name should be taken as a class-name,
4565    even the same name is declared to be another entity in the same
4566    scope.
4567
4568    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
4569    specified by the class-or-namespace-name.  If neither is found the
4570    ERROR_MARK_NODE is returned.  */
4571
4572 static tree
4573 cp_parser_qualifying_entity (cp_parser *parser,
4574                              bool typename_keyword_p,
4575                              bool template_keyword_p,
4576                              bool check_dependency_p,
4577                              bool type_p,
4578                              bool is_declaration)
4579 {
4580   tree saved_scope;
4581   tree saved_qualifying_scope;
4582   tree saved_object_scope;
4583   tree scope;
4584   bool only_class_p;
4585   bool successful_parse_p;
4586
4587   /* Before we try to parse the class-name, we must save away the
4588      current PARSER->SCOPE since cp_parser_class_name will destroy
4589      it.  */
4590   saved_scope = parser->scope;
4591   saved_qualifying_scope = parser->qualifying_scope;
4592   saved_object_scope = parser->object_scope;
4593   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
4594      there is no need to look for a namespace-name.  */
4595   only_class_p = template_keyword_p 
4596     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
4597   if (!only_class_p)
4598     cp_parser_parse_tentatively (parser);
4599   scope = cp_parser_class_name (parser,
4600                                 typename_keyword_p,
4601                                 template_keyword_p,
4602                                 type_p ? class_type : none_type,
4603                                 check_dependency_p,
4604                                 /*class_head_p=*/false,
4605                                 is_declaration);
4606   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
4607   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
4608   if (!only_class_p 
4609       && cxx_dialect != cxx98
4610       && !successful_parse_p)
4611     {
4612       /* Restore the saved scope.  */
4613       parser->scope = saved_scope;
4614       parser->qualifying_scope = saved_qualifying_scope;
4615       parser->object_scope = saved_object_scope;
4616
4617       /* Parse tentatively.  */
4618       cp_parser_parse_tentatively (parser);
4619      
4620       /* Parse a typedef-name or enum-name.  */
4621       scope = cp_parser_nonclass_name (parser);
4622
4623       /* "If the name found does not designate a namespace or a class,
4624          enumeration, or dependent type, the program is ill-formed."
4625
4626          We cover classes and dependent types above and namespaces below,
4627          so this code is only looking for enums.  */
4628       if (!scope || TREE_CODE (scope) != TYPE_DECL
4629           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
4630         cp_parser_simulate_error (parser);
4631
4632       successful_parse_p = cp_parser_parse_definitely (parser);
4633     }
4634   /* If that didn't work, try for a namespace-name.  */
4635   if (!only_class_p && !successful_parse_p)
4636     {
4637       /* Restore the saved scope.  */
4638       parser->scope = saved_scope;
4639       parser->qualifying_scope = saved_qualifying_scope;
4640       parser->object_scope = saved_object_scope;
4641       /* If we are not looking at an identifier followed by the scope
4642          resolution operator, then this is not part of a
4643          nested-name-specifier.  (Note that this function is only used
4644          to parse the components of a nested-name-specifier.)  */
4645       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
4646           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
4647         return error_mark_node;
4648       scope = cp_parser_namespace_name (parser);
4649     }
4650
4651   return scope;
4652 }
4653
4654 /* Parse a postfix-expression.
4655
4656    postfix-expression:
4657      primary-expression
4658      postfix-expression [ expression ]
4659      postfix-expression ( expression-list [opt] )
4660      simple-type-specifier ( expression-list [opt] )
4661      typename :: [opt] nested-name-specifier identifier
4662        ( expression-list [opt] )
4663      typename :: [opt] nested-name-specifier template [opt] template-id
4664        ( expression-list [opt] )
4665      postfix-expression . template [opt] id-expression
4666      postfix-expression -> template [opt] id-expression
4667      postfix-expression . pseudo-destructor-name
4668      postfix-expression -> pseudo-destructor-name
4669      postfix-expression ++
4670      postfix-expression --
4671      dynamic_cast < type-id > ( expression )
4672      static_cast < type-id > ( expression )
4673      reinterpret_cast < type-id > ( expression )
4674      const_cast < type-id > ( expression )
4675      typeid ( expression )
4676      typeid ( type-id )
4677
4678    GNU Extension:
4679
4680    postfix-expression:
4681      ( type-id ) { initializer-list , [opt] }
4682
4683    This extension is a GNU version of the C99 compound-literal
4684    construct.  (The C99 grammar uses `type-name' instead of `type-id',
4685    but they are essentially the same concept.)
4686
4687    If ADDRESS_P is true, the postfix expression is the operand of the
4688    `&' operator.  CAST_P is true if this expression is the target of a
4689    cast.
4690
4691    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
4692    class member access expressions [expr.ref].
4693
4694    Returns a representation of the expression.  */
4695
4696 static tree
4697 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
4698                               bool member_access_only_p,
4699                               cp_id_kind * pidk_return)
4700 {
4701   cp_token *token;
4702   enum rid keyword;
4703   cp_id_kind idk = CP_ID_KIND_NONE;
4704   tree postfix_expression = NULL_TREE;
4705   bool is_member_access = false;
4706
4707   /* Peek at the next token.  */
4708   token = cp_lexer_peek_token (parser->lexer);
4709   /* Some of the productions are determined by keywords.  */
4710   keyword = token->keyword;
4711   switch (keyword)
4712     {
4713     case RID_DYNCAST:
4714     case RID_STATCAST:
4715     case RID_REINTCAST:
4716     case RID_CONSTCAST:
4717       {
4718         tree type;
4719         tree expression;
4720         const char *saved_message;
4721
4722         /* All of these can be handled in the same way from the point
4723            of view of parsing.  Begin by consuming the token
4724            identifying the cast.  */
4725         cp_lexer_consume_token (parser->lexer);
4726
4727         /* New types cannot be defined in the cast.  */
4728         saved_message = parser->type_definition_forbidden_message;
4729         parser->type_definition_forbidden_message
4730           = G_("types may not be defined in casts");
4731
4732         /* Look for the opening `<'.  */
4733         cp_parser_require (parser, CPP_LESS, RT_LESS);
4734         /* Parse the type to which we are casting.  */
4735         type = cp_parser_type_id (parser);
4736         /* Look for the closing `>'.  */
4737         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
4738         /* Restore the old message.  */
4739         parser->type_definition_forbidden_message = saved_message;
4740
4741         /* And the expression which is being cast.  */
4742         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4743         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
4744         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4745
4746         /* Only type conversions to integral or enumeration types
4747            can be used in constant-expressions.  */
4748         if (!cast_valid_in_integral_constant_expression_p (type)
4749             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
4750           return error_mark_node;
4751
4752         switch (keyword)
4753           {
4754           case RID_DYNCAST:
4755             postfix_expression
4756               = build_dynamic_cast (type, expression, tf_warning_or_error);
4757             break;
4758           case RID_STATCAST:
4759             postfix_expression
4760               = build_static_cast (type, expression, tf_warning_or_error);
4761             break;
4762           case RID_REINTCAST:
4763             postfix_expression
4764               = build_reinterpret_cast (type, expression, 
4765                                         tf_warning_or_error);
4766             break;
4767           case RID_CONSTCAST:
4768             postfix_expression
4769               = build_const_cast (type, expression, tf_warning_or_error);
4770             break;
4771           default:
4772             gcc_unreachable ();
4773           }
4774       }
4775       break;
4776
4777     case RID_TYPEID:
4778       {
4779         tree type;
4780         const char *saved_message;
4781         bool saved_in_type_id_in_expr_p;
4782
4783         /* Consume the `typeid' token.  */
4784         cp_lexer_consume_token (parser->lexer);
4785         /* Look for the `(' token.  */
4786         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4787         /* Types cannot be defined in a `typeid' expression.  */
4788         saved_message = parser->type_definition_forbidden_message;
4789         parser->type_definition_forbidden_message
4790           = G_("types may not be defined in a %<typeid%> expression");
4791         /* We can't be sure yet whether we're looking at a type-id or an
4792            expression.  */
4793         cp_parser_parse_tentatively (parser);
4794         /* Try a type-id first.  */
4795         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4796         parser->in_type_id_in_expr_p = true;
4797         type = cp_parser_type_id (parser);
4798         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4799         /* Look for the `)' token.  Otherwise, we can't be sure that
4800            we're not looking at an expression: consider `typeid (int
4801            (3))', for example.  */
4802         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4803         /* If all went well, simply lookup the type-id.  */
4804         if (cp_parser_parse_definitely (parser))
4805           postfix_expression = get_typeid (type);
4806         /* Otherwise, fall back to the expression variant.  */
4807         else
4808           {
4809             tree expression;
4810
4811             /* Look for an expression.  */
4812             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
4813             /* Compute its typeid.  */
4814             postfix_expression = build_typeid (expression);
4815             /* Look for the `)' token.  */
4816             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4817           }
4818         /* Restore the saved message.  */
4819         parser->type_definition_forbidden_message = saved_message;
4820         /* `typeid' may not appear in an integral constant expression.  */
4821         if (cp_parser_non_integral_constant_expression(parser, NIC_TYPEID))
4822           return error_mark_node;
4823       }
4824       break;
4825
4826     case RID_TYPENAME:
4827       {
4828         tree type;
4829         /* The syntax permitted here is the same permitted for an
4830            elaborated-type-specifier.  */
4831         type = cp_parser_elaborated_type_specifier (parser,
4832                                                     /*is_friend=*/false,
4833                                                     /*is_declaration=*/false);
4834         postfix_expression = cp_parser_functional_cast (parser, type);
4835       }
4836       break;
4837
4838     default:
4839       {
4840         tree type;
4841
4842         /* If the next thing is a simple-type-specifier, we may be
4843            looking at a functional cast.  We could also be looking at
4844            an id-expression.  So, we try the functional cast, and if
4845            that doesn't work we fall back to the primary-expression.  */
4846         cp_parser_parse_tentatively (parser);
4847         /* Look for the simple-type-specifier.  */
4848         type = cp_parser_simple_type_specifier (parser,
4849                                                 /*decl_specs=*/NULL,
4850                                                 CP_PARSER_FLAGS_NONE);
4851         /* Parse the cast itself.  */
4852         if (!cp_parser_error_occurred (parser))
4853           postfix_expression
4854             = cp_parser_functional_cast (parser, type);
4855         /* If that worked, we're done.  */
4856         if (cp_parser_parse_definitely (parser))
4857           break;
4858
4859         /* If the functional-cast didn't work out, try a
4860            compound-literal.  */
4861         if (cp_parser_allow_gnu_extensions_p (parser)
4862             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
4863           {
4864             VEC(constructor_elt,gc) *initializer_list = NULL;
4865             bool saved_in_type_id_in_expr_p;
4866
4867             cp_parser_parse_tentatively (parser);
4868             /* Consume the `('.  */
4869             cp_lexer_consume_token (parser->lexer);
4870             /* Parse the type.  */
4871             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
4872             parser->in_type_id_in_expr_p = true;
4873             type = cp_parser_type_id (parser);
4874             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
4875             /* Look for the `)'.  */
4876             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4877             /* Look for the `{'.  */
4878             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
4879             /* If things aren't going well, there's no need to
4880                keep going.  */
4881             if (!cp_parser_error_occurred (parser))
4882               {
4883                 bool non_constant_p;
4884                 /* Parse the initializer-list.  */
4885                 initializer_list
4886                   = cp_parser_initializer_list (parser, &non_constant_p);
4887                 /* Allow a trailing `,'.  */
4888                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
4889                   cp_lexer_consume_token (parser->lexer);
4890                 /* Look for the final `}'.  */
4891                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
4892               }
4893             /* If that worked, we're definitely looking at a
4894                compound-literal expression.  */
4895             if (cp_parser_parse_definitely (parser))
4896               {
4897                 /* Warn the user that a compound literal is not
4898                    allowed in standard C++.  */
4899                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
4900                 /* For simplicity, we disallow compound literals in
4901                    constant-expressions.  We could
4902                    allow compound literals of integer type, whose
4903                    initializer was a constant, in constant
4904                    expressions.  Permitting that usage, as a further
4905                    extension, would not change the meaning of any
4906                    currently accepted programs.  (Of course, as
4907                    compound literals are not part of ISO C++, the
4908                    standard has nothing to say.)  */
4909                 if (cp_parser_non_integral_constant_expression (parser,
4910                                                                 NIC_NCC))
4911                   {
4912                     postfix_expression = error_mark_node;
4913                     break;
4914                   }
4915                 /* Form the representation of the compound-literal.  */
4916                 postfix_expression
4917                   = (finish_compound_literal
4918                      (type, build_constructor (init_list_type_node,
4919                                                initializer_list),
4920                       tf_warning_or_error));
4921                 break;
4922               }
4923           }
4924
4925         /* It must be a primary-expression.  */
4926         postfix_expression
4927           = cp_parser_primary_expression (parser, address_p, cast_p,
4928                                           /*template_arg_p=*/false,
4929                                           &idk);
4930       }
4931       break;
4932     }
4933
4934   /* Keep looping until the postfix-expression is complete.  */
4935   while (true)
4936     {
4937       if (idk == CP_ID_KIND_UNQUALIFIED
4938           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
4939           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
4940         /* It is not a Koenig lookup function call.  */
4941         postfix_expression
4942           = unqualified_name_lookup_error (postfix_expression);
4943
4944       /* Peek at the next token.  */
4945       token = cp_lexer_peek_token (parser->lexer);
4946
4947       switch (token->type)
4948         {
4949         case CPP_OPEN_SQUARE:
4950           postfix_expression
4951             = cp_parser_postfix_open_square_expression (parser,
4952                                                         postfix_expression,
4953                                                         false);
4954           idk = CP_ID_KIND_NONE;
4955           is_member_access = false;
4956           break;
4957
4958         case CPP_OPEN_PAREN:
4959           /* postfix-expression ( expression-list [opt] ) */
4960           {
4961             bool koenig_p;
4962             bool is_builtin_constant_p;
4963             bool saved_integral_constant_expression_p = false;
4964             bool saved_non_integral_constant_expression_p = false;
4965             VEC(tree,gc) *args;
4966
4967             is_member_access = false;
4968
4969             is_builtin_constant_p
4970               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
4971             if (is_builtin_constant_p)
4972               {
4973                 /* The whole point of __builtin_constant_p is to allow
4974                    non-constant expressions to appear as arguments.  */
4975                 saved_integral_constant_expression_p
4976                   = parser->integral_constant_expression_p;
4977                 saved_non_integral_constant_expression_p
4978                   = parser->non_integral_constant_expression_p;
4979                 parser->integral_constant_expression_p = false;
4980               }
4981             args = (cp_parser_parenthesized_expression_list
4982                     (parser, non_attr,
4983                      /*cast_p=*/false, /*allow_expansion_p=*/true,
4984                      /*non_constant_p=*/NULL));
4985             if (is_builtin_constant_p)
4986               {
4987                 parser->integral_constant_expression_p
4988                   = saved_integral_constant_expression_p;
4989                 parser->non_integral_constant_expression_p
4990                   = saved_non_integral_constant_expression_p;
4991               }
4992
4993             if (args == NULL)
4994               {
4995                 postfix_expression = error_mark_node;
4996                 break;
4997               }
4998
4999             /* Function calls are not permitted in
5000                constant-expressions.  */
5001             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5002                 && cp_parser_non_integral_constant_expression (parser,
5003                                                                NIC_FUNC_CALL))
5004               {
5005                 postfix_expression = error_mark_node;
5006                 release_tree_vector (args);
5007                 break;
5008               }
5009
5010             koenig_p = false;
5011             if (idk == CP_ID_KIND_UNQUALIFIED
5012                 || idk == CP_ID_KIND_TEMPLATE_ID)
5013               {
5014                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5015                   {
5016                     if (!VEC_empty (tree, args))
5017                       {
5018                         koenig_p = true;
5019                         if (!any_type_dependent_arguments_p (args))
5020                           postfix_expression
5021                             = perform_koenig_lookup (postfix_expression, args,
5022                                                      /*include_std=*/false,
5023                                                      tf_warning_or_error);
5024                       }
5025                     else
5026                       postfix_expression
5027                         = unqualified_fn_lookup_error (postfix_expression);
5028                   }
5029                 /* We do not perform argument-dependent lookup if
5030                    normal lookup finds a non-function, in accordance
5031                    with the expected resolution of DR 218.  */
5032                 else if (!VEC_empty (tree, args)
5033                          && is_overloaded_fn (postfix_expression))
5034                   {
5035                     tree fn = get_first_fn (postfix_expression);
5036                     fn = STRIP_TEMPLATE (fn);
5037
5038                     /* Do not do argument dependent lookup if regular
5039                        lookup finds a member function or a block-scope
5040                        function declaration.  [basic.lookup.argdep]/3  */
5041                     if (!DECL_FUNCTION_MEMBER_P (fn)
5042                         && !DECL_LOCAL_FUNCTION_P (fn))
5043                       {
5044                         koenig_p = true;
5045                         if (!any_type_dependent_arguments_p (args))
5046                           postfix_expression
5047                             = perform_koenig_lookup (postfix_expression, args,
5048                                                      /*include_std=*/false,
5049                                                      tf_warning_or_error);
5050                       }
5051                   }
5052               }
5053
5054             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5055               {
5056                 tree instance = TREE_OPERAND (postfix_expression, 0);
5057                 tree fn = TREE_OPERAND (postfix_expression, 1);
5058
5059                 if (processing_template_decl
5060                     && (type_dependent_expression_p (instance)
5061                         || (!BASELINK_P (fn)
5062                             && TREE_CODE (fn) != FIELD_DECL)
5063                         || type_dependent_expression_p (fn)
5064                         || any_type_dependent_arguments_p (args)))
5065                   {
5066                     postfix_expression
5067                       = build_nt_call_vec (postfix_expression, args);
5068                     release_tree_vector (args);
5069                     break;
5070                   }
5071
5072                 if (BASELINK_P (fn))
5073                   {
5074                   postfix_expression
5075                     = (build_new_method_call
5076                        (instance, fn, &args, NULL_TREE,
5077                         (idk == CP_ID_KIND_QUALIFIED
5078                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5079                          : LOOKUP_NORMAL),
5080                         /*fn_p=*/NULL,
5081                         tf_warning_or_error));
5082                   }
5083                 else
5084                   postfix_expression
5085                     = finish_call_expr (postfix_expression, &args,
5086                                         /*disallow_virtual=*/false,
5087                                         /*koenig_p=*/false,
5088                                         tf_warning_or_error);
5089               }
5090             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5091                      || TREE_CODE (postfix_expression) == MEMBER_REF
5092                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5093               postfix_expression = (build_offset_ref_call_from_tree
5094                                     (postfix_expression, &args));
5095             else if (idk == CP_ID_KIND_QUALIFIED)
5096               /* A call to a static class member, or a namespace-scope
5097                  function.  */
5098               postfix_expression
5099                 = finish_call_expr (postfix_expression, &args,
5100                                     /*disallow_virtual=*/true,
5101                                     koenig_p,
5102                                     tf_warning_or_error);
5103             else
5104               /* All other function calls.  */
5105               postfix_expression
5106                 = finish_call_expr (postfix_expression, &args,
5107                                     /*disallow_virtual=*/false,
5108                                     koenig_p,
5109                                     tf_warning_or_error);
5110
5111             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5112             idk = CP_ID_KIND_NONE;
5113
5114             release_tree_vector (args);
5115           }
5116           break;
5117
5118         case CPP_DOT:
5119         case CPP_DEREF:
5120           /* postfix-expression . template [opt] id-expression
5121              postfix-expression . pseudo-destructor-name
5122              postfix-expression -> template [opt] id-expression
5123              postfix-expression -> pseudo-destructor-name */
5124
5125           /* Consume the `.' or `->' operator.  */
5126           cp_lexer_consume_token (parser->lexer);
5127
5128           postfix_expression
5129             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5130                                                       postfix_expression,
5131                                                       false, &idk,
5132                                                       token->location);
5133
5134           is_member_access = true;
5135           break;
5136
5137         case CPP_PLUS_PLUS:
5138           /* postfix-expression ++  */
5139           /* Consume the `++' token.  */
5140           cp_lexer_consume_token (parser->lexer);
5141           /* Generate a representation for the complete expression.  */
5142           postfix_expression
5143             = finish_increment_expr (postfix_expression,
5144                                      POSTINCREMENT_EXPR);
5145           /* Increments may not appear in constant-expressions.  */
5146           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5147             postfix_expression = error_mark_node;
5148           idk = CP_ID_KIND_NONE;
5149           is_member_access = false;
5150           break;
5151
5152         case CPP_MINUS_MINUS:
5153           /* postfix-expression -- */
5154           /* Consume the `--' token.  */
5155           cp_lexer_consume_token (parser->lexer);
5156           /* Generate a representation for the complete expression.  */
5157           postfix_expression
5158             = finish_increment_expr (postfix_expression,
5159                                      POSTDECREMENT_EXPR);
5160           /* Decrements may not appear in constant-expressions.  */
5161           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5162             postfix_expression = error_mark_node;
5163           idk = CP_ID_KIND_NONE;
5164           is_member_access = false;
5165           break;
5166
5167         default:
5168           if (pidk_return != NULL)
5169             * pidk_return = idk;
5170           if (member_access_only_p)
5171             return is_member_access? postfix_expression : error_mark_node;
5172           else
5173             return postfix_expression;
5174         }
5175     }
5176
5177   /* We should never get here.  */
5178   gcc_unreachable ();
5179   return error_mark_node;
5180 }
5181
5182 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5183    by cp_parser_builtin_offsetof.  We're looking for
5184
5185      postfix-expression [ expression ]
5186
5187    FOR_OFFSETOF is set if we're being called in that context, which
5188    changes how we deal with integer constant expressions.  */
5189
5190 static tree
5191 cp_parser_postfix_open_square_expression (cp_parser *parser,
5192                                           tree postfix_expression,
5193                                           bool for_offsetof)
5194 {
5195   tree index;
5196
5197   /* Consume the `[' token.  */
5198   cp_lexer_consume_token (parser->lexer);
5199
5200   /* Parse the index expression.  */
5201   /* ??? For offsetof, there is a question of what to allow here.  If
5202      offsetof is not being used in an integral constant expression context,
5203      then we *could* get the right answer by computing the value at runtime.
5204      If we are in an integral constant expression context, then we might
5205      could accept any constant expression; hard to say without analysis.
5206      Rather than open the barn door too wide right away, allow only integer
5207      constant expressions here.  */
5208   if (for_offsetof)
5209     index = cp_parser_constant_expression (parser, false, NULL);
5210   else
5211     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5212
5213   /* Look for the closing `]'.  */
5214   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5215
5216   /* Build the ARRAY_REF.  */
5217   postfix_expression = grok_array_decl (postfix_expression, index);
5218
5219   /* When not doing offsetof, array references are not permitted in
5220      constant-expressions.  */
5221   if (!for_offsetof
5222       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5223     postfix_expression = error_mark_node;
5224
5225   return postfix_expression;
5226 }
5227
5228 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5229    by cp_parser_builtin_offsetof.  We're looking for
5230
5231      postfix-expression . template [opt] id-expression
5232      postfix-expression . pseudo-destructor-name
5233      postfix-expression -> template [opt] id-expression
5234      postfix-expression -> pseudo-destructor-name
5235
5236    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5237    limits what of the above we'll actually accept, but nevermind.
5238    TOKEN_TYPE is the "." or "->" token, which will already have been
5239    removed from the stream.  */
5240
5241 static tree
5242 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5243                                         enum cpp_ttype token_type,
5244                                         tree postfix_expression,
5245                                         bool for_offsetof, cp_id_kind *idk,
5246                                         location_t location)
5247 {
5248   tree name;
5249   bool dependent_p;
5250   bool pseudo_destructor_p;
5251   tree scope = NULL_TREE;
5252
5253   /* If this is a `->' operator, dereference the pointer.  */
5254   if (token_type == CPP_DEREF)
5255     postfix_expression = build_x_arrow (postfix_expression);
5256   /* Check to see whether or not the expression is type-dependent.  */
5257   dependent_p = type_dependent_expression_p (postfix_expression);
5258   /* The identifier following the `->' or `.' is not qualified.  */
5259   parser->scope = NULL_TREE;
5260   parser->qualifying_scope = NULL_TREE;
5261   parser->object_scope = NULL_TREE;
5262   *idk = CP_ID_KIND_NONE;
5263
5264   /* Enter the scope corresponding to the type of the object
5265      given by the POSTFIX_EXPRESSION.  */
5266   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5267     {
5268       scope = TREE_TYPE (postfix_expression);
5269       /* According to the standard, no expression should ever have
5270          reference type.  Unfortunately, we do not currently match
5271          the standard in this respect in that our internal representation
5272          of an expression may have reference type even when the standard
5273          says it does not.  Therefore, we have to manually obtain the
5274          underlying type here.  */
5275       scope = non_reference (scope);
5276       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5277       if (scope == unknown_type_node)
5278         {
5279           error_at (location, "%qE does not have class type",
5280                     postfix_expression);
5281           scope = NULL_TREE;
5282         }
5283       else
5284         scope = complete_type_or_else (scope, NULL_TREE);
5285       /* Let the name lookup machinery know that we are processing a
5286          class member access expression.  */
5287       parser->context->object_type = scope;
5288       /* If something went wrong, we want to be able to discern that case,
5289          as opposed to the case where there was no SCOPE due to the type
5290          of expression being dependent.  */
5291       if (!scope)
5292         scope = error_mark_node;
5293       /* If the SCOPE was erroneous, make the various semantic analysis
5294          functions exit quickly -- and without issuing additional error
5295          messages.  */
5296       if (scope == error_mark_node)
5297         postfix_expression = error_mark_node;
5298     }
5299
5300   /* Assume this expression is not a pseudo-destructor access.  */
5301   pseudo_destructor_p = false;
5302
5303   /* If the SCOPE is a scalar type, then, if this is a valid program,
5304      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5305      is type dependent, it can be pseudo-destructor-name or something else.
5306      Try to parse it as pseudo-destructor-name first.  */
5307   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5308     {
5309       tree s;
5310       tree type;
5311
5312       cp_parser_parse_tentatively (parser);
5313       /* Parse the pseudo-destructor-name.  */
5314       s = NULL_TREE;
5315       cp_parser_pseudo_destructor_name (parser, &s, &type);
5316       if (dependent_p
5317           && (cp_parser_error_occurred (parser)
5318               || TREE_CODE (type) != TYPE_DECL
5319               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5320         cp_parser_abort_tentative_parse (parser);
5321       else if (cp_parser_parse_definitely (parser))
5322         {
5323           pseudo_destructor_p = true;
5324           postfix_expression
5325             = finish_pseudo_destructor_expr (postfix_expression,
5326                                              s, TREE_TYPE (type));
5327         }
5328     }
5329
5330   if (!pseudo_destructor_p)
5331     {
5332       /* If the SCOPE is not a scalar type, we are looking at an
5333          ordinary class member access expression, rather than a
5334          pseudo-destructor-name.  */
5335       bool template_p;
5336       cp_token *token = cp_lexer_peek_token (parser->lexer);
5337       /* Parse the id-expression.  */
5338       name = (cp_parser_id_expression
5339               (parser,
5340                cp_parser_optional_template_keyword (parser),
5341                /*check_dependency_p=*/true,
5342                &template_p,
5343                /*declarator_p=*/false,
5344                /*optional_p=*/false));
5345       /* In general, build a SCOPE_REF if the member name is qualified.
5346          However, if the name was not dependent and has already been
5347          resolved; there is no need to build the SCOPE_REF.  For example;
5348
5349              struct X { void f(); };
5350              template <typename T> void f(T* t) { t->X::f(); }
5351
5352          Even though "t" is dependent, "X::f" is not and has been resolved
5353          to a BASELINK; there is no need to include scope information.  */
5354
5355       /* But we do need to remember that there was an explicit scope for
5356          virtual function calls.  */
5357       if (parser->scope)
5358         *idk = CP_ID_KIND_QUALIFIED;
5359
5360       /* If the name is a template-id that names a type, we will get a
5361          TYPE_DECL here.  That is invalid code.  */
5362       if (TREE_CODE (name) == TYPE_DECL)
5363         {
5364           error_at (token->location, "invalid use of %qD", name);
5365           postfix_expression = error_mark_node;
5366         }
5367       else
5368         {
5369           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5370             {
5371               name = build_qualified_name (/*type=*/NULL_TREE,
5372                                            parser->scope,
5373                                            name,
5374                                            template_p);
5375               parser->scope = NULL_TREE;
5376               parser->qualifying_scope = NULL_TREE;
5377               parser->object_scope = NULL_TREE;
5378             }
5379           if (scope && name && BASELINK_P (name))
5380             adjust_result_of_qualified_name_lookup
5381               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5382           postfix_expression
5383             = finish_class_member_access_expr (postfix_expression, name,
5384                                                template_p, 
5385                                                tf_warning_or_error);
5386         }
5387     }
5388
5389   /* We no longer need to look up names in the scope of the object on
5390      the left-hand side of the `.' or `->' operator.  */
5391   parser->context->object_type = NULL_TREE;
5392
5393   /* Outside of offsetof, these operators may not appear in
5394      constant-expressions.  */
5395   if (!for_offsetof
5396       && (cp_parser_non_integral_constant_expression
5397           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5398     postfix_expression = error_mark_node;
5399
5400   return postfix_expression;
5401 }
5402
5403 /* Parse a parenthesized expression-list.
5404
5405    expression-list:
5406      assignment-expression
5407      expression-list, assignment-expression
5408
5409    attribute-list:
5410      expression-list
5411      identifier
5412      identifier, expression-list
5413
5414    CAST_P is true if this expression is the target of a cast.
5415
5416    ALLOW_EXPANSION_P is true if this expression allows expansion of an
5417    argument pack.
5418
5419    Returns a vector of trees.  Each element is a representation of an
5420    assignment-expression.  NULL is returned if the ( and or ) are
5421    missing.  An empty, but allocated, vector is returned on no
5422    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
5423    if we are parsing an attribute list for an attribute that wants a
5424    plain identifier argument, normal_attr for an attribute that wants
5425    an expression, or non_attr if we aren't parsing an attribute list.  If
5426    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
5427    not all of the expressions in the list were constant.  */
5428
5429 static VEC(tree,gc) *
5430 cp_parser_parenthesized_expression_list (cp_parser* parser,
5431                                          int is_attribute_list,
5432                                          bool cast_p,
5433                                          bool allow_expansion_p,
5434                                          bool *non_constant_p)
5435 {
5436   VEC(tree,gc) *expression_list;
5437   bool fold_expr_p = is_attribute_list != non_attr;
5438   tree identifier = NULL_TREE;
5439   bool saved_greater_than_is_operator_p;
5440
5441   /* Assume all the expressions will be constant.  */
5442   if (non_constant_p)
5443     *non_constant_p = false;
5444
5445   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
5446     return NULL;
5447
5448   expression_list = make_tree_vector ();
5449
5450   /* Within a parenthesized expression, a `>' token is always
5451      the greater-than operator.  */
5452   saved_greater_than_is_operator_p
5453     = parser->greater_than_is_operator_p;
5454   parser->greater_than_is_operator_p = true;
5455
5456   /* Consume expressions until there are no more.  */
5457   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
5458     while (true)
5459       {
5460         tree expr;
5461
5462         /* At the beginning of attribute lists, check to see if the
5463            next token is an identifier.  */
5464         if (is_attribute_list == id_attr
5465             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
5466           {
5467             cp_token *token;
5468
5469             /* Consume the identifier.  */
5470             token = cp_lexer_consume_token (parser->lexer);
5471             /* Save the identifier.  */
5472             identifier = token->u.value;
5473           }
5474         else
5475           {
5476             bool expr_non_constant_p;
5477
5478             /* Parse the next assignment-expression.  */
5479             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5480               {
5481                 /* A braced-init-list.  */
5482                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5483                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
5484                 if (non_constant_p && expr_non_constant_p)
5485                   *non_constant_p = true;
5486               }
5487             else if (non_constant_p)
5488               {
5489                 expr = (cp_parser_constant_expression
5490                         (parser, /*allow_non_constant_p=*/true,
5491                          &expr_non_constant_p));
5492                 if (expr_non_constant_p)
5493                   *non_constant_p = true;
5494               }
5495             else
5496               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
5497
5498             if (fold_expr_p)
5499               expr = fold_non_dependent_expr (expr);
5500
5501             /* If we have an ellipsis, then this is an expression
5502                expansion.  */
5503             if (allow_expansion_p
5504                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
5505               {
5506                 /* Consume the `...'.  */
5507                 cp_lexer_consume_token (parser->lexer);
5508
5509                 /* Build the argument pack.  */
5510                 expr = make_pack_expansion (expr);
5511               }
5512
5513              /* Add it to the list.  We add error_mark_node
5514                 expressions to the list, so that we can still tell if
5515                 the correct form for a parenthesized expression-list
5516                 is found. That gives better errors.  */
5517             VEC_safe_push (tree, gc, expression_list, expr);
5518
5519             if (expr == error_mark_node)
5520               goto skip_comma;
5521           }
5522
5523         /* After the first item, attribute lists look the same as
5524            expression lists.  */
5525         is_attribute_list = non_attr;
5526
5527       get_comma:;
5528         /* If the next token isn't a `,', then we are done.  */
5529         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
5530           break;
5531
5532         /* Otherwise, consume the `,' and keep going.  */
5533         cp_lexer_consume_token (parser->lexer);
5534       }
5535
5536   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
5537     {
5538       int ending;
5539
5540     skip_comma:;
5541       /* We try and resync to an unnested comma, as that will give the
5542          user better diagnostics.  */
5543       ending = cp_parser_skip_to_closing_parenthesis (parser,
5544                                                       /*recovering=*/true,
5545                                                       /*or_comma=*/true,
5546                                                       /*consume_paren=*/true);
5547       if (ending < 0)
5548         goto get_comma;
5549       if (!ending)
5550         {
5551           parser->greater_than_is_operator_p
5552             = saved_greater_than_is_operator_p;
5553           return NULL;
5554         }
5555     }
5556
5557   parser->greater_than_is_operator_p
5558     = saved_greater_than_is_operator_p;
5559
5560   if (identifier)
5561     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
5562
5563   return expression_list;
5564 }
5565
5566 /* Parse a pseudo-destructor-name.
5567
5568    pseudo-destructor-name:
5569      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
5570      :: [opt] nested-name-specifier template template-id :: ~ type-name
5571      :: [opt] nested-name-specifier [opt] ~ type-name
5572
5573    If either of the first two productions is used, sets *SCOPE to the
5574    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
5575    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
5576    or ERROR_MARK_NODE if the parse fails.  */
5577
5578 static void
5579 cp_parser_pseudo_destructor_name (cp_parser* parser,
5580                                   tree* scope,
5581                                   tree* type)
5582 {
5583   bool nested_name_specifier_p;
5584
5585   /* Assume that things will not work out.  */
5586   *type = error_mark_node;
5587
5588   /* Look for the optional `::' operator.  */
5589   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
5590   /* Look for the optional nested-name-specifier.  */
5591   nested_name_specifier_p
5592     = (cp_parser_nested_name_specifier_opt (parser,
5593                                             /*typename_keyword_p=*/false,
5594                                             /*check_dependency_p=*/true,
5595                                             /*type_p=*/false,
5596                                             /*is_declaration=*/false)
5597        != NULL_TREE);
5598   /* Now, if we saw a nested-name-specifier, we might be doing the
5599      second production.  */
5600   if (nested_name_specifier_p
5601       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
5602     {
5603       /* Consume the `template' keyword.  */
5604       cp_lexer_consume_token (parser->lexer);
5605       /* Parse the template-id.  */
5606       cp_parser_template_id (parser,
5607                              /*template_keyword_p=*/true,
5608                              /*check_dependency_p=*/false,
5609                              /*is_declaration=*/true);
5610       /* Look for the `::' token.  */
5611       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5612     }
5613   /* If the next token is not a `~', then there might be some
5614      additional qualification.  */
5615   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
5616     {
5617       /* At this point, we're looking for "type-name :: ~".  The type-name
5618          must not be a class-name, since this is a pseudo-destructor.  So,
5619          it must be either an enum-name, or a typedef-name -- both of which
5620          are just identifiers.  So, we peek ahead to check that the "::"
5621          and "~" tokens are present; if they are not, then we can avoid
5622          calling type_name.  */
5623       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
5624           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
5625           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
5626         {
5627           cp_parser_error (parser, "non-scalar type");
5628           return;
5629         }
5630
5631       /* Look for the type-name.  */
5632       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
5633       if (*scope == error_mark_node)
5634         return;
5635
5636       /* Look for the `::' token.  */
5637       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
5638     }
5639   else
5640     *scope = NULL_TREE;
5641
5642   /* Look for the `~'.  */
5643   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
5644   /* Look for the type-name again.  We are not responsible for
5645      checking that it matches the first type-name.  */
5646   *type = cp_parser_nonclass_name (parser);
5647 }
5648
5649 /* Parse a unary-expression.
5650
5651    unary-expression:
5652      postfix-expression
5653      ++ cast-expression
5654      -- cast-expression
5655      unary-operator cast-expression
5656      sizeof unary-expression
5657      sizeof ( type-id )
5658      alignof ( type-id )  [C++0x]
5659      new-expression
5660      delete-expression
5661
5662    GNU Extensions:
5663
5664    unary-expression:
5665      __extension__ cast-expression
5666      __alignof__ unary-expression
5667      __alignof__ ( type-id )
5668      alignof unary-expression  [C++0x]
5669      __real__ cast-expression
5670      __imag__ cast-expression
5671      && identifier
5672
5673    ADDRESS_P is true iff the unary-expression is appearing as the
5674    operand of the `&' operator.   CAST_P is true if this expression is
5675    the target of a cast.
5676
5677    Returns a representation of the expression.  */
5678
5679 static tree
5680 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
5681                             cp_id_kind * pidk)
5682 {
5683   cp_token *token;
5684   enum tree_code unary_operator;
5685
5686   /* Peek at the next token.  */
5687   token = cp_lexer_peek_token (parser->lexer);
5688   /* Some keywords give away the kind of expression.  */
5689   if (token->type == CPP_KEYWORD)
5690     {
5691       enum rid keyword = token->keyword;
5692
5693       switch (keyword)
5694         {
5695         case RID_ALIGNOF:
5696         case RID_SIZEOF:
5697           {
5698             tree operand;
5699             enum tree_code op;
5700
5701             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
5702             /* Consume the token.  */
5703             cp_lexer_consume_token (parser->lexer);
5704             /* Parse the operand.  */
5705             operand = cp_parser_sizeof_operand (parser, keyword);
5706
5707             if (TYPE_P (operand))
5708               return cxx_sizeof_or_alignof_type (operand, op, true);
5709             else
5710               {
5711                 /* ISO C++ defines alignof only with types, not with
5712                    expressions. So pedwarn if alignof is used with a non-
5713                    type expression. However, __alignof__ is ok.  */
5714                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
5715                   pedwarn (token->location, OPT_pedantic,
5716                            "ISO C++ does not allow %<alignof%> "
5717                            "with a non-type");
5718
5719                 return cxx_sizeof_or_alignof_expr (operand, op, true);
5720               }
5721           }
5722
5723         case RID_NEW:
5724           return cp_parser_new_expression (parser);
5725
5726         case RID_DELETE:
5727           return cp_parser_delete_expression (parser);
5728
5729         case RID_EXTENSION:
5730           {
5731             /* The saved value of the PEDANTIC flag.  */
5732             int saved_pedantic;
5733             tree expr;
5734
5735             /* Save away the PEDANTIC flag.  */
5736             cp_parser_extension_opt (parser, &saved_pedantic);
5737             /* Parse the cast-expression.  */
5738             expr = cp_parser_simple_cast_expression (parser);
5739             /* Restore the PEDANTIC flag.  */
5740             pedantic = saved_pedantic;
5741
5742             return expr;
5743           }
5744
5745         case RID_REALPART:
5746         case RID_IMAGPART:
5747           {
5748             tree expression;
5749
5750             /* Consume the `__real__' or `__imag__' token.  */
5751             cp_lexer_consume_token (parser->lexer);
5752             /* Parse the cast-expression.  */
5753             expression = cp_parser_simple_cast_expression (parser);
5754             /* Create the complete representation.  */
5755             return build_x_unary_op ((keyword == RID_REALPART
5756                                       ? REALPART_EXPR : IMAGPART_EXPR),
5757                                      expression,
5758                                      tf_warning_or_error);
5759           }
5760           break;
5761
5762         case RID_NOEXCEPT:
5763           {
5764             tree expr;
5765             const char *saved_message;
5766             bool saved_integral_constant_expression_p;
5767             bool saved_non_integral_constant_expression_p;
5768             bool saved_greater_than_is_operator_p;
5769
5770             cp_lexer_consume_token (parser->lexer);
5771             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5772
5773             saved_message = parser->type_definition_forbidden_message;
5774             parser->type_definition_forbidden_message
5775               = G_("types may not be defined in %<noexcept%> expressions");
5776
5777             saved_integral_constant_expression_p
5778               = parser->integral_constant_expression_p;
5779             saved_non_integral_constant_expression_p
5780               = parser->non_integral_constant_expression_p;
5781             parser->integral_constant_expression_p = false;
5782
5783             saved_greater_than_is_operator_p
5784               = parser->greater_than_is_operator_p;
5785             parser->greater_than_is_operator_p = true;
5786
5787             ++cp_unevaluated_operand;
5788             ++c_inhibit_evaluation_warnings;
5789             expr = cp_parser_expression (parser, false, NULL);
5790             --c_inhibit_evaluation_warnings;
5791             --cp_unevaluated_operand;
5792
5793             parser->greater_than_is_operator_p
5794               = saved_greater_than_is_operator_p;
5795
5796             parser->integral_constant_expression_p
5797               = saved_integral_constant_expression_p;
5798             parser->non_integral_constant_expression_p
5799               = saved_non_integral_constant_expression_p;
5800
5801             parser->type_definition_forbidden_message = saved_message;
5802
5803             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5804             return finish_noexcept_expr (expr, tf_warning_or_error);
5805           }
5806
5807         default:
5808           break;
5809         }
5810     }
5811
5812   /* Look for the `:: new' and `:: delete', which also signal the
5813      beginning of a new-expression, or delete-expression,
5814      respectively.  If the next token is `::', then it might be one of
5815      these.  */
5816   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
5817     {
5818       enum rid keyword;
5819
5820       /* See if the token after the `::' is one of the keywords in
5821          which we're interested.  */
5822       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
5823       /* If it's `new', we have a new-expression.  */
5824       if (keyword == RID_NEW)
5825         return cp_parser_new_expression (parser);
5826       /* Similarly, for `delete'.  */
5827       else if (keyword == RID_DELETE)
5828         return cp_parser_delete_expression (parser);
5829     }
5830
5831   /* Look for a unary operator.  */
5832   unary_operator = cp_parser_unary_operator (token);
5833   /* The `++' and `--' operators can be handled similarly, even though
5834      they are not technically unary-operators in the grammar.  */
5835   if (unary_operator == ERROR_MARK)
5836     {
5837       if (token->type == CPP_PLUS_PLUS)
5838         unary_operator = PREINCREMENT_EXPR;
5839       else if (token->type == CPP_MINUS_MINUS)
5840         unary_operator = PREDECREMENT_EXPR;
5841       /* Handle the GNU address-of-label extension.  */
5842       else if (cp_parser_allow_gnu_extensions_p (parser)
5843                && token->type == CPP_AND_AND)
5844         {
5845           tree identifier;
5846           tree expression;
5847           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
5848
5849           /* Consume the '&&' token.  */
5850           cp_lexer_consume_token (parser->lexer);
5851           /* Look for the identifier.  */
5852           identifier = cp_parser_identifier (parser);
5853           /* Create an expression representing the address.  */
5854           expression = finish_label_address_expr (identifier, loc);
5855           if (cp_parser_non_integral_constant_expression (parser,
5856                                                           NIC_ADDR_LABEL))
5857             expression = error_mark_node;
5858           return expression;
5859         }
5860     }
5861   if (unary_operator != ERROR_MARK)
5862     {
5863       tree cast_expression;
5864       tree expression = error_mark_node;
5865       non_integral_constant non_constant_p = NIC_NONE;
5866
5867       /* Consume the operator token.  */
5868       token = cp_lexer_consume_token (parser->lexer);
5869       /* Parse the cast-expression.  */
5870       cast_expression
5871         = cp_parser_cast_expression (parser,
5872                                      unary_operator == ADDR_EXPR,
5873                                      /*cast_p=*/false, pidk);
5874       /* Now, build an appropriate representation.  */
5875       switch (unary_operator)
5876         {
5877         case INDIRECT_REF:
5878           non_constant_p = NIC_STAR;
5879           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
5880                                              tf_warning_or_error);
5881           break;
5882
5883         case ADDR_EXPR:
5884            non_constant_p = NIC_ADDR;
5885           /* Fall through.  */
5886         case BIT_NOT_EXPR:
5887           expression = build_x_unary_op (unary_operator, cast_expression,
5888                                          tf_warning_or_error);
5889           break;
5890
5891         case PREINCREMENT_EXPR:
5892         case PREDECREMENT_EXPR:
5893           non_constant_p = unary_operator == PREINCREMENT_EXPR
5894                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
5895           /* Fall through.  */
5896         case UNARY_PLUS_EXPR:
5897         case NEGATE_EXPR:
5898         case TRUTH_NOT_EXPR:
5899           expression = finish_unary_op_expr (unary_operator, cast_expression);
5900           break;
5901
5902         default:
5903           gcc_unreachable ();
5904         }
5905
5906       if (non_constant_p != NIC_NONE
5907           && cp_parser_non_integral_constant_expression (parser,
5908                                                          non_constant_p))
5909         expression = error_mark_node;
5910
5911       return expression;
5912     }
5913
5914   return cp_parser_postfix_expression (parser, address_p, cast_p,
5915                                        /*member_access_only_p=*/false,
5916                                        pidk);
5917 }
5918
5919 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
5920    unary-operator, the corresponding tree code is returned.  */
5921
5922 static enum tree_code
5923 cp_parser_unary_operator (cp_token* token)
5924 {
5925   switch (token->type)
5926     {
5927     case CPP_MULT:
5928       return INDIRECT_REF;
5929
5930     case CPP_AND:
5931       return ADDR_EXPR;
5932
5933     case CPP_PLUS:
5934       return UNARY_PLUS_EXPR;
5935
5936     case CPP_MINUS:
5937       return NEGATE_EXPR;
5938
5939     case CPP_NOT:
5940       return TRUTH_NOT_EXPR;
5941
5942     case CPP_COMPL:
5943       return BIT_NOT_EXPR;
5944
5945     default:
5946       return ERROR_MARK;
5947     }
5948 }
5949
5950 /* Parse a new-expression.
5951
5952    new-expression:
5953      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
5954      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
5955
5956    Returns a representation of the expression.  */
5957
5958 static tree
5959 cp_parser_new_expression (cp_parser* parser)
5960 {
5961   bool global_scope_p;
5962   VEC(tree,gc) *placement;
5963   tree type;
5964   VEC(tree,gc) *initializer;
5965   tree nelts;
5966   tree ret;
5967
5968   /* Look for the optional `::' operator.  */
5969   global_scope_p
5970     = (cp_parser_global_scope_opt (parser,
5971                                    /*current_scope_valid_p=*/false)
5972        != NULL_TREE);
5973   /* Look for the `new' operator.  */
5974   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
5975   /* There's no easy way to tell a new-placement from the
5976      `( type-id )' construct.  */
5977   cp_parser_parse_tentatively (parser);
5978   /* Look for a new-placement.  */
5979   placement = cp_parser_new_placement (parser);
5980   /* If that didn't work out, there's no new-placement.  */
5981   if (!cp_parser_parse_definitely (parser))
5982     {
5983       if (placement != NULL)
5984         release_tree_vector (placement);
5985       placement = NULL;
5986     }
5987
5988   /* If the next token is a `(', then we have a parenthesized
5989      type-id.  */
5990   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5991     {
5992       cp_token *token;
5993       /* Consume the `('.  */
5994       cp_lexer_consume_token (parser->lexer);
5995       /* Parse the type-id.  */
5996       type = cp_parser_type_id (parser);
5997       /* Look for the closing `)'.  */
5998       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5999       token = cp_lexer_peek_token (parser->lexer);
6000       /* There should not be a direct-new-declarator in this production,
6001          but GCC used to allowed this, so we check and emit a sensible error
6002          message for this case.  */
6003       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6004         {
6005           error_at (token->location,
6006                     "array bound forbidden after parenthesized type-id");
6007           inform (token->location, 
6008                   "try removing the parentheses around the type-id");
6009           cp_parser_direct_new_declarator (parser);
6010         }
6011       nelts = NULL_TREE;
6012     }
6013   /* Otherwise, there must be a new-type-id.  */
6014   else
6015     type = cp_parser_new_type_id (parser, &nelts);
6016
6017   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6018   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6019       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6020     initializer = cp_parser_new_initializer (parser);
6021   else
6022     initializer = NULL;
6023
6024   /* A new-expression may not appear in an integral constant
6025      expression.  */
6026   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6027     ret = error_mark_node;
6028   else
6029     {
6030       /* Create a representation of the new-expression.  */
6031       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6032                        tf_warning_or_error);
6033     }
6034
6035   if (placement != NULL)
6036     release_tree_vector (placement);
6037   if (initializer != NULL)
6038     release_tree_vector (initializer);
6039
6040   return ret;
6041 }
6042
6043 /* Parse a new-placement.
6044
6045    new-placement:
6046      ( expression-list )
6047
6048    Returns the same representation as for an expression-list.  */
6049
6050 static VEC(tree,gc) *
6051 cp_parser_new_placement (cp_parser* parser)
6052 {
6053   VEC(tree,gc) *expression_list;
6054
6055   /* Parse the expression-list.  */
6056   expression_list = (cp_parser_parenthesized_expression_list
6057                      (parser, non_attr, /*cast_p=*/false,
6058                       /*allow_expansion_p=*/true,
6059                       /*non_constant_p=*/NULL));
6060
6061   return expression_list;
6062 }
6063
6064 /* Parse a new-type-id.
6065
6066    new-type-id:
6067      type-specifier-seq new-declarator [opt]
6068
6069    Returns the TYPE allocated.  If the new-type-id indicates an array
6070    type, *NELTS is set to the number of elements in the last array
6071    bound; the TYPE will not include the last array bound.  */
6072
6073 static tree
6074 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6075 {
6076   cp_decl_specifier_seq type_specifier_seq;
6077   cp_declarator *new_declarator;
6078   cp_declarator *declarator;
6079   cp_declarator *outer_declarator;
6080   const char *saved_message;
6081   tree type;
6082
6083   /* The type-specifier sequence must not contain type definitions.
6084      (It cannot contain declarations of new types either, but if they
6085      are not definitions we will catch that because they are not
6086      complete.)  */
6087   saved_message = parser->type_definition_forbidden_message;
6088   parser->type_definition_forbidden_message
6089     = G_("types may not be defined in a new-type-id");
6090   /* Parse the type-specifier-seq.  */
6091   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6092                                 /*is_trailing_return=*/false,
6093                                 &type_specifier_seq);
6094   /* Restore the old message.  */
6095   parser->type_definition_forbidden_message = saved_message;
6096   /* Parse the new-declarator.  */
6097   new_declarator = cp_parser_new_declarator_opt (parser);
6098
6099   /* Determine the number of elements in the last array dimension, if
6100      any.  */
6101   *nelts = NULL_TREE;
6102   /* Skip down to the last array dimension.  */
6103   declarator = new_declarator;
6104   outer_declarator = NULL;
6105   while (declarator && (declarator->kind == cdk_pointer
6106                         || declarator->kind == cdk_ptrmem))
6107     {
6108       outer_declarator = declarator;
6109       declarator = declarator->declarator;
6110     }
6111   while (declarator
6112          && declarator->kind == cdk_array
6113          && declarator->declarator
6114          && declarator->declarator->kind == cdk_array)
6115     {
6116       outer_declarator = declarator;
6117       declarator = declarator->declarator;
6118     }
6119
6120   if (declarator && declarator->kind == cdk_array)
6121     {
6122       *nelts = declarator->u.array.bounds;
6123       if (*nelts == error_mark_node)
6124         *nelts = integer_one_node;
6125
6126       if (outer_declarator)
6127         outer_declarator->declarator = declarator->declarator;
6128       else
6129         new_declarator = NULL;
6130     }
6131
6132   type = groktypename (&type_specifier_seq, new_declarator, false);
6133   return type;
6134 }
6135
6136 /* Parse an (optional) new-declarator.
6137
6138    new-declarator:
6139      ptr-operator new-declarator [opt]
6140      direct-new-declarator
6141
6142    Returns the declarator.  */
6143
6144 static cp_declarator *
6145 cp_parser_new_declarator_opt (cp_parser* parser)
6146 {
6147   enum tree_code code;
6148   tree type;
6149   cp_cv_quals cv_quals;
6150
6151   /* We don't know if there's a ptr-operator next, or not.  */
6152   cp_parser_parse_tentatively (parser);
6153   /* Look for a ptr-operator.  */
6154   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6155   /* If that worked, look for more new-declarators.  */
6156   if (cp_parser_parse_definitely (parser))
6157     {
6158       cp_declarator *declarator;
6159
6160       /* Parse another optional declarator.  */
6161       declarator = cp_parser_new_declarator_opt (parser);
6162
6163       return cp_parser_make_indirect_declarator
6164         (code, type, cv_quals, declarator);
6165     }
6166
6167   /* If the next token is a `[', there is a direct-new-declarator.  */
6168   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6169     return cp_parser_direct_new_declarator (parser);
6170
6171   return NULL;
6172 }
6173
6174 /* Parse a direct-new-declarator.
6175
6176    direct-new-declarator:
6177      [ expression ]
6178      direct-new-declarator [constant-expression]
6179
6180    */
6181
6182 static cp_declarator *
6183 cp_parser_direct_new_declarator (cp_parser* parser)
6184 {
6185   cp_declarator *declarator = NULL;
6186
6187   while (true)
6188     {
6189       tree expression;
6190
6191       /* Look for the opening `['.  */
6192       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6193       /* The first expression is not required to be constant.  */
6194       if (!declarator)
6195         {
6196           cp_token *token = cp_lexer_peek_token (parser->lexer);
6197           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6198           /* The standard requires that the expression have integral
6199              type.  DR 74 adds enumeration types.  We believe that the
6200              real intent is that these expressions be handled like the
6201              expression in a `switch' condition, which also allows
6202              classes with a single conversion to integral or
6203              enumeration type.  */
6204           if (!processing_template_decl)
6205             {
6206               expression
6207                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6208                                               expression,
6209                                               /*complain=*/true);
6210               if (!expression)
6211                 {
6212                   error_at (token->location,
6213                             "expression in new-declarator must have integral "
6214                             "or enumeration type");
6215                   expression = error_mark_node;
6216                 }
6217             }
6218         }
6219       /* But all the other expressions must be.  */
6220       else
6221         expression
6222           = cp_parser_constant_expression (parser,
6223                                            /*allow_non_constant=*/false,
6224                                            NULL);
6225       /* Look for the closing `]'.  */
6226       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6227
6228       /* Add this bound to the declarator.  */
6229       declarator = make_array_declarator (declarator, expression);
6230
6231       /* If the next token is not a `[', then there are no more
6232          bounds.  */
6233       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6234         break;
6235     }
6236
6237   return declarator;
6238 }
6239
6240 /* Parse a new-initializer.
6241
6242    new-initializer:
6243      ( expression-list [opt] )
6244      braced-init-list
6245
6246    Returns a representation of the expression-list.  */
6247
6248 static VEC(tree,gc) *
6249 cp_parser_new_initializer (cp_parser* parser)
6250 {
6251   VEC(tree,gc) *expression_list;
6252
6253   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6254     {
6255       tree t;
6256       bool expr_non_constant_p;
6257       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6258       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6259       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6260       expression_list = make_tree_vector_single (t);
6261     }
6262   else
6263     expression_list = (cp_parser_parenthesized_expression_list
6264                        (parser, non_attr, /*cast_p=*/false,
6265                         /*allow_expansion_p=*/true,
6266                         /*non_constant_p=*/NULL));
6267
6268   return expression_list;
6269 }
6270
6271 /* Parse a delete-expression.
6272
6273    delete-expression:
6274      :: [opt] delete cast-expression
6275      :: [opt] delete [ ] cast-expression
6276
6277    Returns a representation of the expression.  */
6278
6279 static tree
6280 cp_parser_delete_expression (cp_parser* parser)
6281 {
6282   bool global_scope_p;
6283   bool array_p;
6284   tree expression;
6285
6286   /* Look for the optional `::' operator.  */
6287   global_scope_p
6288     = (cp_parser_global_scope_opt (parser,
6289                                    /*current_scope_valid_p=*/false)
6290        != NULL_TREE);
6291   /* Look for the `delete' keyword.  */
6292   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6293   /* See if the array syntax is in use.  */
6294   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6295     {
6296       /* Consume the `[' token.  */
6297       cp_lexer_consume_token (parser->lexer);
6298       /* Look for the `]' token.  */
6299       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6300       /* Remember that this is the `[]' construct.  */
6301       array_p = true;
6302     }
6303   else
6304     array_p = false;
6305
6306   /* Parse the cast-expression.  */
6307   expression = cp_parser_simple_cast_expression (parser);
6308
6309   /* A delete-expression may not appear in an integral constant
6310      expression.  */
6311   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6312     return error_mark_node;
6313
6314   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6315                         tf_warning_or_error);
6316 }
6317
6318 /* Returns true if TOKEN may start a cast-expression and false
6319    otherwise.  */
6320
6321 static bool
6322 cp_parser_token_starts_cast_expression (cp_token *token)
6323 {
6324   switch (token->type)
6325     {
6326     case CPP_COMMA:
6327     case CPP_SEMICOLON:
6328     case CPP_QUERY:
6329     case CPP_COLON:
6330     case CPP_CLOSE_SQUARE:
6331     case CPP_CLOSE_PAREN:
6332     case CPP_CLOSE_BRACE:
6333     case CPP_DOT:
6334     case CPP_DOT_STAR:
6335     case CPP_DEREF:
6336     case CPP_DEREF_STAR:
6337     case CPP_DIV:
6338     case CPP_MOD:
6339     case CPP_LSHIFT:
6340     case CPP_RSHIFT:
6341     case CPP_LESS:
6342     case CPP_GREATER:
6343     case CPP_LESS_EQ:
6344     case CPP_GREATER_EQ:
6345     case CPP_EQ_EQ:
6346     case CPP_NOT_EQ:
6347     case CPP_EQ:
6348     case CPP_MULT_EQ:
6349     case CPP_DIV_EQ:
6350     case CPP_MOD_EQ:
6351     case CPP_PLUS_EQ:
6352     case CPP_MINUS_EQ:
6353     case CPP_RSHIFT_EQ:
6354     case CPP_LSHIFT_EQ:
6355     case CPP_AND_EQ:
6356     case CPP_XOR_EQ:
6357     case CPP_OR_EQ:
6358     case CPP_XOR:
6359     case CPP_OR:
6360     case CPP_OR_OR:
6361     case CPP_EOF:
6362       return false;
6363
6364       /* '[' may start a primary-expression in obj-c++.  */
6365     case CPP_OPEN_SQUARE:
6366       return c_dialect_objc ();
6367
6368     default:
6369       return true;
6370     }
6371 }
6372
6373 /* Parse a cast-expression.
6374
6375    cast-expression:
6376      unary-expression
6377      ( type-id ) cast-expression
6378
6379    ADDRESS_P is true iff the unary-expression is appearing as the
6380    operand of the `&' operator.   CAST_P is true if this expression is
6381    the target of a cast.
6382
6383    Returns a representation of the expression.  */
6384
6385 static tree
6386 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6387                            cp_id_kind * pidk)
6388 {
6389   /* If it's a `(', then we might be looking at a cast.  */
6390   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6391     {
6392       tree type = NULL_TREE;
6393       tree expr = NULL_TREE;
6394       bool compound_literal_p;
6395       const char *saved_message;
6396
6397       /* There's no way to know yet whether or not this is a cast.
6398          For example, `(int (3))' is a unary-expression, while `(int)
6399          3' is a cast.  So, we resort to parsing tentatively.  */
6400       cp_parser_parse_tentatively (parser);
6401       /* Types may not be defined in a cast.  */
6402       saved_message = parser->type_definition_forbidden_message;
6403       parser->type_definition_forbidden_message
6404         = G_("types may not be defined in casts");
6405       /* Consume the `('.  */
6406       cp_lexer_consume_token (parser->lexer);
6407       /* A very tricky bit is that `(struct S) { 3 }' is a
6408          compound-literal (which we permit in C++ as an extension).
6409          But, that construct is not a cast-expression -- it is a
6410          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
6411          is legal; if the compound-literal were a cast-expression,
6412          you'd need an extra set of parentheses.)  But, if we parse
6413          the type-id, and it happens to be a class-specifier, then we
6414          will commit to the parse at that point, because we cannot
6415          undo the action that is done when creating a new class.  So,
6416          then we cannot back up and do a postfix-expression.
6417
6418          Therefore, we scan ahead to the closing `)', and check to see
6419          if the token after the `)' is a `{'.  If so, we are not
6420          looking at a cast-expression.
6421
6422          Save tokens so that we can put them back.  */
6423       cp_lexer_save_tokens (parser->lexer);
6424       /* Skip tokens until the next token is a closing parenthesis.
6425          If we find the closing `)', and the next token is a `{', then
6426          we are looking at a compound-literal.  */
6427       compound_literal_p
6428         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
6429                                                   /*consume_paren=*/true)
6430            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
6431       /* Roll back the tokens we skipped.  */
6432       cp_lexer_rollback_tokens (parser->lexer);
6433       /* If we were looking at a compound-literal, simulate an error
6434          so that the call to cp_parser_parse_definitely below will
6435          fail.  */
6436       if (compound_literal_p)
6437         cp_parser_simulate_error (parser);
6438       else
6439         {
6440           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
6441           parser->in_type_id_in_expr_p = true;
6442           /* Look for the type-id.  */
6443           type = cp_parser_type_id (parser);
6444           /* Look for the closing `)'.  */
6445           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6446           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
6447         }
6448
6449       /* Restore the saved message.  */
6450       parser->type_definition_forbidden_message = saved_message;
6451
6452       /* At this point this can only be either a cast or a
6453          parenthesized ctor such as `(T ())' that looks like a cast to
6454          function returning T.  */
6455       if (!cp_parser_error_occurred (parser)
6456           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
6457                                                      (parser->lexer)))
6458         {
6459           cp_parser_parse_definitely (parser);
6460           expr = cp_parser_cast_expression (parser,
6461                                             /*address_p=*/false,
6462                                             /*cast_p=*/true, pidk);
6463
6464           /* Warn about old-style casts, if so requested.  */
6465           if (warn_old_style_cast
6466               && !in_system_header
6467               && !VOID_TYPE_P (type)
6468               && current_lang_name != lang_name_c)
6469             warning (OPT_Wold_style_cast, "use of old-style cast");
6470
6471           /* Only type conversions to integral or enumeration types
6472              can be used in constant-expressions.  */
6473           if (!cast_valid_in_integral_constant_expression_p (type)
6474               && cp_parser_non_integral_constant_expression (parser,
6475                                                              NIC_CAST))
6476             return error_mark_node;
6477
6478           /* Perform the cast.  */
6479           expr = build_c_cast (input_location, type, expr);
6480           return expr;
6481         }
6482       else 
6483         cp_parser_abort_tentative_parse (parser);
6484     }
6485
6486   /* If we get here, then it's not a cast, so it must be a
6487      unary-expression.  */
6488   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
6489 }
6490
6491 /* Parse a binary expression of the general form:
6492
6493    pm-expression:
6494      cast-expression
6495      pm-expression .* cast-expression
6496      pm-expression ->* cast-expression
6497
6498    multiplicative-expression:
6499      pm-expression
6500      multiplicative-expression * pm-expression
6501      multiplicative-expression / pm-expression
6502      multiplicative-expression % pm-expression
6503
6504    additive-expression:
6505      multiplicative-expression
6506      additive-expression + multiplicative-expression
6507      additive-expression - multiplicative-expression
6508
6509    shift-expression:
6510      additive-expression
6511      shift-expression << additive-expression
6512      shift-expression >> additive-expression
6513
6514    relational-expression:
6515      shift-expression
6516      relational-expression < shift-expression
6517      relational-expression > shift-expression
6518      relational-expression <= shift-expression
6519      relational-expression >= shift-expression
6520
6521   GNU Extension:
6522
6523    relational-expression:
6524      relational-expression <? shift-expression
6525      relational-expression >? shift-expression
6526
6527    equality-expression:
6528      relational-expression
6529      equality-expression == relational-expression
6530      equality-expression != relational-expression
6531
6532    and-expression:
6533      equality-expression
6534      and-expression & equality-expression
6535
6536    exclusive-or-expression:
6537      and-expression
6538      exclusive-or-expression ^ and-expression
6539
6540    inclusive-or-expression:
6541      exclusive-or-expression
6542      inclusive-or-expression | exclusive-or-expression
6543
6544    logical-and-expression:
6545      inclusive-or-expression
6546      logical-and-expression && inclusive-or-expression
6547
6548    logical-or-expression:
6549      logical-and-expression
6550      logical-or-expression || logical-and-expression
6551
6552    All these are implemented with a single function like:
6553
6554    binary-expression:
6555      simple-cast-expression
6556      binary-expression <token> binary-expression
6557
6558    CAST_P is true if this expression is the target of a cast.
6559
6560    The binops_by_token map is used to get the tree codes for each <token> type.
6561    binary-expressions are associated according to a precedence table.  */
6562
6563 #define TOKEN_PRECEDENCE(token)                              \
6564 (((token->type == CPP_GREATER                                \
6565    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
6566   && !parser->greater_than_is_operator_p)                    \
6567  ? PREC_NOT_OPERATOR                                         \
6568  : binops_by_token[token->type].prec)
6569
6570 static tree
6571 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
6572                              bool no_toplevel_fold_p,
6573                              enum cp_parser_prec prec,
6574                              cp_id_kind * pidk)
6575 {
6576   cp_parser_expression_stack stack;
6577   cp_parser_expression_stack_entry *sp = &stack[0];
6578   tree lhs, rhs;
6579   cp_token *token;
6580   enum tree_code tree_type, lhs_type, rhs_type;
6581   enum cp_parser_prec new_prec, lookahead_prec;
6582   bool overloaded_p;
6583
6584   /* Parse the first expression.  */
6585   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
6586   lhs_type = ERROR_MARK;
6587
6588   for (;;)
6589     {
6590       /* Get an operator token.  */
6591       token = cp_lexer_peek_token (parser->lexer);
6592
6593       if (warn_cxx0x_compat
6594           && token->type == CPP_RSHIFT
6595           && !parser->greater_than_is_operator_p)
6596         {
6597           if (warning_at (token->location, OPT_Wc__0x_compat, 
6598                           "%<>>%> operator will be treated as"
6599                           " two right angle brackets in C++0x"))
6600             inform (token->location,
6601                     "suggest parentheses around %<>>%> expression");
6602         }
6603
6604       new_prec = TOKEN_PRECEDENCE (token);
6605
6606       /* Popping an entry off the stack means we completed a subexpression:
6607          - either we found a token which is not an operator (`>' where it is not
6608            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
6609            will happen repeatedly;
6610          - or, we found an operator which has lower priority.  This is the case
6611            where the recursive descent *ascends*, as in `3 * 4 + 5' after
6612            parsing `3 * 4'.  */
6613       if (new_prec <= prec)
6614         {
6615           if (sp == stack)
6616             break;
6617           else
6618             goto pop;
6619         }
6620
6621      get_rhs:
6622       tree_type = binops_by_token[token->type].tree_type;
6623
6624       /* We used the operator token.  */
6625       cp_lexer_consume_token (parser->lexer);
6626
6627       /* For "false && x" or "true || x", x will never be executed;
6628          disable warnings while evaluating it.  */
6629       if (tree_type == TRUTH_ANDIF_EXPR)
6630         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
6631       else if (tree_type == TRUTH_ORIF_EXPR)
6632         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
6633
6634       /* Extract another operand.  It may be the RHS of this expression
6635          or the LHS of a new, higher priority expression.  */
6636       rhs = cp_parser_simple_cast_expression (parser);
6637       rhs_type = ERROR_MARK;
6638
6639       /* Get another operator token.  Look up its precedence to avoid
6640          building a useless (immediately popped) stack entry for common
6641          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
6642       token = cp_lexer_peek_token (parser->lexer);
6643       lookahead_prec = TOKEN_PRECEDENCE (token);
6644       if (lookahead_prec > new_prec)
6645         {
6646           /* ... and prepare to parse the RHS of the new, higher priority
6647              expression.  Since precedence levels on the stack are
6648              monotonically increasing, we do not have to care about
6649              stack overflows.  */
6650           sp->prec = prec;
6651           sp->tree_type = tree_type;
6652           sp->lhs = lhs;
6653           sp->lhs_type = lhs_type;
6654           sp++;
6655           lhs = rhs;
6656           lhs_type = rhs_type;
6657           prec = new_prec;
6658           new_prec = lookahead_prec;
6659           goto get_rhs;
6660
6661          pop:
6662           lookahead_prec = new_prec;
6663           /* If the stack is not empty, we have parsed into LHS the right side
6664              (`4' in the example above) of an expression we had suspended.
6665              We can use the information on the stack to recover the LHS (`3')
6666              from the stack together with the tree code (`MULT_EXPR'), and
6667              the precedence of the higher level subexpression
6668              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
6669              which will be used to actually build the additive expression.  */
6670           --sp;
6671           prec = sp->prec;
6672           tree_type = sp->tree_type;
6673           rhs = lhs;
6674           rhs_type = lhs_type;
6675           lhs = sp->lhs;
6676           lhs_type = sp->lhs_type;
6677         }
6678
6679       /* Undo the disabling of warnings done above.  */
6680       if (tree_type == TRUTH_ANDIF_EXPR)
6681         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
6682       else if (tree_type == TRUTH_ORIF_EXPR)
6683         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
6684
6685       overloaded_p = false;
6686       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
6687          ERROR_MARK for everything that is not a binary expression.
6688          This makes warn_about_parentheses miss some warnings that
6689          involve unary operators.  For unary expressions we should
6690          pass the correct tree_code unless the unary expression was
6691          surrounded by parentheses.
6692       */
6693       if (no_toplevel_fold_p
6694           && lookahead_prec <= prec
6695           && sp == stack
6696           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
6697         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
6698       else
6699         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
6700                                  &overloaded_p, tf_warning_or_error);
6701       lhs_type = tree_type;
6702
6703       /* If the binary operator required the use of an overloaded operator,
6704          then this expression cannot be an integral constant-expression.
6705          An overloaded operator can be used even if both operands are
6706          otherwise permissible in an integral constant-expression if at
6707          least one of the operands is of enumeration type.  */
6708
6709       if (overloaded_p
6710           && cp_parser_non_integral_constant_expression (parser,
6711                                                          NIC_OVERLOADED))
6712         return error_mark_node;
6713     }
6714
6715   return lhs;
6716 }
6717
6718
6719 /* Parse the `? expression : assignment-expression' part of a
6720    conditional-expression.  The LOGICAL_OR_EXPR is the
6721    logical-or-expression that started the conditional-expression.
6722    Returns a representation of the entire conditional-expression.
6723
6724    This routine is used by cp_parser_assignment_expression.
6725
6726      ? expression : assignment-expression
6727
6728    GNU Extensions:
6729
6730      ? : assignment-expression */
6731
6732 static tree
6733 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
6734 {
6735   tree expr;
6736   tree assignment_expr;
6737   struct cp_token *token;
6738
6739   /* Consume the `?' token.  */
6740   cp_lexer_consume_token (parser->lexer);
6741   token = cp_lexer_peek_token (parser->lexer);
6742   if (cp_parser_allow_gnu_extensions_p (parser)
6743       && token->type == CPP_COLON)
6744     {
6745       pedwarn (token->location, OPT_pedantic, 
6746                "ISO C++ does not allow ?: with omitted middle operand");
6747       /* Implicit true clause.  */
6748       expr = NULL_TREE;
6749       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
6750       warn_for_omitted_condop (token->location, logical_or_expr);
6751     }
6752   else
6753     {
6754       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
6755       parser->colon_corrects_to_scope_p = false;
6756       /* Parse the expression.  */
6757       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
6758       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6759       c_inhibit_evaluation_warnings +=
6760         ((logical_or_expr == truthvalue_true_node)
6761          - (logical_or_expr == truthvalue_false_node));
6762       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
6763     }
6764
6765   /* The next token should be a `:'.  */
6766   cp_parser_require (parser, CPP_COLON, RT_COLON);
6767   /* Parse the assignment-expression.  */
6768   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
6769   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
6770
6771   /* Build the conditional-expression.  */
6772   return build_x_conditional_expr (logical_or_expr,
6773                                    expr,
6774                                    assignment_expr,
6775                                    tf_warning_or_error);
6776 }
6777
6778 /* Parse an assignment-expression.
6779
6780    assignment-expression:
6781      conditional-expression
6782      logical-or-expression assignment-operator assignment_expression
6783      throw-expression
6784
6785    CAST_P is true if this expression is the target of a cast.
6786
6787    Returns a representation for the expression.  */
6788
6789 static tree
6790 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
6791                                  cp_id_kind * pidk)
6792 {
6793   tree expr;
6794
6795   /* If the next token is the `throw' keyword, then we're looking at
6796      a throw-expression.  */
6797   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
6798     expr = cp_parser_throw_expression (parser);
6799   /* Otherwise, it must be that we are looking at a
6800      logical-or-expression.  */
6801   else
6802     {
6803       /* Parse the binary expressions (logical-or-expression).  */
6804       expr = cp_parser_binary_expression (parser, cast_p, false,
6805                                           PREC_NOT_OPERATOR, pidk);
6806       /* If the next token is a `?' then we're actually looking at a
6807          conditional-expression.  */
6808       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
6809         return cp_parser_question_colon_clause (parser, expr);
6810       else
6811         {
6812           enum tree_code assignment_operator;
6813
6814           /* If it's an assignment-operator, we're using the second
6815              production.  */
6816           assignment_operator
6817             = cp_parser_assignment_operator_opt (parser);
6818           if (assignment_operator != ERROR_MARK)
6819             {
6820               bool non_constant_p;
6821
6822               /* Parse the right-hand side of the assignment.  */
6823               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
6824
6825               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
6826                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6827
6828               /* An assignment may not appear in a
6829                  constant-expression.  */
6830               if (cp_parser_non_integral_constant_expression (parser,
6831                                                               NIC_ASSIGNMENT))
6832                 return error_mark_node;
6833               /* Build the assignment expression.  */
6834               expr = build_x_modify_expr (expr,
6835                                           assignment_operator,
6836                                           rhs,
6837                                           tf_warning_or_error);
6838             }
6839         }
6840     }
6841
6842   return expr;
6843 }
6844
6845 /* Parse an (optional) assignment-operator.
6846
6847    assignment-operator: one of
6848      = *= /= %= += -= >>= <<= &= ^= |=
6849
6850    GNU Extension:
6851
6852    assignment-operator: one of
6853      <?= >?=
6854
6855    If the next token is an assignment operator, the corresponding tree
6856    code is returned, and the token is consumed.  For example, for
6857    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
6858    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
6859    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
6860    operator, ERROR_MARK is returned.  */
6861
6862 static enum tree_code
6863 cp_parser_assignment_operator_opt (cp_parser* parser)
6864 {
6865   enum tree_code op;
6866   cp_token *token;
6867
6868   /* Peek at the next token.  */
6869   token = cp_lexer_peek_token (parser->lexer);
6870
6871   switch (token->type)
6872     {
6873     case CPP_EQ:
6874       op = NOP_EXPR;
6875       break;
6876
6877     case CPP_MULT_EQ:
6878       op = MULT_EXPR;
6879       break;
6880
6881     case CPP_DIV_EQ:
6882       op = TRUNC_DIV_EXPR;
6883       break;
6884
6885     case CPP_MOD_EQ:
6886       op = TRUNC_MOD_EXPR;
6887       break;
6888
6889     case CPP_PLUS_EQ:
6890       op = PLUS_EXPR;
6891       break;
6892
6893     case CPP_MINUS_EQ:
6894       op = MINUS_EXPR;
6895       break;
6896
6897     case CPP_RSHIFT_EQ:
6898       op = RSHIFT_EXPR;
6899       break;
6900
6901     case CPP_LSHIFT_EQ:
6902       op = LSHIFT_EXPR;
6903       break;
6904
6905     case CPP_AND_EQ:
6906       op = BIT_AND_EXPR;
6907       break;
6908
6909     case CPP_XOR_EQ:
6910       op = BIT_XOR_EXPR;
6911       break;
6912
6913     case CPP_OR_EQ:
6914       op = BIT_IOR_EXPR;
6915       break;
6916
6917     default:
6918       /* Nothing else is an assignment operator.  */
6919       op = ERROR_MARK;
6920     }
6921
6922   /* If it was an assignment operator, consume it.  */
6923   if (op != ERROR_MARK)
6924     cp_lexer_consume_token (parser->lexer);
6925
6926   return op;
6927 }
6928
6929 /* Parse an expression.
6930
6931    expression:
6932      assignment-expression
6933      expression , assignment-expression
6934
6935    CAST_P is true if this expression is the target of a cast.
6936
6937    Returns a representation of the expression.  */
6938
6939 static tree
6940 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
6941 {
6942   tree expression = NULL_TREE;
6943
6944   while (true)
6945     {
6946       tree assignment_expression;
6947
6948       /* Parse the next assignment-expression.  */
6949       assignment_expression
6950         = cp_parser_assignment_expression (parser, cast_p, pidk);
6951       /* If this is the first assignment-expression, we can just
6952          save it away.  */
6953       if (!expression)
6954         expression = assignment_expression;
6955       else
6956         expression = build_x_compound_expr (expression,
6957                                             assignment_expression,
6958                                             tf_warning_or_error);
6959       /* If the next token is not a comma, then we are done with the
6960          expression.  */
6961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6962         break;
6963       /* Consume the `,'.  */
6964       cp_lexer_consume_token (parser->lexer);
6965       /* A comma operator cannot appear in a constant-expression.  */
6966       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
6967         expression = error_mark_node;
6968     }
6969
6970   return expression;
6971 }
6972
6973 /* Parse a constant-expression.
6974
6975    constant-expression:
6976      conditional-expression
6977
6978   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
6979   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
6980   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
6981   is false, NON_CONSTANT_P should be NULL.  */
6982
6983 static tree
6984 cp_parser_constant_expression (cp_parser* parser,
6985                                bool allow_non_constant_p,
6986                                bool *non_constant_p)
6987 {
6988   bool saved_integral_constant_expression_p;
6989   bool saved_allow_non_integral_constant_expression_p;
6990   bool saved_non_integral_constant_expression_p;
6991   tree expression;
6992
6993   /* It might seem that we could simply parse the
6994      conditional-expression, and then check to see if it were
6995      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
6996      one that the compiler can figure out is constant, possibly after
6997      doing some simplifications or optimizations.  The standard has a
6998      precise definition of constant-expression, and we must honor
6999      that, even though it is somewhat more restrictive.
7000
7001      For example:
7002
7003        int i[(2, 3)];
7004
7005      is not a legal declaration, because `(2, 3)' is not a
7006      constant-expression.  The `,' operator is forbidden in a
7007      constant-expression.  However, GCC's constant-folding machinery
7008      will fold this operation to an INTEGER_CST for `3'.  */
7009
7010   /* Save the old settings.  */
7011   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7012   saved_allow_non_integral_constant_expression_p
7013     = parser->allow_non_integral_constant_expression_p;
7014   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7015   /* We are now parsing a constant-expression.  */
7016   parser->integral_constant_expression_p = true;
7017   parser->allow_non_integral_constant_expression_p
7018     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7019   parser->non_integral_constant_expression_p = false;
7020   /* Although the grammar says "conditional-expression", we parse an
7021      "assignment-expression", which also permits "throw-expression"
7022      and the use of assignment operators.  In the case that
7023      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7024      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7025      actually essential that we look for an assignment-expression.
7026      For example, cp_parser_initializer_clauses uses this function to
7027      determine whether a particular assignment-expression is in fact
7028      constant.  */
7029   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7030   /* Restore the old settings.  */
7031   parser->integral_constant_expression_p
7032     = saved_integral_constant_expression_p;
7033   parser->allow_non_integral_constant_expression_p
7034     = saved_allow_non_integral_constant_expression_p;
7035   if (cxx_dialect >= cxx0x)
7036     {
7037       /* Require an rvalue constant expression here; that's what our
7038          callers expect.  Reference constant expressions are handled
7039          separately in e.g. cp_parser_template_argument.  */
7040       bool is_const = potential_rvalue_constant_expression (expression);
7041       parser->non_integral_constant_expression_p = !is_const;
7042       if (!is_const && !allow_non_constant_p)
7043         require_potential_rvalue_constant_expression (expression);
7044     }
7045   if (allow_non_constant_p)
7046     *non_constant_p = parser->non_integral_constant_expression_p;
7047   else if (parser->non_integral_constant_expression_p)
7048     expression = error_mark_node;
7049   parser->non_integral_constant_expression_p
7050     = saved_non_integral_constant_expression_p;
7051
7052   return expression;
7053 }
7054
7055 /* Parse __builtin_offsetof.
7056
7057    offsetof-expression:
7058      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7059
7060    offsetof-member-designator:
7061      id-expression
7062      | offsetof-member-designator "." id-expression
7063      | offsetof-member-designator "[" expression "]"
7064      | offsetof-member-designator "->" id-expression  */
7065
7066 static tree
7067 cp_parser_builtin_offsetof (cp_parser *parser)
7068 {
7069   int save_ice_p, save_non_ice_p;
7070   tree type, expr;
7071   cp_id_kind dummy;
7072   cp_token *token;
7073
7074   /* We're about to accept non-integral-constant things, but will
7075      definitely yield an integral constant expression.  Save and
7076      restore these values around our local parsing.  */
7077   save_ice_p = parser->integral_constant_expression_p;
7078   save_non_ice_p = parser->non_integral_constant_expression_p;
7079
7080   /* Consume the "__builtin_offsetof" token.  */
7081   cp_lexer_consume_token (parser->lexer);
7082   /* Consume the opening `('.  */
7083   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7084   /* Parse the type-id.  */
7085   type = cp_parser_type_id (parser);
7086   /* Look for the `,'.  */
7087   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7088   token = cp_lexer_peek_token (parser->lexer);
7089
7090   /* Build the (type *)null that begins the traditional offsetof macro.  */
7091   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7092                             tf_warning_or_error);
7093
7094   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7095   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7096                                                  true, &dummy, token->location);
7097   while (true)
7098     {
7099       token = cp_lexer_peek_token (parser->lexer);
7100       switch (token->type)
7101         {
7102         case CPP_OPEN_SQUARE:
7103           /* offsetof-member-designator "[" expression "]" */
7104           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7105           break;
7106
7107         case CPP_DEREF:
7108           /* offsetof-member-designator "->" identifier */
7109           expr = grok_array_decl (expr, integer_zero_node);
7110           /* FALLTHRU */
7111
7112         case CPP_DOT:
7113           /* offsetof-member-designator "." identifier */
7114           cp_lexer_consume_token (parser->lexer);
7115           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7116                                                          expr, true, &dummy,
7117                                                          token->location);
7118           break;
7119
7120         case CPP_CLOSE_PAREN:
7121           /* Consume the ")" token.  */
7122           cp_lexer_consume_token (parser->lexer);
7123           goto success;
7124
7125         default:
7126           /* Error.  We know the following require will fail, but
7127              that gives the proper error message.  */
7128           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7129           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7130           expr = error_mark_node;
7131           goto failure;
7132         }
7133     }
7134
7135  success:
7136   /* If we're processing a template, we can't finish the semantics yet.
7137      Otherwise we can fold the entire expression now.  */
7138   if (processing_template_decl)
7139     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7140   else
7141     expr = finish_offsetof (expr);
7142
7143  failure:
7144   parser->integral_constant_expression_p = save_ice_p;
7145   parser->non_integral_constant_expression_p = save_non_ice_p;
7146
7147   return expr;
7148 }
7149
7150 /* Parse a trait expression.
7151
7152    Returns a representation of the expression, the underlying type
7153    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7154
7155 static tree
7156 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7157 {
7158   cp_trait_kind kind;
7159   tree type1, type2 = NULL_TREE;
7160   bool binary = false;
7161   cp_decl_specifier_seq decl_specs;
7162
7163   switch (keyword)
7164     {
7165     case RID_HAS_NOTHROW_ASSIGN:
7166       kind = CPTK_HAS_NOTHROW_ASSIGN;
7167       break;
7168     case RID_HAS_NOTHROW_CONSTRUCTOR:
7169       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7170       break;
7171     case RID_HAS_NOTHROW_COPY:
7172       kind = CPTK_HAS_NOTHROW_COPY;
7173       break;
7174     case RID_HAS_TRIVIAL_ASSIGN:
7175       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7176       break;
7177     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7178       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7179       break;
7180     case RID_HAS_TRIVIAL_COPY:
7181       kind = CPTK_HAS_TRIVIAL_COPY;
7182       break;
7183     case RID_HAS_TRIVIAL_DESTRUCTOR:
7184       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7185       break;
7186     case RID_HAS_VIRTUAL_DESTRUCTOR:
7187       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7188       break;
7189     case RID_IS_ABSTRACT:
7190       kind = CPTK_IS_ABSTRACT;
7191       break;
7192     case RID_IS_BASE_OF:
7193       kind = CPTK_IS_BASE_OF;
7194       binary = true;
7195       break;
7196     case RID_IS_CLASS:
7197       kind = CPTK_IS_CLASS;
7198       break;
7199     case RID_IS_CONVERTIBLE_TO:
7200       kind = CPTK_IS_CONVERTIBLE_TO;
7201       binary = true;
7202       break;
7203     case RID_IS_EMPTY:
7204       kind = CPTK_IS_EMPTY;
7205       break;
7206     case RID_IS_ENUM:
7207       kind = CPTK_IS_ENUM;
7208       break;
7209     case RID_IS_LITERAL_TYPE:
7210       kind = CPTK_IS_LITERAL_TYPE;
7211       break;
7212     case RID_IS_POD:
7213       kind = CPTK_IS_POD;
7214       break;
7215     case RID_IS_POLYMORPHIC:
7216       kind = CPTK_IS_POLYMORPHIC;
7217       break;
7218     case RID_IS_STD_LAYOUT:
7219       kind = CPTK_IS_STD_LAYOUT;
7220       break;
7221     case RID_IS_TRIVIAL:
7222       kind = CPTK_IS_TRIVIAL;
7223       break;
7224     case RID_IS_UNION:
7225       kind = CPTK_IS_UNION;
7226       break;
7227     case RID_UNDERLYING_TYPE:
7228       kind = CPTK_UNDERLYING_TYPE;
7229       break;
7230     default:
7231       gcc_unreachable ();
7232     }
7233
7234   /* Consume the token.  */
7235   cp_lexer_consume_token (parser->lexer);
7236
7237   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7238
7239   type1 = cp_parser_type_id (parser);
7240
7241   if (type1 == error_mark_node)
7242     return error_mark_node;
7243
7244   /* Build a trivial decl-specifier-seq.  */
7245   clear_decl_specs (&decl_specs);
7246   decl_specs.type = type1;
7247
7248   /* Call grokdeclarator to figure out what type this is.  */
7249   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7250                           /*initialized=*/0, /*attrlist=*/NULL);
7251
7252   if (binary)
7253     {
7254       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7255  
7256       type2 = cp_parser_type_id (parser);
7257
7258       if (type2 == error_mark_node)
7259         return error_mark_node;
7260
7261       /* Build a trivial decl-specifier-seq.  */
7262       clear_decl_specs (&decl_specs);
7263       decl_specs.type = type2;
7264
7265       /* Call grokdeclarator to figure out what type this is.  */
7266       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7267                               /*initialized=*/0, /*attrlist=*/NULL);
7268     }
7269
7270   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7271
7272   /* Complete the trait expression, which may mean either processing
7273      the trait expr now or saving it for template instantiation.  */
7274   return kind != CPTK_UNDERLYING_TYPE
7275     ? finish_trait_expr (kind, type1, type2)
7276     : finish_underlying_type (type1);
7277 }
7278
7279 /* Lambdas that appear in variable initializer or default argument scope
7280    get that in their mangling, so we need to record it.  We might as well
7281    use the count for function and namespace scopes as well.  */
7282 static GTY(()) tree lambda_scope;
7283 static GTY(()) int lambda_count;
7284 typedef struct GTY(()) tree_int
7285 {
7286   tree t;
7287   int i;
7288 } tree_int;
7289 DEF_VEC_O(tree_int);
7290 DEF_VEC_ALLOC_O(tree_int,gc);
7291 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7292
7293 static void
7294 start_lambda_scope (tree decl)
7295 {
7296   tree_int ti;
7297   gcc_assert (decl);
7298   /* Once we're inside a function, we ignore other scopes and just push
7299      the function again so that popping works properly.  */
7300   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7301     decl = current_function_decl;
7302   ti.t = lambda_scope;
7303   ti.i = lambda_count;
7304   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7305   if (lambda_scope != decl)
7306     {
7307       /* Don't reset the count if we're still in the same function.  */
7308       lambda_scope = decl;
7309       lambda_count = 0;
7310     }
7311 }
7312
7313 static void
7314 record_lambda_scope (tree lambda)
7315 {
7316   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7317   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7318 }
7319
7320 static void
7321 finish_lambda_scope (void)
7322 {
7323   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7324   if (lambda_scope != p->t)
7325     {
7326       lambda_scope = p->t;
7327       lambda_count = p->i;
7328     }
7329   VEC_pop (tree_int, lambda_scope_stack);
7330 }
7331
7332 /* Parse a lambda expression.
7333
7334    lambda-expression:
7335      lambda-introducer lambda-declarator [opt] compound-statement
7336
7337    Returns a representation of the expression.  */
7338
7339 static tree
7340 cp_parser_lambda_expression (cp_parser* parser)
7341 {
7342   tree lambda_expr = build_lambda_expr ();
7343   tree type;
7344
7345   LAMBDA_EXPR_LOCATION (lambda_expr)
7346     = cp_lexer_peek_token (parser->lexer)->location;
7347
7348   if (cp_unevaluated_operand)
7349     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7350               "lambda-expression in unevaluated context");
7351
7352   /* We may be in the middle of deferred access check.  Disable
7353      it now.  */
7354   push_deferring_access_checks (dk_no_deferred);
7355
7356   cp_parser_lambda_introducer (parser, lambda_expr);
7357
7358   type = begin_lambda_type (lambda_expr);
7359
7360   record_lambda_scope (lambda_expr);
7361
7362   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7363   determine_visibility (TYPE_NAME (type));
7364
7365   /* Now that we've started the type, add the capture fields for any
7366      explicit captures.  */
7367   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7368
7369   {
7370     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7371     unsigned int saved_num_template_parameter_lists
7372         = parser->num_template_parameter_lists;
7373
7374     parser->num_template_parameter_lists = 0;
7375
7376     /* By virtue of defining a local class, a lambda expression has access to
7377        the private variables of enclosing classes.  */
7378
7379     cp_parser_lambda_declarator_opt (parser, lambda_expr);
7380
7381     cp_parser_lambda_body (parser, lambda_expr);
7382
7383     /* The capture list was built up in reverse order; fix that now.  */
7384     {
7385       tree newlist = NULL_TREE;
7386       tree elt, next;
7387
7388       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
7389            elt; elt = next)
7390         {
7391           tree field = TREE_PURPOSE (elt);
7392           char *buf;
7393
7394           next = TREE_CHAIN (elt);
7395           TREE_CHAIN (elt) = newlist;
7396           newlist = elt;
7397
7398           /* Also add __ to the beginning of the field name so that code
7399              outside the lambda body can't see the captured name.  We could
7400              just remove the name entirely, but this is more useful for
7401              debugging.  */
7402           if (field == LAMBDA_EXPR_THIS_CAPTURE (lambda_expr))
7403             /* The 'this' capture already starts with __.  */
7404             continue;
7405
7406           buf = (char *) alloca (IDENTIFIER_LENGTH (DECL_NAME (field)) + 3);
7407           buf[1] = buf[0] = '_';
7408           memcpy (buf + 2, IDENTIFIER_POINTER (DECL_NAME (field)),
7409                   IDENTIFIER_LENGTH (DECL_NAME (field)) + 1);
7410           DECL_NAME (field) = get_identifier (buf);
7411         }
7412       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
7413     }
7414
7415     maybe_add_lambda_conv_op (type);
7416
7417     type = finish_struct (type, /*attributes=*/NULL_TREE);
7418
7419     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
7420   }
7421
7422   pop_deferring_access_checks ();
7423
7424   return build_lambda_object (lambda_expr);
7425 }
7426
7427 /* Parse the beginning of a lambda expression.
7428
7429    lambda-introducer:
7430      [ lambda-capture [opt] ]
7431
7432    LAMBDA_EXPR is the current representation of the lambda expression.  */
7433
7434 static void
7435 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
7436 {
7437   /* Need commas after the first capture.  */
7438   bool first = true;
7439
7440   /* Eat the leading `['.  */
7441   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
7442
7443   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
7444   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
7445       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
7446     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
7447   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7448     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
7449
7450   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
7451     {
7452       cp_lexer_consume_token (parser->lexer);
7453       first = false;
7454     }
7455
7456   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
7457     {
7458       cp_token* capture_token;
7459       tree capture_id;
7460       tree capture_init_expr;
7461       cp_id_kind idk = CP_ID_KIND_NONE;
7462       bool explicit_init_p = false;
7463
7464       enum capture_kind_type
7465       {
7466         BY_COPY,
7467         BY_REFERENCE
7468       };
7469       enum capture_kind_type capture_kind = BY_COPY;
7470
7471       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
7472         {
7473           error ("expected end of capture-list");
7474           return;
7475         }
7476
7477       if (first)
7478         first = false;
7479       else
7480         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7481
7482       /* Possibly capture `this'.  */
7483       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
7484         {
7485           cp_lexer_consume_token (parser->lexer);
7486           add_capture (lambda_expr,
7487                        /*id=*/get_identifier ("__this"),
7488                        /*initializer=*/finish_this_expr(),
7489                        /*by_reference_p=*/false,
7490                        explicit_init_p);
7491           continue;
7492         }
7493
7494       /* Remember whether we want to capture as a reference or not.  */
7495       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
7496         {
7497           capture_kind = BY_REFERENCE;
7498           cp_lexer_consume_token (parser->lexer);
7499         }
7500
7501       /* Get the identifier.  */
7502       capture_token = cp_lexer_peek_token (parser->lexer);
7503       capture_id = cp_parser_identifier (parser);
7504
7505       if (capture_id == error_mark_node)
7506         /* Would be nice to have a cp_parser_skip_to_closing_x for general
7507            delimiters, but I modified this to stop on unnested ']' as well.  It
7508            was already changed to stop on unnested '}', so the
7509            "closing_parenthesis" name is no more misleading with my change.  */
7510         {
7511           cp_parser_skip_to_closing_parenthesis (parser,
7512                                                  /*recovering=*/true,
7513                                                  /*or_comma=*/true,
7514                                                  /*consume_paren=*/true);
7515           break;
7516         }
7517
7518       /* Find the initializer for this capture.  */
7519       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
7520         {
7521           /* An explicit expression exists.  */
7522           cp_lexer_consume_token (parser->lexer);
7523           pedwarn (input_location, OPT_pedantic,
7524                    "ISO C++ does not allow initializers "
7525                    "in lambda expression capture lists");
7526           capture_init_expr = cp_parser_assignment_expression (parser,
7527                                                                /*cast_p=*/true,
7528                                                                &idk);
7529           explicit_init_p = true;
7530         }
7531       else
7532         {
7533           const char* error_msg;
7534
7535           /* Turn the identifier into an id-expression.  */
7536           capture_init_expr
7537             = cp_parser_lookup_name
7538                 (parser,
7539                  capture_id,
7540                  none_type,
7541                  /*is_template=*/false,
7542                  /*is_namespace=*/false,
7543                  /*check_dependency=*/true,
7544                  /*ambiguous_decls=*/NULL,
7545                  capture_token->location);
7546
7547           capture_init_expr
7548             = finish_id_expression
7549                 (capture_id,
7550                  capture_init_expr,
7551                  parser->scope,
7552                  &idk,
7553                  /*integral_constant_expression_p=*/false,
7554                  /*allow_non_integral_constant_expression_p=*/false,
7555                  /*non_integral_constant_expression_p=*/NULL,
7556                  /*template_p=*/false,
7557                  /*done=*/true,
7558                  /*address_p=*/false,
7559                  /*template_arg_p=*/false,
7560                  &error_msg,
7561                  capture_token->location);
7562         }
7563
7564       if (TREE_CODE (capture_init_expr) == IDENTIFIER_NODE)
7565         capture_init_expr
7566           = unqualified_name_lookup_error (capture_init_expr);
7567
7568       add_capture (lambda_expr,
7569                    capture_id,
7570                    capture_init_expr,
7571                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
7572                    explicit_init_p);
7573     }
7574
7575   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
7576 }
7577
7578 /* Parse the (optional) middle of a lambda expression.
7579
7580    lambda-declarator:
7581      ( parameter-declaration-clause [opt] )
7582        attribute-specifier [opt]
7583        mutable [opt]
7584        exception-specification [opt]
7585        lambda-return-type-clause [opt]
7586
7587    LAMBDA_EXPR is the current representation of the lambda expression.  */
7588
7589 static void
7590 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
7591 {
7592   /* 5.1.1.4 of the standard says:
7593        If a lambda-expression does not include a lambda-declarator, it is as if
7594        the lambda-declarator were ().
7595      This means an empty parameter list, no attributes, and no exception
7596      specification.  */
7597   tree param_list = void_list_node;
7598   tree attributes = NULL_TREE;
7599   tree exception_spec = NULL_TREE;
7600   tree t;
7601
7602   /* The lambda-declarator is optional, but must begin with an opening
7603      parenthesis if present.  */
7604   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7605     {
7606       cp_lexer_consume_token (parser->lexer);
7607
7608       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
7609
7610       /* Parse parameters.  */
7611       param_list = cp_parser_parameter_declaration_clause (parser);
7612
7613       /* Default arguments shall not be specified in the
7614          parameter-declaration-clause of a lambda-declarator.  */
7615       for (t = param_list; t; t = TREE_CHAIN (t))
7616         if (TREE_PURPOSE (t))
7617           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
7618                    "default argument specified for lambda parameter");
7619
7620       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7621
7622       attributes = cp_parser_attributes_opt (parser);
7623
7624       /* Parse optional `mutable' keyword.  */
7625       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
7626         {
7627           cp_lexer_consume_token (parser->lexer);
7628           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
7629         }
7630
7631       /* Parse optional exception specification.  */
7632       exception_spec = cp_parser_exception_specification_opt (parser);
7633
7634       /* Parse optional trailing return type.  */
7635       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
7636         {
7637           cp_lexer_consume_token (parser->lexer);
7638           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
7639         }
7640
7641       /* The function parameters must be in scope all the way until after the
7642          trailing-return-type in case of decltype.  */
7643       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
7644         pop_binding (DECL_NAME (t), t);
7645
7646       leave_scope ();
7647     }
7648
7649   /* Create the function call operator.
7650
7651      Messing with declarators like this is no uglier than building up the
7652      FUNCTION_DECL by hand, and this is less likely to get out of sync with
7653      other code.  */
7654   {
7655     cp_decl_specifier_seq return_type_specs;
7656     cp_declarator* declarator;
7657     tree fco;
7658     int quals;
7659     void *p;
7660
7661     clear_decl_specs (&return_type_specs);
7662     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7663       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
7664     else
7665       /* Maybe we will deduce the return type later, but we can use void
7666          as a placeholder return type anyways.  */
7667       return_type_specs.type = void_type_node;
7668
7669     p = obstack_alloc (&declarator_obstack, 0);
7670
7671     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
7672                                      sfk_none);
7673
7674     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
7675              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
7676     declarator = make_call_declarator (declarator, param_list, quals,
7677                                        VIRT_SPEC_UNSPECIFIED,
7678                                        exception_spec,
7679                                        /*late_return_type=*/NULL_TREE);
7680     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
7681
7682     fco = grokmethod (&return_type_specs,
7683                       declarator,
7684                       attributes);
7685     DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
7686     DECL_ARTIFICIAL (fco) = 1;
7687
7688     finish_member_declaration (fco);
7689
7690     obstack_free (&declarator_obstack, p);
7691   }
7692 }
7693
7694 /* Parse the body of a lambda expression, which is simply
7695
7696    compound-statement
7697
7698    but which requires special handling.
7699    LAMBDA_EXPR is the current representation of the lambda expression.  */
7700
7701 static void
7702 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
7703 {
7704   bool nested = (current_function_decl != NULL_TREE);
7705   if (nested)
7706     push_function_context ();
7707
7708   /* Finish the function call operator
7709      - class_specifier
7710      + late_parsing_for_member
7711      + function_definition_after_declarator
7712      + ctor_initializer_opt_and_function_body  */
7713   {
7714     tree fco = lambda_function (lambda_expr);
7715     tree body;
7716     bool done = false;
7717
7718     /* Let the front end know that we are going to be defining this
7719        function.  */
7720     start_preparsed_function (fco,
7721                               NULL_TREE,
7722                               SF_PRE_PARSED | SF_INCLASS_INLINE);
7723
7724     start_lambda_scope (fco);
7725     body = begin_function_body ();
7726
7727     /* 5.1.1.4 of the standard says:
7728          If a lambda-expression does not include a trailing-return-type, it
7729          is as if the trailing-return-type denotes the following type:
7730           * if the compound-statement is of the form
7731                { return attribute-specifier [opt] expression ; }
7732              the type of the returned expression after lvalue-to-rvalue
7733              conversion (_conv.lval_ 4.1), array-to-pointer conversion
7734              (_conv.array_ 4.2), and function-to-pointer conversion
7735              (_conv.func_ 4.3);
7736           * otherwise, void.  */
7737
7738     /* In a lambda that has neither a lambda-return-type-clause
7739        nor a deducible form, errors should be reported for return statements
7740        in the body.  Since we used void as the placeholder return type, parsing
7741        the body as usual will give such desired behavior.  */
7742     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
7743         && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
7744         && cp_lexer_peek_nth_token (parser->lexer, 2)->keyword == RID_RETURN
7745         && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_SEMICOLON)
7746       {
7747         tree compound_stmt;
7748         tree expr = NULL_TREE;
7749         cp_id_kind idk = CP_ID_KIND_NONE;
7750
7751         /* Parse tentatively in case there's more after the initial return
7752            statement.  */
7753         cp_parser_parse_tentatively (parser);
7754
7755         cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
7756         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
7757
7758         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
7759
7760         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
7761         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
7762
7763         if (cp_parser_parse_definitely (parser))
7764           {
7765             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
7766
7767             compound_stmt = begin_compound_stmt (0);
7768             /* Will get error here if type not deduced yet.  */
7769             finish_return_stmt (expr);
7770             finish_compound_stmt (compound_stmt);
7771
7772             done = true;
7773           }
7774       }
7775
7776     if (!done)
7777       {
7778         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
7779           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
7780         /* TODO: does begin_compound_stmt want BCS_FN_BODY?
7781            cp_parser_compound_stmt does not pass it.  */
7782         cp_parser_function_body (parser);
7783         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
7784       }
7785
7786     finish_function_body (body);
7787     finish_lambda_scope ();
7788
7789     /* Finish the function and generate code for it if necessary.  */
7790     expand_or_defer_fn (finish_function (/*inline*/2));
7791   }
7792
7793   if (nested)
7794     pop_function_context();
7795 }
7796
7797 /* Statements [gram.stmt.stmt]  */
7798
7799 /* Parse a statement.
7800
7801    statement:
7802      labeled-statement
7803      expression-statement
7804      compound-statement
7805      selection-statement
7806      iteration-statement
7807      jump-statement
7808      declaration-statement
7809      try-block
7810
7811   IN_COMPOUND is true when the statement is nested inside a
7812   cp_parser_compound_statement; this matters for certain pragmas.
7813
7814   If IF_P is not NULL, *IF_P is set to indicate whether the statement
7815   is a (possibly labeled) if statement which is not enclosed in braces
7816   and has an else clause.  This is used to implement -Wparentheses.  */
7817
7818 static void
7819 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
7820                      bool in_compound, bool *if_p)
7821 {
7822   tree statement;
7823   cp_token *token;
7824   location_t statement_location;
7825
7826  restart:
7827   if (if_p != NULL)
7828     *if_p = false;
7829   /* There is no statement yet.  */
7830   statement = NULL_TREE;
7831   /* Peek at the next token.  */
7832   token = cp_lexer_peek_token (parser->lexer);
7833   /* Remember the location of the first token in the statement.  */
7834   statement_location = token->location;
7835   /* If this is a keyword, then that will often determine what kind of
7836      statement we have.  */
7837   if (token->type == CPP_KEYWORD)
7838     {
7839       enum rid keyword = token->keyword;
7840
7841       switch (keyword)
7842         {
7843         case RID_CASE:
7844         case RID_DEFAULT:
7845           /* Looks like a labeled-statement with a case label.
7846              Parse the label, and then use tail recursion to parse
7847              the statement.  */
7848           cp_parser_label_for_labeled_statement (parser);
7849           goto restart;
7850
7851         case RID_IF:
7852         case RID_SWITCH:
7853           statement = cp_parser_selection_statement (parser, if_p);
7854           break;
7855
7856         case RID_WHILE:
7857         case RID_DO:
7858         case RID_FOR:
7859           statement = cp_parser_iteration_statement (parser);
7860           break;
7861
7862         case RID_BREAK:
7863         case RID_CONTINUE:
7864         case RID_RETURN:
7865         case RID_GOTO:
7866           statement = cp_parser_jump_statement (parser);
7867           break;
7868
7869           /* Objective-C++ exception-handling constructs.  */
7870         case RID_AT_TRY:
7871         case RID_AT_CATCH:
7872         case RID_AT_FINALLY:
7873         case RID_AT_SYNCHRONIZED:
7874         case RID_AT_THROW:
7875           statement = cp_parser_objc_statement (parser);
7876           break;
7877
7878         case RID_TRY:
7879           statement = cp_parser_try_block (parser);
7880           break;
7881
7882         case RID_NAMESPACE:
7883           /* This must be a namespace alias definition.  */
7884           cp_parser_declaration_statement (parser);
7885           return;
7886           
7887         default:
7888           /* It might be a keyword like `int' that can start a
7889              declaration-statement.  */
7890           break;
7891         }
7892     }
7893   else if (token->type == CPP_NAME)
7894     {
7895       /* If the next token is a `:', then we are looking at a
7896          labeled-statement.  */
7897       token = cp_lexer_peek_nth_token (parser->lexer, 2);
7898       if (token->type == CPP_COLON)
7899         {
7900           /* Looks like a labeled-statement with an ordinary label.
7901              Parse the label, and then use tail recursion to parse
7902              the statement.  */
7903           cp_parser_label_for_labeled_statement (parser);
7904           goto restart;
7905         }
7906     }
7907   /* Anything that starts with a `{' must be a compound-statement.  */
7908   else if (token->type == CPP_OPEN_BRACE)
7909     statement = cp_parser_compound_statement (parser, NULL, false, false);
7910   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
7911      a statement all its own.  */
7912   else if (token->type == CPP_PRAGMA)
7913     {
7914       /* Only certain OpenMP pragmas are attached to statements, and thus
7915          are considered statements themselves.  All others are not.  In
7916          the context of a compound, accept the pragma as a "statement" and
7917          return so that we can check for a close brace.  Otherwise we
7918          require a real statement and must go back and read one.  */
7919       if (in_compound)
7920         cp_parser_pragma (parser, pragma_compound);
7921       else if (!cp_parser_pragma (parser, pragma_stmt))
7922         goto restart;
7923       return;
7924     }
7925   else if (token->type == CPP_EOF)
7926     {
7927       cp_parser_error (parser, "expected statement");
7928       return;
7929     }
7930
7931   /* Everything else must be a declaration-statement or an
7932      expression-statement.  Try for the declaration-statement
7933      first, unless we are looking at a `;', in which case we know that
7934      we have an expression-statement.  */
7935   if (!statement)
7936     {
7937       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
7938         {
7939           cp_parser_parse_tentatively (parser);
7940           /* Try to parse the declaration-statement.  */
7941           cp_parser_declaration_statement (parser);
7942           /* If that worked, we're done.  */
7943           if (cp_parser_parse_definitely (parser))
7944             return;
7945         }
7946       /* Look for an expression-statement instead.  */
7947       statement = cp_parser_expression_statement (parser, in_statement_expr);
7948     }
7949
7950   /* Set the line number for the statement.  */
7951   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
7952     SET_EXPR_LOCATION (statement, statement_location);
7953 }
7954
7955 /* Parse the label for a labeled-statement, i.e.
7956
7957    identifier :
7958    case constant-expression :
7959    default :
7960
7961    GNU Extension:
7962    case constant-expression ... constant-expression : statement
7963
7964    When a label is parsed without errors, the label is added to the
7965    parse tree by the finish_* functions, so this function doesn't
7966    have to return the label.  */
7967
7968 static void
7969 cp_parser_label_for_labeled_statement (cp_parser* parser)
7970 {
7971   cp_token *token;
7972   tree label = NULL_TREE;
7973   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7974
7975   /* The next token should be an identifier.  */
7976   token = cp_lexer_peek_token (parser->lexer);
7977   if (token->type != CPP_NAME
7978       && token->type != CPP_KEYWORD)
7979     {
7980       cp_parser_error (parser, "expected labeled-statement");
7981       return;
7982     }
7983
7984   parser->colon_corrects_to_scope_p = false;
7985   switch (token->keyword)
7986     {
7987     case RID_CASE:
7988       {
7989         tree expr, expr_hi;
7990         cp_token *ellipsis;
7991
7992         /* Consume the `case' token.  */
7993         cp_lexer_consume_token (parser->lexer);
7994         /* Parse the constant-expression.  */
7995         expr = cp_parser_constant_expression (parser,
7996                                               /*allow_non_constant_p=*/false,
7997                                               NULL);
7998
7999         ellipsis = cp_lexer_peek_token (parser->lexer);
8000         if (ellipsis->type == CPP_ELLIPSIS)
8001           {
8002             /* Consume the `...' token.  */
8003             cp_lexer_consume_token (parser->lexer);
8004             expr_hi =
8005               cp_parser_constant_expression (parser,
8006                                              /*allow_non_constant_p=*/false,
8007                                              NULL);
8008             /* We don't need to emit warnings here, as the common code
8009                will do this for us.  */
8010           }
8011         else
8012           expr_hi = NULL_TREE;
8013
8014         if (parser->in_switch_statement_p)
8015           finish_case_label (token->location, expr, expr_hi);
8016         else
8017           error_at (token->location,
8018                     "case label %qE not within a switch statement",
8019                     expr);
8020       }
8021       break;
8022
8023     case RID_DEFAULT:
8024       /* Consume the `default' token.  */
8025       cp_lexer_consume_token (parser->lexer);
8026
8027       if (parser->in_switch_statement_p)
8028         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8029       else
8030         error_at (token->location, "case label not within a switch statement");
8031       break;
8032
8033     default:
8034       /* Anything else must be an ordinary label.  */
8035       label = finish_label_stmt (cp_parser_identifier (parser));
8036       break;
8037     }
8038
8039   /* Require the `:' token.  */
8040   cp_parser_require (parser, CPP_COLON, RT_COLON);
8041
8042   /* An ordinary label may optionally be followed by attributes.
8043      However, this is only permitted if the attributes are then
8044      followed by a semicolon.  This is because, for backward
8045      compatibility, when parsing
8046        lab: __attribute__ ((unused)) int i;
8047      we want the attribute to attach to "i", not "lab".  */
8048   if (label != NULL_TREE
8049       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8050     {
8051       tree attrs;
8052
8053       cp_parser_parse_tentatively (parser);
8054       attrs = cp_parser_attributes_opt (parser);
8055       if (attrs == NULL_TREE
8056           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8057         cp_parser_abort_tentative_parse (parser);
8058       else if (!cp_parser_parse_definitely (parser))
8059         ;
8060       else
8061         cplus_decl_attributes (&label, attrs, 0);
8062     }
8063
8064   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8065 }
8066
8067 /* Parse an expression-statement.
8068
8069    expression-statement:
8070      expression [opt] ;
8071
8072    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8073    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8074    indicates whether this expression-statement is part of an
8075    expression statement.  */
8076
8077 static tree
8078 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8079 {
8080   tree statement = NULL_TREE;
8081   cp_token *token = cp_lexer_peek_token (parser->lexer);
8082
8083   /* If the next token is a ';', then there is no expression
8084      statement.  */
8085   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8086     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8087
8088   /* Give a helpful message for "A<T>::type t;" and the like.  */
8089   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8090       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8091     {
8092       if (TREE_CODE (statement) == SCOPE_REF)
8093         error_at (token->location, "need %<typename%> before %qE because "
8094                   "%qT is a dependent scope",
8095                   statement, TREE_OPERAND (statement, 0));
8096       else if (is_overloaded_fn (statement)
8097                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8098         {
8099           /* A::A a; */
8100           tree fn = get_first_fn (statement);
8101           error_at (token->location,
8102                     "%<%T::%D%> names the constructor, not the type",
8103                     DECL_CONTEXT (fn), DECL_NAME (fn));
8104         }
8105     }
8106
8107   /* Consume the final `;'.  */
8108   cp_parser_consume_semicolon_at_end_of_statement (parser);
8109
8110   if (in_statement_expr
8111       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8112     /* This is the final expression statement of a statement
8113        expression.  */
8114     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8115   else if (statement)
8116     statement = finish_expr_stmt (statement);
8117   else
8118     finish_stmt ();
8119
8120   return statement;
8121 }
8122
8123 /* Parse a compound-statement.
8124
8125    compound-statement:
8126      { statement-seq [opt] }
8127
8128    GNU extension:
8129
8130    compound-statement:
8131      { label-declaration-seq [opt] statement-seq [opt] }
8132
8133    label-declaration-seq:
8134      label-declaration
8135      label-declaration-seq label-declaration
8136
8137    Returns a tree representing the statement.  */
8138
8139 static tree
8140 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8141                               bool in_try, bool function_body)
8142 {
8143   tree compound_stmt;
8144
8145   /* Consume the `{'.  */
8146   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8147     return error_mark_node;
8148   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8149       && !function_body)
8150     pedwarn (input_location, OPT_pedantic,
8151              "compound-statement in constexpr function");
8152   /* Begin the compound-statement.  */
8153   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8154   /* If the next keyword is `__label__' we have a label declaration.  */
8155   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8156     cp_parser_label_declaration (parser);
8157   /* Parse an (optional) statement-seq.  */
8158   cp_parser_statement_seq_opt (parser, in_statement_expr);
8159   /* Finish the compound-statement.  */
8160   finish_compound_stmt (compound_stmt);
8161   /* Consume the `}'.  */
8162   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8163
8164   return compound_stmt;
8165 }
8166
8167 /* Parse an (optional) statement-seq.
8168
8169    statement-seq:
8170      statement
8171      statement-seq [opt] statement  */
8172
8173 static void
8174 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8175 {
8176   /* Scan statements until there aren't any more.  */
8177   while (true)
8178     {
8179       cp_token *token = cp_lexer_peek_token (parser->lexer);
8180
8181       /* If we are looking at a `}', then we have run out of
8182          statements; the same is true if we have reached the end
8183          of file, or have stumbled upon a stray '@end'.  */
8184       if (token->type == CPP_CLOSE_BRACE
8185           || token->type == CPP_EOF
8186           || token->type == CPP_PRAGMA_EOL
8187           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8188         break;
8189       
8190       /* If we are in a compound statement and find 'else' then
8191          something went wrong.  */
8192       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8193         {
8194           if (parser->in_statement & IN_IF_STMT) 
8195             break;
8196           else
8197             {
8198               token = cp_lexer_consume_token (parser->lexer);
8199               error_at (token->location, "%<else%> without a previous %<if%>");
8200             }
8201         }
8202
8203       /* Parse the statement.  */
8204       cp_parser_statement (parser, in_statement_expr, true, NULL);
8205     }
8206 }
8207
8208 /* Parse a selection-statement.
8209
8210    selection-statement:
8211      if ( condition ) statement
8212      if ( condition ) statement else statement
8213      switch ( condition ) statement
8214
8215    Returns the new IF_STMT or SWITCH_STMT.
8216
8217    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8218    is a (possibly labeled) if statement which is not enclosed in
8219    braces and has an else clause.  This is used to implement
8220    -Wparentheses.  */
8221
8222 static tree
8223 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8224 {
8225   cp_token *token;
8226   enum rid keyword;
8227
8228   if (if_p != NULL)
8229     *if_p = false;
8230
8231   /* Peek at the next token.  */
8232   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8233
8234   /* See what kind of keyword it is.  */
8235   keyword = token->keyword;
8236   switch (keyword)
8237     {
8238     case RID_IF:
8239     case RID_SWITCH:
8240       {
8241         tree statement;
8242         tree condition;
8243
8244         /* Look for the `('.  */
8245         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8246           {
8247             cp_parser_skip_to_end_of_statement (parser);
8248             return error_mark_node;
8249           }
8250
8251         /* Begin the selection-statement.  */
8252         if (keyword == RID_IF)
8253           statement = begin_if_stmt ();
8254         else
8255           statement = begin_switch_stmt ();
8256
8257         /* Parse the condition.  */
8258         condition = cp_parser_condition (parser);
8259         /* Look for the `)'.  */
8260         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8261           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8262                                                  /*consume_paren=*/true);
8263
8264         if (keyword == RID_IF)
8265           {
8266             bool nested_if;
8267             unsigned char in_statement;
8268
8269             /* Add the condition.  */
8270             finish_if_stmt_cond (condition, statement);
8271
8272             /* Parse the then-clause.  */
8273             in_statement = parser->in_statement;
8274             parser->in_statement |= IN_IF_STMT;
8275             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8276               {
8277                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8278                 add_stmt (build_empty_stmt (loc));
8279                 cp_lexer_consume_token (parser->lexer);
8280                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8281                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8282                               "empty body in an %<if%> statement");
8283                 nested_if = false;
8284               }
8285             else
8286               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8287             parser->in_statement = in_statement;
8288
8289             finish_then_clause (statement);
8290
8291             /* If the next token is `else', parse the else-clause.  */
8292             if (cp_lexer_next_token_is_keyword (parser->lexer,
8293                                                 RID_ELSE))
8294               {
8295                 /* Consume the `else' keyword.  */
8296                 cp_lexer_consume_token (parser->lexer);
8297                 begin_else_clause (statement);
8298                 /* Parse the else-clause.  */
8299                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8300                   {
8301                     location_t loc;
8302                     loc = cp_lexer_peek_token (parser->lexer)->location;
8303                     warning_at (loc,
8304                                 OPT_Wempty_body, "suggest braces around "
8305                                 "empty body in an %<else%> statement");
8306                     add_stmt (build_empty_stmt (loc));
8307                     cp_lexer_consume_token (parser->lexer);
8308                   }
8309                 else
8310                   cp_parser_implicitly_scoped_statement (parser, NULL);
8311
8312                 finish_else_clause (statement);
8313
8314                 /* If we are currently parsing a then-clause, then
8315                    IF_P will not be NULL.  We set it to true to
8316                    indicate that this if statement has an else clause.
8317                    This may trigger the Wparentheses warning below
8318                    when we get back up to the parent if statement.  */
8319                 if (if_p != NULL)
8320                   *if_p = true;
8321               }
8322             else
8323               {
8324                 /* This if statement does not have an else clause.  If
8325                    NESTED_IF is true, then the then-clause is an if
8326                    statement which does have an else clause.  We warn
8327                    about the potential ambiguity.  */
8328                 if (nested_if)
8329                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
8330                               "suggest explicit braces to avoid ambiguous"
8331                               " %<else%>");
8332               }
8333
8334             /* Now we're all done with the if-statement.  */
8335             finish_if_stmt (statement);
8336           }
8337         else
8338           {
8339             bool in_switch_statement_p;
8340             unsigned char in_statement;
8341
8342             /* Add the condition.  */
8343             finish_switch_cond (condition, statement);
8344
8345             /* Parse the body of the switch-statement.  */
8346             in_switch_statement_p = parser->in_switch_statement_p;
8347             in_statement = parser->in_statement;
8348             parser->in_switch_statement_p = true;
8349             parser->in_statement |= IN_SWITCH_STMT;
8350             cp_parser_implicitly_scoped_statement (parser, NULL);
8351             parser->in_switch_statement_p = in_switch_statement_p;
8352             parser->in_statement = in_statement;
8353
8354             /* Now we're all done with the switch-statement.  */
8355             finish_switch_stmt (statement);
8356           }
8357
8358         return statement;
8359       }
8360       break;
8361
8362     default:
8363       cp_parser_error (parser, "expected selection-statement");
8364       return error_mark_node;
8365     }
8366 }
8367
8368 /* Parse a condition.
8369
8370    condition:
8371      expression
8372      type-specifier-seq declarator = initializer-clause
8373      type-specifier-seq declarator braced-init-list
8374
8375    GNU Extension:
8376
8377    condition:
8378      type-specifier-seq declarator asm-specification [opt]
8379        attributes [opt] = assignment-expression
8380
8381    Returns the expression that should be tested.  */
8382
8383 static tree
8384 cp_parser_condition (cp_parser* parser)
8385 {
8386   cp_decl_specifier_seq type_specifiers;
8387   const char *saved_message;
8388   int declares_class_or_enum;
8389
8390   /* Try the declaration first.  */
8391   cp_parser_parse_tentatively (parser);
8392   /* New types are not allowed in the type-specifier-seq for a
8393      condition.  */
8394   saved_message = parser->type_definition_forbidden_message;
8395   parser->type_definition_forbidden_message
8396     = G_("types may not be defined in conditions");
8397   /* Parse the type-specifier-seq.  */
8398   cp_parser_decl_specifier_seq (parser,
8399                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
8400                                 &type_specifiers,
8401                                 &declares_class_or_enum);
8402   /* Restore the saved message.  */
8403   parser->type_definition_forbidden_message = saved_message;
8404   /* If all is well, we might be looking at a declaration.  */
8405   if (!cp_parser_error_occurred (parser))
8406     {
8407       tree decl;
8408       tree asm_specification;
8409       tree attributes;
8410       cp_declarator *declarator;
8411       tree initializer = NULL_TREE;
8412
8413       /* Parse the declarator.  */
8414       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
8415                                          /*ctor_dtor_or_conv_p=*/NULL,
8416                                          /*parenthesized_p=*/NULL,
8417                                          /*member_p=*/false);
8418       /* Parse the attributes.  */
8419       attributes = cp_parser_attributes_opt (parser);
8420       /* Parse the asm-specification.  */
8421       asm_specification = cp_parser_asm_specification_opt (parser);
8422       /* If the next token is not an `=' or '{', then we might still be
8423          looking at an expression.  For example:
8424
8425            if (A(a).x)
8426
8427          looks like a decl-specifier-seq and a declarator -- but then
8428          there is no `=', so this is an expression.  */
8429       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
8430           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
8431         cp_parser_simulate_error (parser);
8432         
8433       /* If we did see an `=' or '{', then we are looking at a declaration
8434          for sure.  */
8435       if (cp_parser_parse_definitely (parser))
8436         {
8437           tree pushed_scope;
8438           bool non_constant_p;
8439           bool flags = LOOKUP_ONLYCONVERTING;
8440
8441           /* Create the declaration.  */
8442           decl = start_decl (declarator, &type_specifiers,
8443                              /*initialized_p=*/true,
8444                              attributes, /*prefix_attributes=*/NULL_TREE,
8445                              &pushed_scope);
8446
8447           /* Parse the initializer.  */
8448           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8449             {
8450               initializer = cp_parser_braced_list (parser, &non_constant_p);
8451               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
8452               flags = 0;
8453             }
8454           else
8455             {
8456               /* Consume the `='.  */
8457               cp_parser_require (parser, CPP_EQ, RT_EQ);
8458               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
8459             }
8460           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
8461             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
8462
8463           /* Process the initializer.  */
8464           cp_finish_decl (decl,
8465                           initializer, !non_constant_p,
8466                           asm_specification,
8467                           flags);
8468
8469           if (pushed_scope)
8470             pop_scope (pushed_scope);
8471
8472           return convert_from_reference (decl);
8473         }
8474     }
8475   /* If we didn't even get past the declarator successfully, we are
8476      definitely not looking at a declaration.  */
8477   else
8478     cp_parser_abort_tentative_parse (parser);
8479
8480   /* Otherwise, we are looking at an expression.  */
8481   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
8482 }
8483
8484 /* Parses a for-statement or range-for-statement until the closing ')',
8485    not included. */
8486
8487 static tree
8488 cp_parser_for (cp_parser *parser)
8489 {
8490   tree init, scope, decl;
8491   bool is_range_for;
8492
8493   /* Begin the for-statement.  */
8494   scope = begin_for_scope (&init);
8495
8496   /* Parse the initialization.  */
8497   is_range_for = cp_parser_for_init_statement (parser, &decl);
8498
8499   if (is_range_for)
8500     return cp_parser_range_for (parser, scope, init, decl);
8501   else
8502     return cp_parser_c_for (parser, scope, init);
8503 }
8504
8505 static tree
8506 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
8507 {
8508   /* Normal for loop */
8509   tree condition = NULL_TREE;
8510   tree expression = NULL_TREE;
8511   tree stmt;
8512
8513   stmt = begin_for_stmt (scope, init);
8514   /* The for-init-statement has already been parsed in
8515      cp_parser_for_init_statement, so no work is needed here.  */
8516   finish_for_init_stmt (stmt);
8517
8518   /* If there's a condition, process it.  */
8519   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8520     condition = cp_parser_condition (parser);
8521   finish_for_cond (condition, stmt);
8522   /* Look for the `;'.  */
8523   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8524
8525   /* If there's an expression, process it.  */
8526   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
8527     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8528   finish_for_expr (expression, stmt);
8529
8530   return stmt;
8531 }
8532
8533 /* Tries to parse a range-based for-statement:
8534
8535   range-based-for:
8536     decl-specifier-seq declarator : expression
8537
8538   The decl-specifier-seq declarator and the `:' are already parsed by
8539   cp_parser_for_init_statement. If processing_template_decl it returns a
8540   newly created RANGE_FOR_STMT; if not, it is converted to a
8541   regular FOR_STMT.  */
8542
8543 static tree
8544 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
8545 {
8546   tree stmt, range_expr;
8547
8548   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
8549     {
8550       bool expr_non_constant_p;
8551       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
8552     }
8553   else
8554     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8555
8556   /* If in template, STMT is converted to a normal for-statement
8557      at instantiation. If not, it is done just ahead. */
8558   if (processing_template_decl)
8559     {
8560       stmt = begin_range_for_stmt (scope, init);
8561       finish_range_for_decl (stmt, range_decl, range_expr);
8562     }
8563   else
8564     {
8565       stmt = begin_for_stmt (scope, init);
8566       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
8567     }
8568   return stmt;
8569 }
8570
8571 /* Converts a range-based for-statement into a normal
8572    for-statement, as per the definition.
8573
8574       for (RANGE_DECL : RANGE_EXPR)
8575         BLOCK
8576
8577    should be equivalent to:
8578
8579       {
8580         auto &&__range = RANGE_EXPR;
8581         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
8582               __begin != __end;
8583               ++__begin)
8584           {
8585               RANGE_DECL = *__begin;
8586               BLOCK
8587           }
8588       }
8589
8590    If RANGE_EXPR is an array:
8591         BEGIN_EXPR = __range
8592         END_EXPR = __range + ARRAY_SIZE(__range)
8593    Else if RANGE_EXPR has a member 'begin' or 'end':
8594         BEGIN_EXPR = __range.begin()
8595         END_EXPR = __range.end()
8596    Else:
8597         BEGIN_EXPR = begin(__range)
8598         END_EXPR = end(__range);
8599
8600    If __range has a member 'begin' but not 'end', or vice versa, we must
8601    still use the second alternative (it will surely fail, however).
8602    When calling begin()/end() in the third alternative we must use
8603    argument dependent lookup, but always considering 'std' as an associated
8604    namespace.  */
8605
8606 tree
8607 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
8608 {
8609   tree range_type, range_temp;
8610   tree begin, end;
8611   tree iter_type, begin_expr, end_expr;
8612   tree condition, expression;
8613
8614   if (range_decl == error_mark_node || range_expr == error_mark_node)
8615     /* If an error happened previously do nothing or else a lot of
8616        unhelpful errors would be issued.  */
8617     begin_expr = end_expr = iter_type = error_mark_node;
8618   else
8619     {
8620       /* Find out the type deduced by the declaration
8621          `auto &&__range = range_expr'.  */
8622       range_type = cp_build_reference_type (make_auto (), true);
8623       range_type = do_auto_deduction (range_type, range_expr,
8624                                       type_uses_auto (range_type));
8625
8626       /* Create the __range variable.  */
8627       range_temp = build_decl (input_location, VAR_DECL,
8628                                get_identifier ("__for_range"), range_type);
8629       TREE_USED (range_temp) = 1;
8630       DECL_ARTIFICIAL (range_temp) = 1;
8631       pushdecl (range_temp);
8632       cp_finish_decl (range_temp, range_expr,
8633                       /*is_constant_init*/false, NULL_TREE,
8634                       LOOKUP_ONLYCONVERTING);
8635
8636       range_temp = convert_from_reference (range_temp);
8637       iter_type = cp_parser_perform_range_for_lookup (range_temp,
8638                                                       &begin_expr, &end_expr);
8639     }
8640
8641   /* The new for initialization statement.  */
8642   begin = build_decl (input_location, VAR_DECL,
8643                       get_identifier ("__for_begin"), iter_type);
8644   TREE_USED (begin) = 1;
8645   DECL_ARTIFICIAL (begin) = 1;
8646   pushdecl (begin);
8647   cp_finish_decl (begin, begin_expr,
8648                   /*is_constant_init*/false, NULL_TREE,
8649                   LOOKUP_ONLYCONVERTING);
8650
8651   end = build_decl (input_location, VAR_DECL,
8652                     get_identifier ("__for_end"), iter_type);
8653   TREE_USED (end) = 1;
8654   DECL_ARTIFICIAL (end) = 1;
8655   pushdecl (end);
8656   cp_finish_decl (end, end_expr,
8657                   /*is_constant_init*/false, NULL_TREE,
8658                   LOOKUP_ONLYCONVERTING);
8659
8660   finish_for_init_stmt (statement);
8661
8662   /* The new for condition.  */
8663   condition = build_x_binary_op (NE_EXPR,
8664                                  begin, ERROR_MARK,
8665                                  end, ERROR_MARK,
8666                                  NULL, tf_warning_or_error);
8667   finish_for_cond (condition, statement);
8668
8669   /* The new increment expression.  */
8670   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
8671   finish_for_expr (expression, statement);
8672
8673   /* The declaration is initialized with *__begin inside the loop body.  */
8674   cp_finish_decl (range_decl,
8675                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
8676                   /*is_constant_init*/false, NULL_TREE,
8677                   LOOKUP_ONLYCONVERTING);
8678
8679   return statement;
8680 }
8681
8682 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
8683    We need to solve both at the same time because the method used
8684    depends on the existence of members begin or end.
8685    Returns the type deduced for the iterator expression.  */
8686
8687 static tree
8688 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
8689 {
8690   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
8691     {
8692       error ("range-based %<for%> expression of type %qT "
8693              "has incomplete type", TREE_TYPE (range));
8694       *begin = *end = error_mark_node;
8695       return error_mark_node;
8696     }
8697   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
8698     {
8699       /* If RANGE is an array, we will use pointer arithmetic.  */
8700       *begin = range;
8701       *end = build_binary_op (input_location, PLUS_EXPR,
8702                               range,
8703                               array_type_nelts_top (TREE_TYPE (range)),
8704                               0);
8705       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
8706     }
8707   else
8708     {
8709       /* If it is not an array, we must do a bit of magic.  */
8710       tree id_begin, id_end;
8711       tree member_begin, member_end;
8712
8713       *begin = *end = error_mark_node;
8714
8715       id_begin = get_identifier ("begin");
8716       id_end = get_identifier ("end");
8717       member_begin = lookup_member (TREE_TYPE (range), id_begin,
8718                                     /*protect=*/2, /*want_type=*/false);
8719       member_end = lookup_member (TREE_TYPE (range), id_end,
8720                                   /*protect=*/2, /*want_type=*/false);
8721
8722       if (member_begin != NULL_TREE || member_end != NULL_TREE)
8723         {
8724           /* Use the member functions.  */
8725           if (member_begin != NULL_TREE)
8726             *begin = cp_parser_range_for_member_function (range, id_begin);
8727           else
8728             error ("range-based %<for%> expression of type %qT has an "
8729                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
8730
8731           if (member_end != NULL_TREE)
8732             *end = cp_parser_range_for_member_function (range, id_end);
8733           else
8734             error ("range-based %<for%> expression of type %qT has a "
8735                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
8736         }
8737       else
8738         {
8739           /* Use global functions with ADL.  */
8740           VEC(tree,gc) *vec;
8741           vec = make_tree_vector ();
8742
8743           VEC_safe_push (tree, gc, vec, range);
8744
8745           member_begin = perform_koenig_lookup (id_begin, vec,
8746                                                 /*include_std=*/true,
8747                                                 tf_warning_or_error);
8748           *begin = finish_call_expr (member_begin, &vec, false, true,
8749                                      tf_warning_or_error);
8750           member_end = perform_koenig_lookup (id_end, vec,
8751                                               /*include_std=*/true,
8752                                               tf_warning_or_error);
8753           *end = finish_call_expr (member_end, &vec, false, true,
8754                                    tf_warning_or_error);
8755
8756           release_tree_vector (vec);
8757         }
8758
8759       /* Last common checks.  */
8760       if (*begin == error_mark_node || *end == error_mark_node)
8761         {
8762           /* If one of the expressions is an error do no more checks.  */
8763           *begin = *end = error_mark_node;
8764           return error_mark_node;
8765         }
8766       else
8767         {
8768           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
8769           /* The unqualified type of the __begin and __end temporaries should
8770              be the same, as required by the multiple auto declaration.  */
8771           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
8772             error ("inconsistent begin/end types in range-based %<for%> "
8773                    "statement: %qT and %qT",
8774                    TREE_TYPE (*begin), TREE_TYPE (*end));
8775           return iter_type;
8776         }
8777     }
8778 }
8779
8780 /* Helper function for cp_parser_perform_range_for_lookup.
8781    Builds a tree for RANGE.IDENTIFIER().  */
8782
8783 static tree
8784 cp_parser_range_for_member_function (tree range, tree identifier)
8785 {
8786   tree member, res;
8787   VEC(tree,gc) *vec;
8788
8789   member = finish_class_member_access_expr (range, identifier,
8790                                             false, tf_warning_or_error);
8791   if (member == error_mark_node)
8792     return error_mark_node;
8793
8794   vec = make_tree_vector ();
8795   res = finish_call_expr (member, &vec,
8796                           /*disallow_virtual=*/false,
8797                           /*koenig_p=*/false,
8798                           tf_warning_or_error);
8799   release_tree_vector (vec);
8800   return res;
8801 }
8802
8803 /* Parse an iteration-statement.
8804
8805    iteration-statement:
8806      while ( condition ) statement
8807      do statement while ( expression ) ;
8808      for ( for-init-statement condition [opt] ; expression [opt] )
8809        statement
8810
8811    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
8812
8813 static tree
8814 cp_parser_iteration_statement (cp_parser* parser)
8815 {
8816   cp_token *token;
8817   enum rid keyword;
8818   tree statement;
8819   unsigned char in_statement;
8820
8821   /* Peek at the next token.  */
8822   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
8823   if (!token)
8824     return error_mark_node;
8825
8826   /* Remember whether or not we are already within an iteration
8827      statement.  */
8828   in_statement = parser->in_statement;
8829
8830   /* See what kind of keyword it is.  */
8831   keyword = token->keyword;
8832   switch (keyword)
8833     {
8834     case RID_WHILE:
8835       {
8836         tree condition;
8837
8838         /* Begin the while-statement.  */
8839         statement = begin_while_stmt ();
8840         /* Look for the `('.  */
8841         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8842         /* Parse the condition.  */
8843         condition = cp_parser_condition (parser);
8844         finish_while_stmt_cond (condition, statement);
8845         /* Look for the `)'.  */
8846         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8847         /* Parse the dependent statement.  */
8848         parser->in_statement = IN_ITERATION_STMT;
8849         cp_parser_already_scoped_statement (parser);
8850         parser->in_statement = in_statement;
8851         /* We're done with the while-statement.  */
8852         finish_while_stmt (statement);
8853       }
8854       break;
8855
8856     case RID_DO:
8857       {
8858         tree expression;
8859
8860         /* Begin the do-statement.  */
8861         statement = begin_do_stmt ();
8862         /* Parse the body of the do-statement.  */
8863         parser->in_statement = IN_ITERATION_STMT;
8864         cp_parser_implicitly_scoped_statement (parser, NULL);
8865         parser->in_statement = in_statement;
8866         finish_do_body (statement);
8867         /* Look for the `while' keyword.  */
8868         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
8869         /* Look for the `('.  */
8870         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8871         /* Parse the expression.  */
8872         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8873         /* We're done with the do-statement.  */
8874         finish_do_stmt (expression, statement);
8875         /* Look for the `)'.  */
8876         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8877         /* Look for the `;'.  */
8878         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8879       }
8880       break;
8881
8882     case RID_FOR:
8883       {
8884         /* Look for the `('.  */
8885         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
8886
8887         statement = cp_parser_for (parser);
8888
8889         /* Look for the `)'.  */
8890         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8891
8892         /* Parse the body of the for-statement.  */
8893         parser->in_statement = IN_ITERATION_STMT;
8894         cp_parser_already_scoped_statement (parser);
8895         parser->in_statement = in_statement;
8896
8897         /* We're done with the for-statement.  */
8898         finish_for_stmt (statement);
8899       }
8900       break;
8901
8902     default:
8903       cp_parser_error (parser, "expected iteration-statement");
8904       statement = error_mark_node;
8905       break;
8906     }
8907
8908   return statement;
8909 }
8910
8911 /* Parse a for-init-statement or the declarator of a range-based-for.
8912    Returns true if a range-based-for declaration is seen.
8913
8914    for-init-statement:
8915      expression-statement
8916      simple-declaration  */
8917
8918 static bool
8919 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
8920 {
8921   /* If the next token is a `;', then we have an empty
8922      expression-statement.  Grammatically, this is also a
8923      simple-declaration, but an invalid one, because it does not
8924      declare anything.  Therefore, if we did not handle this case
8925      specially, we would issue an error message about an invalid
8926      declaration.  */
8927   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8928     {
8929       bool is_range_for = false;
8930       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8931
8932       parser->colon_corrects_to_scope_p = false;
8933
8934       /* We're going to speculatively look for a declaration, falling back
8935          to an expression, if necessary.  */
8936       cp_parser_parse_tentatively (parser);
8937       /* Parse the declaration.  */
8938       cp_parser_simple_declaration (parser,
8939                                     /*function_definition_allowed_p=*/false,
8940                                     decl);
8941       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8942       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
8943         {
8944           /* It is a range-for, consume the ':' */
8945           cp_lexer_consume_token (parser->lexer);
8946           is_range_for = true;
8947           if (cxx_dialect < cxx0x)
8948             {
8949               error_at (cp_lexer_peek_token (parser->lexer)->location,
8950                         "range-based %<for%> loops are not allowed "
8951                         "in C++98 mode");
8952               *decl = error_mark_node;
8953             }
8954         }
8955       else
8956           /* The ';' is not consumed yet because we told
8957              cp_parser_simple_declaration not to.  */
8958           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8959
8960       if (cp_parser_parse_definitely (parser))
8961         return is_range_for;
8962       /* If the tentative parse failed, then we shall need to look for an
8963          expression-statement.  */
8964     }
8965   /* If we are here, it is an expression-statement.  */
8966   cp_parser_expression_statement (parser, NULL_TREE);
8967   return false;
8968 }
8969
8970 /* Parse a jump-statement.
8971
8972    jump-statement:
8973      break ;
8974      continue ;
8975      return expression [opt] ;
8976      return braced-init-list ;
8977      goto identifier ;
8978
8979    GNU extension:
8980
8981    jump-statement:
8982      goto * expression ;
8983
8984    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
8985
8986 static tree
8987 cp_parser_jump_statement (cp_parser* parser)
8988 {
8989   tree statement = error_mark_node;
8990   cp_token *token;
8991   enum rid keyword;
8992   unsigned char in_statement;
8993
8994   /* Peek at the next token.  */
8995   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
8996   if (!token)
8997     return error_mark_node;
8998
8999   /* See what kind of keyword it is.  */
9000   keyword = token->keyword;
9001   switch (keyword)
9002     {
9003     case RID_BREAK:
9004       in_statement = parser->in_statement & ~IN_IF_STMT;      
9005       switch (in_statement)
9006         {
9007         case 0:
9008           error_at (token->location, "break statement not within loop or switch");
9009           break;
9010         default:
9011           gcc_assert ((in_statement & IN_SWITCH_STMT)
9012                       || in_statement == IN_ITERATION_STMT);
9013           statement = finish_break_stmt ();
9014           break;
9015         case IN_OMP_BLOCK:
9016           error_at (token->location, "invalid exit from OpenMP structured block");
9017           break;
9018         case IN_OMP_FOR:
9019           error_at (token->location, "break statement used with OpenMP for loop");
9020           break;
9021         }
9022       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9023       break;
9024
9025     case RID_CONTINUE:
9026       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9027         {
9028         case 0:
9029           error_at (token->location, "continue statement not within a loop");
9030           break;
9031         case IN_ITERATION_STMT:
9032         case IN_OMP_FOR:
9033           statement = finish_continue_stmt ();
9034           break;
9035         case IN_OMP_BLOCK:
9036           error_at (token->location, "invalid exit from OpenMP structured block");
9037           break;
9038         default:
9039           gcc_unreachable ();
9040         }
9041       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9042       break;
9043
9044     case RID_RETURN:
9045       {
9046         tree expr;
9047         bool expr_non_constant_p;
9048
9049         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9050           {
9051             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9052             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9053           }
9054         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9055           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9056         else
9057           /* If the next token is a `;', then there is no
9058              expression.  */
9059           expr = NULL_TREE;
9060         /* Build the return-statement.  */
9061         statement = finish_return_stmt (expr);
9062         /* Look for the final `;'.  */
9063         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9064       }
9065       break;
9066
9067     case RID_GOTO:
9068       /* Create the goto-statement.  */
9069       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9070         {
9071           /* Issue a warning about this use of a GNU extension.  */
9072           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9073           /* Consume the '*' token.  */
9074           cp_lexer_consume_token (parser->lexer);
9075           /* Parse the dependent expression.  */
9076           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9077         }
9078       else
9079         finish_goto_stmt (cp_parser_identifier (parser));
9080       /* Look for the final `;'.  */
9081       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9082       break;
9083
9084     default:
9085       cp_parser_error (parser, "expected jump-statement");
9086       break;
9087     }
9088
9089   return statement;
9090 }
9091
9092 /* Parse a declaration-statement.
9093
9094    declaration-statement:
9095      block-declaration  */
9096
9097 static void
9098 cp_parser_declaration_statement (cp_parser* parser)
9099 {
9100   void *p;
9101
9102   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9103   p = obstack_alloc (&declarator_obstack, 0);
9104
9105  /* Parse the block-declaration.  */
9106   cp_parser_block_declaration (parser, /*statement_p=*/true);
9107
9108   /* Free any declarators allocated.  */
9109   obstack_free (&declarator_obstack, p);
9110
9111   /* Finish off the statement.  */
9112   finish_stmt ();
9113 }
9114
9115 /* Some dependent statements (like `if (cond) statement'), are
9116    implicitly in their own scope.  In other words, if the statement is
9117    a single statement (as opposed to a compound-statement), it is
9118    none-the-less treated as if it were enclosed in braces.  Any
9119    declarations appearing in the dependent statement are out of scope
9120    after control passes that point.  This function parses a statement,
9121    but ensures that is in its own scope, even if it is not a
9122    compound-statement.
9123
9124    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9125    is a (possibly labeled) if statement which is not enclosed in
9126    braces and has an else clause.  This is used to implement
9127    -Wparentheses.
9128
9129    Returns the new statement.  */
9130
9131 static tree
9132 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9133 {
9134   tree statement;
9135
9136   if (if_p != NULL)
9137     *if_p = false;
9138
9139   /* Mark if () ; with a special NOP_EXPR.  */
9140   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9141     {
9142       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9143       cp_lexer_consume_token (parser->lexer);
9144       statement = add_stmt (build_empty_stmt (loc));
9145     }
9146   /* if a compound is opened, we simply parse the statement directly.  */
9147   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9148     statement = cp_parser_compound_statement (parser, NULL, false, false);
9149   /* If the token is not a `{', then we must take special action.  */
9150   else
9151     {
9152       /* Create a compound-statement.  */
9153       statement = begin_compound_stmt (0);
9154       /* Parse the dependent-statement.  */
9155       cp_parser_statement (parser, NULL_TREE, false, if_p);
9156       /* Finish the dummy compound-statement.  */
9157       finish_compound_stmt (statement);
9158     }
9159
9160   /* Return the statement.  */
9161   return statement;
9162 }
9163
9164 /* For some dependent statements (like `while (cond) statement'), we
9165    have already created a scope.  Therefore, even if the dependent
9166    statement is a compound-statement, we do not want to create another
9167    scope.  */
9168
9169 static void
9170 cp_parser_already_scoped_statement (cp_parser* parser)
9171 {
9172   /* If the token is a `{', then we must take special action.  */
9173   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9174     cp_parser_statement (parser, NULL_TREE, false, NULL);
9175   else
9176     {
9177       /* Avoid calling cp_parser_compound_statement, so that we
9178          don't create a new scope.  Do everything else by hand.  */
9179       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9180       /* If the next keyword is `__label__' we have a label declaration.  */
9181       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9182         cp_parser_label_declaration (parser);
9183       /* Parse an (optional) statement-seq.  */
9184       cp_parser_statement_seq_opt (parser, NULL_TREE);
9185       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9186     }
9187 }
9188
9189 /* Declarations [gram.dcl.dcl] */
9190
9191 /* Parse an optional declaration-sequence.
9192
9193    declaration-seq:
9194      declaration
9195      declaration-seq declaration  */
9196
9197 static void
9198 cp_parser_declaration_seq_opt (cp_parser* parser)
9199 {
9200   while (true)
9201     {
9202       cp_token *token;
9203
9204       token = cp_lexer_peek_token (parser->lexer);
9205
9206       if (token->type == CPP_CLOSE_BRACE
9207           || token->type == CPP_EOF
9208           || token->type == CPP_PRAGMA_EOL)
9209         break;
9210
9211       if (token->type == CPP_SEMICOLON)
9212         {
9213           /* A declaration consisting of a single semicolon is
9214              invalid.  Allow it unless we're being pedantic.  */
9215           cp_lexer_consume_token (parser->lexer);
9216           if (!in_system_header)
9217             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9218           continue;
9219         }
9220
9221       /* If we're entering or exiting a region that's implicitly
9222          extern "C", modify the lang context appropriately.  */
9223       if (!parser->implicit_extern_c && token->implicit_extern_c)
9224         {
9225           push_lang_context (lang_name_c);
9226           parser->implicit_extern_c = true;
9227         }
9228       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9229         {
9230           pop_lang_context ();
9231           parser->implicit_extern_c = false;
9232         }
9233
9234       if (token->type == CPP_PRAGMA)
9235         {
9236           /* A top-level declaration can consist solely of a #pragma.
9237              A nested declaration cannot, so this is done here and not
9238              in cp_parser_declaration.  (A #pragma at block scope is
9239              handled in cp_parser_statement.)  */
9240           cp_parser_pragma (parser, pragma_external);
9241           continue;
9242         }
9243
9244       /* Parse the declaration itself.  */
9245       cp_parser_declaration (parser);
9246     }
9247 }
9248
9249 /* Parse a declaration.
9250
9251    declaration:
9252      block-declaration
9253      function-definition
9254      template-declaration
9255      explicit-instantiation
9256      explicit-specialization
9257      linkage-specification
9258      namespace-definition
9259
9260    GNU extension:
9261
9262    declaration:
9263       __extension__ declaration */
9264
9265 static void
9266 cp_parser_declaration (cp_parser* parser)
9267 {
9268   cp_token token1;
9269   cp_token token2;
9270   int saved_pedantic;
9271   void *p;
9272   tree attributes = NULL_TREE;
9273
9274   /* Check for the `__extension__' keyword.  */
9275   if (cp_parser_extension_opt (parser, &saved_pedantic))
9276     {
9277       /* Parse the qualified declaration.  */
9278       cp_parser_declaration (parser);
9279       /* Restore the PEDANTIC flag.  */
9280       pedantic = saved_pedantic;
9281
9282       return;
9283     }
9284
9285   /* Try to figure out what kind of declaration is present.  */
9286   token1 = *cp_lexer_peek_token (parser->lexer);
9287
9288   if (token1.type != CPP_EOF)
9289     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
9290   else
9291     {
9292       token2.type = CPP_EOF;
9293       token2.keyword = RID_MAX;
9294     }
9295
9296   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9297   p = obstack_alloc (&declarator_obstack, 0);
9298
9299   /* If the next token is `extern' and the following token is a string
9300      literal, then we have a linkage specification.  */
9301   if (token1.keyword == RID_EXTERN
9302       && cp_parser_is_string_literal (&token2))
9303     cp_parser_linkage_specification (parser);
9304   /* If the next token is `template', then we have either a template
9305      declaration, an explicit instantiation, or an explicit
9306      specialization.  */
9307   else if (token1.keyword == RID_TEMPLATE)
9308     {
9309       /* `template <>' indicates a template specialization.  */
9310       if (token2.type == CPP_LESS
9311           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
9312         cp_parser_explicit_specialization (parser);
9313       /* `template <' indicates a template declaration.  */
9314       else if (token2.type == CPP_LESS)
9315         cp_parser_template_declaration (parser, /*member_p=*/false);
9316       /* Anything else must be an explicit instantiation.  */
9317       else
9318         cp_parser_explicit_instantiation (parser);
9319     }
9320   /* If the next token is `export', then we have a template
9321      declaration.  */
9322   else if (token1.keyword == RID_EXPORT)
9323     cp_parser_template_declaration (parser, /*member_p=*/false);
9324   /* If the next token is `extern', 'static' or 'inline' and the one
9325      after that is `template', we have a GNU extended explicit
9326      instantiation directive.  */
9327   else if (cp_parser_allow_gnu_extensions_p (parser)
9328            && (token1.keyword == RID_EXTERN
9329                || token1.keyword == RID_STATIC
9330                || token1.keyword == RID_INLINE)
9331            && token2.keyword == RID_TEMPLATE)
9332     cp_parser_explicit_instantiation (parser);
9333   /* If the next token is `namespace', check for a named or unnamed
9334      namespace definition.  */
9335   else if (token1.keyword == RID_NAMESPACE
9336            && (/* A named namespace definition.  */
9337                (token2.type == CPP_NAME
9338                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
9339                     != CPP_EQ))
9340                /* An unnamed namespace definition.  */
9341                || token2.type == CPP_OPEN_BRACE
9342                || token2.keyword == RID_ATTRIBUTE))
9343     cp_parser_namespace_definition (parser);
9344   /* An inline (associated) namespace definition.  */
9345   else if (token1.keyword == RID_INLINE
9346            && token2.keyword == RID_NAMESPACE)
9347     cp_parser_namespace_definition (parser);
9348   /* Objective-C++ declaration/definition.  */
9349   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
9350     cp_parser_objc_declaration (parser, NULL_TREE);
9351   else if (c_dialect_objc ()
9352            && token1.keyword == RID_ATTRIBUTE
9353            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
9354     cp_parser_objc_declaration (parser, attributes);
9355   /* We must have either a block declaration or a function
9356      definition.  */
9357   else
9358     /* Try to parse a block-declaration, or a function-definition.  */
9359     cp_parser_block_declaration (parser, /*statement_p=*/false);
9360
9361   /* Free any declarators allocated.  */
9362   obstack_free (&declarator_obstack, p);
9363 }
9364
9365 /* Parse a block-declaration.
9366
9367    block-declaration:
9368      simple-declaration
9369      asm-definition
9370      namespace-alias-definition
9371      using-declaration
9372      using-directive
9373
9374    GNU Extension:
9375
9376    block-declaration:
9377      __extension__ block-declaration
9378
9379    C++0x Extension:
9380
9381    block-declaration:
9382      static_assert-declaration
9383
9384    If STATEMENT_P is TRUE, then this block-declaration is occurring as
9385    part of a declaration-statement.  */
9386
9387 static void
9388 cp_parser_block_declaration (cp_parser *parser,
9389                              bool      statement_p)
9390 {
9391   cp_token *token1;
9392   int saved_pedantic;
9393
9394   /* Check for the `__extension__' keyword.  */
9395   if (cp_parser_extension_opt (parser, &saved_pedantic))
9396     {
9397       /* Parse the qualified declaration.  */
9398       cp_parser_block_declaration (parser, statement_p);
9399       /* Restore the PEDANTIC flag.  */
9400       pedantic = saved_pedantic;
9401
9402       return;
9403     }
9404
9405   /* Peek at the next token to figure out which kind of declaration is
9406      present.  */
9407   token1 = cp_lexer_peek_token (parser->lexer);
9408
9409   /* If the next keyword is `asm', we have an asm-definition.  */
9410   if (token1->keyword == RID_ASM)
9411     {
9412       if (statement_p)
9413         cp_parser_commit_to_tentative_parse (parser);
9414       cp_parser_asm_definition (parser);
9415     }
9416   /* If the next keyword is `namespace', we have a
9417      namespace-alias-definition.  */
9418   else if (token1->keyword == RID_NAMESPACE)
9419     cp_parser_namespace_alias_definition (parser);
9420   /* If the next keyword is `using', we have either a
9421      using-declaration or a using-directive.  */
9422   else if (token1->keyword == RID_USING)
9423     {
9424       cp_token *token2;
9425
9426       if (statement_p)
9427         cp_parser_commit_to_tentative_parse (parser);
9428       /* If the token after `using' is `namespace', then we have a
9429          using-directive.  */
9430       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
9431       if (token2->keyword == RID_NAMESPACE)
9432         cp_parser_using_directive (parser);
9433       /* Otherwise, it's a using-declaration.  */
9434       else
9435         cp_parser_using_declaration (parser,
9436                                      /*access_declaration_p=*/false);
9437     }
9438   /* If the next keyword is `__label__' we have a misplaced label
9439      declaration.  */
9440   else if (token1->keyword == RID_LABEL)
9441     {
9442       cp_lexer_consume_token (parser->lexer);
9443       error_at (token1->location, "%<__label__%> not at the beginning of a block");
9444       cp_parser_skip_to_end_of_statement (parser);
9445       /* If the next token is now a `;', consume it.  */
9446       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9447         cp_lexer_consume_token (parser->lexer);
9448     }
9449   /* If the next token is `static_assert' we have a static assertion.  */
9450   else if (token1->keyword == RID_STATIC_ASSERT)
9451     cp_parser_static_assert (parser, /*member_p=*/false);
9452   /* Anything else must be a simple-declaration.  */
9453   else
9454     cp_parser_simple_declaration (parser, !statement_p,
9455                                   /*maybe_range_for_decl*/NULL);
9456 }
9457
9458 /* Parse a simple-declaration.
9459
9460    simple-declaration:
9461      decl-specifier-seq [opt] init-declarator-list [opt] ;
9462
9463    init-declarator-list:
9464      init-declarator
9465      init-declarator-list , init-declarator
9466
9467    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
9468    function-definition as a simple-declaration.
9469
9470    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
9471    parsed declaration if it is an uninitialized single declarator not followed
9472    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
9473    if present, will not be consumed.  */
9474
9475 static void
9476 cp_parser_simple_declaration (cp_parser* parser,
9477                               bool function_definition_allowed_p,
9478                               tree *maybe_range_for_decl)
9479 {
9480   cp_decl_specifier_seq decl_specifiers;
9481   int declares_class_or_enum;
9482   bool saw_declarator;
9483
9484   if (maybe_range_for_decl)
9485     *maybe_range_for_decl = NULL_TREE;
9486
9487   /* Defer access checks until we know what is being declared; the
9488      checks for names appearing in the decl-specifier-seq should be
9489      done as if we were in the scope of the thing being declared.  */
9490   push_deferring_access_checks (dk_deferred);
9491
9492   /* Parse the decl-specifier-seq.  We have to keep track of whether
9493      or not the decl-specifier-seq declares a named class or
9494      enumeration type, since that is the only case in which the
9495      init-declarator-list is allowed to be empty.
9496
9497      [dcl.dcl]
9498
9499      In a simple-declaration, the optional init-declarator-list can be
9500      omitted only when declaring a class or enumeration, that is when
9501      the decl-specifier-seq contains either a class-specifier, an
9502      elaborated-type-specifier, or an enum-specifier.  */
9503   cp_parser_decl_specifier_seq (parser,
9504                                 CP_PARSER_FLAGS_OPTIONAL,
9505                                 &decl_specifiers,
9506                                 &declares_class_or_enum);
9507   /* We no longer need to defer access checks.  */
9508   stop_deferring_access_checks ();
9509
9510   /* In a block scope, a valid declaration must always have a
9511      decl-specifier-seq.  By not trying to parse declarators, we can
9512      resolve the declaration/expression ambiguity more quickly.  */
9513   if (!function_definition_allowed_p
9514       && !decl_specifiers.any_specifiers_p)
9515     {
9516       cp_parser_error (parser, "expected declaration");
9517       goto done;
9518     }
9519
9520   /* If the next two tokens are both identifiers, the code is
9521      erroneous. The usual cause of this situation is code like:
9522
9523        T t;
9524
9525      where "T" should name a type -- but does not.  */
9526   if (!decl_specifiers.any_type_specifiers_p
9527       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
9528     {
9529       /* If parsing tentatively, we should commit; we really are
9530          looking at a declaration.  */
9531       cp_parser_commit_to_tentative_parse (parser);
9532       /* Give up.  */
9533       goto done;
9534     }
9535
9536   /* If we have seen at least one decl-specifier, and the next token
9537      is not a parenthesis, then we must be looking at a declaration.
9538      (After "int (" we might be looking at a functional cast.)  */
9539   if (decl_specifiers.any_specifiers_p
9540       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
9541       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
9542       && !cp_parser_error_occurred (parser))
9543     cp_parser_commit_to_tentative_parse (parser);
9544
9545   /* Keep going until we hit the `;' at the end of the simple
9546      declaration.  */
9547   saw_declarator = false;
9548   while (cp_lexer_next_token_is_not (parser->lexer,
9549                                      CPP_SEMICOLON))
9550     {
9551       cp_token *token;
9552       bool function_definition_p;
9553       tree decl;
9554
9555       if (saw_declarator)
9556         {
9557           /* If we are processing next declarator, coma is expected */
9558           token = cp_lexer_peek_token (parser->lexer);
9559           gcc_assert (token->type == CPP_COMMA);
9560           cp_lexer_consume_token (parser->lexer);
9561           if (maybe_range_for_decl)
9562             *maybe_range_for_decl = error_mark_node;
9563         }
9564       else
9565         saw_declarator = true;
9566
9567       /* Parse the init-declarator.  */
9568       decl = cp_parser_init_declarator (parser, &decl_specifiers,
9569                                         /*checks=*/NULL,
9570                                         function_definition_allowed_p,
9571                                         /*member_p=*/false,
9572                                         declares_class_or_enum,
9573                                         &function_definition_p,
9574                                         maybe_range_for_decl);
9575       /* If an error occurred while parsing tentatively, exit quickly.
9576          (That usually happens when in the body of a function; each
9577          statement is treated as a declaration-statement until proven
9578          otherwise.)  */
9579       if (cp_parser_error_occurred (parser))
9580         goto done;
9581       /* Handle function definitions specially.  */
9582       if (function_definition_p)
9583         {
9584           /* If the next token is a `,', then we are probably
9585              processing something like:
9586
9587                void f() {}, *p;
9588
9589              which is erroneous.  */
9590           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
9591             {
9592               cp_token *token = cp_lexer_peek_token (parser->lexer);
9593               error_at (token->location,
9594                         "mixing"
9595                         " declarations and function-definitions is forbidden");
9596             }
9597           /* Otherwise, we're done with the list of declarators.  */
9598           else
9599             {
9600               pop_deferring_access_checks ();
9601               return;
9602             }
9603         }
9604       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
9605         *maybe_range_for_decl = decl;
9606       /* The next token should be either a `,' or a `;'.  */
9607       token = cp_lexer_peek_token (parser->lexer);
9608       /* If it's a `,', there are more declarators to come.  */
9609       if (token->type == CPP_COMMA)
9610         /* will be consumed next time around */;
9611       /* If it's a `;', we are done.  */
9612       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
9613         break;
9614       /* Anything else is an error.  */
9615       else
9616         {
9617           /* If we have already issued an error message we don't need
9618              to issue another one.  */
9619           if (decl != error_mark_node
9620               || cp_parser_uncommitted_to_tentative_parse_p (parser))
9621             cp_parser_error (parser, "expected %<,%> or %<;%>");
9622           /* Skip tokens until we reach the end of the statement.  */
9623           cp_parser_skip_to_end_of_statement (parser);
9624           /* If the next token is now a `;', consume it.  */
9625           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9626             cp_lexer_consume_token (parser->lexer);
9627           goto done;
9628         }
9629       /* After the first time around, a function-definition is not
9630          allowed -- even if it was OK at first.  For example:
9631
9632            int i, f() {}
9633
9634          is not valid.  */
9635       function_definition_allowed_p = false;
9636     }
9637
9638   /* Issue an error message if no declarators are present, and the
9639      decl-specifier-seq does not itself declare a class or
9640      enumeration.  */
9641   if (!saw_declarator)
9642     {
9643       if (cp_parser_declares_only_class_p (parser))
9644         shadow_tag (&decl_specifiers);
9645       /* Perform any deferred access checks.  */
9646       perform_deferred_access_checks ();
9647     }
9648
9649   /* Consume the `;'.  */
9650   if (!maybe_range_for_decl)
9651       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9652
9653  done:
9654   pop_deferring_access_checks ();
9655 }
9656
9657 /* Parse a decl-specifier-seq.
9658
9659    decl-specifier-seq:
9660      decl-specifier-seq [opt] decl-specifier
9661
9662    decl-specifier:
9663      storage-class-specifier
9664      type-specifier
9665      function-specifier
9666      friend
9667      typedef
9668
9669    GNU Extension:
9670
9671    decl-specifier:
9672      attributes
9673
9674    Set *DECL_SPECS to a representation of the decl-specifier-seq.
9675
9676    The parser flags FLAGS is used to control type-specifier parsing.
9677
9678    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
9679    flags:
9680
9681      1: one of the decl-specifiers is an elaborated-type-specifier
9682         (i.e., a type declaration)
9683      2: one of the decl-specifiers is an enum-specifier or a
9684         class-specifier (i.e., a type definition)
9685
9686    */
9687
9688 static void
9689 cp_parser_decl_specifier_seq (cp_parser* parser,
9690                               cp_parser_flags flags,
9691                               cp_decl_specifier_seq *decl_specs,
9692                               int* declares_class_or_enum)
9693 {
9694   bool constructor_possible_p = !parser->in_declarator_p;
9695   cp_token *start_token = NULL;
9696
9697   /* Clear DECL_SPECS.  */
9698   clear_decl_specs (decl_specs);
9699
9700   /* Assume no class or enumeration type is declared.  */
9701   *declares_class_or_enum = 0;
9702
9703   /* Keep reading specifiers until there are no more to read.  */
9704   while (true)
9705     {
9706       bool constructor_p;
9707       bool found_decl_spec;
9708       cp_token *token;
9709
9710       /* Peek at the next token.  */
9711       token = cp_lexer_peek_token (parser->lexer);
9712
9713       /* Save the first token of the decl spec list for error
9714          reporting.  */
9715       if (!start_token)
9716         start_token = token;
9717       /* Handle attributes.  */
9718       if (token->keyword == RID_ATTRIBUTE)
9719         {
9720           /* Parse the attributes.  */
9721           decl_specs->attributes
9722             = chainon (decl_specs->attributes,
9723                        cp_parser_attributes_opt (parser));
9724           continue;
9725         }
9726       /* Assume we will find a decl-specifier keyword.  */
9727       found_decl_spec = true;
9728       /* If the next token is an appropriate keyword, we can simply
9729          add it to the list.  */
9730       switch (token->keyword)
9731         {
9732           /* decl-specifier:
9733                friend
9734                constexpr */
9735         case RID_FRIEND:
9736           if (!at_class_scope_p ())
9737             {
9738               error_at (token->location, "%<friend%> used outside of class");
9739               cp_lexer_purge_token (parser->lexer);
9740             }
9741           else
9742             {
9743               ++decl_specs->specs[(int) ds_friend];
9744               /* Consume the token.  */
9745               cp_lexer_consume_token (parser->lexer);
9746             }
9747           break;
9748
9749         case RID_CONSTEXPR:
9750           ++decl_specs->specs[(int) ds_constexpr];
9751           cp_lexer_consume_token (parser->lexer);
9752           break;
9753
9754           /* function-specifier:
9755                inline
9756                virtual
9757                explicit  */
9758         case RID_INLINE:
9759         case RID_VIRTUAL:
9760         case RID_EXPLICIT:
9761           cp_parser_function_specifier_opt (parser, decl_specs);
9762           break;
9763
9764           /* decl-specifier:
9765                typedef  */
9766         case RID_TYPEDEF:
9767           ++decl_specs->specs[(int) ds_typedef];
9768           /* Consume the token.  */
9769           cp_lexer_consume_token (parser->lexer);
9770           /* A constructor declarator cannot appear in a typedef.  */
9771           constructor_possible_p = false;
9772           /* The "typedef" keyword can only occur in a declaration; we
9773              may as well commit at this point.  */
9774           cp_parser_commit_to_tentative_parse (parser);
9775
9776           if (decl_specs->storage_class != sc_none)
9777             decl_specs->conflicting_specifiers_p = true;
9778           break;
9779
9780           /* storage-class-specifier:
9781                auto
9782                register
9783                static
9784                extern
9785                mutable
9786
9787              GNU Extension:
9788                thread  */
9789         case RID_AUTO:
9790           if (cxx_dialect == cxx98) 
9791             {
9792               /* Consume the token.  */
9793               cp_lexer_consume_token (parser->lexer);
9794
9795               /* Complain about `auto' as a storage specifier, if
9796                  we're complaining about C++0x compatibility.  */
9797               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
9798                           " will change meaning in C++0x; please remove it");
9799
9800               /* Set the storage class anyway.  */
9801               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
9802                                            token->location);
9803             }
9804           else
9805             /* C++0x auto type-specifier.  */
9806             found_decl_spec = false;
9807           break;
9808
9809         case RID_REGISTER:
9810         case RID_STATIC:
9811         case RID_EXTERN:
9812         case RID_MUTABLE:
9813           /* Consume the token.  */
9814           cp_lexer_consume_token (parser->lexer);
9815           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
9816                                        token->location);
9817           break;
9818         case RID_THREAD:
9819           /* Consume the token.  */
9820           cp_lexer_consume_token (parser->lexer);
9821           ++decl_specs->specs[(int) ds_thread];
9822           break;
9823
9824         default:
9825           /* We did not yet find a decl-specifier yet.  */
9826           found_decl_spec = false;
9827           break;
9828         }
9829
9830       if (found_decl_spec
9831           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
9832           && token->keyword != RID_CONSTEXPR)
9833         error ("decl-specifier invalid in condition");
9834
9835       /* Constructors are a special case.  The `S' in `S()' is not a
9836          decl-specifier; it is the beginning of the declarator.  */
9837       constructor_p
9838         = (!found_decl_spec
9839            && constructor_possible_p
9840            && (cp_parser_constructor_declarator_p
9841                (parser, decl_specs->specs[(int) ds_friend] != 0)));
9842
9843       /* If we don't have a DECL_SPEC yet, then we must be looking at
9844          a type-specifier.  */
9845       if (!found_decl_spec && !constructor_p)
9846         {
9847           int decl_spec_declares_class_or_enum;
9848           bool is_cv_qualifier;
9849           tree type_spec;
9850
9851           type_spec
9852             = cp_parser_type_specifier (parser, flags,
9853                                         decl_specs,
9854                                         /*is_declaration=*/true,
9855                                         &decl_spec_declares_class_or_enum,
9856                                         &is_cv_qualifier);
9857           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
9858
9859           /* If this type-specifier referenced a user-defined type
9860              (a typedef, class-name, etc.), then we can't allow any
9861              more such type-specifiers henceforth.
9862
9863              [dcl.spec]
9864
9865              The longest sequence of decl-specifiers that could
9866              possibly be a type name is taken as the
9867              decl-specifier-seq of a declaration.  The sequence shall
9868              be self-consistent as described below.
9869
9870              [dcl.type]
9871
9872              As a general rule, at most one type-specifier is allowed
9873              in the complete decl-specifier-seq of a declaration.  The
9874              only exceptions are the following:
9875
9876              -- const or volatile can be combined with any other
9877                 type-specifier.
9878
9879              -- signed or unsigned can be combined with char, long,
9880                 short, or int.
9881
9882              -- ..
9883
9884              Example:
9885
9886                typedef char* Pc;
9887                void g (const int Pc);
9888
9889              Here, Pc is *not* part of the decl-specifier seq; it's
9890              the declarator.  Therefore, once we see a type-specifier
9891              (other than a cv-qualifier), we forbid any additional
9892              user-defined types.  We *do* still allow things like `int
9893              int' to be considered a decl-specifier-seq, and issue the
9894              error message later.  */
9895           if (type_spec && !is_cv_qualifier)
9896             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
9897           /* A constructor declarator cannot follow a type-specifier.  */
9898           if (type_spec)
9899             {
9900               constructor_possible_p = false;
9901               found_decl_spec = true;
9902               if (!is_cv_qualifier)
9903                 decl_specs->any_type_specifiers_p = true;
9904             }
9905         }
9906
9907       /* If we still do not have a DECL_SPEC, then there are no more
9908          decl-specifiers.  */
9909       if (!found_decl_spec)
9910         break;
9911
9912       decl_specs->any_specifiers_p = true;
9913       /* After we see one decl-specifier, further decl-specifiers are
9914          always optional.  */
9915       flags |= CP_PARSER_FLAGS_OPTIONAL;
9916     }
9917
9918   cp_parser_check_decl_spec (decl_specs, start_token->location);
9919
9920   /* Don't allow a friend specifier with a class definition.  */
9921   if (decl_specs->specs[(int) ds_friend] != 0
9922       && (*declares_class_or_enum & 2))
9923     error_at (start_token->location,
9924               "class definition may not be declared a friend");
9925 }
9926
9927 /* Parse an (optional) storage-class-specifier.
9928
9929    storage-class-specifier:
9930      auto
9931      register
9932      static
9933      extern
9934      mutable
9935
9936    GNU Extension:
9937
9938    storage-class-specifier:
9939      thread
9940
9941    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
9942
9943 static tree
9944 cp_parser_storage_class_specifier_opt (cp_parser* parser)
9945 {
9946   switch (cp_lexer_peek_token (parser->lexer)->keyword)
9947     {
9948     case RID_AUTO:
9949       if (cxx_dialect != cxx98)
9950         return NULL_TREE;
9951       /* Fall through for C++98.  */
9952
9953     case RID_REGISTER:
9954     case RID_STATIC:
9955     case RID_EXTERN:
9956     case RID_MUTABLE:
9957     case RID_THREAD:
9958       /* Consume the token.  */
9959       return cp_lexer_consume_token (parser->lexer)->u.value;
9960
9961     default:
9962       return NULL_TREE;
9963     }
9964 }
9965
9966 /* Parse an (optional) function-specifier.
9967
9968    function-specifier:
9969      inline
9970      virtual
9971      explicit
9972
9973    Returns an IDENTIFIER_NODE corresponding to the keyword used.
9974    Updates DECL_SPECS, if it is non-NULL.  */
9975
9976 static tree
9977 cp_parser_function_specifier_opt (cp_parser* parser,
9978                                   cp_decl_specifier_seq *decl_specs)
9979 {
9980   cp_token *token = cp_lexer_peek_token (parser->lexer);
9981   switch (token->keyword)
9982     {
9983     case RID_INLINE:
9984       if (decl_specs)
9985         ++decl_specs->specs[(int) ds_inline];
9986       break;
9987
9988     case RID_VIRTUAL:
9989       /* 14.5.2.3 [temp.mem]
9990
9991          A member function template shall not be virtual.  */
9992       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
9993         error_at (token->location, "templates may not be %<virtual%>");
9994       else if (decl_specs)
9995         ++decl_specs->specs[(int) ds_virtual];
9996       break;
9997
9998     case RID_EXPLICIT:
9999       if (decl_specs)
10000         ++decl_specs->specs[(int) ds_explicit];
10001       break;
10002
10003     default:
10004       return NULL_TREE;
10005     }
10006
10007   /* Consume the token.  */
10008   return cp_lexer_consume_token (parser->lexer)->u.value;
10009 }
10010
10011 /* Parse a linkage-specification.
10012
10013    linkage-specification:
10014      extern string-literal { declaration-seq [opt] }
10015      extern string-literal declaration  */
10016
10017 static void
10018 cp_parser_linkage_specification (cp_parser* parser)
10019 {
10020   tree linkage;
10021
10022   /* Look for the `extern' keyword.  */
10023   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10024
10025   /* Look for the string-literal.  */
10026   linkage = cp_parser_string_literal (parser, false, false);
10027
10028   /* Transform the literal into an identifier.  If the literal is a
10029      wide-character string, or contains embedded NULs, then we can't
10030      handle it as the user wants.  */
10031   if (strlen (TREE_STRING_POINTER (linkage))
10032       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10033     {
10034       cp_parser_error (parser, "invalid linkage-specification");
10035       /* Assume C++ linkage.  */
10036       linkage = lang_name_cplusplus;
10037     }
10038   else
10039     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10040
10041   /* We're now using the new linkage.  */
10042   push_lang_context (linkage);
10043
10044   /* If the next token is a `{', then we're using the first
10045      production.  */
10046   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10047     {
10048       /* Consume the `{' token.  */
10049       cp_lexer_consume_token (parser->lexer);
10050       /* Parse the declarations.  */
10051       cp_parser_declaration_seq_opt (parser);
10052       /* Look for the closing `}'.  */
10053       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10054     }
10055   /* Otherwise, there's just one declaration.  */
10056   else
10057     {
10058       bool saved_in_unbraced_linkage_specification_p;
10059
10060       saved_in_unbraced_linkage_specification_p
10061         = parser->in_unbraced_linkage_specification_p;
10062       parser->in_unbraced_linkage_specification_p = true;
10063       cp_parser_declaration (parser);
10064       parser->in_unbraced_linkage_specification_p
10065         = saved_in_unbraced_linkage_specification_p;
10066     }
10067
10068   /* We're done with the linkage-specification.  */
10069   pop_lang_context ();
10070 }
10071
10072 /* Parse a static_assert-declaration.
10073
10074    static_assert-declaration:
10075      static_assert ( constant-expression , string-literal ) ; 
10076
10077    If MEMBER_P, this static_assert is a class member.  */
10078
10079 static void 
10080 cp_parser_static_assert(cp_parser *parser, bool member_p)
10081 {
10082   tree condition;
10083   tree message;
10084   cp_token *token;
10085   location_t saved_loc;
10086   bool dummy;
10087
10088   /* Peek at the `static_assert' token so we can keep track of exactly
10089      where the static assertion started.  */
10090   token = cp_lexer_peek_token (parser->lexer);
10091   saved_loc = token->location;
10092
10093   /* Look for the `static_assert' keyword.  */
10094   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10095                                   RT_STATIC_ASSERT))
10096     return;
10097
10098   /*  We know we are in a static assertion; commit to any tentative
10099       parse.  */
10100   if (cp_parser_parsing_tentatively (parser))
10101     cp_parser_commit_to_tentative_parse (parser);
10102
10103   /* Parse the `(' starting the static assertion condition.  */
10104   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10105
10106   /* Parse the constant-expression.  Allow a non-constant expression
10107      here in order to give better diagnostics in finish_static_assert.  */
10108   condition = 
10109     cp_parser_constant_expression (parser,
10110                                    /*allow_non_constant_p=*/true,
10111                                    /*non_constant_p=*/&dummy);
10112
10113   /* Parse the separating `,'.  */
10114   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10115
10116   /* Parse the string-literal message.  */
10117   message = cp_parser_string_literal (parser, 
10118                                       /*translate=*/false,
10119                                       /*wide_ok=*/true);
10120
10121   /* A `)' completes the static assertion.  */
10122   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10123     cp_parser_skip_to_closing_parenthesis (parser, 
10124                                            /*recovering=*/true, 
10125                                            /*or_comma=*/false,
10126                                            /*consume_paren=*/true);
10127
10128   /* A semicolon terminates the declaration.  */
10129   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10130
10131   /* Complete the static assertion, which may mean either processing 
10132      the static assert now or saving it for template instantiation.  */
10133   finish_static_assert (condition, message, saved_loc, member_p);
10134 }
10135
10136 /* Parse a `decltype' type. Returns the type. 
10137
10138    simple-type-specifier:
10139      decltype ( expression )  */
10140
10141 static tree
10142 cp_parser_decltype (cp_parser *parser)
10143 {
10144   tree expr;
10145   bool id_expression_or_member_access_p = false;
10146   const char *saved_message;
10147   bool saved_integral_constant_expression_p;
10148   bool saved_non_integral_constant_expression_p;
10149   cp_token *id_expr_start_token;
10150
10151   /* Look for the `decltype' token.  */
10152   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10153     return error_mark_node;
10154
10155   /* Types cannot be defined in a `decltype' expression.  Save away the
10156      old message.  */
10157   saved_message = parser->type_definition_forbidden_message;
10158
10159   /* And create the new one.  */
10160   parser->type_definition_forbidden_message
10161     = G_("types may not be defined in %<decltype%> expressions");
10162
10163   /* The restrictions on constant-expressions do not apply inside
10164      decltype expressions.  */
10165   saved_integral_constant_expression_p
10166     = parser->integral_constant_expression_p;
10167   saved_non_integral_constant_expression_p
10168     = parser->non_integral_constant_expression_p;
10169   parser->integral_constant_expression_p = false;
10170
10171   /* Do not actually evaluate the expression.  */
10172   ++cp_unevaluated_operand;
10173
10174   /* Do not warn about problems with the expression.  */
10175   ++c_inhibit_evaluation_warnings;
10176
10177   /* Parse the opening `('.  */
10178   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10179     return error_mark_node;
10180   
10181   /* First, try parsing an id-expression.  */
10182   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10183   cp_parser_parse_tentatively (parser);
10184   expr = cp_parser_id_expression (parser,
10185                                   /*template_keyword_p=*/false,
10186                                   /*check_dependency_p=*/true,
10187                                   /*template_p=*/NULL,
10188                                   /*declarator_p=*/false,
10189                                   /*optional_p=*/false);
10190
10191   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10192     {
10193       bool non_integral_constant_expression_p = false;
10194       tree id_expression = expr;
10195       cp_id_kind idk;
10196       const char *error_msg;
10197
10198       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10199         /* Lookup the name we got back from the id-expression.  */
10200         expr = cp_parser_lookup_name (parser, expr,
10201                                       none_type,
10202                                       /*is_template=*/false,
10203                                       /*is_namespace=*/false,
10204                                       /*check_dependency=*/true,
10205                                       /*ambiguous_decls=*/NULL,
10206                                       id_expr_start_token->location);
10207
10208       if (expr
10209           && expr != error_mark_node
10210           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10211           && TREE_CODE (expr) != TYPE_DECL
10212           && (TREE_CODE (expr) != BIT_NOT_EXPR
10213               || !TYPE_P (TREE_OPERAND (expr, 0)))
10214           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10215         {
10216           /* Complete lookup of the id-expression.  */
10217           expr = (finish_id_expression
10218                   (id_expression, expr, parser->scope, &idk,
10219                    /*integral_constant_expression_p=*/false,
10220                    /*allow_non_integral_constant_expression_p=*/true,
10221                    &non_integral_constant_expression_p,
10222                    /*template_p=*/false,
10223                    /*done=*/true,
10224                    /*address_p=*/false,
10225                    /*template_arg_p=*/false,
10226                    &error_msg,
10227                    id_expr_start_token->location));
10228
10229           if (expr == error_mark_node)
10230             /* We found an id-expression, but it was something that we
10231                should not have found. This is an error, not something
10232                we can recover from, so note that we found an
10233                id-expression and we'll recover as gracefully as
10234                possible.  */
10235             id_expression_or_member_access_p = true;
10236         }
10237
10238       if (expr 
10239           && expr != error_mark_node
10240           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10241         /* We have an id-expression.  */
10242         id_expression_or_member_access_p = true;
10243     }
10244
10245   if (!id_expression_or_member_access_p)
10246     {
10247       /* Abort the id-expression parse.  */
10248       cp_parser_abort_tentative_parse (parser);
10249
10250       /* Parsing tentatively, again.  */
10251       cp_parser_parse_tentatively (parser);
10252
10253       /* Parse a class member access.  */
10254       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10255                                            /*cast_p=*/false,
10256                                            /*member_access_only_p=*/true, NULL);
10257
10258       if (expr 
10259           && expr != error_mark_node
10260           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10261         /* We have an id-expression.  */
10262         id_expression_or_member_access_p = true;
10263     }
10264
10265   if (id_expression_or_member_access_p)
10266     /* We have parsed the complete id-expression or member access.  */
10267     cp_parser_parse_definitely (parser);
10268   else
10269     {
10270       bool saved_greater_than_is_operator_p;
10271
10272       /* Abort our attempt to parse an id-expression or member access
10273          expression.  */
10274       cp_parser_abort_tentative_parse (parser);
10275
10276       /* Within a parenthesized expression, a `>' token is always
10277          the greater-than operator.  */
10278       saved_greater_than_is_operator_p
10279         = parser->greater_than_is_operator_p;
10280       parser->greater_than_is_operator_p = true;
10281
10282       /* Parse a full expression.  */
10283       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
10284
10285       /* The `>' token might be the end of a template-id or
10286          template-parameter-list now.  */
10287       parser->greater_than_is_operator_p
10288         = saved_greater_than_is_operator_p;
10289     }
10290
10291   /* Go back to evaluating expressions.  */
10292   --cp_unevaluated_operand;
10293   --c_inhibit_evaluation_warnings;
10294
10295   /* Restore the old message and the integral constant expression
10296      flags.  */
10297   parser->type_definition_forbidden_message = saved_message;
10298   parser->integral_constant_expression_p
10299     = saved_integral_constant_expression_p;
10300   parser->non_integral_constant_expression_p
10301     = saved_non_integral_constant_expression_p;
10302
10303   if (expr == error_mark_node)
10304     {
10305       /* Skip everything up to the closing `)'.  */
10306       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10307                                              /*consume_paren=*/true);
10308       return error_mark_node;
10309     }
10310   
10311   /* Parse to the closing `)'.  */
10312   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10313     {
10314       cp_parser_skip_to_closing_parenthesis (parser, true, false,
10315                                              /*consume_paren=*/true);
10316       return error_mark_node;
10317     }
10318
10319   return finish_decltype_type (expr, id_expression_or_member_access_p,
10320                                tf_warning_or_error);
10321 }
10322
10323 /* Special member functions [gram.special] */
10324
10325 /* Parse a conversion-function-id.
10326
10327    conversion-function-id:
10328      operator conversion-type-id
10329
10330    Returns an IDENTIFIER_NODE representing the operator.  */
10331
10332 static tree
10333 cp_parser_conversion_function_id (cp_parser* parser)
10334 {
10335   tree type;
10336   tree saved_scope;
10337   tree saved_qualifying_scope;
10338   tree saved_object_scope;
10339   tree pushed_scope = NULL_TREE;
10340
10341   /* Look for the `operator' token.  */
10342   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10343     return error_mark_node;
10344   /* When we parse the conversion-type-id, the current scope will be
10345      reset.  However, we need that information in able to look up the
10346      conversion function later, so we save it here.  */
10347   saved_scope = parser->scope;
10348   saved_qualifying_scope = parser->qualifying_scope;
10349   saved_object_scope = parser->object_scope;
10350   /* We must enter the scope of the class so that the names of
10351      entities declared within the class are available in the
10352      conversion-type-id.  For example, consider:
10353
10354        struct S {
10355          typedef int I;
10356          operator I();
10357        };
10358
10359        S::operator I() { ... }
10360
10361      In order to see that `I' is a type-name in the definition, we
10362      must be in the scope of `S'.  */
10363   if (saved_scope)
10364     pushed_scope = push_scope (saved_scope);
10365   /* Parse the conversion-type-id.  */
10366   type = cp_parser_conversion_type_id (parser);
10367   /* Leave the scope of the class, if any.  */
10368   if (pushed_scope)
10369     pop_scope (pushed_scope);
10370   /* Restore the saved scope.  */
10371   parser->scope = saved_scope;
10372   parser->qualifying_scope = saved_qualifying_scope;
10373   parser->object_scope = saved_object_scope;
10374   /* If the TYPE is invalid, indicate failure.  */
10375   if (type == error_mark_node)
10376     return error_mark_node;
10377   return mangle_conv_op_name_for_type (type);
10378 }
10379
10380 /* Parse a conversion-type-id:
10381
10382    conversion-type-id:
10383      type-specifier-seq conversion-declarator [opt]
10384
10385    Returns the TYPE specified.  */
10386
10387 static tree
10388 cp_parser_conversion_type_id (cp_parser* parser)
10389 {
10390   tree attributes;
10391   cp_decl_specifier_seq type_specifiers;
10392   cp_declarator *declarator;
10393   tree type_specified;
10394
10395   /* Parse the attributes.  */
10396   attributes = cp_parser_attributes_opt (parser);
10397   /* Parse the type-specifiers.  */
10398   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
10399                                 /*is_trailing_return=*/false,
10400                                 &type_specifiers);
10401   /* If that didn't work, stop.  */
10402   if (type_specifiers.type == error_mark_node)
10403     return error_mark_node;
10404   /* Parse the conversion-declarator.  */
10405   declarator = cp_parser_conversion_declarator_opt (parser);
10406
10407   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
10408                                     /*initialized=*/0, &attributes);
10409   if (attributes)
10410     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
10411
10412   /* Don't give this error when parsing tentatively.  This happens to
10413      work because we always parse this definitively once.  */
10414   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
10415       && type_uses_auto (type_specified))
10416     {
10417       error ("invalid use of %<auto%> in conversion operator");
10418       return error_mark_node;
10419     }
10420
10421   return type_specified;
10422 }
10423
10424 /* Parse an (optional) conversion-declarator.
10425
10426    conversion-declarator:
10427      ptr-operator conversion-declarator [opt]
10428
10429    */
10430
10431 static cp_declarator *
10432 cp_parser_conversion_declarator_opt (cp_parser* parser)
10433 {
10434   enum tree_code code;
10435   tree class_type;
10436   cp_cv_quals cv_quals;
10437
10438   /* We don't know if there's a ptr-operator next, or not.  */
10439   cp_parser_parse_tentatively (parser);
10440   /* Try the ptr-operator.  */
10441   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
10442   /* If it worked, look for more conversion-declarators.  */
10443   if (cp_parser_parse_definitely (parser))
10444     {
10445       cp_declarator *declarator;
10446
10447       /* Parse another optional declarator.  */
10448       declarator = cp_parser_conversion_declarator_opt (parser);
10449
10450       return cp_parser_make_indirect_declarator
10451         (code, class_type, cv_quals, declarator);
10452    }
10453
10454   return NULL;
10455 }
10456
10457 /* Parse an (optional) ctor-initializer.
10458
10459    ctor-initializer:
10460      : mem-initializer-list
10461
10462    Returns TRUE iff the ctor-initializer was actually present.  */
10463
10464 static bool
10465 cp_parser_ctor_initializer_opt (cp_parser* parser)
10466 {
10467   /* If the next token is not a `:', then there is no
10468      ctor-initializer.  */
10469   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
10470     {
10471       /* Do default initialization of any bases and members.  */
10472       if (DECL_CONSTRUCTOR_P (current_function_decl))
10473         finish_mem_initializers (NULL_TREE);
10474
10475       return false;
10476     }
10477
10478   /* Consume the `:' token.  */
10479   cp_lexer_consume_token (parser->lexer);
10480   /* And the mem-initializer-list.  */
10481   cp_parser_mem_initializer_list (parser);
10482
10483   return true;
10484 }
10485
10486 /* Parse a mem-initializer-list.
10487
10488    mem-initializer-list:
10489      mem-initializer ... [opt]
10490      mem-initializer ... [opt] , mem-initializer-list  */
10491
10492 static void
10493 cp_parser_mem_initializer_list (cp_parser* parser)
10494 {
10495   tree mem_initializer_list = NULL_TREE;
10496   cp_token *token = cp_lexer_peek_token (parser->lexer);
10497
10498   /* Let the semantic analysis code know that we are starting the
10499      mem-initializer-list.  */
10500   if (!DECL_CONSTRUCTOR_P (current_function_decl))
10501     error_at (token->location,
10502               "only constructors take member initializers");
10503
10504   /* Loop through the list.  */
10505   while (true)
10506     {
10507       tree mem_initializer;
10508
10509       token = cp_lexer_peek_token (parser->lexer);
10510       /* Parse the mem-initializer.  */
10511       mem_initializer = cp_parser_mem_initializer (parser);
10512       /* If the next token is a `...', we're expanding member initializers. */
10513       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
10514         {
10515           /* Consume the `...'. */
10516           cp_lexer_consume_token (parser->lexer);
10517
10518           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
10519              can be expanded but members cannot. */
10520           if (mem_initializer != error_mark_node
10521               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
10522             {
10523               error_at (token->location,
10524                         "cannot expand initializer for member %<%D%>",
10525                         TREE_PURPOSE (mem_initializer));
10526               mem_initializer = error_mark_node;
10527             }
10528
10529           /* Construct the pack expansion type. */
10530           if (mem_initializer != error_mark_node)
10531             mem_initializer = make_pack_expansion (mem_initializer);
10532         }
10533       /* Add it to the list, unless it was erroneous.  */
10534       if (mem_initializer != error_mark_node)
10535         {
10536           TREE_CHAIN (mem_initializer) = mem_initializer_list;
10537           mem_initializer_list = mem_initializer;
10538         }
10539       /* If the next token is not a `,', we're done.  */
10540       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
10541         break;
10542       /* Consume the `,' token.  */
10543       cp_lexer_consume_token (parser->lexer);
10544     }
10545
10546   /* Perform semantic analysis.  */
10547   if (DECL_CONSTRUCTOR_P (current_function_decl))
10548     finish_mem_initializers (mem_initializer_list);
10549 }
10550
10551 /* Parse a mem-initializer.
10552
10553    mem-initializer:
10554      mem-initializer-id ( expression-list [opt] )
10555      mem-initializer-id braced-init-list
10556
10557    GNU extension:
10558
10559    mem-initializer:
10560      ( expression-list [opt] )
10561
10562    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
10563    class) or FIELD_DECL (for a non-static data member) to initialize;
10564    the TREE_VALUE is the expression-list.  An empty initialization
10565    list is represented by void_list_node.  */
10566
10567 static tree
10568 cp_parser_mem_initializer (cp_parser* parser)
10569 {
10570   tree mem_initializer_id;
10571   tree expression_list;
10572   tree member;
10573   cp_token *token = cp_lexer_peek_token (parser->lexer);
10574
10575   /* Find out what is being initialized.  */
10576   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
10577     {
10578       permerror (token->location,
10579                  "anachronistic old-style base class initializer");
10580       mem_initializer_id = NULL_TREE;
10581     }
10582   else
10583     {
10584       mem_initializer_id = cp_parser_mem_initializer_id (parser);
10585       if (mem_initializer_id == error_mark_node)
10586         return mem_initializer_id;
10587     }
10588   member = expand_member_init (mem_initializer_id);
10589   if (member && !DECL_P (member))
10590     in_base_initializer = 1;
10591
10592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10593     {
10594       bool expr_non_constant_p;
10595       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
10596       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
10597       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
10598       expression_list = build_tree_list (NULL_TREE, expression_list);
10599     }
10600   else
10601     {
10602       VEC(tree,gc)* vec;
10603       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
10604                                                      /*cast_p=*/false,
10605                                                      /*allow_expansion_p=*/true,
10606                                                      /*non_constant_p=*/NULL);
10607       if (vec == NULL)
10608         return error_mark_node;
10609       expression_list = build_tree_list_vec (vec);
10610       release_tree_vector (vec);
10611     }
10612
10613   if (expression_list == error_mark_node)
10614     return error_mark_node;
10615   if (!expression_list)
10616     expression_list = void_type_node;
10617
10618   in_base_initializer = 0;
10619
10620   return member ? build_tree_list (member, expression_list) : error_mark_node;
10621 }
10622
10623 /* Parse a mem-initializer-id.
10624
10625    mem-initializer-id:
10626      :: [opt] nested-name-specifier [opt] class-name
10627      identifier
10628
10629    Returns a TYPE indicating the class to be initializer for the first
10630    production.  Returns an IDENTIFIER_NODE indicating the data member
10631    to be initialized for the second production.  */
10632
10633 static tree
10634 cp_parser_mem_initializer_id (cp_parser* parser)
10635 {
10636   bool global_scope_p;
10637   bool nested_name_specifier_p;
10638   bool template_p = false;
10639   tree id;
10640
10641   cp_token *token = cp_lexer_peek_token (parser->lexer);
10642
10643   /* `typename' is not allowed in this context ([temp.res]).  */
10644   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
10645     {
10646       error_at (token->location, 
10647                 "keyword %<typename%> not allowed in this context (a qualified "
10648                 "member initializer is implicitly a type)");
10649       cp_lexer_consume_token (parser->lexer);
10650     }
10651   /* Look for the optional `::' operator.  */
10652   global_scope_p
10653     = (cp_parser_global_scope_opt (parser,
10654                                    /*current_scope_valid_p=*/false)
10655        != NULL_TREE);
10656   /* Look for the optional nested-name-specifier.  The simplest way to
10657      implement:
10658
10659        [temp.res]
10660
10661        The keyword `typename' is not permitted in a base-specifier or
10662        mem-initializer; in these contexts a qualified name that
10663        depends on a template-parameter is implicitly assumed to be a
10664        type name.
10665
10666      is to assume that we have seen the `typename' keyword at this
10667      point.  */
10668   nested_name_specifier_p
10669     = (cp_parser_nested_name_specifier_opt (parser,
10670                                             /*typename_keyword_p=*/true,
10671                                             /*check_dependency_p=*/true,
10672                                             /*type_p=*/true,
10673                                             /*is_declaration=*/true)
10674        != NULL_TREE);
10675   if (nested_name_specifier_p)
10676     template_p = cp_parser_optional_template_keyword (parser);
10677   /* If there is a `::' operator or a nested-name-specifier, then we
10678      are definitely looking for a class-name.  */
10679   if (global_scope_p || nested_name_specifier_p)
10680     return cp_parser_class_name (parser,
10681                                  /*typename_keyword_p=*/true,
10682                                  /*template_keyword_p=*/template_p,
10683                                  typename_type,
10684                                  /*check_dependency_p=*/true,
10685                                  /*class_head_p=*/false,
10686                                  /*is_declaration=*/true);
10687   /* Otherwise, we could also be looking for an ordinary identifier.  */
10688   cp_parser_parse_tentatively (parser);
10689   /* Try a class-name.  */
10690   id = cp_parser_class_name (parser,
10691                              /*typename_keyword_p=*/true,
10692                              /*template_keyword_p=*/false,
10693                              none_type,
10694                              /*check_dependency_p=*/true,
10695                              /*class_head_p=*/false,
10696                              /*is_declaration=*/true);
10697   /* If we found one, we're done.  */
10698   if (cp_parser_parse_definitely (parser))
10699     return id;
10700   /* Otherwise, look for an ordinary identifier.  */
10701   return cp_parser_identifier (parser);
10702 }
10703
10704 /* Overloading [gram.over] */
10705
10706 /* Parse an operator-function-id.
10707
10708    operator-function-id:
10709      operator operator
10710
10711    Returns an IDENTIFIER_NODE for the operator which is a
10712    human-readable spelling of the identifier, e.g., `operator +'.  */
10713
10714 static tree
10715 cp_parser_operator_function_id (cp_parser* parser)
10716 {
10717   /* Look for the `operator' keyword.  */
10718   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
10719     return error_mark_node;
10720   /* And then the name of the operator itself.  */
10721   return cp_parser_operator (parser);
10722 }
10723
10724 /* Parse an operator.
10725
10726    operator:
10727      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
10728      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
10729      || ++ -- , ->* -> () []
10730
10731    GNU Extensions:
10732
10733    operator:
10734      <? >? <?= >?=
10735
10736    Returns an IDENTIFIER_NODE for the operator which is a
10737    human-readable spelling of the identifier, e.g., `operator +'.  */
10738
10739 static tree
10740 cp_parser_operator (cp_parser* parser)
10741 {
10742   tree id = NULL_TREE;
10743   cp_token *token;
10744
10745   /* Peek at the next token.  */
10746   token = cp_lexer_peek_token (parser->lexer);
10747   /* Figure out which operator we have.  */
10748   switch (token->type)
10749     {
10750     case CPP_KEYWORD:
10751       {
10752         enum tree_code op;
10753
10754         /* The keyword should be either `new' or `delete'.  */
10755         if (token->keyword == RID_NEW)
10756           op = NEW_EXPR;
10757         else if (token->keyword == RID_DELETE)
10758           op = DELETE_EXPR;
10759         else
10760           break;
10761
10762         /* Consume the `new' or `delete' token.  */
10763         cp_lexer_consume_token (parser->lexer);
10764
10765         /* Peek at the next token.  */
10766         token = cp_lexer_peek_token (parser->lexer);
10767         /* If it's a `[' token then this is the array variant of the
10768            operator.  */
10769         if (token->type == CPP_OPEN_SQUARE)
10770           {
10771             /* Consume the `[' token.  */
10772             cp_lexer_consume_token (parser->lexer);
10773             /* Look for the `]' token.  */
10774             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10775             id = ansi_opname (op == NEW_EXPR
10776                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
10777           }
10778         /* Otherwise, we have the non-array variant.  */
10779         else
10780           id = ansi_opname (op);
10781
10782         return id;
10783       }
10784
10785     case CPP_PLUS:
10786       id = ansi_opname (PLUS_EXPR);
10787       break;
10788
10789     case CPP_MINUS:
10790       id = ansi_opname (MINUS_EXPR);
10791       break;
10792
10793     case CPP_MULT:
10794       id = ansi_opname (MULT_EXPR);
10795       break;
10796
10797     case CPP_DIV:
10798       id = ansi_opname (TRUNC_DIV_EXPR);
10799       break;
10800
10801     case CPP_MOD:
10802       id = ansi_opname (TRUNC_MOD_EXPR);
10803       break;
10804
10805     case CPP_XOR:
10806       id = ansi_opname (BIT_XOR_EXPR);
10807       break;
10808
10809     case CPP_AND:
10810       id = ansi_opname (BIT_AND_EXPR);
10811       break;
10812
10813     case CPP_OR:
10814       id = ansi_opname (BIT_IOR_EXPR);
10815       break;
10816
10817     case CPP_COMPL:
10818       id = ansi_opname (BIT_NOT_EXPR);
10819       break;
10820
10821     case CPP_NOT:
10822       id = ansi_opname (TRUTH_NOT_EXPR);
10823       break;
10824
10825     case CPP_EQ:
10826       id = ansi_assopname (NOP_EXPR);
10827       break;
10828
10829     case CPP_LESS:
10830       id = ansi_opname (LT_EXPR);
10831       break;
10832
10833     case CPP_GREATER:
10834       id = ansi_opname (GT_EXPR);
10835       break;
10836
10837     case CPP_PLUS_EQ:
10838       id = ansi_assopname (PLUS_EXPR);
10839       break;
10840
10841     case CPP_MINUS_EQ:
10842       id = ansi_assopname (MINUS_EXPR);
10843       break;
10844
10845     case CPP_MULT_EQ:
10846       id = ansi_assopname (MULT_EXPR);
10847       break;
10848
10849     case CPP_DIV_EQ:
10850       id = ansi_assopname (TRUNC_DIV_EXPR);
10851       break;
10852
10853     case CPP_MOD_EQ:
10854       id = ansi_assopname (TRUNC_MOD_EXPR);
10855       break;
10856
10857     case CPP_XOR_EQ:
10858       id = ansi_assopname (BIT_XOR_EXPR);
10859       break;
10860
10861     case CPP_AND_EQ:
10862       id = ansi_assopname (BIT_AND_EXPR);
10863       break;
10864
10865     case CPP_OR_EQ:
10866       id = ansi_assopname (BIT_IOR_EXPR);
10867       break;
10868
10869     case CPP_LSHIFT:
10870       id = ansi_opname (LSHIFT_EXPR);
10871       break;
10872
10873     case CPP_RSHIFT:
10874       id = ansi_opname (RSHIFT_EXPR);
10875       break;
10876
10877     case CPP_LSHIFT_EQ:
10878       id = ansi_assopname (LSHIFT_EXPR);
10879       break;
10880
10881     case CPP_RSHIFT_EQ:
10882       id = ansi_assopname (RSHIFT_EXPR);
10883       break;
10884
10885     case CPP_EQ_EQ:
10886       id = ansi_opname (EQ_EXPR);
10887       break;
10888
10889     case CPP_NOT_EQ:
10890       id = ansi_opname (NE_EXPR);
10891       break;
10892
10893     case CPP_LESS_EQ:
10894       id = ansi_opname (LE_EXPR);
10895       break;
10896
10897     case CPP_GREATER_EQ:
10898       id = ansi_opname (GE_EXPR);
10899       break;
10900
10901     case CPP_AND_AND:
10902       id = ansi_opname (TRUTH_ANDIF_EXPR);
10903       break;
10904
10905     case CPP_OR_OR:
10906       id = ansi_opname (TRUTH_ORIF_EXPR);
10907       break;
10908
10909     case CPP_PLUS_PLUS:
10910       id = ansi_opname (POSTINCREMENT_EXPR);
10911       break;
10912
10913     case CPP_MINUS_MINUS:
10914       id = ansi_opname (PREDECREMENT_EXPR);
10915       break;
10916
10917     case CPP_COMMA:
10918       id = ansi_opname (COMPOUND_EXPR);
10919       break;
10920
10921     case CPP_DEREF_STAR:
10922       id = ansi_opname (MEMBER_REF);
10923       break;
10924
10925     case CPP_DEREF:
10926       id = ansi_opname (COMPONENT_REF);
10927       break;
10928
10929     case CPP_OPEN_PAREN:
10930       /* Consume the `('.  */
10931       cp_lexer_consume_token (parser->lexer);
10932       /* Look for the matching `)'.  */
10933       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
10934       return ansi_opname (CALL_EXPR);
10935
10936     case CPP_OPEN_SQUARE:
10937       /* Consume the `['.  */
10938       cp_lexer_consume_token (parser->lexer);
10939       /* Look for the matching `]'.  */
10940       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
10941       return ansi_opname (ARRAY_REF);
10942
10943     default:
10944       /* Anything else is an error.  */
10945       break;
10946     }
10947
10948   /* If we have selected an identifier, we need to consume the
10949      operator token.  */
10950   if (id)
10951     cp_lexer_consume_token (parser->lexer);
10952   /* Otherwise, no valid operator name was present.  */
10953   else
10954     {
10955       cp_parser_error (parser, "expected operator");
10956       id = error_mark_node;
10957     }
10958
10959   return id;
10960 }
10961
10962 /* Parse a template-declaration.
10963
10964    template-declaration:
10965      export [opt] template < template-parameter-list > declaration
10966
10967    If MEMBER_P is TRUE, this template-declaration occurs within a
10968    class-specifier.
10969
10970    The grammar rule given by the standard isn't correct.  What
10971    is really meant is:
10972
10973    template-declaration:
10974      export [opt] template-parameter-list-seq
10975        decl-specifier-seq [opt] init-declarator [opt] ;
10976      export [opt] template-parameter-list-seq
10977        function-definition
10978
10979    template-parameter-list-seq:
10980      template-parameter-list-seq [opt]
10981      template < template-parameter-list >  */
10982
10983 static void
10984 cp_parser_template_declaration (cp_parser* parser, bool member_p)
10985 {
10986   /* Check for `export'.  */
10987   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
10988     {
10989       /* Consume the `export' token.  */
10990       cp_lexer_consume_token (parser->lexer);
10991       /* Warn that we do not support `export'.  */
10992       warning (0, "keyword %<export%> not implemented, and will be ignored");
10993     }
10994
10995   cp_parser_template_declaration_after_export (parser, member_p);
10996 }
10997
10998 /* Parse a template-parameter-list.
10999
11000    template-parameter-list:
11001      template-parameter
11002      template-parameter-list , template-parameter
11003
11004    Returns a TREE_LIST.  Each node represents a template parameter.
11005    The nodes are connected via their TREE_CHAINs.  */
11006
11007 static tree
11008 cp_parser_template_parameter_list (cp_parser* parser)
11009 {
11010   tree parameter_list = NULL_TREE;
11011
11012   begin_template_parm_list ();
11013
11014   /* The loop below parses the template parms.  We first need to know
11015      the total number of template parms to be able to compute proper
11016      canonical types of each dependent type. So after the loop, when
11017      we know the total number of template parms,
11018      end_template_parm_list computes the proper canonical types and
11019      fixes up the dependent types accordingly.  */
11020   while (true)
11021     {
11022       tree parameter;
11023       bool is_non_type;
11024       bool is_parameter_pack;
11025       location_t parm_loc;
11026
11027       /* Parse the template-parameter.  */
11028       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11029       parameter = cp_parser_template_parameter (parser, 
11030                                                 &is_non_type,
11031                                                 &is_parameter_pack);
11032       /* Add it to the list.  */
11033       if (parameter != error_mark_node)
11034         parameter_list = process_template_parm (parameter_list,
11035                                                 parm_loc,
11036                                                 parameter,
11037                                                 is_non_type,
11038                                                 is_parameter_pack,
11039                                                 0);
11040       else
11041        {
11042          tree err_parm = build_tree_list (parameter, parameter);
11043          parameter_list = chainon (parameter_list, err_parm);
11044        }
11045
11046       /* If the next token is not a `,', we're done.  */
11047       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11048         break;
11049       /* Otherwise, consume the `,' token.  */
11050       cp_lexer_consume_token (parser->lexer);
11051     }
11052
11053   return end_template_parm_list (parameter_list);
11054 }
11055
11056 /* Parse a template-parameter.
11057
11058    template-parameter:
11059      type-parameter
11060      parameter-declaration
11061
11062    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11063    the parameter.  The TREE_PURPOSE is the default value, if any.
11064    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11065    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11066    set to true iff this parameter is a parameter pack. */
11067
11068 static tree
11069 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11070                               bool *is_parameter_pack)
11071 {
11072   cp_token *token;
11073   cp_parameter_declarator *parameter_declarator;
11074   cp_declarator *id_declarator;
11075   tree parm;
11076
11077   /* Assume it is a type parameter or a template parameter.  */
11078   *is_non_type = false;
11079   /* Assume it not a parameter pack. */
11080   *is_parameter_pack = false;
11081   /* Peek at the next token.  */
11082   token = cp_lexer_peek_token (parser->lexer);
11083   /* If it is `class' or `template', we have a type-parameter.  */
11084   if (token->keyword == RID_TEMPLATE)
11085     return cp_parser_type_parameter (parser, is_parameter_pack);
11086   /* If it is `class' or `typename' we do not know yet whether it is a
11087      type parameter or a non-type parameter.  Consider:
11088
11089        template <typename T, typename T::X X> ...
11090
11091      or:
11092
11093        template <class C, class D*> ...
11094
11095      Here, the first parameter is a type parameter, and the second is
11096      a non-type parameter.  We can tell by looking at the token after
11097      the identifier -- if it is a `,', `=', or `>' then we have a type
11098      parameter.  */
11099   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11100     {
11101       /* Peek at the token after `class' or `typename'.  */
11102       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11103       /* If it's an ellipsis, we have a template type parameter
11104          pack. */
11105       if (token->type == CPP_ELLIPSIS)
11106         return cp_parser_type_parameter (parser, is_parameter_pack);
11107       /* If it's an identifier, skip it.  */
11108       if (token->type == CPP_NAME)
11109         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11110       /* Now, see if the token looks like the end of a template
11111          parameter.  */
11112       if (token->type == CPP_COMMA
11113           || token->type == CPP_EQ
11114           || token->type == CPP_GREATER)
11115         return cp_parser_type_parameter (parser, is_parameter_pack);
11116     }
11117
11118   /* Otherwise, it is a non-type parameter.
11119
11120      [temp.param]
11121
11122      When parsing a default template-argument for a non-type
11123      template-parameter, the first non-nested `>' is taken as the end
11124      of the template parameter-list rather than a greater-than
11125      operator.  */
11126   *is_non_type = true;
11127   parameter_declarator
11128      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11129                                         /*parenthesized_p=*/NULL);
11130
11131   /* If the parameter declaration is marked as a parameter pack, set
11132      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11133      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11134      grokdeclarator. */
11135   if (parameter_declarator
11136       && parameter_declarator->declarator
11137       && parameter_declarator->declarator->parameter_pack_p)
11138     {
11139       *is_parameter_pack = true;
11140       parameter_declarator->declarator->parameter_pack_p = false;
11141     }
11142
11143   /* If the next token is an ellipsis, and we don't already have it
11144      marked as a parameter pack, then we have a parameter pack (that
11145      has no declarator).  */
11146   if (!*is_parameter_pack
11147       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11148       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11149     {
11150       /* Consume the `...'.  */
11151       cp_lexer_consume_token (parser->lexer);
11152       maybe_warn_variadic_templates ();
11153       
11154       *is_parameter_pack = true;
11155     }
11156   /* We might end up with a pack expansion as the type of the non-type
11157      template parameter, in which case this is a non-type template
11158      parameter pack.  */
11159   else if (parameter_declarator
11160            && parameter_declarator->decl_specifiers.type
11161            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11162     {
11163       *is_parameter_pack = true;
11164       parameter_declarator->decl_specifiers.type = 
11165         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11166     }
11167
11168   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11169     {
11170       /* Parameter packs cannot have default arguments.  However, a
11171          user may try to do so, so we'll parse them and give an
11172          appropriate diagnostic here.  */
11173
11174       /* Consume the `='.  */
11175       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11176       cp_lexer_consume_token (parser->lexer);
11177       
11178       /* Find the name of the parameter pack.  */     
11179       id_declarator = parameter_declarator->declarator;
11180       while (id_declarator && id_declarator->kind != cdk_id)
11181         id_declarator = id_declarator->declarator;
11182       
11183       if (id_declarator && id_declarator->kind == cdk_id)
11184         error_at (start_token->location,
11185                   "template parameter pack %qD cannot have a default argument",
11186                   id_declarator->u.id.unqualified_name);
11187       else
11188         error_at (start_token->location,
11189                   "template parameter pack cannot have a default argument");
11190       
11191       /* Parse the default argument, but throw away the result.  */
11192       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11193     }
11194
11195   parm = grokdeclarator (parameter_declarator->declarator,
11196                          &parameter_declarator->decl_specifiers,
11197                          TPARM, /*initialized=*/0,
11198                          /*attrlist=*/NULL);
11199   if (parm == error_mark_node)
11200     return error_mark_node;
11201
11202   return build_tree_list (parameter_declarator->default_argument, parm);
11203 }
11204
11205 /* Parse a type-parameter.
11206
11207    type-parameter:
11208      class identifier [opt]
11209      class identifier [opt] = type-id
11210      typename identifier [opt]
11211      typename identifier [opt] = type-id
11212      template < template-parameter-list > class identifier [opt]
11213      template < template-parameter-list > class identifier [opt]
11214        = id-expression
11215
11216    GNU Extension (variadic templates):
11217
11218    type-parameter:
11219      class ... identifier [opt]
11220      typename ... identifier [opt]
11221
11222    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
11223    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
11224    the declaration of the parameter.
11225
11226    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
11227
11228 static tree
11229 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
11230 {
11231   cp_token *token;
11232   tree parameter;
11233
11234   /* Look for a keyword to tell us what kind of parameter this is.  */
11235   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
11236   if (!token)
11237     return error_mark_node;
11238
11239   switch (token->keyword)
11240     {
11241     case RID_CLASS:
11242     case RID_TYPENAME:
11243       {
11244         tree identifier;
11245         tree default_argument;
11246
11247         /* If the next token is an ellipsis, we have a template
11248            argument pack. */
11249         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11250           {
11251             /* Consume the `...' token. */
11252             cp_lexer_consume_token (parser->lexer);
11253             maybe_warn_variadic_templates ();
11254
11255             *is_parameter_pack = true;
11256           }
11257
11258         /* If the next token is an identifier, then it names the
11259            parameter.  */
11260         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
11261           identifier = cp_parser_identifier (parser);
11262         else
11263           identifier = NULL_TREE;
11264
11265         /* Create the parameter.  */
11266         parameter = finish_template_type_parm (class_type_node, identifier);
11267
11268         /* If the next token is an `=', we have a default argument.  */
11269         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11270           {
11271             /* Consume the `=' token.  */
11272             cp_lexer_consume_token (parser->lexer);
11273             /* Parse the default-argument.  */
11274             push_deferring_access_checks (dk_no_deferred);
11275             default_argument = cp_parser_type_id (parser);
11276
11277             /* Template parameter packs cannot have default
11278                arguments. */
11279             if (*is_parameter_pack)
11280               {
11281                 if (identifier)
11282                   error_at (token->location,
11283                             "template parameter pack %qD cannot have a "
11284                             "default argument", identifier);
11285                 else
11286                   error_at (token->location,
11287                             "template parameter packs cannot have "
11288                             "default arguments");
11289                 default_argument = NULL_TREE;
11290               }
11291             pop_deferring_access_checks ();
11292           }
11293         else
11294           default_argument = NULL_TREE;
11295
11296         /* Create the combined representation of the parameter and the
11297            default argument.  */
11298         parameter = build_tree_list (default_argument, parameter);
11299       }
11300       break;
11301
11302     case RID_TEMPLATE:
11303       {
11304         tree identifier;
11305         tree default_argument;
11306
11307         /* Look for the `<'.  */
11308         cp_parser_require (parser, CPP_LESS, RT_LESS);
11309         /* Parse the template-parameter-list.  */
11310         cp_parser_template_parameter_list (parser);
11311         /* Look for the `>'.  */
11312         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
11313         /* Look for the `class' keyword.  */
11314         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
11315         /* If the next token is an ellipsis, we have a template
11316            argument pack. */
11317         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11318           {
11319             /* Consume the `...' token. */
11320             cp_lexer_consume_token (parser->lexer);
11321             maybe_warn_variadic_templates ();
11322
11323             *is_parameter_pack = true;
11324           }
11325         /* If the next token is an `=', then there is a
11326            default-argument.  If the next token is a `>', we are at
11327            the end of the parameter-list.  If the next token is a `,',
11328            then we are at the end of this parameter.  */
11329         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
11330             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
11331             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11332           {
11333             identifier = cp_parser_identifier (parser);
11334             /* Treat invalid names as if the parameter were nameless.  */
11335             if (identifier == error_mark_node)
11336               identifier = NULL_TREE;
11337           }
11338         else
11339           identifier = NULL_TREE;
11340
11341         /* Create the template parameter.  */
11342         parameter = finish_template_template_parm (class_type_node,
11343                                                    identifier);
11344
11345         /* If the next token is an `=', then there is a
11346            default-argument.  */
11347         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11348           {
11349             bool is_template;
11350
11351             /* Consume the `='.  */
11352             cp_lexer_consume_token (parser->lexer);
11353             /* Parse the id-expression.  */
11354             push_deferring_access_checks (dk_no_deferred);
11355             /* save token before parsing the id-expression, for error
11356                reporting */
11357             token = cp_lexer_peek_token (parser->lexer);
11358             default_argument
11359               = cp_parser_id_expression (parser,
11360                                          /*template_keyword_p=*/false,
11361                                          /*check_dependency_p=*/true,
11362                                          /*template_p=*/&is_template,
11363                                          /*declarator_p=*/false,
11364                                          /*optional_p=*/false);
11365             if (TREE_CODE (default_argument) == TYPE_DECL)
11366               /* If the id-expression was a template-id that refers to
11367                  a template-class, we already have the declaration here,
11368                  so no further lookup is needed.  */
11369                  ;
11370             else
11371               /* Look up the name.  */
11372               default_argument
11373                 = cp_parser_lookup_name (parser, default_argument,
11374                                          none_type,
11375                                          /*is_template=*/is_template,
11376                                          /*is_namespace=*/false,
11377                                          /*check_dependency=*/true,
11378                                          /*ambiguous_decls=*/NULL,
11379                                          token->location);
11380             /* See if the default argument is valid.  */
11381             default_argument
11382               = check_template_template_default_arg (default_argument);
11383
11384             /* Template parameter packs cannot have default
11385                arguments. */
11386             if (*is_parameter_pack)
11387               {
11388                 if (identifier)
11389                   error_at (token->location,
11390                             "template parameter pack %qD cannot "
11391                             "have a default argument",
11392                             identifier);
11393                 else
11394                   error_at (token->location, "template parameter packs cannot "
11395                             "have default arguments");
11396                 default_argument = NULL_TREE;
11397               }
11398             pop_deferring_access_checks ();
11399           }
11400         else
11401           default_argument = NULL_TREE;
11402
11403         /* Create the combined representation of the parameter and the
11404            default argument.  */
11405         parameter = build_tree_list (default_argument, parameter);
11406       }
11407       break;
11408
11409     default:
11410       gcc_unreachable ();
11411       break;
11412     }
11413
11414   return parameter;
11415 }
11416
11417 /* Parse a template-id.
11418
11419    template-id:
11420      template-name < template-argument-list [opt] >
11421
11422    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
11423    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
11424    returned.  Otherwise, if the template-name names a function, or set
11425    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
11426    names a class, returns a TYPE_DECL for the specialization.
11427
11428    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
11429    uninstantiated templates.  */
11430
11431 static tree
11432 cp_parser_template_id (cp_parser *parser,
11433                        bool template_keyword_p,
11434                        bool check_dependency_p,
11435                        bool is_declaration)
11436 {
11437   int i;
11438   tree templ;
11439   tree arguments;
11440   tree template_id;
11441   cp_token_position start_of_id = 0;
11442   deferred_access_check *chk;
11443   VEC (deferred_access_check,gc) *access_check;
11444   cp_token *next_token = NULL, *next_token_2 = NULL;
11445   bool is_identifier;
11446
11447   /* If the next token corresponds to a template-id, there is no need
11448      to reparse it.  */
11449   next_token = cp_lexer_peek_token (parser->lexer);
11450   if (next_token->type == CPP_TEMPLATE_ID)
11451     {
11452       struct tree_check *check_value;
11453
11454       /* Get the stored value.  */
11455       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
11456       /* Perform any access checks that were deferred.  */
11457       access_check = check_value->checks;
11458       if (access_check)
11459         {
11460           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
11461             perform_or_defer_access_check (chk->binfo,
11462                                            chk->decl,
11463                                            chk->diag_decl);
11464         }
11465       /* Return the stored value.  */
11466       return check_value->value;
11467     }
11468
11469   /* Avoid performing name lookup if there is no possibility of
11470      finding a template-id.  */
11471   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
11472       || (next_token->type == CPP_NAME
11473           && !cp_parser_nth_token_starts_template_argument_list_p
11474                (parser, 2)))
11475     {
11476       cp_parser_error (parser, "expected template-id");
11477       return error_mark_node;
11478     }
11479
11480   /* Remember where the template-id starts.  */
11481   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
11482     start_of_id = cp_lexer_token_position (parser->lexer, false);
11483
11484   push_deferring_access_checks (dk_deferred);
11485
11486   /* Parse the template-name.  */
11487   is_identifier = false;
11488   templ = cp_parser_template_name (parser, template_keyword_p,
11489                                    check_dependency_p,
11490                                    is_declaration,
11491                                    &is_identifier);
11492   if (templ == error_mark_node || is_identifier)
11493     {
11494       pop_deferring_access_checks ();
11495       return templ;
11496     }
11497
11498   /* If we find the sequence `[:' after a template-name, it's probably
11499      a digraph-typo for `< ::'. Substitute the tokens and check if we can
11500      parse correctly the argument list.  */
11501   next_token = cp_lexer_peek_token (parser->lexer);
11502   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
11503   if (next_token->type == CPP_OPEN_SQUARE
11504       && next_token->flags & DIGRAPH
11505       && next_token_2->type == CPP_COLON
11506       && !(next_token_2->flags & PREV_WHITE))
11507     {
11508       cp_parser_parse_tentatively (parser);
11509       /* Change `:' into `::'.  */
11510       next_token_2->type = CPP_SCOPE;
11511       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
11512          CPP_LESS.  */
11513       cp_lexer_consume_token (parser->lexer);
11514
11515       /* Parse the arguments.  */
11516       arguments = cp_parser_enclosed_template_argument_list (parser);
11517       if (!cp_parser_parse_definitely (parser))
11518         {
11519           /* If we couldn't parse an argument list, then we revert our changes
11520              and return simply an error. Maybe this is not a template-id
11521              after all.  */
11522           next_token_2->type = CPP_COLON;
11523           cp_parser_error (parser, "expected %<<%>");
11524           pop_deferring_access_checks ();
11525           return error_mark_node;
11526         }
11527       /* Otherwise, emit an error about the invalid digraph, but continue
11528          parsing because we got our argument list.  */
11529       if (permerror (next_token->location,
11530                      "%<<::%> cannot begin a template-argument list"))
11531         {
11532           static bool hint = false;
11533           inform (next_token->location,
11534                   "%<<:%> is an alternate spelling for %<[%>."
11535                   " Insert whitespace between %<<%> and %<::%>");
11536           if (!hint && !flag_permissive)
11537             {
11538               inform (next_token->location, "(if you use %<-fpermissive%>"
11539                       " G++ will accept your code)");
11540               hint = true;
11541             }
11542         }
11543     }
11544   else
11545     {
11546       /* Look for the `<' that starts the template-argument-list.  */
11547       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
11548         {
11549           pop_deferring_access_checks ();
11550           return error_mark_node;
11551         }
11552       /* Parse the arguments.  */
11553       arguments = cp_parser_enclosed_template_argument_list (parser);
11554     }
11555
11556   /* Build a representation of the specialization.  */
11557   if (TREE_CODE (templ) == IDENTIFIER_NODE)
11558     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
11559   else if (DECL_CLASS_TEMPLATE_P (templ)
11560            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
11561     {
11562       bool entering_scope;
11563       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
11564          template (rather than some instantiation thereof) only if
11565          is not nested within some other construct.  For example, in
11566          "template <typename T> void f(T) { A<T>::", A<T> is just an
11567          instantiation of A.  */
11568       entering_scope = (template_parm_scope_p ()
11569                         && cp_lexer_next_token_is (parser->lexer,
11570                                                    CPP_SCOPE));
11571       template_id
11572         = finish_template_type (templ, arguments, entering_scope);
11573     }
11574   else
11575     {
11576       /* If it's not a class-template or a template-template, it should be
11577          a function-template.  */
11578       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
11579                    || TREE_CODE (templ) == OVERLOAD
11580                    || BASELINK_P (templ)));
11581
11582       template_id = lookup_template_function (templ, arguments);
11583     }
11584
11585   /* If parsing tentatively, replace the sequence of tokens that makes
11586      up the template-id with a CPP_TEMPLATE_ID token.  That way,
11587      should we re-parse the token stream, we will not have to repeat
11588      the effort required to do the parse, nor will we issue duplicate
11589      error messages about problems during instantiation of the
11590      template.  */
11591   if (start_of_id)
11592     {
11593       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
11594
11595       /* Reset the contents of the START_OF_ID token.  */
11596       token->type = CPP_TEMPLATE_ID;
11597       /* Retrieve any deferred checks.  Do not pop this access checks yet
11598          so the memory will not be reclaimed during token replacing below.  */
11599       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
11600       token->u.tree_check_value->value = template_id;
11601       token->u.tree_check_value->checks = get_deferred_access_checks ();
11602       token->keyword = RID_MAX;
11603
11604       /* Purge all subsequent tokens.  */
11605       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
11606
11607       /* ??? Can we actually assume that, if template_id ==
11608          error_mark_node, we will have issued a diagnostic to the
11609          user, as opposed to simply marking the tentative parse as
11610          failed?  */
11611       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
11612         error_at (token->location, "parse error in template argument list");
11613     }
11614
11615   pop_deferring_access_checks ();
11616   return template_id;
11617 }
11618
11619 /* Parse a template-name.
11620
11621    template-name:
11622      identifier
11623
11624    The standard should actually say:
11625
11626    template-name:
11627      identifier
11628      operator-function-id
11629
11630    A defect report has been filed about this issue.
11631
11632    A conversion-function-id cannot be a template name because they cannot
11633    be part of a template-id. In fact, looking at this code:
11634
11635    a.operator K<int>()
11636
11637    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
11638    It is impossible to call a templated conversion-function-id with an
11639    explicit argument list, since the only allowed template parameter is
11640    the type to which it is converting.
11641
11642    If TEMPLATE_KEYWORD_P is true, then we have just seen the
11643    `template' keyword, in a construction like:
11644
11645      T::template f<3>()
11646
11647    In that case `f' is taken to be a template-name, even though there
11648    is no way of knowing for sure.
11649
11650    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
11651    name refers to a set of overloaded functions, at least one of which
11652    is a template, or an IDENTIFIER_NODE with the name of the template,
11653    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
11654    names are looked up inside uninstantiated templates.  */
11655
11656 static tree
11657 cp_parser_template_name (cp_parser* parser,
11658                          bool template_keyword_p,
11659                          bool check_dependency_p,
11660                          bool is_declaration,
11661                          bool *is_identifier)
11662 {
11663   tree identifier;
11664   tree decl;
11665   tree fns;
11666   cp_token *token = cp_lexer_peek_token (parser->lexer);
11667
11668   /* If the next token is `operator', then we have either an
11669      operator-function-id or a conversion-function-id.  */
11670   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
11671     {
11672       /* We don't know whether we're looking at an
11673          operator-function-id or a conversion-function-id.  */
11674       cp_parser_parse_tentatively (parser);
11675       /* Try an operator-function-id.  */
11676       identifier = cp_parser_operator_function_id (parser);
11677       /* If that didn't work, try a conversion-function-id.  */
11678       if (!cp_parser_parse_definitely (parser))
11679         {
11680           cp_parser_error (parser, "expected template-name");
11681           return error_mark_node;
11682         }
11683     }
11684   /* Look for the identifier.  */
11685   else
11686     identifier = cp_parser_identifier (parser);
11687
11688   /* If we didn't find an identifier, we don't have a template-id.  */
11689   if (identifier == error_mark_node)
11690     return error_mark_node;
11691
11692   /* If the name immediately followed the `template' keyword, then it
11693      is a template-name.  However, if the next token is not `<', then
11694      we do not treat it as a template-name, since it is not being used
11695      as part of a template-id.  This enables us to handle constructs
11696      like:
11697
11698        template <typename T> struct S { S(); };
11699        template <typename T> S<T>::S();
11700
11701      correctly.  We would treat `S' as a template -- if it were `S<T>'
11702      -- but we do not if there is no `<'.  */
11703
11704   if (processing_template_decl
11705       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
11706     {
11707       /* In a declaration, in a dependent context, we pretend that the
11708          "template" keyword was present in order to improve error
11709          recovery.  For example, given:
11710
11711            template <typename T> void f(T::X<int>);
11712
11713          we want to treat "X<int>" as a template-id.  */
11714       if (is_declaration
11715           && !template_keyword_p
11716           && parser->scope && TYPE_P (parser->scope)
11717           && check_dependency_p
11718           && dependent_scope_p (parser->scope)
11719           /* Do not do this for dtors (or ctors), since they never
11720              need the template keyword before their name.  */
11721           && !constructor_name_p (identifier, parser->scope))
11722         {
11723           cp_token_position start = 0;
11724
11725           /* Explain what went wrong.  */
11726           error_at (token->location, "non-template %qD used as template",
11727                     identifier);
11728           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
11729                   parser->scope, identifier);
11730           /* If parsing tentatively, find the location of the "<" token.  */
11731           if (cp_parser_simulate_error (parser))
11732             start = cp_lexer_token_position (parser->lexer, true);
11733           /* Parse the template arguments so that we can issue error
11734              messages about them.  */
11735           cp_lexer_consume_token (parser->lexer);
11736           cp_parser_enclosed_template_argument_list (parser);
11737           /* Skip tokens until we find a good place from which to
11738              continue parsing.  */
11739           cp_parser_skip_to_closing_parenthesis (parser,
11740                                                  /*recovering=*/true,
11741                                                  /*or_comma=*/true,
11742                                                  /*consume_paren=*/false);
11743           /* If parsing tentatively, permanently remove the
11744              template argument list.  That will prevent duplicate
11745              error messages from being issued about the missing
11746              "template" keyword.  */
11747           if (start)
11748             cp_lexer_purge_tokens_after (parser->lexer, start);
11749           if (is_identifier)
11750             *is_identifier = true;
11751           return identifier;
11752         }
11753
11754       /* If the "template" keyword is present, then there is generally
11755          no point in doing name-lookup, so we just return IDENTIFIER.
11756          But, if the qualifying scope is non-dependent then we can
11757          (and must) do name-lookup normally.  */
11758       if (template_keyword_p
11759           && (!parser->scope
11760               || (TYPE_P (parser->scope)
11761                   && dependent_type_p (parser->scope))))
11762         return identifier;
11763     }
11764
11765   /* Look up the name.  */
11766   decl = cp_parser_lookup_name (parser, identifier,
11767                                 none_type,
11768                                 /*is_template=*/true,
11769                                 /*is_namespace=*/false,
11770                                 check_dependency_p,
11771                                 /*ambiguous_decls=*/NULL,
11772                                 token->location);
11773
11774   /* If DECL is a template, then the name was a template-name.  */
11775   if (TREE_CODE (decl) == TEMPLATE_DECL)
11776     ;
11777   else
11778     {
11779       tree fn = NULL_TREE;
11780
11781       /* The standard does not explicitly indicate whether a name that
11782          names a set of overloaded declarations, some of which are
11783          templates, is a template-name.  However, such a name should
11784          be a template-name; otherwise, there is no way to form a
11785          template-id for the overloaded templates.  */
11786       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
11787       if (TREE_CODE (fns) == OVERLOAD)
11788         for (fn = fns; fn; fn = OVL_NEXT (fn))
11789           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
11790             break;
11791
11792       if (!fn)
11793         {
11794           /* The name does not name a template.  */
11795           cp_parser_error (parser, "expected template-name");
11796           return error_mark_node;
11797         }
11798     }
11799
11800   /* If DECL is dependent, and refers to a function, then just return
11801      its name; we will look it up again during template instantiation.  */
11802   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
11803     {
11804       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
11805       if (TYPE_P (scope) && dependent_type_p (scope))
11806         return identifier;
11807     }
11808
11809   return decl;
11810 }
11811
11812 /* Parse a template-argument-list.
11813
11814    template-argument-list:
11815      template-argument ... [opt]
11816      template-argument-list , template-argument ... [opt]
11817
11818    Returns a TREE_VEC containing the arguments.  */
11819
11820 static tree
11821 cp_parser_template_argument_list (cp_parser* parser)
11822 {
11823   tree fixed_args[10];
11824   unsigned n_args = 0;
11825   unsigned alloced = 10;
11826   tree *arg_ary = fixed_args;
11827   tree vec;
11828   bool saved_in_template_argument_list_p;
11829   bool saved_ice_p;
11830   bool saved_non_ice_p;
11831
11832   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
11833   parser->in_template_argument_list_p = true;
11834   /* Even if the template-id appears in an integral
11835      constant-expression, the contents of the argument list do
11836      not.  */
11837   saved_ice_p = parser->integral_constant_expression_p;
11838   parser->integral_constant_expression_p = false;
11839   saved_non_ice_p = parser->non_integral_constant_expression_p;
11840   parser->non_integral_constant_expression_p = false;
11841   /* Parse the arguments.  */
11842   do
11843     {
11844       tree argument;
11845
11846       if (n_args)
11847         /* Consume the comma.  */
11848         cp_lexer_consume_token (parser->lexer);
11849
11850       /* Parse the template-argument.  */
11851       argument = cp_parser_template_argument (parser);
11852
11853       /* If the next token is an ellipsis, we're expanding a template
11854          argument pack. */
11855       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11856         {
11857           if (argument == error_mark_node)
11858             {
11859               cp_token *token = cp_lexer_peek_token (parser->lexer);
11860               error_at (token->location,
11861                         "expected parameter pack before %<...%>");
11862             }
11863           /* Consume the `...' token. */
11864           cp_lexer_consume_token (parser->lexer);
11865
11866           /* Make the argument into a TYPE_PACK_EXPANSION or
11867              EXPR_PACK_EXPANSION. */
11868           argument = make_pack_expansion (argument);
11869         }
11870
11871       if (n_args == alloced)
11872         {
11873           alloced *= 2;
11874
11875           if (arg_ary == fixed_args)
11876             {
11877               arg_ary = XNEWVEC (tree, alloced);
11878               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
11879             }
11880           else
11881             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
11882         }
11883       arg_ary[n_args++] = argument;
11884     }
11885   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
11886
11887   vec = make_tree_vec (n_args);
11888
11889   while (n_args--)
11890     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
11891
11892   if (arg_ary != fixed_args)
11893     free (arg_ary);
11894   parser->non_integral_constant_expression_p = saved_non_ice_p;
11895   parser->integral_constant_expression_p = saved_ice_p;
11896   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
11897 #ifdef ENABLE_CHECKING
11898   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
11899 #endif
11900   return vec;
11901 }
11902
11903 /* Parse a template-argument.
11904
11905    template-argument:
11906      assignment-expression
11907      type-id
11908      id-expression
11909
11910    The representation is that of an assignment-expression, type-id, or
11911    id-expression -- except that the qualified id-expression is
11912    evaluated, so that the value returned is either a DECL or an
11913    OVERLOAD.
11914
11915    Although the standard says "assignment-expression", it forbids
11916    throw-expressions or assignments in the template argument.
11917    Therefore, we use "conditional-expression" instead.  */
11918
11919 static tree
11920 cp_parser_template_argument (cp_parser* parser)
11921 {
11922   tree argument;
11923   bool template_p;
11924   bool address_p;
11925   bool maybe_type_id = false;
11926   cp_token *token = NULL, *argument_start_token = NULL;
11927   cp_id_kind idk;
11928
11929   /* There's really no way to know what we're looking at, so we just
11930      try each alternative in order.
11931
11932        [temp.arg]
11933
11934        In a template-argument, an ambiguity between a type-id and an
11935        expression is resolved to a type-id, regardless of the form of
11936        the corresponding template-parameter.
11937
11938      Therefore, we try a type-id first.  */
11939   cp_parser_parse_tentatively (parser);
11940   argument = cp_parser_template_type_arg (parser);
11941   /* If there was no error parsing the type-id but the next token is a
11942      '>>', our behavior depends on which dialect of C++ we're
11943      parsing. In C++98, we probably found a typo for '> >'. But there
11944      are type-id which are also valid expressions. For instance:
11945
11946      struct X { int operator >> (int); };
11947      template <int V> struct Foo {};
11948      Foo<X () >> 5> r;
11949
11950      Here 'X()' is a valid type-id of a function type, but the user just
11951      wanted to write the expression "X() >> 5". Thus, we remember that we
11952      found a valid type-id, but we still try to parse the argument as an
11953      expression to see what happens. 
11954
11955      In C++0x, the '>>' will be considered two separate '>'
11956      tokens.  */
11957   if (!cp_parser_error_occurred (parser)
11958       && cxx_dialect == cxx98
11959       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
11960     {
11961       maybe_type_id = true;
11962       cp_parser_abort_tentative_parse (parser);
11963     }
11964   else
11965     {
11966       /* If the next token isn't a `,' or a `>', then this argument wasn't
11967       really finished. This means that the argument is not a valid
11968       type-id.  */
11969       if (!cp_parser_next_token_ends_template_argument_p (parser))
11970         cp_parser_error (parser, "expected template-argument");
11971       /* If that worked, we're done.  */
11972       if (cp_parser_parse_definitely (parser))
11973         return argument;
11974     }
11975   /* We're still not sure what the argument will be.  */
11976   cp_parser_parse_tentatively (parser);
11977   /* Try a template.  */
11978   argument_start_token = cp_lexer_peek_token (parser->lexer);
11979   argument = cp_parser_id_expression (parser,
11980                                       /*template_keyword_p=*/false,
11981                                       /*check_dependency_p=*/true,
11982                                       &template_p,
11983                                       /*declarator_p=*/false,
11984                                       /*optional_p=*/false);
11985   /* If the next token isn't a `,' or a `>', then this argument wasn't
11986      really finished.  */
11987   if (!cp_parser_next_token_ends_template_argument_p (parser))
11988     cp_parser_error (parser, "expected template-argument");
11989   if (!cp_parser_error_occurred (parser))
11990     {
11991       /* Figure out what is being referred to.  If the id-expression
11992          was for a class template specialization, then we will have a
11993          TYPE_DECL at this point.  There is no need to do name lookup
11994          at this point in that case.  */
11995       if (TREE_CODE (argument) != TYPE_DECL)
11996         argument = cp_parser_lookup_name (parser, argument,
11997                                           none_type,
11998                                           /*is_template=*/template_p,
11999                                           /*is_namespace=*/false,
12000                                           /*check_dependency=*/true,
12001                                           /*ambiguous_decls=*/NULL,
12002                                           argument_start_token->location);
12003       if (TREE_CODE (argument) != TEMPLATE_DECL
12004           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12005         cp_parser_error (parser, "expected template-name");
12006     }
12007   if (cp_parser_parse_definitely (parser))
12008     return argument;
12009   /* It must be a non-type argument.  There permitted cases are given
12010      in [temp.arg.nontype]:
12011
12012      -- an integral constant-expression of integral or enumeration
12013         type; or
12014
12015      -- the name of a non-type template-parameter; or
12016
12017      -- the name of an object or function with external linkage...
12018
12019      -- the address of an object or function with external linkage...
12020
12021      -- a pointer to member...  */
12022   /* Look for a non-type template parameter.  */
12023   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12024     {
12025       cp_parser_parse_tentatively (parser);
12026       argument = cp_parser_primary_expression (parser,
12027                                                /*address_p=*/false,
12028                                                /*cast_p=*/false,
12029                                                /*template_arg_p=*/true,
12030                                                &idk);
12031       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12032           || !cp_parser_next_token_ends_template_argument_p (parser))
12033         cp_parser_simulate_error (parser);
12034       if (cp_parser_parse_definitely (parser))
12035         return argument;
12036     }
12037
12038   /* If the next token is "&", the argument must be the address of an
12039      object or function with external linkage.  */
12040   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12041   if (address_p)
12042     cp_lexer_consume_token (parser->lexer);
12043   /* See if we might have an id-expression.  */
12044   token = cp_lexer_peek_token (parser->lexer);
12045   if (token->type == CPP_NAME
12046       || token->keyword == RID_OPERATOR
12047       || token->type == CPP_SCOPE
12048       || token->type == CPP_TEMPLATE_ID
12049       || token->type == CPP_NESTED_NAME_SPECIFIER)
12050     {
12051       cp_parser_parse_tentatively (parser);
12052       argument = cp_parser_primary_expression (parser,
12053                                                address_p,
12054                                                /*cast_p=*/false,
12055                                                /*template_arg_p=*/true,
12056                                                &idk);
12057       if (cp_parser_error_occurred (parser)
12058           || !cp_parser_next_token_ends_template_argument_p (parser))
12059         cp_parser_abort_tentative_parse (parser);
12060       else
12061         {
12062           tree probe;
12063
12064           if (TREE_CODE (argument) == INDIRECT_REF)
12065             {
12066               gcc_assert (REFERENCE_REF_P (argument));
12067               argument = TREE_OPERAND (argument, 0);
12068             }
12069
12070           /* If we're in a template, we represent a qualified-id referring
12071              to a static data member as a SCOPE_REF even if the scope isn't
12072              dependent so that we can check access control later.  */
12073           probe = argument;
12074           if (TREE_CODE (probe) == SCOPE_REF)
12075             probe = TREE_OPERAND (probe, 1);
12076           if (TREE_CODE (probe) == VAR_DECL)
12077             {
12078               /* A variable without external linkage might still be a
12079                  valid constant-expression, so no error is issued here
12080                  if the external-linkage check fails.  */
12081               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12082                 cp_parser_simulate_error (parser);
12083             }
12084           else if (is_overloaded_fn (argument))
12085             /* All overloaded functions are allowed; if the external
12086                linkage test does not pass, an error will be issued
12087                later.  */
12088             ;
12089           else if (address_p
12090                    && (TREE_CODE (argument) == OFFSET_REF
12091                        || TREE_CODE (argument) == SCOPE_REF))
12092             /* A pointer-to-member.  */
12093             ;
12094           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12095             ;
12096           else
12097             cp_parser_simulate_error (parser);
12098
12099           if (cp_parser_parse_definitely (parser))
12100             {
12101               if (address_p)
12102                 argument = build_x_unary_op (ADDR_EXPR, argument,
12103                                              tf_warning_or_error);
12104               return argument;
12105             }
12106         }
12107     }
12108   /* If the argument started with "&", there are no other valid
12109      alternatives at this point.  */
12110   if (address_p)
12111     {
12112       cp_parser_error (parser, "invalid non-type template argument");
12113       return error_mark_node;
12114     }
12115
12116   /* If the argument wasn't successfully parsed as a type-id followed
12117      by '>>', the argument can only be a constant expression now.
12118      Otherwise, we try parsing the constant-expression tentatively,
12119      because the argument could really be a type-id.  */
12120   if (maybe_type_id)
12121     cp_parser_parse_tentatively (parser);
12122   argument = cp_parser_constant_expression (parser,
12123                                             /*allow_non_constant_p=*/false,
12124                                             /*non_constant_p=*/NULL);
12125   argument = fold_non_dependent_expr (argument);
12126   if (!maybe_type_id)
12127     return argument;
12128   if (!cp_parser_next_token_ends_template_argument_p (parser))
12129     cp_parser_error (parser, "expected template-argument");
12130   if (cp_parser_parse_definitely (parser))
12131     return argument;
12132   /* We did our best to parse the argument as a non type-id, but that
12133      was the only alternative that matched (albeit with a '>' after
12134      it). We can assume it's just a typo from the user, and a
12135      diagnostic will then be issued.  */
12136   return cp_parser_template_type_arg (parser);
12137 }
12138
12139 /* Parse an explicit-instantiation.
12140
12141    explicit-instantiation:
12142      template declaration
12143
12144    Although the standard says `declaration', what it really means is:
12145
12146    explicit-instantiation:
12147      template decl-specifier-seq [opt] declarator [opt] ;
12148
12149    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12150    supposed to be allowed.  A defect report has been filed about this
12151    issue.
12152
12153    GNU Extension:
12154
12155    explicit-instantiation:
12156      storage-class-specifier template
12157        decl-specifier-seq [opt] declarator [opt] ;
12158      function-specifier template
12159        decl-specifier-seq [opt] declarator [opt] ;  */
12160
12161 static void
12162 cp_parser_explicit_instantiation (cp_parser* parser)
12163 {
12164   int declares_class_or_enum;
12165   cp_decl_specifier_seq decl_specifiers;
12166   tree extension_specifier = NULL_TREE;
12167
12168   timevar_push (TV_TEMPLATE_INST);
12169
12170   /* Look for an (optional) storage-class-specifier or
12171      function-specifier.  */
12172   if (cp_parser_allow_gnu_extensions_p (parser))
12173     {
12174       extension_specifier
12175         = cp_parser_storage_class_specifier_opt (parser);
12176       if (!extension_specifier)
12177         extension_specifier
12178           = cp_parser_function_specifier_opt (parser,
12179                                               /*decl_specs=*/NULL);
12180     }
12181
12182   /* Look for the `template' keyword.  */
12183   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12184   /* Let the front end know that we are processing an explicit
12185      instantiation.  */
12186   begin_explicit_instantiation ();
12187   /* [temp.explicit] says that we are supposed to ignore access
12188      control while processing explicit instantiation directives.  */
12189   push_deferring_access_checks (dk_no_check);
12190   /* Parse a decl-specifier-seq.  */
12191   cp_parser_decl_specifier_seq (parser,
12192                                 CP_PARSER_FLAGS_OPTIONAL,
12193                                 &decl_specifiers,
12194                                 &declares_class_or_enum);
12195   /* If there was exactly one decl-specifier, and it declared a class,
12196      and there's no declarator, then we have an explicit type
12197      instantiation.  */
12198   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12199     {
12200       tree type;
12201
12202       type = check_tag_decl (&decl_specifiers);
12203       /* Turn access control back on for names used during
12204          template instantiation.  */
12205       pop_deferring_access_checks ();
12206       if (type)
12207         do_type_instantiation (type, extension_specifier,
12208                                /*complain=*/tf_error);
12209     }
12210   else
12211     {
12212       cp_declarator *declarator;
12213       tree decl;
12214
12215       /* Parse the declarator.  */
12216       declarator
12217         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
12218                                 /*ctor_dtor_or_conv_p=*/NULL,
12219                                 /*parenthesized_p=*/NULL,
12220                                 /*member_p=*/false);
12221       if (declares_class_or_enum & 2)
12222         cp_parser_check_for_definition_in_return_type (declarator,
12223                                                        decl_specifiers.type,
12224                                                        decl_specifiers.type_location);
12225       if (declarator != cp_error_declarator)
12226         {
12227           if (decl_specifiers.specs[(int)ds_inline])
12228             permerror (input_location, "explicit instantiation shall not use"
12229                        " %<inline%> specifier");
12230           if (decl_specifiers.specs[(int)ds_constexpr])
12231             permerror (input_location, "explicit instantiation shall not use"
12232                        " %<constexpr%> specifier");
12233
12234           decl = grokdeclarator (declarator, &decl_specifiers,
12235                                  NORMAL, 0, &decl_specifiers.attributes);
12236           /* Turn access control back on for names used during
12237              template instantiation.  */
12238           pop_deferring_access_checks ();
12239           /* Do the explicit instantiation.  */
12240           do_decl_instantiation (decl, extension_specifier);
12241         }
12242       else
12243         {
12244           pop_deferring_access_checks ();
12245           /* Skip the body of the explicit instantiation.  */
12246           cp_parser_skip_to_end_of_statement (parser);
12247         }
12248     }
12249   /* We're done with the instantiation.  */
12250   end_explicit_instantiation ();
12251
12252   cp_parser_consume_semicolon_at_end_of_statement (parser);
12253
12254   timevar_pop (TV_TEMPLATE_INST);
12255 }
12256
12257 /* Parse an explicit-specialization.
12258
12259    explicit-specialization:
12260      template < > declaration
12261
12262    Although the standard says `declaration', what it really means is:
12263
12264    explicit-specialization:
12265      template <> decl-specifier [opt] init-declarator [opt] ;
12266      template <> function-definition
12267      template <> explicit-specialization
12268      template <> template-declaration  */
12269
12270 static void
12271 cp_parser_explicit_specialization (cp_parser* parser)
12272 {
12273   bool need_lang_pop;
12274   cp_token *token = cp_lexer_peek_token (parser->lexer);
12275
12276   /* Look for the `template' keyword.  */
12277   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12278   /* Look for the `<'.  */
12279   cp_parser_require (parser, CPP_LESS, RT_LESS);
12280   /* Look for the `>'.  */
12281   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12282   /* We have processed another parameter list.  */
12283   ++parser->num_template_parameter_lists;
12284   /* [temp]
12285
12286      A template ... explicit specialization ... shall not have C
12287      linkage.  */
12288   if (current_lang_name == lang_name_c)
12289     {
12290       error_at (token->location, "template specialization with C linkage");
12291       /* Give it C++ linkage to avoid confusing other parts of the
12292          front end.  */
12293       push_lang_context (lang_name_cplusplus);
12294       need_lang_pop = true;
12295     }
12296   else
12297     need_lang_pop = false;
12298   /* Let the front end know that we are beginning a specialization.  */
12299   if (!begin_specialization ())
12300     {
12301       end_specialization ();
12302       return;
12303     }
12304
12305   /* If the next keyword is `template', we need to figure out whether
12306      or not we're looking a template-declaration.  */
12307   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
12308     {
12309       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
12310           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
12311         cp_parser_template_declaration_after_export (parser,
12312                                                      /*member_p=*/false);
12313       else
12314         cp_parser_explicit_specialization (parser);
12315     }
12316   else
12317     /* Parse the dependent declaration.  */
12318     cp_parser_single_declaration (parser,
12319                                   /*checks=*/NULL,
12320                                   /*member_p=*/false,
12321                                   /*explicit_specialization_p=*/true,
12322                                   /*friend_p=*/NULL);
12323   /* We're done with the specialization.  */
12324   end_specialization ();
12325   /* For the erroneous case of a template with C linkage, we pushed an
12326      implicit C++ linkage scope; exit that scope now.  */
12327   if (need_lang_pop)
12328     pop_lang_context ();
12329   /* We're done with this parameter list.  */
12330   --parser->num_template_parameter_lists;
12331 }
12332
12333 /* Parse a type-specifier.
12334
12335    type-specifier:
12336      simple-type-specifier
12337      class-specifier
12338      enum-specifier
12339      elaborated-type-specifier
12340      cv-qualifier
12341
12342    GNU Extension:
12343
12344    type-specifier:
12345      __complex__
12346
12347    Returns a representation of the type-specifier.  For a
12348    class-specifier, enum-specifier, or elaborated-type-specifier, a
12349    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
12350
12351    The parser flags FLAGS is used to control type-specifier parsing.
12352
12353    If IS_DECLARATION is TRUE, then this type-specifier is appearing
12354    in a decl-specifier-seq.
12355
12356    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
12357    class-specifier, enum-specifier, or elaborated-type-specifier, then
12358    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
12359    if a type is declared; 2 if it is defined.  Otherwise, it is set to
12360    zero.
12361
12362    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
12363    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
12364    is set to FALSE.  */
12365
12366 static tree
12367 cp_parser_type_specifier (cp_parser* parser,
12368                           cp_parser_flags flags,
12369                           cp_decl_specifier_seq *decl_specs,
12370                           bool is_declaration,
12371                           int* declares_class_or_enum,
12372                           bool* is_cv_qualifier)
12373 {
12374   tree type_spec = NULL_TREE;
12375   cp_token *token;
12376   enum rid keyword;
12377   cp_decl_spec ds = ds_last;
12378
12379   /* Assume this type-specifier does not declare a new type.  */
12380   if (declares_class_or_enum)
12381     *declares_class_or_enum = 0;
12382   /* And that it does not specify a cv-qualifier.  */
12383   if (is_cv_qualifier)
12384     *is_cv_qualifier = false;
12385   /* Peek at the next token.  */
12386   token = cp_lexer_peek_token (parser->lexer);
12387
12388   /* If we're looking at a keyword, we can use that to guide the
12389      production we choose.  */
12390   keyword = token->keyword;
12391   switch (keyword)
12392     {
12393     case RID_ENUM:
12394       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12395         goto elaborated_type_specifier;
12396
12397       /* Look for the enum-specifier.  */
12398       type_spec = cp_parser_enum_specifier (parser);
12399       /* If that worked, we're done.  */
12400       if (type_spec)
12401         {
12402           if (declares_class_or_enum)
12403             *declares_class_or_enum = 2;
12404           if (decl_specs)
12405             cp_parser_set_decl_spec_type (decl_specs,
12406                                           type_spec,
12407                                           token->location,
12408                                           /*user_defined_p=*/true);
12409           return type_spec;
12410         }
12411       else
12412         goto elaborated_type_specifier;
12413
12414       /* Any of these indicate either a class-specifier, or an
12415          elaborated-type-specifier.  */
12416     case RID_CLASS:
12417     case RID_STRUCT:
12418     case RID_UNION:
12419       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
12420         goto elaborated_type_specifier;
12421
12422       /* Parse tentatively so that we can back up if we don't find a
12423          class-specifier.  */
12424       cp_parser_parse_tentatively (parser);
12425       /* Look for the class-specifier.  */
12426       type_spec = cp_parser_class_specifier (parser);
12427       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
12428       /* If that worked, we're done.  */
12429       if (cp_parser_parse_definitely (parser))
12430         {
12431           if (declares_class_or_enum)
12432             *declares_class_or_enum = 2;
12433           if (decl_specs)
12434             cp_parser_set_decl_spec_type (decl_specs,
12435                                           type_spec,
12436                                           token->location,
12437                                           /*user_defined_p=*/true);
12438           return type_spec;
12439         }
12440
12441       /* Fall through.  */
12442     elaborated_type_specifier:
12443       /* We're declaring (not defining) a class or enum.  */
12444       if (declares_class_or_enum)
12445         *declares_class_or_enum = 1;
12446
12447       /* Fall through.  */
12448     case RID_TYPENAME:
12449       /* Look for an elaborated-type-specifier.  */
12450       type_spec
12451         = (cp_parser_elaborated_type_specifier
12452            (parser,
12453             decl_specs && decl_specs->specs[(int) ds_friend],
12454             is_declaration));
12455       if (decl_specs)
12456         cp_parser_set_decl_spec_type (decl_specs,
12457                                       type_spec,
12458                                       token->location,
12459                                       /*user_defined_p=*/true);
12460       return type_spec;
12461
12462     case RID_CONST:
12463       ds = ds_const;
12464       if (is_cv_qualifier)
12465         *is_cv_qualifier = true;
12466       break;
12467
12468     case RID_VOLATILE:
12469       ds = ds_volatile;
12470       if (is_cv_qualifier)
12471         *is_cv_qualifier = true;
12472       break;
12473
12474     case RID_RESTRICT:
12475       ds = ds_restrict;
12476       if (is_cv_qualifier)
12477         *is_cv_qualifier = true;
12478       break;
12479
12480     case RID_COMPLEX:
12481       /* The `__complex__' keyword is a GNU extension.  */
12482       ds = ds_complex;
12483       break;
12484
12485     default:
12486       break;
12487     }
12488
12489   /* Handle simple keywords.  */
12490   if (ds != ds_last)
12491     {
12492       if (decl_specs)
12493         {
12494           ++decl_specs->specs[(int)ds];
12495           decl_specs->any_specifiers_p = true;
12496         }
12497       return cp_lexer_consume_token (parser->lexer)->u.value;
12498     }
12499
12500   /* If we do not already have a type-specifier, assume we are looking
12501      at a simple-type-specifier.  */
12502   type_spec = cp_parser_simple_type_specifier (parser,
12503                                                decl_specs,
12504                                                flags);
12505
12506   /* If we didn't find a type-specifier, and a type-specifier was not
12507      optional in this context, issue an error message.  */
12508   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12509     {
12510       cp_parser_error (parser, "expected type specifier");
12511       return error_mark_node;
12512     }
12513
12514   return type_spec;
12515 }
12516
12517 /* Parse a simple-type-specifier.
12518
12519    simple-type-specifier:
12520      :: [opt] nested-name-specifier [opt] type-name
12521      :: [opt] nested-name-specifier template template-id
12522      char
12523      wchar_t
12524      bool
12525      short
12526      int
12527      long
12528      signed
12529      unsigned
12530      float
12531      double
12532      void
12533
12534    C++0x Extension:
12535
12536    simple-type-specifier:
12537      auto
12538      decltype ( expression )   
12539      char16_t
12540      char32_t
12541      __underlying_type ( type-id )
12542
12543    GNU Extension:
12544
12545    simple-type-specifier:
12546      __int128
12547      __typeof__ unary-expression
12548      __typeof__ ( type-id )
12549
12550    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
12551    appropriately updated.  */
12552
12553 static tree
12554 cp_parser_simple_type_specifier (cp_parser* parser,
12555                                  cp_decl_specifier_seq *decl_specs,
12556                                  cp_parser_flags flags)
12557 {
12558   tree type = NULL_TREE;
12559   cp_token *token;
12560
12561   /* Peek at the next token.  */
12562   token = cp_lexer_peek_token (parser->lexer);
12563
12564   /* If we're looking at a keyword, things are easy.  */
12565   switch (token->keyword)
12566     {
12567     case RID_CHAR:
12568       if (decl_specs)
12569         decl_specs->explicit_char_p = true;
12570       type = char_type_node;
12571       break;
12572     case RID_CHAR16:
12573       type = char16_type_node;
12574       break;
12575     case RID_CHAR32:
12576       type = char32_type_node;
12577       break;
12578     case RID_WCHAR:
12579       type = wchar_type_node;
12580       break;
12581     case RID_BOOL:
12582       type = boolean_type_node;
12583       break;
12584     case RID_SHORT:
12585       if (decl_specs)
12586         ++decl_specs->specs[(int) ds_short];
12587       type = short_integer_type_node;
12588       break;
12589     case RID_INT:
12590       if (decl_specs)
12591         decl_specs->explicit_int_p = true;
12592       type = integer_type_node;
12593       break;
12594     case RID_INT128:
12595       if (!int128_integer_type_node)
12596         break;
12597       if (decl_specs)
12598         decl_specs->explicit_int128_p = true;
12599       type = int128_integer_type_node;
12600       break;
12601     case RID_LONG:
12602       if (decl_specs)
12603         ++decl_specs->specs[(int) ds_long];
12604       type = long_integer_type_node;
12605       break;
12606     case RID_SIGNED:
12607       if (decl_specs)
12608         ++decl_specs->specs[(int) ds_signed];
12609       type = integer_type_node;
12610       break;
12611     case RID_UNSIGNED:
12612       if (decl_specs)
12613         ++decl_specs->specs[(int) ds_unsigned];
12614       type = unsigned_type_node;
12615       break;
12616     case RID_FLOAT:
12617       type = float_type_node;
12618       break;
12619     case RID_DOUBLE:
12620       type = double_type_node;
12621       break;
12622     case RID_VOID:
12623       type = void_type_node;
12624       break;
12625       
12626     case RID_AUTO:
12627       maybe_warn_cpp0x (CPP0X_AUTO);
12628       type = make_auto ();
12629       break;
12630
12631     case RID_DECLTYPE:
12632       /* Parse the `decltype' type.  */
12633       type = cp_parser_decltype (parser);
12634
12635       if (decl_specs)
12636         cp_parser_set_decl_spec_type (decl_specs, type,
12637                                       token->location,
12638                                       /*user_defined_p=*/true);
12639
12640       return type;
12641
12642     case RID_TYPEOF:
12643       /* Consume the `typeof' token.  */
12644       cp_lexer_consume_token (parser->lexer);
12645       /* Parse the operand to `typeof'.  */
12646       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
12647       /* If it is not already a TYPE, take its type.  */
12648       if (!TYPE_P (type))
12649         type = finish_typeof (type);
12650
12651       if (decl_specs)
12652         cp_parser_set_decl_spec_type (decl_specs, type,
12653                                       token->location,
12654                                       /*user_defined_p=*/true);
12655
12656       return type;
12657
12658     case RID_UNDERLYING_TYPE:
12659       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
12660
12661       if (decl_specs)
12662         cp_parser_set_decl_spec_type (decl_specs, type,
12663                                       token->location,
12664                                       /*user_defined_p=*/true);
12665
12666       return type;
12667
12668     default:
12669       break;
12670     }
12671
12672   /* If the type-specifier was for a built-in type, we're done.  */
12673   if (type)
12674     {
12675       /* Record the type.  */
12676       if (decl_specs
12677           && (token->keyword != RID_SIGNED
12678               && token->keyword != RID_UNSIGNED
12679               && token->keyword != RID_SHORT
12680               && token->keyword != RID_LONG))
12681         cp_parser_set_decl_spec_type (decl_specs,
12682                                       type,
12683                                       token->location,
12684                                       /*user_defined=*/false);
12685       if (decl_specs)
12686         decl_specs->any_specifiers_p = true;
12687
12688       /* Consume the token.  */
12689       cp_lexer_consume_token (parser->lexer);
12690
12691       /* There is no valid C++ program where a non-template type is
12692          followed by a "<".  That usually indicates that the user thought
12693          that the type was a template.  */
12694       cp_parser_check_for_invalid_template_id (parser, type, token->location);
12695
12696       return TYPE_NAME (type);
12697     }
12698
12699   /* The type-specifier must be a user-defined type.  */
12700   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
12701     {
12702       bool qualified_p;
12703       bool global_p;
12704
12705       /* Don't gobble tokens or issue error messages if this is an
12706          optional type-specifier.  */
12707       if (flags & CP_PARSER_FLAGS_OPTIONAL)
12708         cp_parser_parse_tentatively (parser);
12709
12710       /* Look for the optional `::' operator.  */
12711       global_p
12712         = (cp_parser_global_scope_opt (parser,
12713                                        /*current_scope_valid_p=*/false)
12714            != NULL_TREE);
12715       /* Look for the nested-name specifier.  */
12716       qualified_p
12717         = (cp_parser_nested_name_specifier_opt (parser,
12718                                                 /*typename_keyword_p=*/false,
12719                                                 /*check_dependency_p=*/true,
12720                                                 /*type_p=*/false,
12721                                                 /*is_declaration=*/false)
12722            != NULL_TREE);
12723       token = cp_lexer_peek_token (parser->lexer);
12724       /* If we have seen a nested-name-specifier, and the next token
12725          is `template', then we are using the template-id production.  */
12726       if (parser->scope
12727           && cp_parser_optional_template_keyword (parser))
12728         {
12729           /* Look for the template-id.  */
12730           type = cp_parser_template_id (parser,
12731                                         /*template_keyword_p=*/true,
12732                                         /*check_dependency_p=*/true,
12733                                         /*is_declaration=*/false);
12734           /* If the template-id did not name a type, we are out of
12735              luck.  */
12736           if (TREE_CODE (type) != TYPE_DECL)
12737             {
12738               cp_parser_error (parser, "expected template-id for type");
12739               type = NULL_TREE;
12740             }
12741         }
12742       /* Otherwise, look for a type-name.  */
12743       else
12744         type = cp_parser_type_name (parser);
12745       /* Keep track of all name-lookups performed in class scopes.  */
12746       if (type
12747           && !global_p
12748           && !qualified_p
12749           && TREE_CODE (type) == TYPE_DECL
12750           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
12751         maybe_note_name_used_in_class (DECL_NAME (type), type);
12752       /* If it didn't work out, we don't have a TYPE.  */
12753       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
12754           && !cp_parser_parse_definitely (parser))
12755         type = NULL_TREE;
12756       if (type && decl_specs)
12757         cp_parser_set_decl_spec_type (decl_specs, type,
12758                                       token->location,
12759                                       /*user_defined=*/true);
12760     }
12761
12762   /* If we didn't get a type-name, issue an error message.  */
12763   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
12764     {
12765       cp_parser_error (parser, "expected type-name");
12766       return error_mark_node;
12767     }
12768
12769   if (type && type != error_mark_node)
12770     {
12771       /* See if TYPE is an Objective-C type, and if so, parse and
12772          accept any protocol references following it.  Do this before
12773          the cp_parser_check_for_invalid_template_id() call, because
12774          Objective-C types can be followed by '<...>' which would
12775          enclose protocol names rather than template arguments, and so
12776          everything is fine.  */
12777       if (c_dialect_objc () && !parser->scope
12778           && (objc_is_id (type) || objc_is_class_name (type)))
12779         {
12780           tree protos = cp_parser_objc_protocol_refs_opt (parser);
12781           tree qual_type = objc_get_protocol_qualified_type (type, protos);
12782
12783           /* Clobber the "unqualified" type previously entered into
12784              DECL_SPECS with the new, improved protocol-qualified version.  */
12785           if (decl_specs)
12786             decl_specs->type = qual_type;
12787
12788           return qual_type;
12789         }
12790
12791       /* There is no valid C++ program where a non-template type is
12792          followed by a "<".  That usually indicates that the user
12793          thought that the type was a template.  */
12794       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
12795                                                token->location);
12796     }
12797
12798   return type;
12799 }
12800
12801 /* Parse a type-name.
12802
12803    type-name:
12804      class-name
12805      enum-name
12806      typedef-name
12807
12808    enum-name:
12809      identifier
12810
12811    typedef-name:
12812      identifier
12813
12814    Returns a TYPE_DECL for the type.  */
12815
12816 static tree
12817 cp_parser_type_name (cp_parser* parser)
12818 {
12819   tree type_decl;
12820
12821   /* We can't know yet whether it is a class-name or not.  */
12822   cp_parser_parse_tentatively (parser);
12823   /* Try a class-name.  */
12824   type_decl = cp_parser_class_name (parser,
12825                                     /*typename_keyword_p=*/false,
12826                                     /*template_keyword_p=*/false,
12827                                     none_type,
12828                                     /*check_dependency_p=*/true,
12829                                     /*class_head_p=*/false,
12830                                     /*is_declaration=*/false);
12831   /* If it's not a class-name, keep looking.  */
12832   if (!cp_parser_parse_definitely (parser))
12833     {
12834       /* It must be a typedef-name or an enum-name.  */
12835       return cp_parser_nonclass_name (parser);
12836     }
12837
12838   return type_decl;
12839 }
12840
12841 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
12842
12843    enum-name:
12844      identifier
12845
12846    typedef-name:
12847      identifier
12848
12849    Returns a TYPE_DECL for the type.  */
12850
12851 static tree
12852 cp_parser_nonclass_name (cp_parser* parser)
12853 {
12854   tree type_decl;
12855   tree identifier;
12856
12857   cp_token *token = cp_lexer_peek_token (parser->lexer);
12858   identifier = cp_parser_identifier (parser);
12859   if (identifier == error_mark_node)
12860     return error_mark_node;
12861
12862   /* Look up the type-name.  */
12863   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
12864
12865   if (TREE_CODE (type_decl) != TYPE_DECL
12866       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
12867     {
12868       /* See if this is an Objective-C type.  */
12869       tree protos = cp_parser_objc_protocol_refs_opt (parser);
12870       tree type = objc_get_protocol_qualified_type (identifier, protos);
12871       if (type)
12872         type_decl = TYPE_NAME (type);
12873     }
12874
12875   /* Issue an error if we did not find a type-name.  */
12876   if (TREE_CODE (type_decl) != TYPE_DECL
12877       /* In Objective-C, we have the complication that class names are
12878          normally type names and start declarations (eg, the
12879          "NSObject" in "NSObject *object;"), but can be used in an
12880          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
12881          is an expression.  So, a classname followed by a dot is not a
12882          valid type-name.  */
12883       || (objc_is_class_name (TREE_TYPE (type_decl))
12884           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
12885     {
12886       if (!cp_parser_simulate_error (parser))
12887         cp_parser_name_lookup_error (parser, identifier, type_decl,
12888                                      NLE_TYPE, token->location);
12889       return error_mark_node;
12890     }
12891   /* Remember that the name was used in the definition of the
12892      current class so that we can check later to see if the
12893      meaning would have been different after the class was
12894      entirely defined.  */
12895   else if (type_decl != error_mark_node
12896            && !parser->scope)
12897     maybe_note_name_used_in_class (identifier, type_decl);
12898   
12899   return type_decl;
12900 }
12901
12902 /* Parse an elaborated-type-specifier.  Note that the grammar given
12903    here incorporates the resolution to DR68.
12904
12905    elaborated-type-specifier:
12906      class-key :: [opt] nested-name-specifier [opt] identifier
12907      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
12908      enum-key :: [opt] nested-name-specifier [opt] identifier
12909      typename :: [opt] nested-name-specifier identifier
12910      typename :: [opt] nested-name-specifier template [opt]
12911        template-id
12912
12913    GNU extension:
12914
12915    elaborated-type-specifier:
12916      class-key attributes :: [opt] nested-name-specifier [opt] identifier
12917      class-key attributes :: [opt] nested-name-specifier [opt]
12918                template [opt] template-id
12919      enum attributes :: [opt] nested-name-specifier [opt] identifier
12920
12921    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
12922    declared `friend'.  If IS_DECLARATION is TRUE, then this
12923    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
12924    something is being declared.
12925
12926    Returns the TYPE specified.  */
12927
12928 static tree
12929 cp_parser_elaborated_type_specifier (cp_parser* parser,
12930                                      bool is_friend,
12931                                      bool is_declaration)
12932 {
12933   enum tag_types tag_type;
12934   tree identifier;
12935   tree type = NULL_TREE;
12936   tree attributes = NULL_TREE;
12937   tree globalscope;
12938   cp_token *token = NULL;
12939
12940   /* See if we're looking at the `enum' keyword.  */
12941   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
12942     {
12943       /* Consume the `enum' token.  */
12944       cp_lexer_consume_token (parser->lexer);
12945       /* Remember that it's an enumeration type.  */
12946       tag_type = enum_type;
12947       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
12948          enums) is used here.  */
12949       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
12950           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
12951         {
12952             pedwarn (input_location, 0, "elaborated-type-specifier "
12953                       "for a scoped enum must not use the %<%D%> keyword",
12954                       cp_lexer_peek_token (parser->lexer)->u.value);
12955           /* Consume the `struct' or `class' and parse it anyway.  */
12956           cp_lexer_consume_token (parser->lexer);
12957         }
12958       /* Parse the attributes.  */
12959       attributes = cp_parser_attributes_opt (parser);
12960     }
12961   /* Or, it might be `typename'.  */
12962   else if (cp_lexer_next_token_is_keyword (parser->lexer,
12963                                            RID_TYPENAME))
12964     {
12965       /* Consume the `typename' token.  */
12966       cp_lexer_consume_token (parser->lexer);
12967       /* Remember that it's a `typename' type.  */
12968       tag_type = typename_type;
12969     }
12970   /* Otherwise it must be a class-key.  */
12971   else
12972     {
12973       tag_type = cp_parser_class_key (parser);
12974       if (tag_type == none_type)
12975         return error_mark_node;
12976       /* Parse the attributes.  */
12977       attributes = cp_parser_attributes_opt (parser);
12978     }
12979
12980   /* Look for the `::' operator.  */
12981   globalscope =  cp_parser_global_scope_opt (parser,
12982                                              /*current_scope_valid_p=*/false);
12983   /* Look for the nested-name-specifier.  */
12984   if (tag_type == typename_type && !globalscope)
12985     {
12986       if (!cp_parser_nested_name_specifier (parser,
12987                                            /*typename_keyword_p=*/true,
12988                                            /*check_dependency_p=*/true,
12989                                            /*type_p=*/true,
12990                                             is_declaration))
12991         return error_mark_node;
12992     }
12993   else
12994     /* Even though `typename' is not present, the proposed resolution
12995        to Core Issue 180 says that in `class A<T>::B', `B' should be
12996        considered a type-name, even if `A<T>' is dependent.  */
12997     cp_parser_nested_name_specifier_opt (parser,
12998                                          /*typename_keyword_p=*/true,
12999                                          /*check_dependency_p=*/true,
13000                                          /*type_p=*/true,
13001                                          is_declaration);
13002  /* For everything but enumeration types, consider a template-id.
13003     For an enumeration type, consider only a plain identifier.  */
13004   if (tag_type != enum_type)
13005     {
13006       bool template_p = false;
13007       tree decl;
13008
13009       /* Allow the `template' keyword.  */
13010       template_p = cp_parser_optional_template_keyword (parser);
13011       /* If we didn't see `template', we don't know if there's a
13012          template-id or not.  */
13013       if (!template_p)
13014         cp_parser_parse_tentatively (parser);
13015       /* Parse the template-id.  */
13016       token = cp_lexer_peek_token (parser->lexer);
13017       decl = cp_parser_template_id (parser, template_p,
13018                                     /*check_dependency_p=*/true,
13019                                     is_declaration);
13020       /* If we didn't find a template-id, look for an ordinary
13021          identifier.  */
13022       if (!template_p && !cp_parser_parse_definitely (parser))
13023         ;
13024       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13025          in effect, then we must assume that, upon instantiation, the
13026          template will correspond to a class.  */
13027       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13028                && tag_type == typename_type)
13029         type = make_typename_type (parser->scope, decl,
13030                                    typename_type,
13031                                    /*complain=*/tf_error);
13032       /* If the `typename' keyword is in effect and DECL is not a type
13033          decl. Then type is non existant.   */
13034       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13035         type = NULL_TREE; 
13036       else 
13037         type = TREE_TYPE (decl);
13038     }
13039
13040   if (!type)
13041     {
13042       token = cp_lexer_peek_token (parser->lexer);
13043       identifier = cp_parser_identifier (parser);
13044
13045       if (identifier == error_mark_node)
13046         {
13047           parser->scope = NULL_TREE;
13048           return error_mark_node;
13049         }
13050
13051       /* For a `typename', we needn't call xref_tag.  */
13052       if (tag_type == typename_type
13053           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13054         return cp_parser_make_typename_type (parser, parser->scope,
13055                                              identifier,
13056                                              token->location);
13057       /* Look up a qualified name in the usual way.  */
13058       if (parser->scope)
13059         {
13060           tree decl;
13061           tree ambiguous_decls;
13062
13063           decl = cp_parser_lookup_name (parser, identifier,
13064                                         tag_type,
13065                                         /*is_template=*/false,
13066                                         /*is_namespace=*/false,
13067                                         /*check_dependency=*/true,
13068                                         &ambiguous_decls,
13069                                         token->location);
13070
13071           /* If the lookup was ambiguous, an error will already have been
13072              issued.  */
13073           if (ambiguous_decls)
13074             return error_mark_node;
13075
13076           /* If we are parsing friend declaration, DECL may be a
13077              TEMPLATE_DECL tree node here.  However, we need to check
13078              whether this TEMPLATE_DECL results in valid code.  Consider
13079              the following example:
13080
13081                namespace N {
13082                  template <class T> class C {};
13083                }
13084                class X {
13085                  template <class T> friend class N::C; // #1, valid code
13086                };
13087                template <class T> class Y {
13088                  friend class N::C;                    // #2, invalid code
13089                };
13090
13091              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13092              name lookup of `N::C'.  We see that friend declaration must
13093              be template for the code to be valid.  Note that
13094              processing_template_decl does not work here since it is
13095              always 1 for the above two cases.  */
13096
13097           decl = (cp_parser_maybe_treat_template_as_class
13098                   (decl, /*tag_name_p=*/is_friend
13099                          && parser->num_template_parameter_lists));
13100
13101           if (TREE_CODE (decl) != TYPE_DECL)
13102             {
13103               cp_parser_diagnose_invalid_type_name (parser,
13104                                                     parser->scope,
13105                                                     identifier,
13106                                                     token->location);
13107               return error_mark_node;
13108             }
13109
13110           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13111             {
13112               bool allow_template = (parser->num_template_parameter_lists
13113                                       || DECL_SELF_REFERENCE_P (decl));
13114               type = check_elaborated_type_specifier (tag_type, decl, 
13115                                                       allow_template);
13116
13117               if (type == error_mark_node)
13118                 return error_mark_node;
13119             }
13120
13121           /* Forward declarations of nested types, such as
13122
13123                class C1::C2;
13124                class C1::C2::C3;
13125
13126              are invalid unless all components preceding the final '::'
13127              are complete.  If all enclosing types are complete, these
13128              declarations become merely pointless.
13129
13130              Invalid forward declarations of nested types are errors
13131              caught elsewhere in parsing.  Those that are pointless arrive
13132              here.  */
13133
13134           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13135               && !is_friend && !processing_explicit_instantiation)
13136             warning (0, "declaration %qD does not declare anything", decl);
13137
13138           type = TREE_TYPE (decl);
13139         }
13140       else
13141         {
13142           /* An elaborated-type-specifier sometimes introduces a new type and
13143              sometimes names an existing type.  Normally, the rule is that it
13144              introduces a new type only if there is not an existing type of
13145              the same name already in scope.  For example, given:
13146
13147                struct S {};
13148                void f() { struct S s; }
13149
13150              the `struct S' in the body of `f' is the same `struct S' as in
13151              the global scope; the existing definition is used.  However, if
13152              there were no global declaration, this would introduce a new
13153              local class named `S'.
13154
13155              An exception to this rule applies to the following code:
13156
13157                namespace N { struct S; }
13158
13159              Here, the elaborated-type-specifier names a new type
13160              unconditionally; even if there is already an `S' in the
13161              containing scope this declaration names a new type.
13162              This exception only applies if the elaborated-type-specifier
13163              forms the complete declaration:
13164
13165                [class.name]
13166
13167                A declaration consisting solely of `class-key identifier ;' is
13168                either a redeclaration of the name in the current scope or a
13169                forward declaration of the identifier as a class name.  It
13170                introduces the name into the current scope.
13171
13172              We are in this situation precisely when the next token is a `;'.
13173
13174              An exception to the exception is that a `friend' declaration does
13175              *not* name a new type; i.e., given:
13176
13177                struct S { friend struct T; };
13178
13179              `T' is not a new type in the scope of `S'.
13180
13181              Also, `new struct S' or `sizeof (struct S)' never results in the
13182              definition of a new type; a new type can only be declared in a
13183              declaration context.  */
13184
13185           tag_scope ts;
13186           bool template_p;
13187
13188           if (is_friend)
13189             /* Friends have special name lookup rules.  */
13190             ts = ts_within_enclosing_non_class;
13191           else if (is_declaration
13192                    && cp_lexer_next_token_is (parser->lexer,
13193                                               CPP_SEMICOLON))
13194             /* This is a `class-key identifier ;' */
13195             ts = ts_current;
13196           else
13197             ts = ts_global;
13198
13199           template_p =
13200             (parser->num_template_parameter_lists
13201              && (cp_parser_next_token_starts_class_definition_p (parser)
13202                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
13203           /* An unqualified name was used to reference this type, so
13204              there were no qualifying templates.  */
13205           if (!cp_parser_check_template_parameters (parser,
13206                                                     /*num_templates=*/0,
13207                                                     token->location,
13208                                                     /*declarator=*/NULL))
13209             return error_mark_node;
13210           type = xref_tag (tag_type, identifier, ts, template_p);
13211         }
13212     }
13213
13214   if (type == error_mark_node)
13215     return error_mark_node;
13216
13217   /* Allow attributes on forward declarations of classes.  */
13218   if (attributes)
13219     {
13220       if (TREE_CODE (type) == TYPENAME_TYPE)
13221         warning (OPT_Wattributes,
13222                  "attributes ignored on uninstantiated type");
13223       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
13224                && ! processing_explicit_instantiation)
13225         warning (OPT_Wattributes,
13226                  "attributes ignored on template instantiation");
13227       else if (is_declaration && cp_parser_declares_only_class_p (parser))
13228         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
13229       else
13230         warning (OPT_Wattributes,
13231                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
13232     }
13233
13234   if (tag_type != enum_type)
13235     cp_parser_check_class_key (tag_type, type);
13236
13237   /* A "<" cannot follow an elaborated type specifier.  If that
13238      happens, the user was probably trying to form a template-id.  */
13239   cp_parser_check_for_invalid_template_id (parser, type, token->location);
13240
13241   return type;
13242 }
13243
13244 /* Parse an enum-specifier.
13245
13246    enum-specifier:
13247      enum-head { enumerator-list [opt] }
13248
13249    enum-head:
13250      enum-key identifier [opt] enum-base [opt]
13251      enum-key nested-name-specifier identifier enum-base [opt]
13252
13253    enum-key:
13254      enum
13255      enum class   [C++0x]
13256      enum struct  [C++0x]
13257
13258    enum-base:   [C++0x]
13259      : type-specifier-seq
13260
13261    opaque-enum-specifier:
13262      enum-key identifier enum-base [opt] ;
13263
13264    GNU Extensions:
13265      enum-key attributes[opt] identifier [opt] enum-base [opt] 
13266        { enumerator-list [opt] }attributes[opt]
13267
13268    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
13269    if the token stream isn't an enum-specifier after all.  */
13270
13271 static tree
13272 cp_parser_enum_specifier (cp_parser* parser)
13273 {
13274   tree identifier;
13275   tree type = NULL_TREE;
13276   tree prev_scope;
13277   tree nested_name_specifier = NULL_TREE;
13278   tree attributes;
13279   bool scoped_enum_p = false;
13280   bool has_underlying_type = false;
13281   bool nested_being_defined = false;
13282   bool new_value_list = false;
13283   bool is_new_type = false;
13284   bool is_anonymous = false;
13285   tree underlying_type = NULL_TREE;
13286   cp_token *type_start_token = NULL;
13287   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
13288
13289   parser->colon_corrects_to_scope_p = false;
13290
13291   /* Parse tentatively so that we can back up if we don't find a
13292      enum-specifier.  */
13293   cp_parser_parse_tentatively (parser);
13294
13295   /* Caller guarantees that the current token is 'enum', an identifier
13296      possibly follows, and the token after that is an opening brace.
13297      If we don't have an identifier, fabricate an anonymous name for
13298      the enumeration being defined.  */
13299   cp_lexer_consume_token (parser->lexer);
13300
13301   /* Parse the "class" or "struct", which indicates a scoped
13302      enumeration type in C++0x.  */
13303   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13304       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13305     {
13306       if (cxx_dialect < cxx0x)
13307         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13308
13309       /* Consume the `struct' or `class' token.  */
13310       cp_lexer_consume_token (parser->lexer);
13311
13312       scoped_enum_p = true;
13313     }
13314
13315   attributes = cp_parser_attributes_opt (parser);
13316
13317   /* Clear the qualification.  */
13318   parser->scope = NULL_TREE;
13319   parser->qualifying_scope = NULL_TREE;
13320   parser->object_scope = NULL_TREE;
13321
13322   /* Figure out in what scope the declaration is being placed.  */
13323   prev_scope = current_scope ();
13324
13325   type_start_token = cp_lexer_peek_token (parser->lexer);
13326
13327   push_deferring_access_checks (dk_no_check);
13328   nested_name_specifier
13329       = cp_parser_nested_name_specifier_opt (parser,
13330                                              /*typename_keyword_p=*/true,
13331                                              /*check_dependency_p=*/false,
13332                                              /*type_p=*/false,
13333                                              /*is_declaration=*/false);
13334
13335   if (nested_name_specifier)
13336     {
13337       tree name;
13338
13339       identifier = cp_parser_identifier (parser);
13340       name =  cp_parser_lookup_name (parser, identifier,
13341                                      enum_type,
13342                                      /*is_template=*/false,
13343                                      /*is_namespace=*/false,
13344                                      /*check_dependency=*/true,
13345                                      /*ambiguous_decls=*/NULL,
13346                                      input_location);
13347       if (name)
13348         {
13349           type = TREE_TYPE (name);
13350           if (TREE_CODE (type) == TYPENAME_TYPE)
13351             {
13352               /* Are template enums allowed in ISO? */
13353               if (template_parm_scope_p ())
13354                 pedwarn (type_start_token->location, OPT_pedantic,
13355                          "%qD is an enumeration template", name);
13356               /* ignore a typename reference, for it will be solved by name
13357                  in start_enum.  */
13358               type = NULL_TREE;
13359             }
13360         }
13361       else
13362         error_at (type_start_token->location,
13363                   "%qD is not an enumerator-name", identifier);
13364     }
13365   else
13366     {
13367       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13368         identifier = cp_parser_identifier (parser);
13369       else
13370         {
13371           identifier = make_anon_name ();
13372           is_anonymous = true;
13373         }
13374     }
13375   pop_deferring_access_checks ();
13376
13377   /* Check for the `:' that denotes a specified underlying type in C++0x.
13378      Note that a ':' could also indicate a bitfield width, however.  */
13379   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
13380     {
13381       cp_decl_specifier_seq type_specifiers;
13382
13383       /* Consume the `:'.  */
13384       cp_lexer_consume_token (parser->lexer);
13385
13386       /* Parse the type-specifier-seq.  */
13387       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
13388                                     /*is_trailing_return=*/false,
13389                                     &type_specifiers);
13390
13391       /* At this point this is surely not elaborated type specifier.  */
13392       if (!cp_parser_parse_definitely (parser))
13393         return NULL_TREE;
13394
13395       if (cxx_dialect < cxx0x)
13396         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
13397
13398       has_underlying_type = true;
13399
13400       /* If that didn't work, stop.  */
13401       if (type_specifiers.type != error_mark_node)
13402         {
13403           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
13404                                             /*initialized=*/0, NULL);
13405           if (underlying_type == error_mark_node)
13406             underlying_type = NULL_TREE;
13407         }
13408     }
13409
13410   /* Look for the `{' but don't consume it yet.  */
13411   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13412     {
13413       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
13414         {
13415           cp_parser_error (parser, "expected %<{%>");
13416           if (has_underlying_type)
13417             {
13418               type = NULL_TREE;
13419               goto out;
13420             }
13421         }
13422       /* An opaque-enum-specifier must have a ';' here.  */
13423       if ((scoped_enum_p || underlying_type)
13424           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13425         {
13426           cp_parser_error (parser, "expected %<;%> or %<{%>");
13427           if (has_underlying_type)
13428             {
13429               type = NULL_TREE;
13430               goto out;
13431             }
13432         }
13433     }
13434
13435   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
13436     return NULL_TREE;
13437
13438   if (nested_name_specifier)
13439     {
13440       if (CLASS_TYPE_P (nested_name_specifier))
13441         {
13442           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
13443           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
13444           push_scope (nested_name_specifier);
13445         }
13446       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13447         {
13448           push_nested_namespace (nested_name_specifier);
13449         }
13450     }
13451
13452   /* Issue an error message if type-definitions are forbidden here.  */
13453   if (!cp_parser_check_type_definition (parser))
13454     type = error_mark_node;
13455   else
13456     /* Create the new type.  We do this before consuming the opening
13457        brace so the enum will be recorded as being on the line of its
13458        tag (or the 'enum' keyword, if there is no tag).  */
13459     type = start_enum (identifier, type, underlying_type,
13460                        scoped_enum_p, &is_new_type);
13461
13462   /* If the next token is not '{' it is an opaque-enum-specifier or an
13463      elaborated-type-specifier.  */
13464   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
13465     {
13466       timevar_push (TV_PARSE_ENUM);
13467       if (nested_name_specifier)
13468         {
13469           /* The following catches invalid code such as:
13470              enum class S<int>::E { A, B, C }; */
13471           if (!processing_specialization
13472               && CLASS_TYPE_P (nested_name_specifier)
13473               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
13474             error_at (type_start_token->location, "cannot add an enumerator "
13475                       "list to a template instantiation");
13476
13477           /* If that scope does not contain the scope in which the
13478              class was originally declared, the program is invalid.  */
13479           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
13480             {
13481               if (at_namespace_scope_p ())
13482                 error_at (type_start_token->location,
13483                           "declaration of %qD in namespace %qD which does not "
13484                           "enclose %qD",
13485                           type, prev_scope, nested_name_specifier);
13486               else
13487                 error_at (type_start_token->location,
13488                           "declaration of %qD in %qD which does not enclose %qD",
13489                           type, prev_scope, nested_name_specifier);
13490               type = error_mark_node;
13491             }
13492         }
13493
13494       if (scoped_enum_p)
13495         begin_scope (sk_scoped_enum, type);
13496
13497       /* Consume the opening brace.  */
13498       cp_lexer_consume_token (parser->lexer);
13499
13500       if (type == error_mark_node)
13501         ; /* Nothing to add */
13502       else if (OPAQUE_ENUM_P (type)
13503                || (cxx_dialect > cxx98 && processing_specialization))
13504         {
13505           new_value_list = true;
13506           SET_OPAQUE_ENUM_P (type, false);
13507           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
13508         }
13509       else
13510         {
13511           error_at (type_start_token->location, "multiple definition of %q#T", type);
13512           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
13513                     "previous definition here");
13514           type = error_mark_node;
13515         }
13516
13517       if (type == error_mark_node)
13518         cp_parser_skip_to_end_of_block_or_statement (parser);
13519       /* If the next token is not '}', then there are some enumerators.  */
13520       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
13521         cp_parser_enumerator_list (parser, type);
13522
13523       /* Consume the final '}'.  */
13524       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13525
13526       if (scoped_enum_p)
13527         finish_scope ();
13528       timevar_pop (TV_PARSE_ENUM);
13529     }
13530   else
13531     {
13532       /* If a ';' follows, then it is an opaque-enum-specifier
13533         and additional restrictions apply.  */
13534       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
13535         {
13536           if (is_anonymous)
13537             error_at (type_start_token->location,
13538                       "opaque-enum-specifier without name");
13539           else if (nested_name_specifier)
13540             error_at (type_start_token->location,
13541                       "opaque-enum-specifier must use a simple identifier");
13542         }
13543     }
13544
13545   /* Look for trailing attributes to apply to this enumeration, and
13546      apply them if appropriate.  */
13547   if (cp_parser_allow_gnu_extensions_p (parser))
13548     {
13549       tree trailing_attr = cp_parser_attributes_opt (parser);
13550       trailing_attr = chainon (trailing_attr, attributes);
13551       cplus_decl_attributes (&type,
13552                              trailing_attr,
13553                              (int) ATTR_FLAG_TYPE_IN_PLACE);
13554     }
13555
13556   /* Finish up the enumeration.  */
13557   if (type != error_mark_node)
13558     {
13559       if (new_value_list)
13560         finish_enum_value_list (type);
13561       if (is_new_type)
13562         finish_enum (type);
13563     }
13564
13565   if (nested_name_specifier)
13566     {
13567       if (CLASS_TYPE_P (nested_name_specifier))
13568         {
13569           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
13570           pop_scope (nested_name_specifier);
13571         }
13572       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
13573         {
13574           pop_nested_namespace (nested_name_specifier);
13575         }
13576     }
13577  out:
13578   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
13579   return type;
13580 }
13581
13582 /* Parse an enumerator-list.  The enumerators all have the indicated
13583    TYPE.
13584
13585    enumerator-list:
13586      enumerator-definition
13587      enumerator-list , enumerator-definition  */
13588
13589 static void
13590 cp_parser_enumerator_list (cp_parser* parser, tree type)
13591 {
13592   while (true)
13593     {
13594       /* Parse an enumerator-definition.  */
13595       cp_parser_enumerator_definition (parser, type);
13596
13597       /* If the next token is not a ',', we've reached the end of
13598          the list.  */
13599       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
13600         break;
13601       /* Otherwise, consume the `,' and keep going.  */
13602       cp_lexer_consume_token (parser->lexer);
13603       /* If the next token is a `}', there is a trailing comma.  */
13604       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
13605         {
13606           if (!in_system_header)
13607             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
13608           break;
13609         }
13610     }
13611 }
13612
13613 /* Parse an enumerator-definition.  The enumerator has the indicated
13614    TYPE.
13615
13616    enumerator-definition:
13617      enumerator
13618      enumerator = constant-expression
13619
13620    enumerator:
13621      identifier  */
13622
13623 static void
13624 cp_parser_enumerator_definition (cp_parser* parser, tree type)
13625 {
13626   tree identifier;
13627   tree value;
13628   location_t loc;
13629
13630   /* Save the input location because we are interested in the location
13631      of the identifier and not the location of the explicit value.  */
13632   loc = cp_lexer_peek_token (parser->lexer)->location;
13633
13634   /* Look for the identifier.  */
13635   identifier = cp_parser_identifier (parser);
13636   if (identifier == error_mark_node)
13637     return;
13638
13639   /* If the next token is an '=', then there is an explicit value.  */
13640   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
13641     {
13642       /* Consume the `=' token.  */
13643       cp_lexer_consume_token (parser->lexer);
13644       /* Parse the value.  */
13645       value = cp_parser_constant_expression (parser,
13646                                              /*allow_non_constant_p=*/false,
13647                                              NULL);
13648     }
13649   else
13650     value = NULL_TREE;
13651
13652   /* If we are processing a template, make sure the initializer of the
13653      enumerator doesn't contain any bare template parameter pack.  */
13654   if (check_for_bare_parameter_packs (value))
13655     value = error_mark_node;
13656
13657   /* integral_constant_value will pull out this expression, so make sure
13658      it's folded as appropriate.  */
13659   value = fold_non_dependent_expr (value);
13660
13661   /* Create the enumerator.  */
13662   build_enumerator (identifier, value, type, loc);
13663 }
13664
13665 /* Parse a namespace-name.
13666
13667    namespace-name:
13668      original-namespace-name
13669      namespace-alias
13670
13671    Returns the NAMESPACE_DECL for the namespace.  */
13672
13673 static tree
13674 cp_parser_namespace_name (cp_parser* parser)
13675 {
13676   tree identifier;
13677   tree namespace_decl;
13678
13679   cp_token *token = cp_lexer_peek_token (parser->lexer);
13680
13681   /* Get the name of the namespace.  */
13682   identifier = cp_parser_identifier (parser);
13683   if (identifier == error_mark_node)
13684     return error_mark_node;
13685
13686   /* Look up the identifier in the currently active scope.  Look only
13687      for namespaces, due to:
13688
13689        [basic.lookup.udir]
13690
13691        When looking up a namespace-name in a using-directive or alias
13692        definition, only namespace names are considered.
13693
13694      And:
13695
13696        [basic.lookup.qual]
13697
13698        During the lookup of a name preceding the :: scope resolution
13699        operator, object, function, and enumerator names are ignored.
13700
13701      (Note that cp_parser_qualifying_entity only calls this
13702      function if the token after the name is the scope resolution
13703      operator.)  */
13704   namespace_decl = cp_parser_lookup_name (parser, identifier,
13705                                           none_type,
13706                                           /*is_template=*/false,
13707                                           /*is_namespace=*/true,
13708                                           /*check_dependency=*/true,
13709                                           /*ambiguous_decls=*/NULL,
13710                                           token->location);
13711   /* If it's not a namespace, issue an error.  */
13712   if (namespace_decl == error_mark_node
13713       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
13714     {
13715       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
13716         error_at (token->location, "%qD is not a namespace-name", identifier);
13717       cp_parser_error (parser, "expected namespace-name");
13718       namespace_decl = error_mark_node;
13719     }
13720
13721   return namespace_decl;
13722 }
13723
13724 /* Parse a namespace-definition.
13725
13726    namespace-definition:
13727      named-namespace-definition
13728      unnamed-namespace-definition
13729
13730    named-namespace-definition:
13731      original-namespace-definition
13732      extension-namespace-definition
13733
13734    original-namespace-definition:
13735      namespace identifier { namespace-body }
13736
13737    extension-namespace-definition:
13738      namespace original-namespace-name { namespace-body }
13739
13740    unnamed-namespace-definition:
13741      namespace { namespace-body } */
13742
13743 static void
13744 cp_parser_namespace_definition (cp_parser* parser)
13745 {
13746   tree identifier, attribs;
13747   bool has_visibility;
13748   bool is_inline;
13749
13750   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
13751     {
13752       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
13753       is_inline = true;
13754       cp_lexer_consume_token (parser->lexer);
13755     }
13756   else
13757     is_inline = false;
13758
13759   /* Look for the `namespace' keyword.  */
13760   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13761
13762   /* Get the name of the namespace.  We do not attempt to distinguish
13763      between an original-namespace-definition and an
13764      extension-namespace-definition at this point.  The semantic
13765      analysis routines are responsible for that.  */
13766   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
13767     identifier = cp_parser_identifier (parser);
13768   else
13769     identifier = NULL_TREE;
13770
13771   /* Parse any specified attributes.  */
13772   attribs = cp_parser_attributes_opt (parser);
13773
13774   /* Look for the `{' to start the namespace.  */
13775   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
13776   /* Start the namespace.  */
13777   push_namespace (identifier);
13778
13779   /* "inline namespace" is equivalent to a stub namespace definition
13780      followed by a strong using directive.  */
13781   if (is_inline)
13782     {
13783       tree name_space = current_namespace;
13784       /* Set up namespace association.  */
13785       DECL_NAMESPACE_ASSOCIATIONS (name_space)
13786         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
13787                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
13788       /* Import the contents of the inline namespace.  */
13789       pop_namespace ();
13790       do_using_directive (name_space);
13791       push_namespace (identifier);
13792     }
13793
13794   has_visibility = handle_namespace_attrs (current_namespace, attribs);
13795
13796   /* Parse the body of the namespace.  */
13797   cp_parser_namespace_body (parser);
13798
13799   if (has_visibility)
13800     pop_visibility (1);
13801
13802   /* Finish the namespace.  */
13803   pop_namespace ();
13804   /* Look for the final `}'.  */
13805   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
13806 }
13807
13808 /* Parse a namespace-body.
13809
13810    namespace-body:
13811      declaration-seq [opt]  */
13812
13813 static void
13814 cp_parser_namespace_body (cp_parser* parser)
13815 {
13816   cp_parser_declaration_seq_opt (parser);
13817 }
13818
13819 /* Parse a namespace-alias-definition.
13820
13821    namespace-alias-definition:
13822      namespace identifier = qualified-namespace-specifier ;  */
13823
13824 static void
13825 cp_parser_namespace_alias_definition (cp_parser* parser)
13826 {
13827   tree identifier;
13828   tree namespace_specifier;
13829
13830   cp_token *token = cp_lexer_peek_token (parser->lexer);
13831
13832   /* Look for the `namespace' keyword.  */
13833   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
13834   /* Look for the identifier.  */
13835   identifier = cp_parser_identifier (parser);
13836   if (identifier == error_mark_node)
13837     return;
13838   /* Look for the `=' token.  */
13839   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
13840       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
13841     {
13842       error_at (token->location, "%<namespace%> definition is not allowed here");
13843       /* Skip the definition.  */
13844       cp_lexer_consume_token (parser->lexer);
13845       if (cp_parser_skip_to_closing_brace (parser))
13846         cp_lexer_consume_token (parser->lexer);
13847       return;
13848     }
13849   cp_parser_require (parser, CPP_EQ, RT_EQ);
13850   /* Look for the qualified-namespace-specifier.  */
13851   namespace_specifier
13852     = cp_parser_qualified_namespace_specifier (parser);
13853   /* Look for the `;' token.  */
13854   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
13855
13856   /* Register the alias in the symbol table.  */
13857   do_namespace_alias (identifier, namespace_specifier);
13858 }
13859
13860 /* Parse a qualified-namespace-specifier.
13861
13862    qualified-namespace-specifier:
13863      :: [opt] nested-name-specifier [opt] namespace-name
13864
13865    Returns a NAMESPACE_DECL corresponding to the specified
13866    namespace.  */
13867
13868 static tree
13869 cp_parser_qualified_namespace_specifier (cp_parser* parser)
13870 {
13871   /* Look for the optional `::'.  */
13872   cp_parser_global_scope_opt (parser,
13873                               /*current_scope_valid_p=*/false);
13874
13875   /* Look for the optional nested-name-specifier.  */
13876   cp_parser_nested_name_specifier_opt (parser,
13877                                        /*typename_keyword_p=*/false,
13878                                        /*check_dependency_p=*/true,
13879                                        /*type_p=*/false,
13880                                        /*is_declaration=*/true);
13881
13882   return cp_parser_namespace_name (parser);
13883 }
13884
13885 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
13886    access declaration.
13887
13888    using-declaration:
13889      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
13890      using :: unqualified-id ;  
13891
13892    access-declaration:
13893      qualified-id ;  
13894
13895    */
13896
13897 static bool
13898 cp_parser_using_declaration (cp_parser* parser, 
13899                              bool access_declaration_p)
13900 {
13901   cp_token *token;
13902   bool typename_p = false;
13903   bool global_scope_p;
13904   tree decl;
13905   tree identifier;
13906   tree qscope;
13907
13908   if (access_declaration_p)
13909     cp_parser_parse_tentatively (parser);
13910   else
13911     {
13912       /* Look for the `using' keyword.  */
13913       cp_parser_require_keyword (parser, RID_USING, RT_USING);
13914       
13915       /* Peek at the next token.  */
13916       token = cp_lexer_peek_token (parser->lexer);
13917       /* See if it's `typename'.  */
13918       if (token->keyword == RID_TYPENAME)
13919         {
13920           /* Remember that we've seen it.  */
13921           typename_p = true;
13922           /* Consume the `typename' token.  */
13923           cp_lexer_consume_token (parser->lexer);
13924         }
13925     }
13926
13927   /* Look for the optional global scope qualification.  */
13928   global_scope_p
13929     = (cp_parser_global_scope_opt (parser,
13930                                    /*current_scope_valid_p=*/false)
13931        != NULL_TREE);
13932
13933   /* If we saw `typename', or didn't see `::', then there must be a
13934      nested-name-specifier present.  */
13935   if (typename_p || !global_scope_p)
13936     qscope = cp_parser_nested_name_specifier (parser, typename_p,
13937                                               /*check_dependency_p=*/true,
13938                                               /*type_p=*/false,
13939                                               /*is_declaration=*/true);
13940   /* Otherwise, we could be in either of the two productions.  In that
13941      case, treat the nested-name-specifier as optional.  */
13942   else
13943     qscope = cp_parser_nested_name_specifier_opt (parser,
13944                                                   /*typename_keyword_p=*/false,
13945                                                   /*check_dependency_p=*/true,
13946                                                   /*type_p=*/false,
13947                                                   /*is_declaration=*/true);
13948   if (!qscope)
13949     qscope = global_namespace;
13950
13951   if (access_declaration_p && cp_parser_error_occurred (parser))
13952     /* Something has already gone wrong; there's no need to parse
13953        further.  Since an error has occurred, the return value of
13954        cp_parser_parse_definitely will be false, as required.  */
13955     return cp_parser_parse_definitely (parser);
13956
13957   token = cp_lexer_peek_token (parser->lexer);
13958   /* Parse the unqualified-id.  */
13959   identifier = cp_parser_unqualified_id (parser,
13960                                          /*template_keyword_p=*/false,
13961                                          /*check_dependency_p=*/true,
13962                                          /*declarator_p=*/true,
13963                                          /*optional_p=*/false);
13964
13965   if (access_declaration_p)
13966     {
13967       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
13968         cp_parser_simulate_error (parser);
13969       if (!cp_parser_parse_definitely (parser))
13970         return false;
13971     }
13972
13973   /* The function we call to handle a using-declaration is different
13974      depending on what scope we are in.  */
13975   if (qscope == error_mark_node || identifier == error_mark_node)
13976     ;
13977   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
13978            && TREE_CODE (identifier) != BIT_NOT_EXPR)
13979     /* [namespace.udecl]
13980
13981        A using declaration shall not name a template-id.  */
13982     error_at (token->location,
13983               "a template-id may not appear in a using-declaration");
13984   else
13985     {
13986       if (at_class_scope_p ())
13987         {
13988           /* Create the USING_DECL.  */
13989           decl = do_class_using_decl (parser->scope, identifier);
13990
13991           if (check_for_bare_parameter_packs (decl))
13992             return false;
13993           else
13994             /* Add it to the list of members in this class.  */
13995             finish_member_declaration (decl);
13996         }
13997       else
13998         {
13999           decl = cp_parser_lookup_name_simple (parser,
14000                                                identifier,
14001                                                token->location);
14002           if (decl == error_mark_node)
14003             cp_parser_name_lookup_error (parser, identifier,
14004                                          decl, NLE_NULL,
14005                                          token->location);
14006           else if (check_for_bare_parameter_packs (decl))
14007             return false;
14008           else if (!at_namespace_scope_p ())
14009             do_local_using_decl (decl, qscope, identifier);
14010           else
14011             do_toplevel_using_decl (decl, qscope, identifier);
14012         }
14013     }
14014
14015   /* Look for the final `;'.  */
14016   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14017   
14018   return true;
14019 }
14020
14021 /* Parse a using-directive.
14022
14023    using-directive:
14024      using namespace :: [opt] nested-name-specifier [opt]
14025        namespace-name ;  */
14026
14027 static void
14028 cp_parser_using_directive (cp_parser* parser)
14029 {
14030   tree namespace_decl;
14031   tree attribs;
14032
14033   /* Look for the `using' keyword.  */
14034   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14035   /* And the `namespace' keyword.  */
14036   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14037   /* Look for the optional `::' operator.  */
14038   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14039   /* And the optional nested-name-specifier.  */
14040   cp_parser_nested_name_specifier_opt (parser,
14041                                        /*typename_keyword_p=*/false,
14042                                        /*check_dependency_p=*/true,
14043                                        /*type_p=*/false,
14044                                        /*is_declaration=*/true);
14045   /* Get the namespace being used.  */
14046   namespace_decl = cp_parser_namespace_name (parser);
14047   /* And any specified attributes.  */
14048   attribs = cp_parser_attributes_opt (parser);
14049   /* Update the symbol table.  */
14050   parse_using_directive (namespace_decl, attribs);
14051   /* Look for the final `;'.  */
14052   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14053 }
14054
14055 /* Parse an asm-definition.
14056
14057    asm-definition:
14058      asm ( string-literal ) ;
14059
14060    GNU Extension:
14061
14062    asm-definition:
14063      asm volatile [opt] ( string-literal ) ;
14064      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14065      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14066                           : asm-operand-list [opt] ) ;
14067      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14068                           : asm-operand-list [opt]
14069                           : asm-clobber-list [opt] ) ;
14070      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14071                                : asm-clobber-list [opt]
14072                                : asm-goto-list ) ;  */
14073
14074 static void
14075 cp_parser_asm_definition (cp_parser* parser)
14076 {
14077   tree string;
14078   tree outputs = NULL_TREE;
14079   tree inputs = NULL_TREE;
14080   tree clobbers = NULL_TREE;
14081   tree labels = NULL_TREE;
14082   tree asm_stmt;
14083   bool volatile_p = false;
14084   bool extended_p = false;
14085   bool invalid_inputs_p = false;
14086   bool invalid_outputs_p = false;
14087   bool goto_p = false;
14088   required_token missing = RT_NONE;
14089
14090   /* Look for the `asm' keyword.  */
14091   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14092   /* See if the next token is `volatile'.  */
14093   if (cp_parser_allow_gnu_extensions_p (parser)
14094       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14095     {
14096       /* Remember that we saw the `volatile' keyword.  */
14097       volatile_p = true;
14098       /* Consume the token.  */
14099       cp_lexer_consume_token (parser->lexer);
14100     }
14101   if (cp_parser_allow_gnu_extensions_p (parser)
14102       && parser->in_function_body
14103       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14104     {
14105       /* Remember that we saw the `goto' keyword.  */
14106       goto_p = true;
14107       /* Consume the token.  */
14108       cp_lexer_consume_token (parser->lexer);
14109     }
14110   /* Look for the opening `('.  */
14111   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14112     return;
14113   /* Look for the string.  */
14114   string = cp_parser_string_literal (parser, false, false);
14115   if (string == error_mark_node)
14116     {
14117       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14118                                              /*consume_paren=*/true);
14119       return;
14120     }
14121
14122   /* If we're allowing GNU extensions, check for the extended assembly
14123      syntax.  Unfortunately, the `:' tokens need not be separated by
14124      a space in C, and so, for compatibility, we tolerate that here
14125      too.  Doing that means that we have to treat the `::' operator as
14126      two `:' tokens.  */
14127   if (cp_parser_allow_gnu_extensions_p (parser)
14128       && parser->in_function_body
14129       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14130           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14131     {
14132       bool inputs_p = false;
14133       bool clobbers_p = false;
14134       bool labels_p = false;
14135
14136       /* The extended syntax was used.  */
14137       extended_p = true;
14138
14139       /* Look for outputs.  */
14140       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14141         {
14142           /* Consume the `:'.  */
14143           cp_lexer_consume_token (parser->lexer);
14144           /* Parse the output-operands.  */
14145           if (cp_lexer_next_token_is_not (parser->lexer,
14146                                           CPP_COLON)
14147               && cp_lexer_next_token_is_not (parser->lexer,
14148                                              CPP_SCOPE)
14149               && cp_lexer_next_token_is_not (parser->lexer,
14150                                              CPP_CLOSE_PAREN)
14151               && !goto_p)
14152             outputs = cp_parser_asm_operand_list (parser);
14153
14154             if (outputs == error_mark_node)
14155               invalid_outputs_p = true;
14156         }
14157       /* If the next token is `::', there are no outputs, and the
14158          next token is the beginning of the inputs.  */
14159       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14160         /* The inputs are coming next.  */
14161         inputs_p = true;
14162
14163       /* Look for inputs.  */
14164       if (inputs_p
14165           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14166         {
14167           /* Consume the `:' or `::'.  */
14168           cp_lexer_consume_token (parser->lexer);
14169           /* Parse the output-operands.  */
14170           if (cp_lexer_next_token_is_not (parser->lexer,
14171                                           CPP_COLON)
14172               && cp_lexer_next_token_is_not (parser->lexer,
14173                                              CPP_SCOPE)
14174               && cp_lexer_next_token_is_not (parser->lexer,
14175                                              CPP_CLOSE_PAREN))
14176             inputs = cp_parser_asm_operand_list (parser);
14177
14178             if (inputs == error_mark_node)
14179               invalid_inputs_p = true;
14180         }
14181       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14182         /* The clobbers are coming next.  */
14183         clobbers_p = true;
14184
14185       /* Look for clobbers.  */
14186       if (clobbers_p
14187           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14188         {
14189           clobbers_p = true;
14190           /* Consume the `:' or `::'.  */
14191           cp_lexer_consume_token (parser->lexer);
14192           /* Parse the clobbers.  */
14193           if (cp_lexer_next_token_is_not (parser->lexer,
14194                                           CPP_COLON)
14195               && cp_lexer_next_token_is_not (parser->lexer,
14196                                              CPP_CLOSE_PAREN))
14197             clobbers = cp_parser_asm_clobber_list (parser);
14198         }
14199       else if (goto_p
14200                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14201         /* The labels are coming next.  */
14202         labels_p = true;
14203
14204       /* Look for labels.  */
14205       if (labels_p
14206           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
14207         {
14208           labels_p = true;
14209           /* Consume the `:' or `::'.  */
14210           cp_lexer_consume_token (parser->lexer);
14211           /* Parse the labels.  */
14212           labels = cp_parser_asm_label_list (parser);
14213         }
14214
14215       if (goto_p && !labels_p)
14216         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
14217     }
14218   else if (goto_p)
14219     missing = RT_COLON_SCOPE;
14220
14221   /* Look for the closing `)'.  */
14222   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
14223                           missing ? missing : RT_CLOSE_PAREN))
14224     cp_parser_skip_to_closing_parenthesis (parser, true, false,
14225                                            /*consume_paren=*/true);
14226   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14227
14228   if (!invalid_inputs_p && !invalid_outputs_p)
14229     {
14230       /* Create the ASM_EXPR.  */
14231       if (parser->in_function_body)
14232         {
14233           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
14234                                       inputs, clobbers, labels);
14235           /* If the extended syntax was not used, mark the ASM_EXPR.  */
14236           if (!extended_p)
14237             {
14238               tree temp = asm_stmt;
14239               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
14240                 temp = TREE_OPERAND (temp, 0);
14241
14242               ASM_INPUT_P (temp) = 1;
14243             }
14244         }
14245       else
14246         cgraph_add_asm_node (string);
14247     }
14248 }
14249
14250 /* Declarators [gram.dcl.decl] */
14251
14252 /* Parse an init-declarator.
14253
14254    init-declarator:
14255      declarator initializer [opt]
14256
14257    GNU Extension:
14258
14259    init-declarator:
14260      declarator asm-specification [opt] attributes [opt] initializer [opt]
14261
14262    function-definition:
14263      decl-specifier-seq [opt] declarator ctor-initializer [opt]
14264        function-body
14265      decl-specifier-seq [opt] declarator function-try-block
14266
14267    GNU Extension:
14268
14269    function-definition:
14270      __extension__ function-definition
14271
14272    The DECL_SPECIFIERS apply to this declarator.  Returns a
14273    representation of the entity declared.  If MEMBER_P is TRUE, then
14274    this declarator appears in a class scope.  The new DECL created by
14275    this declarator is returned.
14276
14277    The CHECKS are access checks that should be performed once we know
14278    what entity is being declared (and, therefore, what classes have
14279    befriended it).
14280
14281    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
14282    for a function-definition here as well.  If the declarator is a
14283    declarator for a function-definition, *FUNCTION_DEFINITION_P will
14284    be TRUE upon return.  By that point, the function-definition will
14285    have been completely parsed.
14286
14287    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
14288    is FALSE.
14289
14290    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
14291    parsed declaration if it is an uninitialized single declarator not followed
14292    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
14293    if present, will not be consumed.  If returned, this declarator will be
14294    created with SD_INITIALIZED but will not call cp_finish_decl.  */
14295
14296 static tree
14297 cp_parser_init_declarator (cp_parser* parser,
14298                            cp_decl_specifier_seq *decl_specifiers,
14299                            VEC (deferred_access_check,gc)* checks,
14300                            bool function_definition_allowed_p,
14301                            bool member_p,
14302                            int declares_class_or_enum,
14303                            bool* function_definition_p,
14304                            tree* maybe_range_for_decl)
14305 {
14306   cp_token *token = NULL, *asm_spec_start_token = NULL,
14307            *attributes_start_token = NULL;
14308   cp_declarator *declarator;
14309   tree prefix_attributes;
14310   tree attributes;
14311   tree asm_specification;
14312   tree initializer;
14313   tree decl = NULL_TREE;
14314   tree scope;
14315   int is_initialized;
14316   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
14317      initialized with "= ..", CPP_OPEN_PAREN if initialized with
14318      "(...)".  */
14319   enum cpp_ttype initialization_kind;
14320   bool is_direct_init = false;
14321   bool is_non_constant_init;
14322   int ctor_dtor_or_conv_p;
14323   bool friend_p;
14324   tree pushed_scope = NULL_TREE;
14325   bool range_for_decl_p = false;
14326
14327   /* Gather the attributes that were provided with the
14328      decl-specifiers.  */
14329   prefix_attributes = decl_specifiers->attributes;
14330
14331   /* Assume that this is not the declarator for a function
14332      definition.  */
14333   if (function_definition_p)
14334     *function_definition_p = false;
14335
14336   /* Defer access checks while parsing the declarator; we cannot know
14337      what names are accessible until we know what is being
14338      declared.  */
14339   resume_deferring_access_checks ();
14340
14341   /* Parse the declarator.  */
14342   token = cp_lexer_peek_token (parser->lexer);
14343   declarator
14344     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
14345                             &ctor_dtor_or_conv_p,
14346                             /*parenthesized_p=*/NULL,
14347                             /*member_p=*/false);
14348   /* Gather up the deferred checks.  */
14349   stop_deferring_access_checks ();
14350
14351   /* If the DECLARATOR was erroneous, there's no need to go
14352      further.  */
14353   if (declarator == cp_error_declarator)
14354     return error_mark_node;
14355
14356   /* Check that the number of template-parameter-lists is OK.  */
14357   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
14358                                                        token->location))
14359     return error_mark_node;
14360
14361   if (declares_class_or_enum & 2)
14362     cp_parser_check_for_definition_in_return_type (declarator,
14363                                                    decl_specifiers->type,
14364                                                    decl_specifiers->type_location);
14365
14366   /* Figure out what scope the entity declared by the DECLARATOR is
14367      located in.  `grokdeclarator' sometimes changes the scope, so
14368      we compute it now.  */
14369   scope = get_scope_of_declarator (declarator);
14370
14371   /* Perform any lookups in the declared type which were thought to be
14372      dependent, but are not in the scope of the declarator.  */
14373   decl_specifiers->type
14374     = maybe_update_decl_type (decl_specifiers->type, scope);
14375
14376   /* If we're allowing GNU extensions, look for an asm-specification
14377      and attributes.  */
14378   if (cp_parser_allow_gnu_extensions_p (parser))
14379     {
14380       /* Look for an asm-specification.  */
14381       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
14382       asm_specification = cp_parser_asm_specification_opt (parser);
14383       /* And attributes.  */
14384       attributes_start_token = cp_lexer_peek_token (parser->lexer);
14385       attributes = cp_parser_attributes_opt (parser);
14386     }
14387   else
14388     {
14389       asm_specification = NULL_TREE;
14390       attributes = NULL_TREE;
14391     }
14392
14393   /* Peek at the next token.  */
14394   token = cp_lexer_peek_token (parser->lexer);
14395   /* Check to see if the token indicates the start of a
14396      function-definition.  */
14397   if (function_declarator_p (declarator)
14398       && cp_parser_token_starts_function_definition_p (token))
14399     {
14400       if (!function_definition_allowed_p)
14401         {
14402           /* If a function-definition should not appear here, issue an
14403              error message.  */
14404           cp_parser_error (parser,
14405                            "a function-definition is not allowed here");
14406           return error_mark_node;
14407         }
14408       else
14409         {
14410           location_t func_brace_location
14411             = cp_lexer_peek_token (parser->lexer)->location;
14412
14413           /* Neither attributes nor an asm-specification are allowed
14414              on a function-definition.  */
14415           if (asm_specification)
14416             error_at (asm_spec_start_token->location,
14417                       "an asm-specification is not allowed "
14418                       "on a function-definition");
14419           if (attributes)
14420             error_at (attributes_start_token->location,
14421                       "attributes are not allowed on a function-definition");
14422           /* This is a function-definition.  */
14423           *function_definition_p = true;
14424
14425           /* Parse the function definition.  */
14426           if (member_p)
14427             decl = cp_parser_save_member_function_body (parser,
14428                                                         decl_specifiers,
14429                                                         declarator,
14430                                                         prefix_attributes);
14431           else
14432             decl
14433               = (cp_parser_function_definition_from_specifiers_and_declarator
14434                  (parser, decl_specifiers, prefix_attributes, declarator));
14435
14436           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
14437             {
14438               /* This is where the prologue starts...  */
14439               DECL_STRUCT_FUNCTION (decl)->function_start_locus
14440                 = func_brace_location;
14441             }
14442
14443           return decl;
14444         }
14445     }
14446
14447   /* [dcl.dcl]
14448
14449      Only in function declarations for constructors, destructors, and
14450      type conversions can the decl-specifier-seq be omitted.
14451
14452      We explicitly postpone this check past the point where we handle
14453      function-definitions because we tolerate function-definitions
14454      that are missing their return types in some modes.  */
14455   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
14456     {
14457       cp_parser_error (parser,
14458                        "expected constructor, destructor, or type conversion");
14459       return error_mark_node;
14460     }
14461
14462   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
14463   if (token->type == CPP_EQ
14464       || token->type == CPP_OPEN_PAREN
14465       || token->type == CPP_OPEN_BRACE)
14466     {
14467       is_initialized = SD_INITIALIZED;
14468       initialization_kind = token->type;
14469       if (maybe_range_for_decl)
14470         *maybe_range_for_decl = error_mark_node;
14471
14472       if (token->type == CPP_EQ
14473           && function_declarator_p (declarator))
14474         {
14475           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
14476           if (t2->keyword == RID_DEFAULT)
14477             is_initialized = SD_DEFAULTED;
14478           else if (t2->keyword == RID_DELETE)
14479             is_initialized = SD_DELETED;
14480         }
14481     }
14482   else
14483     {
14484       /* If the init-declarator isn't initialized and isn't followed by a
14485          `,' or `;', it's not a valid init-declarator.  */
14486       if (token->type != CPP_COMMA
14487           && token->type != CPP_SEMICOLON)
14488         {
14489           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
14490             range_for_decl_p = true;
14491           else
14492             {
14493               cp_parser_error (parser, "expected initializer");
14494               return error_mark_node;
14495             }
14496         }
14497       is_initialized = SD_UNINITIALIZED;
14498       initialization_kind = CPP_EOF;
14499     }
14500
14501   /* Because start_decl has side-effects, we should only call it if we
14502      know we're going ahead.  By this point, we know that we cannot
14503      possibly be looking at any other construct.  */
14504   cp_parser_commit_to_tentative_parse (parser);
14505
14506   /* If the decl specifiers were bad, issue an error now that we're
14507      sure this was intended to be a declarator.  Then continue
14508      declaring the variable(s), as int, to try to cut down on further
14509      errors.  */
14510   if (decl_specifiers->any_specifiers_p
14511       && decl_specifiers->type == error_mark_node)
14512     {
14513       cp_parser_error (parser, "invalid type in declaration");
14514       decl_specifiers->type = integer_type_node;
14515     }
14516
14517   /* Check to see whether or not this declaration is a friend.  */
14518   friend_p = cp_parser_friend_p (decl_specifiers);
14519
14520   /* Enter the newly declared entry in the symbol table.  If we're
14521      processing a declaration in a class-specifier, we wait until
14522      after processing the initializer.  */
14523   if (!member_p)
14524     {
14525       if (parser->in_unbraced_linkage_specification_p)
14526         decl_specifiers->storage_class = sc_extern;
14527       decl = start_decl (declarator, decl_specifiers,
14528                          range_for_decl_p? SD_INITIALIZED : is_initialized,
14529                          attributes, prefix_attributes,
14530                          &pushed_scope);
14531       /* Adjust location of decl if declarator->id_loc is more appropriate:
14532          set, and decl wasn't merged with another decl, in which case its
14533          location would be different from input_location, and more accurate.  */
14534       if (DECL_P (decl)
14535           && declarator->id_loc != UNKNOWN_LOCATION
14536           && DECL_SOURCE_LOCATION (decl) == input_location)
14537         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
14538     }
14539   else if (scope)
14540     /* Enter the SCOPE.  That way unqualified names appearing in the
14541        initializer will be looked up in SCOPE.  */
14542     pushed_scope = push_scope (scope);
14543
14544   /* Perform deferred access control checks, now that we know in which
14545      SCOPE the declared entity resides.  */
14546   if (!member_p && decl)
14547     {
14548       tree saved_current_function_decl = NULL_TREE;
14549
14550       /* If the entity being declared is a function, pretend that we
14551          are in its scope.  If it is a `friend', it may have access to
14552          things that would not otherwise be accessible.  */
14553       if (TREE_CODE (decl) == FUNCTION_DECL)
14554         {
14555           saved_current_function_decl = current_function_decl;
14556           current_function_decl = decl;
14557         }
14558
14559       /* Perform access checks for template parameters.  */
14560       cp_parser_perform_template_parameter_access_checks (checks);
14561
14562       /* Perform the access control checks for the declarator and the
14563          decl-specifiers.  */
14564       perform_deferred_access_checks ();
14565
14566       /* Restore the saved value.  */
14567       if (TREE_CODE (decl) == FUNCTION_DECL)
14568         current_function_decl = saved_current_function_decl;
14569     }
14570
14571   /* Parse the initializer.  */
14572   initializer = NULL_TREE;
14573   is_direct_init = false;
14574   is_non_constant_init = true;
14575   if (is_initialized)
14576     {
14577       if (function_declarator_p (declarator))
14578         {
14579           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
14580            if (initialization_kind == CPP_EQ)
14581              initializer = cp_parser_pure_specifier (parser);
14582            else
14583              {
14584                /* If the declaration was erroneous, we don't really
14585                   know what the user intended, so just silently
14586                   consume the initializer.  */
14587                if (decl != error_mark_node)
14588                  error_at (initializer_start_token->location,
14589                            "initializer provided for function");
14590                cp_parser_skip_to_closing_parenthesis (parser,
14591                                                       /*recovering=*/true,
14592                                                       /*or_comma=*/false,
14593                                                       /*consume_paren=*/true);
14594              }
14595         }
14596       else
14597         {
14598           /* We want to record the extra mangling scope for in-class
14599              initializers of class members and initializers of static data
14600              member templates.  The former is a C++0x feature which isn't
14601              implemented yet, and I expect it will involve deferring
14602              parsing of the initializer until end of class as with default
14603              arguments.  So right here we only handle the latter.  */
14604           if (!member_p && processing_template_decl)
14605             start_lambda_scope (decl);
14606           initializer = cp_parser_initializer (parser,
14607                                                &is_direct_init,
14608                                                &is_non_constant_init);
14609           if (!member_p && processing_template_decl)
14610             finish_lambda_scope ();
14611         }
14612     }
14613
14614   /* The old parser allows attributes to appear after a parenthesized
14615      initializer.  Mark Mitchell proposed removing this functionality
14616      on the GCC mailing lists on 2002-08-13.  This parser accepts the
14617      attributes -- but ignores them.  */
14618   if (cp_parser_allow_gnu_extensions_p (parser)
14619       && initialization_kind == CPP_OPEN_PAREN)
14620     if (cp_parser_attributes_opt (parser))
14621       warning (OPT_Wattributes,
14622                "attributes after parenthesized initializer ignored");
14623
14624   /* For an in-class declaration, use `grokfield' to create the
14625      declaration.  */
14626   if (member_p)
14627     {
14628       if (pushed_scope)
14629         {
14630           pop_scope (pushed_scope);
14631           pushed_scope = NULL_TREE;
14632         }
14633       decl = grokfield (declarator, decl_specifiers,
14634                         initializer, !is_non_constant_init,
14635                         /*asmspec=*/NULL_TREE,
14636                         prefix_attributes);
14637       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
14638         cp_parser_save_default_args (parser, decl);
14639     }
14640
14641   /* Finish processing the declaration.  But, skip member
14642      declarations.  */
14643   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
14644     {
14645       cp_finish_decl (decl,
14646                       initializer, !is_non_constant_init,
14647                       asm_specification,
14648                       /* If the initializer is in parentheses, then this is
14649                          a direct-initialization, which means that an
14650                          `explicit' constructor is OK.  Otherwise, an
14651                          `explicit' constructor cannot be used.  */
14652                       ((is_direct_init || !is_initialized)
14653                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
14654     }
14655   else if ((cxx_dialect != cxx98) && friend_p
14656            && decl && TREE_CODE (decl) == FUNCTION_DECL)
14657     /* Core issue #226 (C++0x only): A default template-argument
14658        shall not be specified in a friend class template
14659        declaration. */
14660     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
14661                              /*is_partial=*/0, /*is_friend_decl=*/1);
14662
14663   if (!friend_p && pushed_scope)
14664     pop_scope (pushed_scope);
14665
14666   return decl;
14667 }
14668
14669 /* Parse a declarator.
14670
14671    declarator:
14672      direct-declarator
14673      ptr-operator declarator
14674
14675    abstract-declarator:
14676      ptr-operator abstract-declarator [opt]
14677      direct-abstract-declarator
14678
14679    GNU Extensions:
14680
14681    declarator:
14682      attributes [opt] direct-declarator
14683      attributes [opt] ptr-operator declarator
14684
14685    abstract-declarator:
14686      attributes [opt] ptr-operator abstract-declarator [opt]
14687      attributes [opt] direct-abstract-declarator
14688
14689    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
14690    detect constructor, destructor or conversion operators. It is set
14691    to -1 if the declarator is a name, and +1 if it is a
14692    function. Otherwise it is set to zero. Usually you just want to
14693    test for >0, but internally the negative value is used.
14694
14695    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
14696    a decl-specifier-seq unless it declares a constructor, destructor,
14697    or conversion.  It might seem that we could check this condition in
14698    semantic analysis, rather than parsing, but that makes it difficult
14699    to handle something like `f()'.  We want to notice that there are
14700    no decl-specifiers, and therefore realize that this is an
14701    expression, not a declaration.)
14702
14703    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
14704    the declarator is a direct-declarator of the form "(...)".
14705
14706    MEMBER_P is true iff this declarator is a member-declarator.  */
14707
14708 static cp_declarator *
14709 cp_parser_declarator (cp_parser* parser,
14710                       cp_parser_declarator_kind dcl_kind,
14711                       int* ctor_dtor_or_conv_p,
14712                       bool* parenthesized_p,
14713                       bool member_p)
14714 {
14715   cp_declarator *declarator;
14716   enum tree_code code;
14717   cp_cv_quals cv_quals;
14718   tree class_type;
14719   tree attributes = NULL_TREE;
14720
14721   /* Assume this is not a constructor, destructor, or type-conversion
14722      operator.  */
14723   if (ctor_dtor_or_conv_p)
14724     *ctor_dtor_or_conv_p = 0;
14725
14726   if (cp_parser_allow_gnu_extensions_p (parser))
14727     attributes = cp_parser_attributes_opt (parser);
14728
14729   /* Check for the ptr-operator production.  */
14730   cp_parser_parse_tentatively (parser);
14731   /* Parse the ptr-operator.  */
14732   code = cp_parser_ptr_operator (parser,
14733                                  &class_type,
14734                                  &cv_quals);
14735   /* If that worked, then we have a ptr-operator.  */
14736   if (cp_parser_parse_definitely (parser))
14737     {
14738       /* If a ptr-operator was found, then this declarator was not
14739          parenthesized.  */
14740       if (parenthesized_p)
14741         *parenthesized_p = true;
14742       /* The dependent declarator is optional if we are parsing an
14743          abstract-declarator.  */
14744       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14745         cp_parser_parse_tentatively (parser);
14746
14747       /* Parse the dependent declarator.  */
14748       declarator = cp_parser_declarator (parser, dcl_kind,
14749                                          /*ctor_dtor_or_conv_p=*/NULL,
14750                                          /*parenthesized_p=*/NULL,
14751                                          /*member_p=*/false);
14752
14753       /* If we are parsing an abstract-declarator, we must handle the
14754          case where the dependent declarator is absent.  */
14755       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
14756           && !cp_parser_parse_definitely (parser))
14757         declarator = NULL;
14758
14759       declarator = cp_parser_make_indirect_declarator
14760         (code, class_type, cv_quals, declarator);
14761     }
14762   /* Everything else is a direct-declarator.  */
14763   else
14764     {
14765       if (parenthesized_p)
14766         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
14767                                                    CPP_OPEN_PAREN);
14768       declarator = cp_parser_direct_declarator (parser, dcl_kind,
14769                                                 ctor_dtor_or_conv_p,
14770                                                 member_p);
14771     }
14772
14773   if (attributes && declarator && declarator != cp_error_declarator)
14774     declarator->attributes = attributes;
14775
14776   return declarator;
14777 }
14778
14779 /* Parse a direct-declarator or direct-abstract-declarator.
14780
14781    direct-declarator:
14782      declarator-id
14783      direct-declarator ( parameter-declaration-clause )
14784        cv-qualifier-seq [opt]
14785        exception-specification [opt]
14786      direct-declarator [ constant-expression [opt] ]
14787      ( declarator )
14788
14789    direct-abstract-declarator:
14790      direct-abstract-declarator [opt]
14791        ( parameter-declaration-clause )
14792        cv-qualifier-seq [opt]
14793        exception-specification [opt]
14794      direct-abstract-declarator [opt] [ constant-expression [opt] ]
14795      ( abstract-declarator )
14796
14797    Returns a representation of the declarator.  DCL_KIND is
14798    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
14799    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
14800    we are parsing a direct-declarator.  It is
14801    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
14802    of ambiguity we prefer an abstract declarator, as per
14803    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
14804    cp_parser_declarator.  */
14805
14806 static cp_declarator *
14807 cp_parser_direct_declarator (cp_parser* parser,
14808                              cp_parser_declarator_kind dcl_kind,
14809                              int* ctor_dtor_or_conv_p,
14810                              bool member_p)
14811 {
14812   cp_token *token;
14813   cp_declarator *declarator = NULL;
14814   tree scope = NULL_TREE;
14815   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
14816   bool saved_in_declarator_p = parser->in_declarator_p;
14817   bool first = true;
14818   tree pushed_scope = NULL_TREE;
14819
14820   while (true)
14821     {
14822       /* Peek at the next token.  */
14823       token = cp_lexer_peek_token (parser->lexer);
14824       if (token->type == CPP_OPEN_PAREN)
14825         {
14826           /* This is either a parameter-declaration-clause, or a
14827              parenthesized declarator. When we know we are parsing a
14828              named declarator, it must be a parenthesized declarator
14829              if FIRST is true. For instance, `(int)' is a
14830              parameter-declaration-clause, with an omitted
14831              direct-abstract-declarator. But `((*))', is a
14832              parenthesized abstract declarator. Finally, when T is a
14833              template parameter `(T)' is a
14834              parameter-declaration-clause, and not a parenthesized
14835              named declarator.
14836
14837              We first try and parse a parameter-declaration-clause,
14838              and then try a nested declarator (if FIRST is true).
14839
14840              It is not an error for it not to be a
14841              parameter-declaration-clause, even when FIRST is
14842              false. Consider,
14843
14844                int i (int);
14845                int i (3);
14846
14847              The first is the declaration of a function while the
14848              second is the definition of a variable, including its
14849              initializer.
14850
14851              Having seen only the parenthesis, we cannot know which of
14852              these two alternatives should be selected.  Even more
14853              complex are examples like:
14854
14855                int i (int (a));
14856                int i (int (3));
14857
14858              The former is a function-declaration; the latter is a
14859              variable initialization.
14860
14861              Thus again, we try a parameter-declaration-clause, and if
14862              that fails, we back out and return.  */
14863
14864           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14865             {
14866               tree params;
14867               unsigned saved_num_template_parameter_lists;
14868               bool is_declarator = false;
14869               tree t;
14870
14871               /* In a member-declarator, the only valid interpretation
14872                  of a parenthesis is the start of a
14873                  parameter-declaration-clause.  (It is invalid to
14874                  initialize a static data member with a parenthesized
14875                  initializer; only the "=" form of initialization is
14876                  permitted.)  */
14877               if (!member_p)
14878                 cp_parser_parse_tentatively (parser);
14879
14880               /* Consume the `('.  */
14881               cp_lexer_consume_token (parser->lexer);
14882               if (first)
14883                 {
14884                   /* If this is going to be an abstract declarator, we're
14885                      in a declarator and we can't have default args.  */
14886                   parser->default_arg_ok_p = false;
14887                   parser->in_declarator_p = true;
14888                 }
14889
14890               /* Inside the function parameter list, surrounding
14891                  template-parameter-lists do not apply.  */
14892               saved_num_template_parameter_lists
14893                 = parser->num_template_parameter_lists;
14894               parser->num_template_parameter_lists = 0;
14895
14896               begin_scope (sk_function_parms, NULL_TREE);
14897
14898               /* Parse the parameter-declaration-clause.  */
14899               params = cp_parser_parameter_declaration_clause (parser);
14900
14901               parser->num_template_parameter_lists
14902                 = saved_num_template_parameter_lists;
14903
14904               /* Consume the `)'.  */
14905               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
14906
14907               /* If all went well, parse the cv-qualifier-seq and the
14908                  exception-specification.  */
14909               if (member_p || cp_parser_parse_definitely (parser))
14910                 {
14911                   cp_cv_quals cv_quals;
14912                   cp_virt_specifiers virt_specifiers;
14913                   tree exception_specification;
14914                   tree late_return;
14915
14916                   is_declarator = true;
14917
14918                   if (ctor_dtor_or_conv_p)
14919                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
14920                   first = false;
14921
14922                   /* Parse the cv-qualifier-seq.  */
14923                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
14924                   /* And the exception-specification.  */
14925                   exception_specification
14926                     = cp_parser_exception_specification_opt (parser);
14927                   /* Parse the virt-specifier-seq.  */
14928                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
14929
14930                   late_return
14931                     = cp_parser_late_return_type_opt (parser);
14932
14933                   /* Create the function-declarator.  */
14934                   declarator = make_call_declarator (declarator,
14935                                                      params,
14936                                                      cv_quals,
14937                                                      virt_specifiers,
14938                                                      exception_specification,
14939                                                      late_return);
14940                   /* Any subsequent parameter lists are to do with
14941                      return type, so are not those of the declared
14942                      function.  */
14943                   parser->default_arg_ok_p = false;
14944                 }
14945
14946               /* Remove the function parms from scope.  */
14947               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
14948                 pop_binding (DECL_NAME (t), t);
14949               leave_scope();
14950
14951               if (is_declarator)
14952                 /* Repeat the main loop.  */
14953                 continue;
14954             }
14955
14956           /* If this is the first, we can try a parenthesized
14957              declarator.  */
14958           if (first)
14959             {
14960               bool saved_in_type_id_in_expr_p;
14961
14962               parser->default_arg_ok_p = saved_default_arg_ok_p;
14963               parser->in_declarator_p = saved_in_declarator_p;
14964
14965               /* Consume the `('.  */
14966               cp_lexer_consume_token (parser->lexer);
14967               /* Parse the nested declarator.  */
14968               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
14969               parser->in_type_id_in_expr_p = true;
14970               declarator
14971                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
14972                                         /*parenthesized_p=*/NULL,
14973                                         member_p);
14974               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
14975               first = false;
14976               /* Expect a `)'.  */
14977               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
14978                 declarator = cp_error_declarator;
14979               if (declarator == cp_error_declarator)
14980                 break;
14981
14982               goto handle_declarator;
14983             }
14984           /* Otherwise, we must be done.  */
14985           else
14986             break;
14987         }
14988       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
14989                && token->type == CPP_OPEN_SQUARE)
14990         {
14991           /* Parse an array-declarator.  */
14992           tree bounds;
14993
14994           if (ctor_dtor_or_conv_p)
14995             *ctor_dtor_or_conv_p = 0;
14996
14997           first = false;
14998           parser->default_arg_ok_p = false;
14999           parser->in_declarator_p = true;
15000           /* Consume the `['.  */
15001           cp_lexer_consume_token (parser->lexer);
15002           /* Peek at the next token.  */
15003           token = cp_lexer_peek_token (parser->lexer);
15004           /* If the next token is `]', then there is no
15005              constant-expression.  */
15006           if (token->type != CPP_CLOSE_SQUARE)
15007             {
15008               bool non_constant_p;
15009
15010               bounds
15011                 = cp_parser_constant_expression (parser,
15012                                                  /*allow_non_constant=*/true,
15013                                                  &non_constant_p);
15014               if (!non_constant_p)
15015                 /* OK */;
15016               /* Normally, the array bound must be an integral constant
15017                  expression.  However, as an extension, we allow VLAs
15018                  in function scopes as long as they aren't part of a
15019                  parameter declaration.  */
15020               else if (!parser->in_function_body
15021                        || current_binding_level->kind == sk_function_parms)
15022                 {
15023                   cp_parser_error (parser,
15024                                    "array bound is not an integer constant");
15025                   bounds = error_mark_node;
15026                 }
15027               else if (processing_template_decl && !error_operand_p (bounds))
15028                 {
15029                   /* Remember this wasn't a constant-expression.  */
15030                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15031                   TREE_SIDE_EFFECTS (bounds) = 1;
15032                 }
15033             }
15034           else
15035             bounds = NULL_TREE;
15036           /* Look for the closing `]'.  */
15037           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15038             {
15039               declarator = cp_error_declarator;
15040               break;
15041             }
15042
15043           declarator = make_array_declarator (declarator, bounds);
15044         }
15045       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15046         {
15047           {
15048             tree qualifying_scope;
15049             tree unqualified_name;
15050             special_function_kind sfk;
15051             bool abstract_ok;
15052             bool pack_expansion_p = false;
15053             cp_token *declarator_id_start_token;
15054
15055             /* Parse a declarator-id */
15056             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15057             if (abstract_ok)
15058               {
15059                 cp_parser_parse_tentatively (parser);
15060
15061                 /* If we see an ellipsis, we should be looking at a
15062                    parameter pack. */
15063                 if (token->type == CPP_ELLIPSIS)
15064                   {
15065                     /* Consume the `...' */
15066                     cp_lexer_consume_token (parser->lexer);
15067
15068                     pack_expansion_p = true;
15069                   }
15070               }
15071
15072             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15073             unqualified_name
15074               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15075             qualifying_scope = parser->scope;
15076             if (abstract_ok)
15077               {
15078                 bool okay = false;
15079
15080                 if (!unqualified_name && pack_expansion_p)
15081                   {
15082                     /* Check whether an error occurred. */
15083                     okay = !cp_parser_error_occurred (parser);
15084
15085                     /* We already consumed the ellipsis to mark a
15086                        parameter pack, but we have no way to report it,
15087                        so abort the tentative parse. We will be exiting
15088                        immediately anyway. */
15089                     cp_parser_abort_tentative_parse (parser);
15090                   }
15091                 else
15092                   okay = cp_parser_parse_definitely (parser);
15093
15094                 if (!okay)
15095                   unqualified_name = error_mark_node;
15096                 else if (unqualified_name
15097                          && (qualifying_scope
15098                              || (TREE_CODE (unqualified_name)
15099                                  != IDENTIFIER_NODE)))
15100                   {
15101                     cp_parser_error (parser, "expected unqualified-id");
15102                     unqualified_name = error_mark_node;
15103                   }
15104               }
15105
15106             if (!unqualified_name)
15107               return NULL;
15108             if (unqualified_name == error_mark_node)
15109               {
15110                 declarator = cp_error_declarator;
15111                 pack_expansion_p = false;
15112                 declarator->parameter_pack_p = false;
15113                 break;
15114               }
15115
15116             if (qualifying_scope && at_namespace_scope_p ()
15117                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15118               {
15119                 /* In the declaration of a member of a template class
15120                    outside of the class itself, the SCOPE will sometimes
15121                    be a TYPENAME_TYPE.  For example, given:
15122
15123                    template <typename T>
15124                    int S<T>::R::i = 3;
15125
15126                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15127                    this context, we must resolve S<T>::R to an ordinary
15128                    type, rather than a typename type.
15129
15130                    The reason we normally avoid resolving TYPENAME_TYPEs
15131                    is that a specialization of `S' might render
15132                    `S<T>::R' not a type.  However, if `S' is
15133                    specialized, then this `i' will not be used, so there
15134                    is no harm in resolving the types here.  */
15135                 tree type;
15136
15137                 /* Resolve the TYPENAME_TYPE.  */
15138                 type = resolve_typename_type (qualifying_scope,
15139                                               /*only_current_p=*/false);
15140                 /* If that failed, the declarator is invalid.  */
15141                 if (TREE_CODE (type) == TYPENAME_TYPE)
15142                   {
15143                     if (typedef_variant_p (type))
15144                       error_at (declarator_id_start_token->location,
15145                                 "cannot define member of dependent typedef "
15146                                 "%qT", type);
15147                     else
15148                       error_at (declarator_id_start_token->location,
15149                                 "%<%T::%E%> is not a type",
15150                                 TYPE_CONTEXT (qualifying_scope),
15151                                 TYPE_IDENTIFIER (qualifying_scope));
15152                   }
15153                 qualifying_scope = type;
15154               }
15155
15156             sfk = sfk_none;
15157
15158             if (unqualified_name)
15159               {
15160                 tree class_type;
15161
15162                 if (qualifying_scope
15163                     && CLASS_TYPE_P (qualifying_scope))
15164                   class_type = qualifying_scope;
15165                 else
15166                   class_type = current_class_type;
15167
15168                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15169                   {
15170                     tree name_type = TREE_TYPE (unqualified_name);
15171                     if (class_type && same_type_p (name_type, class_type))
15172                       {
15173                         if (qualifying_scope
15174                             && CLASSTYPE_USE_TEMPLATE (name_type))
15175                           {
15176                             error_at (declarator_id_start_token->location,
15177                                       "invalid use of constructor as a template");
15178                             inform (declarator_id_start_token->location,
15179                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15180                                     "name the constructor in a qualified name",
15181                                     class_type,
15182                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15183                                     class_type, name_type);
15184                             declarator = cp_error_declarator;
15185                             break;
15186                           }
15187                         else
15188                           unqualified_name = constructor_name (class_type);
15189                       }
15190                     else
15191                       {
15192                         /* We do not attempt to print the declarator
15193                            here because we do not have enough
15194                            information about its original syntactic
15195                            form.  */
15196                         cp_parser_error (parser, "invalid declarator");
15197                         declarator = cp_error_declarator;
15198                         break;
15199                       }
15200                   }
15201
15202                 if (class_type)
15203                   {
15204                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
15205                       sfk = sfk_destructor;
15206                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
15207                       sfk = sfk_conversion;
15208                     else if (/* There's no way to declare a constructor
15209                                 for an anonymous type, even if the type
15210                                 got a name for linkage purposes.  */
15211                              !TYPE_WAS_ANONYMOUS (class_type)
15212                              && constructor_name_p (unqualified_name,
15213                                                     class_type))
15214                       {
15215                         unqualified_name = constructor_name (class_type);
15216                         sfk = sfk_constructor;
15217                       }
15218                     else if (is_overloaded_fn (unqualified_name)
15219                              && DECL_CONSTRUCTOR_P (get_first_fn
15220                                                     (unqualified_name)))
15221                       sfk = sfk_constructor;
15222
15223                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
15224                       *ctor_dtor_or_conv_p = -1;
15225                   }
15226               }
15227             declarator = make_id_declarator (qualifying_scope,
15228                                              unqualified_name,
15229                                              sfk);
15230             declarator->id_loc = token->location;
15231             declarator->parameter_pack_p = pack_expansion_p;
15232
15233             if (pack_expansion_p)
15234               maybe_warn_variadic_templates ();
15235           }
15236
15237         handle_declarator:;
15238           scope = get_scope_of_declarator (declarator);
15239           if (scope)
15240             /* Any names that appear after the declarator-id for a
15241                member are looked up in the containing scope.  */
15242             pushed_scope = push_scope (scope);
15243           parser->in_declarator_p = true;
15244           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
15245               || (declarator && declarator->kind == cdk_id))
15246             /* Default args are only allowed on function
15247                declarations.  */
15248             parser->default_arg_ok_p = saved_default_arg_ok_p;
15249           else
15250             parser->default_arg_ok_p = false;
15251
15252           first = false;
15253         }
15254       /* We're done.  */
15255       else
15256         break;
15257     }
15258
15259   /* For an abstract declarator, we might wind up with nothing at this
15260      point.  That's an error; the declarator is not optional.  */
15261   if (!declarator)
15262     cp_parser_error (parser, "expected declarator");
15263
15264   /* If we entered a scope, we must exit it now.  */
15265   if (pushed_scope)
15266     pop_scope (pushed_scope);
15267
15268   parser->default_arg_ok_p = saved_default_arg_ok_p;
15269   parser->in_declarator_p = saved_in_declarator_p;
15270
15271   return declarator;
15272 }
15273
15274 /* Parse a ptr-operator.
15275
15276    ptr-operator:
15277      * cv-qualifier-seq [opt]
15278      &
15279      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
15280
15281    GNU Extension:
15282
15283    ptr-operator:
15284      & cv-qualifier-seq [opt]
15285
15286    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
15287    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
15288    an rvalue reference. In the case of a pointer-to-member, *TYPE is
15289    filled in with the TYPE containing the member.  *CV_QUALS is
15290    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
15291    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
15292    Note that the tree codes returned by this function have nothing
15293    to do with the types of trees that will be eventually be created
15294    to represent the pointer or reference type being parsed. They are
15295    just constants with suggestive names. */
15296 static enum tree_code
15297 cp_parser_ptr_operator (cp_parser* parser,
15298                         tree* type,
15299                         cp_cv_quals *cv_quals)
15300 {
15301   enum tree_code code = ERROR_MARK;
15302   cp_token *token;
15303
15304   /* Assume that it's not a pointer-to-member.  */
15305   *type = NULL_TREE;
15306   /* And that there are no cv-qualifiers.  */
15307   *cv_quals = TYPE_UNQUALIFIED;
15308
15309   /* Peek at the next token.  */
15310   token = cp_lexer_peek_token (parser->lexer);
15311
15312   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
15313   if (token->type == CPP_MULT)
15314     code = INDIRECT_REF;
15315   else if (token->type == CPP_AND)
15316     code = ADDR_EXPR;
15317   else if ((cxx_dialect != cxx98) &&
15318            token->type == CPP_AND_AND) /* C++0x only */
15319     code = NON_LVALUE_EXPR;
15320
15321   if (code != ERROR_MARK)
15322     {
15323       /* Consume the `*', `&' or `&&'.  */
15324       cp_lexer_consume_token (parser->lexer);
15325
15326       /* A `*' can be followed by a cv-qualifier-seq, and so can a
15327          `&', if we are allowing GNU extensions.  (The only qualifier
15328          that can legally appear after `&' is `restrict', but that is
15329          enforced during semantic analysis.  */
15330       if (code == INDIRECT_REF
15331           || cp_parser_allow_gnu_extensions_p (parser))
15332         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15333     }
15334   else
15335     {
15336       /* Try the pointer-to-member case.  */
15337       cp_parser_parse_tentatively (parser);
15338       /* Look for the optional `::' operator.  */
15339       cp_parser_global_scope_opt (parser,
15340                                   /*current_scope_valid_p=*/false);
15341       /* Look for the nested-name specifier.  */
15342       token = cp_lexer_peek_token (parser->lexer);
15343       cp_parser_nested_name_specifier (parser,
15344                                        /*typename_keyword_p=*/false,
15345                                        /*check_dependency_p=*/true,
15346                                        /*type_p=*/false,
15347                                        /*is_declaration=*/false);
15348       /* If we found it, and the next token is a `*', then we are
15349          indeed looking at a pointer-to-member operator.  */
15350       if (!cp_parser_error_occurred (parser)
15351           && cp_parser_require (parser, CPP_MULT, RT_MULT))
15352         {
15353           /* Indicate that the `*' operator was used.  */
15354           code = INDIRECT_REF;
15355
15356           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
15357             error_at (token->location, "%qD is a namespace", parser->scope);
15358           else
15359             {
15360               /* The type of which the member is a member is given by the
15361                  current SCOPE.  */
15362               *type = parser->scope;
15363               /* The next name will not be qualified.  */
15364               parser->scope = NULL_TREE;
15365               parser->qualifying_scope = NULL_TREE;
15366               parser->object_scope = NULL_TREE;
15367               /* Look for the optional cv-qualifier-seq.  */
15368               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15369             }
15370         }
15371       /* If that didn't work we don't have a ptr-operator.  */
15372       if (!cp_parser_parse_definitely (parser))
15373         cp_parser_error (parser, "expected ptr-operator");
15374     }
15375
15376   return code;
15377 }
15378
15379 /* Parse an (optional) cv-qualifier-seq.
15380
15381    cv-qualifier-seq:
15382      cv-qualifier cv-qualifier-seq [opt]
15383
15384    cv-qualifier:
15385      const
15386      volatile
15387
15388    GNU Extension:
15389
15390    cv-qualifier:
15391      __restrict__
15392
15393    Returns a bitmask representing the cv-qualifiers.  */
15394
15395 static cp_cv_quals
15396 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
15397 {
15398   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
15399
15400   while (true)
15401     {
15402       cp_token *token;
15403       cp_cv_quals cv_qualifier;
15404
15405       /* Peek at the next token.  */
15406       token = cp_lexer_peek_token (parser->lexer);
15407       /* See if it's a cv-qualifier.  */
15408       switch (token->keyword)
15409         {
15410         case RID_CONST:
15411           cv_qualifier = TYPE_QUAL_CONST;
15412           break;
15413
15414         case RID_VOLATILE:
15415           cv_qualifier = TYPE_QUAL_VOLATILE;
15416           break;
15417
15418         case RID_RESTRICT:
15419           cv_qualifier = TYPE_QUAL_RESTRICT;
15420           break;
15421
15422         default:
15423           cv_qualifier = TYPE_UNQUALIFIED;
15424           break;
15425         }
15426
15427       if (!cv_qualifier)
15428         break;
15429
15430       if (cv_quals & cv_qualifier)
15431         {
15432           error_at (token->location, "duplicate cv-qualifier");
15433           cp_lexer_purge_token (parser->lexer);
15434         }
15435       else
15436         {
15437           cp_lexer_consume_token (parser->lexer);
15438           cv_quals |= cv_qualifier;
15439         }
15440     }
15441
15442   return cv_quals;
15443 }
15444
15445 /* Parse an (optional) virt-specifier-seq.
15446
15447    virt-specifier-seq:
15448      virt-specifier virt-specifier-seq [opt]
15449
15450    virt-specifier:
15451      override
15452      final
15453
15454    Returns a bitmask representing the virt-specifiers.  */
15455
15456 static cp_virt_specifiers
15457 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
15458 {
15459   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
15460
15461   while (true)
15462     {
15463       cp_token *token;
15464       cp_virt_specifiers virt_specifier;
15465
15466       /* Peek at the next token.  */
15467       token = cp_lexer_peek_token (parser->lexer);
15468       /* See if it's a virt-specifier-qualifier.  */
15469       if (token->type != CPP_NAME)
15470         break;
15471       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
15472         virt_specifier = VIRT_SPEC_OVERRIDE;
15473       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
15474         virt_specifier = VIRT_SPEC_FINAL;
15475       else
15476         break;
15477
15478       if (virt_specifiers & virt_specifier)
15479         {
15480           error_at (token->location, "duplicate virt-specifier");
15481           cp_lexer_purge_token (parser->lexer);
15482         }
15483       else
15484         {
15485           cp_lexer_consume_token (parser->lexer);
15486           virt_specifiers |= virt_specifier;
15487         }
15488     }
15489   return virt_specifiers;
15490 }
15491
15492 /* Parse a late-specified return type, if any.  This is not a separate
15493    non-terminal, but part of a function declarator, which looks like
15494
15495    -> trailing-type-specifier-seq abstract-declarator(opt)
15496
15497    Returns the type indicated by the type-id.  */
15498
15499 static tree
15500 cp_parser_late_return_type_opt (cp_parser* parser)
15501 {
15502   cp_token *token;
15503
15504   /* Peek at the next token.  */
15505   token = cp_lexer_peek_token (parser->lexer);
15506   /* A late-specified return type is indicated by an initial '->'. */
15507   if (token->type != CPP_DEREF)
15508     return NULL_TREE;
15509
15510   /* Consume the ->.  */
15511   cp_lexer_consume_token (parser->lexer);
15512
15513   return cp_parser_trailing_type_id (parser);
15514 }
15515
15516 /* Parse a declarator-id.
15517
15518    declarator-id:
15519      id-expression
15520      :: [opt] nested-name-specifier [opt] type-name
15521
15522    In the `id-expression' case, the value returned is as for
15523    cp_parser_id_expression if the id-expression was an unqualified-id.
15524    If the id-expression was a qualified-id, then a SCOPE_REF is
15525    returned.  The first operand is the scope (either a NAMESPACE_DECL
15526    or TREE_TYPE), but the second is still just a representation of an
15527    unqualified-id.  */
15528
15529 static tree
15530 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
15531 {
15532   tree id;
15533   /* The expression must be an id-expression.  Assume that qualified
15534      names are the names of types so that:
15535
15536        template <class T>
15537        int S<T>::R::i = 3;
15538
15539      will work; we must treat `S<T>::R' as the name of a type.
15540      Similarly, assume that qualified names are templates, where
15541      required, so that:
15542
15543        template <class T>
15544        int S<T>::R<T>::i = 3;
15545
15546      will work, too.  */
15547   id = cp_parser_id_expression (parser,
15548                                 /*template_keyword_p=*/false,
15549                                 /*check_dependency_p=*/false,
15550                                 /*template_p=*/NULL,
15551                                 /*declarator_p=*/true,
15552                                 optional_p);
15553   if (id && BASELINK_P (id))
15554     id = BASELINK_FUNCTIONS (id);
15555   return id;
15556 }
15557
15558 /* Parse a type-id.
15559
15560    type-id:
15561      type-specifier-seq abstract-declarator [opt]
15562
15563    Returns the TYPE specified.  */
15564
15565 static tree
15566 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
15567                      bool is_trailing_return)
15568 {
15569   cp_decl_specifier_seq type_specifier_seq;
15570   cp_declarator *abstract_declarator;
15571
15572   /* Parse the type-specifier-seq.  */
15573   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
15574                                 is_trailing_return,
15575                                 &type_specifier_seq);
15576   if (type_specifier_seq.type == error_mark_node)
15577     return error_mark_node;
15578
15579   /* There might or might not be an abstract declarator.  */
15580   cp_parser_parse_tentatively (parser);
15581   /* Look for the declarator.  */
15582   abstract_declarator
15583     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
15584                             /*parenthesized_p=*/NULL,
15585                             /*member_p=*/false);
15586   /* Check to see if there really was a declarator.  */
15587   if (!cp_parser_parse_definitely (parser))
15588     abstract_declarator = NULL;
15589
15590   if (type_specifier_seq.type
15591       && type_uses_auto (type_specifier_seq.type))
15592     {
15593       /* A type-id with type 'auto' is only ok if the abstract declarator
15594          is a function declarator with a late-specified return type.  */
15595       if (abstract_declarator
15596           && abstract_declarator->kind == cdk_function
15597           && abstract_declarator->u.function.late_return_type)
15598         /* OK */;
15599       else
15600         {
15601           error ("invalid use of %<auto%>");
15602           return error_mark_node;
15603         }
15604     }
15605   
15606   return groktypename (&type_specifier_seq, abstract_declarator,
15607                        is_template_arg);
15608 }
15609
15610 static tree cp_parser_type_id (cp_parser *parser)
15611 {
15612   return cp_parser_type_id_1 (parser, false, false);
15613 }
15614
15615 static tree cp_parser_template_type_arg (cp_parser *parser)
15616 {
15617   tree r;
15618   const char *saved_message = parser->type_definition_forbidden_message;
15619   parser->type_definition_forbidden_message
15620     = G_("types may not be defined in template arguments");
15621   r = cp_parser_type_id_1 (parser, true, false);
15622   parser->type_definition_forbidden_message = saved_message;
15623   return r;
15624 }
15625
15626 static tree cp_parser_trailing_type_id (cp_parser *parser)
15627 {
15628   return cp_parser_type_id_1 (parser, false, true);
15629 }
15630
15631 /* Parse a type-specifier-seq.
15632
15633    type-specifier-seq:
15634      type-specifier type-specifier-seq [opt]
15635
15636    GNU extension:
15637
15638    type-specifier-seq:
15639      attributes type-specifier-seq [opt]
15640
15641    If IS_DECLARATION is true, we are at the start of a "condition" or
15642    exception-declaration, so we might be followed by a declarator-id.
15643
15644    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
15645    i.e. we've just seen "->".
15646
15647    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
15648
15649 static void
15650 cp_parser_type_specifier_seq (cp_parser* parser,
15651                               bool is_declaration,
15652                               bool is_trailing_return,
15653                               cp_decl_specifier_seq *type_specifier_seq)
15654 {
15655   bool seen_type_specifier = false;
15656   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
15657   cp_token *start_token = NULL;
15658
15659   /* Clear the TYPE_SPECIFIER_SEQ.  */
15660   clear_decl_specs (type_specifier_seq);
15661
15662   /* In the context of a trailing return type, enum E { } is an
15663      elaborated-type-specifier followed by a function-body, not an
15664      enum-specifier.  */
15665   if (is_trailing_return)
15666     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
15667
15668   /* Parse the type-specifiers and attributes.  */
15669   while (true)
15670     {
15671       tree type_specifier;
15672       bool is_cv_qualifier;
15673
15674       /* Check for attributes first.  */
15675       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
15676         {
15677           type_specifier_seq->attributes =
15678             chainon (type_specifier_seq->attributes,
15679                      cp_parser_attributes_opt (parser));
15680           continue;
15681         }
15682
15683       /* record the token of the beginning of the type specifier seq,
15684          for error reporting purposes*/
15685      if (!start_token)
15686        start_token = cp_lexer_peek_token (parser->lexer);
15687
15688       /* Look for the type-specifier.  */
15689       type_specifier = cp_parser_type_specifier (parser,
15690                                                  flags,
15691                                                  type_specifier_seq,
15692                                                  /*is_declaration=*/false,
15693                                                  NULL,
15694                                                  &is_cv_qualifier);
15695       if (!type_specifier)
15696         {
15697           /* If the first type-specifier could not be found, this is not a
15698              type-specifier-seq at all.  */
15699           if (!seen_type_specifier)
15700             {
15701               cp_parser_error (parser, "expected type-specifier");
15702               type_specifier_seq->type = error_mark_node;
15703               return;
15704             }
15705           /* If subsequent type-specifiers could not be found, the
15706              type-specifier-seq is complete.  */
15707           break;
15708         }
15709
15710       seen_type_specifier = true;
15711       /* The standard says that a condition can be:
15712
15713             type-specifier-seq declarator = assignment-expression
15714
15715          However, given:
15716
15717            struct S {};
15718            if (int S = ...)
15719
15720          we should treat the "S" as a declarator, not as a
15721          type-specifier.  The standard doesn't say that explicitly for
15722          type-specifier-seq, but it does say that for
15723          decl-specifier-seq in an ordinary declaration.  Perhaps it
15724          would be clearer just to allow a decl-specifier-seq here, and
15725          then add a semantic restriction that if any decl-specifiers
15726          that are not type-specifiers appear, the program is invalid.  */
15727       if (is_declaration && !is_cv_qualifier)
15728         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
15729     }
15730
15731   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
15732 }
15733
15734 /* Parse a parameter-declaration-clause.
15735
15736    parameter-declaration-clause:
15737      parameter-declaration-list [opt] ... [opt]
15738      parameter-declaration-list , ...
15739
15740    Returns a representation for the parameter declarations.  A return
15741    value of NULL indicates a parameter-declaration-clause consisting
15742    only of an ellipsis.  */
15743
15744 static tree
15745 cp_parser_parameter_declaration_clause (cp_parser* parser)
15746 {
15747   tree parameters;
15748   cp_token *token;
15749   bool ellipsis_p;
15750   bool is_error;
15751
15752   /* Peek at the next token.  */
15753   token = cp_lexer_peek_token (parser->lexer);
15754   /* Check for trivial parameter-declaration-clauses.  */
15755   if (token->type == CPP_ELLIPSIS)
15756     {
15757       /* Consume the `...' token.  */
15758       cp_lexer_consume_token (parser->lexer);
15759       return NULL_TREE;
15760     }
15761   else if (token->type == CPP_CLOSE_PAREN)
15762     /* There are no parameters.  */
15763     {
15764 #ifndef NO_IMPLICIT_EXTERN_C
15765       if (in_system_header && current_class_type == NULL
15766           && current_lang_name == lang_name_c)
15767         return NULL_TREE;
15768       else
15769 #endif
15770         return void_list_node;
15771     }
15772   /* Check for `(void)', too, which is a special case.  */
15773   else if (token->keyword == RID_VOID
15774            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
15775                == CPP_CLOSE_PAREN))
15776     {
15777       /* Consume the `void' token.  */
15778       cp_lexer_consume_token (parser->lexer);
15779       /* There are no parameters.  */
15780       return void_list_node;
15781     }
15782
15783   /* Parse the parameter-declaration-list.  */
15784   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
15785   /* If a parse error occurred while parsing the
15786      parameter-declaration-list, then the entire
15787      parameter-declaration-clause is erroneous.  */
15788   if (is_error)
15789     return NULL;
15790
15791   /* Peek at the next token.  */
15792   token = cp_lexer_peek_token (parser->lexer);
15793   /* If it's a `,', the clause should terminate with an ellipsis.  */
15794   if (token->type == CPP_COMMA)
15795     {
15796       /* Consume the `,'.  */
15797       cp_lexer_consume_token (parser->lexer);
15798       /* Expect an ellipsis.  */
15799       ellipsis_p
15800         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
15801     }
15802   /* It might also be `...' if the optional trailing `,' was
15803      omitted.  */
15804   else if (token->type == CPP_ELLIPSIS)
15805     {
15806       /* Consume the `...' token.  */
15807       cp_lexer_consume_token (parser->lexer);
15808       /* And remember that we saw it.  */
15809       ellipsis_p = true;
15810     }
15811   else
15812     ellipsis_p = false;
15813
15814   /* Finish the parameter list.  */
15815   if (!ellipsis_p)
15816     parameters = chainon (parameters, void_list_node);
15817
15818   return parameters;
15819 }
15820
15821 /* Parse a parameter-declaration-list.
15822
15823    parameter-declaration-list:
15824      parameter-declaration
15825      parameter-declaration-list , parameter-declaration
15826
15827    Returns a representation of the parameter-declaration-list, as for
15828    cp_parser_parameter_declaration_clause.  However, the
15829    `void_list_node' is never appended to the list.  Upon return,
15830    *IS_ERROR will be true iff an error occurred.  */
15831
15832 static tree
15833 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
15834 {
15835   tree parameters = NULL_TREE;
15836   tree *tail = &parameters; 
15837   bool saved_in_unbraced_linkage_specification_p;
15838   int index = 0;
15839
15840   /* Assume all will go well.  */
15841   *is_error = false;
15842   /* The special considerations that apply to a function within an
15843      unbraced linkage specifications do not apply to the parameters
15844      to the function.  */
15845   saved_in_unbraced_linkage_specification_p 
15846     = parser->in_unbraced_linkage_specification_p;
15847   parser->in_unbraced_linkage_specification_p = false;
15848
15849   /* Look for more parameters.  */
15850   while (true)
15851     {
15852       cp_parameter_declarator *parameter;
15853       tree decl = error_mark_node;
15854       bool parenthesized_p;
15855       /* Parse the parameter.  */
15856       parameter
15857         = cp_parser_parameter_declaration (parser,
15858                                            /*template_parm_p=*/false,
15859                                            &parenthesized_p);
15860
15861       /* We don't know yet if the enclosing context is deprecated, so wait
15862          and warn in grokparms if appropriate.  */
15863       deprecated_state = DEPRECATED_SUPPRESS;
15864
15865       if (parameter)
15866         decl = grokdeclarator (parameter->declarator,
15867                                &parameter->decl_specifiers,
15868                                PARM,
15869                                parameter->default_argument != NULL_TREE,
15870                                &parameter->decl_specifiers.attributes);
15871
15872       deprecated_state = DEPRECATED_NORMAL;
15873
15874       /* If a parse error occurred parsing the parameter declaration,
15875          then the entire parameter-declaration-list is erroneous.  */
15876       if (decl == error_mark_node)
15877         {
15878           *is_error = true;
15879           parameters = error_mark_node;
15880           break;
15881         }
15882
15883       if (parameter->decl_specifiers.attributes)
15884         cplus_decl_attributes (&decl,
15885                                parameter->decl_specifiers.attributes,
15886                                0);
15887       if (DECL_NAME (decl))
15888         decl = pushdecl (decl);
15889
15890       if (decl != error_mark_node)
15891         {
15892           retrofit_lang_decl (decl);
15893           DECL_PARM_INDEX (decl) = ++index;
15894           DECL_PARM_LEVEL (decl) = function_parm_depth ();
15895         }
15896
15897       /* Add the new parameter to the list.  */
15898       *tail = build_tree_list (parameter->default_argument, decl);
15899       tail = &TREE_CHAIN (*tail);
15900
15901       /* Peek at the next token.  */
15902       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
15903           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
15904           /* These are for Objective-C++ */
15905           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
15906           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
15907         /* The parameter-declaration-list is complete.  */
15908         break;
15909       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
15910         {
15911           cp_token *token;
15912
15913           /* Peek at the next token.  */
15914           token = cp_lexer_peek_nth_token (parser->lexer, 2);
15915           /* If it's an ellipsis, then the list is complete.  */
15916           if (token->type == CPP_ELLIPSIS)
15917             break;
15918           /* Otherwise, there must be more parameters.  Consume the
15919              `,'.  */
15920           cp_lexer_consume_token (parser->lexer);
15921           /* When parsing something like:
15922
15923                 int i(float f, double d)
15924
15925              we can tell after seeing the declaration for "f" that we
15926              are not looking at an initialization of a variable "i",
15927              but rather at the declaration of a function "i".
15928
15929              Due to the fact that the parsing of template arguments
15930              (as specified to a template-id) requires backtracking we
15931              cannot use this technique when inside a template argument
15932              list.  */
15933           if (!parser->in_template_argument_list_p
15934               && !parser->in_type_id_in_expr_p
15935               && cp_parser_uncommitted_to_tentative_parse_p (parser)
15936               /* However, a parameter-declaration of the form
15937                  "foat(f)" (which is a valid declaration of a
15938                  parameter "f") can also be interpreted as an
15939                  expression (the conversion of "f" to "float").  */
15940               && !parenthesized_p)
15941             cp_parser_commit_to_tentative_parse (parser);
15942         }
15943       else
15944         {
15945           cp_parser_error (parser, "expected %<,%> or %<...%>");
15946           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
15947             cp_parser_skip_to_closing_parenthesis (parser,
15948                                                    /*recovering=*/true,
15949                                                    /*or_comma=*/false,
15950                                                    /*consume_paren=*/false);
15951           break;
15952         }
15953     }
15954
15955   parser->in_unbraced_linkage_specification_p
15956     = saved_in_unbraced_linkage_specification_p;
15957
15958   return parameters;
15959 }
15960
15961 /* Parse a parameter declaration.
15962
15963    parameter-declaration:
15964      decl-specifier-seq ... [opt] declarator
15965      decl-specifier-seq declarator = assignment-expression
15966      decl-specifier-seq ... [opt] abstract-declarator [opt]
15967      decl-specifier-seq abstract-declarator [opt] = assignment-expression
15968
15969    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
15970    declares a template parameter.  (In that case, a non-nested `>'
15971    token encountered during the parsing of the assignment-expression
15972    is not interpreted as a greater-than operator.)
15973
15974    Returns a representation of the parameter, or NULL if an error
15975    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
15976    true iff the declarator is of the form "(p)".  */
15977
15978 static cp_parameter_declarator *
15979 cp_parser_parameter_declaration (cp_parser *parser,
15980                                  bool template_parm_p,
15981                                  bool *parenthesized_p)
15982 {
15983   int declares_class_or_enum;
15984   cp_decl_specifier_seq decl_specifiers;
15985   cp_declarator *declarator;
15986   tree default_argument;
15987   cp_token *token = NULL, *declarator_token_start = NULL;
15988   const char *saved_message;
15989
15990   /* In a template parameter, `>' is not an operator.
15991
15992      [temp.param]
15993
15994      When parsing a default template-argument for a non-type
15995      template-parameter, the first non-nested `>' is taken as the end
15996      of the template parameter-list rather than a greater-than
15997      operator.  */
15998
15999   /* Type definitions may not appear in parameter types.  */
16000   saved_message = parser->type_definition_forbidden_message;
16001   parser->type_definition_forbidden_message
16002     = G_("types may not be defined in parameter types");
16003
16004   /* Parse the declaration-specifiers.  */
16005   cp_parser_decl_specifier_seq (parser,
16006                                 CP_PARSER_FLAGS_NONE,
16007                                 &decl_specifiers,
16008                                 &declares_class_or_enum);
16009
16010   /* Complain about missing 'typename' or other invalid type names.  */
16011   if (!decl_specifiers.any_type_specifiers_p)
16012     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16013
16014   /* If an error occurred, there's no reason to attempt to parse the
16015      rest of the declaration.  */
16016   if (cp_parser_error_occurred (parser))
16017     {
16018       parser->type_definition_forbidden_message = saved_message;
16019       return NULL;
16020     }
16021
16022   /* Peek at the next token.  */
16023   token = cp_lexer_peek_token (parser->lexer);
16024
16025   /* If the next token is a `)', `,', `=', `>', or `...', then there
16026      is no declarator. However, when variadic templates are enabled,
16027      there may be a declarator following `...'.  */
16028   if (token->type == CPP_CLOSE_PAREN
16029       || token->type == CPP_COMMA
16030       || token->type == CPP_EQ
16031       || token->type == CPP_GREATER)
16032     {
16033       declarator = NULL;
16034       if (parenthesized_p)
16035         *parenthesized_p = false;
16036     }
16037   /* Otherwise, there should be a declarator.  */
16038   else
16039     {
16040       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16041       parser->default_arg_ok_p = false;
16042
16043       /* After seeing a decl-specifier-seq, if the next token is not a
16044          "(", there is no possibility that the code is a valid
16045          expression.  Therefore, if parsing tentatively, we commit at
16046          this point.  */
16047       if (!parser->in_template_argument_list_p
16048           /* In an expression context, having seen:
16049
16050                (int((char ...
16051
16052              we cannot be sure whether we are looking at a
16053              function-type (taking a "char" as a parameter) or a cast
16054              of some object of type "char" to "int".  */
16055           && !parser->in_type_id_in_expr_p
16056           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16057           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16058           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16059         cp_parser_commit_to_tentative_parse (parser);
16060       /* Parse the declarator.  */
16061       declarator_token_start = token;
16062       declarator = cp_parser_declarator (parser,
16063                                          CP_PARSER_DECLARATOR_EITHER,
16064                                          /*ctor_dtor_or_conv_p=*/NULL,
16065                                          parenthesized_p,
16066                                          /*member_p=*/false);
16067       parser->default_arg_ok_p = saved_default_arg_ok_p;
16068       /* After the declarator, allow more attributes.  */
16069       decl_specifiers.attributes
16070         = chainon (decl_specifiers.attributes,
16071                    cp_parser_attributes_opt (parser));
16072     }
16073
16074   /* If the next token is an ellipsis, and we have not seen a
16075      declarator name, and the type of the declarator contains parameter
16076      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16077      a parameter pack expansion expression. Otherwise, leave the
16078      ellipsis for a C-style variadic function. */
16079   token = cp_lexer_peek_token (parser->lexer);
16080   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16081     {
16082       tree type = decl_specifiers.type;
16083
16084       if (type && DECL_P (type))
16085         type = TREE_TYPE (type);
16086
16087       if (type
16088           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16089           && declarator_can_be_parameter_pack (declarator)
16090           && (!declarator || !declarator->parameter_pack_p)
16091           && uses_parameter_packs (type))
16092         {
16093           /* Consume the `...'. */
16094           cp_lexer_consume_token (parser->lexer);
16095           maybe_warn_variadic_templates ();
16096           
16097           /* Build a pack expansion type */
16098           if (declarator)
16099             declarator->parameter_pack_p = true;
16100           else
16101             decl_specifiers.type = make_pack_expansion (type);
16102         }
16103     }
16104
16105   /* The restriction on defining new types applies only to the type
16106      of the parameter, not to the default argument.  */
16107   parser->type_definition_forbidden_message = saved_message;
16108
16109   /* If the next token is `=', then process a default argument.  */
16110   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16111     {
16112       /* Consume the `='.  */
16113       cp_lexer_consume_token (parser->lexer);
16114
16115       /* If we are defining a class, then the tokens that make up the
16116          default argument must be saved and processed later.  */
16117       if (!template_parm_p && at_class_scope_p ()
16118           && TYPE_BEING_DEFINED (current_class_type)
16119           && !LAMBDA_TYPE_P (current_class_type))
16120         {
16121           unsigned depth = 0;
16122           int maybe_template_id = 0;
16123           cp_token *first_token;
16124           cp_token *token;
16125
16126           /* Add tokens until we have processed the entire default
16127              argument.  We add the range [first_token, token).  */
16128           first_token = cp_lexer_peek_token (parser->lexer);
16129           while (true)
16130             {
16131               bool done = false;
16132
16133               /* Peek at the next token.  */
16134               token = cp_lexer_peek_token (parser->lexer);
16135               /* What we do depends on what token we have.  */
16136               switch (token->type)
16137                 {
16138                   /* In valid code, a default argument must be
16139                      immediately followed by a `,' `)', or `...'.  */
16140                 case CPP_COMMA:
16141                   if (depth == 0 && maybe_template_id)
16142                     {
16143                       /* If we've seen a '<', we might be in a
16144                          template-argument-list.  Until Core issue 325 is
16145                          resolved, we don't know how this situation ought
16146                          to be handled, so try to DTRT.  We check whether
16147                          what comes after the comma is a valid parameter
16148                          declaration list.  If it is, then the comma ends
16149                          the default argument; otherwise the default
16150                          argument continues.  */
16151                       bool error = false;
16152                       tree t;
16153
16154                       /* Set ITALP so cp_parser_parameter_declaration_list
16155                          doesn't decide to commit to this parse.  */
16156                       bool saved_italp = parser->in_template_argument_list_p;
16157                       parser->in_template_argument_list_p = true;
16158
16159                       cp_parser_parse_tentatively (parser);
16160                       cp_lexer_consume_token (parser->lexer);
16161                       begin_scope (sk_function_parms, NULL_TREE);
16162                       cp_parser_parameter_declaration_list (parser, &error);
16163                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16164                         pop_binding (DECL_NAME (t), t);
16165                       leave_scope ();
16166                       if (!cp_parser_error_occurred (parser) && !error)
16167                         done = true;
16168                       cp_parser_abort_tentative_parse (parser);
16169
16170                       parser->in_template_argument_list_p = saved_italp;
16171                       break;
16172                     }
16173                 case CPP_CLOSE_PAREN:
16174                 case CPP_ELLIPSIS:
16175                   /* If we run into a non-nested `;', `}', or `]',
16176                      then the code is invalid -- but the default
16177                      argument is certainly over.  */
16178                 case CPP_SEMICOLON:
16179                 case CPP_CLOSE_BRACE:
16180                 case CPP_CLOSE_SQUARE:
16181                   if (depth == 0)
16182                     done = true;
16183                   /* Update DEPTH, if necessary.  */
16184                   else if (token->type == CPP_CLOSE_PAREN
16185                            || token->type == CPP_CLOSE_BRACE
16186                            || token->type == CPP_CLOSE_SQUARE)
16187                     --depth;
16188                   break;
16189
16190                 case CPP_OPEN_PAREN:
16191                 case CPP_OPEN_SQUARE:
16192                 case CPP_OPEN_BRACE:
16193                   ++depth;
16194                   break;
16195
16196                 case CPP_LESS:
16197                   if (depth == 0)
16198                     /* This might be the comparison operator, or it might
16199                        start a template argument list.  */
16200                     ++maybe_template_id;
16201                   break;
16202
16203                 case CPP_RSHIFT:
16204                   if (cxx_dialect == cxx98)
16205                     break;
16206                   /* Fall through for C++0x, which treats the `>>'
16207                      operator like two `>' tokens in certain
16208                      cases.  */
16209
16210                 case CPP_GREATER:
16211                   if (depth == 0)
16212                     {
16213                       /* This might be an operator, or it might close a
16214                          template argument list.  But if a previous '<'
16215                          started a template argument list, this will have
16216                          closed it, so we can't be in one anymore.  */
16217                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
16218                       if (maybe_template_id < 0)
16219                         maybe_template_id = 0;
16220                     }
16221                   break;
16222
16223                   /* If we run out of tokens, issue an error message.  */
16224                 case CPP_EOF:
16225                 case CPP_PRAGMA_EOL:
16226                   error_at (token->location, "file ends in default argument");
16227                   done = true;
16228                   break;
16229
16230                 case CPP_NAME:
16231                 case CPP_SCOPE:
16232                   /* In these cases, we should look for template-ids.
16233                      For example, if the default argument is
16234                      `X<int, double>()', we need to do name lookup to
16235                      figure out whether or not `X' is a template; if
16236                      so, the `,' does not end the default argument.
16237
16238                      That is not yet done.  */
16239                   break;
16240
16241                 default:
16242                   break;
16243                 }
16244
16245               /* If we've reached the end, stop.  */
16246               if (done)
16247                 break;
16248
16249               /* Add the token to the token block.  */
16250               token = cp_lexer_consume_token (parser->lexer);
16251             }
16252
16253           /* Create a DEFAULT_ARG to represent the unparsed default
16254              argument.  */
16255           default_argument = make_node (DEFAULT_ARG);
16256           DEFARG_TOKENS (default_argument)
16257             = cp_token_cache_new (first_token, token);
16258           DEFARG_INSTANTIATIONS (default_argument) = NULL;
16259         }
16260       /* Outside of a class definition, we can just parse the
16261          assignment-expression.  */
16262       else
16263         {
16264           token = cp_lexer_peek_token (parser->lexer);
16265           default_argument 
16266             = cp_parser_default_argument (parser, template_parm_p);
16267         }
16268
16269       if (!parser->default_arg_ok_p)
16270         {
16271           if (flag_permissive)
16272             warning (0, "deprecated use of default argument for parameter of non-function");
16273           else
16274             {
16275               error_at (token->location,
16276                         "default arguments are only "
16277                         "permitted for function parameters");
16278               default_argument = NULL_TREE;
16279             }
16280         }
16281       else if ((declarator && declarator->parameter_pack_p)
16282                || (decl_specifiers.type
16283                    && PACK_EXPANSION_P (decl_specifiers.type)))
16284         {
16285           /* Find the name of the parameter pack.  */     
16286           cp_declarator *id_declarator = declarator;
16287           while (id_declarator && id_declarator->kind != cdk_id)
16288             id_declarator = id_declarator->declarator;
16289           
16290           if (id_declarator && id_declarator->kind == cdk_id)
16291             error_at (declarator_token_start->location,
16292                       template_parm_p 
16293                       ? "template parameter pack %qD"
16294                       " cannot have a default argument"
16295                       : "parameter pack %qD cannot have a default argument",
16296                       id_declarator->u.id.unqualified_name);
16297           else
16298             error_at (declarator_token_start->location,
16299                       template_parm_p 
16300                       ? "template parameter pack cannot have a default argument"
16301                       : "parameter pack cannot have a default argument");
16302           
16303           default_argument = NULL_TREE;
16304         }
16305     }
16306   else
16307     default_argument = NULL_TREE;
16308
16309   return make_parameter_declarator (&decl_specifiers,
16310                                     declarator,
16311                                     default_argument);
16312 }
16313
16314 /* Parse a default argument and return it.
16315
16316    TEMPLATE_PARM_P is true if this is a default argument for a
16317    non-type template parameter.  */
16318 static tree
16319 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
16320 {
16321   tree default_argument = NULL_TREE;
16322   bool saved_greater_than_is_operator_p;
16323   bool saved_local_variables_forbidden_p;
16324
16325   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
16326      set correctly.  */
16327   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
16328   parser->greater_than_is_operator_p = !template_parm_p;
16329   /* Local variable names (and the `this' keyword) may not
16330      appear in a default argument.  */
16331   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
16332   parser->local_variables_forbidden_p = true;
16333   /* Parse the assignment-expression.  */
16334   if (template_parm_p)
16335     push_deferring_access_checks (dk_no_deferred);
16336   default_argument
16337     = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
16338   if (template_parm_p)
16339     pop_deferring_access_checks ();
16340   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
16341   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
16342
16343   return default_argument;
16344 }
16345
16346 /* Parse a function-body.
16347
16348    function-body:
16349      compound_statement  */
16350
16351 static void
16352 cp_parser_function_body (cp_parser *parser)
16353 {
16354   cp_parser_compound_statement (parser, NULL, false, true);
16355 }
16356
16357 /* Parse a ctor-initializer-opt followed by a function-body.  Return
16358    true if a ctor-initializer was present.  */
16359
16360 static bool
16361 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
16362 {
16363   tree body, list;
16364   bool ctor_initializer_p;
16365   const bool check_body_p =
16366      DECL_CONSTRUCTOR_P (current_function_decl)
16367      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
16368   tree last = NULL;
16369
16370   /* Begin the function body.  */
16371   body = begin_function_body ();
16372   /* Parse the optional ctor-initializer.  */
16373   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
16374
16375   /* If we're parsing a constexpr constructor definition, we need
16376      to check that the constructor body is indeed empty.  However,
16377      before we get to cp_parser_function_body lot of junk has been
16378      generated, so we can't just check that we have an empty block.
16379      Rather we take a snapshot of the outermost block, and check whether
16380      cp_parser_function_body changed its state.  */
16381   if (check_body_p)
16382     {
16383       list = body;
16384       if (TREE_CODE (list) == BIND_EXPR)
16385         list = BIND_EXPR_BODY (list);
16386       if (TREE_CODE (list) == STATEMENT_LIST
16387           && STATEMENT_LIST_TAIL (list) != NULL)
16388         last = STATEMENT_LIST_TAIL (list)->stmt;
16389     }
16390   /* Parse the function-body.  */
16391   cp_parser_function_body (parser);
16392   if (check_body_p)
16393     check_constexpr_ctor_body (last, list);
16394   /* Finish the function body.  */
16395   finish_function_body (body);
16396
16397   return ctor_initializer_p;
16398 }
16399
16400 /* Parse an initializer.
16401
16402    initializer:
16403      = initializer-clause
16404      ( expression-list )
16405
16406    Returns an expression representing the initializer.  If no
16407    initializer is present, NULL_TREE is returned.
16408
16409    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
16410    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
16411    set to TRUE if there is no initializer present.  If there is an
16412    initializer, and it is not a constant-expression, *NON_CONSTANT_P
16413    is set to true; otherwise it is set to false.  */
16414
16415 static tree
16416 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
16417                        bool* non_constant_p)
16418 {
16419   cp_token *token;
16420   tree init;
16421
16422   /* Peek at the next token.  */
16423   token = cp_lexer_peek_token (parser->lexer);
16424
16425   /* Let our caller know whether or not this initializer was
16426      parenthesized.  */
16427   *is_direct_init = (token->type != CPP_EQ);
16428   /* Assume that the initializer is constant.  */
16429   *non_constant_p = false;
16430
16431   if (token->type == CPP_EQ)
16432     {
16433       /* Consume the `='.  */
16434       cp_lexer_consume_token (parser->lexer);
16435       /* Parse the initializer-clause.  */
16436       init = cp_parser_initializer_clause (parser, non_constant_p);
16437     }
16438   else if (token->type == CPP_OPEN_PAREN)
16439     {
16440       VEC(tree,gc) *vec;
16441       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
16442                                                      /*cast_p=*/false,
16443                                                      /*allow_expansion_p=*/true,
16444                                                      non_constant_p);
16445       if (vec == NULL)
16446         return error_mark_node;
16447       init = build_tree_list_vec (vec);
16448       release_tree_vector (vec);
16449     }
16450   else if (token->type == CPP_OPEN_BRACE)
16451     {
16452       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
16453       init = cp_parser_braced_list (parser, non_constant_p);
16454       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
16455     }
16456   else
16457     {
16458       /* Anything else is an error.  */
16459       cp_parser_error (parser, "expected initializer");
16460       init = error_mark_node;
16461     }
16462
16463   return init;
16464 }
16465
16466 /* Parse an initializer-clause.
16467
16468    initializer-clause:
16469      assignment-expression
16470      braced-init-list
16471
16472    Returns an expression representing the initializer.
16473
16474    If the `assignment-expression' production is used the value
16475    returned is simply a representation for the expression.
16476
16477    Otherwise, calls cp_parser_braced_list.  */
16478
16479 static tree
16480 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
16481 {
16482   tree initializer;
16483
16484   /* Assume the expression is constant.  */
16485   *non_constant_p = false;
16486
16487   /* If it is not a `{', then we are looking at an
16488      assignment-expression.  */
16489   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
16490     {
16491       initializer
16492         = cp_parser_constant_expression (parser,
16493                                         /*allow_non_constant_p=*/true,
16494                                         non_constant_p);
16495       if (!*non_constant_p)
16496         {
16497           /* We only want to fold if this is really a constant
16498              expression.  FIXME Actually, we don't want to fold here, but in
16499              cp_finish_decl.  */
16500           tree folded = fold_non_dependent_expr (initializer);
16501           folded = maybe_constant_value (folded);
16502           if (TREE_CONSTANT (folded))
16503             initializer = folded;
16504         }
16505     }
16506   else
16507     initializer = cp_parser_braced_list (parser, non_constant_p);
16508
16509   return initializer;
16510 }
16511
16512 /* Parse a brace-enclosed initializer list.
16513
16514    braced-init-list:
16515      { initializer-list , [opt] }
16516      { }
16517
16518    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
16519    the elements of the initializer-list (or NULL, if the last
16520    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
16521    NULL_TREE.  There is no way to detect whether or not the optional
16522    trailing `,' was provided.  NON_CONSTANT_P is as for
16523    cp_parser_initializer.  */     
16524
16525 static tree
16526 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
16527 {
16528   tree initializer;
16529
16530   /* Consume the `{' token.  */
16531   cp_lexer_consume_token (parser->lexer);
16532   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
16533   initializer = make_node (CONSTRUCTOR);
16534   /* If it's not a `}', then there is a non-trivial initializer.  */
16535   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
16536     {
16537       /* Parse the initializer list.  */
16538       CONSTRUCTOR_ELTS (initializer)
16539         = cp_parser_initializer_list (parser, non_constant_p);
16540       /* A trailing `,' token is allowed.  */
16541       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16542         cp_lexer_consume_token (parser->lexer);
16543     }
16544   /* Now, there should be a trailing `}'.  */
16545   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16546   TREE_TYPE (initializer) = init_list_type_node;
16547   return initializer;
16548 }
16549
16550 /* Parse an initializer-list.
16551
16552    initializer-list:
16553      initializer-clause ... [opt]
16554      initializer-list , initializer-clause ... [opt]
16555
16556    GNU Extension:
16557
16558    initializer-list:
16559      identifier : initializer-clause
16560      initializer-list, identifier : initializer-clause
16561
16562    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
16563    for the initializer.  If the INDEX of the elt is non-NULL, it is the
16564    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
16565    as for cp_parser_initializer.  */
16566
16567 static VEC(constructor_elt,gc) *
16568 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
16569 {
16570   VEC(constructor_elt,gc) *v = NULL;
16571
16572   /* Assume all of the expressions are constant.  */
16573   *non_constant_p = false;
16574
16575   /* Parse the rest of the list.  */
16576   while (true)
16577     {
16578       cp_token *token;
16579       tree identifier;
16580       tree initializer;
16581       bool clause_non_constant_p;
16582
16583       /* If the next token is an identifier and the following one is a
16584          colon, we are looking at the GNU designated-initializer
16585          syntax.  */
16586       if (cp_parser_allow_gnu_extensions_p (parser)
16587           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
16588           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
16589         {
16590           /* Warn the user that they are using an extension.  */
16591           pedwarn (input_location, OPT_pedantic, 
16592                    "ISO C++ does not allow designated initializers");
16593           /* Consume the identifier.  */
16594           identifier = cp_lexer_consume_token (parser->lexer)->u.value;
16595           /* Consume the `:'.  */
16596           cp_lexer_consume_token (parser->lexer);
16597         }
16598       else
16599         identifier = NULL_TREE;
16600
16601       /* Parse the initializer.  */
16602       initializer = cp_parser_initializer_clause (parser,
16603                                                   &clause_non_constant_p);
16604       /* If any clause is non-constant, so is the entire initializer.  */
16605       if (clause_non_constant_p)
16606         *non_constant_p = true;
16607
16608       /* If we have an ellipsis, this is an initializer pack
16609          expansion.  */
16610       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16611         {
16612           /* Consume the `...'.  */
16613           cp_lexer_consume_token (parser->lexer);
16614
16615           /* Turn the initializer into an initializer expansion.  */
16616           initializer = make_pack_expansion (initializer);
16617         }
16618
16619       /* Add it to the vector.  */
16620       CONSTRUCTOR_APPEND_ELT(v, identifier, initializer);
16621
16622       /* If the next token is not a comma, we have reached the end of
16623          the list.  */
16624       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
16625         break;
16626
16627       /* Peek at the next token.  */
16628       token = cp_lexer_peek_nth_token (parser->lexer, 2);
16629       /* If the next token is a `}', then we're still done.  An
16630          initializer-clause can have a trailing `,' after the
16631          initializer-list and before the closing `}'.  */
16632       if (token->type == CPP_CLOSE_BRACE)
16633         break;
16634
16635       /* Consume the `,' token.  */
16636       cp_lexer_consume_token (parser->lexer);
16637     }
16638
16639   return v;
16640 }
16641
16642 /* Classes [gram.class] */
16643
16644 /* Parse a class-name.
16645
16646    class-name:
16647      identifier
16648      template-id
16649
16650    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
16651    to indicate that names looked up in dependent types should be
16652    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
16653    keyword has been used to indicate that the name that appears next
16654    is a template.  TAG_TYPE indicates the explicit tag given before
16655    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
16656    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
16657    is the class being defined in a class-head.
16658
16659    Returns the TYPE_DECL representing the class.  */
16660
16661 static tree
16662 cp_parser_class_name (cp_parser *parser,
16663                       bool typename_keyword_p,
16664                       bool template_keyword_p,
16665                       enum tag_types tag_type,
16666                       bool check_dependency_p,
16667                       bool class_head_p,
16668                       bool is_declaration)
16669 {
16670   tree decl;
16671   tree scope;
16672   bool typename_p;
16673   cp_token *token;
16674   tree identifier = NULL_TREE;
16675
16676   /* All class-names start with an identifier.  */
16677   token = cp_lexer_peek_token (parser->lexer);
16678   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
16679     {
16680       cp_parser_error (parser, "expected class-name");
16681       return error_mark_node;
16682     }
16683
16684   /* PARSER->SCOPE can be cleared when parsing the template-arguments
16685      to a template-id, so we save it here.  */
16686   scope = parser->scope;
16687   if (scope == error_mark_node)
16688     return error_mark_node;
16689
16690   /* Any name names a type if we're following the `typename' keyword
16691      in a qualified name where the enclosing scope is type-dependent.  */
16692   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
16693                 && dependent_type_p (scope));
16694   /* Handle the common case (an identifier, but not a template-id)
16695      efficiently.  */
16696   if (token->type == CPP_NAME
16697       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
16698     {
16699       cp_token *identifier_token;
16700       bool ambiguous_p;
16701
16702       /* Look for the identifier.  */
16703       identifier_token = cp_lexer_peek_token (parser->lexer);
16704       ambiguous_p = identifier_token->ambiguous_p;
16705       identifier = cp_parser_identifier (parser);
16706       /* If the next token isn't an identifier, we are certainly not
16707          looking at a class-name.  */
16708       if (identifier == error_mark_node)
16709         decl = error_mark_node;
16710       /* If we know this is a type-name, there's no need to look it
16711          up.  */
16712       else if (typename_p)
16713         decl = identifier;
16714       else
16715         {
16716           tree ambiguous_decls;
16717           /* If we already know that this lookup is ambiguous, then
16718              we've already issued an error message; there's no reason
16719              to check again.  */
16720           if (ambiguous_p)
16721             {
16722               cp_parser_simulate_error (parser);
16723               return error_mark_node;
16724             }
16725           /* If the next token is a `::', then the name must be a type
16726              name.
16727
16728              [basic.lookup.qual]
16729
16730              During the lookup for a name preceding the :: scope
16731              resolution operator, object, function, and enumerator
16732              names are ignored.  */
16733           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16734             tag_type = typename_type;
16735           /* Look up the name.  */
16736           decl = cp_parser_lookup_name (parser, identifier,
16737                                         tag_type,
16738                                         /*is_template=*/false,
16739                                         /*is_namespace=*/false,
16740                                         check_dependency_p,
16741                                         &ambiguous_decls,
16742                                         identifier_token->location);
16743           if (ambiguous_decls)
16744             {
16745               if (cp_parser_parsing_tentatively (parser))
16746                 cp_parser_simulate_error (parser);
16747               return error_mark_node;
16748             }
16749         }
16750     }
16751   else
16752     {
16753       /* Try a template-id.  */
16754       decl = cp_parser_template_id (parser, template_keyword_p,
16755                                     check_dependency_p,
16756                                     is_declaration);
16757       if (decl == error_mark_node)
16758         return error_mark_node;
16759     }
16760
16761   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
16762
16763   /* If this is a typename, create a TYPENAME_TYPE.  */
16764   if (typename_p && decl != error_mark_node)
16765     {
16766       decl = make_typename_type (scope, decl, typename_type,
16767                                  /*complain=*/tf_error);
16768       if (decl != error_mark_node)
16769         decl = TYPE_NAME (decl);
16770     }
16771
16772   /* Check to see that it is really the name of a class.  */
16773   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
16774       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
16775       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
16776     /* Situations like this:
16777
16778          template <typename T> struct A {
16779            typename T::template X<int>::I i;
16780          };
16781
16782        are problematic.  Is `T::template X<int>' a class-name?  The
16783        standard does not seem to be definitive, but there is no other
16784        valid interpretation of the following `::'.  Therefore, those
16785        names are considered class-names.  */
16786     {
16787       decl = make_typename_type (scope, decl, tag_type, tf_error);
16788       if (decl != error_mark_node)
16789         decl = TYPE_NAME (decl);
16790     }
16791   else if (TREE_CODE (decl) != TYPE_DECL
16792            || TREE_TYPE (decl) == error_mark_node
16793            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
16794            /* In Objective-C 2.0, a classname followed by '.' starts a
16795               dot-syntax expression, and it's not a type-name.  */
16796            || (c_dialect_objc ()
16797                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
16798                && objc_is_class_name (decl)))
16799     decl = error_mark_node;
16800
16801   if (decl == error_mark_node)
16802     cp_parser_error (parser, "expected class-name");
16803   else if (identifier && !parser->scope)
16804     maybe_note_name_used_in_class (identifier, decl);
16805
16806   return decl;
16807 }
16808
16809 /* Parse a class-specifier.
16810
16811    class-specifier:
16812      class-head { member-specification [opt] }
16813
16814    Returns the TREE_TYPE representing the class.  */
16815
16816 static tree
16817 cp_parser_class_specifier_1 (cp_parser* parser)
16818 {
16819   tree type;
16820   tree attributes = NULL_TREE;
16821   bool nested_name_specifier_p;
16822   unsigned saved_num_template_parameter_lists;
16823   bool saved_in_function_body;
16824   bool saved_in_unbraced_linkage_specification_p;
16825   tree old_scope = NULL_TREE;
16826   tree scope = NULL_TREE;
16827   tree bases;
16828   cp_token *closing_brace;
16829
16830   push_deferring_access_checks (dk_no_deferred);
16831
16832   /* Parse the class-head.  */
16833   type = cp_parser_class_head (parser,
16834                                &nested_name_specifier_p,
16835                                &attributes,
16836                                &bases);
16837   /* If the class-head was a semantic disaster, skip the entire body
16838      of the class.  */
16839   if (!type)
16840     {
16841       cp_parser_skip_to_end_of_block_or_statement (parser);
16842       pop_deferring_access_checks ();
16843       return error_mark_node;
16844     }
16845
16846   /* Look for the `{'.  */
16847   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
16848     {
16849       pop_deferring_access_checks ();
16850       return error_mark_node;
16851     }
16852
16853   /* Process the base classes. If they're invalid, skip the 
16854      entire class body.  */
16855   if (!xref_basetypes (type, bases))
16856     {
16857       /* Consuming the closing brace yields better error messages
16858          later on.  */
16859       if (cp_parser_skip_to_closing_brace (parser))
16860         cp_lexer_consume_token (parser->lexer);
16861       pop_deferring_access_checks ();
16862       return error_mark_node;
16863     }
16864
16865   /* Issue an error message if type-definitions are forbidden here.  */
16866   cp_parser_check_type_definition (parser);
16867   /* Remember that we are defining one more class.  */
16868   ++parser->num_classes_being_defined;
16869   /* Inside the class, surrounding template-parameter-lists do not
16870      apply.  */
16871   saved_num_template_parameter_lists
16872     = parser->num_template_parameter_lists;
16873   parser->num_template_parameter_lists = 0;
16874   /* We are not in a function body.  */
16875   saved_in_function_body = parser->in_function_body;
16876   parser->in_function_body = false;
16877   /* We are not immediately inside an extern "lang" block.  */
16878   saved_in_unbraced_linkage_specification_p
16879     = parser->in_unbraced_linkage_specification_p;
16880   parser->in_unbraced_linkage_specification_p = false;
16881
16882   /* Start the class.  */
16883   if (nested_name_specifier_p)
16884     {
16885       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
16886       old_scope = push_inner_scope (scope);
16887     }
16888   type = begin_class_definition (type, attributes);
16889
16890   if (type == error_mark_node)
16891     /* If the type is erroneous, skip the entire body of the class.  */
16892     cp_parser_skip_to_closing_brace (parser);
16893   else
16894     /* Parse the member-specification.  */
16895     cp_parser_member_specification_opt (parser);
16896
16897   /* Look for the trailing `}'.  */
16898   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
16899   /* Look for trailing attributes to apply to this class.  */
16900   if (cp_parser_allow_gnu_extensions_p (parser))
16901     attributes = cp_parser_attributes_opt (parser);
16902   if (type != error_mark_node)
16903     type = finish_struct (type, attributes);
16904   if (nested_name_specifier_p)
16905     pop_inner_scope (old_scope, scope);
16906
16907   /* We've finished a type definition.  Check for the common syntax
16908      error of forgetting a semicolon after the definition.  We need to
16909      be careful, as we can't just check for not-a-semicolon and be done
16910      with it; the user might have typed:
16911
16912      class X { } c = ...;
16913      class X { } *p = ...;
16914
16915      and so forth.  Instead, enumerate all the possible tokens that
16916      might follow this production; if we don't see one of them, then
16917      complain and silently insert the semicolon.  */
16918   {
16919     cp_token *token = cp_lexer_peek_token (parser->lexer);
16920     bool want_semicolon = true;
16921
16922     switch (token->type)
16923       {
16924       case CPP_NAME:
16925       case CPP_SEMICOLON:
16926       case CPP_MULT:
16927       case CPP_AND:
16928       case CPP_OPEN_PAREN:
16929       case CPP_CLOSE_PAREN:
16930       case CPP_COMMA:
16931         want_semicolon = false;
16932         break;
16933
16934         /* While it's legal for type qualifiers and storage class
16935            specifiers to follow type definitions in the grammar, only
16936            compiler testsuites contain code like that.  Assume that if
16937            we see such code, then what we're really seeing is a case
16938            like:
16939
16940            class X { }
16941            const <type> var = ...;
16942
16943            or
16944
16945            class Y { }
16946            static <type> func (...) ...
16947
16948            i.e. the qualifier or specifier applies to the next
16949            declaration.  To do so, however, we need to look ahead one
16950            more token to see if *that* token is a type specifier.
16951
16952            This code could be improved to handle:
16953
16954            class Z { }
16955            static const <type> var = ...;  */
16956       case CPP_KEYWORD:
16957         if (keyword_is_decl_specifier (token->keyword))
16958           {
16959             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
16960
16961             /* Handling user-defined types here would be nice, but very
16962                tricky.  */
16963             want_semicolon
16964               = (lookahead->type == CPP_KEYWORD
16965                  && keyword_begins_type_specifier (lookahead->keyword));
16966           }
16967         break;
16968       default:
16969         break;
16970       }
16971
16972     /* If we don't have a type, then something is very wrong and we
16973        shouldn't try to do anything clever.  Likewise for not seeing the
16974        closing brace.  */
16975     if (closing_brace && TYPE_P (type) && want_semicolon)
16976       {
16977         cp_token_position prev
16978           = cp_lexer_previous_token_position (parser->lexer);
16979         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
16980         location_t loc = prev_token->location;
16981
16982         if (CLASSTYPE_DECLARED_CLASS (type))
16983           error_at (loc, "expected %<;%> after class definition");
16984         else if (TREE_CODE (type) == RECORD_TYPE)
16985           error_at (loc, "expected %<;%> after struct definition");
16986         else if (TREE_CODE (type) == UNION_TYPE)
16987           error_at (loc, "expected %<;%> after union definition");
16988         else
16989           gcc_unreachable ();
16990
16991         /* Unget one token and smash it to look as though we encountered
16992            a semicolon in the input stream.  */
16993         cp_lexer_set_token_position (parser->lexer, prev);
16994         token = cp_lexer_peek_token (parser->lexer);
16995         token->type = CPP_SEMICOLON;
16996         token->keyword = RID_MAX;
16997       }
16998   }
16999
17000   /* If this class is not itself within the scope of another class,
17001      then we need to parse the bodies of all of the queued function
17002      definitions.  Note that the queued functions defined in a class
17003      are not always processed immediately following the
17004      class-specifier for that class.  Consider:
17005
17006        struct A {
17007          struct B { void f() { sizeof (A); } };
17008        };
17009
17010      If `f' were processed before the processing of `A' were
17011      completed, there would be no way to compute the size of `A'.
17012      Note that the nesting we are interested in here is lexical --
17013      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17014      for:
17015
17016        struct A { struct B; };
17017        struct A::B { void f() { } };
17018
17019      there is no need to delay the parsing of `A::B::f'.  */
17020   if (--parser->num_classes_being_defined == 0)
17021     {
17022       tree fn;
17023       tree class_type = NULL_TREE;
17024       tree pushed_scope = NULL_TREE;
17025       unsigned ix;
17026       cp_default_arg_entry *e;
17027
17028       /* In a first pass, parse default arguments to the functions.
17029          Then, in a second pass, parse the bodies of the functions.
17030          This two-phased approach handles cases like:
17031
17032             struct S {
17033               void f() { g(); }
17034               void g(int i = 3);
17035             };
17036
17037          */
17038       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17039                         ix, e)
17040         {
17041           fn = e->decl;
17042           /* If there are default arguments that have not yet been processed,
17043              take care of them now.  */
17044           if (class_type != e->class_type)
17045             {
17046               if (pushed_scope)
17047                 pop_scope (pushed_scope);
17048               class_type = e->class_type;
17049               pushed_scope = push_scope (class_type);
17050             }
17051           /* Make sure that any template parameters are in scope.  */
17052           maybe_begin_member_template_processing (fn);
17053           /* Parse the default argument expressions.  */
17054           cp_parser_late_parsing_default_args (parser, fn);
17055           /* Remove any template parameters from the symbol table.  */
17056           maybe_end_member_template_processing ();
17057         }
17058       if (pushed_scope)
17059         pop_scope (pushed_scope);
17060       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17061       /* Now parse the body of the functions.  */
17062       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, fn)
17063         cp_parser_late_parsing_for_member (parser, fn);
17064       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17065     }
17066
17067   /* Put back any saved access checks.  */
17068   pop_deferring_access_checks ();
17069
17070   /* Restore saved state.  */
17071   parser->in_function_body = saved_in_function_body;
17072   parser->num_template_parameter_lists
17073     = saved_num_template_parameter_lists;
17074   parser->in_unbraced_linkage_specification_p
17075     = saved_in_unbraced_linkage_specification_p;
17076
17077   return type;
17078 }
17079
17080 static tree
17081 cp_parser_class_specifier (cp_parser* parser)
17082 {
17083   tree ret;
17084   timevar_push (TV_PARSE_STRUCT);
17085   ret = cp_parser_class_specifier_1 (parser);
17086   timevar_pop (TV_PARSE_STRUCT);
17087   return ret;
17088 }
17089
17090 /* Parse a class-head.
17091
17092    class-head:
17093      class-key identifier [opt] base-clause [opt]
17094      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
17095      class-key nested-name-specifier [opt] template-id
17096        base-clause [opt]
17097
17098    class-virt-specifier:
17099      final
17100
17101    GNU Extensions:
17102      class-key attributes identifier [opt] base-clause [opt]
17103      class-key attributes nested-name-specifier identifier base-clause [opt]
17104      class-key attributes nested-name-specifier [opt] template-id
17105        base-clause [opt]
17106
17107    Upon return BASES is initialized to the list of base classes (or
17108    NULL, if there are none) in the same form returned by
17109    cp_parser_base_clause.
17110
17111    Returns the TYPE of the indicated class.  Sets
17112    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
17113    involving a nested-name-specifier was used, and FALSE otherwise.
17114
17115    Returns error_mark_node if this is not a class-head.
17116
17117    Returns NULL_TREE if the class-head is syntactically valid, but
17118    semantically invalid in a way that means we should skip the entire
17119    body of the class.  */
17120
17121 static tree
17122 cp_parser_class_head (cp_parser* parser,
17123                       bool* nested_name_specifier_p,
17124                       tree *attributes_p,
17125                       tree *bases)
17126 {
17127   tree nested_name_specifier;
17128   enum tag_types class_key;
17129   tree id = NULL_TREE;
17130   tree type = NULL_TREE;
17131   tree attributes;
17132   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
17133   bool template_id_p = false;
17134   bool qualified_p = false;
17135   bool invalid_nested_name_p = false;
17136   bool invalid_explicit_specialization_p = false;
17137   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17138   tree pushed_scope = NULL_TREE;
17139   unsigned num_templates;
17140   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
17141   /* Assume no nested-name-specifier will be present.  */
17142   *nested_name_specifier_p = false;
17143   /* Assume no template parameter lists will be used in defining the
17144      type.  */
17145   num_templates = 0;
17146   parser->colon_corrects_to_scope_p = false;
17147
17148   *bases = NULL_TREE;
17149
17150   /* Look for the class-key.  */
17151   class_key = cp_parser_class_key (parser);
17152   if (class_key == none_type)
17153     return error_mark_node;
17154
17155   /* Parse the attributes.  */
17156   attributes = cp_parser_attributes_opt (parser);
17157
17158   /* If the next token is `::', that is invalid -- but sometimes
17159      people do try to write:
17160
17161        struct ::S {};
17162
17163      Handle this gracefully by accepting the extra qualifier, and then
17164      issuing an error about it later if this really is a
17165      class-head.  If it turns out just to be an elaborated type
17166      specifier, remain silent.  */
17167   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
17168     qualified_p = true;
17169
17170   push_deferring_access_checks (dk_no_check);
17171
17172   /* Determine the name of the class.  Begin by looking for an
17173      optional nested-name-specifier.  */
17174   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
17175   nested_name_specifier
17176     = cp_parser_nested_name_specifier_opt (parser,
17177                                            /*typename_keyword_p=*/false,
17178                                            /*check_dependency_p=*/false,
17179                                            /*type_p=*/false,
17180                                            /*is_declaration=*/false);
17181   /* If there was a nested-name-specifier, then there *must* be an
17182      identifier.  */
17183   if (nested_name_specifier)
17184     {
17185       type_start_token = cp_lexer_peek_token (parser->lexer);
17186       /* Although the grammar says `identifier', it really means
17187          `class-name' or `template-name'.  You are only allowed to
17188          define a class that has already been declared with this
17189          syntax.
17190
17191          The proposed resolution for Core Issue 180 says that wherever
17192          you see `class T::X' you should treat `X' as a type-name.
17193
17194          It is OK to define an inaccessible class; for example:
17195
17196            class A { class B; };
17197            class A::B {};
17198
17199          We do not know if we will see a class-name, or a
17200          template-name.  We look for a class-name first, in case the
17201          class-name is a template-id; if we looked for the
17202          template-name first we would stop after the template-name.  */
17203       cp_parser_parse_tentatively (parser);
17204       type = cp_parser_class_name (parser,
17205                                    /*typename_keyword_p=*/false,
17206                                    /*template_keyword_p=*/false,
17207                                    class_type,
17208                                    /*check_dependency_p=*/false,
17209                                    /*class_head_p=*/true,
17210                                    /*is_declaration=*/false);
17211       /* If that didn't work, ignore the nested-name-specifier.  */
17212       if (!cp_parser_parse_definitely (parser))
17213         {
17214           invalid_nested_name_p = true;
17215           type_start_token = cp_lexer_peek_token (parser->lexer);
17216           id = cp_parser_identifier (parser);
17217           if (id == error_mark_node)
17218             id = NULL_TREE;
17219         }
17220       /* If we could not find a corresponding TYPE, treat this
17221          declaration like an unqualified declaration.  */
17222       if (type == error_mark_node)
17223         nested_name_specifier = NULL_TREE;
17224       /* Otherwise, count the number of templates used in TYPE and its
17225          containing scopes.  */
17226       else
17227         {
17228           tree scope;
17229
17230           for (scope = TREE_TYPE (type);
17231                scope && TREE_CODE (scope) != NAMESPACE_DECL;
17232                scope = (TYPE_P (scope)
17233                         ? TYPE_CONTEXT (scope)
17234                         : DECL_CONTEXT (scope)))
17235             if (TYPE_P (scope)
17236                 && CLASS_TYPE_P (scope)
17237                 && CLASSTYPE_TEMPLATE_INFO (scope)
17238                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
17239                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
17240               ++num_templates;
17241         }
17242     }
17243   /* Otherwise, the identifier is optional.  */
17244   else
17245     {
17246       /* We don't know whether what comes next is a template-id,
17247          an identifier, or nothing at all.  */
17248       cp_parser_parse_tentatively (parser);
17249       /* Check for a template-id.  */
17250       type_start_token = cp_lexer_peek_token (parser->lexer);
17251       id = cp_parser_template_id (parser,
17252                                   /*template_keyword_p=*/false,
17253                                   /*check_dependency_p=*/true,
17254                                   /*is_declaration=*/true);
17255       /* If that didn't work, it could still be an identifier.  */
17256       if (!cp_parser_parse_definitely (parser))
17257         {
17258           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
17259             {
17260               type_start_token = cp_lexer_peek_token (parser->lexer);
17261               id = cp_parser_identifier (parser);
17262             }
17263           else
17264             id = NULL_TREE;
17265         }
17266       else
17267         {
17268           template_id_p = true;
17269           ++num_templates;
17270         }
17271     }
17272
17273   pop_deferring_access_checks ();
17274
17275   if (id)
17276     {
17277       cp_parser_check_for_invalid_template_id (parser, id,
17278                                                type_start_token->location);
17279       virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
17280     }
17281
17282   /* If it's not a `:' or a `{' then we can't really be looking at a
17283      class-head, since a class-head only appears as part of a
17284      class-specifier.  We have to detect this situation before calling
17285      xref_tag, since that has irreversible side-effects.  */
17286   if (!cp_parser_next_token_starts_class_definition_p (parser))
17287     {
17288       cp_parser_error (parser, "expected %<{%> or %<:%>");
17289       type = error_mark_node;
17290       goto out;
17291     }
17292
17293   /* At this point, we're going ahead with the class-specifier, even
17294      if some other problem occurs.  */
17295   cp_parser_commit_to_tentative_parse (parser);
17296   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
17297     {
17298       cp_parser_error (parser,
17299                        "cannot specify %<override%> for a class");
17300       type = error_mark_node;
17301       goto out;
17302     }
17303   /* Issue the error about the overly-qualified name now.  */
17304   if (qualified_p)
17305     {
17306       cp_parser_error (parser,
17307                        "global qualification of class name is invalid");
17308       type = error_mark_node;
17309       goto out;
17310     }
17311   else if (invalid_nested_name_p)
17312     {
17313       cp_parser_error (parser,
17314                        "qualified name does not name a class");
17315       type = error_mark_node;
17316       goto out;
17317     }
17318   else if (nested_name_specifier)
17319     {
17320       tree scope;
17321
17322       /* Reject typedef-names in class heads.  */
17323       if (!DECL_IMPLICIT_TYPEDEF_P (type))
17324         {
17325           error_at (type_start_token->location,
17326                     "invalid class name in declaration of %qD",
17327                     type);
17328           type = NULL_TREE;
17329           goto done;
17330         }
17331
17332       /* Figure out in what scope the declaration is being placed.  */
17333       scope = current_scope ();
17334       /* If that scope does not contain the scope in which the
17335          class was originally declared, the program is invalid.  */
17336       if (scope && !is_ancestor (scope, nested_name_specifier))
17337         {
17338           if (at_namespace_scope_p ())
17339             error_at (type_start_token->location,
17340                       "declaration of %qD in namespace %qD which does not "
17341                       "enclose %qD",
17342                       type, scope, nested_name_specifier);
17343           else
17344             error_at (type_start_token->location,
17345                       "declaration of %qD in %qD which does not enclose %qD",
17346                       type, scope, nested_name_specifier);
17347           type = NULL_TREE;
17348           goto done;
17349         }
17350       /* [dcl.meaning]
17351
17352          A declarator-id shall not be qualified except for the
17353          definition of a ... nested class outside of its class
17354          ... [or] the definition or explicit instantiation of a
17355          class member of a namespace outside of its namespace.  */
17356       if (scope == nested_name_specifier)
17357         {
17358           permerror (nested_name_specifier_token_start->location,
17359                      "extra qualification not allowed");
17360           nested_name_specifier = NULL_TREE;
17361           num_templates = 0;
17362         }
17363     }
17364   /* An explicit-specialization must be preceded by "template <>".  If
17365      it is not, try to recover gracefully.  */
17366   if (at_namespace_scope_p ()
17367       && parser->num_template_parameter_lists == 0
17368       && template_id_p)
17369     {
17370       error_at (type_start_token->location,
17371                 "an explicit specialization must be preceded by %<template <>%>");
17372       invalid_explicit_specialization_p = true;
17373       /* Take the same action that would have been taken by
17374          cp_parser_explicit_specialization.  */
17375       ++parser->num_template_parameter_lists;
17376       begin_specialization ();
17377     }
17378   /* There must be no "return" statements between this point and the
17379      end of this function; set "type "to the correct return value and
17380      use "goto done;" to return.  */
17381   /* Make sure that the right number of template parameters were
17382      present.  */
17383   if (!cp_parser_check_template_parameters (parser, num_templates,
17384                                             type_start_token->location,
17385                                             /*declarator=*/NULL))
17386     {
17387       /* If something went wrong, there is no point in even trying to
17388          process the class-definition.  */
17389       type = NULL_TREE;
17390       goto done;
17391     }
17392
17393   /* Look up the type.  */
17394   if (template_id_p)
17395     {
17396       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
17397           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
17398               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
17399         {
17400           error_at (type_start_token->location,
17401                     "function template %qD redeclared as a class template", id);
17402           type = error_mark_node;
17403         }
17404       else
17405         {
17406           type = TREE_TYPE (id);
17407           type = maybe_process_partial_specialization (type);
17408         }
17409       if (nested_name_specifier)
17410         pushed_scope = push_scope (nested_name_specifier);
17411     }
17412   else if (nested_name_specifier)
17413     {
17414       tree class_type;
17415
17416       /* Given:
17417
17418             template <typename T> struct S { struct T };
17419             template <typename T> struct S<T>::T { };
17420
17421          we will get a TYPENAME_TYPE when processing the definition of
17422          `S::T'.  We need to resolve it to the actual type before we
17423          try to define it.  */
17424       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
17425         {
17426           class_type = resolve_typename_type (TREE_TYPE (type),
17427                                               /*only_current_p=*/false);
17428           if (TREE_CODE (class_type) != TYPENAME_TYPE)
17429             type = TYPE_NAME (class_type);
17430           else
17431             {
17432               cp_parser_error (parser, "could not resolve typename type");
17433               type = error_mark_node;
17434             }
17435         }
17436
17437       if (maybe_process_partial_specialization (TREE_TYPE (type))
17438           == error_mark_node)
17439         {
17440           type = NULL_TREE;
17441           goto done;
17442         }
17443
17444       class_type = current_class_type;
17445       /* Enter the scope indicated by the nested-name-specifier.  */
17446       pushed_scope = push_scope (nested_name_specifier);
17447       /* Get the canonical version of this type.  */
17448       type = TYPE_MAIN_DECL (TREE_TYPE (type));
17449       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
17450           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
17451         {
17452           type = push_template_decl (type);
17453           if (type == error_mark_node)
17454             {
17455               type = NULL_TREE;
17456               goto done;
17457             }
17458         }
17459
17460       type = TREE_TYPE (type);
17461       *nested_name_specifier_p = true;
17462     }
17463   else      /* The name is not a nested name.  */
17464     {
17465       /* If the class was unnamed, create a dummy name.  */
17466       if (!id)
17467         id = make_anon_name ();
17468       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
17469                        parser->num_template_parameter_lists);
17470     }
17471
17472   /* Indicate whether this class was declared as a `class' or as a
17473      `struct'.  */
17474   if (TREE_CODE (type) == RECORD_TYPE)
17475     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
17476   cp_parser_check_class_key (class_key, type);
17477
17478   /* If this type was already complete, and we see another definition,
17479      that's an error.  */
17480   if (type != error_mark_node && COMPLETE_TYPE_P (type))
17481     {
17482       error_at (type_start_token->location, "redefinition of %q#T",
17483                 type);
17484       error_at (type_start_token->location, "previous definition of %q+#T",
17485                 type);
17486       type = NULL_TREE;
17487       goto done;
17488     }
17489   else if (type == error_mark_node)
17490     type = NULL_TREE;
17491
17492   /* We will have entered the scope containing the class; the names of
17493      base classes should be looked up in that context.  For example:
17494
17495        struct A { struct B {}; struct C; };
17496        struct A::C : B {};
17497
17498      is valid.  */
17499
17500   /* Get the list of base-classes, if there is one.  */
17501   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
17502     *bases = cp_parser_base_clause (parser);
17503
17504  done:
17505   /* Leave the scope given by the nested-name-specifier.  We will
17506      enter the class scope itself while processing the members.  */
17507   if (pushed_scope)
17508     pop_scope (pushed_scope);
17509
17510   if (invalid_explicit_specialization_p)
17511     {
17512       end_specialization ();
17513       --parser->num_template_parameter_lists;
17514     }
17515
17516   if (type)
17517     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
17518   *attributes_p = attributes;
17519   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
17520     CLASSTYPE_FINAL (type) = 1;
17521  out:
17522   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
17523   return type;
17524 }
17525
17526 /* Parse a class-key.
17527
17528    class-key:
17529      class
17530      struct
17531      union
17532
17533    Returns the kind of class-key specified, or none_type to indicate
17534    error.  */
17535
17536 static enum tag_types
17537 cp_parser_class_key (cp_parser* parser)
17538 {
17539   cp_token *token;
17540   enum tag_types tag_type;
17541
17542   /* Look for the class-key.  */
17543   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
17544   if (!token)
17545     return none_type;
17546
17547   /* Check to see if the TOKEN is a class-key.  */
17548   tag_type = cp_parser_token_is_class_key (token);
17549   if (!tag_type)
17550     cp_parser_error (parser, "expected class-key");
17551   return tag_type;
17552 }
17553
17554 /* Parse an (optional) member-specification.
17555
17556    member-specification:
17557      member-declaration member-specification [opt]
17558      access-specifier : member-specification [opt]  */
17559
17560 static void
17561 cp_parser_member_specification_opt (cp_parser* parser)
17562 {
17563   while (true)
17564     {
17565       cp_token *token;
17566       enum rid keyword;
17567
17568       /* Peek at the next token.  */
17569       token = cp_lexer_peek_token (parser->lexer);
17570       /* If it's a `}', or EOF then we've seen all the members.  */
17571       if (token->type == CPP_CLOSE_BRACE
17572           || token->type == CPP_EOF
17573           || token->type == CPP_PRAGMA_EOL)
17574         break;
17575
17576       /* See if this token is a keyword.  */
17577       keyword = token->keyword;
17578       switch (keyword)
17579         {
17580         case RID_PUBLIC:
17581         case RID_PROTECTED:
17582         case RID_PRIVATE:
17583           /* Consume the access-specifier.  */
17584           cp_lexer_consume_token (parser->lexer);
17585           /* Remember which access-specifier is active.  */
17586           current_access_specifier = token->u.value;
17587           /* Look for the `:'.  */
17588           cp_parser_require (parser, CPP_COLON, RT_COLON);
17589           break;
17590
17591         default:
17592           /* Accept #pragmas at class scope.  */
17593           if (token->type == CPP_PRAGMA)
17594             {
17595               cp_parser_pragma (parser, pragma_external);
17596               break;
17597             }
17598
17599           /* Otherwise, the next construction must be a
17600              member-declaration.  */
17601           cp_parser_member_declaration (parser);
17602         }
17603     }
17604 }
17605
17606 /* Parse a member-declaration.
17607
17608    member-declaration:
17609      decl-specifier-seq [opt] member-declarator-list [opt] ;
17610      function-definition ; [opt]
17611      :: [opt] nested-name-specifier template [opt] unqualified-id ;
17612      using-declaration
17613      template-declaration
17614
17615    member-declarator-list:
17616      member-declarator
17617      member-declarator-list , member-declarator
17618
17619    member-declarator:
17620      declarator pure-specifier [opt]
17621      declarator constant-initializer [opt]
17622      identifier [opt] : constant-expression
17623
17624    GNU Extensions:
17625
17626    member-declaration:
17627      __extension__ member-declaration
17628
17629    member-declarator:
17630      declarator attributes [opt] pure-specifier [opt]
17631      declarator attributes [opt] constant-initializer [opt]
17632      identifier [opt] attributes [opt] : constant-expression  
17633
17634    C++0x Extensions:
17635
17636    member-declaration:
17637      static_assert-declaration  */
17638
17639 static void
17640 cp_parser_member_declaration (cp_parser* parser)
17641 {
17642   cp_decl_specifier_seq decl_specifiers;
17643   tree prefix_attributes;
17644   tree decl;
17645   int declares_class_or_enum;
17646   bool friend_p;
17647   cp_token *token = NULL;
17648   cp_token *decl_spec_token_start = NULL;
17649   cp_token *initializer_token_start = NULL;
17650   int saved_pedantic;
17651   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
17652
17653   /* Check for the `__extension__' keyword.  */
17654   if (cp_parser_extension_opt (parser, &saved_pedantic))
17655     {
17656       /* Recurse.  */
17657       cp_parser_member_declaration (parser);
17658       /* Restore the old value of the PEDANTIC flag.  */
17659       pedantic = saved_pedantic;
17660
17661       return;
17662     }
17663
17664   /* Check for a template-declaration.  */
17665   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
17666     {
17667       /* An explicit specialization here is an error condition, and we
17668          expect the specialization handler to detect and report this.  */
17669       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
17670           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
17671         cp_parser_explicit_specialization (parser);
17672       else
17673         cp_parser_template_declaration (parser, /*member_p=*/true);
17674
17675       return;
17676     }
17677
17678   /* Check for a using-declaration.  */
17679   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
17680     {
17681       /* Parse the using-declaration.  */
17682       cp_parser_using_declaration (parser,
17683                                    /*access_declaration_p=*/false);
17684       return;
17685     }
17686
17687   /* Check for @defs.  */
17688   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
17689     {
17690       tree ivar, member;
17691       tree ivar_chains = cp_parser_objc_defs_expression (parser);
17692       ivar = ivar_chains;
17693       while (ivar)
17694         {
17695           member = ivar;
17696           ivar = TREE_CHAIN (member);
17697           TREE_CHAIN (member) = NULL_TREE;
17698           finish_member_declaration (member);
17699         }
17700       return;
17701     }
17702
17703   /* If the next token is `static_assert' we have a static assertion.  */
17704   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
17705     {
17706       cp_parser_static_assert (parser, /*member_p=*/true);
17707       return;
17708     }
17709
17710   parser->colon_corrects_to_scope_p = false;
17711
17712   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
17713     goto out;
17714
17715   /* Parse the decl-specifier-seq.  */
17716   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
17717   cp_parser_decl_specifier_seq (parser,
17718                                 CP_PARSER_FLAGS_OPTIONAL,
17719                                 &decl_specifiers,
17720                                 &declares_class_or_enum);
17721   prefix_attributes = decl_specifiers.attributes;
17722   decl_specifiers.attributes = NULL_TREE;
17723   /* Check for an invalid type-name.  */
17724   if (!decl_specifiers.any_type_specifiers_p
17725       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
17726     goto out;
17727   /* If there is no declarator, then the decl-specifier-seq should
17728      specify a type.  */
17729   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
17730     {
17731       /* If there was no decl-specifier-seq, and the next token is a
17732          `;', then we have something like:
17733
17734            struct S { ; };
17735
17736          [class.mem]
17737
17738          Each member-declaration shall declare at least one member
17739          name of the class.  */
17740       if (!decl_specifiers.any_specifiers_p)
17741         {
17742           cp_token *token = cp_lexer_peek_token (parser->lexer);
17743           if (!in_system_header_at (token->location))
17744             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
17745         }
17746       else
17747         {
17748           tree type;
17749
17750           /* See if this declaration is a friend.  */
17751           friend_p = cp_parser_friend_p (&decl_specifiers);
17752           /* If there were decl-specifiers, check to see if there was
17753              a class-declaration.  */
17754           type = check_tag_decl (&decl_specifiers);
17755           /* Nested classes have already been added to the class, but
17756              a `friend' needs to be explicitly registered.  */
17757           if (friend_p)
17758             {
17759               /* If the `friend' keyword was present, the friend must
17760                  be introduced with a class-key.  */
17761                if (!declares_class_or_enum && cxx_dialect < cxx0x)
17762                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
17763                           "in C++03 a class-key must be used "
17764                           "when declaring a friend");
17765                /* In this case:
17766
17767                     template <typename T> struct A {
17768                       friend struct A<T>::B;
17769                     };
17770
17771                   A<T>::B will be represented by a TYPENAME_TYPE, and
17772                   therefore not recognized by check_tag_decl.  */
17773                if (!type)
17774                  {
17775                    type = decl_specifiers.type;
17776                    if (type && TREE_CODE (type) == TYPE_DECL)
17777                      type = TREE_TYPE (type);
17778                  }
17779                if (!type || !TYPE_P (type))
17780                  error_at (decl_spec_token_start->location,
17781                            "friend declaration does not name a class or "
17782                            "function");
17783                else
17784                  make_friend_class (current_class_type, type,
17785                                     /*complain=*/true);
17786             }
17787           /* If there is no TYPE, an error message will already have
17788              been issued.  */
17789           else if (!type || type == error_mark_node)
17790             ;
17791           /* An anonymous aggregate has to be handled specially; such
17792              a declaration really declares a data member (with a
17793              particular type), as opposed to a nested class.  */
17794           else if (ANON_AGGR_TYPE_P (type))
17795             {
17796               /* Remove constructors and such from TYPE, now that we
17797                  know it is an anonymous aggregate.  */
17798               fixup_anonymous_aggr (type);
17799               /* And make the corresponding data member.  */
17800               decl = build_decl (decl_spec_token_start->location,
17801                                  FIELD_DECL, NULL_TREE, type);
17802               /* Add it to the class.  */
17803               finish_member_declaration (decl);
17804             }
17805           else
17806             cp_parser_check_access_in_redeclaration
17807                                               (TYPE_NAME (type),
17808                                                decl_spec_token_start->location);
17809         }
17810     }
17811   else
17812     {
17813       bool assume_semicolon = false;
17814
17815       /* See if these declarations will be friends.  */
17816       friend_p = cp_parser_friend_p (&decl_specifiers);
17817
17818       /* Keep going until we hit the `;' at the end of the
17819          declaration.  */
17820       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
17821         {
17822           tree attributes = NULL_TREE;
17823           tree first_attribute;
17824
17825           /* Peek at the next token.  */
17826           token = cp_lexer_peek_token (parser->lexer);
17827
17828           /* Check for a bitfield declaration.  */
17829           if (token->type == CPP_COLON
17830               || (token->type == CPP_NAME
17831                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
17832                   == CPP_COLON))
17833             {
17834               tree identifier;
17835               tree width;
17836
17837               /* Get the name of the bitfield.  Note that we cannot just
17838                  check TOKEN here because it may have been invalidated by
17839                  the call to cp_lexer_peek_nth_token above.  */
17840               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
17841                 identifier = cp_parser_identifier (parser);
17842               else
17843                 identifier = NULL_TREE;
17844
17845               /* Consume the `:' token.  */
17846               cp_lexer_consume_token (parser->lexer);
17847               /* Get the width of the bitfield.  */
17848               width
17849                 = cp_parser_constant_expression (parser,
17850                                                  /*allow_non_constant=*/false,
17851                                                  NULL);
17852
17853               /* Look for attributes that apply to the bitfield.  */
17854               attributes = cp_parser_attributes_opt (parser);
17855               /* Remember which attributes are prefix attributes and
17856                  which are not.  */
17857               first_attribute = attributes;
17858               /* Combine the attributes.  */
17859               attributes = chainon (prefix_attributes, attributes);
17860
17861               /* Create the bitfield declaration.  */
17862               decl = grokbitfield (identifier
17863                                    ? make_id_declarator (NULL_TREE,
17864                                                          identifier,
17865                                                          sfk_none)
17866                                    : NULL,
17867                                    &decl_specifiers,
17868                                    width,
17869                                    attributes);
17870             }
17871           else
17872             {
17873               cp_declarator *declarator;
17874               tree initializer;
17875               tree asm_specification;
17876               int ctor_dtor_or_conv_p;
17877
17878               /* Parse the declarator.  */
17879               declarator
17880                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
17881                                         &ctor_dtor_or_conv_p,
17882                                         /*parenthesized_p=*/NULL,
17883                                         /*member_p=*/true);
17884
17885               /* If something went wrong parsing the declarator, make sure
17886                  that we at least consume some tokens.  */
17887               if (declarator == cp_error_declarator)
17888                 {
17889                   /* Skip to the end of the statement.  */
17890                   cp_parser_skip_to_end_of_statement (parser);
17891                   /* If the next token is not a semicolon, that is
17892                      probably because we just skipped over the body of
17893                      a function.  So, we consume a semicolon if
17894                      present, but do not issue an error message if it
17895                      is not present.  */
17896                   if (cp_lexer_next_token_is (parser->lexer,
17897                                               CPP_SEMICOLON))
17898                     cp_lexer_consume_token (parser->lexer);
17899                   goto out;
17900                 }
17901
17902               if (declares_class_or_enum & 2)
17903                 cp_parser_check_for_definition_in_return_type
17904                                             (declarator, decl_specifiers.type,
17905                                              decl_specifiers.type_location);
17906
17907               /* Look for an asm-specification.  */
17908               asm_specification = cp_parser_asm_specification_opt (parser);
17909               /* Look for attributes that apply to the declaration.  */
17910               attributes = cp_parser_attributes_opt (parser);
17911               /* Remember which attributes are prefix attributes and
17912                  which are not.  */
17913               first_attribute = attributes;
17914               /* Combine the attributes.  */
17915               attributes = chainon (prefix_attributes, attributes);
17916
17917               /* If it's an `=', then we have a constant-initializer or a
17918                  pure-specifier.  It is not correct to parse the
17919                  initializer before registering the member declaration
17920                  since the member declaration should be in scope while
17921                  its initializer is processed.  However, the rest of the
17922                  front end does not yet provide an interface that allows
17923                  us to handle this correctly.  */
17924               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17925                 {
17926                   /* In [class.mem]:
17927
17928                      A pure-specifier shall be used only in the declaration of
17929                      a virtual function.
17930
17931                      A member-declarator can contain a constant-initializer
17932                      only if it declares a static member of integral or
17933                      enumeration type.
17934
17935                      Therefore, if the DECLARATOR is for a function, we look
17936                      for a pure-specifier; otherwise, we look for a
17937                      constant-initializer.  When we call `grokfield', it will
17938                      perform more stringent semantics checks.  */
17939                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
17940                   if (function_declarator_p (declarator))
17941                     initializer = cp_parser_pure_specifier (parser);
17942                   else
17943                     /* Parse the initializer.  */
17944                     initializer = cp_parser_constant_initializer (parser);
17945                 }
17946               /* Otherwise, there is no initializer.  */
17947               else
17948                 initializer = NULL_TREE;
17949
17950               /* See if we are probably looking at a function
17951                  definition.  We are certainly not looking at a
17952                  member-declarator.  Calling `grokfield' has
17953                  side-effects, so we must not do it unless we are sure
17954                  that we are looking at a member-declarator.  */
17955               if (cp_parser_token_starts_function_definition_p
17956                   (cp_lexer_peek_token (parser->lexer)))
17957                 {
17958                   /* The grammar does not allow a pure-specifier to be
17959                      used when a member function is defined.  (It is
17960                      possible that this fact is an oversight in the
17961                      standard, since a pure function may be defined
17962                      outside of the class-specifier.  */
17963                   if (initializer)
17964                     error_at (initializer_token_start->location,
17965                               "pure-specifier on function-definition");
17966                   decl = cp_parser_save_member_function_body (parser,
17967                                                               &decl_specifiers,
17968                                                               declarator,
17969                                                               attributes);
17970                   /* If the member was not a friend, declare it here.  */
17971                   if (!friend_p)
17972                     finish_member_declaration (decl);
17973                   /* Peek at the next token.  */
17974                   token = cp_lexer_peek_token (parser->lexer);
17975                   /* If the next token is a semicolon, consume it.  */
17976                   if (token->type == CPP_SEMICOLON)
17977                     cp_lexer_consume_token (parser->lexer);
17978                   goto out;
17979                 }
17980               else
17981                 if (declarator->kind == cdk_function)
17982                   declarator->id_loc = token->location;
17983                 /* Create the declaration.  */
17984                 decl = grokfield (declarator, &decl_specifiers,
17985                                   initializer, /*init_const_expr_p=*/true,
17986                                   asm_specification,
17987                                   attributes);
17988             }
17989
17990           /* Reset PREFIX_ATTRIBUTES.  */
17991           while (attributes && TREE_CHAIN (attributes) != first_attribute)
17992             attributes = TREE_CHAIN (attributes);
17993           if (attributes)
17994             TREE_CHAIN (attributes) = NULL_TREE;
17995
17996           /* If there is any qualification still in effect, clear it
17997              now; we will be starting fresh with the next declarator.  */
17998           parser->scope = NULL_TREE;
17999           parser->qualifying_scope = NULL_TREE;
18000           parser->object_scope = NULL_TREE;
18001           /* If it's a `,', then there are more declarators.  */
18002           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18003             cp_lexer_consume_token (parser->lexer);
18004           /* If the next token isn't a `;', then we have a parse error.  */
18005           else if (cp_lexer_next_token_is_not (parser->lexer,
18006                                                CPP_SEMICOLON))
18007             {
18008               /* The next token might be a ways away from where the
18009                  actual semicolon is missing.  Find the previous token
18010                  and use that for our error position.  */
18011               cp_token *token = cp_lexer_previous_token (parser->lexer);
18012               error_at (token->location,
18013                         "expected %<;%> at end of member declaration");
18014
18015               /* Assume that the user meant to provide a semicolon.  If
18016                  we were to cp_parser_skip_to_end_of_statement, we might
18017                  skip to a semicolon inside a member function definition
18018                  and issue nonsensical error messages.  */
18019               assume_semicolon = true;
18020             }
18021
18022           if (decl)
18023             {
18024               /* Add DECL to the list of members.  */
18025               if (!friend_p)
18026                 finish_member_declaration (decl);
18027
18028               if (TREE_CODE (decl) == FUNCTION_DECL)
18029                 cp_parser_save_default_args (parser, decl);
18030             }
18031
18032           if (assume_semicolon)
18033             goto out;
18034         }
18035     }
18036
18037   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18038  out:
18039   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18040 }
18041
18042 /* Parse a pure-specifier.
18043
18044    pure-specifier:
18045      = 0
18046
18047    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18048    Otherwise, ERROR_MARK_NODE is returned.  */
18049
18050 static tree
18051 cp_parser_pure_specifier (cp_parser* parser)
18052 {
18053   cp_token *token;
18054
18055   /* Look for the `=' token.  */
18056   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18057     return error_mark_node;
18058   /* Look for the `0' token.  */
18059   token = cp_lexer_peek_token (parser->lexer);
18060
18061   if (token->type == CPP_EOF
18062       || token->type == CPP_PRAGMA_EOL)
18063     return error_mark_node;
18064
18065   cp_lexer_consume_token (parser->lexer);
18066
18067   /* Accept = default or = delete in c++0x mode.  */
18068   if (token->keyword == RID_DEFAULT
18069       || token->keyword == RID_DELETE)
18070     {
18071       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
18072       return token->u.value;
18073     }
18074
18075   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
18076   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
18077     {
18078       cp_parser_error (parser,
18079                        "invalid pure specifier (only %<= 0%> is allowed)");
18080       cp_parser_skip_to_end_of_statement (parser);
18081       return error_mark_node;
18082     }
18083   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
18084     {
18085       error_at (token->location, "templates may not be %<virtual%>");
18086       return error_mark_node;
18087     }
18088
18089   return integer_zero_node;
18090 }
18091
18092 /* Parse a constant-initializer.
18093
18094    constant-initializer:
18095      = constant-expression
18096
18097    Returns a representation of the constant-expression.  */
18098
18099 static tree
18100 cp_parser_constant_initializer (cp_parser* parser)
18101 {
18102   /* Look for the `=' token.  */
18103   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
18104     return error_mark_node;
18105
18106   /* It is invalid to write:
18107
18108        struct S { static const int i = { 7 }; };
18109
18110      */
18111   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
18112     {
18113       cp_parser_error (parser,
18114                        "a brace-enclosed initializer is not allowed here");
18115       /* Consume the opening brace.  */
18116       cp_lexer_consume_token (parser->lexer);
18117       /* Skip the initializer.  */
18118       cp_parser_skip_to_closing_brace (parser);
18119       /* Look for the trailing `}'.  */
18120       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18121
18122       return error_mark_node;
18123     }
18124
18125   return cp_parser_constant_expression (parser,
18126                                         /*allow_non_constant=*/false,
18127                                         NULL);
18128 }
18129
18130 /* Derived classes [gram.class.derived] */
18131
18132 /* Parse a base-clause.
18133
18134    base-clause:
18135      : base-specifier-list
18136
18137    base-specifier-list:
18138      base-specifier ... [opt]
18139      base-specifier-list , base-specifier ... [opt]
18140
18141    Returns a TREE_LIST representing the base-classes, in the order in
18142    which they were declared.  The representation of each node is as
18143    described by cp_parser_base_specifier.
18144
18145    In the case that no bases are specified, this function will return
18146    NULL_TREE, not ERROR_MARK_NODE.  */
18147
18148 static tree
18149 cp_parser_base_clause (cp_parser* parser)
18150 {
18151   tree bases = NULL_TREE;
18152
18153   /* Look for the `:' that begins the list.  */
18154   cp_parser_require (parser, CPP_COLON, RT_COLON);
18155
18156   /* Scan the base-specifier-list.  */
18157   while (true)
18158     {
18159       cp_token *token;
18160       tree base;
18161       bool pack_expansion_p = false;
18162
18163       /* Look for the base-specifier.  */
18164       base = cp_parser_base_specifier (parser);
18165       /* Look for the (optional) ellipsis. */
18166       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18167         {
18168           /* Consume the `...'. */
18169           cp_lexer_consume_token (parser->lexer);
18170
18171           pack_expansion_p = true;
18172         }
18173
18174       /* Add BASE to the front of the list.  */
18175       if (base != error_mark_node)
18176         {
18177           if (pack_expansion_p)
18178             /* Make this a pack expansion type. */
18179             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
18180           
18181
18182           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
18183             {
18184               TREE_CHAIN (base) = bases;
18185               bases = base;
18186             }
18187         }
18188       /* Peek at the next token.  */
18189       token = cp_lexer_peek_token (parser->lexer);
18190       /* If it's not a comma, then the list is complete.  */
18191       if (token->type != CPP_COMMA)
18192         break;
18193       /* Consume the `,'.  */
18194       cp_lexer_consume_token (parser->lexer);
18195     }
18196
18197   /* PARSER->SCOPE may still be non-NULL at this point, if the last
18198      base class had a qualified name.  However, the next name that
18199      appears is certainly not qualified.  */
18200   parser->scope = NULL_TREE;
18201   parser->qualifying_scope = NULL_TREE;
18202   parser->object_scope = NULL_TREE;
18203
18204   return nreverse (bases);
18205 }
18206
18207 /* Parse a base-specifier.
18208
18209    base-specifier:
18210      :: [opt] nested-name-specifier [opt] class-name
18211      virtual access-specifier [opt] :: [opt] nested-name-specifier
18212        [opt] class-name
18213      access-specifier virtual [opt] :: [opt] nested-name-specifier
18214        [opt] class-name
18215
18216    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
18217    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
18218    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
18219    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
18220
18221 static tree
18222 cp_parser_base_specifier (cp_parser* parser)
18223 {
18224   cp_token *token;
18225   bool done = false;
18226   bool virtual_p = false;
18227   bool duplicate_virtual_error_issued_p = false;
18228   bool duplicate_access_error_issued_p = false;
18229   bool class_scope_p, template_p;
18230   tree access = access_default_node;
18231   tree type;
18232
18233   /* Process the optional `virtual' and `access-specifier'.  */
18234   while (!done)
18235     {
18236       /* Peek at the next token.  */
18237       token = cp_lexer_peek_token (parser->lexer);
18238       /* Process `virtual'.  */
18239       switch (token->keyword)
18240         {
18241         case RID_VIRTUAL:
18242           /* If `virtual' appears more than once, issue an error.  */
18243           if (virtual_p && !duplicate_virtual_error_issued_p)
18244             {
18245               cp_parser_error (parser,
18246                                "%<virtual%> specified more than once in base-specified");
18247               duplicate_virtual_error_issued_p = true;
18248             }
18249
18250           virtual_p = true;
18251
18252           /* Consume the `virtual' token.  */
18253           cp_lexer_consume_token (parser->lexer);
18254
18255           break;
18256
18257         case RID_PUBLIC:
18258         case RID_PROTECTED:
18259         case RID_PRIVATE:
18260           /* If more than one access specifier appears, issue an
18261              error.  */
18262           if (access != access_default_node
18263               && !duplicate_access_error_issued_p)
18264             {
18265               cp_parser_error (parser,
18266                                "more than one access specifier in base-specified");
18267               duplicate_access_error_issued_p = true;
18268             }
18269
18270           access = ridpointers[(int) token->keyword];
18271
18272           /* Consume the access-specifier.  */
18273           cp_lexer_consume_token (parser->lexer);
18274
18275           break;
18276
18277         default:
18278           done = true;
18279           break;
18280         }
18281     }
18282   /* It is not uncommon to see programs mechanically, erroneously, use
18283      the 'typename' keyword to denote (dependent) qualified types
18284      as base classes.  */
18285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
18286     {
18287       token = cp_lexer_peek_token (parser->lexer);
18288       if (!processing_template_decl)
18289         error_at (token->location,
18290                   "keyword %<typename%> not allowed outside of templates");
18291       else
18292         error_at (token->location,
18293                   "keyword %<typename%> not allowed in this context "
18294                   "(the base class is implicitly a type)");
18295       cp_lexer_consume_token (parser->lexer);
18296     }
18297
18298   /* Look for the optional `::' operator.  */
18299   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
18300   /* Look for the nested-name-specifier.  The simplest way to
18301      implement:
18302
18303        [temp.res]
18304
18305        The keyword `typename' is not permitted in a base-specifier or
18306        mem-initializer; in these contexts a qualified name that
18307        depends on a template-parameter is implicitly assumed to be a
18308        type name.
18309
18310      is to pretend that we have seen the `typename' keyword at this
18311      point.  */
18312   cp_parser_nested_name_specifier_opt (parser,
18313                                        /*typename_keyword_p=*/true,
18314                                        /*check_dependency_p=*/true,
18315                                        typename_type,
18316                                        /*is_declaration=*/true);
18317   /* If the base class is given by a qualified name, assume that names
18318      we see are type names or templates, as appropriate.  */
18319   class_scope_p = (parser->scope && TYPE_P (parser->scope));
18320   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
18321
18322   /* Finally, look for the class-name.  */
18323   type = cp_parser_class_name (parser,
18324                                class_scope_p,
18325                                template_p,
18326                                typename_type,
18327                                /*check_dependency_p=*/true,
18328                                /*class_head_p=*/false,
18329                                /*is_declaration=*/true);
18330
18331   if (type == error_mark_node)
18332     return error_mark_node;
18333
18334   return finish_base_specifier (TREE_TYPE (type), access, virtual_p);
18335 }
18336
18337 /* Exception handling [gram.exception] */
18338
18339 /* Parse an (optional) exception-specification.
18340
18341    exception-specification:
18342      throw ( type-id-list [opt] )
18343
18344    Returns a TREE_LIST representing the exception-specification.  The
18345    TREE_VALUE of each node is a type.  */
18346
18347 static tree
18348 cp_parser_exception_specification_opt (cp_parser* parser)
18349 {
18350   cp_token *token;
18351   tree type_id_list;
18352   const char *saved_message;
18353
18354   /* Peek at the next token.  */
18355   token = cp_lexer_peek_token (parser->lexer);
18356
18357   /* Is it a noexcept-specification?  */
18358   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
18359     {
18360       tree expr;
18361       cp_lexer_consume_token (parser->lexer);
18362
18363       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
18364         {
18365           cp_lexer_consume_token (parser->lexer);
18366
18367           /* Types may not be defined in an exception-specification.  */
18368           saved_message = parser->type_definition_forbidden_message;
18369           parser->type_definition_forbidden_message
18370             = G_("types may not be defined in an exception-specification");
18371
18372           expr = cp_parser_constant_expression (parser, false, NULL);
18373
18374           /* Restore the saved message.  */
18375           parser->type_definition_forbidden_message = saved_message;
18376
18377           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18378         }
18379       else
18380         expr = boolean_true_node;
18381
18382       return build_noexcept_spec (expr, tf_warning_or_error);
18383     }
18384
18385   /* If it's not `throw', then there's no exception-specification.  */
18386   if (!cp_parser_is_keyword (token, RID_THROW))
18387     return NULL_TREE;
18388
18389 #if 0
18390   /* Enable this once a lot of code has transitioned to noexcept?  */
18391   if (cxx_dialect == cxx0x && !in_system_header)
18392     warning (OPT_Wdeprecated, "dynamic exception specifications are "
18393              "deprecated in C++0x; use %<noexcept%> instead");
18394 #endif
18395
18396   /* Consume the `throw'.  */
18397   cp_lexer_consume_token (parser->lexer);
18398
18399   /* Look for the `('.  */
18400   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18401
18402   /* Peek at the next token.  */
18403   token = cp_lexer_peek_token (parser->lexer);
18404   /* If it's not a `)', then there is a type-id-list.  */
18405   if (token->type != CPP_CLOSE_PAREN)
18406     {
18407       /* Types may not be defined in an exception-specification.  */
18408       saved_message = parser->type_definition_forbidden_message;
18409       parser->type_definition_forbidden_message
18410         = G_("types may not be defined in an exception-specification");
18411       /* Parse the type-id-list.  */
18412       type_id_list = cp_parser_type_id_list (parser);
18413       /* Restore the saved message.  */
18414       parser->type_definition_forbidden_message = saved_message;
18415     }
18416   else
18417     type_id_list = empty_except_spec;
18418
18419   /* Look for the `)'.  */
18420   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18421
18422   return type_id_list;
18423 }
18424
18425 /* Parse an (optional) type-id-list.
18426
18427    type-id-list:
18428      type-id ... [opt]
18429      type-id-list , type-id ... [opt]
18430
18431    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
18432    in the order that the types were presented.  */
18433
18434 static tree
18435 cp_parser_type_id_list (cp_parser* parser)
18436 {
18437   tree types = NULL_TREE;
18438
18439   while (true)
18440     {
18441       cp_token *token;
18442       tree type;
18443
18444       /* Get the next type-id.  */
18445       type = cp_parser_type_id (parser);
18446       /* Parse the optional ellipsis. */
18447       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18448         {
18449           /* Consume the `...'. */
18450           cp_lexer_consume_token (parser->lexer);
18451
18452           /* Turn the type into a pack expansion expression. */
18453           type = make_pack_expansion (type);
18454         }
18455       /* Add it to the list.  */
18456       types = add_exception_specifier (types, type, /*complain=*/1);
18457       /* Peek at the next token.  */
18458       token = cp_lexer_peek_token (parser->lexer);
18459       /* If it is not a `,', we are done.  */
18460       if (token->type != CPP_COMMA)
18461         break;
18462       /* Consume the `,'.  */
18463       cp_lexer_consume_token (parser->lexer);
18464     }
18465
18466   return nreverse (types);
18467 }
18468
18469 /* Parse a try-block.
18470
18471    try-block:
18472      try compound-statement handler-seq  */
18473
18474 static tree
18475 cp_parser_try_block (cp_parser* parser)
18476 {
18477   tree try_block;
18478
18479   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
18480   try_block = begin_try_block ();
18481   cp_parser_compound_statement (parser, NULL, true, false);
18482   finish_try_block (try_block);
18483   cp_parser_handler_seq (parser);
18484   finish_handler_sequence (try_block);
18485
18486   return try_block;
18487 }
18488
18489 /* Parse a function-try-block.
18490
18491    function-try-block:
18492      try ctor-initializer [opt] function-body handler-seq  */
18493
18494 static bool
18495 cp_parser_function_try_block (cp_parser* parser)
18496 {
18497   tree compound_stmt;
18498   tree try_block;
18499   bool ctor_initializer_p;
18500
18501   /* Look for the `try' keyword.  */
18502   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
18503     return false;
18504   /* Let the rest of the front end know where we are.  */
18505   try_block = begin_function_try_block (&compound_stmt);
18506   /* Parse the function-body.  */
18507   ctor_initializer_p
18508     = cp_parser_ctor_initializer_opt_and_function_body (parser);
18509   /* We're done with the `try' part.  */
18510   finish_function_try_block (try_block);
18511   /* Parse the handlers.  */
18512   cp_parser_handler_seq (parser);
18513   /* We're done with the handlers.  */
18514   finish_function_handler_sequence (try_block, compound_stmt);
18515
18516   return ctor_initializer_p;
18517 }
18518
18519 /* Parse a handler-seq.
18520
18521    handler-seq:
18522      handler handler-seq [opt]  */
18523
18524 static void
18525 cp_parser_handler_seq (cp_parser* parser)
18526 {
18527   while (true)
18528     {
18529       cp_token *token;
18530
18531       /* Parse the handler.  */
18532       cp_parser_handler (parser);
18533       /* Peek at the next token.  */
18534       token = cp_lexer_peek_token (parser->lexer);
18535       /* If it's not `catch' then there are no more handlers.  */
18536       if (!cp_parser_is_keyword (token, RID_CATCH))
18537         break;
18538     }
18539 }
18540
18541 /* Parse a handler.
18542
18543    handler:
18544      catch ( exception-declaration ) compound-statement  */
18545
18546 static void
18547 cp_parser_handler (cp_parser* parser)
18548 {
18549   tree handler;
18550   tree declaration;
18551
18552   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
18553   handler = begin_handler ();
18554   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18555   declaration = cp_parser_exception_declaration (parser);
18556   finish_handler_parms (declaration, handler);
18557   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18558   cp_parser_compound_statement (parser, NULL, false, false);
18559   finish_handler (handler);
18560 }
18561
18562 /* Parse an exception-declaration.
18563
18564    exception-declaration:
18565      type-specifier-seq declarator
18566      type-specifier-seq abstract-declarator
18567      type-specifier-seq
18568      ...
18569
18570    Returns a VAR_DECL for the declaration, or NULL_TREE if the
18571    ellipsis variant is used.  */
18572
18573 static tree
18574 cp_parser_exception_declaration (cp_parser* parser)
18575 {
18576   cp_decl_specifier_seq type_specifiers;
18577   cp_declarator *declarator;
18578   const char *saved_message;
18579
18580   /* If it's an ellipsis, it's easy to handle.  */
18581   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
18582     {
18583       /* Consume the `...' token.  */
18584       cp_lexer_consume_token (parser->lexer);
18585       return NULL_TREE;
18586     }
18587
18588   /* Types may not be defined in exception-declarations.  */
18589   saved_message = parser->type_definition_forbidden_message;
18590   parser->type_definition_forbidden_message
18591     = G_("types may not be defined in exception-declarations");
18592
18593   /* Parse the type-specifier-seq.  */
18594   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
18595                                 /*is_trailing_return=*/false,
18596                                 &type_specifiers);
18597   /* If it's a `)', then there is no declarator.  */
18598   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
18599     declarator = NULL;
18600   else
18601     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
18602                                        /*ctor_dtor_or_conv_p=*/NULL,
18603                                        /*parenthesized_p=*/NULL,
18604                                        /*member_p=*/false);
18605
18606   /* Restore the saved message.  */
18607   parser->type_definition_forbidden_message = saved_message;
18608
18609   if (!type_specifiers.any_specifiers_p)
18610     return error_mark_node;
18611
18612   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
18613 }
18614
18615 /* Parse a throw-expression.
18616
18617    throw-expression:
18618      throw assignment-expression [opt]
18619
18620    Returns a THROW_EXPR representing the throw-expression.  */
18621
18622 static tree
18623 cp_parser_throw_expression (cp_parser* parser)
18624 {
18625   tree expression;
18626   cp_token* token;
18627
18628   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
18629   token = cp_lexer_peek_token (parser->lexer);
18630   /* Figure out whether or not there is an assignment-expression
18631      following the "throw" keyword.  */
18632   if (token->type == CPP_COMMA
18633       || token->type == CPP_SEMICOLON
18634       || token->type == CPP_CLOSE_PAREN
18635       || token->type == CPP_CLOSE_SQUARE
18636       || token->type == CPP_CLOSE_BRACE
18637       || token->type == CPP_COLON)
18638     expression = NULL_TREE;
18639   else
18640     expression = cp_parser_assignment_expression (parser,
18641                                                   /*cast_p=*/false, NULL);
18642
18643   return build_throw (expression);
18644 }
18645
18646 /* GNU Extensions */
18647
18648 /* Parse an (optional) asm-specification.
18649
18650    asm-specification:
18651      asm ( string-literal )
18652
18653    If the asm-specification is present, returns a STRING_CST
18654    corresponding to the string-literal.  Otherwise, returns
18655    NULL_TREE.  */
18656
18657 static tree
18658 cp_parser_asm_specification_opt (cp_parser* parser)
18659 {
18660   cp_token *token;
18661   tree asm_specification;
18662
18663   /* Peek at the next token.  */
18664   token = cp_lexer_peek_token (parser->lexer);
18665   /* If the next token isn't the `asm' keyword, then there's no
18666      asm-specification.  */
18667   if (!cp_parser_is_keyword (token, RID_ASM))
18668     return NULL_TREE;
18669
18670   /* Consume the `asm' token.  */
18671   cp_lexer_consume_token (parser->lexer);
18672   /* Look for the `('.  */
18673   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18674
18675   /* Look for the string-literal.  */
18676   asm_specification = cp_parser_string_literal (parser, false, false);
18677
18678   /* Look for the `)'.  */
18679   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18680
18681   return asm_specification;
18682 }
18683
18684 /* Parse an asm-operand-list.
18685
18686    asm-operand-list:
18687      asm-operand
18688      asm-operand-list , asm-operand
18689
18690    asm-operand:
18691      string-literal ( expression )
18692      [ string-literal ] string-literal ( expression )
18693
18694    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
18695    each node is the expression.  The TREE_PURPOSE is itself a
18696    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
18697    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
18698    is a STRING_CST for the string literal before the parenthesis. Returns
18699    ERROR_MARK_NODE if any of the operands are invalid.  */
18700
18701 static tree
18702 cp_parser_asm_operand_list (cp_parser* parser)
18703 {
18704   tree asm_operands = NULL_TREE;
18705   bool invalid_operands = false;
18706
18707   while (true)
18708     {
18709       tree string_literal;
18710       tree expression;
18711       tree name;
18712
18713       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
18714         {
18715           /* Consume the `[' token.  */
18716           cp_lexer_consume_token (parser->lexer);
18717           /* Read the operand name.  */
18718           name = cp_parser_identifier (parser);
18719           if (name != error_mark_node)
18720             name = build_string (IDENTIFIER_LENGTH (name),
18721                                  IDENTIFIER_POINTER (name));
18722           /* Look for the closing `]'.  */
18723           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
18724         }
18725       else
18726         name = NULL_TREE;
18727       /* Look for the string-literal.  */
18728       string_literal = cp_parser_string_literal (parser, false, false);
18729
18730       /* Look for the `('.  */
18731       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18732       /* Parse the expression.  */
18733       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
18734       /* Look for the `)'.  */
18735       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18736
18737       if (name == error_mark_node 
18738           || string_literal == error_mark_node 
18739           || expression == error_mark_node)
18740         invalid_operands = true;
18741
18742       /* Add this operand to the list.  */
18743       asm_operands = tree_cons (build_tree_list (name, string_literal),
18744                                 expression,
18745                                 asm_operands);
18746       /* If the next token is not a `,', there are no more
18747          operands.  */
18748       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18749         break;
18750       /* Consume the `,'.  */
18751       cp_lexer_consume_token (parser->lexer);
18752     }
18753
18754   return invalid_operands ? error_mark_node : nreverse (asm_operands);
18755 }
18756
18757 /* Parse an asm-clobber-list.
18758
18759    asm-clobber-list:
18760      string-literal
18761      asm-clobber-list , string-literal
18762
18763    Returns a TREE_LIST, indicating the clobbers in the order that they
18764    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
18765
18766 static tree
18767 cp_parser_asm_clobber_list (cp_parser* parser)
18768 {
18769   tree clobbers = NULL_TREE;
18770
18771   while (true)
18772     {
18773       tree string_literal;
18774
18775       /* Look for the string literal.  */
18776       string_literal = cp_parser_string_literal (parser, false, false);
18777       /* Add it to the list.  */
18778       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
18779       /* If the next token is not a `,', then the list is
18780          complete.  */
18781       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18782         break;
18783       /* Consume the `,' token.  */
18784       cp_lexer_consume_token (parser->lexer);
18785     }
18786
18787   return clobbers;
18788 }
18789
18790 /* Parse an asm-label-list.
18791
18792    asm-label-list:
18793      identifier
18794      asm-label-list , identifier
18795
18796    Returns a TREE_LIST, indicating the labels in the order that they
18797    appeared.  The TREE_VALUE of each node is a label.  */
18798
18799 static tree
18800 cp_parser_asm_label_list (cp_parser* parser)
18801 {
18802   tree labels = NULL_TREE;
18803
18804   while (true)
18805     {
18806       tree identifier, label, name;
18807
18808       /* Look for the identifier.  */
18809       identifier = cp_parser_identifier (parser);
18810       if (!error_operand_p (identifier))
18811         {
18812           label = lookup_label (identifier);
18813           if (TREE_CODE (label) == LABEL_DECL)
18814             {
18815               TREE_USED (label) = 1;
18816               check_goto (label);
18817               name = build_string (IDENTIFIER_LENGTH (identifier),
18818                                    IDENTIFIER_POINTER (identifier));
18819               labels = tree_cons (name, label, labels);
18820             }
18821         }
18822       /* If the next token is not a `,', then the list is
18823          complete.  */
18824       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
18825         break;
18826       /* Consume the `,' token.  */
18827       cp_lexer_consume_token (parser->lexer);
18828     }
18829
18830   return nreverse (labels);
18831 }
18832
18833 /* Parse an (optional) series of attributes.
18834
18835    attributes:
18836      attributes attribute
18837
18838    attribute:
18839      __attribute__ (( attribute-list [opt] ))
18840
18841    The return value is as for cp_parser_attribute_list.  */
18842
18843 static tree
18844 cp_parser_attributes_opt (cp_parser* parser)
18845 {
18846   tree attributes = NULL_TREE;
18847
18848   while (true)
18849     {
18850       cp_token *token;
18851       tree attribute_list;
18852
18853       /* Peek at the next token.  */
18854       token = cp_lexer_peek_token (parser->lexer);
18855       /* If it's not `__attribute__', then we're done.  */
18856       if (token->keyword != RID_ATTRIBUTE)
18857         break;
18858
18859       /* Consume the `__attribute__' keyword.  */
18860       cp_lexer_consume_token (parser->lexer);
18861       /* Look for the two `(' tokens.  */
18862       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18863       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
18864
18865       /* Peek at the next token.  */
18866       token = cp_lexer_peek_token (parser->lexer);
18867       if (token->type != CPP_CLOSE_PAREN)
18868         /* Parse the attribute-list.  */
18869         attribute_list = cp_parser_attribute_list (parser);
18870       else
18871         /* If the next token is a `)', then there is no attribute
18872            list.  */
18873         attribute_list = NULL;
18874
18875       /* Look for the two `)' tokens.  */
18876       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18877       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
18878
18879       /* Add these new attributes to the list.  */
18880       attributes = chainon (attributes, attribute_list);
18881     }
18882
18883   return attributes;
18884 }
18885
18886 /* Parse an attribute-list.
18887
18888    attribute-list:
18889      attribute
18890      attribute-list , attribute
18891
18892    attribute:
18893      identifier
18894      identifier ( identifier )
18895      identifier ( identifier , expression-list )
18896      identifier ( expression-list )
18897
18898    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
18899    to an attribute.  The TREE_PURPOSE of each node is the identifier
18900    indicating which attribute is in use.  The TREE_VALUE represents
18901    the arguments, if any.  */
18902
18903 static tree
18904 cp_parser_attribute_list (cp_parser* parser)
18905 {
18906   tree attribute_list = NULL_TREE;
18907   bool save_translate_strings_p = parser->translate_strings_p;
18908
18909   parser->translate_strings_p = false;
18910   while (true)
18911     {
18912       cp_token *token;
18913       tree identifier;
18914       tree attribute;
18915
18916       /* Look for the identifier.  We also allow keywords here; for
18917          example `__attribute__ ((const))' is legal.  */
18918       token = cp_lexer_peek_token (parser->lexer);
18919       if (token->type == CPP_NAME
18920           || token->type == CPP_KEYWORD)
18921         {
18922           tree arguments = NULL_TREE;
18923
18924           /* Consume the token.  */
18925           token = cp_lexer_consume_token (parser->lexer);
18926
18927           /* Save away the identifier that indicates which attribute
18928              this is.  */
18929           identifier = (token->type == CPP_KEYWORD) 
18930             /* For keywords, use the canonical spelling, not the
18931                parsed identifier.  */
18932             ? ridpointers[(int) token->keyword]
18933             : token->u.value;
18934           
18935           attribute = build_tree_list (identifier, NULL_TREE);
18936
18937           /* Peek at the next token.  */
18938           token = cp_lexer_peek_token (parser->lexer);
18939           /* If it's an `(', then parse the attribute arguments.  */
18940           if (token->type == CPP_OPEN_PAREN)
18941             {
18942               VEC(tree,gc) *vec;
18943               int attr_flag = (attribute_takes_identifier_p (identifier)
18944                                ? id_attr : normal_attr);
18945               vec = cp_parser_parenthesized_expression_list
18946                     (parser, attr_flag, /*cast_p=*/false,
18947                      /*allow_expansion_p=*/false,
18948                      /*non_constant_p=*/NULL);
18949               if (vec == NULL)
18950                 arguments = error_mark_node;
18951               else
18952                 {
18953                   arguments = build_tree_list_vec (vec);
18954                   release_tree_vector (vec);
18955                 }
18956               /* Save the arguments away.  */
18957               TREE_VALUE (attribute) = arguments;
18958             }
18959
18960           if (arguments != error_mark_node)
18961             {
18962               /* Add this attribute to the list.  */
18963               TREE_CHAIN (attribute) = attribute_list;
18964               attribute_list = attribute;
18965             }
18966
18967           token = cp_lexer_peek_token (parser->lexer);
18968         }
18969       /* Now, look for more attributes.  If the next token isn't a
18970          `,', we're done.  */
18971       if (token->type != CPP_COMMA)
18972         break;
18973
18974       /* Consume the comma and keep going.  */
18975       cp_lexer_consume_token (parser->lexer);
18976     }
18977   parser->translate_strings_p = save_translate_strings_p;
18978
18979   /* We built up the list in reverse order.  */
18980   return nreverse (attribute_list);
18981 }
18982
18983 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
18984    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
18985    current value of the PEDANTIC flag, regardless of whether or not
18986    the `__extension__' keyword is present.  The caller is responsible
18987    for restoring the value of the PEDANTIC flag.  */
18988
18989 static bool
18990 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
18991 {
18992   /* Save the old value of the PEDANTIC flag.  */
18993   *saved_pedantic = pedantic;
18994
18995   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
18996     {
18997       /* Consume the `__extension__' token.  */
18998       cp_lexer_consume_token (parser->lexer);
18999       /* We're not being pedantic while the `__extension__' keyword is
19000          in effect.  */
19001       pedantic = 0;
19002
19003       return true;
19004     }
19005
19006   return false;
19007 }
19008
19009 /* Parse a label declaration.
19010
19011    label-declaration:
19012      __label__ label-declarator-seq ;
19013
19014    label-declarator-seq:
19015      identifier , label-declarator-seq
19016      identifier  */
19017
19018 static void
19019 cp_parser_label_declaration (cp_parser* parser)
19020 {
19021   /* Look for the `__label__' keyword.  */
19022   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19023
19024   while (true)
19025     {
19026       tree identifier;
19027
19028       /* Look for an identifier.  */
19029       identifier = cp_parser_identifier (parser);
19030       /* If we failed, stop.  */
19031       if (identifier == error_mark_node)
19032         break;
19033       /* Declare it as a label.  */
19034       finish_label_decl (identifier);
19035       /* If the next token is a `;', stop.  */
19036       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19037         break;
19038       /* Look for the `,' separating the label declarations.  */
19039       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19040     }
19041
19042   /* Look for the final `;'.  */
19043   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19044 }
19045
19046 /* Support Functions */
19047
19048 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
19049    NAME should have one of the representations used for an
19050    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
19051    is returned.  If PARSER->SCOPE is a dependent type, then a
19052    SCOPE_REF is returned.
19053
19054    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
19055    returned; the name was already resolved when the TEMPLATE_ID_EXPR
19056    was formed.  Abstractly, such entities should not be passed to this
19057    function, because they do not need to be looked up, but it is
19058    simpler to check for this special case here, rather than at the
19059    call-sites.
19060
19061    In cases not explicitly covered above, this function returns a
19062    DECL, OVERLOAD, or baselink representing the result of the lookup.
19063    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
19064    is returned.
19065
19066    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
19067    (e.g., "struct") that was used.  In that case bindings that do not
19068    refer to types are ignored.
19069
19070    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
19071    ignored.
19072
19073    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
19074    are ignored.
19075
19076    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
19077    types.
19078
19079    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
19080    TREE_LIST of candidates if name-lookup results in an ambiguity, and
19081    NULL_TREE otherwise.  */
19082
19083 static tree
19084 cp_parser_lookup_name (cp_parser *parser, tree name,
19085                        enum tag_types tag_type,
19086                        bool is_template,
19087                        bool is_namespace,
19088                        bool check_dependency,
19089                        tree *ambiguous_decls,
19090                        location_t name_location)
19091 {
19092   int flags = 0;
19093   tree decl;
19094   tree object_type = parser->context->object_type;
19095
19096   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
19097     flags |= LOOKUP_COMPLAIN;
19098
19099   /* Assume that the lookup will be unambiguous.  */
19100   if (ambiguous_decls)
19101     *ambiguous_decls = NULL_TREE;
19102
19103   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
19104      no longer valid.  Note that if we are parsing tentatively, and
19105      the parse fails, OBJECT_TYPE will be automatically restored.  */
19106   parser->context->object_type = NULL_TREE;
19107
19108   if (name == error_mark_node)
19109     return error_mark_node;
19110
19111   /* A template-id has already been resolved; there is no lookup to
19112      do.  */
19113   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
19114     return name;
19115   if (BASELINK_P (name))
19116     {
19117       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
19118                   == TEMPLATE_ID_EXPR);
19119       return name;
19120     }
19121
19122   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
19123      it should already have been checked to make sure that the name
19124      used matches the type being destroyed.  */
19125   if (TREE_CODE (name) == BIT_NOT_EXPR)
19126     {
19127       tree type;
19128
19129       /* Figure out to which type this destructor applies.  */
19130       if (parser->scope)
19131         type = parser->scope;
19132       else if (object_type)
19133         type = object_type;
19134       else
19135         type = current_class_type;
19136       /* If that's not a class type, there is no destructor.  */
19137       if (!type || !CLASS_TYPE_P (type))
19138         return error_mark_node;
19139       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
19140         lazily_declare_fn (sfk_destructor, type);
19141       if (!CLASSTYPE_DESTRUCTORS (type))
19142           return error_mark_node;
19143       /* If it was a class type, return the destructor.  */
19144       return CLASSTYPE_DESTRUCTORS (type);
19145     }
19146
19147   /* By this point, the NAME should be an ordinary identifier.  If
19148      the id-expression was a qualified name, the qualifying scope is
19149      stored in PARSER->SCOPE at this point.  */
19150   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
19151
19152   /* Perform the lookup.  */
19153   if (parser->scope)
19154     {
19155       bool dependent_p;
19156
19157       if (parser->scope == error_mark_node)
19158         return error_mark_node;
19159
19160       /* If the SCOPE is dependent, the lookup must be deferred until
19161          the template is instantiated -- unless we are explicitly
19162          looking up names in uninstantiated templates.  Even then, we
19163          cannot look up the name if the scope is not a class type; it
19164          might, for example, be a template type parameter.  */
19165       dependent_p = (TYPE_P (parser->scope)
19166                      && dependent_scope_p (parser->scope));
19167       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
19168           && dependent_p)
19169         /* Defer lookup.  */
19170         decl = error_mark_node;
19171       else
19172         {
19173           tree pushed_scope = NULL_TREE;
19174
19175           /* If PARSER->SCOPE is a dependent type, then it must be a
19176              class type, and we must not be checking dependencies;
19177              otherwise, we would have processed this lookup above.  So
19178              that PARSER->SCOPE is not considered a dependent base by
19179              lookup_member, we must enter the scope here.  */
19180           if (dependent_p)
19181             pushed_scope = push_scope (parser->scope);
19182
19183           /* If the PARSER->SCOPE is a template specialization, it
19184              may be instantiated during name lookup.  In that case,
19185              errors may be issued.  Even if we rollback the current
19186              tentative parse, those errors are valid.  */
19187           decl = lookup_qualified_name (parser->scope, name,
19188                                         tag_type != none_type,
19189                                         /*complain=*/true);
19190
19191           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
19192              lookup result and the nested-name-specifier nominates a class C:
19193                * if the name specified after the nested-name-specifier, when
19194                looked up in C, is the injected-class-name of C (Clause 9), or
19195                * if the name specified after the nested-name-specifier is the
19196                same as the identifier or the simple-template-id's template-
19197                name in the last component of the nested-name-specifier,
19198              the name is instead considered to name the constructor of
19199              class C. [ Note: for example, the constructor is not an
19200              acceptable lookup result in an elaborated-type-specifier so
19201              the constructor would not be used in place of the
19202              injected-class-name. --end note ] Such a constructor name
19203              shall be used only in the declarator-id of a declaration that
19204              names a constructor or in a using-declaration.  */
19205           if (tag_type == none_type
19206               && DECL_SELF_REFERENCE_P (decl)
19207               && same_type_p (DECL_CONTEXT (decl), parser->scope))
19208             decl = lookup_qualified_name (parser->scope, ctor_identifier,
19209                                           tag_type != none_type,
19210                                           /*complain=*/true);
19211
19212           /* If we have a single function from a using decl, pull it out.  */
19213           if (TREE_CODE (decl) == OVERLOAD
19214               && !really_overloaded_fn (decl))
19215             decl = OVL_FUNCTION (decl);
19216
19217           if (pushed_scope)
19218             pop_scope (pushed_scope);
19219         }
19220
19221       /* If the scope is a dependent type and either we deferred lookup or
19222          we did lookup but didn't find the name, rememeber the name.  */
19223       if (decl == error_mark_node && TYPE_P (parser->scope)
19224           && dependent_type_p (parser->scope))
19225         {
19226           if (tag_type)
19227             {
19228               tree type;
19229
19230               /* The resolution to Core Issue 180 says that `struct
19231                  A::B' should be considered a type-name, even if `A'
19232                  is dependent.  */
19233               type = make_typename_type (parser->scope, name, tag_type,
19234                                          /*complain=*/tf_error);
19235               decl = TYPE_NAME (type);
19236             }
19237           else if (is_template
19238                    && (cp_parser_next_token_ends_template_argument_p (parser)
19239                        || cp_lexer_next_token_is (parser->lexer,
19240                                                   CPP_CLOSE_PAREN)))
19241             decl = make_unbound_class_template (parser->scope,
19242                                                 name, NULL_TREE,
19243                                                 /*complain=*/tf_error);
19244           else
19245             decl = build_qualified_name (/*type=*/NULL_TREE,
19246                                          parser->scope, name,
19247                                          is_template);
19248         }
19249       parser->qualifying_scope = parser->scope;
19250       parser->object_scope = NULL_TREE;
19251     }
19252   else if (object_type)
19253     {
19254       tree object_decl = NULL_TREE;
19255       /* Look up the name in the scope of the OBJECT_TYPE, unless the
19256          OBJECT_TYPE is not a class.  */
19257       if (CLASS_TYPE_P (object_type))
19258         /* If the OBJECT_TYPE is a template specialization, it may
19259            be instantiated during name lookup.  In that case, errors
19260            may be issued.  Even if we rollback the current tentative
19261            parse, those errors are valid.  */
19262         object_decl = lookup_member (object_type,
19263                                      name,
19264                                      /*protect=*/0,
19265                                      tag_type != none_type);
19266       /* Look it up in the enclosing context, too.  */
19267       decl = lookup_name_real (name, tag_type != none_type,
19268                                /*nonclass=*/0,
19269                                /*block_p=*/true, is_namespace, flags);
19270       parser->object_scope = object_type;
19271       parser->qualifying_scope = NULL_TREE;
19272       if (object_decl)
19273         decl = object_decl;
19274     }
19275   else
19276     {
19277       decl = lookup_name_real (name, tag_type != none_type,
19278                                /*nonclass=*/0,
19279                                /*block_p=*/true, is_namespace, flags);
19280       parser->qualifying_scope = NULL_TREE;
19281       parser->object_scope = NULL_TREE;
19282     }
19283
19284   /* If the lookup failed, let our caller know.  */
19285   if (!decl || decl == error_mark_node)
19286     return error_mark_node;
19287
19288   /* Pull out the template from an injected-class-name (or multiple).  */
19289   if (is_template)
19290     decl = maybe_get_template_decl_from_type_decl (decl);
19291
19292   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
19293   if (TREE_CODE (decl) == TREE_LIST)
19294     {
19295       if (ambiguous_decls)
19296         *ambiguous_decls = decl;
19297       /* The error message we have to print is too complicated for
19298          cp_parser_error, so we incorporate its actions directly.  */
19299       if (!cp_parser_simulate_error (parser))
19300         {
19301           error_at (name_location, "reference to %qD is ambiguous",
19302                     name);
19303           print_candidates (decl);
19304         }
19305       return error_mark_node;
19306     }
19307
19308   gcc_assert (DECL_P (decl)
19309               || TREE_CODE (decl) == OVERLOAD
19310               || TREE_CODE (decl) == SCOPE_REF
19311               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
19312               || BASELINK_P (decl));
19313
19314   /* If we have resolved the name of a member declaration, check to
19315      see if the declaration is accessible.  When the name resolves to
19316      set of overloaded functions, accessibility is checked when
19317      overload resolution is done.
19318
19319      During an explicit instantiation, access is not checked at all,
19320      as per [temp.explicit].  */
19321   if (DECL_P (decl))
19322     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
19323
19324   return decl;
19325 }
19326
19327 /* Like cp_parser_lookup_name, but for use in the typical case where
19328    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
19329    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
19330
19331 static tree
19332 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
19333 {
19334   return cp_parser_lookup_name (parser, name,
19335                                 none_type,
19336                                 /*is_template=*/false,
19337                                 /*is_namespace=*/false,
19338                                 /*check_dependency=*/true,
19339                                 /*ambiguous_decls=*/NULL,
19340                                 location);
19341 }
19342
19343 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
19344    the current context, return the TYPE_DECL.  If TAG_NAME_P is
19345    true, the DECL indicates the class being defined in a class-head,
19346    or declared in an elaborated-type-specifier.
19347
19348    Otherwise, return DECL.  */
19349
19350 static tree
19351 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
19352 {
19353   /* If the TEMPLATE_DECL is being declared as part of a class-head,
19354      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
19355
19356        struct A {
19357          template <typename T> struct B;
19358        };
19359
19360        template <typename T> struct A::B {};
19361
19362      Similarly, in an elaborated-type-specifier:
19363
19364        namespace N { struct X{}; }
19365
19366        struct A {
19367          template <typename T> friend struct N::X;
19368        };
19369
19370      However, if the DECL refers to a class type, and we are in
19371      the scope of the class, then the name lookup automatically
19372      finds the TYPE_DECL created by build_self_reference rather
19373      than a TEMPLATE_DECL.  For example, in:
19374
19375        template <class T> struct S {
19376          S s;
19377        };
19378
19379      there is no need to handle such case.  */
19380
19381   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
19382     return DECL_TEMPLATE_RESULT (decl);
19383
19384   return decl;
19385 }
19386
19387 /* If too many, or too few, template-parameter lists apply to the
19388    declarator, issue an error message.  Returns TRUE if all went well,
19389    and FALSE otherwise.  */
19390
19391 static bool
19392 cp_parser_check_declarator_template_parameters (cp_parser* parser,
19393                                                 cp_declarator *declarator,
19394                                                 location_t declarator_location)
19395 {
19396   unsigned num_templates;
19397
19398   /* We haven't seen any classes that involve template parameters yet.  */
19399   num_templates = 0;
19400
19401   switch (declarator->kind)
19402     {
19403     case cdk_id:
19404       if (declarator->u.id.qualifying_scope)
19405         {
19406           tree scope;
19407
19408           scope = declarator->u.id.qualifying_scope;
19409
19410           while (scope && CLASS_TYPE_P (scope))
19411             {
19412               /* You're supposed to have one `template <...>'
19413                  for every template class, but you don't need one
19414                  for a full specialization.  For example:
19415
19416                  template <class T> struct S{};
19417                  template <> struct S<int> { void f(); };
19418                  void S<int>::f () {}
19419
19420                  is correct; there shouldn't be a `template <>' for
19421                  the definition of `S<int>::f'.  */
19422               if (!CLASSTYPE_TEMPLATE_INFO (scope))
19423                 /* If SCOPE does not have template information of any
19424                    kind, then it is not a template, nor is it nested
19425                    within a template.  */
19426                 break;
19427               if (explicit_class_specialization_p (scope))
19428                 break;
19429               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
19430                 ++num_templates;
19431
19432               scope = TYPE_CONTEXT (scope);
19433             }
19434         }
19435       else if (TREE_CODE (declarator->u.id.unqualified_name)
19436                == TEMPLATE_ID_EXPR)
19437         /* If the DECLARATOR has the form `X<y>' then it uses one
19438            additional level of template parameters.  */
19439         ++num_templates;
19440
19441       return cp_parser_check_template_parameters 
19442         (parser, num_templates, declarator_location, declarator);
19443
19444
19445     case cdk_function:
19446     case cdk_array:
19447     case cdk_pointer:
19448     case cdk_reference:
19449     case cdk_ptrmem:
19450       return (cp_parser_check_declarator_template_parameters
19451               (parser, declarator->declarator, declarator_location));
19452
19453     case cdk_error:
19454       return true;
19455
19456     default:
19457       gcc_unreachable ();
19458     }
19459   return false;
19460 }
19461
19462 /* NUM_TEMPLATES were used in the current declaration.  If that is
19463    invalid, return FALSE and issue an error messages.  Otherwise,
19464    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
19465    declarator and we can print more accurate diagnostics.  */
19466
19467 static bool
19468 cp_parser_check_template_parameters (cp_parser* parser,
19469                                      unsigned num_templates,
19470                                      location_t location,
19471                                      cp_declarator *declarator)
19472 {
19473   /* If there are the same number of template classes and parameter
19474      lists, that's OK.  */
19475   if (parser->num_template_parameter_lists == num_templates)
19476     return true;
19477   /* If there are more, but only one more, then we are referring to a
19478      member template.  That's OK too.  */
19479   if (parser->num_template_parameter_lists == num_templates + 1)
19480     return true;
19481   /* If there are more template classes than parameter lists, we have
19482      something like:
19483
19484        template <class T> void S<T>::R<T>::f ();  */
19485   if (parser->num_template_parameter_lists < num_templates)
19486     {
19487       if (declarator && !current_function_decl)
19488         error_at (location, "specializing member %<%T::%E%> "
19489                   "requires %<template<>%> syntax", 
19490                   declarator->u.id.qualifying_scope,
19491                   declarator->u.id.unqualified_name);
19492       else if (declarator)
19493         error_at (location, "invalid declaration of %<%T::%E%>",
19494                   declarator->u.id.qualifying_scope,
19495                   declarator->u.id.unqualified_name);
19496       else 
19497         error_at (location, "too few template-parameter-lists");
19498       return false;
19499     }
19500   /* Otherwise, there are too many template parameter lists.  We have
19501      something like:
19502
19503      template <class T> template <class U> void S::f();  */
19504   error_at (location, "too many template-parameter-lists");
19505   return false;
19506 }
19507
19508 /* Parse an optional `::' token indicating that the following name is
19509    from the global namespace.  If so, PARSER->SCOPE is set to the
19510    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
19511    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
19512    Returns the new value of PARSER->SCOPE, if the `::' token is
19513    present, and NULL_TREE otherwise.  */
19514
19515 static tree
19516 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
19517 {
19518   cp_token *token;
19519
19520   /* Peek at the next token.  */
19521   token = cp_lexer_peek_token (parser->lexer);
19522   /* If we're looking at a `::' token then we're starting from the
19523      global namespace, not our current location.  */
19524   if (token->type == CPP_SCOPE)
19525     {
19526       /* Consume the `::' token.  */
19527       cp_lexer_consume_token (parser->lexer);
19528       /* Set the SCOPE so that we know where to start the lookup.  */
19529       parser->scope = global_namespace;
19530       parser->qualifying_scope = global_namespace;
19531       parser->object_scope = NULL_TREE;
19532
19533       return parser->scope;
19534     }
19535   else if (!current_scope_valid_p)
19536     {
19537       parser->scope = NULL_TREE;
19538       parser->qualifying_scope = NULL_TREE;
19539       parser->object_scope = NULL_TREE;
19540     }
19541
19542   return NULL_TREE;
19543 }
19544
19545 /* Returns TRUE if the upcoming token sequence is the start of a
19546    constructor declarator.  If FRIEND_P is true, the declarator is
19547    preceded by the `friend' specifier.  */
19548
19549 static bool
19550 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
19551 {
19552   bool constructor_p;
19553   tree nested_name_specifier;
19554   cp_token *next_token;
19555
19556   /* The common case is that this is not a constructor declarator, so
19557      try to avoid doing lots of work if at all possible.  It's not
19558      valid declare a constructor at function scope.  */
19559   if (parser->in_function_body)
19560     return false;
19561   /* And only certain tokens can begin a constructor declarator.  */
19562   next_token = cp_lexer_peek_token (parser->lexer);
19563   if (next_token->type != CPP_NAME
19564       && next_token->type != CPP_SCOPE
19565       && next_token->type != CPP_NESTED_NAME_SPECIFIER
19566       && next_token->type != CPP_TEMPLATE_ID)
19567     return false;
19568
19569   /* Parse tentatively; we are going to roll back all of the tokens
19570      consumed here.  */
19571   cp_parser_parse_tentatively (parser);
19572   /* Assume that we are looking at a constructor declarator.  */
19573   constructor_p = true;
19574
19575   /* Look for the optional `::' operator.  */
19576   cp_parser_global_scope_opt (parser,
19577                               /*current_scope_valid_p=*/false);
19578   /* Look for the nested-name-specifier.  */
19579   nested_name_specifier
19580     = (cp_parser_nested_name_specifier_opt (parser,
19581                                             /*typename_keyword_p=*/false,
19582                                             /*check_dependency_p=*/false,
19583                                             /*type_p=*/false,
19584                                             /*is_declaration=*/false));
19585   /* Outside of a class-specifier, there must be a
19586      nested-name-specifier.  */
19587   if (!nested_name_specifier &&
19588       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
19589        || friend_p))
19590     constructor_p = false;
19591   else if (nested_name_specifier == error_mark_node)
19592     constructor_p = false;
19593
19594   /* If we have a class scope, this is easy; DR 147 says that S::S always
19595      names the constructor, and no other qualified name could.  */
19596   if (constructor_p && nested_name_specifier
19597       && CLASS_TYPE_P (nested_name_specifier))
19598     {
19599       tree id = cp_parser_unqualified_id (parser,
19600                                           /*template_keyword_p=*/false,
19601                                           /*check_dependency_p=*/false,
19602                                           /*declarator_p=*/true,
19603                                           /*optional_p=*/false);
19604       if (is_overloaded_fn (id))
19605         id = DECL_NAME (get_first_fn (id));
19606       if (!constructor_name_p (id, nested_name_specifier))
19607         constructor_p = false;
19608     }
19609   /* If we still think that this might be a constructor-declarator,
19610      look for a class-name.  */
19611   else if (constructor_p)
19612     {
19613       /* If we have:
19614
19615            template <typename T> struct S {
19616              S();
19617            };
19618
19619          we must recognize that the nested `S' names a class.  */
19620       tree type_decl;
19621       type_decl = cp_parser_class_name (parser,
19622                                         /*typename_keyword_p=*/false,
19623                                         /*template_keyword_p=*/false,
19624                                         none_type,
19625                                         /*check_dependency_p=*/false,
19626                                         /*class_head_p=*/false,
19627                                         /*is_declaration=*/false);
19628       /* If there was no class-name, then this is not a constructor.  */
19629       constructor_p = !cp_parser_error_occurred (parser);
19630
19631       /* If we're still considering a constructor, we have to see a `(',
19632          to begin the parameter-declaration-clause, followed by either a
19633          `)', an `...', or a decl-specifier.  We need to check for a
19634          type-specifier to avoid being fooled into thinking that:
19635
19636            S (f) (int);
19637
19638          is a constructor.  (It is actually a function named `f' that
19639          takes one parameter (of type `int') and returns a value of type
19640          `S'.  */
19641       if (constructor_p
19642           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
19643         constructor_p = false;
19644
19645       if (constructor_p
19646           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
19647           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
19648           /* A parameter declaration begins with a decl-specifier,
19649              which is either the "attribute" keyword, a storage class
19650              specifier, or (usually) a type-specifier.  */
19651           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
19652         {
19653           tree type;
19654           tree pushed_scope = NULL_TREE;
19655           unsigned saved_num_template_parameter_lists;
19656
19657           /* Names appearing in the type-specifier should be looked up
19658              in the scope of the class.  */
19659           if (current_class_type)
19660             type = NULL_TREE;
19661           else
19662             {
19663               type = TREE_TYPE (type_decl);
19664               if (TREE_CODE (type) == TYPENAME_TYPE)
19665                 {
19666                   type = resolve_typename_type (type,
19667                                                 /*only_current_p=*/false);
19668                   if (TREE_CODE (type) == TYPENAME_TYPE)
19669                     {
19670                       cp_parser_abort_tentative_parse (parser);
19671                       return false;
19672                     }
19673                 }
19674               pushed_scope = push_scope (type);
19675             }
19676
19677           /* Inside the constructor parameter list, surrounding
19678              template-parameter-lists do not apply.  */
19679           saved_num_template_parameter_lists
19680             = parser->num_template_parameter_lists;
19681           parser->num_template_parameter_lists = 0;
19682
19683           /* Look for the type-specifier.  */
19684           cp_parser_type_specifier (parser,
19685                                     CP_PARSER_FLAGS_NONE,
19686                                     /*decl_specs=*/NULL,
19687                                     /*is_declarator=*/true,
19688                                     /*declares_class_or_enum=*/NULL,
19689                                     /*is_cv_qualifier=*/NULL);
19690
19691           parser->num_template_parameter_lists
19692             = saved_num_template_parameter_lists;
19693
19694           /* Leave the scope of the class.  */
19695           if (pushed_scope)
19696             pop_scope (pushed_scope);
19697
19698           constructor_p = !cp_parser_error_occurred (parser);
19699         }
19700     }
19701
19702   /* We did not really want to consume any tokens.  */
19703   cp_parser_abort_tentative_parse (parser);
19704
19705   return constructor_p;
19706 }
19707
19708 /* Parse the definition of the function given by the DECL_SPECIFIERS,
19709    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
19710    they must be performed once we are in the scope of the function.
19711
19712    Returns the function defined.  */
19713
19714 static tree
19715 cp_parser_function_definition_from_specifiers_and_declarator
19716   (cp_parser* parser,
19717    cp_decl_specifier_seq *decl_specifiers,
19718    tree attributes,
19719    const cp_declarator *declarator)
19720 {
19721   tree fn;
19722   bool success_p;
19723
19724   /* Begin the function-definition.  */
19725   success_p = start_function (decl_specifiers, declarator, attributes);
19726
19727   /* The things we're about to see are not directly qualified by any
19728      template headers we've seen thus far.  */
19729   reset_specialization ();
19730
19731   /* If there were names looked up in the decl-specifier-seq that we
19732      did not check, check them now.  We must wait until we are in the
19733      scope of the function to perform the checks, since the function
19734      might be a friend.  */
19735   perform_deferred_access_checks ();
19736
19737   if (!success_p)
19738     {
19739       /* Skip the entire function.  */
19740       cp_parser_skip_to_end_of_block_or_statement (parser);
19741       fn = error_mark_node;
19742     }
19743   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
19744     {
19745       /* Seen already, skip it.  An error message has already been output.  */
19746       cp_parser_skip_to_end_of_block_or_statement (parser);
19747       fn = current_function_decl;
19748       current_function_decl = NULL_TREE;
19749       /* If this is a function from a class, pop the nested class.  */
19750       if (current_class_name)
19751         pop_nested_class ();
19752     }
19753   else
19754     {
19755       timevar_id_t tv;
19756       if (DECL_DECLARED_INLINE_P (current_function_decl))
19757         tv = TV_PARSE_INLINE;
19758       else
19759         tv = TV_PARSE_FUNC;
19760       timevar_push (tv);
19761       fn = cp_parser_function_definition_after_declarator (parser,
19762                                                          /*inline_p=*/false);
19763       timevar_pop (tv);
19764     }
19765
19766   return fn;
19767 }
19768
19769 /* Parse the part of a function-definition that follows the
19770    declarator.  INLINE_P is TRUE iff this function is an inline
19771    function defined within a class-specifier.
19772
19773    Returns the function defined.  */
19774
19775 static tree
19776 cp_parser_function_definition_after_declarator (cp_parser* parser,
19777                                                 bool inline_p)
19778 {
19779   tree fn;
19780   bool ctor_initializer_p = false;
19781   bool saved_in_unbraced_linkage_specification_p;
19782   bool saved_in_function_body;
19783   unsigned saved_num_template_parameter_lists;
19784   cp_token *token;
19785
19786   saved_in_function_body = parser->in_function_body;
19787   parser->in_function_body = true;
19788   /* If the next token is `return', then the code may be trying to
19789      make use of the "named return value" extension that G++ used to
19790      support.  */
19791   token = cp_lexer_peek_token (parser->lexer);
19792   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
19793     {
19794       /* Consume the `return' keyword.  */
19795       cp_lexer_consume_token (parser->lexer);
19796       /* Look for the identifier that indicates what value is to be
19797          returned.  */
19798       cp_parser_identifier (parser);
19799       /* Issue an error message.  */
19800       error_at (token->location,
19801                 "named return values are no longer supported");
19802       /* Skip tokens until we reach the start of the function body.  */
19803       while (true)
19804         {
19805           cp_token *token = cp_lexer_peek_token (parser->lexer);
19806           if (token->type == CPP_OPEN_BRACE
19807               || token->type == CPP_EOF
19808               || token->type == CPP_PRAGMA_EOL)
19809             break;
19810           cp_lexer_consume_token (parser->lexer);
19811         }
19812     }
19813   /* The `extern' in `extern "C" void f () { ... }' does not apply to
19814      anything declared inside `f'.  */
19815   saved_in_unbraced_linkage_specification_p
19816     = parser->in_unbraced_linkage_specification_p;
19817   parser->in_unbraced_linkage_specification_p = false;
19818   /* Inside the function, surrounding template-parameter-lists do not
19819      apply.  */
19820   saved_num_template_parameter_lists
19821     = parser->num_template_parameter_lists;
19822   parser->num_template_parameter_lists = 0;
19823
19824   start_lambda_scope (current_function_decl);
19825
19826   /* If the next token is `try', then we are looking at a
19827      function-try-block.  */
19828   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
19829     ctor_initializer_p = cp_parser_function_try_block (parser);
19830   /* A function-try-block includes the function-body, so we only do
19831      this next part if we're not processing a function-try-block.  */
19832   else
19833     ctor_initializer_p
19834       = cp_parser_ctor_initializer_opt_and_function_body (parser);
19835
19836   finish_lambda_scope ();
19837
19838   /* Finish the function.  */
19839   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
19840                         (inline_p ? 2 : 0));
19841   /* Generate code for it, if necessary.  */
19842   expand_or_defer_fn (fn);
19843   /* Restore the saved values.  */
19844   parser->in_unbraced_linkage_specification_p
19845     = saved_in_unbraced_linkage_specification_p;
19846   parser->num_template_parameter_lists
19847     = saved_num_template_parameter_lists;
19848   parser->in_function_body = saved_in_function_body;
19849
19850   return fn;
19851 }
19852
19853 /* Parse a template-declaration, assuming that the `export' (and
19854    `extern') keywords, if present, has already been scanned.  MEMBER_P
19855    is as for cp_parser_template_declaration.  */
19856
19857 static void
19858 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
19859 {
19860   tree decl = NULL_TREE;
19861   VEC (deferred_access_check,gc) *checks;
19862   tree parameter_list;
19863   bool friend_p = false;
19864   bool need_lang_pop;
19865   cp_token *token;
19866
19867   /* Look for the `template' keyword.  */
19868   token = cp_lexer_peek_token (parser->lexer);
19869   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
19870     return;
19871
19872   /* And the `<'.  */
19873   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
19874     return;
19875   if (at_class_scope_p () && current_function_decl)
19876     {
19877       /* 14.5.2.2 [temp.mem]
19878
19879          A local class shall not have member templates.  */
19880       error_at (token->location,
19881                 "invalid declaration of member template in local class");
19882       cp_parser_skip_to_end_of_block_or_statement (parser);
19883       return;
19884     }
19885   /* [temp]
19886
19887      A template ... shall not have C linkage.  */
19888   if (current_lang_name == lang_name_c)
19889     {
19890       error_at (token->location, "template with C linkage");
19891       /* Give it C++ linkage to avoid confusing other parts of the
19892          front end.  */
19893       push_lang_context (lang_name_cplusplus);
19894       need_lang_pop = true;
19895     }
19896   else
19897     need_lang_pop = false;
19898
19899   /* We cannot perform access checks on the template parameter
19900      declarations until we know what is being declared, just as we
19901      cannot check the decl-specifier list.  */
19902   push_deferring_access_checks (dk_deferred);
19903
19904   /* If the next token is `>', then we have an invalid
19905      specialization.  Rather than complain about an invalid template
19906      parameter, issue an error message here.  */
19907   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
19908     {
19909       cp_parser_error (parser, "invalid explicit specialization");
19910       begin_specialization ();
19911       parameter_list = NULL_TREE;
19912     }
19913   else
19914     {
19915       /* Parse the template parameters.  */
19916       parameter_list = cp_parser_template_parameter_list (parser);
19917       fixup_template_parms ();
19918     }
19919
19920   /* Get the deferred access checks from the parameter list.  These
19921      will be checked once we know what is being declared, as for a
19922      member template the checks must be performed in the scope of the
19923      class containing the member.  */
19924   checks = get_deferred_access_checks ();
19925
19926   /* Look for the `>'.  */
19927   cp_parser_skip_to_end_of_template_parameter_list (parser);
19928   /* We just processed one more parameter list.  */
19929   ++parser->num_template_parameter_lists;
19930   /* If the next token is `template', there are more template
19931      parameters.  */
19932   if (cp_lexer_next_token_is_keyword (parser->lexer,
19933                                       RID_TEMPLATE))
19934     cp_parser_template_declaration_after_export (parser, member_p);
19935   else
19936     {
19937       /* There are no access checks when parsing a template, as we do not
19938          know if a specialization will be a friend.  */
19939       push_deferring_access_checks (dk_no_check);
19940       token = cp_lexer_peek_token (parser->lexer);
19941       decl = cp_parser_single_declaration (parser,
19942                                            checks,
19943                                            member_p,
19944                                            /*explicit_specialization_p=*/false,
19945                                            &friend_p);
19946       pop_deferring_access_checks ();
19947
19948       /* If this is a member template declaration, let the front
19949          end know.  */
19950       if (member_p && !friend_p && decl)
19951         {
19952           if (TREE_CODE (decl) == TYPE_DECL)
19953             cp_parser_check_access_in_redeclaration (decl, token->location);
19954
19955           decl = finish_member_template_decl (decl);
19956         }
19957       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
19958         make_friend_class (current_class_type, TREE_TYPE (decl),
19959                            /*complain=*/true);
19960     }
19961   /* We are done with the current parameter list.  */
19962   --parser->num_template_parameter_lists;
19963
19964   pop_deferring_access_checks ();
19965
19966   /* Finish up.  */
19967   finish_template_decl (parameter_list);
19968
19969   /* Register member declarations.  */
19970   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
19971     finish_member_declaration (decl);
19972   /* For the erroneous case of a template with C linkage, we pushed an
19973      implicit C++ linkage scope; exit that scope now.  */
19974   if (need_lang_pop)
19975     pop_lang_context ();
19976   /* If DECL is a function template, we must return to parse it later.
19977      (Even though there is no definition, there might be default
19978      arguments that need handling.)  */
19979   if (member_p && decl
19980       && (TREE_CODE (decl) == FUNCTION_DECL
19981           || DECL_FUNCTION_TEMPLATE_P (decl)))
19982     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
19983 }
19984
19985 /* Perform the deferred access checks from a template-parameter-list.
19986    CHECKS is a TREE_LIST of access checks, as returned by
19987    get_deferred_access_checks.  */
19988
19989 static void
19990 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
19991 {
19992   ++processing_template_parmlist;
19993   perform_access_checks (checks);
19994   --processing_template_parmlist;
19995 }
19996
19997 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
19998    `function-definition' sequence.  MEMBER_P is true, this declaration
19999    appears in a class scope.
20000
20001    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20002    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20003
20004 static tree
20005 cp_parser_single_declaration (cp_parser* parser,
20006                               VEC (deferred_access_check,gc)* checks,
20007                               bool member_p,
20008                               bool explicit_specialization_p,
20009                               bool* friend_p)
20010 {
20011   int declares_class_or_enum;
20012   tree decl = NULL_TREE;
20013   cp_decl_specifier_seq decl_specifiers;
20014   bool function_definition_p = false;
20015   cp_token *decl_spec_token_start;
20016
20017   /* This function is only used when processing a template
20018      declaration.  */
20019   gcc_assert (innermost_scope_kind () == sk_template_parms
20020               || innermost_scope_kind () == sk_template_spec);
20021
20022   /* Defer access checks until we know what is being declared.  */
20023   push_deferring_access_checks (dk_deferred);
20024
20025   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
20026      alternative.  */
20027   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
20028   cp_parser_decl_specifier_seq (parser,
20029                                 CP_PARSER_FLAGS_OPTIONAL,
20030                                 &decl_specifiers,
20031                                 &declares_class_or_enum);
20032   if (friend_p)
20033     *friend_p = cp_parser_friend_p (&decl_specifiers);
20034
20035   /* There are no template typedefs.  */
20036   if (decl_specifiers.specs[(int) ds_typedef])
20037     {
20038       error_at (decl_spec_token_start->location,
20039                 "template declaration of %<typedef%>");
20040       decl = error_mark_node;
20041     }
20042
20043   /* Gather up the access checks that occurred the
20044      decl-specifier-seq.  */
20045   stop_deferring_access_checks ();
20046
20047   /* Check for the declaration of a template class.  */
20048   if (declares_class_or_enum)
20049     {
20050       if (cp_parser_declares_only_class_p (parser))
20051         {
20052           decl = shadow_tag (&decl_specifiers);
20053
20054           /* In this case:
20055
20056                struct C {
20057                  friend template <typename T> struct A<T>::B;
20058                };
20059
20060              A<T>::B will be represented by a TYPENAME_TYPE, and
20061              therefore not recognized by shadow_tag.  */
20062           if (friend_p && *friend_p
20063               && !decl
20064               && decl_specifiers.type
20065               && TYPE_P (decl_specifiers.type))
20066             decl = decl_specifiers.type;
20067
20068           if (decl && decl != error_mark_node)
20069             decl = TYPE_NAME (decl);
20070           else
20071             decl = error_mark_node;
20072
20073           /* Perform access checks for template parameters.  */
20074           cp_parser_perform_template_parameter_access_checks (checks);
20075         }
20076     }
20077
20078   /* Complain about missing 'typename' or other invalid type names.  */
20079   if (!decl_specifiers.any_type_specifiers_p
20080       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
20081     {
20082       /* cp_parser_parse_and_diagnose_invalid_type_name calls
20083          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
20084          the rest of this declaration.  */
20085       decl = error_mark_node;
20086       goto out;
20087     }
20088
20089   /* If it's not a template class, try for a template function.  If
20090      the next token is a `;', then this declaration does not declare
20091      anything.  But, if there were errors in the decl-specifiers, then
20092      the error might well have come from an attempted class-specifier.
20093      In that case, there's no need to warn about a missing declarator.  */
20094   if (!decl
20095       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
20096           || decl_specifiers.type != error_mark_node))
20097     {
20098       decl = cp_parser_init_declarator (parser,
20099                                         &decl_specifiers,
20100                                         checks,
20101                                         /*function_definition_allowed_p=*/true,
20102                                         member_p,
20103                                         declares_class_or_enum,
20104                                         &function_definition_p,
20105                                         NULL);
20106
20107     /* 7.1.1-1 [dcl.stc]
20108
20109        A storage-class-specifier shall not be specified in an explicit
20110        specialization...  */
20111     if (decl
20112         && explicit_specialization_p
20113         && decl_specifiers.storage_class != sc_none)
20114       {
20115         error_at (decl_spec_token_start->location,
20116                   "explicit template specialization cannot have a storage class");
20117         decl = error_mark_node;
20118       }
20119     }
20120
20121   /* Look for a trailing `;' after the declaration.  */
20122   if (!function_definition_p
20123       && (decl == error_mark_node
20124           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
20125     cp_parser_skip_to_end_of_block_or_statement (parser);
20126
20127  out:
20128   pop_deferring_access_checks ();
20129
20130   /* Clear any current qualification; whatever comes next is the start
20131      of something new.  */
20132   parser->scope = NULL_TREE;
20133   parser->qualifying_scope = NULL_TREE;
20134   parser->object_scope = NULL_TREE;
20135
20136   return decl;
20137 }
20138
20139 /* Parse a cast-expression that is not the operand of a unary "&".  */
20140
20141 static tree
20142 cp_parser_simple_cast_expression (cp_parser *parser)
20143 {
20144   return cp_parser_cast_expression (parser, /*address_p=*/false,
20145                                     /*cast_p=*/false, NULL);
20146 }
20147
20148 /* Parse a functional cast to TYPE.  Returns an expression
20149    representing the cast.  */
20150
20151 static tree
20152 cp_parser_functional_cast (cp_parser* parser, tree type)
20153 {
20154   VEC(tree,gc) *vec;
20155   tree expression_list;
20156   tree cast;
20157   bool nonconst_p;
20158
20159   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
20160     {
20161       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
20162       expression_list = cp_parser_braced_list (parser, &nonconst_p);
20163       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
20164       if (TREE_CODE (type) == TYPE_DECL)
20165         type = TREE_TYPE (type);
20166       return finish_compound_literal (type, expression_list,
20167                                       tf_warning_or_error);
20168     }
20169
20170
20171   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
20172                                                  /*cast_p=*/true,
20173                                                  /*allow_expansion_p=*/true,
20174                                                  /*non_constant_p=*/NULL);
20175   if (vec == NULL)
20176     expression_list = error_mark_node;
20177   else
20178     {
20179       expression_list = build_tree_list_vec (vec);
20180       release_tree_vector (vec);
20181     }
20182
20183   cast = build_functional_cast (type, expression_list,
20184                                 tf_warning_or_error);
20185   /* [expr.const]/1: In an integral constant expression "only type
20186      conversions to integral or enumeration type can be used".  */
20187   if (TREE_CODE (type) == TYPE_DECL)
20188     type = TREE_TYPE (type);
20189   if (cast != error_mark_node
20190       && !cast_valid_in_integral_constant_expression_p (type)
20191       && cp_parser_non_integral_constant_expression (parser,
20192                                                      NIC_CONSTRUCTOR))
20193     return error_mark_node;
20194   return cast;
20195 }
20196
20197 /* Save the tokens that make up the body of a member function defined
20198    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
20199    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
20200    specifiers applied to the declaration.  Returns the FUNCTION_DECL
20201    for the member function.  */
20202
20203 static tree
20204 cp_parser_save_member_function_body (cp_parser* parser,
20205                                      cp_decl_specifier_seq *decl_specifiers,
20206                                      cp_declarator *declarator,
20207                                      tree attributes)
20208 {
20209   cp_token *first;
20210   cp_token *last;
20211   tree fn;
20212
20213   /* Create the FUNCTION_DECL.  */
20214   fn = grokmethod (decl_specifiers, declarator, attributes);
20215   /* If something went badly wrong, bail out now.  */
20216   if (fn == error_mark_node)
20217     {
20218       /* If there's a function-body, skip it.  */
20219       if (cp_parser_token_starts_function_definition_p
20220           (cp_lexer_peek_token (parser->lexer)))
20221         cp_parser_skip_to_end_of_block_or_statement (parser);
20222       return error_mark_node;
20223     }
20224
20225   /* Remember it, if there default args to post process.  */
20226   cp_parser_save_default_args (parser, fn);
20227
20228   /* Save away the tokens that make up the body of the
20229      function.  */
20230   first = parser->lexer->next_token;
20231   /* We can have braced-init-list mem-initializers before the fn body.  */
20232   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
20233     {
20234       cp_lexer_consume_token (parser->lexer);
20235       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
20236              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
20237         {
20238           /* cache_group will stop after an un-nested { } pair, too.  */
20239           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
20240             break;
20241
20242           /* variadic mem-inits have ... after the ')'.  */
20243           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20244             cp_lexer_consume_token (parser->lexer);
20245         }
20246     }
20247   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20248   /* Handle function try blocks.  */
20249   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
20250     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
20251   last = parser->lexer->next_token;
20252
20253   /* Save away the inline definition; we will process it when the
20254      class is complete.  */
20255   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
20256   DECL_PENDING_INLINE_P (fn) = 1;
20257
20258   /* We need to know that this was defined in the class, so that
20259      friend templates are handled correctly.  */
20260   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
20261
20262   /* Add FN to the queue of functions to be parsed later.  */
20263   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
20264
20265   return fn;
20266 }
20267
20268 /* Parse a template-argument-list, as well as the trailing ">" (but
20269    not the opening ">").  See cp_parser_template_argument_list for the
20270    return value.  */
20271
20272 static tree
20273 cp_parser_enclosed_template_argument_list (cp_parser* parser)
20274 {
20275   tree arguments;
20276   tree saved_scope;
20277   tree saved_qualifying_scope;
20278   tree saved_object_scope;
20279   bool saved_greater_than_is_operator_p;
20280   int saved_unevaluated_operand;
20281   int saved_inhibit_evaluation_warnings;
20282
20283   /* [temp.names]
20284
20285      When parsing a template-id, the first non-nested `>' is taken as
20286      the end of the template-argument-list rather than a greater-than
20287      operator.  */
20288   saved_greater_than_is_operator_p
20289     = parser->greater_than_is_operator_p;
20290   parser->greater_than_is_operator_p = false;
20291   /* Parsing the argument list may modify SCOPE, so we save it
20292      here.  */
20293   saved_scope = parser->scope;
20294   saved_qualifying_scope = parser->qualifying_scope;
20295   saved_object_scope = parser->object_scope;
20296   /* We need to evaluate the template arguments, even though this
20297      template-id may be nested within a "sizeof".  */
20298   saved_unevaluated_operand = cp_unevaluated_operand;
20299   cp_unevaluated_operand = 0;
20300   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
20301   c_inhibit_evaluation_warnings = 0;
20302   /* Parse the template-argument-list itself.  */
20303   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
20304       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20305     arguments = NULL_TREE;
20306   else
20307     arguments = cp_parser_template_argument_list (parser);
20308   /* Look for the `>' that ends the template-argument-list. If we find
20309      a '>>' instead, it's probably just a typo.  */
20310   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
20311     {
20312       if (cxx_dialect != cxx98)
20313         {
20314           /* In C++0x, a `>>' in a template argument list or cast
20315              expression is considered to be two separate `>'
20316              tokens. So, change the current token to a `>', but don't
20317              consume it: it will be consumed later when the outer
20318              template argument list (or cast expression) is parsed.
20319              Note that this replacement of `>' for `>>' is necessary
20320              even if we are parsing tentatively: in the tentative
20321              case, after calling
20322              cp_parser_enclosed_template_argument_list we will always
20323              throw away all of the template arguments and the first
20324              closing `>', either because the template argument list
20325              was erroneous or because we are replacing those tokens
20326              with a CPP_TEMPLATE_ID token.  The second `>' (which will
20327              not have been thrown away) is needed either to close an
20328              outer template argument list or to complete a new-style
20329              cast.  */
20330           cp_token *token = cp_lexer_peek_token (parser->lexer);
20331           token->type = CPP_GREATER;
20332         }
20333       else if (!saved_greater_than_is_operator_p)
20334         {
20335           /* If we're in a nested template argument list, the '>>' has
20336             to be a typo for '> >'. We emit the error message, but we
20337             continue parsing and we push a '>' as next token, so that
20338             the argument list will be parsed correctly.  Note that the
20339             global source location is still on the token before the
20340             '>>', so we need to say explicitly where we want it.  */
20341           cp_token *token = cp_lexer_peek_token (parser->lexer);
20342           error_at (token->location, "%<>>%> should be %<> >%> "
20343                     "within a nested template argument list");
20344
20345           token->type = CPP_GREATER;
20346         }
20347       else
20348         {
20349           /* If this is not a nested template argument list, the '>>'
20350             is a typo for '>'. Emit an error message and continue.
20351             Same deal about the token location, but here we can get it
20352             right by consuming the '>>' before issuing the diagnostic.  */
20353           cp_token *token = cp_lexer_consume_token (parser->lexer);
20354           error_at (token->location,
20355                     "spurious %<>>%>, use %<>%> to terminate "
20356                     "a template argument list");
20357         }
20358     }
20359   else
20360     cp_parser_skip_to_end_of_template_parameter_list (parser);
20361   /* The `>' token might be a greater-than operator again now.  */
20362   parser->greater_than_is_operator_p
20363     = saved_greater_than_is_operator_p;
20364   /* Restore the SAVED_SCOPE.  */
20365   parser->scope = saved_scope;
20366   parser->qualifying_scope = saved_qualifying_scope;
20367   parser->object_scope = saved_object_scope;
20368   cp_unevaluated_operand = saved_unevaluated_operand;
20369   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
20370
20371   return arguments;
20372 }
20373
20374 /* MEMBER_FUNCTION is a member function, or a friend.  If default
20375    arguments, or the body of the function have not yet been parsed,
20376    parse them now.  */
20377
20378 static void
20379 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
20380 {
20381   timevar_push (TV_PARSE_INMETH);
20382   /* If this member is a template, get the underlying
20383      FUNCTION_DECL.  */
20384   if (DECL_FUNCTION_TEMPLATE_P (member_function))
20385     member_function = DECL_TEMPLATE_RESULT (member_function);
20386
20387   /* There should not be any class definitions in progress at this
20388      point; the bodies of members are only parsed outside of all class
20389      definitions.  */
20390   gcc_assert (parser->num_classes_being_defined == 0);
20391   /* While we're parsing the member functions we might encounter more
20392      classes.  We want to handle them right away, but we don't want
20393      them getting mixed up with functions that are currently in the
20394      queue.  */
20395   push_unparsed_function_queues (parser);
20396
20397   /* Make sure that any template parameters are in scope.  */
20398   maybe_begin_member_template_processing (member_function);
20399
20400   /* If the body of the function has not yet been parsed, parse it
20401      now.  */
20402   if (DECL_PENDING_INLINE_P (member_function))
20403     {
20404       tree function_scope;
20405       cp_token_cache *tokens;
20406
20407       /* The function is no longer pending; we are processing it.  */
20408       tokens = DECL_PENDING_INLINE_INFO (member_function);
20409       DECL_PENDING_INLINE_INFO (member_function) = NULL;
20410       DECL_PENDING_INLINE_P (member_function) = 0;
20411
20412       /* If this is a local class, enter the scope of the containing
20413          function.  */
20414       function_scope = current_function_decl;
20415       if (function_scope)
20416         push_function_context ();
20417
20418       /* Push the body of the function onto the lexer stack.  */
20419       cp_parser_push_lexer_for_tokens (parser, tokens);
20420
20421       /* Let the front end know that we going to be defining this
20422          function.  */
20423       start_preparsed_function (member_function, NULL_TREE,
20424                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
20425
20426       /* Don't do access checking if it is a templated function.  */
20427       if (processing_template_decl)
20428         push_deferring_access_checks (dk_no_check);
20429
20430       /* Now, parse the body of the function.  */
20431       cp_parser_function_definition_after_declarator (parser,
20432                                                       /*inline_p=*/true);
20433
20434       if (processing_template_decl)
20435         pop_deferring_access_checks ();
20436
20437       /* Leave the scope of the containing function.  */
20438       if (function_scope)
20439         pop_function_context ();
20440       cp_parser_pop_lexer (parser);
20441     }
20442
20443   /* Remove any template parameters from the symbol table.  */
20444   maybe_end_member_template_processing ();
20445
20446   /* Restore the queue.  */
20447   pop_unparsed_function_queues (parser);
20448   timevar_pop (TV_PARSE_INMETH);
20449 }
20450
20451 /* If DECL contains any default args, remember it on the unparsed
20452    functions queue.  */
20453
20454 static void
20455 cp_parser_save_default_args (cp_parser* parser, tree decl)
20456 {
20457   tree probe;
20458
20459   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
20460        probe;
20461        probe = TREE_CHAIN (probe))
20462     if (TREE_PURPOSE (probe))
20463       {
20464         cp_default_arg_entry *entry
20465           = VEC_safe_push (cp_default_arg_entry, gc,
20466                            unparsed_funs_with_default_args, NULL);
20467         entry->class_type = current_class_type;
20468         entry->decl = decl;
20469         break;
20470       }
20471 }
20472
20473 /* FN is a FUNCTION_DECL which may contains a parameter with an
20474    unparsed DEFAULT_ARG.  Parse the default args now.  This function
20475    assumes that the current scope is the scope in which the default
20476    argument should be processed.  */
20477
20478 static void
20479 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
20480 {
20481   bool saved_local_variables_forbidden_p;
20482   tree parm, parmdecl;
20483
20484   /* While we're parsing the default args, we might (due to the
20485      statement expression extension) encounter more classes.  We want
20486      to handle them right away, but we don't want them getting mixed
20487      up with default args that are currently in the queue.  */
20488   push_unparsed_function_queues (parser);
20489
20490   /* Local variable names (and the `this' keyword) may not appear
20491      in a default argument.  */
20492   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
20493   parser->local_variables_forbidden_p = true;
20494
20495   push_defarg_context (fn);
20496
20497   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
20498          parmdecl = DECL_ARGUMENTS (fn);
20499        parm && parm != void_list_node;
20500        parm = TREE_CHAIN (parm),
20501          parmdecl = DECL_CHAIN (parmdecl))
20502     {
20503       cp_token_cache *tokens;
20504       tree default_arg = TREE_PURPOSE (parm);
20505       tree parsed_arg;
20506       VEC(tree,gc) *insts;
20507       tree copy;
20508       unsigned ix;
20509
20510       if (!default_arg)
20511         continue;
20512
20513       if (TREE_CODE (default_arg) != DEFAULT_ARG)
20514         /* This can happen for a friend declaration for a function
20515            already declared with default arguments.  */
20516         continue;
20517
20518        /* Push the saved tokens for the default argument onto the parser's
20519           lexer stack.  */
20520       tokens = DEFARG_TOKENS (default_arg);
20521       cp_parser_push_lexer_for_tokens (parser, tokens);
20522
20523       start_lambda_scope (parmdecl);
20524
20525       /* Parse the assignment-expression.  */
20526       parsed_arg = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
20527       if (parsed_arg == error_mark_node)
20528         {
20529           cp_parser_pop_lexer (parser);
20530           continue;
20531         }
20532
20533       if (!processing_template_decl)
20534         parsed_arg = check_default_argument (TREE_VALUE (parm), parsed_arg);
20535
20536       TREE_PURPOSE (parm) = parsed_arg;
20537
20538       /* Update any instantiations we've already created.  */
20539       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
20540            VEC_iterate (tree, insts, ix, copy); ix++)
20541         TREE_PURPOSE (copy) = parsed_arg;
20542
20543       finish_lambda_scope ();
20544
20545       /* If the token stream has not been completely used up, then
20546          there was extra junk after the end of the default
20547          argument.  */
20548       if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
20549         cp_parser_error (parser, "expected %<,%>");
20550
20551       /* Revert to the main lexer.  */
20552       cp_parser_pop_lexer (parser);
20553     }
20554
20555   pop_defarg_context ();
20556
20557   /* Make sure no default arg is missing.  */
20558   check_default_args (fn);
20559
20560   /* Restore the state of local_variables_forbidden_p.  */
20561   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
20562
20563   /* Restore the queue.  */
20564   pop_unparsed_function_queues (parser);
20565 }
20566
20567 /* Parse the operand of `sizeof' (or a similar operator).  Returns
20568    either a TYPE or an expression, depending on the form of the
20569    input.  The KEYWORD indicates which kind of expression we have
20570    encountered.  */
20571
20572 static tree
20573 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
20574 {
20575   tree expr = NULL_TREE;
20576   const char *saved_message;
20577   char *tmp;
20578   bool saved_integral_constant_expression_p;
20579   bool saved_non_integral_constant_expression_p;
20580   bool pack_expansion_p = false;
20581
20582   /* Types cannot be defined in a `sizeof' expression.  Save away the
20583      old message.  */
20584   saved_message = parser->type_definition_forbidden_message;
20585   /* And create the new one.  */
20586   tmp = concat ("types may not be defined in %<",
20587                 IDENTIFIER_POINTER (ridpointers[keyword]),
20588                 "%> expressions", NULL);
20589   parser->type_definition_forbidden_message = tmp;
20590
20591   /* The restrictions on constant-expressions do not apply inside
20592      sizeof expressions.  */
20593   saved_integral_constant_expression_p
20594     = parser->integral_constant_expression_p;
20595   saved_non_integral_constant_expression_p
20596     = parser->non_integral_constant_expression_p;
20597   parser->integral_constant_expression_p = false;
20598
20599   /* If it's a `...', then we are computing the length of a parameter
20600      pack.  */
20601   if (keyword == RID_SIZEOF
20602       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
20603     {
20604       /* Consume the `...'.  */
20605       cp_lexer_consume_token (parser->lexer);
20606       maybe_warn_variadic_templates ();
20607
20608       /* Note that this is an expansion.  */
20609       pack_expansion_p = true;
20610     }
20611
20612   /* Do not actually evaluate the expression.  */
20613   ++cp_unevaluated_operand;
20614   ++c_inhibit_evaluation_warnings;
20615   /* If it's a `(', then we might be looking at the type-id
20616      construction.  */
20617   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
20618     {
20619       tree type;
20620       bool saved_in_type_id_in_expr_p;
20621
20622       /* We can't be sure yet whether we're looking at a type-id or an
20623          expression.  */
20624       cp_parser_parse_tentatively (parser);
20625       /* Consume the `('.  */
20626       cp_lexer_consume_token (parser->lexer);
20627       /* Parse the type-id.  */
20628       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
20629       parser->in_type_id_in_expr_p = true;
20630       type = cp_parser_type_id (parser);
20631       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
20632       /* Now, look for the trailing `)'.  */
20633       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20634       /* If all went well, then we're done.  */
20635       if (cp_parser_parse_definitely (parser))
20636         {
20637           cp_decl_specifier_seq decl_specs;
20638
20639           /* Build a trivial decl-specifier-seq.  */
20640           clear_decl_specs (&decl_specs);
20641           decl_specs.type = type;
20642
20643           /* Call grokdeclarator to figure out what type this is.  */
20644           expr = grokdeclarator (NULL,
20645                                  &decl_specs,
20646                                  TYPENAME,
20647                                  /*initialized=*/0,
20648                                  /*attrlist=*/NULL);
20649         }
20650     }
20651
20652   /* If the type-id production did not work out, then we must be
20653      looking at the unary-expression production.  */
20654   if (!expr)
20655     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
20656                                        /*cast_p=*/false, NULL);
20657
20658   if (pack_expansion_p)
20659     /* Build a pack expansion. */
20660     expr = make_pack_expansion (expr);
20661
20662   /* Go back to evaluating expressions.  */
20663   --cp_unevaluated_operand;
20664   --c_inhibit_evaluation_warnings;
20665
20666   /* Free the message we created.  */
20667   free (tmp);
20668   /* And restore the old one.  */
20669   parser->type_definition_forbidden_message = saved_message;
20670   parser->integral_constant_expression_p
20671     = saved_integral_constant_expression_p;
20672   parser->non_integral_constant_expression_p
20673     = saved_non_integral_constant_expression_p;
20674
20675   return expr;
20676 }
20677
20678 /* If the current declaration has no declarator, return true.  */
20679
20680 static bool
20681 cp_parser_declares_only_class_p (cp_parser *parser)
20682 {
20683   /* If the next token is a `;' or a `,' then there is no
20684      declarator.  */
20685   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
20686           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
20687 }
20688
20689 /* Update the DECL_SPECS to reflect the storage class indicated by
20690    KEYWORD.  */
20691
20692 static void
20693 cp_parser_set_storage_class (cp_parser *parser,
20694                              cp_decl_specifier_seq *decl_specs,
20695                              enum rid keyword,
20696                              location_t location)
20697 {
20698   cp_storage_class storage_class;
20699
20700   if (parser->in_unbraced_linkage_specification_p)
20701     {
20702       error_at (location, "invalid use of %qD in linkage specification",
20703                 ridpointers[keyword]);
20704       return;
20705     }
20706   else if (decl_specs->storage_class != sc_none)
20707     {
20708       decl_specs->conflicting_specifiers_p = true;
20709       return;
20710     }
20711
20712   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
20713       && decl_specs->specs[(int) ds_thread])
20714     {
20715       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
20716       decl_specs->specs[(int) ds_thread] = 0;
20717     }
20718
20719   switch (keyword)
20720     {
20721     case RID_AUTO:
20722       storage_class = sc_auto;
20723       break;
20724     case RID_REGISTER:
20725       storage_class = sc_register;
20726       break;
20727     case RID_STATIC:
20728       storage_class = sc_static;
20729       break;
20730     case RID_EXTERN:
20731       storage_class = sc_extern;
20732       break;
20733     case RID_MUTABLE:
20734       storage_class = sc_mutable;
20735       break;
20736     default:
20737       gcc_unreachable ();
20738     }
20739   decl_specs->storage_class = storage_class;
20740
20741   /* A storage class specifier cannot be applied alongside a typedef 
20742      specifier. If there is a typedef specifier present then set 
20743      conflicting_specifiers_p which will trigger an error later
20744      on in grokdeclarator. */
20745   if (decl_specs->specs[(int)ds_typedef])
20746     decl_specs->conflicting_specifiers_p = true;
20747 }
20748
20749 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If USER_DEFINED_P
20750    is true, the type is a user-defined type; otherwise it is a
20751    built-in type specified by a keyword.  */
20752
20753 static void
20754 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
20755                               tree type_spec,
20756                               location_t location,
20757                               bool user_defined_p)
20758 {
20759   decl_specs->any_specifiers_p = true;
20760
20761   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
20762      (with, for example, in "typedef int wchar_t;") we remember that
20763      this is what happened.  In system headers, we ignore these
20764      declarations so that G++ can work with system headers that are not
20765      C++-safe.  */
20766   if (decl_specs->specs[(int) ds_typedef]
20767       && !user_defined_p
20768       && (type_spec == boolean_type_node
20769           || type_spec == char16_type_node
20770           || type_spec == char32_type_node
20771           || type_spec == wchar_type_node)
20772       && (decl_specs->type
20773           || decl_specs->specs[(int) ds_long]
20774           || decl_specs->specs[(int) ds_short]
20775           || decl_specs->specs[(int) ds_unsigned]
20776           || decl_specs->specs[(int) ds_signed]))
20777     {
20778       decl_specs->redefined_builtin_type = type_spec;
20779       if (!decl_specs->type)
20780         {
20781           decl_specs->type = type_spec;
20782           decl_specs->user_defined_type_p = false;
20783           decl_specs->type_location = location;
20784         }
20785     }
20786   else if (decl_specs->type)
20787     decl_specs->multiple_types_p = true;
20788   else
20789     {
20790       decl_specs->type = type_spec;
20791       decl_specs->user_defined_type_p = user_defined_p;
20792       decl_specs->redefined_builtin_type = NULL_TREE;
20793       decl_specs->type_location = location;
20794     }
20795 }
20796
20797 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
20798    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
20799
20800 static bool
20801 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
20802 {
20803   return decl_specifiers->specs[(int) ds_friend] != 0;
20804 }
20805
20806 /* Issue an error message indicating that TOKEN_DESC was expected.
20807    If KEYWORD is true, it indicated this function is called by
20808    cp_parser_require_keword and the required token can only be
20809    a indicated keyword. */
20810
20811 static void
20812 cp_parser_required_error (cp_parser *parser,
20813                           required_token token_desc,
20814                           bool keyword)
20815 {
20816   switch (token_desc)
20817     {
20818       case RT_NEW:
20819         cp_parser_error (parser, "expected %<new%>");
20820         return;
20821       case RT_DELETE:
20822         cp_parser_error (parser, "expected %<delete%>");
20823         return;
20824       case RT_RETURN:
20825         cp_parser_error (parser, "expected %<return%>");
20826         return;
20827       case RT_WHILE:
20828         cp_parser_error (parser, "expected %<while%>");
20829         return;
20830       case RT_EXTERN:
20831         cp_parser_error (parser, "expected %<extern%>");
20832         return;
20833       case RT_STATIC_ASSERT:
20834         cp_parser_error (parser, "expected %<static_assert%>");
20835         return;
20836       case RT_DECLTYPE:
20837         cp_parser_error (parser, "expected %<decltype%>");
20838         return;
20839       case RT_OPERATOR:
20840         cp_parser_error (parser, "expected %<operator%>");
20841         return;
20842       case RT_CLASS:
20843         cp_parser_error (parser, "expected %<class%>");
20844         return;
20845       case RT_TEMPLATE:
20846         cp_parser_error (parser, "expected %<template%>");
20847         return;
20848       case RT_NAMESPACE:
20849         cp_parser_error (parser, "expected %<namespace%>");
20850         return;
20851       case RT_USING:
20852         cp_parser_error (parser, "expected %<using%>");
20853         return;
20854       case RT_ASM:
20855         cp_parser_error (parser, "expected %<asm%>");
20856         return;
20857       case RT_TRY:
20858         cp_parser_error (parser, "expected %<try%>");
20859         return;
20860       case RT_CATCH:
20861         cp_parser_error (parser, "expected %<catch%>");
20862         return;
20863       case RT_THROW:
20864         cp_parser_error (parser, "expected %<throw%>");
20865         return;
20866       case RT_LABEL:
20867         cp_parser_error (parser, "expected %<__label__%>");
20868         return;
20869       case RT_AT_TRY:
20870         cp_parser_error (parser, "expected %<@try%>");
20871         return;
20872       case RT_AT_SYNCHRONIZED:
20873         cp_parser_error (parser, "expected %<@synchronized%>");
20874         return;
20875       case RT_AT_THROW:
20876         cp_parser_error (parser, "expected %<@throw%>");
20877         return;
20878       default:
20879         break;
20880     }
20881   if (!keyword)
20882     {
20883       switch (token_desc)
20884         {
20885           case RT_SEMICOLON:
20886             cp_parser_error (parser, "expected %<;%>");
20887             return;
20888           case RT_OPEN_PAREN:
20889             cp_parser_error (parser, "expected %<(%>");
20890             return;
20891           case RT_CLOSE_BRACE:
20892             cp_parser_error (parser, "expected %<}%>");
20893             return;
20894           case RT_OPEN_BRACE:
20895             cp_parser_error (parser, "expected %<{%>");
20896             return;
20897           case RT_CLOSE_SQUARE:
20898             cp_parser_error (parser, "expected %<]%>");
20899             return;
20900           case RT_OPEN_SQUARE:
20901             cp_parser_error (parser, "expected %<[%>");
20902             return;
20903           case RT_COMMA:
20904             cp_parser_error (parser, "expected %<,%>");
20905             return;
20906           case RT_SCOPE:
20907             cp_parser_error (parser, "expected %<::%>");
20908             return;
20909           case RT_LESS:
20910             cp_parser_error (parser, "expected %<<%>");
20911             return;
20912           case RT_GREATER:
20913             cp_parser_error (parser, "expected %<>%>");
20914             return;
20915           case RT_EQ:
20916             cp_parser_error (parser, "expected %<=%>");
20917             return;
20918           case RT_ELLIPSIS:
20919             cp_parser_error (parser, "expected %<...%>");
20920             return;
20921           case RT_MULT:
20922             cp_parser_error (parser, "expected %<*%>");
20923             return;
20924           case RT_COMPL:
20925             cp_parser_error (parser, "expected %<~%>");
20926             return;
20927           case RT_COLON:
20928             cp_parser_error (parser, "expected %<:%>");
20929             return;
20930           case RT_COLON_SCOPE:
20931             cp_parser_error (parser, "expected %<:%> or %<::%>");
20932             return;
20933           case RT_CLOSE_PAREN:
20934             cp_parser_error (parser, "expected %<)%>");
20935             return;
20936           case RT_COMMA_CLOSE_PAREN:
20937             cp_parser_error (parser, "expected %<,%> or %<)%>");
20938             return;
20939           case RT_PRAGMA_EOL:
20940             cp_parser_error (parser, "expected end of line");
20941             return;
20942           case RT_NAME:
20943             cp_parser_error (parser, "expected identifier");
20944             return;
20945           case RT_SELECT:
20946             cp_parser_error (parser, "expected selection-statement");
20947             return;
20948           case RT_INTERATION:
20949             cp_parser_error (parser, "expected iteration-statement");
20950             return;
20951           case RT_JUMP:
20952             cp_parser_error (parser, "expected jump-statement");
20953             return;
20954           case RT_CLASS_KEY:
20955             cp_parser_error (parser, "expected class-key");
20956             return;
20957           case RT_CLASS_TYPENAME_TEMPLATE:
20958             cp_parser_error (parser,
20959                  "expected %<class%>, %<typename%>, or %<template%>");
20960             return;
20961           default:
20962             gcc_unreachable ();
20963         }
20964     }
20965   else
20966     gcc_unreachable ();
20967 }
20968
20969
20970
20971 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
20972    issue an error message indicating that TOKEN_DESC was expected.
20973
20974    Returns the token consumed, if the token had the appropriate type.
20975    Otherwise, returns NULL.  */
20976
20977 static cp_token *
20978 cp_parser_require (cp_parser* parser,
20979                    enum cpp_ttype type,
20980                    required_token token_desc)
20981 {
20982   if (cp_lexer_next_token_is (parser->lexer, type))
20983     return cp_lexer_consume_token (parser->lexer);
20984   else
20985     {
20986       /* Output the MESSAGE -- unless we're parsing tentatively.  */
20987       if (!cp_parser_simulate_error (parser))
20988         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
20989       return NULL;
20990     }
20991 }
20992
20993 /* An error message is produced if the next token is not '>'.
20994    All further tokens are skipped until the desired token is
20995    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
20996
20997 static void
20998 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
20999 {
21000   /* Current level of '< ... >'.  */
21001   unsigned level = 0;
21002   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
21003   unsigned nesting_depth = 0;
21004
21005   /* Are we ready, yet?  If not, issue error message.  */
21006   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
21007     return;
21008
21009   /* Skip tokens until the desired token is found.  */
21010   while (true)
21011     {
21012       /* Peek at the next token.  */
21013       switch (cp_lexer_peek_token (parser->lexer)->type)
21014         {
21015         case CPP_LESS:
21016           if (!nesting_depth)
21017             ++level;
21018           break;
21019
21020         case CPP_RSHIFT:
21021           if (cxx_dialect == cxx98)
21022             /* C++0x views the `>>' operator as two `>' tokens, but
21023                C++98 does not. */
21024             break;
21025           else if (!nesting_depth && level-- == 0)
21026             {
21027               /* We've hit a `>>' where the first `>' closes the
21028                  template argument list, and the second `>' is
21029                  spurious.  Just consume the `>>' and stop; we've
21030                  already produced at least one error.  */
21031               cp_lexer_consume_token (parser->lexer);
21032               return;
21033             }
21034           /* Fall through for C++0x, so we handle the second `>' in
21035              the `>>'.  */
21036
21037         case CPP_GREATER:
21038           if (!nesting_depth && level-- == 0)
21039             {
21040               /* We've reached the token we want, consume it and stop.  */
21041               cp_lexer_consume_token (parser->lexer);
21042               return;
21043             }
21044           break;
21045
21046         case CPP_OPEN_PAREN:
21047         case CPP_OPEN_SQUARE:
21048           ++nesting_depth;
21049           break;
21050
21051         case CPP_CLOSE_PAREN:
21052         case CPP_CLOSE_SQUARE:
21053           if (nesting_depth-- == 0)
21054             return;
21055           break;
21056
21057         case CPP_EOF:
21058         case CPP_PRAGMA_EOL:
21059         case CPP_SEMICOLON:
21060         case CPP_OPEN_BRACE:
21061         case CPP_CLOSE_BRACE:
21062           /* The '>' was probably forgotten, don't look further.  */
21063           return;
21064
21065         default:
21066           break;
21067         }
21068
21069       /* Consume this token.  */
21070       cp_lexer_consume_token (parser->lexer);
21071     }
21072 }
21073
21074 /* If the next token is the indicated keyword, consume it.  Otherwise,
21075    issue an error message indicating that TOKEN_DESC was expected.
21076
21077    Returns the token consumed, if the token had the appropriate type.
21078    Otherwise, returns NULL.  */
21079
21080 static cp_token *
21081 cp_parser_require_keyword (cp_parser* parser,
21082                            enum rid keyword,
21083                            required_token token_desc)
21084 {
21085   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
21086
21087   if (token && token->keyword != keyword)
21088     {
21089       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
21090       return NULL;
21091     }
21092
21093   return token;
21094 }
21095
21096 /* Returns TRUE iff TOKEN is a token that can begin the body of a
21097    function-definition.  */
21098
21099 static bool
21100 cp_parser_token_starts_function_definition_p (cp_token* token)
21101 {
21102   return (/* An ordinary function-body begins with an `{'.  */
21103           token->type == CPP_OPEN_BRACE
21104           /* A ctor-initializer begins with a `:'.  */
21105           || token->type == CPP_COLON
21106           /* A function-try-block begins with `try'.  */
21107           || token->keyword == RID_TRY
21108           /* The named return value extension begins with `return'.  */
21109           || token->keyword == RID_RETURN);
21110 }
21111
21112 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
21113    definition.  */
21114
21115 static bool
21116 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
21117 {
21118   cp_token *token;
21119
21120   token = cp_lexer_peek_token (parser->lexer);
21121   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
21122 }
21123
21124 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
21125    C++0x) ending a template-argument.  */
21126
21127 static bool
21128 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
21129 {
21130   cp_token *token;
21131
21132   token = cp_lexer_peek_token (parser->lexer);
21133   return (token->type == CPP_COMMA 
21134           || token->type == CPP_GREATER
21135           || token->type == CPP_ELLIPSIS
21136           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
21137 }
21138
21139 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
21140    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
21141
21142 static bool
21143 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
21144                                                      size_t n)
21145 {
21146   cp_token *token;
21147
21148   token = cp_lexer_peek_nth_token (parser->lexer, n);
21149   if (token->type == CPP_LESS)
21150     return true;
21151   /* Check for the sequence `<::' in the original code. It would be lexed as
21152      `[:', where `[' is a digraph, and there is no whitespace before
21153      `:'.  */
21154   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
21155     {
21156       cp_token *token2;
21157       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
21158       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
21159         return true;
21160     }
21161   return false;
21162 }
21163
21164 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
21165    or none_type otherwise.  */
21166
21167 static enum tag_types
21168 cp_parser_token_is_class_key (cp_token* token)
21169 {
21170   switch (token->keyword)
21171     {
21172     case RID_CLASS:
21173       return class_type;
21174     case RID_STRUCT:
21175       return record_type;
21176     case RID_UNION:
21177       return union_type;
21178
21179     default:
21180       return none_type;
21181     }
21182 }
21183
21184 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
21185
21186 static void
21187 cp_parser_check_class_key (enum tag_types class_key, tree type)
21188 {
21189   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
21190     permerror (input_location, "%qs tag used in naming %q#T",
21191             class_key == union_type ? "union"
21192              : class_key == record_type ? "struct" : "class",
21193              type);
21194 }
21195
21196 /* Issue an error message if DECL is redeclared with different
21197    access than its original declaration [class.access.spec/3].
21198    This applies to nested classes and nested class templates.
21199    [class.mem/1].  */
21200
21201 static void
21202 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
21203 {
21204   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
21205     return;
21206
21207   if ((TREE_PRIVATE (decl)
21208        != (current_access_specifier == access_private_node))
21209       || (TREE_PROTECTED (decl)
21210           != (current_access_specifier == access_protected_node)))
21211     error_at (location, "%qD redeclared with different access", decl);
21212 }
21213
21214 /* Look for the `template' keyword, as a syntactic disambiguator.
21215    Return TRUE iff it is present, in which case it will be
21216    consumed.  */
21217
21218 static bool
21219 cp_parser_optional_template_keyword (cp_parser *parser)
21220 {
21221   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
21222     {
21223       /* The `template' keyword can only be used within templates;
21224          outside templates the parser can always figure out what is a
21225          template and what is not.  */
21226       if (!processing_template_decl)
21227         {
21228           cp_token *token = cp_lexer_peek_token (parser->lexer);
21229           error_at (token->location,
21230                     "%<template%> (as a disambiguator) is only allowed "
21231                     "within templates");
21232           /* If this part of the token stream is rescanned, the same
21233              error message would be generated.  So, we purge the token
21234              from the stream.  */
21235           cp_lexer_purge_token (parser->lexer);
21236           return false;
21237         }
21238       else
21239         {
21240           /* Consume the `template' keyword.  */
21241           cp_lexer_consume_token (parser->lexer);
21242           return true;
21243         }
21244     }
21245
21246   return false;
21247 }
21248
21249 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
21250    set PARSER->SCOPE, and perform other related actions.  */
21251
21252 static void
21253 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
21254 {
21255   int i;
21256   struct tree_check *check_value;
21257   deferred_access_check *chk;
21258   VEC (deferred_access_check,gc) *checks;
21259
21260   /* Get the stored value.  */
21261   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
21262   /* Perform any access checks that were deferred.  */
21263   checks = check_value->checks;
21264   if (checks)
21265     {
21266       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
21267         perform_or_defer_access_check (chk->binfo,
21268                                        chk->decl,
21269                                        chk->diag_decl);
21270     }
21271   /* Set the scope from the stored value.  */
21272   parser->scope = check_value->value;
21273   parser->qualifying_scope = check_value->qualifying_scope;
21274   parser->object_scope = NULL_TREE;
21275 }
21276
21277 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
21278    encounter the end of a block before what we were looking for.  */
21279
21280 static bool
21281 cp_parser_cache_group (cp_parser *parser,
21282                        enum cpp_ttype end,
21283                        unsigned depth)
21284 {
21285   while (true)
21286     {
21287       cp_token *token = cp_lexer_peek_token (parser->lexer);
21288
21289       /* Abort a parenthesized expression if we encounter a semicolon.  */
21290       if ((end == CPP_CLOSE_PAREN || depth == 0)
21291           && token->type == CPP_SEMICOLON)
21292         return true;
21293       /* If we've reached the end of the file, stop.  */
21294       if (token->type == CPP_EOF
21295           || (end != CPP_PRAGMA_EOL
21296               && token->type == CPP_PRAGMA_EOL))
21297         return true;
21298       if (token->type == CPP_CLOSE_BRACE && depth == 0)
21299         /* We've hit the end of an enclosing block, so there's been some
21300            kind of syntax error.  */
21301         return true;
21302
21303       /* Consume the token.  */
21304       cp_lexer_consume_token (parser->lexer);
21305       /* See if it starts a new group.  */
21306       if (token->type == CPP_OPEN_BRACE)
21307         {
21308           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
21309           /* In theory this should probably check end == '}', but
21310              cp_parser_save_member_function_body needs it to exit
21311              after either '}' or ')' when called with ')'.  */
21312           if (depth == 0)
21313             return false;
21314         }
21315       else if (token->type == CPP_OPEN_PAREN)
21316         {
21317           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
21318           if (depth == 0 && end == CPP_CLOSE_PAREN)
21319             return false;
21320         }
21321       else if (token->type == CPP_PRAGMA)
21322         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
21323       else if (token->type == end)
21324         return false;
21325     }
21326 }
21327
21328 /* Begin parsing tentatively.  We always save tokens while parsing
21329    tentatively so that if the tentative parsing fails we can restore the
21330    tokens.  */
21331
21332 static void
21333 cp_parser_parse_tentatively (cp_parser* parser)
21334 {
21335   /* Enter a new parsing context.  */
21336   parser->context = cp_parser_context_new (parser->context);
21337   /* Begin saving tokens.  */
21338   cp_lexer_save_tokens (parser->lexer);
21339   /* In order to avoid repetitive access control error messages,
21340      access checks are queued up until we are no longer parsing
21341      tentatively.  */
21342   push_deferring_access_checks (dk_deferred);
21343 }
21344
21345 /* Commit to the currently active tentative parse.  */
21346
21347 static void
21348 cp_parser_commit_to_tentative_parse (cp_parser* parser)
21349 {
21350   cp_parser_context *context;
21351   cp_lexer *lexer;
21352
21353   /* Mark all of the levels as committed.  */
21354   lexer = parser->lexer;
21355   for (context = parser->context; context->next; context = context->next)
21356     {
21357       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
21358         break;
21359       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
21360       while (!cp_lexer_saving_tokens (lexer))
21361         lexer = lexer->next;
21362       cp_lexer_commit_tokens (lexer);
21363     }
21364 }
21365
21366 /* Abort the currently active tentative parse.  All consumed tokens
21367    will be rolled back, and no diagnostics will be issued.  */
21368
21369 static void
21370 cp_parser_abort_tentative_parse (cp_parser* parser)
21371 {
21372   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
21373               || errorcount > 0);
21374   cp_parser_simulate_error (parser);
21375   /* Now, pretend that we want to see if the construct was
21376      successfully parsed.  */
21377   cp_parser_parse_definitely (parser);
21378 }
21379
21380 /* Stop parsing tentatively.  If a parse error has occurred, restore the
21381    token stream.  Otherwise, commit to the tokens we have consumed.
21382    Returns true if no error occurred; false otherwise.  */
21383
21384 static bool
21385 cp_parser_parse_definitely (cp_parser* parser)
21386 {
21387   bool error_occurred;
21388   cp_parser_context *context;
21389
21390   /* Remember whether or not an error occurred, since we are about to
21391      destroy that information.  */
21392   error_occurred = cp_parser_error_occurred (parser);
21393   /* Remove the topmost context from the stack.  */
21394   context = parser->context;
21395   parser->context = context->next;
21396   /* If no parse errors occurred, commit to the tentative parse.  */
21397   if (!error_occurred)
21398     {
21399       /* Commit to the tokens read tentatively, unless that was
21400          already done.  */
21401       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
21402         cp_lexer_commit_tokens (parser->lexer);
21403
21404       pop_to_parent_deferring_access_checks ();
21405     }
21406   /* Otherwise, if errors occurred, roll back our state so that things
21407      are just as they were before we began the tentative parse.  */
21408   else
21409     {
21410       cp_lexer_rollback_tokens (parser->lexer);
21411       pop_deferring_access_checks ();
21412     }
21413   /* Add the context to the front of the free list.  */
21414   context->next = cp_parser_context_free_list;
21415   cp_parser_context_free_list = context;
21416
21417   return !error_occurred;
21418 }
21419
21420 /* Returns true if we are parsing tentatively and are not committed to
21421    this tentative parse.  */
21422
21423 static bool
21424 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
21425 {
21426   return (cp_parser_parsing_tentatively (parser)
21427           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
21428 }
21429
21430 /* Returns nonzero iff an error has occurred during the most recent
21431    tentative parse.  */
21432
21433 static bool
21434 cp_parser_error_occurred (cp_parser* parser)
21435 {
21436   return (cp_parser_parsing_tentatively (parser)
21437           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
21438 }
21439
21440 /* Returns nonzero if GNU extensions are allowed.  */
21441
21442 static bool
21443 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
21444 {
21445   return parser->allow_gnu_extensions_p;
21446 }
21447 \f
21448 /* Objective-C++ Productions */
21449
21450
21451 /* Parse an Objective-C expression, which feeds into a primary-expression
21452    above.
21453
21454    objc-expression:
21455      objc-message-expression
21456      objc-string-literal
21457      objc-encode-expression
21458      objc-protocol-expression
21459      objc-selector-expression
21460
21461   Returns a tree representation of the expression.  */
21462
21463 static tree
21464 cp_parser_objc_expression (cp_parser* parser)
21465 {
21466   /* Try to figure out what kind of declaration is present.  */
21467   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
21468
21469   switch (kwd->type)
21470     {
21471     case CPP_OPEN_SQUARE:
21472       return cp_parser_objc_message_expression (parser);
21473
21474     case CPP_OBJC_STRING:
21475       kwd = cp_lexer_consume_token (parser->lexer);
21476       return objc_build_string_object (kwd->u.value);
21477
21478     case CPP_KEYWORD:
21479       switch (kwd->keyword)
21480         {
21481         case RID_AT_ENCODE:
21482           return cp_parser_objc_encode_expression (parser);
21483
21484         case RID_AT_PROTOCOL:
21485           return cp_parser_objc_protocol_expression (parser);
21486
21487         case RID_AT_SELECTOR:
21488           return cp_parser_objc_selector_expression (parser);
21489
21490         default:
21491           break;
21492         }
21493     default:
21494       error_at (kwd->location,
21495                 "misplaced %<@%D%> Objective-C++ construct",
21496                 kwd->u.value);
21497       cp_parser_skip_to_end_of_block_or_statement (parser);
21498     }
21499
21500   return error_mark_node;
21501 }
21502
21503 /* Parse an Objective-C message expression.
21504
21505    objc-message-expression:
21506      [ objc-message-receiver objc-message-args ]
21507
21508    Returns a representation of an Objective-C message.  */
21509
21510 static tree
21511 cp_parser_objc_message_expression (cp_parser* parser)
21512 {
21513   tree receiver, messageargs;
21514
21515   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
21516   receiver = cp_parser_objc_message_receiver (parser);
21517   messageargs = cp_parser_objc_message_args (parser);
21518   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
21519
21520   return objc_build_message_expr (receiver, messageargs);
21521 }
21522
21523 /* Parse an objc-message-receiver.
21524
21525    objc-message-receiver:
21526      expression
21527      simple-type-specifier
21528
21529   Returns a representation of the type or expression.  */
21530
21531 static tree
21532 cp_parser_objc_message_receiver (cp_parser* parser)
21533 {
21534   tree rcv;
21535
21536   /* An Objective-C message receiver may be either (1) a type
21537      or (2) an expression.  */
21538   cp_parser_parse_tentatively (parser);
21539   rcv = cp_parser_expression (parser, false, NULL);
21540
21541   if (cp_parser_parse_definitely (parser))
21542     return rcv;
21543
21544   rcv = cp_parser_simple_type_specifier (parser,
21545                                          /*decl_specs=*/NULL,
21546                                          CP_PARSER_FLAGS_NONE);
21547
21548   return objc_get_class_reference (rcv);
21549 }
21550
21551 /* Parse the arguments and selectors comprising an Objective-C message.
21552
21553    objc-message-args:
21554      objc-selector
21555      objc-selector-args
21556      objc-selector-args , objc-comma-args
21557
21558    objc-selector-args:
21559      objc-selector [opt] : assignment-expression
21560      objc-selector-args objc-selector [opt] : assignment-expression
21561
21562    objc-comma-args:
21563      assignment-expression
21564      objc-comma-args , assignment-expression
21565
21566    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
21567    selector arguments and TREE_VALUE containing a list of comma
21568    arguments.  */
21569
21570 static tree
21571 cp_parser_objc_message_args (cp_parser* parser)
21572 {
21573   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
21574   bool maybe_unary_selector_p = true;
21575   cp_token *token = cp_lexer_peek_token (parser->lexer);
21576
21577   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
21578     {
21579       tree selector = NULL_TREE, arg;
21580
21581       if (token->type != CPP_COLON)
21582         selector = cp_parser_objc_selector (parser);
21583
21584       /* Detect if we have a unary selector.  */
21585       if (maybe_unary_selector_p
21586           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
21587         return build_tree_list (selector, NULL_TREE);
21588
21589       maybe_unary_selector_p = false;
21590       cp_parser_require (parser, CPP_COLON, RT_COLON);
21591       arg = cp_parser_assignment_expression (parser, false, NULL);
21592
21593       sel_args
21594         = chainon (sel_args,
21595                    build_tree_list (selector, arg));
21596
21597       token = cp_lexer_peek_token (parser->lexer);
21598     }
21599
21600   /* Handle non-selector arguments, if any. */
21601   while (token->type == CPP_COMMA)
21602     {
21603       tree arg;
21604
21605       cp_lexer_consume_token (parser->lexer);
21606       arg = cp_parser_assignment_expression (parser, false, NULL);
21607
21608       addl_args
21609         = chainon (addl_args,
21610                    build_tree_list (NULL_TREE, arg));
21611
21612       token = cp_lexer_peek_token (parser->lexer);
21613     }
21614
21615   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
21616     {
21617       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
21618       return build_tree_list (error_mark_node, error_mark_node);
21619     }
21620
21621   return build_tree_list (sel_args, addl_args);
21622 }
21623
21624 /* Parse an Objective-C encode expression.
21625
21626    objc-encode-expression:
21627      @encode objc-typename
21628
21629    Returns an encoded representation of the type argument.  */
21630
21631 static tree
21632 cp_parser_objc_encode_expression (cp_parser* parser)
21633 {
21634   tree type;
21635   cp_token *token;
21636
21637   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
21638   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21639   token = cp_lexer_peek_token (parser->lexer);
21640   type = complete_type (cp_parser_type_id (parser));
21641   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21642
21643   if (!type)
21644     {
21645       error_at (token->location, 
21646                 "%<@encode%> must specify a type as an argument");
21647       return error_mark_node;
21648     }
21649
21650   /* This happens if we find @encode(T) (where T is a template
21651      typename or something dependent on a template typename) when
21652      parsing a template.  In that case, we can't compile it
21653      immediately, but we rather create an AT_ENCODE_EXPR which will
21654      need to be instantiated when the template is used.
21655   */
21656   if (dependent_type_p (type))
21657     {
21658       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
21659       TREE_READONLY (value) = 1;
21660       return value;
21661     }
21662
21663   return objc_build_encode_expr (type);
21664 }
21665
21666 /* Parse an Objective-C @defs expression.  */
21667
21668 static tree
21669 cp_parser_objc_defs_expression (cp_parser *parser)
21670 {
21671   tree name;
21672
21673   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
21674   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21675   name = cp_parser_identifier (parser);
21676   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21677
21678   return objc_get_class_ivars (name);
21679 }
21680
21681 /* Parse an Objective-C protocol expression.
21682
21683   objc-protocol-expression:
21684     @protocol ( identifier )
21685
21686   Returns a representation of the protocol expression.  */
21687
21688 static tree
21689 cp_parser_objc_protocol_expression (cp_parser* parser)
21690 {
21691   tree proto;
21692
21693   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
21694   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21695   proto = cp_parser_identifier (parser);
21696   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21697
21698   return objc_build_protocol_expr (proto);
21699 }
21700
21701 /* Parse an Objective-C selector expression.
21702
21703    objc-selector-expression:
21704      @selector ( objc-method-signature )
21705
21706    objc-method-signature:
21707      objc-selector
21708      objc-selector-seq
21709
21710    objc-selector-seq:
21711      objc-selector :
21712      objc-selector-seq objc-selector :
21713
21714   Returns a representation of the method selector.  */
21715
21716 static tree
21717 cp_parser_objc_selector_expression (cp_parser* parser)
21718 {
21719   tree sel_seq = NULL_TREE;
21720   bool maybe_unary_selector_p = true;
21721   cp_token *token;
21722   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
21723
21724   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
21725   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
21726   token = cp_lexer_peek_token (parser->lexer);
21727
21728   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
21729          || token->type == CPP_SCOPE)
21730     {
21731       tree selector = NULL_TREE;
21732
21733       if (token->type != CPP_COLON
21734           || token->type == CPP_SCOPE)
21735         selector = cp_parser_objc_selector (parser);
21736
21737       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
21738           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
21739         {
21740           /* Detect if we have a unary selector.  */
21741           if (maybe_unary_selector_p)
21742             {
21743               sel_seq = selector;
21744               goto finish_selector;
21745             }
21746           else
21747             {
21748               cp_parser_error (parser, "expected %<:%>");
21749             }
21750         }
21751       maybe_unary_selector_p = false;
21752       token = cp_lexer_consume_token (parser->lexer);
21753
21754       if (token->type == CPP_SCOPE)
21755         {
21756           sel_seq
21757             = chainon (sel_seq,
21758                        build_tree_list (selector, NULL_TREE));
21759           sel_seq
21760             = chainon (sel_seq,
21761                        build_tree_list (NULL_TREE, NULL_TREE));
21762         }
21763       else
21764         sel_seq
21765           = chainon (sel_seq,
21766                      build_tree_list (selector, NULL_TREE));
21767
21768       token = cp_lexer_peek_token (parser->lexer);
21769     }
21770
21771  finish_selector:
21772   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21773
21774   return objc_build_selector_expr (loc, sel_seq);
21775 }
21776
21777 /* Parse a list of identifiers.
21778
21779    objc-identifier-list:
21780      identifier
21781      objc-identifier-list , identifier
21782
21783    Returns a TREE_LIST of identifier nodes.  */
21784
21785 static tree
21786 cp_parser_objc_identifier_list (cp_parser* parser)
21787 {
21788   tree identifier;
21789   tree list;
21790   cp_token *sep;
21791
21792   identifier = cp_parser_identifier (parser);
21793   if (identifier == error_mark_node)
21794     return error_mark_node;      
21795
21796   list = build_tree_list (NULL_TREE, identifier);
21797   sep = cp_lexer_peek_token (parser->lexer);
21798
21799   while (sep->type == CPP_COMMA)
21800     {
21801       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
21802       identifier = cp_parser_identifier (parser);
21803       if (identifier == error_mark_node)
21804         return list;
21805
21806       list = chainon (list, build_tree_list (NULL_TREE,
21807                                              identifier));
21808       sep = cp_lexer_peek_token (parser->lexer);
21809     }
21810   
21811   return list;
21812 }
21813
21814 /* Parse an Objective-C alias declaration.
21815
21816    objc-alias-declaration:
21817      @compatibility_alias identifier identifier ;
21818
21819    This function registers the alias mapping with the Objective-C front end.
21820    It returns nothing.  */
21821
21822 static void
21823 cp_parser_objc_alias_declaration (cp_parser* parser)
21824 {
21825   tree alias, orig;
21826
21827   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
21828   alias = cp_parser_identifier (parser);
21829   orig = cp_parser_identifier (parser);
21830   objc_declare_alias (alias, orig);
21831   cp_parser_consume_semicolon_at_end_of_statement (parser);
21832 }
21833
21834 /* Parse an Objective-C class forward-declaration.
21835
21836    objc-class-declaration:
21837      @class objc-identifier-list ;
21838
21839    The function registers the forward declarations with the Objective-C
21840    front end.  It returns nothing.  */
21841
21842 static void
21843 cp_parser_objc_class_declaration (cp_parser* parser)
21844 {
21845   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
21846   while (true)
21847     {
21848       tree id;
21849       
21850       id = cp_parser_identifier (parser);
21851       if (id == error_mark_node)
21852         break;
21853       
21854       objc_declare_class (id);
21855
21856       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
21857         cp_lexer_consume_token (parser->lexer);
21858       else
21859         break;
21860     }
21861   cp_parser_consume_semicolon_at_end_of_statement (parser);
21862 }
21863
21864 /* Parse a list of Objective-C protocol references.
21865
21866    objc-protocol-refs-opt:
21867      objc-protocol-refs [opt]
21868
21869    objc-protocol-refs:
21870      < objc-identifier-list >
21871
21872    Returns a TREE_LIST of identifiers, if any.  */
21873
21874 static tree
21875 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
21876 {
21877   tree protorefs = NULL_TREE;
21878
21879   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
21880     {
21881       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
21882       protorefs = cp_parser_objc_identifier_list (parser);
21883       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
21884     }
21885
21886   return protorefs;
21887 }
21888
21889 /* Parse a Objective-C visibility specification.  */
21890
21891 static void
21892 cp_parser_objc_visibility_spec (cp_parser* parser)
21893 {
21894   cp_token *vis = cp_lexer_peek_token (parser->lexer);
21895
21896   switch (vis->keyword)
21897     {
21898     case RID_AT_PRIVATE:
21899       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
21900       break;
21901     case RID_AT_PROTECTED:
21902       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
21903       break;
21904     case RID_AT_PUBLIC:
21905       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
21906       break;
21907     case RID_AT_PACKAGE:
21908       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
21909       break;
21910     default:
21911       return;
21912     }
21913
21914   /* Eat '@private'/'@protected'/'@public'.  */
21915   cp_lexer_consume_token (parser->lexer);
21916 }
21917
21918 /* Parse an Objective-C method type.  Return 'true' if it is a class
21919    (+) method, and 'false' if it is an instance (-) method.  */
21920
21921 static inline bool
21922 cp_parser_objc_method_type (cp_parser* parser)
21923 {
21924   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
21925     return true;
21926   else
21927     return false;
21928 }
21929
21930 /* Parse an Objective-C protocol qualifier.  */
21931
21932 static tree
21933 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
21934 {
21935   tree quals = NULL_TREE, node;
21936   cp_token *token = cp_lexer_peek_token (parser->lexer);
21937
21938   node = token->u.value;
21939
21940   while (node && TREE_CODE (node) == IDENTIFIER_NODE
21941          && (node == ridpointers [(int) RID_IN]
21942              || node == ridpointers [(int) RID_OUT]
21943              || node == ridpointers [(int) RID_INOUT]
21944              || node == ridpointers [(int) RID_BYCOPY]
21945              || node == ridpointers [(int) RID_BYREF]
21946              || node == ridpointers [(int) RID_ONEWAY]))
21947     {
21948       quals = tree_cons (NULL_TREE, node, quals);
21949       cp_lexer_consume_token (parser->lexer);
21950       token = cp_lexer_peek_token (parser->lexer);
21951       node = token->u.value;
21952     }
21953
21954   return quals;
21955 }
21956
21957 /* Parse an Objective-C typename.  */
21958
21959 static tree
21960 cp_parser_objc_typename (cp_parser* parser)
21961 {
21962   tree type_name = NULL_TREE;
21963
21964   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21965     {
21966       tree proto_quals, cp_type = NULL_TREE;
21967
21968       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
21969       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
21970
21971       /* An ObjC type name may consist of just protocol qualifiers, in which
21972          case the type shall default to 'id'.  */
21973       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
21974         {
21975           cp_type = cp_parser_type_id (parser);
21976           
21977           /* If the type could not be parsed, an error has already
21978              been produced.  For error recovery, behave as if it had
21979              not been specified, which will use the default type
21980              'id'.  */
21981           if (cp_type == error_mark_node)
21982             {
21983               cp_type = NULL_TREE;
21984               /* We need to skip to the closing parenthesis as
21985                  cp_parser_type_id() does not seem to do it for
21986                  us.  */
21987               cp_parser_skip_to_closing_parenthesis (parser,
21988                                                      /*recovering=*/true,
21989                                                      /*or_comma=*/false,
21990                                                      /*consume_paren=*/false);
21991             }
21992         }
21993
21994       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21995       type_name = build_tree_list (proto_quals, cp_type);
21996     }
21997
21998   return type_name;
21999 }
22000
22001 /* Check to see if TYPE refers to an Objective-C selector name.  */
22002
22003 static bool
22004 cp_parser_objc_selector_p (enum cpp_ttype type)
22005 {
22006   return (type == CPP_NAME || type == CPP_KEYWORD
22007           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
22008           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
22009           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
22010           || type == CPP_XOR || type == CPP_XOR_EQ);
22011 }
22012
22013 /* Parse an Objective-C selector.  */
22014
22015 static tree
22016 cp_parser_objc_selector (cp_parser* parser)
22017 {
22018   cp_token *token = cp_lexer_consume_token (parser->lexer);
22019
22020   if (!cp_parser_objc_selector_p (token->type))
22021     {
22022       error_at (token->location, "invalid Objective-C++ selector name");
22023       return error_mark_node;
22024     }
22025
22026   /* C++ operator names are allowed to appear in ObjC selectors.  */
22027   switch (token->type)
22028     {
22029     case CPP_AND_AND: return get_identifier ("and");
22030     case CPP_AND_EQ: return get_identifier ("and_eq");
22031     case CPP_AND: return get_identifier ("bitand");
22032     case CPP_OR: return get_identifier ("bitor");
22033     case CPP_COMPL: return get_identifier ("compl");
22034     case CPP_NOT: return get_identifier ("not");
22035     case CPP_NOT_EQ: return get_identifier ("not_eq");
22036     case CPP_OR_OR: return get_identifier ("or");
22037     case CPP_OR_EQ: return get_identifier ("or_eq");
22038     case CPP_XOR: return get_identifier ("xor");
22039     case CPP_XOR_EQ: return get_identifier ("xor_eq");
22040     default: return token->u.value;
22041     }
22042 }
22043
22044 /* Parse an Objective-C params list.  */
22045
22046 static tree
22047 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
22048 {
22049   tree params = NULL_TREE;
22050   bool maybe_unary_selector_p = true;
22051   cp_token *token = cp_lexer_peek_token (parser->lexer);
22052
22053   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22054     {
22055       tree selector = NULL_TREE, type_name, identifier;
22056       tree parm_attr = NULL_TREE;
22057
22058       if (token->keyword == RID_ATTRIBUTE)
22059         break;
22060
22061       if (token->type != CPP_COLON)
22062         selector = cp_parser_objc_selector (parser);
22063
22064       /* Detect if we have a unary selector.  */
22065       if (maybe_unary_selector_p
22066           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22067         {
22068           params = selector; /* Might be followed by attributes.  */
22069           break;
22070         }
22071
22072       maybe_unary_selector_p = false;
22073       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
22074         {
22075           /* Something went quite wrong.  There should be a colon
22076              here, but there is not.  Stop parsing parameters.  */
22077           break;
22078         }
22079       type_name = cp_parser_objc_typename (parser);
22080       /* New ObjC allows attributes on parameters too.  */
22081       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
22082         parm_attr = cp_parser_attributes_opt (parser);
22083       identifier = cp_parser_identifier (parser);
22084
22085       params
22086         = chainon (params,
22087                    objc_build_keyword_decl (selector,
22088                                             type_name,
22089                                             identifier,
22090                                             parm_attr));
22091
22092       token = cp_lexer_peek_token (parser->lexer);
22093     }
22094
22095   if (params == NULL_TREE)
22096     {
22097       cp_parser_error (parser, "objective-c++ method declaration is expected");
22098       return error_mark_node;
22099     }
22100
22101   /* We allow tail attributes for the method.  */
22102   if (token->keyword == RID_ATTRIBUTE)
22103     {
22104       *attributes = cp_parser_attributes_opt (parser);
22105       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22106           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22107         return params;
22108       cp_parser_error (parser, 
22109                        "method attributes must be specified at the end");
22110       return error_mark_node;
22111     }
22112
22113   if (params == NULL_TREE)
22114     {
22115       cp_parser_error (parser, "objective-c++ method declaration is expected");
22116       return error_mark_node;
22117     }
22118   return params;
22119 }
22120
22121 /* Parse the non-keyword Objective-C params.  */
22122
22123 static tree
22124 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
22125                                        tree* attributes)
22126 {
22127   tree params = make_node (TREE_LIST);
22128   cp_token *token = cp_lexer_peek_token (parser->lexer);
22129   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
22130
22131   while (token->type == CPP_COMMA)
22132     {
22133       cp_parameter_declarator *parmdecl;
22134       tree parm;
22135
22136       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22137       token = cp_lexer_peek_token (parser->lexer);
22138
22139       if (token->type == CPP_ELLIPSIS)
22140         {
22141           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
22142           *ellipsisp = true;
22143           token = cp_lexer_peek_token (parser->lexer);
22144           break;
22145         }
22146
22147       /* TODO: parse attributes for tail parameters.  */
22148       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
22149       parm = grokdeclarator (parmdecl->declarator,
22150                              &parmdecl->decl_specifiers,
22151                              PARM, /*initialized=*/0,
22152                              /*attrlist=*/NULL);
22153
22154       chainon (params, build_tree_list (NULL_TREE, parm));
22155       token = cp_lexer_peek_token (parser->lexer);
22156     }
22157
22158   /* We allow tail attributes for the method.  */
22159   if (token->keyword == RID_ATTRIBUTE)
22160     {
22161       if (*attributes == NULL_TREE)
22162         {
22163           *attributes = cp_parser_attributes_opt (parser);
22164           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22165               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
22166             return params;
22167         }
22168       else        
22169         /* We have an error, but parse the attributes, so that we can 
22170            carry on.  */
22171         *attributes = cp_parser_attributes_opt (parser);
22172
22173       cp_parser_error (parser, 
22174                        "method attributes must be specified at the end");
22175       return error_mark_node;
22176     }
22177
22178   return params;
22179 }
22180
22181 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
22182
22183 static void
22184 cp_parser_objc_interstitial_code (cp_parser* parser)
22185 {
22186   cp_token *token = cp_lexer_peek_token (parser->lexer);
22187
22188   /* If the next token is `extern' and the following token is a string
22189      literal, then we have a linkage specification.  */
22190   if (token->keyword == RID_EXTERN
22191       && cp_parser_is_string_literal (cp_lexer_peek_nth_token (parser->lexer, 2)))
22192     cp_parser_linkage_specification (parser);
22193   /* Handle #pragma, if any.  */
22194   else if (token->type == CPP_PRAGMA)
22195     cp_parser_pragma (parser, pragma_external);
22196   /* Allow stray semicolons.  */
22197   else if (token->type == CPP_SEMICOLON)
22198     cp_lexer_consume_token (parser->lexer);
22199   /* Mark methods as optional or required, when building protocols.  */
22200   else if (token->keyword == RID_AT_OPTIONAL)
22201     {
22202       cp_lexer_consume_token (parser->lexer);
22203       objc_set_method_opt (true);
22204     }
22205   else if (token->keyword == RID_AT_REQUIRED)
22206     {
22207       cp_lexer_consume_token (parser->lexer);
22208       objc_set_method_opt (false);
22209     }
22210   else if (token->keyword == RID_NAMESPACE)
22211     cp_parser_namespace_definition (parser);
22212   /* Other stray characters must generate errors.  */
22213   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
22214     {
22215       cp_lexer_consume_token (parser->lexer);
22216       error ("stray %qs between Objective-C++ methods",
22217              token->type == CPP_OPEN_BRACE ? "{" : "}");
22218     }
22219   /* Finally, try to parse a block-declaration, or a function-definition.  */
22220   else
22221     cp_parser_block_declaration (parser, /*statement_p=*/false);
22222 }
22223
22224 /* Parse a method signature.  */
22225
22226 static tree
22227 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
22228 {
22229   tree rettype, kwdparms, optparms;
22230   bool ellipsis = false;
22231   bool is_class_method;
22232
22233   is_class_method = cp_parser_objc_method_type (parser);
22234   rettype = cp_parser_objc_typename (parser);
22235   *attributes = NULL_TREE;
22236   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
22237   if (kwdparms == error_mark_node)
22238     return error_mark_node;
22239   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
22240   if (optparms == error_mark_node)
22241     return error_mark_node;
22242
22243   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
22244 }
22245
22246 static bool
22247 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
22248 {
22249   tree tattr;  
22250   cp_lexer_save_tokens (parser->lexer);
22251   tattr = cp_parser_attributes_opt (parser);
22252   gcc_assert (tattr) ;
22253   
22254   /* If the attributes are followed by a method introducer, this is not allowed.
22255      Dump the attributes and flag the situation.  */
22256   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
22257       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
22258     return true;
22259
22260   /* Otherwise, the attributes introduce some interstitial code, possibly so
22261      rewind to allow that check.  */
22262   cp_lexer_rollback_tokens (parser->lexer);
22263   return false;  
22264 }
22265
22266 /* Parse an Objective-C method prototype list.  */
22267
22268 static void
22269 cp_parser_objc_method_prototype_list (cp_parser* parser)
22270 {
22271   cp_token *token = cp_lexer_peek_token (parser->lexer);
22272
22273   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22274     {
22275       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22276         {
22277           tree attributes, sig;
22278           bool is_class_method;
22279           if (token->type == CPP_PLUS)
22280             is_class_method = true;
22281           else
22282             is_class_method = false;
22283           sig = cp_parser_objc_method_signature (parser, &attributes);
22284           if (sig == error_mark_node)
22285             {
22286               cp_parser_skip_to_end_of_block_or_statement (parser);
22287               token = cp_lexer_peek_token (parser->lexer);
22288               continue;
22289             }
22290           objc_add_method_declaration (is_class_method, sig, attributes);
22291           cp_parser_consume_semicolon_at_end_of_statement (parser);
22292         }
22293       else if (token->keyword == RID_AT_PROPERTY)
22294         cp_parser_objc_at_property_declaration (parser);
22295       else if (token->keyword == RID_ATTRIBUTE 
22296                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22297         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
22298                     OPT_Wattributes, 
22299                     "prefix attributes are ignored for methods");
22300       else
22301         /* Allow for interspersed non-ObjC++ code.  */
22302         cp_parser_objc_interstitial_code (parser);
22303
22304       token = cp_lexer_peek_token (parser->lexer);
22305     }
22306
22307   if (token->type != CPP_EOF)
22308     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22309   else
22310     cp_parser_error (parser, "expected %<@end%>");
22311
22312   objc_finish_interface ();
22313 }
22314
22315 /* Parse an Objective-C method definition list.  */
22316
22317 static void
22318 cp_parser_objc_method_definition_list (cp_parser* parser)
22319 {
22320   cp_token *token = cp_lexer_peek_token (parser->lexer);
22321
22322   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
22323     {
22324       tree meth;
22325
22326       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
22327         {
22328           cp_token *ptk;
22329           tree sig, attribute;
22330           bool is_class_method;
22331           if (token->type == CPP_PLUS)
22332             is_class_method = true;
22333           else
22334             is_class_method = false;
22335           push_deferring_access_checks (dk_deferred);
22336           sig = cp_parser_objc_method_signature (parser, &attribute);
22337           if (sig == error_mark_node)
22338             {
22339               cp_parser_skip_to_end_of_block_or_statement (parser);
22340               token = cp_lexer_peek_token (parser->lexer);
22341               continue;
22342             }
22343           objc_start_method_definition (is_class_method, sig, attribute,
22344                                         NULL_TREE);
22345
22346           /* For historical reasons, we accept an optional semicolon.  */
22347           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22348             cp_lexer_consume_token (parser->lexer);
22349
22350           ptk = cp_lexer_peek_token (parser->lexer);
22351           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
22352                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
22353             {
22354               perform_deferred_access_checks ();
22355               stop_deferring_access_checks ();
22356               meth = cp_parser_function_definition_after_declarator (parser,
22357                                                                      false);
22358               pop_deferring_access_checks ();
22359               objc_finish_method_definition (meth);
22360             }
22361         }
22362       /* The following case will be removed once @synthesize is
22363          completely implemented.  */
22364       else if (token->keyword == RID_AT_PROPERTY)
22365         cp_parser_objc_at_property_declaration (parser);
22366       else if (token->keyword == RID_AT_SYNTHESIZE)
22367         cp_parser_objc_at_synthesize_declaration (parser);
22368       else if (token->keyword == RID_AT_DYNAMIC)
22369         cp_parser_objc_at_dynamic_declaration (parser);
22370       else if (token->keyword == RID_ATTRIBUTE 
22371                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
22372         warning_at (token->location, OPT_Wattributes,
22373                     "prefix attributes are ignored for methods");
22374       else
22375         /* Allow for interspersed non-ObjC++ code.  */
22376         cp_parser_objc_interstitial_code (parser);
22377
22378       token = cp_lexer_peek_token (parser->lexer);
22379     }
22380
22381   if (token->type != CPP_EOF)
22382     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22383   else
22384     cp_parser_error (parser, "expected %<@end%>");
22385
22386   objc_finish_implementation ();
22387 }
22388
22389 /* Parse Objective-C ivars.  */
22390
22391 static void
22392 cp_parser_objc_class_ivars (cp_parser* parser)
22393 {
22394   cp_token *token = cp_lexer_peek_token (parser->lexer);
22395
22396   if (token->type != CPP_OPEN_BRACE)
22397     return;     /* No ivars specified.  */
22398
22399   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
22400   token = cp_lexer_peek_token (parser->lexer);
22401
22402   while (token->type != CPP_CLOSE_BRACE 
22403         && token->keyword != RID_AT_END && token->type != CPP_EOF)
22404     {
22405       cp_decl_specifier_seq declspecs;
22406       int decl_class_or_enum_p;
22407       tree prefix_attributes;
22408
22409       cp_parser_objc_visibility_spec (parser);
22410
22411       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
22412         break;
22413
22414       cp_parser_decl_specifier_seq (parser,
22415                                     CP_PARSER_FLAGS_OPTIONAL,
22416                                     &declspecs,
22417                                     &decl_class_or_enum_p);
22418
22419       /* auto, register, static, extern, mutable.  */
22420       if (declspecs.storage_class != sc_none)
22421         {
22422           cp_parser_error (parser, "invalid type for instance variable");         
22423           declspecs.storage_class = sc_none;
22424         }
22425
22426       /* __thread.  */
22427       if (declspecs.specs[(int) ds_thread])
22428         {
22429           cp_parser_error (parser, "invalid type for instance variable");
22430           declspecs.specs[(int) ds_thread] = 0;
22431         }
22432       
22433       /* typedef.  */
22434       if (declspecs.specs[(int) ds_typedef])
22435         {
22436           cp_parser_error (parser, "invalid type for instance variable");
22437           declspecs.specs[(int) ds_typedef] = 0;
22438         }
22439
22440       prefix_attributes = declspecs.attributes;
22441       declspecs.attributes = NULL_TREE;
22442
22443       /* Keep going until we hit the `;' at the end of the
22444          declaration.  */
22445       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22446         {
22447           tree width = NULL_TREE, attributes, first_attribute, decl;
22448           cp_declarator *declarator = NULL;
22449           int ctor_dtor_or_conv_p;
22450
22451           /* Check for a (possibly unnamed) bitfield declaration.  */
22452           token = cp_lexer_peek_token (parser->lexer);
22453           if (token->type == CPP_COLON)
22454             goto eat_colon;
22455
22456           if (token->type == CPP_NAME
22457               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
22458                   == CPP_COLON))
22459             {
22460               /* Get the name of the bitfield.  */
22461               declarator = make_id_declarator (NULL_TREE,
22462                                                cp_parser_identifier (parser),
22463                                                sfk_none);
22464
22465              eat_colon:
22466               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22467               /* Get the width of the bitfield.  */
22468               width
22469                 = cp_parser_constant_expression (parser,
22470                                                  /*allow_non_constant=*/false,
22471                                                  NULL);
22472             }
22473           else
22474             {
22475               /* Parse the declarator.  */
22476               declarator
22477                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22478                                         &ctor_dtor_or_conv_p,
22479                                         /*parenthesized_p=*/NULL,
22480                                         /*member_p=*/false);
22481             }
22482
22483           /* Look for attributes that apply to the ivar.  */
22484           attributes = cp_parser_attributes_opt (parser);
22485           /* Remember which attributes are prefix attributes and
22486              which are not.  */
22487           first_attribute = attributes;
22488           /* Combine the attributes.  */
22489           attributes = chainon (prefix_attributes, attributes);
22490
22491           if (width)
22492               /* Create the bitfield declaration.  */
22493               decl = grokbitfield (declarator, &declspecs,
22494                                    width,
22495                                    attributes);
22496           else
22497             decl = grokfield (declarator, &declspecs,
22498                               NULL_TREE, /*init_const_expr_p=*/false,
22499                               NULL_TREE, attributes);
22500
22501           /* Add the instance variable.  */
22502           if (decl != error_mark_node && decl != NULL_TREE)
22503             objc_add_instance_variable (decl);
22504
22505           /* Reset PREFIX_ATTRIBUTES.  */
22506           while (attributes && TREE_CHAIN (attributes) != first_attribute)
22507             attributes = TREE_CHAIN (attributes);
22508           if (attributes)
22509             TREE_CHAIN (attributes) = NULL_TREE;
22510
22511           token = cp_lexer_peek_token (parser->lexer);
22512
22513           if (token->type == CPP_COMMA)
22514             {
22515               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22516               continue;
22517             }
22518           break;
22519         }
22520
22521       cp_parser_consume_semicolon_at_end_of_statement (parser);
22522       token = cp_lexer_peek_token (parser->lexer);
22523     }
22524
22525   if (token->keyword == RID_AT_END)
22526     cp_parser_error (parser, "expected %<}%>");
22527
22528   /* Do not consume the RID_AT_END, so it will be read again as terminating
22529      the @interface of @implementation.  */ 
22530   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
22531     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
22532     
22533   /* For historical reasons, we accept an optional semicolon.  */
22534   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
22535     cp_lexer_consume_token (parser->lexer);
22536 }
22537
22538 /* Parse an Objective-C protocol declaration.  */
22539
22540 static void
22541 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
22542 {
22543   tree proto, protorefs;
22544   cp_token *tok;
22545
22546   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22547   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
22548     {
22549       tok = cp_lexer_peek_token (parser->lexer);
22550       error_at (tok->location, "identifier expected after %<@protocol%>");
22551       cp_parser_consume_semicolon_at_end_of_statement (parser);
22552       return;
22553     }
22554
22555   /* See if we have a forward declaration or a definition.  */
22556   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
22557
22558   /* Try a forward declaration first.  */
22559   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
22560     {
22561       while (true)
22562         {
22563           tree id;
22564           
22565           id = cp_parser_identifier (parser);
22566           if (id == error_mark_node)
22567             break;
22568           
22569           objc_declare_protocol (id, attributes);
22570           
22571           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22572             cp_lexer_consume_token (parser->lexer);
22573           else
22574             break;
22575         }
22576       cp_parser_consume_semicolon_at_end_of_statement (parser);
22577     }
22578
22579   /* Ok, we got a full-fledged definition (or at least should).  */
22580   else
22581     {
22582       proto = cp_parser_identifier (parser);
22583       protorefs = cp_parser_objc_protocol_refs_opt (parser);
22584       objc_start_protocol (proto, protorefs, attributes);
22585       cp_parser_objc_method_prototype_list (parser);
22586     }
22587 }
22588
22589 /* Parse an Objective-C superclass or category.  */
22590
22591 static void
22592 cp_parser_objc_superclass_or_category (cp_parser *parser, 
22593                                        bool iface_p,
22594                                        tree *super,
22595                                        tree *categ, bool *is_class_extension)
22596 {
22597   cp_token *next = cp_lexer_peek_token (parser->lexer);
22598
22599   *super = *categ = NULL_TREE;
22600   *is_class_extension = false;
22601   if (next->type == CPP_COLON)
22602     {
22603       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
22604       *super = cp_parser_identifier (parser);
22605     }
22606   else if (next->type == CPP_OPEN_PAREN)
22607     {
22608       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
22609
22610       /* If there is no category name, and this is an @interface, we
22611          have a class extension.  */
22612       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22613         {
22614           *categ = NULL_TREE;
22615           *is_class_extension = true;
22616         }
22617       else
22618         *categ = cp_parser_identifier (parser);
22619
22620       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22621     }
22622 }
22623
22624 /* Parse an Objective-C class interface.  */
22625
22626 static void
22627 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
22628 {
22629   tree name, super, categ, protos;
22630   bool is_class_extension;
22631
22632   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
22633   name = cp_parser_identifier (parser);
22634   if (name == error_mark_node)
22635     {
22636       /* It's hard to recover because even if valid @interface stuff
22637          is to follow, we can't compile it (or validate it) if we
22638          don't even know which class it refers to.  Let's assume this
22639          was a stray '@interface' token in the stream and skip it.
22640       */
22641       return;
22642     }
22643   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
22644                                          &is_class_extension);
22645   protos = cp_parser_objc_protocol_refs_opt (parser);
22646
22647   /* We have either a class or a category on our hands.  */
22648   if (categ || is_class_extension)
22649     objc_start_category_interface (name, categ, protos, attributes);
22650   else
22651     {
22652       objc_start_class_interface (name, super, protos, attributes);
22653       /* Handle instance variable declarations, if any.  */
22654       cp_parser_objc_class_ivars (parser);
22655       objc_continue_interface ();
22656     }
22657
22658   cp_parser_objc_method_prototype_list (parser);
22659 }
22660
22661 /* Parse an Objective-C class implementation.  */
22662
22663 static void
22664 cp_parser_objc_class_implementation (cp_parser* parser)
22665 {
22666   tree name, super, categ;
22667   bool is_class_extension;
22668
22669   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
22670   name = cp_parser_identifier (parser);
22671   if (name == error_mark_node)
22672     {
22673       /* It's hard to recover because even if valid @implementation
22674          stuff is to follow, we can't compile it (or validate it) if
22675          we don't even know which class it refers to.  Let's assume
22676          this was a stray '@implementation' token in the stream and
22677          skip it.
22678       */
22679       return;
22680     }
22681   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
22682                                          &is_class_extension);
22683
22684   /* We have either a class or a category on our hands.  */
22685   if (categ)
22686     objc_start_category_implementation (name, categ);
22687   else
22688     {
22689       objc_start_class_implementation (name, super);
22690       /* Handle instance variable declarations, if any.  */
22691       cp_parser_objc_class_ivars (parser);
22692       objc_continue_implementation ();
22693     }
22694
22695   cp_parser_objc_method_definition_list (parser);
22696 }
22697
22698 /* Consume the @end token and finish off the implementation.  */
22699
22700 static void
22701 cp_parser_objc_end_implementation (cp_parser* parser)
22702 {
22703   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
22704   objc_finish_implementation ();
22705 }
22706
22707 /* Parse an Objective-C declaration.  */
22708
22709 static void
22710 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
22711 {
22712   /* Try to figure out what kind of declaration is present.  */
22713   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22714
22715   if (attributes)
22716     switch (kwd->keyword)
22717       {
22718         case RID_AT_ALIAS:
22719         case RID_AT_CLASS:
22720         case RID_AT_END:
22721           error_at (kwd->location, "attributes may not be specified before"
22722                     " the %<@%D%> Objective-C++ keyword",
22723                     kwd->u.value);
22724           attributes = NULL;
22725           break;
22726         case RID_AT_IMPLEMENTATION:
22727           warning_at (kwd->location, OPT_Wattributes,
22728                       "prefix attributes are ignored before %<@%D%>",
22729                       kwd->u.value);
22730           attributes = NULL;
22731         default:
22732           break;
22733       }
22734
22735   switch (kwd->keyword)
22736     {
22737     case RID_AT_ALIAS:
22738       cp_parser_objc_alias_declaration (parser);
22739       break;
22740     case RID_AT_CLASS:
22741       cp_parser_objc_class_declaration (parser);
22742       break;
22743     case RID_AT_PROTOCOL:
22744       cp_parser_objc_protocol_declaration (parser, attributes);
22745       break;
22746     case RID_AT_INTERFACE:
22747       cp_parser_objc_class_interface (parser, attributes);
22748       break;
22749     case RID_AT_IMPLEMENTATION:
22750       cp_parser_objc_class_implementation (parser);
22751       break;
22752     case RID_AT_END:
22753       cp_parser_objc_end_implementation (parser);
22754       break;
22755     default:
22756       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22757                 kwd->u.value);
22758       cp_parser_skip_to_end_of_block_or_statement (parser);
22759     }
22760 }
22761
22762 /* Parse an Objective-C try-catch-finally statement.
22763
22764    objc-try-catch-finally-stmt:
22765      @try compound-statement objc-catch-clause-seq [opt]
22766        objc-finally-clause [opt]
22767
22768    objc-catch-clause-seq:
22769      objc-catch-clause objc-catch-clause-seq [opt]
22770
22771    objc-catch-clause:
22772      @catch ( objc-exception-declaration ) compound-statement
22773
22774    objc-finally-clause:
22775      @finally compound-statement
22776
22777    objc-exception-declaration:
22778      parameter-declaration
22779      '...'
22780
22781    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
22782
22783    Returns NULL_TREE.
22784
22785    PS: This function is identical to c_parser_objc_try_catch_finally_statement
22786    for C.  Keep them in sync.  */   
22787
22788 static tree
22789 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
22790 {
22791   location_t location;
22792   tree stmt;
22793
22794   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
22795   location = cp_lexer_peek_token (parser->lexer)->location;
22796   objc_maybe_warn_exceptions (location);
22797   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
22798      node, lest it get absorbed into the surrounding block.  */
22799   stmt = push_stmt_list ();
22800   cp_parser_compound_statement (parser, NULL, false, false);
22801   objc_begin_try_stmt (location, pop_stmt_list (stmt));
22802
22803   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
22804     {
22805       cp_parameter_declarator *parm;
22806       tree parameter_declaration = error_mark_node;
22807       bool seen_open_paren = false;
22808
22809       cp_lexer_consume_token (parser->lexer);
22810       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
22811         seen_open_paren = true;
22812       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22813         {
22814           /* We have "@catch (...)" (where the '...' are literally
22815              what is in the code).  Skip the '...'.
22816              parameter_declaration is set to NULL_TREE, and
22817              objc_being_catch_clauses() knows that that means
22818              '...'.  */
22819           cp_lexer_consume_token (parser->lexer);
22820           parameter_declaration = NULL_TREE;
22821         }
22822       else
22823         {
22824           /* We have "@catch (NSException *exception)" or something
22825              like that.  Parse the parameter declaration.  */
22826           parm = cp_parser_parameter_declaration (parser, false, NULL);
22827           if (parm == NULL)
22828             parameter_declaration = error_mark_node;
22829           else
22830             parameter_declaration = grokdeclarator (parm->declarator,
22831                                                     &parm->decl_specifiers,
22832                                                     PARM, /*initialized=*/0,
22833                                                     /*attrlist=*/NULL);
22834         }
22835       if (seen_open_paren)
22836         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22837       else
22838         {
22839           /* If there was no open parenthesis, we are recovering from
22840              an error, and we are trying to figure out what mistake
22841              the user has made.  */
22842
22843           /* If there is an immediate closing parenthesis, the user
22844              probably forgot the opening one (ie, they typed "@catch
22845              NSException *e)".  Parse the closing parenthesis and keep
22846              going.  */
22847           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
22848             cp_lexer_consume_token (parser->lexer);
22849           
22850           /* If these is no immediate closing parenthesis, the user
22851              probably doesn't know that parenthesis are required at
22852              all (ie, they typed "@catch NSException *e").  So, just
22853              forget about the closing parenthesis and keep going.  */
22854         }
22855       objc_begin_catch_clause (parameter_declaration);
22856       cp_parser_compound_statement (parser, NULL, false, false);
22857       objc_finish_catch_clause ();
22858     }
22859   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
22860     {
22861       cp_lexer_consume_token (parser->lexer);
22862       location = cp_lexer_peek_token (parser->lexer)->location;
22863       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
22864          node, lest it get absorbed into the surrounding block.  */
22865       stmt = push_stmt_list ();
22866       cp_parser_compound_statement (parser, NULL, false, false);
22867       objc_build_finally_clause (location, pop_stmt_list (stmt));
22868     }
22869
22870   return objc_finish_try_stmt ();
22871 }
22872
22873 /* Parse an Objective-C synchronized statement.
22874
22875    objc-synchronized-stmt:
22876      @synchronized ( expression ) compound-statement
22877
22878    Returns NULL_TREE.  */
22879
22880 static tree
22881 cp_parser_objc_synchronized_statement (cp_parser *parser)
22882 {
22883   location_t location;
22884   tree lock, stmt;
22885
22886   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
22887
22888   location = cp_lexer_peek_token (parser->lexer)->location;
22889   objc_maybe_warn_exceptions (location);
22890   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22891   lock = cp_parser_expression (parser, false, NULL);
22892   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22893
22894   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
22895      node, lest it get absorbed into the surrounding block.  */
22896   stmt = push_stmt_list ();
22897   cp_parser_compound_statement (parser, NULL, false, false);
22898
22899   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
22900 }
22901
22902 /* Parse an Objective-C throw statement.
22903
22904    objc-throw-stmt:
22905      @throw assignment-expression [opt] ;
22906
22907    Returns a constructed '@throw' statement.  */
22908
22909 static tree
22910 cp_parser_objc_throw_statement (cp_parser *parser)
22911 {
22912   tree expr = NULL_TREE;
22913   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22914
22915   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
22916
22917   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
22918     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
22919
22920   cp_parser_consume_semicolon_at_end_of_statement (parser);
22921
22922   return objc_build_throw_stmt (loc, expr);
22923 }
22924
22925 /* Parse an Objective-C statement.  */
22926
22927 static tree
22928 cp_parser_objc_statement (cp_parser * parser)
22929 {
22930   /* Try to figure out what kind of declaration is present.  */
22931   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22932
22933   switch (kwd->keyword)
22934     {
22935     case RID_AT_TRY:
22936       return cp_parser_objc_try_catch_finally_statement (parser);
22937     case RID_AT_SYNCHRONIZED:
22938       return cp_parser_objc_synchronized_statement (parser);
22939     case RID_AT_THROW:
22940       return cp_parser_objc_throw_statement (parser);
22941     default:
22942       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
22943                kwd->u.value);
22944       cp_parser_skip_to_end_of_block_or_statement (parser);
22945     }
22946
22947   return error_mark_node;
22948 }
22949
22950 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
22951    look ahead to see if an objc keyword follows the attributes.  This
22952    is to detect the use of prefix attributes on ObjC @interface and 
22953    @protocol.  */
22954
22955 static bool
22956 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
22957 {
22958   cp_lexer_save_tokens (parser->lexer);
22959   *attrib = cp_parser_attributes_opt (parser);
22960   gcc_assert (*attrib);
22961   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
22962     {
22963       cp_lexer_commit_tokens (parser->lexer);
22964       return true;
22965     }
22966   cp_lexer_rollback_tokens (parser->lexer);
22967   return false;  
22968 }
22969
22970 /* This routine is a minimal replacement for
22971    c_parser_struct_declaration () used when parsing the list of
22972    types/names or ObjC++ properties.  For example, when parsing the
22973    code
22974
22975    @property (readonly) int a, b, c;
22976
22977    this function is responsible for parsing "int a, int b, int c" and
22978    returning the declarations as CHAIN of DECLs.
22979
22980    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
22981    similar parsing.  */
22982 static tree
22983 cp_parser_objc_struct_declaration (cp_parser *parser)
22984 {
22985   tree decls = NULL_TREE;
22986   cp_decl_specifier_seq declspecs;
22987   int decl_class_or_enum_p;
22988   tree prefix_attributes;
22989
22990   cp_parser_decl_specifier_seq (parser,
22991                                 CP_PARSER_FLAGS_NONE,
22992                                 &declspecs,
22993                                 &decl_class_or_enum_p);
22994
22995   if (declspecs.type == error_mark_node)
22996     return error_mark_node;
22997
22998   /* auto, register, static, extern, mutable.  */
22999   if (declspecs.storage_class != sc_none)
23000     {
23001       cp_parser_error (parser, "invalid type for property");
23002       declspecs.storage_class = sc_none;
23003     }
23004   
23005   /* __thread.  */
23006   if (declspecs.specs[(int) ds_thread])
23007     {
23008       cp_parser_error (parser, "invalid type for property");
23009       declspecs.specs[(int) ds_thread] = 0;
23010     }
23011   
23012   /* typedef.  */
23013   if (declspecs.specs[(int) ds_typedef])
23014     {
23015       cp_parser_error (parser, "invalid type for property");
23016       declspecs.specs[(int) ds_typedef] = 0;
23017     }
23018
23019   prefix_attributes = declspecs.attributes;
23020   declspecs.attributes = NULL_TREE;
23021
23022   /* Keep going until we hit the `;' at the end of the declaration. */
23023   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23024     {
23025       tree attributes, first_attribute, decl;
23026       cp_declarator *declarator;
23027       cp_token *token;
23028
23029       /* Parse the declarator.  */
23030       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23031                                          NULL, NULL, false);
23032
23033       /* Look for attributes that apply to the ivar.  */
23034       attributes = cp_parser_attributes_opt (parser);
23035       /* Remember which attributes are prefix attributes and
23036          which are not.  */
23037       first_attribute = attributes;
23038       /* Combine the attributes.  */
23039       attributes = chainon (prefix_attributes, attributes);
23040       
23041       decl = grokfield (declarator, &declspecs,
23042                         NULL_TREE, /*init_const_expr_p=*/false,
23043                         NULL_TREE, attributes);
23044
23045       if (decl == error_mark_node || decl == NULL_TREE)
23046         return error_mark_node;
23047       
23048       /* Reset PREFIX_ATTRIBUTES.  */
23049       while (attributes && TREE_CHAIN (attributes) != first_attribute)
23050         attributes = TREE_CHAIN (attributes);
23051       if (attributes)
23052         TREE_CHAIN (attributes) = NULL_TREE;
23053
23054       DECL_CHAIN (decl) = decls;
23055       decls = decl;
23056
23057       token = cp_lexer_peek_token (parser->lexer);
23058       if (token->type == CPP_COMMA)
23059         {
23060           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23061           continue;
23062         }
23063       else
23064         break;
23065     }
23066   return decls;
23067 }
23068
23069 /* Parse an Objective-C @property declaration.  The syntax is:
23070
23071    objc-property-declaration:
23072      '@property' objc-property-attributes[opt] struct-declaration ;
23073
23074    objc-property-attributes:
23075     '(' objc-property-attribute-list ')'
23076
23077    objc-property-attribute-list:
23078      objc-property-attribute
23079      objc-property-attribute-list, objc-property-attribute
23080
23081    objc-property-attribute
23082      'getter' = identifier
23083      'setter' = identifier
23084      'readonly'
23085      'readwrite'
23086      'assign'
23087      'retain'
23088      'copy'
23089      'nonatomic'
23090
23091   For example:
23092     @property NSString *name;
23093     @property (readonly) id object;
23094     @property (retain, nonatomic, getter=getTheName) id name;
23095     @property int a, b, c;
23096
23097    PS: This function is identical to
23098    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
23099 static void 
23100 cp_parser_objc_at_property_declaration (cp_parser *parser)
23101 {
23102   /* The following variables hold the attributes of the properties as
23103      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
23104      seen.  When we see an attribute, we set them to 'true' (if they
23105      are boolean properties) or to the identifier (if they have an
23106      argument, ie, for getter and setter).  Note that here we only
23107      parse the list of attributes, check the syntax and accumulate the
23108      attributes that we find.  objc_add_property_declaration() will
23109      then process the information.  */
23110   bool property_assign = false;
23111   bool property_copy = false;
23112   tree property_getter_ident = NULL_TREE;
23113   bool property_nonatomic = false;
23114   bool property_readonly = false;
23115   bool property_readwrite = false;
23116   bool property_retain = false;
23117   tree property_setter_ident = NULL_TREE;
23118
23119   /* 'properties' is the list of properties that we read.  Usually a
23120      single one, but maybe more (eg, in "@property int a, b, c;" there
23121      are three).  */
23122   tree properties;
23123   location_t loc;
23124
23125   loc = cp_lexer_peek_token (parser->lexer)->location;
23126
23127   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
23128
23129   /* Parse the optional attribute list...  */
23130   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23131     {
23132       /* Eat the '('.  */
23133       cp_lexer_consume_token (parser->lexer);
23134
23135       while (true)
23136         {
23137           bool syntax_error = false;
23138           cp_token *token = cp_lexer_peek_token (parser->lexer);
23139           enum rid keyword;
23140
23141           if (token->type != CPP_NAME)
23142             {
23143               cp_parser_error (parser, "expected identifier");
23144               break;
23145             }
23146           keyword = C_RID_CODE (token->u.value);
23147           cp_lexer_consume_token (parser->lexer);
23148           switch (keyword)
23149             {
23150             case RID_ASSIGN:    property_assign = true;    break;
23151             case RID_COPY:      property_copy = true;      break;
23152             case RID_NONATOMIC: property_nonatomic = true; break;
23153             case RID_READONLY:  property_readonly = true;  break;
23154             case RID_READWRITE: property_readwrite = true; break;
23155             case RID_RETAIN:    property_retain = true;    break;
23156
23157             case RID_GETTER:
23158             case RID_SETTER:
23159               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
23160                 {
23161                   if (keyword == RID_GETTER)
23162                     cp_parser_error (parser,
23163                                      "missing %<=%> (after %<getter%> attribute)");
23164                   else
23165                     cp_parser_error (parser,
23166                                      "missing %<=%> (after %<setter%> attribute)");
23167                   syntax_error = true;
23168                   break;
23169                 }
23170               cp_lexer_consume_token (parser->lexer); /* eat the = */
23171               if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23172                 {
23173                   cp_parser_error (parser, "expected identifier");
23174                   syntax_error = true;
23175                   break;
23176                 }
23177               if (keyword == RID_SETTER)
23178                 {
23179                   if (property_setter_ident != NULL_TREE)
23180                     cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
23181                   else
23182                     property_setter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23183                   cp_lexer_consume_token (parser->lexer);
23184                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23185                     cp_parser_error (parser, "setter name must terminate with %<:%>");
23186                   else
23187                     cp_lexer_consume_token (parser->lexer);
23188                 }
23189               else
23190                 {
23191                   if (property_getter_ident != NULL_TREE)
23192                     cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
23193                   else
23194                     property_getter_ident = cp_lexer_peek_token (parser->lexer)->u.value;
23195                   cp_lexer_consume_token (parser->lexer);
23196                 }
23197               break;
23198             default:
23199               cp_parser_error (parser, "unknown property attribute");
23200               syntax_error = true;
23201               break;
23202             }
23203
23204           if (syntax_error)
23205             break;
23206
23207           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23208             cp_lexer_consume_token (parser->lexer);
23209           else
23210             break;
23211         }
23212
23213       /* FIXME: "@property (setter, assign);" will generate a spurious
23214          "error: expected â€˜)’ before â€˜,’ token".  This is because
23215          cp_parser_require, unlike the C counterpart, will produce an
23216          error even if we are in error recovery.  */
23217       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23218         {
23219           cp_parser_skip_to_closing_parenthesis (parser,
23220                                                  /*recovering=*/true,
23221                                                  /*or_comma=*/false,
23222                                                  /*consume_paren=*/true);
23223         }
23224     }
23225
23226   /* ... and the property declaration(s).  */
23227   properties = cp_parser_objc_struct_declaration (parser);
23228
23229   if (properties == error_mark_node)
23230     {
23231       cp_parser_skip_to_end_of_statement (parser);
23232       /* If the next token is now a `;', consume it.  */
23233       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23234         cp_lexer_consume_token (parser->lexer);
23235       return;
23236     }
23237
23238   if (properties == NULL_TREE)
23239     cp_parser_error (parser, "expected identifier");
23240   else
23241     {
23242       /* Comma-separated properties are chained together in
23243          reverse order; add them one by one.  */
23244       properties = nreverse (properties);
23245       
23246       for (; properties; properties = TREE_CHAIN (properties))
23247         objc_add_property_declaration (loc, copy_node (properties),
23248                                        property_readonly, property_readwrite,
23249                                        property_assign, property_retain,
23250                                        property_copy, property_nonatomic,
23251                                        property_getter_ident, property_setter_ident);
23252     }
23253   
23254   cp_parser_consume_semicolon_at_end_of_statement (parser);
23255 }
23256
23257 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
23258
23259    objc-synthesize-declaration:
23260      @synthesize objc-synthesize-identifier-list ;
23261
23262    objc-synthesize-identifier-list:
23263      objc-synthesize-identifier
23264      objc-synthesize-identifier-list, objc-synthesize-identifier
23265
23266    objc-synthesize-identifier
23267      identifier
23268      identifier = identifier
23269
23270   For example:
23271     @synthesize MyProperty;
23272     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
23273
23274   PS: This function is identical to c_parser_objc_at_synthesize_declaration
23275   for C.  Keep them in sync.
23276 */
23277 static void 
23278 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
23279 {
23280   tree list = NULL_TREE;
23281   location_t loc;
23282   loc = cp_lexer_peek_token (parser->lexer)->location;
23283
23284   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
23285   while (true)
23286     {
23287       tree property, ivar;
23288       property = cp_parser_identifier (parser);
23289       if (property == error_mark_node)
23290         {
23291           cp_parser_consume_semicolon_at_end_of_statement (parser);
23292           return;
23293         }
23294       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
23295         {
23296           cp_lexer_consume_token (parser->lexer);
23297           ivar = cp_parser_identifier (parser);
23298           if (ivar == error_mark_node)
23299             {
23300               cp_parser_consume_semicolon_at_end_of_statement (parser);
23301               return;
23302             }
23303         }
23304       else
23305         ivar = NULL_TREE;
23306       list = chainon (list, build_tree_list (ivar, property));
23307       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23308         cp_lexer_consume_token (parser->lexer);
23309       else
23310         break;
23311     }
23312   cp_parser_consume_semicolon_at_end_of_statement (parser);
23313   objc_add_synthesize_declaration (loc, list);
23314 }
23315
23316 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
23317
23318    objc-dynamic-declaration:
23319      @dynamic identifier-list ;
23320
23321    For example:
23322      @dynamic MyProperty;
23323      @dynamic MyProperty, AnotherProperty;
23324
23325   PS: This function is identical to c_parser_objc_at_dynamic_declaration
23326   for C.  Keep them in sync.
23327 */
23328 static void 
23329 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
23330 {
23331   tree list = NULL_TREE;
23332   location_t loc;
23333   loc = cp_lexer_peek_token (parser->lexer)->location;
23334
23335   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
23336   while (true)
23337     {
23338       tree property;
23339       property = cp_parser_identifier (parser);
23340       if (property == error_mark_node)
23341         {
23342           cp_parser_consume_semicolon_at_end_of_statement (parser);
23343           return;
23344         }
23345       list = chainon (list, build_tree_list (NULL, property));
23346       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23347         cp_lexer_consume_token (parser->lexer);
23348       else
23349         break;
23350     }
23351   cp_parser_consume_semicolon_at_end_of_statement (parser);
23352   objc_add_dynamic_declaration (loc, list);
23353 }
23354
23355 \f
23356 /* OpenMP 2.5 parsing routines.  */
23357
23358 /* Returns name of the next clause.
23359    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
23360    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
23361    returned and the token is consumed.  */
23362
23363 static pragma_omp_clause
23364 cp_parser_omp_clause_name (cp_parser *parser)
23365 {
23366   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
23367
23368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
23369     result = PRAGMA_OMP_CLAUSE_IF;
23370   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
23371     result = PRAGMA_OMP_CLAUSE_DEFAULT;
23372   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
23373     result = PRAGMA_OMP_CLAUSE_PRIVATE;
23374   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23375     {
23376       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23377       const char *p = IDENTIFIER_POINTER (id);
23378
23379       switch (p[0])
23380         {
23381         case 'c':
23382           if (!strcmp ("collapse", p))
23383             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
23384           else if (!strcmp ("copyin", p))
23385             result = PRAGMA_OMP_CLAUSE_COPYIN;
23386           else if (!strcmp ("copyprivate", p))
23387             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
23388           break;
23389         case 'f':
23390           if (!strcmp ("firstprivate", p))
23391             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
23392           break;
23393         case 'l':
23394           if (!strcmp ("lastprivate", p))
23395             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
23396           break;
23397         case 'n':
23398           if (!strcmp ("nowait", p))
23399             result = PRAGMA_OMP_CLAUSE_NOWAIT;
23400           else if (!strcmp ("num_threads", p))
23401             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
23402           break;
23403         case 'o':
23404           if (!strcmp ("ordered", p))
23405             result = PRAGMA_OMP_CLAUSE_ORDERED;
23406           break;
23407         case 'r':
23408           if (!strcmp ("reduction", p))
23409             result = PRAGMA_OMP_CLAUSE_REDUCTION;
23410           break;
23411         case 's':
23412           if (!strcmp ("schedule", p))
23413             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
23414           else if (!strcmp ("shared", p))
23415             result = PRAGMA_OMP_CLAUSE_SHARED;
23416           break;
23417         case 'u':
23418           if (!strcmp ("untied", p))
23419             result = PRAGMA_OMP_CLAUSE_UNTIED;
23420           break;
23421         }
23422     }
23423
23424   if (result != PRAGMA_OMP_CLAUSE_NONE)
23425     cp_lexer_consume_token (parser->lexer);
23426
23427   return result;
23428 }
23429
23430 /* Validate that a clause of the given type does not already exist.  */
23431
23432 static void
23433 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
23434                            const char *name, location_t location)
23435 {
23436   tree c;
23437
23438   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
23439     if (OMP_CLAUSE_CODE (c) == code)
23440       {
23441         error_at (location, "too many %qs clauses", name);
23442         break;
23443       }
23444 }
23445
23446 /* OpenMP 2.5:
23447    variable-list:
23448      identifier
23449      variable-list , identifier
23450
23451    In addition, we match a closing parenthesis.  An opening parenthesis
23452    will have been consumed by the caller.
23453
23454    If KIND is nonzero, create the appropriate node and install the decl
23455    in OMP_CLAUSE_DECL and add the node to the head of the list.
23456
23457    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
23458    return the list created.  */
23459
23460 static tree
23461 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
23462                                 tree list)
23463 {
23464   cp_token *token;
23465   while (1)
23466     {
23467       tree name, decl;
23468
23469       token = cp_lexer_peek_token (parser->lexer);
23470       name = cp_parser_id_expression (parser, /*template_p=*/false,
23471                                       /*check_dependency_p=*/true,
23472                                       /*template_p=*/NULL,
23473                                       /*declarator_p=*/false,
23474                                       /*optional_p=*/false);
23475       if (name == error_mark_node)
23476         goto skip_comma;
23477
23478       decl = cp_parser_lookup_name_simple (parser, name, token->location);
23479       if (decl == error_mark_node)
23480         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
23481                                      token->location);
23482       else if (kind != 0)
23483         {
23484           tree u = build_omp_clause (token->location, kind);
23485           OMP_CLAUSE_DECL (u) = decl;
23486           OMP_CLAUSE_CHAIN (u) = list;
23487           list = u;
23488         }
23489       else
23490         list = tree_cons (decl, NULL_TREE, list);
23491
23492     get_comma:
23493       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
23494         break;
23495       cp_lexer_consume_token (parser->lexer);
23496     }
23497
23498   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23499     {
23500       int ending;
23501
23502       /* Try to resync to an unnested comma.  Copied from
23503          cp_parser_parenthesized_expression_list.  */
23504     skip_comma:
23505       ending = cp_parser_skip_to_closing_parenthesis (parser,
23506                                                       /*recovering=*/true,
23507                                                       /*or_comma=*/true,
23508                                                       /*consume_paren=*/true);
23509       if (ending < 0)
23510         goto get_comma;
23511     }
23512
23513   return list;
23514 }
23515
23516 /* Similarly, but expect leading and trailing parenthesis.  This is a very
23517    common case for omp clauses.  */
23518
23519 static tree
23520 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
23521 {
23522   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23523     return cp_parser_omp_var_list_no_open (parser, kind, list);
23524   return list;
23525 }
23526
23527 /* OpenMP 3.0:
23528    collapse ( constant-expression ) */
23529
23530 static tree
23531 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
23532 {
23533   tree c, num;
23534   location_t loc;
23535   HOST_WIDE_INT n;
23536
23537   loc = cp_lexer_peek_token (parser->lexer)->location;
23538   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23539     return list;
23540
23541   num = cp_parser_constant_expression (parser, false, NULL);
23542
23543   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23544     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23545                                            /*or_comma=*/false,
23546                                            /*consume_paren=*/true);
23547
23548   if (num == error_mark_node)
23549     return list;
23550   num = fold_non_dependent_expr (num);
23551   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
23552       || !host_integerp (num, 0)
23553       || (n = tree_low_cst (num, 0)) <= 0
23554       || (int) n != n)
23555     {
23556       error_at (loc, "collapse argument needs positive constant integer expression");
23557       return list;
23558     }
23559
23560   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
23561   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
23562   OMP_CLAUSE_CHAIN (c) = list;
23563   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
23564
23565   return c;
23566 }
23567
23568 /* OpenMP 2.5:
23569    default ( shared | none ) */
23570
23571 static tree
23572 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
23573 {
23574   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
23575   tree c;
23576
23577   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23578     return list;
23579   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23580     {
23581       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23582       const char *p = IDENTIFIER_POINTER (id);
23583
23584       switch (p[0])
23585         {
23586         case 'n':
23587           if (strcmp ("none", p) != 0)
23588             goto invalid_kind;
23589           kind = OMP_CLAUSE_DEFAULT_NONE;
23590           break;
23591
23592         case 's':
23593           if (strcmp ("shared", p) != 0)
23594             goto invalid_kind;
23595           kind = OMP_CLAUSE_DEFAULT_SHARED;
23596           break;
23597
23598         default:
23599           goto invalid_kind;
23600         }
23601
23602       cp_lexer_consume_token (parser->lexer);
23603     }
23604   else
23605     {
23606     invalid_kind:
23607       cp_parser_error (parser, "expected %<none%> or %<shared%>");
23608     }
23609
23610   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23611     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23612                                            /*or_comma=*/false,
23613                                            /*consume_paren=*/true);
23614
23615   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
23616     return list;
23617
23618   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
23619   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
23620   OMP_CLAUSE_CHAIN (c) = list;
23621   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
23622
23623   return c;
23624 }
23625
23626 /* OpenMP 2.5:
23627    if ( expression ) */
23628
23629 static tree
23630 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
23631 {
23632   tree t, c;
23633
23634   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23635     return list;
23636
23637   t = cp_parser_condition (parser);
23638
23639   if (t == error_mark_node
23640       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23641     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23642                                            /*or_comma=*/false,
23643                                            /*consume_paren=*/true);
23644
23645   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
23646
23647   c = build_omp_clause (location, OMP_CLAUSE_IF);
23648   OMP_CLAUSE_IF_EXPR (c) = t;
23649   OMP_CLAUSE_CHAIN (c) = list;
23650
23651   return c;
23652 }
23653
23654 /* OpenMP 2.5:
23655    nowait */
23656
23657 static tree
23658 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
23659                              tree list, location_t location)
23660 {
23661   tree c;
23662
23663   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
23664
23665   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
23666   OMP_CLAUSE_CHAIN (c) = list;
23667   return c;
23668 }
23669
23670 /* OpenMP 2.5:
23671    num_threads ( expression ) */
23672
23673 static tree
23674 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
23675                                   location_t location)
23676 {
23677   tree t, c;
23678
23679   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23680     return list;
23681
23682   t = cp_parser_expression (parser, false, NULL);
23683
23684   if (t == error_mark_node
23685       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23686     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23687                                            /*or_comma=*/false,
23688                                            /*consume_paren=*/true);
23689
23690   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
23691                              "num_threads", location);
23692
23693   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
23694   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
23695   OMP_CLAUSE_CHAIN (c) = list;
23696
23697   return c;
23698 }
23699
23700 /* OpenMP 2.5:
23701    ordered */
23702
23703 static tree
23704 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
23705                               tree list, location_t location)
23706 {
23707   tree c;
23708
23709   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
23710                              "ordered", location);
23711
23712   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
23713   OMP_CLAUSE_CHAIN (c) = list;
23714   return c;
23715 }
23716
23717 /* OpenMP 2.5:
23718    reduction ( reduction-operator : variable-list )
23719
23720    reduction-operator:
23721      One of: + * - & ^ | && || */
23722
23723 static tree
23724 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
23725 {
23726   enum tree_code code;
23727   tree nlist, c;
23728
23729   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23730     return list;
23731
23732   switch (cp_lexer_peek_token (parser->lexer)->type)
23733     {
23734     case CPP_PLUS:
23735       code = PLUS_EXPR;
23736       break;
23737     case CPP_MULT:
23738       code = MULT_EXPR;
23739       break;
23740     case CPP_MINUS:
23741       code = MINUS_EXPR;
23742       break;
23743     case CPP_AND:
23744       code = BIT_AND_EXPR;
23745       break;
23746     case CPP_XOR:
23747       code = BIT_XOR_EXPR;
23748       break;
23749     case CPP_OR:
23750       code = BIT_IOR_EXPR;
23751       break;
23752     case CPP_AND_AND:
23753       code = TRUTH_ANDIF_EXPR;
23754       break;
23755     case CPP_OR_OR:
23756       code = TRUTH_ORIF_EXPR;
23757       break;
23758     default:
23759       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
23760                                "%<|%>, %<&&%>, or %<||%>");
23761     resync_fail:
23762       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23763                                              /*or_comma=*/false,
23764                                              /*consume_paren=*/true);
23765       return list;
23766     }
23767   cp_lexer_consume_token (parser->lexer);
23768
23769   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23770     goto resync_fail;
23771
23772   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
23773   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
23774     OMP_CLAUSE_REDUCTION_CODE (c) = code;
23775
23776   return nlist;
23777 }
23778
23779 /* OpenMP 2.5:
23780    schedule ( schedule-kind )
23781    schedule ( schedule-kind , expression )
23782
23783    schedule-kind:
23784      static | dynamic | guided | runtime | auto  */
23785
23786 static tree
23787 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
23788 {
23789   tree c, t;
23790
23791   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23792     return list;
23793
23794   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
23795
23796   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
23797     {
23798       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
23799       const char *p = IDENTIFIER_POINTER (id);
23800
23801       switch (p[0])
23802         {
23803         case 'd':
23804           if (strcmp ("dynamic", p) != 0)
23805             goto invalid_kind;
23806           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
23807           break;
23808
23809         case 'g':
23810           if (strcmp ("guided", p) != 0)
23811             goto invalid_kind;
23812           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
23813           break;
23814
23815         case 'r':
23816           if (strcmp ("runtime", p) != 0)
23817             goto invalid_kind;
23818           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
23819           break;
23820
23821         default:
23822           goto invalid_kind;
23823         }
23824     }
23825   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
23826     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
23827   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
23828     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
23829   else
23830     goto invalid_kind;
23831   cp_lexer_consume_token (parser->lexer);
23832
23833   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23834     {
23835       cp_token *token;
23836       cp_lexer_consume_token (parser->lexer);
23837
23838       token = cp_lexer_peek_token (parser->lexer);
23839       t = cp_parser_assignment_expression (parser, false, NULL);
23840
23841       if (t == error_mark_node)
23842         goto resync_fail;
23843       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
23844         error_at (token->location, "schedule %<runtime%> does not take "
23845                   "a %<chunk_size%> parameter");
23846       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
23847         error_at (token->location, "schedule %<auto%> does not take "
23848                   "a %<chunk_size%> parameter");
23849       else
23850         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
23851
23852       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
23853         goto resync_fail;
23854     }
23855   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
23856     goto resync_fail;
23857
23858   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
23859   OMP_CLAUSE_CHAIN (c) = list;
23860   return c;
23861
23862  invalid_kind:
23863   cp_parser_error (parser, "invalid schedule kind");
23864  resync_fail:
23865   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
23866                                          /*or_comma=*/false,
23867                                          /*consume_paren=*/true);
23868   return list;
23869 }
23870
23871 /* OpenMP 3.0:
23872    untied */
23873
23874 static tree
23875 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
23876                              tree list, location_t location)
23877 {
23878   tree c;
23879
23880   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
23881
23882   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
23883   OMP_CLAUSE_CHAIN (c) = list;
23884   return c;
23885 }
23886
23887 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
23888    is a bitmask in MASK.  Return the list of clauses found; the result
23889    of clause default goes in *pdefault.  */
23890
23891 static tree
23892 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
23893                            const char *where, cp_token *pragma_tok)
23894 {
23895   tree clauses = NULL;
23896   bool first = true;
23897   cp_token *token = NULL;
23898
23899   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
23900     {
23901       pragma_omp_clause c_kind;
23902       const char *c_name;
23903       tree prev = clauses;
23904
23905       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23906         cp_lexer_consume_token (parser->lexer);
23907
23908       token = cp_lexer_peek_token (parser->lexer);
23909       c_kind = cp_parser_omp_clause_name (parser);
23910       first = false;
23911
23912       switch (c_kind)
23913         {
23914         case PRAGMA_OMP_CLAUSE_COLLAPSE:
23915           clauses = cp_parser_omp_clause_collapse (parser, clauses,
23916                                                    token->location);
23917           c_name = "collapse";
23918           break;
23919         case PRAGMA_OMP_CLAUSE_COPYIN:
23920           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
23921           c_name = "copyin";
23922           break;
23923         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
23924           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
23925                                             clauses);
23926           c_name = "copyprivate";
23927           break;
23928         case PRAGMA_OMP_CLAUSE_DEFAULT:
23929           clauses = cp_parser_omp_clause_default (parser, clauses,
23930                                                   token->location);
23931           c_name = "default";
23932           break;
23933         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
23934           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
23935                                             clauses);
23936           c_name = "firstprivate";
23937           break;
23938         case PRAGMA_OMP_CLAUSE_IF:
23939           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
23940           c_name = "if";
23941           break;
23942         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
23943           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
23944                                             clauses);
23945           c_name = "lastprivate";
23946           break;
23947         case PRAGMA_OMP_CLAUSE_NOWAIT:
23948           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
23949           c_name = "nowait";
23950           break;
23951         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
23952           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
23953                                                       token->location);
23954           c_name = "num_threads";
23955           break;
23956         case PRAGMA_OMP_CLAUSE_ORDERED:
23957           clauses = cp_parser_omp_clause_ordered (parser, clauses,
23958                                                   token->location);
23959           c_name = "ordered";
23960           break;
23961         case PRAGMA_OMP_CLAUSE_PRIVATE:
23962           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
23963                                             clauses);
23964           c_name = "private";
23965           break;
23966         case PRAGMA_OMP_CLAUSE_REDUCTION:
23967           clauses = cp_parser_omp_clause_reduction (parser, clauses);
23968           c_name = "reduction";
23969           break;
23970         case PRAGMA_OMP_CLAUSE_SCHEDULE:
23971           clauses = cp_parser_omp_clause_schedule (parser, clauses,
23972                                                    token->location);
23973           c_name = "schedule";
23974           break;
23975         case PRAGMA_OMP_CLAUSE_SHARED:
23976           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
23977                                             clauses);
23978           c_name = "shared";
23979           break;
23980         case PRAGMA_OMP_CLAUSE_UNTIED:
23981           clauses = cp_parser_omp_clause_untied (parser, clauses,
23982                                                  token->location);
23983           c_name = "nowait";
23984           break;
23985         default:
23986           cp_parser_error (parser, "expected %<#pragma omp%> clause");
23987           goto saw_error;
23988         }
23989
23990       if (((mask >> c_kind) & 1) == 0)
23991         {
23992           /* Remove the invalid clause(s) from the list to avoid
23993              confusing the rest of the compiler.  */
23994           clauses = prev;
23995           error_at (token->location, "%qs is not valid for %qs", c_name, where);
23996         }
23997     }
23998  saw_error:
23999   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
24000   return finish_omp_clauses (clauses);
24001 }
24002
24003 /* OpenMP 2.5:
24004    structured-block:
24005      statement
24006
24007    In practice, we're also interested in adding the statement to an
24008    outer node.  So it is convenient if we work around the fact that
24009    cp_parser_statement calls add_stmt.  */
24010
24011 static unsigned
24012 cp_parser_begin_omp_structured_block (cp_parser *parser)
24013 {
24014   unsigned save = parser->in_statement;
24015
24016   /* Only move the values to IN_OMP_BLOCK if they weren't false.
24017      This preserves the "not within loop or switch" style error messages
24018      for nonsense cases like
24019         void foo() {
24020         #pragma omp single
24021           break;
24022         }
24023   */
24024   if (parser->in_statement)
24025     parser->in_statement = IN_OMP_BLOCK;
24026
24027   return save;
24028 }
24029
24030 static void
24031 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
24032 {
24033   parser->in_statement = save;
24034 }
24035
24036 static tree
24037 cp_parser_omp_structured_block (cp_parser *parser)
24038 {
24039   tree stmt = begin_omp_structured_block ();
24040   unsigned int save = cp_parser_begin_omp_structured_block (parser);
24041
24042   cp_parser_statement (parser, NULL_TREE, false, NULL);
24043
24044   cp_parser_end_omp_structured_block (parser, save);
24045   return finish_omp_structured_block (stmt);
24046 }
24047
24048 /* OpenMP 2.5:
24049    # pragma omp atomic new-line
24050      expression-stmt
24051
24052    expression-stmt:
24053      x binop= expr | x++ | ++x | x-- | --x
24054    binop:
24055      +, *, -, /, &, ^, |, <<, >>
24056
24057   where x is an lvalue expression with scalar type.  */
24058
24059 static void
24060 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
24061 {
24062   tree lhs, rhs;
24063   enum tree_code code;
24064
24065   cp_parser_require_pragma_eol (parser, pragma_tok);
24066
24067   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
24068                                     /*cast_p=*/false, NULL);
24069   switch (TREE_CODE (lhs))
24070     {
24071     case ERROR_MARK:
24072       goto saw_error;
24073
24074     case PREINCREMENT_EXPR:
24075     case POSTINCREMENT_EXPR:
24076       lhs = TREE_OPERAND (lhs, 0);
24077       code = PLUS_EXPR;
24078       rhs = integer_one_node;
24079       break;
24080
24081     case PREDECREMENT_EXPR:
24082     case POSTDECREMENT_EXPR:
24083       lhs = TREE_OPERAND (lhs, 0);
24084       code = MINUS_EXPR;
24085       rhs = integer_one_node;
24086       break;
24087
24088     case COMPOUND_EXPR:
24089       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
24090          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
24091          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
24092          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
24093          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
24094                                              (TREE_OPERAND (lhs, 1), 0), 0)))
24095             == BOOLEAN_TYPE)
24096        /* Undo effects of boolean_increment for post {in,de}crement.  */
24097        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
24098       /* FALLTHRU */
24099     case MODIFY_EXPR:
24100       if (TREE_CODE (lhs) == MODIFY_EXPR
24101          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
24102        {
24103          /* Undo effects of boolean_increment.  */
24104          if (integer_onep (TREE_OPERAND (lhs, 1)))
24105            {
24106              /* This is pre or post increment.  */
24107              rhs = TREE_OPERAND (lhs, 1);
24108              lhs = TREE_OPERAND (lhs, 0);
24109              code = NOP_EXPR;
24110              break;
24111            }
24112        }
24113       /* FALLTHRU */
24114     default:
24115       switch (cp_lexer_peek_token (parser->lexer)->type)
24116         {
24117         case CPP_MULT_EQ:
24118           code = MULT_EXPR;
24119           break;
24120         case CPP_DIV_EQ:
24121           code = TRUNC_DIV_EXPR;
24122           break;
24123         case CPP_PLUS_EQ:
24124           code = PLUS_EXPR;
24125           break;
24126         case CPP_MINUS_EQ:
24127           code = MINUS_EXPR;
24128           break;
24129         case CPP_LSHIFT_EQ:
24130           code = LSHIFT_EXPR;
24131           break;
24132         case CPP_RSHIFT_EQ:
24133           code = RSHIFT_EXPR;
24134           break;
24135         case CPP_AND_EQ:
24136           code = BIT_AND_EXPR;
24137           break;
24138         case CPP_OR_EQ:
24139           code = BIT_IOR_EXPR;
24140           break;
24141         case CPP_XOR_EQ:
24142           code = BIT_XOR_EXPR;
24143           break;
24144         default:
24145           cp_parser_error (parser,
24146                            "invalid operator for %<#pragma omp atomic%>");
24147           goto saw_error;
24148         }
24149       cp_lexer_consume_token (parser->lexer);
24150
24151       rhs = cp_parser_expression (parser, false, NULL);
24152       if (rhs == error_mark_node)
24153         goto saw_error;
24154       break;
24155     }
24156   finish_omp_atomic (code, lhs, rhs);
24157   cp_parser_consume_semicolon_at_end_of_statement (parser);
24158   return;
24159
24160  saw_error:
24161   cp_parser_skip_to_end_of_block_or_statement (parser);
24162 }
24163
24164
24165 /* OpenMP 2.5:
24166    # pragma omp barrier new-line  */
24167
24168 static void
24169 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
24170 {
24171   cp_parser_require_pragma_eol (parser, pragma_tok);
24172   finish_omp_barrier ();
24173 }
24174
24175 /* OpenMP 2.5:
24176    # pragma omp critical [(name)] new-line
24177      structured-block  */
24178
24179 static tree
24180 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
24181 {
24182   tree stmt, name = NULL;
24183
24184   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24185     {
24186       cp_lexer_consume_token (parser->lexer);
24187
24188       name = cp_parser_identifier (parser);
24189
24190       if (name == error_mark_node
24191           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24192         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24193                                                /*or_comma=*/false,
24194                                                /*consume_paren=*/true);
24195       if (name == error_mark_node)
24196         name = NULL;
24197     }
24198   cp_parser_require_pragma_eol (parser, pragma_tok);
24199
24200   stmt = cp_parser_omp_structured_block (parser);
24201   return c_finish_omp_critical (input_location, stmt, name);
24202 }
24203
24204 /* OpenMP 2.5:
24205    # pragma omp flush flush-vars[opt] new-line
24206
24207    flush-vars:
24208      ( variable-list ) */
24209
24210 static void
24211 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
24212 {
24213   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24214     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
24215   cp_parser_require_pragma_eol (parser, pragma_tok);
24216
24217   finish_omp_flush ();
24218 }
24219
24220 /* Helper function, to parse omp for increment expression.  */
24221
24222 static tree
24223 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
24224 {
24225   tree cond = cp_parser_binary_expression (parser, false, true,
24226                                            PREC_NOT_OPERATOR, NULL);
24227   bool overloaded_p;
24228
24229   if (cond == error_mark_node
24230       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24231     {
24232       cp_parser_skip_to_end_of_statement (parser);
24233       return error_mark_node;
24234     }
24235
24236   switch (TREE_CODE (cond))
24237     {
24238     case GT_EXPR:
24239     case GE_EXPR:
24240     case LT_EXPR:
24241     case LE_EXPR:
24242       break;
24243     default:
24244       return error_mark_node;
24245     }
24246
24247   /* If decl is an iterator, preserve LHS and RHS of the relational
24248      expr until finish_omp_for.  */
24249   if (decl
24250       && (type_dependent_expression_p (decl)
24251           || CLASS_TYPE_P (TREE_TYPE (decl))))
24252     return cond;
24253
24254   return build_x_binary_op (TREE_CODE (cond),
24255                             TREE_OPERAND (cond, 0), ERROR_MARK,
24256                             TREE_OPERAND (cond, 1), ERROR_MARK,
24257                             &overloaded_p, tf_warning_or_error);
24258 }
24259
24260 /* Helper function, to parse omp for increment expression.  */
24261
24262 static tree
24263 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
24264 {
24265   cp_token *token = cp_lexer_peek_token (parser->lexer);
24266   enum tree_code op;
24267   tree lhs, rhs;
24268   cp_id_kind idk;
24269   bool decl_first;
24270
24271   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24272     {
24273       op = (token->type == CPP_PLUS_PLUS
24274             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
24275       cp_lexer_consume_token (parser->lexer);
24276       lhs = cp_parser_cast_expression (parser, false, false, NULL);
24277       if (lhs != decl)
24278         return error_mark_node;
24279       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24280     }
24281
24282   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
24283   if (lhs != decl)
24284     return error_mark_node;
24285
24286   token = cp_lexer_peek_token (parser->lexer);
24287   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
24288     {
24289       op = (token->type == CPP_PLUS_PLUS
24290             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
24291       cp_lexer_consume_token (parser->lexer);
24292       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
24293     }
24294
24295   op = cp_parser_assignment_operator_opt (parser);
24296   if (op == ERROR_MARK)
24297     return error_mark_node;
24298
24299   if (op != NOP_EXPR)
24300     {
24301       rhs = cp_parser_assignment_expression (parser, false, NULL);
24302       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
24303       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24304     }
24305
24306   lhs = cp_parser_binary_expression (parser, false, false,
24307                                      PREC_ADDITIVE_EXPRESSION, NULL);
24308   token = cp_lexer_peek_token (parser->lexer);
24309   decl_first = lhs == decl;
24310   if (decl_first)
24311     lhs = NULL_TREE;
24312   if (token->type != CPP_PLUS
24313       && token->type != CPP_MINUS)
24314     return error_mark_node;
24315
24316   do
24317     {
24318       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
24319       cp_lexer_consume_token (parser->lexer);
24320       rhs = cp_parser_binary_expression (parser, false, false,
24321                                          PREC_ADDITIVE_EXPRESSION, NULL);
24322       token = cp_lexer_peek_token (parser->lexer);
24323       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
24324         {
24325           if (lhs == NULL_TREE)
24326             {
24327               if (op == PLUS_EXPR)
24328                 lhs = rhs;
24329               else
24330                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
24331             }
24332           else
24333             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
24334                                      NULL, tf_warning_or_error);
24335         }
24336     }
24337   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
24338
24339   if (!decl_first)
24340     {
24341       if (rhs != decl || op == MINUS_EXPR)
24342         return error_mark_node;
24343       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
24344     }
24345   else
24346     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
24347
24348   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
24349 }
24350
24351 /* Parse the restricted form of the for statement allowed by OpenMP.  */
24352
24353 static tree
24354 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
24355 {
24356   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
24357   tree real_decl, initv, condv, incrv, declv;
24358   tree this_pre_body, cl;
24359   location_t loc_first;
24360   bool collapse_err = false;
24361   int i, collapse = 1, nbraces = 0;
24362   VEC(tree,gc) *for_block = make_tree_vector ();
24363
24364   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
24365     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
24366       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
24367
24368   gcc_assert (collapse >= 1);
24369
24370   declv = make_tree_vec (collapse);
24371   initv = make_tree_vec (collapse);
24372   condv = make_tree_vec (collapse);
24373   incrv = make_tree_vec (collapse);
24374
24375   loc_first = cp_lexer_peek_token (parser->lexer)->location;
24376
24377   for (i = 0; i < collapse; i++)
24378     {
24379       int bracecount = 0;
24380       bool add_private_clause = false;
24381       location_t loc;
24382
24383       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24384         {
24385           cp_parser_error (parser, "for statement expected");
24386           return NULL;
24387         }
24388       loc = cp_lexer_consume_token (parser->lexer)->location;
24389
24390       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24391         return NULL;
24392
24393       init = decl = real_decl = NULL;
24394       this_pre_body = push_stmt_list ();
24395       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24396         {
24397           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
24398
24399              init-expr:
24400                        var = lb
24401                        integer-type var = lb
24402                        random-access-iterator-type var = lb
24403                        pointer-type var = lb
24404           */
24405           cp_decl_specifier_seq type_specifiers;
24406
24407           /* First, try to parse as an initialized declaration.  See
24408              cp_parser_condition, from whence the bulk of this is copied.  */
24409
24410           cp_parser_parse_tentatively (parser);
24411           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
24412                                         /*is_trailing_return=*/false,
24413                                         &type_specifiers);
24414           if (cp_parser_parse_definitely (parser))
24415             {
24416               /* If parsing a type specifier seq succeeded, then this
24417                  MUST be a initialized declaration.  */
24418               tree asm_specification, attributes;
24419               cp_declarator *declarator;
24420
24421               declarator = cp_parser_declarator (parser,
24422                                                  CP_PARSER_DECLARATOR_NAMED,
24423                                                  /*ctor_dtor_or_conv_p=*/NULL,
24424                                                  /*parenthesized_p=*/NULL,
24425                                                  /*member_p=*/false);
24426               attributes = cp_parser_attributes_opt (parser);
24427               asm_specification = cp_parser_asm_specification_opt (parser);
24428
24429               if (declarator == cp_error_declarator) 
24430                 cp_parser_skip_to_end_of_statement (parser);
24431
24432               else 
24433                 {
24434                   tree pushed_scope, auto_node;
24435
24436                   decl = start_decl (declarator, &type_specifiers,
24437                                      SD_INITIALIZED, attributes,
24438                                      /*prefix_attributes=*/NULL_TREE,
24439                                      &pushed_scope);
24440
24441                   auto_node = type_uses_auto (TREE_TYPE (decl));
24442                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24443                     {
24444                       if (cp_lexer_next_token_is (parser->lexer, 
24445                                                   CPP_OPEN_PAREN))
24446                         error ("parenthesized initialization is not allowed in "
24447                                "OpenMP %<for%> loop");
24448                       else
24449                         /* Trigger an error.  */
24450                         cp_parser_require (parser, CPP_EQ, RT_EQ);
24451
24452                       init = error_mark_node;
24453                       cp_parser_skip_to_end_of_statement (parser);
24454                     }
24455                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
24456                            || type_dependent_expression_p (decl)
24457                            || auto_node)
24458                     {
24459                       bool is_direct_init, is_non_constant_init;
24460
24461                       init = cp_parser_initializer (parser,
24462                                                     &is_direct_init,
24463                                                     &is_non_constant_init);
24464
24465                       if (auto_node && describable_type (init))
24466                         {
24467                           TREE_TYPE (decl)
24468                             = do_auto_deduction (TREE_TYPE (decl), init,
24469                                                  auto_node);
24470
24471                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
24472                               && !type_dependent_expression_p (decl))
24473                             goto non_class;
24474                         }
24475                       
24476                       cp_finish_decl (decl, init, !is_non_constant_init,
24477                                       asm_specification,
24478                                       LOOKUP_ONLYCONVERTING);
24479                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
24480                         {
24481                           VEC_safe_push (tree, gc, for_block, this_pre_body);
24482                           init = NULL_TREE;
24483                         }
24484                       else
24485                         init = pop_stmt_list (this_pre_body);
24486                       this_pre_body = NULL_TREE;
24487                     }
24488                   else
24489                     {
24490                       /* Consume '='.  */
24491                       cp_lexer_consume_token (parser->lexer);
24492                       init = cp_parser_assignment_expression (parser, false, NULL);
24493
24494                     non_class:
24495                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
24496                         init = error_mark_node;
24497                       else
24498                         cp_finish_decl (decl, NULL_TREE,
24499                                         /*init_const_expr_p=*/false,
24500                                         asm_specification,
24501                                         LOOKUP_ONLYCONVERTING);
24502                     }
24503
24504                   if (pushed_scope)
24505                     pop_scope (pushed_scope);
24506                 }
24507             }
24508           else 
24509             {
24510               cp_id_kind idk;
24511               /* If parsing a type specifier sequence failed, then
24512                  this MUST be a simple expression.  */
24513               cp_parser_parse_tentatively (parser);
24514               decl = cp_parser_primary_expression (parser, false, false,
24515                                                    false, &idk);
24516               if (!cp_parser_error_occurred (parser)
24517                   && decl
24518                   && DECL_P (decl)
24519                   && CLASS_TYPE_P (TREE_TYPE (decl)))
24520                 {
24521                   tree rhs;
24522
24523                   cp_parser_parse_definitely (parser);
24524                   cp_parser_require (parser, CPP_EQ, RT_EQ);
24525                   rhs = cp_parser_assignment_expression (parser, false, NULL);
24526                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
24527                                                          rhs,
24528                                                          tf_warning_or_error));
24529                   add_private_clause = true;
24530                 }
24531               else
24532                 {
24533                   decl = NULL;
24534                   cp_parser_abort_tentative_parse (parser);
24535                   init = cp_parser_expression (parser, false, NULL);
24536                   if (init)
24537                     {
24538                       if (TREE_CODE (init) == MODIFY_EXPR
24539                           || TREE_CODE (init) == MODOP_EXPR)
24540                         real_decl = TREE_OPERAND (init, 0);
24541                     }
24542                 }
24543             }
24544         }
24545       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24546       if (this_pre_body)
24547         {
24548           this_pre_body = pop_stmt_list (this_pre_body);
24549           if (pre_body)
24550             {
24551               tree t = pre_body;
24552               pre_body = push_stmt_list ();
24553               add_stmt (t);
24554               add_stmt (this_pre_body);
24555               pre_body = pop_stmt_list (pre_body);
24556             }
24557           else
24558             pre_body = this_pre_body;
24559         }
24560
24561       if (decl)
24562         real_decl = decl;
24563       if (par_clauses != NULL && real_decl != NULL_TREE)
24564         {
24565           tree *c;
24566           for (c = par_clauses; *c ; )
24567             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
24568                 && OMP_CLAUSE_DECL (*c) == real_decl)
24569               {
24570                 error_at (loc, "iteration variable %qD"
24571                           " should not be firstprivate", real_decl);
24572                 *c = OMP_CLAUSE_CHAIN (*c);
24573               }
24574             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
24575                      && OMP_CLAUSE_DECL (*c) == real_decl)
24576               {
24577                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
24578                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
24579                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
24580                 OMP_CLAUSE_DECL (l) = real_decl;
24581                 OMP_CLAUSE_CHAIN (l) = clauses;
24582                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
24583                 clauses = l;
24584                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
24585                 CP_OMP_CLAUSE_INFO (*c) = NULL;
24586                 add_private_clause = false;
24587               }
24588             else
24589               {
24590                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
24591                     && OMP_CLAUSE_DECL (*c) == real_decl)
24592                   add_private_clause = false;
24593                 c = &OMP_CLAUSE_CHAIN (*c);
24594               }
24595         }
24596
24597       if (add_private_clause)
24598         {
24599           tree c;
24600           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24601             {
24602               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
24603                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
24604                   && OMP_CLAUSE_DECL (c) == decl)
24605                 break;
24606               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
24607                        && OMP_CLAUSE_DECL (c) == decl)
24608                 error_at (loc, "iteration variable %qD "
24609                           "should not be firstprivate",
24610                           decl);
24611               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
24612                        && OMP_CLAUSE_DECL (c) == decl)
24613                 error_at (loc, "iteration variable %qD should not be reduction",
24614                           decl);
24615             }
24616           if (c == NULL)
24617             {
24618               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
24619               OMP_CLAUSE_DECL (c) = decl;
24620               c = finish_omp_clauses (c);
24621               if (c)
24622                 {
24623                   OMP_CLAUSE_CHAIN (c) = clauses;
24624                   clauses = c;
24625                 }
24626             }
24627         }
24628
24629       cond = NULL;
24630       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24631         cond = cp_parser_omp_for_cond (parser, decl);
24632       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
24633
24634       incr = NULL;
24635       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
24636         {
24637           /* If decl is an iterator, preserve the operator on decl
24638              until finish_omp_for.  */
24639           if (decl
24640               && ((type_dependent_expression_p (decl)
24641                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
24642                   || CLASS_TYPE_P (TREE_TYPE (decl))))
24643             incr = cp_parser_omp_for_incr (parser, decl);
24644           else
24645             incr = cp_parser_expression (parser, false, NULL);
24646         }
24647
24648       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24649         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24650                                                /*or_comma=*/false,
24651                                                /*consume_paren=*/true);
24652
24653       TREE_VEC_ELT (declv, i) = decl;
24654       TREE_VEC_ELT (initv, i) = init;
24655       TREE_VEC_ELT (condv, i) = cond;
24656       TREE_VEC_ELT (incrv, i) = incr;
24657
24658       if (i == collapse - 1)
24659         break;
24660
24661       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
24662          in between the collapsed for loops to be still considered perfectly
24663          nested.  Hopefully the final version clarifies this.
24664          For now handle (multiple) {'s and empty statements.  */
24665       cp_parser_parse_tentatively (parser);
24666       do
24667         {
24668           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24669             break;
24670           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
24671             {
24672               cp_lexer_consume_token (parser->lexer);
24673               bracecount++;
24674             }
24675           else if (bracecount
24676                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24677             cp_lexer_consume_token (parser->lexer);
24678           else
24679             {
24680               loc = cp_lexer_peek_token (parser->lexer)->location;
24681               error_at (loc, "not enough collapsed for loops");
24682               collapse_err = true;
24683               cp_parser_abort_tentative_parse (parser);
24684               declv = NULL_TREE;
24685               break;
24686             }
24687         }
24688       while (1);
24689
24690       if (declv)
24691         {
24692           cp_parser_parse_definitely (parser);
24693           nbraces += bracecount;
24694         }
24695     }
24696
24697   /* Note that we saved the original contents of this flag when we entered
24698      the structured block, and so we don't need to re-save it here.  */
24699   parser->in_statement = IN_OMP_FOR;
24700
24701   /* Note that the grammar doesn't call for a structured block here,
24702      though the loop as a whole is a structured block.  */
24703   body = push_stmt_list ();
24704   cp_parser_statement (parser, NULL_TREE, false, NULL);
24705   body = pop_stmt_list (body);
24706
24707   if (declv == NULL_TREE)
24708     ret = NULL_TREE;
24709   else
24710     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
24711                           pre_body, clauses);
24712
24713   while (nbraces)
24714     {
24715       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
24716         {
24717           cp_lexer_consume_token (parser->lexer);
24718           nbraces--;
24719         }
24720       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24721         cp_lexer_consume_token (parser->lexer);
24722       else
24723         {
24724           if (!collapse_err)
24725             {
24726               error_at (cp_lexer_peek_token (parser->lexer)->location,
24727                         "collapsed loops not perfectly nested");
24728             }
24729           collapse_err = true;
24730           cp_parser_statement_seq_opt (parser, NULL);
24731           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
24732             break;
24733         }
24734     }
24735
24736   while (!VEC_empty (tree, for_block))
24737     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
24738   release_tree_vector (for_block);
24739
24740   return ret;
24741 }
24742
24743 /* OpenMP 2.5:
24744    #pragma omp for for-clause[optseq] new-line
24745      for-loop  */
24746
24747 #define OMP_FOR_CLAUSE_MASK                             \
24748         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24749         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24750         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24751         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24752         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
24753         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
24754         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
24755         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
24756
24757 static tree
24758 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
24759 {
24760   tree clauses, sb, ret;
24761   unsigned int save;
24762
24763   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
24764                                        "#pragma omp for", pragma_tok);
24765
24766   sb = begin_omp_structured_block ();
24767   save = cp_parser_begin_omp_structured_block (parser);
24768
24769   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
24770
24771   cp_parser_end_omp_structured_block (parser, save);
24772   add_stmt (finish_omp_structured_block (sb));
24773
24774   return ret;
24775 }
24776
24777 /* OpenMP 2.5:
24778    # pragma omp master new-line
24779      structured-block  */
24780
24781 static tree
24782 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
24783 {
24784   cp_parser_require_pragma_eol (parser, pragma_tok);
24785   return c_finish_omp_master (input_location,
24786                               cp_parser_omp_structured_block (parser));
24787 }
24788
24789 /* OpenMP 2.5:
24790    # pragma omp ordered new-line
24791      structured-block  */
24792
24793 static tree
24794 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
24795 {
24796   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24797   cp_parser_require_pragma_eol (parser, pragma_tok);
24798   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
24799 }
24800
24801 /* OpenMP 2.5:
24802
24803    section-scope:
24804      { section-sequence }
24805
24806    section-sequence:
24807      section-directive[opt] structured-block
24808      section-sequence section-directive structured-block  */
24809
24810 static tree
24811 cp_parser_omp_sections_scope (cp_parser *parser)
24812 {
24813   tree stmt, substmt;
24814   bool error_suppress = false;
24815   cp_token *tok;
24816
24817   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
24818     return NULL_TREE;
24819
24820   stmt = push_stmt_list ();
24821
24822   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
24823     {
24824       unsigned save;
24825
24826       substmt = begin_omp_structured_block ();
24827       save = cp_parser_begin_omp_structured_block (parser);
24828
24829       while (1)
24830         {
24831           cp_parser_statement (parser, NULL_TREE, false, NULL);
24832
24833           tok = cp_lexer_peek_token (parser->lexer);
24834           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24835             break;
24836           if (tok->type == CPP_CLOSE_BRACE)
24837             break;
24838           if (tok->type == CPP_EOF)
24839             break;
24840         }
24841
24842       cp_parser_end_omp_structured_block (parser, save);
24843       substmt = finish_omp_structured_block (substmt);
24844       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24845       add_stmt (substmt);
24846     }
24847
24848   while (1)
24849     {
24850       tok = cp_lexer_peek_token (parser->lexer);
24851       if (tok->type == CPP_CLOSE_BRACE)
24852         break;
24853       if (tok->type == CPP_EOF)
24854         break;
24855
24856       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
24857         {
24858           cp_lexer_consume_token (parser->lexer);
24859           cp_parser_require_pragma_eol (parser, tok);
24860           error_suppress = false;
24861         }
24862       else if (!error_suppress)
24863         {
24864           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
24865           error_suppress = true;
24866         }
24867
24868       substmt = cp_parser_omp_structured_block (parser);
24869       substmt = build1 (OMP_SECTION, void_type_node, substmt);
24870       add_stmt (substmt);
24871     }
24872   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
24873
24874   substmt = pop_stmt_list (stmt);
24875
24876   stmt = make_node (OMP_SECTIONS);
24877   TREE_TYPE (stmt) = void_type_node;
24878   OMP_SECTIONS_BODY (stmt) = substmt;
24879
24880   add_stmt (stmt);
24881   return stmt;
24882 }
24883
24884 /* OpenMP 2.5:
24885    # pragma omp sections sections-clause[optseq] newline
24886      sections-scope  */
24887
24888 #define OMP_SECTIONS_CLAUSE_MASK                        \
24889         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24890         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24891         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
24892         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24893         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
24894
24895 static tree
24896 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
24897 {
24898   tree clauses, ret;
24899
24900   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
24901                                        "#pragma omp sections", pragma_tok);
24902
24903   ret = cp_parser_omp_sections_scope (parser);
24904   if (ret)
24905     OMP_SECTIONS_CLAUSES (ret) = clauses;
24906
24907   return ret;
24908 }
24909
24910 /* OpenMP 2.5:
24911    # pragma parallel parallel-clause new-line
24912    # pragma parallel for parallel-for-clause new-line
24913    # pragma parallel sections parallel-sections-clause new-line  */
24914
24915 #define OMP_PARALLEL_CLAUSE_MASK                        \
24916         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
24917         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24918         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24919         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
24920         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
24921         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
24922         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
24923         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
24924
24925 static tree
24926 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
24927 {
24928   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
24929   const char *p_name = "#pragma omp parallel";
24930   tree stmt, clauses, par_clause, ws_clause, block;
24931   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
24932   unsigned int save;
24933   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24934
24935   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
24936     {
24937       cp_lexer_consume_token (parser->lexer);
24938       p_kind = PRAGMA_OMP_PARALLEL_FOR;
24939       p_name = "#pragma omp parallel for";
24940       mask |= OMP_FOR_CLAUSE_MASK;
24941       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24942     }
24943   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24944     {
24945       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24946       const char *p = IDENTIFIER_POINTER (id);
24947       if (strcmp (p, "sections") == 0)
24948         {
24949           cp_lexer_consume_token (parser->lexer);
24950           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
24951           p_name = "#pragma omp parallel sections";
24952           mask |= OMP_SECTIONS_CLAUSE_MASK;
24953           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
24954         }
24955     }
24956
24957   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
24958   block = begin_omp_parallel ();
24959   save = cp_parser_begin_omp_structured_block (parser);
24960
24961   switch (p_kind)
24962     {
24963     case PRAGMA_OMP_PARALLEL:
24964       cp_parser_statement (parser, NULL_TREE, false, NULL);
24965       par_clause = clauses;
24966       break;
24967
24968     case PRAGMA_OMP_PARALLEL_FOR:
24969       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24970       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
24971       break;
24972
24973     case PRAGMA_OMP_PARALLEL_SECTIONS:
24974       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
24975       stmt = cp_parser_omp_sections_scope (parser);
24976       if (stmt)
24977         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
24978       break;
24979
24980     default:
24981       gcc_unreachable ();
24982     }
24983
24984   cp_parser_end_omp_structured_block (parser, save);
24985   stmt = finish_omp_parallel (par_clause, block);
24986   if (p_kind != PRAGMA_OMP_PARALLEL)
24987     OMP_PARALLEL_COMBINED (stmt) = 1;
24988   return stmt;
24989 }
24990
24991 /* OpenMP 2.5:
24992    # pragma omp single single-clause[optseq] new-line
24993      structured-block  */
24994
24995 #define OMP_SINGLE_CLAUSE_MASK                          \
24996         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
24997         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
24998         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
24999         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
25000
25001 static tree
25002 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
25003 {
25004   tree stmt = make_node (OMP_SINGLE);
25005   TREE_TYPE (stmt) = void_type_node;
25006
25007   OMP_SINGLE_CLAUSES (stmt)
25008     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
25009                                  "#pragma omp single", pragma_tok);
25010   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
25011
25012   return add_stmt (stmt);
25013 }
25014
25015 /* OpenMP 3.0:
25016    # pragma omp task task-clause[optseq] new-line
25017      structured-block  */
25018
25019 #define OMP_TASK_CLAUSE_MASK                            \
25020         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
25021         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
25022         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
25023         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
25024         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
25025         | (1u << PRAGMA_OMP_CLAUSE_SHARED))
25026
25027 static tree
25028 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
25029 {
25030   tree clauses, block;
25031   unsigned int save;
25032
25033   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
25034                                        "#pragma omp task", pragma_tok);
25035   block = begin_omp_task ();
25036   save = cp_parser_begin_omp_structured_block (parser);
25037   cp_parser_statement (parser, NULL_TREE, false, NULL);
25038   cp_parser_end_omp_structured_block (parser, save);
25039   return finish_omp_task (clauses, block);
25040 }
25041
25042 /* OpenMP 3.0:
25043    # pragma omp taskwait new-line  */
25044
25045 static void
25046 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
25047 {
25048   cp_parser_require_pragma_eol (parser, pragma_tok);
25049   finish_omp_taskwait ();
25050 }
25051
25052 /* OpenMP 2.5:
25053    # pragma omp threadprivate (variable-list) */
25054
25055 static void
25056 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
25057 {
25058   tree vars;
25059
25060   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25061   cp_parser_require_pragma_eol (parser, pragma_tok);
25062
25063   finish_omp_threadprivate (vars);
25064 }
25065
25066 /* Main entry point to OpenMP statement pragmas.  */
25067
25068 static void
25069 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
25070 {
25071   tree stmt;
25072
25073   switch (pragma_tok->pragma_kind)
25074     {
25075     case PRAGMA_OMP_ATOMIC:
25076       cp_parser_omp_atomic (parser, pragma_tok);
25077       return;
25078     case PRAGMA_OMP_CRITICAL:
25079       stmt = cp_parser_omp_critical (parser, pragma_tok);
25080       break;
25081     case PRAGMA_OMP_FOR:
25082       stmt = cp_parser_omp_for (parser, pragma_tok);
25083       break;
25084     case PRAGMA_OMP_MASTER:
25085       stmt = cp_parser_omp_master (parser, pragma_tok);
25086       break;
25087     case PRAGMA_OMP_ORDERED:
25088       stmt = cp_parser_omp_ordered (parser, pragma_tok);
25089       break;
25090     case PRAGMA_OMP_PARALLEL:
25091       stmt = cp_parser_omp_parallel (parser, pragma_tok);
25092       break;
25093     case PRAGMA_OMP_SECTIONS:
25094       stmt = cp_parser_omp_sections (parser, pragma_tok);
25095       break;
25096     case PRAGMA_OMP_SINGLE:
25097       stmt = cp_parser_omp_single (parser, pragma_tok);
25098       break;
25099     case PRAGMA_OMP_TASK:
25100       stmt = cp_parser_omp_task (parser, pragma_tok);
25101       break;
25102     default:
25103       gcc_unreachable ();
25104     }
25105
25106   if (stmt)
25107     SET_EXPR_LOCATION (stmt, pragma_tok->location);
25108 }
25109 \f
25110 /* The parser.  */
25111
25112 static GTY (()) cp_parser *the_parser;
25113
25114 \f
25115 /* Special handling for the first token or line in the file.  The first
25116    thing in the file might be #pragma GCC pch_preprocess, which loads a
25117    PCH file, which is a GC collection point.  So we need to handle this
25118    first pragma without benefit of an existing lexer structure.
25119
25120    Always returns one token to the caller in *FIRST_TOKEN.  This is
25121    either the true first token of the file, or the first token after
25122    the initial pragma.  */
25123
25124 static void
25125 cp_parser_initial_pragma (cp_token *first_token)
25126 {
25127   tree name = NULL;
25128
25129   cp_lexer_get_preprocessor_token (NULL, first_token);
25130   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
25131     return;
25132
25133   cp_lexer_get_preprocessor_token (NULL, first_token);
25134   if (first_token->type == CPP_STRING)
25135     {
25136       name = first_token->u.value;
25137
25138       cp_lexer_get_preprocessor_token (NULL, first_token);
25139       if (first_token->type != CPP_PRAGMA_EOL)
25140         error_at (first_token->location,
25141                   "junk at end of %<#pragma GCC pch_preprocess%>");
25142     }
25143   else
25144     error_at (first_token->location, "expected string literal");
25145
25146   /* Skip to the end of the pragma.  */
25147   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
25148     cp_lexer_get_preprocessor_token (NULL, first_token);
25149
25150   /* Now actually load the PCH file.  */
25151   if (name)
25152     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
25153
25154   /* Read one more token to return to our caller.  We have to do this
25155      after reading the PCH file in, since its pointers have to be
25156      live.  */
25157   cp_lexer_get_preprocessor_token (NULL, first_token);
25158 }
25159
25160 /* Normal parsing of a pragma token.  Here we can (and must) use the
25161    regular lexer.  */
25162
25163 static bool
25164 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
25165 {
25166   cp_token *pragma_tok;
25167   unsigned int id;
25168
25169   pragma_tok = cp_lexer_consume_token (parser->lexer);
25170   gcc_assert (pragma_tok->type == CPP_PRAGMA);
25171   parser->lexer->in_pragma = true;
25172
25173   id = pragma_tok->pragma_kind;
25174   switch (id)
25175     {
25176     case PRAGMA_GCC_PCH_PREPROCESS:
25177       error_at (pragma_tok->location,
25178                 "%<#pragma GCC pch_preprocess%> must be first");
25179       break;
25180
25181     case PRAGMA_OMP_BARRIER:
25182       switch (context)
25183         {
25184         case pragma_compound:
25185           cp_parser_omp_barrier (parser, pragma_tok);
25186           return false;
25187         case pragma_stmt:
25188           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
25189                     "used in compound statements");
25190           break;
25191         default:
25192           goto bad_stmt;
25193         }
25194       break;
25195
25196     case PRAGMA_OMP_FLUSH:
25197       switch (context)
25198         {
25199         case pragma_compound:
25200           cp_parser_omp_flush (parser, pragma_tok);
25201           return false;
25202         case pragma_stmt:
25203           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
25204                     "used in compound statements");
25205           break;
25206         default:
25207           goto bad_stmt;
25208         }
25209       break;
25210
25211     case PRAGMA_OMP_TASKWAIT:
25212       switch (context)
25213         {
25214         case pragma_compound:
25215           cp_parser_omp_taskwait (parser, pragma_tok);
25216           return false;
25217         case pragma_stmt:
25218           error_at (pragma_tok->location,
25219                     "%<#pragma omp taskwait%> may only be "
25220                     "used in compound statements");
25221           break;
25222         default:
25223           goto bad_stmt;
25224         }
25225       break;
25226
25227     case PRAGMA_OMP_THREADPRIVATE:
25228       cp_parser_omp_threadprivate (parser, pragma_tok);
25229       return false;
25230
25231     case PRAGMA_OMP_ATOMIC:
25232     case PRAGMA_OMP_CRITICAL:
25233     case PRAGMA_OMP_FOR:
25234     case PRAGMA_OMP_MASTER:
25235     case PRAGMA_OMP_ORDERED:
25236     case PRAGMA_OMP_PARALLEL:
25237     case PRAGMA_OMP_SECTIONS:
25238     case PRAGMA_OMP_SINGLE:
25239     case PRAGMA_OMP_TASK:
25240       if (context == pragma_external)
25241         goto bad_stmt;
25242       cp_parser_omp_construct (parser, pragma_tok);
25243       return true;
25244
25245     case PRAGMA_OMP_SECTION:
25246       error_at (pragma_tok->location, 
25247                 "%<#pragma omp section%> may only be used in "
25248                 "%<#pragma omp sections%> construct");
25249       break;
25250
25251     default:
25252       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
25253       c_invoke_pragma_handler (id);
25254       break;
25255
25256     bad_stmt:
25257       cp_parser_error (parser, "expected declaration specifiers");
25258       break;
25259     }
25260
25261   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25262   return false;
25263 }
25264
25265 /* The interface the pragma parsers have to the lexer.  */
25266
25267 enum cpp_ttype
25268 pragma_lex (tree *value)
25269 {
25270   cp_token *tok;
25271   enum cpp_ttype ret;
25272
25273   tok = cp_lexer_peek_token (the_parser->lexer);
25274
25275   ret = tok->type;
25276   *value = tok->u.value;
25277
25278   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
25279     ret = CPP_EOF;
25280   else if (ret == CPP_STRING)
25281     *value = cp_parser_string_literal (the_parser, false, false);
25282   else
25283     {
25284       cp_lexer_consume_token (the_parser->lexer);
25285       if (ret == CPP_KEYWORD)
25286         ret = CPP_NAME;
25287     }
25288
25289   return ret;
25290 }
25291
25292 \f
25293 /* External interface.  */
25294
25295 /* Parse one entire translation unit.  */
25296
25297 void
25298 c_parse_file (void)
25299 {
25300   static bool already_called = false;
25301
25302   if (already_called)
25303     {
25304       sorry ("inter-module optimizations not implemented for C++");
25305       return;
25306     }
25307   already_called = true;
25308
25309   the_parser = cp_parser_new ();
25310   push_deferring_access_checks (flag_access_control
25311                                 ? dk_no_deferred : dk_no_check);
25312   cp_parser_translation_unit (the_parser);
25313   the_parser = NULL;
25314 }
25315
25316 #include "gt-cp-parser.h"